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