Check that sample format is supported by ASIO when building streams
This commit is contained in:
parent
c432f2b18d
commit
9182f00e20
|
@ -18,11 +18,11 @@ use super::sys;
|
|||
/// A ASIO Device
|
||||
#[derive(Debug)]
|
||||
pub struct Device {
|
||||
/// The drivers for this device
|
||||
/// The driver represented by this device.
|
||||
pub driver: Arc<sys::Driver>,
|
||||
}
|
||||
|
||||
/// All available devices
|
||||
/// All available devices.
|
||||
pub struct Devices {
|
||||
asio: Arc<sys::Asio>,
|
||||
drivers: std::vec::IntoIter<String>,
|
||||
|
@ -109,7 +109,8 @@ impl Device {
|
|||
let sample_rate = SampleRate(self.driver.sample_rate().map_err(default_format_err)? as _);
|
||||
// Map th ASIO sample type to a CPAL sample type
|
||||
let data_type = self.driver.data_type().map_err(default_format_err)?;
|
||||
let data_type = convert_data_type(data_type).ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||
let data_type = convert_data_type(&data_type)
|
||||
.ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||
Ok(Format {
|
||||
channels,
|
||||
sample_rate,
|
||||
|
@ -122,7 +123,8 @@ impl Device {
|
|||
let channels = self.driver.channels().map_err(default_format_err)?.outs as u16;
|
||||
let sample_rate = SampleRate(self.driver.sample_rate().map_err(default_format_err)? as _);
|
||||
let data_type = self.driver.data_type().map_err(default_format_err)?;
|
||||
let data_type = convert_data_type(data_type).ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||
let data_type = convert_data_type(&data_type)
|
||||
.ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||
Ok(Format {
|
||||
channels,
|
||||
sample_rate,
|
||||
|
@ -159,14 +161,19 @@ impl Iterator for Devices {
|
|||
}
|
||||
}
|
||||
|
||||
fn convert_data_type(ty: sys::AsioSampleType) -> Option<SampleFormat> {
|
||||
let fmt = match ty {
|
||||
pub(crate) fn convert_data_type(ty: &sys::AsioSampleType) -> Option<SampleFormat> {
|
||||
let fmt = match *ty {
|
||||
sys::AsioSampleType::ASIOSTInt16MSB => SampleFormat::I16,
|
||||
sys::AsioSampleType::ASIOSTInt32MSB => SampleFormat::I16,
|
||||
sys::AsioSampleType::ASIOSTFloat32MSB => SampleFormat::F32,
|
||||
sys::AsioSampleType::ASIOSTInt16LSB => SampleFormat::I16,
|
||||
sys::AsioSampleType::ASIOSTInt32LSB => SampleFormat::I16,
|
||||
sys::AsioSampleType::ASIOSTFloat32MSB => SampleFormat::F32,
|
||||
sys::AsioSampleType::ASIOSTFloat32LSB => SampleFormat::F32,
|
||||
// NOTE: While ASIO does not support these formats directly, the stream callback created by
|
||||
// CPAL supports converting back and forth between the following. This is because many ASIO
|
||||
// drivers only support `Int32` formats, while CPAL does not support this format at all. We
|
||||
// allow for this implicit conversion temporarily until CPAL gets support for an `I32`
|
||||
// format.
|
||||
sys::AsioSampleType::ASIOSTInt32MSB => SampleFormat::I16,
|
||||
sys::AsioSampleType::ASIOSTInt32LSB => SampleFormat::I16,
|
||||
_ => return None,
|
||||
};
|
||||
Some(fmt)
|
||||
|
|
|
@ -86,43 +86,12 @@ impl EventLoop {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_format(
|
||||
&self,
|
||||
driver: &sys::Driver,
|
||||
format: &Format,
|
||||
num_asio_channels: u16,
|
||||
) -> Result<(), BuildStreamError> {
|
||||
let Format {
|
||||
channels,
|
||||
sample_rate,
|
||||
data_type,
|
||||
} = format;
|
||||
// Try and set the sample rate to what the user selected.
|
||||
let sample_rate = sample_rate.0.into();
|
||||
if sample_rate != driver.sample_rate().map_err(build_stream_err)? {
|
||||
if driver.can_sample_rate(sample_rate).map_err(build_stream_err)? {
|
||||
driver
|
||||
.set_sample_rate(sample_rate)
|
||||
.map_err(build_stream_err)?;
|
||||
} else {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
}
|
||||
// unsigned formats are not supported by asio
|
||||
match data_type {
|
||||
SampleFormat::I16 | SampleFormat::F32 => (),
|
||||
SampleFormat::U16 => return Err(BuildStreamError::FormatNotSupported),
|
||||
}
|
||||
if *channels > num_asio_channels {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a new CPAL Input Stream.
|
||||
/// If there is no ASIO Input Stream
|
||||
/// it will be created.
|
||||
fn get_input_stream(
|
||||
///
|
||||
/// If there is no existing ASIO Input Stream it will be created.
|
||||
///
|
||||
/// On success, the buffer size of the stream is returned.
|
||||
fn get_or_create_input_stream(
|
||||
&self,
|
||||
driver: &sys::Driver,
|
||||
format: &Format,
|
||||
|
@ -131,7 +100,7 @@ impl EventLoop {
|
|||
match device.default_input_format() {
|
||||
Ok(f) => {
|
||||
let num_asio_channels = f.channels;
|
||||
self.check_format(driver, format, num_asio_channels)
|
||||
check_format(driver, format, num_asio_channels)
|
||||
},
|
||||
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
||||
}?;
|
||||
|
@ -161,9 +130,11 @@ impl EventLoop {
|
|||
}
|
||||
|
||||
/// Create a new CPAL Output Stream.
|
||||
/// If there is no ASIO Output Stream
|
||||
/// it will be created.
|
||||
fn get_output_stream(
|
||||
///
|
||||
/// If there is no existing ASIO Output Stream it will be created.
|
||||
///
|
||||
/// On success, the buffer size of the stream is returned.
|
||||
fn get_or_create_output_stream(
|
||||
&self,
|
||||
driver: &sys::Driver,
|
||||
format: &Format,
|
||||
|
@ -172,14 +143,13 @@ impl EventLoop {
|
|||
match device.default_output_format() {
|
||||
Ok(f) => {
|
||||
let num_asio_channels = f.channels;
|
||||
self.check_format(driver, format, num_asio_channels)
|
||||
check_format(driver, format, num_asio_channels)
|
||||
},
|
||||
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
||||
}?;
|
||||
let num_channels = format.channels as usize;
|
||||
let ref mut streams = *self.asio_streams.lock().unwrap();
|
||||
// Either create a stream if thers none or had back the
|
||||
// size of the current one.
|
||||
// Either create a stream if there's none or return the size of the current one.
|
||||
match streams.output {
|
||||
Some(ref output) => Ok(output.buffer_size as usize),
|
||||
None => {
|
||||
|
@ -208,9 +178,17 @@ impl EventLoop {
|
|||
format: &Format,
|
||||
) -> Result<StreamId, BuildStreamError> {
|
||||
let Device { driver, .. } = device;
|
||||
let num_channels = format.channels.clone();
|
||||
let stream_type = driver.data_type().map_err(build_stream_err)?;
|
||||
let stream_buffer_size = self.get_input_stream(&driver, format, device)?;
|
||||
|
||||
// Ensure that the desired sample type is supported.
|
||||
let data_type = super::device::convert_data_type(&stream_type)
|
||||
.ok_or(BuildStreamError::FormatNotSupported)?;
|
||||
if format.data_type != data_type {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
|
||||
let num_channels = format.channels.clone();
|
||||
let stream_buffer_size = self.get_or_create_input_stream(&driver, format, device)?;
|
||||
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
||||
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
||||
let asio_streams = self.asio_streams.clone();
|
||||
|
@ -219,7 +197,6 @@ impl EventLoop {
|
|||
|
||||
// Create the buffer depending on the size of the data type.
|
||||
let stream_id = StreamId(count);
|
||||
let data_type = format.data_type;
|
||||
let len_bytes = cpal_num_samples * data_type.sample_size();
|
||||
let mut interleaved = vec![0u8; len_bytes];
|
||||
|
||||
|
@ -384,9 +361,17 @@ impl EventLoop {
|
|||
format: &Format,
|
||||
) -> Result<StreamId, BuildStreamError> {
|
||||
let Device { driver, .. } = device;
|
||||
let num_channels = format.channels.clone();
|
||||
let stream_type = driver.data_type().map_err(build_stream_err)?;
|
||||
let stream_buffer_size = self.get_output_stream(&driver, format, device)?;
|
||||
|
||||
// Ensure that the desired sample type is supported.
|
||||
let data_type = super::device::convert_data_type(&stream_type)
|
||||
.ok_or(BuildStreamError::FormatNotSupported)?;
|
||||
if format.data_type != data_type {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
|
||||
let num_channels = format.channels.clone();
|
||||
let stream_buffer_size = self.get_or_create_output_stream(&driver, format, device)?;
|
||||
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
||||
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
||||
let asio_streams = self.asio_streams.clone();
|
||||
|
@ -395,7 +380,6 @@ impl EventLoop {
|
|||
|
||||
// Create buffers depending on data type.
|
||||
let stream_id = StreamId(count);
|
||||
let data_type = format.data_type;
|
||||
let len_bytes = cpal_num_samples * data_type.sample_size();
|
||||
let mut interleaved = vec![0u8; len_bytes];
|
||||
let mut silence_asio_buffer = SilenceAsioBuffer::default();
|
||||
|
@ -703,6 +687,41 @@ impl AsioSample for f32 {}
|
|||
|
||||
impl AsioSample for f64 {}
|
||||
|
||||
/// Check whether or not the desired format is supported by the stream.
|
||||
///
|
||||
/// Checks sample rate, data type and then finally the number of channels.
|
||||
fn check_format(
|
||||
driver: &sys::Driver,
|
||||
format: &Format,
|
||||
num_asio_channels: u16,
|
||||
) -> Result<(), BuildStreamError> {
|
||||
let Format {
|
||||
channels,
|
||||
sample_rate,
|
||||
data_type,
|
||||
} = format;
|
||||
// Try and set the sample rate to what the user selected.
|
||||
let sample_rate = sample_rate.0.into();
|
||||
if sample_rate != driver.sample_rate().map_err(build_stream_err)? {
|
||||
if driver.can_sample_rate(sample_rate).map_err(build_stream_err)? {
|
||||
driver
|
||||
.set_sample_rate(sample_rate)
|
||||
.map_err(build_stream_err)?;
|
||||
} else {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
}
|
||||
// unsigned formats are not supported by asio
|
||||
match data_type {
|
||||
SampleFormat::I16 | SampleFormat::F32 => (),
|
||||
SampleFormat::U16 => return Err(BuildStreamError::FormatNotSupported),
|
||||
}
|
||||
if *channels > num_asio_channels {
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cast a byte slice into a mutable slice of desired type.
|
||||
///
|
||||
/// Safety: it's up to the caller to ensure that the input slice has valid bit representations.
|
||||
|
|
Loading…
Reference in New Issue