2015-03-26 09:03:13 +00:00
|
|
|
extern crate coreaudio_rs as coreaudio;
|
2015-02-28 18:50:29 +00:00
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use std::sync::mpsc::{channel, Sender, Receiver};
|
2015-09-24 21:24:12 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2016-06-28 23:09:54 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2015-09-24 21:24:12 +00:00
|
|
|
use std::cell::RefCell;
|
2015-09-24 03:02:28 +00:00
|
|
|
use std::mem;
|
2015-09-27 04:04:17 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::marker::PhantomData;
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
use CreationError;
|
|
|
|
use Format;
|
|
|
|
use FormatsEnumerationError;
|
|
|
|
use SampleFormat;
|
|
|
|
use SamplesRate;
|
|
|
|
use ChannelPosition;
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
mod enumerate;
|
|
|
|
|
|
|
|
pub use self::enumerate::{EndpointsIterator,
|
|
|
|
SupportedFormatsIterator,
|
|
|
|
get_default_endpoint};
|
|
|
|
|
2016-01-12 16:06:14 +00:00
|
|
|
use self::coreaudio::audio_unit::{AudioUnit, IOType};
|
2015-09-24 03:02:28 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
pub struct Endpoint;
|
|
|
|
|
|
|
|
impl Endpoint {
|
|
|
|
pub fn get_supported_formats_list(&self)
|
|
|
|
-> Result<SupportedFormatsIterator, FormatsEnumerationError>
|
|
|
|
{
|
2015-09-24 17:54:54 +00:00
|
|
|
Ok(vec!(Format {
|
|
|
|
channels: vec![ChannelPosition::FrontLeft, ChannelPosition::FrontRight],
|
|
|
|
samples_rate: SamplesRate(44100),
|
|
|
|
data_type: SampleFormat::F32
|
|
|
|
}).into_iter())
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> String {
|
|
|
|
"Default AudioUnit Endpoint".to_string()
|
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Buffer<'a, T: 'a> {
|
|
|
|
samples_sender: Sender<(Vec<f32>, NumChannels)>,
|
|
|
|
samples: Vec<T>,
|
|
|
|
num_channels: NumChannels,
|
2015-09-27 04:04:17 +00:00
|
|
|
marker: PhantomData<&'a T>,
|
2016-06-28 23:09:54 +00:00
|
|
|
pending_samples: Arc<AtomicUsize>
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 17:54:54 +00:00
|
|
|
impl<'a, T> Buffer<'a, T> {
|
2015-09-24 03:02:28 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] {
|
|
|
|
&mut self.samples[..]
|
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2015-09-11 08:55:29 +00:00
|
|
|
#[inline]
|
2015-09-24 03:02:28 +00:00
|
|
|
pub fn len(&self) -> usize {
|
2015-09-24 18:19:42 +00:00
|
|
|
self.samples.len()
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn finish(self) {
|
2016-06-28 23:09:54 +00:00
|
|
|
let Buffer { samples_sender, samples, num_channels, pending_samples, .. } = self;
|
2015-09-24 03:02:28 +00:00
|
|
|
// TODO: At the moment this assumes the Vec<T> is a Vec<f32>.
|
|
|
|
// Need to add T: Sample and use Sample::to_vec_f32.
|
2016-06-28 23:09:54 +00:00
|
|
|
let num_samples = samples.len();
|
2015-09-24 17:54:54 +00:00
|
|
|
let samples = unsafe { mem::transmute(samples) };
|
2016-06-28 23:09:54 +00:00
|
|
|
pending_samples.fetch_add(num_samples, Ordering::SeqCst);
|
2015-09-24 03:02:28 +00:00
|
|
|
match samples_sender.send((samples, num_channels)) {
|
|
|
|
Err(_) => panic!("Failed to send samples to audio unit callback."),
|
|
|
|
Ok(()) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NumChannels = usize;
|
|
|
|
type NumFrames = usize;
|
|
|
|
|
2015-09-24 21:24:12 +00:00
|
|
|
#[allow(dead_code)] // the audio_unit will be dropped if we don't hold it.
|
2015-09-24 03:02:28 +00:00
|
|
|
pub struct Voice {
|
|
|
|
audio_unit: AudioUnit,
|
|
|
|
ready_receiver: Receiver<(NumChannels, NumFrames)>,
|
|
|
|
samples_sender: Sender<(Vec<f32>, NumChannels)>,
|
2015-09-24 21:24:12 +00:00
|
|
|
underflow: Arc<Mutex<RefCell<bool>>>,
|
2016-06-28 23:09:54 +00:00
|
|
|
last_ready: Arc<Mutex<RefCell<Option<(NumChannels, NumFrames)>>>>,
|
|
|
|
pending_samples: Arc<AtomicUsize>
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 03:14:32 +00:00
|
|
|
unsafe impl Sync for Voice {}
|
|
|
|
unsafe impl Send for Voice {}
|
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
impl Voice {
|
2015-09-24 21:24:12 +00:00
|
|
|
pub fn new(_: &Endpoint, _: &Format) -> Result<Voice, CreationError> {
|
2015-09-24 03:02:28 +00:00
|
|
|
// A channel for signalling that the audio unit is ready for data.
|
|
|
|
let (ready_sender, ready_receiver) = channel();
|
|
|
|
// A channel for sending the audio callback a pointer to the sample data.
|
|
|
|
let (samples_sender, samples_receiver) = channel();
|
|
|
|
|
2015-09-24 21:24:12 +00:00
|
|
|
let underflow = Arc::new(Mutex::new(RefCell::new(false)));
|
|
|
|
let uf_clone = underflow.clone();
|
|
|
|
|
2016-06-28 23:09:54 +00:00
|
|
|
let pending_samples: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
|
|
|
let pending_samples_c = pending_samples.clone();
|
|
|
|
|
2016-01-12 16:06:14 +00:00
|
|
|
let audio_unit_result = AudioUnit::new(IOType::HalOutput);
|
|
|
|
|
|
|
|
if let Ok(mut audio_unit) = audio_unit_result {
|
|
|
|
if let Ok(()) = audio_unit.set_render_callback(Some(Box::new(move |channels: &mut[&mut[f32]], num_frames: NumFrames| {
|
2015-09-24 03:02:28 +00:00
|
|
|
if let Err(_) = ready_sender.send((channels.len(), num_frames)) {
|
|
|
|
return Err("Callback failed to send 'ready' message.".to_string());
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
if let Ok((samples, num_channels)) = samples_receiver.try_recv() {
|
|
|
|
let samples: Vec<f32> = samples;
|
2015-09-24 21:24:12 +00:00
|
|
|
if let Ok(uf) = uf_clone.lock() {
|
|
|
|
*(uf.borrow_mut()) = num_frames > samples.len() / num_channels;
|
|
|
|
} else { return Err("Couldn't lock underflow flag field.".to_string()) }
|
|
|
|
|
2016-06-28 23:09:54 +00:00
|
|
|
pending_samples_c.fetch_sub(samples.len(), Ordering::SeqCst);
|
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
for (i, frame) in samples.chunks(num_channels).enumerate() {
|
|
|
|
for (channel, sample) in channels.iter_mut().zip(frame.iter()) {
|
|
|
|
channel[i] = *sample;
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 23:09:54 +00:00
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
|
2016-01-12 16:06:14 +00:00
|
|
|
}))) {
|
|
|
|
if let Ok(()) = audio_unit.start() {
|
|
|
|
return Ok(Voice {
|
|
|
|
audio_unit: audio_unit,
|
|
|
|
ready_receiver: ready_receiver,
|
|
|
|
samples_sender: samples_sender,
|
|
|
|
underflow: underflow,
|
2016-06-28 23:09:54 +00:00
|
|
|
last_ready: Arc::new(Mutex::new(RefCell::new(None))),
|
|
|
|
pending_samples: pending_samples
|
2016-01-12 16:06:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
2016-01-12 16:06:14 +00:00
|
|
|
|
|
|
|
Err(CreationError::DeviceNotAvailable)
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 07:17:21 +00:00
|
|
|
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.
|
2015-09-24 21:24:12 +00:00
|
|
|
let (channels, frames) = self.block_until_ready();
|
2015-09-27 04:04:17 +00:00
|
|
|
let buffer_size = cmp::min(channels * frames, max_elements);
|
2015-09-24 21:24:12 +00:00
|
|
|
Buffer {
|
|
|
|
samples_sender: self.samples_sender.clone(),
|
|
|
|
samples: vec![unsafe { mem::uninitialized() }; buffer_size],
|
|
|
|
num_channels: channels as usize,
|
2016-06-28 23:09:54 +00:00
|
|
|
marker: PhantomData,
|
|
|
|
pending_samples: self.pending_samples.clone()
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 08:55:29 +00:00
|
|
|
#[inline]
|
2015-02-28 18:50:29 +00:00
|
|
|
pub fn play(&mut self) {
|
2015-09-24 03:02:28 +00:00
|
|
|
// implicitly playing
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 08:55:29 +00:00
|
|
|
#[inline]
|
2015-02-28 18:50:29 +00:00
|
|
|
pub fn pause(&mut self) {
|
2015-09-24 03:02:28 +00:00
|
|
|
unimplemented!()
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 06:28:59 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get_period(&self) -> usize {
|
2015-09-24 21:24:12 +00:00
|
|
|
if let Some(ready) = self.update_last_ready() {
|
|
|
|
(ready.0 * ready.1) as usize
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:09:54 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn get_pending_samples(&self) -> usize {
|
|
|
|
self.pending_samples.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:24:12 +00:00
|
|
|
/// Attempts to store the most recent ready message into the internal
|
|
|
|
/// ref cell, then return the last ready message. If the last ready hasn't
|
|
|
|
/// been reset with `clear_last_ready`, then it will not be set and the
|
|
|
|
/// current value will be returned. Else, the ready_receiver will be
|
|
|
|
/// try_recv'd and if it is ready, the last ready will be set and returned.
|
|
|
|
/// Finally, if the ready_receiver had no data at try_recv, None will be
|
|
|
|
/// returned.
|
|
|
|
#[inline]
|
|
|
|
fn update_last_ready(&self) -> Option<(NumChannels, NumFrames)> {
|
2015-09-27 04:04:17 +00:00
|
|
|
let refcell = self.last_ready.lock().unwrap();
|
|
|
|
let data = refcell.borrow();
|
|
|
|
if let Some(s) = *data {
|
|
|
|
//
|
|
|
|
return Some(s);
|
2015-09-24 21:24:12 +00:00
|
|
|
} else {
|
2015-09-27 04:04:17 +00:00
|
|
|
drop(data);
|
|
|
|
let mut data = refcell.borrow_mut();
|
|
|
|
if let Ok(ready) = self.ready_receiver.try_recv() {
|
|
|
|
// the audiounit is ready so we can set last_ready
|
|
|
|
*data = Some(ready);
|
|
|
|
return *data;
|
|
|
|
}
|
2015-09-24 21:24:12 +00:00
|
|
|
}
|
2015-09-27 04:04:17 +00:00
|
|
|
None
|
2015-09-24 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
fn block_until_ready(&self) -> (NumChannels, NumFrames) {
|
2015-09-27 04:04:17 +00:00
|
|
|
let refcell = self.last_ready.lock().unwrap();
|
|
|
|
let data = refcell.borrow();
|
|
|
|
if let Some(s) = *data {
|
|
|
|
drop(data);
|
|
|
|
let mut data = refcell.borrow_mut();
|
|
|
|
*data = None;
|
|
|
|
return s;
|
2015-09-24 21:24:12 +00:00
|
|
|
} else {
|
2015-09-27 04:04:17 +00:00
|
|
|
match self.ready_receiver.recv() {
|
|
|
|
Ok(ready) => {
|
|
|
|
return ready;
|
|
|
|
},
|
|
|
|
Err(e) => panic!("Couldn't receive a ready message: \
|
|
|
|
{:?}", e)
|
|
|
|
}
|
2015-09-24 21:24:12 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 18:19:42 +00:00
|
|
|
#[inline]
|
2015-09-24 03:02:28 +00:00
|
|
|
pub fn underflowed(&self) -> bool {
|
2015-09-24 21:24:12 +00:00
|
|
|
let uf = self.underflow.lock().unwrap();
|
|
|
|
let v = uf.borrow();
|
|
|
|
*v
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
}
|