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