Chris@49
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@52
|
4 Sonic Visualiser
|
Chris@52
|
5 An audio file viewer and annotation editor.
|
Chris@52
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@202
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@0
|
8
|
Chris@52
|
9 This program is free software; you can redistribute it and/or
|
Chris@52
|
10 modify it under the terms of the GNU General Public License as
|
Chris@52
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
12 License, or (at your option) any later version. See the file
|
Chris@52
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "FeatureExtractionPluginFactory.h"
|
Chris@0
|
17 #include "PluginIdentifier.h"
|
Chris@0
|
18
|
Chris@66
|
19 #include "vamp/vamp.h"
|
Chris@66
|
20 #include "vamp-sdk/PluginHostAdapter.h"
|
Chris@66
|
21
|
Chris@150
|
22 #include "system/System.h"
|
Chris@66
|
23
|
Chris@66
|
24 #include <QDir>
|
Chris@66
|
25 #include <QFile>
|
Chris@66
|
26 #include <QFileInfo>
|
Chris@165
|
27 #include <QTextStream>
|
Chris@66
|
28
|
Chris@0
|
29 #include <iostream>
|
Chris@0
|
30
|
Chris@249
|
31 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
|
Chris@249
|
32
|
Chris@0
|
33 static FeatureExtractionPluginFactory *_nativeInstance = 0;
|
Chris@0
|
34
|
Chris@0
|
35 FeatureExtractionPluginFactory *
|
Chris@0
|
36 FeatureExtractionPluginFactory::instance(QString pluginType)
|
Chris@0
|
37 {
|
Chris@71
|
38 if (pluginType == "vamp") {
|
Chris@0
|
39 if (!_nativeInstance) {
|
Chris@0
|
40 std::cerr << "FeatureExtractionPluginFactory::instance(" << pluginType.toStdString()
|
Chris@0
|
41 << "): creating new FeatureExtractionPluginFactory" << std::endl;
|
Chris@0
|
42 _nativeInstance = new FeatureExtractionPluginFactory();
|
Chris@0
|
43 }
|
Chris@0
|
44 return _nativeInstance;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 else return 0;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 FeatureExtractionPluginFactory *
|
Chris@0
|
51 FeatureExtractionPluginFactory::instanceFor(QString identifier)
|
Chris@0
|
52 {
|
Chris@0
|
53 QString type, soName, label;
|
Chris@0
|
54 PluginIdentifier::parseIdentifier(identifier, type, soName, label);
|
Chris@0
|
55 return instance(type);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 std::vector<QString>
|
Chris@66
|
59 FeatureExtractionPluginFactory::getPluginPath()
|
Chris@66
|
60 {
|
Chris@117
|
61 if (!m_pluginPath.empty()) return m_pluginPath;
|
Chris@117
|
62
|
Chris@186
|
63 std::vector<std::string> p = Vamp::PluginHostAdapter::getPluginPath();
|
Chris@186
|
64 for (size_t i = 0; i < p.size(); ++i) m_pluginPath.push_back(p[i].c_str());
|
Chris@186
|
65 return m_pluginPath;
|
Chris@66
|
66 }
|
Chris@66
|
67
|
Chris@66
|
68 std::vector<QString>
|
Chris@0
|
69 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
|
Chris@0
|
70 {
|
Chris@0
|
71 FeatureExtractionPluginFactory *factory;
|
Chris@0
|
72 std::vector<QString> rv;
|
Chris@0
|
73
|
Chris@66
|
74 factory = instance("vamp");
|
Chris@0
|
75 if (factory) {
|
Chris@0
|
76 std::vector<QString> tmp = factory->getPluginIdentifiers();
|
Chris@0
|
77 for (size_t i = 0; i < tmp.size(); ++i) {
|
Chris@0
|
78 rv.push_back(tmp[i]);
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 // Plugins can change the locale, revert it to default.
|
Chris@0
|
83 setlocale(LC_ALL, "C");
|
Chris@0
|
84 return rv;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 std::vector<QString>
|
Chris@0
|
88 FeatureExtractionPluginFactory::getPluginIdentifiers()
|
Chris@0
|
89 {
|
Chris@0
|
90 std::vector<QString> rv;
|
Chris@66
|
91 std::vector<QString> path = getPluginPath();
|
Chris@66
|
92
|
Chris@66
|
93 for (std::vector<QString>::iterator i = path.begin(); i != path.end(); ++i) {
|
Chris@66
|
94
|
Chris@249
|
95 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
96 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << i->toStdString() << std::endl;
|
Chris@249
|
97 #endif
|
Chris@66
|
98
|
Chris@66
|
99 QDir pluginDir(*i, PLUGIN_GLOB,
|
Chris@66
|
100 QDir::Name | QDir::IgnoreCase,
|
Chris@66
|
101 QDir::Files | QDir::Readable);
|
Chris@66
|
102
|
Chris@66
|
103 for (unsigned int j = 0; j < pluginDir.count(); ++j) {
|
Chris@66
|
104
|
Chris@66
|
105 QString soname = pluginDir.filePath(pluginDir[j]);
|
Chris@66
|
106
|
Chris@249
|
107 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
108 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: trying potential library " << soname.toStdString() << std::endl;
|
Chris@249
|
109 #endif
|
Chris@249
|
110
|
Chris@66
|
111 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
|
Chris@66
|
112
|
Chris@66
|
113 if (!libraryHandle) {
|
Chris@71
|
114 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl;
|
Chris@66
|
115 continue;
|
Chris@66
|
116 }
|
Chris@66
|
117
|
Chris@249
|
118 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
119 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: It's a library all right, checking for descriptor" << std::endl;
|
Chris@249
|
120 #endif
|
Chris@249
|
121
|
Chris@66
|
122 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
|
Chris@66
|
123 DLSYM(libraryHandle, "vampGetPluginDescriptor");
|
Chris@66
|
124
|
Chris@66
|
125 if (!fn) {
|
Chris@66
|
126 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname.toStdString() << std::endl;
|
Chris@66
|
127 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
128 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
129 }
|
Chris@66
|
130 continue;
|
Chris@66
|
131 }
|
Chris@66
|
132
|
Chris@249
|
133 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
134 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Vamp descriptor found" << std::endl;
|
Chris@249
|
135 #endif
|
Chris@249
|
136
|
Chris@66
|
137 const VampPluginDescriptor *descriptor = 0;
|
Chris@66
|
138 int index = 0;
|
Chris@66
|
139
|
Chris@239
|
140 while ((descriptor = fn(VAMP_API_VERSION, index))) {
|
Chris@82
|
141 QString id = PluginIdentifier::createIdentifier
|
Chris@238
|
142 ("vamp", soname, descriptor->identifier);
|
Chris@66
|
143 rv.push_back(id);
|
Chris@249
|
144 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
145 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Found plugin id " << id.toStdString() << std::endl;
|
Chris@249
|
146 #endif
|
Chris@66
|
147 ++index;
|
Chris@66
|
148 }
|
Chris@66
|
149
|
Chris@66
|
150 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
151 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
152 }
|
Chris@66
|
153 }
|
Chris@66
|
154 }
|
Chris@66
|
155
|
Chris@165
|
156 generateTaxonomy();
|
Chris@165
|
157
|
Chris@0
|
158 return rv;
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@66
|
161 QString
|
Chris@66
|
162 FeatureExtractionPluginFactory::findPluginFile(QString soname, QString inDir)
|
Chris@66
|
163 {
|
Chris@66
|
164 QString file = "";
|
Chris@66
|
165
|
Chris@249
|
166 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
167 std::cerr << "FeatureExtractionPluginFactory::findPluginFile(\""
|
Chris@249
|
168 << soname.toStdString() << "\", \"" << inDir.toStdString() << "\")"
|
Chris@249
|
169 << std::endl;
|
Chris@249
|
170 #endif
|
Chris@249
|
171
|
Chris@66
|
172 if (inDir != "") {
|
Chris@66
|
173
|
Chris@66
|
174 QDir dir(inDir, PLUGIN_GLOB,
|
Chris@66
|
175 QDir::Name | QDir::IgnoreCase,
|
Chris@66
|
176 QDir::Files | QDir::Readable);
|
Chris@66
|
177 if (!dir.exists()) return "";
|
Chris@66
|
178
|
Chris@66
|
179 file = dir.filePath(QFileInfo(soname).fileName());
|
Chris@249
|
180
|
Chris@249
|
181 if (QFileInfo(file).exists() && QFileInfo(file).isFile()) {
|
Chris@249
|
182
|
Chris@249
|
183 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
184 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: "
|
Chris@249
|
185 << "found trivially at " << file.toStdString() << std::endl;
|
Chris@249
|
186 #endif
|
Chris@249
|
187
|
Chris@66
|
188 return file;
|
Chris@66
|
189 }
|
Chris@66
|
190
|
Chris@66
|
191 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@66
|
192 file = dir.filePath(dir[j]);
|
Chris@66
|
193 if (QFileInfo(file).baseName() == QFileInfo(soname).baseName()) {
|
Chris@249
|
194
|
Chris@249
|
195 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
196 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: "
|
Chris@249
|
197 << "found at " << file.toStdString() << std::endl;
|
Chris@249
|
198 #endif
|
Chris@249
|
199
|
Chris@66
|
200 return file;
|
Chris@66
|
201 }
|
Chris@66
|
202 }
|
Chris@66
|
203
|
Chris@249
|
204 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
205 std::cerr << "FeatureExtractionPluginFactory::findPluginFile (with dir): "
|
Chris@249
|
206 << "not found" << std::endl;
|
Chris@249
|
207 #endif
|
Chris@249
|
208
|
Chris@66
|
209 return "";
|
Chris@66
|
210
|
Chris@66
|
211 } else {
|
Chris@66
|
212
|
Chris@66
|
213 QFileInfo fi(soname);
|
Chris@249
|
214
|
Chris@249
|
215 if (fi.isAbsolute() && fi.exists() && fi.isFile()) {
|
Chris@249
|
216 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
217 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: "
|
Chris@249
|
218 << "found trivially at " << soname.toStdString() << std::endl;
|
Chris@249
|
219 #endif
|
Chris@249
|
220 return soname;
|
Chris@249
|
221 }
|
Chris@66
|
222
|
Chris@66
|
223 if (fi.isAbsolute() && fi.absolutePath() != "") {
|
Chris@66
|
224 file = findPluginFile(soname, fi.absolutePath());
|
Chris@66
|
225 if (file != "") return file;
|
Chris@66
|
226 }
|
Chris@66
|
227
|
Chris@66
|
228 std::vector<QString> path = getPluginPath();
|
Chris@66
|
229 for (std::vector<QString>::iterator i = path.begin();
|
Chris@66
|
230 i != path.end(); ++i) {
|
Chris@66
|
231 if (*i != "") {
|
Chris@66
|
232 file = findPluginFile(soname, *i);
|
Chris@66
|
233 if (file != "") return file;
|
Chris@66
|
234 }
|
Chris@66
|
235 }
|
Chris@66
|
236
|
Chris@249
|
237 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
238 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: "
|
Chris@249
|
239 << "not found" << std::endl;
|
Chris@249
|
240 #endif
|
Chris@249
|
241
|
Chris@66
|
242 return "";
|
Chris@66
|
243 }
|
Chris@66
|
244 }
|
Chris@66
|
245
|
Chris@66
|
246 Vamp::Plugin *
|
Chris@0
|
247 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
|
Chris@0
|
248 float inputSampleRate)
|
Chris@0
|
249 {
|
Chris@66
|
250 Vamp::Plugin *rv = 0;
|
Chris@66
|
251
|
Chris@66
|
252 const VampPluginDescriptor *descriptor = 0;
|
Chris@66
|
253 int index = 0;
|
Chris@66
|
254
|
Chris@66
|
255 QString type, soname, label;
|
Chris@66
|
256 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
|
Chris@71
|
257 if (type != "vamp") {
|
Chris@0
|
258 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Wrong factory for plugin type " << type.toStdString() << std::endl;
|
Chris@0
|
259 return 0;
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@66
|
262 QString found = findPluginFile(soname);
|
Chris@66
|
263
|
Chris@66
|
264 if (found == "") {
|
Chris@66
|
265 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find library file " << soname.toStdString() << std::endl;
|
Chris@117
|
266 return 0;
|
Chris@66
|
267 } else if (found != soname) {
|
Chris@249
|
268
|
Chris@249
|
269 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
|
Chris@249
|
270 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Given library name was " << soname.toStdString() << ", found at " << found.toStdString() << std::endl;
|
Chris@249
|
271 std::cerr << soname.toStdString() << " -> " << found.toStdString() << std::endl;
|
Chris@249
|
272 #endif
|
Chris@249
|
273
|
Chris@249
|
274 }
|
Chris@0
|
275
|
Chris@66
|
276 soname = found;
|
Chris@66
|
277
|
Chris@66
|
278 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
|
Chris@66
|
279
|
Chris@66
|
280 if (!libraryHandle) {
|
Chris@71
|
281 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl;
|
Chris@66
|
282 return 0;
|
Chris@19
|
283 }
|
Chris@19
|
284
|
Chris@66
|
285 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
|
Chris@66
|
286 DLSYM(libraryHandle, "vampGetPluginDescriptor");
|
Chris@66
|
287
|
Chris@66
|
288 if (!fn) {
|
Chris@66
|
289 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: No descriptor function in " << soname.toStdString() << std::endl;
|
Chris@66
|
290 goto done;
|
Chris@0
|
291 }
|
Chris@0
|
292
|
Chris@239
|
293 while ((descriptor = fn(VAMP_API_VERSION, index))) {
|
Chris@238
|
294 if (label == descriptor->identifier) break;
|
Chris@66
|
295 ++index;
|
Chris@47
|
296 }
|
Chris@47
|
297
|
Chris@66
|
298 if (!descriptor) {
|
Chris@66
|
299 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label.toStdString() << "\" in library " << soname.toStdString() << std::endl;
|
Chris@66
|
300 goto done;
|
Martin@37
|
301 }
|
Martin@37
|
302
|
Chris@66
|
303 rv = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
|
Chris@66
|
304
|
Chris@117
|
305 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl;
|
Chris@79
|
306
|
Chris@66
|
307 //!!! need to dlclose() when plugins from a given library are unloaded
|
Chris@66
|
308
|
Chris@66
|
309 done:
|
Chris@66
|
310 if (!rv) {
|
Chris@66
|
311 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
312 std::cerr << "WARNING: FeatureExtractionPluginFactory::instantiatePlugin: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
313 }
|
Chris@66
|
314 }
|
Chris@73
|
315
|
Chris@73
|
316 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Instantiated plugin " << label.toStdString() << " from library " << soname.toStdString() << ": descriptor " << descriptor << ", rv "<< rv << ", label " << rv->getName() << ", outputs " << rv->getOutputDescriptors().size() << std::endl;
|
Chris@73
|
317
|
Chris@66
|
318 return rv;
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@165
|
321 QString
|
Chris@165
|
322 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
|
Chris@165
|
323 {
|
Chris@165
|
324 return m_taxonomy[identifier];
|
Chris@165
|
325 }
|
Chris@165
|
326
|
Chris@165
|
327 void
|
Chris@165
|
328 FeatureExtractionPluginFactory::generateTaxonomy()
|
Chris@165
|
329 {
|
Chris@165
|
330 std::vector<QString> pluginPath = getPluginPath();
|
Chris@165
|
331 std::vector<QString> path;
|
Chris@165
|
332
|
Chris@165
|
333 for (size_t i = 0; i < pluginPath.size(); ++i) {
|
Chris@165
|
334 if (pluginPath[i].contains("/lib/")) {
|
Chris@165
|
335 QString p(pluginPath[i]);
|
Chris@165
|
336 path.push_back(p);
|
Chris@165
|
337 p.replace("/lib/", "/share/");
|
Chris@165
|
338 path.push_back(p);
|
Chris@165
|
339 }
|
Chris@165
|
340 path.push_back(pluginPath[i]);
|
Chris@165
|
341 }
|
Chris@165
|
342
|
Chris@165
|
343 for (size_t i = 0; i < path.size(); ++i) {
|
Chris@165
|
344
|
Chris@165
|
345 QDir dir(path[i], "*.cat");
|
Chris@165
|
346
|
Chris@165
|
347 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i].toStdString() << " has " << dir.count() << " .cat files" << std::endl;
|
Chris@165
|
348 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@165
|
349
|
Chris@165
|
350 QFile file(path[i] + "/" + dir[j]);
|
Chris@165
|
351
|
Chris@165
|
352 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i].toStdString() + "/" + dir[j].toStdString()) << std::endl;
|
Chris@165
|
353
|
Chris@165
|
354 if (file.open(QIODevice::ReadOnly)) {
|
Chris@165
|
355 // std::cerr << "...opened" << std::endl;
|
Chris@165
|
356 QTextStream stream(&file);
|
Chris@165
|
357 QString line;
|
Chris@165
|
358
|
Chris@165
|
359 while (!stream.atEnd()) {
|
Chris@165
|
360 line = stream.readLine();
|
Chris@165
|
361 // std::cerr << "line is: \"" << line.toStdString() << "\"" << std::endl;
|
Chris@165
|
362 QString id = PluginIdentifier::canonicalise
|
Chris@165
|
363 (line.section("::", 0, 0));
|
Chris@165
|
364 QString cat = line.section("::", 1, 1);
|
Chris@165
|
365 m_taxonomy[id] = cat;
|
Chris@165
|
366 // std::cerr << "FeatureExtractionPluginFactory: set id \"" << id.toStdString() << "\" to cat \"" << cat.toStdString() << "\"" << std::endl;
|
Chris@165
|
367 }
|
Chris@165
|
368 }
|
Chris@165
|
369 }
|
Chris@165
|
370 }
|
Chris@165
|
371 }
|