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