adamstark@38
|
1 //=======================================================================
|
adamstark@38
|
2 /** @file OnsetDetectionFunction.h
|
adamstark@38
|
3 * @brief A class for calculating onset detection functions
|
adamstark@38
|
4 * @author Adam Stark
|
adamstark@38
|
5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
|
adamstark@38
|
6 *
|
adamstark@38
|
7 * This program is free software: you can redistribute it and/or modify
|
adamstark@38
|
8 * it under the terms of the GNU General Public License as published by
|
adamstark@38
|
9 * the Free Software Foundation, either version 3 of the License, or
|
adamstark@38
|
10 * (at your option) any later version.
|
adamstark@38
|
11 *
|
adamstark@38
|
12 * This program is distributed in the hope that it will be useful,
|
adamstark@38
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
adamstark@38
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
adamstark@38
|
15 * GNU General Public License for more details.
|
adamstark@38
|
16 *
|
adamstark@38
|
17 * You should have received a copy of the GNU General Public License
|
adamstark@38
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
adamstark@38
|
19 */
|
adamstark@38
|
20 //=======================================================================
|
adamstark@38
|
21
|
adamstark@59
|
22 #ifndef __ONSETDETECTIONFUNCTION_H
|
adamstark@59
|
23 #define __ONSETDETECTIONFUNCTION_H
|
adamstark@38
|
24
|
adamstark@93
|
25 #ifdef USE_FFTW
|
adamstark@38
|
26 #include "fftw3.h"
|
adamstark@93
|
27 #endif
|
adamstark@93
|
28
|
adamstark@93
|
29 #ifdef USE_KISS_FFT
|
adamstark@93
|
30 #include "kiss_fft.h"
|
adamstark@93
|
31 #endif
|
adamstark@93
|
32
|
adamstark@64
|
33 #include <vector>
|
adamstark@38
|
34
|
adamstark@57
|
35 //=======================================================================
|
adamstark@60
|
36 /** The type of onset detection function to calculate */
|
adamstark@57
|
37 enum OnsetDetectionFunctionType
|
adamstark@57
|
38 {
|
adamstark@57
|
39 EnergyEnvelope,
|
adamstark@57
|
40 EnergyDifference,
|
adamstark@57
|
41 SpectralDifference,
|
adamstark@57
|
42 SpectralDifferenceHWR,
|
adamstark@57
|
43 PhaseDeviation,
|
adamstark@57
|
44 ComplexSpectralDifference,
|
adamstark@57
|
45 ComplexSpectralDifferenceHWR,
|
adamstark@57
|
46 HighFrequencyContent,
|
adamstark@57
|
47 HighFrequencySpectralDifference,
|
adamstark@57
|
48 HighFrequencySpectralDifferenceHWR
|
adamstark@57
|
49 };
|
adamstark@57
|
50
|
adamstark@57
|
51 //=======================================================================
|
adamstark@60
|
52 /** The type of window to use when calculating onset detection function samples */
|
adamstark@57
|
53 enum WindowType
|
adamstark@57
|
54 {
|
adamstark@57
|
55 RectangularWindow,
|
adamstark@57
|
56 HanningWindow,
|
adamstark@57
|
57 HammingWindow,
|
adamstark@57
|
58 BlackmanWindow,
|
adamstark@57
|
59 TukeyWindow
|
adamstark@57
|
60 };
|
adamstark@57
|
61
|
adamstark@61
|
62 //=======================================================================
|
adamstark@61
|
63 /** A class for calculating onset detection functions. */
|
adamstark@38
|
64 class OnsetDetectionFunction
|
adamstark@38
|
65 {
|
adamstark@38
|
66 public:
|
adamstark@52
|
67
|
adamstark@66
|
68 /** Constructor that defaults the onset detection function type to ComplexSpectralDifferenceHWR
|
adamstark@66
|
69 * and the window type to HanningWindow
|
adamstark@66
|
70 * @param hopSize_ the hop size in audio samples
|
adamstark@66
|
71 * @param frameSize_ the frame size in audio samples
|
adamstark@66
|
72 */
|
adamstark@111
|
73 OnsetDetectionFunction (int hopSize, int frameSize);
|
adamstark@66
|
74
|
adamstark@66
|
75
|
adamstark@60
|
76 /** Constructor
|
adamstark@60
|
77 * @param hopSize_ the hop size in audio samples
|
adamstark@60
|
78 * @param frameSize_ the frame size in audio samples
|
adamstark@60
|
79 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
80 * @param windowType the type of window to use (see WindowType)
|
adamstark@60
|
81 */
|
adamstark@111
|
82 OnsetDetectionFunction (int hopSize, int frameSize, int onsetDetectionFunctionType, int windowType);
|
adamstark@52
|
83
|
adamstark@52
|
84 /** Destructor */
|
adamstark@52
|
85 ~OnsetDetectionFunction();
|
adamstark@52
|
86
|
adamstark@66
|
87 /** Initialisation function for only updating hop size and frame size (and not window type
|
adamstark@66
|
88 * or onset detection function type
|
adamstark@66
|
89 * @param hopSize_ the hop size in audio samples
|
adamstark@66
|
90 * @param frameSize_ the frame size in audio samples
|
adamstark@66
|
91 */
|
adamstark@111
|
92 void initialise (int hopSize, int frameSize);
|
adamstark@66
|
93
|
adamstark@60
|
94 /** Initialisation Function
|
adamstark@60
|
95 * @param hopSize_ the hop size in audio samples
|
adamstark@60
|
96 * @param frameSize_ the frame size in audio samples
|
adamstark@60
|
97 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
98 * @param windowType the type of window to use (see WindowType)
|
adamstark@60
|
99 */
|
adamstark@111
|
100 void initialise (int hopSize, int frameSize, int onsetDetectionFunctionType, int windowType);
|
adamstark@38
|
101
|
adamstark@60
|
102 /** Process input frame and calculate detection function sample
|
adamstark@60
|
103 * @param buffer a pointer to an array containing the audio samples to be processed
|
adamstark@60
|
104 * @returns the onset detection function sample
|
adamstark@60
|
105 */
|
adamstark@92
|
106 double calculateOnsetDetectionFunctionSample (double* buffer);
|
adamstark@52
|
107
|
adamstark@60
|
108 /** Set the detection function type
|
adamstark@60
|
109 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
110 */
|
adamstark@111
|
111 void setOnsetDetectionFunctionType (int onsetDetectionFunctionType);
|
adamstark@38
|
112
|
adamstark@38
|
113 private:
|
adamstark@38
|
114
|
adamstark@60
|
115 /** Perform the FFT on the data in 'frame' */
|
adamstark@59
|
116 void performFFT();
|
adamstark@38
|
117
|
adamstark@59
|
118 //=======================================================================
|
adamstark@60
|
119 /** Calculate energy envelope detection function sample */
|
adamstark@59
|
120 double energyEnvelope();
|
adamstark@52
|
121
|
adamstark@60
|
122 /** Calculate energy difference detection function sample */
|
adamstark@59
|
123 double energyDifference();
|
adamstark@52
|
124
|
adamstark@60
|
125 /** Calculate spectral difference detection function sample */
|
adamstark@59
|
126 double spectralDifference();
|
adamstark@52
|
127
|
adamstark@60
|
128 /** Calculate spectral difference (half wave rectified) detection function sample */
|
adamstark@59
|
129 double spectralDifferenceHWR();
|
adamstark@52
|
130
|
adamstark@60
|
131 /** Calculate phase deviation detection function sample */
|
adamstark@59
|
132 double phaseDeviation();
|
adamstark@52
|
133
|
adamstark@60
|
134 /** Calculate complex spectral difference detection function sample */
|
adamstark@59
|
135 double complexSpectralDifference();
|
adamstark@52
|
136
|
adamstark@60
|
137 /** Calculate complex spectral difference detection function sample (half-wave rectified) */
|
adamstark@59
|
138 double complexSpectralDifferenceHWR();
|
adamstark@52
|
139
|
adamstark@60
|
140 /** Calculate high frequency content detection function sample */
|
adamstark@59
|
141 double highFrequencyContent();
|
adamstark@52
|
142
|
adamstark@60
|
143 /** Calculate high frequency spectral difference detection function sample */
|
adamstark@59
|
144 double highFrequencySpectralDifference();
|
adamstark@52
|
145
|
adamstark@60
|
146 /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */
|
adamstark@59
|
147 double highFrequencySpectralDifferenceHWR();
|
adamstark@38
|
148
|
adamstark@59
|
149 //=======================================================================
|
adamstark@60
|
150 /** Calculate a Rectangular window */
|
adamstark@59
|
151 void calculateRectangularWindow();
|
adamstark@52
|
152
|
adamstark@60
|
153 /** Calculate a Hanning window */
|
adamstark@59
|
154 void calculateHanningWindow();
|
adamstark@52
|
155
|
adamstark@60
|
156 /** Calculate a Hamming window */
|
adamstark@59
|
157 void calclulateHammingWindow();
|
adamstark@52
|
158
|
adamstark@60
|
159 /** Calculate a Blackman window */
|
adamstark@59
|
160 void calculateBlackmanWindow();
|
adamstark@52
|
161
|
adamstark@60
|
162 /** Calculate a Tukey window */
|
adamstark@59
|
163 void calculateTukeyWindow();
|
adamstark@38
|
164
|
adamstark@59
|
165 //=======================================================================
|
adamstark@60
|
166 /** Set phase values between [-pi, pi]
|
adamstark@60
|
167 * @param phaseVal the phase value to process
|
adamstark@60
|
168 * @returns the wrapped phase value
|
adamstark@60
|
169 */
|
adamstark@59
|
170 double princarg(double phaseVal);
|
adamstark@38
|
171
|
adamstark@93
|
172 void initialiseFFT();
|
adamstark@93
|
173 void freeFFT();
|
adamstark@38
|
174
|
adamstark@52
|
175 double pi; /**< pi, the constant */
|
adamstark@38
|
176
|
adamstark@59
|
177 int frameSize; /**< audio framesize */
|
adamstark@59
|
178 int hopSize; /**< audio hopsize */
|
adamstark@59
|
179 int onsetDetectionFunctionType; /**< type of detection function */
|
adamstark@66
|
180 int windowType; /**< type of window used in calculations */
|
adamstark@93
|
181
|
adamstark@93
|
182 //=======================================================================
|
adamstark@93
|
183 #ifdef USE_FFTW
|
adamstark@59
|
184 fftw_plan p; /**< fftw plan */
|
adamstark@92
|
185 fftw_complex* complexIn; /**< to hold complex fft values for input */
|
adamstark@92
|
186 fftw_complex* complexOut; /**< to hold complex fft values for output */
|
adamstark@93
|
187 #endif
|
adamstark@93
|
188
|
adamstark@93
|
189 #ifdef USE_KISS_FFT
|
adamstark@93
|
190 kiss_fft_cfg cfg; /**< Kiss FFT configuration */
|
adamstark@93
|
191 kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
|
adamstark@93
|
192 kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
|
adamstark@93
|
193 std::vector<std::vector<double> > complexOut;
|
adamstark@93
|
194 #endif
|
adamstark@38
|
195
|
adamstark@93
|
196 //=======================================================================
|
adamstark@64
|
197 bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */
|
adamstark@52
|
198
|
adamstark@64
|
199 std::vector<double> frame; /**< audio frame */
|
adamstark@64
|
200 std::vector<double> window; /**< window */
|
adamstark@38
|
201
|
adamstark@59
|
202 double prevEnergySum; /**< to hold the previous energy sum value */
|
adamstark@38
|
203
|
adamstark@64
|
204 std::vector<double> magSpec; /**< magnitude spectrum */
|
adamstark@64
|
205 std::vector<double> prevMagSpec; /**< previous magnitude spectrum */
|
adamstark@38
|
206
|
adamstark@64
|
207 std::vector<double> phase; /**< FFT phase values */
|
adamstark@64
|
208 std::vector<double> prevPhase; /**< previous phase values */
|
adamstark@64
|
209 std::vector<double> prevPhase2; /**< second order previous phase values */
|
adamstark@38
|
210 };
|
adamstark@38
|
211
|
adamstark@38
|
212
|
adamstark@111
|
213 #endif
|