[tempo] make sure all objects have been created, safer del_ method
[aubio.git] / src / tempo / tempo.c
1 /*
2   Copyright (C) 2006-2009 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "cvec.h"
24 #include "spectral/specdesc.h"
25 #include "tempo/beattracking.h"
26 #include "spectral/phasevoc.h"
27 #include "onset/peakpicker.h"
28 #include "mathutils.h"
29 #include "tempo/tempo.h"
30
31 /* structure to store object state */
32 struct _aubio_tempo_t {
33   aubio_specdesc_t * od;   /** onset detection */
34   aubio_pvoc_t * pv;             /** phase vocoder */
35   aubio_peakpicker_t * pp;       /** peak picker */
36   aubio_beattracking_t * bt;     /** beat tracking */
37   cvec_t * fftgrain;             /** spectral frame */
38   fvec_t * of;                   /** onset detection function value */
39   fvec_t * dfframe;              /** peak picked detection function buffer */
40   fvec_t * out;                  /** beat tactus candidates */
41   fvec_t * onset;                /** onset results */
42   smpl_t silence;                /** silence parameter */
43   smpl_t threshold;              /** peak picking threshold */
44   sint_t blockpos;               /** current position in dfframe */
45   uint_t winlen;                 /** dfframe bufsize */
46   uint_t step;                   /** dfframe hopsize */
47   uint_t samplerate;             /** sampling rate of the signal */
48   uint_t hop_size;               /** get hop_size */
49   uint_t total_frames;           /** total frames since beginning */
50   uint_t last_beat;              /** time of latest detected beat, in samples */
51   sint_t delay;                  /** delay to remove to last beat, in samples */
52   uint_t last_tatum;             /** time of latest detected tatum, in samples */
53   uint_t tatum_signature;        /** number of tatum between each beats */
54 };
55
56 /* execute tempo detection function on iput buffer */
57 void aubio_tempo_do(aubio_tempo_t *o, const fvec_t * input, fvec_t * tempo)
58 {
59   uint_t i;
60   uint_t winlen = o->winlen;
61   uint_t step   = o->step;
62   fvec_t * thresholded;
63   aubio_pvoc_do (o->pv, input, o->fftgrain);
64   aubio_specdesc_do (o->od, o->fftgrain, o->of);
65   /*if (usedoubled) {
66     aubio_specdesc_do(o2,fftgrain, onset2);
67     onset->data[0] *= onset2->data[0];
68   }*/
69   /* execute every overlap_size*step */
70   if (o->blockpos == (signed)step -1 ) {
71     /* check dfframe */
72     aubio_beattracking_do(o->bt,o->dfframe,o->out);
73     /* rotate dfframe */
74     for (i = 0 ; i < winlen - step; i++ )
75       o->dfframe->data[i] = o->dfframe->data[i+step];
76     for (i = winlen - step ; i < winlen; i++ )
77       o->dfframe->data[i] = 0.;
78     o->blockpos = -1;
79   }
80   o->blockpos++;
81   aubio_peakpicker_do (o->pp, o->of, o->onset);
82   // store onset detection function in second sample of vector
83   //tempo->data[1] = o->onset->data[0];
84   thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
85   o->dfframe->data[winlen - step + o->blockpos] = thresholded->data[0];
86   /* end of second level loop */
87   tempo->data[0] = 0; /* reset tactus */
88   //i=0;
89   for (i = 1; i < o->out->data[0]; i++ ) {
90     /* if current frame is a predicted tactus */
91     if (o->blockpos == FLOOR(o->out->data[i])) {
92       tempo->data[0] = o->out->data[i] - FLOOR(o->out->data[i]); /* set tactus */
93       /* test for silence */
94       if (aubio_silence_detection(input, o->silence)==1) {
95         tempo->data[0] = 0; // unset beat if silent
96       }
97       o->last_beat = o->total_frames + (uint_t)ROUND(tempo->data[0] * o->hop_size);
98       o->last_tatum = o->last_beat;
99     }
100   }
101   o->total_frames += o->hop_size;
102   return;
103 }
104
105 uint_t aubio_tempo_get_last (aubio_tempo_t *o)
106 {
107   return o->last_beat + o->delay;
108 }
109
110 smpl_t aubio_tempo_get_last_s (aubio_tempo_t *o)
111 {
112   return aubio_tempo_get_last (o) / (smpl_t) (o->samplerate);
113 }
114
115 smpl_t aubio_tempo_get_last_ms (aubio_tempo_t *o)
116 {
117   return aubio_tempo_get_last_s (o) * 1000.;
118 }
119
120 uint_t aubio_tempo_set_delay(aubio_tempo_t * o, sint_t delay) {
121   o->delay = delay;
122   return AUBIO_OK;
123 }
124
125 uint_t aubio_tempo_set_delay_s(aubio_tempo_t * o, smpl_t delay) {
126   o->delay = delay * o->samplerate;
127   return AUBIO_OK;
128 }
129
130 uint_t aubio_tempo_set_delay_ms(aubio_tempo_t * o, smpl_t delay) {
131   o->delay = 1000. * delay * o->samplerate;
132   return AUBIO_OK;
133 }
134
135 uint_t aubio_tempo_get_delay(aubio_tempo_t * o) {
136   return o->delay;
137 }
138
139 smpl_t aubio_tempo_get_delay_s(aubio_tempo_t * o) {
140   return o->delay / (smpl_t)(o->samplerate);
141 }
142
143 smpl_t aubio_tempo_get_delay_ms(aubio_tempo_t * o) {
144   return o->delay / (smpl_t)(o->samplerate) / 1000.;
145 }
146
147 uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) {
148   o->silence = silence;
149   return AUBIO_OK;
150 }
151
152 smpl_t aubio_tempo_get_silence(aubio_tempo_t * o) {
153   return o->silence;
154 }
155
156 uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) {
157   o->threshold = threshold;
158   aubio_peakpicker_set_threshold(o->pp, o->threshold);
159   return AUBIO_OK;
160 }
161
162 smpl_t aubio_tempo_get_threshold(aubio_tempo_t * o) {
163   return o->threshold;
164 }
165
166 /* Allocate memory for an tempo detection */
167 aubio_tempo_t * new_aubio_tempo (const char_t * tempo_mode,
168     uint_t buf_size, uint_t hop_size, uint_t samplerate)
169 {
170   aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t);
171   char_t specdesc_func[PATH_MAX];
172   o->samplerate = samplerate;
173   // check parameters are valid
174   if ((sint_t)hop_size < 1) {
175     AUBIO_ERR("tempo: got hop size %d, but can not be < 1\n", hop_size);
176     goto beach;
177   } else if ((sint_t)buf_size < 2) {
178     AUBIO_ERR("tempo: got window size %d, but can not be < 2\n", buf_size);
179     goto beach;
180   } else if (buf_size < hop_size) {
181     AUBIO_ERR("tempo: hop size (%d) is larger than window size (%d)\n", buf_size, hop_size);
182     goto beach;
183   } else if ((sint_t)samplerate < 1) {
184     AUBIO_ERR("tempo: samplerate (%d) can not be < 1\n", samplerate);
185     goto beach;
186   }
187
188   /* length of observations, worth about 6 seconds */
189   o->winlen = aubio_next_power_of_two(5.8 * samplerate / hop_size);
190   if (o->winlen < 4) o->winlen = 4;
191   o->step = o->winlen/4;
192   o->blockpos = 0;
193   o->threshold = 0.3;
194   o->silence = -90.;
195   o->total_frames = 0;
196   o->last_beat = 0;
197   o->delay = 0;
198   o->hop_size = hop_size;
199   o->dfframe  = new_fvec(o->winlen);
200   o->fftgrain = new_cvec(buf_size);
201   o->out      = new_fvec(o->step);
202   o->pv       = new_aubio_pvoc(buf_size, hop_size);
203   o->pp       = new_aubio_peakpicker();
204   aubio_peakpicker_set_threshold (o->pp, o->threshold);
205   if ( strcmp(tempo_mode, "default") == 0 ) {
206     strncpy(specdesc_func, "specflux", PATH_MAX - 1);
207   } else {
208     strncpy(specdesc_func, tempo_mode, PATH_MAX - 1);
209     specdesc_func[PATH_MAX - 1] = '\0';
210   }
211   o->od       = new_aubio_specdesc(specdesc_func,buf_size);
212   o->of       = new_fvec(1);
213   o->bt       = new_aubio_beattracking(o->winlen, o->hop_size, o->samplerate);
214   o->onset    = new_fvec(1);
215   /*if (usedoubled)    {
216     o2 = new_aubio_specdesc(type_onset2,buffer_size);
217     onset2 = new_fvec(1);
218   }*/
219   if (!o->dfframe || !o->fftgrain || !o->out || !o->pv ||
220       !o->pp || !o->od || !o->of || !o->bt || !o->onset) {
221     AUBIO_ERR("tempo: failed creating tempo object\n");
222     goto beach;
223   }
224   o->last_tatum = 0;
225   o->tatum_signature = 4;
226   return o;
227
228 beach:
229   del_aubio_tempo(o);
230   return NULL;
231 }
232
233 smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) {
234   return aubio_beattracking_get_bpm(o->bt);
235 }
236
237 smpl_t aubio_tempo_get_period (aubio_tempo_t *o)
238 {
239   return aubio_beattracking_get_period (o->bt);
240 }
241
242 smpl_t aubio_tempo_get_period_s (aubio_tempo_t *o)
243 {
244   return aubio_beattracking_get_period_s (o->bt);
245 }
246
247 smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) {
248   return aubio_beattracking_get_confidence(o->bt);
249 }
250
251 uint_t aubio_tempo_was_tatum (aubio_tempo_t *o)
252 {
253   uint_t last_tatum_distance = o->total_frames - o->last_tatum;
254   smpl_t beat_period = aubio_tempo_get_period(o);
255   smpl_t tatum_period = beat_period / o->tatum_signature;
256   if (last_tatum_distance < o->hop_size) {
257     o->last_tatum = o->last_beat;
258     return 2;
259   }
260   else if (last_tatum_distance > tatum_period) {
261     if ( last_tatum_distance + o->hop_size > beat_period ) {
262       // next beat is too close, pass
263       return 0;
264     }
265     o->last_tatum = o->total_frames;
266     return 1;
267   }
268   return 0;
269 }
270
271 smpl_t aubio_tempo_get_last_tatum (aubio_tempo_t *o) {
272   return (smpl_t)o->last_tatum - o->delay;
273 }
274
275 uint_t aubio_tempo_set_tatum_signature (aubio_tempo_t *o, uint_t signature) {
276   if (signature < 1 || signature > 64) {
277     return AUBIO_FAIL;
278   } else {
279     o->tatum_signature = signature;
280     return AUBIO_OK;
281   }
282 }
283
284 void del_aubio_tempo (aubio_tempo_t *o)
285 {
286   if (o->od)
287     del_aubio_specdesc(o->od);
288   if (o->bt)
289     del_aubio_beattracking(o->bt);
290   if (o->pp)
291     del_aubio_peakpicker(o->pp);
292   if (o->pv)
293     del_aubio_pvoc(o->pv);
294   if (o->out)
295     del_fvec(o->out);
296   if (o->of)
297     del_fvec(o->of);
298   if (o->fftgrain)
299     del_cvec(o->fftgrain);
300   if (o->dfframe)
301     del_fvec(o->dfframe);
302   if (o->onset)
303     del_fvec(o->onset);
304   AUBIO_FREE(o);
305 }