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