Make an own class for AKF detector (tested for C2-B5)
This commit is contained in:
1 parent
4c597cb4f8
commit
f1a885b95e
10 files changed
+259
-146
No files matched your search
@@ -95,6 +95,7 @@
|
||||
<PreprocessorDefinitions>WIN64;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<FloatingPointModel>Fast</FloatingPointModel>
|
||||
<MinimalRebuild>false</MinimalRebuild>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -112,6 +113,7 @@
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<FloatingPointModel>Fast</FloatingPointModel>
|
||||
<MinimalRebuild>false</MinimalRebuild>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -169,7 +171,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="compatibility.cpp" />
|
||||
<ClCompile Include="performous\pitch.cc" />
|
||||
<ClCompile Include="pdaAKF.cpp" />
|
||||
<ClCompile Include="ptAKF.cpp" />
|
||||
<ClCompile Include="PitchWrapper.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -177,7 +179,7 @@
|
||||
<ClInclude Include="Helper.h" />
|
||||
<ClInclude Include="performous\libda\fft.hpp" />
|
||||
<ClInclude Include="performous\libda\sample.hpp" />
|
||||
<ClInclude Include="pdaAKF.h" />
|
||||
<ClInclude Include="ptAKF.h" />
|
||||
<ClInclude Include="PitchWrapper.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
<ClCompile Include="performous\pitch.cc">
|
||||
<Filter>Quelldateien\performous</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="pdaAKF.cpp">
|
||||
<ClCompile Include="ptAKF.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@@ -53,7 +53,7 @@
|
||||
<ClInclude Include="performous\libda\sample.hpp">
|
||||
<Filter>Headerdateien\performous\libda</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="pdaAKF.h">
|
||||
<ClInclude Include="ptAKF.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,58 +1,23 @@
|
||||
#include "PitchWrapper.h"
|
||||
#include "pdaAKF.h"
|
||||
#include "Helper.h"
|
||||
|
||||
AnalyzerExt::AnalyzerExt(double baseToneFrequency, int minHalfTone, int maxHalfTone, unsigned step): Analyzer(44100., "", step){
|
||||
PitchTrackerAKF::Init(baseToneFrequency, minHalfTone, maxHalfTone);
|
||||
|
||||
Analyzer* Analyzer_Create(unsigned step){
|
||||
return new Analyzer(44100, "", step);
|
||||
}
|
||||
|
||||
AnalyzerExt::~AnalyzerExt(){
|
||||
PitchTrackerAKF::DeInit();
|
||||
}
|
||||
|
||||
int AnalyzerExt::GetNoteFast(double* maxVolume, float* weights){
|
||||
float AnaylsisBuf[m_SampleCt];
|
||||
size_t size = m_fastAnalysisBuf.size();
|
||||
if(size > m_SampleCt)
|
||||
m_fastAnalysisBuf.pop(size - m_SampleCt);
|
||||
if(!m_fastAnalysisBuf.read(AnaylsisBuf, AnaylsisBuf + m_SampleCt))
|
||||
return -1;
|
||||
return PitchTrackerAKF::GetTone(AnaylsisBuf, m_SampleCt, maxVolume, weights);
|
||||
}
|
||||
|
||||
void PtFast_Init(double baseToneFrequency, int minHalfTone, int maxHalfTone){
|
||||
PitchTrackerAKF::Init(baseToneFrequency, minHalfTone, maxHalfTone);
|
||||
}
|
||||
|
||||
void PtFast_DeInit(){
|
||||
PitchTrackerAKF::DeInit();
|
||||
}
|
||||
|
||||
int PtFast_GetTone(short* samples, int sampleCt, double* maxVolume, float* weights){
|
||||
if(sampleCt <= 0)
|
||||
return -1;
|
||||
double* dataDouble = short2DoubleArray(samples, sampleCt, false);
|
||||
int result = PitchTrackerAKF::GetTone(dataDouble, sampleCt, maxVolume, weights, true);
|
||||
freeDoubleArray(dataDouble);
|
||||
return result;
|
||||
}
|
||||
|
||||
AnalyzerExt* Analyzer_Create(double baseToneFrequency, int minHalfTone, int maxHalfTone, unsigned step){
|
||||
return new AnalyzerExt(baseToneFrequency, minHalfTone, maxHalfTone, step);
|
||||
}
|
||||
|
||||
void Analyzer_Free(AnalyzerExt* analyzer){
|
||||
void Analyzer_Free(Analyzer* analyzer){
|
||||
if(analyzer)
|
||||
delete analyzer;
|
||||
}
|
||||
|
||||
void Analyzer_InputFloat(AnalyzerExt* analyzer, float* data, int sampleCt){
|
||||
void Analyzer_InputFloat(Analyzer* analyzer, float* data, int sampleCt){
|
||||
if(sampleCt == 0 || !analyzer)
|
||||
return;
|
||||
analyzer->input(data, data + sampleCt);
|
||||
}
|
||||
|
||||
void Analyzer_InputShort(AnalyzerExt* analyzer, short* data, int sampleCt){
|
||||
void Analyzer_InputShort(Analyzer* analyzer, short* data, int sampleCt){
|
||||
if(sampleCt == 0 || !analyzer)
|
||||
return;
|
||||
float* dataFloat = short2FloatArray(data, sampleCt);
|
||||
@@ -60,45 +25,64 @@ void Analyzer_InputShort(AnalyzerExt* analyzer, short* data, int sampleCt){
|
||||
freeFloatArray(dataFloat);
|
||||
}
|
||||
|
||||
void Analyzer_InputByte(AnalyzerExt* analyzer, char* data, int sampleCt){
|
||||
if(sampleCt == 0 || !analyzer)
|
||||
void Analyzer_InputByte(Analyzer* analyzer, char* data, int sampleCt){
|
||||
if(sampleCt <= 0 || !analyzer)
|
||||
return;
|
||||
float* dataFloat = short2FloatArray(static_cast<short*>(static_cast<void*>(data)), sampleCt);
|
||||
analyzer->input(dataFloat, dataFloat + sampleCt);
|
||||
freeFloatArray(dataFloat);
|
||||
}
|
||||
|
||||
void Analyzer_Process(AnalyzerExt* analyzer){
|
||||
void Analyzer_Process(Analyzer* analyzer){
|
||||
if(!analyzer)
|
||||
return;
|
||||
analyzer->process();
|
||||
}
|
||||
|
||||
double Analyzer_GetPeak(AnalyzerExt* analyzer){
|
||||
double Analyzer_GetPeak(Analyzer* analyzer){
|
||||
if(!analyzer)
|
||||
return -999;
|
||||
return analyzer->getPeak();
|
||||
}
|
||||
|
||||
double Analyzer_FindNote(AnalyzerExt* analyzer, double minFreq, double maxFreq){
|
||||
double Analyzer_FindNote(Analyzer* analyzer, double minFreq, double maxFreq){
|
||||
if(!analyzer)
|
||||
return -1;
|
||||
const Tone* tone = analyzer->findTone(minFreq, maxFreq);
|
||||
return (tone == NULL) ? -1 : FreqToNote(tone->freq);
|
||||
}
|
||||
|
||||
int Analyzer_GetNoteFast(AnalyzerExt* analyzer, double* maxVolume, float* weights){
|
||||
if(!analyzer)
|
||||
return -1;
|
||||
return analyzer->GetNoteFast(maxVolume, weights);
|
||||
}
|
||||
|
||||
bool Analyzer_OutputFloat(AnalyzerExt* analyzer, float* data, int sampleCt, float rate){
|
||||
bool Analyzer_OutputFloat(Analyzer* analyzer, float* data, int sampleCt, float rate){
|
||||
if(!analyzer)
|
||||
return false;
|
||||
return analyzer->output(data, data + sampleCt, rate);
|
||||
}
|
||||
|
||||
DllExport PtAKF* PtAKF_Create(){
|
||||
return new PtAKF();
|
||||
}
|
||||
|
||||
DllExport void PtAKF_Free(PtAKF* analyzer){
|
||||
if(analyzer)
|
||||
delete analyzer;
|
||||
}
|
||||
|
||||
DllExport int PtAKF_GetNumHalfTones(){
|
||||
return PtAKF::GetNumHalfTones();
|
||||
}
|
||||
|
||||
DllExport void PtAKF_InputByte(PtAKF* analyzer, char* data, int sampleCt){
|
||||
if(sampleCt <= 0 || !analyzer)
|
||||
return;
|
||||
short* dataShort = static_cast<short*>(static_cast<void*>(data));
|
||||
analyzer->input(dataShort, dataShort + sampleCt);
|
||||
}
|
||||
|
||||
DllExport int PtAKF_GetNote(PtAKF* analyzer, double* maxVolume, float* weights){
|
||||
if(!analyzer)
|
||||
return -1;
|
||||
return analyzer->GetNote(maxVolume, weights);
|
||||
}
|
||||
|
||||
/*
|
||||
namespace Native{
|
||||
|
||||
+16
-28
@@ -1,37 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "performous/pitch.hh"
|
||||
#include "ptAKF.h"
|
||||
|
||||
#define DllExport extern "C" __declspec(dllexport)
|
||||
|
||||
class AnalyzerExt: public Analyzer{
|
||||
public:
|
||||
AnalyzerExt(double baseToneFrequency, int minHalfTone, int maxHalfTone, unsigned step = 200);
|
||||
~AnalyzerExt();
|
||||
template <typename InIt> void input(InIt begin, InIt end){
|
||||
m_fastAnalysisBuf.insert(begin, end);
|
||||
Analyzer::input(begin, end);
|
||||
}
|
||||
int GetNoteFast(double* maxVolume, float* weights);
|
||||
private:
|
||||
constexpr static size_t m_SampleCt = 4096;
|
||||
RingBuffer<m_SampleCt * 2> m_fastAnalysisBuf; // Buffer used for fast analysis instead of FFT
|
||||
};
|
||||
DllExport Analyzer* Analyzer_Create(unsigned step);
|
||||
DllExport void Analyzer_Free(Analyzer* analyzer);
|
||||
DllExport void Analyzer_InputFloat(Analyzer* analyzer, float* data, int sampleCt);
|
||||
DllExport void Analyzer_InputShort(Analyzer* analyzer, short* data, int sampleCt);
|
||||
DllExport void Analyzer_InputByte(Analyzer* analyzer, char* data, int sampleCt);
|
||||
DllExport void Analyzer_Process(Analyzer* analyzer);
|
||||
DllExport double Analyzer_GetPeak(Analyzer* analyzer);
|
||||
DllExport double Analyzer_FindNote(Analyzer* analyzer, double minFreq, double maxFreq);
|
||||
DllExport bool Analyzer_OutputFloat(Analyzer* analyzer, float* data, int sampleCt, float rate);
|
||||
|
||||
DllExport void PtFast_Init(double baseToneFrequency, int minHalfTone, int maxHalfTone);
|
||||
DllExport void PtFast_DeInit();
|
||||
DllExport int PtFast_GetTone(short* samples, int sampleCt, float* weights);
|
||||
|
||||
DllExport AnalyzerExt* Analyzer_Create(double baseToneFrequency, int minHalfTone, int maxHalfTone, unsigned step);
|
||||
DllExport void Analyzer_Free(AnalyzerExt* analyzer);
|
||||
DllExport void Analyzer_InputFloat(AnalyzerExt* analyzer, float* data, int sampleCt);
|
||||
DllExport void Analyzer_InputShort(AnalyzerExt* analyzer, short* data, int sampleCt);
|
||||
DllExport void Analyzer_InputByte(AnalyzerExt* analyzer, char* data, int sampleCt);
|
||||
DllExport void Analyzer_Process(AnalyzerExt* analyzer);
|
||||
DllExport double Analyzer_GetPeak(AnalyzerExt* analyzer);
|
||||
DllExport int Analyzer_GetNoteFast(AnalyzerExt* analyzer, double* maxVolume, float* weights);
|
||||
DllExport double Analyzer_FindNote(AnalyzerExt* analyzer, double minFreq, double maxFreq);
|
||||
DllExport bool Analyzer_OutputFloat(AnalyzerExt* analyzer, float* data, int sampleCt, float rate);
|
||||
DllExport PtAKF* PtAKF_Create();
|
||||
DllExport void PtAKF_Free(PtAKF* analyzer);
|
||||
DllExport int PtAKF_GetNumHalfTones();
|
||||
DllExport void PtAKF_InputByte(PtAKF* analyzer, char* data, int sampleCt);
|
||||
DllExport int PtAKF_GetNote(PtAKF* analyzer, double* maxVolume, float* weights);
|
||||
|
||||
/*
|
||||
public ref class CTone {
|
||||
@@ -74,7 +62,7 @@ public:
|
||||
String^ GetId();
|
||||
|
||||
private:
|
||||
AnalyzerExt* m_analyzer;
|
||||
Analyzer* m_analyzer;
|
||||
String^ m_id;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// A pitch detection that is based on the AKF and AMDF (initially from original Vocaluxe/USDx)
|
||||
namespace PitchTrackerAKF{
|
||||
void Init(double baseToneFrequency, int minHalfTone, int maxHalfTone);
|
||||
void DeInit();
|
||||
template<typename T> int GetTone(T *samples, int sampleCt, double* maxVolume, float *weights, bool scale = false);
|
||||
|
||||
typedef void (__stdcall * LogCallback)(const char* text);
|
||||
static LogCallback Log;
|
||||
void SetLogCallback(LogCallback Callback);
|
||||
}
|
||||
@@ -13,28 +13,112 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdaAKF.h"
|
||||
#include "ptAKF.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static constexpr double BaseToneFrequency = 65.4064; // lowest (half-)tone to analyze (C2 = 65.4064 Hz)
|
||||
static constexpr double HalftoneBase = 1.05946309436; // 2^(1/12) -> HalftoneBase^12 = 2 (one octave)
|
||||
int PtAKF::_InitCount = 0;
|
||||
float* PtAKF::_SamplesPerPeriodPerTone = NULL;
|
||||
|
||||
PtAKF::PtAKF(){
|
||||
_InitCount++;
|
||||
if(_InitCount == 1){
|
||||
_SamplesPerPeriodPerTone = new float[_MaxHalfTone + 1];
|
||||
//Init Array to avoid costly calculations
|
||||
for (int toneIndex = 0; toneIndex <= _MaxHalfTone; toneIndex++)
|
||||
{
|
||||
double freq = BaseToneFrequency * pow(HalftoneBase, toneIndex);
|
||||
_SamplesPerPeriodPerTone[toneIndex] = static_cast<float>(44100.0 / freq); // samples in one period
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PtAKF::~PtAKF(){
|
||||
_InitCount--;
|
||||
if(!_InitCount){
|
||||
delete[] _SamplesPerPeriodPerTone;
|
||||
_SamplesPerPeriodPerTone = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int PtAKF::GetNote(double* maxVolume, float* weights){
|
||||
float AnaylsisBuf[_SampleCt];
|
||||
size_t size = _AnalysisBuf.size();
|
||||
if(size > _SampleCt)
|
||||
_AnalysisBuf.pop(size - _SampleCt);
|
||||
if(!_AnalysisBuf.read(AnaylsisBuf, AnaylsisBuf + _SampleCt))
|
||||
return -1;
|
||||
return _GetNote(AnaylsisBuf, maxVolume, weights);
|
||||
}
|
||||
|
||||
int PtAKF::_GetNote(float* samples, double* maxVolume, float* weights){
|
||||
float maxVolumeL = 0;
|
||||
for(int i = 0; i < _SampleCt; i++){
|
||||
float vol = abs(samples[i]);
|
||||
if(vol > maxVolumeL)
|
||||
maxVolumeL = vol;
|
||||
}
|
||||
maxVolumeL /= 32767.;
|
||||
*maxVolume = maxVolumeL;
|
||||
|
||||
float maxWeight = -1;
|
||||
float minWeight = 1;
|
||||
int maxTone = -1;
|
||||
for (int toneIndex = _MinHalfTone; toneIndex <= _MaxHalfTone; toneIndex++)
|
||||
{
|
||||
float curWeight = _AnalyzeToneFunc(samples, toneIndex);
|
||||
|
||||
if (curWeight > maxWeight)
|
||||
{
|
||||
maxWeight = curWeight;
|
||||
maxTone = toneIndex;
|
||||
}
|
||||
|
||||
if (curWeight < minWeight)
|
||||
minWeight = curWeight;
|
||||
|
||||
weights[toneIndex - _MinHalfTone] = curWeight;
|
||||
}
|
||||
|
||||
if(maxWeight - minWeight > 0.01){
|
||||
return maxTone;
|
||||
}else return -1;
|
||||
}
|
||||
|
||||
float PtAKF::_AnalyzeToneFunc(float* samples, int toneIndex){
|
||||
// Use method by Kobayashi and Shimamura (1995): Combine AKF and AMDF to a new f(z)=AKF(z)/(AMDF(z)+k) with k=1
|
||||
|
||||
float samplesPerPeriodD = _SamplesPerPeriodPerTone[toneIndex]; // samples in one period
|
||||
int samplesPerPeriod = static_cast<int>(samplesPerPeriodD);
|
||||
float fHigh = samplesPerPeriodD - samplesPerPeriod;
|
||||
float fLow = 1.0f - fHigh;
|
||||
|
||||
float accumDistAKF = 0; // accumulated distances
|
||||
float accumDistAMDF = 0; // accumulated distances
|
||||
|
||||
// compare correlating samples
|
||||
int sampleIndex = 0; // index of sample to analyze
|
||||
// Start value= index of sample one period ahead
|
||||
for (int correlatingSampleIndex = sampleIndex + samplesPerPeriod; correlatingSampleIndex + 1 < _SampleCt; correlatingSampleIndex++, sampleIndex++)
|
||||
{
|
||||
// calc distance to corresponding sample in next period
|
||||
float xn = samples[sampleIndex];
|
||||
float xnt = samples[correlatingSampleIndex] * fLow + samples[correlatingSampleIndex + 1] * fHigh;
|
||||
accumDistAKF += xn * xnt;
|
||||
accumDistAMDF += abs(xn - xnt);
|
||||
}
|
||||
|
||||
accumDistAKF /= _SampleCt;
|
||||
accumDistAMDF /= sampleIndex;
|
||||
|
||||
return accumDistAKF / (accumDistAMDF * 32767.f + 32767.f); // Need to scale AKF by MAX^2 and AMDF by MAX, so do some maths to divide only once
|
||||
}
|
||||
/*
|
||||
|
||||
|
||||
|
||||
namespace PitchTrackerAKF{
|
||||
|
||||
static void doLog(const char* msg,...){
|
||||
va_list argptr = NULL;
|
||||
va_start(argptr, msg);
|
||||
|
||||
char buffer[200];
|
||||
vsprintf_s(buffer,msg, argptr);
|
||||
if(Log) Log(buffer);
|
||||
va_end(argptr);
|
||||
}
|
||||
|
||||
void SetLogCallback(LogCallback Callback){
|
||||
Log=Callback;
|
||||
}
|
||||
|
||||
static double *SamplesPerPeriodPerTone = NULL;
|
||||
static double BaseToneFrequency = -1;
|
||||
static int MinHalfTone = -1, MaxHalfTone = -1;
|
||||
@@ -138,4 +222,5 @@ namespace PitchTrackerAKF{
|
||||
template int GetTone(float *samples, int sampleCt, double* maxVolume, float *weights, bool scale);
|
||||
template int GetTone(double *samples, int sampleCt, double* maxVolume, float *weights, bool scale);
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include "performous/pitch.hh"
|
||||
|
||||
// A pitch detection that is based on the AKF and AMDF (initially from original Vocaluxe/USDx)
|
||||
class PtAKF{
|
||||
public:
|
||||
PtAKF();
|
||||
~PtAKF();
|
||||
|
||||
void input(short* begin, short* end){
|
||||
_AnalysisBuf.insert(begin, end);
|
||||
}
|
||||
|
||||
int GetNote(double* maxVolume, float* weights);
|
||||
static int GetNumHalfTones(){ return _MaxHalfTone - _MinHalfTone + 1;}
|
||||
private:
|
||||
static int _InitCount;
|
||||
static float* _SamplesPerPeriodPerTone;
|
||||
static constexpr int _MinHalfTone = 0; //C2
|
||||
static constexpr int _MaxHalfTone = 47; //B5
|
||||
|
||||
constexpr static size_t _SampleCt = 4096;
|
||||
RingBuffer<_SampleCt * 2> _AnalysisBuf;
|
||||
|
||||
static int _GetNote(float* samples, double* maxVolume, float* weights);
|
||||
static float _AnalyzeToneFunc(float* samples, int toneIndex);
|
||||
};
|
||||
Reference in new issue
Block a user