[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 */ |
---|
| 46 | uint_t step; /** dfframe hopsize */ |
---|
[d25bd12] | 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 */ |
---|
| 51 | uint_t delay; /** delay to remove to last beat, in samples */ |
---|
[7524d0b] | 52 | }; |
---|
| 53 | |
---|
| 54 | /* execute tempo detection function on iput buffer */ |
---|
[5bf23f7] | 55 | void aubio_tempo_do(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo) |
---|
[7524d0b] | 56 | { |
---|
| 57 | uint_t i; |
---|
| 58 | uint_t winlen = o->winlen; |
---|
| 59 | uint_t step = o->step; |
---|
[e5b9a46] | 60 | fvec_t * thresholded; |
---|
[7524d0b] | 61 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
[31907fd] | 62 | aubio_specdesc_do (o->od, o->fftgrain, o->of); |
---|
[7524d0b] | 63 | /*if (usedoubled) { |
---|
[31907fd] | 64 | aubio_specdesc_do(o2,fftgrain, onset2); |
---|
[d207300] | 65 | onset->data[0] *= onset2->data[0]; |
---|
[7524d0b] | 66 | }*/ |
---|
| 67 | /* execute every overlap_size*step */ |
---|
| 68 | if (o->blockpos == (signed)step -1 ) { |
---|
| 69 | /* check dfframe */ |
---|
| 70 | aubio_beattracking_do(o->bt,o->dfframe,o->out); |
---|
| 71 | /* rotate dfframe */ |
---|
| 72 | for (i = 0 ; i < winlen - step; i++ ) |
---|
[d207300] | 73 | o->dfframe->data[i] = o->dfframe->data[i+step]; |
---|
[7524d0b] | 74 | for (i = winlen - step ; i < winlen; i++ ) |
---|
[d207300] | 75 | o->dfframe->data[i] = 0.; |
---|
[7524d0b] | 76 | o->blockpos = -1; |
---|
| 77 | } |
---|
| 78 | o->blockpos++; |
---|
[56ef7e1] | 79 | aubio_peakpicker_do (o->pp, o->of, o->onset); |
---|
[d207300] | 80 | tempo->data[1] = o->onset->data[0]; |
---|
[e5b9a46] | 81 | thresholded = aubio_peakpicker_get_thresholded_input(o->pp); |
---|
[d207300] | 82 | o->dfframe->data[winlen - step + o->blockpos] = thresholded->data[0]; |
---|
[7524d0b] | 83 | /* end of second level loop */ |
---|
[d207300] | 84 | tempo->data[0] = 0; /* reset tactus */ |
---|
[7524d0b] | 85 | i=0; |
---|
[d207300] | 86 | for (i = 1; i < o->out->data[0]; i++ ) { |
---|
[7524d0b] | 87 | /* if current frame is a predicted tactus */ |
---|
[d207300] | 88 | if (o->blockpos == FLOOR(o->out->data[i])) { |
---|
| 89 | tempo->data[0] = o->out->data[i] - FLOOR(o->out->data[i]); /* set tactus */ |
---|
[7524d0b] | 90 | /* test for silence */ |
---|
[ac67de7] | 91 | /* |
---|
[7524d0b] | 92 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
[ac67de7] | 93 | tempo->data[0] = 0; // unset beat if silent |
---|
[7524d0b] | 94 | } |
---|
[ac67de7] | 95 | */ |
---|
| 96 | o->last_beat = o->total_frames + (uint_t)ROUND(tempo->data[0] * o->hop_size); |
---|
[7524d0b] | 97 | } |
---|
| 98 | } |
---|
[483b883] | 99 | o->total_frames += o->hop_size; |
---|
| 100 | return; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | uint_t aubio_tempo_get_last (aubio_tempo_t *o) |
---|
| 104 | { |
---|
| 105 | return o->last_beat - o->delay; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | smpl_t aubio_tempo_get_last_s (aubio_tempo_t *o) |
---|
| 109 | { |
---|
| 110 | return aubio_tempo_get_last (o) / (smpl_t) (o->samplerate); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | smpl_t aubio_tempo_get_last_ms (aubio_tempo_t *o) |
---|
| 114 | { |
---|
[47e067b] | 115 | return aubio_tempo_get_last_s (o) * 1000.; |
---|
[483b883] | 116 | } |
---|
| 117 | |
---|
| 118 | uint_t aubio_tempo_set_delay(aubio_tempo_t * o, uint_t delay) { |
---|
| 119 | o->delay = delay; |
---|
| 120 | return AUBIO_OK; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | uint_t aubio_tempo_get_delay(aubio_tempo_t * o) { |
---|
| 124 | return o->delay; |
---|
[7524d0b] | 125 | } |
---|
| 126 | |
---|
[0a257a6] | 127 | uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
[7524d0b] | 128 | o->silence = silence; |
---|
[0a257a6] | 129 | return AUBIO_OK; |
---|
[7524d0b] | 130 | } |
---|
| 131 | |
---|
[0a257a6] | 132 | uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
[7524d0b] | 133 | o->threshold = threshold; |
---|
| 134 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
[0a257a6] | 135 | return AUBIO_OK; |
---|
[7524d0b] | 136 | } |
---|
| 137 | |
---|
| 138 | /* Allocate memory for an tempo detection */ |
---|
[b4f5967] | 139 | aubio_tempo_t * new_aubio_tempo (char_t * onset_mode, |
---|
[d207300] | 140 | uint_t buf_size, uint_t hop_size, uint_t samplerate) |
---|
[7524d0b] | 141 | { |
---|
| 142 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
[d25bd12] | 143 | o->samplerate = samplerate; |
---|
[7524d0b] | 144 | o->winlen = SQR(512)/hop_size; |
---|
| 145 | o->step = o->winlen/4; |
---|
| 146 | o->blockpos = 0; |
---|
| 147 | o->threshold = 0.3; |
---|
[d25bd12] | 148 | o->silence = -90.; |
---|
[7524d0b] | 149 | o->blockpos = 0; |
---|
[483b883] | 150 | o->total_frames = 0; |
---|
| 151 | o->last_beat = 0; |
---|
| 152 | o->delay = 0; |
---|
| 153 | o->hop_size = hop_size; |
---|
[d207300] | 154 | o->dfframe = new_fvec(o->winlen); |
---|
| 155 | o->fftgrain = new_cvec(buf_size); |
---|
| 156 | o->out = new_fvec(o->step); |
---|
| 157 | o->pv = new_aubio_pvoc(buf_size, hop_size); |
---|
| 158 | o->pp = new_aubio_peakpicker(); |
---|
[56ef7e1] | 159 | aubio_peakpicker_set_threshold (o->pp, o->threshold); |
---|
[d207300] | 160 | o->od = new_aubio_specdesc(onset_mode,buf_size); |
---|
| 161 | o->of = new_fvec(1); |
---|
| 162 | o->bt = new_aubio_beattracking(o->winlen); |
---|
| 163 | o->onset = new_fvec(1); |
---|
[7524d0b] | 164 | /*if (usedoubled) { |
---|
[d207300] | 165 | o2 = new_aubio_specdesc(type_onset2,buffer_size); |
---|
| 166 | onset2 = new_fvec(1); |
---|
[7524d0b] | 167 | }*/ |
---|
| 168 | return o; |
---|
| 169 | } |
---|
| 170 | |
---|
[cb0415d] | 171 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
| 172 | return aubio_beattracking_get_bpm(o->bt); |
---|
| 173 | } |
---|
| 174 | |
---|
[e34b010] | 175 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
| 176 | return aubio_beattracking_get_confidence(o->bt); |
---|
| 177 | } |
---|
| 178 | |
---|
[7524d0b] | 179 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
| 180 | { |
---|
[31907fd] | 181 | del_aubio_specdesc(o->od); |
---|
[7524d0b] | 182 | del_aubio_beattracking(o->bt); |
---|
| 183 | del_aubio_peakpicker(o->pp); |
---|
| 184 | del_aubio_pvoc(o->pv); |
---|
| 185 | del_fvec(o->out); |
---|
| 186 | del_fvec(o->of); |
---|
| 187 | del_cvec(o->fftgrain); |
---|
| 188 | del_fvec(o->dfframe); |
---|
[56ef7e1] | 189 | del_fvec(o->onset); |
---|
[7524d0b] | 190 | AUBIO_FREE(o); |
---|
| 191 | return; |
---|
| 192 | } |
---|