source: src/pitch/pitchfcomb.c @ e5757cf

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

src/pitch/pitchfcomb.c: update prototypes, make multichannel, remove unneeded samplerate parameter, now returns bin

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2   Copyright (C) 2004, 2005  Mario Lang <mlang@delysid.org>
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 "spectral/fft.h"
25#include "pitch/pitchfcomb.h"
26
27#define MAX_PEAKS 8
28
29typedef struct {
30  smpl_t bin;
31  smpl_t db;
32} aubio_fpeak_t;
33
34struct _aubio_pitchfcomb_t {
35  uint_t fftSize;
36  uint_t stepSize;
37  uint_t rate;
38  fvec_t * winput;
39  fvec_t * win;
40  cvec_t * fftOut;
41  fvec_t * fftLastPhase;
42  aubio_fft_t * fft;
43};
44
45aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels)
46{
47  aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);
48  p->fftSize      = bufsize;
49  p->stepSize     = hopsize;
50  p->winput       = new_fvec(bufsize,1);
51  p->fftOut       = new_cvec(bufsize,1);
52  p->fftLastPhase = new_fvec(bufsize, channels);
53  p->fft = new_aubio_fft(bufsize, 1);
54  p->win = new_aubio_window(bufsize, aubio_win_hanning);
55  return p;
56}
57
58/* input must be stepsize long */
59void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output)
60{
61  uint_t i, k, l, maxharm = 0;
62  smpl_t phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize;
63  aubio_fpeak_t peaks[MAX_PEAKS];
64
65  for (i = 0; i < input->channels; i++) {
66
67  for (k=0; k<MAX_PEAKS; k++) {
68    peaks[k].db = -200.;
69    peaks[k].bin = 0.;
70  }
71
72  for (k=0; k < input->length; k++){
73    p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];
74  }
75  aubio_fft_do(p->fft,p->winput,p->fftOut);
76
77  for (k=0; k<=p->fftSize/2; k++) {
78    smpl_t
79      magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),
80      phase     = p->fftOut->phas[0][k],
81      tmp, bin;
82
83    /* compute phase difference */
84    tmp = phase - p->fftLastPhase->data[i][k];
85    p->fftLastPhase->data[i][k] = phase;
86
87    /* subtract expected phase difference */
88    tmp -= (smpl_t)k*phaseDifference;
89
90    /* map delta phase into +/- Pi interval */
91    tmp = aubio_unwrap2pi(tmp);
92
93    /* get deviation from bin frequency from the +/- Pi interval */
94    tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);
95
96    /* compute the k-th partials' true bin */
97    bin = (smpl_t)k + tmp;
98
99    if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) {
100      memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1));
101      peaks[0].bin = bin;
102      peaks[0].db = magnitude;
103    }
104  }
105
106  k = 0;
107  for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) {
108    sint_t harmonic;
109    for (harmonic=5; harmonic>1; harmonic--) {
110      if (peaks[0].bin / peaks[l].bin < harmonic+.02 &&
111          peaks[0].bin / peaks[l].bin > harmonic-.02) {
112        if (harmonic > (sint_t)maxharm &&
113            peaks[0].db < peaks[l].db/2) {
114          maxharm = harmonic;
115          k = l;
116        }
117      }
118    }
119  }
120  output->data[i][0] = peaks[k].bin;
121  /* quick hack to clean output a bit */
122  if (peaks[k].bin > 5000.) output->data[i][0] = 0.;
123  }
124}
125
126void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
127{
128  del_cvec(p->fftOut);
129  del_fvec(p->fftLastPhase);
130  del_fvec(p->win);
131  del_fvec(p->winput);
132  del_aubio_fft(p->fft);
133  AUBIO_FREE(p);
134}
135
Note: See TracBrowser for help on using the repository browser.