Adapted from https://github.com/irtemed88/PitchDetector