Restore CoreAudio support after API overhaul.
This commit is contained in:
parent
ffadfc990e
commit
c841be235c
|
@ -0,0 +1,29 @@
|
||||||
|
use super::Endpoint;
|
||||||
|
|
||||||
|
use ::Format;
|
||||||
|
|
||||||
|
use std::vec::IntoIter as VecIntoIter;
|
||||||
|
|
||||||
|
pub struct EndpointsIterator(bool);
|
||||||
|
|
||||||
|
unsafe impl Send for EndpointsIterator {}
|
||||||
|
unsafe impl Sync for EndpointsIterator {}
|
||||||
|
|
||||||
|
impl Default for EndpointsIterator {
|
||||||
|
fn default() -> Self {
|
||||||
|
EndpointsIterator(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for EndpointsIterator {
|
||||||
|
type Item = Endpoint;
|
||||||
|
fn next(&mut self) -> Option<Endpoint> {
|
||||||
|
if self.0 { None } else { self.0 = true; Some(Endpoint) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_default_endpoint() -> Option<Endpoint> {
|
||||||
|
Some(Endpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type SupportedFormatsIterator = VecIntoIter<Format>;
|
|
@ -1,65 +1,63 @@
|
||||||
extern crate coreaudio_rs as coreaudio;
|
extern crate coreaudio_rs as coreaudio;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use self::coreaudio::audio_unit::{AudioUnit, Type, SubType};
|
|
||||||
use std::mem;
|
|
||||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
type NumChannels = usize;
|
use CreationError;
|
||||||
type NumFrames = usize;
|
use Format;
|
||||||
|
use FormatsEnumerationError;
|
||||||
|
use SampleFormat;
|
||||||
|
use SamplesRate;
|
||||||
|
use ChannelPosition;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
mod enumerate;
|
||||||
pub struct Voice {
|
|
||||||
audio_unit: AudioUnit,
|
pub use self::enumerate::{EndpointsIterator,
|
||||||
ready_receiver: Receiver<(NumChannels, NumFrames)>,
|
SupportedFormatsIterator,
|
||||||
samples_sender: Sender<(Vec<f32>, NumChannels)>,
|
get_default_endpoint};
|
||||||
|
|
||||||
|
use self::coreaudio::audio_unit::{AudioUnit, Type, SubType};
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
|
pub struct Endpoint;
|
||||||
|
|
||||||
|
impl Endpoint {
|
||||||
|
pub fn get_supported_formats_list(&self)
|
||||||
|
-> Result<SupportedFormatsIterator, FormatsEnumerationError>
|
||||||
|
{
|
||||||
|
Ok(vec!(Format {
|
||||||
|
channels: vec![ChannelPosition::FrontLeft, ChannelPosition::FrontRight],
|
||||||
|
samples_rate: SamplesRate(512),
|
||||||
|
data_type: SampleFormat::F32
|
||||||
|
}).into_iter())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_name(&self) -> String {
|
||||||
|
"Default AudioUnit Endpoint".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Buffer<'a, T: 'a> {
|
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,
|
||||||
|
len: usize,
|
||||||
marker: ::std::marker::PhantomData<&'a T>,
|
marker: ::std::marker::PhantomData<&'a T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Voice {
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn new() -> Voice {
|
|
||||||
new_voice().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
|
||||||
loop {
|
|
||||||
if let Ok((channels, frames)) = self.ready_receiver.try_recv() {
|
|
||||||
let buffer_size = ::std::cmp::min(channels * frames, max_elements);
|
|
||||||
return Buffer {
|
|
||||||
samples_sender: self.samples_sender.clone(),
|
|
||||||
samples: vec![unsafe{ mem::uninitialized() }; buffer_size],
|
|
||||||
num_channels: channels as usize,
|
|
||||||
marker: ::std::marker::PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn play(&mut self) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn pause(&mut self) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> Buffer<'a, T> {
|
impl<'a, T> Buffer<'a, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] {
|
pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] {
|
||||||
&mut self.samples[..]
|
&mut self.samples[..]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.len
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn finish(self) {
|
pub fn finish(self) {
|
||||||
let Buffer { samples_sender, samples, num_channels, .. } = self;
|
let Buffer { samples_sender, samples, num_channels, .. } = self;
|
||||||
// TODO: At the moment this assumes the Vec<T> is a Vec<f32>.
|
// TODO: At the moment this assumes the Vec<T> is a Vec<f32>.
|
||||||
|
@ -72,10 +70,17 @@ impl<'a, T> Buffer<'a, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NumChannels = usize;
|
||||||
|
type NumFrames = usize;
|
||||||
|
|
||||||
/// Construct a new Voice.
|
pub struct Voice {
|
||||||
fn new_voice() -> Result<Voice, String> {
|
audio_unit: AudioUnit,
|
||||||
|
ready_receiver: Receiver<(NumChannels, NumFrames)>,
|
||||||
|
samples_sender: Sender<(Vec<f32>, NumChannels)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Voice {
|
||||||
|
pub fn new(endpoint: &Endpoint, format: &Format) -> Result<Voice, CreationError> {
|
||||||
// A channel for signalling that the audio unit is ready for data.
|
// A channel for signalling that the audio unit is ready for data.
|
||||||
let (ready_sender, ready_receiver) = channel();
|
let (ready_sender, ready_receiver) = channel();
|
||||||
// A channel for sending the audio callback a pointer to the sample data.
|
// A channel for sending the audio callback a pointer to the sample data.
|
||||||
|
@ -112,10 +117,43 @@ fn new_voice() -> Result<Voice, String> {
|
||||||
ready_receiver: ready_receiver,
|
ready_receiver: ready_receiver,
|
||||||
samples_sender: samples_sender
|
samples_sender: samples_sender
|
||||||
}),
|
}),
|
||||||
Err(err) => {
|
Err(_) => {
|
||||||
use ::std::error::Error;
|
Err(CreationError::DeviceNotAvailable)
|
||||||
Err(err.description().to_string())
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
loop {
|
||||||
|
if let Ok((channels, frames)) = self.ready_receiver.try_recv() {
|
||||||
|
let buffer_size = ::std::cmp::min(channels * frames, max_elements);
|
||||||
|
return Buffer {
|
||||||
|
samples_sender: self.samples_sender.clone(),
|
||||||
|
samples: vec![unsafe{ mem::uninitialized() }; buffer_size],
|
||||||
|
num_channels: channels as usize,
|
||||||
|
marker: ::std::marker::PhantomData,
|
||||||
|
len: 512
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn play(&mut self) {
|
||||||
|
// implicitly playing
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn pause(&mut self) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pending_samples(&self) -> usize {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn underflowed(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue