Mercurial > hg > btrack
comparison src/OnsetDetectionFunction.h @ 36:5bd9ae503dcf master 1.0.0
flow: Merged <release> '1.0.0' to <master> ('master').
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Tue, 08 Jul 2014 12:32:27 +0100 |
parents | bddd59087c36 |
children | f6708e4c69f1 |
comparison
equal
deleted
inserted
replaced
13:0d29e68fa2ee | 36:5bd9ae503dcf |
---|---|
17 * You should have received a copy of the GNU General Public License | 17 * You should have received a copy of the GNU General Public License |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 */ | 19 */ |
20 //======================================================================= | 20 //======================================================================= |
21 | 21 |
22 #ifndef __RTONSETDF_H | 22 #ifndef __ONSETDETECTIONFUNCTION_H |
23 #define __RTONSETDF_H | 23 #define __ONSETDETECTIONFUNCTION_H |
24 | 24 |
25 #include "fftw3.h" | 25 #include "fftw3.h" |
26 #include <vector> | |
26 | 27 |
28 //======================================================================= | |
29 /** The type of onset detection function to calculate */ | |
30 enum OnsetDetectionFunctionType | |
31 { | |
32 EnergyEnvelope, | |
33 EnergyDifference, | |
34 SpectralDifference, | |
35 SpectralDifferenceHWR, | |
36 PhaseDeviation, | |
37 ComplexSpectralDifference, | |
38 ComplexSpectralDifferenceHWR, | |
39 HighFrequencyContent, | |
40 HighFrequencySpectralDifference, | |
41 HighFrequencySpectralDifferenceHWR | |
42 }; | |
43 | |
44 //======================================================================= | |
45 /** The type of window to use when calculating onset detection function samples */ | |
46 enum WindowType | |
47 { | |
48 RectangularWindow, | |
49 HanningWindow, | |
50 HammingWindow, | |
51 BlackmanWindow, | |
52 TukeyWindow | |
53 }; | |
54 | |
55 //======================================================================= | |
56 /** A class for calculating onset detection functions. */ | |
27 class OnsetDetectionFunction | 57 class OnsetDetectionFunction |
28 { | 58 { |
29 public: | 59 public: |
30 OnsetDetectionFunction(); // Constructor | 60 |
31 OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Constructor (with arguments) | 61 /** Constructor that defaults the onset detection function type to ComplexSpectralDifferenceHWR |
32 ~OnsetDetectionFunction(); // Destructor | 62 * and the window type to HanningWindow |
33 void initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Initialisation Function | 63 * @param hopSize_ the hop size in audio samples |
64 * @param frameSize_ the frame size in audio samples | |
65 */ | |
66 OnsetDetectionFunction(int hopSize_,int frameSize_); | |
67 | |
68 | |
69 /** Constructor | |
70 * @param hopSize_ the hop size in audio samples | |
71 * @param frameSize_ the frame size in audio samples | |
72 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) | |
73 * @param windowType the type of window to use (see WindowType) | |
74 */ | |
75 OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_); | |
76 | |
77 /** Destructor */ | |
78 ~OnsetDetectionFunction(); | |
79 | |
80 /** Initialisation function for only updating hop size and frame size (and not window type | |
81 * or onset detection function type | |
82 * @param hopSize_ the hop size in audio samples | |
83 * @param frameSize_ the frame size in audio samples | |
84 */ | |
85 void initialise(int hopSize_,int frameSize_); | |
86 | |
87 /** Initialisation Function | |
88 * @param hopSize_ the hop size in audio samples | |
89 * @param frameSize_ the frame size in audio samples | |
90 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) | |
91 * @param windowType the type of window to use (see WindowType) | |
92 */ | |
93 void initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_); | |
34 | 94 |
35 double getDFsample(double inputbuffer[]); // process input buffer and calculate detection function sample | 95 /** Process input frame and calculate detection function sample |
36 void set_df_type(int arg_df_type); // set the detection function type | 96 * @param buffer a pointer to an array containing the audio samples to be processed |
97 * @returns the onset detection function sample | |
98 */ | |
99 double calculateOnsetDetectionFunctionSample(double *buffer); | |
100 | |
101 /** Set the detection function type | |
102 * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) | |
103 */ | |
104 void setOnsetDetectionFunctionType(int onsetDetectionFunctionType_); | |
37 | 105 |
38 private: | 106 private: |
39 | 107 |
40 void perform_FFT(); // perform the FFT on the data in 'frame' | 108 /** Perform the FFT on the data in 'frame' */ |
109 void performFFT(); | |
41 | 110 |
42 double energy_envelope(); // calculate energy envelope detection function sample | 111 //======================================================================= |
43 double energy_difference(); // calculate energy difference detection function sample | 112 /** Calculate energy envelope detection function sample */ |
44 double spectral_difference(); // calculate spectral difference detection function sample | 113 double energyEnvelope(); |
45 double spectral_difference_hwr(); // calculate spectral difference (half wave rectified) detection function sample | 114 |
46 double phase_deviation(); // calculate phase deviation detection function sample | 115 /** Calculate energy difference detection function sample */ |
47 double complex_spectral_difference(); // calculate complex spectral difference detection function sample | 116 double energyDifference(); |
48 double complex_spectral_difference_hwr(); // calculate complex spectral difference detection function sample (half-wave rectified) | 117 |
49 double high_frequency_content(); // calculate high frequency content detection function sample | 118 /** Calculate spectral difference detection function sample */ |
50 double high_frequency_spectral_difference(); // calculate high frequency spectral difference detection function sample | 119 double spectralDifference(); |
51 double high_frequency_spectral_difference_hwr(); // calculate high frequency spectral difference detection function sample (half-wave rectified) | 120 |
121 /** Calculate spectral difference (half wave rectified) detection function sample */ | |
122 double spectralDifferenceHWR(); | |
123 | |
124 /** Calculate phase deviation detection function sample */ | |
125 double phaseDeviation(); | |
126 | |
127 /** Calculate complex spectral difference detection function sample */ | |
128 double complexSpectralDifference(); | |
129 | |
130 /** Calculate complex spectral difference detection function sample (half-wave rectified) */ | |
131 double complexSpectralDifferenceHWR(); | |
132 | |
133 /** Calculate high frequency content detection function sample */ | |
134 double highFrequencyContent(); | |
135 | |
136 /** Calculate high frequency spectral difference detection function sample */ | |
137 double highFrequencySpectralDifference(); | |
138 | |
139 /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */ | |
140 double highFrequencySpectralDifferenceHWR(); | |
52 | 141 |
53 void set_win_rectangular(); // calculate a Rectangular window | 142 //======================================================================= |
54 void set_win_hanning(); // calculate a Hanning window | 143 /** Calculate a Rectangular window */ |
55 void set_win_hamming(); // calculate a Hamming window | 144 void calculateRectangularWindow(); |
56 void set_win_blackman(); // calculate a Blackman window | 145 |
57 void set_win_tukey(); // calculate a Tukey window | 146 /** Calculate a Hanning window */ |
147 void calculateHanningWindow(); | |
148 | |
149 /** Calculate a Hamming window */ | |
150 void calclulateHammingWindow(); | |
151 | |
152 /** Calculate a Blackman window */ | |
153 void calculateBlackmanWindow(); | |
154 | |
155 /** Calculate a Tukey window */ | |
156 void calculateTukeyWindow(); | |
58 | 157 |
59 | 158 //======================================================================= |
60 double princarg(double phaseval); // set phase values between [-pi, pi] | 159 /** Set phase values between [-pi, pi] |
160 * @param phaseVal the phase value to process | |
161 * @returns the wrapped phase value | |
162 */ | |
163 double princarg(double phaseVal); | |
61 | 164 |
62 | 165 |
63 double pi; // pi, the constant | 166 double pi; /**< pi, the constant */ |
64 | 167 |
65 int framesize; // audio framesize | 168 int frameSize; /**< audio framesize */ |
66 int hopsize; // audio hopsize | 169 int hopSize; /**< audio hopsize */ |
67 int df_type; // type of detection function | 170 int onsetDetectionFunctionType; /**< type of detection function */ |
171 int windowType; /**< type of window used in calculations */ | |
68 | 172 |
69 fftw_plan p; // create fft plan | 173 fftw_plan p; /**< fftw plan */ |
70 fftw_complex *in; // to hold complex fft values for input | 174 fftw_complex *complexIn; /**< to hold complex fft values for input */ |
71 fftw_complex *out; // to hold complex fft values for output | 175 fftw_complex *complexOut; /**< to hold complex fft values for output */ |
72 | 176 |
73 int initialised; // flag indicating whether buffers and FFT plans have been initialised | 177 bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */ |
178 | |
179 std::vector<double> frame; /**< audio frame */ | |
180 std::vector<double> window; /**< window */ | |
74 | 181 |
75 | 182 double prevEnergySum; /**< to hold the previous energy sum value */ |
76 double *frame; // audio frame | |
77 double *window; // window | |
78 double *wframe; // windowed frame | |
79 | 183 |
80 double energy_sum_old; // to hold the previous energy sum value | 184 std::vector<double> magSpec; /**< magnitude spectrum */ |
185 std::vector<double> prevMagSpec; /**< previous magnitude spectrum */ | |
81 | 186 |
82 double *mag; // magnitude spectrum | 187 std::vector<double> phase; /**< FFT phase values */ |
83 double *mag_old; // previous magnitude spectrum | 188 std::vector<double> prevPhase; /**< previous phase values */ |
84 | 189 std::vector<double> prevPhase2; /**< second order previous phase values */ |
85 double *phase; // FFT phase values | |
86 double *phase_old; // previous phase values | |
87 double *phase_old_2; // second order previous phase values | |
88 | 190 |
89 }; | 191 }; |
90 | 192 |
91 | 193 |
92 #endif | 194 #endif |