coreaudio: implementation cleanup

This commit is contained in:
Ronald Kinard 2015-09-26 23:04:17 -05:00
parent 1cdf5af808
commit 30e96aa15a
1 changed files with 32 additions and 53 deletions

View File

@ -5,6 +5,8 @@ use std::sync::mpsc::{channel, Sender, Receiver};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::cell::RefCell; use std::cell::RefCell;
use std::mem; use std::mem;
use std::cmp;
use std::marker::PhantomData;
use CreationError; use CreationError;
use Format; use Format;
@ -44,7 +46,7 @@ pub struct Buffer<'a, T: 'a> {
samples_sender: Sender<(Vec<f32>, NumChannels)>, samples_sender: Sender<(Vec<f32>, NumChannels)>,
samples: Vec<T>, samples: Vec<T>,
num_channels: NumChannels, num_channels: NumChannels,
marker: ::std::marker::PhantomData<&'a T>, marker: PhantomData<&'a T>,
} }
impl<'a, T> Buffer<'a, T> { impl<'a, T> Buffer<'a, T> {
@ -138,12 +140,12 @@ impl Voice {
pub fn append_data<'a, T>(&'a mut self, max_elements: usize) -> Buffer<'a, T> where T: Clone { pub fn append_data<'a, T>(&'a mut self, max_elements: usize) -> Buffer<'a, T> where T: Clone {
// Block until the audio callback is ready for more data. // Block until the audio callback is ready for more data.
let (channels, frames) = self.block_until_ready(); let (channels, frames) = self.block_until_ready();
let buffer_size = ::std::cmp::min(channels * frames, max_elements); let buffer_size = cmp::min(channels * frames, max_elements);
Buffer { Buffer {
samples_sender: self.samples_sender.clone(), samples_sender: self.samples_sender.clone(),
samples: vec![unsafe { mem::uninitialized() }; buffer_size], samples: vec![unsafe { mem::uninitialized() }; buffer_size],
num_channels: channels as usize, num_channels: channels as usize,
marker: ::std::marker::PhantomData marker: PhantomData
} }
} }
@ -175,11 +177,7 @@ impl Voice {
/// returned. /// returned.
#[inline] #[inline]
fn update_last_ready(&self) -> Option<(NumChannels, NumFrames)> { fn update_last_ready(&self) -> Option<(NumChannels, NumFrames)> {
use std::ops::Deref; let refcell = self.last_ready.lock().unwrap();
if let Ok(lr) = self.last_ready.lock() {
let refcell = lr.deref();
{
let data = refcell.borrow(); let data = refcell.borrow();
if let Some(s) = *data { if let Some(s) = *data {
// //
@ -193,48 +191,29 @@ impl Voice {
return *data; return *data;
} }
} }
}
drop(refcell);
None None
} else {
panic!("could not lock last_ready mutex; the previous user must \
have panicked.");
}
} }
/// Block until ready to send data. This checks last_ready first. In any /// Block until ready to send data. This checks last_ready first. In any
/// case, last_ready will be set to None when this function returns. /// case, last_ready will be set to None when this function returns.
fn block_until_ready(&self) -> (NumChannels, NumFrames) { fn block_until_ready(&self) -> (NumChannels, NumFrames) {
use std::ops::Deref; let refcell = self.last_ready.lock().unwrap();
if let Ok(lr) = self.last_ready.lock() {
let ret: (NumChannels, NumFrames);
let refcell = lr.deref();
{
let data = refcell.borrow(); let data = refcell.borrow();
if let Some(s) = *data { if let Some(s) = *data {
drop(data); drop(data);
let mut data = refcell.borrow_mut(); let mut data = refcell.borrow_mut();
*data = None; *data = None;
ret = s; return s;
} else { } else {
match self.ready_receiver.recv() { match self.ready_receiver.recv() {
Ok(ready) => { Ok(ready) => {
ret = ready; return ready;
}, },
Err(e) => panic!("Couldn't receive a ready message: \ Err(e) => panic!("Couldn't receive a ready message: \
{:?}", e) {:?}", e)
} }
} }
} }
drop(refcell);
ret
} else {
panic!("could not lock last_ready mutex; the previous user must \
have panicked.");
}
}
#[inline] #[inline]
pub fn underflowed(&self) -> bool { pub fn underflowed(&self) -> bool {