oximedia-metering 0.1.8

Professional broadcast audio metering: ITU-R BS.1770-4, EBU R128, ATSC A/85
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
//! Phase/goniometer scope: Lissajous display data, phase angle, and correlation coefficient.

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

#[cfg(test)]
use std::f64::consts::PI;

/// A single point on the Lissajous (goniometer) display.
/// Coordinates are in the range [-1.0, 1.0].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LissajousPoint {
    /// X-axis coordinate (mid channel: L+R, scaled).
    pub x: f64,
    /// Y-axis coordinate (side channel: L-R, scaled).
    pub y: f64,
}

impl LissajousPoint {
    /// Create from left and right sample values.
    /// Uses M/S (mid/side) encoding: X = (L+R)/√2, Y = (L-R)/√2.
    #[must_use]
    pub fn from_lr(left: f64, right: f64) -> Self {
        let sqrt2 = std::f64::consts::SQRT_2;
        Self {
            x: (left + right) / sqrt2,
            y: (left - right) / sqrt2,
        }
    }

    /// Return the Euclidean distance from the origin.
    #[must_use]
    pub fn magnitude(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    /// Return the angle in radians from the positive X axis.
    #[must_use]
    pub fn angle_radians(&self) -> f64 {
        self.y.atan2(self.x)
    }

    /// Return the angle in degrees from the positive X axis.
    #[must_use]
    pub fn angle_degrees(&self) -> f64 {
        self.angle_radians().to_degrees()
    }
}

/// Configuration for the phase scope.
#[derive(Debug, Clone)]
pub struct PhaseScopeConfig {
    /// Number of Lissajous points to retain in the display buffer.
    pub buffer_size: usize,
    /// Low-pass decay factor for the correlation coefficient (0.0 = no smoothing, 1.0 = hold).
    /// Typical value: 0.9.
    pub correlation_decay: f64,
    /// Low-pass decay factor for the phase angle display (0.0 = instant, 1.0 = hold).
    pub angle_decay: f64,
}

impl PhaseScopeConfig {
    /// Create a default config suitable for broadcast monitoring.
    #[must_use]
    pub fn default_broadcast() -> Self {
        Self {
            buffer_size: 4096,
            correlation_decay: 0.9,
            angle_decay: 0.85,
        }
    }
}

impl Default for PhaseScopeConfig {
    fn default() -> Self {
        Self::default_broadcast()
    }
}

/// Phase scope / goniometer meter.
///
/// Accepts stereo audio frames and exposes:
/// - A ring buffer of Lissajous display points.
/// - A smoothed phase correlation coefficient in [-1, +1].
/// - A smoothed phase angle in radians.
pub struct PhaseScope {
    config: PhaseScopeConfig,
    /// Ring buffer of recent Lissajous points.
    points: Vec<LissajousPoint>,
    /// Write head index into the ring buffer.
    write_pos: usize,
    /// Total samples processed.
    total_samples: usize,
    /// Smoothed correlation coefficient.
    correlation: f64,
    /// Smoothed phase angle (radians).
    phase_angle: f64,
    /// Accumulated numerator for correlation (L·R sum).
    acc_lr: f64,
    /// Accumulated L² sum.
    acc_l2: f64,
    /// Accumulated R² sum.
    acc_r2: f64,
    /// Accumulation window size.
    acc_count: usize,
}

impl PhaseScope {
    /// Create a new phase scope with the given configuration.
    #[must_use]
    pub fn new(config: PhaseScopeConfig) -> Self {
        let buffer_size = config.buffer_size.max(1);
        Self {
            config,
            points: vec![LissajousPoint { x: 0.0, y: 0.0 }; buffer_size],
            write_pos: 0,
            total_samples: 0,
            correlation: 0.0,
            phase_angle: 0.0,
            acc_lr: 0.0,
            acc_l2: 0.0,
            acc_r2: 0.0,
            acc_count: 0,
        }
    }

    /// Create a phase scope with default broadcast configuration.
    #[must_use]
    pub fn default_broadcast() -> Self {
        Self::new(PhaseScopeConfig::default_broadcast())
    }

    /// Process a block of interleaved stereo samples (L, R, L, R, ...).
    pub fn process_interleaved(&mut self, samples: &[f64]) {
        let frames = samples.len() / 2;
        for i in 0..frames {
            let left = samples[i * 2];
            let right = samples[i * 2 + 1];
            self.process_frame(left, right);
        }
    }

    /// Process a single stereo frame.
    pub fn process_frame(&mut self, left: f64, right: f64) {
        // Write Lissajous point
        let pt = LissajousPoint::from_lr(left, right);
        self.points[self.write_pos] = pt;
        self.write_pos = (self.write_pos + 1) % self.points.len();
        self.total_samples += 1;

        // Accumulate for correlation calculation
        self.acc_lr += left * right;
        self.acc_l2 += left * left;
        self.acc_r2 += right * right;
        self.acc_count += 1;

        // Update smoothed correlation every acc_window samples
        let acc_window = (self.config.buffer_size / 16).max(1);
        if self.acc_count >= acc_window {
            let denom = (self.acc_l2 * self.acc_r2).sqrt();
            let instant_corr = if denom > 1e-12 {
                (self.acc_lr / denom).clamp(-1.0, 1.0)
            } else {
                0.0
            };

            let decay = self.config.correlation_decay;
            self.correlation = decay * self.correlation + (1.0 - decay) * instant_corr;

            // Update phase angle from correlation
            // phase angle θ where cos(θ) ≈ correlation
            let instant_angle = instant_corr.clamp(-1.0, 1.0).acos();
            let angle_decay = self.config.angle_decay;
            self.phase_angle = angle_decay * self.phase_angle + (1.0 - angle_decay) * instant_angle;

            // Reset accumulators
            self.acc_lr = 0.0;
            self.acc_l2 = 0.0;
            self.acc_r2 = 0.0;
            self.acc_count = 0;
        }
    }

    /// Return the smoothed phase correlation coefficient in [-1, +1].
    /// +1 = perfect mono-compatible, -1 = phase-inverted.
    #[must_use]
    pub fn correlation(&self) -> f64 {
        self.correlation
    }

    /// Return the smoothed phase angle in radians [0, π].
    #[must_use]
    pub fn phase_angle_radians(&self) -> f64 {
        self.phase_angle
    }

    /// Return the smoothed phase angle in degrees [0, 180].
    #[must_use]
    pub fn phase_angle_degrees(&self) -> f64 {
        self.phase_angle.to_degrees()
    }

    /// Return `true` if correlation is negative (potential phase cancellation).
    #[must_use]
    pub fn has_phase_issues(&self) -> bool {
        self.correlation < -0.1
    }

    /// Return the current Lissajous display buffer as a slice (ordered from oldest to newest).
    #[must_use]
    pub fn lissajous_points(&self) -> Vec<LissajousPoint> {
        let len = self.points.len();
        let mut result = Vec::with_capacity(len);
        for i in 0..len {
            result.push(self.points[(self.write_pos + i) % len]);
        }
        result
    }

    /// Return total samples processed.
    #[must_use]
    pub fn total_samples(&self) -> usize {
        self.total_samples
    }

    /// Reset the scope to initial state.
    pub fn reset(&mut self) {
        let len = self.points.len();
        for pt in &mut self.points {
            *pt = LissajousPoint { x: 0.0, y: 0.0 };
        }
        self.write_pos = 0;
        self.total_samples = 0;
        self.correlation = 0.0;
        self.phase_angle = 0.0;
        self.acc_lr = 0.0;
        self.acc_l2 = 0.0;
        self.acc_r2 = 0.0;
        self.acc_count = 0;
        let _ = len; // suppress unused warning
    }
}

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

    fn make_scope() -> PhaseScope {
        let config = PhaseScopeConfig {
            buffer_size: 64,
            correlation_decay: 0.0, // instant update for testing
            angle_decay: 0.0,
        };
        PhaseScope::new(config)
    }

    #[test]
    fn test_lissajous_point_from_lr_mono() {
        // Equal L and R → Y = 0
        let pt = LissajousPoint::from_lr(0.5, 0.5);
        assert!(pt.y.abs() < 1e-10);
    }

    #[test]
    fn test_lissajous_point_from_lr_side() {
        // Opposite L and R → X = 0
        let pt = LissajousPoint::from_lr(0.5, -0.5);
        assert!(pt.x.abs() < 1e-10);
        assert!(pt.y > 0.0);
    }

    #[test]
    fn test_lissajous_point_magnitude() {
        let pt = LissajousPoint { x: 3.0, y: 4.0 };
        assert!((pt.magnitude() - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_lissajous_point_angle() {
        let pt = LissajousPoint { x: 0.0, y: 1.0 };
        let angle_deg = pt.angle_degrees();
        assert!((angle_deg - 90.0).abs() < 1e-6);
    }

    #[test]
    fn test_phase_scope_default_broadcast() {
        let scope = PhaseScope::default_broadcast();
        assert_eq!(scope.config.buffer_size, 4096);
    }

    #[test]
    fn test_phase_scope_initial_correlation_zero() {
        let scope = make_scope();
        assert_eq!(scope.correlation(), 0.0);
    }

    #[test]
    fn test_phase_scope_perfect_correlation() {
        let mut scope = make_scope();
        // Perfectly correlated (mono) signal
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.5_f64);
            samples.push(0.5_f64);
        }
        scope.process_interleaved(&samples);
        // Correlation should be close to +1
        assert!(
            scope.correlation() > 0.9,
            "correlation={}",
            scope.correlation()
        );
    }

    #[test]
    fn test_phase_scope_negative_correlation() {
        let mut scope = make_scope();
        // Phase-inverted signal
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.5_f64);
            samples.push(-0.5_f64);
        }
        scope.process_interleaved(&samples);
        assert!(
            scope.correlation() < -0.9,
            "correlation={}",
            scope.correlation()
        );
    }

    #[test]
    fn test_phase_scope_has_phase_issues() {
        let mut scope = make_scope();
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.5_f64);
            samples.push(-0.5_f64);
        }
        scope.process_interleaved(&samples);
        assert!(scope.has_phase_issues());
    }

    #[test]
    fn test_phase_scope_no_phase_issues_mono() {
        let mut scope = make_scope();
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.5_f64);
            samples.push(0.5_f64);
        }
        scope.process_interleaved(&samples);
        assert!(!scope.has_phase_issues());
    }

    #[test]
    fn test_phase_scope_total_samples() {
        let mut scope = make_scope();
        let samples: Vec<f64> = vec![0.1; 20]; // 10 frames
        scope.process_interleaved(&samples);
        assert_eq!(scope.total_samples(), 10);
    }

    #[test]
    fn test_phase_scope_lissajous_points_length() {
        let scope = make_scope();
        let pts = scope.lissajous_points();
        assert_eq!(pts.len(), scope.config.buffer_size);
    }

    #[test]
    fn test_phase_scope_reset() {
        let mut scope = make_scope();
        let samples: Vec<f64> = vec![0.5; 64];
        scope.process_interleaved(&samples);
        scope.reset();
        assert_eq!(scope.total_samples(), 0);
        assert_eq!(scope.correlation(), 0.0);
    }

    #[test]
    fn test_phase_scope_phase_angle_range() {
        let mut scope = make_scope();
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.7_f64);
            samples.push(0.3_f64);
        }
        scope.process_interleaved(&samples);
        let angle = scope.phase_angle_radians();
        assert!(angle >= 0.0 && angle <= PI, "angle={}", angle);
    }

    #[test]
    fn test_phase_scope_process_frame() {
        let mut scope = make_scope();
        scope.process_frame(0.5, 0.5);
        assert_eq!(scope.total_samples(), 1);
    }

    #[test]
    fn test_phase_scope_angle_degrees_range() {
        let mut scope = make_scope();
        let mut samples = Vec::new();
        for _ in 0..64 {
            samples.push(0.5_f64);
            samples.push(0.5_f64);
        }
        scope.process_interleaved(&samples);
        let deg = scope.phase_angle_degrees();
        assert!(deg >= 0.0 && deg <= 180.0, "deg={}", deg);
    }
}