1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
4 pYIN - A fundamental frequency estimator for monophonic audio
5 Centre for Digital Music, Queen Mary, University of London.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version. See the file
11 COPYING included with this distribution for more information.
17 #include "vamp-sdk/FFT.h"
31 YinVamp::YinVamp(float inputSampleRate) :
32 Plugin(inputSampleRate),
38 m_yin(2048, inputSampleRate, 0.0),
40 m_outNoPeriodicity(0),
43 m_yinParameter(0.15f),
44 m_outputUnvoiced(2.0f)
53 YinVamp::getIdentifier() const
59 YinVamp::getName() const
65 YinVamp::getDescription() const
67 return "A vamp implementation of the Yin algorithm for monophonic frequency estimation.";
71 YinVamp::getMaker() const
73 return "Matthias Mauch";
77 YinVamp::getPluginVersion() const
79 // Increment this each time you release a version that behaves
80 // differently from the previous one
85 YinVamp::getCopyright() const
91 YinVamp::getInputDomain() const
97 YinVamp::getPreferredBlockSize() const
103 YinVamp::getPreferredStepSize() const
109 YinVamp::getMinChannelCount() const
115 YinVamp::getMaxChannelCount() const
120 YinVamp::ParameterList
121 YinVamp::getParameterDescriptors() const
125 ParameterDescriptor d;
126 d.identifier = "yinThreshold";
127 d.name = "Yin threshold";
128 d.description = "The greedy Yin search for a low value difference function is done once a dip lower than this threshold is reached.";
132 d.defaultValue = 0.15f;
133 d.isQuantized = true;
134 d.quantizeStep = 0.025f;
138 d.identifier = "outputunvoiced";
139 d.valueNames.clear();
140 d.name = "Output estimates classified as unvoiced?";
145 d.defaultValue = 2.0f;
146 d.isQuantized = true;
147 d.quantizeStep = 1.0f;
148 d.valueNames.push_back("No");
149 d.valueNames.push_back("Yes");
150 d.valueNames.push_back("Yes, as negative frequencies");
157 YinVamp::getParameter(string identifier) const
159 if (identifier == "yinThreshold") {
160 return m_yinParameter;
162 if (identifier == "outputunvoiced") {
163 return m_outputUnvoiced;
169 YinVamp::setParameter(string identifier, float value)
171 if (identifier == "yinThreshold")
173 m_yinParameter = value;
175 if (identifier == "outputunvoiced")
177 m_outputUnvoiced = value;
182 YinVamp::getPrograms() const
189 YinVamp::getCurrentProgram() const
191 return ""; // no programs
195 YinVamp::selectProgram(string name)
200 YinVamp::getOutputDescriptors() const
206 int outputNumber = 0;
209 d.name = "Estimated f0";
210 d.description = "Estimated fundamental frequency";
212 d.hasFixedBinCount = true;
214 d.hasKnownExtents = true;
217 d.isQuantized = false;
218 d.sampleType = OutputDescriptor::FixedSampleRate;
219 d.sampleRate = (m_inputSampleRate / m_stepSize);
220 d.hasDuration = false;
221 outputs.push_back(d);
222 m_outNoF0 = outputNumber++;
224 d.identifier = "periodicity";
225 d.name = "Periodicity";
226 d.description = "by-product of Yin f0 estimation";
228 d.hasFixedBinCount = true;
230 d.hasKnownExtents = true;
233 d.isQuantized = false;
234 d.sampleType = OutputDescriptor::FixedSampleRate;
235 d.sampleRate = (m_inputSampleRate / m_stepSize);
236 d.hasDuration = false;
237 outputs.push_back(d);
238 m_outNoPeriodicity = outputNumber++;
240 d.identifier = "rms";
241 d.name = "Root mean square";
242 d.description = "Root mean square of the waveform.";
244 d.hasFixedBinCount = true;
246 d.hasKnownExtents = true;
249 d.isQuantized = false;
250 d.sampleType = OutputDescriptor::FixedSampleRate;
251 d.sampleRate = (m_inputSampleRate / m_stepSize);
252 d.hasDuration = false;
253 outputs.push_back(d);
254 m_outNoRms = outputNumber++;
256 d.identifier = "salience";
258 d.description = "Yin Salience";
259 d.hasFixedBinCount = true;
260 d.binCount = m_blockSize / 2;
261 d.hasKnownExtents = true;
264 d.isQuantized = false;
265 d.sampleType = OutputDescriptor::FixedSampleRate;
266 d.sampleRate = (m_inputSampleRate / m_stepSize);
267 d.hasDuration = false;
268 outputs.push_back(d);
269 m_outNoSalience = outputNumber++;
275 YinVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
277 if (channels < getMinChannelCount() ||
278 channels > getMaxChannelCount()) return false;
281 std::cerr << "YinVamp::initialise: channels = " << channels
282 << ", stepSize = " << stepSize << ", blockSize = " << blockSize
285 m_channels = channels;
286 m_stepSize = stepSize;
287 m_blockSize = blockSize;
297 m_yin.setThreshold(m_yinParameter);
298 m_yin.setFrameSize(m_blockSize);
300 std::cerr << "YinVamp::reset: yin threshold set to " << (m_yinParameter)
301 << ", blockSize = " << m_blockSize
307 YinVamp::process(const float *const *inputBuffers, RealTime timestamp)
309 timestamp = timestamp + Vamp::RealTime::frame2RealTime(m_blockSize/2, lrintf(m_inputSampleRate));
312 double *dInputBuffers = new double[m_blockSize];
313 for (size_t i = 0; i < m_blockSize; ++i) dInputBuffers[i] = inputBuffers[0][i];
315 Yin::YinOutput yo = m_yin.process(dInputBuffers);
316 // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
318 f.hasTimestamp = true;
319 f.timestamp = timestamp;
320 if (m_outputUnvoiced == 0.0f)
322 // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
323 if (yo.f0 > 0 && yo.f0 < m_fmax && yo.f0 > m_fmin) {
324 f.values.push_back(yo.f0);
325 fs[m_outNoF0].push_back(f);
327 } else if (m_outputUnvoiced == 1.0f)
329 if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
330 f.values.push_back(fabs(yo.f0));
331 fs[m_outNoF0].push_back(f);
335 if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
336 f.values.push_back(yo.f0);
337 fs[m_outNoF0].push_back(f);
342 f.values.push_back(yo.rms);
343 fs[m_outNoRms].push_back(f);
346 for (size_t iBin = 0; iBin < yo.salience.size(); ++iBin)
348 f.values.push_back(yo.salience[iBin]);
350 fs[m_outNoSalience].push_back(f);
353 // f.values[0] = yo.periodicity;
354 f.values.push_back(yo.periodicity);
355 fs[m_outNoPeriodicity].push_back(f);
357 delete [] dInputBuffers;
363 YinVamp::getRemainingFeatures()