Skip to content

Repository files navigation

LibPyin pitch detection library

LibPyin is a plug-in library for pitch (fundamental frequency) detection (see here https://en.wikipedia.org/wiki/Pitch_detection_algorithm). The library provides simple C and C++ interface for easy incorporability to projects in other languages.

The library depends on the pYIN algorithm by M. Mauch and S. Dixon. See https://code.soundsoftware.ac.uk/projects/pyin for details.

Compiling

A C++11 compliant compiler is needed, however there are no additional dependencies. The repository contains CMake and QMake files for easy compiling.

To compile the library, run:

cmake CMakeLists.txt
make

If do not want to use either for some reason, and are making a project from the code yourself, do set the LIBPYIN_BUILD_SHARED macro to make sure the symbols are correcly exported (see CMakeLists.txt for details).

Use

When using from C++, include libpyincpp.h. When using from C, include libpyinc.h and link against LibPyin.

Can I use LibPyin Source in My Code?

Absolutelly, just copy-paste the source folder to your project and add it to the include path.

Examples

There is C, C++, and C# example present. Each example generates a short sine-wave of 440 hz and extracts the frequency from the wave. The usage is platform and language dependent.

For each example first compile the library.

C example on Unix

gcc main.c -L"." -lLibPyin -lm
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. # Add the current location to the path so the library is loaded
./a.out

C++ example on Unix

g++ main.cpp -L"." -lLibPyin -std=c++11
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. # Add the current location to the path so the library is loaded
./a.out

Unity example

  1. Download the Release of the complied .dll .
  2. Add LibPyin.dll and LoadLibrary.cs to your Assets.
  3. Call LoadLibrary.use(); from another script attached to a gameobject.

C# example on Windows

  1. Before building LibPyin, set on the /clr support on the VS Settings/General. (You may need to switch /EHs to /EHa at Code Generation).
  2. Build as Debug and Release
  3. Run Visual Studio Developer prompt.
  4. csc main.cs LoadLibrary.cs /unsafe /reference:Debug\LibPyin.dll /platform:x86
  5. main.exe

Cpp interface

Creates a PYIN object, for each track you should create one object
[in]sample_rate frequency of the track, e.g. 44100 samples per second
[in]block_size length of a block used for obtaining a pitch, the higher the slower, 2048 is recommended
[in]step_size length of a step between two mined pitches, the smaller the slower, 512 is recommended

PyinCpp(
constint sample_rate, constint block_size = _DEFAULT_BLOCK_SIZE, constint step_size = _DEFAULT_STEP_SIZE);

The cut off is a number between [0-1] that says whether the pitch is still to be considered as correct based on the estimate probability (the pitch will be ignored if the probability is lower than the number)

voidPyinCpp::setCutOff(constfloat cut_off);
floatPyinCpp::getCutOff();

Reserves the internal vectors for the given number of expected samples

voidPyinCpp::reserve(int sample_count);

Feed new data and obtain the pitches mined using the new data

std::vector<float> PyinCpp::feed(const std::vector<float> & new_samples);

Get all the mined pitches

const std::vector<float> & PyinCpp::getPitches() const;

Get all frequency-probability candidate pairs for each frame

const std::vector<std::vector<std::pair<float, float>>> & PyinCpp::getPitchCandidates() const;

Each element in the returned vector corresponds to a frame. For each frame, you get a vector of (frequency, probability) pairs representing all pitch candidates detected by the PYIN algorithm.

Resets to the after-construction state

voidPyinCpp::clear();

C interface

Initializes a PYIN object, must be called before using pyinc
[in]sample_rate frequency of the track, e.g. 44100 samples per second
[in]block_size length of a block used for obtaining a pitch, the higher the slower, 2048 is recommended
[in]step_size length of a step between two mined pitches, the smaller the slower, 512 is recommended

voidpyinc_init(
constintsample_rate, constintblock_size, constintstep_size);

The cut off is a number between [0-1] that says whether the pitch is still to be considered as correct based on the estimate probability (the pitch will be ignored if the probability is lower than the number)

voidSHARED_EXPORTpyinc_set_cut_off(constfloatcut_off);
floatSHARED_EXPORTpyinc_get_cut_off();

Reserves the internal vectors for the given number of expected samples

voidpyinc_reserve(intsample_count);

Feed new data and obtain the pitches mined using the new data !THE RANGE IS VALID ONLY UNTIL THE NEXT CALL OF pyinc_feed OR pyinc_clear

structpyinc_pitch_rangepyinc_feed(constfloat*new_samples, intsample_count);

Get all the mined pitches

structpyinc_pitch_rangepyinc_get_pitches();

Get the number of frames with pitch candidates

intpyinc_get_candidate_frame_count();

Get pitch candidates for a specific frame index

structpyinc_candidate_rangepyinc_get_candidates_for_frame(intframe_index);

The pyinc_pitch_candidate structure contains:

structpyinc_pitch_candidate {
floatfrequency;
floatprobability;
};

This allows you to access all frequency-probability pairs returned by the PYIN algorithm for each frame, as described in the original PYIN paper.

Resets to the after-construction state

voidpyinc_clear();

Licence

GNU GPLv3 with attribution. The authors of pYIN ask to kindly attribute their work via a citation:

@INPROCEEDINGS{6853678,
author={Mauch, Matthias and Dixon, Simon},
booktitle={2014 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)}, title={PYIN: A fundamental frequency estimator using probabilistic threshold distributions}, year={2014},
pages={659-663},
doi={10.1109/ICASSP.2014.6853678}}
} 

About

Pitch / fundamental frequency detection library for C,C++,C#

Resources

Stars

59 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages