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/peakpick.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 | fvec_t * peek; /** thresholded onset function */ |
---|
43 | smpl_t silence; /** silence parameter */ |
---|
44 | smpl_t threshold; /** peak picking threshold */ |
---|
45 | sint_t blockpos; /** current position in dfframe */ |
---|
46 | uint_t winlen; /** dfframe bufsize */ |
---|
47 | uint_t step; /** dfframe hopsize */ |
---|
48 | uint_t samplerate; /** sampling rate of the signal */ |
---|
49 | }; |
---|
50 | |
---|
51 | /* execute tempo detection function on iput buffer */ |
---|
52 | void aubio_tempo_do(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo) |
---|
53 | { |
---|
54 | uint_t i; |
---|
55 | uint_t winlen = o->winlen; |
---|
56 | uint_t step = o->step; |
---|
57 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
58 | aubio_specdesc_do (o->od, o->fftgrain, o->of); |
---|
59 | /*if (usedoubled) { |
---|
60 | aubio_specdesc_do(o2,fftgrain, onset2); |
---|
61 | onset->data[0][0] *= onset2->data[0][0]; |
---|
62 | }*/ |
---|
63 | /* execute every overlap_size*step */ |
---|
64 | if (o->blockpos == (signed)step -1 ) { |
---|
65 | /* check dfframe */ |
---|
66 | aubio_beattracking_do(o->bt,o->dfframe,o->out); |
---|
67 | /* rotate dfframe */ |
---|
68 | for (i = 0 ; i < winlen - step; i++ ) |
---|
69 | o->dfframe->data[0][i] = o->dfframe->data[0][i+step]; |
---|
70 | for (i = winlen - step ; i < winlen; i++ ) |
---|
71 | o->dfframe->data[0][i] = 0.; |
---|
72 | o->blockpos = -1; |
---|
73 | } |
---|
74 | o->blockpos++; |
---|
75 | aubio_peakpicker_do (o->pp, o->of, o->onset); |
---|
76 | tempo->data[0][1] = o->onset->data[0][0]; |
---|
77 | o->dfframe->data[0][winlen - step + o->blockpos] = |
---|
78 | aubio_peakpicker_get_thresholded_input(o->pp); |
---|
79 | /* end of second level loop */ |
---|
80 | tempo->data[0][0] = 0; /* reset tactus */ |
---|
81 | i=0; |
---|
82 | for (i = 1; i < o->out->data[0][0]; i++ ) { |
---|
83 | /* if current frame is a predicted tactus */ |
---|
84 | if (o->blockpos == FLOOR(o->out->data[0][i])) { |
---|
85 | tempo->data[0][0] = 1. + o->out->data[0][i] - FLOOR(o->out->data[0][i]); /* set tactus */ |
---|
86 | /* test for silence */ |
---|
87 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
88 | tempo->data[0][1] = 0; /* unset onset */ |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
95 | o->silence = silence; |
---|
96 | return AUBIO_OK; |
---|
97 | } |
---|
98 | |
---|
99 | uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
100 | o->threshold = threshold; |
---|
101 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
102 | return AUBIO_OK; |
---|
103 | } |
---|
104 | |
---|
105 | /* Allocate memory for an tempo detection */ |
---|
106 | aubio_tempo_t * new_aubio_tempo (char_t * onset_mode, |
---|
107 | uint_t buf_size, uint_t hop_size, uint_t channels, uint_t samplerate) |
---|
108 | { |
---|
109 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
110 | o->samplerate = samplerate; |
---|
111 | o->winlen = SQR(512)/hop_size; |
---|
112 | o->step = o->winlen/4; |
---|
113 | o->blockpos = 0; |
---|
114 | o->threshold = 0.3; |
---|
115 | o->silence = -90.; |
---|
116 | o->blockpos = 0; |
---|
117 | o->dfframe = new_fvec(o->winlen,channels); |
---|
118 | o->fftgrain = new_cvec(buf_size, channels); |
---|
119 | o->out = new_fvec(o->step,channels); |
---|
120 | o->pv = new_aubio_pvoc(buf_size, hop_size, channels); |
---|
121 | o->pp = new_aubio_peakpicker(channels); |
---|
122 | aubio_peakpicker_set_threshold (o->pp, o->threshold); |
---|
123 | o->od = new_aubio_specdesc(onset_mode,buf_size,channels); |
---|
124 | o->of = new_fvec(1, channels); |
---|
125 | o->bt = new_aubio_beattracking(o->winlen,channels); |
---|
126 | o->onset = new_fvec(1, channels); |
---|
127 | o->peek = new_fvec(3, channels); |
---|
128 | /*if (usedoubled) { |
---|
129 | o2 = new_aubio_specdesc(type_onset2,buffer_size,channels); |
---|
130 | onset2 = new_fvec(1 , channels); |
---|
131 | }*/ |
---|
132 | return o; |
---|
133 | } |
---|
134 | |
---|
135 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
136 | return aubio_beattracking_get_bpm(o->bt); |
---|
137 | } |
---|
138 | |
---|
139 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
140 | return aubio_beattracking_get_confidence(o->bt); |
---|
141 | } |
---|
142 | |
---|
143 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
144 | { |
---|
145 | del_aubio_specdesc(o->od); |
---|
146 | del_aubio_beattracking(o->bt); |
---|
147 | del_aubio_peakpicker(o->pp); |
---|
148 | del_aubio_pvoc(o->pv); |
---|
149 | del_fvec(o->out); |
---|
150 | del_fvec(o->of); |
---|
151 | del_cvec(o->fftgrain); |
---|
152 | del_fvec(o->dfframe); |
---|
153 | del_fvec(o->onset); |
---|
154 | del_fvec(o->peek); |
---|
155 | AUBIO_FREE(o); |
---|
156 | return; |
---|
157 | } |
---|