From c93ffcff6c84eeaae88407a5c5840f1c104b66c8 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sun, 27 Sep 2015 13:00:17 +0200 Subject: [PATCH] Add Voice::get_period() --- Cargo.toml | 2 +- src/alsa/mod.rs | 21 +++++++++++++++------ src/lib.rs | 10 ++++++++++ src/null/mod.rs | 5 +++++ src/wasapi/voice.rs | 5 +++++ 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e7e12e6..27830ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cpal" -version = "0.2.5" +version = "0.2.6" authors = ["Pierre Krieger "] description = "Cross-platform audio playing library in pure Rust." repository = "https://github.com/tomaka/cpal" diff --git a/src/alsa/mod.rs b/src/alsa/mod.rs index dd1b8a5..dca18df 100644 --- a/src/alsa/mod.rs +++ b/src/alsa/mod.rs @@ -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(); diff --git a/src/lib.rs b/src/lib.rs index df40aff..2bfd356 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. diff --git a/src/null/mod.rs b/src/null/mod.rs index 0021a18..c8f6d37 100644 --- a/src/null/mod.rs +++ b/src/null/mod.rs @@ -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!() diff --git a/src/wasapi/voice.rs b/src/wasapi/voice.rs index 802d4d2..bcadf47 100644 --- a/src/wasapi/voice.rs +++ b/src/wasapi/voice.rs @@ -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();