resonant-analysis 0.0.2

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
//! Chroma features — 12-bin pitch class profiles.
//!
//! A chroma vector maps spectral energy onto the 12 pitch classes
//! (C, C#, D, ..., B), collapsing octave information. Chroma features are
//! useful for chord recognition, key detection, and music similarity.
//!
//! The extractor computes an STFT, maps each FFT bin to a pitch class based
//! on its frequency, and accumulates energy per class.

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;

/// Names of the 12 pitch classes, indexed 0–11.
pub const PITCH_CLASS_NAMES: [&str; 12] = [
    "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B",
];

/// A 12-bin chroma vector representing energy per pitch class.
///
/// Bins are ordered: C, C#, D, D#, E, F, F#, G, G#, A, A#, B.
///
/// # Examples
///
/// ```
/// use resonant_analysis::chroma::ChromaVector;
///
/// let cv = ChromaVector { bins: [0.0; 12] };
/// assert_eq!(cv.bins.len(), 12);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChromaVector {
    /// Energy in each of the 12 pitch classes.
    pub bins: [f32; 12],
}

impl ChromaVector {
    /// Returns the index (0–11) of the dominant pitch class.
    #[must_use]
    pub fn dominant_class(&self) -> usize {
        self.bins
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Returns the name of the dominant pitch class.
    #[must_use]
    pub fn dominant_name(&self) -> &'static str {
        PITCH_CLASS_NAMES[self.dominant_class()]
    }
}

/// Chroma feature extractor with configurable STFT and tuning parameters.
///
/// # Examples
///
/// ```
/// use resonant_analysis::chroma::ChromaExtractor;
///
/// let extractor = ChromaExtractor::new(44100.0);
/// let samples = vec![0.0_f32; 8192];
/// let chroma = extractor.extract(&samples).unwrap();
/// for cv in &chroma {
///     assert_eq!(cv.bins.len(), 12);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct ChromaExtractor {
    sample_rate: f32,
    window_size: usize,
    hop_size: usize,
    tuning_hz: f32,
}

impl ChromaExtractor {
    /// Creates an extractor with default parameters.
    ///
    /// Defaults: window 4096, hop 2048, A4 = 440 Hz tuning.
    #[must_use]
    pub fn new(sample_rate: f32) -> Self {
        Self {
            sample_rate,
            window_size: 4096,
            hop_size: 2048,
            tuning_hz: 440.0,
        }
    }

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

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

    /// Sets the tuning reference frequency for A4 (default 440 Hz).
    #[must_use]
    pub fn with_tuning(mut self, hz: f32) -> Self {
        self.tuning_hz = hz;
        self
    }

    /// Extracts chroma vectors from mono audio samples.
    ///
    /// Returns one [`ChromaVector`] per STFT frame.
    ///
    /// # Errors
    ///
    /// Returns [`AnalysisError::EmptyInput`] if `samples` is empty.
    pub fn extract(&self, samples: &[f32]) -> Result<Vec<ChromaVector>, AnalysisError> {
        if samples.is_empty() {
            return Err(AnalysisError::EmptyInput);
        }

        // If signal is shorter than one window, return a single zero chroma
        if samples.len() < self.window_size {
            return Ok(vec![ChromaVector { bins: [0.0; 12] }]);
        }

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

        let stft_frames = stft.analyze(&signal)?;
        if stft_frames.is_empty() {
            return Ok(vec![ChromaVector { bins: [0.0; 12] }]);
        }

        // Precompute the pitch class map: FFT bin index → pitch class (0–11),
        // or None for bins below the audible range.
        let chroma_map = build_chroma_map(self.window_size, self.sample_rate, self.tuning_hz);

        let mut result = Vec::with_capacity(stft_frames.len());

        for frame in &stft_frames {
            let magnitudes = frame.magnitude();
            let mut bins = [0.0_f32; 12];

            for (bin_idx, &mag) in magnitudes.iter().enumerate() {
                if let Some(pitch_class) = chroma_map.get(bin_idx).copied().flatten() {
                    bins[pitch_class] += mag * mag; // accumulate power
                }
            }

            // Normalize: divide by max to get [0, 1] range, or leave zero
            let max = bins.iter().copied().fold(0.0_f32, f32::max);
            if max > f32::EPSILON {
                for b in &mut bins {
                    *b /= max;
                }
            }

            result.push(ChromaVector { bins });
        }

        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

/// Maps a frequency in Hz to a pitch class index (0 = C, 9 = A, etc.).
///
/// Returns `None` for frequencies below C1 (~32.7 Hz) or non-positive.
fn frequency_to_pitch_class(freq_hz: f32, tuning_hz: f32) -> Option<usize> {
    if freq_hz <= 0.0 || tuning_hz <= 0.0 {
        return None;
    }
    // A4 = tuning_hz, MIDI note 69. Pitch class = midi_note % 12.
    // midi_note = 69 + 12 * log2(freq / tuning)
    // We only need the fractional part mod 12.
    let semitones_from_a4 = 12.0 * (freq_hz / tuning_hz).log2();
    // A is pitch class 9. So pitch_class = (9 + round(semitones)) % 12.
    let midi_approx = 69.0 + semitones_from_a4;
    if midi_approx < 24.0 {
        // Below C1 — too low to assign meaningfully
        return None;
    }
    let pitch_class = midi_approx.round() as i32 % 12;
    // Ensure non-negative modulo
    Some(((pitch_class + 12) % 12) as usize)
}

/// Precomputes a lookup table mapping each FFT bin to its pitch class.
///
/// Returns `Vec<Option<usize>>` of length `fft_size / 2 + 1`.
fn build_chroma_map(fft_size: usize, sample_rate: f32, tuning_hz: f32) -> Vec<Option<usize>> {
    let num_bins = fft_size / 2 + 1;
    let bin_freq = sample_rate / fft_size as f32;

    (0..num_bins)
        .map(|bin| {
            let freq = bin as f32 * bin_freq;
            frequency_to_pitch_class(freq, tuning_hz)
        })
        .collect()
}

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

    const SR: f32 = 44100.0;

    #[test]
    fn a4_maps_to_pitch_class_9() {
        let pc = frequency_to_pitch_class(440.0, 440.0);
        assert_eq!(pc, Some(9)); // A = 9
    }

    #[test]
    fn c4_maps_to_pitch_class_0() {
        // C4 ≈ 261.63 Hz
        let pc = frequency_to_pitch_class(261.63, 440.0);
        assert_eq!(pc, Some(0)); // C = 0
    }

    #[test]
    fn e4_maps_to_pitch_class_4() {
        // E4 ≈ 329.63 Hz
        let pc = frequency_to_pitch_class(329.63, 440.0);
        assert_eq!(pc, Some(4)); // E = 4
    }

    #[test]
    fn very_low_frequency_returns_none() {
        let pc = frequency_to_pitch_class(10.0, 440.0);
        assert_eq!(pc, None);
    }

    #[test]
    fn zero_frequency_returns_none() {
        assert_eq!(frequency_to_pitch_class(0.0, 440.0), None);
    }

    #[test]
    fn negative_frequency_returns_none() {
        assert_eq!(frequency_to_pitch_class(-100.0, 440.0), None);
    }

    #[test]
    fn octave_equivalence() {
        // A3 (220 Hz) and A5 (880 Hz) should both map to pitch class 9
        assert_eq!(frequency_to_pitch_class(220.0, 440.0), Some(9));
        assert_eq!(frequency_to_pitch_class(880.0, 440.0), Some(9));
    }

    #[test]
    fn chroma_map_length() {
        let map = build_chroma_map(4096, SR, 440.0);
        assert_eq!(map.len(), 2049); // 4096/2 + 1
    }

    #[test]
    fn chroma_map_dc_is_none() {
        let map = build_chroma_map(4096, SR, 440.0);
        assert_eq!(map[0], None); // DC bin = 0 Hz
    }

    #[test]
    fn pure_a4_dominates_a_bin() {
        let samples: Vec<f32> = (0..16384)
            .map(|i| (2.0 * PI * 440.0 * i as f32 / SR).sin())
            .collect();
        let extractor = ChromaExtractor::new(SR);
        let chroma = extractor.extract(&samples).unwrap();
        assert!(!chroma.is_empty());
        // Most frames should have A (bin 9) as dominant
        let a_dominant = chroma.iter().filter(|cv| cv.dominant_class() == 9).count();
        assert!(
            a_dominant > chroma.len() / 2,
            "A4 should dominate, but only {a_dominant}/{} frames had A dominant",
            chroma.len()
        );
    }

    #[test]
    fn pure_c4_dominates_c_bin() {
        // C4 ≈ 261.63 Hz
        let samples: Vec<f32> = (0..16384)
            .map(|i| (2.0 * PI * 261.63 * i as f32 / SR).sin())
            .collect();
        let extractor = ChromaExtractor::new(SR);
        let chroma = extractor.extract(&samples).unwrap();
        assert!(!chroma.is_empty());
        let c_dominant = chroma.iter().filter(|cv| cv.dominant_class() == 0).count();
        assert!(
            c_dominant > chroma.len() / 2,
            "C4 should dominate, but only {c_dominant}/{} frames had C dominant",
            chroma.len()
        );
    }

    #[test]
    fn silence_all_near_zero() {
        let samples = vec![0.0_f32; 8192];
        let extractor = ChromaExtractor::new(SR);
        let chroma = extractor.extract(&samples).unwrap();
        for cv in &chroma {
            let sum: f32 = cv.bins.iter().sum();
            assert!(
                sum < 1e-6,
                "silence should have near-zero chroma, got {sum}"
            );
        }
    }

    #[test]
    fn empty_input_error() {
        let extractor = ChromaExtractor::new(SR);
        assert_eq!(extractor.extract(&[]), Err(AnalysisError::EmptyInput));
    }

    #[test]
    fn short_signal_returns_zero_chroma() {
        let extractor = ChromaExtractor::new(SR).with_window_size(4096);
        let chroma = extractor.extract(&[1.0; 2048]).unwrap();
        assert_eq!(chroma.len(), 1);
        assert_eq!(chroma[0].bins, [0.0; 12]);
    }

    #[test]
    fn normalized_bins() {
        // Non-silent signal: max bin should be 1.0 after normalization
        let samples: Vec<f32> = (0..16384)
            .map(|i| (2.0 * PI * 440.0 * i as f32 / SR).sin())
            .collect();
        let extractor = ChromaExtractor::new(SR);
        let chroma = extractor.extract(&samples).unwrap();
        for cv in &chroma {
            let max = cv.bins.iter().copied().fold(0.0_f32, f32::max);
            if max > 0.0 {
                assert!(
                    (max - 1.0).abs() < 1e-6,
                    "max bin should be 1.0 after normalization, got {max}"
                );
            }
        }
    }

    #[test]
    fn dominant_name_matches_class() {
        let mut cv = ChromaVector { bins: [0.0; 12] };
        cv.bins[9] = 1.0; // A
        assert_eq!(cv.dominant_name(), "A");
        cv.bins[0] = 2.0; // C is now larger
        assert_eq!(cv.dominant_name(), "C");
    }

    #[test]
    fn builder_methods() {
        let e = ChromaExtractor::new(SR)
            .with_window_size(8192)
            .with_hop_size(4096)
            .with_tuning(442.0);
        assert_eq!(e.window_size, 8192);
        assert_eq!(e.hop_size, 4096);
        assert_eq!(e.tuning_hz, 442.0);
    }

    #[test]
    fn custom_tuning() {
        // A4 at 442 Hz tuning — A should still dominate
        let samples: Vec<f32> = (0..16384)
            .map(|i| (2.0 * PI * 442.0 * i as f32 / SR).sin())
            .collect();
        let extractor = ChromaExtractor::new(SR).with_tuning(442.0);
        let chroma = extractor.extract(&samples).unwrap();
        let a_dominant = chroma.iter().filter(|cv| cv.dominant_class() == 9).count();
        assert!(
            a_dominant > chroma.len() / 2,
            "442 Hz with 442 tuning should map to A"
        );
    }

    #[test]
    fn frame_count_matches_expected() {
        let n_samples = 16384;
        let window = 4096;
        let hop = 2048;
        let samples = vec![0.0_f32; n_samples];
        let extractor = ChromaExtractor::new(SR)
            .with_window_size(window)
            .with_hop_size(hop);
        let chroma = extractor.extract(&samples).unwrap();
        let expected_frames = (n_samples - window) / hop + 1;
        assert_eq!(
            chroma.len(),
            expected_frames,
            "expected {expected_frames} frames, got {}",
            chroma.len()
        );
    }

    #[test]
    fn no_nan_or_inf() {
        let samples: Vec<f32> = (0..8192)
            .map(|i| (i as f32 * 7.3).sin() * (i as f32 * 13.7).cos())
            .collect();
        let extractor = ChromaExtractor::new(SR);
        let chroma = extractor.extract(&samples).unwrap();
        for cv in &chroma {
            for &b in &cv.bins {
                assert!(b.is_finite(), "non-finite chroma bin: {b}");
            }
        }
    }
}