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