Chris@320
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@320
|
2
|
Chris@320
|
3 /*
|
Chris@320
|
4 Sonic Visualiser
|
Chris@320
|
5 An audio file viewer and annotation editor.
|
Chris@320
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@320
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@320
|
8
|
Chris@320
|
9 This program is free software; you can redistribute it and/or
|
Chris@320
|
10 modify it under the terms of the GNU General Public License as
|
Chris@320
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@320
|
12 License, or (at your option) any later version. See the file
|
Chris@320
|
13 COPYING included with this distribution for more information.
|
Chris@320
|
14 */
|
Chris@320
|
15
|
Chris@328
|
16 #include "FeatureExtractionPluginTransformer.h"
|
Chris@320
|
17
|
Chris@320
|
18 #include "plugin/FeatureExtractionPluginFactory.h"
|
Chris@320
|
19 #include "plugin/PluginXml.h"
|
Chris@320
|
20 #include "vamp-sdk/Plugin.h"
|
Chris@320
|
21
|
Chris@320
|
22 #include "data/model/Model.h"
|
Chris@320
|
23 #include "base/Window.h"
|
Chris@320
|
24 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@320
|
25 #include "data/model/SparseTimeValueModel.h"
|
Chris@320
|
26 #include "data/model/EditableDenseThreeDimensionalModel.h"
|
Chris@320
|
27 #include "data/model/DenseTimeValueModel.h"
|
Chris@320
|
28 #include "data/model/NoteModel.h"
|
Chris@320
|
29 #include "data/model/FFTModel.h"
|
Chris@320
|
30 #include "data/model/WaveFileModel.h"
|
Chris@320
|
31
|
Chris@320
|
32 #include <QMessageBox>
|
Chris@320
|
33
|
Chris@320
|
34 #include <iostream>
|
Chris@320
|
35
|
Chris@328
|
36 FeatureExtractionPluginTransformer::FeatureExtractionPluginTransformer(Model *inputModel,
|
Chris@320
|
37 QString pluginId,
|
Chris@320
|
38 const ExecutionContext &context,
|
Chris@320
|
39 QString configurationXml,
|
Chris@320
|
40 QString outputName) :
|
Chris@328
|
41 PluginTransformer(inputModel, context),
|
Chris@320
|
42 m_plugin(0),
|
Chris@320
|
43 m_descriptor(0),
|
Chris@320
|
44 m_outputFeatureNo(0)
|
Chris@320
|
45 {
|
Chris@328
|
46 // std::cerr << "FeatureExtractionPluginTransformer::FeatureExtractionPluginTransformer: plugin " << pluginId.toStdString() << ", outputName " << outputName.toStdString() << std::endl;
|
Chris@320
|
47
|
Chris@320
|
48 FeatureExtractionPluginFactory *factory =
|
Chris@320
|
49 FeatureExtractionPluginFactory::instanceFor(pluginId);
|
Chris@320
|
50
|
Chris@320
|
51 if (!factory) {
|
Chris@328
|
52 std::cerr << "FeatureExtractionPluginTransformer: No factory available for plugin id \""
|
Chris@320
|
53 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@320
|
54 return;
|
Chris@320
|
55 }
|
Chris@320
|
56
|
Chris@320
|
57 m_plugin = factory->instantiatePlugin(pluginId, m_input->getSampleRate());
|
Chris@320
|
58
|
Chris@320
|
59 if (!m_plugin) {
|
Chris@328
|
60 std::cerr << "FeatureExtractionPluginTransformer: Failed to instantiate plugin \""
|
Chris@320
|
61 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@320
|
62 return;
|
Chris@320
|
63 }
|
Chris@320
|
64
|
Chris@320
|
65 if (configurationXml != "") {
|
Chris@320
|
66 PluginXml(m_plugin).setParametersFromXml(configurationXml);
|
Chris@320
|
67 }
|
Chris@320
|
68
|
Chris@320
|
69 DenseTimeValueModel *input = getInput();
|
Chris@320
|
70 if (!input) return;
|
Chris@320
|
71
|
Chris@320
|
72 size_t channelCount = input->getChannelCount();
|
Chris@320
|
73 if (m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
74 channelCount = 1;
|
Chris@320
|
75 }
|
Chris@320
|
76 if (m_plugin->getMinChannelCount() > channelCount) {
|
Chris@328
|
77 std::cerr << "FeatureExtractionPluginTransformer:: "
|
Chris@320
|
78 << "Can't provide enough channels to plugin (plugin min "
|
Chris@320
|
79 << m_plugin->getMinChannelCount() << ", max "
|
Chris@320
|
80 << m_plugin->getMaxChannelCount() << ", input model has "
|
Chris@320
|
81 << input->getChannelCount() << ")" << std::endl;
|
Chris@320
|
82 return;
|
Chris@320
|
83 }
|
Chris@320
|
84
|
Chris@320
|
85 std::cerr << "Initialising feature extraction plugin with channels = "
|
Chris@320
|
86 << channelCount << ", step = " << m_context.stepSize
|
Chris@320
|
87 << ", block = " << m_context.blockSize << std::endl;
|
Chris@320
|
88
|
Chris@320
|
89 if (!m_plugin->initialise(channelCount,
|
Chris@320
|
90 m_context.stepSize,
|
Chris@320
|
91 m_context.blockSize)) {
|
Chris@328
|
92 std::cerr << "FeatureExtractionPluginTransformer: Plugin "
|
Chris@320
|
93 << m_plugin->getIdentifier() << " failed to initialise!" << std::endl;
|
Chris@320
|
94 return;
|
Chris@320
|
95 }
|
Chris@320
|
96
|
Chris@320
|
97 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
|
Chris@320
|
98
|
Chris@320
|
99 if (outputs.empty()) {
|
Chris@328
|
100 std::cerr << "FeatureExtractionPluginTransformer: Plugin \""
|
Chris@320
|
101 << pluginId.toStdString() << "\" has no outputs" << std::endl;
|
Chris@320
|
102 return;
|
Chris@320
|
103 }
|
Chris@320
|
104
|
Chris@320
|
105 for (size_t i = 0; i < outputs.size(); ++i) {
|
Chris@320
|
106 if (outputName == "" || outputs[i].identifier == outputName.toStdString()) {
|
Chris@320
|
107 m_outputFeatureNo = i;
|
Chris@320
|
108 m_descriptor = new Vamp::Plugin::OutputDescriptor
|
Chris@320
|
109 (outputs[i]);
|
Chris@320
|
110 break;
|
Chris@320
|
111 }
|
Chris@320
|
112 }
|
Chris@320
|
113
|
Chris@320
|
114 if (!m_descriptor) {
|
Chris@328
|
115 std::cerr << "FeatureExtractionPluginTransformer: Plugin \""
|
Chris@320
|
116 << pluginId.toStdString() << "\" has no output named \""
|
Chris@320
|
117 << outputName.toStdString() << "\"" << std::endl;
|
Chris@320
|
118 return;
|
Chris@320
|
119 }
|
Chris@320
|
120
|
Chris@328
|
121 // std::cerr << "FeatureExtractionPluginTransformer: output sample type "
|
Chris@320
|
122 // << m_descriptor->sampleType << std::endl;
|
Chris@320
|
123
|
Chris@320
|
124 int binCount = 1;
|
Chris@320
|
125 float minValue = 0.0, maxValue = 0.0;
|
Chris@320
|
126 bool haveExtents = false;
|
Chris@320
|
127
|
Chris@320
|
128 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
129 binCount = m_descriptor->binCount;
|
Chris@320
|
130 }
|
Chris@320
|
131
|
Chris@328
|
132 // std::cerr << "FeatureExtractionPluginTransformer: output bin count "
|
Chris@320
|
133 // << binCount << std::endl;
|
Chris@320
|
134
|
Chris@320
|
135 if (binCount > 0 && m_descriptor->hasKnownExtents) {
|
Chris@320
|
136 minValue = m_descriptor->minValue;
|
Chris@320
|
137 maxValue = m_descriptor->maxValue;
|
Chris@320
|
138 haveExtents = true;
|
Chris@320
|
139 }
|
Chris@320
|
140
|
Chris@320
|
141 size_t modelRate = m_input->getSampleRate();
|
Chris@320
|
142 size_t modelResolution = 1;
|
Chris@320
|
143
|
Chris@320
|
144 switch (m_descriptor->sampleType) {
|
Chris@320
|
145
|
Chris@320
|
146 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
|
Chris@320
|
147 if (m_descriptor->sampleRate != 0.0) {
|
Chris@320
|
148 modelResolution = size_t(modelRate / m_descriptor->sampleRate + 0.001);
|
Chris@320
|
149 }
|
Chris@320
|
150 break;
|
Chris@320
|
151
|
Chris@320
|
152 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
|
Chris@320
|
153 modelResolution = m_context.stepSize;
|
Chris@320
|
154 break;
|
Chris@320
|
155
|
Chris@320
|
156 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
|
Chris@320
|
157 modelRate = size_t(m_descriptor->sampleRate + 0.001);
|
Chris@320
|
158 break;
|
Chris@320
|
159 }
|
Chris@320
|
160
|
Chris@320
|
161 if (binCount == 0) {
|
Chris@320
|
162
|
Chris@320
|
163 m_output = new SparseOneDimensionalModel(modelRate, modelResolution,
|
Chris@320
|
164 false);
|
Chris@320
|
165
|
Chris@320
|
166 } else if (binCount == 1) {
|
Chris@320
|
167
|
Chris@320
|
168 SparseTimeValueModel *model;
|
Chris@320
|
169 if (haveExtents) {
|
Chris@320
|
170 model = new SparseTimeValueModel
|
Chris@320
|
171 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@320
|
172 } else {
|
Chris@320
|
173 model = new SparseTimeValueModel
|
Chris@320
|
174 (modelRate, modelResolution, false);
|
Chris@320
|
175 }
|
Chris@320
|
176 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
|
Chris@320
|
177
|
Chris@320
|
178 m_output = model;
|
Chris@320
|
179
|
Chris@320
|
180 } else if (m_descriptor->sampleType ==
|
Chris@320
|
181 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
182
|
Chris@320
|
183 // We don't have a sparse 3D model, so interpret this as a
|
Chris@320
|
184 // note model. There's nothing to define which values to use
|
Chris@320
|
185 // as which parameters of the note -- for the moment let's
|
Chris@320
|
186 // treat the first as pitch, second as duration in frames,
|
Chris@320
|
187 // third (if present) as velocity. (Our note model doesn't
|
Chris@320
|
188 // yet store velocity.)
|
Chris@320
|
189 //!!! todo: ask the user!
|
Chris@320
|
190
|
Chris@320
|
191 NoteModel *model;
|
Chris@320
|
192 if (haveExtents) {
|
Chris@320
|
193 model = new NoteModel
|
Chris@320
|
194 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@320
|
195 } else {
|
Chris@320
|
196 model = new NoteModel
|
Chris@320
|
197 (modelRate, modelResolution, false);
|
Chris@320
|
198 }
|
Chris@320
|
199 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
|
Chris@320
|
200
|
Chris@320
|
201 m_output = model;
|
Chris@320
|
202
|
Chris@320
|
203 } else {
|
Chris@320
|
204
|
Chris@320
|
205 EditableDenseThreeDimensionalModel *model =
|
Chris@320
|
206 new EditableDenseThreeDimensionalModel
|
Chris@320
|
207 (modelRate, modelResolution, binCount, false);
|
Chris@320
|
208
|
Chris@320
|
209 if (!m_descriptor->binNames.empty()) {
|
Chris@320
|
210 std::vector<QString> names;
|
Chris@320
|
211 for (size_t i = 0; i < m_descriptor->binNames.size(); ++i) {
|
Chris@320
|
212 names.push_back(m_descriptor->binNames[i].c_str());
|
Chris@320
|
213 }
|
Chris@320
|
214 model->setBinNames(names);
|
Chris@320
|
215 }
|
Chris@320
|
216
|
Chris@320
|
217 m_output = model;
|
Chris@320
|
218 }
|
Chris@320
|
219 }
|
Chris@320
|
220
|
Chris@328
|
221 FeatureExtractionPluginTransformer::~FeatureExtractionPluginTransformer()
|
Chris@320
|
222 {
|
Chris@328
|
223 std::cerr << "FeatureExtractionPluginTransformer::~FeatureExtractionPluginTransformer()" << std::endl;
|
Chris@320
|
224 delete m_plugin;
|
Chris@320
|
225 delete m_descriptor;
|
Chris@320
|
226 }
|
Chris@320
|
227
|
Chris@320
|
228 DenseTimeValueModel *
|
Chris@328
|
229 FeatureExtractionPluginTransformer::getInput()
|
Chris@320
|
230 {
|
Chris@320
|
231 DenseTimeValueModel *dtvm =
|
Chris@320
|
232 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@320
|
233 if (!dtvm) {
|
Chris@328
|
234 std::cerr << "FeatureExtractionPluginTransformer::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
|
Chris@320
|
235 }
|
Chris@320
|
236 return dtvm;
|
Chris@320
|
237 }
|
Chris@320
|
238
|
Chris@320
|
239 void
|
Chris@328
|
240 FeatureExtractionPluginTransformer::run()
|
Chris@320
|
241 {
|
Chris@320
|
242 DenseTimeValueModel *input = getInput();
|
Chris@320
|
243 if (!input) return;
|
Chris@320
|
244
|
Chris@320
|
245 if (!m_output) return;
|
Chris@320
|
246
|
Chris@320
|
247 while (!input->isReady()) {
|
Chris@320
|
248 /*
|
Chris@320
|
249 if (dynamic_cast<WaveFileModel *>(input)) {
|
Chris@328
|
250 std::cerr << "FeatureExtractionPluginTransformer::run: Model is not ready, but it's not a WaveFileModel (it's a " << typeid(input).name() << "), so that's OK" << std::endl;
|
Chris@320
|
251 sleep(2);
|
Chris@320
|
252 break; // no need to wait
|
Chris@320
|
253 }
|
Chris@320
|
254 */
|
Chris@328
|
255 std::cerr << "FeatureExtractionPluginTransformer::run: Waiting for input model to be ready..." << std::endl;
|
Chris@320
|
256 sleep(1);
|
Chris@320
|
257 }
|
Chris@320
|
258
|
Chris@320
|
259 size_t sampleRate = m_input->getSampleRate();
|
Chris@320
|
260
|
Chris@320
|
261 size_t channelCount = input->getChannelCount();
|
Chris@320
|
262 if (m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
263 channelCount = 1;
|
Chris@320
|
264 }
|
Chris@320
|
265
|
Chris@320
|
266 float **buffers = new float*[channelCount];
|
Chris@320
|
267 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
268 buffers[ch] = new float[m_context.blockSize + 2];
|
Chris@320
|
269 }
|
Chris@320
|
270
|
Chris@320
|
271 bool frequencyDomain = (m_plugin->getInputDomain() ==
|
Chris@320
|
272 Vamp::Plugin::FrequencyDomain);
|
Chris@320
|
273 std::vector<FFTModel *> fftModels;
|
Chris@320
|
274
|
Chris@320
|
275 if (frequencyDomain) {
|
Chris@320
|
276 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
277 FFTModel *model = new FFTModel
|
Chris@320
|
278 (getInput(),
|
Chris@320
|
279 channelCount == 1 ? m_context.channel : ch,
|
Chris@320
|
280 m_context.windowType,
|
Chris@320
|
281 m_context.blockSize,
|
Chris@320
|
282 m_context.stepSize,
|
Chris@320
|
283 m_context.blockSize,
|
Chris@320
|
284 false);
|
Chris@320
|
285 if (!model->isOK()) {
|
Chris@320
|
286 QMessageBox::critical
|
Chris@320
|
287 (0, tr("FFT cache failed"),
|
Chris@320
|
288 tr("Failed to create the FFT model for this transform.\n"
|
Chris@320
|
289 "There may be insufficient memory or disc space to continue."));
|
Chris@320
|
290 delete model;
|
Chris@320
|
291 setCompletion(100);
|
Chris@320
|
292 return;
|
Chris@320
|
293 }
|
Chris@320
|
294 model->resume();
|
Chris@320
|
295 fftModels.push_back(model);
|
Chris@320
|
296 }
|
Chris@320
|
297 }
|
Chris@320
|
298
|
Chris@320
|
299 long startFrame = m_input->getStartFrame();
|
Chris@320
|
300 long endFrame = m_input->getEndFrame();
|
Chris@320
|
301
|
Chris@320
|
302 long contextStart = m_context.startFrame;
|
Chris@320
|
303 long contextDuration = m_context.duration;
|
Chris@320
|
304
|
Chris@320
|
305 if (contextStart == 0 || contextStart < startFrame) {
|
Chris@320
|
306 contextStart = startFrame;
|
Chris@320
|
307 }
|
Chris@320
|
308
|
Chris@320
|
309 if (contextDuration == 0) {
|
Chris@320
|
310 contextDuration = endFrame - contextStart;
|
Chris@320
|
311 }
|
Chris@320
|
312 if (contextStart + contextDuration > endFrame) {
|
Chris@320
|
313 contextDuration = endFrame - contextStart;
|
Chris@320
|
314 }
|
Chris@320
|
315
|
Chris@320
|
316 long blockFrame = contextStart;
|
Chris@320
|
317
|
Chris@320
|
318 long prevCompletion = 0;
|
Chris@320
|
319
|
Chris@320
|
320 setCompletion(0);
|
Chris@320
|
321
|
Chris@320
|
322 while (!m_abandoned) {
|
Chris@320
|
323
|
Chris@320
|
324 if (frequencyDomain) {
|
Chris@320
|
325 if (blockFrame - int(m_context.blockSize)/2 >
|
Chris@320
|
326 contextStart + contextDuration) break;
|
Chris@320
|
327 } else {
|
Chris@320
|
328 if (blockFrame >=
|
Chris@320
|
329 contextStart + contextDuration) break;
|
Chris@320
|
330 }
|
Chris@320
|
331
|
Chris@328
|
332 // std::cerr << "FeatureExtractionPluginTransformer::run: blockFrame "
|
Chris@320
|
333 // << blockFrame << ", endFrame " << endFrame << ", blockSize "
|
Chris@320
|
334 // << m_context.blockSize << std::endl;
|
Chris@320
|
335
|
Chris@320
|
336 long completion =
|
Chris@320
|
337 (((blockFrame - contextStart) / m_context.stepSize) * 99) /
|
Chris@320
|
338 (contextDuration / m_context.stepSize);
|
Chris@320
|
339
|
Chris@320
|
340 // channelCount is either m_input->channelCount or 1
|
Chris@320
|
341
|
Chris@320
|
342 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
343 if (frequencyDomain) {
|
Chris@320
|
344 int column = (blockFrame - startFrame) / m_context.stepSize;
|
Chris@320
|
345 for (size_t i = 0; i <= m_context.blockSize/2; ++i) {
|
Chris@320
|
346 fftModels[ch]->getValuesAt
|
Chris@320
|
347 (column, i, buffers[ch][i*2], buffers[ch][i*2+1]);
|
Chris@320
|
348 }
|
Chris@320
|
349 } else {
|
Chris@320
|
350 getFrames(ch, channelCount,
|
Chris@320
|
351 blockFrame, m_context.blockSize, buffers[ch]);
|
Chris@320
|
352 }
|
Chris@320
|
353 }
|
Chris@320
|
354
|
Chris@320
|
355 Vamp::Plugin::FeatureSet features = m_plugin->process
|
Chris@320
|
356 (buffers, Vamp::RealTime::frame2RealTime(blockFrame, sampleRate));
|
Chris@320
|
357
|
Chris@320
|
358 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@320
|
359 Vamp::Plugin::Feature feature =
|
Chris@320
|
360 features[m_outputFeatureNo][fi];
|
Chris@320
|
361 addFeature(blockFrame, feature);
|
Chris@320
|
362 }
|
Chris@320
|
363
|
Chris@320
|
364 if (blockFrame == contextStart || completion > prevCompletion) {
|
Chris@320
|
365 setCompletion(completion);
|
Chris@320
|
366 prevCompletion = completion;
|
Chris@320
|
367 }
|
Chris@320
|
368
|
Chris@320
|
369 blockFrame += m_context.stepSize;
|
Chris@320
|
370 }
|
Chris@320
|
371
|
Chris@320
|
372 if (m_abandoned) return;
|
Chris@320
|
373
|
Chris@320
|
374 Vamp::Plugin::FeatureSet features = m_plugin->getRemainingFeatures();
|
Chris@320
|
375
|
Chris@320
|
376 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@320
|
377 Vamp::Plugin::Feature feature =
|
Chris@320
|
378 features[m_outputFeatureNo][fi];
|
Chris@320
|
379 addFeature(blockFrame, feature);
|
Chris@320
|
380 }
|
Chris@320
|
381
|
Chris@320
|
382 if (frequencyDomain) {
|
Chris@320
|
383 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
384 delete fftModels[ch];
|
Chris@320
|
385 }
|
Chris@320
|
386 }
|
Chris@320
|
387
|
Chris@320
|
388 setCompletion(100);
|
Chris@320
|
389 }
|
Chris@320
|
390
|
Chris@320
|
391 void
|
Chris@328
|
392 FeatureExtractionPluginTransformer::getFrames(int channel, int channelCount,
|
Chris@320
|
393 long startFrame, long size,
|
Chris@320
|
394 float *buffer)
|
Chris@320
|
395 {
|
Chris@320
|
396 long offset = 0;
|
Chris@320
|
397
|
Chris@320
|
398 if (startFrame < 0) {
|
Chris@320
|
399 for (int i = 0; i < size && startFrame + i < 0; ++i) {
|
Chris@320
|
400 buffer[i] = 0.0f;
|
Chris@320
|
401 }
|
Chris@320
|
402 offset = -startFrame;
|
Chris@320
|
403 size -= offset;
|
Chris@320
|
404 if (size <= 0) return;
|
Chris@320
|
405 startFrame = 0;
|
Chris@320
|
406 }
|
Chris@320
|
407
|
Chris@320
|
408 long got = getInput()->getData
|
Chris@320
|
409 ((channelCount == 1 ? m_context.channel : channel),
|
Chris@320
|
410 startFrame, size, buffer + offset);
|
Chris@320
|
411
|
Chris@320
|
412 while (got < size) {
|
Chris@320
|
413 buffer[offset + got] = 0.0;
|
Chris@320
|
414 ++got;
|
Chris@320
|
415 }
|
Chris@320
|
416
|
Chris@320
|
417 if (m_context.channel == -1 && channelCount == 1 &&
|
Chris@320
|
418 getInput()->getChannelCount() > 1) {
|
Chris@320
|
419 // use mean instead of sum, as plugin input
|
Chris@320
|
420 int cc = getInput()->getChannelCount();
|
Chris@320
|
421 for (long i = 0; i < size; ++i) {
|
Chris@320
|
422 buffer[i] /= cc;
|
Chris@320
|
423 }
|
Chris@320
|
424 }
|
Chris@320
|
425 }
|
Chris@320
|
426
|
Chris@320
|
427 void
|
Chris@328
|
428 FeatureExtractionPluginTransformer::addFeature(size_t blockFrame,
|
Chris@320
|
429 const Vamp::Plugin::Feature &feature)
|
Chris@320
|
430 {
|
Chris@320
|
431 size_t inputRate = m_input->getSampleRate();
|
Chris@320
|
432
|
Chris@328
|
433 // std::cerr << "FeatureExtractionPluginTransformer::addFeature("
|
Chris@320
|
434 // << blockFrame << ")" << std::endl;
|
Chris@320
|
435
|
Chris@320
|
436 int binCount = 1;
|
Chris@320
|
437 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
438 binCount = m_descriptor->binCount;
|
Chris@320
|
439 }
|
Chris@320
|
440
|
Chris@320
|
441 size_t frame = blockFrame;
|
Chris@320
|
442
|
Chris@320
|
443 if (m_descriptor->sampleType ==
|
Chris@320
|
444 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
445
|
Chris@320
|
446 if (!feature.hasTimestamp) {
|
Chris@320
|
447 std::cerr
|
Chris@328
|
448 << "WARNING: FeatureExtractionPluginTransformer::addFeature: "
|
Chris@320
|
449 << "Feature has variable sample rate but no timestamp!"
|
Chris@320
|
450 << std::endl;
|
Chris@320
|
451 return;
|
Chris@320
|
452 } else {
|
Chris@320
|
453 frame = Vamp::RealTime::realTime2Frame(feature.timestamp, inputRate);
|
Chris@320
|
454 }
|
Chris@320
|
455
|
Chris@320
|
456 } else if (m_descriptor->sampleType ==
|
Chris@320
|
457 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
|
Chris@320
|
458
|
Chris@320
|
459 if (feature.hasTimestamp) {
|
Chris@320
|
460 //!!! warning: sampleRate may be non-integral
|
Chris@320
|
461 frame = Vamp::RealTime::realTime2Frame(feature.timestamp,
|
Chris@320
|
462 lrintf(m_descriptor->sampleRate));
|
Chris@320
|
463 } else {
|
Chris@320
|
464 frame = m_output->getEndFrame();
|
Chris@320
|
465 }
|
Chris@320
|
466 }
|
Chris@320
|
467
|
Chris@320
|
468 if (binCount == 0) {
|
Chris@320
|
469
|
Chris@320
|
470 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
|
Chris@320
|
471 if (!model) return;
|
Chris@320
|
472 model->addPoint(SparseOneDimensionalModel::Point(frame, feature.label.c_str()));
|
Chris@320
|
473
|
Chris@320
|
474 } else if (binCount == 1) {
|
Chris@320
|
475
|
Chris@320
|
476 float value = 0.0;
|
Chris@320
|
477 if (feature.values.size() > 0) value = feature.values[0];
|
Chris@320
|
478
|
Chris@320
|
479 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
|
Chris@320
|
480 if (!model) return;
|
Chris@320
|
481 model->addPoint(SparseTimeValueModel::Point(frame, value, feature.label.c_str()));
|
Chris@320
|
482 // std::cerr << "SparseTimeValueModel::addPoint(" << frame << ", " << value << "), " << feature.label.c_str() << std::endl;
|
Chris@320
|
483
|
Chris@320
|
484 } else if (m_descriptor->sampleType ==
|
Chris@320
|
485 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
486
|
Chris@320
|
487 float pitch = 0.0;
|
Chris@320
|
488 if (feature.values.size() > 0) pitch = feature.values[0];
|
Chris@320
|
489
|
Chris@320
|
490 float duration = 1;
|
Chris@320
|
491 if (feature.values.size() > 1) duration = feature.values[1];
|
Chris@320
|
492
|
Chris@320
|
493 float velocity = 100;
|
Chris@320
|
494 if (feature.values.size() > 2) velocity = feature.values[2];
|
Chris@320
|
495
|
Chris@320
|
496 NoteModel *model = getOutput<NoteModel>();
|
Chris@320
|
497 if (!model) return;
|
Chris@320
|
498
|
Chris@320
|
499 model->addPoint(NoteModel::Point(frame, pitch,
|
Chris@320
|
500 lrintf(duration),
|
Chris@320
|
501 feature.label.c_str()));
|
Chris@320
|
502
|
Chris@320
|
503 } else {
|
Chris@320
|
504
|
Chris@320
|
505 DenseThreeDimensionalModel::Column values = feature.values;
|
Chris@320
|
506
|
Chris@320
|
507 EditableDenseThreeDimensionalModel *model =
|
Chris@320
|
508 getOutput<EditableDenseThreeDimensionalModel>();
|
Chris@320
|
509 if (!model) return;
|
Chris@320
|
510
|
Chris@320
|
511 model->setColumn(frame / model->getResolution(), values);
|
Chris@320
|
512 }
|
Chris@320
|
513 }
|
Chris@320
|
514
|
Chris@320
|
515 void
|
Chris@328
|
516 FeatureExtractionPluginTransformer::setCompletion(int completion)
|
Chris@320
|
517 {
|
Chris@320
|
518 int binCount = 1;
|
Chris@320
|
519 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
520 binCount = m_descriptor->binCount;
|
Chris@320
|
521 }
|
Chris@320
|
522
|
Chris@328
|
523 // std::cerr << "FeatureExtractionPluginTransformer::setCompletion("
|
Chris@320
|
524 // << completion << ")" << std::endl;
|
Chris@320
|
525
|
Chris@320
|
526 if (binCount == 0) {
|
Chris@320
|
527
|
Chris@320
|
528 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
|
Chris@320
|
529 if (!model) return;
|
Chris@320
|
530 model->setCompletion(completion);
|
Chris@320
|
531
|
Chris@320
|
532 } else if (binCount == 1) {
|
Chris@320
|
533
|
Chris@320
|
534 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
|
Chris@320
|
535 if (!model) return;
|
Chris@320
|
536 model->setCompletion(completion);
|
Chris@320
|
537
|
Chris@320
|
538 } else if (m_descriptor->sampleType ==
|
Chris@320
|
539 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
540
|
Chris@320
|
541 NoteModel *model = getOutput<NoteModel>();
|
Chris@320
|
542 if (!model) return;
|
Chris@320
|
543 model->setCompletion(completion);
|
Chris@320
|
544
|
Chris@320
|
545 } else {
|
Chris@320
|
546
|
Chris@320
|
547 EditableDenseThreeDimensionalModel *model =
|
Chris@320
|
548 getOutput<EditableDenseThreeDimensionalModel>();
|
Chris@320
|
549 if (!model) return;
|
Chris@320
|
550 model->setCompletion(completion);
|
Chris@320
|
551 }
|
Chris@320
|
552 }
|
Chris@320
|
553
|