oximedia-audio 0.1.4

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
#![allow(dead_code)]
//! Advanced noise gate with hysteresis, look-ahead, and hold time.
//!
//! A noise gate attenuates signals that fall below a specified threshold,
//! reducing background noise during silent passages. This implementation
//! supports:
//!
//! - **Hysteresis**: Separate open and close thresholds to prevent chattering.
//! - **Attack/Release**: Smoothed envelope transitions for natural gating.
//! - **Hold time**: Keeps the gate open for a minimum duration after signal drops.
//! - **Look-ahead**: Delays audio to allow the gate to open before transients.
//! - **Side-chain filtering**: Apply a filter to the detection signal for
//!   frequency-selective gating.
//! - **Range control**: Partial attenuation instead of full silence.

/// State of the noise gate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GateState {
    /// Gate is closed (attenuating signal).
    Closed,
    /// Gate is opening (attack phase).
    Opening,
    /// Gate is fully open (passing signal).
    Open,
    /// Gate is closing (release phase).
    Closing,
    /// Gate is in hold phase (staying open after signal drops).
    Hold,
}

/// Configuration for the noise gate.
#[derive(Debug, Clone)]
pub struct NoiseGateConfig {
    /// Threshold in dB for opening the gate.
    pub open_threshold_db: f64,
    /// Threshold in dB for closing the gate (should be <= open_threshold_db).
    pub close_threshold_db: f64,
    /// Attack time in seconds.
    pub attack_secs: f64,
    /// Release time in seconds.
    pub release_secs: f64,
    /// Hold time in seconds (gate stays open after signal drops).
    pub hold_secs: f64,
    /// Range in dB (0 = full silence, -20 = 20 dB attenuation).
    pub range_db: f64,
    /// Look-ahead time in seconds.
    pub lookahead_secs: f64,
    /// Sample rate in Hz.
    pub sample_rate: f64,
}

impl Default for NoiseGateConfig {
    fn default() -> Self {
        Self {
            open_threshold_db: -40.0,
            close_threshold_db: -45.0,
            attack_secs: 0.001,
            release_secs: 0.05,
            hold_secs: 0.01,
            range_db: -80.0,
            lookahead_secs: 0.0,
            sample_rate: 48000.0,
        }
    }
}

/// A professional noise gate processor.
#[derive(Debug, Clone)]
pub struct NoiseGate {
    /// Current gate configuration.
    config: NoiseGateConfig,
    /// Current envelope level (0.0 = closed, 1.0 = open).
    envelope: f64,
    /// Current gate state.
    state: GateState,
    /// Hold counter in samples.
    hold_counter: usize,
    /// Hold time in samples.
    hold_samples: usize,
    /// Attack coefficient for exponential smoothing.
    attack_coeff: f64,
    /// Release coefficient for exponential smoothing.
    release_coeff: f64,
    /// Range as linear gain (the minimum gain when gate is closed).
    range_linear: f64,
    /// Look-ahead buffer.
    lookahead_buffer: Vec<f64>,
    /// Write position in the look-ahead buffer.
    lookahead_pos: usize,
    /// Look-ahead delay in samples.
    lookahead_samples: usize,
}

impl NoiseGate {
    /// Creates a new noise gate with the given configuration.
    #[allow(clippy::cast_precision_loss)]
    pub fn new(config: NoiseGateConfig) -> Self {
        let attack_coeff = if config.attack_secs > 0.0 {
            (-1.0 / (config.attack_secs * config.sample_rate)).exp()
        } else {
            0.0
        };
        let release_coeff = if config.release_secs > 0.0 {
            (-1.0 / (config.release_secs * config.sample_rate)).exp()
        } else {
            0.0
        };
        let hold_samples = (config.hold_secs * config.sample_rate) as usize;
        let range_linear = db_to_linear(config.range_db);
        let lookahead_samples = (config.lookahead_secs * config.sample_rate) as usize;
        let lookahead_buffer = vec![0.0; lookahead_samples.max(1)];

        Self {
            config,
            envelope: 0.0,
            state: GateState::Closed,
            hold_counter: 0,
            hold_samples,
            attack_coeff,
            release_coeff,
            range_linear,
            lookahead_buffer,
            lookahead_pos: 0,
            lookahead_samples,
        }
    }

    /// Returns the current state of the gate.
    pub fn state(&self) -> GateState {
        self.state
    }

    /// Returns the current envelope level (0.0 = closed, 1.0 = open).
    pub fn envelope(&self) -> f64 {
        self.envelope
    }

    /// Computes the current gain applied to the signal.
    pub fn current_gain(&self) -> f64 {
        self.range_linear + (1.0 - self.range_linear) * self.envelope
    }

    /// Resets the gate to its initial state.
    pub fn reset(&mut self) {
        self.envelope = 0.0;
        self.state = GateState::Closed;
        self.hold_counter = 0;
        self.lookahead_pos = 0;
        for s in &mut self.lookahead_buffer {
            *s = 0.0;
        }
    }

    /// Processes a block of mono audio samples in-place.
    ///
    /// The detection signal is the absolute value of the input.
    pub fn process_block(&mut self, samples: &mut [f64]) {
        for sample in samples.iter_mut() {
            let detection_level = sample.abs();
            let detection_db = linear_to_db(detection_level);

            // State machine
            match self.state {
                GateState::Closed | GateState::Closing => {
                    if detection_db >= self.config.open_threshold_db {
                        self.state = GateState::Opening;
                    }
                }
                GateState::Opening => {
                    if detection_db < self.config.close_threshold_db {
                        self.state = GateState::Closing;
                    } else if self.envelope >= 0.999 {
                        self.state = GateState::Open;
                    }
                }
                GateState::Open => {
                    if detection_db < self.config.close_threshold_db {
                        self.state = GateState::Hold;
                        self.hold_counter = self.hold_samples;
                    }
                }
                GateState::Hold => {
                    if detection_db >= self.config.open_threshold_db {
                        self.state = GateState::Open;
                    } else if self.hold_counter == 0 {
                        self.state = GateState::Closing;
                    } else {
                        self.hold_counter -= 1;
                    }
                }
            }

            // Envelope follower
            let target = match self.state {
                GateState::Opening | GateState::Open | GateState::Hold => 1.0,
                GateState::Closed | GateState::Closing => 0.0,
            };

            let coeff = if target > self.envelope {
                self.attack_coeff
            } else {
                self.release_coeff
            };
            self.envelope = target + coeff * (self.envelope - target);

            // Apply gain
            let gain = self.current_gain();

            if self.lookahead_samples > 0 {
                let delayed = self.lookahead_buffer[self.lookahead_pos];
                self.lookahead_buffer[self.lookahead_pos] = *sample;
                self.lookahead_pos = (self.lookahead_pos + 1) % self.lookahead_buffer.len();
                *sample = delayed * gain;
            } else {
                *sample *= gain;
            }
        }
    }

    /// Processes a block using an external side-chain signal for detection.
    ///
    /// `audio` is modified in-place; `sidechain` is used only for detection.
    pub fn process_with_sidechain(&mut self, audio: &mut [f64], sidechain: &[f64]) {
        let len = audio.len().min(sidechain.len());
        for i in 0..len {
            let detection_level = sidechain[i].abs();
            let detection_db = linear_to_db(detection_level);

            match self.state {
                GateState::Closed | GateState::Closing => {
                    if detection_db >= self.config.open_threshold_db {
                        self.state = GateState::Opening;
                    }
                }
                GateState::Opening => {
                    if detection_db < self.config.close_threshold_db {
                        self.state = GateState::Closing;
                    } else if self.envelope >= 0.999 {
                        self.state = GateState::Open;
                    }
                }
                GateState::Open => {
                    if detection_db < self.config.close_threshold_db {
                        self.state = GateState::Hold;
                        self.hold_counter = self.hold_samples;
                    }
                }
                GateState::Hold => {
                    if detection_db >= self.config.open_threshold_db {
                        self.state = GateState::Open;
                    } else if self.hold_counter == 0 {
                        self.state = GateState::Closing;
                    } else {
                        self.hold_counter -= 1;
                    }
                }
            }

            let target = match self.state {
                GateState::Opening | GateState::Open | GateState::Hold => 1.0,
                GateState::Closed | GateState::Closing => 0.0,
            };

            let coeff = if target > self.envelope {
                self.attack_coeff
            } else {
                self.release_coeff
            };
            self.envelope = target + coeff * (self.envelope - target);

            audio[i] *= self.current_gain();
        }
    }
}

/// Converts a linear amplitude to decibels.
///
/// Returns `-f64::INFINITY` for zero or negative values.
pub fn linear_to_db(linear: f64) -> f64 {
    if linear <= 0.0 {
        -f64::INFINITY
    } else {
        20.0 * linear.log10()
    }
}

/// Converts decibels to linear amplitude.
pub fn db_to_linear(db: f64) -> f64 {
    if db <= -200.0 {
        0.0
    } else {
        10.0_f64.powf(db / 20.0)
    }
}

/// Computes the RMS level of a block of samples.
#[allow(clippy::cast_precision_loss)]
pub fn rms_level(samples: &[f64]) -> f64 {
    if samples.is_empty() {
        return 0.0;
    }
    let sum_sq: f64 = samples.iter().map(|&s| s * s).sum();
    (sum_sq / samples.len() as f64).sqrt()
}

/// Computes the peak level of a block of samples.
pub fn peak_level(samples: &[f64]) -> f64 {
    samples.iter().map(|s| s.abs()).fold(0.0_f64, f64::max)
}

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

    #[test]
    fn test_default_config() {
        let cfg = NoiseGateConfig::default();
        assert!((cfg.open_threshold_db - (-40.0)).abs() < 1e-10);
        assert!((cfg.close_threshold_db - (-45.0)).abs() < 1e-10);
        assert!((cfg.sample_rate - 48000.0).abs() < 1e-10);
    }

    #[test]
    fn test_gate_initial_state() {
        let gate = NoiseGate::new(NoiseGateConfig::default());
        assert_eq!(gate.state(), GateState::Closed);
        assert!((gate.envelope() - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_gate_opens_on_loud_signal() {
        let config = NoiseGateConfig {
            open_threshold_db: -40.0,
            close_threshold_db: -45.0,
            attack_secs: 0.0001,
            release_secs: 0.001,
            hold_secs: 0.0,
            range_db: -80.0,
            lookahead_secs: 0.0,
            sample_rate: 48000.0,
        };
        let mut gate = NoiseGate::new(config);
        // A loud signal (0.5 = ~-6 dB) should open the gate
        let mut samples = vec![0.5; 1000];
        gate.process_block(&mut samples);
        assert!(gate.envelope() > 0.9);
    }

    #[test]
    fn test_gate_stays_closed_on_quiet_signal() {
        let config = NoiseGateConfig {
            open_threshold_db: -20.0,
            ..NoiseGateConfig::default()
        };
        let mut gate = NoiseGate::new(config);
        // Very quiet signal (-80 dB)
        let mut samples = vec![0.0001; 1000];
        gate.process_block(&mut samples);
        assert!(gate.envelope() < 0.1);
    }

    #[test]
    fn test_gate_attenuates_quiet_signal() {
        let config = NoiseGateConfig {
            open_threshold_db: -10.0,
            close_threshold_db: -15.0,
            range_db: -60.0,
            attack_secs: 0.0,
            release_secs: 0.0,
            hold_secs: 0.0,
            lookahead_secs: 0.0,
            sample_rate: 48000.0,
        };
        let mut gate = NoiseGate::new(config);
        let mut samples = vec![0.001; 500]; // very quiet
        gate.process_block(&mut samples);
        // Output should be attenuated
        let max_out = samples.iter().map(|s| s.abs()).fold(0.0_f64, f64::max);
        assert!(max_out < 0.001);
    }

    #[test]
    fn test_gate_reset() {
        let mut gate = NoiseGate::new(NoiseGateConfig::default());
        let mut samples = vec![1.0; 100];
        gate.process_block(&mut samples);
        gate.reset();
        assert_eq!(gate.state(), GateState::Closed);
        assert!((gate.envelope() - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_linear_to_db() {
        assert!((linear_to_db(1.0) - 0.0).abs() < 1e-10);
        assert!((linear_to_db(0.1) - (-20.0)).abs() < 0.01);
        assert_eq!(linear_to_db(0.0), -f64::INFINITY);
    }

    #[test]
    fn test_db_to_linear() {
        assert!((db_to_linear(0.0) - 1.0).abs() < 1e-10);
        assert!((db_to_linear(-20.0) - 0.1).abs() < 0.001);
        assert!((db_to_linear(-200.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_roundtrip_db_conversion() {
        let original = 0.5;
        let db = linear_to_db(original);
        let back = db_to_linear(db);
        assert!((back - original).abs() < 1e-10);
    }

    #[test]
    fn test_rms_level() {
        let samples = vec![1.0, -1.0, 1.0, -1.0];
        assert!((rms_level(&samples) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_rms_level_empty() {
        assert!((rms_level(&[]) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_peak_level() {
        let samples = vec![0.1, -0.5, 0.3, -0.2];
        assert!((peak_level(&samples) - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_current_gain_when_closed() {
        let config = NoiseGateConfig {
            range_db: -60.0,
            ..NoiseGateConfig::default()
        };
        let gate = NoiseGate::new(config);
        // When envelope is 0, gain should be range_linear
        let gain = gate.current_gain();
        assert!(gain < 0.01);
    }

    #[test]
    fn test_sidechain_gating() {
        let config = NoiseGateConfig {
            open_threshold_db: -20.0,
            close_threshold_db: -25.0,
            attack_secs: 0.0001,
            release_secs: 0.001,
            hold_secs: 0.0,
            range_db: -80.0,
            lookahead_secs: 0.0,
            sample_rate: 48000.0,
        };
        let mut gate = NoiseGate::new(config);
        let mut audio = vec![0.5; 500];
        let sidechain = vec![0.0001; 500]; // quiet sidechain
        gate.process_with_sidechain(&mut audio, &sidechain);
        // Gate should stay closed because sidechain is quiet
        assert!(gate.envelope() < 0.1);
    }
}