2 Copyright (C) 2004, 2005 Mario Lang <mlang@delysid.org>
3 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
5 This file is part of aubio.
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.
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.
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/>.
22 #include "aubio_priv.h"
25 #include "mathutils.h"
26 #include "musicutils.h"
27 #include "spectral/fft.h"
28 #include "pitch/pitchfcomb.h"
38 struct _aubio_pitchfcomb_t
51 new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize)
53 aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t);
55 p->stepSize = hopsize;
56 p->winput = new_fvec (bufsize);
57 p->fftOut = new_cvec (bufsize);
58 p->fftLastPhase = new_fvec (bufsize);
59 p->fft = new_aubio_fft (bufsize);
60 p->win = new_aubio_window ("hanning", bufsize);
64 /* input must be stepsize long */
66 aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, const fvec_t * input, fvec_t * output)
68 uint_t k, l, maxharm = 0;
69 smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize;
70 aubio_fpeak_t peaks[MAX_PEAKS];
72 for (k = 0; k < MAX_PEAKS; k++) {
77 for (k = 0; k < input->length; k++) {
78 p->winput->data[k] = p->win->data[k] * input->data[k];
80 aubio_fft_do (p->fft, p->winput, p->fftOut);
82 for (k = 0; k <= p->fftSize / 2; k++) {
85 20. * LOG10 (2. * p->fftOut->norm[k] / (smpl_t) p->fftSize),
86 phase = p->fftOut->phas[k], tmp, bin;
88 /* compute phase difference */
89 tmp = phase - p->fftLastPhase->data[k];
90 p->fftLastPhase->data[k] = phase;
92 /* subtract expected phase difference */
93 tmp -= (smpl_t) k *phaseDifference;
95 /* map delta phase into +/- Pi interval */
96 tmp = aubio_unwrap2pi (tmp);
98 /* get deviation from bin frequency from the +/- Pi interval */
99 tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI);
101 /* compute the k-th partials' true bin */
102 bin = (smpl_t) k + tmp;
104 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) {
105 memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1));
107 peaks[0].db = magnitude;
112 for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) {
114 for (harmonic = 5; harmonic > 1; harmonic--) {
115 if (peaks[0].bin / peaks[l].bin < harmonic + .02 &&
116 peaks[0].bin / peaks[l].bin > harmonic - .02) {
117 if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) {
124 output->data[0] = peaks[k].bin;
125 /* quick hack to clean output a bit */
126 if (peaks[k].bin > 5000.)
127 output->data[0] = 0.;
131 del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
133 del_cvec (p->fftOut);
134 del_fvec (p->fftLastPhase);
136 del_fvec (p->winput);
137 del_aubio_fft (p->fft);