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