Add Voice::get_period()

This commit is contained in:
Pierre Krieger 2015-09-27 13:00:17 +02:00
parent 6c72a7c08b
commit c93ffcff6c
5 changed files with 36 additions and 7 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "cpal" name = "cpal"
version = "0.2.5" version = "0.2.6"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "Cross-platform audio playing library in pure Rust." description = "Cross-platform audio playing library in pure Rust."
repository = "https://github.com/tomaka/cpal" repository = "https://github.com/tomaka/cpal"

View File

@ -174,6 +174,7 @@ pub struct Voice {
channel: Mutex<*mut alsa::snd_pcm_t>, channel: Mutex<*mut alsa::snd_pcm_t>,
num_channels: u16, num_channels: u16,
buffer_len: usize, // number of samples that can fit in the buffer buffer_len: usize, // number of samples that can fit in the buffer
period_len: usize, // minimum number of samples to put in the buffer
} }
pub struct Buffer<'a, T> { pub struct Buffer<'a, T> {
@ -232,18 +233,21 @@ impl Voice {
check_errors(alsa::snd_pcm_prepare(playback_handle)).unwrap(); check_errors(alsa::snd_pcm_prepare(playback_handle)).unwrap();
let buffer_len = { let (buffer_len, period_len) = {
let mut dummy = mem::uninitialized(); let mut buffer = mem::uninitialized();
let mut val = mem::uninitialized(); let mut period = mem::uninitialized();
check_errors(alsa::snd_pcm_get_params(playback_handle, &mut val, &mut dummy)).unwrap(); check_errors(alsa::snd_pcm_get_params(playback_handle, &mut buffer, &mut period)).unwrap();
assert!(val != 0); assert!(buffer != 0);
val as usize * format.channels.len() let buffer = buffer as usize * format.channels.len();
let period = period as usize * format.channels.len();
(buffer, period)
}; };
Ok(Voice { Ok(Voice {
channel: Mutex::new(playback_handle), channel: Mutex::new(playback_handle),
num_channels: format.channels.len() as u16, num_channels: format.channels.len() as u16,
buffer_len: buffer_len, buffer_len: buffer_len,
period_len: period_len,
}) })
} }
} }
@ -283,6 +287,11 @@ impl Voice {
unimplemented!() unimplemented!()
} }
#[inline]
pub fn get_period(&self) -> usize {
self.period_len
}
pub fn get_pending_samples(&self) -> usize { pub fn get_pending_samples(&self) -> usize {
let available = { let available = {
let channel = self.channel.lock().unwrap(); let channel = self.channel.lock().unwrap();

View File

@ -324,6 +324,16 @@ impl Voice {
self.format().data_type self.format().data_type
} }
/// Returns the minimum number of samples that should be put in a buffer before it is
/// processable by the audio output.
///
/// If you put less than this value in the buffer, the buffer will not be processed and you
/// risk an underrun.
#[inline]
pub fn get_period(&self) -> usize {
self.voice.get_period()
}
/// Adds some PCM data to the voice's buffer. /// Adds some PCM data to the voice's buffer.
/// ///
/// This function indirectly returns a `Buffer` object that must be filled with the audio data. /// This function indirectly returns a `Buffer` object that must be filled with the audio data.

View File

@ -72,6 +72,11 @@ impl Voice {
pub fn pause(&mut self) { pub fn pause(&mut self) {
} }
#[inline]
pub fn get_period(&self) -> usize {
0
}
#[inline] #[inline]
pub fn get_pending_samples(&self) -> usize { pub fn get_pending_samples(&self) -> usize {
unreachable!() unreachable!()

View File

@ -193,6 +193,11 @@ impl Voice {
} }
} }
#[inline]
pub fn get_period(&self) -> usize {
0
}
pub fn get_pending_samples(&self) -> usize { pub fn get_pending_samples(&self) -> usize {
unsafe { unsafe {
let mut padding = mem::uninitialized(); let mut padding = mem::uninitialized();