annotate dsp/keydetection/GetKeyMode.cpp @ 153:715f779d0b4f

Some docs; remove FiltFiltConfig as it's the same as FilterConfig
author Chris Cannam
date Mon, 21 Oct 2013 11:59:57 +0100
parents dcb555b90924
children 05378924b433
rev   line source
cannam@34 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@153 2 /*
Chris@153 3 Copyright (c) 2005 Centre for Digital Music ( C4DM )
Chris@153 4 Queen Mary Univesrity of London
cannam@34 5
Chris@153 6 This program is free software; you can redistribute it and/or
Chris@153 7 modify it under the terms of the GNU General Public License as
Chris@153 8 published by the Free Software Foundation; either version 2 of the
Chris@153 9 License, or (at your option) any later version. See the file
Chris@153 10 COPYING included with this distribution for more information.
Chris@153 11 */
cannam@7 12 // GetKeyMode.cpp: implementation of the CGetKeyMode class.
cannam@7 13 //
cannam@7 14 //////////////////////////////////////////////////////////////////////
cannam@7 15
cannam@7 16 #include "GetKeyMode.h"
cannam@16 17 #include "maths/MathUtilities.h"
cannam@9 18 #include "base/Pitch.h"
cannam@9 19
cannam@9 20 #include <iostream>
cannam@7 21
cannam@47 22 #include <cstring>
cannam@47 23 #include <cstdlib>
cannam@47 24
cannam@7 25 // Chords profile
cannam@7 26 static double MajProfile[36] =
cannam@7 27 { 0.0384, 0.0629, 0.0258, 0.0121, 0.0146, 0.0106, 0.0364, 0.0610, 0.0267,
chriss@15 28 0.0126, 0.0121, 0.0086, 0.0364, 0.0623, 0.0279, 0.0275, 0.0414, 0.0186,
cannam@7 29 0.0173, 0.0248, 0.0145, 0.0364, 0.0631, 0.0262, 0.0129, 0.0150, 0.0098,
cannam@7 30 0.0312, 0.0521, 0.0235, 0.0129, 0.0142, 0.0095, 0.0289, 0.0478, 0.0239};
cannam@7 31
cannam@7 32 static double MinProfile[36] =
cannam@7 33 { 0.0375, 0.0682, 0.0299, 0.0119, 0.0138, 0.0093, 0.0296, 0.0543, 0.0257,
cannam@7 34 0.0292, 0.0519, 0.0246, 0.0159, 0.0234, 0.0135, 0.0291, 0.0544, 0.0248,
cannam@7 35 0.0137, 0.0176, 0.0104, 0.0352, 0.0670, 0.0302, 0.0222, 0.0349, 0.0164,
cannam@7 36 0.0174, 0.0297, 0.0166, 0.0222, 0.0401, 0.0202, 0.0175, 0.0270, 0.0146};
cannam@7 37 //
cannam@40 38
cannam@7 39
cannam@7 40 //////////////////////////////////////////////////////////////////////
cannam@7 41 // Construction/Destruction
cannam@7 42 //////////////////////////////////////////////////////////////////////
cannam@7 43
cannam@9 44 GetKeyMode::GetKeyMode( int sampleRate, float tuningFrequency,
cannam@34 45 double hpcpAverage, double medianAverage ) :
cannam@34 46 m_hpcpAverage( hpcpAverage ),
cannam@34 47 m_medianAverage( medianAverage ),
cannam@34 48 m_ChrPointer(0),
cannam@34 49 m_DecimatedBuffer(0),
cannam@34 50 m_ChromaBuffer(0),
cannam@34 51 m_MeanHPCP(0),
cannam@34 52 m_MajCorr(0),
cannam@34 53 m_MinCorr(0),
cannam@34 54 m_Keys(0),
cannam@34 55 m_MedianFilterBuffer(0),
cannam@40 56 m_SortedBuffer(0),
cannam@40 57 m_keyStrengths(0)
cannam@7 58 {
cannam@34 59 m_DecimationFactor = 8;
cannam@34 60
cannam@34 61 // Chromagram configuration parameters
cannam@34 62 m_ChromaConfig.normalise = MathUtilities::NormaliseUnitMax;
cannam@34 63 m_ChromaConfig.FS = lrint(sampleRate/(double)m_DecimationFactor);
cannam@58 64 if (m_ChromaConfig.FS < 1) m_ChromaConfig.FS = 1;
cannam@7 65
cannam@34 66 // Set C (= MIDI #12) as our base :
cannam@34 67 // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc.
cannam@34 68 m_ChromaConfig.min = Pitch::getFrequencyForPitch
cannam@40 69 (48, 0, tuningFrequency);
cannam@34 70 m_ChromaConfig.max = Pitch::getFrequencyForPitch
cannam@34 71 (96, 0, tuningFrequency);
cannam@9 72
cannam@34 73 m_ChromaConfig.BPO = 36;
cannam@34 74 m_ChromaConfig.CQThresh = 0.0054;
cannam@9 75
cannam@34 76 // Chromagram inst.
cannam@34 77 m_Chroma = new Chromagram( m_ChromaConfig );
cannam@10 78
cannam@34 79 // Get calculated parameters from chroma object
cannam@34 80 m_ChromaFrameSize = m_Chroma->getFrameSize();
cannam@34 81 // override hopsize for this application
cannam@34 82 m_ChromaHopSize = m_ChromaFrameSize;
cannam@34 83 m_BPO = m_ChromaConfig.BPO;
cannam@10 84
cannam@40 85 // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl;
cannam@40 86
cannam@34 87 // Chromagram average and estimated key median filter lengths
cannam@34 88 m_ChromaBuffersize = (int)ceil( m_hpcpAverage * m_ChromaConfig.FS/m_ChromaFrameSize );
cannam@34 89 m_MedianWinsize = (int)ceil( m_medianAverage * m_ChromaConfig.FS/m_ChromaFrameSize );
cannam@34 90
cannam@34 91 // Reset counters
cannam@34 92 m_bufferindex = 0;
cannam@34 93 m_ChromaBufferFilling = 0;
cannam@34 94 m_MedianBufferFilling = 0;
cannam@9 95
cannam@34 96 // Spawn objectc/arrays
cannam@34 97 m_DecimatedBuffer = new double[m_ChromaFrameSize];
cannam@34 98
cannam@34 99 m_ChromaBuffer = new double[m_BPO * m_ChromaBuffersize];
cannam@34 100 memset( m_ChromaBuffer, 0, sizeof(double) * m_BPO * m_ChromaBuffersize);
cannam@34 101
cannam@34 102 m_MeanHPCP = new double[m_BPO];
cannam@34 103
cannam@34 104 m_MajCorr = new double[m_BPO];
cannam@34 105 m_MinCorr = new double[m_BPO];
cannam@34 106 m_Keys = new double[2*m_BPO];
cannam@34 107
cannam@34 108 m_MedianFilterBuffer = new int[ m_MedianWinsize ];
cannam@34 109 memset( m_MedianFilterBuffer, 0, sizeof(int)*m_MedianWinsize);
cannam@34 110
cannam@34 111 m_SortedBuffer = new int[ m_MedianWinsize ];
cannam@34 112 memset( m_SortedBuffer, 0, sizeof(int)*m_MedianWinsize);
cannam@34 113
cannam@34 114 m_Decimator = new Decimator
cannam@34 115 ( m_ChromaFrameSize*m_DecimationFactor, m_DecimationFactor );
cannam@40 116
cannam@40 117 m_keyStrengths = new double[24];
cannam@7 118 }
cannam@7 119
cannam@7 120 GetKeyMode::~GetKeyMode()
cannam@7 121 {
cannam@7 122
cannam@34 123 delete m_Chroma;
cannam@34 124 delete m_Decimator;
cannam@34 125
cannam@34 126 delete [] m_DecimatedBuffer;
cannam@34 127 delete [] m_ChromaBuffer;
cannam@34 128 delete [] m_MeanHPCP;
cannam@34 129 delete [] m_MajCorr;
cannam@34 130 delete [] m_MinCorr;
cannam@34 131 delete [] m_Keys;
cannam@34 132 delete [] m_MedianFilterBuffer;
cannam@34 133 delete [] m_SortedBuffer;
cannam@40 134
cannam@40 135 delete[] m_keyStrengths;
cannam@7 136 }
cannam@7 137
cannam@7 138 double GetKeyMode::krumCorr(double *pData1, double *pData2, unsigned int length)
cannam@7 139 {
cannam@34 140 double retVal= 0.0;
cannam@34 141
cannam@34 142 double num = 0;
cannam@34 143 double den = 0;
cannam@34 144 double mX = MathUtilities::mean( pData1, length );
cannam@34 145 double mY = MathUtilities::mean( pData2, length );
cannam@34 146
cannam@34 147 double sum1 = 0;
cannam@34 148 double sum2 = 0;
cannam@34 149
cannam@34 150 for( unsigned int i = 0; i <length; i++ )
cannam@34 151 {
cannam@34 152 num += ( pData1[i] - mX ) * ( pData2[i] - mY );
cannam@7 153
cannam@34 154 sum1 += ( (pData1[i]-mX) * (pData1[i]-mX) );
cannam@34 155 sum2 += ( (pData2[i]-mY) * (pData2[i]-mY) );
cannam@34 156 }
cannam@34 157
cannam@34 158 den = sqrt(sum1 * sum2);
cannam@34 159
cannam@34 160 if( den>0 )
cannam@34 161 retVal = num/den;
cannam@34 162 else
cannam@34 163 retVal = 0;
cannam@7 164
cannam@7 165
cannam@34 166 return retVal;
cannam@7 167 }
cannam@7 168
cannam@7 169 int GetKeyMode::process(double *PCMData)
cannam@7 170 {
cannam@34 171 int key;
cannam@7 172
cannam@34 173 unsigned int j,k;
cannam@7 174
cannam@34 175 //////////////////////////////////////////////
cannam@34 176 m_Decimator->process( PCMData, m_DecimatedBuffer);
cannam@7 177
cannam@34 178 m_ChrPointer = m_Chroma->process( m_DecimatedBuffer );
cannam@7 179
chriss@15 180
cannam@34 181 // Move bins such that the centre of the base note is in the
cannam@34 182 // middle of its three bins :
cannam@34 183 // Added 21.11.07 by Chris Sutton based on debugging with Katy
cannam@34 184 // Noland + comparison with Matlab equivalent.
cannam@34 185 MathUtilities::circShift( m_ChrPointer, m_BPO, 1);
cannam@23 186 /*
cannam@73 187 std::cout << "raw chroma: ";
cannam@73 188 for (int ii = 0; ii < m_BPO; ++ii) {
cannam@73 189 if (ii % (m_BPO/12) == 0) std::cout << "\n";
cannam@73 190 std::cout << m_ChrPointer[ii] << " ";
cannam@73 191 }
cannam@73 192 std::cout << std::endl;
cannam@23 193 */
cannam@34 194 // populate hpcp values;
cannam@34 195 int cbidx;
cannam@34 196 for( j = 0; j < m_BPO; j++ )
cannam@34 197 {
cannam@34 198 cbidx = (m_bufferindex * m_BPO) + j;
cannam@34 199 m_ChromaBuffer[ cbidx ] = m_ChrPointer[j];
cannam@34 200 }
cannam@7 201
cannam@34 202 //keep track of input buffers;
cannam@34 203 if( m_bufferindex++ >= m_ChromaBuffersize - 1)
cannam@34 204 m_bufferindex = 0;
cannam@7 205
cannam@34 206 // track filling of chroma matrix
cannam@34 207 if( m_ChromaBufferFilling++ >= m_ChromaBuffersize)
cannam@34 208 m_ChromaBufferFilling = m_ChromaBuffersize;
cannam@7 209
cannam@34 210 //calculate mean
cannam@34 211 for( k = 0; k < m_BPO; k++ )
cannam@34 212 {
cannam@34 213 double mnVal = 0.0;
cannam@34 214 for( j = 0; j < m_ChromaBufferFilling; j++ )
cannam@34 215 {
cannam@34 216 mnVal += m_ChromaBuffer[ k + (j*m_BPO) ];
cannam@34 217 }
cannam@7 218
cannam@34 219 m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling;
cannam@34 220 }
cannam@7 221
cannam@7 222
cannam@34 223 for( k = 0; k < m_BPO; k++ )
cannam@34 224 {
cannam@34 225 m_MajCorr[k] = krumCorr( m_MeanHPCP, MajProfile, m_BPO );
cannam@34 226 m_MinCorr[k] = krumCorr( m_MeanHPCP, MinProfile, m_BPO );
cannam@7 227
cannam@34 228 MathUtilities::circShift( MajProfile, m_BPO, 1 );
cannam@34 229 MathUtilities::circShift( MinProfile, m_BPO, 1 );
cannam@34 230 }
chriss@15 231
cannam@34 232 for( k = 0; k < m_BPO; k++ )
cannam@34 233 {
cannam@34 234 m_Keys[k] = m_MajCorr[k];
cannam@34 235 m_Keys[k+m_BPO] = m_MinCorr[k];
cannam@34 236 }
chriss@15 237
cannam@40 238 for (k = 0; k < 24; ++k) {
cannam@40 239 m_keyStrengths[k] = 0;
cannam@40 240 }
cannam@40 241
cannam@40 242 for( k = 0; k < m_BPO*2; k++ )
cannam@40 243 {
cannam@73 244 int idx = k / (m_BPO/12);
cannam@73 245 int rem = k % (m_BPO/12);
cannam@73 246 if (rem == 0 || m_Keys[k] > m_keyStrengths[idx]) {
cannam@73 247 m_keyStrengths[idx] = m_Keys[k];
cannam@73 248 }
cannam@73 249
cannam@73 250 // m_keyStrengths[k/(m_BPO/12)] += m_Keys[k];
cannam@40 251 }
chriss@15 252
cannam@23 253 /*
cannam@34 254 std::cout << "raw keys: ";
cannam@34 255 for (int ii = 0; ii < 2*m_BPO; ++ii) {
cannam@73 256 if (ii % (m_BPO/12) == 0) std::cout << "\n";
cannam@73 257 std::cout << m_Keys[ii] << " ";
cannam@73 258 }
cannam@73 259 std::cout << std::endl;
cannam@73 260
cannam@73 261 std::cout << "key strengths: ";
cannam@73 262 for (int ii = 0; ii < 24; ++ii) {
cannam@73 263 if (ii % 6 == 0) std::cout << "\n";
cannam@73 264 std::cout << m_keyStrengths[ii] << " ";
cannam@34 265 }
cannam@34 266 std::cout << std::endl;
cannam@23 267 */
cannam@34 268 double dummy;
cannam@34 269 // '1 +' because we number keys 1-24, not 0-23.
cannam@34 270 key = 1 + (int)ceil( (double)MathUtilities::getMax( m_Keys, 2* m_BPO, &dummy )/3 );
cannam@9 271
cannam@73 272 // std::cout << "key pre-sorting: " << key << std::endl;
cannam@7 273
cannam@7 274
cannam@34 275 //Median filtering
cannam@7 276
cannam@34 277 // track Median buffer initial filling
cannam@34 278 if( m_MedianBufferFilling++ >= m_MedianWinsize)
cannam@34 279 m_MedianBufferFilling = m_MedianWinsize;
cannam@7 280
cannam@34 281 //shift median buffer
cannam@34 282 for( k = 1; k < m_MedianWinsize; k++ )
cannam@34 283 {
cannam@34 284 m_MedianFilterBuffer[ k - 1 ] = m_MedianFilterBuffer[ k ];
cannam@34 285 }
cannam@7 286
cannam@34 287 //write new key value into median buffer
cannam@34 288 m_MedianFilterBuffer[ m_MedianWinsize - 1 ] = key;
cannam@7 289
cannam@7 290
cannam@34 291 //Copy median into sorting buffer, reversed
cannam@34 292 unsigned int ijx = 0;
cannam@34 293 for( k = 0; k < m_MedianWinsize; k++ )
cannam@34 294 {
cannam@34 295 m_SortedBuffer[k] = m_MedianFilterBuffer[m_MedianWinsize-1-ijx];
cannam@34 296 ijx++;
cannam@34 297 }
cannam@7 298
cannam@34 299 qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int),
cannam@34 300 MathUtilities::compareInt);
cannam@34 301 /*
cannam@34 302 std::cout << "sorted: ";
cannam@34 303 for (int ii = 0; ii < m_MedianBufferFilling; ++ii) {
cannam@34 304 std::cout << m_SortedBuffer[ii] << " ";
cannam@34 305 }
cannam@34 306 std::cout << std::endl;
cannam@34 307 */
cannam@34 308 int sortlength = m_MedianBufferFilling;
cannam@34 309 int midpoint = (int)ceil((double)sortlength/2);
cannam@7 310
cannam@73 311 // std::cout << "midpoint = " << midpoint << endl;
cannam@73 312
cannam@34 313 if( midpoint <= 0 )
cannam@34 314 midpoint = 1;
cannam@7 315
cannam@34 316 key = m_SortedBuffer[midpoint-1];
cannam@7 317
cannam@73 318 // std::cout << "returning key = " << key << endl;
cannam@73 319
cannam@34 320 return key;
cannam@7 321 }
cannam@7 322
cannam@7 323
cannam@43 324 bool GetKeyMode::isModeMinor( int key )
cannam@7 325 {
cannam@34 326 return (key > 12);
cannam@7 327 }