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@331
|
16 #include "FeatureExtractionModelTransformer.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@350
|
32 #include "TransformFactory.h"
|
Chris@350
|
33
|
Chris@320
|
34 #include <QMessageBox>
|
Chris@320
|
35
|
Chris@320
|
36 #include <iostream>
|
Chris@320
|
37
|
Chris@350
|
38 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
|
Chris@350
|
39 const Transform &transform) :
|
Chris@350
|
40 ModelTransformer(in, transform),
|
Chris@320
|
41 m_plugin(0),
|
Chris@320
|
42 m_descriptor(0),
|
Chris@320
|
43 m_outputFeatureNo(0)
|
Chris@320
|
44 {
|
Chris@350
|
45 // std::cerr << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: plugin " << pluginId.toStdString() << ", outputName " << m_transform.getOutput().toStdString() << std::endl;
|
Chris@350
|
46
|
Chris@350
|
47 QString pluginId = transform.getPluginIdentifier();
|
Chris@320
|
48
|
Chris@320
|
49 FeatureExtractionPluginFactory *factory =
|
Chris@320
|
50 FeatureExtractionPluginFactory::instanceFor(pluginId);
|
Chris@320
|
51
|
Chris@320
|
52 if (!factory) {
|
Chris@361
|
53 m_message = tr("No factory available for feature extraction plugin id \"%1\" (unknown plugin type, or internal error?)").arg(pluginId);
|
Chris@320
|
54 return;
|
Chris@320
|
55 }
|
Chris@320
|
56
|
Chris@350
|
57 DenseTimeValueModel *input = getConformingInput();
|
Chris@350
|
58 if (!input) {
|
Chris@361
|
59 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
|
Chris@350
|
60 return;
|
Chris@350
|
61 }
|
Chris@320
|
62
|
Chris@350
|
63 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
|
Chris@320
|
64 if (!m_plugin) {
|
Chris@361
|
65 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
|
Chris@320
|
66 return;
|
Chris@320
|
67 }
|
Chris@320
|
68
|
Chris@350
|
69 TransformFactory::getInstance()->makeContextConsistentWithPlugin
|
Chris@350
|
70 (m_transform, m_plugin);
|
Chris@343
|
71
|
Chris@350
|
72 TransformFactory::getInstance()->setPluginParameters
|
Chris@350
|
73 (m_transform, m_plugin);
|
Chris@320
|
74
|
Chris@320
|
75 size_t channelCount = input->getChannelCount();
|
Chris@320
|
76 if (m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
77 channelCount = 1;
|
Chris@320
|
78 }
|
Chris@320
|
79 if (m_plugin->getMinChannelCount() > channelCount) {
|
Chris@361
|
80 m_message = tr("Cannot provide enough channels to feature extraction plugin \"%1\" (plugin min is %2, max %3; input model has %4)")
|
Chris@361
|
81 .arg(pluginId)
|
Chris@361
|
82 .arg(m_plugin->getMinChannelCount())
|
Chris@361
|
83 .arg(m_plugin->getMaxChannelCount())
|
Chris@361
|
84 .arg(input->getChannelCount());
|
Chris@320
|
85 return;
|
Chris@320
|
86 }
|
Chris@320
|
87
|
Chris@320
|
88 std::cerr << "Initialising feature extraction plugin with channels = "
|
Chris@350
|
89 << channelCount << ", step = " << m_transform.getStepSize()
|
Chris@350
|
90 << ", block = " << m_transform.getBlockSize() << std::endl;
|
Chris@320
|
91
|
Chris@320
|
92 if (!m_plugin->initialise(channelCount,
|
Chris@350
|
93 m_transform.getStepSize(),
|
Chris@350
|
94 m_transform.getBlockSize())) {
|
Chris@361
|
95
|
Chris@361
|
96 size_t pstep = m_transform.getStepSize();
|
Chris@361
|
97 size_t pblock = m_transform.getBlockSize();
|
Chris@361
|
98
|
Chris@361
|
99 m_transform.setStepSize(0);
|
Chris@361
|
100 m_transform.setBlockSize(0);
|
Chris@361
|
101 TransformFactory::getInstance()->makeContextConsistentWithPlugin
|
Chris@361
|
102 (m_transform, m_plugin);
|
Chris@361
|
103
|
Chris@361
|
104 if (m_transform.getStepSize() != pstep ||
|
Chris@361
|
105 m_transform.getBlockSize() != pblock) {
|
Chris@361
|
106
|
Chris@361
|
107 if (!m_plugin->initialise(channelCount,
|
Chris@361
|
108 m_transform.getStepSize(),
|
Chris@361
|
109 m_transform.getBlockSize())) {
|
Chris@361
|
110
|
Chris@361
|
111 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
|
Chris@361
|
112 return;
|
Chris@361
|
113
|
Chris@361
|
114 } else {
|
Chris@361
|
115
|
Chris@361
|
116 m_message = tr("Feature extraction plugin \"%1\" rejected the given step and block sizes (%2 and %3); using plugin defaults (%4 and %5) instead")
|
Chris@361
|
117 .arg(pluginId)
|
Chris@361
|
118 .arg(pstep)
|
Chris@361
|
119 .arg(pblock)
|
Chris@361
|
120 .arg(m_transform.getStepSize())
|
Chris@361
|
121 .arg(m_transform.getBlockSize());
|
Chris@361
|
122 }
|
Chris@361
|
123
|
Chris@361
|
124 } else {
|
Chris@361
|
125
|
Chris@361
|
126 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
|
Chris@361
|
127 return;
|
Chris@361
|
128 }
|
Chris@320
|
129 }
|
Chris@320
|
130
|
Chris@320
|
131 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
|
Chris@320
|
132
|
Chris@320
|
133 if (outputs.empty()) {
|
Chris@361
|
134 m_message = tr("Plugin \"%1\" has no outputs").arg(pluginId);
|
Chris@320
|
135 return;
|
Chris@320
|
136 }
|
Chris@320
|
137
|
Chris@320
|
138 for (size_t i = 0; i < outputs.size(); ++i) {
|
Chris@350
|
139 if (m_transform.getOutput() == "" ||
|
Chris@350
|
140 outputs[i].identifier == m_transform.getOutput().toStdString()) {
|
Chris@320
|
141 m_outputFeatureNo = i;
|
Chris@320
|
142 m_descriptor = new Vamp::Plugin::OutputDescriptor
|
Chris@320
|
143 (outputs[i]);
|
Chris@320
|
144 break;
|
Chris@320
|
145 }
|
Chris@320
|
146 }
|
Chris@320
|
147
|
Chris@320
|
148 if (!m_descriptor) {
|
Chris@361
|
149 m_message = tr("Plugin \"%1\" has no output named \"%2\"")
|
Chris@361
|
150 .arg(pluginId)
|
Chris@361
|
151 .arg(m_transform.getOutput());
|
Chris@320
|
152 return;
|
Chris@320
|
153 }
|
Chris@320
|
154
|
Chris@331
|
155 // std::cerr << "FeatureExtractionModelTransformer: output sample type "
|
Chris@320
|
156 // << m_descriptor->sampleType << std::endl;
|
Chris@320
|
157
|
Chris@320
|
158 int binCount = 1;
|
Chris@320
|
159 float minValue = 0.0, maxValue = 0.0;
|
Chris@320
|
160 bool haveExtents = false;
|
Chris@320
|
161
|
Chris@320
|
162 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
163 binCount = m_descriptor->binCount;
|
Chris@320
|
164 }
|
Chris@320
|
165
|
Chris@331
|
166 // std::cerr << "FeatureExtractionModelTransformer: output bin count "
|
Chris@320
|
167 // << binCount << std::endl;
|
Chris@320
|
168
|
Chris@320
|
169 if (binCount > 0 && m_descriptor->hasKnownExtents) {
|
Chris@320
|
170 minValue = m_descriptor->minValue;
|
Chris@320
|
171 maxValue = m_descriptor->maxValue;
|
Chris@320
|
172 haveExtents = true;
|
Chris@320
|
173 }
|
Chris@320
|
174
|
Chris@350
|
175 size_t modelRate = input->getSampleRate();
|
Chris@320
|
176 size_t modelResolution = 1;
|
Chris@320
|
177
|
Chris@320
|
178 switch (m_descriptor->sampleType) {
|
Chris@320
|
179
|
Chris@320
|
180 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
|
Chris@320
|
181 if (m_descriptor->sampleRate != 0.0) {
|
Chris@320
|
182 modelResolution = size_t(modelRate / m_descriptor->sampleRate + 0.001);
|
Chris@320
|
183 }
|
Chris@320
|
184 break;
|
Chris@320
|
185
|
Chris@320
|
186 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
|
Chris@350
|
187 modelResolution = m_transform.getStepSize();
|
Chris@320
|
188 break;
|
Chris@320
|
189
|
Chris@320
|
190 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
|
Chris@320
|
191 modelRate = size_t(m_descriptor->sampleRate + 0.001);
|
Chris@320
|
192 break;
|
Chris@320
|
193 }
|
Chris@320
|
194
|
Chris@320
|
195 if (binCount == 0) {
|
Chris@320
|
196
|
Chris@320
|
197 m_output = new SparseOneDimensionalModel(modelRate, modelResolution,
|
Chris@320
|
198 false);
|
Chris@320
|
199
|
Chris@320
|
200 } else if (binCount == 1) {
|
Chris@320
|
201
|
Chris@320
|
202 SparseTimeValueModel *model;
|
Chris@320
|
203 if (haveExtents) {
|
Chris@320
|
204 model = new SparseTimeValueModel
|
Chris@320
|
205 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@320
|
206 } else {
|
Chris@320
|
207 model = new SparseTimeValueModel
|
Chris@320
|
208 (modelRate, modelResolution, false);
|
Chris@320
|
209 }
|
Chris@320
|
210 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
|
Chris@320
|
211
|
Chris@320
|
212 m_output = model;
|
Chris@320
|
213
|
Chris@320
|
214 } else if (m_descriptor->sampleType ==
|
Chris@320
|
215 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
216
|
Chris@320
|
217 // We don't have a sparse 3D model, so interpret this as a
|
Chris@320
|
218 // note model. There's nothing to define which values to use
|
Chris@320
|
219 // as which parameters of the note -- for the moment let's
|
Chris@320
|
220 // treat the first as pitch, second as duration in frames,
|
Chris@320
|
221 // third (if present) as velocity. (Our note model doesn't
|
Chris@320
|
222 // yet store velocity.)
|
Chris@320
|
223 //!!! todo: ask the user!
|
Chris@320
|
224
|
Chris@320
|
225 NoteModel *model;
|
Chris@320
|
226 if (haveExtents) {
|
Chris@320
|
227 model = new NoteModel
|
Chris@320
|
228 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@320
|
229 } else {
|
Chris@320
|
230 model = new NoteModel
|
Chris@320
|
231 (modelRate, modelResolution, false);
|
Chris@320
|
232 }
|
Chris@320
|
233 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
|
Chris@320
|
234
|
Chris@320
|
235 m_output = model;
|
Chris@320
|
236
|
Chris@320
|
237 } else {
|
Chris@320
|
238
|
Chris@320
|
239 EditableDenseThreeDimensionalModel *model =
|
Chris@320
|
240 new EditableDenseThreeDimensionalModel
|
Chris@320
|
241 (modelRate, modelResolution, binCount, false);
|
Chris@320
|
242
|
Chris@320
|
243 if (!m_descriptor->binNames.empty()) {
|
Chris@320
|
244 std::vector<QString> names;
|
Chris@320
|
245 for (size_t i = 0; i < m_descriptor->binNames.size(); ++i) {
|
Chris@320
|
246 names.push_back(m_descriptor->binNames[i].c_str());
|
Chris@320
|
247 }
|
Chris@320
|
248 model->setBinNames(names);
|
Chris@320
|
249 }
|
Chris@320
|
250
|
Chris@320
|
251 m_output = model;
|
Chris@320
|
252 }
|
Chris@333
|
253
|
Chris@350
|
254 if (m_output) m_output->setSourceModel(input);
|
Chris@320
|
255 }
|
Chris@320
|
256
|
Chris@331
|
257 FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()
|
Chris@320
|
258 {
|
Chris@331
|
259 std::cerr << "FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()" << std::endl;
|
Chris@320
|
260 delete m_plugin;
|
Chris@320
|
261 delete m_descriptor;
|
Chris@320
|
262 }
|
Chris@320
|
263
|
Chris@320
|
264 DenseTimeValueModel *
|
Chris@350
|
265 FeatureExtractionModelTransformer::getConformingInput()
|
Chris@320
|
266 {
|
Chris@320
|
267 DenseTimeValueModel *dtvm =
|
Chris@320
|
268 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@320
|
269 if (!dtvm) {
|
Chris@350
|
270 std::cerr << "FeatureExtractionModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
|
Chris@320
|
271 }
|
Chris@320
|
272 return dtvm;
|
Chris@320
|
273 }
|
Chris@320
|
274
|
Chris@320
|
275 void
|
Chris@331
|
276 FeatureExtractionModelTransformer::run()
|
Chris@320
|
277 {
|
Chris@350
|
278 DenseTimeValueModel *input = getConformingInput();
|
Chris@320
|
279 if (!input) return;
|
Chris@320
|
280
|
Chris@320
|
281 if (!m_output) return;
|
Chris@320
|
282
|
Chris@320
|
283 while (!input->isReady()) {
|
Chris@320
|
284 /*
|
Chris@320
|
285 if (dynamic_cast<WaveFileModel *>(input)) {
|
Chris@331
|
286 std::cerr << "FeatureExtractionModelTransformer::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
|
287 sleep(2);
|
Chris@320
|
288 break; // no need to wait
|
Chris@320
|
289 }
|
Chris@320
|
290 */
|
Chris@331
|
291 std::cerr << "FeatureExtractionModelTransformer::run: Waiting for input model to be ready..." << std::endl;
|
Chris@320
|
292 sleep(1);
|
Chris@320
|
293 }
|
Chris@320
|
294
|
Chris@350
|
295 size_t sampleRate = input->getSampleRate();
|
Chris@320
|
296
|
Chris@320
|
297 size_t channelCount = input->getChannelCount();
|
Chris@320
|
298 if (m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
299 channelCount = 1;
|
Chris@320
|
300 }
|
Chris@320
|
301
|
Chris@320
|
302 float **buffers = new float*[channelCount];
|
Chris@320
|
303 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@350
|
304 buffers[ch] = new float[m_transform.getBlockSize() + 2];
|
Chris@320
|
305 }
|
Chris@320
|
306
|
Chris@350
|
307 size_t stepSize = m_transform.getStepSize();
|
Chris@350
|
308 size_t blockSize = m_transform.getBlockSize();
|
Chris@350
|
309
|
Chris@320
|
310 bool frequencyDomain = (m_plugin->getInputDomain() ==
|
Chris@320
|
311 Vamp::Plugin::FrequencyDomain);
|
Chris@320
|
312 std::vector<FFTModel *> fftModels;
|
Chris@320
|
313
|
Chris@320
|
314 if (frequencyDomain) {
|
Chris@320
|
315 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
316 FFTModel *model = new FFTModel
|
Chris@350
|
317 (getConformingInput(),
|
Chris@350
|
318 channelCount == 1 ? m_input.getChannel() : ch,
|
Chris@350
|
319 m_transform.getWindowType(),
|
Chris@350
|
320 blockSize,
|
Chris@350
|
321 stepSize,
|
Chris@350
|
322 blockSize,
|
Chris@334
|
323 false,
|
Chris@334
|
324 StorageAdviser::PrecisionCritical);
|
Chris@320
|
325 if (!model->isOK()) {
|
Chris@320
|
326 QMessageBox::critical
|
Chris@320
|
327 (0, tr("FFT cache failed"),
|
Chris@320
|
328 tr("Failed to create the FFT model for this transform.\n"
|
Chris@320
|
329 "There may be insufficient memory or disc space to continue."));
|
Chris@320
|
330 delete model;
|
Chris@320
|
331 setCompletion(100);
|
Chris@320
|
332 return;
|
Chris@320
|
333 }
|
Chris@320
|
334 model->resume();
|
Chris@320
|
335 fftModels.push_back(model);
|
Chris@320
|
336 }
|
Chris@320
|
337 }
|
Chris@320
|
338
|
Chris@350
|
339 long startFrame = m_input.getModel()->getStartFrame();
|
Chris@350
|
340 long endFrame = m_input.getModel()->getEndFrame();
|
Chris@320
|
341
|
Chris@350
|
342 RealTime contextStartRT = m_transform.getStartTime();
|
Chris@350
|
343 RealTime contextDurationRT = m_transform.getDuration();
|
Chris@350
|
344
|
Chris@350
|
345 long contextStart =
|
Chris@350
|
346 RealTime::realTime2Frame(contextStartRT, sampleRate);
|
Chris@350
|
347
|
Chris@350
|
348 long contextDuration =
|
Chris@350
|
349 RealTime::realTime2Frame(contextDurationRT, sampleRate);
|
Chris@320
|
350
|
Chris@320
|
351 if (contextStart == 0 || contextStart < startFrame) {
|
Chris@320
|
352 contextStart = startFrame;
|
Chris@320
|
353 }
|
Chris@320
|
354
|
Chris@320
|
355 if (contextDuration == 0) {
|
Chris@320
|
356 contextDuration = endFrame - contextStart;
|
Chris@320
|
357 }
|
Chris@320
|
358 if (contextStart + contextDuration > endFrame) {
|
Chris@320
|
359 contextDuration = endFrame - contextStart;
|
Chris@320
|
360 }
|
Chris@320
|
361
|
Chris@320
|
362 long blockFrame = contextStart;
|
Chris@320
|
363
|
Chris@320
|
364 long prevCompletion = 0;
|
Chris@320
|
365
|
Chris@320
|
366 setCompletion(0);
|
Chris@320
|
367
|
Chris@320
|
368 while (!m_abandoned) {
|
Chris@320
|
369
|
Chris@320
|
370 if (frequencyDomain) {
|
Chris@350
|
371 if (blockFrame - int(blockSize)/2 >
|
Chris@320
|
372 contextStart + contextDuration) break;
|
Chris@320
|
373 } else {
|
Chris@320
|
374 if (blockFrame >=
|
Chris@320
|
375 contextStart + contextDuration) break;
|
Chris@320
|
376 }
|
Chris@320
|
377
|
Chris@331
|
378 // std::cerr << "FeatureExtractionModelTransformer::run: blockFrame "
|
Chris@320
|
379 // << blockFrame << ", endFrame " << endFrame << ", blockSize "
|
Chris@350
|
380 // << blockSize << std::endl;
|
Chris@320
|
381
|
Chris@320
|
382 long completion =
|
Chris@350
|
383 (((blockFrame - contextStart) / stepSize) * 99) /
|
Chris@350
|
384 (contextDuration / stepSize);
|
Chris@320
|
385
|
Chris@350
|
386 // channelCount is either m_input.getModel()->channelCount or 1
|
Chris@320
|
387
|
Chris@320
|
388 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
389 if (frequencyDomain) {
|
Chris@350
|
390 int column = (blockFrame - startFrame) / stepSize;
|
Chris@350
|
391 for (size_t i = 0; i <= blockSize/2; ++i) {
|
Chris@320
|
392 fftModels[ch]->getValuesAt
|
Chris@320
|
393 (column, i, buffers[ch][i*2], buffers[ch][i*2+1]);
|
Chris@320
|
394 }
|
Chris@320
|
395 } else {
|
Chris@320
|
396 getFrames(ch, channelCount,
|
Chris@350
|
397 blockFrame, blockSize, buffers[ch]);
|
Chris@320
|
398 }
|
Chris@320
|
399 }
|
Chris@320
|
400
|
Chris@320
|
401 Vamp::Plugin::FeatureSet features = m_plugin->process
|
Chris@320
|
402 (buffers, Vamp::RealTime::frame2RealTime(blockFrame, sampleRate));
|
Chris@320
|
403
|
Chris@320
|
404 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@320
|
405 Vamp::Plugin::Feature feature =
|
Chris@320
|
406 features[m_outputFeatureNo][fi];
|
Chris@320
|
407 addFeature(blockFrame, feature);
|
Chris@320
|
408 }
|
Chris@320
|
409
|
Chris@320
|
410 if (blockFrame == contextStart || completion > prevCompletion) {
|
Chris@320
|
411 setCompletion(completion);
|
Chris@320
|
412 prevCompletion = completion;
|
Chris@320
|
413 }
|
Chris@320
|
414
|
Chris@350
|
415 blockFrame += stepSize;
|
Chris@320
|
416 }
|
Chris@320
|
417
|
Chris@320
|
418 if (m_abandoned) return;
|
Chris@320
|
419
|
Chris@320
|
420 Vamp::Plugin::FeatureSet features = m_plugin->getRemainingFeatures();
|
Chris@320
|
421
|
Chris@320
|
422 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@320
|
423 Vamp::Plugin::Feature feature =
|
Chris@320
|
424 features[m_outputFeatureNo][fi];
|
Chris@320
|
425 addFeature(blockFrame, feature);
|
Chris@320
|
426 }
|
Chris@320
|
427
|
Chris@320
|
428 if (frequencyDomain) {
|
Chris@320
|
429 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
430 delete fftModels[ch];
|
Chris@320
|
431 }
|
Chris@320
|
432 }
|
Chris@320
|
433
|
Chris@320
|
434 setCompletion(100);
|
Chris@320
|
435 }
|
Chris@320
|
436
|
Chris@320
|
437 void
|
Chris@331
|
438 FeatureExtractionModelTransformer::getFrames(int channel, int channelCount,
|
Chris@320
|
439 long startFrame, long size,
|
Chris@320
|
440 float *buffer)
|
Chris@320
|
441 {
|
Chris@320
|
442 long offset = 0;
|
Chris@320
|
443
|
Chris@320
|
444 if (startFrame < 0) {
|
Chris@320
|
445 for (int i = 0; i < size && startFrame + i < 0; ++i) {
|
Chris@320
|
446 buffer[i] = 0.0f;
|
Chris@320
|
447 }
|
Chris@320
|
448 offset = -startFrame;
|
Chris@320
|
449 size -= offset;
|
Chris@320
|
450 if (size <= 0) return;
|
Chris@320
|
451 startFrame = 0;
|
Chris@320
|
452 }
|
Chris@320
|
453
|
Chris@350
|
454 DenseTimeValueModel *input = getConformingInput();
|
Chris@350
|
455 if (!input) return;
|
Chris@350
|
456
|
Chris@350
|
457 long got = input->getData
|
Chris@350
|
458 ((channelCount == 1 ? m_input.getChannel() : channel),
|
Chris@320
|
459 startFrame, size, buffer + offset);
|
Chris@320
|
460
|
Chris@320
|
461 while (got < size) {
|
Chris@320
|
462 buffer[offset + got] = 0.0;
|
Chris@320
|
463 ++got;
|
Chris@320
|
464 }
|
Chris@320
|
465
|
Chris@350
|
466 if (m_input.getChannel() == -1 && channelCount == 1 &&
|
Chris@350
|
467 input->getChannelCount() > 1) {
|
Chris@320
|
468 // use mean instead of sum, as plugin input
|
Chris@350
|
469 int cc = input->getChannelCount();
|
Chris@320
|
470 for (long i = 0; i < size; ++i) {
|
Chris@320
|
471 buffer[i] /= cc;
|
Chris@320
|
472 }
|
Chris@320
|
473 }
|
Chris@320
|
474 }
|
Chris@320
|
475
|
Chris@320
|
476 void
|
Chris@331
|
477 FeatureExtractionModelTransformer::addFeature(size_t blockFrame,
|
Chris@320
|
478 const Vamp::Plugin::Feature &feature)
|
Chris@320
|
479 {
|
Chris@350
|
480 size_t inputRate = m_input.getModel()->getSampleRate();
|
Chris@320
|
481
|
Chris@331
|
482 // std::cerr << "FeatureExtractionModelTransformer::addFeature("
|
Chris@320
|
483 // << blockFrame << ")" << std::endl;
|
Chris@320
|
484
|
Chris@320
|
485 int binCount = 1;
|
Chris@320
|
486 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
487 binCount = m_descriptor->binCount;
|
Chris@320
|
488 }
|
Chris@320
|
489
|
Chris@320
|
490 size_t frame = blockFrame;
|
Chris@320
|
491
|
Chris@320
|
492 if (m_descriptor->sampleType ==
|
Chris@320
|
493 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
494
|
Chris@320
|
495 if (!feature.hasTimestamp) {
|
Chris@320
|
496 std::cerr
|
Chris@331
|
497 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
|
Chris@320
|
498 << "Feature has variable sample rate but no timestamp!"
|
Chris@320
|
499 << std::endl;
|
Chris@320
|
500 return;
|
Chris@320
|
501 } else {
|
Chris@320
|
502 frame = Vamp::RealTime::realTime2Frame(feature.timestamp, inputRate);
|
Chris@320
|
503 }
|
Chris@320
|
504
|
Chris@320
|
505 } else if (m_descriptor->sampleType ==
|
Chris@320
|
506 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
|
Chris@320
|
507
|
Chris@320
|
508 if (feature.hasTimestamp) {
|
Chris@320
|
509 //!!! warning: sampleRate may be non-integral
|
Chris@320
|
510 frame = Vamp::RealTime::realTime2Frame(feature.timestamp,
|
Chris@320
|
511 lrintf(m_descriptor->sampleRate));
|
Chris@320
|
512 } else {
|
Chris@320
|
513 frame = m_output->getEndFrame();
|
Chris@320
|
514 }
|
Chris@320
|
515 }
|
Chris@320
|
516
|
Chris@320
|
517 if (binCount == 0) {
|
Chris@320
|
518
|
Chris@350
|
519 SparseOneDimensionalModel *model =
|
Chris@350
|
520 getConformingOutput<SparseOneDimensionalModel>();
|
Chris@320
|
521 if (!model) return;
|
Chris@350
|
522
|
Chris@320
|
523 model->addPoint(SparseOneDimensionalModel::Point(frame, feature.label.c_str()));
|
Chris@320
|
524
|
Chris@320
|
525 } else if (binCount == 1) {
|
Chris@320
|
526
|
Chris@320
|
527 float value = 0.0;
|
Chris@320
|
528 if (feature.values.size() > 0) value = feature.values[0];
|
Chris@320
|
529
|
Chris@350
|
530 SparseTimeValueModel *model =
|
Chris@350
|
531 getConformingOutput<SparseTimeValueModel>();
|
Chris@320
|
532 if (!model) return;
|
Chris@350
|
533
|
Chris@320
|
534 model->addPoint(SparseTimeValueModel::Point(frame, value, feature.label.c_str()));
|
Chris@320
|
535 // std::cerr << "SparseTimeValueModel::addPoint(" << frame << ", " << value << "), " << feature.label.c_str() << std::endl;
|
Chris@320
|
536
|
Chris@320
|
537 } else if (m_descriptor->sampleType ==
|
Chris@320
|
538 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
539
|
Chris@320
|
540 float pitch = 0.0;
|
Chris@320
|
541 if (feature.values.size() > 0) pitch = feature.values[0];
|
Chris@320
|
542
|
Chris@320
|
543 float duration = 1;
|
Chris@320
|
544 if (feature.values.size() > 1) duration = feature.values[1];
|
Chris@320
|
545
|
Chris@320
|
546 float velocity = 100;
|
Chris@320
|
547 if (feature.values.size() > 2) velocity = feature.values[2];
|
Chris@340
|
548 if (velocity < 0) velocity = 127;
|
Chris@340
|
549 if (velocity > 127) velocity = 127;
|
Chris@320
|
550
|
Chris@350
|
551 NoteModel *model = getConformingOutput<NoteModel>();
|
Chris@320
|
552 if (!model) return;
|
Chris@320
|
553
|
Chris@320
|
554 model->addPoint(NoteModel::Point(frame, pitch,
|
Chris@320
|
555 lrintf(duration),
|
Chris@340
|
556 velocity / 127.f,
|
Chris@320
|
557 feature.label.c_str()));
|
Chris@320
|
558
|
Chris@320
|
559 } else {
|
Chris@320
|
560
|
Chris@320
|
561 DenseThreeDimensionalModel::Column values = feature.values;
|
Chris@320
|
562
|
Chris@320
|
563 EditableDenseThreeDimensionalModel *model =
|
Chris@350
|
564 getConformingOutput<EditableDenseThreeDimensionalModel>();
|
Chris@320
|
565 if (!model) return;
|
Chris@320
|
566
|
Chris@320
|
567 model->setColumn(frame / model->getResolution(), values);
|
Chris@320
|
568 }
|
Chris@320
|
569 }
|
Chris@320
|
570
|
Chris@320
|
571 void
|
Chris@331
|
572 FeatureExtractionModelTransformer::setCompletion(int completion)
|
Chris@320
|
573 {
|
Chris@320
|
574 int binCount = 1;
|
Chris@320
|
575 if (m_descriptor->hasFixedBinCount) {
|
Chris@320
|
576 binCount = m_descriptor->binCount;
|
Chris@320
|
577 }
|
Chris@320
|
578
|
Chris@331
|
579 // std::cerr << "FeatureExtractionModelTransformer::setCompletion("
|
Chris@320
|
580 // << completion << ")" << std::endl;
|
Chris@320
|
581
|
Chris@320
|
582 if (binCount == 0) {
|
Chris@320
|
583
|
Chris@350
|
584 SparseOneDimensionalModel *model =
|
Chris@350
|
585 getConformingOutput<SparseOneDimensionalModel>();
|
Chris@320
|
586 if (!model) return;
|
Chris@350
|
587 model->setCompletion(completion, true); //!!!m_context.updates);
|
Chris@320
|
588
|
Chris@320
|
589 } else if (binCount == 1) {
|
Chris@320
|
590
|
Chris@350
|
591 SparseTimeValueModel *model =
|
Chris@350
|
592 getConformingOutput<SparseTimeValueModel>();
|
Chris@320
|
593 if (!model) return;
|
Chris@350
|
594 model->setCompletion(completion, true); //!!!m_context.updates);
|
Chris@320
|
595
|
Chris@320
|
596 } else if (m_descriptor->sampleType ==
|
Chris@320
|
597 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
598
|
Chris@350
|
599 NoteModel *model =
|
Chris@350
|
600 getConformingOutput<NoteModel>();
|
Chris@320
|
601 if (!model) return;
|
Chris@350
|
602 model->setCompletion(completion, true); //!!!m_context.updates);
|
Chris@320
|
603
|
Chris@320
|
604 } else {
|
Chris@320
|
605
|
Chris@320
|
606 EditableDenseThreeDimensionalModel *model =
|
Chris@350
|
607 getConformingOutput<EditableDenseThreeDimensionalModel>();
|
Chris@320
|
608 if (!model) return;
|
Chris@350
|
609 model->setCompletion(completion, true); //!!!m_context.updates);
|
Chris@320
|
610 }
|
Chris@320
|
611 }
|
Chris@320
|
612
|