Chris@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 //include for python extension module: must be first
|
Chris@0
|
4 #include <Python.h>
|
Chris@0
|
5 #include <vampyhost.h>
|
Chris@0
|
6 #include <pyRealTime.h>
|
Chris@0
|
7
|
Chris@4
|
8 //!!! NB all our NumPy stuff is currently using the deprecated API --
|
Chris@4
|
9 //!!! need to work out how to update this
|
Chris@4
|
10 #include "numpy/arrayobject.h"
|
Chris@4
|
11
|
Chris@0
|
12 //includes for vamp host
|
Chris@1
|
13 #include "vamp-hostsdk/Plugin.h"
|
Chris@1
|
14 #include "vamp-hostsdk/PluginHostAdapter.h"
|
Chris@1
|
15 #include "vamp-hostsdk/PluginChannelAdapter.h"
|
Chris@1
|
16 #include "vamp-hostsdk/PluginInputDomainAdapter.h"
|
Chris@1
|
17 #include "vamp-hostsdk/PluginLoader.h"
|
Chris@0
|
18 //#include "vamp/vamp.h"
|
Chris@0
|
19
|
Chris@0
|
20 #include <iostream>
|
Chris@0
|
21 #include <fstream>
|
Chris@0
|
22 #include <set>
|
Chris@0
|
23 #include <sndfile.h>
|
Chris@0
|
24
|
Chris@0
|
25 #include <cstring>
|
Chris@0
|
26 #include <cstdlib>
|
Chris@0
|
27 #include <string>
|
Chris@0
|
28
|
Chris@0
|
29 #include "system.h"
|
Chris@0
|
30
|
Chris@0
|
31 #include <cmath>
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@0
|
34 using namespace std;
|
Chris@0
|
35 using namespace Vamp;
|
Chris@0
|
36
|
Chris@0
|
37 using Vamp::Plugin;
|
Chris@0
|
38 using Vamp::PluginHostAdapter;
|
Chris@0
|
39 using Vamp::RealTime;
|
Chris@0
|
40 using Vamp::HostExt::PluginLoader;
|
Chris@0
|
41
|
Chris@0
|
42 #define HOST_VERSION "1.1"
|
Chris@0
|
43
|
Chris@0
|
44
|
Chris@0
|
45 /* MODULE HELPER FUNCTIONS */
|
Chris@2
|
46 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
|
Chris@0
|
47
|
Chris@0
|
48 /*obtain C plugin handle and key from pyCobject */
|
Chris@2
|
49 bool getPluginHandle
|
Chris@0
|
50 (PyObject *pyPluginHandle, Plugin **plugin, string **pKey=NULL) {
|
Chris@0
|
51
|
Chris@0
|
52 //char errormsg[]="Wrong input argument: Plugin Handle required.";
|
Chris@0
|
53
|
Chris@0
|
54 *plugin = NULL;
|
Chris@2
|
55 if (!PyCObject_Check(pyPluginHandle)) return false;
|
Chris@0
|
56
|
Chris@0
|
57 //try to convert to Plugin pointer
|
Chris@0
|
58 Plugin *p = (Plugin*) PyCObject_AsVoidPtr(pyPluginHandle);
|
Chris@2
|
59 if (!p) return false;
|
Chris@0
|
60
|
Chris@0
|
61 string pId;
|
Chris@0
|
62
|
Chris@0
|
63 if (pKey) {
|
Chris@0
|
64 *pKey = (string*) PyCObject_GetDesc(pyPluginHandle);
|
Chris@2
|
65 if (!*pKey) return false;
|
Chris@0
|
66 pId = *(string*) *pKey;
|
Chris@0
|
67
|
Chris@0
|
68 } else {
|
Chris@0
|
69
|
Chris@0
|
70 void *pKey = PyCObject_GetDesc(pyPluginHandle);
|
Chris@2
|
71 if (!pKey) return false;
|
Chris@0
|
72 pId = *(string*) pKey;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 string::size_type pos = pId.find(':');
|
Chris@2
|
76 if (pos == string::npos) return false;
|
Chris@0
|
77
|
Chris@0
|
78 pId = pId.substr(pId.rfind(':')+1);
|
Chris@0
|
79 string identifier = p->getIdentifier();
|
Chris@0
|
80
|
Chris@2
|
81 if (pId.compare(identifier)) return false;
|
Chris@0
|
82
|
Chris@0
|
83 *plugin = p;
|
Chris@0
|
84 return true;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /*
|
Chris@0
|
88 ----------------------------------------------------------------
|
Chris@0
|
89 */
|
Chris@0
|
90
|
Chris@0
|
91
|
Chris@0
|
92
|
Chris@0
|
93 /*
|
Chris@0
|
94 VAMPYHOST MAIN
|
Chris@0
|
95 ---------------------------------------------------------------------
|
Chris@0
|
96 */
|
Chris@0
|
97
|
Chris@0
|
98 /* ENUMERATE PLUGINS*/
|
Chris@0
|
99
|
Chris@0
|
100 static PyObject *
|
Chris@0
|
101 vampyhost_enumeratePlugins(PyObject *self, PyObject *args)
|
Chris@0
|
102 {
|
Chris@0
|
103 string retType;
|
Chris@0
|
104
|
Chris@0
|
105 if (!PyArg_ParseTuple(args, "|s:enumeratePlugins", &retType))
|
Chris@0
|
106 return NULL;
|
Chris@0
|
107
|
Chris@0
|
108 //list available plugins
|
Chris@0
|
109 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
110 vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
|
Chris@0
|
111
|
Chris@0
|
112 //library Map
|
Chris@0
|
113 typedef multimap<string, PluginLoader::PluginKey> LibraryMap;
|
Chris@0
|
114 LibraryMap libraryMap;
|
Chris@0
|
115
|
Chris@0
|
116 //New list object
|
Chris@0
|
117 PyObject *pyList = PyList_New(plugins.size());
|
Chris@0
|
118
|
Chris@0
|
119 for (size_t i = 0; i < plugins.size(); ++i) {
|
Chris@0
|
120 string path = loader->getLibraryPathForPlugin(plugins[i]);
|
Chris@0
|
121 libraryMap.insert(LibraryMap::value_type(path, plugins[i]));
|
Chris@0
|
122
|
Chris@0
|
123 PyObject *pyPluginKey = PyString_FromString(plugins[i].c_str());
|
Chris@0
|
124 PyList_SET_ITEM(pyList,i,pyPluginKey);
|
Chris@0
|
125
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 PyList_Sort(pyList);
|
Chris@0
|
129 return pyList;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132
|
Chris@0
|
133 /* GET PLUGIN LIBRARY PATH*/
|
Chris@0
|
134
|
Chris@0
|
135 static PyObject *
|
Chris@0
|
136 vampyhost_getLibraryPath(PyObject *self, PyObject *args)
|
Chris@0
|
137 {
|
Chris@0
|
138 PyObject *pyPluginKey;
|
Chris@0
|
139
|
Chris@0
|
140 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
|
Chris@0
|
141 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
142 "String input argument required: pluginKey");
|
Chris@0
|
143 return NULL; }
|
Chris@0
|
144
|
Chris@0
|
145 //convert to stl string
|
Chris@0
|
146 string pluginKey(PyString_AS_STRING(pyPluginKey));
|
Chris@0
|
147
|
Chris@0
|
148 //check pluginKey Validity
|
Chris@0
|
149 string::size_type ki = pluginKey.find(':');
|
Chris@0
|
150 if (ki == string::npos) {
|
Chris@0
|
151 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
152 "String input argument required: pluginLibrary:Identifier");
|
Chris@0
|
153 return NULL;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
157 string path = loader->getLibraryPathForPlugin(pluginKey);
|
Chris@0
|
158 PyObject *pyPath = PyString_FromString(path.c_str());
|
Chris@0
|
159 return pyPath;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162
|
Chris@0
|
163 /* GET PLUGIN CATEGORY*/
|
Chris@0
|
164
|
Chris@0
|
165 static PyObject *
|
Chris@0
|
166 vampyhost_getPluginCategory(PyObject *self, PyObject *args)
|
Chris@0
|
167 {
|
Chris@0
|
168 PyObject *pyPluginKey;
|
Chris@0
|
169
|
Chris@0
|
170 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
|
Chris@0
|
171 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
172 "String input argument required: pluginKey");
|
Chris@0
|
173 return NULL; }
|
Chris@0
|
174
|
Chris@0
|
175 //convert to stl string
|
Chris@0
|
176 string pluginKey(PyString_AS_STRING(pyPluginKey));
|
Chris@0
|
177
|
Chris@0
|
178 //check pluginKey Validity
|
Chris@0
|
179 string::size_type ki = pluginKey.find(':');
|
Chris@0
|
180 if (ki == string::npos) {
|
Chris@0
|
181 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
182 "String input argument required: pluginLibrary:Identifier");
|
Chris@0
|
183 return NULL;
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
187 PluginLoader::PluginCategoryHierarchy
|
Chris@0
|
188 category = loader->getPluginCategory(pluginKey);
|
Chris@0
|
189 string catstring;
|
Chris@0
|
190
|
Chris@0
|
191 if (!category.empty()) {
|
Chris@0
|
192 catstring = "";
|
Chris@0
|
193 for (size_t ci = 0; ci < category.size(); ++ci) {
|
Chris@0
|
194 catstring.append(category[ci]);
|
Chris@0
|
195 catstring.append(" ");
|
Chris@0
|
196 }
|
Chris@0
|
197 PyObject *pyCat = PyString_FromString(catstring.c_str());
|
Chris@0
|
198 return pyCat;
|
Chris@0
|
199 }
|
Chris@0
|
200 PyObject *pyCat = PyString_FromString("");
|
Chris@0
|
201 return pyCat;
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204
|
Chris@0
|
205
|
Chris@0
|
206 /* GET PLUGIN OUTPUT LIST*/
|
Chris@0
|
207
|
Chris@0
|
208 static PyObject *
|
Chris@0
|
209 vampyhost_getOutputList(PyObject *self, PyObject *args)
|
Chris@0
|
210 {
|
Chris@0
|
211 PyObject *pyPluginHandle;
|
Chris@0
|
212 string pluginKey;
|
Chris@0
|
213
|
Chris@0
|
214 if (!PyArg_ParseTuple(args, "O", &pyPluginHandle)) {
|
Chris@0
|
215 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
216 "Invalid argument: plugin handle or plugin key required.");
|
Chris@0
|
217 return NULL;
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 //check if we have a plugin key string or a handle object
|
Chris@0
|
221 if (PyString_Check(pyPluginHandle) ) {
|
Chris@0
|
222
|
Chris@0
|
223 pluginKey.assign(PyString_AS_STRING(pyPluginHandle));
|
Chris@0
|
224 //check pluginKey Validity
|
Chris@0
|
225 string::size_type ki = pluginKey.find(':');
|
Chris@0
|
226 if (ki == string::npos) {
|
Chris@0
|
227 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
228 "String input argument required: pluginLibrary:Identifier");
|
Chris@0
|
229 return NULL;
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 } else {
|
Chris@0
|
233
|
Chris@0
|
234 string *key;
|
Chris@0
|
235 Plugin *plugin;
|
Chris@0
|
236
|
Chris@0
|
237 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) {
|
Chris@0
|
238 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
239 "Invalid or deleted plugin handle.");
|
Chris@0
|
240 return NULL; }
|
Chris@0
|
241 pluginKey.assign(*key);
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 //This code creates new instance of the plugin anyway
|
Chris@0
|
245 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
246
|
Chris@0
|
247 //load plugin
|
Chris@3
|
248 Plugin *plugin = loader->loadPlugin
|
Chris@3
|
249 (pluginKey, 48000, PluginLoader::ADAPT_ALL_SAFE);
|
Chris@0
|
250 if (!plugin) {
|
Chris@0
|
251 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
|
Chris@0
|
252 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@0
|
253 return NULL;
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 Plugin::OutputList outputs = plugin->getOutputDescriptors();
|
Chris@0
|
257 //Plugin::OutputDescriptor od;
|
Chris@0
|
258
|
Chris@0
|
259 if (outputs.size()<1) {
|
Chris@0
|
260 string pyerr("Plugin has no output: "); pyerr += pluginKey;
|
Chris@0
|
261 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@0
|
262 return NULL;
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 //New list object
|
Chris@0
|
266 PyObject *pyList = PyList_New(outputs.size());
|
Chris@0
|
267
|
Chris@0
|
268 for (size_t i = 0; i < outputs.size(); ++i) {
|
Chris@0
|
269 PyObject *pyOutputId =
|
Chris@0
|
270 PyString_FromString(outputs[i].identifier.c_str());
|
Chris@0
|
271 PyList_SET_ITEM(pyList,i,pyOutputId);
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 delete plugin;
|
Chris@0
|
275 return pyList;
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278
|
Chris@0
|
279
|
Chris@0
|
280 /* LOAD PLUGIN */
|
Chris@0
|
281
|
Chris@0
|
282 static PyObject *
|
Chris@0
|
283 vampyhost_loadPlugin(PyObject *self, PyObject *args)
|
Chris@0
|
284 {
|
Chris@0
|
285 PyObject *pyPluginKey;
|
Chris@0
|
286 float inputSampleRate;
|
Chris@0
|
287
|
Chris@0
|
288 if (!PyArg_ParseTuple(args, "Sf",
|
Chris@0
|
289 &pyPluginKey,
|
Chris@0
|
290 &inputSampleRate)) {
|
Chris@0
|
291 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
292 "String input argument required: pluginKey");
|
Chris@0
|
293 return NULL; }
|
Chris@0
|
294
|
Chris@0
|
295 //convert to stl string
|
Chris@0
|
296 string pluginKey(PyString_AS_STRING(pyPluginKey));
|
Chris@0
|
297
|
Chris@0
|
298 //check pluginKey Validity
|
Chris@0
|
299 string::size_type ki = pluginKey.find(':');
|
Chris@0
|
300 if (ki == string::npos) {
|
Chris@0
|
301 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
302 "String input argument required: pluginLibrary:Identifier");
|
Chris@0
|
303 return NULL;
|
Chris@0
|
304 }
|
Chris@0
|
305
|
Chris@0
|
306 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
307
|
Chris@0
|
308 //load plugin
|
Chris@0
|
309 Plugin *plugin = loader->loadPlugin (pluginKey, inputSampleRate);
|
Chris@0
|
310 if (!plugin) {
|
Chris@0
|
311 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
|
Chris@0
|
312 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@0
|
313 return NULL;
|
Chris@0
|
314 }
|
Chris@0
|
315 //void *identifier = (void*) new string(pluginKey);
|
Chris@0
|
316 PyPluginDescriptor *pd = new PyPluginDescriptor;
|
Chris@0
|
317
|
Chris@0
|
318 pd->key = pluginKey;
|
Chris@0
|
319 pd->isInitialised = false;
|
Chris@0
|
320 pd->inputSampleRate = inputSampleRate;
|
Chris@0
|
321
|
Chris@0
|
322 //New PyCObject
|
Chris@0
|
323 //PyObject *pyPluginHandle = PyCObject_FromVoidPtrAndDesc(
|
Chris@0
|
324 //(void*) plugin, identifier, NULL);
|
Chris@0
|
325
|
Chris@0
|
326 PyObject *pyPluginHandle = PyCObject_FromVoidPtrAndDesc(
|
Chris@0
|
327 (void*) plugin, (void*) pd, NULL);
|
Chris@0
|
328
|
Chris@0
|
329 return pyPluginHandle;
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332
|
Chris@0
|
333
|
Chris@0
|
334 /* UNLOAD PLUGIN */
|
Chris@0
|
335
|
Chris@0
|
336 static PyObject *
|
Chris@0
|
337 vampyhost_unloadPlugin(PyObject *self, PyObject *args)
|
Chris@0
|
338 {
|
Chris@0
|
339 PyObject *pyPluginHandle;
|
Chris@0
|
340
|
Chris@0
|
341 if (!PyArg_ParseTuple(args, "O", &pyPluginHandle)) {
|
Chris@0
|
342 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
343 "Wrong input argument: Plugin Handle required.");
|
Chris@0
|
344 return NULL; }
|
Chris@0
|
345
|
Chris@0
|
346 string *key;
|
Chris@0
|
347 Plugin *plugin;
|
Chris@0
|
348
|
Chris@0
|
349 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) {
|
Chris@0
|
350 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
351 "Invalid or already deleted plugin handle.");
|
Chris@0
|
352 return NULL; }
|
Chris@0
|
353
|
Chris@0
|
354 /* Prevent repeated calls from causing segfault
|
Chris@0
|
355 sice it will fail type checking the 2nd time: */
|
Chris@0
|
356 PyCObject_SetVoidPtr(pyPluginHandle,NULL);
|
Chris@0
|
357
|
Chris@0
|
358 PyPluginDescriptor *pd = (PyPluginDescriptor*) key;
|
Chris@0
|
359
|
Chris@0
|
360 delete plugin;
|
Chris@0
|
361 delete pd;
|
Chris@0
|
362 return pyPluginHandle;
|
Chris@0
|
363
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366
|
Chris@0
|
367 /* INITIALISE PLUGIN */
|
Chris@0
|
368
|
Chris@0
|
369 static PyObject *
|
Chris@0
|
370 vampyhost_initialise(PyObject *self, PyObject *args)
|
Chris@0
|
371 {
|
Chris@0
|
372 PyObject *pyPluginHandle;
|
Chris@0
|
373 size_t channels,blockSize,stepSize;
|
Chris@0
|
374
|
Chris@0
|
375 if (!PyArg_ParseTuple (args, "Oiii", &pyPluginHandle,
|
Chris@0
|
376 (size_t) &channels,
|
Chris@0
|
377 (size_t) &stepSize,
|
Chris@0
|
378 (size_t) &blockSize))
|
Chris@0
|
379 {
|
Chris@0
|
380 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
381 "Wrong input arguments: requires a valid plugin handle,channels,stepSize,blockSize.");
|
Chris@0
|
382 return NULL;
|
Chris@0
|
383 }
|
Chris@0
|
384
|
Chris@0
|
385 Plugin *plugin;
|
Chris@0
|
386 string *key;
|
Chris@0
|
387
|
Chris@0
|
388 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) {
|
Chris@0
|
389 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
390 "Invalid plugin handle.");
|
Chris@0
|
391 return NULL; }
|
Chris@0
|
392
|
Chris@0
|
393 // here we cast the void pointer as PyPluginDescriptor instead of string
|
Chris@0
|
394 PyPluginDescriptor *plugDesc = (PyPluginDescriptor*) key;
|
Chris@0
|
395
|
Chris@0
|
396 plugDesc->channels = channels;
|
Chris@0
|
397 plugDesc->stepSize = stepSize;
|
Chris@0
|
398 plugDesc->blockSize = blockSize;
|
Chris@3
|
399
|
Chris@0
|
400 if (!plugin->initialise(channels, stepSize, blockSize)) {
|
Chris@0
|
401 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
402 "Plugin initialization failed.");
|
Chris@0
|
403 return NULL; }
|
Chris@0
|
404
|
Chris@0
|
405 plugDesc->identifier =
|
Chris@0
|
406 plugDesc->key.substr(plugDesc->key.rfind(':')+1);
|
Chris@0
|
407 plugDesc->isInitialised = true;
|
Chris@0
|
408
|
Chris@0
|
409 return Py_True;
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@4
|
412 // These conversion functions are borrowed from PyTypeInterface in VamPy
|
Chris@4
|
413
|
Chris@4
|
414 template<typename RET, typename DTYPE>
|
Chris@4
|
415 static
|
Chris@4
|
416 RET *pyArrayConvert(char* raw_data_ptr, long length, size_t strides)
|
Chris@4
|
417 {
|
Chris@4
|
418 RET *rValue = new RET[length];
|
Chris@4
|
419
|
Chris@4
|
420 /// check if the array is continuous, if not use strides info
|
Chris@4
|
421 if (sizeof(DTYPE)!=strides) {
|
Chris@4
|
422 char* data = (char*) raw_data_ptr;
|
Chris@4
|
423 for (long i = 0; i<length; ++i){
|
Chris@4
|
424 rValue[i] = (RET)(*((DTYPE*)data));
|
Chris@4
|
425 data += strides;
|
Chris@4
|
426 }
|
Chris@4
|
427 return rValue;
|
Chris@4
|
428 }
|
Chris@4
|
429
|
Chris@4
|
430 DTYPE* data = (DTYPE*) raw_data_ptr;
|
Chris@4
|
431 for (long i = 0; i<length; ++i){
|
Chris@4
|
432 rValue[i] = (RET)data[i];
|
Chris@4
|
433 }
|
Chris@4
|
434
|
Chris@4
|
435 return rValue;
|
Chris@4
|
436 }
|
Chris@4
|
437
|
Chris@4
|
438 static float *
|
Chris@4
|
439 pyArrayToFloatArray(PyObject *pyValue)
|
Chris@4
|
440 {
|
Chris@4
|
441 if (!PyArray_Check(pyValue)) {
|
Chris@4
|
442 cerr << "pyArrayToFloatArray: Failed, object has no array interface" << endl;
|
Chris@4
|
443 return 0;
|
Chris@4
|
444 }
|
Chris@4
|
445
|
Chris@4
|
446 PyArrayObject* pyArray = (PyArrayObject*) pyValue;
|
Chris@4
|
447 PyArray_Descr* descr = pyArray->descr;
|
Chris@4
|
448
|
Chris@4
|
449 /// check raw data and descriptor pointers
|
Chris@4
|
450 if (pyArray->data == 0 || descr == 0) {
|
Chris@4
|
451 cerr << "pyArrayToFloatArray: Failed, NumPy array has NULL data or descriptor" << endl;
|
Chris@4
|
452 return 0;
|
Chris@4
|
453 }
|
Chris@4
|
454
|
Chris@4
|
455 /// check dimensions
|
Chris@4
|
456 if (pyArray->nd != 1) {
|
Chris@4
|
457 cerr << "pyArrayToFloatArray: Failed, NumPy array is multi-dimensional" << endl;
|
Chris@4
|
458 return 0;
|
Chris@4
|
459 }
|
Chris@4
|
460
|
Chris@4
|
461 /// check strides (useful if array is not continuous)
|
Chris@4
|
462 size_t strides = *((size_t*) pyArray->strides);
|
Chris@4
|
463
|
Chris@4
|
464 /// convert the array
|
Chris@4
|
465 switch (descr->type_num) {
|
Chris@4
|
466 case NPY_FLOAT : // dtype='float32'
|
Chris@4
|
467 return pyArrayConvert<float,float>(pyArray->data,pyArray->dimensions[0],strides);
|
Chris@4
|
468 case NPY_DOUBLE : // dtype='float64'
|
Chris@4
|
469 return pyArrayConvert<float,double>(pyArray->data,pyArray->dimensions[0],strides);
|
Chris@4
|
470 default:
|
Chris@4
|
471 cerr << "pyArrayToFloatArray: Failed: Unsupported value type " << descr->type_num << " in NumPy array object (only float32, float64 supported)" << endl;
|
Chris@4
|
472 return 0;
|
Chris@4
|
473 }
|
Chris@4
|
474 }
|
Chris@4
|
475
|
Chris@0
|
476
|
Chris@0
|
477 /* RUN PROCESS */
|
Chris@0
|
478
|
Chris@0
|
479 static PyObject *
|
Chris@0
|
480 vampyhost_process(PyObject *self, PyObject *args)
|
Chris@0
|
481 {
|
Chris@0
|
482 PyObject *pyPluginHandle;
|
Chris@0
|
483 PyObject *pyBuffer;
|
Chris@0
|
484 PyObject *pyRealTime;
|
Chris@0
|
485
|
Chris@0
|
486 if (!PyArg_ParseTuple(args, "OOO",
|
Chris@0
|
487 &pyPluginHandle, // C object holding a pointer to a plugin and its descriptor
|
Chris@0
|
488 &pyBuffer, // Audio data
|
Chris@0
|
489 &pyRealTime)) { // TimeStamp
|
Chris@0
|
490 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
491 "Required: plugin handle, buffer, timestmap.");
|
Chris@0
|
492 return NULL; }
|
Chris@0
|
493
|
Chris@0
|
494 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@0
|
495 PyErr_SetString(PyExc_TypeError,"Valid timestamp required.");
|
Chris@0
|
496 return NULL; }
|
Chris@0
|
497
|
Chris@4
|
498 string *key;
|
Chris@0
|
499 Plugin *plugin;
|
Chris@0
|
500
|
Chris@4
|
501 if (!getPluginHandle(pyPluginHandle, &plugin, &key)) {
|
Chris@0
|
502 PyErr_SetString(PyExc_AttributeError,
|
Chris@0
|
503 "Invalid or already deleted plugin handle.");
|
Chris@4
|
504 return NULL;
|
Chris@4
|
505 }
|
Chris@0
|
506
|
Chris@0
|
507 PyPluginDescriptor *pd = (PyPluginDescriptor*) key;
|
Chris@0
|
508
|
Chris@0
|
509 if (!pd->isInitialised) {
|
Chris@0
|
510 PyErr_SetString(PyExc_StandardError,
|
Chris@0
|
511 "Plugin has not been initialised.");
|
Chris@0
|
512 return NULL; }
|
Chris@0
|
513
|
Chris@0
|
514 size_t channels = pd->channels;
|
Chris@0
|
515 size_t blockSize = pd->blockSize;
|
Chris@0
|
516
|
Chris@4
|
517 if (!PyList_Check(pyBuffer)) {
|
Chris@4
|
518 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input.");
|
Chris@4
|
519 return NULL;
|
Chris@0
|
520 }
|
Chris@0
|
521
|
Chris@4
|
522 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@4
|
523 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@4
|
524 return NULL;
|
Chris@4
|
525 }
|
Chris@0
|
526
|
Chris@4
|
527 float **inbuf = new float *[channels];
|
Chris@0
|
528
|
Chris@4
|
529 for (int c = 0; c < channels; ++c) {
|
Chris@4
|
530 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@4
|
531 inbuf[c] = pyArrayToFloatArray(cbuf);
|
Chris@4
|
532 if (!inbuf[c]) {
|
Chris@4
|
533 PyErr_SetString(PyExc_TypeError,"NumPy Array required for each channel in process input.");
|
Chris@4
|
534 return NULL;
|
Chris@0
|
535 }
|
Chris@4
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 RealTime timeStamp = *PyRealTime_AsPointer(pyRealTime);
|
Chris@0
|
539
|
Chris@0
|
540 //Call process and store the output
|
Chris@4
|
541 pd->output = plugin->process(inbuf, timeStamp);
|
Chris@0
|
542
|
Chris@0
|
543 /* TODO: DO SOMETHONG WITH THE FEATURE SET HERE */
|
Chris@0
|
544 /// convert to appropriate python objects, reuse types and conversion utilities from Vampy ...
|
Chris@0
|
545
|
Chris@0
|
546
|
Chris@4
|
547 for (int c = 0; c < channels; ++c){
|
Chris@4
|
548 delete[] inbuf[c];
|
Chris@0
|
549 }
|
Chris@4
|
550 delete[] inbuf;
|
Chris@0
|
551
|
Chris@4
|
552 return NULL; //!!! Need to return actual features!
|
Chris@0
|
553
|
Chris@0
|
554 }
|
Chris@0
|
555
|
Chris@0
|
556 /* GET / SET OUTPUT */
|
Chris@0
|
557
|
Chris@0
|
558 //getOutput(plugin,outputNo)
|
Chris@0
|
559 static PyObject *
|
Chris@0
|
560 vampyhost_getOutput(PyObject *self, PyObject *args) {
|
Chris@0
|
561
|
Chris@0
|
562 PyObject *pyPluginHandle;
|
Chris@0
|
563 // PyObject *pyBuffer;
|
Chris@0
|
564 // PyObject *pyRealTime;
|
Chris@0
|
565 PyObject *pyOutput;
|
Chris@0
|
566
|
Chris@0
|
567 if (!PyArg_ParseTuple(args, "OO",
|
Chris@0
|
568 &pyPluginHandle, // C object holding a pointer to a plugin and its descriptor
|
Chris@0
|
569 &pyOutput)) { // Output reference
|
Chris@0
|
570 PyErr_SetString(PyExc_TypeError,
|
Chris@0
|
571 "Required: plugin handle, buffer, timestmap.");
|
Chris@0
|
572 return NULL; }
|
Chris@0
|
573
|
Chris@0
|
574 string *key;
|
Chris@0
|
575 Plugin *plugin;
|
Chris@0
|
576
|
Chris@0
|
577 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) {
|
Chris@0
|
578 PyErr_SetString(PyExc_AttributeError,
|
Chris@0
|
579 "Invalid or already deleted plugin handle.");
|
Chris@0
|
580 return NULL; }
|
Chris@0
|
581
|
Chris@0
|
582 PyPluginDescriptor *pd = (PyPluginDescriptor*) key;
|
Chris@0
|
583
|
Chris@0
|
584 unsigned int outputNo = (unsigned int) PyInt_AS_LONG(pyOutput);
|
Chris@0
|
585
|
Chris@0
|
586 //Get output list: but we don't need it
|
Chris@0
|
587 //Plugin::FeatureList features = pd->output[outputNo];
|
Chris@0
|
588
|
Chris@0
|
589 size_t outLength = pd->output[outputNo].size();
|
Chris@0
|
590
|
Chris@0
|
591 //New PyList for the featurelist
|
Chris@0
|
592 PyObject *pyFeatureList = PyList_New(outLength);
|
Chris@0
|
593
|
Chris@0
|
594 for (size_t i = 0; i < outLength; ++i) {
|
Chris@0
|
595 // Test:
|
Chris@0
|
596 /*
|
Chris@0
|
597 XxoObject *pyFeature = PyObject_New(XxoObject, &Xxo_Type);
|
Chris@0
|
598 if (pyFeature == NULL) break; //return NULL;
|
Chris@0
|
599
|
Chris@0
|
600 pyFeature->x_attr = NULL;
|
Chris@0
|
601 pyFeature->feature = &pd->output[outputNo][i];
|
Chris@0
|
602
|
Chris@0
|
603 PyList_SET_ITEM(pyFeatureList,i,(PyObject*)pyFeature);
|
Chris@0
|
604 */
|
Chris@0
|
605 }
|
Chris@0
|
606
|
Chris@0
|
607 Py_INCREF(pyFeatureList);
|
Chris@0
|
608 return pyFeatureList;
|
Chris@0
|
609
|
Chris@0
|
610 // EXPLAIN WHAT WE NEED TO DO HERE:
|
Chris@0
|
611 // We have the block output in pd->output
|
Chris@0
|
612 // FeatureSet[output] -> [Feature[x]] -> Feature.hasTimestamp = v
|
Chris@0
|
613 // Vamp::Plugin::FeatureSet output; = pd->output
|
Chris@0
|
614 // typedef std::vector<Feature> FeatureList;
|
Chris@0
|
615 // typedef std::map<int, FeatureList> FeatureSet; // key is output no
|
Chris@0
|
616
|
Chris@0
|
617 // THIS IS FOR OUTPUT id LOOKUP LATER
|
Chris@0
|
618 // Plugin::OutputList outputs = plugin->getOutputDescriptors();
|
Chris@0
|
619 //
|
Chris@0
|
620 // if (outputs.size()<1) {
|
Chris@0
|
621 // string pyerr("Plugin has no output: "); pyerr += pluginKey;
|
Chris@0
|
622 // PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@0
|
623 // return NULL;
|
Chris@0
|
624 // }
|
Chris@0
|
625 //
|
Chris@0
|
626 // //New list object
|
Chris@0
|
627 // PyObject *pyList = PyList_New(outputs.size());
|
Chris@0
|
628 //
|
Chris@0
|
629 // for (size_t i = 0; i < outputs.size(); ++i) {
|
Chris@0
|
630 // PyObject *pyOutputId =
|
Chris@0
|
631 // PyString_FromString(outputs[i].identifier.c_str());
|
Chris@0
|
632 // PyList_SET_ITEM(pyList,i,pyOutputId);
|
Chris@0
|
633 // }
|
Chris@0
|
634
|
Chris@0
|
635 }
|
Chris@0
|
636
|
Chris@0
|
637
|
Chris@0
|
638
|
Chris@0
|
639
|
Chris@0
|
640 /* List of functions defined in this module */
|
Chris@0
|
641 //module methods table
|
Chris@0
|
642 static PyMethodDef vampyhost_methods[] = {
|
Chris@0
|
643
|
Chris@0
|
644 {"enumeratePlugins", vampyhost_enumeratePlugins, METH_VARARGS,
|
Chris@0
|
645 xx_foo_doc},
|
Chris@0
|
646
|
Chris@0
|
647 {"getLibraryPath", vampyhost_getLibraryPath, METH_VARARGS,
|
Chris@0
|
648 xx_foo_doc},
|
Chris@0
|
649
|
Chris@0
|
650 {"getPluginCategory", vampyhost_getPluginCategory, METH_VARARGS,
|
Chris@0
|
651 xx_foo_doc},
|
Chris@0
|
652
|
Chris@0
|
653 {"getOutputList", vampyhost_getOutputList, METH_VARARGS,
|
Chris@0
|
654 xx_foo_doc},
|
Chris@0
|
655
|
Chris@0
|
656 {"loadPlugin", vampyhost_loadPlugin, METH_VARARGS,
|
Chris@0
|
657 xx_foo_doc},
|
Chris@0
|
658
|
Chris@0
|
659 {"process", vampyhost_process, METH_VARARGS,
|
Chris@0
|
660 xx_foo_doc},
|
Chris@0
|
661
|
Chris@0
|
662 {"unloadPlugin", vampyhost_unloadPlugin, METH_VARARGS,
|
Chris@0
|
663 xx_foo_doc},
|
Chris@0
|
664
|
Chris@0
|
665 {"initialise", vampyhost_initialise, METH_VARARGS,
|
Chris@0
|
666 xx_foo_doc},
|
Chris@0
|
667
|
Chris@0
|
668 {"getOutput", vampyhost_getOutput, METH_VARARGS,
|
Chris@0
|
669 xx_foo_doc},
|
Chris@0
|
670
|
Chris@0
|
671 /* Add RealTime Module Methods */
|
Chris@0
|
672
|
Chris@0
|
673 {"frame2RealTime", (PyCFunction)RealTime_frame2RealTime, METH_VARARGS,
|
Chris@0
|
674 PyDoc_STR("frame2RealTime((int64)frame, (uint32)sampleRate ) -> returns new RealTime object from frame.")},
|
Chris@0
|
675
|
Chris@0
|
676 {"realtime", (PyCFunction)RealTime_new, METH_VARARGS,
|
Chris@0
|
677 PyDoc_STR("realtime() -> returns new RealTime object")},
|
Chris@0
|
678
|
Chris@0
|
679 {NULL, NULL} /* sentinel */
|
Chris@0
|
680 };
|
Chris@0
|
681
|
Chris@0
|
682 //Documentation for our new module
|
Chris@0
|
683 PyDoc_STRVAR(module_doc, "This is a template module just for instruction.");
|
Chris@0
|
684
|
Chris@0
|
685 /* Initialization function for the module (*must* be called initxx) */
|
Chris@0
|
686
|
Chris@0
|
687 //module initialization (includes extern C {...} as necessary)
|
Chris@0
|
688 PyMODINIT_FUNC
|
Chris@0
|
689 initvampyhost(void)
|
Chris@0
|
690 {
|
Chris@0
|
691 PyObject *m;
|
Chris@0
|
692
|
Chris@0
|
693 /* Finalize the type object including setting type of the new type
|
Chris@0
|
694 * object; doing it here is required for portability to Windows
|
Chris@0
|
695 * without requiring C++. */
|
Chris@0
|
696
|
Chris@0
|
697 if (PyType_Ready(&RealTime_Type) < 0)
|
Chris@0
|
698 return;
|
Chris@0
|
699 // PyModule_AddObject(m, "Real_Time", (PyObject *)&RealTime_Type);
|
Chris@0
|
700
|
Chris@0
|
701 /* Create the module and add the functions */
|
Chris@0
|
702 m = Py_InitModule3("vampyhost", vampyhost_methods, module_doc);
|
Chris@0
|
703 if (m == NULL) return;
|
Chris@0
|
704
|
Chris@0
|
705 // PyModule_AddObject(m, "realtime", (PyObject *)&RealTime_Type);
|
Chris@0
|
706
|
Chris@0
|
707 }
|