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 "sample.h" |
---|
22 | #include "onsetdetection.h" |
---|
23 | #include "beattracking.h" |
---|
24 | #include "phasevoc.h" |
---|
25 | #include "peakpick.h" |
---|
26 | #include "mathutils.h" |
---|
27 | #include "tempo.h" |
---|
28 | |
---|
29 | /* structure to store object state */ |
---|
30 | struct _aubio_tempo_t { |
---|
31 | aubio_onsetdetection_t * od; /** onset detection */ |
---|
32 | aubio_pvoc_t * pv; /** phase vocoder */ |
---|
33 | aubio_pickpeak_t * pp; /** peak picker */ |
---|
34 | aubio_beattracking_t * bt; /** beat tracking */ |
---|
35 | cvec_t * fftgrain; /** spectral frame */ |
---|
36 | fvec_t * of; /** onset detection function value */ |
---|
37 | fvec_t * dfframe; /** peak picked detection function buffer */ |
---|
38 | fvec_t * out; /** beat tactus candidates */ |
---|
39 | smpl_t silence; /** silence parameter */ |
---|
40 | smpl_t threshold; /** peak picking threshold */ |
---|
41 | sint_t blockpos; /** current position in dfframe */ |
---|
42 | uint_t winlen; /** dfframe bufsize */ |
---|
43 | uint_t step; /** dfframe hopsize */ |
---|
44 | }; |
---|
45 | |
---|
46 | /* execute tempo detection function on iput buffer */ |
---|
47 | void aubio_tempo(aubio_tempo_t *o, fvec_t * input, fvec_t * tempo) |
---|
48 | { |
---|
49 | uint_t i; |
---|
50 | uint_t winlen = o->winlen; |
---|
51 | uint_t step = o->step; |
---|
52 | aubio_pvoc_do (o->pv, input, o->fftgrain); |
---|
53 | aubio_onsetdetection(o->od, o->fftgrain, o->of); |
---|
54 | /*if (usedoubled) { |
---|
55 | aubio_onsetdetection(o2,fftgrain, onset2); |
---|
56 | onset->data[0][0] *= onset2->data[0][0]; |
---|
57 | }*/ |
---|
58 | /* execute every overlap_size*step */ |
---|
59 | if (o->blockpos == (signed)step -1 ) { |
---|
60 | /* check dfframe */ |
---|
61 | aubio_beattracking_do(o->bt,o->dfframe,o->out); |
---|
62 | /* rotate dfframe */ |
---|
63 | for (i = 0 ; i < winlen - step; i++ ) |
---|
64 | o->dfframe->data[0][i] = o->dfframe->data[0][i+step]; |
---|
65 | for (i = winlen - step ; i < winlen; i++ ) |
---|
66 | o->dfframe->data[0][i] = 0.; |
---|
67 | o->blockpos = -1; |
---|
68 | } |
---|
69 | o->blockpos++; |
---|
70 | tempo->data[0][1] = aubio_peakpick_pimrt_wt(o->of,o->pp, |
---|
71 | &(o->dfframe->data[0][winlen - step + o->blockpos])); |
---|
72 | /* end of second level loop */ |
---|
73 | tempo->data[0][0] = 0; /* reset tactus */ |
---|
74 | i=0; |
---|
75 | for (i = 1; i < o->out->data[0][0]; i++ ) { |
---|
76 | /* if current frame is a predicted tactus */ |
---|
77 | if (o->blockpos == o->out->data[0][i]) { |
---|
78 | /* test for silence */ |
---|
79 | if (aubio_silence_detection(input, o->silence)==1) { |
---|
80 | tempo->data[0][1] = 0; /* unset onset */ |
---|
81 | tempo->data[0][0] = 0; /* unset tactus */ |
---|
82 | } else { |
---|
83 | tempo->data[0][0] = 1; /* set tactus */ |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void aubio_tempo_set_silence(aubio_tempo_t * o, smpl_t silence) { |
---|
90 | o->silence = silence; |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | void aubio_tempo_set_threshold(aubio_tempo_t * o, smpl_t threshold) { |
---|
95 | o->threshold = threshold; |
---|
96 | aubio_peakpicker_set_threshold(o->pp, o->threshold); |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | /* Allocate memory for an tempo detection */ |
---|
101 | aubio_tempo_t * new_aubio_tempo (aubio_onsetdetection_type type_onset, |
---|
102 | uint_t buf_size, uint_t hop_size, uint_t channels) |
---|
103 | { |
---|
104 | aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); |
---|
105 | o->winlen = SQR(512)/hop_size; |
---|
106 | o->step = o->winlen/4; |
---|
107 | o->blockpos = 0; |
---|
108 | o->threshold = 0.3; |
---|
109 | o->silence = -90; |
---|
110 | o->blockpos = 0; |
---|
111 | o->dfframe = new_fvec(o->winlen,channels); |
---|
112 | o->fftgrain = new_cvec(buf_size, channels); |
---|
113 | o->out = new_fvec(o->step,channels); |
---|
114 | o->pv = new_aubio_pvoc(buf_size, hop_size, channels); |
---|
115 | o->pp = new_aubio_peakpicker(o->threshold); |
---|
116 | o->od = new_aubio_onsetdetection(type_onset,buf_size,channels); |
---|
117 | o->of = new_fvec(1, channels); |
---|
118 | o->bt = new_aubio_beattracking(o->winlen,channels); |
---|
119 | /*if (usedoubled) { |
---|
120 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
121 | onset2 = new_fvec(1 , channels); |
---|
122 | }*/ |
---|
123 | return o; |
---|
124 | } |
---|
125 | |
---|
126 | smpl_t aubio_tempo_get_bpm(aubio_tempo_t *o) { |
---|
127 | return aubio_beattracking_get_bpm(o->bt); |
---|
128 | } |
---|
129 | |
---|
130 | smpl_t aubio_tempo_get_confidence(aubio_tempo_t *o) { |
---|
131 | return aubio_beattracking_get_confidence(o->bt); |
---|
132 | } |
---|
133 | |
---|
134 | void del_aubio_tempo (aubio_tempo_t *o) |
---|
135 | { |
---|
136 | del_aubio_onsetdetection(o->od); |
---|
137 | del_aubio_beattracking(o->bt); |
---|
138 | del_aubio_peakpicker(o->pp); |
---|
139 | del_aubio_pvoc(o->pv); |
---|
140 | del_fvec(o->out); |
---|
141 | del_fvec(o->of); |
---|
142 | del_cvec(o->fftgrain); |
---|
143 | del_fvec(o->dfframe); |
---|
144 | AUBIO_FREE(o); |
---|
145 | return; |
---|
146 | } |
---|