annotate runner/FeatureExtractionManager.cpp @ 34:76ea8ffc4dfa

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