[7524d0b] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2006-2009 Paul Brossier <piem@aubio.org> |
---|
[7524d0b] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[7524d0b] | 5 | |
---|
[e6a78ea] | 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. |
---|
[7524d0b] | 10 | |
---|
[e6a78ea] | 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/>. |
---|
[7524d0b] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
[31907fd] | 24 | #include "spectral/specdesc.h" |
---|
[bcf38fe] | 25 | #include "tempo/beattracking.h" |
---|
[32d6958] | 26 | #include "spectral/phasevoc.h" |
---|
[3e17aed] | 27 | #include "onset/peakpicker.h" |
---|
[7524d0b] | 28 | #include "mathutils.h" |
---|
[32d6958] | 29 | #include "tempo/tempo.h" |
---|
[7524d0b] | 30 | |
---|
| 31 | /* structure to store object state */ |
---|
| 32 | struct _aubio_tempo_t { |
---|
[31907fd] | 33 | aubio_specdesc_t * od; /** onset detection */ |
---|
[7524d0b] | 34 | aubio_pvoc_t * pv; /** phase vocoder */ |
---|
[8766cb6] | 35 | aubio_peakpicker_t * pp; /** peak picker */ |
---|
[7524d0b] | 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 */ |
---|
[56ef7e1] | 41 | fvec_t * onset; /** onset results */ |
---|
[7524d0b] | 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 */ |
---|
[43b7c6d] | 46 | uint_t step; /** dfframe hopsize */ |
---|
| 47 | uint_t samplerate; /** sampling rate of the signal */ |
---|
[483b883] | 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 */ |
---|
[7e80dc9] | 51 | sint_t delay; /** delay to remove to last beat, in samples */ |
---|
[95748a6] | 52 | uint_t last_tatum; /** time of latest detected tatum, in samples */ |
---|
| 53 | uint_t tatum_signature; /** number of tatum between each beats */ |
---|
[7524d0b] | 54 | }; |
---|
| 55 | |
---|
| 56 | /* execute tempo detection function on iput buffer */ |
---|
[69f74f0] | 57 | void aubio_tempo_do(aubio_tempo_t *o, const fvec_t * input, fvec_t * tempo) |
---|
[7524d0b] | 58 | { |
---|
| 59 | uint_t i; |
---|
| 60 | uint_t winlen = o->winlen; |
---|
| 61 | uint_t step = o->step; |
---|
[e5b9a46] | 62 | fvec_t * thresholded; |
---|
[7524d0b] | 63 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
[31907fd] | 64 | aubio_specdesc_do (o->od, o->fftgrain, o->of); |
---|
[7524d0b] | 65 | /*if (usedoubled) { |
---|
[31907fd] | 66 | aubio_specdesc_do(o2,fftgrain, onset2); |
---|
[d207300] | 67 | onset->data[0] *= onset2->data[0]; |
---|
[7524d0b] | 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 */ |
---|
[43b7c6d] | 74 | for (i = 0 ; i < winlen - step; i++ ) |
---|
[d207300] | 75 | o->dfframe->data[i] = o->dfframe->data[i+step]; |
---|
[43b7c6d] | 76 | for (i = winlen - step ; i < winlen; i++ ) |
---|
[d207300] | 77 | o->dfframe->data[i] = 0.; |
---|
[7524d0b] | 78 | o->blockpos = -1; |
---|
| 79 | } |
---|
| 80 | o->blockpos++; |
---|
[56ef7e1] | 81 | aubio_peakpicker_do (o->pp, o->of, o->onset); |
---|
[e644757] | 82 | // store onset detection function in second sample of vector |
---|
| 83 | //tempo->data[1] = o->onset->data[0]; |
---|
[e5b9a46] | 84 | thresholded = aubio_peakpicker_get_thresholded_input(o->pp); |
---|
[d207300] | 85 | o->dfframe->data[winlen - step + o->blockpos] = thresholded->data[0]; |
---|
[7524d0b] | 86 | /* end of second level loop */ |
---|
[d207300] | 87 | tempo->data[0] = 0; /* reset tactus */ |
---|
[810b3b6] | 88 | //i=0; |
---|
[d207300] | 89 | for (i = 1; i < o->out->data[0]; i++ ) { |
---|
[7524d0b] | 90 | /* if current frame is a predicted tactus */ |
---|
[d207300] | 91 | if (o->blockpos == FLOOR(o->out->data[i])) { |
---|
| 92 | tempo->data[0] = o->out->data[i] - FLOOR(o->out->data[i]); /* set tactus */ |
---|
[7524d0b] | 93 | /* test for silence */ |
---|
| 94 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
[ac67de7] | 95 | tempo->data[0] = 0; // unset beat if silent |
---|
[7524d0b] | 96 | } |
---|
[ac67de7] | 97 | o->last_beat = o->total_frames + (uint_t)ROUND(tempo->data[0] * o->hop_size); |
---|
[95748a6] | 98 | o->last_tatum = o->last_beat; |
---|
[7524d0b] | 99 | } |
---|
| 100 | } |
---|
[483b883] | 101 | o->total_frames += o->hop_size; |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | uint_t aubio_tempo_get_last (aubio_tempo_t *o) |
---|
| 106 | { |
---|
[7e80dc9] | 107 | return o->last_beat + o->delay; |
---|
[483b883] | 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 | { |
---|
[47e067b] | 117 | return aubio_tempo_get_last_s (o) * 1000.; |
---|
[483b883] | 118 | } |
---|
| 119 | |
---|
[7e80dc9] | 120 | uint_t aubio_tempo_set_delay(aubio_tempo_t * o, sint_t delay) { |
---|
[483b883] | 121 | o->delay = delay; |
---|
| 122 | return AUBIO_OK; |
---|
| 123 | } |
---|
| 124 | |
---|
[7e80dc9] | 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) { |
---|
[ef0a430] | 131 | return aubio_tempo_set_delay_s(o, delay / 1000.); |
---|
[7e80dc9] | 132 | } |
---|
| 133 | |
---|
[483b883] | 134 | uint_t aubio_tempo_get_delay(aubio_tempo_t * o) { |
---|
| 135 | return o->delay; |
---|
[7524d0b] | 136 | } |
---|
| 137 | |
---|
[7e80dc9] | 138 | smpl_t aubio_tempo_get_delay_s(aubio_tempo_t * o) { |
---|
| 139 | return o->delay / (smpl_t)(o->samplerate); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | smpl_t aubio_tempo_get_delay_ms(aubio_tempo_t * o) { |
---|
[ef0a430] | 143 | return aubio_tempo_get_delay_s(o) * 1000.; |
---|
[7e80dc9] | 144 | } |
---|
| 145 | |
---|
[0a257a6] | 146 | uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
[7524d0b] | 147 | o->silence = silence; |
---|
[0a257a6] | 148 | return AUBIO_OK; |
---|
[7524d0b] | 149 | } |
---|
| 150 | |
---|
[deb1fd4] | 151 | smpl_t aubio_tempo_get_silence(aubio_tempo_t * o) { |
---|
| 152 | return o->silence; |
---|
| 153 | } |
---|
| 154 | |
---|
[0a257a6] | 155 | uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
[7524d0b] | 156 | o->threshold = threshold; |
---|
| 157 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
[0a257a6] | 158 | return AUBIO_OK; |
---|
[7524d0b] | 159 | } |
---|
| 160 | |
---|
[deb1fd4] | 161 | smpl_t aubio_tempo_get_threshold(aubio_tempo_t * o) { |
---|
| 162 | return o->threshold; |
---|
| 163 | } |
---|
| 164 | |
---|
[7524d0b] | 165 | /* Allocate memory for an tempo detection */ |
---|
[69f74f0] | 166 | aubio_tempo_t * new_aubio_tempo (const char_t * tempo_mode, |
---|
[d207300] | 167 | uint_t buf_size, uint_t hop_size, uint_t samplerate) |
---|
[7524d0b] | 168 | { |
---|
| 169 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
[b1559f4c] | 170 | char_t specdesc_func[PATH_MAX]; |
---|
[d25bd12] | 171 | o->samplerate = samplerate; |
---|
[9be906a] | 172 | // check parameters are valid |
---|
| 173 | if ((sint_t)hop_size < 1) { |
---|
| 174 | AUBIO_ERR("tempo: got hop size %d, but can not be < 1\n", hop_size); |
---|
| 175 | goto beach; |
---|
[d897aae] | 176 | } else if ((sint_t)buf_size < 2) { |
---|
| 177 | AUBIO_ERR("tempo: got window size %d, but can not be < 2\n", buf_size); |
---|
[9be906a] | 178 | goto beach; |
---|
| 179 | } else if (buf_size < hop_size) { |
---|
| 180 | AUBIO_ERR("tempo: hop size (%d) is larger than window size (%d)\n", buf_size, hop_size); |
---|
| 181 | goto beach; |
---|
| 182 | } else if ((sint_t)samplerate < 1) { |
---|
| 183 | AUBIO_ERR("tempo: samplerate (%d) can not be < 1\n", samplerate); |
---|
| 184 | goto beach; |
---|
| 185 | } |
---|
| 186 | |
---|
[77db425] | 187 | /* length of observations, worth about 6 seconds */ |
---|
| 188 | o->winlen = aubio_next_power_of_two(5.8 * samplerate / hop_size); |
---|
[9be906a] | 189 | if (o->winlen < 4) o->winlen = 4; |
---|
[7524d0b] | 190 | o->step = o->winlen/4; |
---|
| 191 | o->blockpos = 0; |
---|
| 192 | o->threshold = 0.3; |
---|
[d25bd12] | 193 | o->silence = -90.; |
---|
[483b883] | 194 | o->total_frames = 0; |
---|
| 195 | o->last_beat = 0; |
---|
| 196 | o->delay = 0; |
---|
| 197 | o->hop_size = hop_size; |
---|
[d207300] | 198 | o->dfframe = new_fvec(o->winlen); |
---|
| 199 | o->fftgrain = new_cvec(buf_size); |
---|
| 200 | o->out = new_fvec(o->step); |
---|
| 201 | o->pv = new_aubio_pvoc(buf_size, hop_size); |
---|
| 202 | o->pp = new_aubio_peakpicker(); |
---|
[56ef7e1] | 203 | aubio_peakpicker_set_threshold (o->pp, o->threshold); |
---|
[a559796] | 204 | if ( strcmp(tempo_mode, "default") == 0 ) { |
---|
[b1559f4c] | 205 | strncpy(specdesc_func, "specflux", PATH_MAX - 1); |
---|
[a559796] | 206 | } else { |
---|
[b1559f4c] | 207 | strncpy(specdesc_func, tempo_mode, PATH_MAX - 1); |
---|
| 208 | specdesc_func[PATH_MAX - 1] = '\0'; |
---|
[a559796] | 209 | } |
---|
| 210 | o->od = new_aubio_specdesc(specdesc_func,buf_size); |
---|
[d207300] | 211 | o->of = new_fvec(1); |
---|
[77db425] | 212 | o->bt = new_aubio_beattracking(o->winlen, o->hop_size, o->samplerate); |
---|
[d207300] | 213 | o->onset = new_fvec(1); |
---|
[7524d0b] | 214 | /*if (usedoubled) { |
---|
[d207300] | 215 | o2 = new_aubio_specdesc(type_onset2,buffer_size); |
---|
| 216 | onset2 = new_fvec(1); |
---|
[7524d0b] | 217 | }*/ |
---|
[b5a0479] | 218 | if (!o->dfframe || !o->fftgrain || !o->out || !o->pv || |
---|
| 219 | !o->pp || !o->od || !o->of || !o->bt || !o->onset) { |
---|
| 220 | AUBIO_ERR("tempo: failed creating tempo object\n"); |
---|
| 221 | goto beach; |
---|
| 222 | } |
---|
[95748a6] | 223 | o->last_tatum = 0; |
---|
| 224 | o->tatum_signature = 4; |
---|
[7524d0b] | 225 | return o; |
---|
[9be906a] | 226 | |
---|
| 227 | beach: |
---|
[b5a0479] | 228 | del_aubio_tempo(o); |
---|
[9be906a] | 229 | return NULL; |
---|
[7524d0b] | 230 | } |
---|
| 231 | |
---|
[cb0415d] | 232 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
| 233 | return aubio_beattracking_get_bpm(o->bt); |
---|
| 234 | } |
---|
| 235 | |
---|
[42c3dc0] | 236 | smpl_t aubio_tempo_get_period (aubio_tempo_t *o) |
---|
| 237 | { |
---|
| 238 | return aubio_beattracking_get_period (o->bt); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | smpl_t aubio_tempo_get_period_s (aubio_tempo_t *o) |
---|
| 242 | { |
---|
| 243 | return aubio_beattracking_get_period_s (o->bt); |
---|
| 244 | } |
---|
| 245 | |
---|
[e34b010] | 246 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
| 247 | return aubio_beattracking_get_confidence(o->bt); |
---|
| 248 | } |
---|
| 249 | |
---|
[95748a6] | 250 | uint_t aubio_tempo_was_tatum (aubio_tempo_t *o) |
---|
| 251 | { |
---|
| 252 | uint_t last_tatum_distance = o->total_frames - o->last_tatum; |
---|
| 253 | smpl_t beat_period = aubio_tempo_get_period(o); |
---|
| 254 | smpl_t tatum_period = beat_period / o->tatum_signature; |
---|
| 255 | if (last_tatum_distance < o->hop_size) { |
---|
| 256 | o->last_tatum = o->last_beat; |
---|
| 257 | return 2; |
---|
| 258 | } |
---|
| 259 | else if (last_tatum_distance > tatum_period) { |
---|
| 260 | if ( last_tatum_distance + o->hop_size > beat_period ) { |
---|
| 261 | // next beat is too close, pass |
---|
| 262 | return 0; |
---|
| 263 | } |
---|
| 264 | o->last_tatum = o->total_frames; |
---|
| 265 | return 1; |
---|
| 266 | } |
---|
| 267 | return 0; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | smpl_t aubio_tempo_get_last_tatum (aubio_tempo_t *o) { |
---|
| 271 | return (smpl_t)o->last_tatum - o->delay; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | uint_t aubio_tempo_set_tatum_signature (aubio_tempo_t *o, uint_t signature) { |
---|
| 275 | if (signature < 1 || signature > 64) { |
---|
| 276 | return AUBIO_FAIL; |
---|
| 277 | } else { |
---|
| 278 | o->tatum_signature = signature; |
---|
| 279 | return AUBIO_OK; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
[7524d0b] | 283 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
| 284 | { |
---|
[b5a0479] | 285 | if (o->od) |
---|
| 286 | del_aubio_specdesc(o->od); |
---|
| 287 | if (o->bt) |
---|
| 288 | del_aubio_beattracking(o->bt); |
---|
| 289 | if (o->pp) |
---|
| 290 | del_aubio_peakpicker(o->pp); |
---|
| 291 | if (o->pv) |
---|
| 292 | del_aubio_pvoc(o->pv); |
---|
| 293 | if (o->out) |
---|
| 294 | del_fvec(o->out); |
---|
| 295 | if (o->of) |
---|
| 296 | del_fvec(o->of); |
---|
| 297 | if (o->fftgrain) |
---|
| 298 | del_cvec(o->fftgrain); |
---|
| 299 | if (o->dfframe) |
---|
| 300 | del_fvec(o->dfframe); |
---|
| 301 | if (o->onset) |
---|
| 302 | del_fvec(o->onset); |
---|
[7524d0b] | 303 | AUBIO_FREE(o); |
---|
| 304 | } |
---|