finished implementing buffersizes for coreaudio
This commit is contained in:
parent
9e660da433
commit
4cd9d0bcec
|
@ -4,9 +4,9 @@ extern crate cpal;
|
||||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||||
|
|
||||||
fn main() -> Result<(), anyhow::Error> {
|
fn main() -> Result<(), anyhow::Error> {
|
||||||
//let host = cpal::default_host();
|
let host = cpal::default_host();
|
||||||
let host =
|
//let host =
|
||||||
cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host");
|
// cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host");
|
||||||
let device = host
|
let device = host
|
||||||
.default_output_device()
|
.default_output_device()
|
||||||
.expect("failed to find a default output device");
|
.expect("failed to find a default output device");
|
||||||
|
@ -26,7 +26,7 @@ where
|
||||||
T: cpal::Sample,
|
T: cpal::Sample,
|
||||||
{
|
{
|
||||||
let mut config: cpal::StreamConfig = config.clone();
|
let mut config: cpal::StreamConfig = config.clone();
|
||||||
config.buffer_size = cpal::BufferSize::Fixed(256);
|
config.buffer_size = cpal::BufferSize::Fixed(16);
|
||||||
|
|
||||||
let sample_rate = config.sample_rate.0 as f32;
|
let sample_rate = config.sample_rate.0 as f32;
|
||||||
let channels = config.channels as usize;
|
let channels = config.channels as usize;
|
||||||
|
@ -43,6 +43,7 @@ where
|
||||||
let stream = device.build_output_stream(
|
let stream = device.build_output_stream(
|
||||||
&config,
|
&config,
|
||||||
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
|
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
|
||||||
|
println!("data len = {}", data.len());
|
||||||
write_data(data, channels, &mut next_value)
|
write_data(data, channels, &mut next_value)
|
||||||
},
|
},
|
||||||
err_fn,
|
err_fn,
|
||||||
|
|
|
@ -278,11 +278,8 @@ impl Device {
|
||||||
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
||||||
let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges);
|
let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges);
|
||||||
|
|
||||||
let mut audio_unit = audio_unit_from_device(self, true)?;
|
let audio_unit = audio_unit_from_device(self, true)?;
|
||||||
let buffer_size = match get_io_buffer_frame_size_range(&audio_unit) {
|
let buffer_size = get_io_buffer_frame_size_range(&audio_unit)?;
|
||||||
Ok(v) => v.unwrap(),
|
|
||||||
Err(_) => return SupportedStreamConfigsError::DeviceNotAvailable,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Collect the supported formats for the device.
|
// Collect the supported formats for the device.
|
||||||
let mut fmts = vec![];
|
let mut fmts = vec![];
|
||||||
|
@ -291,7 +288,7 @@ impl Device {
|
||||||
channels: n_channels as ChannelCount,
|
channels: n_channels as ChannelCount,
|
||||||
min_sample_rate: SampleRate(range.mMinimum as _),
|
min_sample_rate: SampleRate(range.mMinimum as _),
|
||||||
max_sample_rate: SampleRate(range.mMaximum as _),
|
max_sample_rate: SampleRate(range.mMaximum as _),
|
||||||
buffer_size,
|
buffer_size: buffer_size.clone(),
|
||||||
sample_format: sample_format,
|
sample_format: sample_format,
|
||||||
};
|
};
|
||||||
fmts.push(fmt);
|
fmts.push(fmt);
|
||||||
|
@ -439,6 +436,24 @@ impl From<coreaudio::Error> for BuildStreamError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<coreaudio::Error> for SupportedStreamConfigsError {
|
||||||
|
fn from(err: coreaudio::Error) -> SupportedStreamConfigsError {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
let err = BackendSpecificError { description };
|
||||||
|
// Check for possible DeviceNotAvailable variant
|
||||||
|
SupportedStreamConfigsError::BackendSpecific { err }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<coreaudio::Error> for DefaultStreamConfigError {
|
||||||
|
fn from(err: coreaudio::Error) -> DefaultStreamConfigError {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
let err = BackendSpecificError { description };
|
||||||
|
// Check for possible DeviceNotAvailable variant
|
||||||
|
DefaultStreamConfigError::BackendSpecific { err }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create a coreaudio AudioStreamBasicDescription from a CPAL Format.
|
// Create a coreaudio AudioStreamBasicDescription from a CPAL Format.
|
||||||
fn asbd_from_config(
|
fn asbd_from_config(
|
||||||
config: &StreamConfig,
|
config: &StreamConfig,
|
||||||
|
@ -671,12 +686,19 @@ impl Device {
|
||||||
|
|
||||||
// Set the buffersize
|
// Set the buffersize
|
||||||
match config.buffer_size {
|
match config.buffer_size {
|
||||||
BufferSize::Fixed(v) => audio_unit.set_property(
|
BufferSize::Fixed(v) => {
|
||||||
|
let buffer_size_range = get_io_buffer_frame_size_range(&audio_unit)?;
|
||||||
|
if v >= buffer_size_range.min && v <= buffer_size_range.max {
|
||||||
|
audio_unit.set_property(
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
kAudioDevicePropertyBufferFrameSize,
|
||||||
scope,
|
scope,
|
||||||
element,
|
element,
|
||||||
Some(&v),
|
Some(&v),
|
||||||
)?,
|
)?
|
||||||
|
} else {
|
||||||
|
return Err(BuildStreamError::StreamConfigNotSupported);
|
||||||
|
}
|
||||||
|
}
|
||||||
BufferSize::Default => (),
|
BufferSize::Default => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -753,12 +775,19 @@ impl Device {
|
||||||
|
|
||||||
// Set the buffersize
|
// Set the buffersize
|
||||||
match config.buffer_size {
|
match config.buffer_size {
|
||||||
BufferSize::Fixed(v) => audio_unit.set_property(
|
BufferSize::Fixed(v) => {
|
||||||
|
let buffer_size_range = get_io_buffer_frame_size_range(&audio_unit)?;
|
||||||
|
if v >= buffer_size_range.min && v <= buffer_size_range.max {
|
||||||
|
audio_unit.set_property(
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
kAudioDevicePropertyBufferFrameSize,
|
||||||
scope,
|
scope,
|
||||||
element,
|
element,
|
||||||
Some(&v),
|
Some(&v),
|
||||||
)?,
|
)?
|
||||||
|
} else {
|
||||||
|
return Err(BuildStreamError::StreamConfigNotSupported);
|
||||||
|
}
|
||||||
|
}
|
||||||
BufferSize::Default => (),
|
BufferSize::Default => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue