annotate transform/TransformFactory.cpp @ 171:cd58be60e5f2

* Add QuickTime file reader class -- totally untested, shouldn't even compile
author Chris Cannam
date Mon, 06 Aug 2007 14:37:59 +0000
parents 006c90387f40
children aaf806ce329a
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@77 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "TransformFactory.h"
Chris@0 17
Chris@0 18 #include "FeatureExtractionPluginTransform.h"
Chris@0 19 #include "RealTimePluginTransform.h"
Chris@0 20
Chris@0 21 #include "plugin/FeatureExtractionPluginFactory.h"
Chris@0 22 #include "plugin/RealTimePluginFactory.h"
Chris@0 23 #include "plugin/PluginXml.h"
Chris@0 24
Chris@0 25 #include "widgets/PluginParameterDialog.h"
Chris@0 26
Chris@1 27 #include "data/model/DenseTimeValueModel.h"
Chris@0 28
Chris@28 29 #include "vamp-sdk/PluginHostAdapter.h"
Chris@28 30
Chris@41 31 #include "sv/audioio/AudioCallbackPlaySource.h" //!!! shouldn't include here
Chris@41 32
Chris@0 33 #include <iostream>
Chris@0 34 #include <set>
Chris@0 35
Chris@0 36 #include <QRegExp>
Chris@0 37
Chris@0 38 TransformFactory *
Chris@0 39 TransformFactory::m_instance = new TransformFactory;
Chris@0 40
Chris@0 41 TransformFactory *
Chris@0 42 TransformFactory::getInstance()
Chris@0 43 {
Chris@0 44 return m_instance;
Chris@0 45 }
Chris@0 46
Chris@0 47 TransformFactory::~TransformFactory()
Chris@0 48 {
Chris@0 49 }
Chris@0 50
Chris@0 51 TransformFactory::TransformList
Chris@0 52 TransformFactory::getAllTransforms()
Chris@0 53 {
Chris@0 54 if (m_transforms.empty()) populateTransforms();
Chris@0 55
Chris@34 56 std::set<TransformDesc> dset;
Chris@0 57 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@0 58 i != m_transforms.end(); ++i) {
Chris@34 59 dset.insert(i->second);
Chris@34 60 }
Chris@34 61
Chris@34 62 TransformList list;
Chris@34 63 for (std::set<TransformDesc>::const_iterator i = dset.begin();
Chris@34 64 i != dset.end(); ++i) {
Chris@34 65 list.push_back(*i);
Chris@0 66 }
Chris@0 67
Chris@0 68 return list;
Chris@0 69 }
Chris@0 70
Chris@0 71 std::vector<QString>
Chris@0 72 TransformFactory::getAllTransformTypes()
Chris@0 73 {
Chris@0 74 if (m_transforms.empty()) populateTransforms();
Chris@0 75
Chris@0 76 std::set<QString> types;
Chris@0 77 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@0 78 i != m_transforms.end(); ++i) {
Chris@0 79 types.insert(i->second.type);
Chris@0 80 }
Chris@0 81
Chris@0 82 std::vector<QString> rv;
Chris@0 83 for (std::set<QString>::iterator i = types.begin(); i != types.end(); ++i) {
Chris@0 84 rv.push_back(*i);
Chris@0 85 }
Chris@0 86
Chris@0 87 return rv;
Chris@0 88 }
Chris@0 89
Chris@33 90 std::vector<QString>
Chris@33 91 TransformFactory::getTransformCategories(QString transformType)
Chris@33 92 {
Chris@33 93 if (m_transforms.empty()) populateTransforms();
Chris@33 94
Chris@33 95 std::set<QString> categories;
Chris@33 96 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@33 97 i != m_transforms.end(); ++i) {
Chris@33 98 if (i->second.type == transformType) {
Chris@33 99 categories.insert(i->second.category);
Chris@33 100 }
Chris@33 101 }
Chris@33 102
Chris@33 103 bool haveEmpty = false;
Chris@33 104
Chris@33 105 std::vector<QString> rv;
Chris@33 106 for (std::set<QString>::iterator i = categories.begin();
Chris@33 107 i != categories.end(); ++i) {
Chris@33 108 if (*i != "") rv.push_back(*i);
Chris@33 109 else haveEmpty = true;
Chris@33 110 }
Chris@33 111
Chris@33 112 if (haveEmpty) rv.push_back(""); // make sure empty category sorts last
Chris@33 113
Chris@33 114 return rv;
Chris@33 115 }
Chris@33 116
Chris@33 117 std::vector<QString>
Chris@33 118 TransformFactory::getTransformMakers(QString transformType)
Chris@33 119 {
Chris@33 120 if (m_transforms.empty()) populateTransforms();
Chris@33 121
Chris@33 122 std::set<QString> makers;
Chris@33 123 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@33 124 i != m_transforms.end(); ++i) {
Chris@33 125 if (i->second.type == transformType) {
Chris@33 126 makers.insert(i->second.maker);
Chris@33 127 }
Chris@33 128 }
Chris@33 129
Chris@33 130 bool haveEmpty = false;
Chris@33 131
Chris@33 132 std::vector<QString> rv;
Chris@33 133 for (std::set<QString>::iterator i = makers.begin();
Chris@33 134 i != makers.end(); ++i) {
Chris@33 135 if (*i != "") rv.push_back(*i);
Chris@33 136 else haveEmpty = true;
Chris@33 137 }
Chris@33 138
Chris@33 139 if (haveEmpty) rv.push_back(""); // make sure empty category sorts last
Chris@33 140
Chris@33 141 return rv;
Chris@33 142 }
Chris@33 143
Chris@0 144 void
Chris@0 145 TransformFactory::populateTransforms()
Chris@0 146 {
Chris@0 147 TransformDescriptionMap transforms;
Chris@0 148
Chris@0 149 populateFeatureExtractionPlugins(transforms);
Chris@0 150 populateRealTimePlugins(transforms);
Chris@0 151
Chris@80 152 // disambiguate plugins with similar names
Chris@0 153
Chris@107 154 std::map<QString, int> names;
Chris@80 155 std::map<QString, QString> pluginSources;
Chris@80 156 std::map<QString, QString> pluginMakers;
Chris@0 157
Chris@0 158 for (TransformDescriptionMap::iterator i = transforms.begin();
Chris@0 159 i != transforms.end(); ++i) {
Chris@0 160
Chris@0 161 TransformDesc desc = i->second;
Chris@0 162
Chris@107 163 QString td = desc.name;
Chris@80 164 QString tn = td.section(": ", 0, 0);
Chris@107 165 QString pn = desc.identifier.section(":", 1, 1);
Chris@80 166
Chris@80 167 if (pluginSources.find(tn) != pluginSources.end()) {
Chris@80 168 if (pluginSources[tn] != pn && pluginMakers[tn] != desc.maker) {
Chris@107 169 ++names[tn];
Chris@80 170 }
Chris@80 171 } else {
Chris@107 172 ++names[tn];
Chris@80 173 pluginSources[tn] = pn;
Chris@80 174 pluginMakers[tn] = desc.maker;
Chris@80 175 }
Chris@0 176 }
Chris@0 177
Chris@0 178 std::map<QString, int> counts;
Chris@0 179 m_transforms.clear();
Chris@0 180
Chris@0 181 for (TransformDescriptionMap::iterator i = transforms.begin();
Chris@0 182 i != transforms.end(); ++i) {
Chris@0 183
Chris@0 184 TransformDesc desc = i->second;
Chris@107 185 QString identifier = desc.identifier;
Chris@0 186 QString maker = desc.maker;
Chris@0 187
Chris@107 188 QString td = desc.name;
Chris@80 189 QString tn = td.section(": ", 0, 0);
Chris@80 190 QString to = td.section(": ", 1);
Chris@80 191
Chris@107 192 if (names[tn] > 1) {
Chris@80 193 maker.replace(QRegExp(tr(" [\\(<].*$")), "");
Chris@80 194 tn = QString("%1 [%2]").arg(tn).arg(maker);
Chris@0 195 }
Chris@0 196
Chris@80 197 if (to != "") {
Chris@107 198 desc.name = QString("%1: %2").arg(tn).arg(to);
Chris@80 199 } else {
Chris@107 200 desc.name = tn;
Chris@80 201 }
Chris@80 202
Chris@107 203 m_transforms[identifier] = desc;
Chris@0 204 }
Chris@0 205 }
Chris@0 206
Chris@0 207 void
Chris@0 208 TransformFactory::populateFeatureExtractionPlugins(TransformDescriptionMap &transforms)
Chris@0 209 {
Chris@0 210 std::vector<QString> plugs =
Chris@0 211 FeatureExtractionPluginFactory::getAllPluginIdentifiers();
Chris@0 212
Chris@0 213 for (size_t i = 0; i < plugs.size(); ++i) {
Chris@0 214
Chris@0 215 QString pluginId = plugs[i];
Chris@0 216
Chris@0 217 FeatureExtractionPluginFactory *factory =
Chris@0 218 FeatureExtractionPluginFactory::instanceFor(pluginId);
Chris@0 219
Chris@0 220 if (!factory) {
Chris@0 221 std::cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl;
Chris@0 222 continue;
Chris@0 223 }
Chris@0 224
Chris@0 225 Vamp::Plugin *plugin =
Chris@0 226 factory->instantiatePlugin(pluginId, 48000);
Chris@0 227
Chris@0 228 if (!plugin) {
Chris@0 229 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl;
Chris@0 230 continue;
Chris@0 231 }
Chris@0 232
Chris@107 233 QString pluginName = plugin->getName().c_str();
Chris@33 234 QString category = factory->getPluginCategory(pluginId);
Chris@33 235
Chris@0 236 Vamp::Plugin::OutputList outputs =
Chris@0 237 plugin->getOutputDescriptors();
Chris@0 238
Chris@0 239 for (size_t j = 0; j < outputs.size(); ++j) {
Chris@0 240
Chris@107 241 QString transformId = QString("%1:%2")
Chris@107 242 .arg(pluginId).arg(outputs[j].identifier.c_str());
Chris@0 243
Chris@107 244 QString userName;
Chris@0 245 QString friendlyName;
Chris@0 246 QString units = outputs[j].unit.c_str();
Chris@108 247 QString description = plugin->getDescription().c_str();
Chris@108 248 QString maker = plugin->getMaker().c_str();
Chris@108 249 if (maker == "") maker = tr("<unknown maker>");
Chris@108 250
Chris@108 251 if (description == "") {
Chris@108 252 if (outputs.size() == 1) {
Chris@108 253 description = tr("Extract features using \"%1\" plugin (from %2)")
Chris@108 254 .arg(pluginName).arg(maker);
Chris@108 255 } else {
Chris@108 256 description = tr("Extract features using \"%1\" output of \"%2\" plugin (from %3)")
Chris@108 257 .arg(outputs[j].name.c_str()).arg(pluginName).arg(maker);
Chris@108 258 }
Chris@108 259 } else {
Chris@108 260 if (outputs.size() == 1) {
Chris@108 261 description = tr("%1 using \"%2\" plugin (from %3)")
Chris@108 262 .arg(description).arg(pluginName).arg(maker);
Chris@108 263 } else {
Chris@108 264 description = tr("%1 using \"%2\" output of \"%3\" plugin (from %4)")
Chris@108 265 .arg(description).arg(outputs[j].name.c_str()).arg(pluginName).arg(maker);
Chris@108 266 }
Chris@108 267 }
Chris@0 268
Chris@0 269 if (outputs.size() == 1) {
Chris@107 270 userName = pluginName;
Chris@107 271 friendlyName = pluginName;
Chris@0 272 } else {
Chris@107 273 userName = QString("%1: %2")
Chris@107 274 .arg(pluginName)
Chris@107 275 .arg(outputs[j].name.c_str());
Chris@107 276 friendlyName = outputs[j].name.c_str();
Chris@0 277 }
Chris@0 278
Chris@0 279 bool configurable = (!plugin->getPrograms().empty() ||
Chris@0 280 !plugin->getParameterDescriptors().empty());
Chris@0 281
Chris@137 282 // std::cerr << "Feature extraction plugin transform: " << transformId.toStdString() << std::endl;
Chris@107 283
Chris@107 284 transforms[transformId] =
Chris@34 285 TransformDesc(tr("Analysis"),
Chris@33 286 category,
Chris@107 287 transformId,
Chris@107 288 userName,
Chris@0 289 friendlyName,
Chris@108 290 description,
Chris@108 291 maker,
Chris@0 292 units,
Chris@0 293 configurable);
Chris@0 294 }
Chris@0 295 }
Chris@0 296 }
Chris@0 297
Chris@0 298 void
Chris@0 299 TransformFactory::populateRealTimePlugins(TransformDescriptionMap &transforms)
Chris@0 300 {
Chris@0 301 std::vector<QString> plugs =
Chris@0 302 RealTimePluginFactory::getAllPluginIdentifiers();
Chris@0 303
Chris@34 304 static QRegExp unitRE("[\\[\\(]([A-Za-z0-9/]+)[\\)\\]]$");
Chris@0 305
Chris@0 306 for (size_t i = 0; i < plugs.size(); ++i) {
Chris@0 307
Chris@0 308 QString pluginId = plugs[i];
Chris@0 309
Chris@0 310 RealTimePluginFactory *factory =
Chris@0 311 RealTimePluginFactory::instanceFor(pluginId);
Chris@0 312
Chris@0 313 if (!factory) {
Chris@0 314 std::cerr << "WARNING: TransformFactory::populateTransforms: No real time plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl;
Chris@0 315 continue;
Chris@0 316 }
Chris@0 317
Chris@0 318 const RealTimePluginDescriptor *descriptor =
Chris@0 319 factory->getPluginDescriptor(pluginId);
Chris@0 320
Chris@0 321 if (!descriptor) {
Chris@0 322 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to query plugin " << pluginId.toLocal8Bit().data() << std::endl;
Chris@0 323 continue;
Chris@0 324 }
Chris@0 325
Chris@34 326 //!!! if (descriptor->controlOutputPortCount == 0 ||
Chris@34 327 // descriptor->audioInputPortCount == 0) continue;
Chris@0 328
Chris@35 329 // std::cout << "TransformFactory::populateRealTimePlugins: plugin " << pluginId.toStdString() << " has " << descriptor->controlOutputPortCount << " control output ports, " << descriptor->audioOutputPortCount << " audio outputs, " << descriptor->audioInputPortCount << " audio inputs" << std::endl;
Chris@0 330
Chris@107 331 QString pluginName = descriptor->name.c_str();
Chris@33 332 QString category = factory->getPluginCategory(pluginId);
Chris@34 333 bool configurable = (descriptor->parameterCount > 0);
Chris@108 334 QString maker = descriptor->maker.c_str();
Chris@108 335 if (maker == "") maker = tr("<unknown maker>");
Chris@0 336
Chris@34 337 if (descriptor->audioInputPortCount > 0) {
Chris@0 338
Chris@34 339 for (size_t j = 0; j < descriptor->controlOutputPortCount; ++j) {
Chris@0 340
Chris@107 341 QString transformId = QString("%1:%2").arg(pluginId).arg(j);
Chris@107 342 QString userName;
Chris@34 343 QString units;
Chris@108 344 QString portName;
Chris@0 345
Chris@34 346 if (j < descriptor->controlOutputPortNames.size() &&
Chris@34 347 descriptor->controlOutputPortNames[j] != "") {
Chris@0 348
Chris@108 349 portName = descriptor->controlOutputPortNames[j].c_str();
Chris@0 350
Chris@107 351 userName = tr("%1: %2")
Chris@107 352 .arg(pluginName)
Chris@34 353 .arg(portName);
Chris@34 354
Chris@34 355 if (unitRE.indexIn(portName) >= 0) {
Chris@34 356 units = unitRE.cap(1);
Chris@34 357 }
Chris@34 358
Chris@34 359 } else if (descriptor->controlOutputPortCount > 1) {
Chris@34 360
Chris@107 361 userName = tr("%1: Output %2")
Chris@107 362 .arg(pluginName)
Chris@34 363 .arg(j + 1);
Chris@34 364
Chris@34 365 } else {
Chris@34 366
Chris@107 367 userName = pluginName;
Chris@0 368 }
Chris@0 369
Chris@108 370 QString description;
Chris@108 371
Chris@108 372 if (portName != "") {
Chris@108 373 description = tr("Extract \"%1\" data output from \"%2\" effect plugin (from %3)")
Chris@108 374 .arg(portName)
Chris@108 375 .arg(pluginName)
Chris@108 376 .arg(maker);
Chris@108 377 } else {
Chris@108 378 description = tr("Extract data output %1 from \"%2\" effect plugin (from %3)")
Chris@108 379 .arg(j + 1)
Chris@108 380 .arg(pluginName)
Chris@108 381 .arg(maker);
Chris@108 382 }
Chris@0 383
Chris@107 384 transforms[transformId] =
Chris@52 385 TransformDesc(tr("Effects Data"),
Chris@34 386 category,
Chris@107 387 transformId,
Chris@107 388 userName,
Chris@107 389 userName,
Chris@108 390 description,
Chris@108 391 maker,
Chris@34 392 units,
Chris@34 393 configurable);
Chris@34 394 }
Chris@34 395 }
Chris@0 396
Chris@34 397 if (!descriptor->isSynth || descriptor->audioInputPortCount > 0) {
Chris@0 398
Chris@34 399 if (descriptor->audioOutputPortCount > 0) {
Chris@34 400
Chris@107 401 QString transformId = QString("%1:A").arg(pluginId);
Chris@34 402 QString type = tr("Effects");
Chris@108 403
Chris@108 404 QString description = tr("Transform audio signal with \"%1\" effect plugin (from %2)")
Chris@108 405 .arg(pluginName)
Chris@108 406 .arg(maker);
Chris@108 407
Chris@34 408 if (descriptor->audioInputPortCount == 0) {
Chris@34 409 type = tr("Generators");
Chris@108 410 QString description = tr("Generate audio signal using \"%1\" plugin (from %2)")
Chris@108 411 .arg(pluginName)
Chris@108 412 .arg(maker);
Chris@34 413 }
Chris@34 414
Chris@107 415 transforms[transformId] =
Chris@34 416 TransformDesc(type,
Chris@34 417 category,
Chris@107 418 transformId,
Chris@107 419 pluginName,
Chris@107 420 pluginName,
Chris@108 421 description,
Chris@108 422 maker,
Chris@34 423 "",
Chris@34 424 configurable);
Chris@0 425 }
Chris@34 426 }
Chris@0 427 }
Chris@0 428 }
Chris@0 429
Chris@0 430 QString
Chris@107 431 TransformFactory::getTransformName(TransformId identifier)
Chris@0 432 {
Chris@107 433 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@107 434 return m_transforms[identifier].name;
Chris@0 435 } else return "";
Chris@0 436 }
Chris@0 437
Chris@0 438 QString
Chris@107 439 TransformFactory::getTransformFriendlyName(TransformId identifier)
Chris@0 440 {
Chris@107 441 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@107 442 return m_transforms[identifier].friendlyName;
Chris@0 443 } else return "";
Chris@0 444 }
Chris@0 445
Chris@0 446 QString
Chris@107 447 TransformFactory::getTransformUnits(TransformId identifier)
Chris@0 448 {
Chris@107 449 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@107 450 return m_transforms[identifier].units;
Chris@0 451 } else return "";
Chris@0 452 }
Chris@0 453
Chris@0 454 bool
Chris@107 455 TransformFactory::isTransformConfigurable(TransformId identifier)
Chris@0 456 {
Chris@107 457 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@107 458 return m_transforms[identifier].configurable;
Chris@0 459 } else return false;
Chris@0 460 }
Chris@0 461
Chris@0 462 bool
Chris@107 463 TransformFactory::getTransformChannelRange(TransformId identifier,
Chris@0 464 int &min, int &max)
Chris@0 465 {
Chris@107 466 QString id = identifier.section(':', 0, 2);
Chris@0 467
Chris@0 468 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@0 469
Chris@0 470 Vamp::Plugin *plugin =
Chris@0 471 FeatureExtractionPluginFactory::instanceFor(id)->
Chris@0 472 instantiatePlugin(id, 48000);
Chris@0 473 if (!plugin) return false;
Chris@0 474
Chris@0 475 min = plugin->getMinChannelCount();
Chris@0 476 max = plugin->getMaxChannelCount();
Chris@0 477 delete plugin;
Chris@0 478
Chris@0 479 return true;
Chris@0 480
Chris@0 481 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@0 482
Chris@0 483 const RealTimePluginDescriptor *descriptor =
Chris@0 484 RealTimePluginFactory::instanceFor(id)->
Chris@0 485 getPluginDescriptor(id);
Chris@0 486 if (!descriptor) return false;
Chris@0 487
Chris@0 488 min = descriptor->audioInputPortCount;
Chris@0 489 max = descriptor->audioInputPortCount;
Chris@0 490
Chris@0 491 return true;
Chris@0 492 }
Chris@0 493
Chris@0 494 return false;
Chris@0 495 }
Chris@0 496
Chris@0 497 bool
Chris@107 498 TransformFactory::getChannelRange(TransformId identifier, Vamp::PluginBase *plugin,
Chris@0 499 int &minChannels, int &maxChannels)
Chris@0 500 {
Chris@0 501 Vamp::Plugin *vp = 0;
Chris@28 502 if ((vp = dynamic_cast<Vamp::Plugin *>(plugin)) ||
Chris@28 503 (vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin))) {
Chris@0 504 minChannels = vp->getMinChannelCount();
Chris@0 505 maxChannels = vp->getMaxChannelCount();
Chris@0 506 return true;
Chris@0 507 } else {
Chris@107 508 return getTransformChannelRange(identifier, minChannels, maxChannels);
Chris@0 509 }
Chris@0 510 }
Chris@0 511
Chris@53 512 Model *
Chris@107 513 TransformFactory::getConfigurationForTransform(TransformId identifier,
Chris@53 514 const std::vector<Model *> &candidateInputModels,
Chris@27 515 PluginTransform::ExecutionContext &context,
Chris@41 516 QString &configurationXml,
Chris@41 517 AudioCallbackPlaySource *source)
Chris@0 518 {
Chris@53 519 if (candidateInputModels.empty()) return 0;
Chris@53 520
Chris@54 521 //!!! This will need revision -- we'll have to have a callback
Chris@54 522 //from the dialog for when the candidate input model is changed,
Chris@54 523 //as we'll need to reinitialise the channel settings in the dialog
Chris@53 524 Model *inputModel = candidateInputModels[0]; //!!! for now
Chris@54 525 QStringList candidateModelNames;
Chris@54 526 std::map<QString, Model *> modelMap;
Chris@54 527 for (size_t i = 0; i < candidateInputModels.size(); ++i) {
Chris@54 528 QString modelName = candidateInputModels[i]->objectName();
Chris@54 529 QString origModelName = modelName;
Chris@54 530 int dupcount = 1;
Chris@54 531 while (modelMap.find(modelName) != modelMap.end()) {
Chris@54 532 modelName = tr("%1 <%2>").arg(origModelName).arg(++dupcount);
Chris@54 533 }
Chris@54 534 modelMap[modelName] = candidateInputModels[i];
Chris@54 535 candidateModelNames.push_back(modelName);
Chris@54 536 }
Chris@53 537
Chris@107 538 QString id = identifier.section(':', 0, 2);
Chris@107 539 QString output = identifier.section(':', 3);
Chris@53 540 QString outputLabel = "";
Chris@108 541 QString outputDescription = "";
Chris@0 542
Chris@0 543 bool ok = false;
Chris@107 544 configurationXml = m_lastConfigurations[identifier];
Chris@0 545
Chris@0 546 // std::cerr << "last configuration: " << configurationXml.toStdString() << std::endl;
Chris@0 547
Chris@0 548 Vamp::PluginBase *plugin = 0;
Chris@0 549
Chris@10 550 bool frequency = false;
Chris@41 551 bool effect = false;
Chris@58 552 bool generator = false;
Chris@10 553
Chris@0 554 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@0 555
Chris@10 556 Vamp::Plugin *vp =
Chris@10 557 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin
Chris@0 558 (id, inputModel->getSampleRate());
Chris@53 559
Chris@10 560 if (vp) {
Chris@53 561
Chris@10 562 plugin = vp;
Chris@10 563 frequency = (vp->getInputDomain() == Vamp::Plugin::FrequencyDomain);
Chris@53 564
Chris@53 565 std::vector<Vamp::Plugin::OutputDescriptor> od =
Chris@53 566 vp->getOutputDescriptors();
Chris@53 567 if (od.size() > 1) {
Chris@53 568 for (size_t i = 0; i < od.size(); ++i) {
Chris@107 569 if (od[i].identifier == output.toStdString()) {
Chris@107 570 outputLabel = od[i].name.c_str();
Chris@108 571 outputDescription = od[i].description.c_str();
Chris@53 572 break;
Chris@53 573 }
Chris@53 574 }
Chris@53 575 }
Chris@10 576 }
Chris@0 577
Chris@0 578 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@0 579
Chris@41 580 RealTimePluginFactory *factory = RealTimePluginFactory::instanceFor(id);
Chris@41 581 const RealTimePluginDescriptor *desc = factory->getPluginDescriptor(id);
Chris@41 582
Chris@41 583 if (desc->audioInputPortCount > 0 &&
Chris@41 584 desc->audioOutputPortCount > 0 &&
Chris@41 585 !desc->isSynth) {
Chris@41 586 effect = true;
Chris@41 587 }
Chris@41 588
Chris@58 589 if (desc->audioInputPortCount == 0) {
Chris@58 590 generator = true;
Chris@58 591 }
Chris@58 592
Chris@53 593 if (output != "A") {
Chris@53 594 int outputNo = output.toInt();
Chris@137 595 if (outputNo >= 0 && outputNo < int(desc->controlOutputPortCount)) {
Chris@53 596 outputLabel = desc->controlOutputPortNames[outputNo].c_str();
Chris@53 597 }
Chris@53 598 }
Chris@53 599
Chris@41 600 size_t sampleRate = inputModel->getSampleRate();
Chris@41 601 size_t blockSize = 1024;
Chris@41 602 size_t channels = 1;
Chris@41 603 if (effect && source) {
Chris@41 604 sampleRate = source->getTargetSampleRate();
Chris@41 605 blockSize = source->getTargetBlockSize();
Chris@41 606 channels = source->getTargetChannelCount();
Chris@41 607 }
Chris@41 608
Chris@41 609 RealTimePluginInstance *rtp = factory->instantiatePlugin
Chris@41 610 (id, 0, 0, sampleRate, blockSize, channels);
Chris@41 611
Chris@41 612 plugin = rtp;
Chris@41 613
Chris@41 614 if (effect && source && rtp) {
Chris@41 615 source->setAuditioningPlugin(rtp);
Chris@41 616 }
Chris@0 617 }
Chris@0 618
Chris@0 619 if (plugin) {
Chris@27 620
Chris@27 621 context = PluginTransform::ExecutionContext(context.channel, plugin);
Chris@27 622
Chris@0 623 if (configurationXml != "") {
Chris@0 624 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@0 625 }
Chris@0 626
Chris@0 627 int sourceChannels = 1;
Chris@0 628 if (dynamic_cast<DenseTimeValueModel *>(inputModel)) {
Chris@0 629 sourceChannels = dynamic_cast<DenseTimeValueModel *>(inputModel)
Chris@0 630 ->getChannelCount();
Chris@0 631 }
Chris@0 632
Chris@0 633 int minChannels = 1, maxChannels = sourceChannels;
Chris@107 634 getChannelRange(identifier, plugin, minChannels, maxChannels);
Chris@0 635
Chris@0 636 int targetChannels = sourceChannels;
Chris@44 637 if (!effect) {
Chris@44 638 if (sourceChannels < minChannels) targetChannels = minChannels;
Chris@44 639 if (sourceChannels > maxChannels) targetChannels = maxChannels;
Chris@44 640 }
Chris@0 641
Chris@27 642 int defaultChannel = context.channel;
Chris@0 643
Chris@53 644 PluginParameterDialog *dialog = new PluginParameterDialog(plugin);
Chris@53 645
Chris@58 646 if (candidateModelNames.size() > 1 && !generator) {
Chris@54 647 dialog->setCandidateInputModels(candidateModelNames);
Chris@54 648 }
Chris@54 649
Chris@58 650 if (targetChannels > 0) {
Chris@58 651 dialog->setChannelArrangement(sourceChannels, targetChannels,
Chris@58 652 defaultChannel);
Chris@58 653 }
Chris@53 654
Chris@108 655 dialog->setOutputLabel(outputLabel, outputDescription);
Chris@53 656
Chris@53 657 dialog->setShowProcessingOptions(true, frequency);
Chris@53 658
Chris@0 659 if (dialog->exec() == QDialog::Accepted) {
Chris@0 660 ok = true;
Chris@0 661 }
Chris@42 662
Chris@54 663 QString selectedInput = dialog->getInputModel();
Chris@54 664 if (selectedInput != "") {
Chris@54 665 if (modelMap.find(selectedInput) != modelMap.end()) {
Chris@54 666 inputModel = modelMap[selectedInput];
Chris@54 667 std::cerr << "Found selected input \"" << selectedInput.toStdString() << "\" in model map, result is " << inputModel << std::endl;
Chris@54 668 } else {
Chris@54 669 std::cerr << "Failed to find selected input \"" << selectedInput.toStdString() << "\" in model map" << std::endl;
Chris@54 670 }
Chris@54 671 }
Chris@54 672
Chris@0 673 configurationXml = PluginXml(plugin).toXmlString();
Chris@27 674 context.channel = dialog->getChannel();
Chris@26 675
Chris@27 676 dialog->getProcessingParameters(context.stepSize,
Chris@27 677 context.blockSize,
Chris@27 678 context.windowType);
Chris@27 679
Chris@27 680 context.makeConsistentWithPlugin(plugin);
Chris@26 681
Chris@0 682 delete dialog;
Chris@41 683
Chris@41 684 if (effect && source) {
Chris@41 685 source->setAuditioningPlugin(0); // will delete our plugin
Chris@41 686 } else {
Chris@41 687 delete plugin;
Chris@41 688 }
Chris@0 689 }
Chris@0 690
Chris@107 691 if (ok) m_lastConfigurations[identifier] = configurationXml;
Chris@0 692
Chris@53 693 return ok ? inputModel : 0;
Chris@0 694 }
Chris@0 695
Chris@73 696 PluginTransform::ExecutionContext
Chris@107 697 TransformFactory::getDefaultContextForTransform(TransformId identifier,
Chris@73 698 Model *inputModel)
Chris@73 699 {
Chris@73 700 PluginTransform::ExecutionContext context(-1);
Chris@73 701
Chris@107 702 QString id = identifier.section(':', 0, 2);
Chris@73 703
Chris@73 704 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@73 705
Chris@73 706 Vamp::Plugin *vp =
Chris@73 707 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin
Chris@73 708 (id, inputModel ? inputModel->getSampleRate() : 48000);
Chris@73 709
Chris@73 710 if (vp) context = PluginTransform::ExecutionContext(-1, vp);
Chris@73 711
Chris@73 712 }
Chris@73 713
Chris@73 714 return context;
Chris@73 715 }
Chris@73 716
Chris@0 717 Transform *
Chris@107 718 TransformFactory::createTransform(TransformId identifier, Model *inputModel,
Chris@27 719 const PluginTransform::ExecutionContext &context,
Chris@118 720 QString configurationXml)
Chris@0 721 {
Chris@0 722 Transform *transform = 0;
Chris@0 723
Chris@107 724 QString id = identifier.section(':', 0, 2);
Chris@107 725 QString output = identifier.section(':', 3);
Chris@0 726
Chris@0 727 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@0 728 transform = new FeatureExtractionPluginTransform(inputModel,
Chris@0 729 id,
Chris@27 730 context,
Chris@0 731 configurationXml,
Chris@0 732 output);
Chris@0 733 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@0 734 transform = new RealTimePluginTransform(inputModel,
Chris@0 735 id,
Chris@27 736 context,
Chris@0 737 configurationXml,
Chris@107 738 getTransformUnits(identifier),
Chris@34 739 output == "A" ? -1 :
Chris@0 740 output.toInt());
Chris@0 741 } else {
Chris@0 742 std::cerr << "TransformFactory::createTransform: Unknown transform \""
Chris@107 743 << identifier.toStdString() << "\"" << std::endl;
Chris@0 744 return transform;
Chris@0 745 }
Chris@0 746
Chris@118 747 if (transform) transform->setObjectName(identifier);
Chris@0 748 return transform;
Chris@0 749 }
Chris@0 750
Chris@0 751 Model *
Chris@107 752 TransformFactory::transform(TransformId identifier, Model *inputModel,
Chris@27 753 const PluginTransform::ExecutionContext &context,
Chris@27 754 QString configurationXml)
Chris@0 755 {
Chris@107 756 Transform *t = createTransform(identifier, inputModel, context,
Chris@118 757 configurationXml);
Chris@0 758
Chris@0 759 if (!t) return 0;
Chris@0 760
Chris@0 761 connect(t, SIGNAL(finished()), this, SLOT(transformFinished()));
Chris@0 762
Chris@118 763 m_runningTransforms.insert(t);
Chris@118 764
Chris@0 765 t->start();
Chris@55 766 Model *model = t->detachOutputModel();
Chris@55 767
Chris@55 768 if (model) {
Chris@55 769 QString imn = inputModel->objectName();
Chris@107 770 QString trn = getTransformFriendlyName(identifier);
Chris@55 771 if (imn != "") {
Chris@55 772 if (trn != "") {
Chris@55 773 model->setObjectName(tr("%1: %2").arg(imn).arg(trn));
Chris@55 774 } else {
Chris@55 775 model->setObjectName(imn);
Chris@55 776 }
Chris@55 777 } else if (trn != "") {
Chris@55 778 model->setObjectName(trn);
Chris@55 779 }
Chris@55 780 }
Chris@55 781
Chris@55 782 return model;
Chris@0 783 }
Chris@0 784
Chris@0 785 void
Chris@0 786 TransformFactory::transformFinished()
Chris@0 787 {
Chris@0 788 QObject *s = sender();
Chris@0 789 Transform *transform = dynamic_cast<Transform *>(s);
Chris@0 790
Chris@118 791 std::cerr << "TransformFactory::transformFinished(" << transform << ")" << std::endl;
Chris@118 792
Chris@0 793 if (!transform) {
Chris@0 794 std::cerr << "WARNING: TransformFactory::transformFinished: sender is not a transform" << std::endl;
Chris@0 795 return;
Chris@0 796 }
Chris@0 797
Chris@118 798 if (m_runningTransforms.find(transform) == m_runningTransforms.end()) {
Chris@118 799 std::cerr << "WARNING: TransformFactory::transformFinished("
Chris@118 800 << transform
Chris@118 801 << "): I have no record of this transform running!"
Chris@118 802 << std::endl;
Chris@118 803 }
Chris@118 804
Chris@118 805 m_runningTransforms.erase(transform);
Chris@118 806
Chris@0 807 transform->wait(); // unnecessary but reassuring
Chris@0 808 delete transform;
Chris@0 809 }
Chris@0 810
Chris@118 811 void
Chris@118 812 TransformFactory::modelAboutToBeDeleted(Model *m)
Chris@118 813 {
Chris@118 814 TransformSet affected;
Chris@118 815
Chris@118 816 for (TransformSet::iterator i = m_runningTransforms.begin();
Chris@118 817 i != m_runningTransforms.end(); ++i) {
Chris@118 818
Chris@118 819 Transform *t = *i;
Chris@118 820
Chris@118 821 if (t->getInputModel() == m || t->getOutputModel() == m) {
Chris@118 822 affected.insert(t);
Chris@118 823 }
Chris@118 824 }
Chris@118 825
Chris@118 826 for (TransformSet::iterator i = affected.begin();
Chris@118 827 i != affected.end(); ++i) {
Chris@118 828
Chris@118 829 Transform *t = *i;
Chris@118 830
Chris@118 831 t->abandon();
Chris@118 832
Chris@118 833 t->wait(); // this should eventually call back on
Chris@118 834 // transformFinished, which will remove from
Chris@118 835 // m_runningTransforms and delete.
Chris@118 836 }
Chris@118 837 }
Chris@118 838