Skip to main content

qualia_audio/
convert.rs

1//! Sample format and channel conversion (caller-buffered after setup).
2
3use crate::types::{AudioError, AudioView, SampleFormat};
4
5/// Decode mono f32 into `out` (len ≥ frames). Interleaved multi-channel is averaged.
6pub fn to_mono_f32(view: AudioView<'_>, out: &mut [f32]) -> Result<usize, AudioError> {
7    if !view.is_well_formed() {
8        return Err(AudioError::MalformedAudio);
9    }
10    let n = view.frames as usize;
11    if out.len() < n {
12        return Err(AudioError::OutputBufferTooSmall);
13    }
14    let ch = view.channels as usize;
15    let bps = view.bytes_per_sample() as usize;
16    let stride = view.frame_stride_bytes as usize;
17    for i in 0..n {
18        let base = i * stride;
19        let mut acc = 0.0f32;
20        for c in 0..ch {
21            let off = base + c * bps;
22            acc += sample_at(view, off);
23        }
24        out[i] = acc / ch as f32;
25    }
26    Ok(n)
27}
28
29fn sample_at(view: AudioView<'_>, off: usize) -> f32 {
30    let b = view.bytes;
31    match view.format {
32        SampleFormat::I16 => {
33            if off + 1 >= b.len() {
34                return 0.0;
35            }
36            let v = i16::from_le_bytes([b[off], b[off + 1]]);
37            v as f32 / 32768.0
38        }
39        SampleFormat::I32 => {
40            if off + 3 >= b.len() {
41                return 0.0;
42            }
43            let v = i32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]]);
44            v as f32 / 2147483648.0
45        }
46        SampleFormat::F32 => {
47            if off + 3 >= b.len() {
48                return 0.0;
49            }
50            f32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])
51        }
52        SampleFormat::I24Packed => {
53            if off + 2 >= b.len() {
54                return 0.0;
55            }
56            let v =
57                (b[off] as i32) | ((b[off + 1] as i32) << 8) | ((b[off + 2] as i8 as i32) << 16);
58            v as f32 / 8388608.0
59        }
60    }
61}
62
63/// Encode mono f32 as interleaved i16 LE into `out`.
64pub fn mono_f32_to_i16_le(samples: &[f32], out: &mut [u8]) -> Result<(), AudioError> {
65    if out.len() < samples.len() * 2 {
66        return Err(AudioError::OutputBufferTooSmall);
67    }
68    etalon_signal::resample::pcm_f32_to_i16_le_into(samples, out);
69    Ok(())
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn i16_mono_round() {
78        let raw = [0i16, 16384i16, -16384i16];
79        let mut bytes = [0u8; 6];
80        for (i, v) in raw.iter().enumerate() {
81            let b = v.to_le_bytes();
82            bytes[i * 2] = b[0];
83            bytes[i * 2 + 1] = b[1];
84        }
85        let view = AudioView {
86            bytes: &bytes,
87            frames: 3,
88            channels: 1,
89            sample_rate: 16000,
90            frame_stride_bytes: 2,
91            format: SampleFormat::I16,
92        };
93        let mut out = [0.0f32; 3];
94        to_mono_f32(view, &mut out).unwrap();
95        assert!((out[1] - 0.5).abs() < 0.01);
96    }
97}