[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 | |
---|
[2ba3440] | 35 | struct _aubio_pitchyin_t { |
---|
| 36 | fvec_t * yin; |
---|
| 37 | smpl_t tol; |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | /** compute difference function |
---|
| 41 | |
---|
| 42 | \param input input signal |
---|
| 43 | \param yinbuf output buffer to store difference function (half shorter than input) |
---|
| 44 | |
---|
| 45 | */ |
---|
| 46 | void aubio_pitchyin_diff(fvec_t * input, fvec_t * yinbuf); |
---|
| 47 | |
---|
| 48 | /** in place computation of the YIN cumulative normalised function |
---|
| 49 | |
---|
| 50 | \param yinbuf input signal (a square difference function), also used to store function |
---|
| 51 | |
---|
| 52 | */ |
---|
| 53 | void aubio_pitchyin_getcum(fvec_t * yinbuf); |
---|
| 54 | |
---|
| 55 | /** detect pitch in a YIN function |
---|
| 56 | |
---|
| 57 | \param yinbuf input buffer as computed by aubio_pitchyin_getcum |
---|
| 58 | |
---|
| 59 | */ |
---|
| 60 | uint_t aubio_pitchyin_getpitch(fvec_t *yinbuf); |
---|
| 61 | |
---|
| 62 | aubio_pitchyin_t * new_aubio_pitchyin (uint_t bufsize) { |
---|
| 63 | aubio_pitchyin_t * o = AUBIO_NEW(aubio_pitchyin_t); |
---|
| 64 | o->yin = new_fvec (bufsize/2, 1); |
---|
| 65 | o->tol = 0.15; |
---|
| 66 | return o; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void del_aubio_pitchyin (aubio_pitchyin_t *o) { |
---|
| 70 | del_fvec(o->yin); |
---|
| 71 | AUBIO_FREE(o); |
---|
| 72 | } |
---|
| 73 | |
---|
[96fb8ad] | 74 | /* outputs the difference function */ |
---|
| 75 | void aubio_pitchyin_diff(fvec_t * input, fvec_t * yin){ |
---|
[a2f3555] | 76 | uint_t c,j,tau; |
---|
| 77 | smpl_t tmp; |
---|
| 78 | for (c=0;c<input->channels;c++) |
---|
| 79 | { |
---|
| 80 | for (tau=0;tau<yin->length;tau++) |
---|
| 81 | { |
---|
| 82 | yin->data[c][tau] = 0.; |
---|
| 83 | } |
---|
| 84 | for (tau=1;tau<yin->length;tau++) |
---|
| 85 | { |
---|
| 86 | for (j=0;j<yin->length;j++) |
---|
| 87 | { |
---|
| 88 | tmp = input->data[c][j] - input->data[c][j+tau]; |
---|
| 89 | yin->data[c][tau] += SQR(tmp); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
[96fb8ad] | 93 | } |
---|
| 94 | |
---|
| 95 | /* cumulative mean normalized difference function */ |
---|
| 96 | void aubio_pitchyin_getcum(fvec_t * yin) { |
---|
[a2f3555] | 97 | uint_t c,tau; |
---|
| 98 | smpl_t tmp; |
---|
| 99 | for (c=0;c<yin->channels;c++) |
---|
| 100 | { |
---|
| 101 | tmp = 0.; |
---|
| 102 | yin->data[c][0] = 1.; |
---|
| 103 | //AUBIO_DBG("%f\t",yin->data[c][0]); |
---|
| 104 | for (tau=1;tau<yin->length;tau++) |
---|
| 105 | { |
---|
| 106 | tmp += yin->data[c][tau]; |
---|
| 107 | yin->data[c][tau] *= tau/tmp; |
---|
| 108 | //AUBIO_DBG("%f\t",yin->data[c][tau]); |
---|
| 109 | } |
---|
| 110 | //AUBIO_DBG("\n"); |
---|
| 111 | } |
---|
[96fb8ad] | 112 | } |
---|
| 113 | |
---|
| 114 | uint_t aubio_pitchyin_getpitch(fvec_t * yin) { |
---|
[a2f3555] | 115 | uint_t c=0,tau=1; |
---|
| 116 | do |
---|
| 117 | { |
---|
| 118 | if(yin->data[c][tau] < 0.1) { |
---|
| 119 | while (yin->data[c][tau+1] < yin->data[c][tau]) { |
---|
| 120 | tau++; |
---|
| 121 | } |
---|
| 122 | return tau; |
---|
| 123 | } |
---|
| 124 | tau++; |
---|
| 125 | } while (tau<yin->length); |
---|
| 126 | //AUBIO_DBG("No pitch found"); |
---|
| 127 | return 0; |
---|
[96fb8ad] | 128 | } |
---|
| 129 | |
---|
[09b082d] | 130 | |
---|
| 131 | /* all the above in one */ |
---|
[2ba3440] | 132 | void aubio_pitchyin_do(aubio_pitchyin_t *o, fvec_t * input, fvec_t * out){ |
---|
| 133 | smpl_t tol = o->tol; |
---|
| 134 | fvec_t * yin = o->yin; |
---|
| 135 | uint_t c , j,tau = 0; |
---|
[a2f3555] | 136 | sint_t period; |
---|
| 137 | smpl_t tmp = 0., tmp2 = 0.; |
---|
[2ba3440] | 138 | for (c = 0; c < input->channels; c++) { |
---|
| 139 | yin->data[c][0] = 1.; |
---|
| 140 | for (tau=1;tau<yin->length;tau++) |
---|
[a2f3555] | 141 | { |
---|
[2ba3440] | 142 | yin->data[c][tau] = 0.; |
---|
| 143 | for (j=0;j<yin->length;j++) |
---|
| 144 | { |
---|
| 145 | tmp = input->data[c][j] - input->data[c][j+tau]; |
---|
| 146 | yin->data[c][tau] += SQR(tmp); |
---|
| 147 | } |
---|
| 148 | tmp2 += yin->data[c][tau]; |
---|
| 149 | yin->data[c][tau] *= tau/tmp2; |
---|
| 150 | period = tau-3; |
---|
| 151 | if(tau > 4 && (yin->data[c][period] < tol) && |
---|
| 152 | (yin->data[c][period] < yin->data[c][period+1])) { |
---|
[56ef7e1] | 153 | out->data[c][0] = fvec_quadint(yin,period,c); |
---|
[2ba3440] | 154 | goto beach; |
---|
| 155 | } |
---|
[a2f3555] | 156 | } |
---|
[56ef7e1] | 157 | out->data[c][0] = fvec_quadint(yin,fvec_min_elem(yin),c); |
---|
[2ba3440] | 158 | beach: |
---|
| 159 | continue; |
---|
[a2f3555] | 160 | } |
---|
| 161 | //return 0; |
---|
[09b082d] | 162 | } |
---|
| 163 | |
---|
[2ba3440] | 164 | uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t *o, smpl_t tol) { |
---|
| 165 | o->tol = tol; |
---|
| 166 | return 0; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | smpl_t aubio_pitchyin_get_tolerance (aubio_pitchyin_t *o) { |
---|
| 170 | return o->tol; |
---|
| 171 | } |
---|