source: src/pitchmcomb.c @ 9638f84

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

pitchmcomb.c: make sure all variables are initialised

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