cannam@475: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@475: /* cannam@475: QM DSP Library cannam@475: cannam@475: Centre for Digital Music, Queen Mary, University of London. cannam@475: This file 2005-2006 Christian Landone and Katy Noland. cannam@475: cannam@475: Fixes to correct chroma offsets and for thread safety contributed cannam@475: by Daniel Schürmann. cannam@475: cannam@475: This program is free software; you can redistribute it and/or cannam@475: modify it under the terms of the GNU General Public License as cannam@475: published by the Free Software Foundation; either version 2 of the cannam@475: License, or (at your option) any later version. See the file cannam@475: COPYING included with this distribution for more information. cannam@475: */ cannam@475: cannam@475: #include "GetKeyMode.h" cannam@475: #include "maths/MathUtilities.h" cannam@475: #include "base/Pitch.h" cannam@475: cannam@475: #include cannam@475: cannam@475: #include cannam@475: #include cannam@475: cannam@475: static const int kBinsPerOctave = 36; cannam@475: cannam@475: // Chords profile cannam@475: static double MajProfile[kBinsPerOctave] = { cannam@475: 0.0384, 0.0629, 0.0258, 0.0121, 0.0146, 0.0106, 0.0364, 0.0610, 0.0267, cannam@475: 0.0126, 0.0121, 0.0086, 0.0364, 0.0623, 0.0279, 0.0275, 0.0414, 0.0186, cannam@475: 0.0173, 0.0248, 0.0145, 0.0364, 0.0631, 0.0262, 0.0129, 0.0150, 0.0098, cannam@475: 0.0312, 0.0521, 0.0235, 0.0129, 0.0142, 0.0095, 0.0289, 0.0478, 0.0239}; cannam@475: cannam@475: static double MinProfile[kBinsPerOctave] = { cannam@475: 0.0375, 0.0682, 0.0299, 0.0119, 0.0138, 0.0093, 0.0296, 0.0543, 0.0257, cannam@475: 0.0292, 0.0519, 0.0246, 0.0159, 0.0234, 0.0135, 0.0291, 0.0544, 0.0248, cannam@475: 0.0137, 0.0176, 0.0104, 0.0352, 0.0670, 0.0302, 0.0222, 0.0349, 0.0164, cannam@475: 0.0174, 0.0297, 0.0166, 0.0222, 0.0401, 0.0202, 0.0175, 0.0270, 0.0146}; cannam@475: // cannam@475: cannam@475: cannam@475: ////////////////////////////////////////////////////////////////////// cannam@475: // Construction/Destruction cannam@475: ////////////////////////////////////////////////////////////////////// cannam@475: cannam@475: GetKeyMode::GetKeyMode( int sampleRate, float tuningFrequency, cannam@475: double hpcpAverage, double medianAverage ) : cannam@475: m_hpcpAverage( hpcpAverage ), cannam@475: m_medianAverage( medianAverage ), cannam@475: m_ChrPointer(0), cannam@475: m_DecimatedBuffer(0), cannam@475: m_ChromaBuffer(0), cannam@475: m_MeanHPCP(0), cannam@475: m_MajCorr(0), cannam@475: m_MinCorr(0), cannam@475: m_MedianFilterBuffer(0), cannam@475: m_SortedBuffer(0), cannam@475: m_keyStrengths(0) cannam@475: { cannam@475: m_DecimationFactor = 8; cannam@475: cannam@475: // Chromagram configuration parameters cannam@475: m_ChromaConfig.normalise = MathUtilities::NormaliseUnitMax; cannam@475: m_ChromaConfig.FS = sampleRate/(double)m_DecimationFactor; cannam@475: if (m_ChromaConfig.FS < 1) { cannam@475: m_ChromaConfig.FS = 1; cannam@475: } cannam@475: cannam@475: // Set C3 (= MIDI #48) as our base: cannam@475: // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc. cannam@475: m_ChromaConfig.min = Pitch::getFrequencyForPitch( 48, 0, tuningFrequency ); cannam@475: m_ChromaConfig.max = Pitch::getFrequencyForPitch( 96, 0, tuningFrequency ); cannam@475: cannam@475: m_ChromaConfig.BPO = kBinsPerOctave; cannam@475: m_ChromaConfig.CQThresh = 0.0054; cannam@475: cannam@475: // Chromagram inst. cannam@475: m_Chroma = new Chromagram( m_ChromaConfig ); cannam@475: cannam@475: // Get calculated parameters from chroma object cannam@475: m_ChromaFrameSize = m_Chroma->getFrameSize(); cannam@475: // override hopsize for this application cannam@475: m_ChromaHopSize = m_ChromaFrameSize; cannam@475: cannam@475: // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl; cannam@475: cannam@475: // Chromagram average and estimated key median filter lengths cannam@475: m_ChromaBuffersize = (int)ceil( m_hpcpAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); cannam@475: m_MedianWinsize = (int)ceil( m_medianAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); cannam@475: cannam@475: // Reset counters cannam@475: m_bufferindex = 0; cannam@475: m_ChromaBufferFilling = 0; cannam@475: m_MedianBufferFilling = 0; cannam@475: cannam@475: // Spawn objectc/arrays cannam@475: m_DecimatedBuffer = new double[m_ChromaFrameSize]; cannam@475: cannam@475: m_ChromaBuffer = new double[kBinsPerOctave * m_ChromaBuffersize]; cannam@475: memset( m_ChromaBuffer, 0, sizeof(double) * kBinsPerOctave * m_ChromaBuffersize); cannam@475: cannam@475: m_MeanHPCP = new double[kBinsPerOctave]; cannam@475: cannam@475: m_MajCorr = new double[kBinsPerOctave]; cannam@475: m_MinCorr = new double[kBinsPerOctave]; cannam@475: cannam@475: m_MajProfileNorm = new double[kBinsPerOctave]; cannam@475: m_MinProfileNorm = new double[kBinsPerOctave]; cannam@475: cannam@475: double mMaj = MathUtilities::mean( MajProfile, kBinsPerOctave ); cannam@475: double mMin = MathUtilities::mean( MinProfile, kBinsPerOctave ); cannam@475: cannam@475: for( unsigned int i = 0; i < kBinsPerOctave; i++ ) { cannam@475: m_MajProfileNorm[i] = MajProfile[i] - mMaj; cannam@475: m_MinProfileNorm[i] = MinProfile[i] - mMin; cannam@475: } cannam@475: cannam@475: m_MedianFilterBuffer = new int[ m_MedianWinsize ]; cannam@475: memset( m_MedianFilterBuffer, 0, sizeof(int)*m_MedianWinsize); cannam@475: cannam@475: m_SortedBuffer = new int[ m_MedianWinsize ]; cannam@475: memset( m_SortedBuffer, 0, sizeof(int)*m_MedianWinsize); cannam@475: cannam@475: m_Decimator = new Decimator( m_ChromaFrameSize*m_DecimationFactor, m_DecimationFactor ); cannam@475: cannam@475: m_keyStrengths = new double[24]; cannam@475: } cannam@475: cannam@475: GetKeyMode::~GetKeyMode() cannam@475: { cannam@475: delete m_Chroma; cannam@475: delete m_Decimator; cannam@475: cannam@475: delete [] m_DecimatedBuffer; cannam@475: delete [] m_ChromaBuffer; cannam@475: delete [] m_MeanHPCP; cannam@475: delete [] m_MajCorr; cannam@475: delete [] m_MinCorr; cannam@475: delete [] m_MajProfileNorm; cannam@475: delete [] m_MinProfileNorm; cannam@475: delete [] m_MedianFilterBuffer; cannam@475: delete [] m_SortedBuffer; cannam@475: delete [] m_keyStrengths; cannam@475: } cannam@475: cannam@475: double GetKeyMode::krumCorr( const double *pDataNorm, const double *pProfileNorm, cannam@475: int shiftProfile, unsigned int length) cannam@475: { cannam@475: double retVal= 0.0; cannam@475: cannam@475: double num = 0; cannam@475: double den = 0; cannam@475: double sum1 = 0; cannam@475: double sum2 = 0; cannam@475: cannam@478: for( unsigned int i = 0; i 0 ) { cannam@475: retVal = num/den; cannam@475: } else { cannam@475: retVal = 0; cannam@475: } cannam@475: cannam@475: return retVal; cannam@475: } cannam@475: cannam@475: int GetKeyMode::process(double *PCMData) cannam@475: { cannam@475: int key; cannam@475: unsigned int j,k; cannam@475: cannam@475: m_Decimator->process( PCMData, m_DecimatedBuffer); cannam@475: cannam@475: m_ChrPointer = m_Chroma->process( m_DecimatedBuffer ); cannam@475: cannam@475: // populate hpcp values; cannam@475: int cbidx; cannam@475: for( j = 0; j < kBinsPerOctave; j++ ) { cannam@475: cbidx = (m_bufferindex * kBinsPerOctave) + j; cannam@475: m_ChromaBuffer[ cbidx ] = m_ChrPointer[j]; cannam@475: } cannam@475: cannam@475: //keep track of input buffers; cannam@475: if( m_bufferindex++ >= m_ChromaBuffersize - 1) { cannam@475: m_bufferindex = 0; cannam@475: } cannam@475: cannam@475: // track filling of chroma matrix cannam@475: if( m_ChromaBufferFilling++ >= m_ChromaBuffersize) { cannam@475: m_ChromaBufferFilling = m_ChromaBuffersize; cannam@475: } cannam@475: cannam@475: //calculate mean cannam@475: for( k = 0; k < kBinsPerOctave; k++ ) { cannam@475: double mnVal = 0.0; cannam@475: for( j = 0; j < m_ChromaBufferFilling; j++ ) { cannam@475: mnVal += m_ChromaBuffer[ k + (j*kBinsPerOctave) ]; cannam@475: } cannam@475: cannam@475: m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling; cannam@475: } cannam@475: cannam@475: // Normalize for zero average cannam@475: double mHPCP = MathUtilities::mean( m_MeanHPCP, kBinsPerOctave ); cannam@478: for( k = 0; k < kBinsPerOctave; k++ ) { cannam@475: m_MeanHPCP[k] -= mHPCP; cannam@475: } cannam@475: cannam@478: for( k = 0; k < kBinsPerOctave; k++ ) { cannam@478: // The Chromagram has the center of C at bin 0, while the major cannam@475: // and minor profiles have the center of C at 1. We want to have cannam@475: // the correlation for C result also at 1. cannam@475: // To achieve this we have to shift two times: cannam@475: m_MajCorr[k] = krumCorr( m_MeanHPCP, m_MajProfileNorm, (int)k - 2, kBinsPerOctave ); cannam@475: m_MinCorr[k] = krumCorr( m_MeanHPCP, m_MinProfileNorm, (int)k - 2, kBinsPerOctave ); cannam@475: } cannam@475: cannam@475: // m_MajCorr[1] is C center 1 / 3 + 1 = 1 cannam@475: // m_MajCorr[4] is D center 4 / 3 + 1 = 2 cannam@475: // '+ 1' because we number keys 1-24, not 0-23. cannam@475: double maxMaj; cannam@475: int maxMajBin = MathUtilities::getMax( m_MajCorr, kBinsPerOctave, &maxMaj ); cannam@475: double maxMin; cannam@475: int maxMinBin = MathUtilities::getMax( m_MinCorr, kBinsPerOctave, &maxMin ); cannam@475: int maxBin = (maxMaj > maxMin) ? maxMajBin : (maxMinBin + kBinsPerOctave); cannam@475: key = maxBin / 3 + 1; cannam@475: cannam@475: //Median filtering cannam@475: cannam@475: // track Median buffer initial filling cannam@475: if( m_MedianBufferFilling++ >= m_MedianWinsize) { cannam@475: m_MedianBufferFilling = m_MedianWinsize; cannam@475: } cannam@475: cannam@475: //shift median buffer cannam@475: for( k = 1; k < m_MedianWinsize; k++ ) { cannam@475: m_MedianFilterBuffer[ k - 1 ] = m_MedianFilterBuffer[ k ]; cannam@475: } cannam@475: cannam@475: //write new key value into median buffer cannam@475: m_MedianFilterBuffer[ m_MedianWinsize - 1 ] = key; cannam@475: cannam@475: cannam@475: //Copy median into sorting buffer, reversed cannam@475: unsigned int ijx = 0; cannam@475: for( k = 0; k < m_MedianWinsize; k++ ) { cannam@475: m_SortedBuffer[k] = m_MedianFilterBuffer[m_MedianWinsize-1-ijx]; cannam@475: ijx++; cannam@475: } cannam@475: cannam@475: qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), cannam@475: MathUtilities::compareInt); cannam@478: cannam@475: int sortlength = m_MedianBufferFilling; cannam@475: int midpoint = (int)ceil((double)sortlength/2); cannam@475: cannam@475: if( midpoint <= 0 ) { cannam@475: midpoint = 1; cannam@475: } cannam@475: cannam@475: key = m_SortedBuffer[midpoint-1]; cannam@475: cannam@475: return key; cannam@475: } cannam@475: cannam@475: cannam@475: bool GetKeyMode::isModeMinor( int key ) cannam@475: { cannam@475: return (key > 12); cannam@475: } cannam@475: cannam@475: unsigned int getChromaSize() cannam@475: { cannam@475: return kBinsPerOctave; cannam@475: } cannam@475: cannam@475: double* GetKeyMode::getKeyStrengths() { cannam@475: unsigned int k; cannam@475: cannam@475: for (k = 0; k < 24; ++k) { cannam@475: m_keyStrengths[k] = 0; cannam@475: } cannam@475: cannam@478: for( k = 0; k < kBinsPerOctave; k++ ) { cannam@475: int idx = k / (kBinsPerOctave/12); cannam@475: int rem = k % (kBinsPerOctave/12); cannam@475: if (rem == 0 || m_MajCorr[k] > m_keyStrengths[idx]) { cannam@475: m_keyStrengths[idx] = m_MajCorr[k]; cannam@475: } cannam@475: } cannam@475: cannam@478: for( k = 0; k < kBinsPerOctave; k++ ) { cannam@475: int idx = (k + kBinsPerOctave) / (kBinsPerOctave/12); cannam@475: int rem = k % (kBinsPerOctave/12); cannam@475: if (rem == 0 || m_MinCorr[k] > m_keyStrengths[idx]) { cannam@475: m_keyStrengths[idx] = m_MinCorr[k]; cannam@475: } cannam@475: } cannam@475: cannam@475: return m_keyStrengths; cannam@475: }