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