Skip to main content

ff_analysis/analysis/
bpm_detector.rs

1//! BPM (tempo) detection result type.
2//!
3//! This module defines [`BpmResult`], the public output type for BPM detection.
4//! The detector itself (spectral flux + autocorrelation) is added separately.
5
6use std::time::Duration;
7
8/// Result of BPM (beats-per-minute) detection over an audio stream.
9#[derive(Debug, Clone, PartialEq)]
10pub struct BpmResult {
11    /// Detected tempo in beats per minute.
12    pub bpm: f64,
13    /// Timestamp of each detected beat, measured from the start of the file.
14    pub beats: Vec<Duration>,
15    /// Detection confidence in `[0.0, 1.0]`; values below `0.4` indicate an
16    /// ambiguous rhythm.
17    pub confidence: f32,
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn bpm_result_should_hold_fields() {
26        let result = BpmResult {
27            bpm: 128.0,
28            beats: vec![Duration::from_millis(0), Duration::from_millis(469)],
29            confidence: 0.9,
30        };
31        assert!((result.bpm - 128.0).abs() < f64::EPSILON);
32        assert_eq!(result.beats.len(), 2);
33        assert!(result.confidence > 0.4);
34    }
35}