source: src/pitch/pitchfcomb.c @ e6a78ea

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

src: update all headers to GPLv3

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2  Copyright (C) 2004, 2005  Mario Lang <mlang@delysid.org>
3  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4
5  This file is part of aubio.
6
7  aubio is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11
12  aubio is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16
17  You should have received a copy of the GNU General Public License
18  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22#include "aubio_priv.h"
23#include "fvec.h"
24#include "cvec.h"
25#include "mathutils.h"
26#include "musicutils.h"
27#include "spectral/fft.h"
28#include "pitch/pitchfcomb.h"
29
30#define MAX_PEAKS 8
31
32typedef struct {
33  smpl_t bin;
34  smpl_t db;
35} aubio_fpeak_t;
36
37struct _aubio_pitchfcomb_t {
38  uint_t fftSize;
39  uint_t stepSize;
40  uint_t rate;
41  fvec_t * winput;
42  fvec_t * win;
43  cvec_t * fftOut;
44  fvec_t * fftLastPhase;
45  aubio_fft_t * fft;
46};
47
48aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels)
49{
50  aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);
51  p->fftSize      = bufsize;
52  p->stepSize     = hopsize;
53  p->winput       = new_fvec(bufsize,1);
54  p->fftOut       = new_cvec(bufsize,1);
55  p->fftLastPhase = new_fvec(bufsize, channels);
56  p->fft = new_aubio_fft(bufsize, 1);
57  p->win = new_aubio_window("hanning", bufsize);
58  return p;
59}
60
61/* input must be stepsize long */
62void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output)
63{
64  uint_t i, k, l, maxharm = 0;
65  smpl_t phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize;
66  aubio_fpeak_t peaks[MAX_PEAKS];
67
68  for (i = 0; i < input->channels; i++) {
69
70  for (k=0; k<MAX_PEAKS; k++) {
71    peaks[k].db = -200.;
72    peaks[k].bin = 0.;
73  }
74
75  for (k=0; k < input->length; k++){
76    p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];
77  }
78  aubio_fft_do(p->fft,p->winput,p->fftOut);
79
80  for (k=0; k<=p->fftSize/2; k++) {
81    smpl_t
82      magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),
83      phase     = p->fftOut->phas[0][k],
84      tmp, bin;
85
86    /* compute phase difference */
87    tmp = phase - p->fftLastPhase->data[i][k];
88    p->fftLastPhase->data[i][k] = phase;
89
90    /* subtract expected phase difference */
91    tmp -= (smpl_t)k*phaseDifference;
92
93    /* map delta phase into +/- Pi interval */
94    tmp = aubio_unwrap2pi(tmp);
95
96    /* get deviation from bin frequency from the +/- Pi interval */
97    tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);
98
99    /* compute the k-th partials' true bin */
100    bin = (smpl_t)k + tmp;
101
102    if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) {
103      memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1));
104      peaks[0].bin = bin;
105      peaks[0].db = magnitude;
106    }
107  }
108
109  k = 0;
110  for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) {
111    sint_t harmonic;
112    for (harmonic=5; harmonic>1; harmonic--) {
113      if (peaks[0].bin / peaks[l].bin < harmonic+.02 &&
114          peaks[0].bin / peaks[l].bin > harmonic-.02) {
115        if (harmonic > (sint_t)maxharm &&
116            peaks[0].db < peaks[l].db/2) {
117          maxharm = harmonic;
118          k = l;
119        }
120      }
121    }
122  }
123  output->data[i][0] = peaks[k].bin;
124  /* quick hack to clean output a bit */
125  if (peaks[k].bin > 5000.) output->data[i][0] = 0.;
126  }
127}
128
129void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
130{
131  del_cvec(p->fftOut);
132  del_fvec(p->fftLastPhase);
133  del_fvec(p->win);
134  del_fvec(p->winput);
135  del_aubio_fft(p->fft);
136  AUBIO_FREE(p);
137}
138
Note: See TracBrowser for help on using the repository browser.