annotate src/vamp-hostsdk/host-c.cpp @ 390:06988ce35ff0 vh

Initial draft of C API for plugin loading
author Chris Cannam
date Wed, 20 May 2015 16:12:18 +0100
parents
children 632c662e95e7
rev   line source
Chris@390 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@390 2
Chris@390 3 /*
Chris@390 4 Vamp
Chris@390 5
Chris@390 6 An API for audio analysis and feature extraction plugins.
Chris@390 7
Chris@390 8 Centre for Digital Music, Queen Mary, University of London.
Chris@390 9 Copyright 2006-2015 Chris Cannam and QMUL.
Chris@390 10
Chris@390 11 Permission is hereby granted, free of charge, to any person
Chris@390 12 obtaining a copy of this software and associated documentation
Chris@390 13 files (the "Software"), to deal in the Software without
Chris@390 14 restriction, including without limitation the rights to use, copy,
Chris@390 15 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@390 16 of the Software, and to permit persons to whom the Software is
Chris@390 17 furnished to do so, subject to the following conditions:
Chris@390 18
Chris@390 19 The above copyright notice and this permission notice shall be
Chris@390 20 included in all copies or substantial portions of the Software.
Chris@390 21
Chris@390 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@390 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@390 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@390 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@390 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@390 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@390 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@390 29
Chris@390 30 Except as contained in this notice, the names of the Centre for
Chris@390 31 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@390 32 shall not be used in advertising or otherwise to promote the sale,
Chris@390 33 use or other dealings in this Software without prior written
Chris@390 34 authorization.
Chris@390 35 */
Chris@390 36
Chris@390 37 #include <vamp-hostsdk/host-c.h>
Chris@390 38
Chris@390 39 #include "Files.h"
Chris@390 40
Chris@390 41 #include <map>
Chris@390 42 #include <iostream>
Chris@390 43
Chris@390 44 #include <cstring>
Chris@390 45
Chris@390 46 using namespace std;
Chris@390 47
Chris@390 48 static vector<string> files;
Chris@390 49 static map<string, const char *> cnames;
Chris@390 50 static bool haveFiles = false;
Chris@390 51
Chris@390 52 struct vhLibrary_t {
Chris@390 53 vhLibrary_t(void *h, VampGetPluginDescriptorFunction f)
Chris@390 54 : handle(h), func(f), nplugins(0) { }
Chris@390 55 void *handle;
Chris@390 56 VampGetPluginDescriptorFunction func;
Chris@390 57 int nplugins;
Chris@390 58 };
Chris@390 59
Chris@390 60 static void initFilenames()
Chris@390 61 {
Chris@390 62 if (!haveFiles) {
Chris@390 63 files = Files::listLibraryFiles();
Chris@390 64 for (size_t i = 0; i < files.size(); ++i) {
Chris@390 65 cnames[files[i]] = strdup(Files::lcBasename(files[i]).c_str());
Chris@390 66 }
Chris@390 67 haveFiles = true;
Chris@390 68 }
Chris@390 69 }
Chris@390 70
Chris@390 71 int vhGetLibraryCount()
Chris@390 72 {
Chris@390 73 initFilenames();
Chris@390 74 return int(files.size());
Chris@390 75 }
Chris@390 76
Chris@390 77 const char *vhGetLibraryName(int library)
Chris@390 78 {
Chris@390 79 initFilenames();
Chris@390 80 if (library < int(files.size())) {
Chris@390 81 return cnames[files[library]];
Chris@390 82 }
Chris@390 83 else return 0;
Chris@390 84 }
Chris@390 85
Chris@390 86 vhLibrary vhLoadLibrary(const char *libraryName)
Chris@390 87 {
Chris@390 88 for (size_t i = 0; i < files.size(); ++i) {
Chris@390 89
Chris@390 90 if (Files::lcBasename(libraryName) == Files::lcBasename(files[i])) {
Chris@390 91
Chris@390 92 string fullPath = files[i];
Chris@390 93 void *lib = Files::loadLibrary(fullPath);
Chris@390 94
Chris@390 95 if (!lib) return 0;
Chris@390 96
Chris@390 97 VampGetPluginDescriptorFunction func =
Chris@390 98 (VampGetPluginDescriptorFunction)Files::lookupInLibrary
Chris@390 99 (lib, "vampGetPluginDescriptor");
Chris@390 100 if (!func) {
Chris@390 101 cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \""
Chris@390 102 << fullPath << "\"" << endl;
Chris@390 103 Files::unloadLibrary(lib);
Chris@390 104 return 0;
Chris@390 105 }
Chris@390 106
Chris@390 107 vhLibrary_t *vhl = new vhLibrary_t(lib, func);
Chris@390 108 while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) {
Chris@390 109 ++vhl->nplugins;
Chris@390 110 }
Chris@390 111 return vhl;
Chris@390 112 }
Chris@390 113 }
Chris@390 114
Chris@390 115 return 0;
Chris@390 116 }
Chris@390 117
Chris@390 118 int vhGetPluginCount(vhLibrary library)
Chris@390 119 {
Chris@390 120 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 121 if (vhl) return vhl->nplugins;
Chris@390 122 else return 0;
Chris@390 123 }
Chris@390 124
Chris@390 125 const VampPluginDescriptor *vhGetPluginDescriptor(vhLibrary library,
Chris@390 126 int plugin)
Chris@390 127 {
Chris@390 128 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 129 if (vhl && plugin >= 0 && plugin < vhl->nplugins) {
Chris@390 130 return vhl->func(VAMP_API_VERSION, plugin);
Chris@390 131 } else {
Chris@390 132 return 0;
Chris@390 133 }
Chris@390 134 }
Chris@390 135
Chris@390 136 void vhUnloadLibrary(vhLibrary library)
Chris@390 137 {
Chris@390 138 vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
Chris@390 139 if (vhl && vhl->handle) {
Chris@390 140 Files::unloadLibrary(vhl->handle);
Chris@390 141 }
Chris@390 142 delete vhl;
Chris@390 143 }
Chris@390 144