source: src/pitchmcomb.c @ d94f98b

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

fix mcomb: comput instfreq, use pow(mag,0.25) for comb summation
fix mcomb: comput instfreq, use pow(mag,0.25) for comb summation

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