annotate vampy-main.cpp @ 117:9333516e6656

Added tag vampy-2.3 for changeset c85d26cb9dab
author Chris Cannam
date Wed, 27 Feb 2019 17:00:33 +0000
parents c85d26cb9dab
children a38d318c85a9
rev   line source
Chris@66 1 /* -*- c-basic-offset: 8 indent-tabs-mode: t -*- */
fazekasgy@37 2 /*
fazekasgy@37 3
fazekasgy@37 4 * Vampy : This plugin is a wrapper around the Vamp plugin API.
fazekasgy@37 5 * It allows for writing Vamp plugins in Python.
fazekasgy@37 6
fazekasgy@37 7 * Centre for Digital Music, Queen Mary University of London.
fazekasgy@37 8 * Copyright (C) 2008-2009 Gyorgy Fazekas, QMUL. (See Vamp sources
fazekasgy@37 9 * for licence information.)
fazekasgy@37 10
fazekasgy@37 11 */
fazekasgy@37 12
fazekasgy@37 13 #include <Python.h>
fazekasgy@37 14
fazekasgy@37 15 #ifdef HAVE_NUMPY
fazekasgy@51 16
fazekasgy@51 17 // define a unique API pointer
Chris@66 18 #define PY_ARRAY_UNIQUE_SYMBOL VAMPY_ARRAY_API
Chris@66 19 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
fazekasgy@37 20 #include "numpy/arrayobject.h"
fazekasgy@51 21
fazekasgy@51 22 // prevent building with very old versions of numpy
fazekasgy@51 23 #ifndef NPY_VERSION
fazekasgy@51 24 #undef HAVE_NUMPY
fazekasgy@51 25 #endif
fazekasgy@51 26
fazekasgy@51 27 #endif
fazekasgy@51 28
fazekasgy@51 29 // this is not part of the API, but we will require it for a bug workaround
fazekasgy@51 30 // define this symbol if you use another version of numpy in the makefile
fazekasgy@51 31 // Vampy will not attempt to load a lower version than specified
fazekasgy@51 32 #ifdef HAVE_NUMPY
Chris@81 33 #ifndef NUMPY_MAJORVERSION
Chris@81 34 #define NUMPY_MAJORVERSION 1
fazekasgy@51 35 #endif
Chris@81 36 #ifndef NUMPY_MINORVERSION
Chris@81 37 #define NUMPY_MINORVERSION 9
Chris@81 38 #endif
Chris@81 39
fazekasgy@37 40 #endif
fazekasgy@37 41
fazekasgy@37 42 #include "vamp/vamp.h"
fazekasgy@37 43 #include "vamp-sdk/PluginAdapter.h"
fazekasgy@37 44 #include "PyPlugScanner.h"
fazekasgy@37 45 #include "PyPlugin.h"
fazekasgy@37 46 #include "PyExtensionModule.h"
fazekasgy@37 47 #include "PyExtensionManager.h"
Chris@67 48 #include "Debug.h"
gyorgyf@63 49 #include <sstream>
fazekasgy@37 50
fazekasgy@37 51 #ifdef _WIN32
fazekasgy@37 52 #define pathsep ('\\')
fazekasgy@37 53 #include <windows.h>
fazekasgy@37 54 #include <tchar.h>
fazekasgy@37 55 #else
fazekasgy@37 56 #define pathsep ('/')
fazekasgy@37 57 #include <dirent.h>
fazekasgy@37 58 #include <dlfcn.h>
fazekasgy@37 59 #endif
fazekasgy@37 60
Chris@116 61 #include "version.h"
Chris@116 62
fazekasgy@37 63 using std::cerr;
fazekasgy@37 64 using std::endl;
fazekasgy@37 65 using std::string;
fazekasgy@37 66 using std::vector;
fazekasgy@37 67
fazekasgy@37 68 static int adinstcount;
fazekasgy@37 69 static int totinstcount;
fazekasgy@51 70 static bool numpyInstalled = false;
fazekasgy@51 71 static bool arrayApiInitialised = false;
fazekasgy@37 72
fazekasgy@37 73 class PyPluginAdapter : public Vamp::PluginAdapterBase
fazekasgy@37 74 {
fazekasgy@37 75 public:
fazekasgy@37 76 PyPluginAdapter(std::string pyPlugId, PyObject* pyClass) :
fazekasgy@37 77 PluginAdapterBase(),
fazekasgy@37 78 m_plug(pyPlugId),
fazekasgy@37 79 m_pyClass(pyClass),
fazekasgy@37 80 m_failed(false)
fazekasgy@37 81 {
Chris@67 82 DSTREAM << "PyPluginAdapter:ctor:"<< adinstcount << ": " << m_plug << endl;
fazekasgy@37 83 adinstcount++;
fazekasgy@37 84 }
fazekasgy@37 85
fazekasgy@37 86 ~PyPluginAdapter()
fazekasgy@37 87 {
fazekasgy@37 88 }
fazekasgy@37 89
fazekasgy@37 90 bool failed() { return m_failed; }
fazekasgy@37 91 std::string getPlugKey() { return m_plug; }
fazekasgy@37 92
fazekasgy@37 93 protected:
fazekasgy@37 94 Vamp::Plugin *createPlugin(float inputSampleRate)
fazekasgy@37 95 {
fazekasgy@37 96 try {
fazekasgy@51 97 PyPlugin *plugin = new PyPlugin(m_plug, inputSampleRate, m_pyClass, totinstcount, numpyInstalled);
fazekasgy@37 98 return plugin;
fazekasgy@37 99 } catch (...) {
Chris@67 100 cerr << "ERROR: PyPluginAdapter::createPlugin: Failed to construct PyPlugin" << endl;
Chris@67 101 // any plugin with syntax errors will fail to construct
Chris@67 102 m_failed = true;
fazekasgy@37 103 return 0;
fazekasgy@37 104 }
fazekasgy@37 105 }
fazekasgy@37 106
fazekasgy@37 107 std::string m_plug;
fazekasgy@37 108 PyObject *m_pyClass;
fazekasgy@37 109 bool m_failed;
fazekasgy@37 110 };
fazekasgy@37 111
fazekasgy@51 112
fazekasgy@37 113 static void array_API_initialiser()
fazekasgy@37 114 {
fazekasgy@51 115 if (arrayApiInitialised) return;
fazekasgy@51 116
fazekasgy@51 117 /* Numpy 1.3 build note: there seems to be a bug
fazekasgy@51 118 in this version (at least on OS/X) which will cause memory
fazekasgy@51 119 access error in the array API import function if an earlier runtime
fazekasgy@51 120 version of Numpy is used when loading the library.
fazekasgy@51 121 (below is a horrible workaround)
fazekasgy@51 122 */
fazekasgy@51 123
fazekasgy@37 124 #ifdef HAVE_NUMPY
fazekasgy@51 125
Chris@81 126 string ver, majorver, minorver;
gyorgyf@63 127 std::istringstream verStream;
Chris@81 128 int numpyVersionMajor, numpyVersionMinor;
fazekasgy@51 129
fazekasgy@51 130 /// attmept to test numpy version before importing the array API
Chris@67 131 DSTREAM << "Numpy build information: ABI level: " << NPY_VERSION
Chris@81 132 << " Numpy version: " << NUMPY_MAJORVERSION << "." << NUMPY_MINORVERSION << endl;
fazekasgy@51 133
fazekasgy@51 134 PyObject *pyModule, *pyDict, *pyVer;
fazekasgy@51 135
fazekasgy@51 136 pyModule = PyImport_ImportModule("numpy"); //numpy.core.multiarray
fazekasgy@51 137 if (!pyModule) {
Chris@67 138 cerr << "ERROR: Vampy was compiled with Numpy support but Numpy does not seem to be installed." << endl;
fazekasgy@51 139 #ifdef __APPLE__
fazekasgy@51 140 cerr << "Hint: Check if Numpy is installed for the particular setup of Python used by Vampy (given by Python exec prefix)." << endl;
fazekasgy@51 141 #endif
fazekasgy@51 142 goto numpyFailure;
fazekasgy@51 143 }
fazekasgy@51 144
fazekasgy@51 145 pyDict = PyModule_GetDict(pyModule); // borrowed ref
fazekasgy@51 146 if (!pyDict) {
Chris@67 147 cerr << "ERROR: Can not access Numpy module dictionary." << endl;
fazekasgy@51 148 goto numpyFailure;
fazekasgy@51 149 }
fazekasgy@51 150
fazekasgy@51 151 pyVer = PyDict_GetItemString(pyDict,"__version__"); //borrowed ref
fazekasgy@51 152 if (!pyVer) {
Chris@67 153 cerr << "ERROR: Can not access Numpy version information." << endl;
fazekasgy@51 154 goto numpyFailure;
fazekasgy@51 155 }
fazekasgy@51 156
fazekasgy@51 157 ver = PyString_AsString(pyVer);
fazekasgy@51 158 ver = ver.substr(0,ver.rfind("."));
Chris@81 159 majorver = ver.substr(0,ver.rfind("."));
Chris@81 160 minorver = ver.substr(ver.rfind(".")+1);
Chris@81 161
Chris@81 162 // parse version string to float
Chris@81 163 verStream.str(majorver);
Chris@81 164 verStream >> numpyVersionMajor;
Chris@81 165 verStream.str(minorver);
Chris@81 166 verStream >> numpyVersionMinor;
Chris@67 167
Chris@81 168 DSTREAM << "Numpy runtime version: " << numpyVersionMajor << "." << numpyVersionMinor << endl;
Chris@81 169
Chris@81 170 if(numpyVersionMajor < NUMPY_MAJORVERSION ||
Chris@81 171 (numpyVersionMajor < NUMPY_MAJORVERSION &&
Chris@81 172 numpyVersionMinor < NUMPY_MINORVERSION)) {
Chris@81 173 cerr << "ERROR: Incompatible Numpy version found: " << ver << endl;
fazekasgy@51 174 goto numpyFailure;
fazekasgy@51 175 }
fazekasgy@51 176
fazekasgy@51 177 Py_DECREF(pyModule);
fazekasgy@51 178
fazekasgy@51 179 // At least we catch import errors, but if binary compatibility
fazekasgy@51 180 // has changed without notice, this would still fail.
fazekasgy@51 181 // However, we should never get to this point now anyway.
fazekasgy@37 182 import_array();
fazekasgy@51 183 if (PyErr_Occurred()) {
Chris@67 184 cerr << "ERROR: Import error while loading the Numpy Array API." << endl;
fazekasgy@51 185 PyErr_Print(); PyErr_Clear();
fazekasgy@51 186 goto numpyFailure;
fazekasgy@51 187 }
fazekasgy@51 188 else {
fazekasgy@51 189 numpyInstalled = true;
fazekasgy@51 190 arrayApiInitialised = true;
fazekasgy@51 191 return;
fazekasgy@51 192 }
fazekasgy@51 193
fazekasgy@51 194
fazekasgy@51 195 numpyFailure:
Chris@81 196 cerr << "Please make sure you have Numpy " << NUMPY_MAJORVERSION << "." << NUMPY_MINORVERSION << " or greater installed." << endl;
fazekasgy@51 197 cerr << "Vampy: Numpy support disabled." << endl;
fazekasgy@51 198 numpyInstalled = false;
fazekasgy@51 199 arrayApiInitialised = true;
fazekasgy@51 200 if (pyModule) Py_XDECREF(pyModule);
fazekasgy@51 201 return;
fazekasgy@51 202
fazekasgy@51 203 /*HAVE_NUMPY*/
Chris@116 204 #else
Chris@116 205 DSTREAM << "Numpy support deselected at compile time, not attempting to initialise it" << endl;
Chris@116 206 #endif
fazekasgy@51 207
Chris@116 208 numpyInstalled = false;
fazekasgy@51 209 arrayApiInitialised = true;
fazekasgy@51 210 return;
fazekasgy@37 211 }
fazekasgy@37 212
fazekasgy@37 213
fazekasgy@37 214 static std::vector<PyPluginAdapter *> adapters;
fazekasgy@37 215 static bool haveScannedPlugins = false;
fazekasgy@37 216
fazekasgy@37 217 static bool tryPreload(string name)
fazekasgy@37 218 {
cannam@53 219 // cerr << "tryPreload: " << name << endl;
fazekasgy@37 220 #ifdef _WIN32
fazekasgy@37 221 void *lib = LoadLibrary(name.c_str());
fazekasgy@37 222 if (!lib) {
fazekasgy@37 223 return false;
fazekasgy@37 224 }
fazekasgy@37 225 #else
fazekasgy@37 226 void *lib = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
fazekasgy@37 227 if (!lib) {
cannam@56 228 //perror("dlopen");
fazekasgy@37 229 return false;
fazekasgy@37 230 }
fazekasgy@37 231 #endif
Chris@67 232 DSTREAM << "Preloaded Python from " << name << endl;
fazekasgy@37 233 return true;
fazekasgy@37 234 }
fazekasgy@37 235
fazekasgy@37 236 static bool preloadPython()
fazekasgy@37 237 {
fazekasgy@37 238 #ifdef _WIN32
fazekasgy@37 239 // this doesn't seem to be necessary at all on Windows
fazekasgy@37 240 return true;
fazekasgy@37 241 #endif
fazekasgy@37 242
fazekasgy@37 243 string pyver = Py_GetVersion();
fazekasgy@37 244 int dots = 2;
fazekasgy@37 245 string shortver;
fazekasgy@37 246 for (size_t i = 0; i < pyver.length(); ++i) {
fazekasgy@37 247 if (pyver[i] == '.') {
fazekasgy@37 248 if (--dots == 0) {
fazekasgy@37 249 shortver = pyver.substr(0, i);
fazekasgy@37 250 break;
fazekasgy@37 251 }
fazekasgy@37 252 }
fazekasgy@37 253 }
Chris@67 254 DSTREAM << "Short version: " << shortver << endl;
Chris@67 255 // this is useful to find out where the loaded library might be loaded from
Chris@67 256 DSTREAM << "Python exec prefix: " << Py_GetExecPrefix() << endl;
fazekasgy@37 257
cannam@55 258 char *pylib = getenv("VAMPY_PYLIB");
cannam@54 259 if (pylib && *pylib) {
Chris@67 260 DSTREAM << "Trying to preload Python from specified location " << pylib
Chris@67 261 << "..." << endl;
cannam@54 262 return tryPreload(string(pylib));
cannam@54 263 }
cannam@54 264
fazekasgy@37 265 vector<string> pfxs;
cannam@53 266 pfxs.push_back(string(Py_GetExecPrefix()) + "/");
cannam@53 267 pfxs.push_back(string(Py_GetExecPrefix()) + "/lib/");
fazekasgy@37 268 pfxs.push_back("");
fazekasgy@37 269 pfxs.push_back("/usr/lib/");
fazekasgy@37 270 pfxs.push_back("/usr/local/lib/");
fazekasgy@37 271 char buffer[5];
fazekasgy@37 272
fazekasgy@37 273 // hahaha! grossness is like a brother to us
fazekasgy@37 274 #ifdef __APPLE__
fazekasgy@37 275 for (size_t pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
cannam@53 276 // cerr << "prefix: " << pfxs[pfxidx] << endl;
cannam@53 277 if (tryPreload(pfxs[pfxidx] + string("Python"))) return true;
fazekasgy@37 278 for (int minor = 8; minor >= 0; --minor) {
fazekasgy@37 279 sprintf(buffer, "%d", minor);
fazekasgy@37 280 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib." + buffer)) return true;
fazekasgy@37 281 }
fazekasgy@37 282 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib")) return true;
fazekasgy@37 283 if (tryPreload(pfxs[pfxidx] + string("libpython.dylib"))) return true;
fazekasgy@37 284 }
fazekasgy@37 285 #else
fazekasgy@37 286 for (size_t pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
fazekasgy@37 287 for (int minor = 8; minor >= 0; --minor) {
fazekasgy@37 288 sprintf(buffer, "%d", minor);
fazekasgy@37 289 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so." + buffer)) return true;
fazekasgy@37 290 }
fazekasgy@37 291 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so")) return true;
fazekasgy@37 292 if (tryPreload(pfxs[pfxidx] + string("libpython.so"))) return true;
fazekasgy@37 293 }
fazekasgy@37 294 #endif
fazekasgy@37 295
fazekasgy@37 296 return false;
fazekasgy@37 297 }
fazekasgy@37 298
fazekasgy@37 299
fazekasgy@37 300 static PyExtensionManager pyExtensionManager;
fazekasgy@37 301
fazekasgy@37 302 const VampPluginDescriptor
fazekasgy@37 303 *vampGetPluginDescriptor(unsigned int version,unsigned int index)
Chris@79 304 {
Chris@79 305 DSTREAM << "# vampGetPluginDescriptor(" << version << "," << index << ")" << endl;
Chris@79 306
Chris@116 307 if (version < 1) return 0;
fazekasgy@37 308
Chris@116 309 DSTREAM << "# Vampy version: " << VAMPY_VERSION << endl;
Chris@116 310
fazekasgy@37 311 int isPythonInitialized = Py_IsInitialized();
Chris@67 312 DSTREAM << "# isPythonInitialized: " << isPythonInitialized << endl;
Chris@67 313 DSTREAM << "# haveScannedPlugins: " << haveScannedPlugins << endl;
fazekasgy@37 314
fazekasgy@37 315 if (!haveScannedPlugins) {
fazekasgy@37 316
fazekasgy@37 317 if (!isPythonInitialized){
fazekasgy@37 318
fazekasgy@37 319 if (!preloadPython())
fazekasgy@37 320 cerr << "Warning: Could not preload Python. Dynamic loading in scripts will fail." << endl;
fazekasgy@37 321 if (PyImport_AppendInittab("vampy",initvampy) != 0)
fazekasgy@37 322 cerr << "Warning: Extension module could not be added to module inittab." << endl;
fazekasgy@37 323 Py_Initialize();
fazekasgy@51 324 array_API_initialiser();
fazekasgy@37 325 initvampy();
Chris@67 326 DSTREAM << "# isPythonInitialized after initialize: " << Py_IsInitialized() << endl;
fazekasgy@37 327 }
fazekasgy@37 328
fazekasgy@37 329 vector<string> pyPlugs;
fazekasgy@37 330 vector<string> pyPath;
fazekasgy@37 331 vector<PyObject *> pyClasses;
fazekasgy@37 332 static PyPlugScanner *scanner;
fazekasgy@37 333
fazekasgy@37 334 //Scanning Plugins
Chris@67 335 DSTREAM << "Scanning Vampy Plugins" << endl;
fazekasgy@37 336 scanner = PyPlugScanner::getInstance();
fazekasgy@37 337
fazekasgy@37 338 // added env. varable support VAMPY_EXTPATH
fazekasgy@37 339 pyPath=scanner->getAllValidPath();
fazekasgy@37 340 scanner->setPath(pyPath);
fazekasgy@37 341
fazekasgy@37 342 // added env. variable support:
fazekasgy@37 343 // VAMPY_COMPILED=1 to recognise .pyc files (default is 1)
fazekasgy@37 344 pyPlugs = scanner->getPyPlugs();
fazekasgy@37 345
Chris@67 346 DSTREAM << "Found " << pyPlugs.size() << " Scripts." << endl;
fazekasgy@37 347 //TODO: should this support multiple classes per script (?)
fazekasgy@37 348 pyClasses = scanner->getPyClasses();
Chris@67 349 DSTREAM << "Found " << pyClasses.size() << " Classes." << endl;
fazekasgy@37 350
fazekasgy@37 351 for (size_t i = 0; i < pyPlugs.size(); ++i) {
fazekasgy@37 352 adapters.push_back( new PyPluginAdapter(pyPlugs[i],pyClasses[i]));
fazekasgy@37 353 }
fazekasgy@37 354 pyExtensionManager.setPlugModuleNames(pyPlugs);
fazekasgy@37 355 pyExtensionManager.initExtension();
fazekasgy@37 356 array_API_initialiser();
fazekasgy@37 357 haveScannedPlugins=true;
fazekasgy@37 358 }
fazekasgy@37 359
Chris@67 360 DSTREAM << "Accessing adapter index: " << index << " (adapters: " << adapters.size() << ")" << endl;
fazekasgy@37 361
Chris@84 362 while (index < adapters.size()) {
fazekasgy@37 363
fazekasgy@37 364 const VampPluginDescriptor *tmp = adapters[index]->getDescriptor();
fazekasgy@37 365
fazekasgy@37 366 if (adapters[index]->failed()) {
fazekasgy@37 367 cerr << "\nERROR: [in vampGetPluginDescriptor] Removing adapter of: \n'"
Chris@87 368 << adapters[index]->getPlugKey() << "'\n"
Chris@87 369 << "The plugin has failed to construct. Hint: Check __init__() function." << endl;
fazekasgy@37 370 pyExtensionManager.deleteModuleName(adapters[index]->getPlugKey());
fazekasgy@37 371 delete adapters[index];
Chris@87 372 adapters.erase(adapters.begin() + index);
Chris@84 373 continue;
fazekasgy@37 374 }
fazekasgy@37 375
fazekasgy@37 376 return tmp;
Chris@84 377 }
fazekasgy@37 378
Chris@84 379 return 0;
fazekasgy@37 380 }
fazekasgy@37 381
fazekasgy@37 382
fazekasgy@37 383
fazekasgy@37 384
fazekasgy@37 385
fazekasgy@37 386
fazekasgy@37 387
fazekasgy@37 388