Autocorrelation-based C++ pitch detection algorithms with O(nlogn) running time:
- McLeod pitch method - paper - visualization
- YIN - paper - visualization
Using this project should be as easy as make && sudo make install
on Linux with a modern GCC - I don't officially support other platforms.
This project depends on ffts. To run the tests, you need googletest, and run make -C test/ && ./test/test
. To run the bench, you need google benchmark, and run make -C test/ bench && ./test/bench
.
The code is lightly documented in the public header file. Compile your code with -lpitch_detection
.
The namespaces are pitch
and pitch_alloc
.
The pitch
namespace functions are for automatic buffer allocation:
#include <pitch_detection.h>
//std::vector<double> audio_buffer with sample rate e.g. 48000
double pitch_yin = pitch::yin(audio_buffer, 48000);
double pitch_mpm = pitch::mpm(audio_buffer, 48000);
If you want to detect pitch for multiple audio buffers of a uniform size, you can do more manual memory control with the pitch_alloc
namespace:
#include <pitch_detection.h>
//buffers have fixed length e.g. 48000, same as sample rate
pitch_alloc::Mpm ma(48000);
pitch_alloc::Yin ya(48000);
for (int i = 0; i < 10000; ++i) {
//std::vector<double> audio_buffer size 48000 sample rate 48000
auto pitch_yin = pitch_alloc::yin(audio_buffer, 48000, &ya);
auto pitch_mpm = pitch_alloc::mpm(audio_buffer, 48000, &ma);
}
The auto allocation strategy performs hundreds of thousands of allocations:
Manual allocation, as expected, performs less allocations by several orders of magnitude: