annotate plugin/transform/ModelTransformerFactory.cpp @ 384:6f6ab834449d spectrogram-cache-rejig

* Merge from trunk
author Chris Cannam
date Wed, 27 Feb 2008 11:59:42 +0000
parents 13e5870040e6
children
rev   line source
Chris@320 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@320 2
Chris@320 3 /*
Chris@320 4 Sonic Visualiser
Chris@320 5 An audio file viewer and annotation editor.
Chris@320 6 Centre for Digital Music, Queen Mary, University of London.
Chris@320 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@320 8
Chris@320 9 This program is free software; you can redistribute it and/or
Chris@320 10 modify it under the terms of the GNU General Public License as
Chris@320 11 published by the Free Software Foundation; either version 2 of the
Chris@320 12 License, or (at your option) any later version. See the file
Chris@320 13 COPYING included with this distribution for more information.
Chris@320 14 */
Chris@320 15
Chris@331 16 #include "ModelTransformerFactory.h"
Chris@320 17
Chris@331 18 #include "FeatureExtractionModelTransformer.h"
Chris@331 19 #include "RealTimeEffectModelTransformer.h"
Chris@320 20
Chris@332 21 #include "TransformFactory.h"
Chris@332 22
Chris@320 23 #include "plugin/FeatureExtractionPluginFactory.h"
Chris@320 24 #include "plugin/RealTimePluginFactory.h"
Chris@320 25 #include "plugin/PluginXml.h"
Chris@320 26
Chris@320 27 #include "widgets/PluginParameterDialog.h"
Chris@320 28
Chris@320 29 #include "data/model/DenseTimeValueModel.h"
Chris@320 30
Chris@320 31 #include "vamp-sdk/PluginHostAdapter.h"
Chris@320 32
Chris@320 33 #include "audioio/AudioCallbackPlaySource.h" //!!! shouldn't include here
Chris@320 34
Chris@320 35 #include <iostream>
Chris@320 36 #include <set>
Chris@320 37
Chris@320 38 #include <QRegExp>
Chris@320 39
Chris@331 40 ModelTransformerFactory *
Chris@331 41 ModelTransformerFactory::m_instance = new ModelTransformerFactory;
Chris@320 42
Chris@331 43 ModelTransformerFactory *
Chris@331 44 ModelTransformerFactory::getInstance()
Chris@320 45 {
Chris@320 46 return m_instance;
Chris@320 47 }
Chris@320 48
Chris@331 49 ModelTransformerFactory::~ModelTransformerFactory()
Chris@320 50 {
Chris@320 51 }
Chris@320 52
Chris@320 53 bool
Chris@384 54 ModelTransformerFactory::getChannelRange(TransformId identifier,
Chris@384 55 Vamp::PluginBase *plugin,
Chris@332 56 int &minChannels, int &maxChannels)
Chris@320 57 {
Chris@320 58 Vamp::Plugin *vp = 0;
Chris@320 59 if ((vp = dynamic_cast<Vamp::Plugin *>(plugin)) ||
Chris@320 60 (vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin))) {
Chris@320 61 minChannels = vp->getMinChannelCount();
Chris@320 62 maxChannels = vp->getMaxChannelCount();
Chris@320 63 return true;
Chris@320 64 } else {
Chris@332 65 return TransformFactory::getInstance()->
Chris@332 66 getTransformChannelRange(identifier, minChannels, maxChannels);
Chris@320 67 }
Chris@320 68 }
Chris@320 69
Chris@384 70 ModelTransformer::Input
Chris@384 71 ModelTransformerFactory::getConfigurationForTransform(Transform &transform,
Chris@384 72 const std::vector<Model *> &candidateInputModels,
Chris@384 73 Model *defaultInputModel,
Chris@384 74 AudioCallbackPlaySource *source,
Chris@384 75 size_t startFrame,
Chris@384 76 size_t duration)
Chris@320 77 {
Chris@384 78 ModelTransformer::Input input(0);
Chris@384 79
Chris@384 80 if (candidateInputModels.empty()) return input;
Chris@320 81
Chris@320 82 //!!! This will need revision -- we'll have to have a callback
Chris@320 83 //from the dialog for when the candidate input model is changed,
Chris@320 84 //as we'll need to reinitialise the channel settings in the dialog
Chris@384 85 Model *inputModel = candidateInputModels[0];
Chris@320 86 QStringList candidateModelNames;
Chris@384 87 QString defaultModelName;
Chris@320 88 std::map<QString, Model *> modelMap;
Chris@320 89 for (size_t i = 0; i < candidateInputModels.size(); ++i) {
Chris@320 90 QString modelName = candidateInputModels[i]->objectName();
Chris@320 91 QString origModelName = modelName;
Chris@320 92 int dupcount = 1;
Chris@320 93 while (modelMap.find(modelName) != modelMap.end()) {
Chris@320 94 modelName = tr("%1 <%2>").arg(origModelName).arg(++dupcount);
Chris@320 95 }
Chris@320 96 modelMap[modelName] = candidateInputModels[i];
Chris@320 97 candidateModelNames.push_back(modelName);
Chris@384 98 if (candidateInputModels[i] == defaultInputModel) {
Chris@384 99 defaultModelName = modelName;
Chris@384 100 }
Chris@320 101 }
Chris@320 102
Chris@384 103 QString id = transform.getPluginIdentifier();
Chris@384 104 QString output = transform.getOutput();
Chris@320 105 QString outputLabel = "";
Chris@320 106 QString outputDescription = "";
Chris@320 107
Chris@320 108 bool ok = false;
Chris@384 109 QString configurationXml = m_lastConfigurations[transform.getIdentifier()];
Chris@320 110
Chris@384 111 std::cerr << "last configuration: " << configurationXml.toStdString() << std::endl;
Chris@320 112
Chris@320 113 Vamp::PluginBase *plugin = 0;
Chris@320 114
Chris@320 115 bool frequency = false;
Chris@320 116 bool effect = false;
Chris@320 117 bool generator = false;
Chris@320 118
Chris@320 119 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@320 120
Chris@384 121 std::cerr << "getConfigurationForTransform: instantiating Vamp plugin" << std::endl;
Chris@320 122
Chris@320 123 Vamp::Plugin *vp =
Chris@320 124 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin
Chris@320 125 (id, inputModel->getSampleRate());
Chris@320 126
Chris@320 127 if (vp) {
Chris@320 128
Chris@320 129 plugin = vp;
Chris@320 130 frequency = (vp->getInputDomain() == Vamp::Plugin::FrequencyDomain);
Chris@320 131
Chris@320 132 std::vector<Vamp::Plugin::OutputDescriptor> od =
Chris@320 133 vp->getOutputDescriptors();
Chris@320 134 if (od.size() > 1) {
Chris@320 135 for (size_t i = 0; i < od.size(); ++i) {
Chris@320 136 if (od[i].identifier == output.toStdString()) {
Chris@320 137 outputLabel = od[i].name.c_str();
Chris@320 138 outputDescription = od[i].description.c_str();
Chris@320 139 break;
Chris@320 140 }
Chris@320 141 }
Chris@320 142 }
Chris@320 143 }
Chris@320 144
Chris@320 145 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@320 146
Chris@320 147 RealTimePluginFactory *factory = RealTimePluginFactory::instanceFor(id);
Chris@320 148 const RealTimePluginDescriptor *desc = factory->getPluginDescriptor(id);
Chris@320 149
Chris@320 150 if (desc->audioInputPortCount > 0 &&
Chris@320 151 desc->audioOutputPortCount > 0 &&
Chris@320 152 !desc->isSynth) {
Chris@320 153 effect = true;
Chris@320 154 }
Chris@320 155
Chris@320 156 if (desc->audioInputPortCount == 0) {
Chris@320 157 generator = true;
Chris@320 158 }
Chris@320 159
Chris@320 160 if (output != "A") {
Chris@320 161 int outputNo = output.toInt();
Chris@320 162 if (outputNo >= 0 && outputNo < int(desc->controlOutputPortCount)) {
Chris@320 163 outputLabel = desc->controlOutputPortNames[outputNo].c_str();
Chris@320 164 }
Chris@320 165 }
Chris@320 166
Chris@320 167 size_t sampleRate = inputModel->getSampleRate();
Chris@320 168 size_t blockSize = 1024;
Chris@320 169 size_t channels = 1;
Chris@320 170 if (effect && source) {
Chris@320 171 sampleRate = source->getTargetSampleRate();
Chris@320 172 blockSize = source->getTargetBlockSize();
Chris@320 173 channels = source->getTargetChannelCount();
Chris@320 174 }
Chris@320 175
Chris@320 176 RealTimePluginInstance *rtp = factory->instantiatePlugin
Chris@320 177 (id, 0, 0, sampleRate, blockSize, channels);
Chris@320 178
Chris@320 179 plugin = rtp;
Chris@320 180
Chris@320 181 if (effect && source && rtp) {
Chris@320 182 source->setAuditioningPlugin(rtp);
Chris@320 183 }
Chris@320 184 }
Chris@320 185
Chris@320 186 if (plugin) {
Chris@320 187
Chris@384 188 // Ensure block size etc are valid
Chris@384 189 TransformFactory::getInstance()->
Chris@384 190 makeContextConsistentWithPlugin(transform, plugin);
Chris@320 191
Chris@384 192 // Prepare the plugin with any existing parameters already
Chris@384 193 // found in the transform
Chris@384 194 TransformFactory::getInstance()->
Chris@384 195 setPluginParameters(transform, plugin);
Chris@384 196
Chris@384 197 // For this interactive usage, we want to override those with
Chris@384 198 // whatever the user chose last time around
Chris@384 199 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@320 200
Chris@320 201 int sourceChannels = 1;
Chris@320 202 if (dynamic_cast<DenseTimeValueModel *>(inputModel)) {
Chris@320 203 sourceChannels = dynamic_cast<DenseTimeValueModel *>(inputModel)
Chris@320 204 ->getChannelCount();
Chris@320 205 }
Chris@320 206
Chris@320 207 int minChannels = 1, maxChannels = sourceChannels;
Chris@384 208 getChannelRange(transform.getIdentifier(), plugin,
Chris@384 209 minChannels, maxChannels);
Chris@320 210
Chris@320 211 int targetChannels = sourceChannels;
Chris@320 212 if (!effect) {
Chris@320 213 if (sourceChannels < minChannels) targetChannels = minChannels;
Chris@320 214 if (sourceChannels > maxChannels) targetChannels = maxChannels;
Chris@320 215 }
Chris@320 216
Chris@384 217 int defaultChannel = -1; //!!! no longer saved! [was context.channel]
Chris@320 218
Chris@320 219 PluginParameterDialog *dialog = new PluginParameterDialog(plugin);
Chris@320 220
Chris@320 221 if (candidateModelNames.size() > 1 && !generator) {
Chris@384 222 dialog->setCandidateInputModels(candidateModelNames,
Chris@384 223 defaultModelName);
Chris@320 224 }
Chris@320 225
Chris@320 226 if (startFrame != 0 || duration != 0) {
Chris@320 227 dialog->setShowSelectionOnlyOption(true);
Chris@320 228 }
Chris@320 229
Chris@320 230 if (targetChannels > 0) {
Chris@320 231 dialog->setChannelArrangement(sourceChannels, targetChannels,
Chris@320 232 defaultChannel);
Chris@320 233 }
Chris@320 234
Chris@320 235 dialog->setOutputLabel(outputLabel, outputDescription);
Chris@320 236
Chris@320 237 dialog->setShowProcessingOptions(true, frequency);
Chris@320 238
Chris@320 239 if (dialog->exec() == QDialog::Accepted) {
Chris@320 240 ok = true;
Chris@320 241 }
Chris@320 242
Chris@320 243 QString selectedInput = dialog->getInputModel();
Chris@320 244 if (selectedInput != "") {
Chris@320 245 if (modelMap.find(selectedInput) != modelMap.end()) {
Chris@320 246 inputModel = modelMap[selectedInput];
Chris@320 247 std::cerr << "Found selected input \"" << selectedInput.toStdString() << "\" in model map, result is " << inputModel << std::endl;
Chris@320 248 } else {
Chris@320 249 std::cerr << "Failed to find selected input \"" << selectedInput.toStdString() << "\" in model map" << std::endl;
Chris@320 250 }
Chris@320 251 } else {
Chris@320 252 std::cerr << "Selected input empty: \"" << selectedInput.toStdString() << "\"" << std::endl;
Chris@320 253 }
Chris@384 254
Chris@384 255 // Write parameters back to transform object
Chris@384 256 TransformFactory::getInstance()->
Chris@384 257 setParametersFromPlugin(transform, plugin);
Chris@320 258
Chris@384 259 input.setChannel(dialog->getChannel());
Chris@320 260
Chris@384 261 //!!! The dialog ought to be taking & returning transform
Chris@384 262 //objects and input objects and stuff rather than passing
Chris@384 263 //around all this misc stuff, but that's for tomorrow
Chris@384 264 //(whenever that may be)
Chris@384 265
Chris@320 266 if (startFrame != 0 || duration != 0) {
Chris@320 267 if (dialog->getSelectionOnly()) {
Chris@384 268 transform.setStartTime(RealTime::frame2RealTime
Chris@384 269 (startFrame, inputModel->getSampleRate()));
Chris@384 270 transform.setDuration(RealTime::frame2RealTime
Chris@384 271 (duration, inputModel->getSampleRate()));
Chris@320 272 }
Chris@320 273 }
Chris@320 274
Chris@384 275 size_t stepSize = 0, blockSize = 0;
Chris@384 276 WindowType windowType = HanningWindow;
Chris@320 277
Chris@384 278 dialog->getProcessingParameters(stepSize,
Chris@384 279 blockSize,
Chris@384 280 windowType);
Chris@384 281
Chris@384 282 transform.setStepSize(stepSize);
Chris@384 283 transform.setBlockSize(blockSize);
Chris@384 284 transform.setWindowType(windowType);
Chris@384 285
Chris@384 286 TransformFactory::getInstance()->
Chris@384 287 makeContextConsistentWithPlugin(transform, plugin);
Chris@384 288
Chris@384 289 configurationXml = PluginXml(plugin).toXmlString();
Chris@320 290
Chris@320 291 delete dialog;
Chris@320 292
Chris@320 293 if (effect && source) {
Chris@320 294 source->setAuditioningPlugin(0); // will delete our plugin
Chris@320 295 } else {
Chris@320 296 delete plugin;
Chris@320 297 }
Chris@320 298 }
Chris@320 299
Chris@384 300 if (ok) {
Chris@384 301 m_lastConfigurations[transform.getIdentifier()] = configurationXml;
Chris@384 302 input.setModel(inputModel);
Chris@384 303 }
Chris@320 304
Chris@384 305 return input;
Chris@320 306 }
Chris@384 307 /*!!!
Chris@328 308 PluginTransformer::ExecutionContext
Chris@331 309 ModelTransformerFactory::getDefaultContextForTransformer(TransformId identifier,
Chris@320 310 Model *inputModel)
Chris@320 311 {
Chris@328 312 PluginTransformer::ExecutionContext context(-1);
Chris@320 313
Chris@320 314 QString id = identifier.section(':', 0, 2);
Chris@320 315
Chris@320 316 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@320 317
Chris@320 318 Vamp::Plugin *vp =
Chris@320 319 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin
Chris@320 320 (id, inputModel ? inputModel->getSampleRate() : 48000);
Chris@320 321
Chris@320 322 if (vp) {
Chris@328 323 context = PluginTransformer::ExecutionContext(-1, vp);
Chris@320 324 delete vp;
Chris@320 325 }
Chris@320 326 }
Chris@320 327
Chris@320 328 return context;
Chris@320 329 }
Chris@384 330 */
Chris@331 331 ModelTransformer *
Chris@384 332 ModelTransformerFactory::createTransformer(const Transform &transform,
Chris@384 333 const ModelTransformer::Input &input)
Chris@320 334 {
Chris@331 335 ModelTransformer *transformer = 0;
Chris@320 336
Chris@384 337 QString id = transform.getPluginIdentifier();
Chris@320 338
Chris@320 339 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@384 340
Chris@384 341 transformer =
Chris@384 342 new FeatureExtractionModelTransformer(input, transform);
Chris@384 343
Chris@331 344 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@384 345
Chris@384 346 transformer =
Chris@384 347 new RealTimeEffectModelTransformer(input, transform);
Chris@384 348
Chris@320 349 } else {
Chris@331 350 std::cerr << "ModelTransformerFactory::createTransformer: Unknown transform \""
Chris@384 351 << transform.getIdentifier().toStdString() << "\"" << std::endl;
Chris@331 352 return transformer;
Chris@320 353 }
Chris@320 354
Chris@384 355 if (transformer) transformer->setObjectName(transform.getIdentifier());
Chris@331 356 return transformer;
Chris@320 357 }
Chris@320 358
Chris@320 359 Model *
Chris@384 360 ModelTransformerFactory::transform(const Transform &transform,
Chris@384 361 const ModelTransformer::Input &input,
Chris@384 362 QString &message)
Chris@320 363 {
Chris@384 364 ModelTransformer *t = createTransformer(transform, input);
Chris@320 365 if (!t) return 0;
Chris@320 366
Chris@331 367 connect(t, SIGNAL(finished()), this, SLOT(transformerFinished()));
Chris@320 368
Chris@328 369 m_runningTransformers.insert(t);
Chris@320 370
Chris@320 371 t->start();
Chris@320 372 Model *model = t->detachOutputModel();
Chris@320 373
Chris@320 374 if (model) {
Chris@384 375 QString imn = input.getModel()->objectName();
Chris@332 376 QString trn =
Chris@332 377 TransformFactory::getInstance()->getTransformFriendlyName
Chris@384 378 (transform.getIdentifier());
Chris@320 379 if (imn != "") {
Chris@320 380 if (trn != "") {
Chris@320 381 model->setObjectName(tr("%1: %2").arg(imn).arg(trn));
Chris@320 382 } else {
Chris@320 383 model->setObjectName(imn);
Chris@320 384 }
Chris@320 385 } else if (trn != "") {
Chris@320 386 model->setObjectName(trn);
Chris@320 387 }
Chris@320 388 } else {
Chris@320 389 t->wait();
Chris@320 390 }
Chris@320 391
Chris@384 392 message = t->getMessage();
Chris@384 393
Chris@320 394 return model;
Chris@320 395 }
Chris@320 396
Chris@320 397 void
Chris@331 398 ModelTransformerFactory::transformerFinished()
Chris@320 399 {
Chris@320 400 QObject *s = sender();
Chris@331 401 ModelTransformer *transformer = dynamic_cast<ModelTransformer *>(s);
Chris@320 402
Chris@331 403 std::cerr << "ModelTransformerFactory::transformerFinished(" << transformer << ")" << std::endl;
Chris@320 404
Chris@331 405 if (!transformer) {
Chris@331 406 std::cerr << "WARNING: ModelTransformerFactory::transformerFinished: sender is not a transformer" << std::endl;
Chris@320 407 return;
Chris@320 408 }
Chris@320 409
Chris@331 410 if (m_runningTransformers.find(transformer) == m_runningTransformers.end()) {
Chris@331 411 std::cerr << "WARNING: ModelTransformerFactory::transformerFinished("
Chris@331 412 << transformer
Chris@331 413 << "): I have no record of this transformer running!"
Chris@320 414 << std::endl;
Chris@320 415 }
Chris@320 416
Chris@331 417 m_runningTransformers.erase(transformer);
Chris@320 418
Chris@331 419 transformer->wait(); // unnecessary but reassuring
Chris@331 420 delete transformer;
Chris@320 421 }
Chris@320 422
Chris@320 423 void
Chris@331 424 ModelTransformerFactory::modelAboutToBeDeleted(Model *m)
Chris@320 425 {
Chris@328 426 TransformerSet affected;
Chris@320 427
Chris@328 428 for (TransformerSet::iterator i = m_runningTransformers.begin();
Chris@328 429 i != m_runningTransformers.end(); ++i) {
Chris@320 430
Chris@331 431 ModelTransformer *t = *i;
Chris@320 432
Chris@320 433 if (t->getInputModel() == m || t->getOutputModel() == m) {
Chris@320 434 affected.insert(t);
Chris@320 435 }
Chris@320 436 }
Chris@320 437
Chris@328 438 for (TransformerSet::iterator i = affected.begin();
Chris@320 439 i != affected.end(); ++i) {
Chris@320 440
Chris@331 441 ModelTransformer *t = *i;
Chris@320 442
Chris@320 443 t->abandon();
Chris@320 444
Chris@320 445 t->wait(); // this should eventually call back on
Chris@331 446 // transformerFinished, which will remove from
Chris@328 447 // m_runningTransformers and delete.
Chris@320 448 }
Chris@320 449 }
Chris@320 450