1
2#[cfg(feature = "internal-no-panic")]
3use no_panic::no_panic;
4
5const ALAW_VALUES: &[i16; 256] = &[
7 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
8 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
9 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
10 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
11 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
12 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
13 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
14 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
15 -344, -328, -376, -360, -280, -264, -312, -296,
16 -472, -456, -504, -488, -408, -392, -440, -424,
17 -88, -72, -120, -104, -24, -8, -56, -40,
18 -216, -200, -248, -232, -152, -136, -184, -168,
19 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
20 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
21 -688, -656, -752, -720, -560, -528, -624, -592,
22 -944, -912, -1008, -976, -816, -784, -880, -848,
23 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
24 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
25 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
26 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
27 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
28 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
29 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
30 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
31 344, 328, 376, 360, 280, 264, 312, 296,
32 472, 456, 504, 488, 408, 392, 440, 424,
33 88, 72, 120, 104, 24, 8, 56, 40,
34 216, 200, 248, 232, 152, 136, 184, 168,
35 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
36 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
37 688, 656, 752, 720, 560, 528, 624, 592,
38 944, 912, 1008, 976, 816, 784, 880, 848,
39];
40
41#[cfg_attr(feature = "internal-no-panic", no_panic)]
43#[inline(always)]
44pub fn decode_alaw(encoded: u8) -> i16 {
45 ALAW_VALUES[usize::from(encoded)]
46}
47
48#[cfg_attr(feature = "internal-no-panic", no_panic)]
58#[inline(always)]
59pub fn encode_alaw(linear: i16) -> u8 {
60 let sign = if linear >= 0 {
61 0x00
62 } else {
63 0x80
64 };
65 #[allow(clippy::cast_sign_loss)] let linear = (linear >> 3) as u16;
67 let inputval = u32::from(if sign == 0x80 {
68 linear ^ 0xffff
70 } else {
71 linear
72 });
73 let segment = (u32::BITS - (inputval | 0b11111).leading_zeros()) - 5;
78 let shift = if segment < 2 { 1 } else { segment };
80 let compressed_code_word: u32 = (segment << 4) | ((inputval >> shift) & 0b1111);
81 #[allow(clippy::cast_possible_truncation)] let result = (sign | compressed_code_word as u8) ^ 0xd5;
83 result
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_decode_alaw() {
92 assert_eq!(decode_alaw(0), -5504);
94 assert_eq!(decode_alaw(128), 5504);
95 assert_eq!(decode_alaw(255), 848);
96 }
97
98 #[test]
99 fn test_encode_alaw() {
100 let buffer = include_bytes!("../test-files/alaw-reference.bin");
103 let mut bi = 0;
104 for i in -32768..=32767 {
105 let encoded = encode_alaw(i);
106 assert_eq!(buffer[bi], encoded);
107 bi += 1;
108 }
109 }
110}