annotate transform/FeatureExtractionModelTransformer.cpp @ 1233:fac1666e429b project-file-rework

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