oximedia-audio 0.1.6

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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Meter ballistics implementation.
//!
//! Provides time-domain response characteristics for various meter types.

use std::collections::VecDeque;

/// Ballistics configuration for meters.
#[derive(Clone, Debug)]
pub struct BallisticsConfig {
    /// Integration time constant in seconds.
    pub integration_time: f64,
    /// Attack time constant in seconds.
    pub attack_time: f64,
    /// Release time constant in seconds.
    pub release_time: f64,
    /// Peak hold time in seconds (0.0 = no hold).
    pub peak_hold_time: f64,
    /// Return to zero time in seconds (0.0 = instant).
    pub return_time: f64,
    /// Sample rate in Hz.
    pub sample_rate: f64,
}

impl BallisticsConfig {
    /// Create VU meter ballistics (IEC 60268-10).
    ///
    /// - Integration time: 300ms
    /// - Attack time: 300ms
    /// - Release time: 300ms
    #[must_use]
    pub fn vu_meter(sample_rate: f64) -> Self {
        Self {
            integration_time: 0.300,
            attack_time: 0.300,
            release_time: 0.300,
            peak_hold_time: 0.0,
            return_time: 1.0,
            sample_rate,
        }
    }

    /// Create BBC PPM ballistics (BS.6840).
    ///
    /// - Integration time: 10ms
    /// - Attack time: 10ms (fast rise)
    /// - Release time: 2.8s (slow fall)
    #[must_use]
    pub fn bbc_ppm(sample_rate: f64) -> Self {
        Self {
            integration_time: 0.010,
            attack_time: 0.010,
            release_time: 2.8,
            peak_hold_time: 1.0,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Create EBU PPM ballistics (IEC 60268-10 Type IIa).
    ///
    /// - Integration time: 10ms
    /// - Attack time: 5ms
    /// - Release time: 1.7s (20dB in 1.7s)
    #[must_use]
    pub fn ebu_ppm(sample_rate: f64) -> Self {
        Self {
            integration_time: 0.010,
            attack_time: 0.005,
            release_time: 1.7,
            peak_hold_time: 0.0,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Create Nordic PPM ballistics (NRK/SR/DR/YLE).
    ///
    /// - Integration time: 5ms
    /// - Attack time: 5ms
    /// - Release time: 1.5s
    #[must_use]
    pub fn nordic_ppm(sample_rate: f64) -> Self {
        Self {
            integration_time: 0.005,
            attack_time: 0.005,
            release_time: 1.5,
            peak_hold_time: 0.0,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Create DIN PPM ballistics (IEC 60268-10 Type I).
    ///
    /// - Integration time: 10ms
    /// - Attack time: 10ms (instantaneous)
    /// - Release time: 1.5s (20dB in 1.5s)
    #[must_use]
    pub fn din_ppm(sample_rate: f64) -> Self {
        Self {
            integration_time: 0.010,
            attack_time: 0.010,
            release_time: 1.5,
            peak_hold_time: 0.0,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Create digital peak meter ballistics.
    ///
    /// - Integration time: 0ms (sample accurate)
    /// - Attack time: 0ms (instantaneous)
    /// - Release time: 0.0s (follows signal)
    /// - Peak hold: configurable
    #[must_use]
    pub fn digital_peak(sample_rate: f64, peak_hold_seconds: f64) -> Self {
        Self {
            integration_time: 0.0,
            attack_time: 0.0,
            release_time: 0.0,
            peak_hold_time: peak_hold_seconds,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Create RMS meter ballistics.
    ///
    /// - Integration time: configurable
    /// - Attack/release: instant with averaging
    #[must_use]
    pub fn rms(sample_rate: f64, window_seconds: f64) -> Self {
        Self {
            integration_time: window_seconds,
            attack_time: window_seconds,
            release_time: window_seconds,
            peak_hold_time: 0.0,
            return_time: 0.0,
            sample_rate,
        }
    }

    /// Get integration time coefficient.
    #[must_use]
    pub fn integration_coefficient(&self) -> f64 {
        if self.integration_time <= 0.0 {
            1.0
        } else {
            (-1.0 / (self.integration_time * self.sample_rate)).exp()
        }
    }

    /// Get attack time coefficient.
    #[must_use]
    pub fn attack_coefficient(&self) -> f64 {
        if self.attack_time <= 0.0 {
            1.0
        } else {
            (-1.0 / (self.attack_time * self.sample_rate)).exp()
        }
    }

    /// Get release time coefficient.
    #[must_use]
    pub fn release_coefficient(&self) -> f64 {
        if self.release_time <= 0.0 {
            1.0
        } else {
            (-1.0 / (self.release_time * self.sample_rate)).exp()
        }
    }

    /// Get peak hold samples.
    #[must_use]
    pub fn peak_hold_samples(&self) -> usize {
        (self.peak_hold_time * self.sample_rate) as usize
    }

    /// Get return time coefficient.
    #[must_use]
    pub fn return_coefficient(&self) -> f64 {
        if self.return_time <= 0.0 {
            0.0
        } else {
            (-1.0 / (self.return_time * self.sample_rate)).exp()
        }
    }
}

/// Ballistics processor for meter readings.
pub struct BallisticsProcessor {
    /// Configuration.
    config: BallisticsConfig,
    /// Current integrated value.
    integrated: f64,
    /// Current envelope value.
    envelope: f64,
    /// Peak hold value.
    peak_hold: f64,
    /// Peak hold counter (samples).
    peak_hold_counter: usize,
    /// Maximum peak value seen.
    max_peak: f64,
}

impl BallisticsProcessor {
    /// Create a new ballistics processor.
    #[must_use]
    pub fn new(config: BallisticsConfig) -> Self {
        Self {
            config,
            integrated: 0.0,
            envelope: 0.0,
            peak_hold: 0.0,
            peak_hold_counter: 0,
            max_peak: 0.0,
        }
    }

    /// Process a single sample value.
    ///
    /// # Arguments
    ///
    /// * `value` - Input value (linear or dB)
    ///
    /// # Returns
    ///
    /// Processed value with ballistics applied
    pub fn process(&mut self, value: f64) -> f64 {
        // Integration (smoothing)
        let integration_coeff = self.config.integration_coefficient();
        self.integrated = integration_coeff * self.integrated + (1.0 - integration_coeff) * value;

        // Attack/release envelope
        let target = self.integrated;
        let coeff = if target > self.envelope {
            self.config.attack_coefficient()
        } else {
            self.config.release_coefficient()
        };

        self.envelope = coeff * self.envelope + (1.0 - coeff) * target;

        // Peak hold
        if self.envelope > self.peak_hold {
            self.peak_hold = self.envelope;
            self.peak_hold_counter = self.config.peak_hold_samples();
            self.max_peak = self.max_peak.max(self.peak_hold);
        } else if self.peak_hold_counter > 0 {
            self.peak_hold_counter -= 1;
        } else if self.config.return_time > 0.0 {
            // Return to zero
            let return_coeff = self.config.return_coefficient();
            self.peak_hold *= return_coeff;
        } else {
            self.peak_hold = self.envelope;
        }

        self.envelope
    }

    /// Get current envelope value.
    #[must_use]
    pub fn envelope(&self) -> f64 {
        self.envelope
    }

    /// Get current peak hold value.
    #[must_use]
    pub fn peak_hold(&self) -> f64 {
        self.peak_hold
    }

    /// Get maximum peak value.
    #[must_use]
    pub fn max_peak(&self) -> f64 {
        self.max_peak
    }

    /// Reset the processor.
    pub fn reset(&mut self) {
        self.integrated = 0.0;
        self.envelope = 0.0;
        self.peak_hold = 0.0;
        self.peak_hold_counter = 0;
        self.max_peak = 0.0;
    }

    /// Reset peak hold only.
    pub fn reset_peak_hold(&mut self) {
        self.peak_hold = self.envelope;
        self.peak_hold_counter = 0;
    }

    /// Reset max peak only.
    pub fn reset_max_peak(&mut self) {
        self.max_peak = 0.0;
    }
}

/// RMS averaging window.
pub struct RmsWindow {
    /// Sample buffer for RMS calculation.
    buffer: VecDeque<f64>,
    /// Window size in samples.
    window_size: usize,
    /// Running sum of squares.
    sum_squares: f64,
}

impl RmsWindow {
    /// Create a new RMS window.
    ///
    /// # Arguments
    ///
    /// * `window_seconds` - Window duration in seconds
    /// * `sample_rate` - Sample rate in Hz
    #[must_use]
    pub fn new(window_seconds: f64, sample_rate: f64) -> Self {
        let window_size = (window_seconds * sample_rate) as usize;
        Self {
            buffer: VecDeque::with_capacity(window_size),
            window_size,
            sum_squares: 0.0,
        }
    }

    /// Add a sample and compute RMS.
    ///
    /// # Arguments
    ///
    /// * `sample` - Input sample value
    ///
    /// # Returns
    ///
    /// Current RMS value
    pub fn process(&mut self, sample: f64) -> f64 {
        let square = sample * sample;
        self.sum_squares += square;
        self.buffer.push_back(square);

        if self.buffer.len() > self.window_size {
            if let Some(old) = self.buffer.pop_front() {
                self.sum_squares -= old;
            }
        }

        if self.buffer.is_empty() {
            0.0
        } else {
            (self.sum_squares / self.buffer.len() as f64).sqrt()
        }
    }

    /// Get current RMS value without adding a sample.
    #[must_use]
    pub fn rms(&self) -> f64 {
        if self.buffer.is_empty() {
            0.0
        } else {
            (self.sum_squares / self.buffer.len() as f64).sqrt()
        }
    }

    /// Reset the window.
    pub fn reset(&mut self) {
        self.buffer.clear();
        self.sum_squares = 0.0;
    }
}

/// Peak detector with configurable hold time.
pub struct PeakDetector {
    /// Current peak value.
    peak: f64,
    /// Peak hold counter in samples.
    hold_counter: usize,
    /// Peak hold duration in samples.
    hold_samples: usize,
    /// Decay rate per sample (0.0 = instant, 1.0 = no decay).
    decay_rate: f64,
}

impl PeakDetector {
    /// Create a new peak detector.
    ///
    /// # Arguments
    ///
    /// * `hold_seconds` - Peak hold time in seconds
    /// * `decay_seconds` - Decay time in seconds (0.0 = instant)
    /// * `sample_rate` - Sample rate in Hz
    #[must_use]
    pub fn new(hold_seconds: f64, decay_seconds: f64, sample_rate: f64) -> Self {
        let hold_samples = (hold_seconds * sample_rate) as usize;
        let decay_rate = if decay_seconds > 0.0 {
            (-1.0 / (decay_seconds * sample_rate)).exp()
        } else {
            0.0
        };

        Self {
            peak: 0.0,
            hold_counter: 0,
            hold_samples,
            decay_rate,
        }
    }

    /// Process a sample and update peak.
    ///
    /// # Arguments
    ///
    /// * `value` - Input value (absolute)
    ///
    /// # Returns
    ///
    /// Current peak value
    pub fn process(&mut self, value: f64) -> f64 {
        if value > self.peak {
            self.peak = value;
            self.hold_counter = self.hold_samples;
        } else if self.hold_counter > 0 {
            self.hold_counter -= 1;
        } else {
            self.peak *= self.decay_rate;
        }

        self.peak
    }

    /// Get current peak value.
    #[must_use]
    pub fn peak(&self) -> f64 {
        self.peak
    }

    /// Reset peak value.
    pub fn reset(&mut self) {
        self.peak = 0.0;
        self.hold_counter = 0;
    }
}

/// Overload detector with hysteresis.
pub struct OverloadDetector {
    /// Overload threshold (linear).
    threshold: f64,
    /// Overload state.
    overload: bool,
    /// Overload counter (samples).
    overload_counter: usize,
    /// Minimum overload duration in samples.
    min_duration: usize,
    /// Reset delay in samples.
    reset_delay: usize,
    /// Reset counter.
    reset_counter: usize,
}

impl OverloadDetector {
    /// Create a new overload detector.
    ///
    /// # Arguments
    ///
    /// * `threshold_db` - Overload threshold in dB
    /// * `min_duration_ms` - Minimum overload duration in milliseconds
    /// * `reset_delay_ms` - Reset delay in milliseconds
    /// * `sample_rate` - Sample rate in Hz
    #[must_use]
    pub fn new(
        threshold_db: f64,
        min_duration_ms: f64,
        reset_delay_ms: f64,
        sample_rate: f64,
    ) -> Self {
        Self {
            threshold: db_to_linear(threshold_db),
            overload: false,
            overload_counter: 0,
            min_duration: ((min_duration_ms / 1000.0) * sample_rate) as usize,
            reset_delay: ((reset_delay_ms / 1000.0) * sample_rate) as usize,
            reset_counter: 0,
        }
    }

    /// Process a sample and check for overload.
    ///
    /// # Arguments
    ///
    /// * `value` - Input value (linear)
    ///
    /// # Returns
    ///
    /// `true` if overload is detected
    pub fn process(&mut self, value: f64) -> bool {
        if value.abs() >= self.threshold {
            self.overload_counter += 1;
            self.reset_counter = 0;

            if self.overload_counter >= self.min_duration {
                self.overload = true;
            }
        } else {
            self.overload_counter = 0;
            if self.overload {
                self.reset_counter += 1;
                if self.reset_counter >= self.reset_delay {
                    self.overload = false;
                    self.reset_counter = 0;
                }
            }
        }

        self.overload
    }

    /// Check if currently in overload state.
    #[must_use]
    pub fn is_overload(&self) -> bool {
        self.overload
    }

    /// Reset overload state.
    pub fn reset(&mut self) {
        self.overload = false;
        self.overload_counter = 0;
        self.reset_counter = 0;
    }
}

/// Convert decibels to linear amplitude.
#[must_use]
pub fn db_to_linear(db: f64) -> f64 {
    10.0_f64.powf(db / 20.0)
}

/// Convert linear amplitude to decibels.
#[must_use]
pub fn linear_to_db(linear: f64) -> f64 {
    if linear > 0.0 {
        20.0 * linear.log10()
    } else {
        f64::NEG_INFINITY
    }
}

/// Clamp a value to a range.
#[must_use]
pub fn clamp(value: f64, min: f64, max: f64) -> f64 {
    value.max(min).min(max)
}