From bb2356dae10db7a9d1e0a1171c81ddb5b6284d96 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Wed, 27 Jul 2022 09:20:45 +0300 Subject: Fix remainder frame bug, drop it to match c2enc behavior --- c2enc/src/main.rs | 3 ++- libcodec2/src/lib.rs | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/c2enc/src/main.rs b/c2enc/src/main.rs index adbd787..9df5b6a 100644 --- a/c2enc/src/main.rs +++ b/c2enc/src/main.rs @@ -38,7 +38,8 @@ fn main() { let mut file = File::create(&args.out_path).unwrap(); - for samples in speech.chunks(nsam) { + for samples in speech.chunks_exact(nsam) { + // remainder frame will be dropped let mut bits = vec![0u8; nbits]; c.encode(&samples, &mut bits); file.write(&bits).unwrap(); diff --git a/libcodec2/src/lib.rs b/libcodec2/src/lib.rs index 9becb47..b909078 100644 --- a/libcodec2/src/lib.rs +++ b/libcodec2/src/lib.rs @@ -32,11 +32,19 @@ impl Codec2 { pub fn encode(&self, speech: &[i16], bits: &mut [u8]) { let nsamples = self.samples_per_frame(); let nbits = self.bytes_per_frame(); - if bits.len() != nsamples { - panic!("Samples buffer must be of length {}", nsamples); + if speech.len() != nsamples { + panic!( + "Samples buffer must be of length {}, got {}", + nsamples, + speech.len() + ); } - if speech.len() != nbits { - panic!("Speech buffer must be of length {}", nsamples); + if bits.len() != nbits { + panic!( + "Bits buffer must be of length {}, got {}", + nbits, + bits.len() + ); } unsafe { ffi::codec2_encode(self.0, bits.as_ptr() as *mut _, speech.as_ptr() as *mut _); -- cgit v1.3.1