comparison plugin/FeatureExtractionPluginFactory.cpp @ 689:573d45e9487b

Merge from debug-output branch
author Chris Cannam
date Tue, 14 Jun 2011 14:47:59 +0100
parents 06f13a3b9e9e
children 1424aa29ae95
comparison
equal deleted inserted replaced
688:be43b2fe68e8 689:573d45e9487b
56 FeatureExtractionPluginFactory * 56 FeatureExtractionPluginFactory *
57 FeatureExtractionPluginFactory::instance(QString pluginType) 57 FeatureExtractionPluginFactory::instance(QString pluginType)
58 { 58 {
59 if (pluginType == "vamp") { 59 if (pluginType == "vamp") {
60 if (!_nativeInstance) { 60 if (!_nativeInstance) {
61 // std::cerr << "FeatureExtractionPluginFactory::instance(" << pluginType.toStdString() 61 // DEBUG << "FeatureExtractionPluginFactory::instance(" << pluginType// << "): creating new FeatureExtractionPluginFactory" << endl;
62 // << "): creating new FeatureExtractionPluginFactory" << std::endl;
63 _nativeInstance = new FeatureExtractionPluginFactory(); 62 _nativeInstance = new FeatureExtractionPluginFactory();
64 } 63 }
65 return _nativeInstance; 64 return _nativeInstance;
66 } 65 }
67 66
94 93
95 factory = instance("vamp"); 94 factory = instance("vamp");
96 if (factory) { 95 if (factory) {
97 std::vector<QString> tmp = factory->getPluginIdentifiers(); 96 std::vector<QString> tmp = factory->getPluginIdentifiers();
98 for (size_t i = 0; i < tmp.size(); ++i) { 97 for (size_t i = 0; i < tmp.size(); ++i) {
99 // std::cerr << "identifier: " << tmp[i].toStdString() << std::endl; 98 // std::cerr << "identifier: " << tmp[i] << std::endl;
100 rv.push_back(tmp[i]); 99 rv.push_back(tmp[i]);
101 } 100 }
102 } 101 }
103 102
104 // Plugins can change the locale, revert it to default. 103 // Plugins can change the locale, revert it to default.
116 std::vector<QString> path = getPluginPath(); 115 std::vector<QString> path = getPluginPath();
117 116
118 for (std::vector<QString>::iterator i = path.begin(); i != path.end(); ++i) { 117 for (std::vector<QString>::iterator i = path.begin(); i != path.end(); ++i) {
119 118
120 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 119 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
121 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << i->toStdString() << std::endl; 120 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: scanning directory " << i-<< endl;
122 #endif 121 #endif
123 122
124 QDir pluginDir(*i, PLUGIN_GLOB, 123 QDir pluginDir(*i, PLUGIN_GLOB,
125 QDir::Name | QDir::IgnoreCase, 124 QDir::Name | QDir::IgnoreCase,
126 QDir::Files | QDir::Readable); 125 QDir::Files | QDir::Readable);
128 for (unsigned int j = 0; j < pluginDir.count(); ++j) { 127 for (unsigned int j = 0; j < pluginDir.count(); ++j) {
129 128
130 QString soname = pluginDir.filePath(pluginDir[j]); 129 QString soname = pluginDir.filePath(pluginDir[j]);
131 130
132 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 131 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
133 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: trying potential library " << soname.toStdString() << std::endl; 132 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: trying potential library " << soname << endl;
134 #endif 133 #endif
135 134
136 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL); 135 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL);
137 136
138 if (!libraryHandle) { 137 if (!libraryHandle) {
139 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl; 138 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to load library " << soname << ": " << DLERROR() << std::endl;
140 continue; 139 continue;
141 } 140 }
142 141
143 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 142 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
144 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: It's a library all right, checking for descriptor" << std::endl; 143 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: It's a library all right, checking for descriptor" << endl;
145 #endif 144 #endif
146 145
147 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction) 146 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
148 DLSYM(libraryHandle, "vampGetPluginDescriptor"); 147 DLSYM(libraryHandle, "vampGetPluginDescriptor");
149 148
150 if (!fn) { 149 if (!fn) {
151 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname.toStdString() << std::endl; 150 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: No descriptor function in " << soname << std::endl;
152 if (DLCLOSE(libraryHandle) != 0) { 151 if (DLCLOSE(libraryHandle) != 0) {
153 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl; 152 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << std::endl;
154 } 153 }
155 continue; 154 continue;
156 } 155 }
157 156
158 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 157 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
159 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Vamp descriptor found" << std::endl; 158 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Vamp descriptor found" << endl;
160 #endif 159 #endif
161 160
162 const VampPluginDescriptor *descriptor = 0; 161 const VampPluginDescriptor *descriptor = 0;
163 int index = 0; 162 int index = 0;
164 163
172 << soname.toStdString() 171 << soname.toStdString()
173 << " returns the same plugin identifier \"" 172 << " returns the same plugin identifier \""
174 << descriptor->identifier << "\" at indices " 173 << descriptor->identifier << "\" at indices "
175 << known[descriptor->identifier] << " and " 174 << known[descriptor->identifier] << " and "
176 << index << std::endl; 175 << index << std::endl;
177 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Avoiding this library (obsolete API?)" << std::endl; 176 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Avoiding this library (obsolete API?)" << endl;
178 ok = false; 177 ok = false;
179 break; 178 break;
180 } else { 179 } else {
181 known[descriptor->identifier] = index; 180 known[descriptor->identifier] = index;
182 } 181 }
192 191
193 QString id = PluginIdentifier::createIdentifier 192 QString id = PluginIdentifier::createIdentifier
194 ("vamp", soname, descriptor->identifier); 193 ("vamp", soname, descriptor->identifier);
195 rv.push_back(id); 194 rv.push_back(id);
196 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 195 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
197 std::cerr << "FeatureExtractionPluginFactory::getPluginIdentifiers: Found plugin id " << id.toStdString() << " at index " << index << std::endl; 196 DEBUG << "FeatureExtractionPluginFactory::getPluginIdentifiers: Found plugin id " << id << " at index " << index << endl;
198 #endif 197 #endif
199 ++index; 198 ++index;
200 } 199 }
201 } 200 }
202 201
203 if (DLCLOSE(libraryHandle) != 0) { 202 if (DLCLOSE(libraryHandle) != 0) {
204 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname.toStdString() << std::endl; 203 std::cerr << "WARNING: FeatureExtractionPluginFactory::getPluginIdentifiers: Failed to unload library " << soname << std::endl;
205 } 204 }
206 } 205 }
207 } 206 }
208 207
209 generateTaxonomy(); 208 generateTaxonomy();
215 FeatureExtractionPluginFactory::findPluginFile(QString soname, QString inDir) 214 FeatureExtractionPluginFactory::findPluginFile(QString soname, QString inDir)
216 { 215 {
217 QString file = ""; 216 QString file = "";
218 217
219 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 218 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
220 std::cerr << "FeatureExtractionPluginFactory::findPluginFile(\"" 219 DEBUG << "FeatureExtractionPluginFactory::findPluginFile(\""
221 << soname.toStdString() << "\", \"" << inDir.toStdString() << "\")" 220 << soname << "\", \"" << inDir << "\")"
222 << std::endl; 221 << endl;
223 #endif 222 #endif
224 223
225 if (inDir != "") { 224 if (inDir != "") {
226 225
227 QDir dir(inDir, PLUGIN_GLOB, 226 QDir dir(inDir, PLUGIN_GLOB,
232 file = dir.filePath(QFileInfo(soname).fileName()); 231 file = dir.filePath(QFileInfo(soname).fileName());
233 232
234 if (QFileInfo(file).exists() && QFileInfo(file).isFile()) { 233 if (QFileInfo(file).exists() && QFileInfo(file).isFile()) {
235 234
236 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 235 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
237 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: " 236 DEBUG << "FeatureExtractionPluginFactory::findPluginFile: "
238 << "found trivially at " << file.toStdString() << std::endl; 237 << "found trivially at " << file << endl;
239 #endif 238 #endif
240 239
241 return file; 240 return file;
242 } 241 }
243 242
244 for (unsigned int j = 0; j < dir.count(); ++j) { 243 for (unsigned int j = 0; j < dir.count(); ++j) {
245 file = dir.filePath(dir[j]); 244 file = dir.filePath(dir[j]);
246 if (QFileInfo(file).baseName() == QFileInfo(soname).baseName()) { 245 if (QFileInfo(file).baseName() == QFileInfo(soname).baseName()) {
247 246
248 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 247 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
249 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: " 248 DEBUG << "FeatureExtractionPluginFactory::findPluginFile: "
250 << "found \"" << soname.toStdString() << "\" at " << file.toStdString() << std::endl; 249 << "found \"" << soname << "\" at " << file << endl;
251 #endif 250 #endif
252 251
253 return file; 252 return file;
254 } 253 }
255 } 254 }
256 255
257 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 256 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
258 std::cerr << "FeatureExtractionPluginFactory::findPluginFile (with dir): " 257 DEBUG << "FeatureExtractionPluginFactory::findPluginFile (with dir): "
259 << "not found" << std::endl; 258 << "not found" << endl;
260 #endif 259 #endif
261 260
262 return ""; 261 return "";
263 262
264 } else { 263 } else {
265 264
266 QFileInfo fi(soname); 265 QFileInfo fi(soname);
267 266
268 if (fi.isAbsolute() && fi.exists() && fi.isFile()) { 267 if (fi.isAbsolute() && fi.exists() && fi.isFile()) {
269 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 268 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
270 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: " 269 DEBUG << "FeatureExtractionPluginFactory::findPluginFile: "
271 << "found trivially at " << soname.toStdString() << std::endl; 270 << "found trivially at " << soname << endl;
272 #endif 271 #endif
273 return soname; 272 return soname;
274 } 273 }
275 274
276 if (fi.isAbsolute() && fi.absolutePath() != "") { 275 if (fi.isAbsolute() && fi.absolutePath() != "") {
286 if (file != "") return file; 285 if (file != "") return file;
287 } 286 }
288 } 287 }
289 288
290 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 289 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
291 std::cerr << "FeatureExtractionPluginFactory::findPluginFile: " 290 DEBUG << "FeatureExtractionPluginFactory::findPluginFile: "
292 << "not found" << std::endl; 291 << "not found" << endl;
293 #endif 292 #endif
294 293
295 return ""; 294 return "";
296 } 295 }
297 } 296 }
309 int index = 0; 308 int index = 0;
310 309
311 QString type, soname, label; 310 QString type, soname, label;
312 PluginIdentifier::parseIdentifier(identifier, type, soname, label); 311 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
313 if (type != "vamp") { 312 if (type != "vamp") {
314 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Wrong factory for plugin type " << type.toStdString() << std::endl; 313 DEBUG << "FeatureExtractionPluginFactory::instantiatePlugin: Wrong factory for plugin type " << type << endl;
315 return 0; 314 return 0;
316 } 315 }
317 316
318 QString found = findPluginFile(soname); 317 QString found = findPluginFile(soname);
319 318
320 if (found == "") { 319 if (found == "") {
321 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find library file " << soname.toStdString() << std::endl; 320 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find library file " << soname << std::endl;
322 return 0; 321 return 0;
323 } else if (found != soname) { 322 } else if (found != soname) {
324 323
325 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 324 #ifdef DEBUG_PLUGIN_SCAN_AND_INSTANTIATE
326 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Given library name was " << soname.toStdString() << ", found at " << found.toStdString() << std::endl; 325 DEBUG << "FeatureExtractionPluginFactory::instantiatePlugin: Given library name was " << soname << ", found at " << found << endl;
327 std::cerr << soname.toStdString() << " -> " << found.toStdString() << std::endl; 326 std::cerr << soname << " -> " << found << std::endl;
328 #endif 327 #endif
329 328
330 } 329 }
331 330
332 soname = found; 331 soname = found;
333 332
334 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL); 333 void *libraryHandle = DLOPEN(soname, RTLD_LAZY | RTLD_LOCAL);
335 334
336 if (!libraryHandle) { 335 if (!libraryHandle) {
337 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to load library " << soname.toStdString() << ": " << DLERROR() << std::endl; 336 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to load library " << soname << ": " << DLERROR() << std::endl;
338 return 0; 337 return 0;
339 } 338 }
340 339
341 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction) 340 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
342 DLSYM(libraryHandle, "vampGetPluginDescriptor"); 341 DLSYM(libraryHandle, "vampGetPluginDescriptor");
343 342
344 if (!fn) { 343 if (!fn) {
345 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: No descriptor function in " << soname.toStdString() << std::endl; 344 DEBUG << "FeatureExtractionPluginFactory::instantiatePlugin: No descriptor function in " << soname << endl;
346 goto done; 345 goto done;
347 } 346 }
348 347
349 while ((descriptor = fn(VAMP_API_VERSION, index))) { 348 while ((descriptor = fn(VAMP_API_VERSION, index))) {
350 if (label == descriptor->identifier) break; 349 if (label == descriptor->identifier) break;
351 ++index; 350 ++index;
352 } 351 }
353 352
354 if (!descriptor) { 353 if (!descriptor) {
355 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label.toStdString() << "\" in library " << soname.toStdString() << std::endl; 354 std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Failed to find plugin \"" << label << "\" in library " << soname << std::endl;
356 goto done; 355 goto done;
357 } 356 }
358 357
359 plugin = new Vamp::PluginHostAdapter(descriptor, inputSampleRate); 358 plugin = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
360 359
361 if (plugin) { 360 if (plugin) {
362 m_handleMap[plugin] = libraryHandle; 361 m_handleMap[plugin] = libraryHandle;
363 rv = new PluginDeletionNotifyAdapter(plugin, this); 362 rv = new PluginDeletionNotifyAdapter(plugin, this);
364 } 363 }
365 364
366 // std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl; 365 // DEBUG << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << endl;
367 366
368 //!!! need to dlclose() when plugins from a given library are unloaded 367 //!!! need to dlclose() when plugins from a given library are unloaded
369 368
370 done: 369 done:
371 if (!rv) { 370 if (!rv) {
372 if (DLCLOSE(libraryHandle) != 0) { 371 if (DLCLOSE(libraryHandle) != 0) {
373 std::cerr << "WARNING: FeatureExtractionPluginFactory::instantiatePlugin: Failed to unload library " << soname.toStdString() << std::endl; 372 std::cerr << "WARNING: FeatureExtractionPluginFactory::instantiatePlugin: Failed to unload library " << soname << std::endl;
374 } 373 }
375 } 374 }
376 375
377 // 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; 376 // DEBUG << "FeatureExtractionPluginFactory::instantiatePlugin: Instantiated plugin " << label << " from library " << soname << ": descriptor " << descriptor << ", rv "<< rv << ", label " << rv->getName() << ", outputs " << rv->getOutputDescriptors().size() << endl;
378 377
379 return rv; 378 return rv;
380 } 379 }
381 380
382 void 381 void
383 FeatureExtractionPluginFactory::pluginDeleted(Vamp::Plugin *plugin) 382 FeatureExtractionPluginFactory::pluginDeleted(Vamp::Plugin *plugin)
384 { 383 {
385 void *handle = m_handleMap[plugin]; 384 void *handle = m_handleMap[plugin];
386 if (handle) { 385 if (handle) {
387 // std::cerr << "unloading library " << handle << " for plugin " << plugin << std::endl; 386 // DEBUG << "unloading library " << handle << " for plugin " << plugin << endl;
388 DLCLOSE(handle); 387 DLCLOSE(handle);
389 } 388 }
390 m_handleMap.erase(plugin); 389 m_handleMap.erase(plugin);
391 } 390 }
392 391
414 413
415 for (size_t i = 0; i < path.size(); ++i) { 414 for (size_t i = 0; i < path.size(); ++i) {
416 415
417 QDir dir(path[i], "*.cat"); 416 QDir dir(path[i], "*.cat");
418 417
419 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i].toStdString() << " has " << dir.count() << " .cat files" << std::endl; 418 // DEBUG << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i] << " has " << dir.count() << " .cat files" << endl;
420 for (unsigned int j = 0; j < dir.count(); ++j) { 419 for (unsigned int j = 0; j < dir.count(); ++j) {
421 420
422 QFile file(path[i] + "/" + dir[j]); 421 QFile file(path[i] + "/" + dir[j]);
423 422
424 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i].toStdString() + "/" + dir[j].toStdString()) << std::endl; 423 // DEBUG << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i]+ "/" + dir[j]) << endl;
425 424
426 if (file.open(QIODevice::ReadOnly)) { 425 if (file.open(QIODevice::ReadOnly)) {
427 // std::cerr << "...opened" << std::endl; 426 // std::cerr << "...opened" << std::endl;
428 QTextStream stream(&file); 427 QTextStream stream(&file);
429 QString line; 428 QString line;
430 429
431 while (!stream.atEnd()) { 430 while (!stream.atEnd()) {
432 line = stream.readLine(); 431 line = stream.readLine();
433 // std::cerr << "line is: \"" << line.toStdString() << "\"" << std::endl; 432 // std::cerr << "line is: \"" << line << "\"" << std::endl;
434 QString id = PluginIdentifier::canonicalise 433 QString id = PluginIdentifier::canonicalise
435 (line.section("::", 0, 0)); 434 (line.section("::", 0, 0));
436 QString cat = line.section("::", 1, 1); 435 QString cat = line.section("::", 1, 1);
437 m_taxonomy[id] = cat; 436 m_taxonomy[id] = cat;
438 // std::cerr << "FeatureExtractionPluginFactory: set id \"" << id.toStdString() << "\" to cat \"" << cat.toStdString() << "\"" << std::endl; 437 // std::cerr << "FeatureExtractionPluginFactory: set id \"" << id << "\" to cat \"" << cat << "\"" << std::endl;
439 } 438 }
440 } 439 }
441 } 440 }
442 } 441 }
443 } 442 }