ASIO audio mixing fixes (#832)

* Fixed ASIO output mixing

Fixed clippy errors

* Fixed buffer silence bug

* Removed byteorder dependency

* Removed SizedSample bound
This commit is contained in:
Jesper Nordenberg 2024-02-21 01:45:23 +01:00 • committed by GitHub
parent b87ec4b5a7
commit adaa49de8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 138 additions and 100 deletions

View file

@ -1,4 +1,3 @@
use std;
pub type SupportedInputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>; pub type SupportedInputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;
pub type SupportedOutputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>; pub type SupportedOutputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;
@ -15,6 +14,7 @@ use crate::SupportedStreamConfig;
use crate::SupportedStreamConfigRange; use crate::SupportedStreamConfigRange;
use crate::SupportedStreamConfigsError; use crate::SupportedStreamConfigsError;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::sync::atomic::AtomicI32;
use std::sync::Arc; use std::sync::Arc;
/// A ASIO Device /// A ASIO Device
@ -27,6 +27,7 @@ pub struct Device {
// A driver can only have one of each. // A driver can only have one of each.
// They need to be created at the same time. // They need to be created at the same time.
pub asio_streams: Arc<Mutex<sys::AsioStreams>>, pub asio_streams: Arc<Mutex<sys::AsioStreams>>,
pub current_buffer_index: Arc<AtomicI32>,
} }
/// All available devices. /// All available devices.
@ -83,8 +84,8 @@ impl Device {
channels, channels,
min_sample_rate: rate, min_sample_rate: rate,
max_sample_rate: rate, max_sample_rate: rate,
buffer_size: f.buffer_size.clone(), buffer_size: f.buffer_size,
sample_format: f.sample_format.clone(), sample_format: f.sample_format,
}) })
} }
} }
@ -120,8 +121,8 @@ impl Device {
channels, channels,
min_sample_rate: rate, min_sample_rate: rate,
max_sample_rate: rate, max_sample_rate: rate,
buffer_size: f.buffer_size.clone(), buffer_size: f.buffer_size,
sample_format: f.sample_format.clone(), sample_format: f.sample_format,
}) })
} }
} }
@ -194,6 +195,7 @@ impl Iterator for Devices {
return Some(Device { return Some(Device {
driver, driver,
asio_streams, asio_streams,
current_buffer_index: Arc::new(AtomicI32::new(-1)),
}); });
} }
Err(_) => continue, Err(_) => continue,

View file

@ -6,22 +6,12 @@ use super::parking_lot::Mutex;
use super::Device; use super::Device;
use crate::{ use crate::{
BackendSpecificError, BufferSize, BuildStreamError, Data, InputCallbackInfo, BackendSpecificError, BufferSize, BuildStreamError, Data, InputCallbackInfo,
OutputCallbackInfo, PauseStreamError, PlayStreamError, SampleFormat, SizedSample, StreamConfig, OutputCallbackInfo, PauseStreamError, PlayStreamError, SampleFormat, StreamConfig, StreamError,
StreamError,
}; };
use std;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
// Used to keep track of whether or not the current asio stream buffer requires
// being silencing before summing audio.
#[derive(Default)]
struct SilenceAsioBuffer {
first: bool,
second: bool,
}
pub struct Stream { pub struct Stream {
playing: Arc<AtomicBool>, playing: Arc<AtomicBool>,
// Ensure the `Driver` does not terminate until the last stream is dropped. // Ensure the `Driver` does not terminate until the last stream is dropped.
@ -65,7 +55,7 @@ impl Device {
return Err(BuildStreamError::StreamConfigNotSupported); return Err(BuildStreamError::StreamConfigNotSupported);
} }
let num_channels = config.channels.clone(); let num_channels = config.channels;
let buffer_size = self.get_or_create_input_stream(config, sample_format)?; let buffer_size = self.get_or_create_input_stream(config, sample_format)?;
let cpal_num_samples = buffer_size * num_channels as usize; let cpal_num_samples = buffer_size * num_channels as usize;
@ -88,7 +78,7 @@ impl Device {
// There is 0% chance of lock contention the host only locks when recreating streams. // There is 0% chance of lock contention the host only locks when recreating streams.
let stream_lock = asio_streams.lock(); let stream_lock = asio_streams.lock();
let ref asio_stream = match stream_lock.input { let asio_stream = match stream_lock.input {
Some(ref asio_stream) => asio_stream, Some(ref asio_stream) => asio_stream,
None => return, None => return,
}; };
@ -101,9 +91,10 @@ impl Device {
asio_stream: &sys::AsioStream, asio_stream: &sys::AsioStream,
asio_info: &sys::CallbackInfo, asio_info: &sys::CallbackInfo,
sample_rate: crate::SampleRate, sample_rate: crate::SampleRate,
format: SampleFormat,
from_endianness: F, from_endianness: F,
) where ) where
A: SizedSample, A: Copy,
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
F: Fn(A) -> A, F: Fn(A) -> A,
{ {
@ -122,7 +113,7 @@ impl Device {
// 2. Deliver the interleaved buffer to the callback. // 2. Deliver the interleaved buffer to the callback.
let data = interleaved.as_mut_ptr() as *mut (); let data = interleaved.as_mut_ptr() as *mut ();
let len = interleaved.len(); let len = interleaved.len();
let data = Data::from_parts(data, len, A::FORMAT); let data = Data::from_parts(data, len, format);
let callback = system_time_to_stream_instant(asio_info.system_time); let callback = system_time_to_stream_instant(asio_info.system_time);
let delay = frames_to_duration(n_frames, sample_rate); let delay = frames_to_duration(n_frames, sample_rate);
let capture = callback let capture = callback
@ -141,6 +132,7 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
SampleFormat::I16,
from_le, from_le,
); );
} }
@ -151,21 +143,31 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
SampleFormat::I16,
from_be, from_be,
); );
} }
// TODO: Handle endianness conversion for floats? We currently use the `PrimInt` (&sys::AsioSampleType::ASIOSTFloat32LSB, SampleFormat::F32) => {
// trait for the `to_le` and `to_be` methods, but this does not support floats. process_input_callback::<u32, _, _>(
(&sys::AsioSampleType::ASIOSTFloat32LSB, SampleFormat::F32)
| (&sys::AsioSampleType::ASIOSTFloat32MSB, SampleFormat::F32) => {
process_input_callback::<f32, _, _>(
&mut data_callback, &mut data_callback,
&mut interleaved, &mut interleaved,
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
std::convert::identity::<f32>, SampleFormat::F32,
from_le,
);
}
(&sys::AsioSampleType::ASIOSTFloat32MSB, SampleFormat::F32) => {
process_input_callback::<u32, _, _>(
&mut data_callback,
&mut interleaved,
asio_stream,
callback_info,
config.sample_rate,
SampleFormat::F32,
from_be,
); );
} }
@ -176,6 +178,7 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
SampleFormat::I32,
from_le, from_le,
); );
} }
@ -186,21 +189,31 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
SampleFormat::I32,
from_be, from_be,
); );
} }
// TODO: Handle endianness conversion for floats? We currently use the `PrimInt` (&sys::AsioSampleType::ASIOSTFloat64LSB, SampleFormat::F64) => {
// trait for the `to_le` and `to_be` methods, but this does not support floats. process_input_callback::<u64, _, _>(
(&sys::AsioSampleType::ASIOSTFloat64LSB, SampleFormat::F64)
| (&sys::AsioSampleType::ASIOSTFloat64MSB, SampleFormat::F64) => {
process_input_callback::<f64, _, _>(
&mut data_callback, &mut data_callback,
&mut interleaved, &mut interleaved,
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
std::convert::identity::<f64>, SampleFormat::F64,
from_le,
);
}
(&sys::AsioSampleType::ASIOSTFloat64MSB, SampleFormat::F64) => {
process_input_callback::<u64, _, _>(
&mut data_callback,
&mut interleaved,
asio_stream,
callback_info,
config.sample_rate,
SampleFormat::F64,
from_be,
); );
} }
@ -247,14 +260,14 @@ impl Device {
return Err(BuildStreamError::StreamConfigNotSupported); return Err(BuildStreamError::StreamConfigNotSupported);
} }
let num_channels = config.channels.clone(); let num_channels = config.channels;
let buffer_size = self.get_or_create_output_stream(config, sample_format)?; let buffer_size = self.get_or_create_output_stream(config, sample_format)?;
let cpal_num_samples = buffer_size * num_channels as usize; let cpal_num_samples = buffer_size * num_channels as usize;
// Create buffers depending on data type. // Create buffers depending on data type.
let len_bytes = cpal_num_samples * sample_format.sample_size(); let len_bytes = cpal_num_samples * sample_format.sample_size();
let mut interleaved = vec![0u8; len_bytes]; let mut interleaved = vec![0u8; len_bytes];
let mut silence_asio_buffer = SilenceAsioBuffer::default(); let current_buffer_index = self.current_buffer_index.clone();
let stream_playing = Arc::new(AtomicBool::new(false)); let stream_playing = Arc::new(AtomicBool::new(false));
let playing = Arc::clone(&stream_playing); let playing = Arc::clone(&stream_playing);
@ -268,9 +281,9 @@ impl Device {
} }
// There is 0% chance of lock contention the host only locks when recreating streams. // There is 0% chance of lock contention the host only locks when recreating streams.
let stream_lock = asio_streams.lock(); let mut stream_lock = asio_streams.lock();
let ref asio_stream = match stream_lock.output { let asio_stream = match stream_lock.output {
Some(ref asio_stream) => asio_stream, Some(ref mut asio_stream) => asio_stream,
None => return, None => return,
}; };
@ -278,23 +291,12 @@ impl Device {
// //
// This checks if any other callbacks have already silenced the buffer associated with // This checks if any other callbacks have already silenced the buffer associated with
// the current `buffer_index`. // the current `buffer_index`.
// let silence =
// If not, we will silence it and set the opposite buffer half to unsilenced. current_buffer_index.load(Ordering::Acquire) != callback_info.buffer_index;
let silence = match callback_info.buffer_index {
0 if !silence_asio_buffer.first => { if silence {
silence_asio_buffer.first = true; current_buffer_index.store(callback_info.buffer_index, Ordering::Release);
silence_asio_buffer.second = false; }
true
}
0 => false,
1 if !silence_asio_buffer.second => {
silence_asio_buffer.second = true;
silence_asio_buffer.first = false;
true
}
1 => false,
_ => unreachable!("ASIO uses a double-buffer so there should only be 2"),
};
/// 1. Render the given callback to the given buffer of interleaved samples. /// 1. Render the given callback to the given buffer of interleaved samples.
/// 2. If required, silence the ASIO buffer. /// 2. If required, silence the ASIO buffer.
@ -304,20 +306,21 @@ impl Device {
data_callback: &mut D, data_callback: &mut D,
interleaved: &mut [u8], interleaved: &mut [u8],
silence_asio_buffer: bool, silence_asio_buffer: bool,
asio_stream: &sys::AsioStream, asio_stream: &mut sys::AsioStream,
asio_info: &sys::CallbackInfo, asio_info: &sys::CallbackInfo,
sample_rate: crate::SampleRate, sample_rate: crate::SampleRate,
to_endianness: F, format: SampleFormat,
mix_samples: F,
) where ) where
A: SizedSample + std::ops::Add<Output = A>, A: Copy,
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
F: Fn(A) -> A, F: Fn(A, A) -> A,
{ {
// 1. Render interleaved buffer from callback. // 1. Render interleaved buffer from callback.
let interleaved: &mut [A] = cast_slice_mut(interleaved); let interleaved: &mut [A] = cast_slice_mut(interleaved);
let data = interleaved.as_mut_ptr() as *mut (); let data = interleaved.as_mut_ptr() as *mut ();
let len = interleaved.len(); let len = interleaved.len();
let mut data = Data::from_parts(data, len, A::FORMAT); let mut data = Data::from_parts(data, len, format);
let callback = system_time_to_stream_instant(asio_info.system_time); let callback = system_time_to_stream_instant(asio_info.system_time);
let n_frames = asio_stream.buffer_size as usize; let n_frames = asio_stream.buffer_size as usize;
let delay = frames_to_duration(n_frames, sample_rate); let delay = frames_to_duration(n_frames, sample_rate);
@ -335,9 +338,7 @@ impl Device {
for ch_ix in 0..n_channels { for ch_ix in 0..n_channels {
let asio_channel = let asio_channel =
asio_channel_slice_mut::<A>(asio_stream, buffer_index, ch_ix); asio_channel_slice_mut::<A>(asio_stream, buffer_index, ch_ix);
asio_channel asio_channel.align_to_mut::<u8>().1.fill(0);
.iter_mut()
.for_each(|s| *s = to_endianness(A::EQUILIBRIUM));
} }
} }
@ -346,7 +347,7 @@ impl Device {
let asio_channel = let asio_channel =
asio_channel_slice_mut::<A>(asio_stream, buffer_index, ch_ix); asio_channel_slice_mut::<A>(asio_stream, buffer_index, ch_ix);
for (frame, s_asio) in interleaved.chunks(n_channels).zip(asio_channel) { for (frame, s_asio) in interleaved.chunks(n_channels).zip(asio_channel) {
*s_asio = *s_asio + to_endianness(A::from_sample(frame[ch_ix])); *s_asio = mix_samples(*s_asio, frame[ch_ix]);
} }
} }
} }
@ -360,7 +361,10 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
to_le, SampleFormat::I16,
|old_sample, new_sample| {
from_le(old_sample).saturating_add(new_sample).to_le()
},
); );
} }
(SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt16MSB) => { (SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt16MSB) => {
@ -371,22 +375,43 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
to_be, SampleFormat::I16,
|old_sample, new_sample| {
from_be(old_sample).saturating_add(new_sample).to_be()
},
); );
} }
(SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat32LSB) => {
// TODO: Handle endianness conversion for floats? We currently use the `PrimInt` process_output_callback::<u32, _, _>(
// trait for the `to_le` and `to_be` methods, but this does not support floats.
(SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat32LSB)
| (SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat32MSB) => {
process_output_callback::<f32, _, _>(
&mut data_callback, &mut data_callback,
&mut interleaved, &mut interleaved,
silence, silence,
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
std::convert::identity::<f32>, SampleFormat::F32,
|old_sample, new_sample| {
(f32::from_bits(from_le(old_sample)) + f32::from_bits(new_sample))
.to_bits()
.to_le()
},
);
}
(SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat32MSB) => {
process_output_callback::<u32, _, _>(
&mut data_callback,
&mut interleaved,
silence,
asio_stream,
callback_info,
config.sample_rate,
SampleFormat::F32,
|old_sample, new_sample| {
(f32::from_bits(from_be(old_sample)) + f32::from_bits(new_sample))
.to_bits()
.to_be()
},
); );
} }
@ -398,7 +423,10 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
to_le, SampleFormat::I32,
|old_sample, new_sample| {
from_le(old_sample).saturating_add(new_sample).to_le()
},
); );
} }
(SampleFormat::I32, &sys::AsioSampleType::ASIOSTInt32MSB) => { (SampleFormat::I32, &sys::AsioSampleType::ASIOSTInt32MSB) => {
@ -409,22 +437,44 @@ impl Device {
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
to_be, SampleFormat::I32,
|old_sample, new_sample| {
from_be(old_sample).saturating_add(new_sample).to_be()
},
); );
} }
// TODO: Handle endianness conversion for floats? We currently use the `PrimInt` (SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64LSB) => {
// trait for the `to_le` and `to_be` methods, but this does not support floats. process_output_callback::<u64, _, _>(
(SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64LSB)
| (SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64MSB) => {
process_output_callback::<f64, _, _>(
&mut data_callback, &mut data_callback,
&mut interleaved, &mut interleaved,
silence, silence,
asio_stream, asio_stream,
callback_info, callback_info,
config.sample_rate, config.sample_rate,
std::convert::identity::<f64>, SampleFormat::F64,
|old_sample, new_sample| {
(f64::from_bits(from_le(old_sample)) + f64::from_bits(new_sample))
.to_bits()
.to_le()
},
);
}
(SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64MSB) => {
process_output_callback::<u64, _, _>(
&mut data_callback,
&mut interleaved,
silence,
asio_stream,
callback_info,
config.sample_rate,
SampleFormat::F64,
|old_sample, new_sample| {
(f64::from_bits(from_be(old_sample)) + f64::from_bits(new_sample))
.to_bits()
.to_be()
},
); );
} }
@ -468,7 +518,7 @@ impl Device {
Err(_) => Err(BuildStreamError::StreamConfigNotSupported), Err(_) => Err(BuildStreamError::StreamConfigNotSupported),
}?; }?;
let num_channels = config.channels as usize; let num_channels = config.channels as usize;
let ref mut streams = *self.asio_streams.lock(); let mut streams = self.asio_streams.lock();
let buffer_size = match config.buffer_size { let buffer_size = match config.buffer_size {
BufferSize::Fixed(v) => Some(v as i32), BufferSize::Fixed(v) => Some(v as i32),
@ -515,7 +565,7 @@ impl Device {
Err(_) => Err(BuildStreamError::StreamConfigNotSupported), Err(_) => Err(BuildStreamError::StreamConfigNotSupported),
}?; }?;
let num_channels = config.channels as usize; let num_channels = config.channels as usize;
let ref mut streams = *self.asio_streams.lock(); let mut streams = self.asio_streams.lock();
let buffer_size = match config.buffer_size { let buffer_size = match config.buffer_size {
BufferSize::Fixed(v) => Some(v as i32), BufferSize::Fixed(v) => Some(v as i32),
@ -623,16 +673,6 @@ unsafe fn cast_slice_mut<T>(v: &mut [u8]) -> &mut [T] {
std::slice::from_raw_parts_mut(v.as_mut_ptr() as *mut T, v.len() / std::mem::size_of::<T>()) std::slice::from_raw_parts_mut(v.as_mut_ptr() as *mut T, v.len() / std::mem::size_of::<T>())
} }
/// Helper function to convert to little endianness.
fn to_le<T: PrimInt>(t: T) -> T {
t.to_le()
}
/// Helper function to convert to big endianness.
fn to_be<T: PrimInt>(t: T) -> T {
t.to_be()
}
/// Helper function to convert from little endianness. /// Helper function to convert from little endianness.
fn from_le<T: PrimInt>(t: T) -> T { fn from_le<T: PrimInt>(t: T) -> T {
T::from_le(t) T::from_le(t)
@ -644,23 +684,19 @@ fn from_be<T: PrimInt>(t: T) -> T {
} }
/// Shorthand for retrieving the asio buffer slice associated with a channel. /// Shorthand for retrieving the asio buffer slice associated with a channel.
///
/// Safety: it's up to the user to ensure that this function is not called multiple times for the
/// same channel.
unsafe fn asio_channel_slice<T>( unsafe fn asio_channel_slice<T>(
asio_stream: &sys::AsioStream, asio_stream: &sys::AsioStream,
buffer_index: usize, buffer_index: usize,
channel_index: usize, channel_index: usize,
) -> &[T] { ) -> &[T] {
asio_channel_slice_mut(asio_stream, buffer_index, channel_index) let buff_ptr: *const T =
asio_stream.buffer_infos[channel_index].buffers[buffer_index as usize] as *const _;
std::slice::from_raw_parts(buff_ptr, asio_stream.buffer_size as usize)
} }
/// Shorthand for retrieving the asio buffer slice associated with a channel. /// Shorthand for retrieving the asio buffer slice associated with a channel.
///
/// Safety: it's up to the user to ensure that this function is not called multiple times for the
/// same channel.
unsafe fn asio_channel_slice_mut<T>( unsafe fn asio_channel_slice_mut<T>(
asio_stream: &sys::AsioStream, asio_stream: &mut sys::AsioStream,
buffer_index: usize, buffer_index: usize,
channel_index: usize, channel_index: usize,
) -> &mut [T] { ) -> &mut [T] {