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