source: src/tempo/tempo.c @ fa74361

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since fa74361 was 95748a6, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/tempo/tempo.{c,h}: add tatum, a subdivision of the beat period, default to 4

  • Property mode set to 100644
File size: 8.7 KB
Line 
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// TODO implement get/set_delay
32
33/** set current delay
34
35  \param o beat tracking object
36
37  \return current delay, in samples
38
39 */
40uint_t aubio_tempo_get_delay(aubio_tempo_t * o);
41
42/** set current delay
43
44  \param o beat tracking object
45  \param delay delay to set tempo to, in samples
46
47  \return `0` if successful, non-zero otherwise
48
49 */
50uint_t aubio_tempo_set_delay(aubio_tempo_t * o, uint_t delay);
51
52/* structure to store object state */
53struct _aubio_tempo_t {
54  aubio_specdesc_t * od;   /** onset detection */
55  aubio_pvoc_t * pv;             /** phase vocoder */
56  aubio_peakpicker_t * pp;       /** peak picker */
57  aubio_beattracking_t * bt;     /** beat tracking */
58  cvec_t * fftgrain;             /** spectral frame */
59  fvec_t * of;                   /** onset detection function value */
60  fvec_t * dfframe;              /** peak picked detection function buffer */
61  fvec_t * out;                  /** beat tactus candidates */
62  fvec_t * onset;                /** onset results */
63  smpl_t silence;                /** silence parameter */
64  smpl_t threshold;              /** peak picking threshold */
65  sint_t blockpos;               /** current position in dfframe */
66  uint_t winlen;                 /** dfframe bufsize */
67  uint_t step;                   /** dfframe hopsize */
68  uint_t samplerate;             /** sampling rate of the signal */
69  uint_t hop_size;               /** get hop_size */
70  uint_t total_frames;           /** total frames since beginning */
71  uint_t last_beat;              /** time of latest detected beat, in samples */
72  uint_t delay;                  /** delay to remove to last beat, in samples */
73  uint_t last_tatum;             /** time of latest detected tatum, in samples */
74  uint_t tatum_signature;        /** number of tatum between each beats */
75};
76
77/* execute tempo detection function on iput buffer */
78void aubio_tempo_do(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo)
79{
80  uint_t i;
81  uint_t winlen = o->winlen;
82  uint_t step   = o->step;
83  fvec_t * thresholded;
84  aubio_pvoc_do (o->pv, input, o->fftgrain);
85  aubio_specdesc_do (o->od, o->fftgrain, o->of);
86  /*if (usedoubled) {
87    aubio_specdesc_do(o2,fftgrain, onset2);
88    onset->data[0] *= onset2->data[0];
89  }*/
90  /* execute every overlap_size*step */
91  if (o->blockpos == (signed)step -1 ) {
92    /* check dfframe */
93    aubio_beattracking_do(o->bt,o->dfframe,o->out);
94    /* rotate dfframe */
95    for (i = 0 ; i < winlen - step; i++ )
96      o->dfframe->data[i] = o->dfframe->data[i+step];
97    for (i = winlen - step ; i < winlen; i++ )
98      o->dfframe->data[i] = 0.;
99    o->blockpos = -1;
100  }
101  o->blockpos++;
102  aubio_peakpicker_do (o->pp, o->of, o->onset);
103  tempo->data[1] = o->onset->data[0];
104  thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
105  o->dfframe->data[winlen - step + o->blockpos] = thresholded->data[0];
106  /* end of second level loop */
107  tempo->data[0] = 0; /* reset tactus */
108  //i=0;
109  for (i = 1; i < o->out->data[0]; i++ ) {
110    /* if current frame is a predicted tactus */
111    if (o->blockpos == FLOOR(o->out->data[i])) {
112      tempo->data[0] = o->out->data[i] - FLOOR(o->out->data[i]); /* set tactus */
113      /* test for silence */
114      if (aubio_silence_detection(input, o->silence)==1) {
115        tempo->data[0] = 0; // unset beat if silent
116      }
117      o->last_beat = o->total_frames + (uint_t)ROUND(tempo->data[0] * o->hop_size);
118      o->last_tatum = o->last_beat;
119    }
120  }
121  o->total_frames += o->hop_size;
122  return;
123}
124
125uint_t aubio_tempo_get_last (aubio_tempo_t *o)
126{
127  return o->last_beat - o->delay;
128}
129
130smpl_t aubio_tempo_get_last_s (aubio_tempo_t *o)
131{
132  return aubio_tempo_get_last (o) / (smpl_t) (o->samplerate);
133}
134
135smpl_t aubio_tempo_get_last_ms (aubio_tempo_t *o)
136{
137  return aubio_tempo_get_last_s (o) * 1000.;
138}
139
140uint_t aubio_tempo_set_delay(aubio_tempo_t * o, uint_t delay) {
141  o->delay = delay;
142  return AUBIO_OK;
143}
144
145uint_t aubio_tempo_get_delay(aubio_tempo_t * o) {
146  return o->delay;
147}
148
149uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) {
150  o->silence = silence;
151  return AUBIO_OK;
152}
153
154smpl_t aubio_tempo_get_silence(aubio_tempo_t * o) {
155  return o->silence;
156}
157
158uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) {
159  o->threshold = threshold;
160  aubio_peakpicker_set_threshold(o->pp, o->threshold);
161  return AUBIO_OK;
162}
163
164smpl_t aubio_tempo_get_threshold(aubio_tempo_t * o) {
165  return o->threshold;
166}
167
168/* Allocate memory for an tempo detection */
169aubio_tempo_t * new_aubio_tempo (char_t * tempo_mode,
170    uint_t buf_size, uint_t hop_size, uint_t samplerate)
171{
172  aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t);
173  char_t specdesc_func[20];
174  o->samplerate = samplerate;
175  // check parameters are valid
176  if ((sint_t)hop_size < 1) {
177    AUBIO_ERR("tempo: got hop size %d, but can not be < 1\n", hop_size);
178    goto beach;
179  } else if ((sint_t)buf_size < 1) {
180    AUBIO_ERR("tempo: got window size %d, but can not be < 1\n", buf_size);
181    goto beach;
182  } else if (buf_size < hop_size) {
183    AUBIO_ERR("tempo: hop size (%d) is larger than window size (%d)\n", buf_size, hop_size);
184    goto beach;
185  } else if ((sint_t)samplerate < 1) {
186    AUBIO_ERR("tempo: samplerate (%d) can not be < 1\n", samplerate);
187    goto beach;
188  }
189
190  /* length of observations, worth about 6 seconds */
191  o->winlen = aubio_next_power_of_two(5.8 * samplerate / hop_size);
192  if (o->winlen < 4) o->winlen = 4;
193  o->step = o->winlen/4;
194  o->blockpos = 0;
195  o->threshold = 0.3;
196  o->silence = -90.;
197  o->total_frames = 0;
198  o->last_beat = 0;
199  o->delay = 0;
200  o->hop_size = hop_size;
201  o->dfframe  = new_fvec(o->winlen);
202  o->fftgrain = new_cvec(buf_size);
203  o->out      = new_fvec(o->step);
204  o->pv       = new_aubio_pvoc(buf_size, hop_size);
205  o->pp       = new_aubio_peakpicker();
206  aubio_peakpicker_set_threshold (o->pp, o->threshold);
207  if ( strcmp(tempo_mode, "default") == 0 ) {
208    strcpy(specdesc_func, "specflux");
209  } else {
210    strcpy(specdesc_func, tempo_mode);
211  }
212  o->od       = new_aubio_specdesc(specdesc_func,buf_size);
213  o->of       = new_fvec(1);
214  o->bt       = new_aubio_beattracking(o->winlen, o->hop_size, o->samplerate);
215  o->onset    = new_fvec(1);
216  /*if (usedoubled)    {
217    o2 = new_aubio_specdesc(type_onset2,buffer_size);
218    onset2 = new_fvec(1);
219  }*/
220  o->last_tatum = 0;
221  o->tatum_signature = 4;
222  return o;
223
224beach:
225  AUBIO_FREE(o);
226  return NULL;
227}
228
229smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) {
230  return aubio_beattracking_get_bpm(o->bt);
231}
232
233smpl_t aubio_tempo_get_period (aubio_tempo_t *o)
234{
235  return aubio_beattracking_get_period (o->bt);
236}
237
238smpl_t aubio_tempo_get_period_s (aubio_tempo_t *o)
239{
240  return aubio_beattracking_get_period_s (o->bt);
241}
242
243smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) {
244  return aubio_beattracking_get_confidence(o->bt);
245}
246
247uint_t aubio_tempo_was_tatum (aubio_tempo_t *o)
248{
249  uint_t last_tatum_distance = o->total_frames - o->last_tatum;
250  smpl_t beat_period = aubio_tempo_get_period(o);
251  smpl_t tatum_period = beat_period / o->tatum_signature;
252  if (last_tatum_distance < o->hop_size) {
253    o->last_tatum = o->last_beat;
254    return 2;
255  }
256  else if (last_tatum_distance > tatum_period) {
257    if ( last_tatum_distance + o->hop_size > beat_period ) {
258      // next beat is too close, pass
259      return 0;
260    }
261    o->last_tatum = o->total_frames;
262    return 1;
263  }
264  return 0;
265}
266
267smpl_t aubio_tempo_get_last_tatum (aubio_tempo_t *o) {
268  return (smpl_t)o->last_tatum - o->delay;
269}
270
271uint_t aubio_tempo_set_tatum_signature (aubio_tempo_t *o, uint_t signature) {
272  if (signature < 1 || signature > 64) {
273    return AUBIO_FAIL;
274  } else {
275    o->tatum_signature = signature;
276    return AUBIO_OK;
277  }
278}
279
280void del_aubio_tempo (aubio_tempo_t *o)
281{
282  del_aubio_specdesc(o->od);
283  del_aubio_beattracking(o->bt);
284  del_aubio_peakpicker(o->pp);
285  del_aubio_pvoc(o->pv);
286  del_fvec(o->out);
287  del_fvec(o->of);
288  del_cvec(o->fftgrain);
289  del_fvec(o->dfframe);
290  del_fvec(o->onset);
291  AUBIO_FREE(o);
292  return;
293}
Note: See TracBrowser for help on using the repository browser.