Mercurial > hg > btrack
comparison src/OnsetDetectionFunction.cpp @ 64:d3c52c6b3905
Changed all pointers to arrays in OnsetDetectionFunction into vectors
author | Adam Stark <adamstark@users.noreply.github.com> |
---|---|
date | Tue, 28 Jan 2014 00:19:21 +0000 |
parents | ba3fc238ccad |
children | bddd59087c36 |
comparison
equal
deleted
inserted
replaced
63:1395895f6cdf | 64:d3c52c6b3905 |
---|---|
24 | 24 |
25 //======================================================================= | 25 //======================================================================= |
26 OnsetDetectionFunction::OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType) | 26 OnsetDetectionFunction::OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType) |
27 { | 27 { |
28 // indicate that we have not initialised yet | 28 // indicate that we have not initialised yet |
29 initialised = 0; | 29 initialised = false; |
30 | 30 |
31 // set pi | 31 // set pi |
32 pi = 3.14159265358979; | 32 pi = 3.14159265358979; |
33 | 33 |
34 // initialise with arguments to constructor | 34 // initialise with arguments to constructor |
37 | 37 |
38 | 38 |
39 //======================================================================= | 39 //======================================================================= |
40 OnsetDetectionFunction::~OnsetDetectionFunction() | 40 OnsetDetectionFunction::~OnsetDetectionFunction() |
41 { | 41 { |
42 // destroy fft plan | 42 if (initialised) |
43 fftw_destroy_plan(p); | 43 { |
44 fftw_free(complexIn); | 44 // destroy fft plan |
45 fftw_free(complexOut); | 45 fftw_destroy_plan(p); |
46 | 46 fftw_free(complexIn); |
47 // deallocate memory | 47 fftw_free(complexOut); |
48 delete [] frame; | 48 } |
49 frame = NULL; | |
50 delete [] window; | |
51 window = NULL; | |
52 delete [] magSpec; | |
53 magSpec = NULL; | |
54 delete [] prevMagSpec; | |
55 prevMagSpec = NULL; | |
56 delete [] phase; | |
57 phase = NULL; | |
58 delete [] prevPhase; | |
59 prevPhase = NULL; | |
60 delete [] prevPhase2; | |
61 prevPhase2 = NULL; | |
62 } | 49 } |
63 | 50 |
64 //======================================================================= | 51 //======================================================================= |
65 void OnsetDetectionFunction::initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType) | 52 void OnsetDetectionFunction::initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType) |
66 { | 53 { |
67 if (initialised == 1) // if we have already initialised some buffers and an FFT plan | 54 if (initialised) // if we have already initialised FFT plan |
68 { | 55 { |
69 ////////////////////////////////// | |
70 // TIDY UP FIRST - If initialise is called after the class has been initialised | |
71 // then we want to free up memory and cancel existing FFT plans | |
72 | |
73 // destroy fft plan | 56 // destroy fft plan |
74 fftw_destroy_plan(p); | 57 fftw_destroy_plan(p); |
75 fftw_free(complexIn); | 58 fftw_free(complexIn); |
76 fftw_free(complexOut); | 59 fftw_free(complexOut); |
77 | 60 |
78 | |
79 // deallocate memory | |
80 delete [] frame; | |
81 frame = NULL; | |
82 delete [] window; | |
83 window = NULL; | |
84 delete [] magSpec; | |
85 magSpec = NULL; | |
86 delete [] prevMagSpec; | |
87 prevMagSpec = NULL; | |
88 delete [] phase; | |
89 phase = NULL; | |
90 delete [] prevPhase; | |
91 prevPhase = NULL; | |
92 delete [] prevPhase2; | |
93 prevPhase2 = NULL; | |
94 | |
95 ////// END TIDY UP /////////////// | |
96 ////////////////////////////////// | |
97 } | 61 } |
98 | 62 |
99 hopSize = hopSize_; // set hopsize | 63 hopSize = hopSize_; // set hopsize |
100 frameSize = frameSize_; // set framesize | 64 frameSize = frameSize_; // set framesize |
101 | 65 |
102 onsetDetectionFunctionType = onsetDetectionFunctionType_; // set detection function type | 66 onsetDetectionFunctionType = onsetDetectionFunctionType_; // set detection function type |
103 | 67 |
104 // initialise buffers | 68 // initialise buffers |
105 frame = new double[frameSize]; | 69 frame.resize(frameSize); |
106 window = new double[frameSize]; | 70 window.resize(frameSize); |
107 | 71 magSpec.resize(frameSize); |
108 magSpec = new double[frameSize]; | 72 prevMagSpec.resize(frameSize); |
109 prevMagSpec = new double[frameSize]; | 73 phase.resize(frameSize); |
110 | 74 prevPhase.resize(frameSize); |
111 phase = new double[frameSize]; | 75 prevPhase2.resize(frameSize); |
112 prevPhase = new double[frameSize]; | |
113 prevPhase2 = new double[frameSize]; | |
114 | 76 |
115 | 77 |
116 // set the window to the specified type | 78 // set the window to the specified type |
117 switch (windowType){ | 79 switch (windowType){ |
118 case RectangularWindow: | 80 case RectangularWindow: |
132 break; | 94 break; |
133 default: | 95 default: |
134 calculateHanningWindow(); // DEFAULT: Hanning Window | 96 calculateHanningWindow(); // DEFAULT: Hanning Window |
135 } | 97 } |
136 | 98 |
137 | |
138 | |
139 | |
140 // initialise previous magnitude spectrum to zero | 99 // initialise previous magnitude spectrum to zero |
141 for (int i = 0;i < frameSize;i++) | 100 for (int i = 0;i < frameSize;i++) |
142 { | 101 { |
143 prevMagSpec[i] = 0.0; | 102 prevMagSpec[i] = 0.0; |
144 prevPhase[i] = 0.0; | 103 prevPhase[i] = 0.0; |
151 /* Init fft */ | 110 /* Init fft */ |
152 complexIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * frameSize); // complex array to hold fft data | 111 complexIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * frameSize); // complex array to hold fft data |
153 complexOut = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * frameSize); // complex array to hold fft data | 112 complexOut = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * frameSize); // complex array to hold fft data |
154 p = fftw_plan_dft_1d(frameSize, complexIn, complexOut, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation | 113 p = fftw_plan_dft_1d(frameSize, complexIn, complexOut, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation |
155 | 114 |
156 initialised = 1; | 115 initialised = true; |
157 } | 116 } |
158 | 117 |
159 //======================================================================= | 118 //======================================================================= |
160 void OnsetDetectionFunction :: setOnsetDetectionFunctionType(int onsetDetectionFunctionType_) | 119 void OnsetDetectionFunction :: setOnsetDetectionFunctionType(int onsetDetectionFunctionType_) |
161 { | 120 { |