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