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