# HG changeset patch # User Carl Bussey # Date 1407923146 -3600 # Node ID 09fb76606b2b150aed51fb00ff14b41f36c54872 # Parent be59b4a73f49fde077183598ed28045c9a029331 * Removed many unnecessary heap allocations with objects diff -r be59b4a73f49 -r 09fb76606b2b NoveltyCurve.cpp --- a/NoveltyCurve.cpp Tue Aug 12 14:40:37 2014 +0100 +++ b/NoveltyCurve.cpp Wed Aug 13 10:45:46 2014 +0100 @@ -6,7 +6,10 @@ // Copyright (c) 2014 Carl Bussey. All rights reserved. // +//Spectrogram dimensions should be flipped? + #include "NoveltyCurve.h" +#include using namespace std; NoveltyCurve::NoveltyCurve(float samplingFrequency, int fftLength, int numberOfBlocks, int compressionConstant) : @@ -74,10 +77,8 @@ float * m_hannWindow = new float[m_hannLength]; WindowFunction::hanning(m_hannWindow, m_hannLength, true); - FIRFilter *filter = new FIRFilter(m_numberOfBlocks, m_hannLength); - filter->process(&noveltyCurve[0], m_hannWindow, &localAverage[0]); - delete filter; - filter = NULL; + FIRFilter filter(m_numberOfBlocks, m_hannLength); + filter.process(&noveltyCurve[0], m_hannWindow, &localAverage[0]); assert(noveltyCurve.size() == m_numberOfBlocks); for (int i = 0; i < m_numberOfBlocks; i++){ @@ -85,7 +86,7 @@ noveltyCurve[i] = noveltyCurve[i] >= 0 ? noveltyCurve[i] : 0; } - delete m_hannWindow; + delete []m_hannWindow; m_hannWindow = NULL; } @@ -100,14 +101,11 @@ diffHannWindow[i] = -diffHannWindow[i]; } - FIRFilter *smoothFilter = new FIRFilter(m_numberOfBlocks, smoothLength); + FIRFilter smoothFilter(m_numberOfBlocks, smoothLength); for (int i = 0; i < m_blockSize; i++){ - smoothFilter->process(&spectrogram[i][0], diffHannWindow, &spectrogram[i][0]); + smoothFilter.process(&spectrogram[i][0], diffHannWindow, &spectrogram[i][0]); } - - delete smoothFilter; - smoothFilter = NULL; } //half rectification (set negative to zero) @@ -122,7 +120,7 @@ //process method vector -NoveltyCurve::spectrogramToNoveltyCurve(vector< vector > &spectrogram){ +NoveltyCurve::spectrogramToNoveltyCurve(vector< vector > spectrogram){ assert(spectrogram.size() == m_blockSize); assert(spectrogram[0].size() == m_numberOfBlocks); diff -r be59b4a73f49 -r 09fb76606b2b NoveltyCurve.h --- a/NoveltyCurve.h Tue Aug 12 14:40:37 2014 +0100 +++ b/NoveltyCurve.h Wed Aug 13 10:45:46 2014 +0100 @@ -6,6 +6,8 @@ // Copyright (c) 2014 Carl Bussey. All rights reserved. // +// Don't new delete objects + #ifndef __Tempogram__NoveltyCurve__ #define __Tempogram__NoveltyCurve__ @@ -27,6 +29,7 @@ int * m_bandBoundaries; int m_hannLength; float * m_bandSum; + std::vector data; void initialise(); void cleanup(); @@ -36,11 +39,10 @@ void halfWaveRectify(std::vector< std::vector > &spectrogram); public: - std::vector data; NoveltyCurve(float samplingFrequency, int fftLength, int numberOfBlocks, int compressionConstant); ~NoveltyCurve(); - std::vector spectrogramToNoveltyCurve(std::vector< std::vector > &spectrogram); + std::vector spectrogramToNoveltyCurve(std::vector< std::vector > spectrogram); }; #endif /* defined(__Tempogram__NoveltyCurve__) */ diff -r be59b4a73f49 -r 09fb76606b2b Spectrogram.cpp --- a/Spectrogram.cpp Tue Aug 12 14:40:37 2014 +0100 +++ b/Spectrogram.cpp Wed Aug 13 10:45:46 2014 +0100 @@ -10,7 +10,7 @@ using namespace std; using Vamp::FFT; -Spectrogram::Spectrogram(unsigned int inputLength, unsigned int windowLength, unsigned int fftLength, unsigned int hopSize) : +SpectrogramProcessor::SpectrogramProcessor(unsigned int inputLength, unsigned int windowLength, unsigned int fftLength, unsigned int hopSize) : m_inputLength(inputLength), m_windowLength(windowLength), m_fftLength(fftLength), @@ -23,11 +23,11 @@ initialise(); } -Spectrogram::~Spectrogram(){ +SpectrogramProcessor::~SpectrogramProcessor(){ cleanup(); } -void Spectrogram::initialise(){ +void SpectrogramProcessor::initialise(){ fftInput = new double [m_fftLength]; fftOutputReal = new double [m_fftLength]; fftOutputImag = new double [m_fftLength]; @@ -36,7 +36,7 @@ spectrogramOutput = vector< vector >(m_numberOfOutputBins, vector(numberOfBlocks)); } -void Spectrogram::cleanup(){ +void SpectrogramProcessor::cleanup(){ delete []fftInput; delete []fftOutputReal; delete []fftOutputImag; @@ -45,7 +45,7 @@ } //process method -vector< vector > Spectrogram::audioToMagnitudeSpectrogram(const float * const input, const float * window){ +vector< vector > SpectrogramProcessor::process(const float * const input, const float * window){ int readPointerBeginIndex = m_hopSize-m_windowLength; int writeBlockPointer = 0; diff -r be59b4a73f49 -r 09fb76606b2b Spectrogram.h --- a/Spectrogram.h Tue Aug 12 14:40:37 2014 +0100 +++ b/Spectrogram.h Wed Aug 13 10:45:46 2014 +0100 @@ -12,7 +12,7 @@ #include #include -class Spectrogram{ +class SpectrogramProcessor{ int m_inputLength; int m_windowLength; int m_fftLength; @@ -26,9 +26,9 @@ void initialise(); void cleanup(); public: - std::vector< std::vector > audioToMagnitudeSpectrogram(const float * const input, const float * window); - Spectrogram(unsigned int inputLength, unsigned int windowLength, unsigned int fftLength, unsigned int hopSize); - ~Spectrogram(); + std::vector< std::vector > process(const float * const input, const float * window); + SpectrogramProcessor(unsigned int inputLength, unsigned int windowLength, unsigned int fftLength, unsigned int hopSize); + ~SpectrogramProcessor(); }; #endif /* defined(__Tempogram__Spectrogram__) */ diff -r be59b4a73f49 -r 09fb76606b2b Tempogram.cpp --- a/Tempogram.cpp Tue Aug 12 14:40:37 2014 +0100 +++ b/Tempogram.cpp Wed Aug 13 10:45:46 2014 +0100 @@ -329,7 +329,6 @@ Tempogram::reset() { // Clear buffers, reset stored values, etc - cleanupForGRF(); ncTimestamps.clear(); specData.clear(); specData = vector< vector >(m_blockSize/2 + 1); @@ -358,28 +357,15 @@ return featureSet; } -void -Tempogram::initialiseForGRF(){ - hannWindowtN = new float[windowLength]; +Tempogram::FeatureSet +Tempogram::getRemainingFeatures() +{ + float * hannWindowtN = new float[windowLength]; for (int i = 0; i < windowLength; i++){ hannWindowtN[i] = 0.0; } -} - -void -Tempogram::cleanupForGRF(){ - delete []hannWindowtN; - hannWindowtN = NULL; -} - - - -Tempogram::FeatureSet -Tempogram::getRemainingFeatures() -{ - //Make sure this is called at the beginning of the function - initialiseForGRF(); + FeatureSet featureSet; //initialise noveltycurve processor @@ -399,11 +385,9 @@ WindowFunction::hanning(hannWindowtN,windowLength); //initialise spectrogram processor - Spectrogram * spectrogramProcessor = new Spectrogram(numberOfBlocks, windowLength, fftLength, thopSize); + SpectrogramProcessor spectrogramProcessor(numberOfBlocks, windowLength, fftLength, thopSize); //compute spectrogram from novelty curve data (i.e., tempogram) - vector< vector > tempogram = spectrogramProcessor->audioToMagnitudeSpectrogram(&noveltyCurve[0], hannWindowtN); - delete spectrogramProcessor; - spectrogramProcessor = NULL; + vector< vector > tempogram = spectrogramProcessor.process(&noveltyCurve[0], hannWindowtN); int timePointer = thopSize-windowLength/2; int tempogramLength = tempogram[0].size(); @@ -425,8 +409,10 @@ timePointer += thopSize; } - //Make sure this is called at the end of the function - cleanupForGRF(); + //float func = [](){ cout << "Hello"; }; + + delete []hannWindowtN; + hannWindowtN = NULL; return featureSet; } diff -r be59b4a73f49 -r 09fb76606b2b Tempogram.h --- a/Tempogram.h Tue Aug 12 14:40:37 2014 +0100 +++ b/Tempogram.h Wed Aug 13 10:45:46 2014 +0100 @@ -83,8 +83,6 @@ float minDB; void cleanup(); //used to release anything allocated in initialise() - void initialiseForGRF(); //used to initialise anything for getRemainingFeatures() - void cleanupForGRF(); //used to clean up anything allocated in initialiseForGRF() string floatToString(float value) const; void updateBPMParameters(); @@ -99,7 +97,6 @@ unsigned int maxBin; int numberOfBlocks; - float *hannWindowtN; vector ncTimestamps; }; diff -r be59b4a73f49 -r 09fb76606b2b WindowFunction.cpp --- a/WindowFunction.cpp Tue Aug 12 14:40:37 2014 +0100 +++ b/WindowFunction.cpp Wed Aug 13 10:45:46 2014 +0100 @@ -11,15 +11,16 @@ //static function void -WindowFunction::hanning(float *signal, const unsigned int N, const bool normalise){ +WindowFunction::hanning(float *window, const unsigned int N, const bool normalise){ float sum = 0; for(int i = 0; i < N; i++){ - sum += signal[i] = 0.5*(1-cos((float)2*M_PI*i/N)); + window[i] = 0.5*(1-cos((float)2*M_PI*i/N)); + sum += window[i]; } if (normalise){ for(int i = 0; i < N; i++){ - signal[i] /= sum; + window[i] /= sum; } } } \ No newline at end of file