[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 | |
---|
[029bf4e] | 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 | */ |
---|
| 40 | uint_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 | */ |
---|
| 50 | uint_t aubio_tempo_set_delay(aubio_tempo_t * o, uint_t delay); |
---|
| 51 | |
---|
[7524d0b] | 52 | /* structure to store object state */ |
---|
| 53 | struct _aubio_tempo_t { |
---|
[31907fd] | 54 | aubio_specdesc_t * od; /** onset detection */ |
---|
[7524d0b] | 55 | aubio_pvoc_t * pv; /** phase vocoder */ |
---|
[8766cb6] | 56 | aubio_peakpicker_t * pp; /** peak picker */ |
---|
[7524d0b] | 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 */ |
---|
[56ef7e1] | 62 | fvec_t * onset; /** onset results */ |
---|
[7524d0b] | 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 */ |
---|
[d25bd12] | 68 | uint_t samplerate; /** sampling rate of the signal */ |
---|
[483b883] | 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 */ |
---|
[7524d0b] | 73 | }; |
---|
| 74 | |
---|
| 75 | /* execute tempo detection function on iput buffer */ |
---|
[5bf23f7] | 76 | void aubio_tempo_do(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo) |
---|
[7524d0b] | 77 | { |
---|
| 78 | uint_t i; |
---|
| 79 | uint_t winlen = o->winlen; |
---|
| 80 | uint_t step = o->step; |
---|
[e5b9a46] | 81 | fvec_t * thresholded; |
---|
[7524d0b] | 82 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
[31907fd] | 83 | aubio_specdesc_do (o->od, o->fftgrain, o->of); |
---|
[7524d0b] | 84 | /*if (usedoubled) { |
---|
[31907fd] | 85 | aubio_specdesc_do(o2,fftgrain, onset2); |
---|
[d207300] | 86 | onset->data[0] *= onset2->data[0]; |
---|
[7524d0b] | 87 | }*/ |
---|
| 88 | /* execute every overlap_size*step */ |
---|
| 89 | if (o->blockpos == (signed)step -1 ) { |
---|
| 90 | /* check dfframe */ |
---|
| 91 | aubio_beattracking_do(o->bt,o->dfframe,o->out); |
---|
| 92 | /* rotate dfframe */ |
---|
| 93 | for (i = 0 ; i < winlen - step; i++ ) |
---|
[d207300] | 94 | o->dfframe->data[i] = o->dfframe->data[i+step]; |
---|
[7524d0b] | 95 | for (i = winlen - step ; i < winlen; i++ ) |
---|
[d207300] | 96 | o->dfframe->data[i] = 0.; |
---|
[7524d0b] | 97 | o->blockpos = -1; |
---|
| 98 | } |
---|
| 99 | o->blockpos++; |
---|
[56ef7e1] | 100 | aubio_peakpicker_do (o->pp, o->of, o->onset); |
---|
[d207300] | 101 | tempo->data[1] = o->onset->data[0]; |
---|
[e5b9a46] | 102 | thresholded = aubio_peakpicker_get_thresholded_input(o->pp); |
---|
[d207300] | 103 | o->dfframe->data[winlen - step + o->blockpos] = thresholded->data[0]; |
---|
[7524d0b] | 104 | /* end of second level loop */ |
---|
[d207300] | 105 | tempo->data[0] = 0; /* reset tactus */ |
---|
[7524d0b] | 106 | i=0; |
---|
[d207300] | 107 | for (i = 1; i < o->out->data[0]; i++ ) { |
---|
[7524d0b] | 108 | /* if current frame is a predicted tactus */ |
---|
[d207300] | 109 | if (o->blockpos == FLOOR(o->out->data[i])) { |
---|
| 110 | tempo->data[0] = o->out->data[i] - FLOOR(o->out->data[i]); /* set tactus */ |
---|
[7524d0b] | 111 | /* test for silence */ |
---|
[ac67de7] | 112 | /* |
---|
[7524d0b] | 113 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
[ac67de7] | 114 | tempo->data[0] = 0; // unset beat if silent |
---|
[7524d0b] | 115 | } |
---|
[ac67de7] | 116 | */ |
---|
| 117 | o->last_beat = o->total_frames + (uint_t)ROUND(tempo->data[0] * o->hop_size); |
---|
[7524d0b] | 118 | } |
---|
| 119 | } |
---|
[483b883] | 120 | o->total_frames += o->hop_size; |
---|
| 121 | return; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | uint_t aubio_tempo_get_last (aubio_tempo_t *o) |
---|
| 125 | { |
---|
| 126 | return o->last_beat - o->delay; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | smpl_t aubio_tempo_get_last_s (aubio_tempo_t *o) |
---|
| 130 | { |
---|
| 131 | return aubio_tempo_get_last (o) / (smpl_t) (o->samplerate); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | smpl_t aubio_tempo_get_last_ms (aubio_tempo_t *o) |
---|
| 135 | { |
---|
[47e067b] | 136 | return aubio_tempo_get_last_s (o) * 1000.; |
---|
[483b883] | 137 | } |
---|
| 138 | |
---|
| 139 | uint_t aubio_tempo_set_delay(aubio_tempo_t * o, uint_t delay) { |
---|
| 140 | o->delay = delay; |
---|
| 141 | return AUBIO_OK; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | uint_t aubio_tempo_get_delay(aubio_tempo_t * o) { |
---|
| 145 | return o->delay; |
---|
[7524d0b] | 146 | } |
---|
| 147 | |
---|
[0a257a6] | 148 | uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
[7524d0b] | 149 | o->silence = silence; |
---|
[0a257a6] | 150 | return AUBIO_OK; |
---|
[7524d0b] | 151 | } |
---|
| 152 | |
---|
[deb1fd4] | 153 | smpl_t aubio_tempo_get_silence(aubio_tempo_t * o) { |
---|
| 154 | return o->silence; |
---|
| 155 | } |
---|
| 156 | |
---|
[0a257a6] | 157 | uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
[7524d0b] | 158 | o->threshold = threshold; |
---|
| 159 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
[0a257a6] | 160 | return AUBIO_OK; |
---|
[7524d0b] | 161 | } |
---|
| 162 | |
---|
[deb1fd4] | 163 | smpl_t aubio_tempo_get_threshold(aubio_tempo_t * o) { |
---|
| 164 | return o->threshold; |
---|
| 165 | } |
---|
| 166 | |
---|
[7524d0b] | 167 | /* Allocate memory for an tempo detection */ |
---|
[a559796] | 168 | aubio_tempo_t * new_aubio_tempo (char_t * tempo_mode, |
---|
[d207300] | 169 | uint_t buf_size, uint_t hop_size, uint_t samplerate) |
---|
[7524d0b] | 170 | { |
---|
| 171 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
[a559796] | 172 | char_t specdesc_func[20]; |
---|
[d25bd12] | 173 | o->samplerate = samplerate; |
---|
[9be906a] | 174 | // check parameters are valid |
---|
| 175 | if ((sint_t)hop_size < 1) { |
---|
| 176 | AUBIO_ERR("tempo: got hop size %d, but can not be < 1\n", hop_size); |
---|
| 177 | goto beach; |
---|
| 178 | } else if ((sint_t)buf_size < 1) { |
---|
| 179 | AUBIO_ERR("tempo: got window size %d, but can not be < 1\n", buf_size); |
---|
| 180 | goto beach; |
---|
| 181 | } else if (buf_size < hop_size) { |
---|
| 182 | AUBIO_ERR("tempo: hop size (%d) is larger than window size (%d)\n", buf_size, hop_size); |
---|
| 183 | goto beach; |
---|
| 184 | } else if ((sint_t)samplerate < 1) { |
---|
| 185 | AUBIO_ERR("tempo: samplerate (%d) can not be < 1\n", samplerate); |
---|
| 186 | goto beach; |
---|
| 187 | } |
---|
| 188 | |
---|
[77db425] | 189 | /* length of observations, worth about 6 seconds */ |
---|
| 190 | o->winlen = aubio_next_power_of_two(5.8 * samplerate / hop_size); |
---|
[9be906a] | 191 | if (o->winlen < 4) o->winlen = 4; |
---|
[7524d0b] | 192 | o->step = o->winlen/4; |
---|
| 193 | o->blockpos = 0; |
---|
| 194 | o->threshold = 0.3; |
---|
[d25bd12] | 195 | o->silence = -90.; |
---|
[483b883] | 196 | o->total_frames = 0; |
---|
| 197 | o->last_beat = 0; |
---|
| 198 | o->delay = 0; |
---|
| 199 | o->hop_size = hop_size; |
---|
[d207300] | 200 | o->dfframe = new_fvec(o->winlen); |
---|
| 201 | o->fftgrain = new_cvec(buf_size); |
---|
| 202 | o->out = new_fvec(o->step); |
---|
| 203 | o->pv = new_aubio_pvoc(buf_size, hop_size); |
---|
| 204 | o->pp = new_aubio_peakpicker(); |
---|
[56ef7e1] | 205 | aubio_peakpicker_set_threshold (o->pp, o->threshold); |
---|
[a559796] | 206 | if ( strcmp(tempo_mode, "default") == 0 ) { |
---|
| 207 | strcpy(specdesc_func, "specflux"); |
---|
| 208 | } else { |
---|
| 209 | strcpy(specdesc_func, tempo_mode); |
---|
| 210 | } |
---|
| 211 | o->od = new_aubio_specdesc(specdesc_func,buf_size); |
---|
[d207300] | 212 | o->of = new_fvec(1); |
---|
[77db425] | 213 | o->bt = new_aubio_beattracking(o->winlen, o->hop_size, o->samplerate); |
---|
[d207300] | 214 | o->onset = new_fvec(1); |
---|
[7524d0b] | 215 | /*if (usedoubled) { |
---|
[d207300] | 216 | o2 = new_aubio_specdesc(type_onset2,buffer_size); |
---|
| 217 | onset2 = new_fvec(1); |
---|
[7524d0b] | 218 | }*/ |
---|
| 219 | return o; |
---|
[9be906a] | 220 | |
---|
| 221 | beach: |
---|
| 222 | AUBIO_FREE(o); |
---|
| 223 | return NULL; |
---|
[7524d0b] | 224 | } |
---|
| 225 | |
---|
[cb0415d] | 226 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
| 227 | return aubio_beattracking_get_bpm(o->bt); |
---|
| 228 | } |
---|
| 229 | |
---|
[e34b010] | 230 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
| 231 | return aubio_beattracking_get_confidence(o->bt); |
---|
| 232 | } |
---|
| 233 | |
---|
[7524d0b] | 234 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
| 235 | { |
---|
[31907fd] | 236 | del_aubio_specdesc(o->od); |
---|
[7524d0b] | 237 | del_aubio_beattracking(o->bt); |
---|
| 238 | del_aubio_peakpicker(o->pp); |
---|
| 239 | del_aubio_pvoc(o->pv); |
---|
| 240 | del_fvec(o->out); |
---|
| 241 | del_fvec(o->of); |
---|
| 242 | del_cvec(o->fftgrain); |
---|
| 243 | del_fvec(o->dfframe); |
---|
[56ef7e1] | 244 | del_fvec(o->onset); |
---|
[7524d0b] | 245 | AUBIO_FREE(o); |
---|
| 246 | return; |
---|
| 247 | } |
---|