File: PitchDetector.cpp

package info (click to toggle)
bespokesynth 1.3.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,716 kB
  • sloc: cpp: 117,136; ansic: 18,752; python: 593; xml: 74; makefile: 4
file content (270 lines) | stat: -rw-r--r-- 6,788 bytes parent folder | download | duplicates (2)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
    bespoke synth, a software modular synthesizer
    Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/
//
//  PitchDetector.cpp
//  modularSynth
//
//  Created by Ryan Challinor on 3/16/14.
//
//

#include "PitchDetector.h"
#include "FFT.h"
#include "SynthGlobals.h"

#define L2SC (float)3.32192809488736218171

PitchDetector::PitchDetector()
{
   mfs = gSampleRate;

   mcbsize = 2048;
   mcorrsize = mcbsize / 2 + 1;

   mpmax = 1 / (float)70; // max and min periods (ms)
   mpmin = 1 / (float)700; // eventually may want to bring these out as sliders

   mnmax = (unsigned long)(gSampleRate * mpmax);
   if (mnmax > mcorrsize)
   {
      mnmax = mcorrsize;
   }
   mnmin = (unsigned long)(gSampleRate * mpmin);

   mcbi = (float*)calloc(mcbsize, sizeof(float));

   mcbiwr = 0;

   // Generate a window with a single raised cosine from N/4 to 3N/4
   mcbwindow = (float*)calloc(mcbsize, sizeof(float));
   for (int ti = 0; ti < (mcbsize / 2); ti++)
   {
      mcbwindow[ti + mcbsize / 4] = -0.5 * cos(4 * PI * ti / (mcbsize - 1)) + 0.5;
   }

   mnoverlap = 4;

   mFFT = new ::FFT((int)mcbsize);

   mffttime = (float*)calloc(mcbsize, sizeof(float));
   mfftfreqre = (float*)calloc(mcorrsize, sizeof(float));
   mfftfreqim = (float*)calloc(mcorrsize, sizeof(float));


   // ---- Calculate autocorrelation of window ----
   macwinv = (float*)calloc(mcbsize, sizeof(float));
   for (int ti = 0; ti < mcbsize; ti++)
   {
      mffttime[ti] = mcbwindow[ti];
   }
   mFFT->Forward(mcbwindow, mfftfreqre, mfftfreqim);
   for (int ti = 0; ti < mcorrsize; ti++)
   {
      mfftfreqre[ti] = (mfftfreqre[ti]) * (mfftfreqre[ti]) + (mfftfreqim[ti]) * (mfftfreqim[ti]);
      mfftfreqim[ti] = 0;
   }
   mFFT->Inverse(mfftfreqre, mfftfreqim, mffttime);
   for (long ti = 1; ti < mcbsize; ti++)
   {
      macwinv[ti] = mffttime[ti] / mffttime[0];
      if (macwinv[ti] > 0.000001)
      {
         macwinv[ti] = (float)1 / macwinv[ti];
      }
      else
      {
         macwinv[ti] = 0;
      }
   }
   macwinv[0] = 1;
   // ---- END Calculate autocorrelation of window ----


   mlrshift = 0;
   mptarget = 0;
   msptarget = 0;

   mvthresh = 0.7; //  The voiced confidence (unbiased peak) threshold level
}

PitchDetector::~PitchDetector()
{
   delete mFFT;
   free(mcbi);
   free(mcbwindow);
   free(macwinv);
   free(mffttime);
   free(mfftfreqre);
   free(mfftfreqim);
}

float PitchDetector::DetectPitch(float* buffer, int bufferSize)
{
   long int N;
   long int Nf;
   long int fs;

   long int ti;
   long int ti2;
   long int ti3;
   long int ti4;
   float tf;
   float tf2;

   float pperiod;

   float* pfInput = buffer;

   maref = (float)mTune;

   N = mcbsize;
   Nf = mcorrsize;
   fs = mfs;

   float inpitch = 0;
   float conf = mconf;


   /*******************
    *  MAIN DSP LOOP  *
    *******************/
   for (int lSampleIndex = 0; lSampleIndex < bufferSize; lSampleIndex++)
   {
      // load data into circular buffer
      tf = (float)*(pfInput++);
      ti4 = mcbiwr;
      mcbi[ti4] = tf;

      // Input write pointer logic
      mcbiwr++;
      if (mcbiwr >= N)
      {
         mcbiwr = 0;
      }


      // ********************
      // * Low-rate section *
      // ********************

      // Every N/noverlap samples, run pitch estimation / manipulation code
      if ((mcbiwr) % (N / mnoverlap) == 0)
      {
         // ---- Obtain autocovariance ----

         // Window and fill FFT buffer
         ti2 = mcbiwr;
         for (ti = 0; ti < N; ti++)
         {
            mffttime[ti] = (float)(mcbi[(ti2 - ti + N) % N] * mcbwindow[ti]);
         }

         // Calculate FFT
         mFFT->Forward(mffttime, mfftfreqre, mfftfreqim);

         // Remove DC
         mfftfreqre[0] = 0;
         mfftfreqim[0] = 0;

         // Take magnitude squared
         for (ti = 1; ti < Nf; ti++)
         {
            mfftfreqre[ti] = (mfftfreqre[ti]) * (mfftfreqre[ti]) + (mfftfreqim[ti]) * (mfftfreqim[ti]);
            mfftfreqim[ti] = 0;
         }

         // Calculate IFFT
         mFFT->Inverse(mfftfreqre, mfftfreqim, mffttime);

         // Normalize
         tf = (float)1 / mffttime[0];
         for (ti = 1; ti < N; ti++)
         {
            mffttime[ti] = mffttime[ti] * tf;
         }
         mffttime[0] = 1;

         //  ---- END Obtain autocovariance ----


         //  ---- Calculate pitch and confidence ----

         // Calculate pitch period
         //   Pitch period is determined by the location of the max (biased)
         //     peak within a given range
         //   Confidence is determined by the corresponding unbiased height
         tf2 = 0;
         pperiod = mpmin;
         for (ti = mnmin; ti < mnmax; ti++)
         {
            ti2 = ti - 1;
            ti3 = ti + 1;
            if (ti2 < 0)
            {
               ti2 = 0;
            }
            if (ti3 > Nf)
            {
               ti3 = Nf;
            }
            tf = mffttime[ti];

            if (tf > mffttime[ti2] && tf >= mffttime[ti3] && tf > tf2)
            {
               tf2 = tf;
               ti4 = ti;
            }
         }
         if (tf2 > 0)
         {
            conf = tf2 * macwinv[ti4];
            if (ti4 > 0 && ti4 < Nf)
            {
               // Find the center of mass in the vicinity of the detected peak
               tf = mffttime[ti4 - 1] * (ti4 - 1);
               tf = tf + mffttime[ti4] * (ti4);
               tf = tf + mffttime[ti4 + 1] * (ti4 + 1);
               tf = tf / (mffttime[ti4 - 1] + mffttime[ti4] + mffttime[ti4 + 1]);
               pperiod = tf / fs;
            }
            else
            {
               pperiod = (float)ti4 / fs;
            }
         }

         // Convert to semitones
         tf = (float)-12 * log10((float)maref * pperiod) * L2SC;
         if (conf >= mvthresh)
         {
            inpitch = tf;
         }
         mconf = conf;

         mPitch = inpitch + 69;
         mConfidence = MIN(conf, 1);

         //  ---- END Calculate pitch and confidence ----
      }
   }

   // Tell the host the algorithm latency
   mLatency = (N - 1);

   return mPitch;
}