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