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@1225
|
19
|
Chris@320
|
20 #include "plugin/PluginXml.h"
|
Chris@475
|
21 #include <vamp-hostsdk/Plugin.h>
|
Chris@320
|
22
|
Chris@320
|
23 #include "data/model/Model.h"
|
Chris@320
|
24 #include "base/Window.h"
|
Chris@387
|
25 #include "base/Exceptions.h"
|
Chris@320
|
26 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@320
|
27 #include "data/model/SparseTimeValueModel.h"
|
Chris@320
|
28 #include "data/model/EditableDenseThreeDimensionalModel.h"
|
Chris@320
|
29 #include "data/model/DenseTimeValueModel.h"
|
Chris@320
|
30 #include "data/model/NoteModel.h"
|
gyorgyf@786
|
31 #include "data/model/FlexiNoteModel.h"
|
Chris@441
|
32 #include "data/model/RegionModel.h"
|
Chris@320
|
33 #include "data/model/FFTModel.h"
|
Chris@320
|
34 #include "data/model/WaveFileModel.h"
|
Chris@558
|
35 #include "rdf/PluginRDFDescription.h"
|
Chris@320
|
36
|
Chris@350
|
37 #include "TransformFactory.h"
|
Chris@350
|
38
|
Chris@320
|
39 #include <iostream>
|
Chris@320
|
40
|
Chris@859
|
41 #include <QSettings>
|
Chris@859
|
42
|
Chris@350
|
43 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
|
Chris@859
|
44 const Transform &transform) :
|
Chris@350
|
45 ModelTransformer(in, transform),
|
Chris@1211
|
46 m_plugin(0),
|
Chris@1211
|
47 m_haveOutputs(false)
|
Chris@320
|
48 {
|
Chris@1080
|
49 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl;
|
Chris@849
|
50 }
|
Chris@849
|
51
|
Chris@849
|
52 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
|
Chris@859
|
53 const Transforms &transforms) :
|
Chris@849
|
54 ModelTransformer(in, transforms),
|
Chris@1211
|
55 m_plugin(0),
|
Chris@1211
|
56 m_haveOutputs(false)
|
Chris@849
|
57 {
|
Chris@1080
|
58 if (m_transforms.empty()) {
|
Chris@1080
|
59 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s)" << endl;
|
Chris@1080
|
60 } else {
|
Chris@1080
|
61 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s), first has plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl;
|
Chris@1080
|
62 }
|
Chris@849
|
63 }
|
Chris@849
|
64
|
Chris@849
|
65 static bool
|
Chris@849
|
66 areTransformsSimilar(const Transform &t1, const Transform &t2)
|
Chris@849
|
67 {
|
Chris@849
|
68 Transform t2o(t2);
|
Chris@849
|
69 t2o.setOutput(t1.getOutput());
|
Chris@849
|
70 return t1 == t2o;
|
Chris@849
|
71 }
|
Chris@849
|
72
|
Chris@849
|
73 bool
|
Chris@849
|
74 FeatureExtractionModelTransformer::initialise()
|
Chris@849
|
75 {
|
Chris@1237
|
76 // This is (now) called from the run thread. The plugin is
|
Chris@1237
|
77 // constructed, initialised, used, and destroyed all from a single
|
Chris@1237
|
78 // thread.
|
Chris@1237
|
79
|
Chris@849
|
80 // All transforms must use the same plugin, parameters, and
|
Chris@849
|
81 // inputs: they can differ only in choice of plugin output. So we
|
Chris@849
|
82 // initialise based purely on the first transform in the list (but
|
Chris@849
|
83 // first check that they are actually similar as promised)
|
Chris@849
|
84
|
Chris@849
|
85 for (int j = 1; j < (int)m_transforms.size(); ++j) {
|
Chris@849
|
86 if (!areTransformsSimilar(m_transforms[0], m_transforms[j])) {
|
Chris@849
|
87 m_message = tr("Transforms supplied to a single FeatureExtractionModelTransformer instance must be similar in every respect except plugin output");
|
Chris@1368
|
88 SVCERR << m_message << endl;
|
Chris@849
|
89 return false;
|
Chris@849
|
90 }
|
Chris@849
|
91 }
|
Chris@849
|
92
|
Chris@849
|
93 Transform primaryTransform = m_transforms[0];
|
Chris@849
|
94
|
Chris@849
|
95 QString pluginId = primaryTransform.getPluginIdentifier();
|
Chris@320
|
96
|
Chris@1226
|
97 FeatureExtractionPluginFactory *factory =
|
Chris@1226
|
98 FeatureExtractionPluginFactory::instance();
|
Chris@320
|
99
|
Chris@320
|
100 if (!factory) {
|
Chris@361
|
101 m_message = tr("No factory available for feature extraction plugin id \"%1\" (unknown plugin type, or internal error?)").arg(pluginId);
|
Chris@1368
|
102 SVCERR << m_message << endl;
|
Chris@849
|
103 return false;
|
Chris@320
|
104 }
|
Chris@320
|
105
|
Chris@350
|
106 DenseTimeValueModel *input = getConformingInput();
|
Chris@350
|
107 if (!input) {
|
Chris@361
|
108 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
|
Chris@1368
|
109 SVCERR << m_message << endl;
|
Chris@849
|
110 return false;
|
Chris@350
|
111 }
|
Chris@320
|
112
|
Chris@1264
|
113 SVDEBUG << "FeatureExtractionModelTransformer: Instantiating plugin for transform in thread "
|
Chris@1264
|
114 << QThread::currentThreadId() << endl;
|
Chris@1211
|
115
|
Chris@1040
|
116 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
|
Chris@320
|
117 if (!m_plugin) {
|
Chris@361
|
118 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
|
Chris@1368
|
119 SVCERR << m_message << endl;
|
Chris@849
|
120 return false;
|
Chris@320
|
121 }
|
Chris@320
|
122
|
Chris@350
|
123 TransformFactory::getInstance()->makeContextConsistentWithPlugin
|
Chris@849
|
124 (primaryTransform, m_plugin);
|
Chris@1368
|
125
|
Chris@350
|
126 TransformFactory::getInstance()->setPluginParameters
|
Chris@849
|
127 (primaryTransform, m_plugin);
|
Chris@1368
|
128
|
Chris@930
|
129 int channelCount = input->getChannelCount();
|
Chris@930
|
130 if ((int)m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
131 channelCount = 1;
|
Chris@320
|
132 }
|
Chris@930
|
133 if ((int)m_plugin->getMinChannelCount() > channelCount) {
|
Chris@361
|
134 m_message = tr("Cannot provide enough channels to feature extraction plugin \"%1\" (plugin min is %2, max %3; input model has %4)")
|
Chris@361
|
135 .arg(pluginId)
|
Chris@361
|
136 .arg(m_plugin->getMinChannelCount())
|
Chris@361
|
137 .arg(m_plugin->getMaxChannelCount())
|
Chris@361
|
138 .arg(input->getChannelCount());
|
Chris@1368
|
139 SVCERR << m_message << endl;
|
Chris@849
|
140 return false;
|
Chris@320
|
141 }
|
Chris@1368
|
142
|
Chris@690
|
143 SVDEBUG << "Initialising feature extraction plugin with channels = "
|
Chris@1264
|
144 << channelCount << ", step = " << primaryTransform.getStepSize()
|
Chris@1264
|
145 << ", block = " << primaryTransform.getBlockSize() << endl;
|
Chris@320
|
146
|
Chris@320
|
147 if (!m_plugin->initialise(channelCount,
|
Chris@849
|
148 primaryTransform.getStepSize(),
|
Chris@849
|
149 primaryTransform.getBlockSize())) {
|
Chris@1264
|
150
|
Chris@930
|
151 int pstep = primaryTransform.getStepSize();
|
Chris@930
|
152 int pblock = primaryTransform.getBlockSize();
|
Chris@361
|
153
|
Chris@850
|
154 ///!!! hang on, this isn't right -- we're modifying a copy
|
Chris@849
|
155 primaryTransform.setStepSize(0);
|
Chris@849
|
156 primaryTransform.setBlockSize(0);
|
Chris@361
|
157 TransformFactory::getInstance()->makeContextConsistentWithPlugin
|
Chris@849
|
158 (primaryTransform, m_plugin);
|
Chris@361
|
159
|
Chris@849
|
160 if (primaryTransform.getStepSize() != pstep ||
|
Chris@849
|
161 primaryTransform.getBlockSize() != pblock) {
|
Chris@1264
|
162
|
Chris@1264
|
163 SVDEBUG << "Initialisation failed, trying again with default step = "
|
Chris@1264
|
164 << primaryTransform.getStepSize()
|
Chris@1264
|
165 << ", block = " << primaryTransform.getBlockSize() << endl;
|
Chris@361
|
166
|
Chris@361
|
167 if (!m_plugin->initialise(channelCount,
|
Chris@849
|
168 primaryTransform.getStepSize(),
|
Chris@849
|
169 primaryTransform.getBlockSize())) {
|
Chris@361
|
170
|
Chris@1264
|
171 SVDEBUG << "Initialisation failed again" << endl;
|
Chris@1264
|
172
|
Chris@361
|
173 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
|
Chris@1368
|
174 SVCERR << m_message << endl;
|
Chris@849
|
175 return false;
|
Chris@361
|
176
|
Chris@361
|
177 } else {
|
Chris@1264
|
178
|
Chris@1264
|
179 SVDEBUG << "Initialisation succeeded this time" << endl;
|
Chris@1264
|
180
|
Chris@361
|
181 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
|
182 .arg(pluginId)
|
Chris@361
|
183 .arg(pstep)
|
Chris@361
|
184 .arg(pblock)
|
Chris@849
|
185 .arg(primaryTransform.getStepSize())
|
Chris@849
|
186 .arg(primaryTransform.getBlockSize());
|
Chris@1368
|
187 SVCERR << m_message << endl;
|
Chris@361
|
188 }
|
Chris@361
|
189
|
Chris@361
|
190 } else {
|
Chris@361
|
191
|
Chris@1264
|
192 SVDEBUG << "Initialisation failed" << endl;
|
Chris@1264
|
193
|
Chris@361
|
194 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
|
Chris@1368
|
195 SVCERR << m_message << endl;
|
Chris@849
|
196 return false;
|
Chris@361
|
197 }
|
Chris@1264
|
198 } else {
|
Chris@1264
|
199 SVDEBUG << "Initialisation succeeded" << endl;
|
Chris@320
|
200 }
|
Chris@320
|
201
|
Chris@849
|
202 if (primaryTransform.getPluginVersion() != "") {
|
Chris@366
|
203 QString pv = QString("%1").arg(m_plugin->getPluginVersion());
|
Chris@849
|
204 if (pv != primaryTransform.getPluginVersion()) {
|
Chris@366
|
205 QString vm = tr("Transform was configured for version %1 of plugin \"%2\", but the plugin being used is version %3")
|
Chris@849
|
206 .arg(primaryTransform.getPluginVersion())
|
Chris@366
|
207 .arg(pluginId)
|
Chris@366
|
208 .arg(pv);
|
Chris@366
|
209 if (m_message != "") {
|
Chris@366
|
210 m_message = QString("%1; %2").arg(vm).arg(m_message);
|
Chris@366
|
211 } else {
|
Chris@366
|
212 m_message = vm;
|
Chris@366
|
213 }
|
Chris@1368
|
214 SVCERR << m_message << endl;
|
Chris@366
|
215 }
|
Chris@366
|
216 }
|
Chris@366
|
217
|
Chris@320
|
218 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
|
Chris@320
|
219
|
Chris@320
|
220 if (outputs.empty()) {
|
Chris@361
|
221 m_message = tr("Plugin \"%1\" has no outputs").arg(pluginId);
|
Chris@1368
|
222 SVCERR << m_message << endl;
|
Chris@849
|
223 return false;
|
Chris@320
|
224 }
|
Chris@320
|
225
|
Chris@849
|
226 for (int j = 0; j < (int)m_transforms.size(); ++j) {
|
Chris@849
|
227
|
Chris@849
|
228 for (int i = 0; i < (int)outputs.size(); ++i) {
|
Chris@849
|
229 // SVDEBUG << "comparing output " << i << " name \"" << outputs[i].identifier << "\" with expected \"" << m_transform.getOutput() << "\"" << endl;
|
Chris@849
|
230 if (m_transforms[j].getOutput() == "" ||
|
Chris@849
|
231 outputs[i].identifier == m_transforms[j].getOutput().toStdString()) {
|
Chris@849
|
232 m_outputNos.push_back(i);
|
Chris@849
|
233 m_descriptors.push_back(new Vamp::Plugin::OutputDescriptor(outputs[i]));
|
Chris@849
|
234 m_fixedRateFeatureNos.push_back(-1); // we increment before use
|
Chris@849
|
235 break;
|
Chris@849
|
236 }
|
Chris@849
|
237 }
|
Chris@849
|
238
|
Chris@930
|
239 if ((int)m_descriptors.size() <= j) {
|
Chris@849
|
240 m_message = tr("Plugin \"%1\" has no output named \"%2\"")
|
Chris@849
|
241 .arg(pluginId)
|
Chris@849
|
242 .arg(m_transforms[j].getOutput());
|
Chris@1368
|
243 SVCERR << m_message << endl;
|
Chris@849
|
244 return false;
|
Chris@849
|
245 }
|
Chris@320
|
246 }
|
Chris@320
|
247
|
Chris@849
|
248 for (int j = 0; j < (int)m_transforms.size(); ++j) {
|
Chris@876
|
249 createOutputModels(j);
|
Chris@849
|
250 }
|
Chris@849
|
251
|
Chris@1211
|
252 m_outputMutex.lock();
|
Chris@1211
|
253 m_haveOutputs = true;
|
Chris@1211
|
254 m_outputsCondition.wakeAll();
|
Chris@1211
|
255 m_outputMutex.unlock();
|
Chris@1211
|
256
|
Chris@849
|
257 return true;
|
Chris@558
|
258 }
|
Chris@558
|
259
|
Chris@558
|
260 void
|
Chris@1237
|
261 FeatureExtractionModelTransformer::deinitialise()
|
Chris@1237
|
262 {
|
Chris@1264
|
263 SVDEBUG << "FeatureExtractionModelTransformer: deleting plugin for transform in thread "
|
Chris@1264
|
264 << QThread::currentThreadId() << endl;
|
Chris@1372
|
265
|
Chris@1372
|
266 try {
|
Chris@1372
|
267 delete m_plugin;
|
Chris@1372
|
268 } catch (const std::exception &e) {
|
Chris@1372
|
269 // A destructor shouldn't throw an exception. But at one point
|
Chris@1372
|
270 // (now fixed) our plugin stub destructor could have
|
Chris@1372
|
271 // accidentally done so, so just in case:
|
Chris@1372
|
272 SVCERR << "FeatureExtractionModelTransformer: caught exception while deleting plugin: " << e.what() << endl;
|
Chris@1372
|
273 m_message = e.what();
|
Chris@1372
|
274 }
|
Chris@1372
|
275 m_plugin = 0;
|
Chris@1372
|
276
|
Chris@1237
|
277 for (int j = 0; j < (int)m_descriptors.size(); ++j) {
|
Chris@1237
|
278 delete m_descriptors[j];
|
Chris@1237
|
279 }
|
Chris@1237
|
280 }
|
Chris@1237
|
281
|
Chris@1237
|
282 void
|
Chris@876
|
283 FeatureExtractionModelTransformer::createOutputModels(int n)
|
Chris@558
|
284 {
|
Chris@558
|
285 DenseTimeValueModel *input = getConformingInput();
|
Chris@712
|
286
|
Chris@849
|
287 PluginRDFDescription description(m_transforms[n].getPluginIdentifier());
|
Chris@849
|
288 QString outputId = m_transforms[n].getOutput();
|
Chris@558
|
289
|
Chris@320
|
290 int binCount = 1;
|
Chris@320
|
291 float minValue = 0.0, maxValue = 0.0;
|
Chris@320
|
292 bool haveExtents = false;
|
Chris@876
|
293 bool haveBinCount = m_descriptors[n]->hasFixedBinCount;
|
Chris@876
|
294
|
Chris@876
|
295 if (haveBinCount) {
|
Chris@1039
|
296 binCount = (int)m_descriptors[n]->binCount;
|
Chris@320
|
297 }
|
Chris@320
|
298
|
Chris@876
|
299 m_needAdditionalModels[n] = false;
|
Chris@876
|
300
|
Chris@843
|
301 // cerr << "FeatureExtractionModelTransformer: output bin count "
|
Chris@843
|
302 // << binCount << endl;
|
Chris@320
|
303
|
Chris@849
|
304 if (binCount > 0 && m_descriptors[n]->hasKnownExtents) {
|
Chris@849
|
305 minValue = m_descriptors[n]->minValue;
|
Chris@849
|
306 maxValue = m_descriptors[n]->maxValue;
|
Chris@320
|
307 haveExtents = true;
|
Chris@320
|
308 }
|
Chris@320
|
309
|
Chris@1040
|
310 sv_samplerate_t modelRate = input->getSampleRate();
|
Chris@1254
|
311 sv_samplerate_t outputRate = modelRate;
|
Chris@930
|
312 int modelResolution = 1;
|
Chris@712
|
313
|
Chris@849
|
314 if (m_descriptors[n]->sampleType !=
|
Chris@785
|
315 Vamp::Plugin::OutputDescriptor::OneSamplePerStep) {
|
Chris@1254
|
316
|
Chris@1254
|
317 outputRate = m_descriptors[n]->sampleRate;
|
Chris@1254
|
318
|
Chris@1254
|
319 //!!! SV doesn't actually support display of models that have
|
Chris@1254
|
320 //!!! different underlying rates together -- so we always set
|
Chris@1254
|
321 //!!! the model rate to be the input model's rate, and adjust
|
Chris@1254
|
322 //!!! the resolution appropriately. We can't properly display
|
Chris@1254
|
323 //!!! data with a higher resolution than the base model at all
|
Chris@1254
|
324 if (outputRate > input->getSampleRate()) {
|
Chris@1264
|
325 SVDEBUG << "WARNING: plugin reports output sample rate as "
|
Chris@1264
|
326 << outputRate
|
Chris@1264
|
327 << " (can't display features with finer resolution than the input rate of "
|
Chris@1264
|
328 << modelRate << ")" << endl;
|
Chris@1254
|
329 outputRate = modelRate;
|
Chris@785
|
330 }
|
Chris@785
|
331 }
|
Chris@785
|
332
|
Chris@849
|
333 switch (m_descriptors[n]->sampleType) {
|
Chris@320
|
334
|
Chris@320
|
335 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
|
Chris@1254
|
336 if (outputRate != 0.0) {
|
Chris@1254
|
337 modelResolution = int(round(modelRate / outputRate));
|
Chris@320
|
338 }
|
Chris@320
|
339 break;
|
Chris@320
|
340
|
Chris@320
|
341 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
|
Chris@849
|
342 modelResolution = m_transforms[n].getStepSize();
|
Chris@320
|
343 break;
|
Chris@320
|
344
|
Chris@320
|
345 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
|
Chris@1254
|
346 if (outputRate <= 0.0) {
|
Chris@1264
|
347 SVDEBUG << "WARNING: Fixed sample-rate plugin reports invalid sample rate " << m_descriptors[n]->sampleRate << "; defaulting to input rate of " << input->getSampleRate() << endl;
|
Chris@1071
|
348 modelResolution = 1;
|
Chris@451
|
349 } else {
|
Chris@1254
|
350 modelResolution = int(round(modelRate / outputRate));
|
Chris@1254
|
351 // cerr << "modelRate = " << modelRate << ", descriptor rate = " << outputRate << ", modelResolution = " << modelResolution << endl;
|
Chris@451
|
352 }
|
Chris@320
|
353 break;
|
Chris@320
|
354 }
|
Chris@320
|
355
|
Chris@441
|
356 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2);
|
Chris@441
|
357
|
Chris@849
|
358 Model *out = 0;
|
Chris@849
|
359
|
Chris@441
|
360 if (binCount == 0 &&
|
Chris@849
|
361 (preDurationPlugin || !m_descriptors[n]->hasDuration)) {
|
Chris@320
|
362
|
Chris@445
|
363 // Anything with no value and no duration is an instant
|
Chris@445
|
364
|
Chris@849
|
365 out = new SparseOneDimensionalModel(modelRate, modelResolution, false);
|
Chris@558
|
366 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
|
Chris@849
|
367 out->setRDFTypeURI(outputEventTypeURI);
|
Chris@558
|
368
|
Chris@441
|
369 } else if ((preDurationPlugin && binCount > 1 &&
|
Chris@849
|
370 (m_descriptors[n]->sampleType ==
|
Chris@441
|
371 Vamp::Plugin::OutputDescriptor::VariableSampleRate)) ||
|
Chris@849
|
372 (!preDurationPlugin && m_descriptors[n]->hasDuration)) {
|
Chris@441
|
373
|
Chris@441
|
374 // For plugins using the old v1 API without explicit duration,
|
Chris@441
|
375 // we treat anything that has multiple bins (i.e. that has the
|
Chris@441
|
376 // potential to have value and duration) and a variable sample
|
Chris@441
|
377 // rate as a note model, taking its values as pitch, duration
|
Chris@441
|
378 // and velocity (if present) respectively. This is the same
|
Chris@441
|
379 // behaviour as always applied by SV to these plugins in the
|
Chris@441
|
380 // past.
|
Chris@441
|
381
|
Chris@441
|
382 // For plugins with the newer API, we treat anything with
|
Chris@441
|
383 // duration as either a note model with pitch and velocity, or
|
Chris@441
|
384 // a region model.
|
Chris@441
|
385
|
Chris@441
|
386 // How do we know whether it's an interval or note model?
|
Chris@441
|
387 // What's the essential difference? Is a note model any
|
Chris@441
|
388 // interval model using a Hz or "MIDI pitch" scale? There
|
Chris@441
|
389 // isn't really a reliable test for "MIDI pitch"... Does a
|
Chris@441
|
390 // note model always have velocity? This is a good question
|
Chris@441
|
391 // to be addressed by accompanying RDF, but for the moment we
|
Chris@441
|
392 // will do the following...
|
Chris@441
|
393
|
Chris@441
|
394 bool isNoteModel = false;
|
Chris@441
|
395
|
Chris@441
|
396 // Regions have only value (and duration -- we can't extract a
|
Chris@441
|
397 // region model from an old-style plugin that doesn't support
|
Chris@441
|
398 // duration)
|
Chris@441
|
399 if (binCount > 1) isNoteModel = true;
|
Chris@441
|
400
|
Chris@595
|
401 // Regions do not have units of Hz or MIDI things (a sweeping
|
Chris@595
|
402 // assumption!)
|
Chris@849
|
403 if (m_descriptors[n]->unit == "Hz" ||
|
Chris@849
|
404 m_descriptors[n]->unit.find("MIDI") != std::string::npos ||
|
Chris@849
|
405 m_descriptors[n]->unit.find("midi") != std::string::npos) {
|
Chris@595
|
406 isNoteModel = true;
|
Chris@595
|
407 }
|
Chris@441
|
408
|
Chris@441
|
409 // If we had a "sparse 3D model", we would have the additional
|
Chris@441
|
410 // problem of determining whether to use that here (if bin
|
Chris@441
|
411 // count > 1). But we don't.
|
Chris@441
|
412
|
Chris@859
|
413 QSettings settings;
|
Chris@859
|
414 settings.beginGroup("Transformer");
|
Chris@859
|
415 bool flexi = settings.value("use-flexi-note-model", false).toBool();
|
Chris@859
|
416 settings.endGroup();
|
Chris@859
|
417
|
Chris@859
|
418 cerr << "flexi = " << flexi << endl;
|
Chris@859
|
419
|
Chris@859
|
420 if (isNoteModel && !flexi) {
|
Chris@441
|
421
|
Chris@441
|
422 NoteModel *model;
|
Chris@441
|
423 if (haveExtents) {
|
Chris@859
|
424 model = new NoteModel
|
Chris@859
|
425 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@441
|
426 } else {
|
Chris@859
|
427 model = new NoteModel
|
Chris@859
|
428 (modelRate, modelResolution, false);
|
gyorgyf@786
|
429 }
|
Chris@849
|
430 model->setScaleUnits(m_descriptors[n]->unit.c_str());
|
Chris@849
|
431 out = model;
|
gyorgyf@786
|
432
|
Chris@859
|
433 } else if (isNoteModel && flexi) {
|
gyorgyf@786
|
434
|
gyorgyf@786
|
435 FlexiNoteModel *model;
|
gyorgyf@786
|
436 if (haveExtents) {
|
Chris@859
|
437 model = new FlexiNoteModel
|
Chris@859
|
438 (modelRate, modelResolution, minValue, maxValue, false);
|
gyorgyf@786
|
439 } else {
|
Chris@859
|
440 model = new FlexiNoteModel
|
Chris@859
|
441 (modelRate, modelResolution, false);
|
Chris@441
|
442 }
|
Chris@849
|
443 model->setScaleUnits(m_descriptors[n]->unit.c_str());
|
Chris@849
|
444 out = model;
|
Chris@441
|
445
|
Chris@441
|
446 } else {
|
Chris@441
|
447
|
Chris@441
|
448 RegionModel *model;
|
Chris@441
|
449 if (haveExtents) {
|
Chris@441
|
450 model = new RegionModel
|
Chris@441
|
451 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@441
|
452 } else {
|
Chris@441
|
453 model = new RegionModel
|
Chris@441
|
454 (modelRate, modelResolution, false);
|
Chris@441
|
455 }
|
Chris@849
|
456 model->setScaleUnits(m_descriptors[n]->unit.c_str());
|
Chris@849
|
457 out = model;
|
Chris@441
|
458 }
|
Chris@441
|
459
|
Chris@558
|
460 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
|
Chris@849
|
461 out->setRDFTypeURI(outputEventTypeURI);
|
Chris@558
|
462
|
Chris@876
|
463 } else if (binCount == 1 ||
|
Chris@849
|
464 (m_descriptors[n]->sampleType ==
|
Chris@441
|
465 Vamp::Plugin::OutputDescriptor::VariableSampleRate)) {
|
Chris@441
|
466
|
Chris@441
|
467 // Anything that is not a 1D, note, or interval model and that
|
Chris@441
|
468 // has only one value per result must be a sparse time value
|
Chris@441
|
469 // model.
|
Chris@441
|
470
|
Chris@441
|
471 // Anything that is not a 1D, note, or interval model and that
|
Chris@876
|
472 // has a variable sample rate is treated as a set of sparse
|
Chris@876
|
473 // time value models, one per output bin, because we lack a
|
Chris@441
|
474 // sparse 3D model.
|
Chris@320
|
475
|
Chris@876
|
476 // Anything that is not a 1D, note, or interval model and that
|
Chris@876
|
477 // has a fixed sample rate but an unknown number of values per
|
Chris@876
|
478 // result is also treated as a set of sparse time value models.
|
Chris@876
|
479
|
Chris@876
|
480 // For sets of sparse time value models, we create a single
|
Chris@876
|
481 // model first as the "standard" output and then create models
|
Chris@876
|
482 // for bins 1+ in the additional model map (mapping the output
|
Chris@876
|
483 // descriptor to a list of models indexed by bin-1). But we
|
Chris@876
|
484 // don't create the additional models yet, as this case has to
|
Chris@876
|
485 // work even if the number of bins is unknown at this point --
|
Chris@877
|
486 // we create an additional model (copying its parameters from
|
Chris@877
|
487 // the default one) each time a new bin is encountered.
|
Chris@876
|
488
|
Chris@876
|
489 if (!haveBinCount || binCount > 1) {
|
Chris@876
|
490 m_needAdditionalModels[n] = true;
|
Chris@876
|
491 }
|
Chris@876
|
492
|
Chris@320
|
493 SparseTimeValueModel *model;
|
Chris@320
|
494 if (haveExtents) {
|
Chris@320
|
495 model = new SparseTimeValueModel
|
Chris@320
|
496 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@320
|
497 } else {
|
Chris@320
|
498 model = new SparseTimeValueModel
|
Chris@320
|
499 (modelRate, modelResolution, false);
|
Chris@320
|
500 }
|
Chris@558
|
501
|
Chris@558
|
502 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
|
Chris@849
|
503 model->setScaleUnits(outputs[m_outputNos[n]].unit.c_str());
|
Chris@320
|
504
|
Chris@849
|
505 out = model;
|
Chris@320
|
506
|
Chris@558
|
507 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
|
Chris@849
|
508 out->setRDFTypeURI(outputEventTypeURI);
|
Chris@558
|
509
|
Chris@441
|
510 } else {
|
Chris@320
|
511
|
Chris@441
|
512 // Anything that is not a 1D, note, or interval model and that
|
Chris@441
|
513 // has a fixed sample rate and more than one value per result
|
Chris@441
|
514 // must be a dense 3D model.
|
Chris@320
|
515
|
Chris@320
|
516 EditableDenseThreeDimensionalModel *model =
|
Chris@320
|
517 new EditableDenseThreeDimensionalModel
|
Chris@535
|
518 (modelRate, modelResolution, binCount,
|
Chris@535
|
519 EditableDenseThreeDimensionalModel::BasicMultirateCompression,
|
Chris@535
|
520 false);
|
Chris@320
|
521
|
Chris@849
|
522 if (!m_descriptors[n]->binNames.empty()) {
|
Chris@320
|
523 std::vector<QString> names;
|
Chris@930
|
524 for (int i = 0; i < (int)m_descriptors[n]->binNames.size(); ++i) {
|
Chris@849
|
525 names.push_back(m_descriptors[n]->binNames[i].c_str());
|
Chris@320
|
526 }
|
Chris@320
|
527 model->setBinNames(names);
|
Chris@320
|
528 }
|
Chris@320
|
529
|
Chris@849
|
530 out = model;
|
Chris@558
|
531
|
Chris@558
|
532 QString outputSignalTypeURI = description.getOutputSignalTypeURI(outputId);
|
Chris@849
|
533 out->setRDFTypeURI(outputSignalTypeURI);
|
Chris@320
|
534 }
|
Chris@333
|
535
|
Chris@849
|
536 if (out) {
|
Chris@849
|
537 out->setSourceModel(input);
|
Chris@849
|
538 m_outputs.push_back(out);
|
Chris@849
|
539 }
|
Chris@320
|
540 }
|
Chris@320
|
541
|
Chris@1211
|
542 void
|
Chris@1211
|
543 FeatureExtractionModelTransformer::awaitOutputModels()
|
Chris@1211
|
544 {
|
Chris@1211
|
545 m_outputMutex.lock();
|
Chris@1368
|
546 while (!m_haveOutputs && !m_abandoned) {
|
Chris@1368
|
547 m_outputsCondition.wait(&m_outputMutex, 500);
|
Chris@1211
|
548 }
|
Chris@1211
|
549 m_outputMutex.unlock();
|
Chris@1211
|
550 }
|
Chris@1211
|
551
|
Chris@331
|
552 FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()
|
Chris@320
|
553 {
|
Chris@1237
|
554 // Parent class dtor set the abandoned flag and waited for the run
|
Chris@1237
|
555 // thread to exit; the run thread owns the plugin, and should have
|
Chris@1237
|
556 // destroyed it before exiting (via a call to deinitialise)
|
Chris@320
|
557 }
|
Chris@320
|
558
|
Chris@876
|
559 FeatureExtractionModelTransformer::Models
|
Chris@876
|
560 FeatureExtractionModelTransformer::getAdditionalOutputModels()
|
Chris@876
|
561 {
|
Chris@876
|
562 Models mm;
|
Chris@876
|
563 for (AdditionalModelMap::iterator i = m_additionalModels.begin();
|
Chris@876
|
564 i != m_additionalModels.end(); ++i) {
|
Chris@876
|
565 for (std::map<int, SparseTimeValueModel *>::iterator j =
|
Chris@876
|
566 i->second.begin();
|
Chris@876
|
567 j != i->second.end(); ++j) {
|
Chris@876
|
568 SparseTimeValueModel *m = j->second;
|
Chris@876
|
569 if (m) mm.push_back(m);
|
Chris@876
|
570 }
|
Chris@876
|
571 }
|
Chris@876
|
572 return mm;
|
Chris@876
|
573 }
|
Chris@876
|
574
|
Chris@877
|
575 bool
|
Chris@877
|
576 FeatureExtractionModelTransformer::willHaveAdditionalOutputModels()
|
Chris@877
|
577 {
|
Chris@877
|
578 for (std::map<int, bool>::const_iterator i =
|
Chris@877
|
579 m_needAdditionalModels.begin();
|
Chris@877
|
580 i != m_needAdditionalModels.end(); ++i) {
|
Chris@877
|
581 if (i->second) return true;
|
Chris@877
|
582 }
|
Chris@877
|
583 return false;
|
Chris@877
|
584 }
|
Chris@877
|
585
|
Chris@876
|
586 SparseTimeValueModel *
|
Chris@876
|
587 FeatureExtractionModelTransformer::getAdditionalModel(int n, int binNo)
|
Chris@876
|
588 {
|
Chris@893
|
589 // std::cerr << "getAdditionalModel(" << n << ", " << binNo << ")" << std::endl;
|
Chris@876
|
590
|
Chris@876
|
591 if (binNo == 0) {
|
Chris@876
|
592 std::cerr << "Internal error: binNo == 0 in getAdditionalModel (should be using primary model)" << std::endl;
|
Chris@876
|
593 return 0;
|
Chris@876
|
594 }
|
Chris@876
|
595
|
Chris@876
|
596 if (!m_needAdditionalModels[n]) return 0;
|
Chris@876
|
597 if (!isOutput<SparseTimeValueModel>(n)) return 0;
|
Chris@876
|
598 if (m_additionalModels[n][binNo]) return m_additionalModels[n][binNo];
|
Chris@876
|
599
|
Chris@876
|
600 std::cerr << "getAdditionalModel(" << n << ", " << binNo << "): creating" << std::endl;
|
Chris@876
|
601
|
Chris@876
|
602 SparseTimeValueModel *baseModel = getConformingOutput<SparseTimeValueModel>(n);
|
Chris@876
|
603 if (!baseModel) return 0;
|
Chris@876
|
604
|
Chris@876
|
605 std::cerr << "getAdditionalModel(" << n << ", " << binNo << "): (from " << baseModel << ")" << std::endl;
|
Chris@876
|
606
|
Chris@876
|
607 SparseTimeValueModel *additional =
|
Chris@876
|
608 new SparseTimeValueModel(baseModel->getSampleRate(),
|
Chris@876
|
609 baseModel->getResolution(),
|
Chris@876
|
610 baseModel->getValueMinimum(),
|
Chris@876
|
611 baseModel->getValueMaximum(),
|
Chris@876
|
612 false);
|
Chris@876
|
613
|
Chris@876
|
614 additional->setScaleUnits(baseModel->getScaleUnits());
|
Chris@876
|
615 additional->setRDFTypeURI(baseModel->getRDFTypeURI());
|
Chris@876
|
616
|
Chris@876
|
617 m_additionalModels[n][binNo] = additional;
|
Chris@876
|
618 return additional;
|
Chris@876
|
619 }
|
Chris@876
|
620
|
Chris@320
|
621 DenseTimeValueModel *
|
Chris@350
|
622 FeatureExtractionModelTransformer::getConformingInput()
|
Chris@320
|
623 {
|
Chris@690
|
624 // SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: input model is " << getInputModel() << endl;
|
Chris@408
|
625
|
Chris@320
|
626 DenseTimeValueModel *dtvm =
|
Chris@320
|
627 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@320
|
628 if (!dtvm) {
|
Chris@690
|
629 SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << endl;
|
Chris@320
|
630 }
|
Chris@320
|
631 return dtvm;
|
Chris@320
|
632 }
|
Chris@320
|
633
|
Chris@320
|
634 void
|
Chris@331
|
635 FeatureExtractionModelTransformer::run()
|
Chris@320
|
636 {
|
Chris@1373
|
637 try {
|
Chris@1373
|
638 if (!initialise()) {
|
Chris@1373
|
639 abandon();
|
Chris@1373
|
640 return;
|
Chris@1373
|
641 }
|
Chris@1373
|
642 } catch (const std::exception &e) {
|
Chris@1368
|
643 abandon();
|
Chris@1373
|
644 m_message = e.what();
|
Chris@1368
|
645 return;
|
Chris@1368
|
646 }
|
Chris@1211
|
647
|
Chris@350
|
648 DenseTimeValueModel *input = getConformingInput();
|
Chris@1368
|
649 if (!input) {
|
Chris@1368
|
650 abandon();
|
Chris@1368
|
651 return;
|
Chris@1368
|
652 }
|
Chris@320
|
653
|
Chris@1368
|
654 if (m_outputs.empty()) {
|
Chris@1368
|
655 abandon();
|
Chris@1368
|
656 return;
|
Chris@1368
|
657 }
|
Chris@320
|
658
|
Chris@850
|
659 Transform primaryTransform = m_transforms[0];
|
Chris@850
|
660
|
Chris@497
|
661 while (!input->isReady() && !m_abandoned) {
|
Chris@877
|
662 cerr << "FeatureExtractionModelTransformer::run: Waiting for input model to be ready..." << endl;
|
Chris@497
|
663 usleep(500000);
|
Chris@320
|
664 }
|
Chris@497
|
665 if (m_abandoned) return;
|
Chris@320
|
666
|
Chris@1040
|
667 sv_samplerate_t sampleRate = input->getSampleRate();
|
Chris@320
|
668
|
Chris@930
|
669 int channelCount = input->getChannelCount();
|
Chris@930
|
670 if ((int)m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@320
|
671 channelCount = 1;
|
Chris@320
|
672 }
|
Chris@320
|
673
|
Chris@320
|
674 float **buffers = new float*[channelCount];
|
Chris@930
|
675 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@850
|
676 buffers[ch] = new float[primaryTransform.getBlockSize() + 2];
|
Chris@320
|
677 }
|
Chris@320
|
678
|
Chris@930
|
679 int stepSize = primaryTransform.getStepSize();
|
Chris@930
|
680 int blockSize = primaryTransform.getBlockSize();
|
Chris@350
|
681
|
Chris@320
|
682 bool frequencyDomain = (m_plugin->getInputDomain() ==
|
Chris@320
|
683 Vamp::Plugin::FrequencyDomain);
|
Chris@320
|
684 std::vector<FFTModel *> fftModels;
|
Chris@320
|
685
|
Chris@320
|
686 if (frequencyDomain) {
|
Chris@930
|
687 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
688 FFTModel *model = new FFTModel
|
Chris@350
|
689 (getConformingInput(),
|
Chris@350
|
690 channelCount == 1 ? m_input.getChannel() : ch,
|
Chris@850
|
691 primaryTransform.getWindowType(),
|
Chris@350
|
692 blockSize,
|
Chris@350
|
693 stepSize,
|
Chris@1090
|
694 blockSize);
|
Chris@1080
|
695 if (!model->isOK() || model->getError() != "") {
|
Chris@1080
|
696 QString err = model->getError();
|
Chris@320
|
697 delete model;
|
Chris@850
|
698 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@850
|
699 setCompletion(j, 100);
|
Chris@850
|
700 }
|
Chris@387
|
701 //!!! need a better way to handle this -- previously we were using a QMessageBox but that isn't an appropriate thing to do here either
|
Chris@1080
|
702 throw AllocationFailed("Failed to create the FFT model for this feature extraction model transformer: error is: " + err);
|
Chris@320
|
703 }
|
Chris@320
|
704 fftModels.push_back(model);
|
Chris@1080
|
705 cerr << "created model for channel " << ch << endl;
|
Chris@320
|
706 }
|
Chris@320
|
707 }
|
Chris@320
|
708
|
Chris@1040
|
709 sv_frame_t startFrame = m_input.getModel()->getStartFrame();
|
Chris@1040
|
710 sv_frame_t endFrame = m_input.getModel()->getEndFrame();
|
Chris@320
|
711
|
Chris@850
|
712 RealTime contextStartRT = primaryTransform.getStartTime();
|
Chris@850
|
713 RealTime contextDurationRT = primaryTransform.getDuration();
|
Chris@350
|
714
|
Chris@1040
|
715 sv_frame_t contextStart =
|
Chris@350
|
716 RealTime::realTime2Frame(contextStartRT, sampleRate);
|
Chris@350
|
717
|
Chris@1040
|
718 sv_frame_t contextDuration =
|
Chris@350
|
719 RealTime::realTime2Frame(contextDurationRT, sampleRate);
|
Chris@320
|
720
|
Chris@320
|
721 if (contextStart == 0 || contextStart < startFrame) {
|
Chris@320
|
722 contextStart = startFrame;
|
Chris@320
|
723 }
|
Chris@320
|
724
|
Chris@320
|
725 if (contextDuration == 0) {
|
Chris@320
|
726 contextDuration = endFrame - contextStart;
|
Chris@320
|
727 }
|
Chris@320
|
728 if (contextStart + contextDuration > endFrame) {
|
Chris@320
|
729 contextDuration = endFrame - contextStart;
|
Chris@320
|
730 }
|
Chris@320
|
731
|
Chris@1039
|
732 sv_frame_t blockFrame = contextStart;
|
Chris@320
|
733
|
Chris@320
|
734 long prevCompletion = 0;
|
Chris@320
|
735
|
Chris@850
|
736 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@850
|
737 setCompletion(j, 0);
|
Chris@850
|
738 }
|
Chris@320
|
739
|
Chris@556
|
740 float *reals = 0;
|
Chris@556
|
741 float *imaginaries = 0;
|
Chris@556
|
742 if (frequencyDomain) {
|
Chris@556
|
743 reals = new float[blockSize/2 + 1];
|
Chris@556
|
744 imaginaries = new float[blockSize/2 + 1];
|
Chris@556
|
745 }
|
Chris@556
|
746
|
Chris@678
|
747 QString error = "";
|
Chris@678
|
748
|
Chris@1372
|
749 try {
|
Chris@1372
|
750 while (!m_abandoned) {
|
Chris@320
|
751
|
Chris@1372
|
752 if (frequencyDomain) {
|
Chris@1372
|
753 if (blockFrame - int(blockSize)/2 >
|
Chris@1372
|
754 contextStart + contextDuration) break;
|
Chris@1372
|
755 } else {
|
Chris@1372
|
756 if (blockFrame >=
|
Chris@1372
|
757 contextStart + contextDuration) break;
|
Chris@1372
|
758 }
|
Chris@320
|
759
|
Chris@690
|
760 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame "
|
Chris@320
|
761 // << blockFrame << ", endFrame " << endFrame << ", blockSize "
|
Chris@687
|
762 // << blockSize << endl;
|
Chris@320
|
763
|
Chris@1372
|
764 int completion = int
|
Chris@1372
|
765 ((((blockFrame - contextStart) / stepSize) * 99) /
|
Chris@1372
|
766 (contextDuration / stepSize + 1));
|
Chris@320
|
767
|
Chris@1372
|
768 // channelCount is either m_input.getModel()->channelCount or 1
|
Chris@320
|
769
|
Chris@1372
|
770 if (frequencyDomain) {
|
Chris@1372
|
771 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@1372
|
772 int column = int((blockFrame - startFrame) / stepSize);
|
Chris@1372
|
773 if (fftModels[ch]->getValuesAt(column, reals, imaginaries)) {
|
Chris@1372
|
774 for (int i = 0; i <= blockSize/2; ++i) {
|
Chris@1372
|
775 buffers[ch][i*2] = reals[i];
|
Chris@1372
|
776 buffers[ch][i*2+1] = imaginaries[i];
|
Chris@1372
|
777 }
|
Chris@1372
|
778 } else {
|
Chris@1372
|
779 for (int i = 0; i <= blockSize/2; ++i) {
|
Chris@1372
|
780 buffers[ch][i*2] = 0.f;
|
Chris@1372
|
781 buffers[ch][i*2+1] = 0.f;
|
Chris@1372
|
782 }
|
Chris@1372
|
783 }
|
Chris@1372
|
784 error = fftModels[ch]->getError();
|
Chris@1372
|
785 if (error != "") {
|
Chris@1372
|
786 SVCERR << "FeatureExtractionModelTransformer::run: Abandoning, error is " << error << endl;
|
Chris@1372
|
787 m_abandoned = true;
|
Chris@1372
|
788 m_message = error;
|
Chris@1372
|
789 break;
|
Chris@1008
|
790 }
|
Chris@1372
|
791 }
|
Chris@1372
|
792 } else {
|
Chris@1372
|
793 getFrames(channelCount, blockFrame, blockSize, buffers);
|
Chris@1372
|
794 }
|
Chris@1372
|
795
|
Chris@1372
|
796 if (m_abandoned) break;
|
Chris@1372
|
797
|
Chris@1372
|
798 Vamp::Plugin::FeatureSet features = m_plugin->process
|
Chris@1372
|
799 (buffers, RealTime::frame2RealTime(blockFrame, sampleRate).toVampRealTime());
|
Chris@1372
|
800
|
Chris@1372
|
801 if (m_abandoned) break;
|
Chris@1372
|
802
|
Chris@1372
|
803 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@1372
|
804 for (int fi = 0; fi < (int)features[m_outputNos[j]].size(); ++fi) {
|
Chris@1372
|
805 Vamp::Plugin::Feature feature = features[m_outputNos[j]][fi];
|
Chris@1372
|
806 addFeature(j, blockFrame, feature);
|
Chris@678
|
807 }
|
Chris@363
|
808 }
|
Chris@1372
|
809
|
Chris@1372
|
810 if (blockFrame == contextStart || completion > prevCompletion) {
|
Chris@1372
|
811 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@1372
|
812 setCompletion(j, completion);
|
Chris@1372
|
813 }
|
Chris@1372
|
814 prevCompletion = completion;
|
Chris@1372
|
815 }
|
Chris@1372
|
816
|
Chris@1372
|
817 blockFrame += stepSize;
|
Chris@1372
|
818
|
Chris@320
|
819 }
|
Chris@320
|
820
|
Chris@1372
|
821 if (!m_abandoned) {
|
Chris@1372
|
822 Vamp::Plugin::FeatureSet features = m_plugin->getRemainingFeatures();
|
Chris@497
|
823
|
Chris@1372
|
824 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@1372
|
825 for (int fi = 0; fi < (int)features[m_outputNos[j]].size(); ++fi) {
|
Chris@1372
|
826 Vamp::Plugin::Feature feature = features[m_outputNos[j]][fi];
|
Chris@1372
|
827 addFeature(j, blockFrame, feature);
|
Chris@1372
|
828 }
|
Chris@850
|
829 }
|
Chris@850
|
830 }
|
Chris@1372
|
831 } catch (const std::exception &e) {
|
Chris@1372
|
832 SVCERR << "FeatureExtractionModelTransformer::run: Exception caught: "
|
Chris@1372
|
833 << e.what() << endl;
|
Chris@1372
|
834 m_abandoned = true;
|
Chris@1372
|
835 m_message = e.what();
|
Chris@497
|
836 }
|
Chris@320
|
837
|
Chris@850
|
838 for (int j = 0; j < (int)m_outputNos.size(); ++j) {
|
Chris@850
|
839 setCompletion(j, 100);
|
Chris@850
|
840 }
|
Chris@320
|
841
|
Chris@320
|
842 if (frequencyDomain) {
|
Chris@930
|
843 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@320
|
844 delete fftModels[ch];
|
Chris@320
|
845 }
|
Chris@556
|
846 delete[] reals;
|
Chris@556
|
847 delete[] imaginaries;
|
Chris@320
|
848 }
|
Chris@974
|
849
|
Chris@974
|
850 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@974
|
851 delete[] buffers[ch];
|
Chris@974
|
852 }
|
Chris@974
|
853 delete[] buffers;
|
Chris@1237
|
854
|
Chris@1237
|
855 deinitialise();
|
Chris@320
|
856 }
|
Chris@320
|
857
|
Chris@320
|
858 void
|
Chris@363
|
859 FeatureExtractionModelTransformer::getFrames(int channelCount,
|
Chris@1039
|
860 sv_frame_t startFrame,
|
Chris@1039
|
861 sv_frame_t size,
|
Chris@363
|
862 float **buffers)
|
Chris@320
|
863 {
|
Chris@1039
|
864 sv_frame_t offset = 0;
|
Chris@320
|
865
|
Chris@320
|
866 if (startFrame < 0) {
|
Chris@363
|
867 for (int c = 0; c < channelCount; ++c) {
|
Chris@1039
|
868 for (sv_frame_t i = 0; i < size && startFrame + i < 0; ++i) {
|
Chris@363
|
869 buffers[c][i] = 0.0f;
|
Chris@363
|
870 }
|
Chris@320
|
871 }
|
Chris@320
|
872 offset = -startFrame;
|
Chris@320
|
873 size -= offset;
|
Chris@320
|
874 if (size <= 0) return;
|
Chris@320
|
875 startFrame = 0;
|
Chris@320
|
876 }
|
Chris@320
|
877
|
Chris@350
|
878 DenseTimeValueModel *input = getConformingInput();
|
Chris@350
|
879 if (!input) return;
|
Chris@363
|
880
|
Chris@1039
|
881 sv_frame_t got = 0;
|
Chris@350
|
882
|
Chris@363
|
883 if (channelCount == 1) {
|
Chris@363
|
884
|
Chris@1096
|
885 auto data = input->getData(m_input.getChannel(), startFrame, size);
|
Chris@1096
|
886 got = data.size();
|
Chris@1096
|
887
|
Chris@1096
|
888 copy(data.begin(), data.end(), buffers[0] + offset);
|
Chris@363
|
889
|
Chris@363
|
890 if (m_input.getChannel() == -1 && input->getChannelCount() > 1) {
|
Chris@363
|
891 // use mean instead of sum, as plugin input
|
Chris@363
|
892 float cc = float(input->getChannelCount());
|
Chris@1096
|
893 for (sv_frame_t i = 0; i < got; ++i) {
|
Chris@363
|
894 buffers[0][i + offset] /= cc;
|
Chris@363
|
895 }
|
Chris@363
|
896 }
|
Chris@363
|
897
|
Chris@363
|
898 } else {
|
Chris@363
|
899
|
Chris@1096
|
900 auto data = input->getMultiChannelData(0, channelCount-1, startFrame, size);
|
Chris@1096
|
901 if (!data.empty()) {
|
Chris@1096
|
902 got = data[0].size();
|
Chris@1096
|
903 for (int c = 0; in_range_for(data, c); ++c) {
|
Chris@1096
|
904 copy(data[c].begin(), data[c].end(), buffers[c] + offset);
|
Chris@363
|
905 }
|
Chris@363
|
906 }
|
Chris@363
|
907 }
|
Chris@320
|
908
|
Chris@320
|
909 while (got < size) {
|
Chris@363
|
910 for (int c = 0; c < channelCount; ++c) {
|
Chris@363
|
911 buffers[c][got + offset] = 0.0;
|
Chris@363
|
912 }
|
Chris@320
|
913 ++got;
|
Chris@320
|
914 }
|
Chris@320
|
915 }
|
Chris@320
|
916
|
Chris@320
|
917 void
|
Chris@850
|
918 FeatureExtractionModelTransformer::addFeature(int n,
|
Chris@1039
|
919 sv_frame_t blockFrame,
|
Chris@850
|
920 const Vamp::Plugin::Feature &feature)
|
Chris@320
|
921 {
|
Chris@1040
|
922 sv_samplerate_t inputRate = m_input.getModel()->getSampleRate();
|
Chris@320
|
923
|
Chris@843
|
924 // cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = "
|
Chris@712
|
925 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp
|
Chris@712
|
926 // << ", timestamp = " << feature.timestamp << ", hasDuration = "
|
Chris@712
|
927 // << feature.hasDuration << ", duration = " << feature.duration
|
Chris@843
|
928 // << endl;
|
Chris@320
|
929
|
Chris@1039
|
930 sv_frame_t frame = blockFrame;
|
Chris@320
|
931
|
Chris@849
|
932 if (m_descriptors[n]->sampleType ==
|
Chris@320
|
933 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
|
Chris@320
|
934
|
Chris@320
|
935 if (!feature.hasTimestamp) {
|
Chris@1264
|
936 SVDEBUG
|
Chris@331
|
937 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
|
Chris@320
|
938 << "Feature has variable sample rate but no timestamp!"
|
Chris@843
|
939 << endl;
|
Chris@320
|
940 return;
|
Chris@320
|
941 } else {
|
Chris@1040
|
942 frame = RealTime::realTime2Frame(feature.timestamp, inputRate);
|
Chris@320
|
943 }
|
Chris@320
|
944
|
Chris@1071
|
945 // cerr << "variable sample rate: timestamp = " << feature.timestamp
|
Chris@1071
|
946 // << " at input rate " << inputRate << " -> " << frame << endl;
|
Chris@1071
|
947
|
Chris@849
|
948 } else if (m_descriptors[n]->sampleType ==
|
Chris@320
|
949 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
|
Chris@320
|
950
|
Chris@1071
|
951 sv_samplerate_t rate = m_descriptors[n]->sampleRate;
|
Chris@1071
|
952 if (rate <= 0.0) {
|
Chris@1071
|
953 rate = inputRate;
|
Chris@1071
|
954 }
|
Chris@1071
|
955
|
Chris@779
|
956 if (!feature.hasTimestamp) {
|
Chris@849
|
957 ++m_fixedRateFeatureNos[n];
|
Chris@779
|
958 } else {
|
Chris@779
|
959 RealTime ts(feature.timestamp.sec, feature.timestamp.nsec);
|
Chris@1071
|
960 m_fixedRateFeatureNos[n] = (int)lrint(ts.toDouble() * rate);
|
Chris@779
|
961 }
|
Chris@862
|
962
|
Chris@1071
|
963 // cerr << "m_fixedRateFeatureNo = " << m_fixedRateFeatureNos[n]
|
Chris@1071
|
964 // << ", m_descriptor->sampleRate = " << m_descriptors[n]->sampleRate
|
Chris@862
|
965 // << ", inputRate = " << inputRate
|
Chris@862
|
966 // << " giving frame = ";
|
Chris@1071
|
967 frame = lrint((double(m_fixedRateFeatureNos[n]) / rate) * inputRate);
|
Chris@1071
|
968 // cerr << frame << endl;
|
Chris@320
|
969 }
|
Chris@862
|
970
|
Chris@862
|
971 if (frame < 0) {
|
Chris@1264
|
972 SVDEBUG
|
Chris@862
|
973 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
|
Chris@862
|
974 << "Negative frame counts are not supported (frame = " << frame
|
Chris@862
|
975 << " from timestamp " << feature.timestamp
|
Chris@862
|
976 << "), dropping feature"
|
Chris@862
|
977 << endl;
|
Chris@862
|
978 return;
|
Chris@862
|
979 }
|
Chris@862
|
980
|
Chris@441
|
981 // Rather than repeat the complicated tests from the constructor
|
Chris@441
|
982 // to determine what sort of model we must be adding the features
|
Chris@441
|
983 // to, we instead test what sort of model the constructor decided
|
Chris@441
|
984 // to create.
|
Chris@320
|
985
|
Chris@849
|
986 if (isOutput<SparseOneDimensionalModel>(n)) {
|
Chris@441
|
987
|
Chris@441
|
988 SparseOneDimensionalModel *model =
|
Chris@849
|
989 getConformingOutput<SparseOneDimensionalModel>(n);
|
Chris@320
|
990 if (!model) return;
|
Chris@350
|
991
|
Chris@441
|
992 model->addPoint(SparseOneDimensionalModel::Point
|
Chris@441
|
993 (frame, feature.label.c_str()));
|
Chris@320
|
994
|
Chris@849
|
995 } else if (isOutput<SparseTimeValueModel>(n)) {
|
Chris@320
|
996
|
Chris@350
|
997 SparseTimeValueModel *model =
|
Chris@849
|
998 getConformingOutput<SparseTimeValueModel>(n);
|
Chris@320
|
999 if (!model) return;
|
Chris@350
|
1000
|
Chris@930
|
1001 for (int i = 0; i < (int)feature.values.size(); ++i) {
|
Chris@454
|
1002
|
Chris@454
|
1003 float value = feature.values[i];
|
Chris@454
|
1004
|
Chris@454
|
1005 QString label = feature.label.c_str();
|
Chris@454
|
1006 if (feature.values.size() > 1) {
|
Chris@454
|
1007 label = QString("[%1] %2").arg(i+1).arg(label);
|
Chris@454
|
1008 }
|
Chris@454
|
1009
|
Chris@876
|
1010 SparseTimeValueModel *targetModel = model;
|
Chris@876
|
1011
|
Chris@876
|
1012 if (m_needAdditionalModels[n] && i > 0) {
|
Chris@876
|
1013 targetModel = getAdditionalModel(n, i);
|
Chris@876
|
1014 if (!targetModel) targetModel = model;
|
Chris@893
|
1015 // std::cerr << "adding point to model " << targetModel
|
Chris@893
|
1016 // << " for output " << n << " bin " << i << std::endl;
|
Chris@876
|
1017 }
|
Chris@876
|
1018
|
Chris@876
|
1019 targetModel->addPoint
|
Chris@876
|
1020 (SparseTimeValueModel::Point(frame, value, label));
|
Chris@454
|
1021 }
|
Chris@320
|
1022
|
Chris@849
|
1023 } else if (isOutput<FlexiNoteModel>(n) || isOutput<NoteModel>(n) || isOutput<RegionModel>(n)) { //GF: Added Note Model
|
Chris@320
|
1024
|
Chris@441
|
1025 int index = 0;
|
Chris@441
|
1026
|
Chris@441
|
1027 float value = 0.0;
|
Chris@930
|
1028 if ((int)feature.values.size() > index) {
|
Chris@441
|
1029 value = feature.values[index++];
|
Chris@441
|
1030 }
|
Chris@320
|
1031
|
Chris@1039
|
1032 sv_frame_t duration = 1;
|
Chris@441
|
1033 if (feature.hasDuration) {
|
Chris@1040
|
1034 duration = RealTime::realTime2Frame(feature.duration, inputRate);
|
Chris@441
|
1035 } else {
|
Chris@1039
|
1036 if (in_range_for(feature.values, index)) {
|
Chris@1039
|
1037 duration = lrintf(feature.values[index++]);
|
Chris@441
|
1038 }
|
Chris@441
|
1039 }
|
gyorgyf@786
|
1040
|
Chris@891
|
1041 if (isOutput<FlexiNoteModel>(n)) { // GF: added for flexi note model
|
gyorgyf@786
|
1042
|
gyorgyf@786
|
1043 float velocity = 100;
|
Chris@930
|
1044 if ((int)feature.values.size() > index) {
|
gyorgyf@786
|
1045 velocity = feature.values[index++];
|
gyorgyf@786
|
1046 }
|
gyorgyf@786
|
1047 if (velocity < 0) velocity = 127;
|
gyorgyf@786
|
1048 if (velocity > 127) velocity = 127;
|
gyorgyf@786
|
1049
|
Chris@849
|
1050 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n);
|
gyorgyf@786
|
1051 if (!model) return;
|
Chris@1039
|
1052 model->addPoint(FlexiNoteModel::Point(frame,
|
Chris@1039
|
1053 value, // value is pitch
|
Chris@1039
|
1054 duration,
|
Chris@1039
|
1055 velocity / 127.f,
|
Chris@1039
|
1056 feature.label.c_str()));
|
gyorgyf@786
|
1057 // GF: end -- added for flexi note model
|
Chris@849
|
1058 } else if (isOutput<NoteModel>(n)) {
|
Chris@320
|
1059
|
Chris@441
|
1060 float velocity = 100;
|
Chris@930
|
1061 if ((int)feature.values.size() > index) {
|
Chris@441
|
1062 velocity = feature.values[index++];
|
Chris@441
|
1063 }
|
Chris@441
|
1064 if (velocity < 0) velocity = 127;
|
Chris@441
|
1065 if (velocity > 127) velocity = 127;
|
Chris@320
|
1066
|
Chris@849
|
1067 NoteModel *model = getConformingOutput<NoteModel>(n);
|
Chris@441
|
1068 if (!model) return;
|
Chris@441
|
1069 model->addPoint(NoteModel::Point(frame, value, // value is pitch
|
Chris@1039
|
1070 duration,
|
Chris@441
|
1071 velocity / 127.f,
|
Chris@441
|
1072 feature.label.c_str()));
|
Chris@441
|
1073 } else {
|
gyorgyf@786
|
1074
|
Chris@849
|
1075 RegionModel *model = getConformingOutput<RegionModel>(n);
|
Chris@454
|
1076 if (!model) return;
|
Chris@454
|
1077
|
Chris@474
|
1078 if (feature.hasDuration && !feature.values.empty()) {
|
Chris@454
|
1079
|
Chris@930
|
1080 for (int i = 0; i < (int)feature.values.size(); ++i) {
|
Chris@454
|
1081
|
Chris@454
|
1082 float value = feature.values[i];
|
Chris@454
|
1083
|
Chris@454
|
1084 QString label = feature.label.c_str();
|
Chris@454
|
1085 if (feature.values.size() > 1) {
|
Chris@454
|
1086 label = QString("[%1] %2").arg(i+1).arg(label);
|
Chris@454
|
1087 }
|
Chris@454
|
1088
|
Chris@1039
|
1089 model->addPoint(RegionModel::Point(frame,
|
Chris@1039
|
1090 value,
|
Chris@1039
|
1091 duration,
|
Chris@454
|
1092 label));
|
Chris@454
|
1093 }
|
Chris@454
|
1094 } else {
|
Chris@454
|
1095
|
Chris@1039
|
1096 model->addPoint(RegionModel::Point(frame,
|
Chris@1039
|
1097 value,
|
Chris@1039
|
1098 duration,
|
Chris@441
|
1099 feature.label.c_str()));
|
Chris@454
|
1100 }
|
Chris@441
|
1101 }
|
Chris@320
|
1102
|
Chris@849
|
1103 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) {
|
Chris@320
|
1104
|
Chris@1154
|
1105 DenseThreeDimensionalModel::Column values = feature.values;
|
Chris@320
|
1106
|
Chris@320
|
1107 EditableDenseThreeDimensionalModel *model =
|
Chris@849
|
1108 getConformingOutput<EditableDenseThreeDimensionalModel>(n);
|
Chris@320
|
1109 if (!model) return;
|
Chris@320
|
1110
|
Chris@889
|
1111 // cerr << "(note: model resolution = " << model->getResolution() << ")"
|
Chris@889
|
1112 // << endl;
|
Chris@889
|
1113
|
Chris@891
|
1114 if (!feature.hasTimestamp && m_fixedRateFeatureNos[n] >= 0) {
|
Chris@891
|
1115 model->setColumn(m_fixedRateFeatureNos[n], values);
|
Chris@889
|
1116 } else {
|
Chris@1039
|
1117 model->setColumn(int(frame / model->getResolution()), values);
|
Chris@889
|
1118 }
|
Chris@441
|
1119
|
Chris@441
|
1120 } else {
|
Chris@690
|
1121 SVDEBUG << "FeatureExtractionModelTransformer::addFeature: Unknown output model type!" << endl;
|
Chris@320
|
1122 }
|
Chris@320
|
1123 }
|
Chris@320
|
1124
|
Chris@320
|
1125 void
|
Chris@850
|
1126 FeatureExtractionModelTransformer::setCompletion(int n, int completion)
|
Chris@320
|
1127 {
|
Chris@690
|
1128 // SVDEBUG << "FeatureExtractionModelTransformer::setCompletion("
|
Chris@687
|
1129 // << completion << ")" << endl;
|
Chris@320
|
1130
|
Chris@849
|
1131 if (isOutput<SparseOneDimensionalModel>(n)) {
|
Chris@320
|
1132
|
Chris@350
|
1133 SparseOneDimensionalModel *model =
|
Chris@849
|
1134 getConformingOutput<SparseOneDimensionalModel>(n);
|
Chris@320
|
1135 if (!model) return;
|
Chris@923
|
1136 if (model->isAbandoning()) abandon();
|
Chris@441
|
1137 model->setCompletion(completion, true);
|
Chris@320
|
1138
|
Chris@849
|
1139 } else if (isOutput<SparseTimeValueModel>(n)) {
|
Chris@320
|
1140
|
Chris@350
|
1141 SparseTimeValueModel *model =
|
Chris@849
|
1142 getConformingOutput<SparseTimeValueModel>(n);
|
Chris@320
|
1143 if (!model) return;
|
Chris@923
|
1144 if (model->isAbandoning()) abandon();
|
Chris@441
|
1145 model->setCompletion(completion, true);
|
Chris@320
|
1146
|
Chris@849
|
1147 } else if (isOutput<NoteModel>(n)) {
|
Chris@320
|
1148
|
Chris@849
|
1149 NoteModel *model = getConformingOutput<NoteModel>(n);
|
Chris@320
|
1150 if (!model) return;
|
Chris@923
|
1151 if (model->isAbandoning()) abandon();
|
Chris@441
|
1152 model->setCompletion(completion, true);
|
gyorgyf@786
|
1153
|
Chris@923
|
1154 } else if (isOutput<FlexiNoteModel>(n)) {
|
gyorgyf@786
|
1155
|
Chris@849
|
1156 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>(n);
|
gyorgyf@786
|
1157 if (!model) return;
|
Chris@923
|
1158 if (model->isAbandoning()) abandon();
|
gyorgyf@786
|
1159 model->setCompletion(completion, true);
|
Chris@320
|
1160
|
Chris@849
|
1161 } else if (isOutput<RegionModel>(n)) {
|
Chris@441
|
1162
|
Chris@849
|
1163 RegionModel *model = getConformingOutput<RegionModel>(n);
|
Chris@441
|
1164 if (!model) return;
|
Chris@923
|
1165 if (model->isAbandoning()) abandon();
|
Chris@441
|
1166 model->setCompletion(completion, true);
|
Chris@441
|
1167
|
Chris@849
|
1168 } else if (isOutput<EditableDenseThreeDimensionalModel>(n)) {
|
Chris@320
|
1169
|
Chris@320
|
1170 EditableDenseThreeDimensionalModel *model =
|
Chris@849
|
1171 getConformingOutput<EditableDenseThreeDimensionalModel>(n);
|
Chris@320
|
1172 if (!model) return;
|
Chris@923
|
1173 if (model->isAbandoning()) abandon();
|
Chris@350
|
1174 model->setCompletion(completion, true); //!!!m_context.updates);
|
Chris@320
|
1175 }
|
Chris@320
|
1176 }
|
Chris@320
|
1177
|