1 | /* |
---|
2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
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/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | /* This algorithm was developed by A. de Cheveigné 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" |
---|
31 | #include "fvec.h" |
---|
32 | #include "mathutils.h" |
---|
33 | #include "pitch/pitchyin.h" |
---|
34 | |
---|
35 | struct _aubio_pitchyin_t |
---|
36 | { |
---|
37 | fvec_t *yin; |
---|
38 | smpl_t tol; |
---|
39 | uint_t peak_pos; |
---|
40 | }; |
---|
41 | |
---|
42 | #if 0 |
---|
43 | /** compute difference function |
---|
44 | |
---|
45 | \param input input signal |
---|
46 | \param yinbuf output buffer to store difference function (half shorter than input) |
---|
47 | |
---|
48 | */ |
---|
49 | void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf); |
---|
50 | |
---|
51 | /** in place computation of the YIN cumulative normalised function |
---|
52 | |
---|
53 | \param yinbuf input signal (a square difference function), also used to store function |
---|
54 | |
---|
55 | */ |
---|
56 | void aubio_pitchyin_getcum (fvec_t * yinbuf); |
---|
57 | |
---|
58 | /** detect pitch in a YIN function |
---|
59 | |
---|
60 | \param yinbuf input buffer as computed by aubio_pitchyin_getcum |
---|
61 | |
---|
62 | */ |
---|
63 | uint_t aubio_pitchyin_getpitch (const fvec_t * yinbuf); |
---|
64 | #endif |
---|
65 | |
---|
66 | aubio_pitchyin_t * |
---|
67 | new_aubio_pitchyin (uint_t bufsize) |
---|
68 | { |
---|
69 | aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t); |
---|
70 | o->yin = new_fvec (bufsize / 2); |
---|
71 | o->tol = 0.15; |
---|
72 | o->peak_pos = 0; |
---|
73 | return o; |
---|
74 | } |
---|
75 | |
---|
76 | void |
---|
77 | del_aubio_pitchyin (aubio_pitchyin_t * o) |
---|
78 | { |
---|
79 | del_fvec (o->yin); |
---|
80 | AUBIO_FREE (o); |
---|
81 | } |
---|
82 | |
---|
83 | #if 0 |
---|
84 | /* outputs the difference function */ |
---|
85 | void |
---|
86 | aubio_pitchyin_diff (fvec_t * input, fvec_t * yin) |
---|
87 | { |
---|
88 | uint_t j, tau; |
---|
89 | smpl_t tmp; |
---|
90 | for (tau = 0; tau < yin->length; tau++) { |
---|
91 | yin->data[tau] = 0.; |
---|
92 | } |
---|
93 | for (tau = 1; tau < yin->length; tau++) { |
---|
94 | for (j = 0; j < yin->length; j++) { |
---|
95 | tmp = input->data[j] - input->data[j + tau]; |
---|
96 | yin->data[tau] += SQR (tmp); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | /* cumulative mean normalized difference function */ |
---|
102 | void |
---|
103 | aubio_pitchyin_getcum (fvec_t * yin) |
---|
104 | { |
---|
105 | uint_t tau; |
---|
106 | smpl_t tmp = 0.; |
---|
107 | yin->data[0] = 1.; |
---|
108 | //AUBIO_DBG("%f\t",yin->data[0]); |
---|
109 | for (tau = 1; tau < yin->length; tau++) { |
---|
110 | tmp += yin->data[tau]; |
---|
111 | yin->data[tau] *= tau / tmp; |
---|
112 | //AUBIO_DBG("%f\t",yin->data[tau]); |
---|
113 | } |
---|
114 | //AUBIO_DBG("\n"); |
---|
115 | } |
---|
116 | |
---|
117 | uint_t |
---|
118 | aubio_pitchyin_getpitch (const fvec_t * yin) |
---|
119 | { |
---|
120 | uint_t tau = 1; |
---|
121 | do { |
---|
122 | if (yin->data[tau] < 0.1) { |
---|
123 | while (yin->data[tau + 1] < yin->data[tau]) { |
---|
124 | tau++; |
---|
125 | } |
---|
126 | return tau; |
---|
127 | } |
---|
128 | tau++; |
---|
129 | } while (tau < yin->length); |
---|
130 | //AUBIO_DBG("No pitch found"); |
---|
131 | return 0; |
---|
132 | } |
---|
133 | #endif |
---|
134 | |
---|
135 | /* all the above in one */ |
---|
136 | void |
---|
137 | aubio_pitchyin_do (aubio_pitchyin_t * o, const fvec_t * input, fvec_t * out) |
---|
138 | { |
---|
139 | const smpl_t tol = o->tol; |
---|
140 | fvec_t* yin = o->yin; |
---|
141 | const smpl_t *input_data = input->data; |
---|
142 | const uint_t length = yin->length; |
---|
143 | smpl_t *yin_data = yin->data; |
---|
144 | uint_t j, tau; |
---|
145 | sint_t period; |
---|
146 | smpl_t tmp, tmp2 = 0.; |
---|
147 | |
---|
148 | yin_data[0] = 1.; |
---|
149 | for (tau = 1; tau < length; tau++) { |
---|
150 | yin_data[tau] = 0.; |
---|
151 | for (j = 0; j < length; j++) { |
---|
152 | tmp = input_data[j] - input_data[j + tau]; |
---|
153 | yin_data[tau] += SQR (tmp); |
---|
154 | } |
---|
155 | tmp2 += yin_data[tau]; |
---|
156 | if (tmp2 != 0) { |
---|
157 | yin->data[tau] *= tau / tmp2; |
---|
158 | } else { |
---|
159 | yin->data[tau] = 1.; |
---|
160 | } |
---|
161 | period = tau - 3; |
---|
162 | if (tau > 4 && (yin_data[period] < tol) && |
---|
163 | (yin_data[period] < yin_data[period + 1])) { |
---|
164 | o->peak_pos = (uint_t)period; |
---|
165 | out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos); |
---|
166 | return; |
---|
167 | } |
---|
168 | } |
---|
169 | o->peak_pos = (uint_t)fvec_min_elem (yin); |
---|
170 | out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos); |
---|
171 | } |
---|
172 | |
---|
173 | smpl_t |
---|
174 | aubio_pitchyin_get_confidence (aubio_pitchyin_t * o) { |
---|
175 | return 1. - o->yin->data[o->peak_pos]; |
---|
176 | } |
---|
177 | |
---|
178 | uint_t |
---|
179 | aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol) |
---|
180 | { |
---|
181 | o->tol = tol; |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | smpl_t |
---|
186 | aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o) |
---|
187 | { |
---|
188 | return o->tol; |
---|
189 | } |
---|