oximedia-audio 0.1.0

Audio codec implementations for OxiMedia
Documentation
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Audio dithering for bit-depth reduction.
//!
//! Dithering adds a small amount of controlled noise before quantisation in
//! order to linearise the quantisation error and prevent unwanted harmonic
//! distortion in low-level signals.  This module supports several dither
//! shapes plus an optional noise-shaper that pushes quantisation noise into
//! frequency bands where it is less audible.
//!
//! # Quick start
//!
//! ```
//! use oximedia_audio::dither::{Ditherer, DitherType};
//!
//! let mut ditherer = Ditherer::new(DitherType::Tpdf);
//! let output = ditherer.process(0.123_f32, 16);
//! assert!(output.is_finite());
//! ```
//!
//! # Dither types
//!
//! | Variant | Description |
//! |---|---|
//! | [`DitherType::None`] | No dither (hard truncation) |
//! | [`DitherType::Rectangular`] | Uniform noise – ½ LSB RMS |
//! | [`DitherType::Triangular`] | Triangular PDF – lower distortion |
//! | [`DitherType::Tpdf`] | Triangular PDF (alias, most common) |
//! | [`DitherType::Highpass`] | Spectrally shaped high-pass dither |

#![forbid(unsafe_code)]
#![allow(clippy::cast_precision_loss)]

// ─────────────────────────────────────────────────────────────────────────────
// Deterministic pseudo-random source (no external crate required)
// ─────────────────────────────────────────────────────────────────────────────

/// Minimal xorshift32 PRNG – deterministic, cheap, and dependency-free.
#[derive(Clone, Debug)]
#[allow(dead_code)]
struct Xorshift32 {
    state: u32,
}

impl Xorshift32 {
    /// Seed must be non-zero; any non-zero seed is fine.
    fn new(seed: u32) -> Self {
        Self {
            state: if seed == 0 { 0xBAD_5EED } else { seed },
        }
    }

    /// Return the next pseudo-random `u32`.
    fn next_u32(&mut self) -> u32 {
        let mut x = self.state;
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        self.state = x;
        x
    }

    /// Return a uniform sample in `[−1, 1)`.
    fn next_f32(&mut self) -> f32 {
        // Map u32 → [0, 1) → [−1, 1)
        let u = self.next_u32() as f32 / u32::MAX as f32;
        u * 2.0 - 1.0
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// DitherType
// ─────────────────────────────────────────────────────────────────────────────

/// The type of dither noise added before quantisation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub enum DitherType {
    /// No dither – plain truncation.
    None,
    /// Rectangular (uniform) probability density – ½ LSB amplitude noise.
    Rectangular,
    /// Triangular probability density – sum of two rectangular distributions.
    Triangular,
    /// TPDF (Triangular Probability Density Function) – common alias for
    /// [`DitherType::Triangular`].
    Tpdf,
    /// Spectrally-shaped high-frequency dither (noise shaping).
    Highpass,
}

impl DitherType {
    /// Return a short human-readable name for the noise shape.
    #[must_use]
    pub fn noise_shape(&self) -> &str {
        match self {
            Self::None => "none",
            Self::Rectangular => "rectangular",
            Self::Triangular => "triangular",
            Self::Tpdf => "tpdf",
            Self::Highpass => "highpass",
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// NoiseShaper
// ─────────────────────────────────────────────────────────────────────────────

/// First-order or higher-order IIR noise shaper.
///
/// The shaper feeds the previous quantisation errors back through a set of
/// FIR coefficients, effectively pushing the noise energy into higher
/// frequencies where it is less perceptible.
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct NoiseShaper {
    /// Ring buffer of past quantisation errors.
    feedback: Vec<f32>,
    /// Shaping filter coefficients.
    coeffs: Vec<f32>,
    /// Current write position in the ring buffer.
    pos: usize,
}

impl NoiseShaper {
    /// Create a new noise shaper of the given order.
    ///
    /// Higher orders push more noise energy toward Nyquist, at the cost of
    /// a slightly higher total noise floor.
    ///
    /// # Arguments
    ///
    /// * `order` – Filter order (number of feedback taps; 0 disables shaping).
    #[must_use]
    pub fn new(order: usize) -> Self {
        // Simple high-frequency emphasis coefficients
        let coeffs = match order {
            0 => vec![],
            1 => vec![1.0_f32],
            2 => vec![1.5, -0.5],
            3 => vec![1.75, -1.0, 0.25],
            _ => {
                // Generic: linearly-decaying weights
                (0..order).map(|i| 1.0 - i as f32 / order as f32).collect()
            }
        };
        let len = coeffs.len().max(1);
        Self {
            feedback: vec![0.0; len],
            coeffs,
            pos: 0,
        }
    }

    /// Feed a quantisation error into the shaper and return the noise
    /// compensation term based on **previous** errors.
    ///
    /// The output reflects errors fed in on previous calls; the current
    /// `quantize_error` is stored and will influence future outputs.
    pub fn process(&mut self, quantize_error: f32) -> f32 {
        if self.coeffs.is_empty() {
            return 0.0;
        }

        // Compute the shaping output from previously stored errors
        // (read before writing, so k=0 references the oldest stored value
        //  at the current write position, not the new incoming error).
        let mut shaped = 0.0_f32;
        for (k, coeff) in self.coeffs.iter().enumerate() {
            // k=0 reads the slot we are about to overwrite – i.e., the
            // oldest sample in the ring buffer (one full period ago).
            let idx = (self.pos + self.feedback.len() - k) % self.feedback.len();
            shaped += coeff * self.feedback[idx];
        }

        // Store the new error at the current position
        self.feedback[self.pos] = quantize_error;

        // Advance ring pointer
        self.pos = (self.pos + 1) % self.feedback.len();
        shaped
    }

    /// Reset the feedback ring buffer to silence.
    pub fn reset(&mut self) {
        for x in &mut self.feedback {
            *x = 0.0;
        }
        self.pos = 0;
    }

    /// Return the shaper order (number of feedback taps).
    #[must_use]
    pub fn order(&self) -> usize {
        self.coeffs.len()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Ditherer
// ─────────────────────────────────────────────────────────────────────────────

/// Complete ditherer: noise generation + optional noise shaping + quantisation.
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct Ditherer {
    dither_type: DitherType,
    shaper: Option<NoiseShaper>,
    prng: Xorshift32,
}

impl Ditherer {
    /// Create a new ditherer.
    ///
    /// * [`DitherType::Highpass`] automatically attaches a first-order
    ///   noise shaper.
    /// * All other variants leave the shaper disabled.
    #[must_use]
    pub fn new(dither_type: DitherType) -> Self {
        let shaper = match dither_type {
            DitherType::Highpass => Some(NoiseShaper::new(1)),
            _ => None,
        };
        Self {
            dither_type,
            shaper,
            prng: Xorshift32::new(0x1234_5678),
        }
    }

    /// Create a new ditherer with an explicit noise shaper.
    #[must_use]
    pub fn with_shaper(dither_type: DitherType, shaper: NoiseShaper) -> Self {
        Self {
            dither_type,
            shaper: Some(shaper),
            prng: Xorshift32::new(0xDEAD_BEEF),
        }
    }

    /// Process a single sample, adding dither noise and quantising to
    /// `target_bits` bits.
    ///
    /// The input sample is assumed to be normalised to `[−1, 1]`.
    ///
    /// # Arguments
    ///
    /// * `sample` – Input sample in `[−1, 1]`.
    /// * `target_bits` – Target bit depth (e.g. 16, 20, 24).
    ///
    /// # Returns
    ///
    /// Quantised sample, still normalised to `[−1, 1]`.
    pub fn process(&mut self, sample: f32, target_bits: u8) -> f32 {
        let levels = (1u32 << (target_bits - 1)) as f32; // e.g. 32768 for 16-bit
        let lsb = 1.0 / levels; // one least-significant bit in normalised space

        // Generate dither noise
        let dither_noise = match self.dither_type {
            DitherType::None => 0.0,
            DitherType::Rectangular => self.prng.next_f32() * 0.5 * lsb,
            DitherType::Triangular | DitherType::Tpdf => {
                let r1 = self.prng.next_f32();
                let r2 = self.prng.next_f32();
                (r1 + r2) * 0.5 * lsb
            }
            DitherType::Highpass => {
                // White noise base; shaping happens via the feedback term
                self.prng.next_f32() * 0.5 * lsb
            }
        };

        let dithered = sample + dither_noise;

        // Quantise: round to nearest integer multiple of lsb, then clamp
        let quantised = (dithered * levels).round() / levels;
        let quantised = quantised.clamp(-1.0, 1.0 - lsb);

        // Feed quantisation error into the shaper for the next sample
        if let Some(ref mut shaper) = self.shaper {
            let error = quantised - sample;
            shaper.process(error);
        }

        quantised
    }

    /// Return the dither type.
    #[must_use]
    pub fn dither_type(&self) -> DitherType {
        self.dither_type
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Convenience function
// ─────────────────────────────────────────────────────────────────────────────

/// Dither and quantise an entire buffer.
///
/// # Arguments
///
/// * `samples` – Input samples (normalised `[−1, 1]`).
/// * `target_bits` – Target bit depth.
/// * `dither_type` – Type of dither to apply.
///
/// # Returns
///
/// A new `Vec<f32>` of quantised samples.
#[must_use]
#[allow(dead_code)]
pub fn dither_buffer(samples: &[f32], target_bits: u8, dither_type: DitherType) -> Vec<f32> {
    let mut ditherer = Ditherer::new(dither_type);
    samples
        .iter()
        .map(|&s| ditherer.process(s, target_bits))
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── DitherType ────────────────────────────────────────────────────────────

    #[test]
    fn test_dither_type_noise_shape_none() {
        assert_eq!(DitherType::None.noise_shape(), "none");
    }

    #[test]
    fn test_dither_type_noise_shape_rectangular() {
        assert_eq!(DitherType::Rectangular.noise_shape(), "rectangular");
    }

    #[test]
    fn test_dither_type_noise_shape_triangular() {
        assert_eq!(DitherType::Triangular.noise_shape(), "triangular");
    }

    #[test]
    fn test_dither_type_noise_shape_tpdf() {
        assert_eq!(DitherType::Tpdf.noise_shape(), "tpdf");
    }

    #[test]
    fn test_dither_type_noise_shape_highpass() {
        assert_eq!(DitherType::Highpass.noise_shape(), "highpass");
    }

    // ── NoiseShaper ───────────────────────────────────────────────────────────

    #[test]
    fn test_noise_shaper_order_zero_returns_zero() {
        let mut shaper = NoiseShaper::new(0);
        let out = shaper.process(0.5);
        assert_eq!(out, 0.0);
    }

    #[test]
    fn test_noise_shaper_order_returns_expected() {
        let shaper = NoiseShaper::new(3);
        assert_eq!(shaper.order(), 3);
    }

    #[test]
    fn test_noise_shaper_reset() {
        let mut shaper = NoiseShaper::new(2);
        shaper.process(1.0);
        shaper.process(0.5);
        shaper.reset();
        // After reset, the next process call uses zeroed feedback
        let out = shaper.process(0.0);
        assert_eq!(out, 0.0);
    }

    #[test]
    fn test_noise_shaper_first_order_feedback() {
        let mut shaper = NoiseShaper::new(1);
        // Feed in an error; next call should reflect it
        let _first = shaper.process(1.0);
        let second = shaper.process(0.0);
        // With order=1 the coefficient is 1.0, so second output = 1.0 * previous error
        assert!(
            second.abs() > 0.0,
            "Shaper should produce non-zero output after non-zero error, got {second}"
        );
    }

    // ── Ditherer::process ─────────────────────────────────────────────────────

    #[test]
    fn test_dither_none_output_is_finite() {
        let mut d = Ditherer::new(DitherType::None);
        assert!(d.process(0.5, 16).is_finite());
    }

    #[test]
    fn test_dither_none_quantises_cleanly() {
        // With no dither a DC value should quantise to the nearest level
        let mut d = Ditherer::new(DitherType::None);
        let out = d.process(0.0, 16);
        // 0.0 rounds to 0.0 at any bit depth
        assert!(out.abs() < 1.0 / 32768.0 + 1e-8);
    }

    #[test]
    fn test_dither_rectangular_output_in_range() {
        let mut d = Ditherer::new(DitherType::Rectangular);
        for _ in 0..1000 {
            let out = d.process(0.5, 16);
            assert!(out >= -1.0 && out <= 1.0, "Output out of range: {out}");
        }
    }

    #[test]
    fn test_dither_tpdf_output_in_range() {
        let mut d = Ditherer::new(DitherType::Tpdf);
        for _ in 0..1000 {
            let out = d.process(-0.5, 16);
            assert!(out >= -1.0 && out <= 1.0);
        }
    }

    #[test]
    fn test_dither_triangular_output_is_finite() {
        let mut d = Ditherer::new(DitherType::Triangular);
        for _ in 0..500 {
            assert!(d.process(0.25, 24).is_finite());
        }
    }

    #[test]
    fn test_dither_highpass_output_is_finite() {
        let mut d = Ditherer::new(DitherType::Highpass);
        for _ in 0..500 {
            assert!(d.process(0.1, 16).is_finite());
        }
    }

    #[test]
    fn test_dither_type_returned_correctly() {
        let d = Ditherer::new(DitherType::Tpdf);
        assert_eq!(d.dither_type(), DitherType::Tpdf);
    }

    // ── dither_buffer ─────────────────────────────────────────────────────────

    #[test]
    fn test_dither_buffer_length_preserved() {
        let input = vec![0.1_f32; 256];
        let output = dither_buffer(&input, 16, DitherType::Tpdf);
        assert_eq!(output.len(), input.len());
    }

    #[test]
    fn test_dither_buffer_all_finite() {
        let input: Vec<f32> = (0..256).map(|i| (i as f32 / 128.0) - 1.0).collect();
        let output = dither_buffer(&input, 16, DitherType::Rectangular);
        assert!(output.iter().all(|x| x.is_finite()));
    }

    #[test]
    fn test_dither_buffer_values_in_range() {
        let input = vec![0.9_f32; 128];
        let output = dither_buffer(&input, 16, DitherType::Tpdf);
        for &v in &output {
            assert!(v >= -1.0 && v <= 1.0, "Out of range: {v}");
        }
    }
}