annotate plugin/LADSPAPluginFactory.cpp @ 57:7439f1696314

* Add editing for auralisation plugin parameters and programs * Rename and reorganise the sample plugin sample set
author Chris Cannam
date Thu, 23 Mar 2006 15:49:41 +0000
parents d397ea0a79f5
children 3086ff194ea0
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@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam and Richard Bown.
Chris@0 19 */
Chris@0 20
Chris@0 21 #include "LADSPAPluginFactory.h"
Chris@0 22 #include <iostream>
Chris@0 23
Chris@0 24 #include <QDir>
Chris@0 25 #include <QFile>
Chris@0 26 #include <QTextStream>
Chris@0 27
Chris@0 28 #include <cmath>
Chris@0 29
Chris@0 30 #include "LADSPAPluginInstance.h"
Chris@0 31 #include "PluginIdentifier.h"
Chris@0 32
Chris@0 33 #include "base/System.h"
Chris@0 34
Chris@35 35 #ifdef HAVE_LRDF
Chris@0 36 #include "lrdf.h"
Chris@35 37 #endif // HAVE_LRDF
Chris@0 38
Chris@0 39
Chris@0 40 LADSPAPluginFactory::LADSPAPluginFactory()
Chris@0 41 {
Chris@0 42 }
Chris@0 43
Chris@0 44 LADSPAPluginFactory::~LADSPAPluginFactory()
Chris@0 45 {
Chris@0 46 for (std::set<RealTimePluginInstance *>::iterator i = m_instances.begin();
Chris@0 47 i != m_instances.end(); ++i) {
Chris@0 48 (*i)->setFactory(0);
Chris@0 49 delete *i;
Chris@0 50 }
Chris@0 51 m_instances.clear();
Chris@0 52 unloadUnusedLibraries();
Chris@0 53 }
Chris@0 54
Chris@0 55 const std::vector<QString> &
Chris@0 56 LADSPAPluginFactory::getPluginIdentifiers() const
Chris@0 57 {
Chris@0 58 return m_identifiers;
Chris@0 59 }
Chris@0 60
Chris@0 61 void
Chris@0 62 LADSPAPluginFactory::enumeratePlugins(std::vector<QString> &list)
Chris@0 63 {
Chris@0 64 for (std::vector<QString>::iterator i = m_identifiers.begin();
Chris@0 65 i != m_identifiers.end(); ++i) {
Chris@0 66
Chris@0 67 const LADSPA_Descriptor *descriptor = getLADSPADescriptor(*i);
Chris@0 68
Chris@0 69 if (!descriptor) {
Chris@0 70 std::cerr << "WARNING: LADSPAPluginFactory::enumeratePlugins: couldn't get descriptor for identifier " << i->toStdString() << std::endl;
Chris@0 71 continue;
Chris@0 72 }
Chris@0 73
Chris@0 74 list.push_back(*i);
Chris@0 75 list.push_back(descriptor->Name);
Chris@0 76 list.push_back(QString("%1").arg(descriptor->UniqueID));
Chris@0 77 list.push_back(descriptor->Label);
Chris@0 78 list.push_back(descriptor->Maker);
Chris@0 79 list.push_back(descriptor->Copyright);
Chris@0 80 list.push_back("false"); // is synth
Chris@0 81 list.push_back("false"); // is grouped
Chris@0 82
Chris@0 83 if (m_taxonomy.find(descriptor->UniqueID) != m_taxonomy.end() &&
Chris@0 84 m_taxonomy[descriptor->UniqueID] != "") {
Chris@0 85 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString()<< " found in taxonomy as " << m_taxonomy[descriptor->UniqueID] << std::endl;
Chris@0 86 list.push_back(m_taxonomy[descriptor->UniqueID]);
Chris@0 87
Chris@0 88 } else if (m_fallbackCategories.find(*i) !=
Chris@0 89 m_fallbackCategories.end()) {
Chris@0 90 list.push_back(m_fallbackCategories[*i]);
Chris@0 91 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString() <<" found in fallbacks as " << m_fallbackCategories[*i] << std::endl;
Chris@0 92
Chris@0 93 } else {
Chris@0 94 list.push_back("");
Chris@0 95 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString() << " not found (despite having " << m_fallbackCategories.size() << " fallbacks)" << std::endl;
Chris@0 96
Chris@0 97 }
Chris@0 98
Chris@0 99 list.push_back(QString("%1").arg(descriptor->PortCount));
Chris@0 100
Chris@0 101 for (unsigned long p = 0; p < descriptor->PortCount; ++p) {
Chris@0 102
Chris@0 103 int type = 0;
Chris@0 104 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) {
Chris@0 105 type |= PortType::Control;
Chris@0 106 } else {
Chris@0 107 type |= PortType::Audio;
Chris@0 108 }
Chris@0 109 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) {
Chris@0 110 type |= PortType::Input;
Chris@0 111 } else {
Chris@0 112 type |= PortType::Output;
Chris@0 113 }
Chris@0 114
Chris@0 115 list.push_back(QString("%1").arg(p));
Chris@0 116 list.push_back(descriptor->PortNames[p]);
Chris@0 117 list.push_back(QString("%1").arg(type));
Chris@0 118 list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p)));
Chris@0 119 list.push_back(QString("%1").arg(getPortMinimum(descriptor, p)));
Chris@0 120 list.push_back(QString("%1").arg(getPortMaximum(descriptor, p)));
Chris@0 121 list.push_back(QString("%1").arg(getPortDefault(descriptor, p)));
Chris@0 122 }
Chris@0 123 }
Chris@0 124
Chris@0 125 unloadUnusedLibraries();
Chris@0 126 }
Chris@0 127
Chris@0 128 float
Chris@0 129 LADSPAPluginFactory::getPortMinimum(const LADSPA_Descriptor *descriptor, int port)
Chris@0 130 {
Chris@0 131 LADSPA_PortRangeHintDescriptor d =
Chris@0 132 descriptor->PortRangeHints[port].HintDescriptor;
Chris@0 133
Chris@0 134 float minimum = 0.0;
Chris@0 135
Chris@0 136 if (LADSPA_IS_HINT_BOUNDED_BELOW(d)) {
Chris@0 137 float lb = descriptor->PortRangeHints[port].LowerBound;
Chris@0 138 minimum = lb;
Chris@0 139 } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
Chris@0 140 float ub = descriptor->PortRangeHints[port].UpperBound;
Chris@0 141 minimum = std::min(0.0, ub - 1.0);
Chris@0 142 }
Chris@0 143
Chris@0 144 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
Chris@0 145 minimum *= m_sampleRate;
Chris@0 146 }
Chris@0 147
Chris@0 148 return minimum;
Chris@0 149 }
Chris@0 150
Chris@0 151 float
Chris@0 152 LADSPAPluginFactory::getPortMaximum(const LADSPA_Descriptor *descriptor, int port)
Chris@0 153 {
Chris@0 154 LADSPA_PortRangeHintDescriptor d =
Chris@0 155 descriptor->PortRangeHints[port].HintDescriptor;
Chris@0 156
Chris@0 157 float maximum = 1.0;
Chris@0 158
Chris@0 159 if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
Chris@0 160 float ub = descriptor->PortRangeHints[port].UpperBound;
Chris@0 161 maximum = ub;
Chris@0 162 } else {
Chris@0 163 float lb = descriptor->PortRangeHints[port].LowerBound;
Chris@0 164 maximum = lb + 1.0;
Chris@0 165 }
Chris@0 166
Chris@0 167 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
Chris@0 168 maximum *= m_sampleRate;
Chris@0 169 }
Chris@0 170
Chris@0 171 return maximum;
Chris@0 172 }
Chris@0 173
Chris@0 174 float
Chris@0 175 LADSPAPluginFactory::getPortDefault(const LADSPA_Descriptor *descriptor, int port)
Chris@0 176 {
Chris@0 177 float minimum = getPortMinimum(descriptor, port);
Chris@0 178 float maximum = getPortMaximum(descriptor, port);
Chris@0 179 float deft;
Chris@0 180
Chris@0 181 if (m_portDefaults.find(descriptor->UniqueID) !=
Chris@0 182 m_portDefaults.end()) {
Chris@0 183 if (m_portDefaults[descriptor->UniqueID].find(port) !=
Chris@0 184 m_portDefaults[descriptor->UniqueID].end()) {
Chris@0 185
Chris@0 186 deft = m_portDefaults[descriptor->UniqueID][port];
Chris@0 187 if (deft < minimum) deft = minimum;
Chris@0 188 if (deft > maximum) deft = maximum;
Chris@0 189 return deft;
Chris@0 190 }
Chris@0 191 }
Chris@0 192
Chris@0 193 LADSPA_PortRangeHintDescriptor d =
Chris@0 194 descriptor->PortRangeHints[port].HintDescriptor;
Chris@0 195
Chris@0 196 bool logarithmic = LADSPA_IS_HINT_LOGARITHMIC(d);
Chris@0 197
Chris@0 198 if (!LADSPA_IS_HINT_HAS_DEFAULT(d)) {
Chris@0 199
Chris@0 200 deft = minimum;
Chris@0 201
Chris@0 202 } else if (LADSPA_IS_HINT_DEFAULT_MINIMUM(d)) {
Chris@0 203
Chris@0 204 deft = minimum;
Chris@0 205
Chris@0 206 } else if (LADSPA_IS_HINT_DEFAULT_LOW(d)) {
Chris@0 207
Chris@0 208 if (logarithmic) {
Chris@0 209 deft = powf(10, log10(minimum) * 0.75 +
Chris@0 210 log10(maximum) * 0.25);
Chris@0 211 } else {
Chris@0 212 deft = minimum * 0.75 + maximum * 0.25;
Chris@0 213 }
Chris@0 214
Chris@0 215 } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(d)) {
Chris@0 216
Chris@0 217 if (logarithmic) {
Chris@0 218 deft = powf(10, log10(minimum) * 0.5 +
Chris@0 219 log10(maximum) * 0.5);
Chris@0 220 } else {
Chris@0 221 deft = minimum * 0.5 + maximum * 0.5;
Chris@0 222 }
Chris@0 223
Chris@0 224 } else if (LADSPA_IS_HINT_DEFAULT_HIGH(d)) {
Chris@0 225
Chris@0 226 if (logarithmic) {
Chris@0 227 deft = powf(10, log10(minimum) * 0.25 +
Chris@0 228 log10(maximum) * 0.75);
Chris@0 229 } else {
Chris@0 230 deft = minimum * 0.25 + maximum * 0.75;
Chris@0 231 }
Chris@0 232
Chris@0 233 } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(d)) {
Chris@0 234
Chris@0 235 deft = maximum;
Chris@0 236
Chris@0 237 } else if (LADSPA_IS_HINT_DEFAULT_0(d)) {
Chris@0 238
Chris@0 239 deft = 0.0;
Chris@0 240
Chris@0 241 } else if (LADSPA_IS_HINT_DEFAULT_1(d)) {
Chris@0 242
Chris@0 243 deft = 1.0;
Chris@0 244
Chris@0 245 } else if (LADSPA_IS_HINT_DEFAULT_100(d)) {
Chris@0 246
Chris@0 247 deft = 100.0;
Chris@0 248
Chris@0 249 } else if (LADSPA_IS_HINT_DEFAULT_440(d)) {
Chris@0 250
Chris@0 251 deft = 440.0;
Chris@0 252
Chris@0 253 } else {
Chris@0 254
Chris@0 255 deft = minimum;
Chris@0 256 }
Chris@0 257
Chris@0 258 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
Chris@0 259 deft *= m_sampleRate;
Chris@0 260 }
Chris@0 261
Chris@0 262 return deft;
Chris@0 263 }
Chris@0 264
Chris@57 265 float
Chris@57 266 LADSPAPluginFactory::getPortQuantization(const LADSPA_Descriptor *descriptor, int port)
Chris@57 267 {
Chris@57 268 int displayHint = getPortDisplayHint(descriptor, port);
Chris@57 269 if (displayHint & PortHint::Toggled) {
Chris@57 270 return lrintf(getPortMaximum(descriptor, port)) -
Chris@57 271 lrintf(getPortMinimum(descriptor, port));
Chris@57 272 }
Chris@57 273 if (displayHint & PortHint::Integer) {
Chris@57 274 return 1.0;
Chris@57 275 }
Chris@57 276 return 0.0;
Chris@57 277 }
Chris@57 278
Chris@0 279 int
Chris@0 280 LADSPAPluginFactory::getPortDisplayHint(const LADSPA_Descriptor *descriptor, int port)
Chris@0 281 {
Chris@0 282 LADSPA_PortRangeHintDescriptor d =
Chris@0 283 descriptor->PortRangeHints[port].HintDescriptor;
Chris@0 284 int hint = PortHint::NoHint;
Chris@0 285
Chris@0 286 if (LADSPA_IS_HINT_TOGGLED(d)) hint |= PortHint::Toggled;
Chris@0 287 if (LADSPA_IS_HINT_INTEGER(d)) hint |= PortHint::Integer;
Chris@0 288 if (LADSPA_IS_HINT_LOGARITHMIC(d)) hint |= PortHint::Logarithmic;
Chris@0 289
Chris@0 290 return hint;
Chris@0 291 }
Chris@0 292
Chris@0 293
Chris@0 294 RealTimePluginInstance *
Chris@0 295 LADSPAPluginFactory::instantiatePlugin(QString identifier,
Chris@0 296 int instrument,
Chris@0 297 int position,
Chris@0 298 unsigned int sampleRate,
Chris@0 299 unsigned int blockSize,
Chris@0 300 unsigned int channels)
Chris@0 301 {
Chris@0 302 const LADSPA_Descriptor *descriptor = getLADSPADescriptor(identifier);
Chris@0 303
Chris@0 304 if (descriptor) {
Chris@0 305
Chris@0 306 LADSPAPluginInstance *instance =
Chris@0 307 new LADSPAPluginInstance
Chris@0 308 (this, instrument, identifier, position, sampleRate, blockSize, channels,
Chris@0 309 descriptor);
Chris@0 310
Chris@0 311 m_instances.insert(instance);
Chris@0 312
Chris@0 313 return instance;
Chris@0 314 }
Chris@0 315
Chris@0 316 return 0;
Chris@0 317 }
Chris@0 318
Chris@0 319 void
Chris@0 320 LADSPAPluginFactory::releasePlugin(RealTimePluginInstance *instance,
Chris@0 321 QString identifier)
Chris@0 322 {
Chris@0 323 if (m_instances.find(instance) == m_instances.end()) {
Chris@0 324 std::cerr << "WARNING: LADSPAPluginFactory::releasePlugin: Not one of mine!"
Chris@0 325 << std::endl;
Chris@0 326 return;
Chris@0 327 }
Chris@0 328
Chris@0 329 QString type, soname, label;
Chris@0 330 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@0 331
Chris@0 332 m_instances.erase(instance);
Chris@0 333
Chris@0 334 bool stillInUse = false;
Chris@0 335
Chris@0 336 for (std::set<RealTimePluginInstance *>::iterator ii = m_instances.begin();
Chris@0 337 ii != m_instances.end(); ++ii) {
Chris@0 338 QString itype, isoname, ilabel;
Chris@0 339 PluginIdentifier::parseIdentifier((*ii)->getIdentifier(), itype, isoname, ilabel);
Chris@0 340 if (isoname == soname) {
Chris@0 341 // std::cerr << "LADSPAPluginFactory::releasePlugin: dll " << soname.toStdString() << " is still in use for plugin " << ilabel << std::endl;
Chris@0 342 stillInUse = true;
Chris@0 343 break;
Chris@0 344 }
Chris@0 345 }
Chris@0 346
Chris@0 347 if (!stillInUse) {
Chris@0 348 // std::cerr << "LADSPAPluginFactory::releasePlugin: dll " << soname.toStdString() << " no longer in use, unloading" << std::endl;
Chris@0 349 unloadLibrary(soname);
Chris@0 350 }
Chris@0 351 }
Chris@0 352
Chris@0 353 const LADSPA_Descriptor *
Chris@0 354 LADSPAPluginFactory::getLADSPADescriptor(QString identifier)
Chris@0 355 {
Chris@0 356 QString type, soname, label;
Chris@0 357 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@0 358
Chris@0 359 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
Chris@0 360 loadLibrary(soname);
Chris@0 361 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
Chris@0 362 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: loadLibrary failed for " << soname.toStdString() << std::endl;
Chris@0 363 return 0;
Chris@0 364 }
Chris@0 365 }
Chris@0 366
Chris@0 367 void *libraryHandle = m_libraryHandles[soname];
Chris@0 368
Chris@0 369 LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function)
Chris@0 370 DLSYM(libraryHandle, "ladspa_descriptor");
Chris@0 371
Chris@0 372 if (!fn) {
Chris@0 373 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No descriptor function in library " << soname.toStdString() << std::endl;
Chris@0 374 return 0;
Chris@0 375 }
Chris@0 376
Chris@0 377 const LADSPA_Descriptor *descriptor = 0;
Chris@0 378
Chris@0 379 int index = 0;
Chris@0 380 while ((descriptor = fn(index))) {
Chris@0 381 if (descriptor->Label == label) return descriptor;
Chris@0 382 ++index;
Chris@0 383 }
Chris@0 384
Chris@0 385 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No such plugin as " << label.toStdString() << " in library " << soname.toStdString() << std::endl;
Chris@0 386
Chris@0 387 return 0;
Chris@0 388 }
Chris@0 389
Chris@0 390 void
Chris@0 391 LADSPAPluginFactory::loadLibrary(QString soName)
Chris@0 392 {
Chris@0 393 void *libraryHandle = DLOPEN(soName, RTLD_NOW);
Chris@0 394 if (libraryHandle) m_libraryHandles[soName] = libraryHandle;
Chris@0 395 }
Chris@0 396
Chris@0 397 void
Chris@0 398 LADSPAPluginFactory::unloadLibrary(QString soName)
Chris@0 399 {
Chris@0 400 LibraryHandleMap::iterator li = m_libraryHandles.find(soName);
Chris@0 401 if (li != m_libraryHandles.end()) {
Chris@0 402 // std::cerr << "unloading " << soname.toStdString() << std::endl;
Chris@0 403 DLCLOSE(m_libraryHandles[soName]);
Chris@0 404 m_libraryHandles.erase(li);
Chris@0 405 }
Chris@0 406 }
Chris@0 407
Chris@0 408 void
Chris@0 409 LADSPAPluginFactory::unloadUnusedLibraries()
Chris@0 410 {
Chris@0 411 std::vector<QString> toUnload;
Chris@0 412
Chris@0 413 for (LibraryHandleMap::iterator i = m_libraryHandles.begin();
Chris@0 414 i != m_libraryHandles.end(); ++i) {
Chris@0 415
Chris@0 416 bool stillInUse = false;
Chris@0 417
Chris@0 418 for (std::set<RealTimePluginInstance *>::iterator ii = m_instances.begin();
Chris@0 419 ii != m_instances.end(); ++ii) {
Chris@0 420
Chris@0 421 QString itype, isoname, ilabel;
Chris@0 422 PluginIdentifier::parseIdentifier((*ii)->getIdentifier(), itype, isoname, ilabel);
Chris@0 423 if (isoname == i->first) {
Chris@0 424 stillInUse = true;
Chris@0 425 break;
Chris@0 426 }
Chris@0 427 }
Chris@0 428
Chris@0 429 if (!stillInUse) toUnload.push_back(i->first);
Chris@0 430 }
Chris@0 431
Chris@0 432 for (std::vector<QString>::iterator i = toUnload.begin();
Chris@0 433 i != toUnload.end(); ++i) {
Chris@0 434 unloadLibrary(*i);
Chris@0 435 }
Chris@0 436 }
Chris@0 437
Chris@0 438
Chris@0 439 // It is only later, after they've gone,
Chris@0 440 // I realize they have delivered a letter.
Chris@0 441 // It's a letter from my wife. "What are you doing
Chris@0 442 // there?" my wife asks. "Are you drinking?"
Chris@0 443 // I study the postmark for hours. Then it, too, begins to fade.
Chris@0 444 // I hope someday to forget all this.
Chris@0 445
Chris@0 446
Chris@0 447 std::vector<QString>
Chris@0 448 LADSPAPluginFactory::getPluginPath()
Chris@0 449 {
Chris@0 450 std::vector<QString> pathList;
Chris@0 451 std::string path;
Chris@0 452
Chris@0 453 char *cpath = getenv("LADSPA_PATH");
Chris@0 454 if (cpath) path = cpath;
Chris@0 455
Chris@0 456 if (path == "") {
Chris@0 457 path = "/usr/local/lib/ladspa:/usr/lib/ladspa";
Chris@0 458 char *home = getenv("HOME");
Chris@0 459 if (home) path = std::string(home) + "/.ladspa:" + path;
Chris@0 460 }
Chris@0 461
Chris@0 462 std::string::size_type index = 0, newindex = 0;
Chris@0 463
Chris@0 464 while ((newindex = path.find(':', index)) < path.size()) {
Chris@0 465 pathList.push_back(path.substr(index, newindex - index).c_str());
Chris@0 466 index = newindex + 1;
Chris@0 467 }
Chris@0 468
Chris@0 469 pathList.push_back(path.substr(index).c_str());
Chris@0 470
Chris@0 471 return pathList;
Chris@0 472 }
Chris@0 473
Chris@0 474
Chris@35 475 #ifdef HAVE_LRDF
Chris@0 476 std::vector<QString>
Chris@0 477 LADSPAPluginFactory::getLRDFPath(QString &baseUri)
Chris@0 478 {
Chris@0 479 std::vector<QString> pathList = getPluginPath();
Chris@0 480 std::vector<QString> lrdfPaths;
Chris@0 481
Chris@0 482 lrdfPaths.push_back("/usr/local/share/ladspa/rdf");
Chris@0 483 lrdfPaths.push_back("/usr/share/ladspa/rdf");
Chris@0 484
Chris@0 485 for (std::vector<QString>::iterator i = pathList.begin();
Chris@0 486 i != pathList.end(); ++i) {
Chris@0 487 lrdfPaths.push_back(*i + "/rdf");
Chris@0 488 }
Chris@0 489
Chris@0 490 baseUri = LADSPA_BASE;
Chris@0 491 return lrdfPaths;
Chris@0 492 }
Chris@0 493 #endif
Chris@0 494
Chris@0 495 void
Chris@0 496 LADSPAPluginFactory::discoverPlugins()
Chris@0 497 {
Chris@0 498 std::vector<QString> pathList = getPluginPath();
Chris@0 499
Chris@0 500 // std::cerr << "LADSPAPluginFactory::discoverPlugins - "
Chris@0 501 // << "discovering plugins; path is ";
Chris@0 502 for (std::vector<QString>::iterator i = pathList.begin();
Chris@0 503 i != pathList.end(); ++i) {
Chris@0 504 std::cerr << "[" << i->toStdString() << "] ";
Chris@0 505 }
Chris@0 506 std::cerr << std::endl;
Chris@0 507
Chris@35 508 #ifdef HAVE_LRDF
Chris@0 509 // Initialise liblrdf and read the description files
Chris@0 510 //
Chris@0 511 lrdf_init();
Chris@0 512
Chris@0 513 QString baseUri;
Chris@0 514 std::vector<QString> lrdfPaths = getLRDFPath(baseUri);
Chris@0 515
Chris@0 516 bool haveSomething = false;
Chris@0 517
Chris@0 518 for (size_t i = 0; i < lrdfPaths.size(); ++i) {
Chris@0 519 QDir dir(lrdfPaths[i], "*.rdf;*.rdfs");
Chris@0 520 for (unsigned int j = 0; j < dir.count(); ++j) {
Chris@0 521 if (!lrdf_read_file(QString("file:" + lrdfPaths[i] + "/" + dir[j]).toStdString().c_str())) {
Chris@0 522 // std::cerr << "LADSPAPluginFactory: read RDF file " << (lrdfPaths[i] + "/" + dir[j]) << std::endl;
Chris@0 523 haveSomething = true;
Chris@0 524 }
Chris@0 525 }
Chris@0 526 }
Chris@0 527
Chris@0 528 if (haveSomething) {
Chris@0 529 generateTaxonomy(baseUri + "Plugin", "");
Chris@0 530 }
Chris@35 531 #endif // HAVE_LRDF
Chris@0 532
Chris@0 533 generateFallbackCategories();
Chris@0 534
Chris@0 535 for (std::vector<QString>::iterator i = pathList.begin();
Chris@0 536 i != pathList.end(); ++i) {
Chris@0 537
Chris@0 538 QDir pluginDir(*i, PLUGIN_GLOB);
Chris@0 539
Chris@0 540 for (unsigned int j = 0; j < pluginDir.count(); ++j) {
Chris@0 541 discoverPlugins(QString("%1/%2").arg(*i).arg(pluginDir[j]));
Chris@0 542 }
Chris@0 543 }
Chris@0 544
Chris@35 545 #ifdef HAVE_LRDF
Chris@0 546 // Cleanup after the RDF library
Chris@0 547 //
Chris@0 548 lrdf_cleanup();
Chris@35 549 #endif // HAVE_LRDF
Chris@0 550 }
Chris@0 551
Chris@0 552 void
Chris@0 553 LADSPAPluginFactory::discoverPlugins(QString soname)
Chris@0 554 {
Chris@0 555 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
Chris@0 556
Chris@0 557 if (!libraryHandle) {
Chris@0 558 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: couldn't load plugin library "
Chris@0 559 << soname.toStdString() << " - " << DLERROR() << std::endl;
Chris@0 560 return;
Chris@0 561 }
Chris@0 562
Chris@0 563 LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function)
Chris@0 564 DLSYM(libraryHandle, "ladspa_descriptor");
Chris@0 565
Chris@0 566 if (!fn) {
Chris@0 567 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: No descriptor function in " << soname.toStdString() << std::endl;
Chris@0 568 return;
Chris@0 569 }
Chris@0 570
Chris@0 571 const LADSPA_Descriptor *descriptor = 0;
Chris@0 572
Chris@0 573 int index = 0;
Chris@0 574 while ((descriptor = fn(index))) {
Chris@0 575
Chris@35 576 #ifdef HAVE_LRDF
Chris@0 577 char *def_uri = 0;
Chris@0 578 lrdf_defaults *defs = 0;
Chris@0 579
Chris@0 580 QString category = m_taxonomy[descriptor->UniqueID];
Chris@0 581
Chris@0 582 if (category == "" && descriptor->Name != 0) {
Chris@0 583 std::string name = descriptor->Name;
Chris@0 584 if (name.length() > 4 &&
Chris@0 585 name.substr(name.length() - 4) == " VST") {
Chris@0 586 category = "VST effects";
Chris@0 587 m_taxonomy[descriptor->UniqueID] = category;
Chris@0 588 }
Chris@0 589 }
Chris@0 590
Chris@0 591 // std::cerr << "Plugin id is " << descriptor->UniqueID
Chris@0 592 // << ", category is \"" << (category ? category : QString("(none)"))
Chris@0 593 // << "\", name is " << descriptor->Name
Chris@0 594 // << ", label is " << descriptor->Label
Chris@0 595 // << std::endl;
Chris@0 596
Chris@0 597 def_uri = lrdf_get_default_uri(descriptor->UniqueID);
Chris@0 598 if (def_uri) {
Chris@0 599 defs = lrdf_get_setting_values(def_uri);
Chris@0 600 }
Chris@0 601
Chris@0 602 int controlPortNumber = 1;
Chris@0 603
Chris@0 604 for (unsigned long i = 0; i < descriptor->PortCount; i++) {
Chris@0 605
Chris@0 606 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) {
Chris@0 607
Chris@0 608 if (def_uri && defs) {
Chris@0 609
Chris@0 610 for (int j = 0; j < defs->count; j++) {
Chris@0 611 if (defs->items[j].pid == controlPortNumber) {
Chris@0 612 // std::cerr << "Default for this port (" << defs->items[j].pid << ", " << defs->items[j].label << ") is " << defs->items[j].value << "; applying this to port number " << i << " with name " << descriptor->PortNames[i] << std::endl;
Chris@0 613 m_portDefaults[descriptor->UniqueID][i] =
Chris@0 614 defs->items[j].value;
Chris@0 615 }
Chris@0 616 }
Chris@0 617 }
Chris@0 618
Chris@0 619 ++controlPortNumber;
Chris@0 620 }
Chris@0 621 }
Chris@35 622 #endif // HAVE_LRDF
Chris@0 623
Chris@0 624 QString identifier = PluginIdentifier::createIdentifier
Chris@0 625 ("ladspa", soname, descriptor->Label);
Chris@0 626 m_identifiers.push_back(identifier);
Chris@0 627
Chris@0 628 ++index;
Chris@0 629 }
Chris@0 630
Chris@0 631 if (DLCLOSE(libraryHandle) != 0) {
Chris@0 632 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins - can't unload " << libraryHandle << std::endl;
Chris@0 633 return;
Chris@0 634 }
Chris@0 635 }
Chris@0 636
Chris@0 637 void
Chris@0 638 LADSPAPluginFactory::generateFallbackCategories()
Chris@0 639 {
Chris@0 640 std::vector<QString> pluginPath = getPluginPath();
Chris@0 641 std::vector<QString> path;
Chris@0 642
Chris@0 643 for (size_t i = 0; i < pluginPath.size(); ++i) {
Chris@0 644 if (pluginPath[i].contains("/lib/")) {
Chris@0 645 QString p(pluginPath[i]);
Chris@0 646 p.replace("/lib/", "/share/");
Chris@0 647 path.push_back(p);
Chris@0 648 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: path element " << p << std::endl;
Chris@0 649 }
Chris@0 650 path.push_back(pluginPath[i]);
Chris@0 651 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: path element " << pluginPath[i] << std::endl;
Chris@0 652 }
Chris@0 653
Chris@0 654 for (size_t i = 0; i < path.size(); ++i) {
Chris@0 655
Chris@0 656 QDir dir(path[i], "*.cat");
Chris@0 657
Chris@0 658 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i] << " has " << dir.count() << " .cat files" << std::endl;
Chris@0 659 for (unsigned int j = 0; j < dir.count(); ++j) {
Chris@0 660
Chris@0 661 QFile file(path[i] + "/" + dir[j]);
Chris@0 662
Chris@0 663 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i] + "/" + dir[j]) << std::endl;
Chris@0 664
Chris@0 665 if (file.open(QIODevice::ReadOnly)) {
Chris@0 666 // std::cerr << "...opened" << std::endl;
Chris@0 667 QTextStream stream(&file);
Chris@0 668 QString line;
Chris@0 669
Chris@0 670 while (!stream.atEnd()) {
Chris@0 671 line = stream.readLine();
Chris@0 672 // std::cerr << "line is: \"" << line << "\"" << std::endl;
Chris@0 673 QString id = line.section("::", 0, 0);
Chris@0 674 QString cat = line.section("::", 1, 1);
Chris@0 675 m_fallbackCategories[id] = cat;
Chris@0 676 // std::cerr << "set id \"" << id << "\" to cat \"" << cat << "\"" << std::endl;
Chris@0 677 }
Chris@0 678 }
Chris@0 679 }
Chris@0 680 }
Chris@0 681 }
Chris@0 682
Chris@0 683 void
Chris@0 684 LADSPAPluginFactory::generateTaxonomy(QString uri, QString base)
Chris@0 685 {
Chris@35 686 #ifdef HAVE_LRDF
Chris@0 687 lrdf_uris *uris = lrdf_get_instances(uri.toStdString().c_str());
Chris@0 688
Chris@0 689 if (uris != NULL) {
Chris@0 690 for (int i = 0; i < uris->count; ++i) {
Chris@0 691 m_taxonomy[lrdf_get_uid(uris->items[i])] = base;
Chris@0 692 }
Chris@0 693 lrdf_free_uris(uris);
Chris@0 694 }
Chris@0 695
Chris@0 696 uris = lrdf_get_subclasses(uri.toStdString().c_str());
Chris@0 697
Chris@0 698 if (uris != NULL) {
Chris@0 699 for (int i = 0; i < uris->count; ++i) {
Chris@0 700 char *label = lrdf_get_label(uris->items[i]);
Chris@0 701 generateTaxonomy(uris->items[i],
Chris@0 702 base + (base.length() > 0 ? " > " : "") + label);
Chris@0 703 }
Chris@0 704 lrdf_free_uris(uris);
Chris@0 705 }
Chris@0 706 #endif
Chris@0 707 }
Chris@0 708
Chris@0 709