annotate transform/FeatureExtractionModelTransformer.cpp @ 787:4faea021b5c1 tonioni

cleanup
author gyorgyf
date Sat, 13 Apr 2013 15:36:54 +0100
parents 5295bdb58840
children 54829c1e155e
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@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@350 40 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
gyorgyf@786 41 const Transform &transform,
gyorgyf@786 42 const PreferredOutputModel outputmodel) :
Chris@350 43 ModelTransformer(in, transform),
Chris@320 44 m_plugin(0),
Chris@320 45 m_descriptor(0),
gyorgyf@786 46 m_outputFeatureNo(0),
gyorgyf@786 47 m_preferredOutputModel(outputmodel)
Chris@320 48 {
Chris@690 49 // SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: plugin " << pluginId << ", outputName " << m_transform.getOutput() << endl;
Chris@350 50
Chris@350 51 QString pluginId = transform.getPluginIdentifier();
Chris@320 52
Chris@320 53 FeatureExtractionPluginFactory *factory =
Chris@320 54 FeatureExtractionPluginFactory::instanceFor(pluginId);
Chris@320 55
Chris@320 56 if (!factory) {
Chris@361 57 m_message = tr("No factory available for feature extraction plugin id \"%1\" (unknown plugin type, or internal error?)").arg(pluginId);
Chris@320 58 return;
Chris@320 59 }
Chris@320 60
Chris@350 61 DenseTimeValueModel *input = getConformingInput();
Chris@350 62 if (!input) {
Chris@361 63 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
Chris@350 64 return;
Chris@350 65 }
Chris@320 66
Chris@350 67 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
Chris@320 68 if (!m_plugin) {
Chris@361 69 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
Chris@320 70 return;
Chris@320 71 }
Chris@320 72
Chris@350 73 TransformFactory::getInstance()->makeContextConsistentWithPlugin
Chris@350 74 (m_transform, m_plugin);
Chris@343 75
Chris@350 76 TransformFactory::getInstance()->setPluginParameters
Chris@350 77 (m_transform, m_plugin);
Chris@320 78
Chris@320 79 size_t channelCount = input->getChannelCount();
Chris@320 80 if (m_plugin->getMaxChannelCount() < channelCount) {
Chris@320 81 channelCount = 1;
Chris@320 82 }
Chris@320 83 if (m_plugin->getMinChannelCount() > channelCount) {
Chris@361 84 m_message = tr("Cannot provide enough channels to feature extraction plugin \"%1\" (plugin min is %2, max %3; input model has %4)")
Chris@361 85 .arg(pluginId)
Chris@361 86 .arg(m_plugin->getMinChannelCount())
Chris@361 87 .arg(m_plugin->getMaxChannelCount())
Chris@361 88 .arg(input->getChannelCount());
Chris@320 89 return;
Chris@320 90 }
Chris@320 91
Chris@690 92 SVDEBUG << "Initialising feature extraction plugin with channels = "
Chris@350 93 << channelCount << ", step = " << m_transform.getStepSize()
Chris@687 94 << ", block = " << m_transform.getBlockSize() << endl;
Chris@320 95
Chris@320 96 if (!m_plugin->initialise(channelCount,
Chris@350 97 m_transform.getStepSize(),
Chris@350 98 m_transform.getBlockSize())) {
Chris@361 99
Chris@361 100 size_t pstep = m_transform.getStepSize();
Chris@361 101 size_t pblock = m_transform.getBlockSize();
Chris@361 102
Chris@361 103 m_transform.setStepSize(0);
Chris@361 104 m_transform.setBlockSize(0);
Chris@361 105 TransformFactory::getInstance()->makeContextConsistentWithPlugin
Chris@361 106 (m_transform, m_plugin);
Chris@361 107
Chris@361 108 if (m_transform.getStepSize() != pstep ||
Chris@361 109 m_transform.getBlockSize() != pblock) {
Chris@361 110
Chris@361 111 if (!m_plugin->initialise(channelCount,
Chris@361 112 m_transform.getStepSize(),
Chris@361 113 m_transform.getBlockSize())) {
Chris@361 114
Chris@361 115 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
Chris@361 116 return;
Chris@361 117
Chris@361 118 } else {
Chris@361 119
Chris@361 120 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 121 .arg(pluginId)
Chris@361 122 .arg(pstep)
Chris@361 123 .arg(pblock)
Chris@361 124 .arg(m_transform.getStepSize())
Chris@361 125 .arg(m_transform.getBlockSize());
Chris@361 126 }
Chris@361 127
Chris@361 128 } else {
Chris@361 129
Chris@361 130 m_message = tr("Failed to initialise feature extraction plugin \"%1\"").arg(pluginId);
Chris@361 131 return;
Chris@361 132 }
Chris@320 133 }
Chris@320 134
Chris@366 135 if (m_transform.getPluginVersion() != "") {
Chris@366 136 QString pv = QString("%1").arg(m_plugin->getPluginVersion());
Chris@366 137 if (pv != m_transform.getPluginVersion()) {
Chris@366 138 QString vm = tr("Transform was configured for version %1 of plugin \"%2\", but the plugin being used is version %3")
Chris@366 139 .arg(m_transform.getPluginVersion())
Chris@366 140 .arg(pluginId)
Chris@366 141 .arg(pv);
Chris@366 142 if (m_message != "") {
Chris@366 143 m_message = QString("%1; %2").arg(vm).arg(m_message);
Chris@366 144 } else {
Chris@366 145 m_message = vm;
Chris@366 146 }
Chris@366 147 }
Chris@366 148 }
Chris@366 149
Chris@320 150 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
Chris@320 151
Chris@320 152 if (outputs.empty()) {
Chris@361 153 m_message = tr("Plugin \"%1\" has no outputs").arg(pluginId);
Chris@320 154 return;
Chris@320 155 }
Chris@320 156
Chris@320 157 for (size_t i = 0; i < outputs.size(); ++i) {
Chris@690 158 // SVDEBUG << "comparing output " << i << " name \"" << outputs[i].identifier << "\" with expected \"" << m_transform.getOutput() << "\"" << endl;
Chris@350 159 if (m_transform.getOutput() == "" ||
Chris@350 160 outputs[i].identifier == m_transform.getOutput().toStdString()) {
Chris@320 161 m_outputFeatureNo = i;
Chris@441 162 m_descriptor = new Vamp::Plugin::OutputDescriptor(outputs[i]);
Chris@320 163 break;
Chris@320 164 }
Chris@320 165 }
Chris@320 166
Chris@320 167 if (!m_descriptor) {
Chris@361 168 m_message = tr("Plugin \"%1\" has no output named \"%2\"")
Chris@361 169 .arg(pluginId)
Chris@361 170 .arg(m_transform.getOutput());
Chris@320 171 return;
Chris@320 172 }
Chris@320 173
Chris@558 174 createOutputModel();
Chris@558 175 }
Chris@558 176
Chris@558 177 void
Chris@558 178 FeatureExtractionModelTransformer::createOutputModel()
Chris@558 179 {
Chris@558 180 DenseTimeValueModel *input = getConformingInput();
Chris@558 181
Chris@712 182 // std::cerr << "FeatureExtractionModelTransformer::createOutputModel: sample type " << m_descriptor->sampleType << ", rate " << m_descriptor->sampleRate << std::endl;
Chris@712 183
Chris@558 184 PluginRDFDescription description(m_transform.getPluginIdentifier());
Chris@558 185 QString outputId = m_transform.getOutput();
Chris@558 186
Chris@320 187 int binCount = 1;
Chris@320 188 float minValue = 0.0, maxValue = 0.0;
Chris@320 189 bool haveExtents = false;
Chris@320 190
Chris@320 191 if (m_descriptor->hasFixedBinCount) {
Chris@320 192 binCount = m_descriptor->binCount;
Chris@320 193 }
Chris@320 194
Chris@331 195 // std::cerr << "FeatureExtractionModelTransformer: output bin count "
Chris@320 196 // << binCount << std::endl;
Chris@320 197
Chris@320 198 if (binCount > 0 && m_descriptor->hasKnownExtents) {
Chris@320 199 minValue = m_descriptor->minValue;
Chris@320 200 maxValue = m_descriptor->maxValue;
Chris@320 201 haveExtents = true;
Chris@320 202 }
Chris@320 203
Chris@350 204 size_t modelRate = input->getSampleRate();
Chris@320 205 size_t modelResolution = 1;
Chris@712 206
Chris@320 207 switch (m_descriptor->sampleType) {
Chris@320 208
Chris@320 209 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
Chris@320 210 if (m_descriptor->sampleRate != 0.0) {
Chris@320 211 modelResolution = size_t(modelRate / m_descriptor->sampleRate + 0.001);
Chris@320 212 }
Chris@320 213 break;
Chris@320 214
Chris@320 215 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
Chris@350 216 modelResolution = m_transform.getStepSize();
Chris@320 217 break;
Chris@320 218
Chris@320 219 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
Chris@451 220 //!!! SV doesn't actually support display of models that have
Chris@451 221 //!!! different underlying rates together -- so we always set
Chris@451 222 //!!! the model rate to be the input model's rate, and adjust
Chris@451 223 //!!! the resolution appropriately. We can't properly display
Chris@451 224 //!!! data with a higher resolution than the base model at all
Chris@451 225 // modelRate = size_t(m_descriptor->sampleRate + 0.001);
Chris@451 226 if (m_descriptor->sampleRate > input->getSampleRate()) {
Chris@451 227 modelResolution = 1;
Chris@451 228 } else {
Chris@451 229 modelResolution = size_t(input->getSampleRate() /
Chris@451 230 m_descriptor->sampleRate);
Chris@451 231 }
Chris@320 232 break;
Chris@320 233 }
Chris@320 234
Chris@441 235 bool preDurationPlugin = (m_plugin->getVampApiVersion() < 2);
Chris@441 236
Chris@441 237 if (binCount == 0 &&
Chris@441 238 (preDurationPlugin || !m_descriptor->hasDuration)) {
Chris@320 239
Chris@445 240 // Anything with no value and no duration is an instant
Chris@445 241
Chris@320 242 m_output = new SparseOneDimensionalModel(modelRate, modelResolution,
Chris@320 243 false);
Chris@320 244
Chris@558 245 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
Chris@558 246 m_output->setRDFTypeURI(outputEventTypeURI);
Chris@558 247
Chris@441 248 } else if ((preDurationPlugin && binCount > 1 &&
Chris@441 249 (m_descriptor->sampleType ==
Chris@441 250 Vamp::Plugin::OutputDescriptor::VariableSampleRate)) ||
Chris@441 251 (!preDurationPlugin && m_descriptor->hasDuration)) {
Chris@441 252
Chris@441 253 // For plugins using the old v1 API without explicit duration,
Chris@441 254 // we treat anything that has multiple bins (i.e. that has the
Chris@441 255 // potential to have value and duration) and a variable sample
Chris@441 256 // rate as a note model, taking its values as pitch, duration
Chris@441 257 // and velocity (if present) respectively. This is the same
Chris@441 258 // behaviour as always applied by SV to these plugins in the
Chris@441 259 // past.
Chris@441 260
Chris@441 261 // For plugins with the newer API, we treat anything with
Chris@441 262 // duration as either a note model with pitch and velocity, or
Chris@441 263 // a region model.
Chris@441 264
Chris@441 265 // How do we know whether it's an interval or note model?
Chris@441 266 // What's the essential difference? Is a note model any
Chris@441 267 // interval model using a Hz or "MIDI pitch" scale? There
Chris@441 268 // isn't really a reliable test for "MIDI pitch"... Does a
Chris@441 269 // note model always have velocity? This is a good question
Chris@441 270 // to be addressed by accompanying RDF, but for the moment we
Chris@441 271 // will do the following...
Chris@441 272
Chris@441 273 bool isNoteModel = false;
Chris@441 274
Chris@441 275 // Regions have only value (and duration -- we can't extract a
Chris@441 276 // region model from an old-style plugin that doesn't support
Chris@441 277 // duration)
Chris@441 278 if (binCount > 1) isNoteModel = true;
Chris@441 279
Chris@595 280 // Regions do not have units of Hz or MIDI things (a sweeping
Chris@595 281 // assumption!)
Chris@595 282 if (m_descriptor->unit == "Hz" ||
Chris@595 283 m_descriptor->unit.find("MIDI") != std::string::npos ||
Chris@595 284 m_descriptor->unit.find("midi") != std::string::npos) {
Chris@595 285 isNoteModel = true;
Chris@595 286 }
Chris@441 287
Chris@441 288 // If we had a "sparse 3D model", we would have the additional
Chris@441 289 // problem of determining whether to use that here (if bin
Chris@441 290 // count > 1). But we don't.
Chris@441 291
gyorgyf@786 292 if (isNoteModel && m_preferredOutputModel == NoteOutputModel) {
Chris@441 293
Chris@441 294 NoteModel *model;
Chris@441 295 if (haveExtents) {
gyorgyf@786 296 model = new NoteModel (modelRate, modelResolution, minValue, maxValue, false);
Chris@441 297 } else {
gyorgyf@786 298 model = new NoteModel (modelRate, modelResolution, false);
gyorgyf@786 299 }
gyorgyf@786 300 model->setScaleUnits(m_descriptor->unit.c_str());
gyorgyf@786 301 m_output = model;
gyorgyf@786 302
gyorgyf@787 303 // GF: FlexiNoteModel is selected if the m_preferredOutputModel is set
gyorgyf@786 304 } else if (isNoteModel && m_preferredOutputModel == FlexiNoteOutputModel) {
gyorgyf@786 305
gyorgyf@786 306 FlexiNoteModel *model;
gyorgyf@786 307 if (haveExtents) {
gyorgyf@786 308 model = new FlexiNoteModel (modelRate, modelResolution, minValue, maxValue, false);
gyorgyf@786 309 } else {
gyorgyf@786 310 model = new FlexiNoteModel (modelRate, modelResolution, false);
Chris@441 311 }
Chris@441 312 model->setScaleUnits(m_descriptor->unit.c_str());
Chris@441 313 m_output = model;
Chris@441 314
Chris@441 315 } else {
Chris@441 316
Chris@441 317 RegionModel *model;
Chris@441 318 if (haveExtents) {
Chris@441 319 model = new RegionModel
Chris@441 320 (modelRate, modelResolution, minValue, maxValue, false);
Chris@441 321 } else {
Chris@441 322 model = new RegionModel
Chris@441 323 (modelRate, modelResolution, false);
Chris@441 324 }
Chris@441 325 model->setScaleUnits(m_descriptor->unit.c_str());
Chris@441 326 m_output = model;
Chris@441 327 }
Chris@441 328
Chris@558 329 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
Chris@558 330 m_output->setRDFTypeURI(outputEventTypeURI);
Chris@558 331
Chris@441 332 } else if (binCount == 1 ||
Chris@441 333 (m_descriptor->sampleType ==
Chris@441 334 Vamp::Plugin::OutputDescriptor::VariableSampleRate)) {
Chris@441 335
Chris@441 336 // Anything that is not a 1D, note, or interval model and that
Chris@441 337 // has only one value per result must be a sparse time value
Chris@441 338 // model.
Chris@441 339
Chris@441 340 // Anything that is not a 1D, note, or interval model and that
Chris@441 341 // has a variable sample rate is also treated as a sparse time
Chris@441 342 // value model regardless of its bin count, because we lack a
Chris@441 343 // sparse 3D model.
Chris@320 344
Chris@320 345 SparseTimeValueModel *model;
Chris@320 346 if (haveExtents) {
Chris@320 347 model = new SparseTimeValueModel
Chris@320 348 (modelRate, modelResolution, minValue, maxValue, false);
Chris@320 349 } else {
Chris@320 350 model = new SparseTimeValueModel
Chris@320 351 (modelRate, modelResolution, false);
Chris@320 352 }
Chris@558 353
Chris@558 354 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
Chris@320 355 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
Chris@320 356
Chris@320 357 m_output = model;
Chris@320 358
Chris@558 359 QString outputEventTypeURI = description.getOutputEventTypeURI(outputId);
Chris@558 360 m_output->setRDFTypeURI(outputEventTypeURI);
Chris@558 361
Chris@441 362 } else {
Chris@320 363
Chris@441 364 // Anything that is not a 1D, note, or interval model and that
Chris@441 365 // has a fixed sample rate and more than one value per result
Chris@441 366 // must be a dense 3D model.
Chris@320 367
Chris@320 368 EditableDenseThreeDimensionalModel *model =
Chris@320 369 new EditableDenseThreeDimensionalModel
Chris@535 370 (modelRate, modelResolution, binCount,
Chris@535 371 EditableDenseThreeDimensionalModel::BasicMultirateCompression,
Chris@535 372 false);
Chris@320 373
Chris@320 374 if (!m_descriptor->binNames.empty()) {
Chris@320 375 std::vector<QString> names;
Chris@320 376 for (size_t i = 0; i < m_descriptor->binNames.size(); ++i) {
Chris@320 377 names.push_back(m_descriptor->binNames[i].c_str());
Chris@320 378 }
Chris@320 379 model->setBinNames(names);
Chris@320 380 }
Chris@320 381
Chris@320 382 m_output = model;
Chris@558 383
Chris@558 384 QString outputSignalTypeURI = description.getOutputSignalTypeURI(outputId);
Chris@558 385 m_output->setRDFTypeURI(outputSignalTypeURI);
Chris@320 386 }
Chris@333 387
Chris@350 388 if (m_output) m_output->setSourceModel(input);
Chris@320 389 }
Chris@320 390
Chris@331 391 FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()
Chris@320 392 {
Chris@690 393 // SVDEBUG << "FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()" << endl;
Chris@320 394 delete m_plugin;
Chris@320 395 delete m_descriptor;
Chris@320 396 }
Chris@320 397
Chris@320 398 DenseTimeValueModel *
Chris@350 399 FeatureExtractionModelTransformer::getConformingInput()
Chris@320 400 {
Chris@690 401 // SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: input model is " << getInputModel() << endl;
Chris@408 402
Chris@320 403 DenseTimeValueModel *dtvm =
Chris@320 404 dynamic_cast<DenseTimeValueModel *>(getInputModel());
Chris@320 405 if (!dtvm) {
Chris@690 406 SVDEBUG << "FeatureExtractionModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << endl;
Chris@320 407 }
Chris@320 408 return dtvm;
Chris@320 409 }
Chris@320 410
Chris@320 411 void
Chris@331 412 FeatureExtractionModelTransformer::run()
Chris@320 413 {
Chris@350 414 DenseTimeValueModel *input = getConformingInput();
Chris@320 415 if (!input) return;
Chris@320 416
Chris@320 417 if (!m_output) return;
Chris@320 418
Chris@497 419 while (!input->isReady() && !m_abandoned) {
Chris@690 420 SVDEBUG << "FeatureExtractionModelTransformer::run: Waiting for input model to be ready..." << endl;
Chris@497 421 usleep(500000);
Chris@320 422 }
Chris@497 423 if (m_abandoned) return;
Chris@320 424
Chris@350 425 size_t sampleRate = input->getSampleRate();
Chris@320 426
Chris@320 427 size_t channelCount = input->getChannelCount();
Chris@320 428 if (m_plugin->getMaxChannelCount() < channelCount) {
Chris@320 429 channelCount = 1;
Chris@320 430 }
Chris@320 431
Chris@320 432 float **buffers = new float*[channelCount];
Chris@320 433 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@350 434 buffers[ch] = new float[m_transform.getBlockSize() + 2];
Chris@320 435 }
Chris@320 436
Chris@350 437 size_t stepSize = m_transform.getStepSize();
Chris@350 438 size_t blockSize = m_transform.getBlockSize();
Chris@350 439
Chris@320 440 bool frequencyDomain = (m_plugin->getInputDomain() ==
Chris@320 441 Vamp::Plugin::FrequencyDomain);
Chris@320 442 std::vector<FFTModel *> fftModels;
Chris@320 443
Chris@320 444 if (frequencyDomain) {
Chris@320 445 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@320 446 FFTModel *model = new FFTModel
Chris@350 447 (getConformingInput(),
Chris@350 448 channelCount == 1 ? m_input.getChannel() : ch,
Chris@350 449 m_transform.getWindowType(),
Chris@350 450 blockSize,
Chris@350 451 stepSize,
Chris@350 452 blockSize,
Chris@334 453 false,
Chris@334 454 StorageAdviser::PrecisionCritical);
Chris@320 455 if (!model->isOK()) {
Chris@320 456 delete model;
Chris@320 457 setCompletion(100);
Chris@387 458 //!!! need a better way to handle this -- previously we were using a QMessageBox but that isn't an appropriate thing to do here either
Chris@387 459 throw AllocationFailed("Failed to create the FFT model for this feature extraction model transformer");
Chris@320 460 }
Chris@320 461 model->resume();
Chris@320 462 fftModels.push_back(model);
Chris@320 463 }
Chris@320 464 }
Chris@320 465
Chris@350 466 long startFrame = m_input.getModel()->getStartFrame();
Chris@350 467 long endFrame = m_input.getModel()->getEndFrame();
Chris@320 468
Chris@350 469 RealTime contextStartRT = m_transform.getStartTime();
Chris@350 470 RealTime contextDurationRT = m_transform.getDuration();
Chris@350 471
Chris@350 472 long contextStart =
Chris@350 473 RealTime::realTime2Frame(contextStartRT, sampleRate);
Chris@350 474
Chris@350 475 long contextDuration =
Chris@350 476 RealTime::realTime2Frame(contextDurationRT, sampleRate);
Chris@320 477
Chris@320 478 if (contextStart == 0 || contextStart < startFrame) {
Chris@320 479 contextStart = startFrame;
Chris@320 480 }
Chris@320 481
Chris@320 482 if (contextDuration == 0) {
Chris@320 483 contextDuration = endFrame - contextStart;
Chris@320 484 }
Chris@320 485 if (contextStart + contextDuration > endFrame) {
Chris@320 486 contextDuration = endFrame - contextStart;
Chris@320 487 }
Chris@320 488
Chris@320 489 long blockFrame = contextStart;
Chris@320 490
Chris@320 491 long prevCompletion = 0;
Chris@320 492
Chris@320 493 setCompletion(0);
Chris@320 494
Chris@556 495 float *reals = 0;
Chris@556 496 float *imaginaries = 0;
Chris@556 497 if (frequencyDomain) {
Chris@556 498 reals = new float[blockSize/2 + 1];
Chris@556 499 imaginaries = new float[blockSize/2 + 1];
Chris@556 500 }
Chris@556 501
Chris@678 502 QString error = "";
Chris@678 503
Chris@320 504 while (!m_abandoned) {
Chris@320 505
Chris@320 506 if (frequencyDomain) {
Chris@350 507 if (blockFrame - int(blockSize)/2 >
Chris@320 508 contextStart + contextDuration) break;
Chris@320 509 } else {
Chris@320 510 if (blockFrame >=
Chris@320 511 contextStart + contextDuration) break;
Chris@320 512 }
Chris@320 513
Chris@690 514 // SVDEBUG << "FeatureExtractionModelTransformer::run: blockFrame "
Chris@320 515 // << blockFrame << ", endFrame " << endFrame << ", blockSize "
Chris@687 516 // << blockSize << endl;
Chris@320 517
Chris@320 518 long completion =
Chris@350 519 (((blockFrame - contextStart) / stepSize) * 99) /
Chris@557 520 (contextDuration / stepSize + 1);
Chris@320 521
Chris@350 522 // channelCount is either m_input.getModel()->channelCount or 1
Chris@320 523
Chris@363 524 if (frequencyDomain) {
Chris@363 525 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@350 526 int column = (blockFrame - startFrame) / stepSize;
Chris@556 527 fftModels[ch]->getValuesAt(column, reals, imaginaries);
Chris@350 528 for (size_t i = 0; i <= blockSize/2; ++i) {
Chris@556 529 buffers[ch][i*2] = reals[i];
Chris@556 530 buffers[ch][i*2+1] = imaginaries[i];
Chris@320 531 }
Chris@678 532 error = fftModels[ch]->getError();
Chris@678 533 if (error != "") {
Chris@686 534 std::cerr << "FeatureExtractionModelTransformer::run: Abandoning, error is " << error << std::endl;
Chris@678 535 m_abandoned = true;
Chris@678 536 m_message = error;
Chris@678 537 }
Chris@363 538 }
Chris@363 539 } else {
Chris@363 540 getFrames(channelCount, blockFrame, blockSize, buffers);
Chris@320 541 }
Chris@320 542
Chris@497 543 if (m_abandoned) break;
Chris@497 544
Chris@320 545 Vamp::Plugin::FeatureSet features = m_plugin->process
Chris@320 546 (buffers, Vamp::RealTime::frame2RealTime(blockFrame, sampleRate));
Chris@320 547
Chris@497 548 if (m_abandoned) break;
Chris@497 549
Chris@320 550 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
Chris@320 551 Vamp::Plugin::Feature feature =
Chris@320 552 features[m_outputFeatureNo][fi];
Chris@320 553 addFeature(blockFrame, feature);
Chris@320 554 }
Chris@320 555
Chris@320 556 if (blockFrame == contextStart || completion > prevCompletion) {
Chris@320 557 setCompletion(completion);
Chris@320 558 prevCompletion = completion;
Chris@320 559 }
Chris@320 560
Chris@350 561 blockFrame += stepSize;
Chris@320 562 }
Chris@320 563
Chris@497 564 if (!m_abandoned) {
Chris@497 565 Vamp::Plugin::FeatureSet features = m_plugin->getRemainingFeatures();
Chris@320 566
Chris@497 567 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
Chris@497 568 Vamp::Plugin::Feature feature =
Chris@497 569 features[m_outputFeatureNo][fi];
Chris@497 570 addFeature(blockFrame, feature);
Chris@497 571 }
Chris@497 572 }
Chris@320 573
Chris@497 574 setCompletion(100);
Chris@320 575
Chris@320 576 if (frequencyDomain) {
Chris@320 577 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@320 578 delete fftModels[ch];
Chris@320 579 }
Chris@556 580 delete[] reals;
Chris@556 581 delete[] imaginaries;
Chris@320 582 }
Chris@320 583 }
Chris@320 584
Chris@320 585 void
Chris@363 586 FeatureExtractionModelTransformer::getFrames(int channelCount,
Chris@363 587 long startFrame, long size,
Chris@363 588 float **buffers)
Chris@320 589 {
Chris@320 590 long offset = 0;
Chris@320 591
Chris@320 592 if (startFrame < 0) {
Chris@363 593 for (int c = 0; c < channelCount; ++c) {
Chris@363 594 for (int i = 0; i < size && startFrame + i < 0; ++i) {
Chris@363 595 buffers[c][i] = 0.0f;
Chris@363 596 }
Chris@320 597 }
Chris@320 598 offset = -startFrame;
Chris@320 599 size -= offset;
Chris@320 600 if (size <= 0) return;
Chris@320 601 startFrame = 0;
Chris@320 602 }
Chris@320 603
Chris@350 604 DenseTimeValueModel *input = getConformingInput();
Chris@350 605 if (!input) return;
Chris@363 606
Chris@363 607 long got = 0;
Chris@350 608
Chris@363 609 if (channelCount == 1) {
Chris@363 610
Chris@363 611 got = input->getData(m_input.getChannel(), startFrame, size,
Chris@363 612 buffers[0] + offset);
Chris@363 613
Chris@363 614 if (m_input.getChannel() == -1 && input->getChannelCount() > 1) {
Chris@363 615 // use mean instead of sum, as plugin input
Chris@363 616 float cc = float(input->getChannelCount());
Chris@363 617 for (long i = 0; i < size; ++i) {
Chris@363 618 buffers[0][i + offset] /= cc;
Chris@363 619 }
Chris@363 620 }
Chris@363 621
Chris@363 622 } else {
Chris@363 623
Chris@363 624 float **writebuf = buffers;
Chris@363 625 if (offset > 0) {
Chris@363 626 writebuf = new float *[channelCount];
Chris@363 627 for (int i = 0; i < channelCount; ++i) {
Chris@363 628 writebuf[i] = buffers[i] + offset;
Chris@363 629 }
Chris@363 630 }
Chris@363 631
Chris@363 632 got = input->getData(0, channelCount-1, startFrame, size, writebuf);
Chris@363 633
Chris@363 634 if (writebuf != buffers) delete[] writebuf;
Chris@363 635 }
Chris@320 636
Chris@320 637 while (got < size) {
Chris@363 638 for (int c = 0; c < channelCount; ++c) {
Chris@363 639 buffers[c][got + offset] = 0.0;
Chris@363 640 }
Chris@320 641 ++got;
Chris@320 642 }
Chris@320 643 }
Chris@320 644
Chris@320 645 void
Chris@331 646 FeatureExtractionModelTransformer::addFeature(size_t blockFrame,
Chris@320 647 const Vamp::Plugin::Feature &feature)
Chris@320 648 {
Chris@350 649 size_t inputRate = m_input.getModel()->getSampleRate();
Chris@320 650
Chris@712 651 // std::cerr << "FeatureExtractionModelTransformer::addFeature: blockFrame = "
Chris@712 652 // << blockFrame << ", hasTimestamp = " << feature.hasTimestamp
Chris@712 653 // << ", timestamp = " << feature.timestamp << ", hasDuration = "
Chris@712 654 // << feature.hasDuration << ", duration = " << feature.duration
Chris@712 655 // << std::endl;
Chris@320 656
Chris@320 657 int binCount = 1;
Chris@320 658 if (m_descriptor->hasFixedBinCount) {
Chris@320 659 binCount = m_descriptor->binCount;
Chris@320 660 }
Chris@320 661
Chris@320 662 size_t frame = blockFrame;
Chris@320 663
Chris@320 664 if (m_descriptor->sampleType ==
Chris@320 665 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
Chris@320 666
Chris@320 667 if (!feature.hasTimestamp) {
Chris@320 668 std::cerr
Chris@331 669 << "WARNING: FeatureExtractionModelTransformer::addFeature: "
Chris@320 670 << "Feature has variable sample rate but no timestamp!"
Chris@320 671 << std::endl;
Chris@320 672 return;
Chris@320 673 } else {
Chris@320 674 frame = Vamp::RealTime::realTime2Frame(feature.timestamp, inputRate);
Chris@320 675 }
Chris@320 676
Chris@320 677 } else if (m_descriptor->sampleType ==
Chris@320 678 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
Chris@320 679
Chris@320 680 if (feature.hasTimestamp) {
Chris@320 681 //!!! warning: sampleRate may be non-integral
Chris@320 682 frame = Vamp::RealTime::realTime2Frame(feature.timestamp,
Chris@451 683 //!!! see comment above when setting up modelResolution and modelRate
Chris@451 684 // lrintf(m_descriptor->sampleRate));
Chris@451 685 inputRate);
Chris@320 686 } else {
Chris@320 687 frame = m_output->getEndFrame();
Chris@320 688 }
Chris@320 689 }
Chris@320 690
Chris@441 691 // Rather than repeat the complicated tests from the constructor
Chris@441 692 // to determine what sort of model we must be adding the features
Chris@441 693 // to, we instead test what sort of model the constructor decided
Chris@441 694 // to create.
Chris@320 695
Chris@441 696 if (isOutput<SparseOneDimensionalModel>()) {
Chris@441 697
Chris@441 698 SparseOneDimensionalModel *model =
Chris@350 699 getConformingOutput<SparseOneDimensionalModel>();
Chris@320 700 if (!model) return;
Chris@350 701
Chris@441 702 model->addPoint(SparseOneDimensionalModel::Point
Chris@441 703 (frame, feature.label.c_str()));
Chris@320 704
Chris@441 705 } else if (isOutput<SparseTimeValueModel>()) {
Chris@320 706
Chris@350 707 SparseTimeValueModel *model =
Chris@350 708 getConformingOutput<SparseTimeValueModel>();
Chris@320 709 if (!model) return;
Chris@350 710
Chris@454 711 for (int i = 0; i < feature.values.size(); ++i) {
Chris@454 712
Chris@454 713 float value = feature.values[i];
Chris@454 714
Chris@454 715 QString label = feature.label.c_str();
Chris@454 716 if (feature.values.size() > 1) {
Chris@454 717 label = QString("[%1] %2").arg(i+1).arg(label);
Chris@454 718 }
Chris@454 719
Chris@454 720 model->addPoint(SparseTimeValueModel::Point(frame, value, label));
Chris@454 721 }
Chris@320 722
gyorgyf@786 723 } else if (isOutput<FlexiNoteModel>() || isOutput<NoteModel>() || isOutput<RegionModel>()) { //GF: Added Note Model
Chris@320 724
Chris@441 725 int index = 0;
Chris@441 726
Chris@441 727 float value = 0.0;
Chris@441 728 if (feature.values.size() > index) {
Chris@441 729 value = feature.values[index++];
Chris@441 730 }
Chris@320 731
Chris@320 732 float duration = 1;
Chris@441 733 if (feature.hasDuration) {
Chris@441 734 duration = Vamp::RealTime::realTime2Frame(feature.duration, inputRate);
Chris@441 735 } else {
Chris@441 736 if (feature.values.size() > index) {
Chris@441 737 duration = feature.values[index++];
Chris@441 738 }
Chris@441 739 }
gyorgyf@786 740
gyorgyf@786 741 if (isOutput<FlexiNoteModel>()) { // GF: added for flexi note model
gyorgyf@786 742
gyorgyf@786 743 float velocity = 100;
gyorgyf@786 744 if (feature.values.size() > index) {
gyorgyf@786 745 velocity = feature.values[index++];
gyorgyf@786 746 }
gyorgyf@786 747 if (velocity < 0) velocity = 127;
gyorgyf@786 748 if (velocity > 127) velocity = 127;
gyorgyf@786 749
gyorgyf@786 750 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>();
gyorgyf@786 751 if (!model) return;
gyorgyf@786 752 model->addPoint(FlexiNoteModel::Point(frame, value, // value is pitch
gyorgyf@786 753 lrintf(duration),
gyorgyf@786 754 velocity / 127.f,
gyorgyf@786 755 feature.label.c_str()));
gyorgyf@786 756 // GF: end -- added for flexi note model
gyorgyf@786 757 } else if (isOutput<NoteModel>()) {
Chris@320 758
Chris@441 759 float velocity = 100;
Chris@441 760 if (feature.values.size() > index) {
Chris@441 761 velocity = feature.values[index++];
Chris@441 762 }
Chris@441 763 if (velocity < 0) velocity = 127;
Chris@441 764 if (velocity > 127) velocity = 127;
Chris@320 765
Chris@441 766 NoteModel *model = getConformingOutput<NoteModel>();
Chris@441 767 if (!model) return;
Chris@441 768 model->addPoint(NoteModel::Point(frame, value, // value is pitch
Chris@441 769 lrintf(duration),
Chris@441 770 velocity / 127.f,
Chris@441 771 feature.label.c_str()));
Chris@441 772 } else {
gyorgyf@786 773
Chris@441 774 RegionModel *model = getConformingOutput<RegionModel>();
Chris@454 775 if (!model) return;
Chris@454 776
Chris@474 777 if (feature.hasDuration && !feature.values.empty()) {
Chris@454 778
Chris@454 779 for (int i = 0; i < feature.values.size(); ++i) {
Chris@454 780
Chris@454 781 float value = feature.values[i];
Chris@454 782
Chris@454 783 QString label = feature.label.c_str();
Chris@454 784 if (feature.values.size() > 1) {
Chris@454 785 label = QString("[%1] %2").arg(i+1).arg(label);
Chris@454 786 }
Chris@454 787
Chris@454 788 model->addPoint(RegionModel::Point(frame, value,
Chris@454 789 lrintf(duration),
Chris@454 790 label));
Chris@454 791 }
Chris@454 792 } else {
Chris@454 793
Chris@441 794 model->addPoint(RegionModel::Point(frame, value,
Chris@441 795 lrintf(duration),
Chris@441 796 feature.label.c_str()));
Chris@454 797 }
Chris@441 798 }
Chris@320 799
Chris@441 800 } else if (isOutput<EditableDenseThreeDimensionalModel>()) {
Chris@320 801
Chris@533 802 DenseThreeDimensionalModel::Column values =
Chris@533 803 DenseThreeDimensionalModel::Column::fromStdVector(feature.values);
Chris@320 804
Chris@320 805 EditableDenseThreeDimensionalModel *model =
Chris@350 806 getConformingOutput<EditableDenseThreeDimensionalModel>();
Chris@320 807 if (!model) return;
Chris@320 808
Chris@320 809 model->setColumn(frame / model->getResolution(), values);
Chris@441 810
Chris@441 811 } else {
Chris@690 812 SVDEBUG << "FeatureExtractionModelTransformer::addFeature: Unknown output model type!" << endl;
Chris@320 813 }
Chris@320 814 }
Chris@320 815
Chris@320 816 void
Chris@331 817 FeatureExtractionModelTransformer::setCompletion(int completion)
Chris@320 818 {
Chris@320 819 int binCount = 1;
Chris@320 820 if (m_descriptor->hasFixedBinCount) {
Chris@320 821 binCount = m_descriptor->binCount;
Chris@320 822 }
Chris@320 823
Chris@690 824 // SVDEBUG << "FeatureExtractionModelTransformer::setCompletion("
Chris@687 825 // << completion << ")" << endl;
Chris@320 826
Chris@441 827 if (isOutput<SparseOneDimensionalModel>()) {
Chris@320 828
Chris@350 829 SparseOneDimensionalModel *model =
Chris@350 830 getConformingOutput<SparseOneDimensionalModel>();
Chris@320 831 if (!model) return;
Chris@441 832 model->setCompletion(completion, true);
Chris@320 833
Chris@441 834 } else if (isOutput<SparseTimeValueModel>()) {
Chris@320 835
Chris@350 836 SparseTimeValueModel *model =
Chris@350 837 getConformingOutput<SparseTimeValueModel>();
Chris@320 838 if (!model) return;
Chris@441 839 model->setCompletion(completion, true);
Chris@320 840
Chris@441 841 } else if (isOutput<NoteModel>()) {
Chris@320 842
Chris@441 843 NoteModel *model = getConformingOutput<NoteModel>();
Chris@320 844 if (!model) return;
Chris@441 845 model->setCompletion(completion, true);
gyorgyf@786 846
gyorgyf@786 847 } else if (isOutput<FlexiNoteModel>()) {
gyorgyf@786 848
gyorgyf@786 849 FlexiNoteModel *model = getConformingOutput<FlexiNoteModel>();
gyorgyf@786 850 if (!model) return;
gyorgyf@786 851 model->setCompletion(completion, true);
Chris@320 852
Chris@441 853 } else if (isOutput<RegionModel>()) {
Chris@441 854
Chris@441 855 RegionModel *model = getConformingOutput<RegionModel>();
Chris@441 856 if (!model) return;
Chris@441 857 model->setCompletion(completion, true);
Chris@441 858
Chris@441 859 } else if (isOutput<EditableDenseThreeDimensionalModel>()) {
Chris@320 860
Chris@320 861 EditableDenseThreeDimensionalModel *model =
Chris@350 862 getConformingOutput<EditableDenseThreeDimensionalModel>();
Chris@320 863 if (!model) return;
Chris@350 864 model->setCompletion(completion, true); //!!!m_context.updates);
Chris@320 865 }
Chris@320 866 }
Chris@320 867