annotate transform/FeatureExtractionPluginTransform.cpp @ 142:0ba66b160a02

* Add frequency plot to window shape preference -- just because... * Fix some window shapes
author Chris Cannam
date Mon, 24 Jul 2006 14:36:35 +0000
parents b18b07474e11
children 82f529a08cf3
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@52 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@52 9 This program is free software; you can redistribute it and/or
Chris@52 10 modify it under the terms of the GNU General Public License as
Chris@52 11 published by the Free Software Foundation; either version 2 of the
Chris@52 12 License, or (at your option) any later version. See the file
Chris@52 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "FeatureExtractionPluginTransform.h"
Chris@0 17
Chris@0 18 #include "plugin/FeatureExtractionPluginFactory.h"
Chris@66 19 #include "plugin/PluginXml.h"
Chris@66 20 #include "vamp-sdk/Plugin.h"
Chris@0 21
Chris@0 22 #include "base/Model.h"
Chris@67 23 #include "base/Window.h"
Chris@0 24 #include "model/SparseOneDimensionalModel.h"
Chris@0 25 #include "model/SparseTimeValueModel.h"
Chris@0 26 #include "model/DenseThreeDimensionalModel.h"
Chris@0 27 #include "model/DenseTimeValueModel.h"
Chris@115 28 #include "model/NoteModel.h"
Chris@132 29 #include "fileio/FFTFuzzyAdapter.h"
Chris@128 30
Chris@67 31 #include <fftw3.h>
Chris@67 32
Chris@0 33 #include <iostream>
Chris@0 34
Chris@0 35 FeatureExtractionPluginTransform::FeatureExtractionPluginTransform(Model *inputModel,
Chris@0 36 QString pluginId,
Chris@64 37 int channel,
Chris@56 38 QString configurationXml,
Chris@0 39 QString outputName) :
Chris@0 40 Transform(inputModel),
Chris@0 41 m_plugin(0),
Chris@64 42 m_channel(channel),
Chris@68 43 m_stepSize(0),
Chris@68 44 m_blockSize(0),
Chris@0 45 m_descriptor(0),
Chris@0 46 m_outputFeatureNo(0)
Chris@0 47 {
Chris@117 48 // std::cerr << "FeatureExtractionPluginTransform::FeatureExtractionPluginTransform: plugin " << pluginId.toStdString() << ", outputName " << outputName.toStdString() << std::endl;
Chris@0 49
Chris@0 50 FeatureExtractionPluginFactory *factory =
Chris@0 51 FeatureExtractionPluginFactory::instanceFor(pluginId);
Chris@0 52
Chris@0 53 if (!factory) {
Chris@0 54 std::cerr << "FeatureExtractionPluginTransform: No factory available for plugin id \""
Chris@0 55 << pluginId.toStdString() << "\"" << std::endl;
Chris@0 56 return;
Chris@0 57 }
Chris@0 58
Chris@0 59 m_plugin = factory->instantiatePlugin(pluginId, m_input->getSampleRate());
Chris@0 60
Chris@0 61 if (!m_plugin) {
Chris@0 62 std::cerr << "FeatureExtractionPluginTransform: Failed to instantiate plugin \""
Chris@0 63 << pluginId.toStdString() << "\"" << std::endl;
Chris@0 64 return;
Chris@0 65 }
Chris@0 66
Chris@56 67 if (configurationXml != "") {
Chris@66 68 PluginXml(m_plugin).setParametersFromXml(configurationXml);
Chris@56 69 }
Chris@56 70
Chris@68 71 m_blockSize = m_plugin->getPreferredBlockSize();
Chris@68 72 m_stepSize = m_plugin->getPreferredStepSize();
Chris@68 73
Chris@68 74 if (m_blockSize == 0) m_blockSize = 1024; //!!! todo: ask user
Chris@68 75 if (m_stepSize == 0) m_stepSize = m_blockSize; //!!! likewise
Chris@68 76
Chris@114 77 DenseTimeValueModel *input = getInput();
Chris@114 78 if (!input) return;
Chris@114 79
Chris@114 80 size_t channelCount = input->getChannelCount();
Chris@114 81 if (m_plugin->getMaxChannelCount() < channelCount) {
Chris@114 82 channelCount = 1;
Chris@114 83 }
Chris@114 84 if (m_plugin->getMinChannelCount() > channelCount) {
Chris@114 85 std::cerr << "FeatureExtractionPluginTransform:: "
Chris@114 86 << "Can't provide enough channels to plugin (plugin min "
Chris@114 87 << m_plugin->getMinChannelCount() << ", max "
Chris@114 88 << m_plugin->getMaxChannelCount() << ", input model has "
Chris@114 89 << input->getChannelCount() << ")" << std::endl;
Chris@114 90 return;
Chris@114 91 }
Chris@114 92
Chris@114 93 if (!m_plugin->initialise(channelCount, m_stepSize, m_blockSize)) {
Chris@114 94 std::cerr << "FeatureExtractionPluginTransform: Plugin "
Chris@114 95 << m_plugin->getName() << " failed to initialise!" << std::endl;
Chris@114 96 return;
Chris@114 97 }
Chris@114 98
Chris@74 99 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
Chris@0 100
Chris@0 101 if (outputs.empty()) {
Chris@0 102 std::cerr << "FeatureExtractionPluginTransform: Plugin \""
Chris@0 103 << pluginId.toStdString() << "\" has no outputs" << std::endl;
Chris@0 104 return;
Chris@0 105 }
Chris@0 106
Chris@0 107 for (size_t i = 0; i < outputs.size(); ++i) {
Chris@0 108 if (outputName == "" || outputs[i].name == outputName.toStdString()) {
Chris@0 109 m_outputFeatureNo = i;
Chris@66 110 m_descriptor = new Vamp::Plugin::OutputDescriptor
Chris@0 111 (outputs[i]);
Chris@0 112 break;
Chris@0 113 }
Chris@0 114 }
Chris@0 115
Chris@0 116 if (!m_descriptor) {
Chris@0 117 std::cerr << "FeatureExtractionPluginTransform: Plugin \""
Chris@0 118 << pluginId.toStdString() << "\" has no output named \""
Chris@0 119 << outputName.toStdString() << "\"" << std::endl;
Chris@0 120 return;
Chris@0 121 }
Chris@0 122
Chris@117 123 // std::cerr << "FeatureExtractionPluginTransform: output sample type "
Chris@117 124 // << m_descriptor->sampleType << std::endl;
Chris@0 125
Chris@70 126 int binCount = 1;
Chris@0 127 float minValue = 0.0, maxValue = 0.0;
Chris@0 128
Chris@70 129 if (m_descriptor->hasFixedBinCount) {
Chris@70 130 binCount = m_descriptor->binCount;
Chris@0 131 }
Chris@0 132
Chris@117 133 // std::cerr << "FeatureExtractionPluginTransform: output bin count "
Chris@117 134 // << binCount << std::endl;
Chris@114 135
Chris@70 136 if (binCount > 0 && m_descriptor->hasKnownExtents) {
Chris@0 137 minValue = m_descriptor->minValue;
Chris@0 138 maxValue = m_descriptor->maxValue;
Chris@0 139 }
Chris@0 140
Chris@0 141 size_t modelRate = m_input->getSampleRate();
Chris@0 142 size_t modelResolution = 1;
Chris@0 143
Chris@0 144 switch (m_descriptor->sampleType) {
Chris@0 145
Chris@66 146 case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
Chris@0 147 if (m_descriptor->sampleRate != 0.0) {
Chris@0 148 modelResolution = size_t(modelRate / m_descriptor->sampleRate + 0.001);
Chris@0 149 }
Chris@0 150 break;
Chris@0 151
Chris@66 152 case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
Chris@72 153 modelResolution = m_stepSize;
Chris@0 154 break;
Chris@0 155
Chris@66 156 case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
Chris@73 157 modelRate = size_t(m_descriptor->sampleRate + 0.001);
Chris@0 158 break;
Chris@0 159 }
Chris@0 160
Chris@70 161 if (binCount == 0) {
Chris@0 162
Chris@20 163 m_output = new SparseOneDimensionalModel(modelRate, modelResolution,
Chris@20 164 false);
Chris@0 165
Chris@115 166 } else if (binCount == 1) {
Chris@0 167
Chris@115 168 SparseTimeValueModel *model = new SparseTimeValueModel
Chris@115 169 (modelRate, modelResolution, minValue, maxValue, false);
Chris@115 170 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
Chris@115 171
Chris@115 172 m_output = model;
Chris@115 173
Chris@115 174 } else if (m_descriptor->sampleType ==
Chris@66 175 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
Chris@115 176
Chris@115 177 // We don't have a sparse 3D model, so interpret this as a
Chris@115 178 // note model. There's nothing to define which values to use
Chris@115 179 // as which parameters of the note -- for the moment let's
Chris@115 180 // treat the first as pitch, second as duration in frames,
Chris@115 181 // third (if present) as velocity. (Our note model doesn't
Chris@115 182 // yet store velocity.)
Chris@115 183 //!!! todo: ask the user!
Chris@0 184
Chris@115 185 NoteModel *model = new NoteModel
Chris@63 186 (modelRate, modelResolution, minValue, maxValue, false);
Chris@63 187 model->setScaleUnits(outputs[m_outputFeatureNo].unit.c_str());
Chris@63 188
Chris@63 189 m_output = model;
Chris@0 190
Chris@0 191 } else {
Chris@0 192
Chris@0 193 m_output = new DenseThreeDimensionalModel(modelRate, modelResolution,
Chris@70 194 binCount, false);
Chris@20 195
Chris@70 196 if (!m_descriptor->binNames.empty()) {
Chris@20 197 std::vector<QString> names;
Chris@70 198 for (size_t i = 0; i < m_descriptor->binNames.size(); ++i) {
Chris@70 199 names.push_back(m_descriptor->binNames[i].c_str());
Chris@20 200 }
Chris@20 201 (dynamic_cast<DenseThreeDimensionalModel *>(m_output))
Chris@20 202 ->setBinNames(names);
Chris@20 203 }
Chris@0 204 }
Chris@0 205 }
Chris@0 206
Chris@0 207 FeatureExtractionPluginTransform::~FeatureExtractionPluginTransform()
Chris@0 208 {
Chris@0 209 delete m_plugin;
Chris@0 210 delete m_descriptor;
Chris@0 211 }
Chris@0 212
Chris@0 213 DenseTimeValueModel *
Chris@0 214 FeatureExtractionPluginTransform::getInput()
Chris@0 215 {
Chris@0 216 DenseTimeValueModel *dtvm =
Chris@0 217 dynamic_cast<DenseTimeValueModel *>(getInputModel());
Chris@0 218 if (!dtvm) {
Chris@0 219 std::cerr << "FeatureExtractionPluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
Chris@0 220 }
Chris@0 221 return dtvm;
Chris@0 222 }
Chris@0 223
Chris@0 224 void
Chris@0 225 FeatureExtractionPluginTransform::run()
Chris@0 226 {
Chris@0 227 DenseTimeValueModel *input = getInput();
Chris@0 228 if (!input) return;
Chris@0 229
Chris@0 230 if (!m_output) return;
Chris@0 231
Chris@114 232 size_t sampleRate = m_input->getSampleRate();
Chris@114 233
Chris@0 234 size_t channelCount = input->getChannelCount();
Chris@0 235 if (m_plugin->getMaxChannelCount() < channelCount) {
Chris@0 236 channelCount = 1;
Chris@0 237 }
Chris@0 238
Chris@0 239 float **buffers = new float*[channelCount];
Chris@0 240 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@68 241 buffers[ch] = new float[m_blockSize];
Chris@0 242 }
Chris@0 243
Chris@128 244 bool frequencyDomain = (m_plugin->getInputDomain() ==
Chris@128 245 Vamp::Plugin::FrequencyDomain);
Chris@132 246 std::vector<FFTFuzzyAdapter *> fftAdapters;
Chris@131 247
Chris@128 248 if (frequencyDomain) {
Chris@128 249 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@132 250 fftAdapters.push_back(new FFTFuzzyAdapter
Chris@132 251 (getInput(),
Chris@132 252 channelCount == 1 ? m_channel : ch,
Chris@132 253 HanningWindow,
Chris@132 254 m_blockSize,
Chris@132 255 m_stepSize,
Chris@132 256 m_blockSize,
Chris@132 257 false));
Chris@128 258 }
Chris@128 259 }
Chris@67 260
Chris@70 261 long startFrame = m_input->getStartFrame();
Chris@70 262 long endFrame = m_input->getEndFrame();
Chris@70 263 long blockFrame = startFrame;
Chris@0 264
Chris@70 265 long prevCompletion = 0;
Chris@0 266
Chris@70 267 while (1) {
Chris@70 268
Chris@128 269 if (frequencyDomain) {
Chris@73 270 if (blockFrame - int(m_blockSize)/2 > endFrame) break;
Chris@70 271 } else {
Chris@70 272 if (blockFrame >= endFrame) break;
Chris@70 273 }
Chris@0 274
Chris@0 275 // std::cerr << "FeatureExtractionPluginTransform::run: blockFrame "
Chris@0 276 // << blockFrame << std::endl;
Chris@0 277
Chris@70 278 long completion =
Chris@68 279 (((blockFrame - startFrame) / m_stepSize) * 99) /
Chris@68 280 ( (endFrame - startFrame) / m_stepSize);
Chris@0 281
Chris@0 282 // channelCount is either m_input->channelCount or 1
Chris@0 283
Chris@73 284 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@128 285 if (frequencyDomain) {
Chris@128 286 int column = (blockFrame - startFrame) / m_stepSize;
Chris@128 287 for (size_t i = 0; i < m_blockSize/2; ++i) {
Chris@132 288 fftAdapters[ch]->getValuesAt
Chris@128 289 (column, i, buffers[ch][i*2], buffers[ch][i*2+1]);
Chris@128 290 }
Chris@133 291 //!!!
Chris@133 292 float sum = 0.0;
Chris@133 293 for (size_t i = 0; i < m_blockSize/2; ++i) {
Chris@133 294 sum += buffers[ch][i*2];
Chris@133 295 }
Chris@133 296 if (fabs(sum) < 0.0001) {
Chris@133 297 std::cerr << "WARNING: small sum for column " << column << " (sum is " << sum << ")" << std::endl;
Chris@133 298 }
Chris@70 299 } else {
Chris@70 300 getFrames(ch, channelCount,
Chris@128 301 blockFrame, m_blockSize, buffers[ch]);
Chris@70 302 }
Chris@70 303 }
Chris@132 304
Chris@66 305 Vamp::Plugin::FeatureSet features = m_plugin->process
Chris@66 306 (buffers, Vamp::RealTime::frame2RealTime(blockFrame, sampleRate));
Chris@0 307
Chris@0 308 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
Chris@66 309 Vamp::Plugin::Feature feature =
Chris@0 310 features[m_outputFeatureNo][fi];
Chris@0 311 addFeature(blockFrame, feature);
Chris@0 312 }
Chris@0 313
Chris@0 314 if (blockFrame == startFrame || completion > prevCompletion) {
Chris@0 315 setCompletion(completion);
Chris@0 316 prevCompletion = completion;
Chris@0 317 }
Chris@0 318
Chris@68 319 blockFrame += m_stepSize;
Chris@0 320 }
Chris@132 321
Chris@66 322 Vamp::Plugin::FeatureSet features = m_plugin->getRemainingFeatures();
Chris@0 323
Chris@0 324 for (size_t fi = 0; fi < features[m_outputFeatureNo].size(); ++fi) {
Chris@66 325 Vamp::Plugin::Feature feature =
Chris@0 326 features[m_outputFeatureNo][fi];
Chris@0 327 addFeature(blockFrame, feature);
Chris@0 328 }
Chris@0 329
Chris@132 330 if (frequencyDomain) {
Chris@132 331 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@132 332 delete fftAdapters[ch];
Chris@132 333 }
Chris@132 334 }
Chris@132 335
Chris@0 336 setCompletion(100);
Chris@0 337 }
Chris@0 338
Chris@70 339 void
Chris@70 340 FeatureExtractionPluginTransform::getFrames(int channel, int channelCount,
Chris@70 341 long startFrame, long size,
Chris@70 342 float *buffer)
Chris@70 343 {
Chris@70 344 long offset = 0;
Chris@70 345
Chris@70 346 if (startFrame < 0) {
Chris@70 347 for (int i = 0; i < size && startFrame + i < 0; ++i) {
Chris@70 348 buffer[i] = 0.0f;
Chris@70 349 }
Chris@70 350 offset = -startFrame;
Chris@70 351 size -= offset;
Chris@70 352 if (size <= 0) return;
Chris@70 353 startFrame = 0;
Chris@70 354 }
Chris@70 355
Chris@73 356 long got = getInput()->getValues
Chris@70 357 ((channelCount == 1 ? m_channel : channel),
Chris@70 358 startFrame, startFrame + size, buffer + offset);
Chris@70 359
Chris@70 360 while (got < size) {
Chris@70 361 buffer[offset + got] = 0.0;
Chris@70 362 ++got;
Chris@70 363 }
Chris@74 364
Chris@74 365 if (m_channel == -1 && channelCount == 1 &&
Chris@74 366 getInput()->getChannelCount() > 1) {
Chris@74 367 // use mean instead of sum, as plugin input
Chris@74 368 int cc = getInput()->getChannelCount();
Chris@74 369 for (long i = 0; i < size; ++i) {
Chris@74 370 buffer[i] /= cc;
Chris@74 371 }
Chris@74 372 }
Chris@70 373 }
Chris@0 374
Chris@0 375 void
Chris@0 376 FeatureExtractionPluginTransform::addFeature(size_t blockFrame,
Chris@66 377 const Vamp::Plugin::Feature &feature)
Chris@0 378 {
Chris@0 379 size_t inputRate = m_input->getSampleRate();
Chris@0 380
Chris@0 381 // std::cerr << "FeatureExtractionPluginTransform::addFeature("
Chris@0 382 // << blockFrame << ")" << std::endl;
Chris@0 383
Chris@70 384 int binCount = 1;
Chris@70 385 if (m_descriptor->hasFixedBinCount) {
Chris@70 386 binCount = m_descriptor->binCount;
Chris@0 387 }
Chris@0 388
Chris@0 389 size_t frame = blockFrame;
Chris@0 390
Chris@0 391 if (m_descriptor->sampleType ==
Chris@66 392 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
Chris@0 393
Chris@0 394 if (!feature.hasTimestamp) {
Chris@0 395 std::cerr
Chris@0 396 << "WARNING: FeatureExtractionPluginTransform::addFeature: "
Chris@0 397 << "Feature has variable sample rate but no timestamp!"
Chris@0 398 << std::endl;
Chris@0 399 return;
Chris@0 400 } else {
Chris@66 401 frame = Vamp::RealTime::realTime2Frame(feature.timestamp, inputRate);
Chris@0 402 }
Chris@0 403
Chris@0 404 } else if (m_descriptor->sampleType ==
Chris@66 405 Vamp::Plugin::OutputDescriptor::FixedSampleRate) {
Chris@0 406
Chris@0 407 if (feature.hasTimestamp) {
Chris@0 408 //!!! warning: sampleRate may be non-integral
Chris@66 409 frame = Vamp::RealTime::realTime2Frame(feature.timestamp,
Chris@66 410 m_descriptor->sampleRate);
Chris@0 411 } else {
Chris@0 412 frame = m_output->getEndFrame() + 1;
Chris@0 413 }
Chris@0 414 }
Chris@0 415
Chris@70 416 if (binCount == 0) {
Chris@0 417
Chris@0 418 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
Chris@0 419 if (!model) return;
Chris@0 420 model->addPoint(SparseOneDimensionalModel::Point(frame, feature.label.c_str()));
Chris@0 421
Chris@115 422 } else if (binCount == 1) {
Chris@0 423
Chris@0 424 float value = 0.0;
Chris@0 425 if (feature.values.size() > 0) value = feature.values[0];
Chris@0 426
Chris@0 427 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
Chris@0 428 if (!model) return;
Chris@0 429 model->addPoint(SparseTimeValueModel::Point(frame, value, feature.label.c_str()));
Chris@115 430
Chris@115 431 } else if (m_descriptor->sampleType ==
Chris@115 432 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
Chris@115 433
Chris@115 434 float pitch = 0.0;
Chris@115 435 if (feature.values.size() > 0) pitch = feature.values[0];
Chris@115 436
Chris@115 437 float duration = 1;
Chris@115 438 if (feature.values.size() > 1) duration = feature.values[1];
Chris@115 439
Chris@115 440 float velocity = 100;
Chris@115 441 if (feature.values.size() > 2) velocity = feature.values[2];
Chris@115 442
Chris@115 443 NoteModel *model = getOutput<NoteModel>();
Chris@115 444 if (!model) return;
Chris@115 445
Chris@115 446 model->addPoint(NoteModel::Point(frame, pitch, duration, feature.label.c_str()));
Chris@0 447
Chris@0 448 } else {
Chris@0 449
Chris@0 450 DenseThreeDimensionalModel::BinValueSet values = feature.values;
Chris@0 451
Chris@0 452 DenseThreeDimensionalModel *model = getOutput<DenseThreeDimensionalModel>();
Chris@0 453 if (!model) return;
Chris@0 454
Chris@0 455 model->setBinValues(frame, values);
Chris@0 456 }
Chris@0 457 }
Chris@0 458
Chris@0 459 void
Chris@0 460 FeatureExtractionPluginTransform::setCompletion(int completion)
Chris@0 461 {
Chris@70 462 int binCount = 1;
Chris@70 463 if (m_descriptor->hasFixedBinCount) {
Chris@70 464 binCount = m_descriptor->binCount;
Chris@0 465 }
Chris@0 466
Chris@70 467 if (binCount == 0) {
Chris@0 468
Chris@0 469 SparseOneDimensionalModel *model = getOutput<SparseOneDimensionalModel>();
Chris@0 470 if (!model) return;
Chris@0 471 model->setCompletion(completion);
Chris@0 472
Chris@115 473 } else if (binCount == 1) {
Chris@115 474
Chris@115 475 SparseTimeValueModel *model = getOutput<SparseTimeValueModel>();
Chris@115 476 if (!model) return;
Chris@115 477 model->setCompletion(completion);
Chris@115 478
Chris@115 479 } else if (m_descriptor->sampleType ==
Chris@66 480 Vamp::Plugin::OutputDescriptor::VariableSampleRate) {
Chris@0 481
Chris@115 482 NoteModel *model = getOutput<NoteModel>();
Chris@0 483 if (!model) return;
Chris@0 484 model->setCompletion(completion);
Chris@0 485
Chris@0 486 } else {
Chris@0 487
Chris@19 488 DenseThreeDimensionalModel *model = getOutput<DenseThreeDimensionalModel>();
Chris@19 489 if (!model) return;
Chris@19 490 model->setCompletion(completion);
Chris@0 491 }
Chris@0 492 }
Chris@0 493