Chris@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@26
|
3 /*
|
Chris@26
|
4 VampyHost
|
Chris@26
|
5
|
Chris@26
|
6 Use Vamp audio analysis plugins in Python
|
Chris@26
|
7
|
Chris@26
|
8 Gyorgy Fazekas and Chris Cannam
|
Chris@26
|
9 Centre for Digital Music, Queen Mary, University of London
|
Chris@26
|
10 Copyright 2008-2014 Queen Mary, University of London
|
Chris@26
|
11
|
Chris@26
|
12 Permission is hereby granted, free of charge, to any person
|
Chris@26
|
13 obtaining a copy of this software and associated documentation
|
Chris@26
|
14 files (the "Software"), to deal in the Software without
|
Chris@26
|
15 restriction, including without limitation the rights to use, copy,
|
Chris@26
|
16 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@26
|
17 of the Software, and to permit persons to whom the Software is
|
Chris@26
|
18 furnished to do so, subject to the following conditions:
|
Chris@26
|
19
|
Chris@26
|
20 The above copyright notice and this permission notice shall be
|
Chris@26
|
21 included in all copies or substantial portions of the Software.
|
Chris@26
|
22
|
Chris@26
|
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@26
|
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@26
|
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@26
|
26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@26
|
27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@26
|
28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@26
|
29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@26
|
30
|
Chris@26
|
31 Except as contained in this notice, the names of the Centre for
|
Chris@26
|
32 Digital Music; Queen Mary, University of London; and the authors
|
Chris@26
|
33 shall not be used in advertising or otherwise to promote the sale,
|
Chris@26
|
34 use or other dealings in this Software without prior written
|
Chris@26
|
35 authorization.
|
Chris@26
|
36 */
|
Chris@26
|
37
|
Chris@31
|
38 #include "PyPluginObject.h"
|
Chris@14
|
39
|
Chris@14
|
40 // define a unique API pointer
|
Chris@27
|
41 #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API
|
Chris@14
|
42 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
Chris@31
|
43 #define NO_IMPORT_ARRAY
|
Chris@14
|
44 #include "numpy/arrayobject.h"
|
Chris@14
|
45
|
Chris@29
|
46 #include "VectorConversion.h"
|
Chris@16
|
47 #include "PyRealTime.h"
|
Chris@0
|
48
|
Chris@0
|
49 #include <string>
|
Chris@31
|
50 #include <vector>
|
Chris@0
|
51
|
Chris@0
|
52 using namespace std;
|
Chris@0
|
53 using namespace Vamp;
|
Chris@0
|
54
|
Chris@21
|
55 static void
|
Chris@21
|
56 PyPluginObject_dealloc(PyPluginObject *self)
|
Chris@21
|
57 {
|
Chris@21
|
58 cerr << "PyPluginObject_dealloc" << endl;
|
Chris@21
|
59 delete self->plugin;
|
Chris@21
|
60 PyObject_Del(self);
|
Chris@21
|
61 }
|
Chris@21
|
62
|
Chris@2
|
63 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
|
Chris@0
|
64
|
Chris@28
|
65 //!!! todo: conv errors
|
Chris@28
|
66
|
Chris@31
|
67 static
|
Chris@21
|
68 PyPluginObject *
|
Chris@21
|
69 getPluginObject(PyObject *pyPluginHandle)
|
Chris@21
|
70 {
|
Chris@21
|
71 cerr << "getPluginObject" << endl;
|
Chris@0
|
72
|
Chris@21
|
73 PyPluginObject *pd = 0;
|
Chris@21
|
74 if (PyPlugin_Check(pyPluginHandle)) {
|
Chris@21
|
75 pd = (PyPluginObject *)pyPluginHandle;
|
Chris@16
|
76 }
|
Chris@16
|
77 if (!pd || !pd->plugin) {
|
Chris@16
|
78 PyErr_SetString(PyExc_AttributeError,
|
Chris@16
|
79 "Invalid or already deleted plugin handle.");
|
Chris@16
|
80 return 0;
|
Chris@0
|
81 } else {
|
Chris@16
|
82 return pd;
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@31
|
86 PyObject *
|
Chris@31
|
87 PyPluginObject_From_Plugin(Plugin *plugin)
|
Chris@0
|
88 {
|
Chris@31
|
89 PyPluginObject *pd =
|
Chris@31
|
90 (PyPluginObject *)PyType_GenericAlloc(&Plugin_Type, 0);
|
Chris@21
|
91 pd->plugin = plugin;
|
Chris@21
|
92 pd->isInitialised = false;
|
Chris@21
|
93 pd->channels = 0;
|
Chris@21
|
94 pd->blockSize = 0;
|
Chris@21
|
95 pd->stepSize = 0;
|
Chris@21
|
96 return (PyObject *)pd;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 static PyObject *
|
Chris@0
|
100 vampyhost_initialise(PyObject *self, PyObject *args)
|
Chris@0
|
101 {
|
Chris@21
|
102 cerr << "vampyhost_initialise" << endl;
|
Chris@21
|
103
|
luis@7
|
104 size_t channels, blockSize, stepSize;
|
Chris@0
|
105
|
Chris@23
|
106 if (!PyArg_ParseTuple (args, "nnn",
|
luis@7
|
107 (size_t) &channels,
|
luis@7
|
108 (size_t) &stepSize,
|
Chris@23
|
109 (size_t) &blockSize)) {
|
Chris@0
|
110 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
111 "initialise() takes channel count, step size, and block size arguments");
|
Chris@16
|
112 return 0;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@23
|
115 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
116 if (!pd) return 0;
|
Chris@0
|
117
|
Chris@16
|
118 pd->channels = channels;
|
Chris@16
|
119 pd->stepSize = stepSize;
|
Chris@16
|
120 pd->blockSize = blockSize;
|
Chris@0
|
121
|
Chris@16
|
122 if (!pd->plugin->initialise(channels, stepSize, blockSize)) {
|
Chris@17
|
123 cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << " and ADAPT_ALL_SAFE set" << endl;
|
Chris@0
|
124 PyErr_SetString(PyExc_TypeError,
|
Chris@17
|
125 "Plugin initialization failed");
|
Chris@16
|
126 return 0;
|
Chris@6
|
127 }
|
Chris@0
|
128
|
Chris@16
|
129 pd->isInitialised = true;
|
luis@7
|
130
|
Chris@0
|
131 return Py_True;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 static PyObject *
|
Chris@23
|
135 vampyhost_reset(PyObject *self, PyObject *)
|
Chris@18
|
136 {
|
Chris@21
|
137 cerr << "vampyhost_reset" << endl;
|
Chris@21
|
138
|
Chris@23
|
139 PyPluginObject *pd = getPluginObject(self);
|
Chris@18
|
140 if (!pd) return 0;
|
Chris@18
|
141
|
Chris@18
|
142 if (!pd->isInitialised) {
|
Chris@18
|
143 PyErr_SetString(PyExc_StandardError,
|
Chris@18
|
144 "Plugin has not been initialised");
|
Chris@18
|
145 return 0;
|
Chris@18
|
146 }
|
Chris@18
|
147
|
Chris@18
|
148 pd->plugin->reset();
|
Chris@18
|
149 return Py_True;
|
Chris@18
|
150 }
|
Chris@18
|
151
|
Chris@18
|
152 static PyObject *
|
Chris@20
|
153 vampyhost_getParameter(PyObject *self, PyObject *args)
|
Chris@20
|
154 {
|
Chris@21
|
155 cerr << "vampyhost_getParameter" << endl;
|
Chris@21
|
156
|
Chris@20
|
157 PyObject *pyParam;
|
Chris@20
|
158
|
Chris@23
|
159 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@20
|
160 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
161 "getParameter() takes parameter id (string) argument");
|
Chris@20
|
162 return 0; }
|
Chris@20
|
163
|
Chris@23
|
164 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
165 if (!pd) return 0;
|
Chris@20
|
166
|
Chris@20
|
167 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam));
|
Chris@20
|
168 return PyFloat_FromDouble(double(value));
|
Chris@20
|
169 }
|
Chris@20
|
170
|
Chris@20
|
171 static PyObject *
|
Chris@20
|
172 vampyhost_setParameter(PyObject *self, PyObject *args)
|
Chris@20
|
173 {
|
Chris@21
|
174 cerr << "vampyhost_setParameter" << endl;
|
Chris@21
|
175
|
Chris@20
|
176 PyObject *pyParam;
|
Chris@20
|
177 float value;
|
Chris@20
|
178
|
Chris@23
|
179 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
|
Chris@20
|
180 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
181 "setParameter() takes parameter id (string), and value (float) arguments");
|
Chris@20
|
182 return 0; }
|
Chris@20
|
183
|
Chris@23
|
184 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
185 if (!pd) return 0;
|
Chris@20
|
186
|
Chris@20
|
187 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value);
|
Chris@20
|
188 return Py_True;
|
Chris@20
|
189 }
|
Chris@20
|
190
|
Chris@20
|
191 static PyObject *
|
Chris@0
|
192 vampyhost_process(PyObject *self, PyObject *args)
|
Chris@0
|
193 {
|
Chris@21
|
194 cerr << "vampyhost_process" << endl;
|
Chris@21
|
195
|
Chris@0
|
196 PyObject *pyBuffer;
|
Chris@0
|
197 PyObject *pyRealTime;
|
Chris@0
|
198
|
Chris@23
|
199 if (!PyArg_ParseTuple(args, "OO",
|
Chris@0
|
200 &pyBuffer, // Audio data
|
Chris@0
|
201 &pyRealTime)) { // TimeStamp
|
Chris@0
|
202 PyErr_SetString(PyExc_TypeError,
|
Chris@17
|
203 "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments");
|
Chris@16
|
204 return 0; }
|
Chris@0
|
205
|
Chris@0
|
206 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@0
|
207 PyErr_SetString(PyExc_TypeError,"Valid timestamp required.");
|
Chris@16
|
208 return 0; }
|
Chris@0
|
209
|
Chris@17
|
210 if (!PyList_Check(pyBuffer)) {
|
Chris@17
|
211 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input.");
|
Chris@17
|
212 return 0;
|
Chris@17
|
213 }
|
Chris@17
|
214
|
Chris@23
|
215 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
216 if (!pd) return 0;
|
Chris@0
|
217
|
Chris@0
|
218 if (!pd->isInitialised) {
|
Chris@0
|
219 PyErr_SetString(PyExc_StandardError,
|
Chris@0
|
220 "Plugin has not been initialised.");
|
Chris@16
|
221 return 0;
|
Chris@16
|
222 }
|
Chris@0
|
223
|
Chris@12
|
224 int channels = pd->channels;
|
Chris@0
|
225
|
Chris@4
|
226 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@17
|
227 cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
|
Chris@4
|
228 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@16
|
229 return 0;
|
Chris@4
|
230 }
|
Chris@0
|
231
|
Chris@4
|
232 float **inbuf = new float *[channels];
|
Chris@0
|
233
|
Chris@29
|
234 VectorConversion typeConv;
|
Chris@17
|
235
|
Chris@12
|
236 vector<vector<float> > data;
|
Chris@4
|
237 for (int c = 0; c < channels; ++c) {
|
Chris@4
|
238 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@17
|
239 data.push_back(typeConv.PyValue_To_FloatVector(cbuf));
|
Chris@12
|
240 }
|
Chris@12
|
241
|
Chris@12
|
242 for (int c = 0; c < channels; ++c) {
|
Chris@17
|
243 if (data[c].size() != pd->blockSize) {
|
Chris@17
|
244 cerr << "Wrong number of samples on channel " << c << ": expected " << pd->blockSize << " (plugin's block size), got " << data[c].size() << endl;
|
Chris@17
|
245 PyErr_SetString(PyExc_TypeError, "Wrong number of samples");
|
Chris@17
|
246 return 0;
|
Chris@17
|
247 }
|
Chris@12
|
248 inbuf[c] = &data[c][0];
|
Chris@4
|
249 }
|
Chris@0
|
250
|
Chris@12
|
251 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
|
Chris@0
|
252
|
Chris@18
|
253 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
|
Chris@0
|
254
|
Chris@4
|
255 delete[] inbuf;
|
Chris@0
|
256
|
Chris@29
|
257 VectorConversion conv;
|
Chris@18
|
258
|
Chris@18
|
259 PyObject *pyFs = PyDict_New();
|
Chris@0
|
260
|
Chris@18
|
261 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
|
Chris@18
|
262 fsi != fs.end(); ++fsi) {
|
Chris@18
|
263
|
Chris@18
|
264 int fno = fsi->first;
|
Chris@18
|
265 const Plugin::FeatureList &fl = fsi->second;
|
Chris@18
|
266
|
Chris@18
|
267 if (!fl.empty()) {
|
Chris@18
|
268
|
Chris@18
|
269 PyObject *pyFl = PyList_New(fl.size());
|
Chris@18
|
270
|
Chris@18
|
271 for (int fli = 0; fli < (int)fl.size(); ++fli) {
|
Chris@18
|
272
|
Chris@18
|
273 const Plugin::Feature &f = fl[fli];
|
Chris@18
|
274 PyObject *pyF = PyDict_New();
|
Chris@18
|
275
|
Chris@18
|
276 if (f.hasTimestamp) {
|
Chris@18
|
277 PyDict_SetItemString
|
Chris@18
|
278 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
|
Chris@18
|
279 }
|
Chris@18
|
280 if (f.hasDuration) {
|
Chris@18
|
281 PyDict_SetItemString
|
Chris@18
|
282 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
|
Chris@18
|
283 }
|
Chris@18
|
284
|
Chris@18
|
285 PyDict_SetItemString
|
Chris@18
|
286 (pyF, "label", PyString_FromString(f.label.c_str()));
|
Chris@18
|
287
|
Chris@18
|
288 if (!f.values.empty()) {
|
Chris@18
|
289 PyDict_SetItemString
|
Chris@28
|
290 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
|
Chris@18
|
291 }
|
Chris@18
|
292
|
Chris@18
|
293 PyList_SET_ITEM(pyFl, fli, pyF);
|
Chris@18
|
294 }
|
Chris@18
|
295
|
Chris@18
|
296 PyObject *pyN = PyInt_FromLong(fno);
|
Chris@18
|
297 PyDict_SetItem(pyFs, pyN, pyFl);
|
Chris@18
|
298 }
|
Chris@18
|
299 }
|
Chris@18
|
300
|
Chris@18
|
301 return pyFs;
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@23
|
304 static PyObject *
|
Chris@23
|
305 vampyhost_unload(PyObject *self, PyObject *)
|
Chris@23
|
306 {
|
Chris@23
|
307 cerr << "vampyhost_unloadPlugin" << endl;
|
Chris@23
|
308
|
Chris@23
|
309 PyPluginObject *pd = getPluginObject(self);
|
Chris@23
|
310 if (!pd) return 0;
|
Chris@23
|
311
|
Chris@23
|
312 delete pd->plugin;
|
Chris@32
|
313 pd->plugin = 0; // This is checked by getPluginObject, so we avoid
|
Chris@32
|
314 // blowing up if called repeatedly
|
Chris@23
|
315
|
Chris@23
|
316 return Py_True;
|
Chris@23
|
317 }
|
Chris@23
|
318
|
Chris@21
|
319 static PyMethodDef PyPluginObject_methods[] =
|
Chris@21
|
320 {
|
Chris@23
|
321 {"getParameter", vampyhost_getParameter, METH_VARARGS,
|
Chris@23
|
322 xx_foo_doc}, //!!! fix all these!
|
Chris@23
|
323
|
Chris@23
|
324 {"setParameter", vampyhost_setParameter, METH_VARARGS,
|
Chris@23
|
325 xx_foo_doc},
|
Chris@23
|
326
|
Chris@23
|
327 {"initialise", vampyhost_initialise, METH_VARARGS,
|
Chris@23
|
328 xx_foo_doc},
|
Chris@23
|
329
|
Chris@23
|
330 {"reset", vampyhost_reset, METH_NOARGS,
|
Chris@23
|
331 xx_foo_doc},
|
Chris@23
|
332
|
Chris@23
|
333 {"process", vampyhost_process, METH_VARARGS,
|
Chris@23
|
334 xx_foo_doc},
|
Chris@23
|
335
|
Chris@23
|
336 {"unload", vampyhost_unload, METH_NOARGS,
|
Chris@23
|
337 xx_foo_doc},
|
Chris@23
|
338
|
Chris@21
|
339 {0, 0}
|
Chris@21
|
340 };
|
Chris@21
|
341
|
Chris@23
|
342 static int
|
Chris@23
|
343 PyPluginObject_setattr(PyPluginObject *self, char *name, PyObject *value)
|
Chris@23
|
344 {
|
Chris@23
|
345 return -1;
|
Chris@23
|
346 }
|
Chris@23
|
347
|
Chris@23
|
348 static PyObject *
|
Chris@23
|
349 PyPluginObject_getattr(PyPluginObject *self, char *name)
|
Chris@23
|
350 {
|
Chris@23
|
351 return Py_FindMethod(PyPluginObject_methods, (PyObject *)self, name);
|
Chris@23
|
352 }
|
Chris@23
|
353
|
Chris@21
|
354 /* Doc:: 10.3 Type Objects */ /* static */
|
Chris@21
|
355 PyTypeObject Plugin_Type =
|
Chris@21
|
356 {
|
Chris@21
|
357 PyObject_HEAD_INIT(NULL)
|
Chris@21
|
358 0, /*ob_size*/
|
Chris@21
|
359 "vampyhost.Plugin", /*tp_name*/
|
Chris@21
|
360 sizeof(PyPluginObject), /*tp_basicsize*/
|
Chris@21
|
361 0, /*tp_itemsize*/
|
Chris@21
|
362 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
|
Chris@21
|
363 0, /*tp_print*/
|
Chris@23
|
364 (getattrfunc)PyPluginObject_getattr, /*tp_getattr*/
|
Chris@23
|
365 (setattrfunc)PyPluginObject_setattr, /*tp_setattr*/
|
Chris@21
|
366 0, /*tp_compare*/
|
Chris@21
|
367 0, /*tp_repr*/
|
Chris@21
|
368 0, /*tp_as_number*/
|
Chris@21
|
369 0, /*tp_as_sequence*/
|
Chris@21
|
370 0, /*tp_as_mapping*/
|
Chris@21
|
371 0, /*tp_hash*/
|
Chris@21
|
372 0, /*tp_call*/
|
Chris@21
|
373 0, /*tp_str*/
|
Chris@21
|
374 0, /*tp_getattro*/
|
Chris@21
|
375 0, /*tp_setattro*/
|
Chris@21
|
376 0, /*tp_as_buffer*/
|
Chris@21
|
377 Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
Chris@21
|
378 "Plugin Object", /*tp_doc*/
|
Chris@21
|
379 0, /*tp_traverse*/
|
Chris@21
|
380 0, /*tp_clear*/
|
Chris@21
|
381 0, /*tp_richcompare*/
|
Chris@21
|
382 0, /*tp_weaklistoffset*/
|
Chris@21
|
383 0, /*tp_iter*/
|
Chris@21
|
384 0, /*tp_iternext*/
|
Chris@21
|
385 PyPluginObject_methods, /*tp_methods*/
|
Chris@21
|
386 0, /*tp_members*/
|
Chris@21
|
387 0, /*tp_getset*/
|
Chris@21
|
388 0, /*tp_base*/
|
Chris@21
|
389 0, /*tp_dict*/
|
Chris@21
|
390 0, /*tp_descr_get*/
|
Chris@21
|
391 0, /*tp_descr_set*/
|
Chris@21
|
392 0, /*tp_dictoffset*/
|
Chris@21
|
393 0, /*tp_init*/
|
Chris@21
|
394 PyType_GenericAlloc, /*tp_alloc*/
|
Chris@21
|
395 0, /*tp_new*/
|
Chris@21
|
396 PyObject_Del, /*tp_free*/
|
Chris@21
|
397 0, /*tp_is_gc*/
|
Chris@21
|
398 };
|
Chris@0
|
399
|