From 803e17a241d9f00cdfcb442b5c76d0664cf0badd Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Fri, 5 Jan 2024 12:45:12 +0100 Subject: [PATCH] audio queue: support interleaved formats --- src/acvt.rs | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/acvt.rs b/src/acvt.rs index 349e10b..ef41918 100644 --- a/src/acvt.rs +++ b/src/acvt.rs @@ -21,7 +21,14 @@ impl> AudioQueue { data: vec![0.into(); rec_size * channels], } } - fn get_cur_size(&self) -> usize { self.end - self.start } + fn get_cur_size(&self) -> usize { + let size = self.end - self.start; + if !self.ileaved { + size + } else { + size / self.channels + } + } fn get_cur_avail(&self) -> usize { self.stride - self.end } fn get_potentially_avail(&self) -> usize { self.stride - self.get_cur_size() } fn read(&mut self, src: &NAAudioBuffer) { @@ -59,19 +66,45 @@ impl> AudioQueue { return; } } - for (dst, src) in self.data.chunks_exact_mut(self.stride).zip(src.get_data().chunks_exact(src.get_stride())) { - dst[self.end..][..to_copy].copy_from_slice(&src[..to_copy]); - } + match (src.get_step() != 1, self.ileaved) { + (false, false) => { + for (dst, src) in self.data.chunks_exact_mut(self.stride).zip(src.get_data().chunks_exact(src.get_stride())) { + dst[self.end..][..to_copy].copy_from_slice(&src[..to_copy]); + } + }, + (true, false) => { + for (i, chunk) in src.get_data().chunks_exact(src.get_step()).enumerate() { + for (ch, &samp) in chunk.iter().enumerate() { + self.data[self.stride * ch + self.end + i] = samp; + } + } + } + (true, true) => { + let sdata = src.get_data(); + self.data[self.end..][..to_copy].copy_from_slice(&sdata[..to_copy]); + }, + _ => unimplemented!(), + }; self.end += to_copy; } fn write(&mut self, dbuf: &mut NAAudioBuffer) { - let dst_len = dbuf.get_length(); + let mut dst_len = dbuf.get_length(); let dst_stride = dbuf.get_stride(); + let dst_step = dbuf.get_step(); let dst = dbuf.get_data_mut().unwrap(); - for (dst, src) in dst.chunks_mut(dst_stride).zip(self.data.chunks_exact(self.stride)) { - dst[..dst_len].copy_from_slice(&src[self.start..][..dst_len]); - } + match (self.ileaved, dst_step != 1) { + (false, false) => { + for (dst, src) in dst.chunks_mut(dst_stride).zip(self.data.chunks_exact(self.stride)) { + dst[..dst_len].copy_from_slice(&src[self.start..][..dst_len]); + } + }, + (true, true) => { + dst_len *= self.channels; + dst[..dst_len].copy_from_slice(&self.data[self.start..][..dst_len]); + }, + _ => unimplemented!(), + }; self.start += dst_len; } fn renorm(&mut self) { -- 2.39.5