Update CoreAudio host for the addition of the new stream `Data` type

This commit is contained in:
mitchmindtree 2020-01-19 19:42:43 +01:00
parent c0a28b5198
commit 64f8fd12cc
1 changed files with 21 additions and 27 deletions

View File

@ -5,15 +5,13 @@ use crate::{
ChannelCount, ChannelCount,
BackendSpecificError, BackendSpecificError,
BuildStreamError, BuildStreamError,
Data,
DefaultFormatError, DefaultFormatError,
DeviceNameError, DeviceNameError,
DevicesError, DevicesError,
Format, Format,
InputData,
OutputData,
PauseStreamError, PauseStreamError,
PlayStreamError, PlayStreamError,
Sample,
SampleFormat, SampleFormat,
SampleRate, SampleRate,
StreamError, StreamError,
@ -131,33 +129,29 @@ impl DeviceTrait for Device {
Device::default_output_format(self) Device::default_output_format(self)
} }
fn build_input_stream<T, D, E>( fn build_input_stream<D, E>(
&self, &self,
format: &Format, format: &Format,
data_callback: D, data_callback: D,
error_callback: E, error_callback: E,
) -> Result<Self::Stream, BuildStreamError> ) -> Result<Self::Stream, BuildStreamError>
where where
T: Sample, D: FnMut(&Data) + Send + 'static,
D: FnMut(InputData<T>) + Send + 'static,
E: FnMut(StreamError) + Send + 'static, E: FnMut(StreamError) + Send + 'static,
{ {
assert_eq!(T::FORMAT, format.data_type);
Device::build_input_stream(self, format, data_callback, error_callback) Device::build_input_stream(self, format, data_callback, error_callback)
} }
fn build_output_stream<T, D, E>( fn build_output_stream<D, E>(
&self, &self,
format: &Format, format: &Format,
data_callback: D, data_callback: D,
error_callback: E, error_callback: E,
) -> Result<Self::Stream, BuildStreamError> ) -> Result<Self::Stream, BuildStreamError>
where where
T: Sample, D: FnMut(&mut Data) + Send + 'static,
D: FnMut(OutputData<T>) + Send + 'static,
E: FnMut(StreamError) + Send + 'static, E: FnMut(StreamError) + Send + 'static,
{ {
assert_eq!(T::FORMAT, format.data_type);
Device::build_output_stream(self, format, data_callback, error_callback) Device::build_output_stream(self, format, data_callback, error_callback)
} }
} }
@ -500,15 +494,14 @@ fn audio_unit_from_device(device: &Device, input: bool) -> Result<AudioUnit, cor
} }
impl Device { impl Device {
fn build_input_stream<T, D, E>( fn build_input_stream<D, E>(
&self, &self,
format: &Format, format: &Format,
mut data_callback: D, mut data_callback: D,
_error_callback: E, _error_callback: E,
) -> Result<Stream, BuildStreamError> ) -> Result<Stream, BuildStreamError>
where where
T: Sample, D: FnMut(&Data) + Send + 'static,
D: FnMut(InputData<T>) + Send + 'static,
E: FnMut(StreamError) + Send + 'static, E: FnMut(StreamError) + Send + 'static,
{ {
// The scope and element for working with a device's input stream. // The scope and element for working with a device's input stream.
@ -657,7 +650,8 @@ impl Device {
// Register the callback that is being called by coreaudio whenever it needs data to be // Register the callback that is being called by coreaudio whenever it needs data to be
// fed to the audio buffer. // fed to the audio buffer.
let bytes_per_channel = std::mem::size_of::<T>(); let sample_format = format.data_type;
let bytes_per_channel = sample_format.sample_size();
type Args = render_callback::Args<data::Raw>; type Args = render_callback::Args<data::Raw>;
audio_unit.set_input_callback(move |args: Args| unsafe { audio_unit.set_input_callback(move |args: Args| unsafe {
let ptr = (*args.data.data).mBuffers.as_ptr() as *const AudioBuffer; let ptr = (*args.data.data).mBuffers.as_ptr() as *const AudioBuffer;
@ -671,10 +665,10 @@ impl Device {
mData: data mData: data
} = buffers[0]; } = buffers[0];
let data_len = (data_byte_size as usize / bytes_per_channel) as usize; let data = data as *mut ();
let data_slice = slice::from_raw_parts(data as *const T, data_len); let len = (data_byte_size as usize / bytes_per_channel) as usize;
let input_data = InputData { buffer: data_slice }; let data = Data::from_parts(data, len, sample_format);
data_callback(input_data); data_callback(&data);
Ok(()) Ok(())
})?; })?;
@ -687,15 +681,14 @@ impl Device {
})) }))
} }
fn build_output_stream<T, D, E>( fn build_output_stream<D, E>(
&self, &self,
format: &Format, format: &Format,
mut data_callback: D, mut data_callback: D,
_error_callback: E, _error_callback: E,
) -> Result<Stream, BuildStreamError> ) -> Result<Stream, BuildStreamError>
where where
T: Sample, D: FnMut(&mut Data) + Send + 'static,
D: FnMut(OutputData<T>) + Send + 'static,
E: FnMut(StreamError) + Send + 'static, E: FnMut(StreamError) + Send + 'static,
{ {
let mut audio_unit = audio_unit_from_device(self, false)?; let mut audio_unit = audio_unit_from_device(self, false)?;
@ -710,7 +703,8 @@ impl Device {
// Register the callback that is being called by coreaudio whenever it needs data to be // Register the callback that is being called by coreaudio whenever it needs data to be
// fed to the audio buffer. // fed to the audio buffer.
let bytes_per_channel = std::mem::size_of::<T>(); let sample_format = format.data_type;
let bytes_per_channel = sample_format.sample_size();
type Args = render_callback::Args<data::Raw>; type Args = render_callback::Args<data::Raw>;
audio_unit.set_render_callback(move |args: Args| unsafe { audio_unit.set_render_callback(move |args: Args| unsafe {
// If `run()` is currently running, then a callback will be available from this list. // If `run()` is currently running, then a callback will be available from this list.
@ -722,10 +716,10 @@ impl Device {
mData: data mData: data
} = (*args.data.data).mBuffers[0]; } = (*args.data.data).mBuffers[0];
let data_len = (data_byte_size as usize / bytes_per_channel) as usize; let data = data as *mut ();
let data_slice = slice::from_raw_parts_mut(data as *mut T, data_len); let len = (data_byte_size as usize / bytes_per_channel) as usize;
let output_data = OutputData { buffer: data_slice }; let mut data = Data::from_parts(data, len, sample_format);
data_callback(output_data); data_callback(&mut data);
Ok(()) Ok(())
})?; })?;