resonant-analysis 0.4.0

High-level audio analysis: onset detection, beat tracking, pitch estimation, MFCCs
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
//! Onset detection via spectral flux and adaptive thresholding.
//!
//! An onset marks the beginning of a new musical event (note, drum hit, etc.).
//! This module computes the onset strength envelope using half-wave rectified
//! spectral flux, then picks peaks above an adaptive threshold.

extern crate alloc;

use alloc::vec;
use alloc::vec::Vec;

use resonant_core::signal::Signal;
use resonant_core::window;
use resonant_fft::stft::Stft;
use resonant_fft::SignalFreqExt;

use crate::error::AnalysisError;

/// A detected onset event.
///
/// # Examples
///
/// ```
/// use resonant_analysis::onset::Onset;
///
/// let o = Onset { sample_index: 22050, time_secs: 0.5, strength: 1.2 };
/// assert_eq!(o.time_secs, 0.5);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Onset {
    /// Sample index of the onset.
    pub sample_index: usize,
    /// Time in seconds.
    pub time_secs: f32,
    /// Strength of the onset (spectral flux value).
    pub strength: f32,
}

/// Onset detector with configurable STFT and threshold parameters.
///
/// # Examples
///
/// ```
/// use resonant_analysis::onset::OnsetDetector;
///
/// let detector = OnsetDetector::new(44100.0);
/// // Silence → no onsets
/// let onsets = detector.detect(&vec![0.0_f32; 4096]).unwrap();
/// assert!(onsets.is_empty());
/// ```
#[derive(Debug, Clone)]
pub struct OnsetDetector {
    sample_rate: f32,
    window_size: usize,
    hop_size: usize,
    threshold_multiplier: f32,
    median_window: usize,
}

impl OnsetDetector {
    /// Creates a detector with default parameters.
    ///
    /// Defaults: window 1024, hop 512, threshold multiplier 1.5, median window 10.
    #[must_use]
    pub fn new(sample_rate: f32) -> Self {
        Self {
            sample_rate,
            window_size: 1024,
            hop_size: 512,
            threshold_multiplier: 1.5,
            median_window: 10,
        }
    }

    /// Sets the STFT window size (must be a power of two).
    #[must_use]
    pub fn with_window_size(mut self, size: usize) -> Self {
        self.window_size = size;
        self
    }

    /// Returns the configured hop size in samples.
    #[must_use]
    #[inline]
    pub fn hop_size(&self) -> usize {
        self.hop_size
    }

    /// Sets the STFT hop size.
    #[must_use]
    pub fn with_hop_size(mut self, hop: usize) -> Self {
        self.hop_size = hop;
        self
    }

    /// Sets the adaptive threshold multiplier.
    ///
    /// Higher values → fewer onsets (stricter). Lower → more onsets.
    #[must_use]
    pub fn with_threshold_multiplier(mut self, m: f32) -> Self {
        self.threshold_multiplier = m;
        self
    }

    /// Sets the median filter window size for adaptive thresholding.
    #[must_use]
    pub fn with_median_window(mut self, w: usize) -> Self {
        self.median_window = w;
        self
    }

    /// Detects onsets in mono audio samples.
    ///
    /// Returns a list of [`Onset`] events sorted by time.
    ///
    /// # Errors
    ///
    /// Returns [`AnalysisError::EmptyInput`] if `samples` is empty, or
    /// an FFT error if the window size is invalid.
    pub fn detect(&self, samples: &[f32]) -> Result<Vec<Onset>, AnalysisError> {
        let envelope = self.onset_strength(samples)?;
        let threshold =
            adaptive_threshold(&envelope, self.median_window, self.threshold_multiplier);
        let peaks = pick_peaks(&envelope, &threshold);

        let onsets = peaks
            .into_iter()
            .map(|(frame_idx, strength)| {
                let sample_index = frame_idx * self.hop_size;
                Onset {
                    sample_index,
                    time_secs: sample_index as f32 / self.sample_rate,
                    strength,
                }
            })
            .collect();

        Ok(onsets)
    }

    /// Computes the onset strength envelope (spectral flux per STFT frame).
    ///
    /// Returns one value per frame. The first frame always has strength 0.
    ///
    /// # Errors
    ///
    /// Returns [`AnalysisError::EmptyInput`] if `samples` is empty.
    pub fn onset_strength(&self, samples: &[f32]) -> Result<Vec<f32>, AnalysisError> {
        if samples.is_empty() {
            return Err(AnalysisError::EmptyInput);
        }

        // If the signal is shorter than one window, no frames can be extracted
        if samples.len() < self.window_size {
            return Ok(vec![0.0]);
        }

        let signal = Signal::from_samples(samples.to_vec());
        let stft = Stft::builder(self.window_size, self.hop_size)
            .window_fn(window::hann)
            .build();

        let frames = stft.analyze(&signal)?;
        if frames.is_empty() {
            return Ok(vec![0.0]);
        }

        let magnitudes: Vec<Vec<f32>> = frames.iter().map(|f| f.magnitude()).collect();

        let mut envelope = Vec::with_capacity(magnitudes.len());
        envelope.push(0.0); // first frame has no previous frame

        for i in 1..magnitudes.len() {
            envelope.push(spectral_flux(&magnitudes[i - 1], &magnitudes[i]));
        }

        Ok(envelope)
    }
}

/// Half-wave rectified spectral flux between consecutive magnitude frames.
fn spectral_flux(prev: &[f32], curr: &[f32]) -> f32 {
    prev.iter()
        .zip(curr)
        .map(|(prev_mag, curr_mag)| (curr_mag - prev_mag).max(0.0))
        .sum()
}

/// Adaptive threshold: local median + multiplier × local mean absolute deviation.
fn adaptive_threshold(envelope: &[f32], window: usize, multiplier: f32) -> Vec<f32> {
    let envelope_len = envelope.len();
    let mut thresholds = Vec::with_capacity(envelope_len);
    let half_window = window / 2;

    for i in 0..envelope_len {
        let start = i.saturating_sub(half_window);
        let end = (i + half_window + 1).min(envelope_len);
        let local = &envelope[start..end];

        let median = local_median(local);
        let mad = local_mad(local, median);
        thresholds.push(median + multiplier * mad);
    }

    thresholds
}

/// Median of a small slice (copies and sorts).
fn local_median(values: &[f32]) -> f32 {
    if values.is_empty() {
        return 0.0;
    }
    let mut sorted: Vec<f32> = values.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal));
    let median_idx = sorted.len() / 2;
    if sorted.len() % 2 == 0 {
        (sorted[median_idx - 1] + sorted[median_idx]) / 2.0
    } else {
        sorted[median_idx]
    }
}

/// Mean absolute deviation from the median.
fn local_mad(values: &[f32], median: f32) -> f32 {
    if values.is_empty() {
        return 0.0;
    }
    let sum: f32 = values.iter().map(|sample| (sample - median).abs()).sum();
    sum / values.len() as f32
}

/// Picks local maxima in the envelope that exceed the adaptive threshold.
fn pick_peaks(envelope: &[f32], threshold: &[f32]) -> Vec<(usize, f32)> {
    let mut peaks = Vec::new();
    for i in 1..envelope.len().saturating_sub(1) {
        let flux_value = envelope[i];
        if flux_value > threshold[i]
            && flux_value >= envelope[i - 1]
            && flux_value >= envelope[i + 1]
        {
            peaks.push((i, flux_value));
        }
    }
    peaks
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::f32::consts::PI;

    const SR: f32 = 44100.0;

    #[test]
    fn silence_no_onsets() {
        let detector = OnsetDetector::new(SR);
        let samples = vec![0.0_f32; 8192];
        let onsets = detector.detect(&samples).ok();
        assert_eq!(onsets.as_ref().map(|o| o.len()), Some(0));
    }

    #[test]
    fn empty_input_returns_error() {
        let detector = OnsetDetector::new(SR);
        let result = detector.detect(&[]);
        assert_eq!(result, Err(AnalysisError::EmptyInput));
    }

    #[test]
    fn onset_strength_empty_error() {
        let detector = OnsetDetector::new(SR);
        let result = detector.onset_strength(&[]);
        assert_eq!(result, Err(AnalysisError::EmptyInput));
    }

    #[test]
    fn onset_strength_nonnegative() {
        let detector = OnsetDetector::new(SR);
        let samples: Vec<f32> = (0..8192)
            .map(|i| (2.0 * PI * 440.0 * i as f32 / SR).sin())
            .collect();
        let envelope = detector.onset_strength(&samples).ok();
        if let Some(env) = &envelope {
            for &v in env {
                assert!(v >= 0.0, "negative onset strength: {v}");
            }
        }
    }

    #[test]
    fn onset_strength_first_frame_zero() {
        let detector = OnsetDetector::new(SR);
        let samples = vec![1.0_f32; 4096];
        let envelope = detector.onset_strength(&samples).ok();
        assert!(envelope.as_ref().is_some_and(|e| e[0] == 0.0));
    }

    #[test]
    fn click_track_detects_onsets() {
        // Create a click track: silence with impulses at known positions
        // 1 second of silence; place clicks at 0.2s, 0.5s, 0.8s
        let mut samples = vec![0.0_f32; 44100];
        let click_positions = [8820_usize, 22050, 35280];
        for &pos in &click_positions {
            // Short burst (64 samples of sine at 1kHz)
            for j in 0..64 {
                if pos + j < samples.len() {
                    samples[pos + j] = (2.0 * PI * 1000.0 * j as f32 / SR).sin();
                }
            }
        }

        let detector = OnsetDetector::new(SR)
            .with_window_size(1024)
            .with_hop_size(512)
            .with_threshold_multiplier(1.0);

        let onsets = detector.detect(&samples).ok();
        let onsets = onsets.as_ref();

        // Should detect at least 2 of the 3 clicks (the first may be missed
        // due to edge effects)
        assert!(
            onsets.is_some_and(|o| o.len() >= 2),
            "expected >=2 onsets, got {:?}",
            onsets.map(|o| o.len())
        );

        // Each detected onset should be near one of the click positions
        if let Some(onsets) = onsets {
            for onset in onsets {
                let near_click = click_positions.iter().any(|&pos| {
                    let diff = (onset.sample_index as i64 - pos as i64).unsigned_abs() as usize;
                    diff < 2048 // within ~2 hops
                });
                assert!(
                    near_click,
                    "onset at sample {} not near any click",
                    onset.sample_index
                );
            }
        }
    }

    #[test]
    fn sine_onset_detected() {
        // Silence then sine: onset at the transition
        let mut samples = vec![0.0_f32; 8192];
        let onset_pos = 4096;
        for i in onset_pos..8192 {
            samples[i] = (2.0 * PI * 440.0 * (i - onset_pos) as f32 / SR).sin();
        }

        let detector = OnsetDetector::new(SR)
            .with_window_size(1024)
            .with_hop_size(512)
            .with_threshold_multiplier(1.0);

        let onsets = detector.detect(&samples).ok();
        assert!(
            onsets.as_ref().is_some_and(|o| !o.is_empty()),
            "should detect onset at silence→sine transition"
        );

        // The detected onset should be near the actual transition
        if let Some(onsets) = &onsets {
            let nearest = onsets
                .iter()
                .min_by_key(|o| (o.sample_index as i64 - onset_pos as i64).unsigned_abs())
                .map(|o| o.sample_index);
            assert!(
                nearest.is_some_and(|n| (n as i64 - onset_pos as i64).unsigned_abs() < 2048),
                "onset too far from transition: {nearest:?}"
            );
        }
    }

    #[test]
    fn short_signal_returns_single_zero() {
        let detector = OnsetDetector::new(SR).with_window_size(1024);
        let samples = vec![1.0_f32; 512]; // shorter than window
        let env = detector.onset_strength(&samples).ok();
        assert_eq!(env.as_deref(), Some([0.0].as_slice()));
    }

    #[test]
    fn builder_methods() {
        let d = OnsetDetector::new(SR)
            .with_window_size(2048)
            .with_hop_size(1024)
            .with_threshold_multiplier(2.0)
            .with_median_window(20);
        assert_eq!(d.window_size, 2048);
        assert_eq!(d.hop_size, 1024);
        assert_eq!(d.threshold_multiplier, 2.0);
        assert_eq!(d.median_window, 20);
    }

    #[test]
    fn onset_time_matches_sample_index() {
        // Check that time_secs = sample_index / sample_rate
        let mut samples = vec![0.0_f32; 8192];
        for i in 4096..8192 {
            samples[i] = (2.0 * PI * 1000.0 * i as f32 / SR).sin();
        }

        let detector = OnsetDetector::new(SR)
            .with_window_size(1024)
            .with_hop_size(512)
            .with_threshold_multiplier(1.0);

        let onsets = detector.detect(&samples).ok();
        if let Some(onsets) = &onsets {
            for o in onsets {
                let expected_time = o.sample_index as f32 / SR;
                assert!(
                    (o.time_secs - expected_time).abs() < 1e-6,
                    "time mismatch: {} vs {expected_time}",
                    o.time_secs
                );
            }
        }
    }

    // --- internal helper tests ---

    #[test]
    fn spectral_flux_identical_is_zero() {
        let a = [1.0, 2.0, 3.0];
        assert_eq!(spectral_flux(&a, &a), 0.0);
    }

    #[test]
    fn spectral_flux_increase_only() {
        let prev = [0.0, 0.0, 0.0];
        let curr = [1.0, 2.0, 3.0];
        assert_eq!(spectral_flux(&prev, &curr), 6.0);
    }

    #[test]
    fn spectral_flux_decrease_ignored() {
        let prev = [3.0, 2.0, 1.0];
        let curr = [0.0, 0.0, 0.0];
        assert_eq!(spectral_flux(&prev, &curr), 0.0);
    }

    #[test]
    fn local_median_odd() {
        assert_eq!(local_median(&[3.0, 1.0, 2.0]), 2.0);
    }

    #[test]
    fn local_median_even() {
        assert_eq!(local_median(&[1.0, 3.0, 2.0, 4.0]), 2.5);
    }

    #[test]
    fn local_median_empty() {
        assert_eq!(local_median(&[]), 0.0);
    }

    #[test]
    fn local_mad_uniform() {
        assert_eq!(local_mad(&[5.0, 5.0, 5.0], 5.0), 0.0);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn onset_serde_roundtrip() {
        let o = Onset {
            sample_index: 22050,
            time_secs: 0.5,
            strength: 1.2,
        };
        let json = serde_json::to_string(&o).unwrap_or_else(|e| panic!("serialize Onset: {e}"));
        let back: Onset =
            serde_json::from_str(&json).unwrap_or_else(|e| panic!("deserialize Onset: {e}"));
        assert_eq!(o, back);
    }
}