annotate runner/FeatureExtractionManager.cpp @ 302:1e6cee321cc0

Added tag sonic-annotator-1.5 for changeset 929e37acf0d9
author Chris Cannam
date Thu, 25 May 2017 15:01:41 +0100
parents 5ba121f3889e
children 0e866ef12d87
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@106 17 #include "MultiplexedReader.h"
Chris@0 18
Chris@0 19 #include <vamp-hostsdk/PluginChannelAdapter.h>
Chris@0 20 #include <vamp-hostsdk/PluginBufferingAdapter.h>
Chris@0 21 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
Chris@0 22 #include <vamp-hostsdk/PluginSummarisingAdapter.h>
Chris@8 23 #include <vamp-hostsdk/PluginWrapper.h>
Chris@0 24 #include <vamp-hostsdk/PluginLoader.h>
Chris@0 25
Chris@263 26 #include "base/Debug.h"
Chris@21 27 #include "base/Exceptions.h"
Chris@21 28
Chris@0 29 #include <iostream>
Chris@0 30
Chris@0 31 using namespace std;
Chris@0 32
Chris@0 33 using Vamp::Plugin;
Chris@0 34 using Vamp::PluginBase;
Chris@0 35 using Vamp::HostExt::PluginLoader;
Chris@0 36 using Vamp::HostExt::PluginChannelAdapter;
Chris@0 37 using Vamp::HostExt::PluginBufferingAdapter;
Chris@0 38 using Vamp::HostExt::PluginInputDomainAdapter;
Chris@0 39 using Vamp::HostExt::PluginSummarisingAdapter;
Chris@8 40 using Vamp::HostExt::PluginWrapper;
Chris@0 41
Chris@0 42 #include "data/fileio/FileSource.h"
Chris@0 43 #include "data/fileio/AudioFileReader.h"
Chris@0 44 #include "data/fileio/AudioFileReaderFactory.h"
Chris@0 45 #include "base/TempDirectory.h"
Chris@0 46 #include "base/ProgressPrinter.h"
Chris@0 47 #include "transform/TransformFactory.h"
Chris@0 48 #include "rdf/RDFTransformFactory.h"
Chris@0 49 #include "transform/FeatureWriter.h"
Chris@0 50
Chris@0 51 #include <QTextStream>
Chris@0 52 #include <QFile>
Chris@0 53 #include <QFileInfo>
Chris@0 54
Chris@263 55 FeatureExtractionManager::FeatureExtractionManager(bool verbose) :
Chris@263 56 m_verbose(verbose),
Chris@0 57 m_summariesOnly(false),
Chris@0 58 // We can read using an arbitrary fixed block size --
Chris@177 59 // PluginBufferingAdapter handles this for us. But while this
Chris@177 60 // doesn't affect the step and block size actually passed to the
Chris@177 61 // plugin, it does affect the overall time range of the audio
Chris@177 62 // input (which gets rounded to the nearest block boundary). So
Chris@177 63 // although a larger blocksize will normally run faster, and we
Chris@177 64 // used a blocksize of 16384 in earlier releases of Sonic
Chris@177 65 // Annotator for that reason, a smaller blocksize produces
Chris@177 66 // "better" results and this is particularly relevant now we
Chris@177 67 // support the start and duration flags for a transform.
Chris@177 68 m_blockSize(1024),
Chris@0 69 m_defaultSampleRate(0),
Chris@0 70 m_sampleRate(0),
Chris@116 71 m_channels(0),
Chris@116 72 m_normalise(false)
Chris@0 73 {
Chris@0 74 }
Chris@0 75
Chris@0 76 FeatureExtractionManager::~FeatureExtractionManager()
Chris@0 77 {
Chris@263 78 SVDEBUG << "FeatureExtractionManager::~FeatureExtractionManager: cleaning up"
Chris@263 79 << endl;
Chris@263 80
Chris@0 81 for (PluginMap::iterator pi = m_plugins.begin();
Chris@0 82 pi != m_plugins.end(); ++pi) {
Chris@0 83 delete pi->first;
Chris@0 84 }
Chris@45 85 foreach (AudioFileReader *r, m_readyReaders) {
Chris@45 86 delete r;
Chris@45 87 }
Chris@263 88
Chris@263 89 SVDEBUG << "FeatureExtractionManager::~FeatureExtractionManager: done" << endl;
Chris@0 90 }
Chris@0 91
Chris@0 92 void FeatureExtractionManager::setChannels(int channels)
Chris@0 93 {
Chris@0 94 m_channels = channels;
Chris@0 95 }
Chris@0 96
Chris@0 97 void FeatureExtractionManager::setDefaultSampleRate(int sampleRate)
Chris@0 98 {
Chris@0 99 m_defaultSampleRate = sampleRate;
Chris@0 100 }
Chris@0 101
Chris@116 102 void FeatureExtractionManager::setNormalise(bool normalise)
Chris@116 103 {
Chris@116 104 m_normalise = normalise;
Chris@116 105 }
Chris@116 106
Chris@0 107 static PluginSummarisingAdapter::SummaryType
Chris@0 108 getSummaryType(string name)
Chris@0 109 {
Chris@0 110 if (name == "min") return PluginSummarisingAdapter::Minimum;
Chris@0 111 if (name == "max") return PluginSummarisingAdapter::Maximum;
Chris@0 112 if (name == "mean") return PluginSummarisingAdapter::Mean;
Chris@0 113 if (name == "median") return PluginSummarisingAdapter::Median;
Chris@0 114 if (name == "mode") return PluginSummarisingAdapter::Mode;
Chris@0 115 if (name == "sum") return PluginSummarisingAdapter::Sum;
Chris@0 116 if (name == "variance") return PluginSummarisingAdapter::Variance;
Chris@0 117 if (name == "sd") return PluginSummarisingAdapter::StandardDeviation;
Chris@0 118 if (name == "count") return PluginSummarisingAdapter::Count;
Chris@0 119 return PluginSummarisingAdapter::UnknownSummaryType;
Chris@0 120 }
Chris@0 121
Chris@102 122 bool
Chris@102 123 FeatureExtractionManager::setSummaryTypes(const set<string> &names,
Chris@102 124 const PluginSummarisingAdapter::SegmentBoundaries &boundaries)
Chris@0 125 {
Chris@0 126 for (SummaryNameSet::const_iterator i = names.begin();
Chris@0 127 i != names.end(); ++i) {
Chris@0 128 if (getSummaryType(*i) == PluginSummarisingAdapter::UnknownSummaryType) {
Chris@263 129 SVCERR << "ERROR: Unknown summary type \"" << *i << "\"" << endl;
Chris@0 130 return false;
Chris@0 131 }
Chris@0 132 }
Chris@0 133 m_summaries = names;
Chris@0 134 m_boundaries = boundaries;
Chris@0 135 return true;
Chris@0 136 }
Chris@0 137
Chris@102 138 void
Chris@102 139 FeatureExtractionManager::setSummariesOnly(bool summariesOnly)
Chris@102 140 {
Chris@102 141 m_summariesOnly = summariesOnly;
Chris@102 142 }
Chris@102 143
Chris@51 144 static PluginInputDomainAdapter::WindowType
Chris@51 145 convertWindowType(WindowType t)
Chris@51 146 {
Chris@51 147 switch (t) {
Chris@51 148 case RectangularWindow:
Chris@51 149 return PluginInputDomainAdapter::RectangularWindow;
Chris@51 150 case BartlettWindow:
Chris@51 151 return PluginInputDomainAdapter::BartlettWindow;
Chris@51 152 case HammingWindow:
Chris@51 153 return PluginInputDomainAdapter::HammingWindow;
Chris@51 154 case HanningWindow:
Chris@51 155 return PluginInputDomainAdapter::HanningWindow;
Chris@51 156 case BlackmanWindow:
Chris@51 157 return PluginInputDomainAdapter::BlackmanWindow;
Chris@51 158 case NuttallWindow:
Chris@51 159 return PluginInputDomainAdapter::NuttallWindow;
Chris@51 160 case BlackmanHarrisWindow:
Chris@51 161 return PluginInputDomainAdapter::BlackmanHarrisWindow;
Chris@138 162 case GaussianWindow:
Chris@138 163 case ParzenWindow:
Chris@138 164 // Not supported in Vamp SDK, fall through
Chris@51 165 default:
Chris@263 166 SVCERR << "ERROR: Unknown or unsupported window type \"" << t << "\", using Hann (\"" << HanningWindow << "\")" << endl;
Chris@51 167 return PluginInputDomainAdapter::HanningWindow;
Chris@51 168 }
Chris@51 169 }
Chris@51 170
Chris@0 171 bool FeatureExtractionManager::addFeatureExtractor
Chris@0 172 (Transform transform, const vector<FeatureWriter*> &writers)
Chris@0 173 {
Chris@0 174 //!!! exceptions rather than return values?
Chris@0 175
Chris@0 176 if (transform.getSampleRate() == 0) {
Chris@0 177 if (m_sampleRate == 0) {
Chris@263 178 SVCERR << "NOTE: Transform does not specify a sample rate, using default rate of " << m_defaultSampleRate << endl;
Chris@0 179 transform.setSampleRate(m_defaultSampleRate);
Chris@0 180 m_sampleRate = m_defaultSampleRate;
Chris@0 181 } else {
Chris@263 182 SVCERR << "NOTE: Transform does not specify a sample rate, using previous transform's rate of " << m_sampleRate << endl;
Chris@0 183 transform.setSampleRate(m_sampleRate);
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 if (m_sampleRate == 0) {
Chris@0 188 m_sampleRate = transform.getSampleRate();
Chris@0 189 }
Chris@0 190
Chris@0 191 if (transform.getSampleRate() != m_sampleRate) {
Chris@263 192 SVCERR << "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@263 193 SVCERR << "WARNING: Using previous rate of " << m_sampleRate << " for this transform as well" << endl;
Chris@0 194 transform.setSampleRate(m_sampleRate);
Chris@0 195 }
Chris@0 196
Chris@0 197 Plugin *plugin = 0;
Chris@0 198
Chris@0 199 // Remember what the original transform looked like, and index
Chris@0 200 // based on this -- because we may be about to fill in the zeros
Chris@0 201 // for step and block size, but we want any further copies with
Chris@0 202 // the same zeros to match this one
Chris@0 203 Transform originalTransform = transform;
Chris@0 204
Chris@0 205 if (m_transformPluginMap.find(transform) == m_transformPluginMap.end()) {
Chris@0 206
Chris@0 207 // Test whether we already have a transform that is identical
Chris@0 208 // to this, except for the output requested and/or the summary
Chris@0 209 // type -- if so, they should share plugin instances (a vital
Chris@0 210 // optimisation)
Chris@0 211
Chris@0 212 for (TransformPluginMap::iterator i = m_transformPluginMap.begin();
Chris@0 213 i != m_transformPluginMap.end(); ++i) {
Chris@0 214 Transform test = i->first;
Chris@0 215 test.setOutput(transform.getOutput());
Chris@0 216 test.setSummaryType(transform.getSummaryType());
Chris@0 217 if (transform == test) {
Chris@263 218 SVCERR << "NOTE: Already have transform identical to this one (for \""
Chris@0 219 << transform.getIdentifier().toStdString()
Chris@0 220 << "\") in every detail except output identifier and/or "
Chris@0 221 << "summary type; sharing its plugin instance" << endl;
Chris@0 222 plugin = i->second;
Chris@0 223 if (transform.getSummaryType() != Transform::NoSummary &&
Chris@0 224 !dynamic_cast<PluginSummarisingAdapter *>(plugin)) {
Chris@0 225 plugin = new PluginSummarisingAdapter(plugin);
Chris@0 226 i->second = plugin;
Chris@0 227 }
Chris@0 228 break;
Chris@0 229 }
Chris@0 230 }
Chris@0 231
Chris@0 232 if (!plugin) {
Chris@0 233
Chris@0 234 TransformFactory *tf = TransformFactory::getInstance();
Chris@0 235
Chris@0 236 PluginBase *pb = tf->instantiatePluginFor(transform);
Chris@0 237 plugin = tf->downcastVampPlugin(pb);
Chris@0 238 if (!plugin) {
Chris@0 239 //!!! todo: handle non-Vamp plugins too, or make the main --list
Chris@0 240 // option print out only Vamp transforms
Chris@263 241 SVCERR << "ERROR: Failed to load plugin for transform \""
Chris@0 242 << transform.getIdentifier().toStdString() << "\"" << endl;
Chris@203 243 if (pb) {
Chris@263 244 SVCERR << "NOTE: (A plugin was loaded, but apparently not a Vamp plugin)" << endl;
Chris@203 245 }
Chris@0 246 delete pb;
Chris@0 247 return false;
Chris@0 248 }
Chris@0 249
Chris@0 250 // We will provide the plugin with arbitrary step and
Chris@0 251 // block sizes (so that we can use the same read/write
Chris@0 252 // block size for all transforms), and to that end we use
Chris@0 253 // a PluginBufferingAdapter. However, we need to know the
Chris@0 254 // underlying step size so that we can provide the right
Chris@0 255 // context for dense outputs. (Although, don't forget
Chris@0 256 // that the PluginBufferingAdapter rewrites
Chris@0 257 // OneSamplePerStep outputs so as to use FixedSampleRate
Chris@0 258 // -- so it supplies the sample rate in the output
Chris@0 259 // feature. I'm not sure whether we can easily use that.)
Chris@0 260
Chris@0 261 size_t pluginStepSize = plugin->getPreferredStepSize();
Chris@0 262 size_t pluginBlockSize = plugin->getPreferredBlockSize();
Chris@0 263
Chris@25 264 PluginInputDomainAdapter *pida = 0;
Chris@25 265
Chris@0 266 // adapt the plugin for buffering, channels, etc.
Chris@0 267 if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
Chris@51 268
Chris@25 269 pida = new PluginInputDomainAdapter(plugin);
Chris@26 270 pida->setProcessTimestampMethod(PluginInputDomainAdapter::ShiftData);
Chris@51 271
Chris@51 272 PluginInputDomainAdapter::WindowType wtype =
Chris@51 273 convertWindowType(transform.getWindowType());
Chris@51 274 pida->setWindowType(wtype);
Chris@25 275 plugin = pida;
Chris@0 276 }
Chris@0 277
Chris@0 278 PluginBufferingAdapter *pba = new PluginBufferingAdapter(plugin);
Chris@0 279 plugin = pba;
Chris@0 280
Chris@0 281 if (transform.getStepSize() != 0) {
Chris@0 282 pba->setPluginStepSize(transform.getStepSize());
Chris@0 283 } else {
Chris@0 284 transform.setStepSize(pluginStepSize);
Chris@0 285 }
Chris@0 286
Chris@0 287 if (transform.getBlockSize() != 0) {
Chris@0 288 pba->setPluginBlockSize(transform.getBlockSize());
Chris@0 289 } else {
Chris@0 290 transform.setBlockSize(pluginBlockSize);
Chris@0 291 }
Chris@0 292
Chris@0 293 plugin = new PluginChannelAdapter(plugin);
Chris@0 294
Chris@0 295 if (!m_summaries.empty() ||
Chris@0 296 transform.getSummaryType() != Transform::NoSummary) {
Chris@0 297 PluginSummarisingAdapter *adapter =
Chris@0 298 new PluginSummarisingAdapter(plugin);
Chris@0 299 adapter->setSummarySegmentBoundaries(m_boundaries);
Chris@0 300 plugin = adapter;
Chris@0 301 }
Chris@0 302
Chris@0 303 if (!plugin->initialise(m_channels, m_blockSize, m_blockSize)) {
Chris@263 304 SVCERR << "ERROR: Plugin initialise (channels = " << m_channels << ", stepSize = " << m_blockSize << ", blockSize = " << m_blockSize << ") failed." << endl;
Chris@0 305 delete plugin;
Chris@0 306 return false;
Chris@0 307 }
Chris@0 308
Chris@263 309 SVDEBUG << "Initialised plugin" << endl;
Chris@0 310
Chris@0 311 size_t actualStepSize = 0;
Chris@0 312 size_t actualBlockSize = 0;
Chris@0 313 pba->getActualStepAndBlockSizes(actualStepSize, actualBlockSize);
Chris@0 314 transform.setStepSize(actualStepSize);
Chris@0 315 transform.setBlockSize(actualBlockSize);
Chris@0 316
Chris@0 317 Plugin::OutputList outputs = plugin->getOutputDescriptors();
Chris@0 318 for (int i = 0; i < (int)outputs.size(); ++i) {
Chris@0 319
Chris@263 320 SVDEBUG << "Newly initialised plugin output " << i << " has bin count " << outputs[i].binCount << endl;
Chris@0 321
Chris@0 322 m_pluginOutputs[plugin][outputs[i].identifier] = outputs[i];
Chris@0 323 m_pluginOutputIndices[outputs[i].identifier] = i;
Chris@0 324 }
Chris@0 325
Chris@263 326 SVCERR << "NOTE: Loaded and initialised plugin for transform \""
Chris@25 327 << transform.getIdentifier().toStdString()
Chris@25 328 << "\" with plugin step size " << actualStepSize
Chris@25 329 << " and block size " << actualBlockSize
Chris@25 330 << " (adapter step and block size " << m_blockSize << ")"
Chris@25 331 << endl;
Chris@25 332
Chris@263 333 SVDEBUG << "NOTE: That transform is: " << transform.toXmlString() << endl;
Chris@233 334
Chris@25 335 if (pida) {
Chris@263 336 SVCERR << "NOTE: PluginInputDomainAdapter timestamp adjustment is "
Chris@25 337 << pida->getTimestampAdjustment() << endl;
Chris@25 338 }
Chris@8 339
Chris@8 340 } else {
Chris@8 341
Chris@8 342 if (transform.getStepSize() == 0 || transform.getBlockSize() == 0) {
Chris@8 343
Chris@8 344 PluginWrapper *pw = dynamic_cast<PluginWrapper *>(plugin);
Chris@8 345 if (pw) {
Chris@8 346 PluginBufferingAdapter *pba =
Chris@8 347 pw->getWrapper<PluginBufferingAdapter>();
Chris@8 348 if (pba) {
Chris@8 349 size_t actualStepSize = 0;
Chris@8 350 size_t actualBlockSize = 0;
Chris@8 351 pba->getActualStepAndBlockSizes(actualStepSize,
Chris@8 352 actualBlockSize);
Chris@8 353 if (transform.getStepSize() == 0) {
Chris@8 354 transform.setStepSize(actualStepSize);
Chris@8 355 }
Chris@8 356 if (transform.getBlockSize() == 0) {
Chris@8 357 transform.setBlockSize(actualBlockSize);
Chris@8 358 }
Chris@8 359 }
Chris@8 360 }
Chris@8 361 }
Chris@0 362 }
Chris@0 363
Chris@130 364 if (transform.getPluginVersion() != "") {
Chris@130 365 if (QString("%1").arg(plugin->getPluginVersion())
Chris@130 366 != transform.getPluginVersion()) {
Chris@263 367 SVCERR << "ERROR: Transform specifies version "
Chris@130 368 << transform.getPluginVersion()
Chris@130 369 << " of plugin \"" << plugin->getIdentifier()
Chris@130 370 << "\", but installed plugin is version "
Chris@130 371 << plugin->getPluginVersion()
Chris@130 372 << endl;
Chris@130 373 return false;
Chris@130 374 }
Chris@130 375 }
Chris@130 376
Chris@0 377 if (transform.getOutput() == "") {
Chris@0 378 transform.setOutput
Chris@0 379 (plugin->getOutputDescriptors()[0].identifier.c_str());
Chris@129 380 } else {
Chris@129 381 if (m_pluginOutputs[plugin].find
Chris@129 382 (transform.getOutput().toLocal8Bit().data()) ==
Chris@129 383 m_pluginOutputs[plugin].end()) {
Chris@263 384 SVCERR << "ERROR: Transform requests nonexistent plugin output \""
Chris@129 385 << transform.getOutput()
Chris@129 386 << "\"" << endl;
Chris@129 387 return false;
Chris@129 388 }
Chris@0 389 }
Chris@0 390
Chris@0 391 m_transformPluginMap[transform] = plugin;
Chris@0 392
Chris@263 393 SVDEBUG << "NOTE: Assigned plugin " << plugin << " for transform: " << transform.toXmlString() << endl;
Chris@233 394
Chris@0 395 if (!(originalTransform == transform)) {
Chris@0 396 m_transformPluginMap[originalTransform] = plugin;
Chris@263 397 SVDEBUG << "NOTE: Also assigned plugin " << plugin << " for original transform: " << originalTransform.toXmlString() << endl;
Chris@0 398 }
Chris@0 399
Chris@0 400 } else {
Chris@0 401
Chris@0 402 plugin = m_transformPluginMap[transform];
Chris@0 403 }
Chris@0 404
Chris@109 405 if (m_plugins.find(plugin) == m_plugins.end()) {
Chris@109 406 m_orderedPlugins.push_back(plugin);
Chris@109 407 }
Chris@109 408
Chris@0 409 m_plugins[plugin][transform] = writers;
Chris@0 410
Chris@0 411 return true;
Chris@0 412 }
Chris@0 413
Chris@0 414 bool FeatureExtractionManager::addDefaultFeatureExtractor
Chris@0 415 (TransformId transformId, const vector<FeatureWriter*> &writers)
Chris@0 416 {
Chris@0 417 TransformFactory *tf = TransformFactory::getInstance();
Chris@0 418
Chris@0 419 if (m_sampleRate == 0) {
Chris@0 420 if (m_defaultSampleRate == 0) {
Chris@263 421 SVCERR << "ERROR: Default transform requested, but no default sample rate available" << endl;
Chris@0 422 return false;
Chris@0 423 } else {
Chris@263 424 SVCERR << "NOTE: Using default sample rate of " << m_defaultSampleRate << " for default transform" << endl;
Chris@0 425 m_sampleRate = m_defaultSampleRate;
Chris@0 426 }
Chris@0 427 }
Chris@0 428
Chris@0 429 Transform transform = tf->getDefaultTransformFor(transformId, m_sampleRate);
Chris@0 430
Chris@203 431 bool result = addFeatureExtractor(transform, writers);
Chris@203 432 if (!result) {
Chris@203 433 if (transform.getType() == Transform::UnknownType) {
Chris@263 434 SVCERR << "(Maybe mixed up filename with transform, or --transform with --default?)" << endl;
Chris@203 435 }
Chris@203 436 }
Chris@203 437 return result;
Chris@0 438 }
Chris@0 439
Chris@0 440 bool FeatureExtractionManager::addFeatureExtractorFromFile
Chris@227 441 (QString transformFile, const vector<FeatureWriter*> &writers)
Chris@0 442 {
Chris@227 443 // We support two formats for transform description files, XML (in
Chris@227 444 // a format specific to Sonic Annotator) and RDF/Turtle. The RDF
Chris@227 445 // format can describe multiple transforms in a single file, the
Chris@227 446 // XML only one.
Chris@227 447
Chris@227 448 // Possible errors we should report:
Chris@227 449 //
Chris@227 450 // 1. File does not exist or cannot be opened
Chris@227 451 // 2. File is ostensibly XML, but is not parseable
Chris@227 452 // 3. File is ostensibly Turtle, but is not parseable
Chris@227 453 // 4. File is XML, but contains no valid transform (e.g. is unrelated XML)
Chris@227 454 // 5. File is Turtle, but contains no valid transform(s)
Chris@227 455 // 6. File is Turtle and contains both valid and invalid transform(s)
Chris@227 456
Chris@227 457 {
Chris@227 458 // We don't actually need to open this here yet, we just hoist
Chris@227 459 // it to the top for error reporting purposes
Chris@227 460 QFile file(transformFile);
Chris@227 461 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@227 462 // Error case 1. File does not exist or cannot be opened
Chris@263 463 SVCERR << "ERROR: Failed to open transform file \"" << transformFile
Chris@227 464 << "\" for reading" << endl;
Chris@227 465 return false;
Chris@227 466 }
Chris@227 467 }
Chris@227 468
Chris@99 469 bool tryRdf = true;
Chris@227 470 if (transformFile.endsWith(".xml") || transformFile.endsWith(".XML")) {
Chris@99 471 // We don't support RDF-XML (and nor does the underlying
Chris@99 472 // parser library) so skip the RDF parse if the filename
Chris@99 473 // suggests XML, to avoid puking out a load of errors from
Chris@99 474 // feeding XML to a Turtle parser
Chris@99 475 tryRdf = false;
Chris@0 476 }
Chris@99 477
Chris@227 478 bool tryXml = true;
Chris@227 479 if (transformFile.endsWith(".ttl") || transformFile.endsWith(".TTL") ||
Chris@227 480 transformFile.endsWith(".ntriples") || transformFile.endsWith(".NTRIPLES") ||
Chris@227 481 transformFile.endsWith(".n3") || transformFile.endsWith(".N3")) {
Chris@227 482 tryXml = false;
Chris@227 483 }
Chris@227 484
Chris@227 485 QString rdfError, xmlError;
Chris@227 486
Chris@99 487 if (tryRdf) {
Chris@227 488
Chris@99 489 RDFTransformFactory factory
Chris@227 490 (QUrl::fromLocalFile(QFileInfo(transformFile).absoluteFilePath())
Chris@99 491 .toString());
Chris@99 492 ProgressPrinter printer("Parsing transforms RDF file");
Chris@263 493 std::vector<Transform> transforms = factory.getTransforms
Chris@263 494 (m_verbose ? &printer : 0);
Chris@227 495
Chris@227 496 if (factory.isOK()) {
Chris@227 497 if (transforms.empty()) {
Chris@263 498 SVCERR << "ERROR: Transform file \"" << transformFile
Chris@227 499 << "\" is valid RDF but defines no transforms" << endl;
Chris@227 500 return false;
Chris@227 501 } else {
Chris@227 502 bool success = true;
Chris@227 503 for (int i = 0; i < (int)transforms.size(); ++i) {
Chris@227 504 if (!addFeatureExtractor(transforms[i], writers)) {
Chris@227 505 success = false;
Chris@227 506 }
Chris@227 507 }
Chris@227 508 return success;
Chris@227 509 }
Chris@227 510 } else { // !factory.isOK()
Chris@99 511 if (factory.isRDF()) {
Chris@263 512 SVCERR << "ERROR: Invalid transform RDF file \"" << transformFile
Chris@227 513 << "\": " << factory.getErrorString() << endl;
Chris@227 514 return false;
Chris@0 515 }
Chris@227 516
Chris@227 517 // the not-RDF case: fall through without reporting an
Chris@227 518 // error, so we try the file as XML, and if that fails, we
Chris@227 519 // print a general unparseable-file error
Chris@227 520 rdfError = factory.getErrorString();
Chris@99 521 }
Chris@0 522 }
Chris@0 523
Chris@227 524 if (tryXml) {
Chris@227 525
Chris@227 526 QFile file(transformFile);
Chris@227 527 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@263 528 SVCERR << "ERROR: Failed to open transform file \""
Chris@227 529 << transformFile.toStdString() << "\" for reading" << endl;
Chris@227 530 return false;
Chris@227 531 }
Chris@227 532
Chris@227 533 QTextStream *qts = new QTextStream(&file);
Chris@227 534 QString qs = qts->readAll();
Chris@227 535 delete qts;
Chris@227 536 file.close();
Chris@227 537
Chris@227 538 Transform transform(qs);
Chris@227 539 xmlError = transform.getErrorString();
Chris@227 540
Chris@227 541 if (xmlError == "") {
Chris@227 542
Chris@227 543 if (transform.getIdentifier() == "") {
Chris@263 544 SVCERR << "ERROR: Transform file \"" << transformFile
Chris@227 545 << "\" is valid XML but defines no transform" << endl;
Chris@227 546 return false;
Chris@227 547 }
Chris@227 548
Chris@227 549 return addFeatureExtractor(transform, writers);
Chris@227 550 }
Chris@0 551 }
Chris@0 552
Chris@263 553 SVCERR << "ERROR: Transform file \"" << transformFile
Chris@227 554 << "\" could not be parsed" << endl;
Chris@227 555 if (rdfError != "") {
Chris@263 556 SVCERR << "ERROR: RDF parser reported: " << rdfError << endl;
Chris@227 557 }
Chris@227 558 if (xmlError != "") {
Chris@263 559 SVCERR << "ERROR: XML parser reported: " << xmlError << endl;
Chris@227 560 }
Chris@0 561
Chris@227 562 return false;
Chris@0 563 }
Chris@0 564
Chris@106 565 void FeatureExtractionManager::addSource(QString audioSource, bool willMultiplex)
Chris@0 566 {
Chris@263 567 SVCERR << "Have audio source: \"" << audioSource.toStdString() << "\"" << endl;
Chris@45 568
Chris@45 569 // We don't actually do anything with it here, unless it's the
Chris@45 570 // first audio source and we need it to establish default channel
Chris@45 571 // count and sample rate
Chris@45 572
Chris@45 573 if (m_channels == 0 || m_defaultSampleRate == 0) {
Chris@45 574
Chris@227 575 ProgressPrinter retrievalProgress("Retrieving first input file to determine default rate and channel count...");
Chris@45 576
Chris@263 577 FileSource source(audioSource, m_verbose ? &retrievalProgress : 0);
Chris@45 578 if (!source.isAvailable()) {
Chris@263 579 SVCERR << "ERROR: File or URL \"" << audioSource.toStdString()
Chris@117 580 << "\" could not be located";
Chris@117 581 if (source.getErrorString() != "") {
Chris@263 582 SVCERR << ": " << source.getErrorString();
Chris@117 583 }
Chris@263 584 SVCERR << endl;
Chris@45 585 throw FileNotFound(audioSource);
Chris@45 586 }
Chris@45 587
Chris@45 588 source.waitForData();
Chris@45 589
Chris@45 590 // Open to determine validity, channel count, sample rate only
Chris@45 591 // (then close, and open again later with actual desired rate &c)
Chris@45 592
Chris@286 593 AudioFileReaderFactory::Parameters params;
Chris@286 594 params.normalisation = (m_normalise ?
Chris@286 595 AudioFileReaderFactory::Normalisation::Peak :
Chris@286 596 AudioFileReaderFactory::Normalisation::None);
Chris@286 597
Chris@45 598 AudioFileReader *reader =
Chris@263 599 AudioFileReaderFactory::createReader
Chris@286 600 (source, params, m_verbose ? &retrievalProgress : 0);
Chris@45 601
Chris@45 602 if (!reader) {
Chris@45 603 throw FailedToOpenFile(audioSource);
Chris@45 604 }
Chris@45 605
Chris@263 606 if (m_verbose) retrievalProgress.done();
Chris@45 607
Chris@263 608 SVCERR << "File or URL \"" << audioSource.toStdString() << "\" opened successfully" << endl;
Chris@45 609
Chris@113 610 if (!willMultiplex) {
Chris@106 611 if (m_channels == 0) {
Chris@106 612 m_channels = reader->getChannelCount();
Chris@263 613 SVCERR << "Taking default channel count of "
Chris@227 614 << reader->getChannelCount() << " from audio file" << endl;
Chris@106 615 }
Chris@45 616 }
Chris@45 617
Chris@45 618 if (m_defaultSampleRate == 0) {
Chris@45 619 m_defaultSampleRate = reader->getNativeRate();
Chris@263 620 SVCERR << "Taking default sample rate of "
Chris@227 621 << reader->getNativeRate() << "Hz from audio file" << endl;
Chris@263 622 SVCERR << "(Note: Default may be overridden by transforms)" << endl;
Chris@45 623 }
Chris@45 624
Chris@45 625 m_readyReaders[audioSource] = reader;
Chris@45 626 }
Chris@113 627
Chris@113 628 if (willMultiplex) {
Chris@113 629 ++m_channels; // channel count is simply number of sources
Chris@263 630 SVCERR << "Multiplexing, incremented target channel count to "
Chris@113 631 << m_channels << endl;
Chris@113 632 }
Chris@45 633 }
Chris@45 634
Chris@112 635 void FeatureExtractionManager::extractFeatures(QString audioSource)
Chris@45 636 {
Chris@45 637 if (m_plugins.empty()) return;
Chris@45 638
Chris@45 639 testOutputFiles(audioSource);
Chris@45 640
Chris@0 641 if (m_sampleRate == 0) {
Chris@45 642 throw FileOperationFailed
Chris@45 643 (audioSource, "internal error: have sources and plugins, but no sample rate");
Chris@45 644 }
Chris@45 645 if (m_channels == 0) {
Chris@45 646 throw FileOperationFailed
Chris@45 647 (audioSource, "internal error: have sources and plugins, but no channel count");
Chris@0 648 }
Chris@0 649
Chris@106 650 AudioFileReader *reader = prepareReader(audioSource);
Chris@106 651 extractFeaturesFor(reader, audioSource); // Note this also deletes reader
Chris@106 652 }
Chris@102 653
Chris@106 654 void FeatureExtractionManager::extractFeaturesMultiplexed(QStringList sources)
Chris@106 655 {
Chris@106 656 if (m_plugins.empty() || sources.empty()) return;
Chris@106 657
Chris@106 658 QString nominalSource = sources[0];
Chris@106 659
Chris@106 660 testOutputFiles(nominalSource);
Chris@106 661
Chris@106 662 if (m_sampleRate == 0) {
Chris@106 663 throw FileOperationFailed
Chris@106 664 (nominalSource, "internal error: have sources and plugins, but no sample rate");
Chris@106 665 }
Chris@106 666 if (m_channels == 0) {
Chris@106 667 throw FileOperationFailed
Chris@106 668 (nominalSource, "internal error: have sources and plugins, but no channel count");
Chris@106 669 }
Chris@106 670
Chris@106 671 QList<AudioFileReader *> readers;
Chris@106 672 foreach (QString source, sources) {
Chris@106 673 AudioFileReader *reader = prepareReader(source);
Chris@106 674 readers.push_back(reader);
Chris@106 675 }
Chris@106 676
Chris@106 677 AudioFileReader *reader = new MultiplexedReader(readers);
Chris@106 678 extractFeaturesFor(reader, nominalSource); // Note this also deletes reader
Chris@106 679 }
Chris@106 680
Chris@106 681 AudioFileReader *
Chris@106 682 FeatureExtractionManager::prepareReader(QString source)
Chris@106 683 {
Chris@45 684 AudioFileReader *reader = 0;
Chris@106 685 if (m_readyReaders.contains(source)) {
Chris@106 686 reader = m_readyReaders[source];
Chris@106 687 m_readyReaders.remove(source);
Chris@106 688 if (reader->getSampleRate() != m_sampleRate) {
Chris@45 689 // can't use this; open it again
Chris@45 690 delete reader;
Chris@45 691 reader = 0;
Chris@45 692 }
Chris@45 693 }
Chris@286 694
Chris@45 695 if (!reader) {
Chris@45 696 ProgressPrinter retrievalProgress("Retrieving audio data...");
Chris@263 697 FileSource fs(source, m_verbose ? &retrievalProgress : 0);
Chris@106 698 fs.waitForData();
Chris@286 699
Chris@286 700 AudioFileReaderFactory::Parameters params;
Chris@286 701 params.targetRate = m_sampleRate;
Chris@286 702 params.normalisation = (m_normalise ?
Chris@286 703 AudioFileReaderFactory::Normalisation::Peak :
Chris@286 704 AudioFileReaderFactory::Normalisation::None);
Chris@286 705
Chris@263 706 reader = AudioFileReaderFactory::createReader
Chris@286 707 (fs, params, m_verbose ? &retrievalProgress : 0);
Chris@263 708 if (m_verbose) retrievalProgress.done();
Chris@45 709 }
Chris@286 710
Chris@102 711 if (!reader) {
Chris@107 712 throw FailedToOpenFile(source);
Chris@102 713 }
Chris@219 714 if (reader->getChannelCount() != m_channels ||
Chris@219 715 reader->getNativeRate() != m_sampleRate) {
Chris@263 716 SVCERR << "NOTE: File will be mixed or resampled for processing, to: "
Chris@219 717 << m_channels << "ch at "
Chris@219 718 << m_sampleRate << "Hz" << endl;
Chris@219 719 }
Chris@106 720 return reader;
Chris@106 721 }
Chris@45 722
Chris@106 723 void
Chris@106 724 FeatureExtractionManager::extractFeaturesFor(AudioFileReader *reader,
Chris@106 725 QString audioSource)
Chris@106 726 {
Chris@106 727 // Note: This also deletes reader
Chris@0 728
Chris@263 729 SVCERR << "Audio file \"" << audioSource.toStdString() << "\": "
Chris@45 730 << reader->getChannelCount() << "ch at "
Chris@45 731 << reader->getNativeRate() << "Hz" << endl;
Chris@11 732
Chris@0 733 // allocate audio buffers
Chris@0 734 float **data = new float *[m_channels];
Chris@0 735 for (int c = 0; c < m_channels; ++c) {
Chris@0 736 data[c] = new float[m_blockSize];
Chris@0 737 }
Chris@31 738
Chris@31 739 struct LifespanMgr { // unintrusive hack introduced to ensure
Chris@31 740 // destruction on exceptions
Chris@31 741 AudioFileReader *m_r;
Chris@31 742 int m_c;
Chris@31 743 float **m_d;
Chris@31 744 LifespanMgr(AudioFileReader *r, int c, float **d) :
Chris@31 745 m_r(r), m_c(c), m_d(d) { }
Chris@31 746 ~LifespanMgr() { destroy(); }
Chris@31 747 void destroy() {
Chris@31 748 if (!m_r) return;
Chris@31 749 delete m_r;
Chris@31 750 for (int i = 0; i < m_c; ++i) delete[] m_d[i];
Chris@31 751 delete[] m_d;
Chris@31 752 m_r = 0;
Chris@31 753 }
Chris@31 754 };
Chris@31 755 LifespanMgr lifemgr(reader, m_channels, data);
Chris@0 756
Chris@0 757 size_t frameCount = reader->getFrameCount();
Chris@0 758
Chris@263 759 SVDEBUG << "FeatureExtractionManager: file has " << frameCount << " frames" << endl;
Chris@0 760
Chris@99 761 int earliestStartFrame = 0;
Chris@99 762 int latestEndFrame = frameCount;
Chris@99 763 bool haveExtents = false;
Chris@99 764
Chris@109 765 foreach (Plugin *plugin, m_orderedPlugins) {
Chris@0 766
Chris@109 767 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 768
Chris@263 769 SVDEBUG << "FeatureExtractionManager: Calling reset on " << plugin << endl;
Chris@0 770 plugin->reset();
Chris@0 771
Chris@0 772 for (TransformWriterMap::iterator ti = pi->second.begin();
Chris@0 773 ti != pi->second.end(); ++ti) {
Chris@0 774
Chris@0 775 const Transform &transform = ti->first;
Chris@0 776
Chris@99 777 int startFrame = RealTime::realTime2Frame
Chris@99 778 (transform.getStartTime(), m_sampleRate);
Chris@99 779 int duration = RealTime::realTime2Frame
Chris@99 780 (transform.getDuration(), m_sampleRate);
Chris@99 781 if (duration == 0) {
Chris@99 782 duration = frameCount - startFrame;
Chris@99 783 }
Chris@99 784
Chris@99 785 if (!haveExtents || startFrame < earliestStartFrame) {
Chris@99 786 earliestStartFrame = startFrame;
Chris@99 787 }
Chris@99 788 if (!haveExtents || startFrame + duration > latestEndFrame) {
Chris@99 789 latestEndFrame = startFrame + duration;
Chris@99 790 }
Chris@263 791
Chris@131 792 /*
Chris@263 793 SVDEBUG << "startFrame for transform " << startFrame << endl;
Chris@263 794 SVDEBUG << "duration for transform " << duration << endl;
Chris@263 795 SVDEBUG << "earliestStartFrame becomes " << earliestStartFrame << endl;
Chris@263 796 SVDEBUG << "latestEndFrame becomes " << latestEndFrame << endl;
Chris@131 797 */
Chris@99 798 haveExtents = true;
Chris@0 799
Chris@0 800 string outputId = transform.getOutput().toStdString();
Chris@0 801 if (m_pluginOutputs[plugin].find(outputId) ==
Chris@0 802 m_pluginOutputs[plugin].end()) {
Chris@129 803 // We shouldn't actually reach this point:
Chris@129 804 // addFeatureExtractor tests whether the output exists
Chris@263 805 SVCERR << "ERROR: Nonexistent plugin output \"" << outputId << "\" requested for transform \""
Chris@0 806 << transform.getIdentifier().toStdString() << "\", ignoring this transform"
Chris@0 807 << endl;
Chris@263 808
Chris@263 809 SVDEBUG << "Known outputs for all plugins are as follows:" << endl;
Chris@0 810 for (PluginOutputMap::const_iterator k = m_pluginOutputs.begin();
Chris@0 811 k != m_pluginOutputs.end(); ++k) {
Chris@263 812 SVDEBUG << "Plugin " << k->first << ": ";
Chris@0 813 if (k->second.empty()) {
Chris@263 814 SVDEBUG << "(none)";
Chris@0 815 }
Chris@0 816 for (OutputMap::const_iterator i = k->second.begin();
Chris@0 817 i != k->second.end(); ++i) {
Chris@263 818 SVDEBUG << "\"" << i->first << "\" ";
Chris@0 819 }
Chris@263 820 SVDEBUG << endl;
Chris@0 821 }
Chris@0 822 }
Chris@0 823 }
Chris@0 824 }
Chris@0 825
Chris@99 826 int startFrame = earliestStartFrame;
Chris@99 827 int endFrame = latestEndFrame;
Chris@0 828
Chris@109 829 foreach (Plugin *plugin, m_orderedPlugins) {
Chris@109 830
Chris@109 831 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 832
Chris@0 833 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 834 ti != pi->second.end(); ++ti) {
Chris@0 835
Chris@0 836 const vector<FeatureWriter *> &writers = ti->second;
Chris@0 837
Chris@0 838 for (int j = 0; j < (int)writers.size(); ++j) {
Chris@0 839 FeatureWriter::TrackMetadata m;
Chris@0 840 m.title = reader->getTitle();
Chris@0 841 m.maker = reader->getMaker();
Chris@208 842 m.duration = RealTime::frame2RealTime(reader->getFrameCount(),
Chris@208 843 reader->getSampleRate());
Chris@208 844 writers[j]->setTrackMetadata(audioSource, m);
Chris@0 845 }
Chris@0 846 }
Chris@0 847 }
Chris@0 848
Chris@0 849 ProgressPrinter extractionProgress("Extracting and writing features...");
Chris@0 850 int progress = 0;
Chris@0 851
Chris@99 852 for (int i = startFrame; i < endFrame; i += m_blockSize) {
Chris@0 853
Chris@0 854 //!!! inefficient, although much of the inefficiency may be
Chris@99 855 // susceptible to compiler optimisation
Chris@0 856
Chris@258 857 auto frames = reader->getInterleavedFrames(i, m_blockSize);
Chris@0 858
Chris@0 859 // We have to do our own channel handling here; we can't just
Chris@0 860 // leave it to the plugin adapter because the same plugin
Chris@0 861 // adapter may have to serve for input files with various
Chris@0 862 // numbers of channels (so the adapter is simply configured
Chris@34 863 // with a fixed channel count).
Chris@0 864
Chris@0 865 int rc = reader->getChannelCount();
Chris@0 866
Chris@34 867 // m_channels is the number of channels we need for the plugin
Chris@34 868
Chris@34 869 int index;
Chris@34 870 int fc = (int)frames.size();
Chris@46 871
Chris@34 872 if (m_channels == 1) { // only case in which we can sensibly mix down
Chris@34 873 for (int j = 0; j < m_blockSize; ++j) {
Chris@34 874 data[0][j] = 0.f;
Chris@34 875 }
Chris@34 876 for (int c = 0; c < rc; ++c) {
Chris@34 877 for (int j = 0; j < m_blockSize; ++j) {
Chris@0 878 index = j * rc + c;
Chris@34 879 if (index < fc) data[0][j] += frames[index];
Chris@0 880 }
Chris@0 881 }
Chris@34 882 for (int j = 0; j < m_blockSize; ++j) {
Chris@34 883 data[0][j] /= rc;
Chris@34 884 }
Chris@34 885 } else {
Chris@34 886 for (int c = 0; c < m_channels; ++c) {
Chris@34 887 for (int j = 0; j < m_blockSize; ++j) {
Chris@34 888 data[c][j] = 0.f;
Chris@34 889 }
Chris@34 890 if (c < rc) {
Chris@34 891 for (int j = 0; j < m_blockSize; ++j) {
Chris@34 892 index = j * rc + c;
Chris@34 893 if (index < fc) data[c][j] += frames[index];
Chris@34 894 }
Chris@34 895 }
Chris@34 896 }
Chris@34 897 }
Chris@0 898
Chris@0 899 Vamp::RealTime timestamp = Vamp::RealTime::frame2RealTime
Chris@0 900 (i, m_sampleRate);
Chris@0 901
Chris@109 902 foreach (Plugin *plugin, m_orderedPlugins) {
Chris@0 903
Chris@109 904 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@123 905
Chris@123 906 // Skip any plugin none of whose transforms have come
Chris@123 907 // around yet. (Though actually, all transforms for a
Chris@123 908 // given plugin must have the same start time -- they can
Chris@123 909 // only differ in output and summary type.)
Chris@123 910 bool inRange = false;
Chris@123 911 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@123 912 ti != pi->second.end(); ++ti) {
Chris@123 913 int startFrame = RealTime::realTime2Frame
Chris@123 914 (ti->first.getStartTime(), m_sampleRate);
Chris@123 915 if (i >= startFrame || i + m_blockSize > startFrame) {
Chris@123 916 inRange = true;
Chris@123 917 break;
Chris@123 918 }
Chris@123 919 }
Chris@123 920 if (!inRange) {
Chris@123 921 continue;
Chris@123 922 }
Chris@123 923
Chris@0 924 Plugin::FeatureSet featureSet = plugin->process(data, timestamp);
Chris@0 925
Chris@0 926 if (!m_summariesOnly) {
Chris@0 927 writeFeatures(audioSource, plugin, featureSet);
Chris@0 928 }
Chris@0 929 }
Chris@0 930
Chris@0 931 int pp = progress;
Chris@6 932 progress = int(((i - startFrame) * 100.0) / (endFrame - startFrame) + 0.1);
Chris@263 933 if (progress > pp && m_verbose) extractionProgress.setProgress(progress);
Chris@0 934 }
Chris@10 935
Chris@263 936 SVDEBUG << "FeatureExtractionManager: deleting audio file reader" << endl;
Chris@12 937
Chris@31 938 lifemgr.destroy(); // deletes reader, data
Chris@57 939
Chris@109 940 foreach (Plugin *plugin, m_orderedPlugins) {
Chris@57 941
Chris@109 942 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 943 Plugin::FeatureSet featureSet = plugin->getRemainingFeatures();
Chris@0 944
Chris@0 945 if (!m_summariesOnly) {
Chris@0 946 writeFeatures(audioSource, plugin, featureSet);
Chris@0 947 }
Chris@0 948
Chris@0 949 if (!m_summaries.empty()) {
Chris@233 950 // Summaries requested on the command line, for all transforms
Chris@0 951 PluginSummarisingAdapter *adapter =
Chris@0 952 dynamic_cast<PluginSummarisingAdapter *>(plugin);
Chris@0 953 if (!adapter) {
Chris@263 954 SVCERR << "WARNING: Summaries requested, but plugin is not a summarising adapter" << endl;
Chris@0 955 } else {
Chris@0 956 for (SummaryNameSet::const_iterator sni = m_summaries.begin();
Chris@0 957 sni != m_summaries.end(); ++sni) {
Chris@0 958 featureSet.clear();
Chris@0 959 //!!! problem here -- we are requesting summaries
Chris@0 960 //!!! for all outputs, but they in principle have
Chris@0 961 //!!! different averaging requirements depending
Chris@0 962 //!!! on whether their features have duration or
Chris@0 963 //!!! not
Chris@0 964 featureSet = adapter->getSummaryForAllOutputs
Chris@0 965 (getSummaryType(*sni),
Chris@0 966 PluginSummarisingAdapter::ContinuousTimeAverage);
Chris@233 967 writeFeatures(audioSource, plugin, featureSet,
Chris@0 968 Transform::stringToSummaryType(sni->c_str()));
Chris@0 969 }
Chris@0 970 }
Chris@0 971 }
Chris@0 972
Chris@233 973 // Summaries specified in transform definitions themselves
Chris@0 974 writeSummaries(audioSource, plugin);
Chris@0 975 }
Chris@0 976
Chris@263 977 if (m_verbose) extractionProgress.done();
Chris@3 978
Chris@0 979 finish();
Chris@0 980
Chris@0 981 TempDirectory::getInstance()->cleanup();
Chris@0 982 }
Chris@0 983
Chris@0 984 void
Chris@0 985 FeatureExtractionManager::writeSummaries(QString audioSource, Plugin *plugin)
Chris@0 986 {
Chris@0 987 // caller should have ensured plugin is in m_plugins
Chris@0 988 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 989
Chris@0 990 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 991 ti != pi->second.end(); ++ti) {
Chris@0 992
Chris@0 993 const Transform &transform = ti->first;
Chris@0 994
Chris@263 995 SVDEBUG << "FeatureExtractionManager::writeSummaries: plugin is " << plugin
Chris@263 996 << ", found transform: " << transform.toXmlString() << endl;
Chris@233 997
Chris@0 998 Transform::SummaryType summaryType = transform.getSummaryType();
Chris@0 999 PluginSummarisingAdapter::SummaryType pType =
Chris@0 1000 (PluginSummarisingAdapter::SummaryType)summaryType;
Chris@0 1001
Chris@0 1002 if (transform.getSummaryType() == Transform::NoSummary) {
Chris@263 1003 SVDEBUG << "FeatureExtractionManager::writeSummaries: no summary for this transform" << endl;
Chris@0 1004 continue;
Chris@0 1005 }
Chris@0 1006
Chris@0 1007 PluginSummarisingAdapter *adapter =
Chris@0 1008 dynamic_cast<PluginSummarisingAdapter *>(plugin);
Chris@0 1009 if (!adapter) {
Chris@263 1010 SVCERR << "FeatureExtractionManager::writeSummaries: INTERNAL ERROR: Summary requested for transform, but plugin is not a summarising adapter" << endl;
Chris@0 1011 continue;
Chris@0 1012 }
Chris@0 1013
Chris@0 1014 Plugin::FeatureSet featureSet = adapter->getSummaryForAllOutputs
Chris@0 1015 (pType, PluginSummarisingAdapter::ContinuousTimeAverage);
Chris@0 1016
Chris@263 1017 SVDEBUG << "summary type " << int(pType) << " for transform:" << endl << transform.toXmlString().toStdString()<< endl << "... feature set with " << featureSet.size() << " elts" << endl;
Chris@0 1018
Chris@0 1019 writeFeatures(audioSource, plugin, featureSet, summaryType);
Chris@0 1020 }
Chris@0 1021 }
Chris@0 1022
Chris@0 1023 void FeatureExtractionManager::writeFeatures(QString audioSource,
Chris@0 1024 Plugin *plugin,
Chris@0 1025 const Plugin::FeatureSet &features,
Chris@0 1026 Transform::SummaryType summaryType)
Chris@0 1027 {
Chris@0 1028 // caller should have ensured plugin is in m_plugins
Chris@0 1029 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 1030
Chris@233 1031 // Write features from the feature set passed in, according to the
Chris@233 1032 // transforms listed for the given plugin with the given summary type
Chris@233 1033
Chris@0 1034 for (TransformWriterMap::const_iterator ti = pi->second.begin();
Chris@0 1035 ti != pi->second.end(); ++ti) {
Chris@0 1036
Chris@0 1037 const Transform &transform = ti->first;
Chris@0 1038 const vector<FeatureWriter *> &writers = ti->second;
Chris@0 1039
Chris@263 1040 // SVDEBUG << "writeFeatures: plugin " << plugin << " has transform: " << transform.toXmlString() << endl;
Chris@233 1041
Chris@233 1042 if (transform.getSummaryType() == Transform::NoSummary &&
Chris@233 1043 !m_summaries.empty()) {
Chris@265 1044 SVDEBUG << "writeFeatures: transform has no summary, but summaries requested on command line, so going for it anyway" << endl;
Chris@233 1045 } else if (transform.getSummaryType() != summaryType) {
Chris@233 1046 // Either we're not writing a summary and the transform
Chris@233 1047 // has one, or we're writing a summary but the transform
Chris@233 1048 // has none or a different one; either way, skip it
Chris@265 1049 SVDEBUG << "writeFeatures: transform summary type " << transform.getSummaryType() << " differs from passed-in one " << summaryType << ", skipping" << endl;
Chris@0 1050 continue;
Chris@0 1051 }
Chris@0 1052
Chris@0 1053 string outputId = transform.getOutput().toStdString();
Chris@0 1054
Chris@0 1055 if (m_pluginOutputs[plugin].find(outputId) ==
Chris@0 1056 m_pluginOutputs[plugin].end()) {
Chris@0 1057 continue;
Chris@0 1058 }
Chris@0 1059
Chris@0 1060 const Plugin::OutputDescriptor &desc =
Chris@0 1061 m_pluginOutputs[plugin][outputId];
Chris@0 1062
Chris@0 1063 int outputIndex = m_pluginOutputIndices[outputId];
Chris@0 1064 Plugin::FeatureSet::const_iterator fsi = features.find(outputIndex);
Chris@0 1065 if (fsi == features.end()) continue;
Chris@0 1066
Chris@263 1067 // SVDEBUG << "this transform has " << writers.size() << " writer(s)" << endl;
Chris@233 1068
Chris@0 1069 for (int j = 0; j < (int)writers.size(); ++j) {
Chris@0 1070 writers[j]->write
Chris@0 1071 (audioSource, transform, desc, fsi->second,
Chris@0 1072 Transform::summaryTypeToString(summaryType).toStdString());
Chris@0 1073 }
Chris@0 1074 }
Chris@0 1075 }
Chris@0 1076
Chris@31 1077 void FeatureExtractionManager::testOutputFiles(QString audioSource)
Chris@31 1078 {
Chris@31 1079 for (PluginMap::iterator pi = m_plugins.begin();
Chris@31 1080 pi != m_plugins.end(); ++pi) {
Chris@31 1081
Chris@31 1082 for (TransformWriterMap::iterator ti = pi->second.begin();
Chris@31 1083 ti != pi->second.end(); ++ti) {
Chris@31 1084
Chris@31 1085 vector<FeatureWriter *> &writers = ti->second;
Chris@31 1086
Chris@31 1087 for (int i = 0; i < (int)writers.size(); ++i) {
Chris@31 1088 writers[i]->testOutputFile(audioSource, ti->first.getIdentifier());
Chris@31 1089 }
Chris@31 1090 }
Chris@31 1091 }
Chris@31 1092 }
Chris@31 1093
Chris@0 1094 void FeatureExtractionManager::finish()
Chris@0 1095 {
Chris@109 1096 foreach (Plugin *plugin, m_orderedPlugins) {
Chris@109 1097
Chris@109 1098 PluginMap::iterator pi = m_plugins.find(plugin);
Chris@0 1099
Chris@0 1100 for (TransformWriterMap::iterator ti = pi->second.begin();
Chris@0 1101 ti != pi->second.end(); ++ti) {
Chris@0 1102
Chris@0 1103 vector<FeatureWriter *> &writers = ti->second;
Chris@0 1104
Chris@0 1105 for (int i = 0; i < (int)writers.size(); ++i) {
Chris@0 1106 writers[i]->flush();
Chris@0 1107 writers[i]->finish();
Chris@0 1108 }
Chris@0 1109 }
Chris@0 1110 }
Chris@0 1111 }
Chris@0 1112
Chris@0 1113 void FeatureExtractionManager::print(Transform transform) const
Chris@0 1114 {
Chris@0 1115 QString qs;
Chris@0 1116 QTextStream qts(&qs);
Chris@0 1117 transform.toXml(qts);
Chris@0 1118 cerr << qs.toStdString() << endl;
Chris@0 1119 }