From ce94ba52c337e46c19b8035424badaa55ee3063a Mon Sep 17 00:00:00 2001 From: Dan Spencer Date: Sat, 31 Jan 2015 07:39:41 -0700 Subject: [PATCH] Add is_conversion_needed() and get_capacity() to AudioCVT These methods can help the user improve performance. * is_conversion_needed() can be used to decide if convert() needs to be called. If not, the user can decide to omit the call. * get_capacity() can be used to avoid vector reallocations that are possible in the convert() method. --- src/sdl2/audio.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index fd248bbb..eed94b57 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -442,7 +442,7 @@ impl AudioCVT { // calculate the size of the dst buffer raw.len = num::cast(src.len()).expect("Buffer length overflow"); - let dst_size = (raw.len * raw.len_mult) as usize; + let dst_size = self.get_capacity(src.len()); let needed = dst_size - src.len(); src.reserve_exact(needed); @@ -465,6 +465,17 @@ impl AudioCVT { } } } + + /// Checks if any conversion is needed. i.e. if the buffer that goes + /// into `convert()` is unchanged from the result. + pub fn is_conversion_needed(&self) -> bool { self.raw.needed != 0 } + + /// Gets the buffer capacity that can contain both the original and + /// converted data. + pub fn get_capacity(&self, src_len: usize) -> usize { + use std::num::Int; + src_len.checked_mul(self.raw.len_mult as usize).expect("Interger overflow") + } } @@ -483,6 +494,9 @@ mod test { let new_buffer_expected: Vec = range(0, 255).flat_map(|v| repeat(v).take(2)).collect(); let cvt = AudioCVT::new(AUDIOU8, 1, 44100, AUDIOU8, 2, 44100).unwrap(); + assert!(cvt.is_conversion_needed()); + assert_eq!(cvt.get_capacity(255), 255*2); + let new_buffer = cvt.convert(buffer); assert_eq!(new_buffer.len(), new_buffer_expected.len()); assert_eq!(new_buffer, new_buffer_expected);