Mercurial > hg > qm-dsp
comparison dsp/keydetection/GetKeyMode.cpp @ 478:c92718cc6ef1
Untabify, indent, tidy
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 30 May 2019 18:40:16 +0100 |
parents | 64fc3009d0a3 |
children | af5b7ef02aa7 |
comparison
equal
deleted
inserted
replaced
477:fa407c1d9923 | 478:c92718cc6ef1 |
---|---|
153 double num = 0; | 153 double num = 0; |
154 double den = 0; | 154 double den = 0; |
155 double sum1 = 0; | 155 double sum1 = 0; |
156 double sum2 = 0; | 156 double sum2 = 0; |
157 | 157 |
158 for( unsigned int i = 0; i <length; i++ ) | 158 for( unsigned int i = 0; i <length; i++ ) { |
159 { | 159 |
160 int k = (i - shiftProfile + length) % length; | 160 int k = (i - shiftProfile + length) % length; |
161 | 161 |
162 num += pDataNorm[i] * pProfileNorm[k]; | 162 num += pDataNorm[i] * pProfileNorm[k]; |
163 | 163 |
164 sum1 += ( pDataNorm[i] * pDataNorm[i] ); | 164 sum1 += ( pDataNorm[i] * pDataNorm[i] ); |
165 sum2 += ( pProfileNorm[k] * pProfileNorm[k] ); | 165 sum2 += ( pProfileNorm[k] * pProfileNorm[k] ); |
166 } | 166 } |
167 | 167 |
168 den = sqrt(sum1 * sum2); | 168 den = sqrt(sum1 * sum2); |
169 | 169 |
170 if( den>0 ) { | 170 if( den>0 ) { |
171 retVal = num/den; | 171 retVal = num/den; |
172 } else { | 172 } else { |
179 int GetKeyMode::process(double *PCMData) | 179 int GetKeyMode::process(double *PCMData) |
180 { | 180 { |
181 int key; | 181 int key; |
182 unsigned int j,k; | 182 unsigned int j,k; |
183 | 183 |
184 ////////////////////////////////////////////// | |
185 m_Decimator->process( PCMData, m_DecimatedBuffer); | 184 m_Decimator->process( PCMData, m_DecimatedBuffer); |
186 | 185 |
187 m_ChrPointer = m_Chroma->process( m_DecimatedBuffer ); | 186 m_ChrPointer = m_Chroma->process( m_DecimatedBuffer ); |
188 | 187 |
189 /* | |
190 std::cout << "raw chroma: "; | |
191 for (int ii = 0; ii < kBinsPerOctave; ++ii) { | |
192 if (ii % (kBinsPerOctave/12) == 0) std::cout << "\n"; | |
193 std::cout << m_ChrPointer[ii] << " "; | |
194 } | |
195 std::cout << std::endl; | |
196 */ | |
197 // populate hpcp values; | 188 // populate hpcp values; |
198 int cbidx; | 189 int cbidx; |
199 for( j = 0; j < kBinsPerOctave; j++ ) { | 190 for( j = 0; j < kBinsPerOctave; j++ ) { |
200 cbidx = (m_bufferindex * kBinsPerOctave) + j; | 191 cbidx = (m_bufferindex * kBinsPerOctave) + j; |
201 m_ChromaBuffer[ cbidx ] = m_ChrPointer[j]; | 192 m_ChromaBuffer[ cbidx ] = m_ChrPointer[j]; |
221 m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling; | 212 m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling; |
222 } | 213 } |
223 | 214 |
224 // Normalize for zero average | 215 // Normalize for zero average |
225 double mHPCP = MathUtilities::mean( m_MeanHPCP, kBinsPerOctave ); | 216 double mHPCP = MathUtilities::mean( m_MeanHPCP, kBinsPerOctave ); |
226 for( k = 0; k < kBinsPerOctave; k++ ) | 217 for( k = 0; k < kBinsPerOctave; k++ ) { |
227 { | |
228 m_MeanHPCP[k] -= mHPCP; | 218 m_MeanHPCP[k] -= mHPCP; |
229 } | 219 } |
230 | 220 |
231 | 221 for( k = 0; k < kBinsPerOctave; k++ ) { |
232 for( k = 0; k < kBinsPerOctave; k++ ) | 222 // The Chromagram has the center of C at bin 0, while the major |
233 { | |
234 // The Cromagram has the center of C at bin 0, while the major | |
235 // and minor profiles have the center of C at 1. We want to have | 223 // and minor profiles have the center of C at 1. We want to have |
236 // the correlation for C result also at 1. | 224 // the correlation for C result also at 1. |
237 // To achieve this we have to shift two times: | 225 // To achieve this we have to shift two times: |
238 m_MajCorr[k] = krumCorr( m_MeanHPCP, m_MajProfileNorm, (int)k - 2, kBinsPerOctave ); | 226 m_MajCorr[k] = krumCorr( m_MeanHPCP, m_MajProfileNorm, (int)k - 2, kBinsPerOctave ); |
239 m_MinCorr[k] = krumCorr( m_MeanHPCP, m_MinProfileNorm, (int)k - 2, kBinsPerOctave ); | 227 m_MinCorr[k] = krumCorr( m_MeanHPCP, m_MinProfileNorm, (int)k - 2, kBinsPerOctave ); |
240 } | 228 } |
241 | |
242 /* | |
243 std::cout << "raw keys: "; | |
244 for (int ii = 0; ii < kBinsPerOctave; ++ii) { | |
245 if (ii % (kBinsPerOctave/12) == 0) std::cout << "\n"; | |
246 std::cout << m_MajCorr[ii] << " "; | |
247 } | |
248 for (int ii = 0; ii < kBinsPerOctave; ++ii) { | |
249 if (ii % (kBinsPerOctave/12) == 0) std::cout << "\n"; | |
250 std::cout << m_MinCorr[ii] << " "; | |
251 } | |
252 std::cout << std::endl; | |
253 */ | |
254 | 229 |
255 // m_MajCorr[1] is C center 1 / 3 + 1 = 1 | 230 // m_MajCorr[1] is C center 1 / 3 + 1 = 1 |
256 // m_MajCorr[4] is D center 4 / 3 + 1 = 2 | 231 // m_MajCorr[4] is D center 4 / 3 + 1 = 2 |
257 // '+ 1' because we number keys 1-24, not 0-23. | 232 // '+ 1' because we number keys 1-24, not 0-23. |
258 double maxMaj; | 233 double maxMaj; |
260 double maxMin; | 235 double maxMin; |
261 int maxMinBin = MathUtilities::getMax( m_MinCorr, kBinsPerOctave, &maxMin ); | 236 int maxMinBin = MathUtilities::getMax( m_MinCorr, kBinsPerOctave, &maxMin ); |
262 int maxBin = (maxMaj > maxMin) ? maxMajBin : (maxMinBin + kBinsPerOctave); | 237 int maxBin = (maxMaj > maxMin) ? maxMajBin : (maxMinBin + kBinsPerOctave); |
263 key = maxBin / 3 + 1; | 238 key = maxBin / 3 + 1; |
264 | 239 |
265 // std::cout << "fractional key pre-sorting: " << (maxBin + 2) / 3.0 << std::endl; | |
266 // std::cout << "key pre-sorting: " << key << std::endl; | |
267 | |
268 | |
269 //Median filtering | 240 //Median filtering |
270 | 241 |
271 // track Median buffer initial filling | 242 // track Median buffer initial filling |
272 if( m_MedianBufferFilling++ >= m_MedianWinsize) { | 243 if( m_MedianBufferFilling++ >= m_MedianWinsize) { |
273 m_MedianBufferFilling = m_MedianWinsize; | 244 m_MedianBufferFilling = m_MedianWinsize; |
289 ijx++; | 260 ijx++; |
290 } | 261 } |
291 | 262 |
292 qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), | 263 qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), |
293 MathUtilities::compareInt); | 264 MathUtilities::compareInt); |
294 /* | 265 |
295 std::cout << "sorted: "; | |
296 for (int ii = 0; ii < m_MedianBufferFilling; ++ii) { | |
297 std::cout << m_SortedBuffer[ii] << " "; | |
298 } | |
299 std::cout << std::endl; | |
300 */ | |
301 int sortlength = m_MedianBufferFilling; | 266 int sortlength = m_MedianBufferFilling; |
302 int midpoint = (int)ceil((double)sortlength/2); | 267 int midpoint = (int)ceil((double)sortlength/2); |
303 | 268 |
304 // std::cout << "midpoint = " << midpoint << endl; | |
305 | |
306 if( midpoint <= 0 ) { | 269 if( midpoint <= 0 ) { |
307 midpoint = 1; | 270 midpoint = 1; |
308 } | 271 } |
309 | 272 |
310 key = m_SortedBuffer[midpoint-1]; | 273 key = m_SortedBuffer[midpoint-1]; |
311 | |
312 // std::cout << "returning key = " << key << endl; | |
313 | 274 |
314 return key; | 275 return key; |
315 } | 276 } |
316 | 277 |
317 | 278 |
330 | 291 |
331 for (k = 0; k < 24; ++k) { | 292 for (k = 0; k < 24; ++k) { |
332 m_keyStrengths[k] = 0; | 293 m_keyStrengths[k] = 0; |
333 } | 294 } |
334 | 295 |
335 for( k = 0; k < kBinsPerOctave; k++ ) | 296 for( k = 0; k < kBinsPerOctave; k++ ) { |
336 { | |
337 int idx = k / (kBinsPerOctave/12); | 297 int idx = k / (kBinsPerOctave/12); |
338 int rem = k % (kBinsPerOctave/12); | 298 int rem = k % (kBinsPerOctave/12); |
339 if (rem == 0 || m_MajCorr[k] > m_keyStrengths[idx]) { | 299 if (rem == 0 || m_MajCorr[k] > m_keyStrengths[idx]) { |
340 m_keyStrengths[idx] = m_MajCorr[k]; | 300 m_keyStrengths[idx] = m_MajCorr[k]; |
341 } | 301 } |
342 } | 302 } |
343 | 303 |
344 for( k = 0; k < kBinsPerOctave; k++ ) | 304 for( k = 0; k < kBinsPerOctave; k++ ) { |
345 { | |
346 int idx = (k + kBinsPerOctave) / (kBinsPerOctave/12); | 305 int idx = (k + kBinsPerOctave) / (kBinsPerOctave/12); |
347 int rem = k % (kBinsPerOctave/12); | 306 int rem = k % (kBinsPerOctave/12); |
348 if (rem == 0 || m_MinCorr[k] > m_keyStrengths[idx]) { | 307 if (rem == 0 || m_MinCorr[k] > m_keyStrengths[idx]) { |
349 m_keyStrengths[idx] = m_MinCorr[k]; | 308 m_keyStrengths[idx] = m_MinCorr[k]; |
350 } | 309 } |
351 } | 310 } |
352 | 311 |
353 /* | |
354 std::cout << "key strengths: "; | |
355 for (int ii = 0; ii < 24; ++ii) { | |
356 if (ii % 6 == 0) std::cout << "\n"; | |
357 std::cout << m_keyStrengths[ii] << " "; | |
358 } | |
359 std::cout << std::endl; | |
360 */ | |
361 | |
362 return m_keyStrengths; | 312 return m_keyStrengths; |
363 } | 313 } |