annotate transform/TransformFactory.cpp @ 460:93fb1ebff76b

* Add persistent cache file support to FileSource (e.g. for RDF descriptions) * Query RDF plugin data in a background thread on startup
author Chris Cannam
date Fri, 17 Oct 2008 13:32:55 +0000
parents ef14acd6d102
children 2019d89ebcf9
rev   line source
Chris@330 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@330 2
Chris@330 3 /*
Chris@330 4 Sonic Visualiser
Chris@330 5 An audio file viewer and annotation editor.
Chris@330 6 Centre for Digital Music, Queen Mary, University of London.
Chris@330 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@330 8
Chris@330 9 This program is free software; you can redistribute it and/or
Chris@330 10 modify it under the terms of the GNU General Public License as
Chris@330 11 published by the Free Software Foundation; either version 2 of the
Chris@330 12 License, or (at your option) any later version. See the file
Chris@330 13 COPYING included with this distribution for more information.
Chris@330 14 */
Chris@330 15
Chris@330 16 #include "TransformFactory.h"
Chris@330 17
Chris@330 18 #include "plugin/FeatureExtractionPluginFactory.h"
Chris@330 19 #include "plugin/RealTimePluginFactory.h"
Chris@332 20 #include "plugin/RealTimePluginInstance.h"
Chris@330 21 #include "plugin/PluginXml.h"
Chris@330 22
Chris@332 23 #include "vamp-sdk/Plugin.h"
Chris@330 24 #include "vamp-sdk/PluginHostAdapter.h"
Chris@332 25 #include "vamp-sdk/hostext/PluginWrapper.h"
Chris@330 26
Chris@457 27 #include "rdf/PluginRDFIndexer.h"
Chris@457 28 #include "rdf/PluginRDFDescription.h"
Chris@457 29
Chris@446 30 #include "base/XmlExportable.h"
Chris@446 31
Chris@330 32 #include <iostream>
Chris@330 33 #include <set>
Chris@330 34
Chris@330 35 #include <QRegExp>
Chris@350 36 #include <QTextStream>
Chris@330 37
Chris@460 38 #include "base/Thread.h"
Chris@460 39
Chris@443 40 using std::cerr;
Chris@443 41 using std::endl;
Chris@443 42
Chris@330 43 TransformFactory *
Chris@330 44 TransformFactory::m_instance = new TransformFactory;
Chris@330 45
Chris@330 46 TransformFactory *
Chris@330 47 TransformFactory::getInstance()
Chris@330 48 {
Chris@330 49 return m_instance;
Chris@330 50 }
Chris@330 51
Chris@457 52 TransformFactory::TransformFactory() :
Chris@457 53 m_transformsPopulated(false),
Chris@457 54 m_uninstalledTransformsPopulated(false)
Chris@457 55 {
Chris@460 56 UninstalledTransformsPopulateThread *thread =
Chris@460 57 new UninstalledTransformsPopulateThread(this);
Chris@460 58 thread->start();
Chris@457 59 }
Chris@457 60
Chris@330 61 TransformFactory::~TransformFactory()
Chris@330 62 {
Chris@330 63 }
Chris@330 64
Chris@330 65 TransformList
Chris@350 66 TransformFactory::getAllTransformDescriptions()
Chris@330 67 {
Chris@460 68 populateTransforms();
Chris@330 69
Chris@330 70 std::set<TransformDescription> dset;
Chris@330 71 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@330 72 i != m_transforms.end(); ++i) {
Chris@443 73 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl;
Chris@330 74 dset.insert(i->second);
Chris@330 75 }
Chris@330 76
Chris@330 77 TransformList list;
Chris@330 78 for (std::set<TransformDescription>::const_iterator i = dset.begin();
Chris@330 79 i != dset.end(); ++i) {
Chris@443 80 // cerr << "inserting transform into list: id = " << i->identifier.toStdString() << endl;
Chris@330 81 list.push_back(*i);
Chris@330 82 }
Chris@330 83
Chris@330 84 return list;
Chris@330 85 }
Chris@330 86
Chris@350 87 TransformDescription
Chris@350 88 TransformFactory::getTransformDescription(TransformId id)
Chris@350 89 {
Chris@460 90 populateTransforms();
Chris@350 91
Chris@350 92 if (m_transforms.find(id) == m_transforms.end()) {
Chris@350 93 return TransformDescription();
Chris@350 94 }
Chris@350 95
Chris@350 96 return m_transforms[id];
Chris@350 97 }
Chris@350 98
Chris@457 99 TransformList
Chris@457 100 TransformFactory::getUninstalledTransformDescriptions()
Chris@457 101 {
Chris@460 102 populateUninstalledTransforms();
Chris@457 103
Chris@457 104 std::set<TransformDescription> dset;
Chris@457 105 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin();
Chris@457 106 i != m_uninstalledTransforms.end(); ++i) {
Chris@457 107 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl;
Chris@457 108 dset.insert(i->second);
Chris@457 109 }
Chris@457 110
Chris@457 111 TransformList list;
Chris@457 112 for (std::set<TransformDescription>::const_iterator i = dset.begin();
Chris@457 113 i != dset.end(); ++i) {
Chris@460 114 // cerr << "inserting transform into uninstalled list: id = " << i->identifier.toStdString() << endl;
Chris@457 115 list.push_back(*i);
Chris@457 116 }
Chris@457 117
Chris@457 118 return list;
Chris@457 119 }
Chris@457 120
Chris@457 121 TransformDescription
Chris@457 122 TransformFactory::getUninstalledTransformDescription(TransformId id)
Chris@457 123 {
Chris@460 124 populateUninstalledTransforms();
Chris@457 125
Chris@457 126 if (m_uninstalledTransforms.find(id) == m_uninstalledTransforms.end()) {
Chris@457 127 return TransformDescription();
Chris@457 128 }
Chris@457 129
Chris@457 130 return m_uninstalledTransforms[id];
Chris@457 131 }
Chris@457 132
Chris@457 133 TransformFactory::TransformInstallStatus
Chris@457 134 TransformFactory::getTransformInstallStatus(TransformId id)
Chris@457 135 {
Chris@460 136 populateTransforms();
Chris@460 137 populateUninstalledTransforms();
Chris@457 138
Chris@457 139 if (m_transforms.find(id) != m_transforms.end()) {
Chris@457 140 return TransformInstalled;
Chris@457 141 }
Chris@457 142 if (m_uninstalledTransforms.find(id) != m_uninstalledTransforms.end()) {
Chris@457 143 return TransformNotInstalled;
Chris@457 144 }
Chris@457 145 return TransformUnknown;
Chris@457 146 }
Chris@457 147
Chris@457 148
Chris@330 149 std::vector<QString>
Chris@330 150 TransformFactory::getAllTransformTypes()
Chris@330 151 {
Chris@460 152 populateTransforms();
Chris@330 153
Chris@330 154 std::set<QString> types;
Chris@330 155 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@330 156 i != m_transforms.end(); ++i) {
Chris@330 157 types.insert(i->second.type);
Chris@330 158 }
Chris@330 159
Chris@330 160 std::vector<QString> rv;
Chris@330 161 for (std::set<QString>::iterator i = types.begin(); i != types.end(); ++i) {
Chris@330 162 rv.push_back(*i);
Chris@330 163 }
Chris@330 164
Chris@330 165 return rv;
Chris@330 166 }
Chris@330 167
Chris@330 168 std::vector<QString>
Chris@330 169 TransformFactory::getTransformCategories(QString transformType)
Chris@330 170 {
Chris@460 171 populateTransforms();
Chris@330 172
Chris@330 173 std::set<QString> categories;
Chris@330 174 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@330 175 i != m_transforms.end(); ++i) {
Chris@330 176 if (i->second.type == transformType) {
Chris@330 177 categories.insert(i->second.category);
Chris@330 178 }
Chris@330 179 }
Chris@330 180
Chris@330 181 bool haveEmpty = false;
Chris@330 182
Chris@330 183 std::vector<QString> rv;
Chris@330 184 for (std::set<QString>::iterator i = categories.begin();
Chris@330 185 i != categories.end(); ++i) {
Chris@330 186 if (*i != "") rv.push_back(*i);
Chris@330 187 else haveEmpty = true;
Chris@330 188 }
Chris@330 189
Chris@330 190 if (haveEmpty) rv.push_back(""); // make sure empty category sorts last
Chris@330 191
Chris@330 192 return rv;
Chris@330 193 }
Chris@330 194
Chris@330 195 std::vector<QString>
Chris@330 196 TransformFactory::getTransformMakers(QString transformType)
Chris@330 197 {
Chris@460 198 populateTransforms();
Chris@330 199
Chris@330 200 std::set<QString> makers;
Chris@330 201 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@330 202 i != m_transforms.end(); ++i) {
Chris@330 203 if (i->second.type == transformType) {
Chris@330 204 makers.insert(i->second.maker);
Chris@330 205 }
Chris@330 206 }
Chris@330 207
Chris@330 208 bool haveEmpty = false;
Chris@330 209
Chris@330 210 std::vector<QString> rv;
Chris@330 211 for (std::set<QString>::iterator i = makers.begin();
Chris@330 212 i != makers.end(); ++i) {
Chris@330 213 if (*i != "") rv.push_back(*i);
Chris@330 214 else haveEmpty = true;
Chris@330 215 }
Chris@330 216
Chris@330 217 if (haveEmpty) rv.push_back(""); // make sure empty category sorts last
Chris@330 218
Chris@330 219 return rv;
Chris@330 220 }
Chris@330 221
Chris@330 222 void
Chris@330 223 TransformFactory::populateTransforms()
Chris@330 224 {
Chris@460 225 MutexLocker locker(&m_transformsMutex,
Chris@460 226 "TransformFactory::populateTransforms");
Chris@460 227 if (m_transformsPopulated) {
Chris@460 228 return;
Chris@460 229 }
Chris@460 230
Chris@330 231 TransformDescriptionMap transforms;
Chris@330 232
Chris@330 233 populateFeatureExtractionPlugins(transforms);
Chris@330 234 populateRealTimePlugins(transforms);
Chris@330 235
Chris@330 236 // disambiguate plugins with similar names
Chris@330 237
Chris@330 238 std::map<QString, int> names;
Chris@330 239 std::map<QString, QString> pluginSources;
Chris@330 240 std::map<QString, QString> pluginMakers;
Chris@330 241
Chris@330 242 for (TransformDescriptionMap::iterator i = transforms.begin();
Chris@330 243 i != transforms.end(); ++i) {
Chris@330 244
Chris@330 245 TransformDescription desc = i->second;
Chris@330 246
Chris@330 247 QString td = desc.name;
Chris@330 248 QString tn = td.section(": ", 0, 0);
Chris@330 249 QString pn = desc.identifier.section(":", 1, 1);
Chris@330 250
Chris@330 251 if (pluginSources.find(tn) != pluginSources.end()) {
Chris@330 252 if (pluginSources[tn] != pn && pluginMakers[tn] != desc.maker) {
Chris@330 253 ++names[tn];
Chris@330 254 }
Chris@330 255 } else {
Chris@330 256 ++names[tn];
Chris@330 257 pluginSources[tn] = pn;
Chris@330 258 pluginMakers[tn] = desc.maker;
Chris@330 259 }
Chris@330 260 }
Chris@330 261
Chris@330 262 std::map<QString, int> counts;
Chris@330 263 m_transforms.clear();
Chris@330 264
Chris@330 265 for (TransformDescriptionMap::iterator i = transforms.begin();
Chris@330 266 i != transforms.end(); ++i) {
Chris@330 267
Chris@330 268 TransformDescription desc = i->second;
Chris@330 269 QString identifier = desc.identifier;
Chris@330 270 QString maker = desc.maker;
Chris@330 271
Chris@330 272 QString td = desc.name;
Chris@330 273 QString tn = td.section(": ", 0, 0);
Chris@330 274 QString to = td.section(": ", 1);
Chris@330 275
Chris@330 276 if (names[tn] > 1) {
Chris@330 277 maker.replace(QRegExp(tr(" [\\(<].*$")), "");
Chris@330 278 tn = QString("%1 [%2]").arg(tn).arg(maker);
Chris@330 279 }
Chris@330 280
Chris@330 281 if (to != "") {
Chris@330 282 desc.name = QString("%1: %2").arg(tn).arg(to);
Chris@330 283 } else {
Chris@330 284 desc.name = tn;
Chris@330 285 }
Chris@330 286
Chris@330 287 m_transforms[identifier] = desc;
Chris@330 288 }
Chris@457 289
Chris@457 290 m_transformsPopulated = true;
Chris@330 291 }
Chris@330 292
Chris@330 293 void
Chris@330 294 TransformFactory::populateFeatureExtractionPlugins(TransformDescriptionMap &transforms)
Chris@330 295 {
Chris@330 296 std::vector<QString> plugs =
Chris@330 297 FeatureExtractionPluginFactory::getAllPluginIdentifiers();
Chris@330 298
Chris@330 299 for (size_t i = 0; i < plugs.size(); ++i) {
Chris@330 300
Chris@330 301 QString pluginId = plugs[i];
Chris@330 302
Chris@330 303 FeatureExtractionPluginFactory *factory =
Chris@330 304 FeatureExtractionPluginFactory::instanceFor(pluginId);
Chris@330 305
Chris@330 306 if (!factory) {
Chris@443 307 cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << endl;
Chris@330 308 continue;
Chris@330 309 }
Chris@330 310
Chris@330 311 Vamp::Plugin *plugin =
Chris@350 312 factory->instantiatePlugin(pluginId, 44100);
Chris@330 313
Chris@330 314 if (!plugin) {
Chris@443 315 cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << endl;
Chris@330 316 continue;
Chris@330 317 }
Chris@330 318
Chris@330 319 QString pluginName = plugin->getName().c_str();
Chris@330 320 QString category = factory->getPluginCategory(pluginId);
Chris@330 321
Chris@330 322 Vamp::Plugin::OutputList outputs =
Chris@330 323 plugin->getOutputDescriptors();
Chris@330 324
Chris@330 325 for (size_t j = 0; j < outputs.size(); ++j) {
Chris@330 326
Chris@330 327 QString transformId = QString("%1:%2")
Chris@330 328 .arg(pluginId).arg(outputs[j].identifier.c_str());
Chris@330 329
Chris@330 330 QString userName;
Chris@330 331 QString friendlyName;
Chris@330 332 QString units = outputs[j].unit.c_str();
Chris@330 333 QString description = plugin->getDescription().c_str();
Chris@330 334 QString maker = plugin->getMaker().c_str();
Chris@330 335 if (maker == "") maker = tr("<unknown maker>");
Chris@330 336
Chris@443 337 QString longDescription = description;
Chris@443 338
Chris@443 339 if (longDescription == "") {
Chris@330 340 if (outputs.size() == 1) {
Chris@443 341 longDescription = tr("Extract features using \"%1\" plugin (from %2)")
Chris@330 342 .arg(pluginName).arg(maker);
Chris@330 343 } else {
Chris@443 344 longDescription = tr("Extract features using \"%1\" output of \"%2\" plugin (from %3)")
Chris@330 345 .arg(outputs[j].name.c_str()).arg(pluginName).arg(maker);
Chris@330 346 }
Chris@330 347 } else {
Chris@330 348 if (outputs.size() == 1) {
Chris@443 349 longDescription = tr("%1 using \"%2\" plugin (from %3)")
Chris@443 350 .arg(longDescription).arg(pluginName).arg(maker);
Chris@330 351 } else {
Chris@443 352 longDescription = tr("%1 using \"%2\" output of \"%3\" plugin (from %4)")
Chris@443 353 .arg(longDescription).arg(outputs[j].name.c_str()).arg(pluginName).arg(maker);
Chris@330 354 }
Chris@330 355 }
Chris@330 356
Chris@330 357 if (outputs.size() == 1) {
Chris@330 358 userName = pluginName;
Chris@330 359 friendlyName = pluginName;
Chris@330 360 } else {
Chris@330 361 userName = QString("%1: %2")
Chris@330 362 .arg(pluginName)
Chris@330 363 .arg(outputs[j].name.c_str());
Chris@330 364 friendlyName = outputs[j].name.c_str();
Chris@330 365 }
Chris@330 366
Chris@330 367 bool configurable = (!plugin->getPrograms().empty() ||
Chris@330 368 !plugin->getParameterDescriptors().empty());
Chris@330 369
Chris@443 370 // cerr << "Feature extraction plugin transform: " << transformId.toStdString() << " friendly name: " << friendlyName.toStdString() << endl;
Chris@330 371
Chris@330 372 transforms[transformId] =
Chris@330 373 TransformDescription(tr("Analysis"),
Chris@332 374 category,
Chris@332 375 transformId,
Chris@332 376 userName,
Chris@332 377 friendlyName,
Chris@332 378 description,
Chris@443 379 longDescription,
Chris@332 380 maker,
Chris@332 381 units,
Chris@332 382 configurable);
Chris@330 383 }
Chris@330 384
Chris@330 385 delete plugin;
Chris@330 386 }
Chris@330 387 }
Chris@330 388
Chris@330 389 void
Chris@330 390 TransformFactory::populateRealTimePlugins(TransformDescriptionMap &transforms)
Chris@330 391 {
Chris@330 392 std::vector<QString> plugs =
Chris@330 393 RealTimePluginFactory::getAllPluginIdentifiers();
Chris@330 394
Chris@330 395 static QRegExp unitRE("[\\[\\(]([A-Za-z0-9/]+)[\\)\\]]$");
Chris@330 396
Chris@330 397 for (size_t i = 0; i < plugs.size(); ++i) {
Chris@330 398
Chris@330 399 QString pluginId = plugs[i];
Chris@330 400
Chris@330 401 RealTimePluginFactory *factory =
Chris@330 402 RealTimePluginFactory::instanceFor(pluginId);
Chris@330 403
Chris@330 404 if (!factory) {
Chris@443 405 cerr << "WARNING: TransformFactory::populateTransforms: No real time plugin factory for instance " << pluginId.toLocal8Bit().data() << endl;
Chris@330 406 continue;
Chris@330 407 }
Chris@330 408
Chris@330 409 const RealTimePluginDescriptor *descriptor =
Chris@330 410 factory->getPluginDescriptor(pluginId);
Chris@330 411
Chris@330 412 if (!descriptor) {
Chris@443 413 cerr << "WARNING: TransformFactory::populateTransforms: Failed to query plugin " << pluginId.toLocal8Bit().data() << endl;
Chris@330 414 continue;
Chris@330 415 }
Chris@330 416
Chris@330 417 //!!! if (descriptor->controlOutputPortCount == 0 ||
Chris@330 418 // descriptor->audioInputPortCount == 0) continue;
Chris@330 419
Chris@443 420 // std::cout << "TransformFactory::populateRealTimePlugins: plugin " << pluginId.toStdString() << " has " << descriptor->controlOutputPortCount << " control output ports, " << descriptor->audioOutputPortCount << " audio outputs, " << descriptor->audioInputPortCount << " audio inputs" << endl;
Chris@330 421
Chris@330 422 QString pluginName = descriptor->name.c_str();
Chris@330 423 QString category = factory->getPluginCategory(pluginId);
Chris@330 424 bool configurable = (descriptor->parameterCount > 0);
Chris@330 425 QString maker = descriptor->maker.c_str();
Chris@330 426 if (maker == "") maker = tr("<unknown maker>");
Chris@330 427
Chris@330 428 if (descriptor->audioInputPortCount > 0) {
Chris@330 429
Chris@330 430 for (size_t j = 0; j < descriptor->controlOutputPortCount; ++j) {
Chris@330 431
Chris@330 432 QString transformId = QString("%1:%2").arg(pluginId).arg(j);
Chris@330 433 QString userName;
Chris@330 434 QString units;
Chris@330 435 QString portName;
Chris@330 436
Chris@330 437 if (j < descriptor->controlOutputPortNames.size() &&
Chris@330 438 descriptor->controlOutputPortNames[j] != "") {
Chris@330 439
Chris@330 440 portName = descriptor->controlOutputPortNames[j].c_str();
Chris@330 441
Chris@330 442 userName = tr("%1: %2")
Chris@330 443 .arg(pluginName)
Chris@330 444 .arg(portName);
Chris@330 445
Chris@330 446 if (unitRE.indexIn(portName) >= 0) {
Chris@330 447 units = unitRE.cap(1);
Chris@330 448 }
Chris@330 449
Chris@330 450 } else if (descriptor->controlOutputPortCount > 1) {
Chris@330 451
Chris@330 452 userName = tr("%1: Output %2")
Chris@330 453 .arg(pluginName)
Chris@330 454 .arg(j + 1);
Chris@330 455
Chris@330 456 } else {
Chris@330 457
Chris@330 458 userName = pluginName;
Chris@330 459 }
Chris@330 460
Chris@330 461 QString description;
Chris@330 462
Chris@330 463 if (portName != "") {
Chris@330 464 description = tr("Extract \"%1\" data output from \"%2\" effect plugin (from %3)")
Chris@330 465 .arg(portName)
Chris@330 466 .arg(pluginName)
Chris@330 467 .arg(maker);
Chris@330 468 } else {
Chris@330 469 description = tr("Extract data output %1 from \"%2\" effect plugin (from %3)")
Chris@330 470 .arg(j + 1)
Chris@330 471 .arg(pluginName)
Chris@330 472 .arg(maker);
Chris@330 473 }
Chris@330 474
Chris@330 475 transforms[transformId] =
Chris@330 476 TransformDescription(tr("Effects Data"),
Chris@332 477 category,
Chris@332 478 transformId,
Chris@332 479 userName,
Chris@332 480 userName,
Chris@443 481 "",
Chris@332 482 description,
Chris@332 483 maker,
Chris@332 484 units,
Chris@332 485 configurable);
Chris@330 486 }
Chris@330 487 }
Chris@330 488
Chris@330 489 if (!descriptor->isSynth || descriptor->audioInputPortCount > 0) {
Chris@330 490
Chris@330 491 if (descriptor->audioOutputPortCount > 0) {
Chris@330 492
Chris@330 493 QString transformId = QString("%1:A").arg(pluginId);
Chris@330 494 QString type = tr("Effects");
Chris@330 495
Chris@330 496 QString description = tr("Transform audio signal with \"%1\" effect plugin (from %2)")
Chris@330 497 .arg(pluginName)
Chris@330 498 .arg(maker);
Chris@330 499
Chris@330 500 if (descriptor->audioInputPortCount == 0) {
Chris@330 501 type = tr("Generators");
Chris@330 502 QString description = tr("Generate audio signal using \"%1\" plugin (from %2)")
Chris@330 503 .arg(pluginName)
Chris@330 504 .arg(maker);
Chris@330 505 }
Chris@330 506
Chris@330 507 transforms[transformId] =
Chris@330 508 TransformDescription(type,
Chris@332 509 category,
Chris@332 510 transformId,
Chris@332 511 pluginName,
Chris@332 512 pluginName,
Chris@443 513 "",
Chris@332 514 description,
Chris@332 515 maker,
Chris@332 516 "",
Chris@332 517 configurable);
Chris@330 518 }
Chris@330 519 }
Chris@330 520 }
Chris@330 521 }
Chris@330 522
Chris@457 523 void
Chris@457 524 TransformFactory::populateUninstalledTransforms()
Chris@457 525 {
Chris@460 526 populateTransforms();
Chris@460 527
Chris@460 528 MutexLocker locker(&m_uninstalledTransformsMutex,
Chris@460 529 "TransformFactory::populateUninstalledTransforms");
Chris@460 530 if (m_uninstalledTransformsPopulated) return;
Chris@460 531
Chris@460 532 PluginRDFIndexer::getInstance()->indexURL
Chris@460 533 ("http://www.vamp-plugins.org/rdf/plugins/vamp-example-plugins");
Chris@457 534
Chris@457 535 //!!! This will be amazingly slow
Chris@457 536
Chris@457 537 QStringList ids = PluginRDFIndexer::getInstance()->getIndexedPluginIds();
Chris@457 538
Chris@457 539 for (QStringList::const_iterator i = ids.begin(); i != ids.end(); ++i) {
Chris@457 540
Chris@457 541 PluginRDFDescription desc(*i);
Chris@457 542
Chris@457 543 QString name = desc.getPluginName();
Chris@457 544 // if (name == "") {
Chris@457 545 // std::cerr << "TransformFactory::populateUninstalledTransforms: "
Chris@457 546 // << "No name available for plugin " << i->toStdString()
Chris@457 547 // << ", skipping" << std::endl;
Chris@457 548 // continue;
Chris@457 549 // }
Chris@457 550
Chris@457 551 QString description = desc.getPluginDescription();
Chris@457 552 QString maker = desc.getPluginMaker();
Chris@457 553
Chris@457 554 QStringList oids = desc.getOutputIds();
Chris@457 555
Chris@457 556 for (QStringList::const_iterator j = oids.begin(); j != oids.end(); ++j) {
Chris@457 557
Chris@457 558 TransformId tid = Transform::getIdentifierForPluginOutput(*i, *j);
Chris@457 559
Chris@457 560 if (m_transforms.find(tid) != m_transforms.end()) {
Chris@457 561 std::cerr << "TransformFactory::populateUninstalledTransforms: "
Chris@457 562 << tid.toStdString() << " is installed, skipping" << std::endl;
Chris@457 563 continue;
Chris@457 564 }
Chris@457 565
Chris@457 566 std::cerr << "TransformFactory::populateUninstalledTransforms: "
Chris@457 567 << "adding " << tid.toStdString() << std::endl;
Chris@457 568
Chris@457 569 QString oname = desc.getOutputName(*j);
Chris@457 570 if (oname == "") oname = *j;
Chris@457 571
Chris@457 572 TransformDescription td;
Chris@457 573 td.type = tr("Analysis"); //!!! should be enum or something
Chris@457 574 td.category = "";
Chris@457 575 td.identifier = tid;
Chris@457 576
Chris@457 577 if (oids.size() == 1) {
Chris@457 578 td.name = name;
Chris@457 579 } else if (name != "") {
Chris@457 580 td.name = tr("%1: %2").arg(name).arg(oname);
Chris@457 581 }
Chris@457 582
Chris@457 583 td.friendlyName = name; //!!!???
Chris@457 584 td.description = description;
Chris@457 585 td.longDescription = ""; //!!!
Chris@457 586 td.maker = maker;
Chris@457 587 td.units = "";
Chris@457 588 td.configurable = false;
Chris@457 589
Chris@457 590 m_uninstalledTransforms[tid] = td;
Chris@457 591 }
Chris@457 592 }
Chris@457 593
Chris@457 594 m_uninstalledTransformsPopulated = true;
Chris@460 595
Chris@460 596 std::cerr << "populateUninstalledTransforms exiting" << std::endl;
Chris@457 597 }
Chris@350 598
Chris@350 599 Transform
Chris@350 600 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate)
Chris@350 601 {
Chris@350 602 Transform t;
Chris@350 603 t.setIdentifier(id);
Chris@350 604 if (rate != 0) t.setSampleRate(rate);
Chris@350 605
Chris@351 606 Vamp::PluginBase *plugin = instantiateDefaultPluginFor(id, rate);
Chris@350 607
Chris@350 608 if (plugin) {
Chris@366 609 t.setPluginVersion(QString("%1").arg(plugin->getPluginVersion()));
Chris@350 610 setParametersFromPlugin(t, plugin);
Chris@350 611 makeContextConsistentWithPlugin(t, plugin);
Chris@350 612 delete plugin;
Chris@350 613 }
Chris@350 614
Chris@350 615 return t;
Chris@350 616 }
Chris@350 617
Chris@350 618 Vamp::PluginBase *
Chris@351 619 TransformFactory::instantiatePluginFor(const Transform &transform)
Chris@351 620 {
Chris@351 621 Vamp::PluginBase *plugin = instantiateDefaultPluginFor
Chris@351 622 (transform.getIdentifier(), transform.getSampleRate());
Chris@351 623 if (plugin) {
Chris@351 624 setPluginParameters(transform, plugin);
Chris@351 625 }
Chris@351 626 return plugin;
Chris@351 627 }
Chris@351 628
Chris@351 629 Vamp::PluginBase *
Chris@351 630 TransformFactory::instantiateDefaultPluginFor(TransformId identifier, size_t rate)
Chris@350 631 {
Chris@350 632 Transform t;
Chris@350 633 t.setIdentifier(identifier);
Chris@350 634 if (rate == 0) rate = 44100;
Chris@350 635 QString pluginId = t.getPluginIdentifier();
Chris@350 636
Chris@350 637 Vamp::PluginBase *plugin = 0;
Chris@350 638
Chris@350 639 if (t.getType() == Transform::FeatureExtraction) {
Chris@350 640
Chris@350 641 FeatureExtractionPluginFactory *factory =
Chris@350 642 FeatureExtractionPluginFactory::instanceFor(pluginId);
Chris@350 643
Chris@439 644 if (factory) {
Chris@439 645 plugin = factory->instantiatePlugin(pluginId, rate);
Chris@439 646 }
Chris@350 647
Chris@350 648 } else {
Chris@350 649
Chris@350 650 RealTimePluginFactory *factory =
Chris@350 651 RealTimePluginFactory::instanceFor(pluginId);
Chris@439 652
Chris@439 653 if (factory) {
Chris@439 654 plugin = factory->instantiatePlugin(pluginId, 0, 0, rate, 1024, 1);
Chris@439 655 }
Chris@350 656 }
Chris@350 657
Chris@350 658 return plugin;
Chris@350 659 }
Chris@350 660
Chris@350 661 Vamp::Plugin *
Chris@350 662 TransformFactory::downcastVampPlugin(Vamp::PluginBase *plugin)
Chris@350 663 {
Chris@350 664 Vamp::Plugin *vp = dynamic_cast<Vamp::Plugin *>(plugin);
Chris@350 665 if (!vp) {
Chris@443 666 // cerr << "makeConsistentWithPlugin: not a Vamp::Plugin" << endl;
Chris@350 667 vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin); //!!! why?
Chris@350 668 }
Chris@350 669 if (!vp) {
Chris@443 670 // cerr << "makeConsistentWithPlugin: not a Vamp::PluginHostAdapter" << endl;
Chris@350 671 vp = dynamic_cast<Vamp::HostExt::PluginWrapper *>(plugin); //!!! no, I mean really why?
Chris@350 672 }
Chris@350 673 if (!vp) {
Chris@443 674 // cerr << "makeConsistentWithPlugin: not a Vamp::HostExt::PluginWrapper" << endl;
Chris@350 675 }
Chris@350 676 return vp;
Chris@350 677 }
Chris@350 678
Chris@330 679 bool
Chris@330 680 TransformFactory::haveTransform(TransformId identifier)
Chris@330 681 {
Chris@460 682 populateTransforms();
Chris@330 683 return (m_transforms.find(identifier) != m_transforms.end());
Chris@330 684 }
Chris@330 685
Chris@330 686 QString
Chris@330 687 TransformFactory::getTransformName(TransformId identifier)
Chris@330 688 {
Chris@330 689 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@330 690 return m_transforms[identifier].name;
Chris@330 691 } else return "";
Chris@330 692 }
Chris@330 693
Chris@330 694 QString
Chris@330 695 TransformFactory::getTransformFriendlyName(TransformId identifier)
Chris@330 696 {
Chris@330 697 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@330 698 return m_transforms[identifier].friendlyName;
Chris@330 699 } else return "";
Chris@330 700 }
Chris@330 701
Chris@330 702 QString
Chris@330 703 TransformFactory::getTransformUnits(TransformId identifier)
Chris@330 704 {
Chris@330 705 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@330 706 return m_transforms[identifier].units;
Chris@330 707 } else return "";
Chris@330 708 }
Chris@330 709
Chris@350 710 Vamp::Plugin::InputDomain
Chris@350 711 TransformFactory::getTransformInputDomain(TransformId identifier)
Chris@350 712 {
Chris@350 713 Transform transform;
Chris@350 714 transform.setIdentifier(identifier);
Chris@350 715
Chris@350 716 if (transform.getType() != Transform::FeatureExtraction) {
Chris@350 717 return Vamp::Plugin::TimeDomain;
Chris@350 718 }
Chris@350 719
Chris@350 720 Vamp::Plugin *plugin =
Chris@351 721 downcastVampPlugin(instantiateDefaultPluginFor(identifier, 0));
Chris@350 722
Chris@350 723 if (plugin) {
Chris@350 724 Vamp::Plugin::InputDomain d = plugin->getInputDomain();
Chris@350 725 delete plugin;
Chris@350 726 return d;
Chris@350 727 }
Chris@350 728
Chris@350 729 return Vamp::Plugin::TimeDomain;
Chris@350 730 }
Chris@350 731
Chris@330 732 bool
Chris@330 733 TransformFactory::isTransformConfigurable(TransformId identifier)
Chris@330 734 {
Chris@330 735 if (m_transforms.find(identifier) != m_transforms.end()) {
Chris@330 736 return m_transforms[identifier].configurable;
Chris@330 737 } else return false;
Chris@330 738 }
Chris@330 739
Chris@330 740 bool
Chris@330 741 TransformFactory::getTransformChannelRange(TransformId identifier,
Chris@330 742 int &min, int &max)
Chris@330 743 {
Chris@330 744 QString id = identifier.section(':', 0, 2);
Chris@330 745
Chris@330 746 if (FeatureExtractionPluginFactory::instanceFor(id)) {
Chris@330 747
Chris@330 748 Vamp::Plugin *plugin =
Chris@330 749 FeatureExtractionPluginFactory::instanceFor(id)->
Chris@350 750 instantiatePlugin(id, 44100);
Chris@330 751 if (!plugin) return false;
Chris@330 752
Chris@330 753 min = plugin->getMinChannelCount();
Chris@330 754 max = plugin->getMaxChannelCount();
Chris@330 755 delete plugin;
Chris@330 756
Chris@330 757 return true;
Chris@330 758
Chris@330 759 } else if (RealTimePluginFactory::instanceFor(id)) {
Chris@330 760
Chris@350 761 // don't need to instantiate
Chris@350 762
Chris@330 763 const RealTimePluginDescriptor *descriptor =
Chris@330 764 RealTimePluginFactory::instanceFor(id)->
Chris@330 765 getPluginDescriptor(id);
Chris@330 766 if (!descriptor) return false;
Chris@330 767
Chris@330 768 min = descriptor->audioInputPortCount;
Chris@330 769 max = descriptor->audioInputPortCount;
Chris@330 770
Chris@330 771 return true;
Chris@330 772 }
Chris@330 773
Chris@330 774 return false;
Chris@330 775 }
Chris@332 776
Chris@332 777 void
Chris@332 778 TransformFactory::setParametersFromPlugin(Transform &transform,
Chris@332 779 Vamp::PluginBase *plugin)
Chris@332 780 {
Chris@332 781 Transform::ParameterMap pmap;
Chris@332 782
Chris@350 783 //!!! record plugin & API version
Chris@350 784
Chris@350 785 //!!! check that this is the right plugin!
Chris@350 786
Chris@332 787 Vamp::PluginBase::ParameterList parameters =
Chris@332 788 plugin->getParameterDescriptors();
Chris@332 789
Chris@332 790 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
Chris@332 791 i != parameters.end(); ++i) {
Chris@332 792 pmap[i->identifier.c_str()] = plugin->getParameter(i->identifier);
Chris@332 793 }
Chris@332 794
Chris@332 795 transform.setParameters(pmap);
Chris@332 796
Chris@332 797 if (plugin->getPrograms().empty()) {
Chris@332 798 transform.setProgram("");
Chris@332 799 } else {
Chris@332 800 transform.setProgram(plugin->getCurrentProgram().c_str());
Chris@332 801 }
Chris@332 802
Chris@332 803 RealTimePluginInstance *rtpi =
Chris@332 804 dynamic_cast<RealTimePluginInstance *>(plugin);
Chris@332 805
Chris@332 806 Transform::ConfigurationMap cmap;
Chris@332 807
Chris@332 808 if (rtpi) {
Chris@332 809
Chris@332 810 RealTimePluginInstance::ConfigurationPairMap configurePairs =
Chris@332 811 rtpi->getConfigurePairs();
Chris@332 812
Chris@332 813 for (RealTimePluginInstance::ConfigurationPairMap::const_iterator i
Chris@332 814 = configurePairs.begin(); i != configurePairs.end(); ++i) {
Chris@332 815 cmap[i->first.c_str()] = i->second.c_str();
Chris@332 816 }
Chris@332 817 }
Chris@332 818
Chris@332 819 transform.setConfiguration(cmap);
Chris@332 820 }
Chris@332 821
Chris@332 822 void
Chris@350 823 TransformFactory::setPluginParameters(const Transform &transform,
Chris@350 824 Vamp::PluginBase *plugin)
Chris@350 825 {
Chris@350 826 //!!! check plugin & API version (see e.g. PluginXml::setParameters)
Chris@350 827
Chris@350 828 //!!! check that this is the right plugin!
Chris@350 829
Chris@350 830 RealTimePluginInstance *rtpi =
Chris@350 831 dynamic_cast<RealTimePluginInstance *>(plugin);
Chris@350 832
Chris@350 833 if (rtpi) {
Chris@350 834 const Transform::ConfigurationMap &cmap = transform.getConfiguration();
Chris@350 835 for (Transform::ConfigurationMap::const_iterator i = cmap.begin();
Chris@350 836 i != cmap.end(); ++i) {
Chris@350 837 rtpi->configure(i->first.toStdString(), i->second.toStdString());
Chris@350 838 }
Chris@350 839 }
Chris@350 840
Chris@350 841 if (transform.getProgram() != "") {
Chris@350 842 plugin->selectProgram(transform.getProgram().toStdString());
Chris@350 843 }
Chris@350 844
Chris@350 845 const Transform::ParameterMap &pmap = transform.getParameters();
Chris@350 846
Chris@350 847 Vamp::PluginBase::ParameterList parameters =
Chris@350 848 plugin->getParameterDescriptors();
Chris@350 849
Chris@350 850 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
Chris@350 851 i != parameters.end(); ++i) {
Chris@350 852 QString key = i->identifier.c_str();
Chris@350 853 Transform::ParameterMap::const_iterator pmi = pmap.find(key);
Chris@350 854 if (pmi != pmap.end()) {
Chris@350 855 plugin->setParameter(i->identifier, pmi->second);
Chris@350 856 }
Chris@350 857 }
Chris@350 858 }
Chris@350 859
Chris@350 860 void
Chris@332 861 TransformFactory::makeContextConsistentWithPlugin(Transform &transform,
Chris@332 862 Vamp::PluginBase *plugin)
Chris@332 863 {
Chris@350 864 const Vamp::Plugin *vp = downcastVampPlugin(plugin);
Chris@332 865
Chris@332 866 if (!vp) {
Chris@332 867 // time domain input for real-time effects plugin
Chris@332 868 if (!transform.getBlockSize()) {
Chris@332 869 if (!transform.getStepSize()) transform.setStepSize(1024);
Chris@332 870 transform.setBlockSize(transform.getStepSize());
Chris@332 871 } else {
Chris@332 872 transform.setStepSize(transform.getBlockSize());
Chris@332 873 }
Chris@332 874 } else {
Chris@332 875 Vamp::Plugin::InputDomain domain = vp->getInputDomain();
Chris@332 876 if (!transform.getStepSize()) {
Chris@332 877 transform.setStepSize(vp->getPreferredStepSize());
Chris@332 878 }
Chris@332 879 if (!transform.getBlockSize()) {
Chris@332 880 transform.setBlockSize(vp->getPreferredBlockSize());
Chris@332 881 }
Chris@332 882 if (!transform.getBlockSize()) {
Chris@332 883 transform.setBlockSize(1024);
Chris@332 884 }
Chris@332 885 if (!transform.getStepSize()) {
Chris@332 886 if (domain == Vamp::Plugin::FrequencyDomain) {
Chris@443 887 // cerr << "frequency domain, step = " << blockSize/2 << endl;
Chris@332 888 transform.setStepSize(transform.getBlockSize()/2);
Chris@332 889 } else {
Chris@443 890 // cerr << "time domain, step = " << blockSize/2 << endl;
Chris@332 891 transform.setStepSize(transform.getBlockSize());
Chris@332 892 }
Chris@332 893 }
Chris@332 894 }
Chris@332 895 }
Chris@332 896
Chris@350 897 QString
Chris@350 898 TransformFactory::getPluginConfigurationXml(const Transform &t)
Chris@332 899 {
Chris@350 900 QString xml;
Chris@350 901
Chris@351 902 Vamp::PluginBase *plugin = instantiateDefaultPluginFor
Chris@351 903 (t.getIdentifier(), 0);
Chris@350 904 if (!plugin) {
Chris@443 905 cerr << "TransformFactory::getPluginConfigurationXml: "
Chris@350 906 << "Unable to instantiate plugin for transform \""
Chris@443 907 << t.getIdentifier().toStdString() << "\"" << endl;
Chris@350 908 return xml;
Chris@332 909 }
Chris@332 910
Chris@351 911 setPluginParameters(t, plugin);
Chris@351 912
Chris@350 913 QTextStream out(&xml);
Chris@350 914 PluginXml(plugin).toXml(out);
Chris@350 915 delete plugin;
Chris@332 916
Chris@350 917 return xml;
Chris@350 918 }
Chris@332 919
Chris@350 920 void
Chris@350 921 TransformFactory::setParametersFromPluginConfigurationXml(Transform &t,
Chris@350 922 QString xml)
Chris@350 923 {
Chris@351 924 Vamp::PluginBase *plugin = instantiateDefaultPluginFor
Chris@351 925 (t.getIdentifier(), 0);
Chris@350 926 if (!plugin) {
Chris@443 927 cerr << "TransformFactory::setParametersFromPluginConfigurationXml: "
Chris@350 928 << "Unable to instantiate plugin for transform \""
Chris@443 929 << t.getIdentifier().toStdString() << "\"" << endl;
Chris@350 930 return;
Chris@332 931 }
Chris@332 932
Chris@350 933 PluginXml(plugin).setParametersFromXml(xml);
Chris@350 934 setParametersFromPlugin(t, plugin);
Chris@350 935 delete plugin;
Chris@332 936 }
Chris@332 937
Chris@443 938 TransformFactory::SearchResults
Chris@443 939 TransformFactory::search(QString keyword)
Chris@443 940 {
Chris@443 941 QStringList keywords;
Chris@443 942 keywords << keyword;
Chris@443 943 return search(keywords);
Chris@443 944 }
Chris@443 945
Chris@443 946 TransformFactory::SearchResults
Chris@443 947 TransformFactory::search(QStringList keywords)
Chris@443 948 {
Chris@460 949 populateTransforms();
Chris@443 950
Chris@447 951 if (keywords.size() > 1) {
Chris@447 952 // Additional score for all keywords in a row
Chris@447 953 keywords.push_back(keywords.join(" "));
Chris@447 954 }
Chris@447 955
Chris@443 956 SearchResults results;
Chris@457 957 TextMatcher matcher;
Chris@443 958
Chris@443 959 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
Chris@443 960 i != m_transforms.end(); ++i) {
Chris@443 961
Chris@457 962 TextMatcher::Match match;
Chris@443 963
Chris@457 964 match.key = i->first;
Chris@443 965
Chris@457 966 matcher.test(match, keywords, i->second.type, tr("Plugin type"), 5);
Chris@457 967 matcher.test(match, keywords, i->second.category, tr("Category"), 20);
Chris@457 968 matcher.test(match, keywords, i->second.identifier, tr("System Identifier"), 6);
Chris@457 969 matcher.test(match, keywords, i->second.name, tr("Name"), 30);
Chris@457 970 matcher.test(match, keywords, i->second.description, tr("Description"), 20);
Chris@457 971 matcher.test(match, keywords, i->second.maker, tr("Maker"), 10);
Chris@457 972 matcher.test(match, keywords, i->second.units, tr("Units"), 10);
Chris@457 973
Chris@457 974 if (match.score > 0) results[i->first] = match;
Chris@457 975 }
Chris@457 976
Chris@460 977 if (!m_uninstalledTransformsMutex.tryLock()) {
Chris@460 978 // uninstalled transforms are being populated; this may take some time,
Chris@460 979 // and they aren't critical
Chris@460 980 std::cerr << "TransformFactory::search: Uninstalled transforms mutex is held, skipping" << std::endl;
Chris@460 981 return results;
Chris@460 982 }
Chris@460 983
Chris@460 984 if (!m_uninstalledTransformsPopulated) {
Chris@460 985 std::cerr << "WARNING: TransformFactory::search: Uninstalled transforms are not populated yet" << endl
Chris@460 986 << "and are not being populated either -- was the thread not started correctly?" << endl;
Chris@460 987 m_uninstalledTransformsMutex.unlock();
Chris@460 988 return results;
Chris@460 989 }
Chris@460 990
Chris@460 991 m_uninstalledTransformsMutex.unlock();
Chris@457 992
Chris@457 993 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin();
Chris@457 994 i != m_uninstalledTransforms.end(); ++i) {
Chris@457 995
Chris@457 996 TextMatcher::Match match;
Chris@457 997
Chris@457 998 match.key = i->first;
Chris@457 999
Chris@457 1000 matcher.test(match, keywords, i->second.type, tr("Plugin type"), 2);
Chris@457 1001 matcher.test(match, keywords, i->second.category, tr("Category"), 10);
Chris@457 1002 matcher.test(match, keywords, i->second.identifier, tr("System Identifier"), 3);
Chris@457 1003 matcher.test(match, keywords, i->second.name, tr("Name"), 15);
Chris@457 1004 matcher.test(match, keywords, i->second.description, tr("Description"), 10);
Chris@457 1005 matcher.test(match, keywords, i->second.maker, tr("Maker"), 5);
Chris@457 1006 matcher.test(match, keywords, i->second.units, tr("Units"), 5);
Chris@443 1007
Chris@443 1008 if (match.score > 0) results[i->first] = match;
Chris@443 1009 }
Chris@443 1010
Chris@443 1011 return results;
Chris@443 1012 }
Chris@443 1013