annotate runner/FeatureExtractionManager.cpp @ 8:f98b58fb294f

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