Merge pull request #83 from tomaka/get-period

Add Voice::get_period()
This commit is contained in:
tomaka 2015-09-27 13:05:39 +02:00
commit 5082cb9358
4 changed files with 35 additions and 6 deletions

View File

@ -174,6 +174,7 @@ pub struct Voice {
channel: Mutex<*mut alsa::snd_pcm_t>,
num_channels: u16,
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> {
@ -232,18 +233,21 @@ impl Voice {
check_errors(alsa::snd_pcm_prepare(playback_handle)).unwrap();
let buffer_len = {
let mut dummy = mem::uninitialized();
let mut val = mem::uninitialized();
check_errors(alsa::snd_pcm_get_params(playback_handle, &mut val, &mut dummy)).unwrap();
assert!(val != 0);
val as usize * format.channels.len()
let (buffer_len, period_len) = {
let mut buffer = mem::uninitialized();
let mut period = mem::uninitialized();
check_errors(alsa::snd_pcm_get_params(playback_handle, &mut buffer, &mut period)).unwrap();
assert!(buffer != 0);
let buffer = buffer as usize * format.channels.len();
let period = period as usize * format.channels.len();
(buffer, period)
};
Ok(Voice {
channel: Mutex::new(playback_handle),
num_channels: format.channels.len() as u16,
buffer_len: buffer_len,
period_len: period_len,
})
}
}
@ -283,6 +287,11 @@ impl Voice {
unimplemented!()
}
#[inline]
pub fn get_period(&self) -> usize {
self.period_len
}
pub fn get_pending_samples(&self) -> usize {
let available = {
let channel = self.channel.lock().unwrap();

View File

@ -324,6 +324,16 @@ impl Voice {
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.
///
/// 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) {
}
#[inline]
pub fn get_period(&self) -> usize {
0
}
#[inline]
pub fn get_pending_samples(&self) -> usize {
unreachable!()

View File

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