1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Audio companding: the non-linear µ-law and "A"-law compression curves used by the ITU-T
//! G.711 telephony standard to compress a wide-dynamic-range linear audio sample down to a
//! lower bit depth with minimal perceptual loss (Steven W. Smith, "The Scientist and
//! Engineer's Guide to DSP", Ch. 22, Eq. 22-1 / 22-2).
//!
//! All functions operate on samples normalized to `-1.0..=1.0`.
use crateFloatMath;
const MU: f32 = 255.0;
const A_LAW: f32 = 87.6;
// Precomputed so the per-sample hot path only needs one transcendental call, not two.
const LN_1P_MU: f32 = 5.545_177_5; // ln(1 + MU)
const LN_A_LAW: f32 = 4.472_781; // ln(A_LAW)
const A_LAW_DENOM: f32 = 1.0 + LN_A_LAW; // 1 + ln(A_LAW)
/// Compresses a normalized linear sample `x` (`-1.0..=1.0`) using µ255-law companding
/// (Eq. 22-1), expanding resolution for small amplitudes at the expense of large ones.
/// Expands a µ-law-compressed sample `y` (`-1.0..=1.0`) back to a normalized linear sample,
/// inverting [`mu_law_compress_f32`].
/// Compresses a normalized linear sample `x` (`-1.0..=1.0`) using "A"-law companding
/// (Eq. 22-2): a linear segment near zero, transitioning to a logarithmic curve.
/// Expands an "A"-law-compressed sample `y` (`-1.0..=1.0`) back to a normalized linear sample,
/// inverting [`a_law_compress_f32`].