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 {
vhLibrary_tvhLibrary_t53     vhLibrary_t(void *h, VampGetPluginDescriptorFunction f)
54         : handle(h), func(f), nplugins(0) { }
55     void *handle;
56     VampGetPluginDescriptorFunction func;
57     int nplugins;
58 };
59 
initFilenames()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 
vhGetLibraryCount()71 int vhGetLibraryCount()
72 {
73     initFilenames();
74     return int(files.size());
75 }
76 
vhGetLibraryName(int index)77 const char *vhGetLibraryName(int index)
78 {
79     initFilenames();
80     if (index >= 0 && index < int(files.size())) {
81         return cnames[files[index]];
82     }
83     else return 0;
84 }
85 
vhGetLibraryIndex(const char * name)86 int vhGetLibraryIndex(const char *name)
87 {
88     for (size_t i = 0; i < files.size(); ++i) {
89         if (Files::lcBasename(name) == Files::lcBasename(files[i])) {
90             return i;
91         }
92     }
93     return -1;
94 }
95 
vhLoadLibrary(int index)96 vhLibrary vhLoadLibrary(int index)
97 {
98     initFilenames();
99     if (index < 0 || index >= int(files.size())) {
100         return 0;
101     }
102 
103     string fullPath = files[index];
104     void *lib = Files::loadLibrary(fullPath);
105 
106     if (!lib) return 0;
107 
108     VampGetPluginDescriptorFunction func =
109         (VampGetPluginDescriptorFunction)Files::lookupInLibrary
110         (lib, "vampGetPluginDescriptor");
111     if (!func) {
112         cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \""
113              << fullPath << "\"" << endl;
114         Files::unloadLibrary(lib);
115         return 0;
116     }
117 
118     vhLibrary_t *vhl = new vhLibrary_t(lib, func);
119     while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) {
120         ++vhl->nplugins;
121     }
122     return vhl;
123 }
124 
vhGetPluginCount(vhLibrary library)125 int vhGetPluginCount(vhLibrary library)
126 {
127     vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
128     if (vhl) return vhl->nplugins;
129     else return 0;
130 }
131 
vhGetPluginDescriptor(vhLibrary library,int plugin)132 const VampPluginDescriptor *vhGetPluginDescriptor(vhLibrary library,
133                                                   int plugin)
134 {
135     vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
136     if (vhl && plugin >= 0 && plugin < vhl->nplugins) {
137         return vhl->func(VAMP_API_VERSION, plugin);
138     } else {
139         return 0;
140     }
141 }
142 
vhUnloadLibrary(vhLibrary library)143 void vhUnloadLibrary(vhLibrary library)
144 {
145     vhLibrary_t *vhl = static_cast<vhLibrary_t *>(library);
146     if (vhl && vhl->handle) {
147         Files::unloadLibrary(vhl->handle);
148     }
149     delete vhl;
150 }
151 
152