include Yin pitch tracking algo

This commit is contained in:
Trent
2017-06-26 22:30:05 -04:00
parent 025d7cd6aa
commit 7db2d6c00b
10 changed files with 324 additions and 172 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
JuceLibraryCode/
Builds/
*bak
*jucer

View File

@@ -1,7 +1,6 @@
#include "PluginProcessor.h"
#include "PluginEditor.h"
LampshadeAudioProcessor::LampshadeAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor (BusesProperties()
@@ -69,6 +68,7 @@ void LampshadeAudioProcessor::changeProgramName (int index, const String& newNam
void LampshadeAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
myYin = Yin(sampleRate, samplesPerBlock);
}
void LampshadeAudioProcessor::releaseResources()
@@ -105,8 +105,12 @@ void LampshadeAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffe
for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear(i, 0, buffer.getNumSamples());
float pitch = myYin.getPitch(buffer.getWritePointer (0)); // returns Pitch in Hertz
printf("pitch: %f", pitch);
for (int channel = 0; channel < totalNumInputChannels; ++channel) {
//float* channelData = buffer.getWritePointer (channel);
}
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "Yin.h"
class LampshadeAudioProcessor : public AudioProcessor
{
@@ -38,4 +38,5 @@ public:
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LampshadeAudioProcessor)
Yin myYin;
};

146
Source/Yin.cpp Normal file
View File

@@ -0,0 +1,146 @@
#include "Yin.h"
#include <stdlib.h>
//#include "WProgram.h"
void Yin::initialize(float yinSampleRate,int yinBufferSize){
bufferSize = yinBufferSize;
sampleRate = yinSampleRate;
halfBufferSize = bufferSize / 2;
threshold = 0.15;
probability = 0.0;
//initialize array and set it to zero
yinBuffer = (float *) malloc(sizeof(float)* halfBufferSize);
for(int i = 0; i < halfBufferSize; i++){
yinBuffer[i] = 0;
}
}
Yin::Yin(){
}
Yin::Yin(float yinSampleRate,int yinBufferSize){
initialize(yinSampleRate,yinBufferSize);
}
float Yin::getProbability(){
return probability;
}
float Yin::getPitch(float* buffer){
int tauEstimate = -1;
float pitchInHertz = -1;
//step 2
difference(buffer);
// step 3
cumulativeMeanNormalizedDifference();
//step 4
tauEstimate = absoluteThreshold();
//step 5
if(tauEstimate != -1){
pitchInHertz = sampleRate / parabolicInterpolation(tauEstimate);
}
return pitchInHertz;
}
float Yin::parabolicInterpolation(int tauEstimate) {
float betterTau;
int x0;
int x2;
if (tauEstimate < 1) {
x0 = tauEstimate;
}
else {
x0 = tauEstimate - 1;
}
if (tauEstimate + 1 < halfBufferSize) {
x2 = tauEstimate + 1;
}
else {
x2 = tauEstimate;
}
if (x0 == tauEstimate) {
if (yinBuffer[tauEstimate] <= yinBuffer[x2]) {
betterTau = tauEstimate;
}
else {
betterTau = x2;
}
}
else if (x2 == tauEstimate) {
if (yinBuffer[tauEstimate] <= yinBuffer[x0]) {
betterTau = tauEstimate;
}
else {
betterTau = x0;
}
}
else {
float s0, s1, s2;
s0 = yinBuffer[x0];
s1 = yinBuffer[tauEstimate];
s2 = yinBuffer[x2];
// fixed AUBIO implementation, thanks to Karl Helgason:
// (2.0f * s1 - s2 - s0) was incorrectly multiplied with -1
betterTau = tauEstimate + (s2 - s0) / (2 * (2 * s1 - s2 - s0));
}
return betterTau;
}
void Yin::cumulativeMeanNormalizedDifference(){
int tau;
yinBuffer[0] = 1;
float runningSum = 0;
for (tau = 1; tau < halfBufferSize; tau++) {
runningSum += yinBuffer[tau];
yinBuffer[tau] *= tau / runningSum;
}
}
void Yin::difference(float* buffer){
int index;
int tau;
float delta;
for(tau = 0 ; tau < halfBufferSize; tau++){
for(index = 0; index < halfBufferSize; index++){
delta= buffer[index] - buffer[index + tau];
yinBuffer[tau] += delta * delta;
}
}
}
int Yin::absoluteThreshold(){
int tau;
// first two positions in yinBuffer are always 1
// So start at the third (index 2)
for (tau = 2; tau < halfBufferSize ; tau++) {
if (yinBuffer[tau] < threshold) {
while (tau + 1 < halfBufferSize && yinBuffer[tau + 1] < yinBuffer[tau]) {
tau++;
}
// found tau, exit loop and return
// store the probability
// From the YIN paper: The threshold determines the list of
// candidates admitted to the set, and can be interpreted as the
// proportion of aperiodic power tolerated
// within a ëëperiodicíí signal.
//
// Since we want the periodicity and and not aperiodicity:
// periodicity = 1 - aperiodicity
probability = 1 - yinBuffer[tau];
break;
}
}
// if no pitch found, tau => -1
if (tau == halfBufferSize || yinBuffer[tau] >= threshold) {
tau = -1;
probability = 0;
}
return tau;
}

26
Source/Yin.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
//#include "WProgram.h"
class Yin{
public:
Yin();
Yin(float sampleRate,int bufferSize);
void initialize(float sampleRate,int bufferSize);
float getPitch(float* buffer);
float getProbability();
private:
float parabolicInterpolation(int tauEstimate);
int absoluteThreshold();
void cumulativeMeanNormalizedDifference();
void difference(float* buffer);
double threshold;
int bufferSize;
int halfBufferSize;
float sampleRate;
float* yinBuffer;
float probability;
};

View File

@@ -1,65 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<JUCERPROJECT id="RELS1e" name="lampshade" displaySplashScreen="1" reportAppUsage="1"
splashScreenColour="Dark" projectType="audioplug" version="1.0.0"
bundleIdentifier="com.yourcompany.lampshade" includeBinaryInAppConfig="1"
buildVST="1" buildVST3="0" buildAU="1" buildAUv3="0" buildRTAS="0"
buildAAX="0" buildStandalone="0" enableIAA="0" pluginName="lampshade"
pluginDesc="lampshade" pluginManufacturer="yourcompany" pluginManufacturerCode="Manu"
pluginCode="Rels" pluginChannelConfigs="" pluginIsSynth="0" pluginWantsMidiIn="0"
pluginProducesMidiOut="0" pluginIsMidiEffectPlugin="0" pluginEditorRequiresKeys="0"
pluginAUExportPrefix="lampshadeAU" pluginRTASCategory="" aaxIdentifier="com.yourcompany.lampshade"
pluginAAXCategory="AAX_ePlugInCategory_Dynamics" jucerVersion="5.0.2">
<MAINGROUP id="GHLPwD" name="lampshade">
<GROUP id="{C42E5ACC-06AB-BF28-4550-5DDDE3A4BA5F}" name="Source">
<FILE id="YxKhY1" name="PluginProcessor.cpp" compile="1" resource="0"
file="Source/PluginProcessor.cpp"/>
<FILE id="n6D5RN" name="PluginProcessor.h" compile="0" resource="0"
file="Source/PluginProcessor.h"/>
<FILE id="LeuZUn" name="PluginEditor.cpp" compile="1" resource="0"
file="Source/PluginEditor.cpp"/>
<FILE id="ReytWw" name="PluginEditor.h" compile="0" resource="0" file="Source/PluginEditor.h"/>
</GROUP>
</MAINGROUP>
<EXPORTFORMATS>
<XCODE_MAC targetFolder="Builds/MacOSX">
<CONFIGURATIONS>
<CONFIGURATION name="Debug" isDebug="1" optimisation="1" targetName="lampshade"/>
<CONFIGURATION name="Release" isDebug="0" optimisation="3" targetName="lampshade"/>
</CONFIGURATIONS>
<MODULEPATHS>
<MODULEPATH id="juce_core" path="../JUCE/modules"/>
<MODULEPATH id="juce_events" path="../JUCE/modules"/>
<MODULEPATH id="juce_graphics" path="../JUCE/modules"/>
<MODULEPATH id="juce_data_structures" path="../JUCE/modules"/>
<MODULEPATH id="juce_gui_basics" path="../JUCE/modules"/>
<MODULEPATH id="juce_gui_extra" path="../JUCE/modules"/>
<MODULEPATH id="juce_cryptography" path="../JUCE/modules"/>
<MODULEPATH id="juce_video" path="../JUCE/modules"/>
<MODULEPATH id="juce_opengl" path="../JUCE/modules"/>
<MODULEPATH id="juce_audio_basics" path="../JUCE/modules"/>
<MODULEPATH id="juce_audio_devices" path="../JUCE/modules"/>
<MODULEPATH id="juce_audio_formats" path="../JUCE/modules"/>
<MODULEPATH id="juce_audio_processors" path="../JUCE/modules"/>
<MODULEPATH id="juce_audio_plugin_client" path="../JUCE/modules"/>
</MODULEPATHS>
</XCODE_MAC>
</EXPORTFORMATS>
<MODULES>
<MODULE id="juce_audio_basics" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_audio_devices" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_audio_formats" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_audio_plugin_client" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_audio_processors" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_core" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_cryptography" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_data_structures" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_events" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_graphics" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_gui_basics" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_gui_extra" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_opengl" showAllCode="1" useLocalCopy="0"/>
<MODULE id="juce_video" showAllCode="1" useLocalCopy="0"/>
</MODULES>
<JUCEOPTIONS JUCE_QUICKTIME="disabled"/>
</JUCERPROJECT>

Binary file not shown.

0
test Project/Icon Normal file
View File

BIN
test Project/test.als Normal file

Binary file not shown.

View File

@@ -1,4 +1,4 @@
<REAPER_PROJECT 0.1 "5.40/OSX64" 1497899251
<REAPER_PROJECT 0.1 "5.40/OSX64" 1498529808
RIPPLE 0
GROUPOVERRIDE 0 0 0
AUTOXFADE 1
@@ -102,4 +102,43 @@
>
<PROJBAY
>
<TRACK {0285AA2F-2341-854C-BBEE-985C6E7A9ADD}
NAME ""
PEAKCOL 16576
BEAT -1
AUTOMODE 0
VOLPAN 1 0 -1 -1 1
MUTESOLO 0 0 0
IPHASE 0
ISBUS 0 0
BUSCOMP 0 0
SHOWINMIX 1 0.6667 0.5 1 0.5 0 0 0
FREEMODE 0
SEL 0
REC 1 0 0 0 0 0 0
VU 2
TRACKHEIGHT 0 0
INQ 0 0 0 0.5 100 0 0 100
NCHAN 2
FX 1
TRACKID {0285AA2F-2341-854C-BBEE-985C6E7A9ADD}
PERF 0
MIDIOUT -1
MAINSEND 1 0
<FXCHAIN
WNDRECT 125 469 639 376
SHOW 0
LASTSEL 0
DOCKED 0
BYPASS 0 0 0
<VST "VST: lampshade (yourcompany)" lampshade.vst 0 "" 1382378611
c2xlUu5e7f4CAAAAAQAAAAAAAAACAAAAAAAAAAIAAAABAAAAAAAAAAIAAAAAAAAACAAAAAAAAAAAABAA
776t3g3wrd4=
AAAQAAAA
>
FLOATPOS 0 0 0 0
FXID {B7DD42FB-8D0E-C24C-924A-407EC4334EB7}
WAK 0
>
>
>