annotate runner/FeatureExtractionManager.cpp @ 1:92911f967a16

* some reorganisation
author Chris Cannam
date Thu, 11 Dec 2008 10:26:12 +0000
parents FeatureExtractionManager.cpp@581b1b150a4d
children 03a02c1f0a9f
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Annotator
Chris@0 5 A utility for batch feature extraction from audio files.
Chris@0 6 Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Chris@0 7 Copyright 2007-2008 QMUL.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "FeatureExtractionManager.h"
Chris@0 17
Chris@0 18 #include <vamp-hostsdk/PluginChannelAdapter.h>
Chris@0 19 #include <vamp-hostsdk/PluginBufferingAdapter.h>
Chris@0 20 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
Chris@0 21 #include <vamp-hostsdk/PluginSummarisingAdapter.h>
Chris@0 22 #include <vamp-hostsdk/PluginLoader.h>
Chris@0 23
Chris@0 24 #include <iostream>
Chris@0 25
Chris@0 26 using namespace std;
Chris@0 27
Chris@0 28 using Vamp::Plugin;
Chris@0 29 using Vamp::PluginBase;
Chris@0 30 using Vamp::HostExt::PluginLoader;
Chris@0 31 using Vamp::HostExt::PluginChannelAdapter;
Chris@0 32 using Vamp::HostExt::PluginBufferingAdapter;
Chris@0 33 using Vamp::HostExt::PluginInputDomainAdapter;
Chris@0 34 using Vamp::HostExt::PluginSummarisingAdapter;
Chris@0 35
Chris@0 36 #include "data/fileio/FileSource.h"
Chris@0 37 #include "data/fileio/AudioFileReader.h"
Chris@0 38 #include "data/fileio/AudioFileReaderFactory.h"
Chris@0 39 #include "data/fileio/PlaylistFileReader.h"
Chris@0 40 #include "base/TempDirectory.h"
Chris@0 41 #include "base/ProgressPrinter.h"
Chris@0 42 #include "transform/TransformFactory.h"
Chris@0 43 #include "rdf/RDFTransformFactory.h"
Chris@0 44 #include "transform/FeatureWriter.h"
Chris@0 45
Chris@0 46 #include <QTextStream>
Chris@0 47 #include <QFile>
Chris@0 48 #include <QFileInfo>
Chris@0 49
Chris@0 50 FeatureExtractionManager::FeatureExtractionManager() :
Chris@0 51 m_summariesOnly(false),
Chris@0 52 // We can read using an arbitrary fixed block size --
Chris@0 53 // PluginBufferingAdapter handles this for us. It's likely to be
Chris@0 54 // quicker to use larger sizes than smallish ones like 1024
Chris@0 55 m_blockSize(16384),
Chris@0 56 m_defaultSampleRate(0),
Chris@0 57 m_sampleRate(0),
Chris@0 58 m_channels(1)
Chris@0 59 {
Chris@0 60 }
Chris@0 61
Chris@0 62 FeatureExtractionManager::~FeatureExtractionManager()
Chris@0 63 {
Chris@0 64 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 65 pi != m_plugins.end(); ++pi) {
Chris@0 66 delete pi->first;
Chris@0 67 }
Chris@0 68 }
Chris@0 69
Chris@0 70 void FeatureExtractionManager::setChannels(int channels)
Chris@0 71 {
Chris@0 72 m_channels = channels;
Chris@0 73 }
Chris@0 74
Chris@0 75 void FeatureExtractionManager::setDefaultSampleRate(int sampleRate)
Chris@0 76 {
Chris@0 77 m_defaultSampleRate = sampleRate;
Chris@0 78 }
Chris@0 79
Chris@0 80 static PluginSummarisingAdapter::SummaryType
Chris@0 81 getSummaryType(string name)
Chris@0 82 {
Chris@0 83 if (name == "min") return PluginSummarisingAdapter::Minimum;
Chris@0 84 if (name == "max") return PluginSummarisingAdapter::Maximum;
Chris@0 85 if (name == "mean") return PluginSummarisingAdapter::Mean;
Chris@0 86 if (name == "median") return PluginSummarisingAdapter::Median;
Chris@0 87 if (name == "mode") return PluginSummarisingAdapter::Mode;
Chris@0 88 if (name == "sum") return PluginSummarisingAdapter::Sum;
Chris@0 89 if (name == "variance") return PluginSummarisingAdapter::Variance;
Chris@0 90 if (name == "sd") return PluginSummarisingAdapter::StandardDeviation;
Chris@0 91 if (name == "count") return PluginSummarisingAdapter::Count;
Chris@0 92 return PluginSummarisingAdapter::UnknownSummaryType;
Chris@0 93 }
Chris@0 94
Chris@0 95 bool FeatureExtractionManager::setSummaryTypes(const set<string> &names,
Chris@0 96 bool summariesOnly,
Chris@0 97 const PluginSummarisingAdapter::SegmentBoundaries &boundaries)
Chris@0 98 {
Chris@0 99 for (SummaryNameSet::const_iterator i = names.begin();
Chris@0 100 i != names.end(); ++i) {
Chris@0 101 if (getSummaryType(*i) == PluginSummarisingAdapter::UnknownSummaryType) {
Chris@0 102 cerr << "ERROR: Unknown summary type \"" << *i << "\"" << endl;
Chris@0 103 return false;
Chris@0 104 }
Chris@0 105 }
Chris@0 106 m_summaries = names;
Chris@0 107 m_summariesOnly = summariesOnly;
Chris@0 108 m_boundaries = boundaries;
Chris@0 109 return true;
Chris@0 110 }
Chris@0 111
Chris@0 112 bool FeatureExtractionManager::addFeatureExtractor
Chris@0 113 (Transform transform, const vector<FeatureWriter*> &writers)
Chris@0 114 {
Chris@0 115 //!!! exceptions rather than return values?
Chris@0 116
Chris@0 117 if (transform.getSampleRate() == 0) {
Chris@0 118 if (m_sampleRate == 0) {
Chris@0 119 cerr << "NOTE: Transform does not specify a sample rate, using default rate of " << m_defaultSampleRate << endl;
Chris@0 120 transform.setSampleRate(m_defaultSampleRate);
Chris@0 121 m_sampleRate = m_defaultSampleRate;
Chris@0 122 } else {
Chris@0 123 cerr << "NOTE: Transform does not specify a sample rate, using previous transform's rate of " << m_sampleRate << endl;
Chris@0 124 transform.setSampleRate(m_sampleRate);
Chris@0 125 }
Chris@0 126 }
Chris@0 127
Chris@0 128 if (m_sampleRate == 0) {
Chris@0 129 m_sampleRate = transform.getSampleRate();
Chris@0 130 }
Chris@0 131
Chris@0 132 if (transform.getSampleRate() != m_sampleRate) {
Chris@0 133 cerr << "WARNING: Transform sample rate " << transform.getSampleRate() << " does not match previously specified transform rate of " << m_sampleRate << " -- only a single rate is supported for each run" << endl;
Chris@0 134 cerr << "WARNING: Using previous rate of " << m_sampleRate << " for this transform as well" << endl;
Chris@0 135 transform.setSampleRate(m_sampleRate);
Chris@0 136 }
Chris@0 137
Chris@0 138 Plugin *plugin = 0;
Chris@0 139
Chris@0 140 // Remember what the original transform looked like, and index
Chris@0 141 // based on this -- because we may be about to fill in the zeros
Chris@0 142 // for step and block size, but we want any further copies with
Chris@0 143 // the same zeros to match this one
Chris@0 144 Transform originalTransform = transform;
Chris@0 145
Chris@0 146 if (m_transformPluginMap.find(transform) == m_transformPluginMap.end()) {
Chris@0 147
Chris@0 148 // Test whether we already have a transform that is identical
Chris@0 149 // to this, except for the output requested and/or the summary
Chris@0 150 // type -- if so, they should share plugin instances (a vital
Chris@0 151 // optimisation)
Chris@0 152
Chris@0 153 for (TransformPluginMap::iterator i = m_transformPluginMap.begin();
Chris@0 154 i != m_transformPluginMap.end(); ++i) {
Chris@0 155 Transform test = i->first;
Chris@0 156 test.setOutput(transform.getOutput());
Chris@0 157 test.setSummaryType(transform.getSummaryType());
Chris@0 158 if (transform == test) {
Chris@0 159 cerr << "NOTE: Already have transform identical to this one (for \""
Chris@0 160 << transform.getIdentifier().toStdString()
Chris@0 161 << "\") in every detail except output identifier and/or "
Chris@0 162 << "summary type; sharing its plugin instance" << endl;
Chris@0 163 plugin = i->second;
Chris@0 164 if (transform.getSummaryType() != Transform::NoSummary &&
Chris@0 165 !dynamic_cast<PluginSummarisingAdapter *>(plugin)) {
Chris@0 166 plugin = new PluginSummarisingAdapter(plugin);
Chris@0 167 i->second = plugin;
Chris@0 168 }
Chris@0 169 break;
Chris@0 170 }
Chris@0 171 }
Chris@0 172
Chris@0 173 if (!plugin) {
Chris@0 174
Chris@0 175 TransformFactory *tf = TransformFactory::getInstance();
Chris@0 176
Chris@0 177 PluginBase *pb = tf->instantiatePluginFor(transform);
Chris@0 178 plugin = tf->downcastVampPlugin(pb);
Chris@0 179 if (!plugin) {
Chris@0 180 //!!! todo: handle non-Vamp plugins too, or make the main --list
Chris@0 181 // option print out only Vamp transforms
Chris@0 182 cerr << "ERROR: Failed to load plugin for transform \""
Chris@0 183 << transform.getIdentifier().toStdString() << "\"" << endl;
Chris@0 184 delete pb;
Chris@0 185 return false;
Chris@0 186 }
Chris@0 187
Chris@0 188 // We will provide the plugin with arbitrary step and
Chris@0 189 // block sizes (so that we can use the same read/write
Chris@0 190 // block size for all transforms), and to that end we use
Chris@0 191 // a PluginBufferingAdapter. However, we need to know the
Chris@0 192 // underlying step size so that we can provide the right
Chris@0 193 // context for dense outputs. (Although, don't forget
Chris@0 194 // that the PluginBufferingAdapter rewrites
Chris@0 195 // OneSamplePerStep outputs so as to use FixedSampleRate
Chris@0 196 // -- so it supplies the sample rate in the output
Chris@0 197 // feature. I'm not sure whether we can easily use that.)
Chris@0 198
Chris@0 199 size_t pluginStepSize = plugin->getPreferredStepSize();
Chris@0 200 size_t pluginBlockSize = plugin->getPreferredBlockSize();
Chris@0 201
Chris@0 202 // adapt the plugin for buffering, channels, etc.
Chris@0 203 if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
Chris@0 204 plugin = new PluginInputDomainAdapter(plugin);
Chris@0 205 }
Chris@0 206
Chris@0 207 PluginBufferingAdapter *pba = new PluginBufferingAdapter(plugin);
Chris@0 208 plugin = pba;
Chris@0 209
Chris@0 210 if (transform.getStepSize() != 0) {
Chris@0 211 pba->setPluginStepSize(transform.getStepSize());
Chris@0 212 } else {
Chris@0 213 transform.setStepSize(pluginStepSize);
Chris@0 214 }
Chris@0 215
Chris@0 216 if (transform.getBlockSize() != 0) {
Chris@0 217 pba->setPluginBlockSize(transform.getBlockSize());
Chris@0 218 } else {
Chris@0 219 transform.setBlockSize(pluginBlockSize);
Chris@0 220 }
Chris@0 221
Chris@0 222 plugin = new PluginChannelAdapter(plugin);
Chris@0 223
Chris@0 224 if (!m_summaries.empty() ||
Chris@0 225 transform.getSummaryType() != Transform::NoSummary) {
Chris@0 226 PluginSummarisingAdapter *adapter =
Chris@0 227 new PluginSummarisingAdapter(plugin);
Chris@0 228 adapter->setSummarySegmentBoundaries(m_boundaries);
Chris@0 229 plugin = adapter;
Chris@0 230 }
Chris@0 231
Chris@0 232 if (!plugin->initialise(m_channels, m_blockSize, m_blockSize)) {
Chris@0 233 cerr << "ERROR: Plugin initialise (channels = " << m_channels << ", stepSize = " << m_blockSize << ", blockSize = " << m_blockSize << ") failed." << endl;
Chris@0 234 delete plugin;
Chris@0 235 return false;
Chris@0 236 }
Chris@0 237
Chris@0 238 // cerr << "Initialised plugin" << endl;
Chris@0 239
Chris@0 240 size_t actualStepSize = 0;
Chris@0 241 size_t actualBlockSize = 0;
Chris@0 242 pba->getActualStepAndBlockSizes(actualStepSize, actualBlockSize);
Chris@0 243 transform.setStepSize(actualStepSize);
Chris@0 244 transform.setBlockSize(actualBlockSize);
Chris@0 245
Chris@0 246 Plugin::OutputList outputs = plugin->getOutputDescriptors();
Chris@0 247 for (int i = 0; i < (int)outputs.size(); ++i) {
Chris@0 248
Chris@0 249 // cerr << "Newly initialised plugin output " << i << " has bin count " << outputs[i].binCount << endl;
Chris@0 250
Chris@0 251 m_pluginOutputs[plugin][outputs[i].identifier] = outputs[i];
Chris@0 252 m_pluginOutputIndices[outputs[i].identifier] = i;
Chris@0 253 }
Chris@0 254
Chris@0 255 cerr << "NOTE: Loaded and initialised plugin " << plugin
Chris@0 256 << " for transform \""
Chris@0 257 << transform.getIdentifier().toStdString() << "\"" << endl;
Chris@0 258 }
Chris@0 259
Chris@0 260 if (transform.getOutput() == "") {
Chris@0 261 transform.setOutput
Chris@0 262 (plugin->getOutputDescriptors()[0].identifier.c_str());
Chris@0 263 }
Chris@0 264
Chris@0 265 m_transformPluginMap[transform] = plugin;
Chris@0 266
Chris@0 267 if (!(originalTransform == transform)) {
Chris@0 268 m_transformPluginMap[originalTransform] = plugin;
Chris@0 269 }
Chris@0 270
Chris@0 271 } else {
Chris@0 272
Chris@0 273 plugin = m_transformPluginMap[transform];
Chris@0 274 }
Chris@0 275
Chris@0 276 m_plugins[plugin][transform] = writers;
Chris@0 277
Chris@0 278 return true;
Chris@0 279 }
Chris@0 280
Chris@0 281 bool FeatureExtractionManager::addDefaultFeatureExtractor
Chris@0 282 (TransformId transformId, const vector<FeatureWriter*> &writers)
Chris@0 283 {
Chris@0 284 TransformFactory *tf = TransformFactory::getInstance();
Chris@0 285
Chris@0 286 if (m_sampleRate == 0) {
Chris@0 287 if (m_defaultSampleRate == 0) {
Chris@0 288 cerr << "ERROR: Default transform requested, but no default sample rate available" << endl;
Chris@0 289 return false;
Chris@0 290 } else {
Chris@0 291 cerr << "NOTE: Using default sample rate of " << m_defaultSampleRate << " for default transform" << endl;
Chris@0 292 m_sampleRate = m_defaultSampleRate;
Chris@0 293 }
Chris@0 294 }
Chris@0 295
Chris@0 296 Transform transform = tf->getDefaultTransformFor(transformId, m_sampleRate);
Chris@0 297
Chris@0 298 return addFeatureExtractor(transform, writers);
Chris@0 299 }
Chris@0 300
Chris@0 301 bool FeatureExtractionManager::addFeatureExtractorFromFile
Chris@0 302 (QString transformXmlFile, const vector<FeatureWriter*> &writers)
Chris@0 303 {
Chris@0 304 RDFTransformFactory factory
Chris@0 305 (QUrl::fromLocalFile(QFileInfo(transformXmlFile).absoluteFilePath())
Chris@0 306 .toString());
Chris@0 307 ProgressPrinter printer("Parsing transforms RDF file");
Chris@0 308 std::vector<Transform> transforms = factory.getTransforms(&printer);
Chris@0 309 if (!factory.isOK()) {
Chris@0 310 cerr << "WARNING: FeatureExtractionManager::addFeatureExtractorFromFile: Failed to parse transforms file: " << factory.getErrorString().toStdString() << endl;
Chris@0 311 if (factory.isRDF()) {
Chris@0 312 return false; // no point trying it as XML
Chris@0 313 }
Chris@0 314 }
Chris@0 315 if (!transforms.empty()) {
Chris@0 316 bool success = true;
Chris@0 317 for (int i = 0; i < (int)transforms.size(); ++i) {
Chris@0 318 if (!addFeatureExtractor(transforms[i], writers)) {
Chris@0 319 success = false;
Chris@0 320 }
Chris@0 321 }
Chris@0 322 return success;
Chris@0 323 }
Chris@0 324
Chris@0 325 QFile file(transformXmlFile);
Chris@0 326 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@0 327 cerr << "ERROR: Failed to open transform XML file \""
Chris@0 328 << transformXmlFile.toStdString() << "\" for reading" << endl;
Chris@0 329 return false;
Chris@0 330 }
Chris@0 331
Chris@0 332 QTextStream *qts = new QTextStream(&file);
Chris@0 333 QString qs = qts->readAll();
Chris@0 334 delete qts;
Chris@0 335 file.close();
Chris@0 336
Chris@0 337 Transform transform(qs);
Chris@0 338
Chris@0 339 return addFeatureExtractor(transform, writers);
Chris@0 340 }
Chris@0 341
Chris@0 342 void FeatureExtractionManager::extractFeatures(QString audioSource)
Chris@0 343 {
Chris@0 344 if (m_plugins.empty()) return;
Chris@0 345
Chris@0 346 ProgressPrinter printer("Retrieving audio data...");
Chris@0 347
Chris@0 348 FileSource source(audioSource, &printer);
Chris@0 349 if (!source.isAvailable()) {
Chris@0 350 cerr << "ERROR: File or URL \"" << audioSource.toStdString()
Chris@0 351 << "\" could not be located" << endl;
Chris@0 352 exit(1);
Chris@0 353 }
Chris@0 354
Chris@0 355 source.waitForData();
Chris@0 356
Chris@0 357 if (QFileInfo(audioSource).suffix().toLower() == "m3u") {
Chris@0 358 PlaylistFileReader reader(source);
Chris@0 359 if (reader.isOK()) {
Chris@0 360 vector<QString> files = reader.load();
Chris@0 361 for (int i = 0; i < (int)files.size(); ++i) {
Chris@0 362 extractFeatures(files[i]);
Chris@0 363 }
Chris@0 364 return;
Chris@0 365 } else {
Chris@0 366 cerr << "ERROR: Playlist \"" << audioSource.toStdString()
Chris@0 367 << "\" could not be opened" << endl;
Chris@0 368 exit(1);
Chris@0 369 }
Chris@0 370 }
Chris@0 371
Chris@0 372 if (m_sampleRate == 0) {
Chris@0 373 cerr << "ERROR: Internal error in FeatureExtractionManager::extractFeatures: Plugin list is non-empty, but no sample rate set" << endl;
Chris@0 374 exit(1);
Chris@0 375 }
Chris@0 376
Chris@0 377 AudioFileReader *reader =
Chris@0 378 AudioFileReaderFactory::createReader(source, m_sampleRate, &printer);
Chris@0 379
Chris@0 380 if (!reader) {
Chris@0 381 cerr << "ERROR: File or URL \"" << audioSource.toStdString()
Chris@0 382 << "\" could not be opened" << endl;
Chris@0 383 exit(1);
Chris@0 384 }
Chris@0 385
Chris@0 386 size_t channels = reader->getChannelCount();
Chris@0 387
Chris@0 388 cerr << "Opened " << channels << "-channel file or URL \"" << audioSource.toStdString() << "\"" << endl;
Chris@0 389
Chris@0 390 // reject file if it has too few channels, plugin will handle if it has too many
Chris@0 391 if ((int)channels < m_channels) {
Chris@0 392 //!!! should not be terminating here!
Chris@0 393 cerr << "ERROR: File or URL \"" << audioSource.toStdString() << "\" has less than " << m_channels << " channels" << endl;
Chris@0 394 exit(1);
Chris@0 395 }
Chris@0 396
Chris@0 397 // allocate audio buffers
Chris@0 398 float **data = new float *[m_channels];
Chris@0 399 for (int c = 0; c < m_channels; ++c) {
Chris@0 400 data[c] = new float[m_blockSize];
Chris@0 401 }
Chris@0 402
Chris@0 403 size_t frameCount = reader->getFrameCount();
Chris@0 404
Chris@0 405 // cerr << "file has " << frameCount << " frames" << endl;
Chris@0 406
Chris@0 407 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 408 pi != m_plugins.end(); ++pi) {
Chris@0 409
Chris@0 410 Plugin *plugin = pi->first;
Chris@0 411
Chris@0 412 // std::cerr << "Calling reset on " << plugin << std::endl;
Chris@0 413 plugin->reset();
Chris@0 414
Chris@0 415 for (TransformWriterMap::iterator ti = pi->second.begin();
Chris@0 416 ti != pi->second.end(); ++ti) {
Chris@0 417
Chris@0 418 const Transform &transform = ti->first;
Chris@0 419
Chris@0 420 //!!! we may want to set the start and duration times for extraction
Chris@0 421 // in the transform record (defaults of zero indicate extraction
Chris@0 422 // from the whole file)
Chris@0 423 // transform.setStartTime(RealTime::zeroTime);
Chris@0 424 // transform.setDuration
Chris@0 425 // (RealTime::frame2RealTime(reader->getFrameCount(), m_sampleRate));
Chris@0 426
Chris@0 427 string outputId = transform.getOutput().toStdString();
Chris@0 428 if (m_pluginOutputs[plugin].find(outputId) ==
Chris@0 429 m_pluginOutputs[plugin].end()) {
Chris@0 430 //!!! throw?
Chris@0 431 cerr << "WARNING: Nonexistent plugin output \"" << outputId << "\" requested for transform \""
Chris@0 432 << transform.getIdentifier().toStdString() << "\", ignoring this transform"
Chris@0 433 << endl;
Chris@0 434 /*
Chris@0 435 cerr << "Known outputs for all plugins are as follows:" << endl;
Chris@0 436 for (PluginOutputMap::const_iterator k = m_pluginOutputs.begin();
Chris@0 437 k != m_pluginOutputs.end(); ++k) {
Chris@0 438 cerr << "Plugin " << k->first << ": ";
Chris@0 439 if (k->second.empty()) {
Chris@0 440 cerr << "(none)";
Chris@0 441 }
Chris@0 442 for (OutputMap::const_iterator i = k->second.begin();
Chris@0 443 i != k->second.end(); ++i) {
Chris@0 444 cerr << "\"" << i->first << "\" ";
Chris@0 445 }
Chris@0 446 cerr << endl;
Chris@0 447 }
Chris@0 448 */
Chris@0 449 }
Chris@0 450 }
Chris@0 451 }
Chris@0 452
Chris@0 453 long startFrame = 0;
Chris@0 454 long endFrame = frameCount;
Chris@0 455
Chris@0 456 /*!!! No -- there is no single transform to pull this stuff from --
Chris@0 457 * the transforms may have various start and end times, need to be far
Chris@0 458 * cleverer about this if we're going to support them
Chris@0 459
Chris@0 460 RealTime trStartRT = transform.getStartTime();
Chris@0 461 RealTime trDurationRT = transform.getDuration();
Chris@0 462
Chris@0 463 long trStart = RealTime::realTime2Frame(trStartRT, m_sampleRate);
Chris@0 464 long trDuration = RealTime::realTime2Frame(trDurationRT, m_sampleRate);
Chris@0 465
Chris@0 466 if (trStart == 0 || trStart < startFrame) {
Chris@0 467 trStart = startFrame;
Chris@0 468 }
Chris@0 469
Chris@0 470 if (trDuration == 0) {
Chris@0 471 trDuration = endFrame - trStart;
Chris@0 472 }
Chris@0 473 if (trStart + trDuration > endFrame) {
Chris@0 474 trDuration = endFrame - trStart;
Chris@0 475 }
Chris@0 476
Chris@0 477 startFrame = trStart;
Chris@0 478 endFrame = trStart + trDuration;
Chris@0 479 */
Chris@0 480
Chris@0 481 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 482 pi != m_plugins.end(); ++pi) {
Chris@0 483
Chris@0 484 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 485 ti != pi->second.end(); ++ti) {
Chris@0 486
Chris@0 487 const vector<FeatureWriter *> &writers = ti->second;
Chris@0 488
Chris@0 489 for (int j = 0; j < (int)writers.size(); ++j) {
Chris@0 490 FeatureWriter::TrackMetadata m;
Chris@0 491 m.title = reader->getTitle();
Chris@0 492 m.maker = reader->getMaker();
Chris@0 493 writers[j]->setTrackMetadata(audioSource, m);
Chris@0 494 }
Chris@0 495 }
Chris@0 496 }
Chris@0 497
Chris@0 498 ProgressPrinter extractionProgress("Extracting and writing features...");
Chris@0 499 int progress = 0;
Chris@0 500
Chris@0 501 for (long i = startFrame; i < endFrame; i += m_blockSize) {
Chris@0 502
Chris@0 503 //!!! inefficient, although much of the inefficiency may be
Chris@0 504 // susceptible to optimisation
Chris@0 505
Chris@0 506 SampleBlock frames;
Chris@0 507 reader->getInterleavedFrames(i, m_blockSize, frames);
Chris@0 508
Chris@0 509 // We have to do our own channel handling here; we can't just
Chris@0 510 // leave it to the plugin adapter because the same plugin
Chris@0 511 // adapter may have to serve for input files with various
Chris@0 512 // numbers of channels (so the adapter is simply configured
Chris@0 513 // with a fixed channel count, generally 1).
Chris@0 514
Chris@0 515 int rc = reader->getChannelCount();
Chris@0 516
Chris@0 517 for (int j = 0; j < m_blockSize; ++j) {
Chris@0 518 for (int c = 0; c < m_channels; ++c) {
Chris@0 519 int index;
Chris@0 520 if (c < rc) {
Chris@0 521 index = j * rc + c;
Chris@0 522 data[c][j] = 0.f;
Chris@0 523 } else {
Chris@0 524 index = j * rc + (c % rc);
Chris@0 525 }
Chris@0 526 if (index < (int)frames.size()) {
Chris@0 527 data[c][j] += frames[index];
Chris@0 528 }
Chris@0 529 }
Chris@0 530 }
Chris@0 531
Chris@0 532 Vamp::RealTime timestamp = Vamp::RealTime::frame2RealTime
Chris@0 533 (i, m_sampleRate);
Chris@0 534
Chris@0 535 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 536 pi != m_plugins.end(); ++pi) {
Chris@0 537
Chris@0 538 Plugin *plugin = pi->first;
Chris@0 539 Plugin::FeatureSet featureSet = plugin->process(data, timestamp);
Chris@0 540
Chris@0 541 if (!m_summariesOnly) {
Chris@0 542 writeFeatures(audioSource, plugin, featureSet);
Chris@0 543 }
Chris@0 544 }
Chris@0 545
Chris@0 546 int pp = progress;
Chris@0 547 progress = ((i - startFrame) * 100) / (endFrame - startFrame);
Chris@0 548 if (progress > pp) extractionProgress.setProgress(progress);
Chris@0 549 }
Chris@0 550
Chris@0 551 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 552 pi != m_plugins.end(); ++pi) {
Chris@0 553
Chris@0 554 Plugin *plugin = pi->first;
Chris@0 555 Plugin::FeatureSet featureSet = plugin->getRemainingFeatures();
Chris@0 556
Chris@0 557 if (!m_summariesOnly) {
Chris@0 558 writeFeatures(audioSource, plugin, featureSet);
Chris@0 559 }
Chris@0 560
Chris@0 561 if (!m_summaries.empty()) {
Chris@0 562 PluginSummarisingAdapter *adapter =
Chris@0 563 dynamic_cast<PluginSummarisingAdapter *>(plugin);
Chris@0 564 if (!adapter) {
Chris@0 565 cerr << "WARNING: Summaries requested, but plugin is not a summarising adapter" << endl;
Chris@0 566 } else {
Chris@0 567 for (SummaryNameSet::const_iterator sni = m_summaries.begin();
Chris@0 568 sni != m_summaries.end(); ++sni) {
Chris@0 569 featureSet.clear();
Chris@0 570 //!!! problem here -- we are requesting summaries
Chris@0 571 //!!! for all outputs, but they in principle have
Chris@0 572 //!!! different averaging requirements depending
Chris@0 573 //!!! on whether their features have duration or
Chris@0 574 //!!! not
Chris@0 575 featureSet = adapter->getSummaryForAllOutputs
Chris@0 576 (getSummaryType(*sni),
Chris@0 577 PluginSummarisingAdapter::ContinuousTimeAverage);
Chris@0 578 writeFeatures(audioSource, plugin, featureSet,//!!! *sni);
Chris@0 579 Transform::stringToSummaryType(sni->c_str()));
Chris@0 580 }
Chris@0 581 }
Chris@0 582 }
Chris@0 583
Chris@0 584 writeSummaries(audioSource, plugin);
Chris@0 585 }
Chris@0 586
Chris@0 587 finish();
Chris@0 588
Chris@0 589 extractionProgress.setProgress(100);
Chris@0 590
Chris@0 591 TempDirectory::getInstance()->cleanup();
Chris@0 592 }
Chris@0 593
Chris@0 594 void
Chris@0 595 FeatureExtractionManager::writeSummaries(QString audioSource, Plugin *plugin)
Chris@0 596 {
Chris@0 597 // caller should have ensured plugin is in m_plugins
Chris@0 598 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 599
Chris@0 600 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 601 ti != pi->second.end(); ++ti) {
Chris@0 602
Chris@0 603 const Transform &transform = ti->first;
Chris@0 604 const vector<FeatureWriter *> &writers = ti->second;
Chris@0 605
Chris@0 606 Transform::SummaryType summaryType = transform.getSummaryType();
Chris@0 607 PluginSummarisingAdapter::SummaryType pType =
Chris@0 608 (PluginSummarisingAdapter::SummaryType)summaryType;
Chris@0 609
Chris@0 610 if (transform.getSummaryType() == Transform::NoSummary) {
Chris@0 611 continue;
Chris@0 612 }
Chris@0 613
Chris@0 614 PluginSummarisingAdapter *adapter =
Chris@0 615 dynamic_cast<PluginSummarisingAdapter *>(plugin);
Chris@0 616 if (!adapter) {
Chris@0 617 cerr << "FeatureExtractionManager::writeSummaries: INTERNAL ERROR: Summary requested for transform, but plugin is not a summarising adapter" << endl;
Chris@0 618 continue;
Chris@0 619 }
Chris@0 620
Chris@0 621 Plugin::FeatureSet featureSet = adapter->getSummaryForAllOutputs
Chris@0 622 (pType, PluginSummarisingAdapter::ContinuousTimeAverage);
Chris@0 623
Chris@0 624 // cout << "summary type " << int(pType) << " for transform:" << endl << transform.toXmlString().toStdString()<< endl << "... feature set with " << featureSet.size() << " elts" << endl;
Chris@0 625
Chris@0 626 writeFeatures(audioSource, plugin, featureSet, summaryType);
Chris@0 627 }
Chris@0 628 }
Chris@0 629
Chris@0 630 void FeatureExtractionManager::writeFeatures(QString audioSource,
Chris@0 631 Plugin *plugin,
Chris@0 632 const Plugin::FeatureSet &features,
Chris@0 633 Transform::SummaryType summaryType)
Chris@0 634 {
Chris@0 635 // caller should have ensured plugin is in m_plugins
Chris@0 636 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 637
Chris@0 638 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 639 ti != pi->second.end(); ++ti) {
Chris@0 640
Chris@0 641 const Transform &transform = ti->first;
Chris@0 642 const vector<FeatureWriter *> &writers = ti->second;
Chris@0 643
Chris@0 644 if (transform.getSummaryType() != Transform::NoSummary &&
Chris@0 645 m_summaries.empty() &&
Chris@0 646 summaryType == Transform::NoSummary) {
Chris@0 647 continue;
Chris@0 648 }
Chris@0 649
Chris@0 650 if (transform.getSummaryType() != Transform::NoSummary &&
Chris@0 651 summaryType != Transform::NoSummary &&
Chris@0 652 transform.getSummaryType() != summaryType) {
Chris@0 653 continue;
Chris@0 654 }
Chris@0 655
Chris@0 656 string outputId = transform.getOutput().toStdString();
Chris@0 657
Chris@0 658 if (m_pluginOutputs[plugin].find(outputId) ==
Chris@0 659 m_pluginOutputs[plugin].end()) {
Chris@0 660 continue;
Chris@0 661 }
Chris@0 662
Chris@0 663 const Plugin::OutputDescriptor &desc =
Chris@0 664 m_pluginOutputs[plugin][outputId];
Chris@0 665
Chris@0 666 int outputIndex = m_pluginOutputIndices[outputId];
Chris@0 667 Plugin::FeatureSet::const_iterator fsi = features.find(outputIndex);
Chris@0 668 if (fsi == features.end()) continue;
Chris@0 669
Chris@0 670 for (int j = 0; j < (int)writers.size(); ++j) {
Chris@0 671 writers[j]->write
Chris@0 672 (audioSource, transform, desc, fsi->second,
Chris@0 673 Transform::summaryTypeToString(summaryType).toStdString());
Chris@0 674 }
Chris@0 675 }
Chris@0 676 }
Chris@0 677
Chris@0 678 void FeatureExtractionManager::finish()
Chris@0 679 {
Chris@0 680 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 681 pi != m_plugins.end(); ++pi) {
Chris@0 682
Chris@0 683 for (TransformWriterMap::iterator ti = pi->second.begin();
Chris@0 684 ti != pi->second.end(); ++ti) {
Chris@0 685
Chris@0 686 vector<FeatureWriter *> &writers = ti->second;
Chris@0 687
Chris@0 688 for (int i = 0; i < (int)writers.size(); ++i) {
Chris@0 689 writers[i]->flush();
Chris@0 690 writers[i]->finish();
Chris@0 691 }
Chris@0 692 }
Chris@0 693 }
Chris@0 694 }
Chris@0 695
Chris@0 696 void FeatureExtractionManager::print(Transform transform) const
Chris@0 697 {
Chris@0 698 QString qs;
Chris@0 699 QTextStream qts(&qs);
Chris@0 700 transform.toXml(qts);
Chris@0 701 cerr << qs.toStdString() << endl;
Chris@0 702 }