#pitch #detection #sound #frequency

no-std pitch-detection

A collection of algorithms to determine the pitch of a sound sample

3 releases (breaking)

0.3.0 Jun 30, 2022
0.2.0 Nov 13, 2020
0.1.0 Mar 17, 2019

#272 in Audio

Download history 71/week @ 2024-01-05 81/week @ 2024-01-12 105/week @ 2024-01-19 68/week @ 2024-01-26 4/week @ 2024-02-02 27/week @ 2024-02-09 106/week @ 2024-02-16 91/week @ 2024-02-23 73/week @ 2024-03-01 78/week @ 2024-03-08 78/week @ 2024-03-15 67/week @ 2024-03-22 112/week @ 2024-03-29 61/week @ 2024-04-05 125/week @ 2024-04-12 174/week @ 2024-04-19

486 downloads per month

MIT/Apache

1MB
668 lines

workflow status crates.io

pitch_detection

Usage

use pitch_detection::detector::mcleod::McLeodDetector;
use pitch_detection::detector::PitchDetector;

fn main() {
    const SAMPLE_RATE: usize = 44100;
    const SIZE: usize = 1024;
    const PADDING: usize = SIZE / 2;
    const POWER_THRESHOLD: f64 = 5.0;
    const CLARITY_THRESHOLD: f64 = 0.7;

    // Signal coming from some source (microphone, generated, etc...)
    let dt = 1.0 / SAMPLE_RATE as f64;
    let freq = 300.0;
    let signal: Vec<f64> = (0..SIZE)
        .map(|x| (2.0 * std::f64::consts::PI * x as f64 * dt * freq).sin())
        .collect();

    let mut detector = McLeodDetector::new(SIZE, PADDING);

    let pitch = detector
        .get_pitch(&signal, SAMPLE_RATE, POWER_THRESHOLD, CLARITY_THRESHOLD)
        .unwrap();

    println!("Frequency: {}, Clarity: {}", pitch.frequency, pitch.clarity);
}

Live Demo

Demo Page Source

Documentation

LaTeX formulas can be used in documentation. This is enabled by a method outlined in rust-latex-doc-minimal-example. To build the docs, use

cargo doc --no-deps

The --no-deps flag is needed because special headers are included to auto-process the math in the documentation. This header is specified using a relative path and so an error is produced if cargo tries generate documentation for dependencies.

Dependencies

~3MB
~57K SLoC