Chris@390: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@390: Chris@390: /* Chris@390: Vamp Chris@390: Chris@390: An API for audio analysis and feature extraction plugins. Chris@390: Chris@390: Centre for Digital Music, Queen Mary, University of London. Chris@390: Copyright 2006-2015 Chris Cannam and QMUL. Chris@390: Chris@390: Permission is hereby granted, free of charge, to any person Chris@390: obtaining a copy of this software and associated documentation Chris@390: files (the "Software"), to deal in the Software without Chris@390: restriction, including without limitation the rights to use, copy, Chris@390: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@390: of the Software, and to permit persons to whom the Software is Chris@390: furnished to do so, subject to the following conditions: Chris@390: Chris@390: The above copyright notice and this permission notice shall be Chris@390: included in all copies or substantial portions of the Software. Chris@390: Chris@390: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@390: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@390: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@390: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@390: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@390: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@390: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@390: Chris@390: Except as contained in this notice, the names of the Centre for Chris@390: Digital Music; Queen Mary, University of London; and Chris Cannam Chris@390: shall not be used in advertising or otherwise to promote the sale, Chris@390: use or other dealings in this Software without prior written Chris@390: authorization. Chris@390: */ Chris@390: Chris@390: #include Chris@390: Chris@390: #include "Files.h" Chris@390: Chris@390: #include Chris@390: #include Chris@390: Chris@390: #include Chris@390: Chris@390: using namespace std; Chris@390: Chris@390: static vector files; Chris@390: static map cnames; Chris@390: static bool haveFiles = false; Chris@390: Chris@390: struct vhLibrary_t { Chris@390: vhLibrary_t(void *h, VampGetPluginDescriptorFunction f) Chris@390: : handle(h), func(f), nplugins(0) { } Chris@390: void *handle; Chris@390: VampGetPluginDescriptorFunction func; Chris@390: int nplugins; Chris@390: }; Chris@390: Chris@390: static void initFilenames() Chris@390: { Chris@390: if (!haveFiles) { Chris@390: files = Files::listLibraryFiles(); Chris@390: for (size_t i = 0; i < files.size(); ++i) { Chris@390: cnames[files[i]] = strdup(Files::lcBasename(files[i]).c_str()); Chris@390: } Chris@390: haveFiles = true; Chris@390: } Chris@390: } Chris@390: Chris@390: int vhGetLibraryCount() Chris@390: { Chris@390: initFilenames(); Chris@390: return int(files.size()); Chris@390: } Chris@390: Chris@393: const char *vhGetLibraryName(int index) Chris@390: { Chris@390: initFilenames(); Chris@393: if (index >= 0 && index < int(files.size())) { Chris@393: return cnames[files[index]]; Chris@390: } Chris@390: else return 0; Chris@390: } Chris@390: Chris@393: int vhGetLibraryIndex(const char *name) Chris@390: { Chris@390: for (size_t i = 0; i < files.size(); ++i) { Chris@393: if (Files::lcBasename(name) == Files::lcBasename(files[i])) { Chris@393: return i; Chris@390: } Chris@390: } Chris@393: return -1; Chris@393: } Chris@390: Chris@393: vhLibrary vhLoadLibrary(int index) Chris@393: { Chris@393: initFilenames(); Chris@393: if (index < 0 || index >= int(files.size())) { Chris@393: return 0; Chris@393: } Chris@393: Chris@393: string fullPath = files[index]; Chris@393: void *lib = Files::loadLibrary(fullPath); Chris@393: Chris@393: if (!lib) return 0; Chris@393: Chris@393: VampGetPluginDescriptorFunction func = Chris@393: (VampGetPluginDescriptorFunction)Files::lookupInLibrary Chris@393: (lib, "vampGetPluginDescriptor"); Chris@393: if (!func) { Chris@393: cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \"" Chris@393: << fullPath << "\"" << endl; Chris@393: Files::unloadLibrary(lib); Chris@393: return 0; Chris@393: } Chris@393: Chris@393: vhLibrary_t *vhl = new vhLibrary_t(lib, func); Chris@393: while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) { Chris@393: ++vhl->nplugins; Chris@393: } Chris@393: return vhl; Chris@390: } Chris@390: Chris@390: int vhGetPluginCount(vhLibrary library) Chris@390: { Chris@390: vhLibrary_t *vhl = static_cast(library); Chris@390: if (vhl) return vhl->nplugins; Chris@390: else return 0; Chris@390: } Chris@390: Chris@390: const VampPluginDescriptor *vhGetPluginDescriptor(vhLibrary library, Chris@390: int plugin) Chris@390: { Chris@390: vhLibrary_t *vhl = static_cast(library); Chris@390: if (vhl && plugin >= 0 && plugin < vhl->nplugins) { Chris@390: return vhl->func(VAMP_API_VERSION, plugin); Chris@390: } else { Chris@390: return 0; Chris@390: } Chris@390: } Chris@390: Chris@390: void vhUnloadLibrary(vhLibrary library) Chris@390: { Chris@390: vhLibrary_t *vhl = static_cast(library); Chris@390: if (vhl && vhl->handle) { Chris@390: Files::unloadLibrary(vhl->handle); Chris@390: } Chris@390: delete vhl; Chris@390: } Chris@390: