qm-dsp
1.8
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 /* 00003 Copyright (c) 2005 Centre for Digital Music ( C4DM ) 00004 Queen Mary Univesrity of London 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License as 00008 published by the Free Software Foundation; either version 2 of the 00009 License, or (at your option) any later version. See the file 00010 COPYING included with this distribution for more information. 00011 */ 00012 // GetKeyMode.cpp: implementation of the CGetKeyMode class. 00013 // 00015 00016 #include "GetKeyMode.h" 00017 #include "maths/MathUtilities.h" 00018 #include "base/Pitch.h" 00019 00020 #include <iostream> 00021 00022 #include <cstring> 00023 #include <cstdlib> 00024 00025 // Chords profile 00026 static double MajProfile[36] = 00027 { 0.0384, 0.0629, 0.0258, 0.0121, 0.0146, 0.0106, 0.0364, 0.0610, 0.0267, 00028 0.0126, 0.0121, 0.0086, 0.0364, 0.0623, 0.0279, 0.0275, 0.0414, 0.0186, 00029 0.0173, 0.0248, 0.0145, 0.0364, 0.0631, 0.0262, 0.0129, 0.0150, 0.0098, 00030 0.0312, 0.0521, 0.0235, 0.0129, 0.0142, 0.0095, 0.0289, 0.0478, 0.0239}; 00031 00032 static double MinProfile[36] = 00033 { 0.0375, 0.0682, 0.0299, 0.0119, 0.0138, 0.0093, 0.0296, 0.0543, 0.0257, 00034 0.0292, 0.0519, 0.0246, 0.0159, 0.0234, 0.0135, 0.0291, 0.0544, 0.0248, 00035 0.0137, 0.0176, 0.0104, 0.0352, 0.0670, 0.0302, 0.0222, 0.0349, 0.0164, 00036 0.0174, 0.0297, 0.0166, 0.0222, 0.0401, 0.0202, 0.0175, 0.0270, 0.0146}; 00037 // 00038 00039 00041 // Construction/Destruction 00043 00044 GetKeyMode::GetKeyMode( int sampleRate, float tuningFrequency, 00045 double hpcpAverage, double medianAverage ) : 00046 m_hpcpAverage( hpcpAverage ), 00047 m_medianAverage( medianAverage ), 00048 m_ChrPointer(0), 00049 m_DecimatedBuffer(0), 00050 m_ChromaBuffer(0), 00051 m_MeanHPCP(0), 00052 m_MajCorr(0), 00053 m_MinCorr(0), 00054 m_Keys(0), 00055 m_MedianFilterBuffer(0), 00056 m_SortedBuffer(0), 00057 m_keyStrengths(0) 00058 { 00059 m_DecimationFactor = 8; 00060 00061 // Chromagram configuration parameters 00062 m_ChromaConfig.normalise = MathUtilities::NormaliseUnitMax; 00063 m_ChromaConfig.FS = lrint(sampleRate/(double)m_DecimationFactor); 00064 if (m_ChromaConfig.FS < 1) m_ChromaConfig.FS = 1; 00065 00066 // Set C (= MIDI #12) as our base : 00067 // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc. 00068 m_ChromaConfig.min = Pitch::getFrequencyForPitch 00069 (48, 0, tuningFrequency); 00070 m_ChromaConfig.max = Pitch::getFrequencyForPitch 00071 (96, 0, tuningFrequency); 00072 00073 m_ChromaConfig.BPO = 36; 00074 m_ChromaConfig.CQThresh = 0.0054; 00075 00076 // Chromagram inst. 00077 m_Chroma = new Chromagram( m_ChromaConfig ); 00078 00079 // Get calculated parameters from chroma object 00080 m_ChromaFrameSize = m_Chroma->getFrameSize(); 00081 // override hopsize for this application 00082 m_ChromaHopSize = m_ChromaFrameSize; 00083 m_BPO = m_ChromaConfig.BPO; 00084 00085 // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl; 00086 00087 // Chromagram average and estimated key median filter lengths 00088 m_ChromaBuffersize = (int)ceil( m_hpcpAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); 00089 m_MedianWinsize = (int)ceil( m_medianAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); 00090 00091 // Reset counters 00092 m_bufferindex = 0; 00093 m_ChromaBufferFilling = 0; 00094 m_MedianBufferFilling = 0; 00095 00096 // Spawn objectc/arrays 00097 m_DecimatedBuffer = new double[m_ChromaFrameSize]; 00098 00099 m_ChromaBuffer = new double[m_BPO * m_ChromaBuffersize]; 00100 memset( m_ChromaBuffer, 0, sizeof(double) * m_BPO * m_ChromaBuffersize); 00101 00102 m_MeanHPCP = new double[m_BPO]; 00103 00104 m_MajCorr = new double[m_BPO]; 00105 m_MinCorr = new double[m_BPO]; 00106 m_Keys = new double[2*m_BPO]; 00107 00108 m_MedianFilterBuffer = new int[ m_MedianWinsize ]; 00109 memset( m_MedianFilterBuffer, 0, sizeof(int)*m_MedianWinsize); 00110 00111 m_SortedBuffer = new int[ m_MedianWinsize ]; 00112 memset( m_SortedBuffer, 0, sizeof(int)*m_MedianWinsize); 00113 00114 m_Decimator = new Decimator 00115 ( m_ChromaFrameSize*m_DecimationFactor, m_DecimationFactor ); 00116 00117 m_keyStrengths = new double[24]; 00118 } 00119 00120 GetKeyMode::~GetKeyMode() 00121 { 00122 00123 delete m_Chroma; 00124 delete m_Decimator; 00125 00126 delete [] m_DecimatedBuffer; 00127 delete [] m_ChromaBuffer; 00128 delete [] m_MeanHPCP; 00129 delete [] m_MajCorr; 00130 delete [] m_MinCorr; 00131 delete [] m_Keys; 00132 delete [] m_MedianFilterBuffer; 00133 delete [] m_SortedBuffer; 00134 00135 delete[] m_keyStrengths; 00136 } 00137 00138 double GetKeyMode::krumCorr(double *pData1, double *pData2, unsigned int length) 00139 { 00140 double retVal= 0.0; 00141 00142 double num = 0; 00143 double den = 0; 00144 double mX = MathUtilities::mean( pData1, length ); 00145 double mY = MathUtilities::mean( pData2, length ); 00146 00147 double sum1 = 0; 00148 double sum2 = 0; 00149 00150 for( unsigned int i = 0; i <length; i++ ) 00151 { 00152 num += ( pData1[i] - mX ) * ( pData2[i] - mY ); 00153 00154 sum1 += ( (pData1[i]-mX) * (pData1[i]-mX) ); 00155 sum2 += ( (pData2[i]-mY) * (pData2[i]-mY) ); 00156 } 00157 00158 den = sqrt(sum1 * sum2); 00159 00160 if( den>0 ) 00161 retVal = num/den; 00162 else 00163 retVal = 0; 00164 00165 00166 return retVal; 00167 } 00168 00169 int GetKeyMode::process(double *PCMData) 00170 { 00171 int key; 00172 00173 unsigned int j,k; 00174 00176 m_Decimator->process( PCMData, m_DecimatedBuffer); 00177 00178 m_ChrPointer = m_Chroma->process( m_DecimatedBuffer ); 00179 00180 00181 // Move bins such that the centre of the base note is in the 00182 // middle of its three bins : 00183 // Added 21.11.07 by Chris Sutton based on debugging with Katy 00184 // Noland + comparison with Matlab equivalent. 00185 MathUtilities::circShift( m_ChrPointer, m_BPO, 1); 00186 /* 00187 std::cout << "raw chroma: "; 00188 for (int ii = 0; ii < m_BPO; ++ii) { 00189 if (ii % (m_BPO/12) == 0) std::cout << "\n"; 00190 std::cout << m_ChrPointer[ii] << " "; 00191 } 00192 std::cout << std::endl; 00193 */ 00194 // populate hpcp values; 00195 int cbidx; 00196 for( j = 0; j < m_BPO; j++ ) 00197 { 00198 cbidx = (m_bufferindex * m_BPO) + j; 00199 m_ChromaBuffer[ cbidx ] = m_ChrPointer[j]; 00200 } 00201 00202 //keep track of input buffers; 00203 if( m_bufferindex++ >= m_ChromaBuffersize - 1) 00204 m_bufferindex = 0; 00205 00206 // track filling of chroma matrix 00207 if( m_ChromaBufferFilling++ >= m_ChromaBuffersize) 00208 m_ChromaBufferFilling = m_ChromaBuffersize; 00209 00210 //calculate mean 00211 for( k = 0; k < m_BPO; k++ ) 00212 { 00213 double mnVal = 0.0; 00214 for( j = 0; j < m_ChromaBufferFilling; j++ ) 00215 { 00216 mnVal += m_ChromaBuffer[ k + (j*m_BPO) ]; 00217 } 00218 00219 m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling; 00220 } 00221 00222 00223 for( k = 0; k < m_BPO; k++ ) 00224 { 00225 m_MajCorr[k] = krumCorr( m_MeanHPCP, MajProfile, m_BPO ); 00226 m_MinCorr[k] = krumCorr( m_MeanHPCP, MinProfile, m_BPO ); 00227 00228 MathUtilities::circShift( MajProfile, m_BPO, 1 ); 00229 MathUtilities::circShift( MinProfile, m_BPO, 1 ); 00230 } 00231 00232 for( k = 0; k < m_BPO; k++ ) 00233 { 00234 m_Keys[k] = m_MajCorr[k]; 00235 m_Keys[k+m_BPO] = m_MinCorr[k]; 00236 } 00237 00238 for (k = 0; k < 24; ++k) { 00239 m_keyStrengths[k] = 0; 00240 } 00241 00242 for( k = 0; k < m_BPO*2; k++ ) 00243 { 00244 int idx = k / (m_BPO/12); 00245 int rem = k % (m_BPO/12); 00246 if (rem == 0 || m_Keys[k] > m_keyStrengths[idx]) { 00247 m_keyStrengths[idx] = m_Keys[k]; 00248 } 00249 00250 // m_keyStrengths[k/(m_BPO/12)] += m_Keys[k]; 00251 } 00252 00253 /* 00254 std::cout << "raw keys: "; 00255 for (int ii = 0; ii < 2*m_BPO; ++ii) { 00256 if (ii % (m_BPO/12) == 0) std::cout << "\n"; 00257 std::cout << m_Keys[ii] << " "; 00258 } 00259 std::cout << std::endl; 00260 00261 std::cout << "key strengths: "; 00262 for (int ii = 0; ii < 24; ++ii) { 00263 if (ii % 6 == 0) std::cout << "\n"; 00264 std::cout << m_keyStrengths[ii] << " "; 00265 } 00266 std::cout << std::endl; 00267 */ 00268 double dummy; 00269 // '1 +' because we number keys 1-24, not 0-23. 00270 key = 1 + (int)ceil( (double)MathUtilities::getMax( m_Keys, 2* m_BPO, &dummy )/3 ); 00271 00272 // std::cout << "key pre-sorting: " << key << std::endl; 00273 00274 00275 //Median filtering 00276 00277 // track Median buffer initial filling 00278 if( m_MedianBufferFilling++ >= m_MedianWinsize) 00279 m_MedianBufferFilling = m_MedianWinsize; 00280 00281 //shift median buffer 00282 for( k = 1; k < m_MedianWinsize; k++ ) 00283 { 00284 m_MedianFilterBuffer[ k - 1 ] = m_MedianFilterBuffer[ k ]; 00285 } 00286 00287 //write new key value into median buffer 00288 m_MedianFilterBuffer[ m_MedianWinsize - 1 ] = key; 00289 00290 00291 //Copy median into sorting buffer, reversed 00292 unsigned int ijx = 0; 00293 for( k = 0; k < m_MedianWinsize; k++ ) 00294 { 00295 m_SortedBuffer[k] = m_MedianFilterBuffer[m_MedianWinsize-1-ijx]; 00296 ijx++; 00297 } 00298 00299 qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), 00300 MathUtilities::compareInt); 00301 /* 00302 std::cout << "sorted: "; 00303 for (int ii = 0; ii < m_MedianBufferFilling; ++ii) { 00304 std::cout << m_SortedBuffer[ii] << " "; 00305 } 00306 std::cout << std::endl; 00307 */ 00308 int sortlength = m_MedianBufferFilling; 00309 int midpoint = (int)ceil((double)sortlength/2); 00310 00311 // std::cout << "midpoint = " << midpoint << endl; 00312 00313 if( midpoint <= 0 ) 00314 midpoint = 1; 00315 00316 key = m_SortedBuffer[midpoint-1]; 00317 00318 // std::cout << "returning key = " << key << endl; 00319 00320 return key; 00321 } 00322 00323 00324 bool GetKeyMode::isModeMinor( int key ) 00325 { 00326 return (key > 12); 00327 }