fn supported_formats(&self) -> Result<SupportedInputConfigs, SupportedStreamConfigsError> {
        // initializing COM because we call `CoTaskMemFree` to release the format.
        com::com_initialized();

        // Retrieve the `IAudioClient`.
        let lock = match self.ensure_future_audio_client() {
            Ok(lock) => lock,
            Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
                return Err(SupportedStreamConfigsError::DeviceNotAvailable)
            }
            Err(e) => {
                let description = format!("{}", e);
                let err = BackendSpecificError { description };
                return Err(err.into());
            }
        };
        let client = lock.unwrap().0;

        unsafe {
            // Retrieve the pointer to the default WAVEFORMATEX.
            let mut default_waveformatex_ptr = WaveFormatExPtr(ptr::null_mut());
            match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
                Ok(()) => (),
                Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
                    return Err(SupportedStreamConfigsError::DeviceNotAvailable);
                }
                Err(e) => {
                    let description = format!("{}", e);
                    let err = BackendSpecificError { description };
                    return Err(err.into());
                }
            };

            // 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
            );

            // Copy the format to use as a test format (as to avoid mutating the original format).
            let mut test_format = {
                match WaveFormat::copy_from_waveformatex_ptr(default_waveformatex_ptr.0) {
                    Some(f) => f,
                    // If the format is neither EX nor EXTENSIBLE we don't know how to work with it.
                    None => return Ok(vec![].into_iter()),
                }
            };

            // Begin testing common sample rates.
            //
            // NOTE: We should really be testing for whole ranges here, but it is infeasible to
            // test every sample rate up to the overflow limit as the `IsFormatSupported` method is
            // quite slow.
            let mut supported_sample_rates: Vec<u32> = Vec::new();
            for &rate in COMMON_SAMPLE_RATES {
                let rate = rate.0 as DWORD;
                test_format.nSamplesPerSec = rate;
                test_format.nAvgBytesPerSec =
                    rate * u32::from((*default_waveformatex_ptr.0).nBlockAlign);
                if is_format_supported(client, test_format.as_ptr())? {
                    supported_sample_rates.push(rate);
                }
            }

            // If the common rates don't include the default one, add the default.
            let default_sr = (*default_waveformatex_ptr.0).nSamplesPerSec as _;
            if !supported_sample_rates.iter().any(|&r| r == default_sr) {
                supported_sample_rates.push(default_sr);
            }

            // Reset the sample rate on the test format now that we're done.
            test_format.nSamplesPerSec = (*default_waveformatex_ptr.0).nSamplesPerSec;
            test_format.nAvgBytesPerSec = (*default_waveformatex_ptr.0).nAvgBytesPerSec;

            // TODO: Test the different sample formats?

            // Create the supported formats.
            let format = match format_from_waveformatex_ptr(default_waveformatex_ptr.0) {
                Some(fmt) => fmt,
                None => {
                    let description =
                        "could not create a `cpal::SupportedStreamConfig` from a `WAVEFORMATEX`"
                            .to_string();
                    let err = BackendSpecificError { description };
                    return Err(err.into());
                }
            };
            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(),
                    min_sample_rate: SampleRate(rate as _),
                    max_sample_rate: SampleRate(rate as _),
                    buffer_size: format.buffer_size.clone(),
                    sample_format: format.sample_format.clone(),
                })
            }
            Ok(supported_formats.into_iter())
        }
    }
// There is no way to query the list of all formats that are supported by the
// audio processor, so instead we just trial some commonly supported formats.
//
// Common formats are trialed by first getting the default format (returned via
// `GetMixFormat`) and then mutating that format with common sample rates and
// querying them via `IsFormatSupported`.
//
// When calling `IsFormatSupported` with the shared-mode audio engine, only the default
// number of channels seems to be supported. Any, more or less returns an invalid
// parameter error. Thus, we just assume that the default number of channels is the only
// number supported.
There is no way to query the list of all formats that are supported by the
audio processor, so instead we just trial some commonly supported formats.

Common formats are trialed by first getting the default format (returned via
`GetMixFormat`) and then mutating that format with common sample rates and
querying them via `IsFormatSupported`.

When calling `IsFormatSupported` with the shared-mode audio engine, only the default
number of channels seems to be supported. Any, more or less returns an invalid
parameter error. Thus, we just assume that the default number of channels is the only
number supported.
There is no way to query the list of all formats that are supported by the audio processor so instead we just trial some commonly supported formats . Common formats are trialed by first getting the default format ( returned via ` GetMixFormat ` ) and then mutating that format with common sample rates and querying them via ` IsFormatSupported ` . When calling ` IsFormatSupported ` with the shared - mode audio engine only the default number of channels seems to be supported . Any more or less returns an invalid parameter error . Thus we just assume that the default number of channels is the only number supported .
fn supported_formats(&self) -> Result<SupportedInputConfigs, SupportedStreamConfigsError> {
        // initializing COM because we call `CoTaskMemFree` to release the format.
        com::com_initialized();
        // Retrieve the `IAudioClient`.
        let lock = match self.ensure_future_audio_client() {
            Ok(lock) => lock,
            Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
                return Err(SupportedStreamConfigsError::DeviceNotAvailable)
            }
            Err(e) => {
                let description = format!("{}", e);
                let err = BackendSpecificError { description };
                return Err(err.into());
            }
        };
        let client = lock.unwrap().0;
        unsafe {
            // Retrieve the pointer to the default WAVEFORMATEX.
            let mut default_waveformatex_ptr = WaveFormatExPtr(ptr::null_mut());
            match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
                Ok(()) => (),
                Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
                    return Err(SupportedStreamConfigsError::DeviceNotAvailable);
                }
                Err(e) => {
                    let description = format!("{}", e);
                    let err = BackendSpecificError { description };
                    return Err(err.into());
                }
            };
            // 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
            );
            // Copy the format to use as a test format (as to avoid mutating the original format).
            let mut test_format = {
                match WaveFormat::copy_from_waveformatex_ptr(default_waveformatex_ptr.0) {
                    Some(f) => f,
                    // If the format is neither EX nor EXTENSIBLE we don't know how to work with it.
                    None => return Ok(vec![].into_iter()),
                }
            };
            // Begin testing common sample rates.
            //
            // NOTE: We should really be testing for whole ranges here, but it is infeasible to
            // test every sample rate up to the overflow limit as the `IsFormatSupported` method is
            // quite slow.
            let mut supported_sample_rates: Vec<u32> = Vec::new();
            for &rate in COMMON_SAMPLE_RATES {
                let rate = rate.0 as DWORD;
                test_format.nSamplesPerSec = rate;
                test_format.nAvgBytesPerSec =
                    rate * u32::from((*default_waveformatex_ptr.0).nBlockAlign);
                if is_format_supported(client, test_format.as_ptr())? {
                    supported_sample_rates.push(rate);
                }
            }
            // If the common rates don't include the default one, add the default.
            let default_sr = (*default_waveformatex_ptr.0).nSamplesPerSec as _;
            if !supported_sample_rates.iter().any(|&r| r == default_sr) {
                supported_sample_rates.push(default_sr);
            }
            // Reset the sample rate on the test format now that we're done.
            test_format.nSamplesPerSec = (*default_waveformatex_ptr.0).nSamplesPerSec;
            test_format.nAvgBytesPerSec = (*default_waveformatex_ptr.0).nAvgBytesPerSec;
            // TODO: Test the different sample formats?
            // Create the supported formats.
            let format = match format_from_waveformatex_ptr(default_waveformatex_ptr.0) {
                Some(fmt) => fmt,
                None => {
                    let description =
                        "could not create a `cpal::SupportedStreamConfig` from a `WAVEFORMATEX`"
                            .to_string();
                    let err = BackendSpecificError { description };
                    return Err(err.into());
                }
            };
            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(),
                    min_sample_rate: SampleRate(rate as _),
                    max_sample_rate: SampleRate(rate as _),
                    buffer_size: format.buffer_size.clone(),
                    sample_format: format.sample_format.clone(),
                })
            }
            Ok(supported_formats.into_iter())
        }
    }
fn supported_formats ( & self ) -> Result < SupportedInputConfigs , SupportedStreamConfigsError > { // initializing COM because we call `CoTaskMemFree` to release the format. com :: com_initialized ( ) ; // Retrieve the `IAudioClient`. let lock = match self . ensure_future_audio_client ( ) { Ok ( lock ) => lock , Err ( ref e ) if e . raw_os_error ( ) == Some ( AUDCLNT_E_DEVICE_INVALIDATED ) => { return Err ( SupportedStreamConfigsError :: DeviceNotAvailable ) } Err ( e ) => { let description = format ! ( " " e ) ; let err = BackendSpecificError { description } ; return Err ( err . into ( ) ) ; } } ; let client = lock . unwrap ( ) . 0 ; unsafe { // Retrieve the pointer to the default WAVEFORMATEX. let mut default_waveformatex_ptr = WaveFormatExPtr ( ptr :: null_mut ( ) ) ; match check_result ( ( * client ) . GetMixFormat ( & mut default_waveformatex_ptr . 0 ) ) { Ok ( ( ) ) => ( ) , Err ( ref e ) if e . raw_os_error ( ) == Some ( AUDCLNT_E_DEVICE_INVALIDATED ) => { return Err ( SupportedStreamConfigsError :: DeviceNotAvailable ) ; } Err ( e ) => { let description = format ! ( " " e ) ; let err = BackendSpecificError { description } ; return Err ( err . into ( ) ) ; } } ; // 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 ) ; // Copy the format to use as a test format (as to avoid mutating the original format). let mut test_format = { match WaveFormat :: copy_from_waveformatex_ptr ( default_waveformatex_ptr . 0 ) { Some ( f ) => f , // If the format is neither EX nor EXTENSIBLE we don't know how to work with it. None => return Ok ( vec ! [ ] . into_iter ( ) ) , } } ; // Begin testing common sample rates. // // NOTE: We should really be testing for whole ranges here, but it is infeasible to // test every sample rate up to the overflow limit as the `IsFormatSupported` method is // quite slow. let mut supported_sample_rates : Vec < u32 > = Vec :: new ( ) ; for & rate in COMMON_SAMPLE_RATES { let rate = rate . 0 as DWORD ; test_format . nSamplesPerSec = rate ; test_format . nAvgBytesPerSec = rate * u32 :: from ( ( * default_waveformatex_ptr . 0 ) . nBlockAlign ) ; if is_format_supported ( client , test_format . as_ptr ( ) ) ? { supported_sample_rates . push ( rate ) ; } } // If the common rates don't include the default one, add the default. let default_sr = ( * default_waveformatex_ptr . 0 ) . nSamplesPerSec as _ ; if ! supported_sample_rates . iter ( ) . any ( | & r | r == default_sr ) { supported_sample_rates . push ( default_sr ) ; } // Reset the sample rate on the test format now that we're done. test_format . nSamplesPerSec = ( * default_waveformatex_ptr . 0 ) . nSamplesPerSec ; test_format . nAvgBytesPerSec = ( * default_waveformatex_ptr . 0 ) . nAvgBytesPerSec ; // TODO: Test the different sample formats? // Create the supported formats. let format = match format_from_waveformatex_ptr ( default_waveformatex_ptr . 0 ) { Some ( fmt ) => fmt , None => { let description = " " . to_string ( ) ; let err = BackendSpecificError { description } ; return Err ( err . into ( ) ) ; } } ; 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 ( ) , min_sample_rate : SampleRate ( rate as _ ) , max_sample_rate : SampleRate ( rate as _ ) , buffer_size : format . buffer_size . clone ( ) , sample_format : format . sample_format . clone ( ) , } ) } Ok ( supported_formats . into_iter ( ) ) } }
There is no way to query the list of all formats that are supported by the
audio processor, so instead we just trial some commonly supported formats.
There is no way to query the list of all formats that are supported by the audio processor so instead we just trial some commonly supported formats .
&self
&self