comparison vamp-hostsdk/PluginLoader.cpp @ 56:4ab6224110ef host-factory-stuff

* implement plugin loader and plugin input-domain adapter (to do basic ffts)
author cannam
date Fri, 04 May 2007 15:21:12 +0000
parents
children 09a1aac6c362
comparison
equal deleted inserted replaced
55:0dad357a3406 56:4ab6224110ef
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 Chris Cannam.
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 "PluginLoader.h"
38 #include "PluginHostAdapter.h"
39
40 #include "system.h"
41
42 #include <dirent.h> // POSIX directory open and read
43
44 namespace Vamp {
45
46 PluginLoader::PluginLoader()
47 {
48 }
49
50 PluginLoader::~PluginLoader()
51 {
52 }
53
54 std::vector<PluginLoader::PluginKey>
55 PluginLoader::listPlugins()
56 {
57 if (m_pluginLibraryMap.empty()) {
58
59 std::vector<std::string> path = PluginHostAdapter::getPluginPath();
60
61 size_t suffixLen = strlen(PLUGIN_SUFFIX);
62
63 for (size_t i = 0; i < path.size(); ++i) {
64
65 DIR *d = opendir(path[i].c_str());
66 if (!d) {
67 // perror("Failed to open directory");
68 continue;
69 }
70
71 struct dirent *e = 0;
72 while ((e = readdir(d))) {
73
74 if (!(e->d_type & DT_REG) || !e->d_name) {
75 continue;
76 }
77
78 int len = strlen(e->d_name);
79 if (len < int(suffixLen + 2) ||
80 e->d_name[len - suffixLen - 1] != '.' ||
81 strcmp(e->d_name + len - suffixLen, PLUGIN_SUFFIX)) {
82 continue;
83 }
84
85 std::string basename = e->d_name;
86 basename = basename.substr(0, basename.length() - suffixLen - 1);
87 std::string fullPath = path[i].c_str();
88 fullPath = fullPath + "/" + e->d_name;
89 void *handle = DLOPEN(fullPath, RTLD_LAZY);
90
91 if (!handle) {
92 std::cerr << "Vamp::PluginLoader: " << e->d_name
93 << ": unable to load library (" << DLERROR()
94 << ")" << std::endl;
95 continue;
96 }
97
98 VampGetPluginDescriptorFunction fn =
99 (VampGetPluginDescriptorFunction)DLSYM
100 (handle, "vampGetPluginDescriptor");
101
102 if (!fn) {
103 DLCLOSE(handle);
104 continue;
105 }
106
107 int index = 0;
108 const VampPluginDescriptor *descriptor = 0;
109
110 while ((descriptor = fn(VAMP_API_VERSION, index))) {
111 PluginKey key = basename + ":" + descriptor->identifier;
112 if (m_pluginLibraryMap.find(key) ==
113 m_pluginLibraryMap.end()) {
114 m_pluginLibraryMap[key] = fullPath;
115 }
116 ++index;
117 }
118
119 DLCLOSE(handle);
120 }
121
122 closedir(d);
123 }
124 }
125
126 std::vector<PluginKey> plugins;
127 for (std::map<PluginKey, std::string>::iterator mi =
128 m_pluginLibraryMap.begin();
129 mi != m_pluginLibraryMap.end(); ++mi) {
130 plugins.push_back(mi->first);
131 }
132
133 return plugins;
134 }
135
136 std::string
137 PluginLoader::getLibraryPath(PluginKey key)
138 {
139 if (m_pluginLibraryMap.empty()) (void)listPlugins();
140 if (m_pluginLibraryMap.find(key) == m_pluginLibraryMap.end()) return "";
141 return m_pluginLibraryMap[key];
142 }
143
144 Plugin *
145 PluginLoader::load(PluginKey key, float inputSampleRate)
146 {
147 std::string fullPath = getLibraryPath(key);
148 if (fullPath == "") return 0;
149
150 std::string::size_type ki = key.find(':');
151 if (ki == std::string::npos) {
152 //!!! flag error
153 return 0;
154 }
155
156 std::string identifier = key.substr(ki + 1);
157
158 void *handle = DLOPEN(fullPath, RTLD_LAZY);
159
160 if (!handle) {
161 std::cerr << "Vamp::PluginLoader: " << fullPath
162 << ": unable to load library (" << DLERROR()
163 << ")" << std::endl;
164 return 0;
165 }
166
167 VampGetPluginDescriptorFunction fn =
168 (VampGetPluginDescriptorFunction)DLSYM
169 (handle, "vampGetPluginDescriptor");
170
171 if (!fn) {
172 //!!! refcount this! --!!! no, POSIX says dlopen/dlclose will
173 // reference count. check on win32
174 DLCLOSE(handle);
175 return 0;
176 }
177
178 int index = 0;
179 const VampPluginDescriptor *descriptor = 0;
180
181 while ((descriptor = fn(VAMP_API_VERSION, index))) {
182 if (std::string(descriptor->identifier) == identifier) {
183 return new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
184 }
185 ++index;
186 }
187
188 //!!! flag error
189 return 0;
190 }
191
192 }
193