fazekasgy@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
fazekasgy@0
|
2
|
fazekasgy@0
|
3 /*
|
fazekasgy@0
|
4 Vamp
|
fazekasgy@0
|
5
|
fazekasgy@0
|
6 An API for audio analysis and feature extraction plugins.
|
fazekasgy@0
|
7
|
fazekasgy@0
|
8 Centre for Digital Music, Queen Mary, University of London.
|
fazekasgy@0
|
9 Copyright 2006 Chris Cannam.
|
fazekasgy@0
|
10
|
fazekasgy@0
|
11 Permission is hereby granted, free of charge, to any person
|
fazekasgy@0
|
12 obtaining a copy of this software and associated documentation
|
fazekasgy@0
|
13 files (the "Software"), to deal in the Software without
|
fazekasgy@0
|
14 restriction, including without limitation the rights to use, copy,
|
fazekasgy@0
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
fazekasgy@0
|
16 of the Software, and to permit persons to whom the Software is
|
fazekasgy@0
|
17 furnished to do so, subject to the following conditions:
|
fazekasgy@0
|
18
|
fazekasgy@0
|
19 The above copyright notice and this permission notice shall be
|
fazekasgy@0
|
20 included in all copies or substantial portions of the Software.
|
fazekasgy@0
|
21
|
fazekasgy@0
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
fazekasgy@0
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
fazekasgy@0
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
fazekasgy@0
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
fazekasgy@0
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
fazekasgy@0
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
fazekasgy@0
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
fazekasgy@0
|
29
|
fazekasgy@0
|
30 Except as contained in this notice, the names of the Centre for
|
fazekasgy@0
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
fazekasgy@0
|
32 shall not be used in advertising or otherwise to promote the sale,
|
fazekasgy@0
|
33 use or other dealings in this Software without prior written
|
fazekasgy@0
|
34 authorization.
|
fazekasgy@0
|
35 */
|
fazekasgy@0
|
36
|
fazekasgy@0
|
37 /**
|
cannam@7
|
38 * This Vamp plugin is a wrapper for Python Scripts. (VamPy)
|
fazekasgy@0
|
39 * Centre for Digital Music, Queen Mary, University of London.
|
fazekasgy@0
|
40 * Copyright 2008, George Fazekas.
|
fazekasgy@0
|
41 */
|
fazekasgy@0
|
42
|
cannam@3
|
43 #include <Python.h>
|
fazekasgy@0
|
44 #include "vamp/vamp.h"
|
fazekasgy@0
|
45 #include "vamp-sdk/PluginAdapter.h"
|
fazekasgy@0
|
46 #include "PyPlugScanner.h"
|
fazekasgy@0
|
47 #include "PyPlugin.h"
|
fazekasgy@0
|
48
|
fazekasgy@0
|
49 #ifdef _WIN32
|
fazekasgy@0
|
50 #define pathsep ('\\')
|
fazekasgy@0
|
51 #include <windows.h>
|
fazekasgy@0
|
52 #include <tchar.h>
|
fazekasgy@0
|
53 #else
|
fazekasgy@0
|
54 #define pathsep ('/')
|
fazekasgy@0
|
55 #include <dirent.h>
|
fazekasgy@0
|
56 #include <dlfcn.h>
|
fazekasgy@0
|
57 #endif
|
fazekasgy@0
|
58
|
fazekasgy@0
|
59 using std::cerr;
|
fazekasgy@0
|
60 using std::endl;
|
fazekasgy@0
|
61 using std::string;
|
fazekasgy@0
|
62 using std::vector;
|
fazekasgy@0
|
63
|
fazekasgy@6
|
64 //volatile bool mutex = false;
|
fazekasgy@0
|
65 static int adinstcount;
|
fazekasgy@0
|
66
|
fazekasgy@0
|
67 class PyPluginAdapter : public Vamp::PluginAdapterBase
|
fazekasgy@0
|
68 {
|
fazekasgy@0
|
69 public:
|
fazekasgy@0
|
70 PyPluginAdapter(std::string pyPlugId, PyObject* pyInstance) :
|
fazekasgy@0
|
71 PluginAdapterBase(),
|
fazekasgy@0
|
72 m_plug(pyPlugId),
|
fazekasgy@0
|
73 m_pyInstance(pyInstance)
|
fazekasgy@0
|
74 {
|
fazekasgy@0
|
75 cerr << "PyPluginAdapter:ctor:"<< adinstcount << ": " << m_plug << endl;
|
fazekasgy@0
|
76 adinstcount++;
|
fazekasgy@0
|
77 m_instanceCount = 0;
|
fazekasgy@0
|
78 }
|
fazekasgy@0
|
79
|
fazekasgy@0
|
80 ~PyPluginAdapter()
|
fazekasgy@0
|
81 {
|
fazekasgy@0
|
82 }
|
fazekasgy@0
|
83
|
fazekasgy@0
|
84 protected:
|
fazekasgy@0
|
85 Vamp::Plugin *createPlugin(float inputSampleRate) {
|
fazekasgy@0
|
86
|
fazekasgy@0
|
87 std::string pclass = m_plug.substr(m_plug.rfind(':')+1,m_plug.size()-1);
|
fazekasgy@0
|
88 std::string ppath = m_plug.substr(0,m_plug.rfind(pathsep));
|
fazekasgy@0
|
89 PyPlugin *plugin = new PyPlugin(m_plug,inputSampleRate,m_pyInstance);
|
fazekasgy@0
|
90 m_instanceCount++;
|
fazekasgy@0
|
91 cerr << "PyPluginAdapter::createPlugin:" << pclass << " (instance: " << m_instanceCount << ")" << endl;
|
fazekasgy@0
|
92 return plugin;
|
fazekasgy@0
|
93
|
fazekasgy@0
|
94 }
|
fazekasgy@0
|
95
|
fazekasgy@0
|
96 std::string m_plug;
|
fazekasgy@0
|
97 bool m_haveInitialized;
|
fazekasgy@0
|
98 PyObject *m_pyInstance;
|
fazekasgy@0
|
99 int m_instanceCount;
|
fazekasgy@0
|
100
|
fazekasgy@0
|
101 };
|
fazekasgy@0
|
102
|
fazekasgy@0
|
103
|
fazekasgy@0
|
104 static std::vector<PyPluginAdapter *> adapters;
|
fazekasgy@0
|
105 static bool haveScannedPlugins = false;
|
fazekasgy@0
|
106
|
cannam@16
|
107 static bool tryPreload(string name)
|
cannam@16
|
108 {
|
cannam@16
|
109 // cerr << "Trying to load Python interpreter library \"" << name << "\"...";
|
cannam@16
|
110 void *lib = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
|
cannam@16
|
111 if (!lib) {
|
cannam@16
|
112 // char *err = dlerror();
|
cannam@16
|
113 // if (err && err[0]) {
|
cannam@16
|
114 // cerr << " failed (" << err << ")" << endl;
|
cannam@16
|
115 // } else {
|
cannam@16
|
116 // cerr << " failed" << endl;
|
cannam@16
|
117 // }
|
cannam@16
|
118 return false;
|
cannam@16
|
119 }
|
cannam@16
|
120 // cerr << " succeeded" << endl;
|
cannam@16
|
121 return true;
|
cannam@16
|
122 }
|
cannam@16
|
123
|
cannam@16
|
124 static bool preloadPython()
|
cannam@16
|
125 {
|
cannam@17
|
126 // useless on Windows
|
cannam@16
|
127
|
cannam@16
|
128 string pyver = Py_GetVersion();
|
cannam@16
|
129 int dots = 2;
|
cannam@16
|
130 string shortver;
|
cannam@16
|
131 for (int i = 0; i < pyver.length(); ++i) {
|
cannam@16
|
132 if (pyver[i] == '.') {
|
cannam@16
|
133 if (--dots == 0) {
|
cannam@16
|
134 shortver = pyver.substr(0, i);
|
cannam@16
|
135 break;
|
cannam@16
|
136 }
|
cannam@16
|
137 }
|
cannam@16
|
138 }
|
cannam@16
|
139 cerr << "Short version: " << shortver << endl;
|
cannam@16
|
140
|
cannam@16
|
141 vector<string> pfxs;
|
cannam@16
|
142 pfxs.push_back("");
|
cannam@16
|
143 pfxs.push_back(string(Py_GetExecPrefix()) + "/lib/");
|
cannam@16
|
144 pfxs.push_back(string(Py_GetExecPrefix()) + "/");
|
cannam@16
|
145 pfxs.push_back("/usr/lib/");
|
cannam@16
|
146 pfxs.push_back("/usr/local/lib/");
|
cannam@16
|
147 char buffer[5];
|
cannam@16
|
148
|
cannam@16
|
149 // hahaha! grossness is like a brother to us
|
cannam@17
|
150 #ifdef __APPLE__
|
cannam@17
|
151 for (int pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
|
cannam@17
|
152 for (int minor = 8; minor >= 0; --minor) {
|
cannam@17
|
153 sprintf(buffer, "%d", minor);
|
cannam@17
|
154 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib." + buffer)) return true;
|
cannam@17
|
155 }
|
cannam@17
|
156 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib")) return true;
|
cannam@17
|
157 if (tryPreload(pfxs[pfxidx] + string("libpython.dylib"))) return true;
|
cannam@17
|
158 }
|
cannam@17
|
159 #else
|
cannam@16
|
160 for (int pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
|
cannam@16
|
161 for (int minor = 8; minor >= 0; --minor) {
|
cannam@16
|
162 sprintf(buffer, "%d", minor);
|
cannam@16
|
163 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so." + buffer)) return true;
|
cannam@16
|
164 }
|
cannam@16
|
165 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so")) return true;
|
cannam@16
|
166 if (tryPreload(pfxs[pfxidx] + string("libpython.so"))) return true;
|
cannam@16
|
167 }
|
cannam@17
|
168 #endif
|
cannam@16
|
169
|
cannam@16
|
170 return false;
|
cannam@16
|
171 }
|
cannam@16
|
172
|
cannam@16
|
173
|
fazekasgy@0
|
174 const VampPluginDescriptor
|
fazekasgy@0
|
175 *vampGetPluginDescriptor(unsigned int version,unsigned int index)
|
fazekasgy@0
|
176 {
|
fazekasgy@0
|
177 if (version < 1) return 0;
|
fazekasgy@0
|
178
|
fazekasgy@0
|
179 int isPythonInitialized = Py_IsInitialized();
|
fazekasgy@0
|
180 //cerr << "# isPythonInitialized: " << isPythonInitialized << endl;
|
fazekasgy@0
|
181 //cerr << "# haveScannedPlugins: " << haveScannedPlugins << endl;
|
fazekasgy@0
|
182
|
fazekasgy@0
|
183 if (!haveScannedPlugins) {
|
fazekasgy@8
|
184
|
fazekasgy@0
|
185 if (!isPythonInitialized) {
|
fazekasgy@0
|
186
|
cannam@16
|
187 if (!preloadPython()) {
|
cannam@16
|
188 cerr << "Warning: Could not preload Python."
|
cannam@16
|
189 << " Dynamic loading in scripts will fail." << endl;
|
cannam@16
|
190 }
|
cannam@16
|
191
|
cannam@16
|
192 /*
|
fazekasgy@0
|
193 void *pylib = 0;
|
fazekasgy@0
|
194
|
fazekasgy@0
|
195 cerr << "Loading Python Interpreter at: " << pythonPath << endl;
|
fazekasgy@1
|
196 //Preloading the binary allows the load of shared libs
|
fazekasgy@0
|
197 //TODO: check how to do RTLD_NOW on Windows
|
fazekasgy@2
|
198 #ifdef _WIN32
|
fazekasgy@0
|
199 pylib = LoadLibrary(pythonPath.c_str());
|
fazekasgy@0
|
200 #else
|
fazekasgy@0
|
201 pylib = dlopen(pythonPath.c_str(), RTLD_NOW|RTLD_GLOBAL);
|
fazekasgy@0
|
202 #endif
|
fazekasgy@0
|
203 if (!pylib) cerr << "Warning: Could not preload Python."
|
fazekasgy@1
|
204 << " Dynamic loading in scripts will fail." << endl;
|
cannam@16
|
205 */
|
fazekasgy@0
|
206 Py_Initialize();
|
fazekasgy@0
|
207 PyEval_InitThreads();
|
fazekasgy@0
|
208 } else {
|
fazekasgy@0
|
209 //Py_InitializeEx(1);
|
fazekasgy@0
|
210 }
|
fazekasgy@0
|
211
|
fazekasgy@0
|
212 vector<string> pyPlugs;
|
fazekasgy@0
|
213 vector<string> pyPath;
|
fazekasgy@0
|
214 vector<PyObject *> pyInstances;
|
fazekasgy@0
|
215 static PyPlugScanner *scanner;
|
fazekasgy@0
|
216
|
fazekasgy@0
|
217 //Scanning Plugins
|
fazekasgy@0
|
218 cerr << "Scanning PyPlugins" << endl;
|
fazekasgy@0
|
219 scanner = PyPlugScanner::getInstance();
|
fazekasgy@0
|
220 pyPath=scanner->getAllValidPath();
|
fazekasgy@0
|
221 //add this as extra path for development
|
fazekasgy@6
|
222 //pyPath.push_back("/Users/Shared/Development/vamp-experiments");
|
fazekasgy@0
|
223 scanner->setPath(pyPath);
|
fazekasgy@0
|
224 pyPlugs = scanner->getPyPlugs();
|
fazekasgy@0
|
225 cerr << "Found " << pyPlugs.size() << " Scripts ...OK" << endl;
|
fazekasgy@0
|
226 //TODO: this will support multiple classes per script
|
fazekasgy@0
|
227 pyInstances = scanner->getPyInstances();
|
fazekasgy@0
|
228 cerr << "Found " << pyInstances.size() << " Instances ...OK" << endl;
|
fazekasgy@0
|
229
|
fazekasgy@0
|
230 for (size_t i = 0; i < pyPlugs.size(); ++i) {
|
fazekasgy@0
|
231 adapters.push_back( new PyPluginAdapter(pyPlugs[i],pyInstances[i]));
|
fazekasgy@0
|
232 }
|
fazekasgy@0
|
233 haveScannedPlugins=true;
|
fazekasgy@6
|
234
|
fazekasgy@0
|
235 }
|
fazekasgy@0
|
236
|
fazekasgy@0
|
237 cerr << "Accessing adapter index: " << index << " (adapters: " << adapters.size() << ")" << endl;
|
fazekasgy@0
|
238 if (index<adapters.size()) {
|
fazekasgy@0
|
239 const VampPluginDescriptor *tmp = adapters[index]->getDescriptor();
|
fazekasgy@0
|
240 return tmp;
|
fazekasgy@0
|
241 } else return 0;
|
fazekasgy@0
|
242
|
fazekasgy@0
|
243
|
fazekasgy@0
|
244 }
|
fazekasgy@0
|
245
|
fazekasgy@0
|
246
|
fazekasgy@0
|
247
|
fazekasgy@0
|
248
|
fazekasgy@0
|
249
|
fazekasgy@0
|
250
|
fazekasgy@0
|
251
|
fazekasgy@0
|
252
|