Handle F32 formats in Voice::new

This commit is contained in:
Pierre Krieger 2015-09-01 14:51:35 +02:00
parent 52052b6d07
commit bf20431901
2 changed files with 38 additions and 28 deletions

View File

@ -119,7 +119,7 @@ impl Endpoint {
Format { Format {
channels: 2, channels: 2,
samples_rate: SamplesRate(44100), samples_rate: SamplesRate(44100),
data_type: SampleFormat::U16, data_type: SampleFormat::I16,
} }
}; };

View File

@ -12,6 +12,7 @@ use std::marker::PhantomData;
use CreationError; use CreationError;
use Format; use Format;
use SampleFormat;
pub struct Voice { pub struct Voice {
audio_client: *mut winapi::IAudioClient, audio_client: *mut winapi::IAudioClient,
@ -44,8 +45,13 @@ impl Voice {
// computing the format and initializing the device // computing the format and initializing the device
let format = { let format = {
let format_attempt = winapi::WAVEFORMATEX { let format_attempt = winapi::WAVEFORMATEXTENSIBLE {
wFormatTag: winapi::WAVE_FORMAT_PCM, Format: winapi::WAVEFORMATEX {
wFormatTag: match format.data_type {
SampleFormat::I16 => winapi::WAVE_FORMAT_PCM,
SampleFormat::F32 => winapi::WAVE_FORMAT_EXTENSIBLE,
SampleFormat::U16 => return Err(CreationError::FormatNotSupported),
},
nChannels: format.channels as winapi::WORD, nChannels: format.channels as winapi::WORD,
nSamplesPerSec: format.samples_rate.0 as winapi::DWORD, nSamplesPerSec: format.samples_rate.0 as winapi::DWORD,
nAvgBytesPerSec: format.channels as winapi::DWORD * nAvgBytesPerSec: format.channels as winapi::DWORD *
@ -54,12 +60,29 @@ impl Voice {
nBlockAlign: format.channels as winapi::WORD * nBlockAlign: format.channels as winapi::WORD *
format.data_type.get_sample_size() as winapi::WORD, format.data_type.get_sample_size() as winapi::WORD,
wBitsPerSample: 8 * format.data_type.get_sample_size() as winapi::WORD, wBitsPerSample: 8 * format.data_type.get_sample_size() as winapi::WORD,
cbSize: 0, cbSize: match format.data_type {
SampleFormat::I16 => 0,
SampleFormat::F32 => (mem::size_of::<winapi::WAVEFORMATEXTENSIBLE>() -
mem::size_of::<winapi::WAVEFORMATEX>()) as winapi::WORD,
SampleFormat::U16 => return Err(CreationError::FormatNotSupported),
},
},
Samples: 8 * format.data_type.get_sample_size() as winapi::WORD,
dwChannelMask: 3, // LEFT | RIGHT
SubFormat: match format.data_type {
SampleFormat::I16 => winapi::KSDATAFORMAT_SUBTYPE_PCM,
SampleFormat::F32 => winapi::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
SampleFormat::U16 => return Err(CreationError::FormatNotSupported),
},
}; };
let mut format_ptr: *mut winapi::WAVEFORMATEX = mem::uninitialized(); let mut format_ptr: *mut winapi::WAVEFORMATEX = mem::uninitialized();
let hresult = (*audio_client).IsFormatSupported(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED, let hresult = (*audio_client).IsFormatSupported(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
&format_attempt, &mut format_ptr); &format_attempt.Format, &mut format_ptr);
if !format_ptr.is_null() {
ole32::CoTaskMemFree(format_ptr as *mut _);
}
if hresult == winapi::S_FALSE { if hresult == winapi::S_FALSE {
return Err(CreationError::FormatNotSupported); return Err(CreationError::FormatNotSupported);
@ -78,21 +101,8 @@ impl Voice {
Ok(()) => (), Ok(()) => (),
}; };
let format = if format_ptr.is_null() {
&format_attempt
} else {
&*format_ptr
};
let format_copy = ptr::read(format);
let hresult = (*audio_client).Initialize(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED, let hresult = (*audio_client).Initialize(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
0, 10000000, 0, format, ptr::null()); 0, 10000000, 0, &format_attempt.Format, ptr::null());
if !format_ptr.is_null() {
ole32::CoTaskMemFree(format_ptr as *mut _);
}
match check_result(hresult) { match check_result(hresult) {
Err(ref e) if e.raw_os_error() == Some(winapi::AUDCLNT_E_DEVICE_INVALIDATED) => Err(ref e) if e.raw_os_error() == Some(winapi::AUDCLNT_E_DEVICE_INVALIDATED) =>
@ -107,7 +117,7 @@ impl Voice {
Ok(()) => (), Ok(()) => (),
}; };
format_copy format_attempt.Format
}; };
// //