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