1 | /* |
---|
2 | Copyright (C) 2006 Paul Brossier |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | #include "aubio_priv.h" |
---|
21 | #include "fvec.h" |
---|
22 | #include "cvec.h" |
---|
23 | #include "onset/onsetdetection.h" |
---|
24 | #include "tempo/beattracking.h" |
---|
25 | #include "spectral/phasevoc.h" |
---|
26 | #include "onset/peakpick.h" |
---|
27 | #include "mathutils.h" |
---|
28 | #include "tempo/tempo.h" |
---|
29 | |
---|
30 | /* structure to store object state */ |
---|
31 | struct _aubio_tempo_t { |
---|
32 | aubio_onsetdetection_t * od; /** onset detection */ |
---|
33 | aubio_pvoc_t * pv; /** phase vocoder */ |
---|
34 | aubio_peakpicker_t * pp; /** peak picker */ |
---|
35 | aubio_beattracking_t * bt; /** beat tracking */ |
---|
36 | cvec_t * fftgrain; /** spectral frame */ |
---|
37 | fvec_t * of; /** onset detection function value */ |
---|
38 | fvec_t * dfframe; /** peak picked detection function buffer */ |
---|
39 | fvec_t * out; /** beat tactus candidates */ |
---|
40 | fvec_t * onset; /** onset results */ |
---|
41 | fvec_t * peek; /** thresholded onset function */ |
---|
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 */ |
---|
47 | uint_t samplerate; /** sampling rate of the signal */ |
---|
48 | }; |
---|
49 | |
---|
50 | /* execute tempo detection function on iput buffer */ |
---|
51 | void aubio_tempo_do(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo) |
---|
52 | { |
---|
53 | uint_t i; |
---|
54 | uint_t winlen = o->winlen; |
---|
55 | uint_t step = o->step; |
---|
56 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
57 | aubio_onsetdetection_do (o->od, o->fftgrain, o->of); |
---|
58 | /*if (usedoubled) { |
---|
59 | aubio_onsetdetection_do(o2,fftgrain, onset2); |
---|
60 | onset->data[0][0] *= onset2->data[0][0]; |
---|
61 | }*/ |
---|
62 | /* execute every overlap_size*step */ |
---|
63 | if (o->blockpos == (signed)step -1 ) { |
---|
64 | /* check dfframe */ |
---|
65 | aubio_beattracking_do(o->bt,o->dfframe,o->out); |
---|
66 | /* rotate dfframe */ |
---|
67 | for (i = 0 ; i < winlen - step; i++ ) |
---|
68 | o->dfframe->data[0][i] = o->dfframe->data[0][i+step]; |
---|
69 | for (i = winlen - step ; i < winlen; i++ ) |
---|
70 | o->dfframe->data[0][i] = 0.; |
---|
71 | o->blockpos = -1; |
---|
72 | } |
---|
73 | o->blockpos++; |
---|
74 | aubio_peakpicker_do (o->pp, o->of, o->onset); |
---|
75 | tempo->data[0][1] = o->onset->data[0][0]; |
---|
76 | o->dfframe->data[0][winlen - step + o->blockpos] = |
---|
77 | aubio_peakpicker_get_thresholded_input(o->pp); |
---|
78 | /* end of second level loop */ |
---|
79 | tempo->data[0][0] = 0; /* reset tactus */ |
---|
80 | i=0; |
---|
81 | for (i = 1; i < o->out->data[0][0]; i++ ) { |
---|
82 | /* if current frame is a predicted tactus */ |
---|
83 | if (o->blockpos == FLOOR(o->out->data[0][i])) { |
---|
84 | tempo->data[0][0] = 1. + o->out->data[0][i] - FLOOR(o->out->data[0][i]); /* set tactus */ |
---|
85 | /* test for silence */ |
---|
86 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
87 | tempo->data[0][1] = 0; /* unset onset */ |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | uint_t aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
94 | o->silence = silence; |
---|
95 | return AUBIO_OK; |
---|
96 | } |
---|
97 | |
---|
98 | uint_t aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
99 | o->threshold = threshold; |
---|
100 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
101 | return AUBIO_OK; |
---|
102 | } |
---|
103 | |
---|
104 | /* Allocate memory for an tempo detection */ |
---|
105 | aubio_tempo_t * new_aubio_tempo (char_t * onset_mode, |
---|
106 | uint_t buf_size, uint_t hop_size, uint_t channels, uint_t samplerate) |
---|
107 | { |
---|
108 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
109 | o->samplerate = samplerate; |
---|
110 | o->winlen = SQR(512)/hop_size; |
---|
111 | o->step = o->winlen/4; |
---|
112 | o->blockpos = 0; |
---|
113 | o->threshold = 0.3; |
---|
114 | o->silence = -90.; |
---|
115 | o->blockpos = 0; |
---|
116 | o->dfframe = new_fvec(o->winlen,channels); |
---|
117 | o->fftgrain = new_cvec(buf_size, channels); |
---|
118 | o->out = new_fvec(o->step,channels); |
---|
119 | o->pv = new_aubio_pvoc(buf_size, hop_size, channels); |
---|
120 | o->pp = new_aubio_peakpicker(channels); |
---|
121 | aubio_peakpicker_set_threshold (o->pp, o->threshold); |
---|
122 | o->od = new_aubio_onsetdetection(onset_mode,buf_size,channels); |
---|
123 | o->of = new_fvec(1, channels); |
---|
124 | o->bt = new_aubio_beattracking(o->winlen,channels); |
---|
125 | o->onset = new_fvec(1, channels); |
---|
126 | o->peek = new_fvec(3, channels); |
---|
127 | /*if (usedoubled) { |
---|
128 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
129 | onset2 = new_fvec(1 , channels); |
---|
130 | }*/ |
---|
131 | return o; |
---|
132 | } |
---|
133 | |
---|
134 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
135 | return aubio_beattracking_get_bpm(o->bt); |
---|
136 | } |
---|
137 | |
---|
138 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
139 | return aubio_beattracking_get_confidence(o->bt); |
---|
140 | } |
---|
141 | |
---|
142 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
143 | { |
---|
144 | del_aubio_onsetdetection(o->od); |
---|
145 | del_aubio_beattracking(o->bt); |
---|
146 | del_aubio_peakpicker(o->pp); |
---|
147 | del_aubio_pvoc(o->pv); |
---|
148 | del_fvec(o->out); |
---|
149 | del_fvec(o->of); |
---|
150 | del_cvec(o->fftgrain); |
---|
151 | del_fvec(o->dfframe); |
---|
152 | del_fvec(o->onset); |
---|
153 | del_fvec(o->peek); |
---|
154 | AUBIO_FREE(o); |
---|
155 | return; |
---|
156 | } |
---|