source: src/pitch/pitchmcomb.c @ 06f2d78

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 06f2d78 was 9a12264, checked in by Paul Brossier <piem@piem.org>, 15 years ago

src/pitch/pitchmcomb.c: hide aubio_pitch_cands

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