cannam@233: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@233: cannam@233: /* cannam@233: Vamp cannam@233: cannam@233: An API for audio analysis and feature extraction plugins. cannam@233: cannam@233: Centre for Digital Music, Queen Mary, University of London. Chris@423: Copyright 2006-2016 Chris Cannam and QMUL. cannam@233: cannam@233: Permission is hereby granted, free of charge, to any person cannam@233: obtaining a copy of this software and associated documentation cannam@233: files (the "Software"), to deal in the Software without cannam@233: restriction, including without limitation the rights to use, copy, cannam@233: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@233: of the Software, and to permit persons to whom the Software is cannam@233: furnished to do so, subject to the following conditions: cannam@233: cannam@233: The above copyright notice and this permission notice shall be cannam@233: included in all copies or substantial portions of the Software. cannam@233: cannam@233: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@233: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@233: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@233: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@233: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@233: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@233: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@233: cannam@233: Except as contained in this notice, the names of the Centre for cannam@233: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@233: shall not be used in advertising or otherwise to promote the sale, cannam@233: use or other dealings in this Software without prior written cannam@233: authorization. cannam@233: */ cannam@233: cannam@233: #include cannam@233: #include cannam@233: #include cannam@233: #include Chris@390: #include Chris@390: Chris@390: #include Chris@390: Chris@390: #include "Files.h" cannam@233: cannam@233: #include cannam@233: cannam@233: using namespace std; cannam@233: cannam@263: _VAMP_SDK_HOSTSPACE_BEGIN(PluginLoader.cpp) cannam@263: cannam@233: namespace Vamp { cannam@233: cannam@233: namespace HostExt { cannam@233: cannam@233: class PluginLoader::Impl cannam@233: { cannam@233: public: cannam@233: Impl(); cannam@233: virtual ~Impl(); cannam@233: cannam@233: PluginKeyList listPlugins(); Chris@426: PluginStaticDataList listPluginData(); cannam@233: cannam@233: Plugin *loadPlugin(PluginKey key, cannam@233: float inputSampleRate, cannam@233: int adapterFlags); cannam@233: Chris@423: LoadResponse loadPlugin(LoadRequest req); Chris@423: Chris@431: ConfigurationResponse configurePlugin(ConfigurationRequest req); Chris@423: cannam@233: PluginKey composePluginKey(string libraryName, string identifier); cannam@233: cannam@233: PluginCategoryHierarchy getPluginCategory(PluginKey key); cannam@233: cannam@233: string getLibraryPathForPlugin(PluginKey key); cannam@233: cannam@233: static void setInstanceToClean(PluginLoader *instance); cannam@233: cannam@233: protected: cannam@233: class PluginDeletionNotifyAdapter : public PluginWrapper { cannam@233: public: cannam@233: PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader); cannam@233: virtual ~PluginDeletionNotifyAdapter(); cannam@233: protected: cannam@233: Impl *m_loader; cannam@233: }; cannam@233: cannam@233: class InstanceCleaner { cannam@233: public: cannam@233: InstanceCleaner() : m_instance(0) { } cannam@233: ~InstanceCleaner() { delete m_instance; } cannam@233: void setInstance(PluginLoader *instance) { m_instance = instance; } cannam@233: protected: cannam@233: PluginLoader *m_instance; cannam@233: }; cannam@233: cannam@233: virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter); cannam@233: cannam@233: map m_pluginLibraryNameMap; cannam@233: bool m_allPluginsEnumerated; cannam@233: void enumeratePlugins(PluginKey forPlugin = ""); cannam@233: cannam@233: map m_taxonomy; cannam@233: void generateTaxonomy(); cannam@233: cannam@233: map m_pluginLibraryHandleMap; cannam@233: cannam@233: bool decomposePluginKey(PluginKey key, cannam@233: string &libraryName, string &identifier); cannam@233: cannam@233: static InstanceCleaner m_cleaner; cannam@233: }; cannam@233: cannam@233: PluginLoader * cannam@233: PluginLoader::m_instance = 0; cannam@233: cannam@233: PluginLoader::Impl::InstanceCleaner cannam@233: PluginLoader::Impl::m_cleaner; cannam@233: cannam@233: PluginLoader::PluginLoader() cannam@233: { cannam@233: m_impl = new Impl(); cannam@233: } cannam@233: cannam@233: PluginLoader::~PluginLoader() cannam@233: { cannam@233: delete m_impl; cannam@233: } cannam@233: cannam@233: PluginLoader * cannam@233: PluginLoader::getInstance() cannam@233: { cannam@233: if (!m_instance) { cannam@233: // The cleaner doesn't own the instance, because we leave the cannam@233: // instance pointer in the base class for binary backwards cannam@233: // compatibility reasons and to avoid waste cannam@233: m_instance = new PluginLoader(); cannam@233: Impl::setInstanceToClean(m_instance); cannam@233: } cannam@233: return m_instance; cannam@233: } cannam@233: Chris@426: PluginLoader::PluginKeyList cannam@233: PluginLoader::listPlugins() cannam@233: { cannam@233: return m_impl->listPlugins(); cannam@233: } cannam@233: Chris@426: PluginLoader::PluginStaticDataList Chris@426: PluginLoader::listPluginData() Chris@426: { Chris@426: return m_impl->listPluginData(); Chris@426: } Chris@426: cannam@233: Plugin * cannam@233: PluginLoader::loadPlugin(PluginKey key, cannam@233: float inputSampleRate, cannam@233: int adapterFlags) cannam@233: { cannam@233: return m_impl->loadPlugin(key, inputSampleRate, adapterFlags); cannam@233: } cannam@233: Chris@423: LoadResponse Chris@423: PluginLoader::loadPlugin(LoadRequest req) Chris@423: { Chris@423: return m_impl->loadPlugin(req); Chris@423: } Chris@423: Chris@431: ConfigurationResponse Chris@431: PluginLoader::configurePlugin(ConfigurationRequest req) Chris@423: { Chris@431: return m_impl->configurePlugin(req); Chris@423: } Chris@423: cannam@233: PluginLoader::PluginKey cannam@233: PluginLoader::composePluginKey(string libraryName, string identifier) cannam@233: { cannam@233: return m_impl->composePluginKey(libraryName, identifier); cannam@233: } cannam@233: cannam@233: PluginLoader::PluginCategoryHierarchy cannam@233: PluginLoader::getPluginCategory(PluginKey key) cannam@233: { cannam@233: return m_impl->getPluginCategory(key); cannam@233: } cannam@233: cannam@233: string cannam@233: PluginLoader::getLibraryPathForPlugin(PluginKey key) cannam@233: { cannam@233: return m_impl->getLibraryPathForPlugin(key); cannam@233: } cannam@233: cannam@233: PluginLoader::Impl::Impl() : cannam@233: m_allPluginsEnumerated(false) cannam@233: { cannam@233: } cannam@233: cannam@233: PluginLoader::Impl::~Impl() cannam@233: { cannam@233: } cannam@233: cannam@233: void cannam@233: PluginLoader::Impl::setInstanceToClean(PluginLoader *instance) cannam@233: { cannam@233: m_cleaner.setInstance(instance); cannam@233: } cannam@233: Chris@426: PluginLoader::PluginKeyList cannam@233: PluginLoader::Impl::listPlugins() cannam@233: { cannam@233: if (!m_allPluginsEnumerated) enumeratePlugins(); cannam@233: cannam@233: vector plugins; cannam@233: for (map::iterator mi = m_pluginLibraryNameMap.begin(); cannam@233: mi != m_pluginLibraryNameMap.end(); ++mi) { cannam@233: plugins.push_back(mi->first); cannam@233: } cannam@233: cannam@233: return plugins; cannam@233: } cannam@233: Chris@426: PluginLoader::PluginStaticDataList Chris@426: PluginLoader::Impl::listPluginData() Chris@426: { Chris@426: PluginKeyList keys = listPlugins(); Chris@426: PluginStaticDataList dataList; Chris@426: Chris@426: for (PluginKeyList::const_iterator ki = keys.begin(); ki != keys.end(); ++ki) { Chris@426: string key = *ki; Chris@426: Plugin *p = loadPlugin(key, 44100, 0); Chris@426: if (p) { Chris@426: PluginCategoryHierarchy category = getPluginCategory(key); Chris@426: dataList.push_back(PluginStaticData::fromPlugin(key, category, p)); Chris@426: } Chris@426: delete p; Chris@426: } Chris@426: Chris@426: return dataList; Chris@426: } Chris@426: cannam@233: void cannam@233: PluginLoader::Impl::enumeratePlugins(PluginKey forPlugin) cannam@233: { cannam@233: string libraryName, identifier; Chris@390: vector fullPaths; Chris@390: cannam@233: if (forPlugin != "") { cannam@233: if (!decomposePluginKey(forPlugin, libraryName, identifier)) { cannam@233: std::cerr << "WARNING: Vamp::HostExt::PluginLoader: Invalid plugin key \"" cannam@233: << forPlugin << "\" in enumerate" << std::endl; cannam@233: return; cannam@233: } Chris@390: fullPaths = Files::listLibraryFilesMatching(libraryName); Chris@390: } else { Chris@390: fullPaths = Files::listLibraryFiles(); cannam@233: } cannam@233: Chris@390: for (size_t i = 0; i < fullPaths.size(); ++i) { cannam@233: Chris@390: string fullPath = fullPaths[i]; Chris@390: void *handle = Files::loadLibrary(fullPath); Chris@390: if (!handle) continue; cannam@233: Chris@390: VampGetPluginDescriptorFunction fn = Chris@390: (VampGetPluginDescriptorFunction)Files::lookupInLibrary Chris@390: (handle, "vampGetPluginDescriptor"); cannam@233: Chris@390: if (!fn) { Chris@390: if (forPlugin != "") { Chris@390: cerr << "Vamp::HostExt::PluginLoader: No vampGetPluginDescriptor function found in library \"" cannam@295: << fullPath << "\"" << endl; cannam@295: } Chris@390: Files::unloadLibrary(handle); Chris@390: continue; Chris@390: } cannam@233: Chris@390: int index = 0; Chris@390: const VampPluginDescriptor *descriptor = 0; Chris@390: bool found = false; Chris@390: Chris@390: while ((descriptor = fn(VAMP_API_VERSION, index))) { Chris@390: ++index; Chris@390: if (identifier != "") { Chris@390: if (descriptor->identifier != identifier) continue; Chris@390: } Chris@390: found = true; Chris@390: PluginKey key = composePluginKey(fullPath, descriptor->identifier); Chris@390: // std::cerr << "enumerate: " << key << " (path: " << fullPath << ")" << std::endl; Chris@390: if (m_pluginLibraryNameMap.find(key) == Chris@390: m_pluginLibraryNameMap.end()) { Chris@390: m_pluginLibraryNameMap[key] = fullPath; Chris@390: } cannam@233: } Chris@390: Chris@390: if (!found && forPlugin != "") { Chris@390: cerr << "Vamp::HostExt::PluginLoader: Plugin \"" Chris@390: << identifier << "\" not found in library \"" Chris@390: << fullPath << "\"" << endl; Chris@390: } Chris@390: Chris@390: Files::unloadLibrary(handle); cannam@233: } cannam@233: cannam@233: if (forPlugin == "") m_allPluginsEnumerated = true; cannam@233: } cannam@233: cannam@233: PluginLoader::PluginKey cannam@233: PluginLoader::Impl::composePluginKey(string libraryName, string identifier) cannam@233: { Chris@390: string basename = Files::lcBasename(libraryName); cannam@233: return basename + ":" + identifier; cannam@233: } cannam@233: cannam@233: bool cannam@233: PluginLoader::Impl::decomposePluginKey(PluginKey key, cannam@233: string &libraryName, cannam@233: string &identifier) cannam@233: { cannam@233: string::size_type ki = key.find(':'); cannam@233: if (ki == string::npos) { cannam@233: return false; cannam@233: } cannam@233: cannam@233: libraryName = key.substr(0, ki); cannam@233: identifier = key.substr(ki + 1); cannam@233: return true; cannam@233: } cannam@233: cannam@233: PluginLoader::PluginCategoryHierarchy cannam@233: PluginLoader::Impl::getPluginCategory(PluginKey plugin) cannam@233: { cannam@233: if (m_taxonomy.empty()) generateTaxonomy(); cannam@233: if (m_taxonomy.find(plugin) == m_taxonomy.end()) { cannam@233: return PluginCategoryHierarchy(); cannam@233: } cannam@233: return m_taxonomy[plugin]; cannam@233: } cannam@233: cannam@233: string cannam@233: PluginLoader::Impl::getLibraryPathForPlugin(PluginKey plugin) cannam@233: { cannam@233: if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) { cannam@233: if (m_allPluginsEnumerated) return ""; cannam@233: enumeratePlugins(plugin); cannam@233: } cannam@233: if (m_pluginLibraryNameMap.find(plugin) == m_pluginLibraryNameMap.end()) { cannam@233: return ""; cannam@233: } cannam@233: return m_pluginLibraryNameMap[plugin]; cannam@233: } cannam@233: cannam@233: Plugin * cannam@233: PluginLoader::Impl::loadPlugin(PluginKey key, cannam@233: float inputSampleRate, int adapterFlags) cannam@233: { cannam@233: string libname, identifier; cannam@233: if (!decomposePluginKey(key, libname, identifier)) { cannam@233: std::cerr << "Vamp::HostExt::PluginLoader: Invalid plugin key \"" cannam@233: << key << "\" in loadPlugin" << std::endl; cannam@233: return 0; cannam@233: } cannam@233: cannam@233: string fullPath = getLibraryPathForPlugin(key); cannam@293: if (fullPath == "") { cannam@295: std::cerr << "Vamp::HostExt::PluginLoader: No library found in Vamp path for plugin \"" << key << "\"" << std::endl; cannam@293: return 0; cannam@293: } cannam@233: Chris@390: void *handle = Files::loadLibrary(fullPath); cannam@233: if (!handle) return 0; cannam@233: cannam@233: VampGetPluginDescriptorFunction fn = Chris@390: (VampGetPluginDescriptorFunction)Files::lookupInLibrary cannam@233: (handle, "vampGetPluginDescriptor"); cannam@233: cannam@233: if (!fn) { cannam@293: cerr << "Vamp::HostExt::PluginLoader: No vampGetPluginDescriptor function found in library \"" cannam@293: << fullPath << "\"" << endl; Chris@390: Files::unloadLibrary(handle); cannam@233: return 0; cannam@233: } cannam@233: cannam@233: int index = 0; cannam@233: const VampPluginDescriptor *descriptor = 0; cannam@233: cannam@233: while ((descriptor = fn(VAMP_API_VERSION, index))) { cannam@233: cannam@233: if (string(descriptor->identifier) == identifier) { cannam@233: cannam@233: Vamp::PluginHostAdapter *plugin = cannam@233: new Vamp::PluginHostAdapter(descriptor, inputSampleRate); cannam@233: cannam@233: Plugin *adapter = new PluginDeletionNotifyAdapter(plugin, this); cannam@233: cannam@233: m_pluginLibraryHandleMap[adapter] = handle; cannam@233: cannam@233: if (adapterFlags & ADAPT_INPUT_DOMAIN) { cannam@233: if (adapter->getInputDomain() == Plugin::FrequencyDomain) { cannam@233: adapter = new PluginInputDomainAdapter(adapter); cannam@233: } cannam@233: } cannam@233: cannam@233: if (adapterFlags & ADAPT_BUFFER_SIZE) { cannam@233: adapter = new PluginBufferingAdapter(adapter); cannam@233: } cannam@233: cannam@233: if (adapterFlags & ADAPT_CHANNEL_COUNT) { cannam@233: adapter = new PluginChannelAdapter(adapter); cannam@233: } cannam@233: cannam@233: return adapter; cannam@233: } cannam@233: cannam@233: ++index; cannam@233: } cannam@233: cannam@233: cerr << "Vamp::HostExt::PluginLoader: Plugin \"" cannam@233: << identifier << "\" not found in library \"" cannam@233: << fullPath << "\"" << endl; cannam@233: cannam@233: return 0; cannam@233: } cannam@233: Chris@423: LoadResponse Chris@423: PluginLoader::Impl::loadPlugin(LoadRequest req) Chris@423: { Chris@423: Plugin *plugin = loadPlugin(req.pluginKey, Chris@423: req.inputSampleRate, Chris@423: req.adapterFlags); Chris@423: LoadResponse response; Chris@425: response.plugin = plugin; Chris@423: if (!plugin) return response; Chris@423: Chris@423: response.plugin = plugin; Chris@423: response.staticData = PluginStaticData::fromPlugin Chris@423: (req.pluginKey, Chris@423: getPluginCategory(req.pluginKey), Chris@423: plugin); Chris@423: Chris@423: int defaultChannels = 0; Chris@423: if (plugin->getMinChannelCount() == plugin->getMaxChannelCount()) { Chris@423: defaultChannels = plugin->getMinChannelCount(); Chris@423: } Chris@423: Chris@423: response.defaultConfiguration = PluginConfiguration::fromPlugin Chris@423: (plugin, Chris@423: defaultChannels, Chris@423: plugin->getPreferredStepSize(), Chris@423: plugin->getPreferredBlockSize()); Chris@423: Chris@423: return response; Chris@423: } Chris@423: Chris@431: ConfigurationResponse Chris@431: PluginLoader::Impl::configurePlugin(ConfigurationRequest req) Chris@423: { Chris@423: for (PluginConfiguration::ParameterMap::const_iterator i = Chris@431: req.configuration.parameterValues.begin(); Chris@431: i != req.configuration.parameterValues.end(); ++i) { Chris@431: req.plugin->setParameter(i->first, i->second); Chris@423: } Chris@423: Chris@431: if (req.configuration.currentProgram != "") { Chris@431: req.plugin->selectProgram(req.configuration.currentProgram); Chris@423: } Chris@423: Chris@431: ConfigurationResponse response; Chris@431: Chris@431: if (req.plugin->initialise(req.configuration.channelCount, Chris@431: req.configuration.stepSize, Chris@431: req.configuration.blockSize)) { Chris@431: response.outputs = req.plugin->getOutputDescriptors(); Chris@423: } Chris@431: Chris@431: return response; Chris@423: } Chris@423: cannam@233: void cannam@233: PluginLoader::Impl::generateTaxonomy() cannam@233: { cannam@233: // cerr << "PluginLoader::Impl::generateTaxonomy" << endl; cannam@233: cannam@233: vector path = PluginHostAdapter::getPluginPath(); cannam@233: string libfragment = "/lib/"; cannam@233: vector catpath; cannam@233: cannam@233: string suffix = "cat"; cannam@233: cannam@233: for (vector::iterator i = path.begin(); cannam@233: i != path.end(); ++i) { cannam@233: cannam@233: // It doesn't matter that we're using literal forward-slash in cannam@233: // this bit, as it's only relevant if the path contains cannam@233: // "/lib/", which is only meaningful and only plausible on cannam@233: // systems with forward-slash delimiters cannam@233: cannam@233: string dir = *i; cannam@233: string::size_type li = dir.find(libfragment); cannam@233: cannam@233: if (li != string::npos) { cannam@233: catpath.push_back cannam@233: (dir.substr(0, li) cannam@233: + "/share/" cannam@233: + dir.substr(li + libfragment.length())); cannam@233: } cannam@233: cannam@233: catpath.push_back(dir); cannam@233: } cannam@233: cannam@233: char buffer[1024]; cannam@233: cannam@233: for (vector::iterator i = catpath.begin(); cannam@233: i != catpath.end(); ++i) { cannam@233: Chris@390: vector files = Files::listFiles(*i, suffix); cannam@233: cannam@233: for (vector::iterator fi = files.begin(); cannam@233: fi != files.end(); ++fi) { cannam@233: Chris@390: string filepath = Files::splicePath(*i, *fi); cannam@233: ifstream is(filepath.c_str(), ifstream::in | ifstream::binary); cannam@233: cannam@233: if (is.fail()) { cannam@233: // cerr << "failed to open: " << filepath << endl; cannam@233: continue; cannam@233: } cannam@233: cannam@233: // cerr << "opened: " << filepath << endl; cannam@233: cannam@233: while (!!is.getline(buffer, 1024)) { cannam@233: cannam@233: string line(buffer); cannam@233: cannam@233: // cerr << "line = " << line << endl; cannam@233: cannam@233: string::size_type di = line.find("::"); cannam@233: if (di == string::npos) continue; cannam@233: cannam@233: string id = line.substr(0, di); cannam@233: string encodedCat = line.substr(di + 2); cannam@233: cannam@233: if (id.substr(0, 5) != "vamp:") continue; cannam@233: id = id.substr(5); cannam@233: cannam@233: while (encodedCat.length() >= 1 && cannam@233: encodedCat[encodedCat.length()-1] == '\r') { cannam@233: encodedCat = encodedCat.substr(0, encodedCat.length()-1); cannam@233: } cannam@233: cannam@233: // cerr << "id = " << id << ", cat = " << encodedCat << endl; cannam@233: cannam@233: PluginCategoryHierarchy category; cannam@233: string::size_type ai; cannam@233: while ((ai = encodedCat.find(" > ")) != string::npos) { cannam@233: category.push_back(encodedCat.substr(0, ai)); cannam@233: encodedCat = encodedCat.substr(ai + 3); cannam@233: } cannam@233: if (encodedCat != "") category.push_back(encodedCat); cannam@233: cannam@233: m_taxonomy[id] = category; cannam@233: } cannam@233: } cannam@233: } cannam@233: } cannam@233: cannam@233: void cannam@233: PluginLoader::Impl::pluginDeleted(PluginDeletionNotifyAdapter *adapter) cannam@233: { cannam@233: void *handle = m_pluginLibraryHandleMap[adapter]; Chris@390: if (handle) Files::unloadLibrary(handle); cannam@233: m_pluginLibraryHandleMap.erase(adapter); cannam@233: } cannam@233: cannam@233: PluginLoader::Impl::PluginDeletionNotifyAdapter::PluginDeletionNotifyAdapter(Plugin *plugin, cannam@233: Impl *loader) : cannam@233: PluginWrapper(plugin), cannam@233: m_loader(loader) cannam@233: { cannam@233: } cannam@233: cannam@233: PluginLoader::Impl::PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter() cannam@233: { cannam@233: // We need to delete the plugin before calling pluginDeleted, as cannam@233: // the delete call may require calling through to the descriptor cannam@233: // (for e.g. cleanup) but pluginDeleted may unload the required cannam@233: // library for the call. To prevent a double deletion when our cannam@233: // parent's destructor runs (after this one), be sure to set cannam@233: // m_plugin to 0 after deletion. cannam@233: delete m_plugin; cannam@233: m_plugin = 0; cannam@233: cannam@233: if (m_loader) m_loader->pluginDeleted(this); cannam@233: } cannam@233: cannam@233: } cannam@233: cannam@233: } cannam@263: cannam@263: _VAMP_SDK_HOSTSPACE_END(PluginLoader.cpp) cannam@263: