source: src/pitchmcomb.c @ 9771488

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

activate adapt thresh, reduce numb partials for high frequencies, add support for samplerate
activate adapt thresh, reduce numb partials for high frequencies, add support for samplerate

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