changeset 232:2b74f9646331

* Add key mode detector
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 11 Dec 2006 09:48:33 +0000
parents dbc354a00714
children b478b1e23ab2
files dsp/keydetection/GetKeyMode.cpp dsp/keydetection/GetKeyMode.h dsp/maths/MathUtilities.cpp dsp/maths/MathUtilities.h qm-dsp.pro
diffstat 5 files changed, 399 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dsp/keydetection/GetKeyMode.cpp	Mon Dec 11 09:48:33 2006 +0000
@@ -0,0 +1,240 @@
+// GetKeyMode.cpp: implementation of the CGetKeyMode class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "GetKeyMode.h"
+#include "dsp/maths/MathUtilities.h"
+
+// Chords profile
+static double MajProfile[36] = 
+{ 0.0384, 0.0629, 0.0258, 0.0121, 0.0146, 0.0106, 0.0364, 0.0610, 0.0267,
+  0.0126, 0.0121, 0.0086, 0.0364, 0.0623, 0.0279, 0.0275, 0.0414, 0.0186,
+  0.0173, 0.0248, 0.0145, 0.0364, 0.0631, 0.0262, 0.0129, 0.0150, 0.0098,
+  0.0312, 0.0521, 0.0235, 0.0129, 0.0142, 0.0095, 0.0289, 0.0478, 0.0239};
+
+static double MinProfile[36] =
+{ 0.0375, 0.0682, 0.0299, 0.0119, 0.0138, 0.0093, 0.0296, 0.0543, 0.0257,
+  0.0292, 0.0519, 0.0246, 0.0159, 0.0234, 0.0135, 0.0291, 0.0544, 0.0248,
+  0.0137, 0.0176, 0.0104, 0.0352, 0.0670, 0.0302, 0.0222, 0.0349, 0.0164,
+  0.0174, 0.0297, 0.0166, 0.0222, 0.0401, 0.0202, 0.0175, 0.0270, 0.0146};
+//
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+
+GetKeyMode::GetKeyMode( double hpcpAverage, double medianAverage )
+:
+m_hpcpAverage( hpcpAverage ),
+m_medianAverage( medianAverage ),
+m_ChrPointer(0),
+m_DecimatedBuffer(0),
+m_ChromaBuffer(0),
+m_MeanHPCP(0),
+m_MajCorr(0),
+m_MinCorr(0),
+m_Keys(0),
+m_MedianFilterBuffer(0),
+m_SortedBuffer(0)
+{
+	m_DecimationFactor = 8;
+
+	//Chromagram configuration parameters
+	m_CromaConfig.isNormalised = 1;
+	m_CromaConfig.FS = lrint(44100.0/(double)m_DecimationFactor);
+	m_CromaConfig.min = 111.0641;
+	m_CromaConfig.max = 1.7770e+003;
+	m_CromaConfig.BPO = 36;
+	m_CromaConfig.CQThresh = 0.0054;
+
+	//Chromagram inst.
+	m_Chroma = new Chromagram( m_CromaConfig );
+
+	//Get calculated parameters from chroma object
+	m_ChromaFrameSize = m_Chroma->getFrameSize();
+	//override hopsize for this application
+	m_ChromaHopSize = m_ChromaFrameSize;//m_Chroma->GetHopSize();
+	m_BPO = m_CromaConfig.BPO;
+
+	//Chromagram average and estimated key median filter lengths
+	m_ChromaBuffersize = (int)ceil( m_hpcpAverage * m_CromaConfig.FS/m_ChromaFrameSize );
+	m_MedianWinsize = (int)ceil( m_medianAverage * m_CromaConfig.FS/m_ChromaFrameSize );
+
+	//Reset counters
+	m_bufferindex = 0;
+	m_ChromaBufferFilling = 0;
+	m_MedianBufferFilling = 0;
+
+
+	//Spawn objectc/arrays
+	m_DecimatedBuffer = new double[m_ChromaFrameSize];
+
+	m_ChromaBuffer = new double[m_BPO * m_ChromaBuffersize];
+	memset( m_ChromaBuffer, 0, sizeof(double) * m_BPO * m_ChromaBuffersize);
+	
+	m_MeanHPCP = new double[m_BPO];
+
+	m_MajCorr = new double[m_BPO];
+	m_MinCorr = new double[m_BPO];
+	m_Keys  = new double[2*m_BPO];
+	
+	m_MedianFilterBuffer = new int[ m_MedianWinsize ];
+	memset( m_MedianFilterBuffer, 0, sizeof(int)*m_MedianWinsize);
+	
+	m_SortedBuffer = new int[ m_MedianWinsize ];
+	memset( m_SortedBuffer, 0, sizeof(int)*m_MedianWinsize);	
+
+	m_Decimator = new Decimator( m_ChromaFrameSize*m_DecimationFactor, m_DecimationFactor );
+}
+
+GetKeyMode::~GetKeyMode()
+{
+
+	delete m_Chroma;
+	delete m_Decimator;
+
+	delete [] m_DecimatedBuffer;
+	delete [] m_ChromaBuffer;
+	delete [] m_MeanHPCP;
+	delete [] m_MajCorr;
+	delete [] m_MinCorr;
+	delete [] m_Keys;
+	delete [] m_MedianFilterBuffer;
+	delete [] m_SortedBuffer;
+}
+
+double GetKeyMode::krumCorr(double *pData1, double *pData2, unsigned int length)
+{
+	double retVal= 0.0;
+		
+	double num = 0;
+	double den = 0;
+	double mX = MathUtilities::mean( pData1, length );
+	double mY = MathUtilities::mean( pData2, length );
+
+	double sum1 = 0;
+	double sum2 = 0;
+
+	for( unsigned int i = 0; i <length; i++ )
+	{
+		num += ( pData1[i] - mX ) * ( pData2[i] - mY );
+
+		sum1 += ( (pData1[i]-mX) * (pData1[i]-mX) );
+		sum2 += ( (pData2[i]-mY) * (pData2[i]-mY) );
+	}
+	
+	den = sqrt(sum1 * sum2);
+	
+	if( den>0 )
+		retVal = num/den;
+	else
+		retVal = 0;
+
+
+	return retVal;
+}
+
+int GetKeyMode::process(double *PCMData)
+{
+	int key;
+
+	unsigned int j,k;
+
+	//////////////////////////////////////////////
+	m_Decimator->process( PCMData, m_DecimatedBuffer);
+
+	m_ChrPointer = m_Chroma->process( m_DecimatedBuffer );		
+
+	// populate hpcp values;
+	int cbidx;
+	for( j = 0; j < m_BPO; j++ )
+	{
+		cbidx = (m_bufferindex * m_BPO) + j;
+		m_ChromaBuffer[ cbidx ] = m_ChrPointer[j];
+	}
+
+	//keep track of input buffers;
+	if( m_bufferindex++ >= m_ChromaBuffersize - 1) 
+		m_bufferindex = 0;
+
+	// track filling of chroma matrix
+	if( m_ChromaBufferFilling++ >= m_ChromaBuffersize)
+		m_ChromaBufferFilling = m_ChromaBuffersize;
+
+	//calculate mean 		
+	for( k = 0; k < m_BPO; k++ )
+	{
+		double mnVal = 0.0;
+		for( j = 0; j < m_ChromaBufferFilling; j++ )
+		{
+			mnVal += m_ChromaBuffer[ k + (j*m_BPO) ];
+		}
+
+		m_MeanHPCP[k] = mnVal/(double)m_ChromaBufferFilling;
+	}
+
+
+	for( k = 0; k < m_BPO; k++ )
+	{
+		m_MajCorr[k] = krumCorr( m_MeanHPCP, MajProfile, m_BPO );
+		m_MinCorr[k] = krumCorr( m_MeanHPCP, MinProfile, m_BPO );
+
+		MathUtilities::circShift( MajProfile, m_BPO, 1 );
+		MathUtilities::circShift( MinProfile, m_BPO, 1 );
+	}
+
+	for( k = 0; k < m_BPO; k++ )
+	{
+		m_Keys[k] = m_MajCorr[k];
+		m_Keys[k+m_BPO] = m_MinCorr[k];
+	}
+
+
+	double dummy;
+	key = 1 + (int)ceil( (double)MathUtilities::getMax( m_Keys, 2* m_BPO, &dummy )/3 );
+
+
+	//Median filtering
+
+	// track Median buffer initial filling
+	if( m_MedianBufferFilling++ >= m_MedianWinsize)
+		m_MedianBufferFilling = m_MedianWinsize;
+		
+	//shift median buffer
+	for( k = 1; k < m_MedianWinsize; k++ )
+	{
+		m_MedianFilterBuffer[ k - 1 ] = m_MedianFilterBuffer[ k ];
+	}
+
+	//write new key value into median buffer
+	m_MedianFilterBuffer[ m_MedianWinsize - 1 ] = key;
+
+
+	//Copy median into sorting buffer, reversed
+	unsigned int ijx = 0;
+	for( k = 0; k < m_MedianWinsize; k++ )
+	{
+		m_SortedBuffer[k] = m_MedianFilterBuffer[m_MedianWinsize-1-ijx];
+		ijx++;
+	}
+
+
+	//quicksort 
+	qsort(m_SortedBuffer, m_MedianBufferFilling, sizeof(unsigned int), MathUtilities::compareInt);
+
+	int sortlength = m_MedianBufferFilling;
+	int midpoint = (int)ceil((double)sortlength/2);
+
+	if( midpoint <= 0 )
+		midpoint = 1;
+
+	key = m_SortedBuffer[midpoint-1];
+
+	return key;
+}
+
+
+int GetKeyMode::isModeMinor( int key )
+{ 
+	return ((key-1 - (int)MathUtilities::mod((double)(key-1),(double)12))/12); 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dsp/keydetection/GetKeyMode.h	Mon Dec 11 09:48:33 2006 +0000
@@ -0,0 +1,91 @@
+/*
+ * Author: c.landone 
+ * Description:
+ *
+ * Syntax: C++
+ *
+ * Copyright (c) 2005 Centre for Digital Music ( C4DM )
+ *                    Queen Mary Univesrity of London
+ *
+ *
+ * This program is not free software; you cannot redistribute it 
+ * without the explicit authorization from the centre for digital music,
+ * queen mary university of london 
+ *
+ */
+
+#if !defined GETKEYMODE_H
+#define GETKEYMODE_H
+
+
+#include "dsp/rateconversion/Decimator.h"
+#include "dsp/chromagram/Chromagram.h"
+
+
+class GetKeyMode  
+{
+public:
+	GetKeyMode( double hpcpAverage, double medianAverage );
+
+	virtual ~GetKeyMode();
+
+	int process( double* PCMData );
+
+	double krumCorr( double* pData1, double* pData2, unsigned int length );
+
+	unsigned int getBlockSize() { return m_ChromaFrameSize*m_DecimationFactor; }
+	unsigned int getHopSize() { return m_ChromaHopSize*m_DecimationFactor; }
+
+	double* getChroma() { return m_ChrPointer; }
+	unsigned int getChromaSize() { return m_BPO; }
+
+	double* getMeanHPCP() { return m_MeanHPCP; }
+
+	int isModeMinor( int key ); 
+
+protected:
+
+	double m_hpcpAverage;
+	double m_medianAverage;
+	unsigned int m_DecimationFactor;
+
+	//Decimator (fixed)
+	Decimator* m_Decimator;
+
+	//chroma configuration
+	ChromaConfig m_CromaConfig;
+
+	//Chromagram object
+	Chromagram* m_Chroma;
+
+	//Chromagram output pointer
+	double* m_ChrPointer;
+
+	//Framesize
+	unsigned int m_ChromaFrameSize;
+	//Hop
+	unsigned int m_ChromaHopSize;
+	//Bins per octave
+	unsigned int m_BPO;
+
+
+	unsigned int m_ChromaBuffersize;
+	unsigned int m_MedianWinsize;
+	
+	unsigned int m_bufferindex;
+	unsigned int m_ChromaBufferFilling;
+	unsigned int m_MedianBufferFilling;
+	
+
+	double* m_DecimatedBuffer;
+	double* m_ChromaBuffer;
+	double* m_MeanHPCP;
+
+	double* m_MajCorr;
+	double* m_MinCorr;
+	double* m_Keys;
+	int* m_MedianFilterBuffer;
+	int* m_SortedBuffer;
+};
+
+#endif // !defined GETKEYMODE_H
--- a/dsp/maths/MathUtilities.cpp	Fri Dec 08 18:05:36 2006 +0000
+++ b/dsp/maths/MathUtilities.cpp	Mon Dec 11 09:48:33 2006 +0000
@@ -168,3 +168,54 @@
 		
     }
 }
+
+int MathUtilities::getMax( double* pData, unsigned int Length, double* pMax )
+{
+	unsigned int index = 0;
+	unsigned int i;
+	double temp = 0.0;
+	
+	double max = pData[0];
+
+	for( i = 0; i < Length; i++)
+	{
+		temp = pData[ i ];
+
+		if( temp > max )
+		{
+			max =  temp ;
+			index = i;
+		}
+		
+   	}
+
+	*pMax = max;
+
+
+	return index;
+}
+
+void MathUtilities::circShift( double* pData, int length, int shift)
+{
+	shift = shift % length;
+	double temp;
+	int i,n;
+
+	for( i = 0; i < shift; i++)
+	{
+		temp=*(pData + length - 1);
+
+		for( n = length-2; n >= 0; n--)
+		{
+			*(pData+n+1)=*(pData+n);
+		}
+
+        *pData = temp;
+    }
+}
+
+int MathUtilities::compareInt (const void * a, const void * b)
+{
+  return ( *(int*)a - *(int*)b );
+}
+
--- a/dsp/maths/MathUtilities.h	Fri Dec 08 18:05:36 2006 +0000
+++ b/dsp/maths/MathUtilities.h	Mon Dec 11 09:48:33 2006 +0000
@@ -25,6 +25,9 @@
     static double mod( double x, double y);
     static void	  getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm);
     static double getAlphaNorm(const std::vector <double> &data, unsigned int alpha );
+    static void   circShift( double* data, int length, int shift);
+    static int	  getMax( double* data, unsigned int length, double* max );
+    static int    compareInt(const void * a, const void * b);
 };
 
 #endif
--- a/qm-dsp.pro	Fri Dec 08 18:05:36 2006 +0000
+++ b/qm-dsp.pro	Mon Dec 11 09:48:33 2006 +0000
@@ -4,27 +4,18 @@
 OBJECTS_DIR = tmp_obj
 MOC_DIR = tmp_moc
 
-DEPENDPATH += base \
-              dsp/chromagram \
-              dsp/maths \
-              dsp/onsets \
-              dsp/phasevocoder \
-              dsp/rateconversion \
-              dsp/signalconditioning \
-              dsp/tempotracking \
-              dsp/tonal \
-              dsp/transforms
-INCLUDEPATH += . \
-               base \
-               dsp/maths \
-               dsp/chromagram \
-               dsp/transforms \
-               dsp/onsets \
-               dsp/phasevocoder \
-               dsp/signalconditioning \
-               dsp/rateconversion \
-               dsp/tempotracking \
-               dsp/tonal
+#DEPENDPATH += base \
+#              dsp/chromagram \
+#              dsp/keydetection \
+#              dsp/maths \
+#              dsp/onsets \
+#              dsp/phasevocoder \
+#              dsp/rateconversion \
+#              dsp/signalconditioning \
+#              dsp/tempotracking \
+#              dsp/tonal \
+#              dsp/transforms
+INCLUDEPATH += . 
 
 # Input
 HEADERS += base/Pitch.h \
@@ -32,6 +23,7 @@
            dsp/chromagram/Chromagram.h \
            dsp/chromagram/ChromaProcess.h \
            dsp/chromagram/ConstantQ.h \
+           dsp/keydetection/GetKeyMode.h \
            dsp/maths/Correlation.h \
            dsp/maths/Histogram.h \
            dsp/maths/MathAliases.h \
@@ -54,6 +46,7 @@
            dsp/chromagram/Chromagram.cpp \
            dsp/chromagram/ChromaProcess.cpp \
            dsp/chromagram/ConstantQ.cpp \
+           dsp/keydetection/GetKeyMode.cpp \
            dsp/maths/Correlation.cpp \
            dsp/maths/MathUtilities.cpp \
            dsp/onsets/DetectionFunction.cpp \