Chris@0
|
1
|
Chris@49
|
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
3
|
Chris@0
|
4 /*
|
Chris@52
|
5 Sonic Visualiser
|
Chris@52
|
6 An audio file viewer and annotation editor.
|
Chris@52
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@52
|
8 This file copyright 2006 Chris Cannam.
|
Chris@0
|
9
|
Chris@52
|
10 This program is free software; you can redistribute it and/or
|
Chris@52
|
11 modify it under the terms of the GNU General Public License as
|
Chris@52
|
12 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
13 License, or (at your option) any later version. See the file
|
Chris@52
|
14 COPYING included with this distribution for more information.
|
Chris@0
|
15 */
|
Chris@0
|
16
|
Chris@0
|
17 #include "FeatureExtractionPluginTransform.h"
|
Chris@0
|
18
|
Chris@0
|
19 #include "plugin/FeatureExtractionPluginFactory.h"
|
Chris@0
|
20 #include "plugin/FeatureExtractionPlugin.h"
|
Chris@0
|
21
|
Chris@0
|
22 #include "base/Model.h"
|
Chris@0
|
23 #include "model/SparseOneDimensionalModel.h"
|
Chris@0
|
24 #include "model/SparseTimeValueModel.h"
|
Chris@0
|
25 #include "model/DenseThreeDimensionalModel.h"
|
Chris@0
|
26 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
27
|
Chris@0
|
28 #include <iostream>
|
Chris@0
|
29
|
Chris@0
|
30 FeatureExtractionPluginTransform::FeatureExtractionPluginTransform(Model *inputModel,
|
Chris@0
|
31 QString pluginId,
|
Chris@64
|
32 int channel,
|
Chris@56
|
33 QString configurationXml,
|
Chris@0
|
34 QString outputName) :
|
Chris@0
|
35 Transform(inputModel),
|
Chris@0
|
36 m_plugin(0),
|
Chris@64
|
37 m_channel(channel),
|
Chris@0
|
38 m_descriptor(0),
|
Chris@0
|
39 m_outputFeatureNo(0)
|
Chris@0
|
40 {
|
Chris@0
|
41 std::cerr << "FeatureExtractionPluginTransform::FeatureExtractionPluginTransform: plugin " << pluginId.toStdString() << ", outputName " << outputName.toStdString() << std::endl;
|
Chris@0
|
42
|
Chris@0
|
43 FeatureExtractionPluginFactory *factory =
|
Chris@0
|
44 FeatureExtractionPluginFactory::instanceFor(pluginId);
|
Chris@0
|
45
|
Chris@0
|
46 if (!factory) {
|
Chris@0
|
47 std::cerr << "FeatureExtractionPluginTransform: No factory available for plugin id \""
|
Chris@0
|
48 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@0
|
49 return;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 m_plugin = factory->instantiatePlugin(pluginId, m_input->getSampleRate());
|
Chris@0
|
53
|
Chris@0
|
54 if (!m_plugin) {
|
Chris@0
|
55 std::cerr << "FeatureExtractionPluginTransform: Failed to instantiate plugin \""
|
Chris@0
|
56 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@0
|
57 return;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@56
|
60 if (configurationXml != "") {
|
Chris@56
|
61 m_plugin->setParametersFromXml(configurationXml);
|
Chris@56
|
62 }
|
Chris@56
|
63
|
Chris@0
|
64 FeatureExtractionPlugin::OutputList outputs =
|
Chris@0
|
65 m_plugin->getOutputDescriptors();
|
Chris@0
|
66
|
Chris@0
|
67 if (outputs.empty()) {
|
Chris@0
|
68 std::cerr << "FeatureExtractionPluginTransform: Plugin \""
|
Chris@0
|
69 << pluginId.toStdString() << "\" has no outputs" << std::endl;
|
Chris@0
|
70 return;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 for (size_t i = 0; i < outputs.size(); ++i) {
|
Chris@0
|
74 if (outputName == "" || outputs[i].name == outputName.toStdString()) {
|
Chris@0
|
75 m_outputFeatureNo = i;
|
Chris@0
|
76 m_descriptor = new FeatureExtractionPlugin::OutputDescriptor
|
Chris@0
|
77 (outputs[i]);
|
Chris@0
|
78 break;
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if (!m_descriptor) {
|
Chris@0
|
83 std::cerr << "FeatureExtractionPluginTransform: Plugin \""
|
Chris@0
|
84 << pluginId.toStdString() << "\" has no output named \""
|
Chris@0
|
85 << outputName.toStdString() << "\"" << std::endl;
|
Chris@0
|
86 return;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 std::cerr << "FeatureExtractionPluginTransform: output sample type "
|
Chris@0
|
90 << m_descriptor->sampleType << std::endl;
|
Chris@0
|
91
|
Chris@0
|
92 int valueCount = 1;
|
Chris@0
|
93 float minValue = 0.0, maxValue = 0.0;
|
Chris@0
|
94
|
Chris@0
|
95 if (m_descriptor->hasFixedValueCount) {
|
Chris@0
|
96 valueCount = m_descriptor->valueCount;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 if (valueCount > 0 && m_descriptor->hasKnownExtents) {
|
Chris@0
|
100 minValue = m_descriptor->minValue;
|
Chris@0
|
101 maxValue = m_descriptor->maxValue;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 size_t modelRate = m_input->getSampleRate();
|
Chris@0
|
105 size_t modelResolution = 1;
|
Chris@0
|
106
|
Chris@0
|
107 switch (m_descriptor->sampleType) {
|
Chris@0
|
108
|
Chris@0
|
109 case FeatureExtractionPlugin::OutputDescriptor::VariableSampleRate:
|
Chris@0
|
110 if (m_descriptor->sampleRate != 0.0) {
|
Chris@0
|
111 modelResolution = size_t(modelRate / m_descriptor->sampleRate + 0.001);
|
Chris@0
|
112 }
|
Chris@0
|
113 break;
|
Chris@0
|
114
|
Chris@0
|
115 case FeatureExtractionPlugin::OutputDescriptor::OneSamplePerStep:
|
Chris@0
|
116 modelResolution = m_plugin->getPreferredStepSize();
|
Chris@0
|
117 break;
|
Chris@0
|
118
|
Chris@0
|
119 case FeatureExtractionPlugin::OutputDescriptor::FixedSampleRate:
|
Chris@0
|
120 modelRate = m_descriptor->sampleRate;
|
Chris@0
|
121 break;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 if (valueCount == 0) {
|
Chris@0
|
125
|
Chris@20
|
126 m_output = new SparseOneDimensionalModel(modelRate, modelResolution,
|
Chris@20
|
127 false);
|
Chris@0
|
128
|
Chris@0
|
129 } else if (valueCount == 1 ||
|
Chris@0
|
130
|
Chris@0
|
131 // We don't have a sparse 3D model
|
Chris@0
|
132 m_descriptor->sampleType ==
|
Chris@0
|
133 FeatureExtractionPlugin::OutputDescriptor::VariableSampleRate) {
|
Chris@0
|
134
|
Chris@63
|
135 SparseTimeValueModel *model = new SparseTimeValueModel
|
Chris@63
|
136 (modelRate, modelResolution, minValue, maxValue, false);
|
Chris@63
|
137 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
|
Chris@63
|
138
|
Chris@63
|
139 m_output = model;
|
Chris@0
|
140
|
Chris@0
|
141 } else {
|
Chris@0
|
142
|
Chris@0
|
143 m_output = new DenseThreeDimensionalModel(modelRate, modelResolution,
|
Chris@19
|
144 valueCount, false);
|
Chris@20
|
145
|
Chris@20
|
146 if (!m_descriptor->valueNames.empty()) {
|
Chris@20
|
147 std::vector<QString> names;
|
Chris@20
|
148 for (size_t i = 0; i < m_descriptor->valueNames.size(); ++i) {
|
Chris@20
|
149 names.push_back(m_descriptor->valueNames[i].c_str());
|
Chris@20
|
150 }
|
Chris@20
|
151 (dynamic_cast<DenseThreeDimensionalModel *>(m_output))
|
Chris@20
|
152 ->setBinNames(names);
|
Chris@20
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 FeatureExtractionPluginTransform::~FeatureExtractionPluginTransform()
|
Chris@0
|
158 {
|
Chris@0
|
159 delete m_plugin;
|
Chris@0
|
160 delete m_descriptor;
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 DenseTimeValueModel *
|
Chris@0
|
164 FeatureExtractionPluginTransform::getInput()
|
Chris@0
|
165 {
|
Chris@0
|
166 DenseTimeValueModel *dtvm =
|
Chris@0
|
167 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@0
|
168 if (!dtvm) {
|
Chris@0
|
169 std::cerr << "FeatureExtractionPluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
|
Chris@0
|
170 }
|
Chris@0
|
171 return dtvm;
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 void
|
Chris@0
|
175 FeatureExtractionPluginTransform::run()
|
Chris@0
|
176 {
|
Chris@0
|
177 DenseTimeValueModel *input = getInput();
|
Chris@0
|
178 if (!input) return;
|
Chris@0
|
179
|
Chris@0
|
180 if (!m_output) return;
|
Chris@0
|
181
|
Chris@0
|
182 size_t channelCount = input->getChannelCount();
|
Chris@0
|
183 if (m_plugin->getMaxChannelCount() < channelCount) {
|
Chris@0
|
184 channelCount = 1;
|
Chris@0
|
185 }
|
Chris@0
|
186 if (m_plugin->getMinChannelCount() > channelCount) {
|
Chris@0
|
187 std::cerr << "FeatureExtractionPluginTransform::run: "
|
Chris@0
|
188 << "Can't provide enough channels to plugin (plugin min "
|
Chris@0
|
189 << m_plugin->getMinChannelCount() << ", max "
|
Chris@0
|
190 << m_plugin->getMaxChannelCount() << ", input model has "
|
Chris@0
|
191 << input->getChannelCount() << ")" << std::endl;
|
Chris@0
|
192 return;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 size_t sampleRate = m_input->getSampleRate();
|
Chris@0
|
196
|
Chris@0
|
197 size_t stepSize = m_plugin->getPreferredStepSize();
|
Chris@0
|
198 size_t blockSize = m_plugin->getPreferredBlockSize();
|
Chris@0
|
199
|
Chris@0
|
200 m_plugin->initialise(channelCount, stepSize, blockSize);
|
Chris@0
|
201
|
Chris@0
|
202 float **buffers = new float*[channelCount];
|
Chris@0
|
203 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@0
|
204 buffers[ch] = new float[blockSize];
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 size_t startFrame = m_input->getStartFrame();
|
Chris@0
|
208 size_t endFrame = m_input->getEndFrame();
|
Chris@0
|
209 size_t blockFrame = startFrame;
|
Chris@0
|
210
|
Chris@0
|
211 size_t prevCompletion = 0;
|
Chris@0
|
212
|
Chris@0
|
213 while (blockFrame < endFrame) {
|
Chris@0
|
214
|
Chris@0
|
215 // std::cerr << "FeatureExtractionPluginTransform::run: blockFrame "
|
Chris@0
|
216 // << blockFrame << std::endl;
|
Chris@0
|
217
|
Chris@0
|
218 size_t completion =
|
Chris@0
|
219 (((blockFrame - startFrame) / stepSize) * 99) /
|
Chris@0
|
220 ( (endFrame - startFrame) / stepSize);
|
Chris@0
|
221
|
Chris@0
|
222 // channelCount is either m_input->channelCount or 1
|
Chris@0
|
223
|
Chris@0
|
224 size_t got = 0;
|
Chris@0
|
225
|
Chris@0
|
226 if (channelCount == 1) {
|
Chris@0
|
227 got = input->getValues
|
Chris@64
|
228 (m_channel, blockFrame, blockFrame + blockSize, buffers[0]);
|
Chris@0
|
229 while (got < blockSize) {
|
Chris@0
|
230 buffers[0][got++] = 0.0;
|
Chris@0
|
231 }
|
Chris@0
|
232 } else {
|
Chris@0
|
233 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@0
|
234 got = input->getValues
|
Chris@0
|
235 (ch, blockFrame, blockFrame + blockSize, buffers[ch]);
|
Chris@0
|
236 while (got < blockSize) {
|
Chris@0
|
237 buffers[ch][got++] = 0.0;
|
Chris@0
|
238 }
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 FeatureExtractionPlugin::FeatureSet features = m_plugin->process
|
Chris@0
|
243 (buffers, RealTime::frame2RealTime(blockFrame, sampleRate));
|
Chris@0
|
244
|
Chris@0
|
245 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@0
|
246 FeatureExtractionPlugin::Feature feature =
|
Chris@0
|
247 features[m_outputFeatureNo][fi];
|
Chris@0
|
248 addFeature(blockFrame, feature);
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 if (blockFrame == startFrame || completion > prevCompletion) {
|
Chris@0
|
252 setCompletion(completion);
|
Chris@0
|
253 prevCompletion = completion;
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 blockFrame += stepSize;
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 FeatureExtractionPlugin::FeatureSet features = m_plugin->getRemainingFeatures();
|
Chris@0
|
260
|
Chris@0
|
261 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
|
Chris@0
|
262 FeatureExtractionPlugin::Feature feature =
|
Chris@0
|
263 features[m_outputFeatureNo][fi];
|
Chris@0
|
264 addFeature(blockFrame, feature);
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 setCompletion(100);
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270
|
Chris@0
|
271 void
|
Chris@0
|
272 FeatureExtractionPluginTransform::addFeature(size_t blockFrame,
|
Chris@0
|
273 const FeatureExtractionPlugin::Feature &feature)
|
Chris@0
|
274 {
|
Chris@0
|
275 size_t inputRate = m_input->getSampleRate();
|
Chris@0
|
276
|
Chris@0
|
277 // std::cerr << "FeatureExtractionPluginTransform::addFeature("
|
Chris@0
|
278 // << blockFrame << ")" << std::endl;
|
Chris@0
|
279
|
Chris@0
|
280 int valueCount = 1;
|
Chris@0
|
281 if (m_descriptor->hasFixedValueCount) {
|
Chris@0
|
282 valueCount = m_descriptor->valueCount;
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 size_t frame = blockFrame;
|
Chris@0
|
286
|
Chris@0
|
287 if (m_descriptor->sampleType ==
|
Chris@0
|
288 FeatureExtractionPlugin::OutputDescriptor::VariableSampleRate) {
|
Chris@0
|
289
|
Chris@0
|
290 if (!feature.hasTimestamp) {
|
Chris@0
|
291 std::cerr
|
Chris@0
|
292 << "WARNING: FeatureExtractionPluginTransform::addFeature: "
|
Chris@0
|
293 << "Feature has variable sample rate but no timestamp!"
|
Chris@0
|
294 << std::endl;
|
Chris@0
|
295 return;
|
Chris@0
|
296 } else {
|
Chris@0
|
297 frame = RealTime::realTime2Frame(feature.timestamp, inputRate);
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 } else if (m_descriptor->sampleType ==
|
Chris@0
|
301 FeatureExtractionPlugin::OutputDescriptor::FixedSampleRate) {
|
Chris@0
|
302
|
Chris@0
|
303 if (feature.hasTimestamp) {
|
Chris@0
|
304 //!!! warning: sampleRate may be non-integral
|
Chris@0
|
305 frame = RealTime::realTime2Frame(feature.timestamp,
|
Chris@0
|
306 m_descriptor->sampleRate);
|
Chris@0
|
307 } else {
|
Chris@0
|
308 frame = m_output->getEndFrame() + 1;
|
Chris@0
|
309 }
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 if (valueCount == 0) {
|
Chris@0
|
313
|
Chris@0
|
314 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
|
Chris@0
|
315 if (!model) return;
|
Chris@0
|
316 model->addPoint(SparseOneDimensionalModel::Point(frame, feature.label.c_str()));
|
Chris@0
|
317
|
Chris@0
|
318 } else if (valueCount == 1 ||
|
Chris@0
|
319 m_descriptor->sampleType ==
|
Chris@0
|
320 FeatureExtractionPlugin::OutputDescriptor::VariableSampleRate) {
|
Chris@0
|
321
|
Chris@0
|
322 float value = 0.0;
|
Chris@0
|
323 if (feature.values.size() > 0) value = feature.values[0];
|
Chris@0
|
324
|
Chris@0
|
325 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
|
Chris@0
|
326 if (!model) return;
|
Chris@0
|
327 model->addPoint(SparseTimeValueModel::Point(frame, value, feature.label.c_str()));
|
Chris@0
|
328
|
Chris@0
|
329 } else {
|
Chris@0
|
330
|
Chris@0
|
331 DenseThreeDimensionalModel::BinValueSet values = feature.values;
|
Chris@0
|
332
|
Chris@0
|
333 DenseThreeDimensionalModel *model = getOutput<DenseThreeDimensionalModel>();
|
Chris@0
|
334 if (!model) return;
|
Chris@0
|
335
|
Chris@0
|
336 model->setBinValues(frame, values);
|
Chris@0
|
337 }
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 void
|
Chris@0
|
341 FeatureExtractionPluginTransform::setCompletion(int completion)
|
Chris@0
|
342 {
|
Chris@0
|
343 int valueCount = 1;
|
Chris@0
|
344 if (m_descriptor->hasFixedValueCount) {
|
Chris@0
|
345 valueCount = m_descriptor->valueCount;
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 if (valueCount == 0) {
|
Chris@0
|
349
|
Chris@0
|
350 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
|
Chris@0
|
351 if (!model) return;
|
Chris@0
|
352 model->setCompletion(completion);
|
Chris@0
|
353
|
Chris@0
|
354 } else if (valueCount == 1 ||
|
Chris@0
|
355 m_descriptor->sampleType ==
|
Chris@0
|
356 FeatureExtractionPlugin::OutputDescriptor::VariableSampleRate) {
|
Chris@0
|
357
|
Chris@0
|
358 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
|
Chris@0
|
359 if (!model) return;
|
Chris@0
|
360 model->setCompletion(completion);
|
Chris@0
|
361
|
Chris@0
|
362 } else {
|
Chris@0
|
363
|
Chris@19
|
364 DenseThreeDimensionalModel *model = getOutput<DenseThreeDimensionalModel>();
|
Chris@19
|
365 if (!model) return;
|
Chris@19
|
366 model->setCompletion(completion);
|
Chris@0
|
367 }
|
Chris@0
|
368 }
|
Chris@0
|
369
|