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