changeset 11:09fb76606b2b

* Removed many unnecessary heap allocations with objects
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Wed, 13 Aug 2014 10:45:46 +0100
parents be59b4a73f49
children d58409ecd720
files NoveltyCurve.cpp NoveltyCurve.h Spectrogram.cpp Spectrogram.h Tempogram.cpp Tempogram.h WindowFunction.cpp
diffstat 7 files changed, 37 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- 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 <memory>
 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<float>
-NoveltyCurve::spectrogramToNoveltyCurve(vector< vector<float> > &spectrogram){
+NoveltyCurve::spectrogramToNoveltyCurve(vector< vector<float> > spectrogram){
     
     assert(spectrogram.size() == m_blockSize);
     assert(spectrogram[0].size() == m_numberOfBlocks);
--- 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<float> data;
     
     void initialise();
     void cleanup();
@@ -36,11 +39,10 @@
     void halfWaveRectify(std::vector< std::vector<float> > &spectrogram);
     
 public:
-    std::vector<float> data;
     
     NoveltyCurve(float samplingFrequency, int fftLength, int numberOfBlocks, int compressionConstant);
     ~NoveltyCurve();
-    std::vector<float> spectrogramToNoveltyCurve(std::vector< std::vector<float> > &spectrogram);
+    std::vector<float> spectrogramToNoveltyCurve(std::vector< std::vector<float> > spectrogram);
 };
 
 #endif /* defined(__Tempogram__NoveltyCurve__) */
--- 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<float> >(m_numberOfOutputBins, vector<float>(numberOfBlocks));
 }
 
-void Spectrogram::cleanup(){
+void SpectrogramProcessor::cleanup(){
     delete []fftInput;
     delete []fftOutputReal;
     delete []fftOutputImag;
@@ -45,7 +45,7 @@
 }
 
 //process method
-vector< vector<float> > Spectrogram::audioToMagnitudeSpectrogram(const float * const input, const float * window){
+vector< vector<float> > SpectrogramProcessor::process(const float * const input, const float * window){
     
     int readPointerBeginIndex = m_hopSize-m_windowLength;
     int writeBlockPointer = 0;
--- 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 <vamp-sdk/FFT.h>
 #include <cmath>
 
-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<float> > 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<float> > 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__) */
--- 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<float> >(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<float> > tempogram = spectrogramProcessor->audioToMagnitudeSpectrogram(&noveltyCurve[0], hannWindowtN);
-    delete spectrogramProcessor;
-    spectrogramProcessor = NULL;
+    vector< vector<float> > 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;
 }
--- 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<Vamp::RealTime> ncTimestamps;
 };
--- 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