cpal/src/alsa/mod.rs

158 lines
4.6 KiB
Rust
Raw Normal View History

2015-03-26 09:03:13 +00:00
extern crate alsa_sys as alsa;
2014-12-16 15:45:45 +00:00
extern crate libc;
2015-09-01 15:15:49 +00:00
pub use self::enumerate::{EndpointsIterator, get_default_endpoint};
2015-09-10 10:43:37 +00:00
use ChannelPosition;
2015-09-01 15:15:49 +00:00
use CreationError;
use Format;
use FormatsEnumerationError;
use SampleFormat;
use SamplesRate;
2015-01-08 19:34:27 +00:00
use std::{ffi, iter, mem};
2015-09-01 15:15:49 +00:00
use std::option::IntoIter as OptionIntoIter;
use std::sync::Mutex;
2014-12-16 15:45:45 +00:00
2015-09-01 15:15:49 +00:00
pub type SupportedFormatsIterator = OptionIntoIter<Format>;
mod enumerate;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Endpoint(String);
impl Endpoint {
pub fn get_supported_formats_list(&self)
-> Result<SupportedFormatsIterator, FormatsEnumerationError>
{
let format = Format {
2015-09-10 10:43:37 +00:00
channels: vec![ChannelPosition::FrontLeft, ChannelPosition::FrontRight],
2015-09-01 15:15:49 +00:00
samples_rate: SamplesRate(44100),
data_type: SampleFormat::I16,
};
Ok(Some(format).into_iter())
}
}
2014-12-17 08:16:26 +00:00
pub struct Voice {
channel: Mutex<*mut alsa::snd_pcm_t>,
2014-12-16 15:45:45 +00:00
num_channels: u16,
}
pub struct Buffer<'a, T> {
2014-12-17 08:16:26 +00:00
channel: &'a mut Voice,
2014-12-16 15:45:45 +00:00
buffer: Vec<T>,
}
2014-12-17 08:16:26 +00:00
impl Voice {
2015-09-01 15:15:49 +00:00
pub fn new(endpoint: &Endpoint, _format: &Format) -> Result<Voice, CreationError> {
2014-12-16 15:45:45 +00:00
unsafe {
2015-09-01 15:15:49 +00:00
let name = ffi::CString::new(endpoint.0.clone()).unwrap();
2014-12-16 15:45:45 +00:00
let mut playback_handle = mem::uninitialized();
2015-02-22 09:31:25 +00:00
check_errors(alsa::snd_pcm_open(&mut playback_handle, name.as_ptr(), alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_NONBLOCK)).unwrap();
2014-12-16 15:45:45 +00:00
let mut hw_params = mem::uninitialized();
check_errors(alsa::snd_pcm_hw_params_malloc(&mut hw_params)).unwrap();
check_errors(alsa::snd_pcm_hw_params_any(playback_handle, hw_params)).unwrap();
check_errors(alsa::snd_pcm_hw_params_set_access(playback_handle, hw_params, alsa::SND_PCM_ACCESS_RW_INTERLEAVED)).unwrap();
check_errors(alsa::snd_pcm_hw_params_set_format(playback_handle, hw_params, alsa::SND_PCM_FORMAT_S16_LE)).unwrap(); // TODO: check endianess
check_errors(alsa::snd_pcm_hw_params_set_rate(playback_handle, hw_params, 44100, 0)).unwrap();
check_errors(alsa::snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2)).unwrap();
check_errors(alsa::snd_pcm_hw_params(playback_handle, hw_params)).unwrap();
alsa::snd_pcm_hw_params_free(hw_params);
check_errors(alsa::snd_pcm_prepare(playback_handle)).unwrap();
2015-09-01 15:15:49 +00:00
Ok(Voice {
channel: Mutex::new(playback_handle),
2014-12-16 15:45:45 +00:00
num_channels: 2,
2015-09-01 15:15:49 +00:00
})
2014-12-16 15:45:45 +00:00
}
}
pub fn get_channels(&self) -> ::ChannelsCount {
self.num_channels
}
pub fn get_samples_rate(&self) -> ::SamplesRate {
::SamplesRate(44100)
}
pub fn get_samples_format(&self) -> ::SampleFormat {
2015-02-24 22:02:22 +00:00
::SampleFormat::I16
2014-12-16 15:45:45 +00:00
}
2015-01-09 20:25:51 +00:00
pub fn append_data<'a, T>(&'a mut self, max_elements: usize) -> Buffer<'a, T> where T: Clone {
let available = {
let channel = self.channel.lock().unwrap();
let available = unsafe { alsa::snd_pcm_avail(*channel) };
available * self.num_channels as alsa::snd_pcm_sframes_t
};
2014-12-16 15:45:45 +00:00
2015-01-09 20:25:51 +00:00
let elements = ::std::cmp::min(available as usize, max_elements);
2014-12-16 15:45:45 +00:00
Buffer {
channel: self,
2015-01-08 19:34:27 +00:00
buffer: iter::repeat(unsafe { mem::uninitialized() }).take(elements).collect(),
2014-12-16 15:45:45 +00:00
}
}
2014-12-22 13:16:47 +00:00
pub fn play(&mut self) {
// already playing
//unimplemented!()
}
pub fn pause(&mut self) {
unimplemented!()
}
2014-12-16 15:45:45 +00:00
}
2014-12-30 07:35:13 +00:00
unsafe impl Send for Voice {}
unsafe impl Sync for Voice {}
2014-12-17 08:16:26 +00:00
impl Drop for Voice {
2014-12-16 15:45:45 +00:00
fn drop(&mut self) {
unsafe {
alsa::snd_pcm_close(*self.channel.lock().unwrap());
2014-12-16 15:45:45 +00:00
}
}
}
impl<'a, T> Buffer<'a, T> {
pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] {
2015-03-30 09:06:46 +00:00
&mut self.buffer
2014-12-16 15:45:45 +00:00
}
pub fn finish(self) {
let written = (self.buffer.len() / self.channel.num_channels as usize)
as alsa::snd_pcm_uframes_t;
let channel = self.channel.channel.lock().unwrap();
2014-12-16 15:45:45 +00:00
unsafe {
let result = alsa::snd_pcm_writei(*channel,
2014-12-16 15:45:45 +00:00
self.buffer.as_ptr() as *const libc::c_void,
written);
if result < 0 {
check_errors(result as libc::c_int).unwrap();
}
}
}
}
fn check_errors(err: libc::c_int) -> Result<(), String> {
2015-01-08 19:34:27 +00:00
use std::ffi;
2014-12-16 15:45:45 +00:00
if err < 0 {
unsafe {
2015-04-04 07:06:46 +00:00
let s = ffi::CStr::from_ptr(alsa::snd_strerror(err)).to_bytes().to_vec();
let s = String::from_utf8(s).unwrap();
return Err(s);
2014-12-16 15:45:45 +00:00
}
}
Ok(())
}