diff --git a/Cargo.toml b/Cargo.toml index 8972176..d5f54c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for se [dependencies] thiserror = "1.0.2" +dasp_sample = "0.11.0" [dev-dependencies] anyhow = "1.0.12" diff --git a/examples/android.rs b/examples/android.rs index 7ca17da..9cdad5e 100644 --- a/examples/android.rs +++ b/examples/android.rs @@ -3,7 +3,11 @@ extern crate anyhow; extern crate cpal; -use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{ + traits::{DeviceTrait, HostTrait, StreamTrait}, + SizedSample, +}; +use cpal::{FromSample, Sample}; #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "full"))] fn main() { @@ -16,15 +20,27 @@ fn main() { let config = device.default_output_config().unwrap(); match config.sample_format() { - cpal::SampleFormat::F32 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::I8 => run::(&device, &config.into()).unwrap(), cpal::SampleFormat::I16 => run::(&device, &config.into()).unwrap(), + // cpal::SampleFormat::I24 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::I32 => run::(&device, &config.into()).unwrap(), + // cpal::SampleFormat::I48 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::I64 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::U8 => run::(&device, &config.into()).unwrap(), cpal::SampleFormat::U16 => run::(&device, &config.into()).unwrap(), + // cpal::SampleFormat::U24 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::U32 => run::(&device, &config.into()).unwrap(), + // cpal::SampleFormat::U48 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::U64 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::F32 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::F64 => run::(&device, &config.into()).unwrap(), + sample_format => panic!("Unsupported sample format '{sample_format}'"), } } fn run(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error> where - T: cpal::Sample, + T: SizedSample + FromSample, { let sample_rate = config.sample_rate.0 as f32; let channels = config.channels as usize; @@ -54,10 +70,10 @@ where fn write_data(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32) where - T: cpal::Sample, + T: Sample + FromSample, { for frame in output.chunks_mut(channels) { - let value: T = cpal::Sample::from::(&next_sample()); + let value: T = T::from_sample(next_sample()); for sample in frame.iter_mut() { *sample = value; } diff --git a/examples/beep.rs b/examples/beep.rs index 6ede2ea..7deb717 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -3,7 +3,11 @@ extern crate clap; extern crate cpal; use clap::arg; -use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{ + traits::{DeviceTrait, HostTrait, StreamTrait}, + SizedSample, +}; +use cpal::{FromSample, Sample}; #[derive(Debug)] struct Opt { @@ -84,15 +88,27 @@ fn main() -> anyhow::Result<()> { println!("Default output config: {:?}", config); match config.sample_format() { - cpal::SampleFormat::F32 => run::(&device, &config.into()), + cpal::SampleFormat::I8 => run::(&device, &config.into()), cpal::SampleFormat::I16 => run::(&device, &config.into()), + // cpal::SampleFormat::I24 => run::(&device, &config.into()), + cpal::SampleFormat::I32 => run::(&device, &config.into()), + // cpal::SampleFormat::I48 => run::(&device, &config.into()), + cpal::SampleFormat::I64 => run::(&device, &config.into()), + cpal::SampleFormat::U8 => run::(&device, &config.into()), cpal::SampleFormat::U16 => run::(&device, &config.into()), + // cpal::SampleFormat::U24 => run::(&device, &config.into()), + cpal::SampleFormat::U32 => run::(&device, &config.into()), + // cpal::SampleFormat::U48 => run::(&device, &config.into()), + cpal::SampleFormat::U64 => run::(&device, &config.into()), + cpal::SampleFormat::F32 => run::(&device, &config.into()), + cpal::SampleFormat::F64 => run::(&device, &config.into()), + sample_format => panic!("Unsupported sample format '{sample_format}'"), } } pub fn run(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error> where - T: cpal::Sample, + T: SizedSample + FromSample, { let sample_rate = config.sample_rate.0 as f32; let channels = config.channels as usize; @@ -122,10 +138,10 @@ where fn write_data(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32) where - T: cpal::Sample, + T: Sample + FromSample, { for frame in output.chunks_mut(channels) { - let value: T = cpal::Sample::from::(&next_sample()); + let value: T = T::from_sample(next_sample()); for sample in frame.iter_mut() { *sample = value; } diff --git a/examples/record_wav.rs b/examples/record_wav.rs index a3d52a7..8f7ca18 100644 --- a/examples/record_wav.rs +++ b/examples/record_wav.rs @@ -9,6 +9,7 @@ extern crate hound; use clap::arg; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{FromSample, Sample}; use std::fs::File; use std::io::BufWriter; use std::sync::{Arc, Mutex}; @@ -112,9 +113,9 @@ fn main() -> Result<(), anyhow::Error> { }; let stream = match config.sample_format() { - cpal::SampleFormat::F32 => device.build_input_stream( + cpal::SampleFormat::I8 => device.build_input_stream( &config.into(), - move |data, _: &_| write_input_data::(data, &writer_2), + move |data, _: &_| write_input_data::(data, &writer_2), err_fn, )?, cpal::SampleFormat::I16 => device.build_input_stream( @@ -122,11 +123,21 @@ fn main() -> Result<(), anyhow::Error> { move |data, _: &_| write_input_data::(data, &writer_2), err_fn, )?, - cpal::SampleFormat::U16 => device.build_input_stream( + cpal::SampleFormat::I32 => device.build_input_stream( &config.into(), - move |data, _: &_| write_input_data::(data, &writer_2), + move |data, _: &_| write_input_data::(data, &writer_2), err_fn, )?, + cpal::SampleFormat::F32 => device.build_input_stream( + &config.into(), + move |data, _: &_| write_input_data::(data, &writer_2), + err_fn, + )?, + sample_format => { + return Err(anyhow::Error::msg(format!( + "Unsupported sample format '{sample_format}'" + ))) + } }; stream.play()?; @@ -140,10 +151,10 @@ fn main() -> Result<(), anyhow::Error> { } fn sample_format(format: cpal::SampleFormat) -> hound::SampleFormat { - match format { - cpal::SampleFormat::U16 => hound::SampleFormat::Int, - cpal::SampleFormat::I16 => hound::SampleFormat::Int, - cpal::SampleFormat::F32 => hound::SampleFormat::Float, + if format.is_float() { + hound::SampleFormat::Float + } else { + hound::SampleFormat::Int } } @@ -160,13 +171,13 @@ type WavWriterHandle = Arc>>>>; fn write_input_data(input: &[T], writer: &WavWriterHandle) where - T: cpal::Sample, - U: cpal::Sample + hound::Sample, + T: Sample, + U: Sample + hound::Sample + FromSample, { if let Ok(mut guard) = writer.try_lock() { if let Some(writer) = guard.as_mut() { for &sample in input.iter() { - let sample: U = cpal::Sample::from(&sample); + let sample: U = U::from_sample(sample); writer.write_sample(sample).ok(); } } diff --git a/examples/synth_tones.rs b/examples/synth_tones.rs index d1fa5cb..abb8ab4 100644 --- a/examples/synth_tones.rs +++ b/examples/synth_tones.rs @@ -6,7 +6,11 @@ extern crate anyhow; extern crate clap; extern crate cpal; -use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{ + traits::{DeviceTrait, HostTrait, StreamTrait}, + SizedSample, +}; +use cpal::{FromSample, Sample}; fn main() -> anyhow::Result<()> { let stream = stream_setup_for(sample_next)?; @@ -43,9 +47,23 @@ where let (_host, device, config) = host_device_setup()?; match config.sample_format() { - cpal::SampleFormat::F32 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::I8 => stream_make::(&device, &config.into(), on_sample), cpal::SampleFormat::I16 => stream_make::(&device, &config.into(), on_sample), + // cpal::SampleFormat::I24 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::I32 => stream_make::(&device, &config.into(), on_sample), + // cpal::SampleFormat::I48 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::I64 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::U8 => stream_make::(&device, &config.into(), on_sample), cpal::SampleFormat::U16 => stream_make::(&device, &config.into(), on_sample), + // cpal::SampleFormat::U24 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::U32 => stream_make::(&device, &config.into(), on_sample), + // cpal::SampleFormat::U48 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::U64 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::F32 => stream_make::(&device, &config.into(), on_sample), + cpal::SampleFormat::F64 => stream_make::(&device, &config.into(), on_sample), + sample_format => Err(anyhow::Error::msg(format!( + "Unsupported sample format '{sample_format}'" + ))), } } @@ -70,7 +88,7 @@ pub fn stream_make( on_sample: F, ) -> Result where - T: cpal::Sample, + T: SizedSample + FromSample, F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy, { let sample_rate = config.sample_rate.0 as f32; @@ -96,11 +114,11 @@ where fn on_window(output: &mut [T], request: &mut SampleRequestOptions, mut on_sample: F) where - T: cpal::Sample, + T: Sample + FromSample, F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static, { for frame in output.chunks_mut(request.nchannels) { - let value: T = cpal::Sample::from::(&on_sample(request)); + let value: T = T::from_sample(on_sample(request)); for sample in frame.iter_mut() { *sample = value; } diff --git a/src/host/alsa/mod.rs b/src/host/alsa/mod.rs index c7a88d1..455d8bc 100644 --- a/src/host/alsa/mod.rs +++ b/src/host/alsa/mod.rs @@ -312,9 +312,9 @@ impl Device { let hw_params = alsa::pcm::HwParams::any(handle)?; // TODO: check endianness - const FORMATS: [(SampleFormat, alsa::pcm::Format); 3] = [ - //SND_PCM_FORMAT_S8, - //SND_PCM_FORMAT_U8, + const FORMATS: [(SampleFormat, alsa::pcm::Format); 8] = [ + (SampleFormat::I8, alsa::pcm::Format::S8), + (SampleFormat::U8, alsa::pcm::Format::U8), (SampleFormat::I16, alsa::pcm::Format::S16LE), //SND_PCM_FORMAT_S16_BE, (SampleFormat::U16, alsa::pcm::Format::U16LE), @@ -323,13 +323,13 @@ impl Device { //SND_PCM_FORMAT_S24_BE, //SND_PCM_FORMAT_U24_LE, //SND_PCM_FORMAT_U24_BE, - //SND_PCM_FORMAT_S32_LE, + (SampleFormat::I32, alsa::pcm::Format::S32LE), //SND_PCM_FORMAT_S32_BE, - //SND_PCM_FORMAT_U32_LE, + (SampleFormat::U32, alsa::pcm::Format::U32LE), //SND_PCM_FORMAT_U32_BE, (SampleFormat::F32, alsa::pcm::Format::FloatLE), //SND_PCM_FORMAT_FLOAT_BE, - //SND_PCM_FORMAT_FLOAT64_LE, + (SampleFormat::F64, alsa::pcm::Format::Float64LE), //SND_PCM_FORMAT_FLOAT64_BE, //SND_PCM_FORMAT_IEC958_SUBFRAME_LE, //SND_PCM_FORMAT_IEC958_SUBFRAME_BE, @@ -956,15 +956,53 @@ fn set_hw_params_from_format( let sample_format = if cfg!(target_endian = "big") { match sample_format { + SampleFormat::I8 => alsa::pcm::Format::S8, SampleFormat::I16 => alsa::pcm::Format::S16BE, + // SampleFormat::I24 => alsa::pcm::Format::S24BE, + SampleFormat::I32 => alsa::pcm::Format::S32BE, + // SampleFormat::I48 => alsa::pcm::Format::S48BE, + // SampleFormat::I64 => alsa::pcm::Format::S64BE, + SampleFormat::U8 => alsa::pcm::Format::U8, SampleFormat::U16 => alsa::pcm::Format::U16BE, + // SampleFormat::U24 => alsa::pcm::Format::U24BE, + SampleFormat::U32 => alsa::pcm::Format::U32BE, + // SampleFormat::U48 => alsa::pcm::Format::U48BE, + // SampleFormat::U64 => alsa::pcm::Format::U64BE, SampleFormat::F32 => alsa::pcm::Format::FloatBE, + SampleFormat::F64 => alsa::pcm::Format::Float64BE, + sample_format => { + return Err(BackendSpecificError { + description: format!( + "Sample format '{}' is not supported by this backend", + sample_format + ), + }) + } } } else { match sample_format { + SampleFormat::I8 => alsa::pcm::Format::S8, SampleFormat::I16 => alsa::pcm::Format::S16LE, + // SampleFormat::I24 => alsa::pcm::Format::S24LE, + SampleFormat::I32 => alsa::pcm::Format::S32LE, + // SampleFormat::I48 => alsa::pcm::Format::S48LE, + // SampleFormat::I64 => alsa::pcm::Format::S64LE, + SampleFormat::U8 => alsa::pcm::Format::U8, SampleFormat::U16 => alsa::pcm::Format::U16LE, + // SampleFormat::U24 => alsa::pcm::Format::U24LE, + SampleFormat::U32 => alsa::pcm::Format::U32LE, + // SampleFormat::U48 => alsa::pcm::Format::U48LE, + // SampleFormat::U64 => alsa::pcm::Format::U64LE, SampleFormat::F32 => alsa::pcm::Format::FloatLE, + SampleFormat::F64 => alsa::pcm::Format::Float64LE, + sample_format => { + return Err(BackendSpecificError { + description: format!( + "Sample format '{}' is not supported by this backend", + sample_format + ), + }) + } } }; diff --git a/src/host/asio/device.rs b/src/host/asio/device.rs index b4659b0..7fc5dd7 100644 --- a/src/host/asio/device.rs +++ b/src/host/asio/device.rs @@ -61,7 +61,7 @@ impl Device { ) -> Result { // Retrieve the default config for the total supported channels and supported sample // format. - let mut f = match self.default_input_config() { + let f = match self.default_input_config() { Err(_) => return Err(SupportedStreamConfigsError::DeviceNotAvailable), Ok(f) => f, }; @@ -98,7 +98,7 @@ impl Device { ) -> Result { // Retrieve the default config for the total supported channels and supported sample // format. - let mut f = match self.default_output_config() { + let f = match self.default_output_config() { Err(_) => return Err(SupportedStreamConfigsError::DeviceNotAvailable), Ok(f) => f, }; diff --git a/src/host/asio/mod.rs b/src/host/asio/mod.rs index ae99c3e..5a511d7 100644 --- a/src/host/asio/mod.rs +++ b/src/host/asio/mod.rs @@ -1,12 +1,12 @@ extern crate asio_sys as sys; extern crate parking_lot; +use crate::traits::{DeviceTrait, HostTrait, StreamTrait}; use crate::{ BuildStreamError, Data, DefaultStreamConfigError, DeviceNameError, DevicesError, InputCallbackInfo, OutputCallbackInfo, PauseStreamError, PlayStreamError, SampleFormat, StreamConfig, StreamError, SupportedStreamConfig, SupportedStreamConfigsError, }; -use traits::{DeviceTrait, HostTrait, StreamTrait}; pub use self::device::{Device, Devices, SupportedInputConfigs, SupportedOutputConfigs}; pub use self::stream::Stream; diff --git a/src/host/asio/stream.rs b/src/host/asio/stream.rs index cc7c6e7..a5da93f 100644 --- a/src/host/asio/stream.rs +++ b/src/host/asio/stream.rs @@ -6,24 +6,13 @@ use super::parking_lot::Mutex; use super::Device; use crate::{ BackendSpecificError, BufferSize, BuildStreamError, Data, InputCallbackInfo, - OutputCallbackInfo, PauseStreamError, PlayStreamError, Sample, SampleFormat, StreamConfig, + OutputCallbackInfo, PauseStreamError, PlayStreamError, SampleFormat, SizedSample, StreamConfig, StreamError, }; use std; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -/// Sample types whose constant silent value is known. -trait Silence { - const SILENCE: Self; -} - -/// Constraints on the ASIO sample types. -trait AsioSample: Clone + Copy + Silence + std::ops::Add { - fn to_cpal_sample(&self) -> T; - fn from_cpal_sample(_: &T) -> Self; -} - // Used to keep track of whether or not the current asio stream buffer requires // being silencing before summing audio. #[derive(Default)] @@ -103,7 +92,7 @@ impl Device { /// 1. Write from the ASIO buffer to the interleaved CPAL buffer. /// 2. Deliver the CPAL buffer to the user callback. - unsafe fn process_input_callback( + unsafe fn process_input_callback( data_callback: &mut D, interleaved: &mut [u8], asio_stream: &sys::AsioStream, @@ -111,27 +100,26 @@ impl Device { sample_rate: crate::SampleRate, from_endianness: F, ) where - A: AsioSample, - B: Sample, + A: SizedSample, D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, F: Fn(A) -> A, { // 1. Write the ASIO channels to the CPAL buffer. - let interleaved: &mut [B] = cast_slice_mut(interleaved); + let interleaved: &mut [A] = cast_slice_mut(interleaved); let n_frames = asio_stream.buffer_size as usize; let n_channels = interleaved.len() / n_frames; let buffer_index = asio_info.buffer_index as usize; for ch_ix in 0..n_channels { let asio_channel = asio_channel_slice::(asio_stream, buffer_index, ch_ix); for (frame, s_asio) in interleaved.chunks_mut(n_channels).zip(asio_channel) { - frame[ch_ix] = from_endianness(*s_asio).to_cpal_sample(); + frame[ch_ix] = from_endianness(*s_asio); } } // 2. Deliver the interleaved buffer to the callback. let data = interleaved.as_mut_ptr() as *mut (); let len = interleaved.len(); - let data = Data::from_parts(data, len, B::FORMAT); + let data = Data::from_parts(data, len, A::FORMAT); let callback = system_time_to_stream_instant(asio_info.system_time); let delay = frames_to_duration(n_frames, sample_rate); let capture = callback @@ -144,7 +132,7 @@ impl Device { match (&stream_type, sample_format) { (&sys::AsioSampleType::ASIOSTInt16LSB, SampleFormat::I16) => { - process_input_callback::( + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -154,7 +142,7 @@ impl Device { ); } (&sys::AsioSampleType::ASIOSTInt16MSB, SampleFormat::I16) => { - process_input_callback::( + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -168,7 +156,7 @@ impl Device { // trait for the `to_le` and `to_be` methods, but this does not support floats. (&sys::AsioSampleType::ASIOSTFloat32LSB, SampleFormat::F32) | (&sys::AsioSampleType::ASIOSTFloat32MSB, SampleFormat::F32) => { - process_input_callback::( + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -178,11 +166,8 @@ impl Device { ); } - // TODO: Add support for the following sample formats to CPAL and simplify the - // `process_output_callback` function above by removing the unnecessary sample - // conversion function. - (&sys::AsioSampleType::ASIOSTInt32LSB, SampleFormat::I16) => { - process_input_callback::( + (&sys::AsioSampleType::ASIOSTInt32LSB, SampleFormat::I32) => { + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -191,8 +176,8 @@ impl Device { from_le, ); } - (&sys::AsioSampleType::ASIOSTInt32MSB, SampleFormat::I16) => { - process_input_callback::( + (&sys::AsioSampleType::ASIOSTInt32MSB, SampleFormat::I32) => { + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -201,11 +186,12 @@ impl Device { from_be, ); } + // TODO: Handle endianness conversion for floats? We currently use the `PrimInt` // trait for the `to_le` and `to_be` methods, but this does not support floats. - (&sys::AsioSampleType::ASIOSTFloat64LSB, SampleFormat::F32) - | (&sys::AsioSampleType::ASIOSTFloat64MSB, SampleFormat::F32) => { - process_input_callback::( + (&sys::AsioSampleType::ASIOSTFloat64LSB, SampleFormat::F64) + | (&sys::AsioSampleType::ASIOSTFloat64MSB, SampleFormat::F64) => { + process_input_callback::( &mut data_callback, &mut interleaved, asio_stream, @@ -310,7 +296,7 @@ impl Device { /// 2. If required, silence the ASIO buffer. /// 3. Finally, write the interleaved data to the non-interleaved ASIO buffer, /// performing endianness conversions as necessary. - unsafe fn process_output_callback( + unsafe fn process_output_callback( data_callback: &mut D, interleaved: &mut [u8], silence_asio_buffer: bool, @@ -319,10 +305,9 @@ impl Device { sample_rate: crate::SampleRate, to_endianness: F, ) where - A: Sample, - B: AsioSample, + A: SizedSample + std::ops::Add, D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, - F: Fn(B) -> B, + F: Fn(A) -> A, { // 1. Render interleaved buffer from callback. let interleaved: &mut [A] = cast_slice_mut(interleaved); @@ -345,26 +330,26 @@ impl Device { if silence_asio_buffer { for ch_ix in 0..n_channels { let asio_channel = - asio_channel_slice_mut::(asio_stream, buffer_index, ch_ix); + asio_channel_slice_mut::(asio_stream, buffer_index, ch_ix); asio_channel .iter_mut() - .for_each(|s| *s = to_endianness(B::SILENCE)); + .for_each(|s| *s = to_endianness(A::EQUILIBRIUM)); } } // 3. Write interleaved samples to ASIO channels, one channel at a time. for ch_ix in 0..n_channels { let asio_channel = - asio_channel_slice_mut::(asio_stream, buffer_index, ch_ix); + asio_channel_slice_mut::(asio_stream, buffer_index, ch_ix); for (frame, s_asio) in interleaved.chunks(n_channels).zip(asio_channel) { - *s_asio = *s_asio + to_endianness(B::from_cpal_sample(&frame[ch_ix])); + *s_asio = *s_asio + to_endianness(A::from_sample(frame[ch_ix])); } } } match (sample_format, &stream_type) { (SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt16LSB) => { - process_output_callback::( + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -375,7 +360,7 @@ impl Device { ); } (SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt16MSB) => { - process_output_callback::( + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -390,7 +375,7 @@ impl Device { // 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::( + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -401,11 +386,8 @@ impl Device { ); } - // TODO: Add support for the following sample formats to CPAL and simplify the - // `process_output_callback` function above by removing the unnecessary sample - // conversion function. - (SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt32LSB) => { - process_output_callback::( + (SampleFormat::I32, &sys::AsioSampleType::ASIOSTInt32LSB) => { + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -415,8 +397,8 @@ impl Device { to_le, ); } - (SampleFormat::I16, &sys::AsioSampleType::ASIOSTInt32MSB) => { - process_output_callback::( + (SampleFormat::I32, &sys::AsioSampleType::ASIOSTInt32MSB) => { + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -426,11 +408,12 @@ impl Device { to_be, ); } + // TODO: Handle endianness conversion for floats? We currently use the `PrimInt` // trait for the `to_le` and `to_be` methods, but this does not support floats. - (SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat64LSB) - | (SampleFormat::F32, &sys::AsioSampleType::ASIOSTFloat64MSB) => { - process_output_callback::( + (SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64LSB) + | (SampleFormat::F64, &sys::AsioSampleType::ASIOSTFloat64MSB) => { + process_output_callback::( &mut data_callback, &mut interleaved, silence, @@ -566,62 +549,6 @@ impl Drop for Stream { } } -impl Silence for i16 { - const SILENCE: Self = 0; -} - -impl Silence for i32 { - const SILENCE: Self = 0; -} - -impl Silence for f32 { - const SILENCE: Self = 0.0; -} - -impl Silence for f64 { - const SILENCE: Self = 0.0; -} - -impl AsioSample for i16 { - fn to_cpal_sample(&self) -> T { - T::from(self) - } - fn from_cpal_sample(t: &T) -> Self { - Sample::from(t) - } -} - -impl AsioSample for i32 { - fn to_cpal_sample(&self) -> T { - let s = (*self >> 16) as i16; - s.to_cpal_sample() - } - fn from_cpal_sample(t: &T) -> Self { - let s = i16::from_cpal_sample(t); - (s as i32) << 16 - } -} - -impl AsioSample for f32 { - fn to_cpal_sample(&self) -> T { - T::from(self) - } - fn from_cpal_sample(t: &T) -> Self { - Sample::from(t) - } -} - -impl AsioSample for f64 { - fn to_cpal_sample(&self) -> T { - let f = *self as f32; - f.to_cpal_sample() - } - fn from_cpal_sample(t: &T) -> Self { - let f = f32::from_cpal_sample(t); - f as f64 - } -} - fn asio_ns_to_double(val: sys::bindings::asio_import::ASIOTimeStamp) -> f64 { let two_raised_to_32 = 4294967296.0; val.lo as f64 + val.hi as f64 * two_raised_to_32 @@ -657,7 +584,7 @@ fn check_config( let StreamConfig { channels, sample_rate, - buffer_size, + buffer_size: _, } = config; // Try and set the sample rate to what the user selected. let sample_rate = sample_rate.0.into(); @@ -676,7 +603,7 @@ fn check_config( // unsigned formats are not supported by asio match sample_format { SampleFormat::I16 | SampleFormat::F32 => (), - SampleFormat::U16 => return Err(BuildStreamError::StreamConfigNotSupported), + _ => return Err(BuildStreamError::StreamConfigNotSupported), } if *channels > num_asio_channels { return Err(BuildStreamError::StreamConfigNotSupported); diff --git a/src/host/oboe/input_callback.rs b/src/host/oboe/input_callback.rs index be19ed9..0dfb9c8 100644 --- a/src/host/oboe/input_callback.rs +++ b/src/host/oboe/input_callback.rs @@ -4,7 +4,7 @@ use std::time::Instant; extern crate oboe; use super::convert::{stream_instant, to_stream_instant}; -use crate::{Data, InputCallbackInfo, InputStreamTimestamp, Sample, StreamError}; +use crate::{Data, InputCallbackInfo, InputStreamTimestamp, SizedSample, StreamError}; pub struct CpalInputCallback { data_cb: Box, @@ -42,7 +42,7 @@ impl CpalInputCallback { } } -impl oboe::AudioInputCallback for CpalInputCallback +impl oboe::AudioInputCallback for CpalInputCallback where (T, C): oboe::IsFrameType, { diff --git a/src/host/oboe/mod.rs b/src/host/oboe/mod.rs index 45441f9..a25aca1 100644 --- a/src/host/oboe/mod.rs +++ b/src/host/oboe/mod.rs @@ -9,7 +9,7 @@ use crate::traits::{DeviceTrait, HostTrait, StreamTrait}; use crate::{ BackendSpecificError, BufferSize, BuildStreamError, Data, DefaultStreamConfigError, DeviceNameError, DevicesError, InputCallbackInfo, OutputCallbackInfo, PauseStreamError, - PlayStreamError, Sample, SampleFormat, SampleRate, StreamConfig, StreamError, + PlayStreamError, SampleFormat, SampleRate, SizedSample, StreamConfig, StreamError, SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange, SupportedStreamConfigsError, }; @@ -225,7 +225,7 @@ fn build_input_stream( builder: oboe::AudioStreamBuilder, ) -> Result where - T: Sample + oboe::IsFormat + Send + 'static, + T: SizedSample + oboe::IsFormat + Send + 'static, C: oboe::IsChannelCount + Send + 'static, (T, C): oboe::IsFrameType, D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, @@ -249,7 +249,7 @@ fn build_output_stream( builder: oboe::AudioStreamBuilder, ) -> Result where - T: Sample + oboe::IsFormat + Send + 'static, + T: SizedSample + oboe::IsFormat + Send + 'static, C: oboe::IsChannelCount + Send + 'static, (T, C): oboe::IsFrameType, D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, @@ -385,8 +385,8 @@ impl DeviceTrait for Device { .into()) } } - SampleFormat::U16 => Err(BackendSpecificError { - description: "U16 format is not supported on Android.".to_owned(), + sample_format => Err(BackendSpecificError { + description: format!("{} format is not supported on Android.", sample_format), } .into()), } @@ -458,8 +458,8 @@ impl DeviceTrait for Device { .into()) } } - SampleFormat::U16 => Err(BackendSpecificError { - description: "U16 format is not supported on Android.".to_owned(), + sample_format => Err(BackendSpecificError { + description: format!("{} format is not supported on Android.", sample_format), } .into()), } diff --git a/src/host/oboe/output_callback.rs b/src/host/oboe/output_callback.rs index a3ee9e0..6ab6960 100644 --- a/src/host/oboe/output_callback.rs +++ b/src/host/oboe/output_callback.rs @@ -4,7 +4,7 @@ use std::time::Instant; extern crate oboe; use super::convert::{stream_instant, to_stream_instant}; -use crate::{Data, OutputCallbackInfo, OutputStreamTimestamp, Sample, StreamError}; +use crate::{Data, OutputCallbackInfo, OutputStreamTimestamp, SizedSample, StreamError}; pub struct CpalOutputCallback { data_cb: Box, @@ -42,7 +42,7 @@ impl CpalOutputCallback { } } -impl oboe::AudioOutputCallback for CpalOutputCallback +impl oboe::AudioOutputCallback for CpalOutputCallback where (T, C): oboe::IsFrameType, { diff --git a/src/host/wasapi/device.rs b/src/host/wasapi/device.rs index 3bad3c3..8095d91 100644 --- a/src/host/wasapi/device.rs +++ b/src/host/wasapi/device.rs @@ -6,7 +6,6 @@ use crate::{ SupportedStreamConfigsError, COMMON_SAMPLE_RATES, }; use once_cell::sync::Lazy; -use std; use std::ffi::OsString; use std::fmt; use std::mem; @@ -453,10 +452,7 @@ impl Device { .map_err(windows_err_to_cpal_err::)?; // If the default format can't succeed we have no hope of finding other formats. - assert_eq!( - is_format_supported(client, default_waveformatex_ptr.0)?, - true - ); + assert!(is_format_supported(client, default_waveformatex_ptr.0)?); // Copy the format to use as a test format (as to avoid mutating the original format). let mut test_format = { @@ -496,7 +492,7 @@ impl Device { // TODO: Test the different sample formats? // Create the supported formats. - let format = match format_from_waveformatex_ptr(default_waveformatex_ptr.0, &client) { + let format = match format_from_waveformatex_ptr(default_waveformatex_ptr.0, client) { Some(fmt) => fmt, None => { let description = @@ -509,11 +505,11 @@ impl Device { let mut supported_formats = Vec::with_capacity(supported_sample_rates.len()); for rate in supported_sample_rates { supported_formats.push(SupportedStreamConfigRange { - channels: format.channels.clone(), + channels: format.channels, min_sample_rate: SampleRate(rate as _), max_sample_rate: SampleRate(rate as _), buffer_size: format.buffer_size.clone(), - sample_format: format.sample_format.clone(), + sample_format: format.sample_format, }) } Ok(supported_formats.into_iter()) @@ -1015,7 +1011,7 @@ fn config_to_waveformatextensible( let format_tag = match sample_format { SampleFormat::I16 => Audio::WAVE_FORMAT_PCM, SampleFormat::F32 => KernelStreaming::WAVE_FORMAT_EXTENSIBLE, - SampleFormat::U16 => return None, + _ => return None, } as u16; let channels = config.channels; let sample_rate = config.sample_rate.0; @@ -1030,7 +1026,7 @@ fn config_to_waveformatextensible( let ex_size = mem::size_of::(); (extensible_size - ex_size) as u16 } - SampleFormat::U16 => return None, + _ => return None, }; let waveformatex = Audio::WAVEFORMATEX { wFormatTag: format_tag, @@ -1048,7 +1044,7 @@ fn config_to_waveformatextensible( let sub_format = match sample_format { SampleFormat::I16 => KernelStreaming::KSDATAFORMAT_SUBTYPE_PCM, SampleFormat::F32 => Multimedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, - SampleFormat::U16 => return None, + _ => return None, }; let waveformatextensible = Audio::WAVEFORMATEXTENSIBLE { Format: waveformatex, diff --git a/src/host/wasapi/stream.rs b/src/host/wasapi/stream.rs index b980bf2..aee6c64 100644 --- a/src/host/wasapi/stream.rs +++ b/src/host/wasapi/stream.rs @@ -287,7 +287,7 @@ fn run_input( _ => unreachable!(), }; match process_input( - &mut run_ctxt.stream, + &run_ctxt.stream, capture_client, data_callback, error_callback, @@ -314,7 +314,7 @@ fn run_output( _ => unreachable!(), }; match process_output( - &mut run_ctxt.stream, + &run_ctxt.stream, render_client, data_callback, error_callback, @@ -439,7 +439,7 @@ fn process_output( error_callback: &mut dyn FnMut(StreamError), ) -> ControlFlow { // The number of frames available for writing. - let frames_available = match get_available_frames(&stream) { + let frames_available = match get_available_frames(stream) { Ok(0) => return ControlFlow::Continue, // TODO: Can this happen? Ok(n) => n, Err(err) => { diff --git a/src/lib.rs b/src/lib.rs index 98da3e0..1b41c76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,7 +88,7 @@ //! In this example, we simply fill the given output buffer with silence. //! //! ```no_run -//! use cpal::{Data, Sample, SampleFormat}; +//! use cpal::{Data, Sample, SampleFormat, FromSample}; //! use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; //! # let host = cpal::default_host(); //! # let device = host.default_output_device().unwrap(); @@ -100,11 +100,12 @@ //! SampleFormat::F32 => device.build_output_stream(&config, write_silence::, err_fn), //! SampleFormat::I16 => device.build_output_stream(&config, write_silence::, err_fn), //! SampleFormat::U16 => device.build_output_stream(&config, write_silence::, err_fn), +//! sample_format => panic!("Unsupported sample format '{sample_format}'") //! }.unwrap(); //! //! fn write_silence(data: &mut [T], _: &cpal::OutputCallbackInfo) { //! for sample in data.iter_mut() { -//! *sample = Sample::from(&0.0); +//! *sample = Sample::EQUILIBRIUM; //! } //! } //! ``` @@ -154,7 +155,7 @@ pub use platform::{ available_hosts, default_host, host_from_id, Device, Devices, Host, HostId, Stream, SupportedInputConfigs, SupportedOutputConfigs, ALL_HOSTS, }; -pub use samples_formats::{Sample, SampleFormat}; +pub use samples_formats::{FromSample, Sample, SampleFormat, SizedSample, I24, I48, U24, U48}; use std::convert::TryInto; use std::ops::{Div, Mul}; use std::time::Duration; @@ -516,7 +517,7 @@ impl Data { /// Returns `None` if the sample type does not match the expected sample format. pub fn as_slice(&self) -> Option<&[T]> where - T: Sample, + T: SizedSample, { if T::FORMAT == self.sample_format { // The safety of this block relies on correct construction of the `Data` instance. See @@ -532,7 +533,7 @@ impl Data { /// Returns `None` if the sample type does not match the expected sample format. pub fn as_slice_mut(&mut self) -> Option<&mut [T]> where - T: Sample, + T: SizedSample, { if T::FORMAT == self.sample_format { // The safety of this block relies on correct construction of the `Data` instance. See @@ -762,7 +763,7 @@ impl From for StreamConfig { // // If a rate you desire is missing from this list, feel free to add it! #[cfg(target_os = "windows")] -const COMMON_SAMPLE_RATES: &'static [SampleRate] = &[ +const COMMON_SAMPLE_RATES: &[SampleRate] = &[ SampleRate(5512), SampleRate(8000), SampleRate(11025), diff --git a/src/samples_formats.rs b/src/samples_formats.rs index 27d48a4..51b2b84 100644 --- a/src/samples_formats.rs +++ b/src/samples_formats.rs @@ -1,206 +1,164 @@ -use std::mem; +use std::{fmt::Display, mem}; + +pub use dasp_sample::{FromSample, Sample, I24, I48, U24, U48}; /// Format that each sample has. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] pub enum SampleFormat { - /// The value 0 corresponds to 0. + /// `i8` with a valid range of 'u8::MIN..=u8::MAX' with `0` being the origin + I8, + + /// `i16` with a valid range of 'u16::MIN..=u16::MAX' with `0` being the origin I16, - /// The value 0 corresponds to 32768. + + // /// `I24` with a valid range of '-(1 << 23)..(1 << 23)' with `0` being the origin + // I24, + /// `i32` with a valid range of 'u32::MIN..=u32::MAX' with `0` being the origin + I32, + + // /// `I24` with a valid range of '-(1 << 47)..(1 << 47)' with `0` being the origin + // I48, + /// `i64` with a valid range of 'u64::MIN..=u64::MAX' with `0` being the origin + I64, + + /// `u8` with a valid range of 'u8::MIN..=u8::MAX' with `1 << 7 == 128` being the origin + U8, + + /// `u16` with a valid range of 'u16::MIN..=u16::MAX' with `1 << 15 == 32768` being the origin U16, - /// The boundaries are (-1.0, 1.0). + + // /// `U24` with a valid range of '0..16777216' with `1 << 23 == 8388608` being the origin + // U24, + /// `u32` with a valid range of 'u32::MIN..=u32::MAX' with `1 << 31` being the origin + U32, + + // /// `U48` with a valid range of '0..(1 << 48)' with `1 << 47` being the origin + // U48, + /// `u64` with a valid range of 'u64::MIN..=u64::MAX' with `1 << 63` being the origin + U64, + + /// `f32` with a valid range of `-1.0..1.0` with `0.0` being the origin F32, + + /// `f64` with a valid range of -1.0..1.0 with 0.0 being the origin + F64, } impl SampleFormat { /// Returns the size in bytes of a sample of this format. #[inline] + #[must_use] pub fn sample_size(&self) -> usize { match *self { - SampleFormat::I16 => mem::size_of::(), - SampleFormat::U16 => mem::size_of::(), + SampleFormat::I8 | SampleFormat::U8 => mem::size_of::(), + SampleFormat::I16 | SampleFormat::U16 => mem::size_of::(), + // SampleFormat::I24 | SampleFormat::U24 => 3, + SampleFormat::I32 | SampleFormat::U32 => mem::size_of::(), + // SampleFormat::I48 | SampleFormat::U48 => 6, + SampleFormat::I64 | SampleFormat::U64 => mem::size_of::(), SampleFormat::F32 => mem::size_of::(), + SampleFormat::F64 => mem::size_of::(), } } + + #[inline] + #[must_use] + pub fn is_int(&self) -> bool { + //matches!(*self, SampleFormat::I8 | SampleFormat::I16 | SampleFormat::I24 | SampleFormat::I32 | SampleFormat::I48 | SampleFormat::I64) + matches!( + *self, + SampleFormat::I8 | SampleFormat::I16 | SampleFormat::I32 | SampleFormat::I64 + ) + } + + #[inline] + #[must_use] + pub fn is_uint(&self) -> bool { + //matches!(*self, SampleFormat::U8 | SampleFormat::U16 | SampleFormat::U24 | SampleFormat::U32 | SampleFormat::U48 | SampleFormat::U64) + matches!( + *self, + SampleFormat::U8 | SampleFormat::U16 | SampleFormat::U32 | SampleFormat::U64 + ) + } + + #[inline] + #[must_use] + pub fn is_float(&self) -> bool { + matches!(*self, SampleFormat::F32 | SampleFormat::F64) + } } -/// Trait for containers that contain PCM data. -pub unsafe trait Sample: Copy + Clone { - /// The `SampleFormat` corresponding to this data type. +impl Display for SampleFormat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match *self { + SampleFormat::I8 => "i8", + SampleFormat::I16 => "i16", + // SampleFormat::I24 => "i24", + SampleFormat::I32 => "i32", + // SampleFormat::I48 => "i48", + SampleFormat::I64 => "i64", + SampleFormat::U8 => "u8", + SampleFormat::U16 => "u16", + // SampleFormat::U24 => "u24", + SampleFormat::U32 => "u32", + // SampleFormat::U48 => "u48", + SampleFormat::U64 => "u64", + SampleFormat::F32 => "f32", + SampleFormat::F64 => "f64", + } + .fmt(f) + } +} + +pub trait SizedSample: Sample { const FORMAT: SampleFormat; - - /// Turns the sample into its equivalent as a floating-point. - fn to_f32(&self) -> f32; - /// Converts this sample into a standard i16 sample. - fn to_i16(&self) -> i16; - /// Converts this sample into a standard u16 sample. - fn to_u16(&self) -> u16; - - /// Converts any sample type to this one by calling `to_i16`, `to_u16` or `to_f32`. - fn from(s: &S) -> Self - where - S: Sample; } -unsafe impl Sample for u16 { - const FORMAT: SampleFormat = SampleFormat::U16; - - #[inline] - fn to_f32(&self) -> f32 { - self.to_i16().to_f32() - } - - #[inline] - fn to_i16(&self) -> i16 { - (*self as i16).wrapping_add(i16::MIN) - } - - #[inline] - fn to_u16(&self) -> u16 { - *self - } - - #[inline] - fn from(sample: &S) -> Self - where - S: Sample, - { - sample.to_u16() - } +impl SizedSample for i8 { + const FORMAT: SampleFormat = SampleFormat::I8; } -unsafe impl Sample for i16 { +impl SizedSample for i16 { const FORMAT: SampleFormat = SampleFormat::I16; - - #[inline] - fn to_f32(&self) -> f32 { - if *self < 0 { - *self as f32 / -(i16::MIN as f32) - } else { - *self as f32 / i16::MAX as f32 - } - } - - #[inline] - fn to_i16(&self) -> i16 { - *self - } - - #[inline] - fn to_u16(&self) -> u16 { - self.wrapping_add(i16::MIN) as u16 - } - - #[inline] - fn from(sample: &S) -> Self - where - S: Sample, - { - sample.to_i16() - } } -const F32_TO_16BIT_INT_MULTIPLIER: f32 = u16::MAX as f32 * 0.5; -unsafe impl Sample for f32 { + +// impl SizedSample for I24 { const FORMAT: SampleFormat = SampleFormat::I24; } + +impl SizedSample for i32 { + const FORMAT: SampleFormat = SampleFormat::I32; +} + +// impl SizedSample for I48 { const FORMAT: SampleFormat = SampleFormat::I48; } + +impl SizedSample for i64 { + const FORMAT: SampleFormat = SampleFormat::I64; +} + +impl SizedSample for u8 { + const FORMAT: SampleFormat = SampleFormat::U8; +} + +impl SizedSample for u16 { + const FORMAT: SampleFormat = SampleFormat::U16; +} + +// impl SizedSample for U24 { const FORMAT: SampleFormat = SampleFormat::U24; } + +impl SizedSample for u32 { + const FORMAT: SampleFormat = SampleFormat::U32; +} + +// impl SizedSample for U48 { const FORMAT: SampleFormat = SampleFormat::U48; } + +impl SizedSample for u64 { + const FORMAT: SampleFormat = SampleFormat::U64; +} + +impl SizedSample for f32 { const FORMAT: SampleFormat = SampleFormat::F32; - - #[inline] - fn to_f32(&self) -> f32 { - *self - } - - #[inline] - fn to_i16(&self) -> i16 { - if *self >= 0.0 { - (*self * i16::MAX as f32) as i16 - } else { - (-*self * i16::MIN as f32) as i16 - } - } - - #[inline] - fn to_u16(&self) -> u16 { - self.mul_add(F32_TO_16BIT_INT_MULTIPLIER, F32_TO_16BIT_INT_MULTIPLIER) - .round() as u16 - } - - #[inline] - fn from(sample: &S) -> Self - where - S: Sample, - { - sample.to_f32() - } } -#[cfg(test)] -mod test { - use super::Sample; - - #[test] - fn i16_to_i16() { - assert_eq!(0i16.to_i16(), 0); - assert_eq!((-467i16).to_i16(), -467); - assert_eq!(32767i16.to_i16(), 32767); - assert_eq!((-32768i16).to_i16(), -32768); - } - - #[test] - fn i16_to_u16() { - assert_eq!(0i16.to_u16(), 32768); - assert_eq!((-16384i16).to_u16(), 16384); - assert_eq!(32767i16.to_u16(), 65535); - assert_eq!((-32768i16).to_u16(), 0); - } - - #[test] - fn i16_to_f32() { - assert_eq!(0i16.to_f32(), 0.0); - assert_eq!((-16384i16).to_f32(), -0.5); - assert_eq!(32767i16.to_f32(), 1.0); - assert_eq!((-32768i16).to_f32(), -1.0); - } - - #[test] - fn u16_to_i16() { - assert_eq!(32768u16.to_i16(), 0); - assert_eq!(16384u16.to_i16(), -16384); - assert_eq!(65535u16.to_i16(), 32767); - assert_eq!(0u16.to_i16(), -32768); - } - - #[test] - fn u16_to_u16() { - assert_eq!(0u16.to_u16(), 0); - assert_eq!(467u16.to_u16(), 467); - assert_eq!(32767u16.to_u16(), 32767); - assert_eq!(65535u16.to_u16(), 65535); - } - - #[test] - fn u16_to_f32() { - assert_eq!(0u16.to_f32(), -1.0); - assert_eq!(32768u16.to_f32(), 0.0); - assert_eq!(65535u16.to_f32(), 1.0); - } - - #[test] - fn f32_to_i16() { - assert_eq!(0.0f32.to_i16(), 0); - assert_eq!((-0.5f32).to_i16(), i16::MIN / 2); - assert_eq!(1.0f32.to_i16(), i16::MAX); - assert_eq!((-1.0f32).to_i16(), i16::MIN); - } - - #[test] - fn f32_to_u16() { - assert_eq!((-1.0f32).to_u16(), 0); - assert_eq!(0.0f32.to_u16(), 32768); - assert_eq!(1.0f32.to_u16(), 65535); - } - - #[test] - fn f32_to_f32() { - assert_eq!(0.1f32.to_f32(), 0.1); - assert_eq!((-0.7f32).to_f32(), -0.7); - assert_eq!(1.0f32.to_f32(), 1.0); - } +impl SizedSample for f64 { + const FORMAT: SampleFormat = SampleFormat::F64; } diff --git a/src/traits.rs b/src/traits.rs index 6eefbe2..4ea6edf 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -3,7 +3,7 @@ use crate::{ BuildStreamError, Data, DefaultStreamConfigError, DeviceNameError, DevicesError, InputCallbackInfo, InputDevices, OutputCallbackInfo, OutputDevices, PauseStreamError, - PlayStreamError, Sample, SampleFormat, StreamConfig, StreamError, SupportedStreamConfig, + PlayStreamError, SampleFormat, SizedSample, StreamConfig, StreamError, SupportedStreamConfig, SupportedStreamConfigRange, SupportedStreamConfigsError, }; @@ -122,7 +122,7 @@ pub trait DeviceTrait { error_callback: E, ) -> Result where - T: Sample, + T: SizedSample, D: FnMut(&[T], &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static, { @@ -148,7 +148,7 @@ pub trait DeviceTrait { error_callback: E, ) -> Result where - T: Sample, + T: SizedSample, D: FnMut(&mut [T], &OutputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static, {