pitch_detection/lib.rs
1//! # Pitch Detection
2//! *pitch_detection* implements several algorithms for estimating the
3//! fundamental frequency of a sound wave stored in a buffer. It is designed
4//! to be usable in a WASM environment.
5//!
6//! # Detectors
7//! A *detector* is an implementation of a pitch detection algorithm. Each detector's tolerance
8//! for noise and polyphonic sounds varies.
9//!
10//! * [AutocorrelationDetector][detector::autocorrelation]
11//! * [McLeodDetector][detector::mcleod]
12//! * [YINDetector][detector::yin]
13//!
14//! # Examples
15//! ```
16//! use pitch_detection::detector::mcleod::McLeodDetector;
17//! use pitch_detection::detector::PitchDetector;
18//!
19//! fn main() {
20//! const SAMPLE_RATE: usize = 44100;
21//! const SIZE: usize = 1024;
22//! const PADDING: usize = SIZE / 2;
23//! const POWER_THRESHOLD: f64 = 5.0;
24//! const CLARITY_THRESHOLD: f64 = 0.7;
25//!
26//! // Signal coming from some source (microphone, generated, etc...)
27//! let dt = 1.0 / SAMPLE_RATE as f64;
28//! let freq = 300.0;
29//! let signal: Vec<f64> = (0..SIZE)
30//! .map(|x| (2.0 * std::f64::consts::PI * x as f64 * dt * freq).sin())
31//! .collect();
32//!
33//! let mut detector = McLeodDetector::new(SIZE, PADDING);
34//!
35//! let pitch = detector
36//! .get_pitch(&signal, SAMPLE_RATE, POWER_THRESHOLD, CLARITY_THRESHOLD)
37//! .unwrap();
38//!
39//! println!("Frequency: {}, Clarity: {}", pitch.frequency, pitch.clarity);
40//! }
41//! ```
42
43pub use detector::internals::Pitch;
44
45pub mod detector;
46pub mod float;
47pub mod utils;