detect_note_in_range

Function detect_note_in_range 

Source
pub fn detect_note_in_range<D: PitchDetector>(
    signal: &[f64],
    freq_detector: &mut D,
    sample_rate: f64,
    freq_range: Range<f64>,
) -> Option<NoteDetectionResult>
Expand description

Returns the predominant note of the given signal within the specified range.

ยงExamples

use pitch_detector::{
    core::{utils::sine_wave_signal, NoteName},
    note::{detect_note_in_range},
    pitch::{HannedFftDetector, PitchDetector},
};
const MAX_FREQ: f64 = 1046.50; // C6
const MIN_FREQ: f64 = 32.7; // C1

let mut detector = HannedFftDetector::default();
let slightly_sharp_a = 448.;
let signal = sine_wave_signal(NUM_SAMPLES, slightly_sharp_a, SAMPLE_RATE);
let note = detect_note_in_range(
    &signal,
    &mut detector,
    SAMPLE_RATE,
    MIN_FREQ..MAX_FREQ,
)?;
assert_eq!(note.note_name, NoteName::A);
assert!(note.cents_offset > 0.);

let high_pitch = sine_wave_signal(NUM_SAMPLES, 2000., SAMPLE_RATE);
let note = detect_note_in_range(
    &signal,
    &mut detector,
    SAMPLE_RATE,
    MIN_FREQ..MAX_FREQ,
);
assert!(note.is_none());