1 | /* |
---|
2 | Copyright (C) 2003 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 "mathutils.h" |
---|
24 | #include "pitch/pitchmcomb.h" |
---|
25 | |
---|
26 | #define CAND_SWAP(a,b) { register aubio_spectralcandidate_t *t=(a);(a)=(b);(b)=t; } |
---|
27 | |
---|
28 | typedef struct _aubio_spectralpeak_t aubio_spectralpeak_t; |
---|
29 | typedef struct _aubio_spectralcandidate_t aubio_spectralcandidate_t; |
---|
30 | uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length); |
---|
31 | uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X); |
---|
32 | void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * oldmag); |
---|
33 | void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag); |
---|
34 | /* not used but useful : sort by amplitudes (or anything else) |
---|
35 | * sort_pitchpeak(peaks, length); |
---|
36 | */ |
---|
37 | /** spectral_peak comparison function (must return signed int) */ |
---|
38 | static sint_t aubio_pitchmcomb_sort_peak_comp(const void *x, const void *y); |
---|
39 | /** sort spectral_peak against their mag */ |
---|
40 | void aubio_pitchmcomb_sort_peak(aubio_spectralpeak_t * peaks, uint_t nbins); |
---|
41 | /** select the best candidates */ |
---|
42 | uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands); |
---|
43 | |
---|
44 | /** sort spectral_candidate against their comb ene */ |
---|
45 | void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins); |
---|
46 | /** sort spectral_candidate against their frequency */ |
---|
47 | void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins); |
---|
48 | |
---|
49 | struct _aubio_pitchmcomb_t { |
---|
50 | smpl_t threshold; /**< offset threshold [0.033 or 0.01] */ |
---|
51 | smpl_t alpha; /**< normalisation exponent [9] */ |
---|
52 | smpl_t cutoff; /**< low-pass filter cutoff [0.34, 1] */ |
---|
53 | smpl_t tol; /**< tolerance [0.05] */ |
---|
54 | // smpl_t tau; /**< frequency precision [44100/4096] */ |
---|
55 | uint_t win_post; /**< median filter window length */ |
---|
56 | uint_t win_pre; /**< median filter window */ |
---|
57 | uint_t ncand; /**< maximum number of candidates (combs) */ |
---|
58 | uint_t npartials; /**< maximum number of partials per combs */ |
---|
59 | uint_t count; /**< picked picks */ |
---|
60 | uint_t goodcandidate; /**< best candidate */ |
---|
61 | uint_t spec_partition; /**< spectrum partition to consider */ |
---|
62 | aubio_spectralpeak_t * peaks; /**< up to length win/spec_partition */ |
---|
63 | aubio_spectralcandidate_t ** candidates; /** up to five candidates */ |
---|
64 | /* some scratch pads */ |
---|
65 | /** \bug (unnecessary copied from fftgrain?) */ |
---|
66 | fvec_t * newmag; /**< vec to store mag */ |
---|
67 | fvec_t * scratch; /**< vec to store modified mag */ |
---|
68 | fvec_t * scratch2; /**< vec to compute moving median */ |
---|
69 | fvec_t * theta; /**< vec to store phase */ |
---|
70 | smpl_t phasediff; |
---|
71 | smpl_t phasefreq; |
---|
72 | /** threshfn: name or handle of fn for computing adaptive threshold [median] */ |
---|
73 | /** aubio_thresholdfn_t thresholdfn; */ |
---|
74 | /** picker: name or handle of fn for picking event times [quadpick] */ |
---|
75 | /** aubio_pickerfn_t pickerfn; */ |
---|
76 | }; |
---|
77 | |
---|
78 | /** spectral peak object */ |
---|
79 | struct _aubio_spectralpeak_t { |
---|
80 | uint_t bin; /**< bin [0-(length-1)] */ |
---|
81 | smpl_t ebin; /**< estimated bin */ |
---|
82 | smpl_t mag; /**< peak magnitude */ |
---|
83 | }; |
---|
84 | |
---|
85 | /** spectral candidates array object */ |
---|
86 | struct _aubio_spectralcandidate_t { |
---|
87 | smpl_t ebin; /**< interpolated bin */ |
---|
88 | smpl_t * ecomb; /**< comb */ |
---|
89 | smpl_t ene; /**< candidate energy */ |
---|
90 | smpl_t len; /**< length */ |
---|
91 | }; |
---|
92 | |
---|
93 | |
---|
94 | void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output) { |
---|
95 | uint_t i,j; |
---|
96 | smpl_t instfreq; |
---|
97 | fvec_t * newmag = (fvec_t *)p->newmag; |
---|
98 | //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta; |
---|
99 | /* copy incoming grain to newmag */ |
---|
100 | for (i=0; i< fftgrain->channels; i++) { |
---|
101 | for (j=0; j< newmag->length; j++) |
---|
102 | newmag->data[0][j]=fftgrain->norm[i][j]; |
---|
103 | /* detect only if local energy > 10. */ |
---|
104 | //if (fvec_local_energy(newmag)>10.) { |
---|
105 | //hfc = fvec_local_hfc(newmag); //not used |
---|
106 | aubio_pitchmcomb_spectral_pp(p, newmag); |
---|
107 | aubio_pitchmcomb_combdet(p,newmag); |
---|
108 | //aubio_pitchmcomb_sort_cand_freq(p->candidates,p->ncand); |
---|
109 | //return p->candidates[p->goodcandidate]->ebin; |
---|
110 | j = (uint_t)FLOOR(p->candidates[p->goodcandidate]->ebin+.5); |
---|
111 | instfreq = aubio_unwrap2pi(fftgrain->phas[i][j] |
---|
112 | - p->theta->data[i][j] - j*p->phasediff); |
---|
113 | instfreq *= p->phasefreq; |
---|
114 | /* store phase for next run */ |
---|
115 | for (j=0; j< p->theta->length; j++) { |
---|
116 | p->theta->data[i][j]=fftgrain->phas[i][j]; |
---|
117 | } |
---|
118 | //return p->candidates[p->goodcandidate]->ebin; |
---|
119 | output->data[i][0] = FLOOR(p->candidates[p->goodcandidate]->ebin+.5) + instfreq; |
---|
120 | /*} else { |
---|
121 | return -1.; |
---|
122 | }*/ |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain, |
---|
127 | smpl_t * cands) { |
---|
128 | uint_t i=0,j; |
---|
129 | uint_t k; |
---|
130 | fvec_t * newmag = (fvec_t *)p->newmag; |
---|
131 | aubio_spectralcandidate_t ** scands = |
---|
132 | (aubio_spectralcandidate_t **)(p->candidates); |
---|
133 | //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta; |
---|
134 | /* copy incoming grain to newmag */ |
---|
135 | for (j=0; j< newmag->length; j++) |
---|
136 | newmag->data[i][j]=fftgrain->norm[i][j]; |
---|
137 | /* detect only if local energy > 10. */ |
---|
138 | if (fvec_local_energy(newmag)>10.) { |
---|
139 | /* hfc = fvec_local_hfc(newmag); do not use */ |
---|
140 | aubio_pitchmcomb_spectral_pp(p, newmag); |
---|
141 | aubio_pitchmcomb_combdet(p,newmag); |
---|
142 | aubio_pitchmcomb_sort_cand_freq(scands,p->ncand); |
---|
143 | /* store ncand comb energies in cands[1:ncand] */ |
---|
144 | for (k = 0; k<p->ncand; k++) |
---|
145 | cands[k] = p->candidates[k]->ene; |
---|
146 | /* store ncand[end] freq in cands[end] */ |
---|
147 | cands[p->ncand] = p->candidates[p->ncand-1]->ebin; |
---|
148 | return 1; |
---|
149 | } else { |
---|
150 | for (k = 0; k<p->ncand; k++) |
---|
151 | cands[k] = 0; |
---|
152 | return 0; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * newmag) { |
---|
157 | fvec_t * mag = (fvec_t *)p->scratch; |
---|
158 | fvec_t * tmp = (fvec_t *)p->scratch2; |
---|
159 | uint_t i=0,j; |
---|
160 | uint_t length = mag->length; |
---|
161 | /* copy newmag to mag (scracth) */ |
---|
162 | for (j=0;j<length;j++) { |
---|
163 | mag->data[i][j] = newmag->data[i][j]; |
---|
164 | } |
---|
165 | fvec_min_removal(mag); /* min removal */ |
---|
166 | fvec_alpha_normalise(mag,p->alpha); /* alpha normalisation */ |
---|
167 | /* skipped */ /* low pass filtering */ |
---|
168 | /** \bug fvec_moving_thres may write out of bounds */ |
---|
169 | fvec_adapt_thres(mag,tmp,p->win_post,p->win_pre,i); /* adaptative threshold */ |
---|
170 | fvec_add(mag,-p->threshold); /* fixed threshold */ |
---|
171 | { |
---|
172 | aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks; |
---|
173 | uint_t count; |
---|
174 | /* return bin and ebin */ |
---|
175 | count = aubio_pitchmcomb_quadpick(peaks,mag); |
---|
176 | for (j=0;j<count;j++) |
---|
177 | peaks[j].mag = newmag->data[i][peaks[j].bin]; |
---|
178 | /* reset non peaks */ |
---|
179 | for (j=count;j<length;j++) |
---|
180 | peaks[j].mag = 0.; |
---|
181 | p->peaks = peaks; |
---|
182 | p->count = count; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag) { |
---|
187 | aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks; |
---|
188 | aubio_spectralcandidate_t ** candidate = |
---|
189 | (aubio_spectralcandidate_t **)p->candidates; |
---|
190 | |
---|
191 | /* parms */ |
---|
192 | uint_t N = p->npartials; /* maximum number of partials to be considered 10 */ |
---|
193 | uint_t M = p->ncand; /* maximum number of combs to be considered 5 */ |
---|
194 | uint_t length = newmag->length; |
---|
195 | uint_t count = p->count; |
---|
196 | uint_t k; |
---|
197 | uint_t l; |
---|
198 | uint_t d; |
---|
199 | uint_t curlen = 0; |
---|
200 | |
---|
201 | smpl_t delta2; |
---|
202 | smpl_t xx; |
---|
203 | uint_t position = 0; |
---|
204 | |
---|
205 | uint_t root_peak = 0; |
---|
206 | uint_t tmpl = 0; |
---|
207 | smpl_t tmpene = 0.; |
---|
208 | |
---|
209 | /* get the biggest peak in the spectrum */ |
---|
210 | root_peak = aubio_pitchmcomb_get_root_peak(peaks,count); |
---|
211 | /* not enough partials in highest notes, could be forced */ |
---|
212 | //if (peaks[root_peak].ebin >= aubio_miditofreq(85.)/p->tau) N=2; |
---|
213 | //if (peaks[root_peak].ebin >= aubio_miditofreq(90.)/p->tau) N=1; |
---|
214 | /* now calculate the energy of each of the 5 combs */ |
---|
215 | for (l=0;l<M;l++) { |
---|
216 | smpl_t scaler = (1./(l+1.)); |
---|
217 | candidate[l]->ene = 0.; /* reset ene and len sums */ |
---|
218 | candidate[l]->len = 0.; |
---|
219 | candidate[l]->ebin=scaler*peaks[root_peak].ebin; |
---|
220 | /* if less than N peaks available, curlen < N */ |
---|
221 | if (candidate[l]->ebin != 0.) |
---|
222 | curlen = (uint_t)FLOOR(length/(candidate[l]->ebin)); |
---|
223 | curlen = (N < curlen )? N : curlen; |
---|
224 | /* fill candidate[l]->ecomb[k] with (k+1)*candidate[l]->ebin */ |
---|
225 | for (k=0;k<curlen;k++) |
---|
226 | candidate[l]->ecomb[k]=(candidate[l]->ebin)*(k+1.); |
---|
227 | for (k=curlen;k<length;k++) |
---|
228 | candidate[l]->ecomb[k]=0.; |
---|
229 | /* for each in candidate[l]->ecomb[k] */ |
---|
230 | for (k=0;k<curlen;k++) { |
---|
231 | xx = 100000.; |
---|
232 | /** get the candidate->ecomb the closer to peaks.ebin |
---|
233 | * (to cope with the inharmonicity)*/ |
---|
234 | for (d=0;d<count;d++) { |
---|
235 | delta2 = ABS(candidate[l]->ecomb[k]-peaks[d].ebin); |
---|
236 | if (delta2 <= xx) { |
---|
237 | position = d; |
---|
238 | xx = delta2; |
---|
239 | } |
---|
240 | } |
---|
241 | /* for a Q factor of 17, maintaining "constant Q filtering", |
---|
242 | * and sum energy and length over non null combs */ |
---|
243 | if ( 17. * xx < candidate[l]->ecomb[k] ) { |
---|
244 | candidate[l]->ecomb[k]=peaks[position].ebin; |
---|
245 | candidate[l]->ene += /* ecomb rounded to nearest int */ |
---|
246 | POW(newmag->data[0][(uint_t)FLOOR(candidate[l]->ecomb[k]+.5)],0.25); |
---|
247 | candidate[l]->len += 1./curlen; |
---|
248 | } else |
---|
249 | candidate[l]->ecomb[k]=0.; |
---|
250 | } |
---|
251 | /* punishment */ |
---|
252 | /*if (candidate[l]->len<0.6) |
---|
253 | candidate[l]->ene=0.; */ |
---|
254 | /* remember best candidate energy (in polyphonic, could check for |
---|
255 | * tmpene*1.1 < candidate->ene to reduce jumps towards low frequencies) */ |
---|
256 | if (tmpene < candidate[l]->ene) { |
---|
257 | tmpl = l; |
---|
258 | tmpene = candidate[l]->ene; |
---|
259 | } |
---|
260 | } |
---|
261 | //p->candidates=candidate; |
---|
262 | //p->peaks=peaks; |
---|
263 | p->goodcandidate = tmpl; |
---|
264 | } |
---|
265 | |
---|
266 | /** T=quadpick(X): return indices of elements of X which are peaks and positive |
---|
267 | * exact peak positions are retrieved by quadratic interpolation |
---|
268 | * |
---|
269 | * \bug peak-picking too picky, sometimes counts too many peaks ? |
---|
270 | */ |
---|
271 | uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X){ |
---|
272 | uint_t i, j, ispeak, count = 0; |
---|
273 | for (i=0;i<X->channels;i++) |
---|
274 | for (j=1;j<X->length-1;j++) { |
---|
275 | ispeak = fvec_peakpick(X,j); |
---|
276 | if (ispeak) { |
---|
277 | count += ispeak; |
---|
278 | spectral_peaks[count-1].bin = j; |
---|
279 | spectral_peaks[count-1].ebin = fvec_quadint(X, j, i) - 1.; |
---|
280 | } |
---|
281 | } |
---|
282 | return count; |
---|
283 | } |
---|
284 | |
---|
285 | /* get predominant partial */ |
---|
286 | uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length) { |
---|
287 | uint_t i,pos=0; |
---|
288 | smpl_t tmp = 0.; |
---|
289 | for (i=0;i<length;i++) |
---|
290 | if (tmp <= peaks[i].mag) { |
---|
291 | pos = i; |
---|
292 | tmp = peaks[i].mag; |
---|
293 | } |
---|
294 | return pos; |
---|
295 | } |
---|
296 | |
---|
297 | void aubio_pitchmcomb_sort_peak(aubio_spectralpeak_t * peaks, uint_t nbins) { |
---|
298 | qsort(peaks, nbins, sizeof(aubio_spectralpeak_t), |
---|
299 | aubio_pitchmcomb_sort_peak_comp); |
---|
300 | } |
---|
301 | static sint_t aubio_pitchmcomb_sort_peak_comp(const void *x, const void *y) { |
---|
302 | return (((aubio_spectralpeak_t *)y)->mag - ((aubio_spectralpeak_t *)x)->mag); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins) { |
---|
307 | uint_t cur = 0; |
---|
308 | uint_t run = 0; |
---|
309 | for (cur=0;cur<nbins;cur++) { |
---|
310 | run = cur + 1; |
---|
311 | for (run=cur;run<nbins;run++) { |
---|
312 | if(candidates[run]->ene > candidates[cur]->ene) |
---|
313 | CAND_SWAP(candidates[run], candidates[cur]); |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins) { |
---|
320 | uint_t cur = 0; |
---|
321 | uint_t run = 0; |
---|
322 | for (cur=0;cur<nbins;cur++) { |
---|
323 | run = cur + 1; |
---|
324 | for (run=cur;run<nbins;run++) { |
---|
325 | if(candidates[run]->ebin < candidates[cur]->ebin) |
---|
326 | CAND_SWAP(candidates[run], candidates[cur]); |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels) { |
---|
332 | aubio_pitchmcomb_t * p = AUBIO_NEW(aubio_pitchmcomb_t); |
---|
333 | /* bug: should check if size / 8 > post+pre+1 */ |
---|
334 | uint_t i, j; |
---|
335 | uint_t spec_size; |
---|
336 | p->spec_partition = 4; |
---|
337 | p->ncand = 5; |
---|
338 | p->npartials = 5; |
---|
339 | p->cutoff = 1.; |
---|
340 | p->threshold = 0.01; |
---|
341 | p->win_post = 8; |
---|
342 | p->win_pre = 7; |
---|
343 | // p->tau = samplerate/bufsize; |
---|
344 | p->alpha = 9.; |
---|
345 | p->goodcandidate = 0; |
---|
346 | p->phasefreq = bufsize/hopsize/TWO_PI; |
---|
347 | p->phasediff = TWO_PI*hopsize/bufsize; |
---|
348 | spec_size = bufsize/p->spec_partition; |
---|
349 | //p->pickerfn = quadpick; |
---|
350 | //p->biquad = new_biquad(0.1600,0.3200,0.1600, -0.5949, 0.2348); |
---|
351 | /* allocate temp memory */ |
---|
352 | p->newmag = new_fvec(spec_size,1); |
---|
353 | /* array for median */ |
---|
354 | p->scratch = new_fvec(spec_size,1); |
---|
355 | /* array for phase */ |
---|
356 | p->theta = new_fvec(spec_size,channels); |
---|
357 | /* array for adaptative threshold */ |
---|
358 | p->scratch2 = new_fvec(p->win_post+p->win_pre+1,1); |
---|
359 | /* array of spectral peaks */ |
---|
360 | p->peaks = AUBIO_ARRAY(aubio_spectralpeak_t,spec_size); |
---|
361 | for (i = 0; i < spec_size; i++) { |
---|
362 | p->peaks[i].bin = 0.; |
---|
363 | p->peaks[i].ebin = 0.; |
---|
364 | p->peaks[i].mag = 0.; |
---|
365 | } |
---|
366 | /* array of pointers to spectral candidates */ |
---|
367 | p->candidates = AUBIO_ARRAY(aubio_spectralcandidate_t *,p->ncand); |
---|
368 | for (i=0;i<p->ncand;i++) { |
---|
369 | p->candidates[i] = AUBIO_NEW(aubio_spectralcandidate_t); |
---|
370 | p->candidates[i]->ecomb = AUBIO_ARRAY(smpl_t, spec_size); |
---|
371 | for (j=0; j < spec_size; j++) { |
---|
372 | p->candidates[i]->ecomb[j] = 0.; |
---|
373 | } |
---|
374 | p->candidates[i]->ene = 0.; |
---|
375 | p->candidates[i]->ebin = 0.; |
---|
376 | p->candidates[i]->len = 0.; |
---|
377 | } |
---|
378 | return p; |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | void del_aubio_pitchmcomb (aubio_pitchmcomb_t *p) { |
---|
383 | uint_t i; |
---|
384 | del_fvec(p->newmag); |
---|
385 | del_fvec(p->scratch); |
---|
386 | del_fvec(p->theta); |
---|
387 | del_fvec(p->scratch2); |
---|
388 | AUBIO_FREE(p->peaks); |
---|
389 | for (i=0;i<p->ncand;i++) { |
---|
390 | AUBIO_FREE(p->candidates[i]->ecomb); |
---|
391 | AUBIO_FREE(p->candidates[i]); |
---|
392 | } |
---|
393 | AUBIO_FREE(p->candidates); |
---|
394 | AUBIO_FREE(p); |
---|
395 | } |
---|