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@38
|
25 #include "fftw3.h"
|
adamstark@64
|
26 #include <vector>
|
adamstark@38
|
27
|
adamstark@57
|
28 //=======================================================================
|
adamstark@60
|
29 /** The type of onset detection function to calculate */
|
adamstark@57
|
30 enum OnsetDetectionFunctionType
|
adamstark@57
|
31 {
|
adamstark@57
|
32 EnergyEnvelope,
|
adamstark@57
|
33 EnergyDifference,
|
adamstark@57
|
34 SpectralDifference,
|
adamstark@57
|
35 SpectralDifferenceHWR,
|
adamstark@57
|
36 PhaseDeviation,
|
adamstark@57
|
37 ComplexSpectralDifference,
|
adamstark@57
|
38 ComplexSpectralDifferenceHWR,
|
adamstark@57
|
39 HighFrequencyContent,
|
adamstark@57
|
40 HighFrequencySpectralDifference,
|
adamstark@57
|
41 HighFrequencySpectralDifferenceHWR
|
adamstark@57
|
42 };
|
adamstark@57
|
43
|
adamstark@57
|
44 //=======================================================================
|
adamstark@60
|
45 /** The type of window to use when calculating onset detection function samples */
|
adamstark@57
|
46 enum WindowType
|
adamstark@57
|
47 {
|
adamstark@57
|
48 RectangularWindow,
|
adamstark@57
|
49 HanningWindow,
|
adamstark@57
|
50 HammingWindow,
|
adamstark@57
|
51 BlackmanWindow,
|
adamstark@57
|
52 TukeyWindow
|
adamstark@57
|
53 };
|
adamstark@57
|
54
|
adamstark@61
|
55 //=======================================================================
|
adamstark@61
|
56 /** A class for calculating onset detection functions. */
|
adamstark@38
|
57 class OnsetDetectionFunction
|
adamstark@38
|
58 {
|
adamstark@38
|
59 public:
|
adamstark@52
|
60
|
adamstark@66
|
61 /** Constructor that defaults the onset detection function type to ComplexSpectralDifferenceHWR
|
adamstark@66
|
62 * and the window type to HanningWindow
|
adamstark@66
|
63 * @param hopSize_ the hop size in audio samples
|
adamstark@66
|
64 * @param frameSize_ the frame size in audio samples
|
adamstark@66
|
65 */
|
adamstark@92
|
66 OnsetDetectionFunction (int hopSize_, int frameSize_);
|
adamstark@66
|
67
|
adamstark@66
|
68
|
adamstark@60
|
69 /** Constructor
|
adamstark@60
|
70 * @param hopSize_ the hop size in audio samples
|
adamstark@60
|
71 * @param frameSize_ the frame size in audio samples
|
adamstark@60
|
72 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
73 * @param windowType the type of window to use (see WindowType)
|
adamstark@60
|
74 */
|
adamstark@92
|
75 OnsetDetectionFunction (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_);
|
adamstark@52
|
76
|
adamstark@52
|
77 /** Destructor */
|
adamstark@52
|
78 ~OnsetDetectionFunction();
|
adamstark@52
|
79
|
adamstark@66
|
80 /** Initialisation function for only updating hop size and frame size (and not window type
|
adamstark@66
|
81 * or onset detection function type
|
adamstark@66
|
82 * @param hopSize_ the hop size in audio samples
|
adamstark@66
|
83 * @param frameSize_ the frame size in audio samples
|
adamstark@66
|
84 */
|
adamstark@92
|
85 void initialise (int hopSize_, int frameSize_);
|
adamstark@66
|
86
|
adamstark@60
|
87 /** Initialisation Function
|
adamstark@60
|
88 * @param hopSize_ the hop size in audio samples
|
adamstark@60
|
89 * @param frameSize_ the frame size in audio samples
|
adamstark@60
|
90 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
91 * @param windowType the type of window to use (see WindowType)
|
adamstark@60
|
92 */
|
adamstark@92
|
93 void initialise (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_);
|
adamstark@38
|
94
|
adamstark@60
|
95 /** Process input frame and calculate detection function sample
|
adamstark@60
|
96 * @param buffer a pointer to an array containing the audio samples to be processed
|
adamstark@60
|
97 * @returns the onset detection function sample
|
adamstark@60
|
98 */
|
adamstark@92
|
99 double calculateOnsetDetectionFunctionSample (double* buffer);
|
adamstark@52
|
100
|
adamstark@60
|
101 /** Set the detection function type
|
adamstark@60
|
102 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
|
adamstark@60
|
103 */
|
adamstark@92
|
104 void setOnsetDetectionFunctionType (int onsetDetectionFunctionType_);
|
adamstark@38
|
105
|
adamstark@38
|
106 private:
|
adamstark@38
|
107
|
adamstark@60
|
108 /** Perform the FFT on the data in 'frame' */
|
adamstark@59
|
109 void performFFT();
|
adamstark@38
|
110
|
adamstark@59
|
111 //=======================================================================
|
adamstark@60
|
112 /** Calculate energy envelope detection function sample */
|
adamstark@59
|
113 double energyEnvelope();
|
adamstark@52
|
114
|
adamstark@60
|
115 /** Calculate energy difference detection function sample */
|
adamstark@59
|
116 double energyDifference();
|
adamstark@52
|
117
|
adamstark@60
|
118 /** Calculate spectral difference detection function sample */
|
adamstark@59
|
119 double spectralDifference();
|
adamstark@52
|
120
|
adamstark@60
|
121 /** Calculate spectral difference (half wave rectified) detection function sample */
|
adamstark@59
|
122 double spectralDifferenceHWR();
|
adamstark@52
|
123
|
adamstark@60
|
124 /** Calculate phase deviation detection function sample */
|
adamstark@59
|
125 double phaseDeviation();
|
adamstark@52
|
126
|
adamstark@60
|
127 /** Calculate complex spectral difference detection function sample */
|
adamstark@59
|
128 double complexSpectralDifference();
|
adamstark@52
|
129
|
adamstark@60
|
130 /** Calculate complex spectral difference detection function sample (half-wave rectified) */
|
adamstark@59
|
131 double complexSpectralDifferenceHWR();
|
adamstark@52
|
132
|
adamstark@60
|
133 /** Calculate high frequency content detection function sample */
|
adamstark@59
|
134 double highFrequencyContent();
|
adamstark@52
|
135
|
adamstark@60
|
136 /** Calculate high frequency spectral difference detection function sample */
|
adamstark@59
|
137 double highFrequencySpectralDifference();
|
adamstark@52
|
138
|
adamstark@60
|
139 /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */
|
adamstark@59
|
140 double highFrequencySpectralDifferenceHWR();
|
adamstark@38
|
141
|
adamstark@59
|
142 //=======================================================================
|
adamstark@60
|
143 /** Calculate a Rectangular window */
|
adamstark@59
|
144 void calculateRectangularWindow();
|
adamstark@52
|
145
|
adamstark@60
|
146 /** Calculate a Hanning window */
|
adamstark@59
|
147 void calculateHanningWindow();
|
adamstark@52
|
148
|
adamstark@60
|
149 /** Calculate a Hamming window */
|
adamstark@59
|
150 void calclulateHammingWindow();
|
adamstark@52
|
151
|
adamstark@60
|
152 /** Calculate a Blackman window */
|
adamstark@59
|
153 void calculateBlackmanWindow();
|
adamstark@52
|
154
|
adamstark@60
|
155 /** Calculate a Tukey window */
|
adamstark@59
|
156 void calculateTukeyWindow();
|
adamstark@38
|
157
|
adamstark@59
|
158 //=======================================================================
|
adamstark@60
|
159 /** Set phase values between [-pi, pi]
|
adamstark@60
|
160 * @param phaseVal the phase value to process
|
adamstark@60
|
161 * @returns the wrapped phase value
|
adamstark@60
|
162 */
|
adamstark@59
|
163 double princarg(double phaseVal);
|
adamstark@38
|
164
|
adamstark@38
|
165
|
adamstark@52
|
166 double pi; /**< pi, the constant */
|
adamstark@38
|
167
|
adamstark@59
|
168 int frameSize; /**< audio framesize */
|
adamstark@59
|
169 int hopSize; /**< audio hopsize */
|
adamstark@59
|
170 int onsetDetectionFunctionType; /**< type of detection function */
|
adamstark@66
|
171 int windowType; /**< type of window used in calculations */
|
adamstark@38
|
172
|
adamstark@59
|
173 fftw_plan p; /**< fftw plan */
|
adamstark@92
|
174 fftw_complex* complexIn; /**< to hold complex fft values for input */
|
adamstark@92
|
175 fftw_complex* complexOut; /**< to hold complex fft values for output */
|
adamstark@38
|
176
|
adamstark@64
|
177 bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */
|
adamstark@52
|
178
|
adamstark@64
|
179 std::vector<double> frame; /**< audio frame */
|
adamstark@64
|
180 std::vector<double> window; /**< window */
|
adamstark@38
|
181
|
adamstark@59
|
182 double prevEnergySum; /**< to hold the previous energy sum value */
|
adamstark@38
|
183
|
adamstark@64
|
184 std::vector<double> magSpec; /**< magnitude spectrum */
|
adamstark@64
|
185 std::vector<double> prevMagSpec; /**< previous magnitude spectrum */
|
adamstark@38
|
186
|
adamstark@64
|
187 std::vector<double> phase; /**< FFT phase values */
|
adamstark@64
|
188 std::vector<double> prevPhase; /**< previous phase values */
|
adamstark@64
|
189 std::vector<double> prevPhase2; /**< second order previous phase values */
|
adamstark@38
|
190
|
adamstark@38
|
191 };
|
adamstark@38
|
192
|
adamstark@38
|
193
|
adamstark@38
|
194 #endif |