andrew@0
|
1 /*
|
andrew@0
|
2 * AubioOnsetDetector.cpp
|
andrew@0
|
3 * aubioonset~
|
andrew@0
|
4 *
|
andrew@0
|
5 * Created by Andrew Robertson on 13/08/2010.
|
andrew@0
|
6 * Copyright 2010 __MyCompanyName__. All rights reserved.
|
andrew@0
|
7 *
|
andrew@0
|
8 */
|
andrew@0
|
9
|
andrew@0
|
10 #include "AubioOnsetDetector.h"
|
andrew@0
|
11
|
andrew@0
|
12 AubioOnsetDetector :: AubioOnsetDetector(){
|
andrew@0
|
13 buffersize = 1024;
|
andrew@0
|
14 hopsize = 512;
|
andrew@0
|
15 //aubio related setup
|
andrew@0
|
16 o = new_aubio_onsetdetection(aubio_onset_complex, buffersize, 1);//initially in complex mode
|
andrew@0
|
17 pv = (aubio_pvoc_t *)new_aubio_pvoc(buffersize, hopsize, 1);
|
andrew@0
|
18 parms = new_aubio_peakpicker(threshold);
|
andrew@0
|
19 vec = (fvec_t *)new_fvec(hopsize,1);
|
andrew@0
|
20
|
andrew@0
|
21 threshold = 1;
|
andrew@0
|
22 threshold2 = -70.;
|
andrew@0
|
23
|
andrew@0
|
24 resetValues();
|
andrew@0
|
25 thresholdRelativeToMedian = 1.3;
|
andrew@0
|
26 cutoffForRepeatOnsetsMillis = 100;
|
andrew@0
|
27 medianSpeed = 10;
|
andrew@0
|
28 pos = 0;
|
andrew@0
|
29 }
|
andrew@0
|
30
|
andrew@0
|
31 AubioOnsetDetector :: ~AubioOnsetDetector(){
|
andrew@0
|
32 aubio_onsetdetection_free (o);
|
andrew@0
|
33
|
andrew@0
|
34 }
|
andrew@0
|
35
|
andrew@0
|
36 void AubioOnsetDetector :: resetValues(){
|
andrew@0
|
37 rawDetectionValue = 1;
|
andrew@0
|
38 peakPickedDetectionValue = 1;
|
andrew@0
|
39 medianDetectionValue = 1;
|
andrew@0
|
40 lastMedianOnsetFrame = 0;
|
andrew@0
|
41 currentFrame = 0;
|
andrew@0
|
42 aubioLongTermAverage = 1;
|
andrew@2
|
43 lastDfValue = 0;
|
andrew@2
|
44 bestSlopeValue = 0;
|
andrew@2
|
45 recentValueIndex = 0;
|
andrew@2
|
46 lastSlopeOnsetFrame = 0;
|
andrew@2
|
47 bestSlopeMedian = 10;
|
andrew@2
|
48 slopeFallenBelowMedian = true;
|
andrew@2
|
49
|
andrew@2
|
50 for (int i = 0;i< numberOfDetectionValues;i++)
|
andrew@2
|
51 recentRawDetectionValues[i] = 1;
|
andrew@0
|
52
|
andrew@0
|
53 }
|
andrew@0
|
54
|
andrew@0
|
55
|
andrew@0
|
56 void AubioOnsetDetector :: initialise(){
|
andrew@0
|
57 //reinitialises our object
|
andrew@0
|
58 o = new_aubio_onsetdetection(aubio_onset_complex, buffersize, 1);//initially in complex mode
|
andrew@0
|
59 pv = (aubio_pvoc_t *)new_aubio_pvoc(buffersize, hopsize, 1);
|
andrew@0
|
60 parms = new_aubio_peakpicker(threshold);
|
andrew@0
|
61 vec = (fvec_t *)new_fvec(hopsize,1);
|
andrew@0
|
62 pos = 0;
|
andrew@0
|
63 fvec_write_sample(vec, 0.234, 0, pos);
|
andrew@0
|
64 fftgrain = (cvec_t *)new_cvec(buffersize,1);
|
andrew@0
|
65 onset = (fvec_t *)new_fvec(1,1);
|
andrew@0
|
66 }
|
andrew@0
|
67
|
andrew@0
|
68 bool AubioOnsetDetector :: processframe(float frame[], int n){
|
andrew@0
|
69 bool newFrameResult = false;
|
andrew@0
|
70 //Paul Brossier's aubioonsetclass~ code ported from Pd
|
andrew@0
|
71 int j,isonset;
|
andrew@0
|
72 for (j=0;j<n;j++) {
|
andrew@0
|
73 // write input to datanew
|
andrew@0
|
74 fvec_write_sample(vec, frame[j], 0, pos);//vec->data[0][pos] = frame[j]
|
andrew@0
|
75 //time for fft
|
andrew@0
|
76
|
andrew@0
|
77 if (pos == hopsize-1) { //hopsize is 512
|
andrew@0
|
78 newFrameResult = true;
|
andrew@0
|
79 aubioOnsetFound = false;
|
andrew@0
|
80 // block loop
|
andrew@0
|
81 aubio_pvoc_do (pv,vec, fftgrain);
|
andrew@0
|
82
|
andrew@0
|
83 fftgrain->norm[0][0] = fabs(fftgrain->norm[0][0]);
|
andrew@0
|
84 //added hack to solve bug that norm[0][0] is negative sometimes.
|
andrew@0
|
85
|
andrew@0
|
86 aubio_onsetdetection(o, fftgrain, onset);
|
andrew@0
|
87 rawDetectionValue = onset->data[0][0];
|
andrew@0
|
88 //Paul Brossier's method to return value of peak picking process
|
andrew@0
|
89
|
andrew@0
|
90 anrMedianProcessedOnsetFound = checkForMedianOnset(rawDetectionValue);
|
andrew@0
|
91
|
andrew@2
|
92 bestSlopeValue = getBestSlopeValue(rawDetectionValue);
|
andrew@2
|
93 anrBestSlopeOnset = checkForSlopeOnset(bestSlopeValue);
|
andrew@2
|
94
|
andrew@0
|
95 // smpl_t my_sample_value;
|
andrew@0
|
96 peakPickedDetectionValue = aubio_peakpick_pimrt_getval(parms);
|
andrew@0
|
97 //peakPickedDetectionValue = my_sample_value;
|
andrew@0
|
98
|
andrew@0
|
99 //this was what got sent from max object::
|
andrew@0
|
100 // outlet_float(x->detectionFunctionOutlet, my_sample_value);
|
andrew@0
|
101 // outlet_float(x->rawDetectionFunctionOutlet, x->onset->data[0][0]);
|
andrew@0
|
102
|
andrew@0
|
103 isonset = aubio_peakpick_pimrt(onset,parms);
|
andrew@0
|
104 if (isonset) {
|
andrew@0
|
105 // test for silence
|
andrew@0
|
106 if (aubio_silence_detection(vec, threshold2)==1)
|
andrew@0
|
107 {
|
andrew@0
|
108 isonset=0;
|
andrew@0
|
109 }
|
andrew@0
|
110 else{
|
andrew@0
|
111 // outlet_bang(x->bangoutlet);
|
andrew@0
|
112 aubioOnsetFound = true;
|
andrew@0
|
113
|
andrew@0
|
114 }
|
andrew@0
|
115 }//end if (isonset)
|
andrew@0
|
116
|
andrew@0
|
117
|
andrew@0
|
118
|
andrew@0
|
119 // end of block loop
|
andrew@0
|
120 pos = -1; // so it will be zero next j loop
|
andrew@0
|
121 }
|
andrew@0
|
122 pos++;
|
andrew@0
|
123 // outL[j] = frame[j];//have added this so signal is "see through": outputting the input signal
|
andrew@0
|
124
|
andrew@0
|
125 }
|
andrew@0
|
126 //end of Paul's code
|
andrew@0
|
127
|
andrew@0
|
128 return newFrameResult;
|
andrew@0
|
129
|
andrew@0
|
130 }
|
andrew@0
|
131
|
andrew@0
|
132
|
andrew@0
|
133 bool AubioOnsetDetector :: checkForMedianOnset(float dfvalue){
|
andrew@0
|
134 bool onsetDetected = false;
|
andrew@0
|
135 //check for onset relative to our rising and falling median threshold
|
andrew@0
|
136 if (dfvalue > medianDetectionValue * thresholdRelativeToMedian &&
|
andrew@0
|
137 dfvalue > aubioLongTermAverage &&
|
andrew@2
|
138 //lastDfValue < medianDetectionValue &&
|
andrew@0
|
139 1000*framesToSeconds(currentFrame - lastMedianOnsetFrame) > cutoffForRepeatOnsetsMillis){
|
andrew@2
|
140 printf("frame diff between onsets %6.1f", (1000*framesToSeconds(currentFrame - lastMedianOnsetFrame)) );
|
andrew@0
|
141 onsetDetected = true;
|
andrew@2
|
142 lastMedianOnsetFrame = currentFrame;
|
andrew@0
|
143 }
|
andrew@0
|
144
|
andrew@0
|
145 aubioLongTermAverage *= 0.999;
|
andrew@0
|
146 aubioLongTermAverage += 0.001*(dfvalue - aubioLongTermAverage);
|
andrew@0
|
147
|
andrew@0
|
148 if (dfvalue > medianDetectionValue)
|
andrew@0
|
149 medianDetectionValue = dfvalue;
|
andrew@0
|
150 else
|
andrew@0
|
151 medianDetectionValue += 0.01*medianSpeed*(dfvalue - medianDetectionValue);
|
andrew@0
|
152
|
andrew@0
|
153 currentFrame++;
|
andrew@2
|
154 lastDfValue = dfvalue;
|
andrew@2
|
155
|
andrew@0
|
156
|
andrew@0
|
157 return onsetDetected;
|
andrew@0
|
158 }
|
andrew@0
|
159
|
andrew@2
|
160 double AubioOnsetDetector::getBestSlopeValue(float dfvalue){
|
andrew@2
|
161 //the idea is we want a high slope
|
andrew@2
|
162 recentRawDetectionValues[recentValueIndex] = dfvalue;
|
andrew@2
|
163 double bestValue = 0;
|
andrew@2
|
164 for (int i = 1;i < numberOfDetectionValues;i++){
|
andrew@2
|
165 double angle = 0;
|
andrew@2
|
166 int otherIndex = (recentValueIndex - i + numberOfDetectionValues)%numberOfDetectionValues;
|
andrew@2
|
167 double testValue = 0;
|
andrew@2
|
168 if (otherIndex > 0 && recentRawDetectionValues[otherIndex] > 0){
|
andrew@2
|
169 angle = atan((float)(i * dfvalue)/ (numberOfDetectionValues*(dfvalue-recentRawDetectionValues[otherIndex])) );
|
andrew@2
|
170 testValue = (dfvalue - recentRawDetectionValues[otherIndex]) * cos(angle);
|
andrew@2
|
171 }
|
andrew@2
|
172
|
andrew@2
|
173 if (testValue > bestValue)
|
andrew@2
|
174 bestValue = testValue;
|
andrew@2
|
175 }
|
andrew@2
|
176
|
andrew@2
|
177 recentValueIndex++;
|
andrew@2
|
178
|
andrew@2
|
179 if (recentValueIndex == numberOfDetectionValues)
|
andrew@2
|
180 recentValueIndex = 0;
|
andrew@2
|
181
|
andrew@2
|
182
|
andrew@2
|
183 return bestValue;
|
andrew@2
|
184
|
andrew@2
|
185 }
|
andrew@2
|
186
|
andrew@2
|
187
|
andrew@2
|
188
|
andrew@2
|
189
|
andrew@2
|
190 bool AubioOnsetDetector :: checkForSlopeOnset(float bestValue){
|
andrew@2
|
191 bool onsetDetected = false;
|
andrew@2
|
192 //check for onset relative to our processed slope function
|
andrew@2
|
193 //a mix between increase in value and the gradient of that increase
|
andrew@2
|
194
|
andrew@2
|
195 if (bestValue > bestSlopeMedian * thresholdRelativeToMedian &&
|
andrew@2
|
196 1000*framesToSeconds(currentFrame - lastSlopeOnsetFrame) > cutoffForRepeatOnsetsMillis
|
andrew@2
|
197 && slopeFallenBelowMedian
|
andrew@2
|
198 ){
|
andrew@2
|
199 printf("frame diff between onsets %6.1f", (1000*framesToSeconds(currentFrame - lastMedianOnsetFrame)) );
|
andrew@2
|
200 onsetDetected = true;
|
andrew@2
|
201 lastSlopeOnsetFrame = currentFrame;
|
andrew@2
|
202 slopeFallenBelowMedian = false;
|
andrew@2
|
203 }
|
andrew@2
|
204
|
andrew@2
|
205
|
andrew@2
|
206 if (bestValue > bestSlopeMedian)
|
andrew@2
|
207 bestSlopeMedian += (bestValue - bestSlopeMedian)*0.1;
|
andrew@2
|
208 else{
|
andrew@2
|
209 bestSlopeMedian *= 0.995;
|
andrew@2
|
210 slopeFallenBelowMedian = true;;
|
andrew@2
|
211 }
|
andrew@2
|
212 return onsetDetected;
|
andrew@2
|
213 }
|
andrew@2
|
214
|
andrew@0
|
215 double AubioOnsetDetector::framesToSeconds(float frames){
|
andrew@0
|
216 double seconds = frames * buffersize / 44100.;
|
andrew@0
|
217 return seconds;
|
andrew@0
|
218 }
|
andrew@0
|
219
|
andrew@0
|
220 float AubioOnsetDetector :: getRawDetectionFrame(){
|
andrew@0
|
221 return rawDetectionValue;
|
andrew@0
|
222 }
|
andrew@0
|
223
|
andrew@0
|
224 float AubioOnsetDetector :: getPeakPickedDetectionFrame(){
|
andrew@0
|
225 return peakPickedDetectionValue;
|
andrew@0
|
226 }
|
andrew@0
|
227
|
andrew@0
|
228
|
andrew@0
|
229 void AubioOnsetDetector :: onsetclass_energy(){
|
andrew@0
|
230 //aubio_onsetdetection_type
|
andrew@0
|
231 aubio_onsetdetection_free (o);
|
andrew@0
|
232 o = new_aubio_onsetdetection(aubio_onset_energy, buffersize, 1);
|
andrew@0
|
233 }
|
andrew@0
|
234
|
andrew@0
|
235 void AubioOnsetDetector :: onsetclass_hfc(){
|
andrew@0
|
236 /** High Frequency Content onset detection function
|
andrew@0
|
237
|
andrew@0
|
238 This method computes the High Frequency Content (HFC) of the input spectral
|
andrew@0
|
239 frame. The resulting function is efficient at detecting percussive onsets.
|
andrew@0
|
240
|
andrew@0
|
241 Paul Masri. Computer modeling of Sound for Transformation and Synthesis of
|
andrew@0
|
242 Musical Signal. PhD dissertation, University of Bristol, UK, 1996.*/
|
andrew@0
|
243 aubio_onsetdetection_free (o);
|
andrew@0
|
244 o = new_aubio_onsetdetection(aubio_onset_hfc, buffersize, 1);
|
andrew@0
|
245 }
|
andrew@0
|
246
|
andrew@0
|
247
|
andrew@0
|
248 void AubioOnsetDetector :: onsetclass_complex(){
|
andrew@0
|
249 //aubio_onsetdetection_type
|
andrew@0
|
250 //Complex Domain Method onset detection function
|
andrew@0
|
251 //Christopher Duxbury, Mike E. Davies, and Mark B. Sandler. Complex domain
|
andrew@0
|
252 //onset detection for musical signals. In Proceedings of the Digital Audio
|
andrew@0
|
253 //Effects Conference, DAFx-03, pages 90-93, London, UK, 2003.
|
andrew@0
|
254 aubio_onsetdetection_free (o);
|
andrew@0
|
255 o = new_aubio_onsetdetection(aubio_onset_complex, buffersize, 1);
|
andrew@0
|
256 }
|
andrew@0
|
257
|
andrew@0
|
258 void AubioOnsetDetector :: onsetclass_phase(){
|
andrew@0
|
259 /** Phase Based Method onset detection function
|
andrew@0
|
260
|
andrew@0
|
261 Juan-Pablo Bello, Mike P. Davies, and Mark B. Sandler. Phase-based note onset
|
andrew@0
|
262 detection for music signals. In Proceedings of the IEEE International
|
andrew@0
|
263 Conference on Acoustics Speech and Signal Processing, pages 441­444,
|
andrew@0
|
264 Hong-Kong, 2003.*/
|
andrew@0
|
265 aubio_onsetdetection_free (o);
|
andrew@0
|
266 o = new_aubio_onsetdetection(aubio_onset_phase, buffersize, 1);
|
andrew@0
|
267
|
andrew@0
|
268 }
|
andrew@0
|
269
|
andrew@0
|
270 void AubioOnsetDetector :: onsetclass_specdiff(){
|
andrew@0
|
271 /* Spectral difference method onset detection function
|
andrew@0
|
272 Jonhatan Foote and Shingo Uchihashi. The beat spectrum: a new approach to
|
andrew@0
|
273 rhythm analysis. In IEEE International Conference on Multimedia and Expo
|
andrew@0
|
274 (ICME 2001), pages 881­884, Tokyo, Japan, August 2001.
|
andrew@0
|
275 */
|
andrew@0
|
276 //aubio_onsetdetection_type
|
andrew@0
|
277 aubio_onsetdetection_free (o);
|
andrew@0
|
278 o = new_aubio_onsetdetection(aubio_onset_specdiff, buffersize, 1);
|
andrew@0
|
279 }
|
andrew@0
|
280
|
andrew@0
|
281 void AubioOnsetDetector :: onsetclass_kl(){
|
andrew@0
|
282 /** Kullback-Liebler onset detection function
|
andrew@0
|
283
|
andrew@0
|
284 Stephen Hainsworth and Malcom Macleod. Onset detection in music audio
|
andrew@0
|
285 signals. In Proceedings of the International Computer Music Conference
|
andrew@0
|
286 (ICMC), Singapore, 2003.
|
andrew@0
|
287 */
|
andrew@0
|
288 aubio_onsetdetection_free (o);
|
andrew@0
|
289 o = new_aubio_onsetdetection(aubio_onset_kl, buffersize, 1);
|
andrew@0
|
290 }
|
andrew@0
|
291
|
andrew@0
|
292 void AubioOnsetDetector :: onsetclass_mkl(){
|
andrew@0
|
293
|
andrew@0
|
294 /** Modified Kullback-Liebler onset detection function
|
andrew@0
|
295
|
andrew@0
|
296 Paul Brossier, ``Automatic annotation of musical audio for interactive
|
andrew@0
|
297 systems'', Chapter 2, Temporal segmentation, PhD thesis, Centre for Digital
|
andrew@0
|
298 music, Queen Mary University of London, London, UK, 2003.*/
|
andrew@0
|
299 aubio_onsetdetection_free (o);
|
andrew@0
|
300 o = new_aubio_onsetdetection(aubio_onset_hfc, buffersize, 1);
|
andrew@0
|
301 }
|
andrew@0
|
302
|