annotate plugin/LADSPAPluginFactory.cpp @ 14:b101cc2ae1ab

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