voice_engine/media/codecs/
pcmu.rs

1use super::{Decoder, Encoder};
2use crate::media::{PcmBuf, Sample};
3
4const BIAS: i16 = 0x84;
5const CLIP: i16 = 32635;
6
7const fn linear2ulaw_algo(mut sample: i16) -> u8 {
8    // Clip the sample to prevent overflow
9    if sample > CLIP {
10        sample = CLIP;
11    } else if sample < -CLIP {
12        sample = -CLIP;
13    }
14
15    // Get the sample's sign and make it positive
16    let sign: i16 = if sample < 0 { 0x80 } else { 0x00 };
17    if sign == 0x80 {
18        sample = -sample;
19    }
20
21    // Add bias
22    sample += BIAS;
23
24    // Compute segment number and step size
25    let mut segment: i16 = 0;
26    let mut value = sample;
27    while value >= 256 {
28        segment += 1;
29        value >>= 1;
30    }
31
32    // Combine sign, segment, and quantization
33    let uval = if segment >= 8 {
34        0x7F ^ sign
35    } else {
36        let shift = if segment == 0 { 7 } else { segment + 3 };
37        let mask = 0xFF ^ (0xFF >> shift);
38        (sign | (segment << 4) | ((sample >> (segment + 3)) & 0x0F)) ^ mask
39    };
40
41    uval as u8
42}
43
44static MULAW_ENCODE_TABLE: [u8; 65536] = {
45    let mut table = [0; 65536];
46    let mut i = 0;
47    while i < 65536 {
48        let s = (i as i32 - 32768) as i16;
49        table[i] = linear2ulaw_algo(s);
50        i += 1;
51    }
52    table
53};
54
55static MULAW_DECODE_TABLE: [i16; 256] = [
56    -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860,
57    -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
58    -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908,
59    -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772,
60    -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108,
61    -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180,
62    -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556,
63    -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212,
64    -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32,
65    -24, -16, -8, 0, 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884,
66    20860, 19836, 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
67    11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396,
68    6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132,
69    3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500,
70    1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876, 844, 812, 780, 748, 716, 684, 652,
71    620, 588, 556, 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276, 260, 244, 228, 212,
72    196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0,
73];
74
75/// Decodes a single μ-law encoded byte to a 16-bit PCM sample
76fn decode_mu_law(mu_law_sample: u8) -> i16 {
77    MULAW_DECODE_TABLE[mu_law_sample as usize]
78}
79
80/// Decoder for μ-law (PCMU) format
81#[derive(Default)]
82pub struct PcmuDecoder {}
83
84impl PcmuDecoder {
85    /// Creates a new PcmuDecoder instance
86    pub fn new() -> Self {
87        Self {}
88    }
89}
90
91impl Decoder for PcmuDecoder {
92    fn decode(&mut self, data: &[u8]) -> PcmBuf {
93        data.iter().map(|&sample| decode_mu_law(sample)).collect()
94    }
95
96    fn sample_rate(&self) -> u32 {
97        8000
98    }
99
100    fn channels(&self) -> u16 {
101        1
102    }
103}
104
105/// Encoder for μ-law (PCMU) format
106#[derive(Default)]
107pub struct PcmuEncoder {}
108
109impl PcmuEncoder {
110    /// Creates a new PcmuEncoder instance
111    pub fn new() -> Self {
112        Self {}
113    }
114
115    /// Converts a linear 16-bit PCM sample to a μ-law encoded byte using lookup table
116    fn linear2ulaw(&self, sample: i16) -> u8 {
117        let index = (sample as i32 + 32768) as usize;
118        MULAW_ENCODE_TABLE[index]
119    }
120}
121
122impl Encoder for PcmuEncoder {
123    fn encode(&mut self, samples: &[Sample]) -> Vec<u8> {
124        samples
125            .iter()
126            .map(|&sample| self.linear2ulaw(sample))
127            .collect()
128    }
129
130    fn sample_rate(&self) -> u32 {
131        8000
132    }
133
134    fn channels(&self) -> u16 {
135        1
136    }
137}