1 //! Sound format conversion.
3 //! This module implements the functionality for conversion between different sound formats: packed or planar audio, 8-/16-/24-/32-bit, integer or floating point, different number of channels.
4 //! Eventually this might support resampling as well.
5 pub use crate::formats::{NASoniton,NAChannelMap};
6 pub use crate::frame::{NAAudioBuffer,NAAudioInfo,NABufferType};
7 use crate::formats::NAChannelType;
8 use crate::frame::alloc_audio_buffer;
9 use crate::io::byteio::*;
10 use std::f32::consts::SQRT_2;
12 /// A list specifying general sound conversion errors.
13 #[derive(Clone,Copy,Debug,PartialEq)]
14 pub enum SoundConvertError {
15 /// Invalid input arguments.
17 /// Allocation failed.
19 /// Requested feature is not supported.
31 fn is_remix(&self) -> bool {
33 ChannelOp::Remix(_) => true,
34 ChannelOp::DupMono(_) => true,
40 fn apply_channel_op<T:Copy>(ch_op: &ChannelOp, src: &[T], dst: &mut Vec<T>) {
42 ChannelOp::Passthrough => {
43 dst.copy_from_slice(src);
45 ChannelOp::Reorder(ref reorder) => {
46 for (out, idx) in dst.iter_mut().zip(reorder.iter()) {
54 fn remix_i32(ch_op: &ChannelOp, src: &[i32], dst: &mut Vec<i32>) {
55 if let ChannelOp::Remix(ref remix_mat) = ch_op {
57 for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) {
59 for (inval, coef) in src.iter().zip(coeffs.iter()) {
60 sum += (*inval as f32) * *coef;
65 if let ChannelOp::DupMono(ref dup_mat) = ch_op {
67 for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) {
68 *out = if *copy { src } else { 0 };
73 fn remix_f32(ch_op: &ChannelOp, src: &[f32], dst: &mut Vec<f32>) {
74 if let ChannelOp::Remix(ref remix_mat) = ch_op {
76 for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) {
78 for (inval, coef) in src.iter().zip(coeffs.iter()) {
79 sum += *inval * *coef;
84 if let ChannelOp::DupMono(ref dup_mat) = ch_op {
86 for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) {
87 *out = if *copy { src } else { 0.0 };
92 trait FromFmt<T:Copy> {
93 fn cvt_from(val: T) -> Self;
96 impl FromFmt<u8> for u8 {
97 fn cvt_from(val: u8) -> u8 { val }
99 impl FromFmt<u8> for i16 {
100 fn cvt_from(val: u8) -> i16 { u16::from(val ^ 0x80).wrapping_mul(0x101) as i16}
102 impl FromFmt<u8> for i32 {
103 fn cvt_from(val: u8) -> i32 { u32::from(val ^ 0x80).wrapping_mul(0x01010101) as i32 }
105 impl FromFmt<u8> for f32 {
106 fn cvt_from(val: u8) -> f32 { (f32::from(val) - 128.0) / 128.0 }
109 impl FromFmt<i16> for u8 {
110 fn cvt_from(val: i16) -> u8 { ((val >> 8) + 128).min(255).max(0) as u8 }
112 impl FromFmt<i16> for i16 {
113 fn cvt_from(val: i16) -> i16 { val }
115 impl FromFmt<i16> for i32 {
116 fn cvt_from(val: i16) -> i32 { (i32::from(val) & 0xFFFF) | (i32::from(val) << 16) }
118 impl FromFmt<i16> for f32 {
119 fn cvt_from(val: i16) -> f32 { f32::from(val) / 32768.0 }
122 impl FromFmt<i32> for u8 {
123 fn cvt_from(val: i32) -> u8 { ((val >> 24) + 128).min(255).max(0) as u8 }
125 impl FromFmt<i32> for i16 {
126 fn cvt_from(val: i32) -> i16 { (val >> 16) as i16 }
128 impl FromFmt<i32> for i32 {
129 fn cvt_from(val: i32) -> i32 { val }
131 impl FromFmt<i32> for f32 {
132 fn cvt_from(val: i32) -> f32 { (val as f32) / 31.0f32.exp2() }
135 impl FromFmt<f32> for u8 {
136 fn cvt_from(val: f32) -> u8 { ((val * 128.0) + 128.0).min(255.0).max(0.0) as u8 }
138 impl FromFmt<f32> for i16 {
139 fn cvt_from(val: f32) -> i16 { (val * 32768.0).min(32767.0).max(-32768.0) as i16 }
141 impl FromFmt<f32> for i32 {
142 fn cvt_from(val: f32) -> i32 {
145 } else if val <= -1.0 {
148 let scale = (1u32 << 31) as f32;
153 impl FromFmt<f32> for f32 {
154 fn cvt_from(val: f32) -> f32 { val }
157 trait IntoFmt<T:Copy> {
158 fn cvt_into(self) -> T;
161 impl<T:Copy, U:Copy> IntoFmt<U> for T where U: FromFmt<T> {
162 fn cvt_into(self) -> U { U::cvt_from(self) }
167 fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>);
168 fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>);
171 struct GenericSampleReader<'a, T:Copy> {
176 impl<'a, T:Copy+IntoFmt<i32>+IntoFmt<f32>> SampleReader for GenericSampleReader<'a, T> {
177 fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>) {
179 for el in dst.iter_mut() {
180 *el = self.data[off].cvt_into();
184 fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>) {
186 for el in dst.iter_mut() {
187 *el = self.data[off].cvt_into();
193 struct S8SampleReader<'a> {
198 impl<'a> SampleReader for S8SampleReader<'a> {
199 fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>) {
201 for el in dst.iter_mut() {
202 *el = (self.data[off] ^ 0x80).cvt_into();
206 fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>) {
208 for el in dst.iter_mut() {
209 *el = (self.data[off] ^ 0x80).cvt_into();
215 struct PackedSampleReader<'a> {
221 impl<'a> PackedSampleReader<'a> {
222 fn new(data: &'a [u8], fmt: NASoniton) -> Self {
223 if (fmt.bits & 7) != 0 { unimplemented!(); }
224 let bpp = (fmt.bits >> 3) as usize;
225 Self { data, fmt, bpp }
227 fn get_samples<T:Copy>(&self, pos: usize, dst: &mut Vec<T>) where u8: IntoFmt<T>, i16: IntoFmt<T>, i32: IntoFmt<T>, f32: IntoFmt<T> {
228 let mut offset = pos * self.bpp * dst.len();
230 for el in dst.iter_mut() {
231 let src = &self.data[offset..];
232 *el = if !self.fmt.float {
233 match (self.bpp, self.fmt.be) {
234 (1, _) => if !self.fmt.signed { src[0].cvt_into() } else { (src[0] ^ 0x80).cvt_into() },
235 (2, true) => (read_u16be(src).unwrap() as i16).cvt_into(),
236 (2, false) => (read_u16le(src).unwrap() as i16).cvt_into(),
237 (3, true) => ((read_u24be(src).unwrap() << 8) as i32).cvt_into(),
238 (3, false) => ((read_u24be(src).unwrap() << 8) as i32).cvt_into(),
239 (4, true) => (read_u32be(src).unwrap() as i32).cvt_into(),
240 (4, false) => (read_u32be(src).unwrap() as i32).cvt_into(),
244 match (self.bpp, self.fmt.be) {
245 (4, true) => read_f32be(src).unwrap().cvt_into(),
246 (4, false) => read_f32le(src).unwrap().cvt_into(),
247 (8, true) => (read_f64be(src).unwrap() as f32).cvt_into(),
248 (8, false) => (read_f64le(src).unwrap() as f32).cvt_into(),
249 (_, _) => unreachable!(),
257 impl SampleReader for PackedSampleReader<'_> {
258 fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>) {
259 self.get_samples(pos, dst);
261 fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>) {
262 self.get_samples(pos, dst);
267 fn store_samples_i32(&mut self, pos: usize, src: &[i32]);
268 fn store_samples_f32(&mut self, pos: usize, src: &[f32]);
271 struct GenericSampleWriter<'a, T:Copy> {
276 impl<'a, T:Copy+FromFmt<i32>+FromFmt<f32>> SampleWriter for GenericSampleWriter<'a, T> {
277 fn store_samples_i32(&mut self, pos: usize, src: &[i32]) {
279 for el in src.iter() {
280 self.data[off] = (*el).cvt_into();
284 fn store_samples_f32(&mut self, pos: usize, src: &[f32]) {
286 for el in src.iter() {
287 self.data[off] = (*el).cvt_into();
293 struct PackedSampleWriter<'a> {
299 impl<'a> PackedSampleWriter<'a> {
300 fn new(data: &'a mut [u8], fmt: NASoniton) -> Self {
301 if (fmt.bits & 7) != 0 { unimplemented!(); }
302 let bpp = (fmt.bits >> 3) as usize;
303 Self { data, fmt, bpp }
306 fn store_samples<T:Copy>(&mut self, pos: usize, src: &[T]) where u8: FromFmt<T>, i16: FromFmt<T>, i32: FromFmt<T>, f32: FromFmt<T> {
307 let mut offset = pos * self.bpp * src.len();
308 for el in src.iter() {
309 let dst = &mut self.data[offset..];
311 match (self.bpp, self.fmt.be) {
313 dst[0] = u8::cvt_from(*el);
318 (2, true) => write_u16be(dst, i16::cvt_from(*el) as u16).unwrap(),
319 (2, false) => write_u16le(dst, i16::cvt_from(*el) as u16).unwrap(),
320 (3, true) => write_u24be(dst, (i32::cvt_from(*el) >> 8) as u32).unwrap(),
321 (3, false) => write_u24le(dst, (i32::cvt_from(*el) >> 8) as u32).unwrap(),
322 (4, true) => write_u32be(dst, i32::cvt_from(*el) as u32).unwrap(),
323 (4, false) => write_u32le(dst, i32::cvt_from(*el) as u32).unwrap(),
327 match (self.bpp, self.fmt.be) {
328 (4, true) => write_f32be(dst, f32::cvt_from(*el)).unwrap(),
329 (4, false) => write_f32le(dst, f32::cvt_from(*el)).unwrap(),
330 (8, true) => write_f64be(dst, f64::from(f32::cvt_from(*el))).unwrap(),
331 (8, false) => write_f64le(dst, f64::from(f32::cvt_from(*el))).unwrap(),
332 (_, _) => unreachable!(),
340 impl SampleWriter for PackedSampleWriter<'_> {
341 fn store_samples_i32(&mut self, pos: usize, src: &[i32]) {
342 self.store_samples(pos, src);
344 fn store_samples_f32(&mut self, pos: usize, src: &[f32]) {
345 self.store_samples(pos, src);
349 /// Converts input audio buffer into desired format and returns a newly allocated buffer.
350 pub fn convert_audio_frame(src: &NABufferType, dst_info: &NAAudioInfo, dst_chmap: &NAChannelMap) ->
351 Result<NABufferType, SoundConvertError> {
352 let mut nsamples = src.get_audio_length();
354 return Err(SoundConvertError::InvalidInput);
356 let src_chmap = src.get_chmap().unwrap();
357 let src_info = src.get_audio_info().unwrap();
358 if (src_chmap.num_channels() == 0) || (dst_chmap.num_channels() == 0) {
359 return Err(SoundConvertError::InvalidInput);
362 if let NABufferType::AudioPacked(_) = src {
363 nsamples = nsamples * 8 / (src_info.get_format().get_bits() as usize) / src_chmap.num_channels();
366 let needs_remix = src_chmap.num_channels() != dst_chmap.num_channels();
367 let no_channel_needs = !needs_remix && channel_maps_equal(src_chmap, dst_chmap);
368 let needs_reorder = !needs_remix && !no_channel_needs && channel_maps_reordered(src_chmap, dst_chmap);
370 let channel_op = if no_channel_needs {
371 ChannelOp::Passthrough
372 } else if needs_reorder {
373 let reorder_mat = calculate_reorder_matrix(src_chmap, dst_chmap);
374 ChannelOp::Reorder(reorder_mat)
375 } else if src_chmap.num_channels() > 1 {
376 let remix_mat = calculate_remix_matrix(src_chmap, dst_chmap);
377 ChannelOp::Remix(remix_mat)
379 let mut dup_mat: Vec<bool> = Vec::with_capacity(dst_chmap.num_channels());
380 for i in 0..dst_chmap.num_channels() {
381 let ch = dst_chmap.get_channel(i);
382 if ch.is_left() || ch.is_right() || ch == NAChannelType::C {
388 ChannelOp::DupMono(dup_mat)
391 let src_fmt = src_info.get_format();
392 let dst_fmt = dst_info.get_format();
393 let no_conversion = src_fmt == dst_fmt;
395 if no_conversion && no_channel_needs {
396 return Ok(src.clone());
399 let ret = alloc_audio_buffer(*dst_info, nsamples, dst_chmap.clone());
401 return Err(SoundConvertError::AllocError);
403 let mut dst_buf = ret.unwrap();
405 let sstep = src.get_audio_step().max(1);
406 let dstep = dst_buf.get_audio_step().max(1);
407 let sr: Box<dyn SampleReader> = match src {
408 NABufferType::AudioU8(ref ab) => {
409 let stride = ab.get_stride();
410 let data = ab.get_data();
412 Box::new(GenericSampleReader { data, stride })
414 Box::new(S8SampleReader { data, stride })
417 NABufferType::AudioI16(ref ab) => {
418 let data = ab.get_data();
419 let stride = ab.get_stride();
420 Box::new(GenericSampleReader { data, stride })
422 NABufferType::AudioI32(ref ab) => {
423 let data = ab.get_data();
424 let stride = ab.get_stride();
425 Box::new(GenericSampleReader { data, stride })
427 NABufferType::AudioF32(ref ab) => {
428 let data = ab.get_data();
429 let stride = ab.get_stride();
430 Box::new(GenericSampleReader { data, stride })
432 NABufferType::AudioPacked(ref ab) => {
433 let data = ab.get_data();
434 Box::new(PackedSampleReader::new(data, src_fmt))
436 _ => unimplemented!(),
438 let mut sw: Box<dyn SampleWriter> = match dst_buf {
439 NABufferType::AudioU8(ref mut ab) => {
440 let stride = ab.get_stride();
441 let data = ab.get_data_mut().unwrap();
442 Box::new(GenericSampleWriter { data, stride })
444 NABufferType::AudioI16(ref mut ab) => {
445 let stride = ab.get_stride();
446 let data = ab.get_data_mut().unwrap();
447 Box::new(GenericSampleWriter { data, stride })
449 NABufferType::AudioI32(ref mut ab) => {
450 let stride = ab.get_stride();
451 let data = ab.get_data_mut().unwrap();
452 Box::new(GenericSampleWriter { data, stride })
454 NABufferType::AudioF32(ref mut ab) => {
455 let stride = ab.get_stride();
456 let data = ab.get_data_mut().unwrap();
457 Box::new(GenericSampleWriter { data, stride })
459 NABufferType::AudioPacked(ref mut ab) => {
460 let data = ab.get_data_mut().unwrap();
461 Box::new(PackedSampleWriter::new(data, dst_fmt))
463 _ => unimplemented!(),
466 let into_float = dst_fmt.float;
468 let mut svec = vec![0; src_chmap.num_channels()];
469 let mut dvec = vec![0; dst_chmap.num_channels()];
472 for _ in 0..nsamples {
473 sr.get_samples_i32(spos, &mut svec);
474 if !channel_op.is_remix() {
475 apply_channel_op(&channel_op, &svec, &mut dvec);
477 remix_i32(&channel_op, &svec, &mut dvec);
479 sw.store_samples_i32(dpos, &dvec);
484 let mut svec = vec![0.0; src_chmap.num_channels()];
485 let mut dvec = vec![0.0; dst_chmap.num_channels()];
488 for _ in 0..nsamples {
489 sr.get_samples_f32(spos, &mut svec);
490 if !channel_op.is_remix() {
491 apply_channel_op(&channel_op, &svec, &mut dvec);
493 remix_f32(&channel_op, &svec, &mut dvec);
495 sw.store_samples_f32(dpos, &dvec);
505 /// Checks whether two channel maps are identical.
506 pub fn channel_maps_equal(a: &NAChannelMap, b: &NAChannelMap) -> bool {
507 if a.num_channels() != b.num_channels() { return false; }
508 for i in 0..a.num_channels() {
509 if a.get_channel(i) != b.get_channel(i) {
516 /// Checks whether two channel maps have identical channels (but maybe in different order).
517 pub fn channel_maps_reordered(a: &NAChannelMap, b: &NAChannelMap) -> bool {
518 if a.num_channels() != b.num_channels() { return false; }
519 let mut count_a = [0u8; 32];
520 let mut count_b = [0u8; 32];
521 for i in 0..a.num_channels() {
522 count_a[a.get_channel(i) as usize] += 1;
523 count_b[b.get_channel(i) as usize] += 1;
525 for (c0, c1) in count_a.iter().zip(count_b.iter()) {
533 /// Calculates permutation matrix for reordering channels from source channel map into destination one.
534 pub fn calculate_reorder_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec<usize> {
535 if src.num_channels() != dst.num_channels() { return Vec::new(); }
536 let num_channels = src.num_channels();
537 let mut reorder: Vec<usize> = Vec::with_capacity(num_channels);
538 for i in 0..num_channels {
539 let dst_ch = dst.get_channel(i);
540 for j in 0..num_channels {
541 if src.get_channel(j) == dst_ch {
547 if reorder.len() != num_channels { reorder.clear(); }
551 fn is_stereo(chmap: &NAChannelMap) -> bool {
552 (chmap.num_channels() == 2) &&
553 (chmap.get_channel(0) == NAChannelType::L) &&
554 (chmap.get_channel(1) == NAChannelType::R)
557 /// Calculates matrix of remixing coefficients for converting input channel layout into destination one.
558 pub fn calculate_remix_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec<f32> {
559 if is_stereo(src) && dst.num_channels() == 1 &&
560 (dst.get_channel(0) == NAChannelType::L || dst.get_channel(0) == NAChannelType::C) {
561 return vec![0.5, 0.5];
563 if src.num_channels() >= 5 && is_stereo(dst) {
564 let src_nch = src.num_channels();
565 let mut mat = vec![0.0f32; src_nch * 2];
566 let (l_mat, r_mat) = mat.split_at_mut(src_nch);
567 for ch in 0..src_nch {
568 match src.get_channel(ch) {
569 NAChannelType::L => l_mat[ch] = 1.0,
570 NAChannelType::R => r_mat[ch] = 1.0,
571 NAChannelType::C => { l_mat[ch] = SQRT_2 / 2.0; r_mat[ch] = SQRT_2 / 2.0; },
572 NAChannelType::Ls => l_mat[ch] = SQRT_2 / 2.0,
573 NAChannelType::Rs => r_mat[ch] = SQRT_2 / 2.0,
585 use std::str::FromStr;
586 use crate::formats::*;
590 let chcfg51 = NAChannelMap::from_str("L,R,C,LFE,Ls,Rs").unwrap();
591 let chcfg52 = NAChannelMap::from_str("C,L,R,Ls,Rs,LFE").unwrap();
592 let stereo = NAChannelMap::from_str("L,R").unwrap();
593 let reorder = calculate_reorder_matrix(&chcfg51, &chcfg52);
594 assert_eq!(reorder.as_slice(), [ 2, 0, 1, 4, 5, 3]);
595 let remix = calculate_remix_matrix(&chcfg51, &stereo);
596 assert_eq!(remix.as_slice(), [ 1.0, 0.0, SQRT_2 / 2.0, 0.0, SQRT_2 / 2.0, 0.0,
597 0.0, 1.0, SQRT_2 / 2.0, 0.0, 0.0, SQRT_2 / 2.0 ]);
600 fn test_conversion() {
601 const CHANNEL_VALUES: [u8; 6] = [ 140, 90, 130, 128, 150, 70 ];
602 let chcfg51 = NAChannelMap::from_str("L,R,C,LFE,Ls,Rs").unwrap();
603 let stereo = NAChannelMap::from_str("L,R").unwrap();
604 let src_ainfo = NAAudioInfo {
606 channels: chcfg51.num_channels() as u8,
607 format: SND_U8_FORMAT,
610 let mut dst_ainfo = NAAudioInfo {
612 channels: stereo.num_channels() as u8,
613 format: SND_S16P_FORMAT,
616 let mut src_frm = alloc_audio_buffer(src_ainfo, 42, chcfg51.clone()).unwrap();
617 if let NABufferType::AudioU8(ref mut abuf) = src_frm {
618 let data = abuf.get_data_mut().unwrap();
621 for ch in 0..chcfg51.num_channels() {
622 data[idx] = CHANNEL_VALUES[ch];
627 panic!("wrong buffer type");
630 let out_frm = convert_audio_frame(&src_frm, &dst_ainfo, &stereo).unwrap();
631 if let NABufferType::AudioI16(ref abuf) = out_frm {
632 let off0 = abuf.get_offset(0);
633 let off1 = abuf.get_offset(1);
634 let data = abuf.get_data();
638 assert_eq!(r, -19505);
640 panic!("wrong buffer type");
643 dst_ainfo.format = SND_F32P_FORMAT;
644 let out_frm = convert_audio_frame(&src_frm, &dst_ainfo, &stereo).unwrap();
645 if let NABufferType::AudioF32(ref abuf) = out_frm {
646 let off0 = abuf.get_offset(0);
647 let off1 = abuf.get_offset(1);
648 let data = abuf.get_data();
651 assert_eq!(l, 0.22633252);
652 assert_eq!(r, -0.6062342);
654 panic!("wrong buffer type");