Add encode methods returning newly-created Vecs

This commit is contained in:
Jean-Marc Valin 2016-07-20 15:10:10 -04:00 committed by Tad Hardesty
parent 73b8da5e6d
commit 4173ad0371
No known key found for this signature in database
GPG Key ID: AEFCC915C75ACC47
2 changed files with 22 additions and 0 deletions

View File

@ -153,6 +153,22 @@ impl Encoder {
Ok(len as usize)
}
/// Encode an Opus frame to a new buffer.
pub fn encode_vec(&mut self, input: &[i16], max_size: usize) -> Result<Vec<u8>> {
let mut output: Vec<u8> = vec![0; max_size];
let result = try!(self.encode(input, output.as_mut_slice()));
output.truncate(result);
Ok(output)
}
/// Encode an Opus frame from floating point input to a new buffer.
pub fn encode_vec_float(&mut self, input: &[f32], max_size: usize) -> Result<Vec<u8>> {
let mut output: Vec<u8> = vec![0; max_size];
let result = try!(self.encode_float(input, output.as_mut_slice()));
output.truncate(result);
Ok(output)
}
/// Reset the codec state to be equivalent to a freshly initialized state.
pub fn reset_state(&mut self) -> Result<()> {
check("opus_encoder_ctl(OPUS_RESET_STATE)", unsafe { ffi::opus_encoder_ctl(self.ptr, OPUS_RESET_STATE) })

View File

@ -24,6 +24,9 @@ fn encode_mono() {
let len = encoder.encode(&[0_i16; MONO_20MS], &mut output).unwrap();
assert!(len > 170 && len < 190);
let myvec = encoder.encode_vec(&[1_i16; MONO_20MS], output.len()).unwrap();
assert!(myvec.len() > 120 && myvec.len() < 140);
}
#[test]
@ -46,6 +49,9 @@ fn encode_stereo() {
// Very small buffer should still succeed
let len = encoder.encode(&[95_i16; 2 * MONO_20MS], &mut [0; 20]).unwrap();
assert!(len <= 20);
let myvec = encoder.encode_vec(&[95_i16; 2 * MONO_20MS], 20).unwrap();
assert!(myvec.len() <= 20);
}
#[test]