1 | /* |
---|
2 | Copyright (C) 2003-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 "mathutils.h" |
---|
24 | #include "lvec.h" |
---|
25 | #include "temporal/filter.h" |
---|
26 | #include "temporal/biquad.h" |
---|
27 | #include "onset/peakpicker.h" |
---|
28 | |
---|
29 | /** function pointer to thresholding function */ |
---|
30 | typedef smpl_t (*aubio_thresholdfn_t)(fvec_t *input); |
---|
31 | /** function pointer to peak-picking function */ |
---|
32 | typedef uint_t (*aubio_pickerfn_t)(fvec_t *input, uint_t pos); |
---|
33 | |
---|
34 | /** set peak picker thresholding function */ |
---|
35 | uint_t aubio_peakpicker_set_thresholdfn(aubio_peakpicker_t * p, aubio_thresholdfn_t thresholdfn); |
---|
36 | /** get peak picker thresholding function */ |
---|
37 | aubio_thresholdfn_t aubio_peakpicker_get_thresholdfn(aubio_peakpicker_t * p); |
---|
38 | |
---|
39 | /* peak picking parameters, default values in brackets |
---|
40 | * |
---|
41 | * [<----post----|--pre-->] |
---|
42 | * .................|............. |
---|
43 | * time-> ^now |
---|
44 | */ |
---|
45 | struct _aubio_peakpicker_t |
---|
46 | { |
---|
47 | /** thresh: offset threshold [0.033 or 0.01] */ |
---|
48 | smpl_t threshold; |
---|
49 | /** win_post: median filter window length (causal part) [8] */ |
---|
50 | uint_t win_post; |
---|
51 | /** pre: median filter window (anti-causal part) [post-1] */ |
---|
52 | uint_t win_pre; |
---|
53 | /** threshfn: name or handle of fn for computing adaptive threshold [median] */ |
---|
54 | aubio_thresholdfn_t thresholdfn; |
---|
55 | /** picker: name or handle of fn for picking event times [peakpick] */ |
---|
56 | aubio_pickerfn_t pickerfn; |
---|
57 | |
---|
58 | /** biquad lowpass filter */ |
---|
59 | aubio_filter_t *biquad; |
---|
60 | /** original onsets */ |
---|
61 | fvec_t *onset_keep; |
---|
62 | /** modified onsets */ |
---|
63 | fvec_t *onset_proc; |
---|
64 | /** peak picked window [3] */ |
---|
65 | fvec_t *onset_peek; |
---|
66 | /** thresholded function */ |
---|
67 | fvec_t *thresholded; |
---|
68 | /** scratch pad for biquad and median */ |
---|
69 | fvec_t *scratch; |
---|
70 | |
---|
71 | /** \bug should be used to calculate filter coefficients */ |
---|
72 | /* cutoff: low-pass filter cutoff [0.34, 1] */ |
---|
73 | /* smpl_t cutoff; */ |
---|
74 | |
---|
75 | /* not used anymore */ |
---|
76 | /* time precision [512/44100 winlength/samplerate, fs/buffer_size */ |
---|
77 | /* smpl_t tau; */ |
---|
78 | /* alpha: normalisation exponent [9] */ |
---|
79 | /* smpl_t alpha; */ |
---|
80 | }; |
---|
81 | |
---|
82 | |
---|
83 | /** modified version for real time, moving mean adaptive threshold this method |
---|
84 | * is slightly more permissive than the offline one, and yelds to an increase |
---|
85 | * of false positives. best */ |
---|
86 | void |
---|
87 | aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out) |
---|
88 | { |
---|
89 | fvec_t *onset_keep = p->onset_keep; |
---|
90 | fvec_t *onset_proc = p->onset_proc; |
---|
91 | fvec_t *onset_peek = p->onset_peek; |
---|
92 | fvec_t *thresholded = p->thresholded; |
---|
93 | fvec_t *scratch = p->scratch; |
---|
94 | smpl_t mean = 0., median = 0.; |
---|
95 | uint_t length = p->win_post + p->win_pre + 1; |
---|
96 | uint_t j = 0; |
---|
97 | |
---|
98 | /* store onset in onset_keep */ |
---|
99 | /* shift all elements but last, then write last */ |
---|
100 | for (j = 0; j < length - 1; j++) { |
---|
101 | onset_keep->data[j] = onset_keep->data[j + 1]; |
---|
102 | onset_proc->data[j] = onset_keep->data[j]; |
---|
103 | } |
---|
104 | onset_keep->data[length - 1] = onset->data[0]; |
---|
105 | onset_proc->data[length - 1] = onset->data[0]; |
---|
106 | |
---|
107 | /* filter onset_proc */ |
---|
108 | /** \bug filtfilt calculated post+pre times, should be only once !? */ |
---|
109 | aubio_filter_do_filtfilt (p->biquad, onset_proc, scratch); |
---|
110 | |
---|
111 | /* calculate mean and median for onset_proc */ |
---|
112 | mean = fvec_mean (onset_proc); |
---|
113 | /* copy to scratch */ |
---|
114 | for (j = 0; j < length; j++) |
---|
115 | scratch->data[j] = onset_proc->data[j]; |
---|
116 | median = p->thresholdfn (scratch); |
---|
117 | |
---|
118 | /* shift peek array */ |
---|
119 | for (j = 0; j < 3 - 1; j++) |
---|
120 | onset_peek->data[j] = onset_peek->data[j + 1]; |
---|
121 | /* calculate new tresholded value */ |
---|
122 | thresholded->data[0] = |
---|
123 | onset_proc->data[p->win_post] - median - mean * p->threshold; |
---|
124 | onset_peek->data[2] = thresholded->data[0]; |
---|
125 | out->data[0] = (p->pickerfn) (onset_peek, 1); |
---|
126 | if (out->data[0]) { |
---|
127 | out->data[0] = fvec_quadratic_peak_pos (onset_peek, 1); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** this method returns the current value in the pick peaking buffer |
---|
132 | * after smoothing |
---|
133 | */ |
---|
134 | fvec_t * |
---|
135 | aubio_peakpicker_get_thresholded_input (aubio_peakpicker_t * p) |
---|
136 | { |
---|
137 | return p->thresholded; |
---|
138 | } |
---|
139 | |
---|
140 | uint_t |
---|
141 | aubio_peakpicker_set_threshold (aubio_peakpicker_t * p, smpl_t threshold) |
---|
142 | { |
---|
143 | p->threshold = threshold; |
---|
144 | return AUBIO_OK; |
---|
145 | } |
---|
146 | |
---|
147 | smpl_t |
---|
148 | aubio_peakpicker_get_threshold (aubio_peakpicker_t * p) |
---|
149 | { |
---|
150 | return p->threshold; |
---|
151 | } |
---|
152 | |
---|
153 | uint_t |
---|
154 | aubio_peakpicker_set_thresholdfn (aubio_peakpicker_t * p, |
---|
155 | aubio_thresholdfn_t thresholdfn) |
---|
156 | { |
---|
157 | p->thresholdfn = thresholdfn; |
---|
158 | return AUBIO_OK; |
---|
159 | } |
---|
160 | |
---|
161 | aubio_thresholdfn_t |
---|
162 | aubio_peakpicker_get_thresholdfn (aubio_peakpicker_t * p) |
---|
163 | { |
---|
164 | return (aubio_thresholdfn_t) (p->thresholdfn); |
---|
165 | } |
---|
166 | |
---|
167 | aubio_peakpicker_t * |
---|
168 | new_aubio_peakpicker (void) |
---|
169 | { |
---|
170 | aubio_peakpicker_t *t = AUBIO_NEW (aubio_peakpicker_t); |
---|
171 | t->threshold = 0.1; /* 0.0668; 0.33; 0.082; 0.033; */ |
---|
172 | t->win_post = 5; |
---|
173 | t->win_pre = 1; |
---|
174 | |
---|
175 | t->thresholdfn = (aubio_thresholdfn_t) (fvec_median); /* (fvec_mean); */ |
---|
176 | t->pickerfn = (aubio_pickerfn_t) (fvec_peakpick); |
---|
177 | |
---|
178 | t->scratch = new_fvec (t->win_post + t->win_pre + 1); |
---|
179 | t->onset_keep = new_fvec (t->win_post + t->win_pre + 1); |
---|
180 | t->onset_proc = new_fvec (t->win_post + t->win_pre + 1); |
---|
181 | t->onset_peek = new_fvec (3); |
---|
182 | t->thresholded = new_fvec (1); |
---|
183 | |
---|
184 | /* cutoff: low-pass filter with cutoff reduced frequency at 0.34 |
---|
185 | generated with octave butter function: [b,a] = butter(2, 0.34); |
---|
186 | */ |
---|
187 | t->biquad = new_aubio_filter_biquad (0.15998789, 0.31997577, 0.15998789, |
---|
188 | -0.59488894, 0.23484048); |
---|
189 | |
---|
190 | return t; |
---|
191 | } |
---|
192 | |
---|
193 | void |
---|
194 | del_aubio_peakpicker (aubio_peakpicker_t * p) |
---|
195 | { |
---|
196 | del_aubio_filter (p->biquad); |
---|
197 | del_fvec (p->onset_keep); |
---|
198 | del_fvec (p->onset_proc); |
---|
199 | del_fvec (p->onset_peek); |
---|
200 | del_fvec (p->thresholded); |
---|
201 | del_fvec (p->scratch); |
---|
202 | AUBIO_FREE (p); |
---|
203 | } |
---|