2 Copyright (C) 2003 Paul Brossier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "aubio_priv.h"
23 #include "mathutils.h"
25 #include "onsetdetection.h"
28 /** structure to store object state */
29 struct _aubio_onsetdetection_t {
30 aubio_onsetdetection_type type; /**< onset detection type */
31 /** Pointer to aubio_onsetdetection_<type> function */
32 void (*funcpointer)(aubio_onsetdetection_t *o,
33 cvec_t * fftgrain, fvec_t * onset);
34 smpl_t threshold; /**< minimum norm threshold for phase and specdiff */
35 fvec_t *oldmag; /**< previous norm vector */
36 fft_data_t *meas; /**< current onset detection measure complex vector */
37 fvec_t *dev1 ; /**< current onset detection measure vector */
38 fvec_t *theta1; /**< previous phase vector, one frame behind */
39 fvec_t *theta2; /**< previous phase vector, two frames behind */
40 aubio_hist_t * histog; /**< histogram */
44 /* Energy based onset detection function */
45 void aubio_onsetdetection_energy (aubio_onsetdetection_t *o,
46 cvec_t * fftgrain, fvec_t * onset) {
48 for (i=0;i<fftgrain->channels;i++) {
49 onset->data[i][0] = 0.;
50 for (j=0;j<fftgrain->length;j++) {
51 onset->data[i][0] += SQR(fftgrain->norm[i][j]);
56 /* High Frequency Content onset detection function */
57 void aubio_onsetdetection_hfc(aubio_onsetdetection_t *o, cvec_t * fftgrain, fvec_t * onset){
59 for (i=0;i<fftgrain->channels;i++) {
60 onset->data[i][0] = 0.;
61 for (j=0;j<fftgrain->length;j++) {
62 onset->data[i][0] += (j+1)*fftgrain->norm[i][j];
68 /* Complex Domain Method onset detection function */
69 void aubio_onsetdetection_complex (aubio_onsetdetection_t *o, cvec_t * fftgrain, fvec_t * onset) {
71 uint_t nbins = fftgrain->length;
72 for (i=0;i<fftgrain->channels; i++) {
73 onset->data[i][0] = 0.;
74 for (j=0;j<nbins; j++) {
75 o->dev1->data[i][j] = aubio_unwrap2pi(
77 -2.0*o->theta1->data[i][j]+
78 o->theta2->data[i][j]);
79 o->meas[j] = fftgrain->norm[i][j]*CEXPC(I*o->dev1->data[i][j]);
81 onset->data[i][0] += //(fftgrain->norm[i][j]);
82 SQRT(SQR( REAL(o->oldmag->data[i][j]-o->meas[j]) )
83 + SQR( IMAG(o->oldmag->data[i][j]-o->meas[j]) )
85 /* swap old phase data (need to remember 2 frames behind)*/
86 o->theta2->data[i][j] = o->theta1->data[i][j];
87 o->theta1->data[i][j] = fftgrain->phas[i][j];
88 /* swap old magnitude data (1 frame is enough) */
89 o->oldmag->data[i][j] = fftgrain->norm[i][j];
95 /* Phase Based Method onset detection function */
96 void aubio_onsetdetection_phase(aubio_onsetdetection_t *o,
97 cvec_t * fftgrain, fvec_t * onset){
99 uint_t nbins = fftgrain->length;
100 for (i=0;i<fftgrain->channels; i++) {
101 onset->data[i][0] = 0.0f;
102 o->dev1->data[i][0]=0.;
103 for ( j=0;j<nbins; j++ ) {
104 o->dev1->data[i][j] =
107 -2.0*o->theta1->data[i][j]
108 +o->theta2->data[i][j]);
109 if ( o->threshold < fftgrain->norm[i][j] )
110 o->dev1->data[i][j] = ABS(o->dev1->data[i][j]);
112 o->dev1->data[i][j] = 0.0f;
113 /* keep a track of the past frames */
114 o->theta2->data[i][j] = o->theta1->data[i][j];
115 o->theta1->data[i][j] = fftgrain->phas[i][j];
117 /* apply o->histogram */
118 aubio_hist_dyn_notnull(o->histog,o->dev1);
120 aubio_hist_weigth(o->histog);
121 /* its mean is the result */
122 onset->data[i][0] = aubio_hist_mean(o->histog);
123 //onset->data[i][0] = vec_mean(o->dev1);
127 /* Spectral difference method onset detection function */
128 void aubio_onsetdetection_specdiff(aubio_onsetdetection_t *o,
129 cvec_t * fftgrain, fvec_t * onset){
131 uint_t nbins = fftgrain->length;
132 for (i=0;i<fftgrain->channels; i++) {
133 onset->data[i][0] = 0.0f;
134 for (j=0;j<nbins; j++) {
135 o->dev1->data[i][j] = SQRT(
136 ABS(SQR( fftgrain->norm[i][j])
137 - SQR(o->oldmag->data[i][j])));
138 if (o->threshold < fftgrain->norm[i][j] )
139 o->dev1->data[i][j] = ABS(o->dev1->data[i][j]);
141 o->dev1->data[i][j] = 0.0f;
142 o->oldmag->data[i][j] = fftgrain->norm[i][j];
145 /* apply o->histogram (act somewhat as a low pass on the
146 * overall function)*/
147 aubio_hist_dyn_notnull(o->histog,o->dev1);
149 aubio_hist_weigth(o->histog);
150 /* its mean is the result */
151 onset->data[i][0] = aubio_hist_mean(o->histog);
156 /* Kullback Liebler onset detection function
157 * note we use ln(1+Xn/(Xn-1+0.0001)) to avoid
158 * negative (1.+) and infinite values (+1.e-10) */
159 void aubio_onsetdetection_kl(aubio_onsetdetection_t *o, cvec_t * fftgrain, fvec_t * onset){
161 for (i=0;i<fftgrain->channels;i++) {
162 onset->data[i][0] = 0.;
163 for (j=0;j<fftgrain->length;j++) {
164 onset->data[i][0] += fftgrain->norm[i][j]
165 *LOG(1.+fftgrain->norm[i][j]/(o->oldmag->data[i][j]+1.e-10));
166 o->oldmag->data[i][j] = fftgrain->norm[i][j];
168 if (isnan(onset->data[i][0])) onset->data[i][0] = 0.;
172 /* Modified Kullback Liebler onset detection function
173 * note we use ln(1+Xn/(Xn-1+0.0001)) to avoid
174 * negative (1.+) and infinite values (+1.e-10) */
175 void aubio_onsetdetection_mkl(aubio_onsetdetection_t *o, cvec_t * fftgrain, fvec_t * onset){
177 for (i=0;i<fftgrain->channels;i++) {
178 onset->data[i][0] = 0.;
179 for (j=0;j<fftgrain->length;j++) {
180 onset->data[i][0] += LOG(1.+fftgrain->norm[i][j]/(o->oldmag->data[i][j]+1.e-10));
181 o->oldmag->data[i][j] = fftgrain->norm[i][j];
183 if (isnan(onset->data[i][0])) onset->data[i][0] = 0.;
187 /* Generic function pointing to the choosen one */
189 aubio_onsetdetection(aubio_onsetdetection_t *o, cvec_t * fftgrain,
191 o->funcpointer(o,fftgrain,onset);
194 /* Allocate memory for an onset detection
195 * depending on the choosen type, allocate memory as needed
197 aubio_onsetdetection_t *
198 new_aubio_onsetdetection (aubio_onsetdetection_type type,
199 uint_t size, uint_t channels){
200 aubio_onsetdetection_t * o = AUBIO_NEW(aubio_onsetdetection_t);
201 uint_t rsize = size/2+1;
203 /* for both energy and hfc, only fftgrain->norm is required */
204 case aubio_onset_energy:
206 case aubio_onset_hfc:
208 /* the other approaches will need some more memory spaces */
209 case aubio_onset_complex:
210 o->oldmag = new_fvec(rsize,channels);
211 /** bug: must be complex array */
212 o->meas = AUBIO_ARRAY(fft_data_t,size);
213 o->dev1 = new_fvec(rsize,channels);
214 o->theta1 = new_fvec(rsize,channels);
215 o->theta2 = new_fvec(rsize,channels);
217 case aubio_onset_phase:
218 o->dev1 = new_fvec(rsize,channels);
219 o->theta1 = new_fvec(rsize,channels);
220 o->theta2 = new_fvec(rsize,channels);
221 o->histog = new_aubio_hist(0.0f, PI, 10, channels);
224 case aubio_onset_specdiff:
225 o->oldmag = new_fvec(rsize,channels);
226 o->dev1 = new_fvec(rsize,channels);
227 o->histog = new_aubio_hist(0.0f, PI, 10, channels);
231 o->oldmag = new_fvec(rsize,channels);
233 case aubio_onset_mkl:
234 o->oldmag = new_fvec(rsize,channels);
240 /* this switch could be in its own function to change between
241 * detections on the fly. this would need getting rid of the switch
242 * above and always allocate all the structure */
245 case aubio_onset_energy:
246 o->funcpointer = aubio_onsetdetection_energy;
248 case aubio_onset_hfc:
249 o->funcpointer = aubio_onsetdetection_hfc;
251 case aubio_onset_complex:
252 o->funcpointer = aubio_onsetdetection_complex;
254 case aubio_onset_phase:
255 o->funcpointer = aubio_onsetdetection_phase;
257 case aubio_onset_specdiff:
258 o->funcpointer = aubio_onsetdetection_specdiff;
261 o->funcpointer = aubio_onsetdetection_kl;
263 case aubio_onset_mkl:
264 o->funcpointer = aubio_onsetdetection_mkl;
273 void aubio_onsetdetection_free (aubio_onsetdetection_t *o){
274 del_aubio_onsetdetection(o);
277 void del_aubio_onsetdetection (aubio_onsetdetection_t *o){
280 /* for both energy and hfc, only fftgrain->norm is required */
281 case aubio_onset_energy:
283 case aubio_onset_hfc:
285 /* the other approaches will need some more memory spaces */
286 case aubio_onset_complex:
293 case aubio_onset_phase:
297 del_aubio_hist(o->histog);
299 case aubio_onset_specdiff:
302 del_aubio_hist(o->histog);
307 case aubio_onset_mkl: