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@1480: using std::string; 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@1429: i != m_instances.end(); ++i) { Chris@1429: (*i)->setFactory(0); Chris@1429: 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@1464: QString Chris@1464: LADSPAPluginFactory::getPluginLibraryPath(QString identifier) Chris@1464: { Chris@1464: return m_libraries[identifier]; Chris@1464: } Chris@1464: 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@1429: i != m_identifiers.end(); ++i) { Chris@0: Chris@1429: const LADSPA_Descriptor *descriptor = getLADSPADescriptor(*i); Chris@0: Chris@1429: if (!descriptor) { Chris@1429: cerr << "WARNING: LADSPAPluginFactory::enumeratePlugins: couldn't get descriptor for identifier " << *i << endl; Chris@1429: continue; Chris@1429: } Chris@1429: Chris@1429: list.push_back(*i); Chris@1429: list.push_back(descriptor->Name); Chris@1429: list.push_back(QString("%1").arg(descriptor->UniqueID)); Chris@1429: list.push_back(descriptor->Label); Chris@1429: list.push_back(descriptor->Maker); Chris@1429: list.push_back(descriptor->Copyright); Chris@1429: list.push_back("false"); // is synth Chris@1429: list.push_back("false"); // is grouped Chris@165: Chris@1429: if (m_taxonomy.find(*i) != m_taxonomy.end() && m_taxonomy[*i] != "") { Chris@1429: // cerr << "LADSPAPluginFactory: cat for " << *i << " found in taxonomy as " << m_taxonomy[descriptor->UniqueID] << endl; Chris@1429: list.push_back(m_taxonomy[*i]); Chris@1429: } else { Chris@1429: list.push_back(""); Chris@1429: // cerr << "LADSPAPluginFactory: cat for " << *i << " not found (despite having " << m_fallbackCategories.size() << " fallbacks)" << endl; Chris@1429: Chris@1429: } Chris@0: Chris@1429: list.push_back(QString("%1").arg(descriptor->PortCount)); Chris@0: Chris@1429: for (int p = 0; p < (int)descriptor->PortCount; ++p) { Chris@0: Chris@1429: int type = 0; Chris@1429: if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) { Chris@1429: type |= PortType::Control; Chris@1429: } else { Chris@1429: type |= PortType::Audio; Chris@1429: } Chris@1429: if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) { Chris@1429: type |= PortType::Input; Chris@1429: } else { Chris@1429: type |= PortType::Output; Chris@1429: } Chris@0: Chris@1429: list.push_back(QString("%1").arg(p)); Chris@1429: list.push_back(descriptor->PortNames[p]); Chris@1429: list.push_back(QString("%1").arg(type)); Chris@1429: list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p))); Chris@1429: list.push_back(QString("%1").arg(getPortMinimum(descriptor, p))); Chris@1429: list.push_back(QString("%1").arg(getPortMaximum(descriptor, p))); Chris@1429: list.push_back(QString("%1").arg(getPortDefault(descriptor, p))); Chris@1429: } Chris@0: } Chris@0: Chris@0: unloadUnusedLibraries(); Chris@0: } Chris@1429: 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@1429: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: Chris@1039: float minimum = 0.f; Chris@1429: Chris@0: if (LADSPA_IS_HINT_BOUNDED_BELOW(d)) { Chris@1429: float lb = descriptor->PortRangeHints[port].LowerBound; Chris@1429: minimum = lb; Chris@0: } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { Chris@1429: float ub = descriptor->PortRangeHints[port].UpperBound; Chris@1429: minimum = std::min(0.f, ub - 1.f); Chris@0: } Chris@0: Chris@0: if (LADSPA_IS_HINT_SAMPLE_RATE(d)) { Chris@1429: 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@1429: descriptor->PortRangeHints[port].HintDescriptor; Chris@0: Chris@1039: float maximum = 1.f; Chris@0: Chris@0: if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { Chris@1429: float ub = descriptor->PortRangeHints[port].UpperBound; Chris@1429: maximum = ub; Chris@0: } else { Chris@1429: float lb = descriptor->PortRangeHints[port].LowerBound; Chris@1429: maximum = lb + 1.f; Chris@0: } Chris@0: Chris@0: if (LADSPA_IS_HINT_SAMPLE_RATE(d)) { Chris@1429: 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@1429: m_portDefaults.end()) { Chris@1429: if (m_portDefaults[descriptor->UniqueID].find(port) != Chris@1429: m_portDefaults[descriptor->UniqueID].end()) { Chris@0: Chris@1429: deft = m_portDefaults[descriptor->UniqueID][port]; Chris@1429: if (deft < minimum) deft = minimum; Chris@1429: if (deft > maximum) deft = maximum; Chris@1429: return deft; Chris@1429: } Chris@0: } Chris@0: Chris@0: LADSPA_PortRangeHintDescriptor d = Chris@1429: 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@1429: Chris@1429: deft = minimum; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MINIMUM(d)) { Chris@1429: Chris@1429: deft = minimum; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_LOW(d)) { Chris@1429: Chris@1429: if (logarithmic) { Chris@1429: deft = powf(10, logmin * 0.75f + logmax * 0.25f); Chris@1429: } else { Chris@1429: deft = minimum * 0.75f + maximum * 0.25f; Chris@1429: } Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(d)) { Chris@1429: Chris@1429: if (logarithmic) { Chris@1429: deft = powf(10, logmin * 0.5f + logmax * 0.5f); Chris@1429: } else { Chris@1429: deft = minimum * 0.5f + maximum * 0.5f; Chris@1429: } Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_HIGH(d)) { Chris@1429: Chris@1429: if (logarithmic) { Chris@1429: deft = powf(10, logmin * 0.25f + logmax * 0.75f); Chris@1429: } else { Chris@1429: deft = minimum * 0.25f + maximum * 0.75f; Chris@1429: } Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(d)) { Chris@1429: Chris@1429: deft = maximum; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_0(d)) { Chris@1429: Chris@1429: deft = 0.0; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_1(d)) { Chris@1429: Chris@1429: deft = 1.0; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_100(d)) { Chris@1429: Chris@1429: deft = 100.0; Chris@1429: Chris@0: } else if (LADSPA_IS_HINT_DEFAULT_440(d)) { Chris@1429: Chris@1429: // deft = 440.0; Chris@1039: deft = (float)Preferences::getInstance()->getTuningFrequency(); Chris@1429: Chris@0: } else { Chris@1429: Chris@1429: 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@1429: // 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@1429: 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@1429: int instrument, Chris@1429: int position, Chris@1429: sv_samplerate_t sampleRate, Chris@1429: int blockSize, Chris@1429: 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@1429: LADSPAPluginInstance *instance = Chris@1429: new LADSPAPluginInstance Chris@1429: (this, instrument, identifier, position, sampleRate, blockSize, channels, Chris@1429: descriptor); Chris@0: Chris@1429: 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@1429: return instance; Chris@0: } Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: void Chris@0: LADSPAPluginFactory::releasePlugin(RealTimePluginInstance *instance, Chris@1429: QString identifier) Chris@0: { Chris@408: Profiler profiler("LADSPAPluginFactory::releasePlugin"); Chris@408: Chris@0: if (m_instances.find(instance) == m_instances.end()) { Chris@1429: cerr << "WARNING: LADSPAPluginFactory::releasePlugin: Not one of mine!" Chris@1429: << endl; Chris@1429: 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@1429: ii != m_instances.end(); ++ii) { Chris@1429: QString itype, isoname, ilabel; Chris@1429: PluginIdentifier::parseIdentifier((*ii)->getPluginIdentifier(), itype, isoname, ilabel); Chris@1429: if (isoname == soname) { Chris@260: #ifdef DEBUG_LADSPA_PLUGIN_FACTORY Chris@1429: SVDEBUG << "LADSPAPluginFactory::releasePlugin: dll " << soname << " is still in use for plugin " << ilabel << endl; Chris@260: #endif Chris@1429: stillInUse = true; Chris@1429: break; Chris@1429: } 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@1429: loadLibrary(soname); Chris@1429: if (m_libraryHandles.find(soname) == m_libraryHandles.end()) { Chris@1498: SVCERR << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: loadLibrary failed for " << soname << endl; Chris@1429: return 0; Chris@1429: } Chris@0: } Chris@0: Chris@0: void *libraryHandle = m_libraryHandles[soname]; Chris@0: Chris@0: LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function) Chris@1429: DLSYM(libraryHandle, "ladspa_descriptor"); Chris@0: Chris@0: if (!fn) { Chris@1498: SVCERR << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No descriptor function in library " << soname << endl; Chris@1429: 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@1429: if (descriptor->Label == label) return descriptor; Chris@1429: ++index; Chris@0: } Chris@0: Chris@1498: SVCERR << "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@1498: SVCERR << "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@1429: 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@1498: SVDEBUG << "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@1429: 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@1498: SVDEBUG << "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@1498: SVCERR << "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@1429: // SVDEBUG << "unloading " << soname << endl; Chris@1429: DLCLOSE(m_libraryHandles[soName]); Chris@1429: 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@1429: i != m_libraryHandles.end(); ++i) { Chris@0: Chris@1429: bool stillInUse = false; Chris@0: Chris@1429: for (std::set::iterator ii = m_instances.begin(); Chris@1429: ii != m_instances.end(); ++ii) { Chris@0: Chris@1429: QString itype, isoname, ilabel; Chris@1429: PluginIdentifier::parseIdentifier((*ii)->getPluginIdentifier(), itype, isoname, ilabel); Chris@1429: if (isoname == i->first) { Chris@1429: stillInUse = true; Chris@1429: break; Chris@1429: } Chris@1429: } Chris@0: Chris@1429: if (!stillInUse) toUnload.push_back(i->first); Chris@0: } Chris@0: Chris@0: for (std::vector::iterator i = toUnload.begin(); Chris@1429: 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@1480: string path; Chris@0: Chris@1497: (void)getEnvUtf8("LADSPA_PATH", path); Chris@0: Chris@0: if (path == "") { Chris@186: Chris@186: path = DEFAULT_LADSPA_PATH; Chris@186: Chris@1480: string home; Chris@1480: if (getEnvUtf8("HOME", home)) { Chris@1480: string::size_type f; Chris@1480: while ((f = path.find("$HOME")) != 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@1480: string pfiles; Chris@1480: if (!getEnvUtf8("ProgramFiles", pfiles)) { Chris@1480: pfiles = "C:\\Program Files"; Chris@1480: } Chris@1480: Chris@1480: string::size_type f; Chris@1480: while ((f = path.find("%ProgramFiles%")) != string::npos && Chris@186: f < path.length()) { Chris@186: path.replace(f, 14, pfiles); Chris@186: } Chris@186: #endif Chris@0: } Chris@0: Chris@1480: string::size_type index = 0, newindex = 0; Chris@0: Chris@223: while ((newindex = path.find(PATH_SEPARATOR, index)) < path.size()) { Chris@1429: pathList.push_back(path.substr(index, newindex - index).c_str()); Chris@1429: 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@1429: i != pathList.end(); ++i) { Chris@1429: 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@1429: // << "discovering plugins; path is "; Chris@259: // for (std::vector::iterator i = pathList.begin(); Chris@1429: // i != pathList.end(); ++i) { Chris@1429: // 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@1429: QDir dir(lrdfPaths[i], "*.rdf;*.rdfs"); Chris@1429: for (unsigned int j = 0; j < dir.count(); ++j) { Chris@1429: if (!lrdf_read_file(QString("file:" + lrdfPaths[i] + "/" + dir[j]).toStdString().c_str())) { Chris@1429: // cerr << "LADSPAPluginFactory: read RDF file " << (lrdfPaths[i] + "/" + dir[j]) << endl; Chris@1429: haveSomething = true; Chris@1429: } Chris@1429: } Chris@0: } Chris@0: Chris@0: if (haveSomething) { Chris@1429: generateTaxonomy(baseUri + "Plugin", ""); Chris@0: } Chris@35: #endif // HAVE_LRDF Chris@0: Chris@0: generateFallbackCategories(); Chris@0: Chris@1246: auto candidates = Chris@1179: PluginScan::getInstance()->getCandidateLibrariesFor(getPluginType()); Chris@0: Chris@1246: for (auto c: candidates) { Chris@1246: discoverPluginsFrom(c.libraryPath); 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@1429: DLSYM(libraryHandle, "ladspa_descriptor"); Chris@0: Chris@0: if (!fn) { Chris@1429: cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: No descriptor function in " << soname << endl; Chris@1429: 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@1429: QString identifier = PluginIdentifier::createIdentifier Chris@1429: ("ladspa", soname, descriptor->Label); Chris@165: Chris@35: #ifdef HAVE_LRDF Chris@1429: char *def_uri = 0; Chris@1429: lrdf_defaults *defs = 0; Chris@1429: 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@1429: QString category = m_taxonomy[identifier]; Chris@1429: Chris@1429: if (category == "") { Chris@1480: string name = rtd->name; Chris@1429: if (name.length() > 4 && Chris@1429: name.substr(name.length() - 4) == " VST") { Chris@1429: category = "VST effects"; Chris@1429: m_taxonomy[identifier] = category; Chris@1429: } Chris@1429: } Chris@1429: Chris@60: rtd->category = category.toStdString(); Chris@60: Chris@1429: // cerr << "Plugin id is " << descriptor->UniqueID Chris@1429: // << ", category is \"" << (category ? category : QString("(none)")) Chris@1429: // << "\", name is " << descriptor->Name Chris@1429: // << ", label is " << descriptor->Label Chris@1429: // << endl; Chris@1429: Chris@1429: def_uri = lrdf_get_default_uri(descriptor->UniqueID); Chris@1429: if (def_uri) { Chris@1429: defs = lrdf_get_setting_values(def_uri); Chris@1429: } Chris@0: Chris@1429: unsigned int controlPortNumber = 1; Chris@1429: Chris@1429: for (int i = 0; i < (int)descriptor->PortCount; i++) { Chris@1429: Chris@1429: if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) { Chris@1429: Chris@1429: if (def_uri && defs) { Chris@1429: Chris@1429: for (unsigned int j = 0; j < defs->count; j++) { Chris@1429: if (defs->items[j].pid == controlPortNumber) { Chris@1429: // 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@1429: m_portDefaults[descriptor->UniqueID][i] = Chris@1429: defs->items[j].value; Chris@1429: } Chris@1429: } Chris@1429: } Chris@1429: Chris@1429: ++controlPortNumber; Chris@1429: } Chris@1429: } Chris@35: #endif // HAVE_LRDF Chris@0: Chris@1429: for (int i = 0; i < (int)descriptor->PortCount; i++) { Chris@1429: 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@1429: m_identifiers.push_back(identifier); Chris@0: Chris@1464: m_libraries[identifier] = soname; Chris@1464: Chris@60: m_rtDescriptors[identifier] = rtd; Chris@60: Chris@1429: ++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@1429: if (pluginPath[i].contains("/lib/")) { Chris@1429: QString p(pluginPath[i]); Chris@165: path.push_back(p); Chris@1429: p.replace("/lib/", "/share/"); Chris@1429: path.push_back(p); Chris@1429: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: path element " << p << endl; Chris@1429: } Chris@1429: path.push_back(pluginPath[i]); Chris@1429: // 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@1429: QDir dir(path[i], "*.cat"); Chris@0: Chris@1429: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i] << " has " << dir.count() << " .cat files" << endl; Chris@1429: for (unsigned int j = 0; j < dir.count(); ++j) { Chris@0: Chris@1429: QFile file(path[i] + "/" + dir[j]); Chris@0: Chris@1429: // SVDEBUG << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i]+ "/" + dir[j]) << endl; Chris@0: Chris@1429: if (file.open(QIODevice::ReadOnly)) { Chris@1429: // cerr << "...opened" << endl; Chris@1429: QTextStream stream(&file); Chris@1429: QString line; Chris@0: Chris@1429: while (!stream.atEnd()) { Chris@1429: line = stream.readLine(); Chris@1429: // cerr << "line is: \"" << line << "\"" << endl; Chris@1429: QString id = PluginIdentifier::canonicalise Chris@165: (line.section("::", 0, 0)); Chris@1429: QString cat = line.section("::", 1, 1); Chris@1429: m_taxonomy[id] = cat; Chris@1429: // cerr << "set id \"" << id << "\" to cat \"" << cat << "\"" << endl; Chris@1429: } Chris@1429: } Chris@1429: } 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@1429: for (unsigned int i = 0; i < uris->count; ++i) { Chris@1429: m_lrdfTaxonomy[lrdf_get_uid(uris->items[i])] = base; Chris@1429: } Chris@1429: 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@1429: for (unsigned int i = 0; i < uris->count; ++i) { Chris@1429: char *label = lrdf_get_label(uris->items[i]); Chris@1429: generateTaxonomy(uris->items[i], Chris@1429: base + (base.length() > 0 ? " > " : "") + label); Chris@1429: } Chris@1429: 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: