oximedia-audio-analysis 0.1.0

Audio analysis tools for media including loudness, spectrum, and speech detection
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
#![allow(dead_code)]
//! Energy contour analysis for audio signals.
//!
//! This module computes and analyzes the energy envelope of an audio signal over
//! time. It provides short-time energy tracking, smoothing, segmentation by
//! energy level, and detection of energy transients (sudden rises or drops).

/// Configuration for energy contour computation.
#[derive(Debug, Clone)]
pub struct EnergyContourConfig {
    /// Frame length in samples.
    pub frame_length: usize,
    /// Hop size in samples.
    pub hop_size: usize,
    /// Smoothing window radius (0 = no smoothing).
    pub smoothing_radius: usize,
    /// Threshold in dB below peak for "active" detection.
    pub active_threshold_db: f64,
}

impl Default for EnergyContourConfig {
    fn default() -> Self {
        Self {
            frame_length: 1024,
            hop_size: 512,
            smoothing_radius: 3,
            active_threshold_db: -30.0,
        }
    }
}

/// A single point in the energy contour.
#[derive(Debug, Clone, Copy)]
pub struct EnergyPoint {
    /// Frame index.
    pub frame: usize,
    /// Time position in seconds.
    pub time_seconds: f64,
    /// RMS energy (linear).
    pub rms: f64,
    /// Energy in dB (relative to full scale).
    pub db: f64,
}

/// Result of energy contour analysis.
#[derive(Debug, Clone)]
pub struct EnergyContour {
    /// Per-frame energy points.
    pub points: Vec<EnergyPoint>,
    /// Peak energy in dB.
    pub peak_db: f64,
    /// Mean energy in dB.
    pub mean_db: f64,
    /// Minimum energy in dB.
    pub min_db: f64,
    /// Dynamic range in dB (peak - min among active frames).
    pub dynamic_range_db: f64,
}

/// A segment classified by energy level.
#[derive(Debug, Clone)]
pub struct EnergySegment {
    /// Start frame index.
    pub start_frame: usize,
    /// End frame index (exclusive).
    pub end_frame: usize,
    /// Start time in seconds.
    pub start_time: f64,
    /// End time in seconds.
    pub end_time: f64,
    /// Classification of the segment.
    pub classification: EnergyClass,
    /// Mean energy in dB over this segment.
    pub mean_db: f64,
}

/// Classification of an energy segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnergyClass {
    /// Very quiet / silence.
    Silence,
    /// Quiet but audible.
    Quiet,
    /// Moderate energy.
    Moderate,
    /// Loud.
    Loud,
}

/// An energy transient (sudden change in energy).
#[derive(Debug, Clone, Copy)]
pub struct EnergyTransient {
    /// Frame index where the transient occurs.
    pub frame: usize,
    /// Time in seconds.
    pub time_seconds: f64,
    /// Change in dB.
    pub delta_db: f64,
    /// Whether this is a rise (true) or drop (false).
    pub is_rise: bool,
}

/// Compute RMS energy for a single frame.
#[allow(clippy::cast_precision_loss)]
fn frame_rms(samples: &[f32]) -> f64 {
    if samples.is_empty() {
        return 0.0;
    }
    let sum: f64 = samples.iter().map(|&s| f64::from(s) * f64::from(s)).sum();
    (sum / samples.len() as f64).sqrt()
}

/// Convert linear amplitude to dB (with floor).
fn linear_to_db(val: f64) -> f64 {
    if val > 1e-15 {
        20.0 * val.log10()
    } else {
        -300.0
    }
}

/// Apply simple moving average smoothing.
#[allow(clippy::cast_precision_loss)]
fn smooth(values: &[f64], radius: usize) -> Vec<f64> {
    if radius == 0 {
        return values.to_vec();
    }
    let n = values.len();
    let mut out = Vec::with_capacity(n);
    for i in 0..n {
        let lo = i.saturating_sub(radius);
        let hi = (i + radius + 1).min(n);
        let count = (hi - lo) as f64;
        let sum: f64 = values[lo..hi].iter().sum();
        out.push(sum / count);
    }
    out
}

/// Compute the energy contour of an audio signal.
#[allow(clippy::cast_precision_loss)]
pub fn compute_contour(
    samples: &[f32],
    sample_rate: f64,
    config: &EnergyContourConfig,
) -> EnergyContour {
    let mut rms_values: Vec<f64> = Vec::new();
    let mut pos = 0_usize;
    while pos + config.frame_length <= samples.len() {
        let frame = &samples[pos..pos + config.frame_length];
        rms_values.push(frame_rms(frame));
        pos += config.hop_size;
    }

    // Smooth
    let smoothed = smooth(&rms_values, config.smoothing_radius);

    // Build points
    let points: Vec<EnergyPoint> = smoothed
        .iter()
        .enumerate()
        .map(|(i, &rms)| {
            let time = (i * config.hop_size) as f64 / sample_rate;
            let db = linear_to_db(rms);
            EnergyPoint {
                frame: i,
                time_seconds: time,
                rms,
                db,
            }
        })
        .collect();

    let peak_db = points
        .iter()
        .map(|p| p.db)
        .fold(f64::NEG_INFINITY, f64::max);
    let min_db = points.iter().map(|p| p.db).fold(f64::INFINITY, f64::min);
    let mean_db = if points.is_empty() {
        -300.0
    } else {
        points.iter().map(|p| p.db).sum::<f64>() / points.len() as f64
    };

    // Dynamic range: among active frames (above threshold)
    let active_thresh = peak_db + config.active_threshold_db;
    let active_dbs: Vec<f64> = points
        .iter()
        .filter(|p| p.db >= active_thresh)
        .map(|p| p.db)
        .collect();
    let dynamic_range_db = if active_dbs.len() >= 2 {
        let amax = active_dbs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
        let amin = active_dbs.iter().copied().fold(f64::INFINITY, f64::min);
        amax - amin
    } else {
        0.0
    };

    EnergyContour {
        points,
        peak_db,
        mean_db,
        min_db,
        dynamic_range_db,
    }
}

/// Segment the energy contour into classified regions.
#[allow(clippy::cast_precision_loss)]
#[must_use]
pub fn segment_by_energy(
    contour: &EnergyContour,
    silence_threshold_db: f64,
    quiet_threshold_db: f64,
    loud_threshold_db: f64,
    hop_size: usize,
    sample_rate: f64,
) -> Vec<EnergySegment> {
    if contour.points.is_empty() {
        return Vec::new();
    }

    let classify = |db: f64| -> EnergyClass {
        if db < silence_threshold_db {
            EnergyClass::Silence
        } else if db < quiet_threshold_db {
            EnergyClass::Quiet
        } else if db < loud_threshold_db {
            EnergyClass::Moderate
        } else {
            EnergyClass::Loud
        }
    };

    let mut segments = Vec::new();
    let mut start = 0_usize;
    let mut current_class = classify(contour.points[0].db);
    let mut db_sum = contour.points[0].db;
    let mut count = 1_usize;

    for i in 1..contour.points.len() {
        let cls = classify(contour.points[i].db);
        if cls == current_class {
            db_sum += contour.points[i].db;
            count += 1;
        } else {
            segments.push(EnergySegment {
                start_frame: start,
                end_frame: i,
                start_time: (start * hop_size) as f64 / sample_rate,
                end_time: (i * hop_size) as f64 / sample_rate,
                classification: current_class,
                mean_db: db_sum / count as f64,
            });
            start = i;
            current_class = cls;
            db_sum = contour.points[i].db;
            count = 1;
        }
    }

    // Final segment
    let n = contour.points.len();
    segments.push(EnergySegment {
        start_frame: start,
        end_frame: n,
        start_time: (start * hop_size) as f64 / sample_rate,
        end_time: (n * hop_size) as f64 / sample_rate,
        classification: current_class,
        mean_db: db_sum / count as f64,
    });

    segments
}

/// Detect energy transients (sudden rises or drops).
#[must_use]
pub fn detect_transients(contour: &EnergyContour, threshold_db: f64) -> Vec<EnergyTransient> {
    if contour.points.len() < 2 {
        return Vec::new();
    }

    let mut transients = Vec::new();
    for i in 1..contour.points.len() {
        let delta = contour.points[i].db - contour.points[i - 1].db;
        if delta.abs() >= threshold_db {
            transients.push(EnergyTransient {
                frame: i,
                time_seconds: contour.points[i].time_seconds,
                delta_db: delta,
                is_rise: delta > 0.0,
            });
        }
    }
    transients
}

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

    fn sine_signal(freq: f64, sample_rate: f64, duration: f64, amplitude: f32) -> Vec<f32> {
        let n = (sample_rate * duration) as usize;
        (0..n)
            .map(|i| {
                let t = i as f64 / sample_rate;
                #[allow(clippy::cast_possible_truncation)]
                let sample =
                    (amplitude as f64 * (2.0 * std::f64::consts::PI * freq * t).sin()) as f32;
                sample
            })
            .collect()
    }

    #[test]
    fn test_frame_rms_silence() {
        let samples = vec![0.0_f32; 1024];
        assert!((frame_rms(&samples)).abs() < 1e-10);
    }

    #[test]
    fn test_frame_rms_unity() {
        let samples = vec![1.0_f32; 100];
        assert!((frame_rms(&samples) - 1.0).abs() < 1e-10);
    }

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

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

    #[test]
    fn test_linear_to_db_floor() {
        assert!(linear_to_db(0.0) < -200.0);
    }

    #[test]
    fn test_smooth_no_radius() {
        let vals = vec![1.0, 2.0, 3.0];
        let out = smooth(&vals, 0);
        assert_eq!(out, vals);
    }

    #[test]
    fn test_smooth_with_radius() {
        let vals = vec![0.0, 0.0, 10.0, 0.0, 0.0];
        let out = smooth(&vals, 1);
        // Center value: (0 + 10 + 0) / 3 ≈ 3.33
        assert!((out[2] - 10.0 / 3.0).abs() < 0.01);
    }

    #[test]
    fn test_compute_contour_basic() {
        let signal = sine_signal(440.0, 16000.0, 0.5, 0.5);
        let config = EnergyContourConfig {
            frame_length: 512,
            hop_size: 256,
            smoothing_radius: 0,
            active_threshold_db: -30.0,
        };
        let contour = compute_contour(&signal, 16000.0, &config);
        assert!(!contour.points.is_empty());
        assert!(contour.peak_db > -20.0);
    }

    #[test]
    fn test_contour_silence_has_low_energy() {
        let signal = vec![0.0_f32; 8000];
        let config = EnergyContourConfig::default();
        let contour = compute_contour(&signal, 16000.0, &config);
        for p in &contour.points {
            assert!(p.db < -100.0);
        }
    }

    #[test]
    fn test_segment_by_energy_single_class() {
        let signal = vec![0.5_f32; 16000];
        let config = EnergyContourConfig {
            frame_length: 512,
            hop_size: 256,
            smoothing_radius: 0,
            active_threshold_db: -30.0,
        };
        let contour = compute_contour(&signal, 16000.0, &config);
        let segments = segment_by_energy(&contour, -80.0, -40.0, -6.0, 256, 16000.0);
        assert!(!segments.is_empty());
        // All frames have same DC level, so should be one segment
        assert_eq!(segments.len(), 1);
    }

    #[test]
    fn test_detect_transients_sudden_onset() {
        // Silence then loud
        let mut signal = vec![0.0001_f32; 8000];
        signal.extend(vec![0.5_f32; 8000]);
        let config = EnergyContourConfig {
            frame_length: 512,
            hop_size: 256,
            smoothing_radius: 0,
            active_threshold_db: -30.0,
        };
        let contour = compute_contour(&signal, 16000.0, &config);
        let transients = detect_transients(&contour, 10.0);
        assert!(!transients.is_empty());
        // At least one rise
        assert!(transients.iter().any(|t| t.is_rise));
    }

    #[test]
    fn test_detect_transients_none_for_steady() {
        let signal = vec![0.3_f32; 16000];
        let config = EnergyContourConfig {
            frame_length: 512,
            hop_size: 256,
            smoothing_radius: 0,
            active_threshold_db: -30.0,
        };
        let contour = compute_contour(&signal, 16000.0, &config);
        let transients = detect_transients(&contour, 10.0);
        assert!(transients.is_empty());
    }

    #[test]
    fn test_energy_contour_default_config() {
        let config = EnergyContourConfig::default();
        assert_eq!(config.frame_length, 1024);
        assert_eq!(config.hop_size, 512);
        assert_eq!(config.smoothing_radius, 3);
    }
}