c@259: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@378: /* c@378: Copyright (c) 2005 Centre for Digital Music ( C4DM ) c@378: Queen Mary Univesrity of London c@259: c@378: This program is free software; you can redistribute it and/or c@378: modify it under the terms of the GNU General Public License as c@378: published by the Free Software Foundation; either version 2 of the c@378: License, or (at your option) any later version. See the file c@378: COPYING included with this distribution for more information. c@378: */ c@232: // GetKeyMode.cpp: implementation of the CGetKeyMode class. c@232: // c@232: ////////////////////////////////////////////////////////////////////// c@232: c@232: #include "GetKeyMode.h" c@241: #include "maths/MathUtilities.h" c@234: #include "base/Pitch.h" c@234: c@234: #include c@232: c@272: #include c@272: #include c@272: c@232: // Chords profile c@232: static double MajProfile[36] = c@232: { 0.0384, 0.0629, 0.0258, 0.0121, 0.0146, 0.0106, 0.0364, 0.0610, 0.0267, devnull@240: 0.0126, 0.0121, 0.0086, 0.0364, 0.0623, 0.0279, 0.0275, 0.0414, 0.0186, c@232: 0.0173, 0.0248, 0.0145, 0.0364, 0.0631, 0.0262, 0.0129, 0.0150, 0.0098, c@232: 0.0312, 0.0521, 0.0235, 0.0129, 0.0142, 0.0095, 0.0289, 0.0478, 0.0239}; c@232: c@232: static double MinProfile[36] = c@232: { 0.0375, 0.0682, 0.0299, 0.0119, 0.0138, 0.0093, 0.0296, 0.0543, 0.0257, c@232: 0.0292, 0.0519, 0.0246, 0.0159, 0.0234, 0.0135, 0.0291, 0.0544, 0.0248, c@232: 0.0137, 0.0176, 0.0104, 0.0352, 0.0670, 0.0302, 0.0222, 0.0349, 0.0164, c@232: 0.0174, 0.0297, 0.0166, 0.0222, 0.0401, 0.0202, 0.0175, 0.0270, 0.0146}; c@232: // c@265: c@232: c@232: ////////////////////////////////////////////////////////////////////// c@232: // Construction/Destruction c@232: ////////////////////////////////////////////////////////////////////// c@232: c@234: GetKeyMode::GetKeyMode( int sampleRate, float tuningFrequency, c@259: double hpcpAverage, double medianAverage ) : c@259: m_hpcpAverage( hpcpAverage ), c@259: m_medianAverage( medianAverage ), c@259: m_ChrPointer(0), c@259: m_DecimatedBuffer(0), c@259: m_ChromaBuffer(0), c@259: m_MeanHPCP(0), c@259: m_MajCorr(0), c@259: m_MinCorr(0), c@259: m_Keys(0), c@259: m_MedianFilterBuffer(0), c@265: m_SortedBuffer(0), c@265: m_keyStrengths(0) c@232: { c@259: m_DecimationFactor = 8; c@259: c@259: // Chromagram configuration parameters c@259: m_ChromaConfig.normalise = MathUtilities::NormaliseUnitMax; c@259: m_ChromaConfig.FS = lrint(sampleRate/(double)m_DecimationFactor); c@283: if (m_ChromaConfig.FS < 1) m_ChromaConfig.FS = 1; c@232: c@259: // Set C (= MIDI #12) as our base : c@259: // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc. c@259: m_ChromaConfig.min = Pitch::getFrequencyForPitch c@265: (48, 0, tuningFrequency); c@259: m_ChromaConfig.max = Pitch::getFrequencyForPitch c@259: (96, 0, tuningFrequency); c@234: c@259: m_ChromaConfig.BPO = 36; c@259: m_ChromaConfig.CQThresh = 0.0054; c@234: c@259: // Chromagram inst. c@259: m_Chroma = new Chromagram( m_ChromaConfig ); c@235: c@259: // Get calculated parameters from chroma object c@259: m_ChromaFrameSize = m_Chroma->getFrameSize(); c@259: // override hopsize for this application c@259: m_ChromaHopSize = m_ChromaFrameSize; c@259: m_BPO = m_ChromaConfig.BPO; c@235: c@265: // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl; c@265: c@259: // Chromagram average and estimated key median filter lengths c@259: m_ChromaBuffersize = (int)ceil( m_hpcpAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); c@259: m_MedianWinsize = (int)ceil( m_medianAverage * m_ChromaConfig.FS/m_ChromaFrameSize ); c@259: c@259: // Reset counters c@259: m_bufferindex = 0; c@259: m_ChromaBufferFilling = 0; c@259: m_MedianBufferFilling = 0; c@234: c@259: // Spawn objectc/arrays c@259: m_DecimatedBuffer = new double[m_ChromaFrameSize]; c@259: c@259: m_ChromaBuffer = new double[m_BPO * m_ChromaBuffersize]; c@259: memset( m_ChromaBuffer, 0, sizeof(double) * m_BPO * m_ChromaBuffersize); c@259: c@259: m_MeanHPCP = new double[m_BPO]; c@259: c@259: m_MajCorr = new double[m_BPO]; c@259: m_MinCorr = new double[m_BPO]; c@259: m_Keys = new double[2*m_BPO]; c@259: c@259: m_MedianFilterBuffer = new int[ m_MedianWinsize ]; c@259: memset( m_MedianFilterBuffer, 0, sizeof(int)*m_MedianWinsize); c@259: c@259: m_SortedBuffer = new int[ m_MedianWinsize ]; c@259: memset( m_SortedBuffer, 0, sizeof(int)*m_MedianWinsize); c@259: c@259: m_Decimator = new Decimator c@259: ( m_ChromaFrameSize*m_DecimationFactor, m_DecimationFactor ); c@265: c@265: m_keyStrengths = new double[24]; c@232: } c@232: c@232: GetKeyMode::~GetKeyMode() c@232: { c@232: c@259: delete m_Chroma; c@259: delete m_Decimator; c@259: c@259: delete [] m_DecimatedBuffer; c@259: delete [] m_ChromaBuffer; c@259: delete [] m_MeanHPCP; c@259: delete [] m_MajCorr; c@259: delete [] m_MinCorr; c@259: delete [] m_Keys; c@259: delete [] m_MedianFilterBuffer; c@259: delete [] m_SortedBuffer; c@265: c@265: delete[] m_keyStrengths; c@232: } c@232: c@232: double GetKeyMode::krumCorr(double *pData1, double *pData2, unsigned int length) c@232: { c@259: double retVal= 0.0; c@259: c@259: double num = 0; c@259: double den = 0; c@259: double mX = MathUtilities::mean( pData1, length ); c@259: double mY = MathUtilities::mean( pData2, length ); c@259: c@259: double sum1 = 0; c@259: double sum2 = 0; c@259: c@259: for( unsigned int i = 0; i 0 ) c@259: retVal = num/den; c@259: else c@259: retVal = 0; c@232: c@232: c@259: return retVal; c@232: } c@232: c@232: int GetKeyMode::process(double *PCMData) c@232: { c@259: int key; c@232: c@259: unsigned int j,k; c@232: c@259: ////////////////////////////////////////////// c@259: m_Decimator->process( PCMData, m_DecimatedBuffer); c@232: c@259: m_ChrPointer = m_Chroma->process( m_DecimatedBuffer ); c@232: daschuer@456: // The Cromagram has the center of C at bin 0, while the major daschuer@456: // and minor profiles have the center of C at 1. We want to have daschuer@456: // the correlation for C result also at 1. daschuer@456: // To achieve this we have to shift two times: daschuer@456: MathUtilities::circShift( m_ChrPointer, m_BPO, 2); c@248: /* c@298: std::cout << "raw chroma: "; c@298: for (int ii = 0; ii < m_BPO; ++ii) { c@298: if (ii % (m_BPO/12) == 0) std::cout << "\n"; c@298: std::cout << m_ChrPointer[ii] << " "; c@298: } c@298: std::cout << std::endl; c@248: */ c@259: // populate hpcp values; c@259: int cbidx; c@259: for( j = 0; j < m_BPO; j++ ) c@259: { c@259: cbidx = (m_bufferindex * m_BPO) + j; c@259: m_ChromaBuffer[ cbidx ] = m_ChrPointer[j]; c@259: } c@232: c@259: //keep track of input buffers; c@259: if( m_bufferindex++ >= m_ChromaBuffersize - 1) c@259: m_bufferindex = 0; c@232: c@259: // track filling of chroma matrix c@259: if( m_ChromaBufferFilling++ >= m_ChromaBuffersize) c@259: m_ChromaBufferFilling = m_ChromaBuffersize; c@232: c@259: //calculate mean c@259: for( k = 0; k < m_BPO; k++ ) c@259: { c@259: double mnVal = 0.0; c@259: for( j = 0; j < m_ChromaBufferFilling; j++ ) c@259: { c@259: mnVal += m_ChromaBuffer[ k + (j*m_BPO) ]; c@259: } c@232: c@259: m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling; c@259: } c@232: c@232: c@259: for( k = 0; k < m_BPO; k++ ) c@259: { c@259: m_MajCorr[k] = krumCorr( m_MeanHPCP, MajProfile, m_BPO ); c@259: m_MinCorr[k] = krumCorr( m_MeanHPCP, MinProfile, m_BPO ); c@232: c@259: MathUtilities::circShift( MajProfile, m_BPO, 1 ); c@259: MathUtilities::circShift( MinProfile, m_BPO, 1 ); c@259: } devnull@240: c@259: for( k = 0; k < m_BPO; k++ ) c@259: { c@259: m_Keys[k] = m_MajCorr[k]; c@259: m_Keys[k+m_BPO] = m_MinCorr[k]; c@259: } devnull@240: c@265: for (k = 0; k < 24; ++k) { c@265: m_keyStrengths[k] = 0; c@265: } c@265: c@265: for( k = 0; k < m_BPO*2; k++ ) c@265: { c@298: int idx = k / (m_BPO/12); c@298: int rem = k % (m_BPO/12); c@298: if (rem == 0 || m_Keys[k] > m_keyStrengths[idx]) { c@298: m_keyStrengths[idx] = m_Keys[k]; c@298: } c@298: c@298: // m_keyStrengths[k/(m_BPO/12)] += m_Keys[k]; c@265: } devnull@240: c@248: /* c@259: std::cout << "raw keys: "; c@259: for (int ii = 0; ii < 2*m_BPO; ++ii) { c@298: if (ii % (m_BPO/12) == 0) std::cout << "\n"; c@298: std::cout << m_Keys[ii] << " "; c@298: } c@298: std::cout << std::endl; c@298: c@298: std::cout << "key strengths: "; c@298: for (int ii = 0; ii < 24; ++ii) { c@298: if (ii % 6 == 0) std::cout << "\n"; c@298: std::cout << m_keyStrengths[ii] << " "; c@259: } c@259: std::cout << std::endl; c@248: */ c@259: double dummy; daschuer@456: // m_Keys[1] is C center 1 / 3 + 1 = 1 daschuer@456: // m_Keys[4] is D center 4 / 3 + 1 = 2 daschuer@456: // '+ 1' because we number keys 1-24, not 0-23. daschuer@456: key = MathUtilities::getMax( m_Keys, 2* m_BPO, &dummy ) / 3 + 1; c@234: c@298: // std::cout << "key pre-sorting: " << key << std::endl; c@232: c@232: c@259: //Median filtering c@232: c@259: // track Median buffer initial filling c@259: if( m_MedianBufferFilling++ >= m_MedianWinsize) c@259: m_MedianBufferFilling = m_MedianWinsize; c@232: c@259: //shift median buffer c@259: for( k = 1; k < m_MedianWinsize; k++ ) c@259: { c@259: m_MedianFilterBuffer[ k - 1 ] = m_MedianFilterBuffer[ k ]; c@259: } c@232: c@259: //write new key value into median buffer c@259: m_MedianFilterBuffer[ m_MedianWinsize - 1 ] = key; c@232: c@232: c@259: //Copy median into sorting buffer, reversed c@259: unsigned int ijx = 0; c@259: for( k = 0; k < m_MedianWinsize; k++ ) c@259: { c@259: m_SortedBuffer[k] = m_MedianFilterBuffer[m_MedianWinsize-1-ijx]; c@259: ijx++; c@259: } c@232: c@259: qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), c@259: MathUtilities::compareInt); c@259: /* c@259: std::cout << "sorted: "; c@259: for (int ii = 0; ii < m_MedianBufferFilling; ++ii) { c@259: std::cout << m_SortedBuffer[ii] << " "; c@259: } c@259: std::cout << std::endl; c@259: */ c@259: int sortlength = m_MedianBufferFilling; c@259: int midpoint = (int)ceil((double)sortlength/2); c@232: c@298: // std::cout << "midpoint = " << midpoint << endl; c@298: c@259: if( midpoint <= 0 ) c@259: midpoint = 1; c@232: c@259: key = m_SortedBuffer[midpoint-1]; c@232: c@298: // std::cout << "returning key = " << key << endl; c@298: c@259: return key; c@232: } c@232: c@232: c@268: bool GetKeyMode::isModeMinor( int key ) c@232: { c@259: return (key > 12); c@232: }