#pitch #note #frequency #detection #detect

pitch-detector

A frequency and note detector library written in Rust

3 unstable releases

0.3.1 Jun 3, 2022
0.3.0 Apr 18, 2022
0.1.0 Mar 25, 2022

#369 in Audio

41 downloads per month

MIT license

60KB
1.5K SLoC

pitch-detector

Build status docs.rs codecov Crates.io GitHub

A pitch and note detector library written in Rust.

Usage

Probably the most common use case is to detect the predominant frequency of a signal.

use pitch_detector::{
    pitch::{hanned_fft::HannedFftDetector, PitchDetector},
};

let sample_rate = 44100.
let signal: Vec<f64> = ...;

let mut detector = HannedFftDetector::default();
let freq: f64 = detector.detect_pitch(&signal, sample_rate)?;

Another common use case is to detect the predominant note of a signal. This use case is similar to the first, but the predominant note of the signal maps to a range of frequencies, which includes out-of-tune frequencies. This use case is common for tuner applications, where the user would still want to know which note is being played, even if it's out of tune. The return type of detect_note includes the offset in cents from the note name, in-tune frequency, and other useful information.

use pitch_detector::{
    pitch::{hanned_fft::HannedFftDetector, PitchDetector},
    note::{detect_note},
};

let sample_rate = 44100.
let signal: Vec<f64> = ...;

let mut detector = HannedFftDetector::default();
let note = detect_note(
        &signal,
        &mut detector,
        sample_rate,
    )?;
assert_eq!(note.note_name, NoteName::A);

The last use case is to detect a note with a hint. So far, the previous use cases have been about detecting the predominant frequency or note. In this use case, we are providing the detector a hint so that it can detect a frequency that might not be the predominant note. This is useful when there are multiple frequencies in a signal (as there commonly are), but you want to know if the signal contains a specific note, and the degree to which this specific note is in tune or not.

let sample_rate = 44100.
let mixed_signal: Vec<f64> = ... // mixed_signal contains multiple overlapping frequencies

let note = detector
        .detect_note_with_hint(
            NoteName::A,
            &mixed_signal,
            sample_rate,
        )?;
assert_eq!(note.note_name, NoteName::A);

Check out the examples directory for more.

Testing

Run cargo pitch to run tests for all features, and cargo benchmark alias to run benchmarks with features setup correctly.

License

Licensed under the MIT license

Dependencies

~5–9MB
~147K SLoC