From 2bc9f859708f737d3914fbc693a3744f797d1942 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Thu, 8 Nov 2018 12:54:32 +0700 Subject: [PATCH] Update supported_formats methods for all sample rates and channel combos Closes #32. --- src/platform/windows/asio/device.rs | 69 ++++++++++++++++------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/src/platform/windows/asio/device.rs b/src/platform/windows/asio/device.rs index c8c07ca..e31d9de 100644 --- a/src/platform/windows/asio/device.rs +++ b/src/platform/windows/asio/device.rs @@ -50,21 +50,26 @@ impl Device { pub fn supported_input_formats( &self, ) -> Result { - match self.default_input_format() { - Ok(f) => { - // Can this device support both 44100 and 48000 - let supported_formats: Vec = [44100, 48000] - .into_iter() - .filter(|rate| self.drivers.can_sample_rate(**rate as u32)) - .map(|rate| { - let mut format = f.clone(); - format.sample_rate = SampleRate(*rate); - SupportedFormat::from(format) - }).collect(); - Ok(supported_formats.into_iter()) - }, - Err(_) => Err(FormatsEnumerationError::DeviceNotAvailable), + // Retrieve the default format for the total supported channels and supported sample + // format. + let mut f = match self.default_input_format() { + Err(_) => return Err(FormatsEnumerationError::DeviceNotAvailable), + Ok(f) => f, + }; + + // Collect a format for every combination of supported sample rate and number of channels. + let mut supported_formats = vec![]; + for &rate in ::COMMON_SAMPLE_RATES { + if !self.drivers.can_sample_rate(rate.0 as u32) { + continue; + } + for channels in 1..f.channels + 1 { + f.channels = channels; + f.sample_rate = rate; + supported_formats.push(SupportedFormat::from(f.clone())); + } } + Ok(supported_formats.into_iter()) } /// Gets the supported output formats. @@ -73,22 +78,26 @@ impl Device { pub fn supported_output_formats( &self, ) -> Result { - match self.default_output_format() { - Ok(f) => { - // Can this device support both 44100 and 48000 - let supported_formats: Vec = [44100, 48000] - .into_iter() - .filter(|rate| self.drivers.can_sample_rate(**rate as u32)) - .map(|rate| { - let mut format = f.clone(); - format.sample_rate = SampleRate(*rate); - SupportedFormat::from(format) - }).collect(); - //Ok(vec![SupportedFormat::from(f)].into_iter()) - Ok(supported_formats.into_iter()) - }, - Err(_) => Err(FormatsEnumerationError::DeviceNotAvailable), + // Retrieve the default format for the total supported channels and supported sample + // format. + let mut f = match self.default_output_format() { + Err(_) => return Err(FormatsEnumerationError::DeviceNotAvailable), + Ok(f) => f, + }; + + // Collect a format for every combination of supported sample rate and number of channels. + let mut supported_formats = vec![]; + for &rate in ::COMMON_SAMPLE_RATES { + if !self.drivers.can_sample_rate(rate.0 as u32) { + continue; + } + for channels in 1..f.channels + 1 { + f.channels = channels; + f.sample_rate = rate; + supported_formats.push(SupportedFormat::from(f.clone())); + } } + Ok(supported_formats.into_iter()) } /// Returns the default input format @@ -192,4 +201,4 @@ fn online_devices() -> Vec { .into_iter() .filter(|name| sys::Drivers::load(&name).is_ok()) .collect() -} \ No newline at end of file +}