Use the format passed as parameter in Voice::new

This commit is contained in:
Pierre Krieger 2015-09-01 14:26:25 +02:00
parent 98b931edff
commit 52052b6d07
2 changed files with 59 additions and 16 deletions

View File

@ -13,6 +13,7 @@ pub enum SampleFormat {
impl SampleFormat { impl SampleFormat {
/// Returns the size in bytes of a sample of this format. /// Returns the size in bytes of a sample of this format.
#[inline]
pub fn get_sample_size(&self) -> usize { pub fn get_sample_size(&self) -> usize {
match self { match self {
&SampleFormat::I16 => mem::size_of::<i16>(), &SampleFormat::I16 => mem::size_of::<i16>(),
@ -29,18 +30,21 @@ pub unsafe trait Sample: Copy + Clone {
} }
unsafe impl Sample for u16 { unsafe impl Sample for u16 {
#[inline]
fn get_format() -> SampleFormat { fn get_format() -> SampleFormat {
SampleFormat::U16 SampleFormat::U16
} }
} }
unsafe impl Sample for i16 { unsafe impl Sample for i16 {
#[inline]
fn get_format() -> SampleFormat { fn get_format() -> SampleFormat {
SampleFormat::I16 SampleFormat::I16
} }
} }
unsafe impl Sample for f32 { unsafe impl Sample for f32 {
#[inline]
fn get_format() -> SampleFormat { fn get_format() -> SampleFormat {
SampleFormat::F32 SampleFormat::F32
} }

View File

@ -45,12 +45,15 @@ 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::WAVEFORMATEX {
wFormatTag: 1, // WAVE_FORMAT_PCM ; TODO: replace by constant wFormatTag: winapi::WAVE_FORMAT_PCM,
nChannels: 2, nChannels: format.channels as winapi::WORD,
nSamplesPerSec: 44100, nSamplesPerSec: format.samples_rate.0 as winapi::DWORD,
nAvgBytesPerSec: 2 * 44100 * 2, nAvgBytesPerSec: format.channels as winapi::DWORD *
nBlockAlign: (2 * 16) / 8, format.samples_rate.0 as winapi::DWORD *
wBitsPerSample: 16, format.data_type.get_sample_size() as winapi::DWORD,
nBlockAlign: format.channels as winapi::WORD *
format.data_type.get_sample_size() as winapi::WORD,
wBitsPerSample: 8 * format.data_type.get_sample_size() as winapi::WORD,
cbSize: 0, cbSize: 0,
}; };
@ -58,12 +61,24 @@ impl Voice {
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, &mut format_ptr);
if hresult == winapi::S_FALSE {
return Err(CreationError::FormatNotSupported);
}
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) =>
return Err(CreationError::DeviceNotAvailable), {
e => e.unwrap(), (*audio_client).Release();
return Err(CreationError::DeviceNotAvailable);
},
Err(e) => {
(*audio_client).Release();
panic!("{:?}", e);
},
Ok(()) => (),
}; };
let format = if format_ptr.is_null() { let format = if format_ptr.is_null() {
&format_attempt &format_attempt
} else { } else {
@ -81,8 +96,15 @@ impl Voice {
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) =>
return Err(CreationError::DeviceNotAvailable), {
e => e.unwrap(), (*audio_client).Release();
return Err(CreationError::DeviceNotAvailable);
},
Err(e) => {
(*audio_client).Release();
panic!("{:?}", e);
},
Ok(()) => (),
}; };
format_copy format_copy
@ -92,11 +114,20 @@ impl Voice {
let max_frames_in_buffer = { let max_frames_in_buffer = {
let mut max_frames_in_buffer = mem::uninitialized(); let mut max_frames_in_buffer = mem::uninitialized();
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer); let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
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) =>
return Err(CreationError::DeviceNotAvailable), {
e => e.unwrap(), (*audio_client).Release();
return Err(CreationError::DeviceNotAvailable);
},
Err(e) => {
(*audio_client).Release();
panic!("{:?}", e);
},
Ok(()) => (),
}; };
max_frames_in_buffer max_frames_in_buffer
}; };
@ -106,10 +137,18 @@ impl Voice {
let hresult = (*audio_client).GetService(&winapi::IID_IAudioRenderClient, let hresult = (*audio_client).GetService(&winapi::IID_IAudioRenderClient,
&mut render_client as *mut *mut winapi::IAudioRenderClient &mut render_client as *mut *mut winapi::IAudioRenderClient
as *mut _); 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) =>
return Err(CreationError::DeviceNotAvailable), {
e => e.unwrap(), (*audio_client).Release();
return Err(CreationError::DeviceNotAvailable);
},
Err(e) => {
(*audio_client).Release();
panic!("{:?}", e);
},
Ok(()) => (),
}; };
&mut *render_client &mut *render_client