summaryrefslogtreecommitdiff
path: root/libs/vamp-pyin/YinVamp.cpp
blob: 75fe5fca3a1053943baca0bc4bd778075c1ba2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    pYIN - A fundamental frequency estimator for monophonic audio
    Centre for Digital Music, Queen Mary, University of London.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "YinVamp.h"
#include "MonoNote.h"

#include "vamp-sdk/FFT.h"

#include <vector>
#include <algorithm>

#include <cstdio>
#include <cmath>
#include <complex>

using std::string;
using std::vector;
using Vamp::RealTime;


YinVamp::YinVamp(float inputSampleRate) :
    Plugin(inputSampleRate),
    m_channels(0),
    m_stepSize(256),
    m_blockSize(2048),
    m_fmin(40),
    m_fmax(1600),
    m_yin(2048, inputSampleRate, 0.0),
    m_outNoF0(0),
    m_outNoPeriodicity(0),
    m_outNoRms(0),
    m_outNoSalience(0),
    m_yinParameter(0.15f),
    m_outputUnvoiced(2.0f)
{
}

YinVamp::~YinVamp()
{
}

string
YinVamp::getIdentifier() const
{
    return "yin";
}

string
YinVamp::getName() const
{
    return "Yin";
}

string
YinVamp::getDescription() const
{
    return "A vamp implementation of the Yin algorithm for monophonic frequency estimation.";
}

string
YinVamp::getMaker() const
{
    return "Matthias Mauch";
}

int
YinVamp::getPluginVersion() const
{
    // Increment this each time you release a version that behaves
    // differently from the previous one
    return 2;
}

string
YinVamp::getCopyright() const
{
    return "GPL";
}

YinVamp::InputDomain
YinVamp::getInputDomain() const
{
    return TimeDomain;
}

size_t
YinVamp::getPreferredBlockSize() const
{
    return 2048;
}

size_t
YinVamp::getPreferredStepSize() const
{
    return 256;
}

size_t
YinVamp::getMinChannelCount() const
{
    return 1;
}

size_t
YinVamp::getMaxChannelCount() const
{
    return 1;
}

YinVamp::ParameterList
YinVamp::getParameterDescriptors() const
{
    ParameterList list;

    ParameterDescriptor d;
    d.identifier = "yinThreshold";
    d.name = "Yin threshold";
    d.description = "The greedy Yin search for a low value difference function is done once a dip lower than this threshold is reached.";
    d.unit = "";
    d.minValue = 0.025f;
    d.maxValue = 1.0f;
    d.defaultValue = 0.15f;
    d.isQuantized = true;
    d.quantizeStep = 0.025f;

    list.push_back(d);

    d.identifier = "outputunvoiced";
    d.valueNames.clear();
    d.name = "Output estimates classified as unvoiced?";
    d.description = ".";
    d.unit = "";
    d.minValue = 0.0f;
    d.maxValue = 2.0f;
    d.defaultValue = 2.0f;
    d.isQuantized = true;
    d.quantizeStep = 1.0f;
    d.valueNames.push_back("No");
    d.valueNames.push_back("Yes");
    d.valueNames.push_back("Yes, as negative frequencies");
    list.push_back(d);

    return list;
}

float
YinVamp::getParameter(string identifier) const
{
    if (identifier == "yinThreshold") {
        return m_yinParameter;
    }
    if (identifier == "outputunvoiced") {
        return m_outputUnvoiced;
    }
    return 0.f;
}

void
YinVamp::setParameter(string identifier, float value)
{
    if (identifier == "yinThreshold")
    {
        m_yinParameter = value;
    }
    if (identifier == "outputunvoiced")
    {
        m_outputUnvoiced = value;
    }
}

YinVamp::ProgramList
YinVamp::getPrograms() const
{
    ProgramList list;
    return list;
}

string
YinVamp::getCurrentProgram() const
{
    return ""; // no programs
}

void
YinVamp::selectProgram(string name)
{
}

YinVamp::OutputList
YinVamp::getOutputDescriptors() const
{
    OutputList outputs;

    OutputDescriptor d;

    int outputNumber = 0;

    d.identifier = "f0";
    d.name = "Estimated f0";
    d.description = "Estimated fundamental frequency";
    d.unit = "Hz";
    d.hasFixedBinCount = true;
    d.binCount = 1;
    d.hasKnownExtents = true;
    d.minValue = m_fmin;
    d.maxValue = 500;
    d.isQuantized = false;
    d.sampleType = OutputDescriptor::FixedSampleRate;
    d.sampleRate = (m_inputSampleRate / m_stepSize);
    d.hasDuration = false;
    outputs.push_back(d);
    m_outNoF0 = outputNumber++;

    d.identifier = "periodicity";
    d.name = "Periodicity";
    d.description = "by-product of Yin f0 estimation";
    d.unit = "";
    d.hasFixedBinCount = true;
    d.binCount = 1;
    d.hasKnownExtents = true;
    d.minValue = 0;
    d.maxValue = 1;
    d.isQuantized = false;
    d.sampleType = OutputDescriptor::FixedSampleRate;
    d.sampleRate = (m_inputSampleRate / m_stepSize);
    d.hasDuration = false;
    outputs.push_back(d);
    m_outNoPeriodicity = outputNumber++;

    d.identifier = "rms";
    d.name = "Root mean square";
    d.description = "Root mean square of the waveform.";
    d.unit = "";
    d.hasFixedBinCount = true;
    d.binCount = 1;
    d.hasKnownExtents = true;
    d.minValue = 0;
    d.maxValue = 1;
    d.isQuantized = false;
    d.sampleType = OutputDescriptor::FixedSampleRate;
    d.sampleRate = (m_inputSampleRate / m_stepSize);
    d.hasDuration = false;
    outputs.push_back(d);
    m_outNoRms = outputNumber++;

    d.identifier = "salience";
    d.name = "Salience";
    d.description = "Yin Salience";
    d.hasFixedBinCount = true;
    d.binCount = m_blockSize / 2;
    d.hasKnownExtents = true;
    d.minValue = 0;
    d.maxValue = 1;
    d.isQuantized = false;
    d.sampleType = OutputDescriptor::FixedSampleRate;
    d.sampleRate = (m_inputSampleRate / m_stepSize);
    d.hasDuration = false;
    outputs.push_back(d);
    m_outNoSalience = outputNumber++;

    return outputs;
}

bool
YinVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
{
    if (channels < getMinChannelCount() ||
	channels > getMaxChannelCount()) return false;

/*
    std::cerr << "YinVamp::initialise: channels = " << channels
          << ", stepSize = " << stepSize << ", blockSize = " << blockSize
          << std::endl;
*/
    m_channels = channels;
    m_stepSize = stepSize;
    m_blockSize = blockSize;

    reset();

    return true;
}

void
YinVamp::reset()
{
    m_yin.setThreshold(m_yinParameter);
    m_yin.setFrameSize(m_blockSize);
/*
    std::cerr << "YinVamp::reset: yin threshold set to " << (m_yinParameter)
          << ", blockSize = " << m_blockSize
          << std::endl;
*/
}

YinVamp::FeatureSet
YinVamp::process(const float *const *inputBuffers, RealTime timestamp)
{
    timestamp = timestamp + Vamp::RealTime::frame2RealTime(m_blockSize/2, lrintf(m_inputSampleRate));
    FeatureSet fs;

    double *dInputBuffers = new double[m_blockSize];
    for (size_t i = 0; i < m_blockSize; ++i) dInputBuffers[i] = inputBuffers[0][i];

    Yin::YinOutput yo = m_yin.process(dInputBuffers);
    // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
    Feature f;
    f.hasTimestamp = true;
    f.timestamp = timestamp;
    if (m_outputUnvoiced == 0.0f)
    {
        // std::cerr << "f0 in YinVamp: " << yo.f0 << std::endl;
        if (yo.f0 > 0 && yo.f0 < m_fmax && yo.f0 > m_fmin) {
            f.values.push_back(yo.f0);
            fs[m_outNoF0].push_back(f);
        }
    } else if (m_outputUnvoiced == 1.0f)
    {
        if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
            f.values.push_back(fabs(yo.f0));
            fs[m_outNoF0].push_back(f);
        }
    } else
    {
        if (fabs(yo.f0) < m_fmax && fabs(yo.f0) > m_fmin) {
            f.values.push_back(yo.f0);
            fs[m_outNoF0].push_back(f);
        }
    }

    f.values.clear();
    f.values.push_back(yo.rms);
    fs[m_outNoRms].push_back(f);

    f.values.clear();
    for (size_t iBin = 0; iBin < yo.salience.size(); ++iBin)
    {
        f.values.push_back(yo.salience[iBin]);
    }
    fs[m_outNoSalience].push_back(f);

    f.values.clear();
    // f.values[0] = yo.periodicity;
    f.values.push_back(yo.periodicity);
    fs[m_outNoPeriodicity].push_back(f);

    delete [] dInputBuffers;

    return fs;
}

YinVamp::FeatureSet
YinVamp::getRemainingFeatures()
{
    FeatureSet fs;
    return fs;
}