Merge pull request #44 from retep998/com

Use shiny new COM
This commit is contained in:
tomaka 2015-05-27 18:38:15 +02:00
commit ed995c098b
2 changed files with 19 additions and 41 deletions

View File

@ -12,17 +12,13 @@ keywords = ["audio", "sound"]
[dependencies] [dependencies]
libc = "*" libc = "*"
[target.i686-pc-windows-gnu.dependencies.winapi] [target.i686-pc-windows-gnu.dependencies]
version = "0" winapi = "0.1.19"
ole32-sys = "0.1"
[target.x86_64-pc-windows-gnu.dependencies.winapi] [target.x86_64-pc-windows-gnu.dependencies]
version = "0" winapi = "0.1.19"
ole32-sys = "0.1"
[target.i686-pc-windows-gnu.dependencies.ole32-sys]
version = "0.1"
[target.x86_64-pc-windows-gnu.dependencies.ole32-sys]
version = "0.1"
[target.i686-unknown-linux-gnu.dependencies.alsa-sys] [target.i686-unknown-linux-gnu.dependencies.alsa-sys]
version = "0" version = "0"

View File

@ -51,8 +51,7 @@ impl Voice {
// //
let frames_available = { let frames_available = {
let mut padding = mem::uninitialized(); let mut padding = mem::uninitialized();
let f = (&*(&mut *self.audio_client).lpVtbl).GetCurrentPadding; let hresult = (*self.audio_client).GetCurrentPadding(&mut padding);
let hresult = f(self.audio_client, &mut padding);
check_result(hresult).unwrap(); check_result(hresult).unwrap();
self.max_frames_in_buffer - padding self.max_frames_in_buffer - padding
}; };
@ -71,8 +70,7 @@ impl Voice {
// loading buffer // loading buffer
let (buffer_data, buffer_len) = { let (buffer_data, buffer_len) = {
let mut buffer: *mut winapi::BYTE = mem::uninitialized(); let mut buffer: *mut winapi::BYTE = mem::uninitialized();
let f = (&*(&mut *self.render_client).lpVtbl).GetBuffer; let hresult = (*self.render_client).GetBuffer(frames_available,
let hresult = f(self.render_client, frames_available,
&mut buffer as *mut *mut libc::c_uchar); &mut buffer as *mut *mut libc::c_uchar);
check_result(hresult).unwrap(); check_result(hresult).unwrap();
assert!(!buffer.is_null()); assert!(!buffer.is_null());
@ -98,8 +96,7 @@ impl Voice {
pub fn play(&mut self) { pub fn play(&mut self) {
if !self.playing { if !self.playing {
unsafe { unsafe {
let f = (&*(&mut *self.audio_client).lpVtbl).Start; let hresult = (*self.audio_client).Start();
let hresult = f(self.audio_client);
check_result(hresult).unwrap(); check_result(hresult).unwrap();
} }
} }
@ -110,8 +107,7 @@ impl Voice {
pub fn pause(&mut self) { pub fn pause(&mut self) {
if self.playing { if self.playing {
unsafe { unsafe {
let f = (&*(&mut *self.audio_client).lpVtbl).Stop; let hresult = (*self.audio_client).Stop();
let hresult = f(self.audio_client);
check_result(hresult).unwrap(); check_result(hresult).unwrap();
} }
} }
@ -126,15 +122,8 @@ unsafe impl Sync for Voice {}
impl Drop for Voice { impl Drop for Voice {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
{ (*self.render_client).Release();
let f = (&*(&mut *self.render_client).lpVtbl).Release; (*self.audio_client).Release();
f(self.render_client);
}
{
let f = (&*(&mut *self.audio_client).lpVtbl).Release;
f(self.audio_client);
}
} }
} }
} }
@ -149,8 +138,7 @@ impl<'a, T> Buffer<'a, T> {
pub fn finish(self) { pub fn finish(self) {
// releasing buffer // releasing buffer
unsafe { unsafe {
let f = (&*(&mut *self.render_client).lpVtbl).ReleaseBuffer; let hresult = (*self.render_client).ReleaseBuffer(self.frames as u32, 0);
let hresult = f(self.render_client, self.frames as u32, 0);
check_result(hresult).unwrap(); check_result(hresult).unwrap();
}; };
} }
@ -177,8 +165,7 @@ fn init() -> Result<Voice, String> {
// getting the default end-point // getting the default end-point
let device = { let device = {
let mut device: *mut winapi::IMMDevice = mem::uninitialized(); let mut device: *mut winapi::IMMDevice = mem::uninitialized();
let f = (&*(&mut *enumerator).lpVtbl).GetDefaultAudioEndpoint; let hresult = enumerator.GetDefaultAudioEndpoint(winapi::EDataFlow::eRender, winapi::ERole::eConsole,
let hresult = f(enumerator, winapi::EDataFlow::eRender, winapi::ERole::eConsole,
mem::transmute(&mut device)); mem::transmute(&mut device));
try!(check_result(hresult)); try!(check_result(hresult));
&mut *device &mut *device
@ -187,8 +174,7 @@ fn init() -> Result<Voice, String> {
// activating in order to get a `IAudioClient` // activating in order to get a `IAudioClient`
let audio_client: &mut winapi::IAudioClient = { let audio_client: &mut winapi::IAudioClient = {
let mut audio_client: *mut winapi::IAudioClient = mem::uninitialized(); let mut audio_client: *mut winapi::IAudioClient = mem::uninitialized();
let f = (&*(&mut *device).lpVtbl).Activate; let hresult = device.Activate(&winapi::IID_IAudioClient, winapi::CLSCTX_ALL,
let hresult = f(device, &winapi::IID_IAudioClient, winapi::CLSCTX_ALL,
ptr::null_mut(), mem::transmute(&mut audio_client)); ptr::null_mut(), mem::transmute(&mut audio_client));
try!(check_result(hresult)); try!(check_result(hresult));
&mut *audio_client &mut *audio_client
@ -207,8 +193,7 @@ fn init() -> Result<Voice, String> {
}; };
let mut format_ptr: *mut winapi::WAVEFORMATEX = mem::uninitialized(); let mut format_ptr: *mut winapi::WAVEFORMATEX = mem::uninitialized();
let f = (&*(&mut *audio_client).lpVtbl).IsFormatSupported; let hresult = audio_client.IsFormatSupported(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
let hresult = f(audio_client, winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
&format_attempt, &mut format_ptr); &format_attempt, &mut format_ptr);
try!(check_result(hresult)); try!(check_result(hresult));
@ -220,8 +205,7 @@ fn init() -> Result<Voice, String> {
let format_copy = ptr::read(format); let format_copy = ptr::read(format);
let f = (&*(&mut *audio_client).lpVtbl).Initialize; let hresult = audio_client.Initialize(winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
let hresult = f(audio_client, winapi::AUDCLNT_SHAREMODE::AUDCLNT_SHAREMODE_SHARED,
0, 10000000, 0, format, ptr::null()); 0, 10000000, 0, format, ptr::null());
if !format_ptr.is_null() { if !format_ptr.is_null() {
@ -236,8 +220,7 @@ fn init() -> Result<Voice, String> {
// //
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 f = (&*(&mut *audio_client).lpVtbl).GetBufferSize; let hresult = audio_client.GetBufferSize(&mut max_frames_in_buffer);
let hresult = f(audio_client, &mut max_frames_in_buffer);
try!(check_result(hresult)); try!(check_result(hresult));
max_frames_in_buffer max_frames_in_buffer
}; };
@ -245,8 +228,7 @@ fn init() -> Result<Voice, String> {
// //
let render_client = { let render_client = {
let mut render_client: *mut winapi::IAudioRenderClient = mem::uninitialized(); let mut render_client: *mut winapi::IAudioRenderClient = mem::uninitialized();
let f = (&*(&mut *audio_client).lpVtbl).GetService; let hresult = audio_client.GetService(&winapi::IID_IAudioRenderClient,
let hresult = f(audio_client, &winapi::IID_IAudioRenderClient,
mem::transmute(&mut render_client)); mem::transmute(&mut render_client));
try!(check_result(hresult)); try!(check_result(hresult));
&mut *render_client &mut *render_client