Quickly and accurately determine the pitch and volume of a sound sample.
This crate uses a Bitstream Autocorrelation Function (BCF) invented by Joel de Guzman to determine the pitch of the sound sample.
It is really quite simple. Just write:
let (hz, amplitude) = pitch::detect(&samples)
samples is a fixed-size array of f32s. I use 2,048 for the array length.
The length of the array should be 2 times the period of the lowest note you
want to pick up (because of math and some theories). So 2,048 gives us
notes up to 1,024 samples per period, which in 48,000Hz is about 47Hz
(48,000Hz / 1,024 Samples). For reference, the lowest sine wave that a
human can hear is 20Hz (a little more than an octave lower).
Example code can be found in
test.rs. The audio files I used were me
playing my trombone on notes A1, A2, A3, and A4, and generating sine, saw,
and square waves with Audacity on the note A4 (making sure to set 48,000hz
sample rate). Here is my example output (Note: I didn't tune my trombone
beforehand since this program is a tuner, and may give different results
than other tuners):
A1: 55.422962 Hz, 0.7379681 Vl A2: 107.865166 Hz, 0.43305764 Vl A3: 215.78847 Hz, 0.69536424 Vl A4: 436.30658 Hz, 0.117435224 Vl SINE_A4: 439.99805 Hz, 0.8001343 Vl SAW_A4: 439.99167 Hz, 0.8001343 Vl SQUARE_A4: 434.3891 Hz, 0.80022585 Vl
Do the BCF calculation on raw samples. Returns