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