2 Copyright (C) 2003 Paul Brossier
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.
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.
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.
19 /* This algorithm was developped by A. de Cheveigne and H. Kawahara and
22 * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
23 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930.
25 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
28 #include "aubio_priv.h"
30 #include "mathutils.h"
31 #include "pitch/pitchyin.h"
33 struct _aubio_pitchyin_t {
38 /** compute difference function
40 \param input input signal
41 \param yinbuf output buffer to store difference function (half shorter than input)
44 void aubio_pitchyin_diff(fvec_t * input, fvec_t * yinbuf);
46 /** in place computation of the YIN cumulative normalised function
48 \param yinbuf input signal (a square difference function), also used to store function
51 void aubio_pitchyin_getcum(fvec_t * yinbuf);
53 /** detect pitch in a YIN function
55 \param yinbuf input buffer as computed by aubio_pitchyin_getcum
58 uint_t aubio_pitchyin_getpitch(fvec_t *yinbuf);
60 aubio_pitchyin_t * new_aubio_pitchyin (uint_t bufsize) {
61 aubio_pitchyin_t * o = AUBIO_NEW(aubio_pitchyin_t);
62 o->yin = new_fvec (bufsize/2, 1);
67 void del_aubio_pitchyin (aubio_pitchyin_t *o) {
72 /* outputs the difference function */
73 void aubio_pitchyin_diff(fvec_t * input, fvec_t * yin){
76 for (c=0;c<input->channels;c++)
78 for (tau=0;tau<yin->length;tau++)
80 yin->data[c][tau] = 0.;
82 for (tau=1;tau<yin->length;tau++)
84 for (j=0;j<yin->length;j++)
86 tmp = input->data[c][j] - input->data[c][j+tau];
87 yin->data[c][tau] += SQR(tmp);
93 /* cumulative mean normalized difference function */
94 void aubio_pitchyin_getcum(fvec_t * yin) {
97 for (c=0;c<yin->channels;c++)
100 yin->data[c][0] = 1.;
101 //AUBIO_DBG("%f\t",yin->data[c][0]);
102 for (tau=1;tau<yin->length;tau++)
104 tmp += yin->data[c][tau];
105 yin->data[c][tau] *= tau/tmp;
106 //AUBIO_DBG("%f\t",yin->data[c][tau]);
112 uint_t aubio_pitchyin_getpitch(fvec_t * yin) {
116 if(yin->data[c][tau] < 0.1) {
117 while (yin->data[c][tau+1] < yin->data[c][tau]) {
123 } while (tau<yin->length);
124 //AUBIO_DBG("No pitch found");
129 /* all the above in one */
130 void aubio_pitchyin_do(aubio_pitchyin_t *o, fvec_t * input, fvec_t * out){
132 fvec_t * yin = o->yin;
133 uint_t c , j,tau = 0;
135 smpl_t tmp = 0., tmp2 = 0.;
136 for (c = 0; c < input->channels; c++) {
137 yin->data[c][0] = 1.;
138 for (tau=1;tau<yin->length;tau++)
140 yin->data[c][tau] = 0.;
141 for (j=0;j<yin->length;j++)
143 tmp = input->data[c][j] - input->data[c][j+tau];
144 yin->data[c][tau] += SQR(tmp);
146 tmp2 += yin->data[c][tau];
147 yin->data[c][tau] *= tau/tmp2;
149 if(tau > 4 && (yin->data[c][period] < tol) &&
150 (yin->data[c][period] < yin->data[c][period+1])) {
151 out->data[c][0] = fvec_quadint(yin,period,1);
155 out->data[c][0] = fvec_quadint(yin,fvec_min_elem(yin),1);
162 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t *o, smpl_t tol) {
167 smpl_t aubio_pitchyin_get_tolerance (aubio_pitchyin_t *o) {