-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin_PitchDetector.cpp
211 lines (180 loc) · 8.07 KB
/
Plugin_PitchDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "AudioPluginUtil.h"
namespace PitchDetector
{
const int MAXINDEX = 16;
const int FFTSIZE = 8192;
const int WINSIZE = 4096;
const int HOPSIZE = 1024;
static float detected_freqs[MAXINDEX];
static float debugdata[FFTSIZE];
enum Param
{
P_INDEX,
P_LOCUT,
P_HICUT,
P_LOBIN,
P_HIBIN,
P_THR,
P_OSCPITCH,
P_MONITOR,
P_NUM
};
struct EffectData
{
struct Data
{
float p[P_NUM];
int counter;
float env;
float phase;
float buffer[WINSIZE];
float window[WINSIZE];
float acnf[FFTSIZE];
UnityComplexNumber spec[FFTSIZE];
};
union
{
Data data;
unsigned char pad[(sizeof(Data) + 15) & ~15]; // This entire structure must be a multiple of 16 bytes (and and instance 16 byte aligned) for PS3 SPU DMA requirements
};
};
int InternalRegisterEffectDefinition(UnityAudioEffectDefinition& definition)
{
int numparams = P_NUM;
definition.paramdefs = new UnityAudioParameterDefinition[numparams];
RegisterParameter(definition, "Index", "", 0.0f, MAXINDEX - 1, 0.0f, 1.0f, 1.0f, P_INDEX, "Determines where the pitch data is written to for access by scripts via PitchDetectorGetFreq");
RegisterParameter(definition, "Low Cut", "", 0.0f, (FFTSIZE / 2) - 1, 20, 1.0f, 1.0f, P_LOCUT, "Low frequency cut-off for input preprocessing");
RegisterParameter(definition, "High Cut", "", 0.0f, (FFTSIZE / 2) - 1, 1000, 1.0f, 1.0f, P_HICUT, "High frequency cut-off for input preprocessing");
RegisterParameter(definition, "Low Bin", "", 0.0f, (FFTSIZE / 2) - 1, 50, 1.0f, 1.0f, P_LOBIN, "Low detection bin in autocorrelation");
RegisterParameter(definition, "High Bin", "", 0.0f, (FFTSIZE / 2) - 1, 1500, 1.0f, 1.0f, P_HIBIN, "High detection bin in autocorrelation");
RegisterParameter(definition, "Threshold", "%", 0.0f, 1.0f, 0.05f, 1.0f, 1.0f, P_THR, "Input signal envelope threshold above which pitch detection is attempted");
RegisterParameter(definition, "Osc Pitch", "", -48.0f, 48.0f, 0.0f, 1.0f, 1.0f, P_OSCPITCH, "Relative oscillator pitch in semitones");
RegisterParameter(definition, "Monitor", "%", 0.0f, 1.0f, 0.5f, 100.0f, 1.0f, P_MONITOR, "Monitor mix for auditioning the pitch tracking");
return numparams;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK CreateCallback(UnityAudioEffectState* state)
{
EffectData* effectdata = new EffectData;
memset(effectdata, 0, sizeof(EffectData));
// Calculate window and autocorrelation normalization factor
EffectData::Data* data = &effectdata->data;
for (int i = 0; i < WINSIZE; i++)
{
float w = 0.5f - 0.5f * cosf(i * kPI / (float)WINSIZE);
data->window[i] = w;
data->spec[i].re = w;
}
// Window correction
FFT::Forward(data->spec, FFTSIZE, true);
for (int i = 0; i < FFTSIZE; i++)
data->acnf[i] = 1.0f / data->spec[i].Magnitude2();
state->effectdata = effectdata;
InitParametersFromDefinitions(InternalRegisterEffectDefinition, effectdata->data.p);
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ReleaseCallback(UnityAudioEffectState* state)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
delete data;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK SetFloatParameterCallback(UnityAudioEffectState* state, int index, float value)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
data->p[index] = value;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK GetFloatParameterCallback(UnityAudioEffectState* state, int index, float* value, char *valuestr)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
if (value != NULL)
*value = data->p[index];
if (valuestr != NULL)
valuestr[0] = 0;
return UNITY_AUDIODSP_OK;
}
int UNITY_AUDIODSP_CALLBACK GetFloatBufferCallback(UnityAudioEffectState* state, const char* name, float* buffer, int numsamples)
{
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ProcessCallback(UnityAudioEffectState* state, float* inbuffer, float* outbuffer, unsigned int length, int inchannels, int outchannels)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
const float sampletime = 1.0f / state->samplerate;
const float monitor = data->p[P_MONITOR];
const float oscpitch = powf(2.0f, data->p[P_OSCPITCH] / 12.0f) * sampletime;
memcpy(outbuffer, inbuffer, length * inchannels * sizeof(float));
for (unsigned int n = 0; n < length; n++)
{
float input = inbuffer[n * inchannels];
data->env += (fabsf(input) - data->env) * 0.01f;
data->buffer[data->counter + WINSIZE - HOPSIZE] = input;
if (++data->counter == HOPSIZE)
{
data->counter = 0;
for (int i = 0; i < WINSIZE - HOPSIZE; i++)
data->buffer[i] = data->buffer[i + HOPSIZE];
for (int i = 0; i < WINSIZE; i++)
{
data->spec[i].re = data->buffer[i] * data->window[i];
data->spec[i].im = 0.0f;
}
for (int i = WINSIZE; i < FFTSIZE; i++)
{
data->spec[i].re = 0.0f;
data->spec[i].im = 0.0f;
}
FFT::Forward(data->spec, FFTSIZE, true);
int locut = (int)data->p[P_LOCUT];
int hicut = (int)data->p[P_HICUT];
memset(data->spec, 0, sizeof(UnityComplexNumber) * locut);
memset(data->spec + hicut, 0, sizeof(UnityComplexNumber) * (FFTSIZE - 2 * hicut));
memset(data->spec + FFTSIZE - locut, 0, sizeof(UnityComplexNumber) * locut);
// Fast autocorrelation
for (int i = 0; i < FFTSIZE; i++)
{
data->spec[i].re = data->spec[i].Magnitude2() * data->acnf[n]; // Correct for windowing
data->spec[i].im = 0.0f;
debugdata[i] = data->spec[i].re;
}
FFT::Backward(data->spec, FFTSIZE, true);
int startbin = (int)data->p[P_LOBIN];
int endbin = (int)data->p[P_HIBIN];
int maxbin = 0;
float maxval = 0.0f;
for (int n = startbin; n < endbin; n++)
{
float a = data->spec[n].re;
if (a > maxval)
{
maxbin = n;
maxval = a;
}
}
if (data->env > data->p[P_THR] && maxbin > 0)
detected_freqs[(int)data->p[P_INDEX]] = (float)state->samplerate / (float)maxbin;
}
data->phase += detected_freqs[(int)data->p[P_INDEX]] * oscpitch;
data->phase -= FastFloor(data->phase);
for (int c = 0; c < outchannels; c++)
outbuffer[n * outchannels + c] += ((data->phase * 2.0f - 1.0f) * data->env - outbuffer[n * outchannels + c]) * monitor;
}
return UNITY_AUDIODSP_OK;
}
}
extern "C" UNITY_AUDIODSP_EXPORT_API float PitchDetectorGetFreq(int index)
{
if (index < 0 || index >= PitchDetector::MAXINDEX)
return 0.0f;
return PitchDetector::detected_freqs[index];
}
extern "C" UNITY_AUDIODSP_EXPORT_API int PitchDetectorDebug(float* data)
{
for (int i = 0; i < PitchDetector::FFTSIZE; i++)
data[i] = PitchDetector::debugdata[i];
return PitchDetector::FFTSIZE;
}