cannam@56: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@56: cannam@56: /* cannam@56: Vamp cannam@56: cannam@56: An API for audio analysis and feature extraction plugins. cannam@56: cannam@56: Centre for Digital Music, Queen Mary, University of London. cannam@56: Copyright 2006 Chris Cannam. cannam@56: cannam@56: Permission is hereby granted, free of charge, to any person cannam@56: obtaining a copy of this software and associated documentation cannam@56: files (the "Software"), to deal in the Software without cannam@56: restriction, including without limitation the rights to use, copy, cannam@56: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@56: of the Software, and to permit persons to whom the Software is cannam@56: furnished to do so, subject to the following conditions: cannam@56: cannam@56: The above copyright notice and this permission notice shall be cannam@56: included in all copies or substantial portions of the Software. cannam@56: cannam@56: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@56: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@56: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@56: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@56: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@56: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@56: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@56: cannam@56: Except as contained in this notice, the names of the Centre for cannam@56: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@56: shall not be used in advertising or otherwise to promote the sale, cannam@56: use or other dealings in this Software without prior written cannam@56: authorization. cannam@56: */ cannam@56: cannam@56: #include "PluginLoader.h" cannam@56: #include "PluginHostAdapter.h" cannam@56: cannam@56: #include "system.h" cannam@56: cannam@57: #include cannam@57: cannam@56: #include // POSIX directory open and read cannam@56: cannam@57: using namespace std; cannam@57: cannam@56: namespace Vamp { cannam@56: cannam@56: PluginLoader::PluginLoader() cannam@56: { cannam@56: } cannam@56: cannam@56: PluginLoader::~PluginLoader() cannam@56: { cannam@56: } cannam@56: cannam@57: vector cannam@56: PluginLoader::listPlugins() cannam@56: { cannam@56: if (m_pluginLibraryMap.empty()) { cannam@56: cannam@57: vector path = PluginHostAdapter::getPluginPath(); cannam@56: cannam@56: size_t suffixLen = strlen(PLUGIN_SUFFIX); cannam@56: cannam@56: for (size_t i = 0; i < path.size(); ++i) { cannam@57: cannam@57: vector files = getFilesInDir(path[i], PLUGIN_SUFFIX); cannam@57: cannam@56: cannam@57: for (vector::iterator fi = files.begin(); cannam@57: fi != files.end(); ++fi) { cannam@56: cannam@57: string basename = *fi; cannam@57: basename = basename.substr(0, basename.length() - suffixLen - 1); cannam@56: cannam@57: string fullPath = path[i]; cannam@57: fullPath = fullPath + "/" + *fi; //!!! systemize cannam@56: void *handle = DLOPEN(fullPath, RTLD_LAZY); cannam@56: cannam@56: if (!handle) { cannam@57: cerr << "Vamp::PluginLoader: " << *fi cannam@56: << ": unable to load library (" << DLERROR() cannam@57: << ")" << endl; cannam@56: continue; cannam@56: } cannam@56: cannam@56: VampGetPluginDescriptorFunction fn = cannam@56: (VampGetPluginDescriptorFunction)DLSYM cannam@56: (handle, "vampGetPluginDescriptor"); cannam@56: cannam@56: if (!fn) { cannam@56: DLCLOSE(handle); cannam@56: continue; cannam@56: } cannam@56: cannam@56: int index = 0; cannam@56: const VampPluginDescriptor *descriptor = 0; cannam@56: cannam@56: while ((descriptor = fn(VAMP_API_VERSION, index))) { cannam@56: PluginKey key = basename + ":" + descriptor->identifier; cannam@56: if (m_pluginLibraryMap.find(key) == cannam@56: m_pluginLibraryMap.end()) { cannam@56: m_pluginLibraryMap[key] = fullPath; cannam@56: } cannam@56: ++index; cannam@56: } cannam@56: cannam@56: DLCLOSE(handle); cannam@56: } cannam@56: } cannam@56: } cannam@56: cannam@57: vector plugins; cannam@57: for (map::iterator mi = cannam@56: m_pluginLibraryMap.begin(); cannam@56: mi != m_pluginLibraryMap.end(); ++mi) { cannam@56: plugins.push_back(mi->first); cannam@56: } cannam@56: cannam@56: return plugins; cannam@56: } cannam@56: cannam@57: PluginLoader::PluginCategoryHierarchy cannam@57: PluginLoader::getPluginCategory(PluginKey plugin) cannam@57: { cannam@57: if (m_taxonomy.empty()) generateTaxonomy(); cannam@57: if (m_taxonomy.find(plugin) == m_taxonomy.end()) return PluginCategoryHierarchy(); cannam@57: return m_taxonomy[plugin]; cannam@57: } cannam@57: cannam@57: string cannam@57: PluginLoader::getLibraryPathForPlugin(PluginKey plugin) cannam@56: { cannam@56: if (m_pluginLibraryMap.empty()) (void)listPlugins(); cannam@57: if (m_pluginLibraryMap.find(plugin) == m_pluginLibraryMap.end()) return ""; cannam@57: return m_pluginLibraryMap[plugin]; cannam@56: } cannam@56: cannam@56: Plugin * cannam@56: PluginLoader::load(PluginKey key, float inputSampleRate) cannam@56: { cannam@57: string fullPath = getLibraryPathForPlugin(key); cannam@56: if (fullPath == "") return 0; cannam@56: cannam@57: string::size_type ki = key.find(':'); cannam@57: if (ki == string::npos) { cannam@56: //!!! flag error cannam@56: return 0; cannam@56: } cannam@56: cannam@57: string identifier = key.substr(ki + 1); cannam@56: cannam@56: void *handle = DLOPEN(fullPath, RTLD_LAZY); cannam@56: cannam@56: if (!handle) { cannam@57: cerr << "Vamp::PluginLoader: " << fullPath cannam@56: << ": unable to load library (" << DLERROR() cannam@57: << ")" << endl; cannam@56: return 0; cannam@56: } cannam@56: cannam@56: VampGetPluginDescriptorFunction fn = cannam@56: (VampGetPluginDescriptorFunction)DLSYM cannam@56: (handle, "vampGetPluginDescriptor"); cannam@56: cannam@56: if (!fn) { cannam@56: //!!! refcount this! --!!! no, POSIX says dlopen/dlclose will cannam@56: // reference count. check on win32 cannam@56: DLCLOSE(handle); cannam@56: return 0; cannam@56: } cannam@56: cannam@56: int index = 0; cannam@56: const VampPluginDescriptor *descriptor = 0; cannam@56: cannam@56: while ((descriptor = fn(VAMP_API_VERSION, index))) { cannam@57: if (string(descriptor->identifier) == identifier) { cannam@56: return new Vamp::PluginHostAdapter(descriptor, inputSampleRate); cannam@56: } cannam@56: ++index; cannam@56: } cannam@56: cannam@56: //!!! flag error cannam@56: return 0; cannam@56: } cannam@56: cannam@57: vector cannam@57: PluginLoader::getFilesInDir(string dir, string extension) cannam@57: { cannam@57: vector files; cannam@57: cannam@57: DIR *d = opendir(dir.c_str()); cannam@57: if (!d) return files; cannam@57: cannam@57: struct dirent *e = 0; cannam@57: while ((e = readdir(d))) { cannam@57: cannam@57: if (!(e->d_type & DT_REG) || !e->d_name) { cannam@57: continue; cannam@57: } cannam@57: cannam@57: int len = strlen(e->d_name); cannam@57: if (len < int(extension.length() + 2) || cannam@57: e->d_name[len - extension.length() - 1] != '.' || cannam@57: strcmp(e->d_name + len - extension.length(), extension.c_str())) { cannam@57: continue; cannam@57: } cannam@57: cannam@57: files.push_back(e->d_name); cannam@57: } cannam@57: cannam@57: closedir(d); cannam@57: cannam@57: return files; cannam@56: } cannam@56: cannam@57: void cannam@57: PluginLoader::generateTaxonomy() cannam@57: { cannam@57: // cerr << "PluginLoader::generateTaxonomy" << endl; cannam@57: cannam@57: vector path = PluginHostAdapter::getPluginPath(); cannam@57: string libfragment = "/lib/"; cannam@57: vector catpath; cannam@57: cannam@57: string suffix = "cat"; cannam@57: cannam@57: for (vector::iterator i = path.begin(); cannam@57: i != path.end(); ++i) { cannam@57: cannam@57: string dir = *i; cannam@57: string::size_type li = dir.find(libfragment); cannam@57: cannam@57: if (li != string::npos) { cannam@57: catpath.push_back cannam@57: (dir.substr(0, li) cannam@57: + "/share/" cannam@57: + dir.substr(li + libfragment.length())); cannam@57: } cannam@57: cannam@57: catpath.push_back(dir); cannam@57: } cannam@57: cannam@57: char buffer[1024]; cannam@57: cannam@57: for (vector::iterator i = catpath.begin(); cannam@57: i != catpath.end(); ++i) { cannam@57: cannam@57: vector files = getFilesInDir(*i, suffix); cannam@57: cannam@57: for (vector::iterator fi = files.begin(); cannam@57: fi != files.end(); ++fi) { cannam@57: cannam@57: string filepath = *i + "/" + *fi; //!!! systemize cannam@57: ifstream is(filepath.c_str(), ifstream::in | ifstream::binary); cannam@57: cannam@57: if (is.fail()) { cannam@57: // cerr << "failed to open: " << filepath << endl; cannam@57: continue; cannam@57: } cannam@57: cannam@57: // cerr << "opened: " << filepath << endl; cannam@57: cannam@57: while (!!is.getline(buffer, 1024)) { cannam@57: cannam@57: string line(buffer); cannam@57: cannam@57: // cerr << "line = " << line << endl; cannam@57: cannam@57: string::size_type di = line.find("::"); cannam@57: if (di == string::npos) continue; cannam@57: cannam@57: string id = line.substr(0, di); cannam@57: string encodedCat = line.substr(di + 2); cannam@57: cannam@57: if (id.substr(0, 5) != "vamp:") continue; cannam@57: id = id.substr(5); cannam@57: cannam@57: while (encodedCat.length() >= 1 && cannam@57: encodedCat[encodedCat.length()-1] == '\r') { cannam@57: encodedCat = encodedCat.substr(0, encodedCat.length()-1); cannam@57: } cannam@57: cannam@57: // cerr << "id = " << id << ", cat = " << encodedCat << endl; cannam@57: cannam@57: PluginCategoryHierarchy category; cannam@57: string::size_type ai; cannam@57: while ((ai = encodedCat.find(" > ")) != string::npos) { cannam@57: category.push_back(encodedCat.substr(0, ai)); cannam@57: encodedCat = encodedCat.substr(ai + 3); cannam@57: } cannam@57: if (encodedCat != "") category.push_back(encodedCat); cannam@57: cannam@57: m_taxonomy[id] = category; cannam@57: } cannam@57: } cannam@57: } cannam@57: } cannam@57: cannam@57: cannam@57: }