matthiasm@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
matthiasm@0
|
2
|
Chris@9
|
3 /*
|
Chris@9
|
4 pYIN - A fundamental frequency estimator for monophonic audio
|
Chris@9
|
5 Centre for Digital Music, Queen Mary, University of London.
|
Chris@9
|
6
|
Chris@9
|
7 This program is free software; you can redistribute it and/or
|
Chris@9
|
8 modify it under the terms of the GNU General Public License as
|
Chris@9
|
9 published by the Free Software Foundation; either version 2 of the
|
Chris@9
|
10 License, or (at your option) any later version. See the file
|
Chris@9
|
11 COPYING included with this distribution for more information.
|
Chris@9
|
12 */
|
Chris@9
|
13
|
matthiasm@35
|
14 #include "YinVamp.h"
|
matthiasm@0
|
15 #include "MonoNote.h"
|
matthiasm@0
|
16
|
matthiasm@0
|
17 #include "vamp-sdk/FFT.h"
|
matthiasm@0
|
18
|
matthiasm@0
|
19 #include <vector>
|
matthiasm@0
|
20 #include <algorithm>
|
matthiasm@0
|
21
|
matthiasm@0
|
22 #include <cstdio>
|
matthiasm@0
|
23 #include <cmath>
|
matthiasm@0
|
24 #include <complex>
|
matthiasm@0
|
25
|
matthiasm@0
|
26 using std::string;
|
matthiasm@0
|
27 using std::vector;
|
matthiasm@0
|
28 using Vamp::RealTime;
|
matthiasm@0
|
29
|
matthiasm@0
|
30
|
matthiasm@35
|
31 YinVamp::YinVamp(float inputSampleRate) :
|
matthiasm@0
|
32 Plugin(inputSampleRate),
|
matthiasm@0
|
33 m_channels(0),
|
matthiasm@0
|
34 m_stepSize(256),
|
matthiasm@0
|
35 m_blockSize(2048),
|
matthiasm@0
|
36 m_fmin(40),
|
matthiasm@58
|
37 m_fmax(1600),
|
matthiasm@0
|
38 m_yin(2048, inputSampleRate, 0.0),
|
matthiasm@0
|
39 m_outNoF0(0),
|
matthiasm@0
|
40 m_outNoPeriodicity(0),
|
matthiasm@0
|
41 m_outNoRms(0),
|
matthiasm@0
|
42 m_outNoSalience(0),
|
matthiasm@0
|
43 m_yinParameter(0.15f),
|
Chris@4
|
44 m_outputUnvoiced(2.0f)
|
matthiasm@0
|
45 {
|
matthiasm@0
|
46 }
|
matthiasm@0
|
47
|
matthiasm@35
|
48 YinVamp::~YinVamp()
|
matthiasm@0
|
49 {
|
matthiasm@0
|
50 }
|
matthiasm@0
|
51
|
matthiasm@0
|
52 string
|
matthiasm@35
|
53 YinVamp::getIdentifier() const
|
matthiasm@0
|
54 {
|
matthiasm@0
|
55 return "yin";
|
matthiasm@0
|
56 }
|
matthiasm@0
|
57
|
matthiasm@0
|
58 string
|
matthiasm@35
|
59 YinVamp::getName() const
|
matthiasm@0
|
60 {
|
matthiasm@0
|
61 return "Yin";
|
matthiasm@0
|
62 }
|
matthiasm@0
|
63
|
matthiasm@0
|
64 string
|
matthiasm@35
|
65 YinVamp::getDescription() const
|
matthiasm@0
|
66 {
|
matthiasm@0
|
67 return "A vamp implementation of the Yin algorithm for monophonic frequency estimation.";
|
matthiasm@0
|
68 }
|
matthiasm@0
|
69
|
matthiasm@0
|
70 string
|
matthiasm@35
|
71 YinVamp::getMaker() const
|
matthiasm@0
|
72 {
|
matthiasm@0
|
73 return "Matthias Mauch";
|
matthiasm@0
|
74 }
|
matthiasm@0
|
75
|
matthiasm@0
|
76 int
|
matthiasm@35
|
77 YinVamp::getPluginVersion() const
|
matthiasm@0
|
78 {
|
matthiasm@0
|
79 // Increment this each time you release a version that behaves
|
matthiasm@0
|
80 // differently from the previous one
|
Chris@143
|
81 return 3;
|
matthiasm@0
|
82 }
|
matthiasm@0
|
83
|
matthiasm@0
|
84 string
|
matthiasm@35
|
85 YinVamp::getCopyright() const
|
matthiasm@0
|
86 {
|
matthiasm@0
|
87 return "GPL";
|
matthiasm@0
|
88 }
|
matthiasm@0
|
89
|
matthiasm@35
|
90 YinVamp::InputDomain
|
matthiasm@35
|
91 YinVamp::getInputDomain() const
|
matthiasm@0
|
92 {
|
matthiasm@0
|
93 return TimeDomain;
|
matthiasm@0
|
94 }
|
matthiasm@0
|
95
|
matthiasm@0
|
96 size_t
|
matthiasm@35
|
97 YinVamp::getPreferredBlockSize() const
|
matthiasm@0
|
98 {
|
matthiasm@0
|
99 return 2048;
|
matthiasm@0
|
100 }
|
matthiasm@0
|
101
|
matthiasm@0
|
102 size_t
|
matthiasm@35
|
103 YinVamp::getPreferredStepSize() const
|
matthiasm@0
|
104 {
|
matthiasm@0
|
105 return 256;
|
matthiasm@0
|
106 }
|
matthiasm@0
|
107
|
matthiasm@0
|
108 size_t
|
matthiasm@35
|
109 YinVamp::getMinChannelCount() const
|
matthiasm@0
|
110 {
|
matthiasm@0
|
111 return 1;
|
matthiasm@0
|
112 }
|
matthiasm@0
|
113
|
matthiasm@0
|
114 size_t
|
matthiasm@35
|
115 YinVamp::getMaxChannelCount() const
|
matthiasm@0
|
116 {
|
matthiasm@0
|
117 return 1;
|
matthiasm@0
|
118 }
|
matthiasm@0
|
119
|
matthiasm@35
|
120 YinVamp::ParameterList
|
matthiasm@35
|
121 YinVamp::getParameterDescriptors() const
|
matthiasm@0
|
122 {
|
matthiasm@0
|
123 ParameterList list;
|
matthiasm@0
|
124
|
matthiasm@0
|
125 ParameterDescriptor d;
|
matthiasm@0
|
126 d.identifier = "yinThreshold";
|
matthiasm@0
|
127 d.name = "Yin threshold";
|
matthiasm@0
|
128 d.description = "The greedy Yin search for a low value difference function is done once a dip lower than this threshold is reached.";
|
matthiasm@0
|
129 d.unit = "";
|
matthiasm@0
|
130 d.minValue = 0.025f;
|
matthiasm@0
|
131 d.maxValue = 1.0f;
|
matthiasm@0
|
132 d.defaultValue = 0.15f;
|
matthiasm@0
|
133 d.isQuantized = true;
|
matthiasm@0
|
134 d.quantizeStep = 0.025f;
|
matthiasm@0
|
135
|
matthiasm@0
|
136 list.push_back(d);
|
matthiasm@0
|
137
|
matthiasm@0
|
138 d.identifier = "outputunvoiced";
|
matthiasm@0
|
139 d.valueNames.clear();
|
matthiasm@0
|
140 d.name = "Output estimates classified as unvoiced?";
|
matthiasm@0
|
141 d.description = ".";
|
matthiasm@0
|
142 d.unit = "";
|
matthiasm@0
|
143 d.minValue = 0.0f;
|
matthiasm@0
|
144 d.maxValue = 2.0f;
|
matthiasm@0
|
145 d.defaultValue = 2.0f;
|
matthiasm@0
|
146 d.isQuantized = true;
|
matthiasm@0
|
147 d.quantizeStep = 1.0f;
|
matthiasm@0
|
148 d.valueNames.push_back("No");
|
matthiasm@0
|
149 d.valueNames.push_back("Yes");
|
matthiasm@0
|
150 d.valueNames.push_back("Yes, as negative frequencies");
|
matthiasm@0
|
151 list.push_back(d);
|
matthiasm@0
|
152
|
matthiasm@0
|
153 return list;
|
matthiasm@0
|
154 }
|
matthiasm@0
|
155
|
matthiasm@0
|
156 float
|
matthiasm@35
|
157 YinVamp::getParameter(string identifier) const
|
matthiasm@0
|
158 {
|
matthiasm@0
|
159 if (identifier == "yinThreshold") {
|
matthiasm@0
|
160 return m_yinParameter;
|
matthiasm@0
|
161 }
|
matthiasm@0
|
162 if (identifier == "outputunvoiced") {
|
matthiasm@0
|
163 return m_outputUnvoiced;
|
matthiasm@0
|
164 }
|
matthiasm@0
|
165 return 0.f;
|
matthiasm@0
|
166 }
|
matthiasm@0
|
167
|
matthiasm@0
|
168 void
|
matthiasm@35
|
169 YinVamp::setParameter(string identifier, float value)
|
matthiasm@0
|
170 {
|
matthiasm@0
|
171 if (identifier == "yinThreshold")
|
matthiasm@0
|
172 {
|
matthiasm@0
|
173 m_yinParameter = value;
|
matthiasm@0
|
174 }
|
matthiasm@0
|
175 if (identifier == "outputunvoiced")
|
matthiasm@0
|
176 {
|
matthiasm@0
|
177 m_outputUnvoiced = value;
|
matthiasm@0
|
178 }
|
matthiasm@0
|
179 }
|
matthiasm@0
|
180
|
matthiasm@35
|
181 YinVamp::ProgramList
|
matthiasm@35
|
182 YinVamp::getPrograms() const
|
matthiasm@0
|
183 {
|
matthiasm@0
|
184 ProgramList list;
|
matthiasm@0
|
185 return list;
|
matthiasm@0
|
186 }
|
matthiasm@0
|
187
|
matthiasm@0
|
188 string
|
matthiasm@35
|
189 YinVamp::getCurrentProgram() const
|
matthiasm@0
|
190 {
|
matthiasm@0
|
191 return ""; // no programs
|
matthiasm@0
|
192 }
|
matthiasm@0
|
193
|
matthiasm@0
|
194 void
|
Chris@138
|
195 YinVamp::selectProgram(string)
|
matthiasm@0
|
196 {
|
matthiasm@0
|
197 }
|
matthiasm@0
|
198
|
matthiasm@35
|
199 YinVamp::OutputList
|
matthiasm@35
|
200 YinVamp::getOutputDescriptors() const
|
matthiasm@0
|
201 {
|
matthiasm@0
|
202 OutputList outputs;
|
matthiasm@0
|
203
|
matthiasm@0
|
204 OutputDescriptor d;
|
matthiasm@0
|
205
|
matthiasm@0
|
206 int outputNumber = 0;
|
matthiasm@0
|
207
|
matthiasm@0
|
208 d.identifier = "f0";
|
matthiasm@0
|
209 d.name = "Estimated f0";
|
matthiasm@0
|
210 d.description = "Estimated fundamental frequency";
|
matthiasm@0
|
211 d.unit = "Hz";
|
matthiasm@0
|
212 d.hasFixedBinCount = true;
|
matthiasm@0
|
213 d.binCount = 1;
|
matthiasm@0
|
214 d.hasKnownExtents = true;
|
matthiasm@0
|
215 d.minValue = m_fmin;
|
matthiasm@0
|
216 d.maxValue = 500;
|
matthiasm@0
|
217 d.isQuantized = false;
|
matthiasm@0
|
218 d.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
219 d.sampleRate = (m_inputSampleRate / m_stepSize);
|
matthiasm@0
|
220 d.hasDuration = false;
|
matthiasm@0
|
221 outputs.push_back(d);
|
matthiasm@0
|
222 m_outNoF0 = outputNumber++;
|
matthiasm@0
|
223
|
matthiasm@0
|
224 d.identifier = "periodicity";
|
matthiasm@0
|
225 d.name = "Periodicity";
|
matthiasm@0
|
226 d.description = "by-product of Yin f0 estimation";
|
matthiasm@0
|
227 d.unit = "";
|
matthiasm@0
|
228 d.hasFixedBinCount = true;
|
matthiasm@0
|
229 d.binCount = 1;
|
matthiasm@0
|
230 d.hasKnownExtents = true;
|
matthiasm@0
|
231 d.minValue = 0;
|
matthiasm@0
|
232 d.maxValue = 1;
|
matthiasm@0
|
233 d.isQuantized = false;
|
matthiasm@0
|
234 d.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
235 d.sampleRate = (m_inputSampleRate / m_stepSize);
|
matthiasm@0
|
236 d.hasDuration = false;
|
matthiasm@0
|
237 outputs.push_back(d);
|
matthiasm@0
|
238 m_outNoPeriodicity = outputNumber++;
|
matthiasm@0
|
239
|
matthiasm@0
|
240 d.identifier = "rms";
|
Chris@15
|
241 d.name = "Root mean square";
|
matthiasm@0
|
242 d.description = "Root mean square of the waveform.";
|
matthiasm@0
|
243 d.unit = "";
|
matthiasm@0
|
244 d.hasFixedBinCount = true;
|
matthiasm@0
|
245 d.binCount = 1;
|
matthiasm@0
|
246 d.hasKnownExtents = true;
|
matthiasm@0
|
247 d.minValue = 0;
|
matthiasm@0
|
248 d.maxValue = 1;
|
matthiasm@0
|
249 d.isQuantized = false;
|
matthiasm@0
|
250 d.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
251 d.sampleRate = (m_inputSampleRate / m_stepSize);
|
matthiasm@0
|
252 d.hasDuration = false;
|
matthiasm@0
|
253 outputs.push_back(d);
|
matthiasm@0
|
254 m_outNoRms = outputNumber++;
|
matthiasm@0
|
255
|
matthiasm@0
|
256 d.identifier = "salience";
|
matthiasm@0
|
257 d.name = "Salience";
|
matthiasm@0
|
258 d.description = "Yin Salience";
|
matthiasm@0
|
259 d.hasFixedBinCount = true;
|
matthiasm@0
|
260 d.binCount = m_blockSize / 2;
|
matthiasm@0
|
261 d.hasKnownExtents = true;
|
matthiasm@0
|
262 d.minValue = 0;
|
matthiasm@0
|
263 d.maxValue = 1;
|
matthiasm@0
|
264 d.isQuantized = false;
|
matthiasm@0
|
265 d.sampleType = OutputDescriptor::FixedSampleRate;
|
matthiasm@0
|
266 d.sampleRate = (m_inputSampleRate / m_stepSize);
|
matthiasm@0
|
267 d.hasDuration = false;
|
matthiasm@0
|
268 outputs.push_back(d);
|
matthiasm@0
|
269 m_outNoSalience = outputNumber++;
|
matthiasm@0
|
270
|
matthiasm@0
|
271 return outputs;
|
matthiasm@0
|
272 }
|
matthiasm@0
|
273
|
matthiasm@0
|
274 bool
|
matthiasm@35
|
275 YinVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
matthiasm@0
|
276 {
|
matthiasm@0
|
277 if (channels < getMinChannelCount() ||
|
matthiasm@0
|
278 channels > getMaxChannelCount()) return false;
|
matthiasm@0
|
279
|
Chris@9
|
280 /*
|
matthiasm@35
|
281 std::cerr << "YinVamp::initialise: channels = " << channels
|
matthiasm@0
|
282 << ", stepSize = " << stepSize << ", blockSize = " << blockSize
|
matthiasm@0
|
283 << std::endl;
|
Chris@9
|
284 */
|
matthiasm@0
|
285 m_channels = channels;
|
matthiasm@0
|
286 m_stepSize = stepSize;
|
matthiasm@0
|
287 m_blockSize = blockSize;
|
matthiasm@0
|
288
|
matthiasm@0
|
289 reset();
|
matthiasm@0
|
290
|
matthiasm@0
|
291 return true;
|
matthiasm@0
|
292 }
|
matthiasm@0
|
293
|
matthiasm@0
|
294 void
|
matthiasm@35
|
295 YinVamp::reset()
|
matthiasm@0
|
296 {
|
matthiasm@0
|
297 m_yin.setThreshold(m_yinParameter);
|
matthiasm@0
|
298 m_yin.setFrameSize(m_blockSize);
|
Chris@9
|
299 /*
|
matthiasm@35
|
300 std::cerr << "YinVamp::reset: yin threshold set to " << (m_yinParameter)
|
matthiasm@0
|
301 << ", blockSize = " << m_blockSize
|
matthiasm@0
|
302 << std::endl;
|
Chris@9
|
303 */
|
matthiasm@0
|
304 }
|
matthiasm@0
|
305
|
matthiasm@35
|
306 YinVamp::FeatureSet
|
matthiasm@35
|
307 YinVamp::process(const float *const *inputBuffers, RealTime timestamp)
|
matthiasm@0
|
308 {
|
matthiasm@60
|
309 timestamp = timestamp + Vamp::RealTime::frame2RealTime(m_blockSize/2, lrintf(m_inputSampleRate));
|
matthiasm@0
|
310 FeatureSet fs;
|
matthiasm@0
|
311
|
matthiasm@0
|
312 double *dInputBuffers = new double[m_blockSize];
|
matthiasm@0
|
313 for (size_t i = 0; i < m_blockSize; ++i) dInputBuffers[i] = inputBuffers[0][i];
|
matthiasm@0
|
314
|
matthiasm@0
|
315 Yin::YinOutput yo = m_yin.process(dInputBuffers);
|
matthiasm@35
|
316 // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
|
matthiasm@0
|
317 Feature f;
|
matthiasm@0
|
318 f.hasTimestamp = true;
|
matthiasm@0
|
319 f.timestamp = timestamp;
|
matthiasm@0
|
320 if (m_outputUnvoiced == 0.0f)
|
matthiasm@0
|
321 {
|
matthiasm@35
|
322 // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
|
matthiasm@0
|
323 if (yo.f0 > 0 && yo.f0 < m_fmax && yo.f0 > m_fmin) {
|
matthiasm@0
|
324 f.values.push_back(yo.f0);
|
matthiasm@0
|
325 fs[m_outNoF0].push_back(f);
|
matthiasm@0
|
326 }
|
matthiasm@0
|
327 } else if (m_outputUnvoiced == 1.0f)
|
matthiasm@0
|
328 {
|
matthiasm@29
|
329 if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
|
matthiasm@29
|
330 f.values.push_back(fabs(yo.f0));
|
matthiasm@0
|
331 fs[m_outNoF0].push_back(f);
|
matthiasm@0
|
332 }
|
matthiasm@0
|
333 } else
|
matthiasm@0
|
334 {
|
matthiasm@29
|
335 if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
|
matthiasm@0
|
336 f.values.push_back(yo.f0);
|
matthiasm@0
|
337 fs[m_outNoF0].push_back(f);
|
matthiasm@0
|
338 }
|
matthiasm@0
|
339 }
|
matthiasm@0
|
340
|
matthiasm@0
|
341 f.values.clear();
|
matthiasm@0
|
342 f.values.push_back(yo.rms);
|
matthiasm@0
|
343 fs[m_outNoRms].push_back(f);
|
matthiasm@0
|
344
|
matthiasm@0
|
345 f.values.clear();
|
matthiasm@0
|
346 for (size_t iBin = 0; iBin < yo.salience.size(); ++iBin)
|
matthiasm@0
|
347 {
|
matthiasm@0
|
348 f.values.push_back(yo.salience[iBin]);
|
matthiasm@0
|
349 }
|
matthiasm@0
|
350 fs[m_outNoSalience].push_back(f);
|
matthiasm@0
|
351
|
matthiasm@0
|
352 f.values.clear();
|
matthiasm@0
|
353 // f.values[0] = yo.periodicity;
|
matthiasm@0
|
354 f.values.push_back(yo.periodicity);
|
matthiasm@0
|
355 fs[m_outNoPeriodicity].push_back(f);
|
matthiasm@0
|
356
|
matthiasm@0
|
357 delete [] dInputBuffers;
|
matthiasm@0
|
358
|
matthiasm@0
|
359 return fs;
|
matthiasm@0
|
360 }
|
matthiasm@0
|
361
|
matthiasm@35
|
362 YinVamp::FeatureSet
|
matthiasm@35
|
363 YinVamp::getRemainingFeatures()
|
matthiasm@0
|
364 {
|
matthiasm@0
|
365 FeatureSet fs;
|
matthiasm@0
|
366 return fs;
|
matthiasm@0
|
367 }
|