source: src/pitch/pitchyin.c @ 75be651

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

src/pitch/: indent

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[96fb8ad]1/*
[e6a78ea]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[e6a78ea]4  This file is part of aubio.
[96fb8ad]5
[e6a78ea]6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[96fb8ad]18
19*/
20
21/* This algorithm was developped by A. de Cheveigne and H. Kawahara and
22 * published in:
23 *
24 * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
25 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930. 
26 *
27 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
28 */
29
30#include "aubio_priv.h"
[6c7d49b]31#include "fvec.h"
[96fb8ad]32#include "mathutils.h"
[2d8cffa]33#include "pitch/pitchyin.h"
[96fb8ad]34
[fddfa64]35struct _aubio_pitchyin_t
36{
37  fvec_t *yin;
[2ba3440]38  smpl_t tol;
39};
40
41/** compute difference function
42 
43  \param input input signal
44  \param yinbuf output buffer to store difference function (half shorter than input)
45
46*/
[fddfa64]47void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);
[2ba3440]48
49/** in place computation of the YIN cumulative normalised function
50 
51  \param yinbuf input signal (a square difference function), also used to store function
52
53*/
[fddfa64]54void aubio_pitchyin_getcum (fvec_t * yinbuf);
[2ba3440]55
56/** detect pitch in a YIN function
57 
58  \param yinbuf input buffer as computed by aubio_pitchyin_getcum
59
60*/
[fddfa64]61uint_t aubio_pitchyin_getpitch (fvec_t * yinbuf);
[2ba3440]62
[fddfa64]63aubio_pitchyin_t *
64new_aubio_pitchyin (uint_t bufsize)
65{
66  aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t);
67  o->yin = new_fvec (bufsize / 2, 1);
[2ba3440]68  o->tol = 0.15;
69  return o;
70}
71
[fddfa64]72void
73del_aubio_pitchyin (aubio_pitchyin_t * o)
74{
75  del_fvec (o->yin);
76  AUBIO_FREE (o);
[2ba3440]77}
78
[96fb8ad]79/* outputs the difference function */
[fddfa64]80void
81aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
82{
83  uint_t c, j, tau;
[a2f3555]84  smpl_t tmp;
[fddfa64]85  for (c = 0; c < input->channels; c++) {
86    for (tau = 0; tau < yin->length; tau++) {
[a2f3555]87      yin->data[c][tau] = 0.;
88    }
[fddfa64]89    for (tau = 1; tau < yin->length; tau++) {
90      for (j = 0; j < yin->length; j++) {
91        tmp = input->data[c][j] - input->data[c][j + tau];
92        yin->data[c][tau] += SQR (tmp);
[a2f3555]93      }
94    }
95  }
[96fb8ad]96}
97
98/* cumulative mean normalized difference function */
[fddfa64]99void
100aubio_pitchyin_getcum (fvec_t * yin)
101{
102  uint_t c, tau;
[a2f3555]103  smpl_t tmp;
[fddfa64]104  for (c = 0; c < yin->channels; c++) {
[a2f3555]105    tmp = 0.;
106    yin->data[c][0] = 1.;
107    //AUBIO_DBG("%f\t",yin->data[c][0]);
[fddfa64]108    for (tau = 1; tau < yin->length; tau++) {
[a2f3555]109      tmp += yin->data[c][tau];
[fddfa64]110      yin->data[c][tau] *= tau / tmp;
[a2f3555]111      //AUBIO_DBG("%f\t",yin->data[c][tau]);
112    }
113    //AUBIO_DBG("\n");
114  }
[96fb8ad]115}
116
[fddfa64]117uint_t
118aubio_pitchyin_getpitch (fvec_t * yin)
119{
120  uint_t c = 0, tau = 1;
121  do {
122    if (yin->data[c][tau] < 0.1) {
123      while (yin->data[c][tau + 1] < yin->data[c][tau]) {
[a2f3555]124        tau++;
125      }
126      return tau;
127    }
128    tau++;
[fddfa64]129  } while (tau < yin->length);
[a2f3555]130  //AUBIO_DBG("No pitch found");
131  return 0;
[96fb8ad]132}
133
[09b082d]134
135/* all the above in one */
[fddfa64]136void
137aubio_pitchyin_do (aubio_pitchyin_t * o, fvec_t * input, fvec_t * out)
138{
[2ba3440]139  smpl_t tol = o->tol;
[fddfa64]140  fvec_t *yin = o->yin;
141  uint_t c, j, tau = 0;
[a2f3555]142  sint_t period;
143  smpl_t tmp = 0., tmp2 = 0.;
[2ba3440]144  for (c = 0; c < input->channels; c++) {
145    yin->data[c][0] = 1.;
[fddfa64]146    for (tau = 1; tau < yin->length; tau++) {
[2ba3440]147      yin->data[c][tau] = 0.;
[fddfa64]148      for (j = 0; j < yin->length; j++) {
149        tmp = input->data[c][j] - input->data[c][j + tau];
150        yin->data[c][tau] += SQR (tmp);
[2ba3440]151      }
152      tmp2 += yin->data[c][tau];
[fddfa64]153      yin->data[c][tau] *= tau / tmp2;
154      period = tau - 3;
155      if (tau > 4 && (yin->data[c][period] < tol) &&
156          (yin->data[c][period] < yin->data[c][period + 1])) {
157        out->data[c][0] = fvec_quadint (yin, period, c);
[2ba3440]158        goto beach;
159      }
[a2f3555]160    }
[fddfa64]161    out->data[c][0] = fvec_quadint (yin, fvec_min_elem (yin), c);
162  beach:
[2ba3440]163    continue;
[a2f3555]164  }
165  //return 0;
[09b082d]166}
167
[fddfa64]168uint_t
169aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol)
170{
[2ba3440]171  o->tol = tol;
172  return 0;
173}
174
[fddfa64]175smpl_t
176aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o)
177{
[2ba3440]178  return o->tol;
179}
Note: See TracBrowser for help on using the repository browser.