X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fsoundcvt%2Fmod.rs;h=bea95d19ccdb0e99afc73883f1f47de3e39739ed;hb=152e8f78a8e9bec46513e55394fe85e79d162a30;hp=bc34b23da0e386a25b4ed6bfbf9961f802cb3452;hpb=8809c626d8dbe3c3f09f15396410680cc4c1fbba;p=nihav.git diff --git a/nihav-core/src/soundcvt/mod.rs b/nihav-core/src/soundcvt/mod.rs index bc34b23..bea95d1 100644 --- a/nihav-core/src/soundcvt/mod.rs +++ b/nihav-core/src/soundcvt/mod.rs @@ -1,3 +1,7 @@ +//! Sound format conversion. +//! +//! 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. +//! Eventually this might support resampling as well. pub use crate::formats::{NASoniton,NAChannelMap}; pub use crate::frame::{NAAudioBuffer,NAAudioInfo,NABufferType}; use crate::formats::NAChannelType; @@ -5,10 +9,14 @@ use crate::frame::alloc_audio_buffer; use crate::io::byteio::*; use std::f32::consts::SQRT_2; +/// A list specifying general sound conversion errors. #[derive(Clone,Copy,Debug,PartialEq)] pub enum SoundConvertError { + /// Invalid input arguments. InvalidInput, + /// Allocation failed. AllocError, + /// Requested feature is not supported. Unsupported, } @@ -304,7 +312,8 @@ impl SampleWriter for PackedSampleWriter<'_> { } } -pub fn convert_audio_frame(src: &NABufferType, dst_info: &NAAudioInfo, dst_chmap: &NAChannelMap) -> +/// Converts input audio buffer into desired format and returns a newly allocated buffer. +pub fn convert_audio_frame(src: &NABufferType, dst_info: &NAAudioInfo, dst_chmap: &NAChannelMap) -> Result { let mut nsamples = src.get_audio_length(); if nsamples == 0 { @@ -359,6 +368,8 @@ Result { } let mut dst_buf = ret.unwrap(); + let sstep = src.get_audio_step(); + let dstep = dst_buf.get_audio_step(); let sr: Box = match src { NABufferType::AudioU8(ref ab) => { let stride = ab.get_stride(); @@ -418,33 +429,42 @@ Result { if !into_float { let mut svec = vec![0; src_chmap.num_channels()]; let mut dvec = vec![0; dst_chmap.num_channels()]; - for i in 0..nsamples { - sr.get_samples_i32(i, &mut svec); + let mut spos = 0; + let mut dpos = 0; + for _ in 0..nsamples { + sr.get_samples_i32(spos, &mut svec); if !channel_op.is_remix() { apply_channel_op(&channel_op, &svec, &mut dvec); } else { remix_i32(&channel_op, &svec, &mut dvec); } - sw.store_samples_i32(i, &dvec); + sw.store_samples_i32(dpos, &dvec); + spos += sstep; + dpos += dstep; } } else { let mut svec = vec![0.0; src_chmap.num_channels()]; let mut dvec = vec![0.0; dst_chmap.num_channels()]; - for i in 0..nsamples { - sr.get_samples_f32(i, &mut svec); + let mut spos = 0; + let mut dpos = 0; + for _ in 0..nsamples { + sr.get_samples_f32(spos, &mut svec); if !channel_op.is_remix() { apply_channel_op(&channel_op, &svec, &mut dvec); } else { remix_f32(&channel_op, &svec, &mut dvec); } - sw.store_samples_f32(i, &dvec); + sw.store_samples_f32(dpos, &dvec); + spos += sstep; + dpos += dstep; } } drop(sw); - + Ok(dst_buf) } +/// Checks whether two channel maps are identical. pub fn channel_maps_equal(a: &NAChannelMap, b: &NAChannelMap) -> bool { if a.num_channels() != b.num_channels() { return false; } for i in 0..a.num_channels() { @@ -455,6 +475,7 @@ pub fn channel_maps_equal(a: &NAChannelMap, b: &NAChannelMap) -> bool { true } +/// Checks whether two channel maps have identical channels (but maybe in different order). pub fn channel_maps_reordered(a: &NAChannelMap, b: &NAChannelMap) -> bool { if a.num_channels() != b.num_channels() { return false; } let mut count_a = [0u8; 32]; @@ -471,6 +492,7 @@ pub fn channel_maps_reordered(a: &NAChannelMap, b: &NAChannelMap) -> bool { true } +/// Calculates permutation matrix for reordering channels from source channel map into destination one. pub fn calculate_reorder_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec { if src.num_channels() != dst.num_channels() { return Vec::new(); } let num_channels = src.num_channels(); @@ -490,10 +512,11 @@ pub fn calculate_reorder_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec bool { (chmap.num_channels() == 2) && - (chmap.get_channel(0) == NAChannelType::L) && + (chmap.get_channel(0) == NAChannelType::L) && (chmap.get_channel(1) == NAChannelType::R) } +/// Calculates matrix of remixing coefficients for converting input channel layout into destination one. pub fn calculate_remix_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec { if is_stereo(src) && dst.num_channels() == 1 && (dst.get_channel(0) == NAChannelType::L || dst.get_channel(0) == NAChannelType::C) { @@ -553,7 +576,7 @@ mod test { block_len: 512, }; let mut src_frm = alloc_audio_buffer(src_ainfo, 42, chcfg51.clone()).unwrap(); - if let NABufferType::AudioPacked(ref mut abuf) = src_frm { + if let NABufferType::AudioU8(ref mut abuf) = src_frm { let data = abuf.get_data_mut().unwrap(); let mut idx = 0; for _ in 0..42 {