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@52
|
7 This file copyright 2006 Chris Cannam.
|
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@66
|
22 #include "base/System.h"
|
Chris@66
|
23
|
Chris@66
|
24 #include <QDir>
|
Chris@66
|
25 #include <QFile>
|
Chris@66
|
26 #include <QFileInfo>
|
Chris@66
|
27
|
Chris@0
|
28 #include <iostream>
|
Chris@0
|
29
|
Chris@0
|
30 static FeatureExtractionPluginFactory *_nativeInstance = 0;
|
Chris@0
|
31
|
Chris@0
|
32 FeatureExtractionPluginFactory *
|
Chris@0
|
33 FeatureExtractionPluginFactory::instance(QString pluginType)
|
Chris@0
|
34 {
|
Chris@71
|
35 if (pluginType == "vamp") {
|
Chris@0
|
36 if (!_nativeInstance) {
|
Chris@0
|
37 std::cerr << "FeatureExtractionPluginFactory::instance(" << pluginType.toStdString()
|
Chris@0
|
38 << "): creating new FeatureExtractionPluginFactory" << std::endl;
|
Chris@0
|
39 _nativeInstance = new FeatureExtractionPluginFactory();
|
Chris@0
|
40 }
|
Chris@0
|
41 return _nativeInstance;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 else return 0;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 FeatureExtractionPluginFactory *
|
Chris@0
|
48 FeatureExtractionPluginFactory::instanceFor(QString identifier)
|
Chris@0
|
49 {
|
Chris@0
|
50 QString type, soName, label;
|
Chris@0
|
51 PluginIdentifier::parseIdentifier(identifier, type, soName, label);
|
Chris@0
|
52 return instance(type);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 std::vector<QString>
|
Chris@66
|
56 FeatureExtractionPluginFactory::getPluginPath()
|
Chris@66
|
57 {
|
Chris@66
|
58 std::vector<QString> path;
|
Chris@66
|
59 std::string envPath;
|
Chris@66
|
60
|
Chris@78
|
61 char *cpath = getenv("VAMP_PATH");
|
Chris@66
|
62 if (cpath) envPath = cpath;
|
Chris@66
|
63
|
Chris@66
|
64 if (envPath == "") {
|
Chris@78
|
65 envPath = DEFAULT_VAMP_PATH;
|
Chris@66
|
66 char *chome = getenv("HOME");
|
Chris@66
|
67 if (chome) {
|
Chris@78
|
68 std::string home(chome);
|
Chris@78
|
69 int f;
|
Chris@78
|
70 while ((f = envPath.find("$HOME")) >= 0 && f < envPath.length()) {
|
Chris@78
|
71 envPath.replace(f, 5, home);
|
Chris@78
|
72 }
|
Chris@66
|
73 }
|
Chris@66
|
74 }
|
Chris@66
|
75
|
Chris@78
|
76 std::cerr << "VAMP path is: \"" << envPath << "\"" << std::endl;
|
Chris@78
|
77
|
Chris@66
|
78 std::string::size_type index = 0, newindex = 0;
|
Chris@66
|
79
|
Chris@78
|
80 while ((newindex = envPath.find(PATH_SEPARATOR, index)) < envPath.size()) {
|
Chris@66
|
81 path.push_back(envPath.substr(index, newindex - index).c_str());
|
Chris@66
|
82 index = newindex + 1;
|
Chris@66
|
83 }
|
Chris@66
|
84
|
Chris@66
|
85 path.push_back(envPath.substr(index).c_str());
|
Chris@66
|
86
|
Chris@66
|
87 return path;
|
Chris@66
|
88 }
|
Chris@66
|
89
|
Chris@66
|
90 std::vector<QString>
|
Chris@0
|
91 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
|
Chris@0
|
92 {
|
Chris@0
|
93 FeatureExtractionPluginFactory *factory;
|
Chris@0
|
94 std::vector<QString> rv;
|
Chris@0
|
95
|
Chris@66
|
96 factory = instance("vamp");
|
Chris@0
|
97 if (factory) {
|
Chris@0
|
98 std::vector<QString> tmp = factory->getPluginIdentifiers();
|
Chris@0
|
99 for (size_t i = 0; i < tmp.size(); ++i) {
|
Chris@0
|
100 rv.push_back(tmp[i]);
|
Chris@0
|
101 }
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 // Plugins can change the locale, revert it to default.
|
Chris@0
|
105 setlocale(LC_ALL, "C");
|
Chris@0
|
106 return rv;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 std::vector<QString>
|
Chris@0
|
110 FeatureExtractionPluginFactory::getPluginIdentifiers()
|
Chris@0
|
111 {
|
Chris@0
|
112 std::vector<QString> rv;
|
Chris@66
|
113 std::vector<QString> path = getPluginPath();
|
Chris@66
|
114
|
Chris@66
|
115 for (std::vector<QString>::iterator i = path.begin(); i != path.end(); ++i) {
|
Chris@66
|
116
|
Chris@66
|
117 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << i->toStdString() << std::endl;
|
Chris@66
|
118
|
Chris@66
|
119 QDir pluginDir(*i, PLUGIN_GLOB,
|
Chris@66
|
120 QDir::Name | QDir::IgnoreCase,
|
Chris@66
|
121 QDir::Files | QDir::Readable);
|
Chris@66
|
122
|
Chris@66
|
123 for (unsigned int j = 0; j < pluginDir.count(); ++j) {
|
Chris@66
|
124
|
Chris@66
|
125 QString soname = pluginDir.filePath(pluginDir[j]);
|
Chris@66
|
126
|
Chris@66
|
127 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
|
Chris@66
|
128
|
Chris@66
|
129 if (!libraryHandle) {
|
Chris@71
|
130 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl;
|
Chris@66
|
131 continue;
|
Chris@66
|
132 }
|
Chris@66
|
133
|
Chris@66
|
134 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
|
Chris@66
|
135 DLSYM(libraryHandle, "vampGetPluginDescriptor");
|
Chris@66
|
136
|
Chris@66
|
137 if (!fn) {
|
Chris@66
|
138 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname.toStdString() << std::endl;
|
Chris@66
|
139 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
140 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
141 }
|
Chris@66
|
142 continue;
|
Chris@66
|
143 }
|
Chris@66
|
144
|
Chris@66
|
145 const VampPluginDescriptor *descriptor = 0;
|
Chris@66
|
146 int index = 0;
|
Chris@66
|
147
|
Chris@66
|
148 while ((descriptor = fn(index))) {
|
Chris@82
|
149 QString id = PluginIdentifier::createIdentifier
|
Chris@82
|
150 ("vamp", soname, descriptor->name);
|
Chris@66
|
151 rv.push_back(id);
|
Chris@66
|
152 std::cerr << "Found id " << id.toStdString() << std::endl;
|
Chris@66
|
153 ++index;
|
Chris@66
|
154 }
|
Chris@66
|
155
|
Chris@66
|
156 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
157 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
158 }
|
Chris@66
|
159 }
|
Chris@66
|
160 }
|
Chris@66
|
161
|
Chris@0
|
162 return rv;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@66
|
165 QString
|
Chris@66
|
166 FeatureExtractionPluginFactory::findPluginFile(QString soname, QString inDir)
|
Chris@66
|
167 {
|
Chris@66
|
168 QString file = "";
|
Chris@66
|
169
|
Chris@66
|
170 if (inDir != "") {
|
Chris@66
|
171
|
Chris@66
|
172 QDir dir(inDir, PLUGIN_GLOB,
|
Chris@66
|
173 QDir::Name | QDir::IgnoreCase,
|
Chris@66
|
174 QDir::Files | QDir::Readable);
|
Chris@66
|
175 if (!dir.exists()) return "";
|
Chris@66
|
176
|
Chris@66
|
177 file = dir.filePath(QFileInfo(soname).fileName());
|
Chris@66
|
178 if (QFileInfo(file).exists()) {
|
Chris@66
|
179 return file;
|
Chris@66
|
180 }
|
Chris@66
|
181
|
Chris@66
|
182 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@66
|
183 file = dir.filePath(dir[j]);
|
Chris@66
|
184 if (QFileInfo(file).baseName() == QFileInfo(soname).baseName()) {
|
Chris@66
|
185 return file;
|
Chris@66
|
186 }
|
Chris@66
|
187 }
|
Chris@66
|
188
|
Chris@66
|
189 return "";
|
Chris@66
|
190
|
Chris@66
|
191 } else {
|
Chris@66
|
192
|
Chris@66
|
193 QFileInfo fi(soname);
|
Chris@66
|
194 if (fi.exists()) return soname;
|
Chris@66
|
195
|
Chris@66
|
196 if (fi.isAbsolute() && fi.absolutePath() != "") {
|
Chris@66
|
197 file = findPluginFile(soname, fi.absolutePath());
|
Chris@66
|
198 if (file != "") return file;
|
Chris@66
|
199 }
|
Chris@66
|
200
|
Chris@66
|
201 std::vector<QString> path = getPluginPath();
|
Chris@66
|
202 for (std::vector<QString>::iterator i = path.begin();
|
Chris@66
|
203 i != path.end(); ++i) {
|
Chris@66
|
204 if (*i != "") {
|
Chris@66
|
205 file = findPluginFile(soname, *i);
|
Chris@66
|
206 if (file != "") return file;
|
Chris@66
|
207 }
|
Chris@66
|
208 }
|
Chris@66
|
209
|
Chris@66
|
210 return "";
|
Chris@66
|
211 }
|
Chris@66
|
212 }
|
Chris@66
|
213
|
Chris@66
|
214 Vamp::Plugin *
|
Chris@0
|
215 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
|
Chris@0
|
216 float inputSampleRate)
|
Chris@0
|
217 {
|
Chris@66
|
218 Vamp::Plugin *rv = 0;
|
Chris@66
|
219
|
Chris@66
|
220 const VampPluginDescriptor *descriptor = 0;
|
Chris@66
|
221 int index = 0;
|
Chris@66
|
222
|
Chris@66
|
223 QString type, soname, label;
|
Chris@66
|
224 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
|
Chris@71
|
225 if (type != "vamp") {
|
Chris@0
|
226 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Wrong factory for plugin type " << type.toStdString() << std::endl;
|
Chris@0
|
227 return 0;
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@66
|
230 QString found = findPluginFile(soname);
|
Chris@66
|
231
|
Chris@66
|
232 if (found == "") {
|
Chris@66
|
233 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find library file " << soname.toStdString() << std::endl;
|
Chris@66
|
234 } else if (found != soname) {
|
Chris@66
|
235 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: WARNING: Given library name was " << soname.toStdString() << ", found at " << found.toStdString() << std::endl;
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@66
|
238 soname = found;
|
Chris@66
|
239
|
Chris@66
|
240 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
|
Chris@66
|
241
|
Chris@66
|
242 if (!libraryHandle) {
|
Chris@71
|
243 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl;
|
Chris@66
|
244 return 0;
|
Chris@19
|
245 }
|
Chris@19
|
246
|
Chris@66
|
247 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
|
Chris@66
|
248 DLSYM(libraryHandle, "vampGetPluginDescriptor");
|
Chris@66
|
249
|
Chris@66
|
250 if (!fn) {
|
Chris@66
|
251 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: No descriptor function in " << soname.toStdString() << std::endl;
|
Chris@66
|
252 goto done;
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@66
|
255 while ((descriptor = fn(index))) {
|
Chris@66
|
256 if (label == descriptor->name) break;
|
Chris@66
|
257 ++index;
|
Chris@47
|
258 }
|
Chris@47
|
259
|
Chris@66
|
260 if (!descriptor) {
|
Chris@66
|
261 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label.toStdString() << "\" in library " << soname.toStdString() << std::endl;
|
Chris@66
|
262 goto done;
|
Martin@37
|
263 }
|
Martin@37
|
264
|
Chris@66
|
265 rv = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
|
Chris@66
|
266
|
Chris@79
|
267 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl;
|
Chris@79
|
268
|
Chris@66
|
269 //!!! need to dlclose() when plugins from a given library are unloaded
|
Chris@66
|
270
|
Chris@66
|
271 done:
|
Chris@66
|
272 if (!rv) {
|
Chris@66
|
273 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@66
|
274 std::cerr << "WARNING: FeatureExtractionPluginFactory::instantiatePlugin: Failed to unload library " << soname.toStdString() << std::endl;
|
Chris@66
|
275 }
|
Chris@66
|
276 }
|
Chris@73
|
277
|
Chris@73
|
278 // 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
|
279
|
Chris@66
|
280 return rv;
|
Chris@0
|
281 }
|
Chris@0
|
282
|