Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: /* Chris@0: This is a modified version of a source file from the Chris@0: Rosegarden MIDI and audio sequencer and notation editor. Chris@17: This file copyright 2000-2006 Chris Cannam and Richard Bown. Chris@0: */ Chris@0: Chris@0: #include "LADSPAPluginFactory.h" Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "LADSPAPluginInstance.h" Chris@0: #include "PluginIdentifier.h" Chris@0: Chris@150: #include "system/System.h" Chris@145: #include "base/Preferences.h" Chris@408: #include "base/Profiler.h" Chris@0: Chris@260: //#define DEBUG_LADSPA_PLUGIN_FACTORY 1 Chris@260: Chris@35: #ifdef HAVE_LRDF Chris@0: #include "lrdf.h" Chris@35: #endif // HAVE_LRDF Chris@0: Chris@0: Chris@0: LADSPAPluginFactory::LADSPAPluginFactory() Chris@0: { Chris@166: #ifdef HAVE_LRDF Chris@166: lrdf_init(); Chris@166: #endif Chris@0: } Chris@0: Chris@0: LADSPAPluginFactory::~LADSPAPluginFactory() Chris@0: { Chris@0: for (std::set::iterator i = m_instances.begin(); Chris@0: i != m_instances.end(); ++i) { Chris@0: (*i)->setFactory(0); Chris@0: delete *i; Chris@0: } Chris@0: m_instances.clear(); Chris@0: unloadUnusedLibraries(); Chris@166: Chris@166: #ifdef HAVE_LRDF Chris@166: lrdf_cleanup(); Chris@166: #endif // HAVE_LRDF Chris@0: } Chris@0: Chris@0: const std::vector & Chris@0: LADSPAPluginFactory::getPluginIdentifiers() const Chris@0: { Chris@0: return m_identifiers; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::enumeratePlugins(std::vector &list) Chris@0: { Chris@408: Profiler profiler("LADSPAPluginFactory::enumeratePlugins"); Chris@408: Chris@0: for (std::vector::iterator i = m_identifiers.begin(); Chris@0: i != m_identifiers.end(); ++i) { Chris@0: Chris@0: const LADSPA_Descriptor *descriptor = getLADSPADescriptor(*i); Chris@0: Chris@0: if (!descriptor) { Chris@844: cerr << "WARNING: LADSPAPluginFactory::enumeratePlugins: couldn't get descriptor for identifier " << *i << endl; Chris@0: continue; Chris@0: } Chris@0: Chris@0: list.push_back(*i); Chris@0: list.push_back(descriptor->Name); Chris@0: list.push_back(QString("%1").arg(descriptor->UniqueID)); Chris@0: list.push_back(descriptor->Label); Chris@0: list.push_back(descriptor->Maker); Chris@0: list.push_back(descriptor->Copyright); Chris@0: list.push_back("false"); // is synth Chris@0: list.push_back("false"); // is grouped Chris@165: Chris@165: if (m_taxonomy.find(*i) != m_taxonomy.end() && m_taxonomy[*i] != "") { Chris@844: // cerr << "LADSPAPluginFactory: cat for " << *i << " found in taxonomy as " << m_taxonomy[descriptor->UniqueID] << endl; Chris@165: list.push_back(m_taxonomy[*i]); Chris@0: } else { Chris@0: list.push_back(""); Chris@844: // cerr << "LADSPAPluginFactory: cat for " << *i << " not found (despite having " << m_fallbackCategories.size() << " fallbacks)" << endl; Chris@0: Chris@0: } Chris@0: Chris@0: list.push_back(QString("%1").arg(descriptor->PortCount)); Chris@0: Chris@1039: for (int p = 0; p < (int)descriptor->PortCount; ++p) { Chris@0: Chris@0: int type = 0; Chris@0: if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) { Chris@0: type |= PortType::Control; Chris@0: } else { Chris@0: type |= PortType::Audio; Chris@0: } Chris@0: if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) { Chris@0: type |= PortType::Input; Chris@0: } else { Chris@0: type |= PortType::Output; Chris@0: } Chris@0: Chris@0: list.push_back(QString("%1").arg(p)); Chris@0: list.push_back(descriptor->PortNames[p]); Chris@0: list.push_back(QString("%1").arg(type)); Chris@0: list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p))); Chris@0: list.push_back(QString("%1").arg(getPortMinimum(descriptor, p))); Chris@0: list.push_back(QString("%1").arg(getPortMaximum(descriptor, p))); Chris@0: list.push_back(QString("%1").arg(getPortDefault(descriptor, p))); Chris@0: } Chris@0: } Chris@0: Chris@0: unloadUnusedLibraries(); Chris@0: } Chris@0: Chris@60: const RealTimePluginDescriptor * Chris@60: LADSPAPluginFactory::getPluginDescriptor(QString identifier) const Chris@60: { Chris@60: std::map::const_iterator i = Chris@60: m_rtDescriptors.find(identifier); Chris@60: Chris@60: if (i != m_rtDescriptors.end()) { Chris@60: return i->second; Chris@60: } Chris@60: Chris@60: return 0; Chris@60: } Chris@60: Chris@0: float Chris@0: LADSPAPluginFactory::getPortMinimum(const LADSPA_Descriptor *descriptor, int port) Chris@0: { Chris@0: LADSPA_PortRangeHintDescriptor d = Chris@0: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: Chris@1039: float minimum = 0.f; Chris@0: Chris@0: if (LADSPA_IS_HINT_BOUNDED_BELOW(d)) { Chris@0: float lb = descriptor->PortRangeHints[port].LowerBound; Chris@0: minimum = lb; Chris@0: } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { Chris@0: float ub = descriptor->PortRangeHints[port].UpperBound; Chris@1039: minimum = std::min(0.f, ub - 1.f); Chris@0: } Chris@0: Chris@0: if (LADSPA_IS_HINT_SAMPLE_RATE(d)) { Chris@1040: minimum = float(minimum * m_sampleRate); Chris@0: } Chris@0: Chris@356: if (LADSPA_IS_HINT_LOGARITHMIC(d)) { Chris@356: if (minimum == 0.f) minimum = 1.f; Chris@356: } Chris@356: Chris@0: return minimum; Chris@0: } Chris@0: Chris@0: float Chris@0: LADSPAPluginFactory::getPortMaximum(const LADSPA_Descriptor *descriptor, int port) Chris@0: { Chris@0: LADSPA_PortRangeHintDescriptor d = Chris@0: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: Chris@1039: float maximum = 1.f; Chris@0: Chris@0: if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { Chris@0: float ub = descriptor->PortRangeHints[port].UpperBound; Chris@0: maximum = ub; Chris@0: } else { Chris@0: float lb = descriptor->PortRangeHints[port].LowerBound; Chris@1039: maximum = lb + 1.f; Chris@0: } Chris@0: Chris@0: if (LADSPA_IS_HINT_SAMPLE_RATE(d)) { Chris@1040: maximum = float(maximum * m_sampleRate); Chris@0: } Chris@0: Chris@0: return maximum; Chris@0: } Chris@0: Chris@0: float Chris@0: LADSPAPluginFactory::getPortDefault(const LADSPA_Descriptor *descriptor, int port) Chris@0: { Chris@0: float minimum = getPortMinimum(descriptor, port); Chris@0: float maximum = getPortMaximum(descriptor, port); Chris@0: float deft; Chris@0: Chris@0: if (m_portDefaults.find(descriptor->UniqueID) != Chris@0: m_portDefaults.end()) { Chris@0: if (m_portDefaults[descriptor->UniqueID].find(port) != Chris@0: m_portDefaults[descriptor->UniqueID].end()) { Chris@0: Chris@0: deft = m_portDefaults[descriptor->UniqueID][port]; Chris@0: if (deft < minimum) deft = minimum; Chris@0: if (deft > maximum) deft = maximum; Chris@0: return deft; Chris@0: } Chris@0: } Chris@0: Chris@0: LADSPA_PortRangeHintDescriptor d = Chris@0: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: Chris@0: bool logarithmic = LADSPA_IS_HINT_LOGARITHMIC(d); Chris@0: Chris@356: float logmin = 0, logmax = 0; Chris@356: if (logarithmic) { Chris@356: float thresh = powf(10, -10); Chris@356: if (minimum < thresh) logmin = -10; Chris@356: else logmin = log10f(minimum); Chris@356: if (maximum < thresh) logmax = -10; Chris@356: else logmax = log10f(maximum); Chris@356: } Chris@356: Chris@690: // SVDEBUG << "LADSPAPluginFactory::getPortDefault: hint = " << d << endl; Chris@356: Chris@0: if (!LADSPA_IS_HINT_HAS_DEFAULT(d)) { Chris@0: Chris@0: deft = minimum; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MINIMUM(d)) { Chris@0: Chris@0: deft = minimum; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_LOW(d)) { Chris@0: Chris@0: if (logarithmic) { Chris@1039: deft = powf(10, logmin * 0.75f + logmax * 0.25f); Chris@0: } else { Chris@1039: deft = minimum * 0.75f + maximum * 0.25f; Chris@0: } Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(d)) { Chris@0: Chris@0: if (logarithmic) { Chris@1039: deft = powf(10, logmin * 0.5f + logmax * 0.5f); Chris@0: } else { Chris@1039: deft = minimum * 0.5f + maximum * 0.5f; Chris@0: } Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_HIGH(d)) { Chris@0: Chris@0: if (logarithmic) { Chris@1039: deft = powf(10, logmin * 0.25f + logmax * 0.75f); Chris@0: } else { Chris@1039: deft = minimum * 0.25f + maximum * 0.75f; Chris@0: } Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(d)) { Chris@0: Chris@0: deft = maximum; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_0(d)) { Chris@0: Chris@0: deft = 0.0; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_1(d)) { Chris@0: Chris@0: deft = 1.0; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_100(d)) { Chris@0: Chris@0: deft = 100.0; Chris@0: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_440(d)) { Chris@0: Chris@145: // deft = 440.0; Chris@1039: deft = (float)Preferences::getInstance()->getTuningFrequency(); Chris@0: Chris@0: } else { Chris@0: Chris@0: deft = minimum; Chris@0: } Chris@356: Chris@356: //!!! No -- the min and max have already been multiplied by the rate, Chris@356: //so it would happen twice if we did it here -- and e.g. DEFAULT_440 Chris@356: //doesn't want to be multiplied by the rate either Chris@0: Chris@356: // if (LADSPA_IS_HINT_SAMPLE_RATE(d)) { Chris@356: // deft *= m_sampleRate; Chris@356: // } Chris@0: Chris@0: return deft; Chris@0: } Chris@0: Chris@57: float Chris@57: LADSPAPluginFactory::getPortQuantization(const LADSPA_Descriptor *descriptor, int port) Chris@57: { Chris@57: int displayHint = getPortDisplayHint(descriptor, port); Chris@57: if (displayHint & PortHint::Toggled) { Chris@1039: return float(lrintf(getPortMaximum(descriptor, port)) - Chris@1039: lrintf(getPortMinimum(descriptor, port))); Chris@57: } Chris@57: if (displayHint & PortHint::Integer) { Chris@57: return 1.0; Chris@57: } Chris@57: return 0.0; Chris@57: } Chris@57: Chris@0: int Chris@0: LADSPAPluginFactory::getPortDisplayHint(const LADSPA_Descriptor *descriptor, int port) Chris@0: { Chris@0: LADSPA_PortRangeHintDescriptor d = Chris@0: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: int hint = PortHint::NoHint; Chris@0: Chris@0: if (LADSPA_IS_HINT_TOGGLED(d)) hint |= PortHint::Toggled; Chris@0: if (LADSPA_IS_HINT_INTEGER(d)) hint |= PortHint::Integer; Chris@0: if (LADSPA_IS_HINT_LOGARITHMIC(d)) hint |= PortHint::Logarithmic; Chris@0: Chris@0: return hint; Chris@0: } Chris@0: Chris@0: Chris@0: RealTimePluginInstance * Chris@0: LADSPAPluginFactory::instantiatePlugin(QString identifier, Chris@0: int instrument, Chris@0: int position, Chris@1040: sv_samplerate_t sampleRate, Chris@1040: int blockSize, Chris@1040: int channels) Chris@0: { Chris@408: Profiler profiler("LADSPAPluginFactory::instantiatePlugin"); Chris@408: Chris@0: const LADSPA_Descriptor *descriptor = getLADSPADescriptor(identifier); Chris@0: Chris@0: if (descriptor) { Chris@0: Chris@0: LADSPAPluginInstance *instance = Chris@0: new LADSPAPluginInstance Chris@0: (this, instrument, identifier, position, sampleRate, blockSize, channels, Chris@0: descriptor); Chris@0: Chris@0: m_instances.insert(instance); Chris@0: Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@690: SVDEBUG << "LADSPAPluginFactory::instantiatePlugin(" Chris@687: << identifier << ": now have " << m_instances.size() << " instances" << endl; Chris@260: #endif Chris@78: Chris@0: return instance; Chris@0: } Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::releasePlugin(RealTimePluginInstance *instance, Chris@0: QString identifier) Chris@0: { Chris@408: Profiler profiler("LADSPAPluginFactory::releasePlugin"); Chris@408: Chris@0: if (m_instances.find(instance) == m_instances.end()) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::releasePlugin: Not one of mine!" Chris@843: << endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: QString type, soname, label; Chris@0: PluginIdentifier::parseIdentifier(identifier, type, soname, label); Chris@0: Chris@0: m_instances.erase(instance); Chris@0: Chris@0: bool stillInUse = false; Chris@0: Chris@0: for (std::set::iterator ii = m_instances.begin(); Chris@0: ii != m_instances.end(); ++ii) { Chris@0: QString itype, isoname, ilabel; Chris@237: PluginIdentifier::parseIdentifier((*ii)->getPluginIdentifier(), itype, isoname, ilabel); Chris@0: if (isoname == soname) { Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@690: SVDEBUG << "LADSPAPluginFactory::releasePlugin: dll " << soname << " is still in use for plugin " << ilabel << endl; Chris@260: #endif Chris@0: stillInUse = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!stillInUse) { Chris@118: if (soname != PluginIdentifier::BUILTIN_PLUGIN_SONAME) { Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@690: SVDEBUG << "LADSPAPluginFactory::releasePlugin: dll " << soname << " no longer in use, unloading" << endl; Chris@260: #endif Chris@118: unloadLibrary(soname); Chris@118: } Chris@0: } Chris@78: Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@690: SVDEBUG << "LADSPAPluginFactory::releasePlugin(" Chris@687: << identifier << ": now have " << m_instances.size() << " instances" << endl; Chris@260: #endif Chris@0: } Chris@0: Chris@0: const LADSPA_Descriptor * Chris@0: LADSPAPluginFactory::getLADSPADescriptor(QString identifier) Chris@0: { Chris@0: QString type, soname, label; Chris@0: PluginIdentifier::parseIdentifier(identifier, type, soname, label); Chris@0: Chris@0: if (m_libraryHandles.find(soname) == m_libraryHandles.end()) { Chris@0: loadLibrary(soname); Chris@0: if (m_libraryHandles.find(soname) == m_libraryHandles.end()) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: loadLibrary failed for " << soname << endl; Chris@0: return 0; Chris@0: } Chris@0: } Chris@0: Chris@0: void *libraryHandle = m_libraryHandles[soname]; Chris@0: Chris@0: LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function) Chris@0: DLSYM(libraryHandle, "ladspa_descriptor"); Chris@0: Chris@0: if (!fn) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No descriptor function in library " << soname << endl; Chris@0: return 0; Chris@0: } Chris@0: Chris@0: const LADSPA_Descriptor *descriptor = 0; Chris@0: Chris@0: int index = 0; Chris@0: while ((descriptor = fn(index))) { Chris@0: if (descriptor->Label == label) return descriptor; Chris@0: ++index; Chris@0: } Chris@0: Chris@843: cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No such plugin as " << label << " in library " << soname << endl; Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::loadLibrary(QString soName) Chris@0: { Chris@0: void *libraryHandle = DLOPEN(soName, RTLD_NOW); Chris@106: if (libraryHandle) { Chris@106: m_libraryHandles[soName] = libraryHandle; Chris@690: SVDEBUG << "LADSPAPluginFactory::loadLibrary: Loaded library \"" << soName << "\"" << endl; Chris@106: return; Chris@106: } Chris@106: Chris@106: if (QFileInfo(soName).exists()) { Chris@106: DLERROR(); Chris@843: cerr << "LADSPAPluginFactory::loadLibrary: Library \"" << soName << "\" exists, but failed to load it" << endl; Chris@106: return; Chris@106: } Chris@106: Chris@106: std::vector pathList = getPluginPath(); Chris@106: Chris@106: QString fileName = QFile(soName).fileName(); Chris@106: QString base = QFileInfo(soName).baseName(); Chris@106: Chris@106: for (std::vector::iterator i = pathList.begin(); Chris@106: i != pathList.end(); ++i) { Chris@106: Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@690: SVDEBUG << "Looking at: " << (*i) << endl; Chris@260: #endif Chris@106: Chris@106: QDir dir(*i, PLUGIN_GLOB, Chris@106: QDir::Name | QDir::IgnoreCase, Chris@106: QDir::Files | QDir::Readable); Chris@106: Chris@106: if (QFileInfo(dir.filePath(fileName)).exists()) { Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@843: cerr << "Loading: " << fileName << endl; Chris@260: #endif Chris@106: libraryHandle = DLOPEN(dir.filePath(fileName), RTLD_NOW); Chris@166: if (libraryHandle) { Chris@166: m_libraryHandles[soName] = libraryHandle; Chris@166: return; Chris@166: } Chris@106: } Chris@106: Chris@106: for (unsigned int j = 0; j < dir.count(); ++j) { Chris@106: QString file = dir.filePath(dir[j]); Chris@106: if (QFileInfo(file).baseName() == base) { Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@843: cerr << "Loading: " << file << endl; Chris@260: #endif Chris@106: libraryHandle = DLOPEN(file, RTLD_NOW); Chris@166: if (libraryHandle) { Chris@166: m_libraryHandles[soName] = libraryHandle; Chris@166: return; Chris@166: } Chris@106: } Chris@106: } Chris@106: } Chris@106: Chris@843: cerr << "LADSPAPluginFactory::loadLibrary: Failed to locate plugin library \"" << soName << "\"" << endl; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::unloadLibrary(QString soName) Chris@0: { Chris@0: LibraryHandleMap::iterator li = m_libraryHandles.find(soName); Chris@0: if (li != m_libraryHandles.end()) { Chris@690: // SVDEBUG << "unloading " << soname << endl; Chris@0: DLCLOSE(m_libraryHandles[soName]); Chris@0: m_libraryHandles.erase(li); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::unloadUnusedLibraries() Chris@0: { Chris@0: std::vector toUnload; Chris@0: Chris@0: for (LibraryHandleMap::iterator i = m_libraryHandles.begin(); Chris@0: i != m_libraryHandles.end(); ++i) { Chris@0: Chris@0: bool stillInUse = false; Chris@0: Chris@0: for (std::set::iterator ii = m_instances.begin(); Chris@0: ii != m_instances.end(); ++ii) { Chris@0: Chris@0: QString itype, isoname, ilabel; Chris@237: PluginIdentifier::parseIdentifier((*ii)->getPluginIdentifier(), itype, isoname, ilabel); Chris@0: if (isoname == i->first) { Chris@0: stillInUse = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!stillInUse) toUnload.push_back(i->first); Chris@0: } Chris@0: Chris@0: for (std::vector::iterator i = toUnload.begin(); Chris@0: i != toUnload.end(); ++i) { Chris@118: if (*i != PluginIdentifier::BUILTIN_PLUGIN_SONAME) { Chris@118: unloadLibrary(*i); Chris@118: } Chris@0: } Chris@0: } Chris@0: Chris@0: Chris@0: // It is only later, after they've gone, Chris@0: // I realize they have delivered a letter. Chris@0: // It's a letter from my wife. "What are you doing Chris@0: // there?" my wife asks. "Are you drinking?" Chris@0: // I study the postmark for hours. Then it, too, begins to fade. Chris@0: // I hope someday to forget all this. Chris@0: Chris@0: Chris@0: std::vector Chris@0: LADSPAPluginFactory::getPluginPath() Chris@0: { Chris@0: std::vector pathList; Chris@0: std::string path; Chris@0: Chris@0: char *cpath = getenv("LADSPA_PATH"); Chris@0: if (cpath) path = cpath; Chris@0: Chris@0: if (path == "") { Chris@186: Chris@186: path = DEFAULT_LADSPA_PATH; Chris@186: Chris@0: char *home = getenv("HOME"); Chris@186: if (home) { Chris@186: std::string::size_type f; Chris@186: while ((f = path.find("$HOME")) != std::string::npos && Chris@186: f < path.length()) { Chris@186: path.replace(f, 5, home); Chris@186: } Chris@186: } Chris@186: Chris@186: #ifdef _WIN32 Chris@220: char *pfiles = getenv("ProgramFiles"); Chris@220: if (!pfiles) pfiles = "C:\\Program Files"; Chris@220: { Chris@186: std::string::size_type f; Chris@186: while ((f = path.find("%ProgramFiles%")) != std::string::npos && Chris@186: f < path.length()) { Chris@186: path.replace(f, 14, pfiles); Chris@186: } Chris@220: } Chris@186: #endif Chris@0: } Chris@0: Chris@0: std::string::size_type index = 0, newindex = 0; Chris@0: Chris@223: while ((newindex = path.find(PATH_SEPARATOR, index)) < path.size()) { Chris@0: pathList.push_back(path.substr(index, newindex - index).c_str()); Chris@0: index = newindex + 1; Chris@0: } Chris@0: Chris@0: pathList.push_back(path.substr(index).c_str()); Chris@0: Chris@0: return pathList; Chris@0: } Chris@0: Chris@0: Chris@0: std::vector Chris@0: LADSPAPluginFactory::getLRDFPath(QString &baseUri) Chris@0: { Chris@150: std::vector lrdfPaths; Chris@150: Chris@150: #ifdef HAVE_LRDF Chris@0: std::vector pathList = getPluginPath(); Chris@0: Chris@0: lrdfPaths.push_back("/usr/local/share/ladspa/rdf"); Chris@0: lrdfPaths.push_back("/usr/share/ladspa/rdf"); Chris@0: Chris@0: for (std::vector::iterator i = pathList.begin(); Chris@0: i != pathList.end(); ++i) { Chris@0: lrdfPaths.push_back(*i + "/rdf"); Chris@0: } Chris@0: Chris@0: baseUri = LADSPA_BASE; Chris@953: #else Chris@953: baseUri = ""; Chris@150: #endif Chris@150: Chris@0: return lrdfPaths; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::discoverPlugins() Chris@0: { Chris@408: Profiler profiler("LADSPAPluginFactory::discoverPlugins"); Chris@408: Chris@0: std::vector pathList = getPluginPath(); Chris@0: Chris@690: // SVDEBUG << "LADSPAPluginFactory::discoverPlugins - " Chris@0: // << "discovering plugins; path is "; Chris@259: // for (std::vector::iterator i = pathList.begin(); Chris@259: // i != pathList.end(); ++i) { Chris@690: // SVDEBUG << "[" << i-<< "] "; Chris@259: // } Chris@690: // SVDEBUG << endl; Chris@0: Chris@35: #ifdef HAVE_LRDF Chris@166: // read the description files Chris@0: // Chris@0: QString baseUri; Chris@0: std::vector lrdfPaths = getLRDFPath(baseUri); Chris@0: Chris@0: bool haveSomething = false; Chris@0: Chris@0: for (size_t i = 0; i < lrdfPaths.size(); ++i) { Chris@0: QDir dir(lrdfPaths[i], "*.rdf;*.rdfs"); Chris@0: for (unsigned int j = 0; j < dir.count(); ++j) { Chris@0: if (!lrdf_read_file(QString("file:" + lrdfPaths[i] + "/" + dir[j]).toStdString().c_str())) { Chris@843: // cerr << "LADSPAPluginFactory: read RDF file " << (lrdfPaths[i] + "/" + dir[j]) << endl; Chris@0: haveSomething = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (haveSomething) { Chris@0: generateTaxonomy(baseUri + "Plugin", ""); Chris@0: } Chris@35: #endif // HAVE_LRDF Chris@0: Chris@0: generateFallbackCategories(); Chris@0: Chris@0: for (std::vector::iterator i = pathList.begin(); Chris@0: i != pathList.end(); ++i) { Chris@0: Chris@0: QDir pluginDir(*i, PLUGIN_GLOB); Chris@0: Chris@0: for (unsigned int j = 0; j < pluginDir.count(); ++j) { Chris@929: discoverPluginsFrom(QString("%1/%2").arg(*i).arg(pluginDir[j])); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@929: LADSPAPluginFactory::discoverPluginsFrom(QString soname) Chris@0: { Chris@0: void *libraryHandle = DLOPEN(soname, RTLD_LAZY); Chris@0: Chris@0: if (!libraryHandle) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: couldn't load plugin library " Chris@843: << soname << " - " << DLERROR() << endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function) Chris@0: DLSYM(libraryHandle, "ladspa_descriptor"); Chris@0: Chris@0: if (!fn) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: No descriptor function in " << soname << endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: const LADSPA_Descriptor *descriptor = 0; Chris@0: Chris@0: int index = 0; Chris@0: while ((descriptor = fn(index))) { Chris@0: Chris@60: RealTimePluginDescriptor *rtd = new RealTimePluginDescriptor; Chris@60: rtd->name = descriptor->Name; Chris@60: rtd->label = descriptor->Label; Chris@60: rtd->maker = descriptor->Maker; Chris@60: rtd->copyright = descriptor->Copyright; Chris@60: rtd->category = ""; Chris@60: rtd->isSynth = false; Chris@60: rtd->parameterCount = 0; Chris@60: rtd->audioInputPortCount = 0; Chris@166: rtd->audioOutputPortCount = 0; Chris@60: rtd->controlOutputPortCount = 0; Chris@60: Chris@165: QString identifier = PluginIdentifier::createIdentifier Chris@165: ("ladspa", soname, descriptor->Label); Chris@165: Chris@35: #ifdef HAVE_LRDF Chris@0: char *def_uri = 0; Chris@0: lrdf_defaults *defs = 0; Chris@0: Chris@165: if (m_lrdfTaxonomy[descriptor->UniqueID] != "") { Chris@165: m_taxonomy[identifier] = m_lrdfTaxonomy[descriptor->UniqueID]; Chris@843: // cerr << "set id \"" << identifier << "\" to cat \"" << m_taxonomy[identifier] << "\" from LRDF" << endl; Chris@843: // cout << identifier << "::" << m_taxonomy[identifier] << endl; Chris@165: } Chris@165: Chris@165: QString category = m_taxonomy[identifier]; Chris@0: Chris@973: if (category == "") { Chris@973: std::string name = rtd->name; Chris@0: if (name.length() > 4 && Chris@0: name.substr(name.length() - 4) == " VST") { Chris@0: category = "VST effects"; Chris@165: m_taxonomy[identifier] = category; Chris@0: } Chris@0: } Chris@0: Chris@60: rtd->category = category.toStdString(); Chris@60: Chris@843: // cerr << "Plugin id is " << descriptor->UniqueID Chris@0: // << ", category is \"" << (category ? category : QString("(none)")) Chris@0: // << "\", name is " << descriptor->Name Chris@0: // << ", label is " << descriptor->Label Chris@843: // << endl; Chris@0: Chris@0: def_uri = lrdf_get_default_uri(descriptor->UniqueID); Chris@0: if (def_uri) { Chris@0: defs = lrdf_get_setting_values(def_uri); Chris@0: } Chris@0: Chris@259: unsigned int controlPortNumber = 1; Chris@0: Chris@1039: for (int i = 0; i < (int)descriptor->PortCount; i++) { Chris@0: Chris@0: if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) { Chris@0: Chris@0: if (def_uri && defs) { Chris@0: Chris@259: for (unsigned int j = 0; j < defs->count; j++) { Chris@0: if (defs->items[j].pid == controlPortNumber) { Chris@843: // 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] << endl; Chris@0: m_portDefaults[descriptor->UniqueID][i] = Chris@0: defs->items[j].value; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: ++controlPortNumber; Chris@0: } Chris@0: } Chris@35: #endif // HAVE_LRDF Chris@0: Chris@1039: for (int i = 0; i < (int)descriptor->PortCount; i++) { Chris@60: if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) { Chris@60: if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) { Chris@60: ++rtd->parameterCount; Chris@60: } else { Chris@60: if (strcmp(descriptor->PortNames[i], "latency") && Chris@60: strcmp(descriptor->PortNames[i], "_latency")) { Chris@60: ++rtd->controlOutputPortCount; Chris@60: rtd->controlOutputPortNames.push_back Chris@60: (descriptor->PortNames[i]); Chris@60: } Chris@60: } Chris@60: } else { Chris@60: if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) { Chris@60: ++rtd->audioInputPortCount; Chris@166: } else if (LADSPA_IS_PORT_OUTPUT(descriptor->PortDescriptors[i])) { Chris@166: ++rtd->audioOutputPortCount; Chris@60: } Chris@60: } Chris@60: } Chris@60: Chris@0: m_identifiers.push_back(identifier); Chris@0: Chris@60: m_rtDescriptors[identifier] = rtd; Chris@60: Chris@0: ++index; Chris@0: } Chris@0: Chris@0: if (DLCLOSE(libraryHandle) != 0) { Chris@843: cerr << "WARNING: LADSPAPluginFactory::discoverPlugins - can't unload " << libraryHandle << endl; Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::generateFallbackCategories() Chris@0: { Chris@0: std::vector pluginPath = getPluginPath(); Chris@0: std::vector path; Chris@0: Chris@0: for (size_t i = 0; i < pluginPath.size(); ++i) { Chris@0: if (pluginPath[i].contains("/lib/")) { Chris@0: QString p(pluginPath[i]); Chris@165: path.push_back(p); Chris@0: p.replace("/lib/", "/share/"); Chris@0: path.push_back(p); Chris@690: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: path element " << p << endl; Chris@0: } Chris@0: path.push_back(pluginPath[i]); Chris@690: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: path element " << pluginPath[i] << endl; Chris@0: } Chris@0: Chris@0: for (size_t i = 0; i < path.size(); ++i) { Chris@0: Chris@0: QDir dir(path[i], "*.cat"); Chris@0: Chris@690: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i] << " has " << dir.count() << " .cat files" << endl; Chris@0: for (unsigned int j = 0; j < dir.count(); ++j) { Chris@0: Chris@0: QFile file(path[i] + "/" + dir[j]); Chris@0: Chris@690: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i]+ "/" + dir[j]) << endl; Chris@0: Chris@0: if (file.open(QIODevice::ReadOnly)) { Chris@843: // cerr << "...opened" << endl; Chris@0: QTextStream stream(&file); Chris@0: QString line; Chris@0: Chris@0: while (!stream.atEnd()) { Chris@0: line = stream.readLine(); Chris@843: // cerr << "line is: \"" << line << "\"" << endl; Chris@165: QString id = PluginIdentifier::canonicalise Chris@165: (line.section("::", 0, 0)); Chris@0: QString cat = line.section("::", 1, 1); Chris@165: m_taxonomy[id] = cat; Chris@843: // cerr << "set id \"" << id << "\" to cat \"" << cat << "\"" << endl; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::generateTaxonomy(QString uri, QString base) Chris@0: { Chris@35: #ifdef HAVE_LRDF Chris@0: lrdf_uris *uris = lrdf_get_instances(uri.toStdString().c_str()); Chris@0: Chris@0: if (uris != NULL) { Chris@259: for (unsigned int i = 0; i < uris->count; ++i) { Chris@165: m_lrdfTaxonomy[lrdf_get_uid(uris->items[i])] = base; Chris@0: } Chris@0: lrdf_free_uris(uris); Chris@0: } Chris@0: Chris@0: uris = lrdf_get_subclasses(uri.toStdString().c_str()); Chris@0: Chris@0: if (uris != NULL) { Chris@259: for (unsigned int i = 0; i < uris->count; ++i) { Chris@0: char *label = lrdf_get_label(uris->items[i]); Chris@0: generateTaxonomy(uris->items[i], Chris@0: base + (base.length() > 0 ? " > " : "") + label); Chris@0: } Chris@0: lrdf_free_uris(uris); Chris@0: } Chris@953: #else Chris@953: // avoid unused parameter Chris@953: (void)uri; Chris@953: (void)base; Chris@0: #endif Chris@0: } Chris@0: Chris@165: QString Chris@165: LADSPAPluginFactory::getPluginCategory(QString identifier) Chris@165: { Chris@165: return m_taxonomy[identifier]; Chris@165: } Chris@0: