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@33
|
46 #include "structmember.h"
|
Chris@33
|
47
|
Chris@29
|
48 #include "VectorConversion.h"
|
Chris@16
|
49 #include "PyRealTime.h"
|
Chris@0
|
50
|
Chris@0
|
51 #include <string>
|
Chris@31
|
52 #include <vector>
|
Chris@33
|
53 #include <cstddef>
|
Chris@0
|
54
|
Chris@0
|
55 using namespace std;
|
Chris@0
|
56 using namespace Vamp;
|
Chris@0
|
57
|
Chris@2
|
58 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
|
Chris@0
|
59
|
Chris@28
|
60 //!!! todo: conv errors
|
Chris@28
|
61
|
Chris@31
|
62 static
|
Chris@21
|
63 PyPluginObject *
|
Chris@21
|
64 getPluginObject(PyObject *pyPluginHandle)
|
Chris@21
|
65 {
|
Chris@21
|
66 cerr << "getPluginObject" << endl;
|
Chris@0
|
67
|
Chris@21
|
68 PyPluginObject *pd = 0;
|
Chris@21
|
69 if (PyPlugin_Check(pyPluginHandle)) {
|
Chris@21
|
70 pd = (PyPluginObject *)pyPluginHandle;
|
Chris@16
|
71 }
|
Chris@16
|
72 if (!pd || !pd->plugin) {
|
Chris@16
|
73 PyErr_SetString(PyExc_AttributeError,
|
Chris@16
|
74 "Invalid or already deleted plugin handle.");
|
Chris@16
|
75 return 0;
|
Chris@0
|
76 } else {
|
Chris@16
|
77 return pd;
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@31
|
81 PyObject *
|
Chris@31
|
82 PyPluginObject_From_Plugin(Plugin *plugin)
|
Chris@0
|
83 {
|
Chris@31
|
84 PyPluginObject *pd =
|
Chris@31
|
85 (PyPluginObject *)PyType_GenericAlloc(&Plugin_Type, 0);
|
Chris@21
|
86 pd->plugin = plugin;
|
Chris@21
|
87 pd->isInitialised = false;
|
Chris@21
|
88 pd->channels = 0;
|
Chris@21
|
89 pd->blockSize = 0;
|
Chris@21
|
90 pd->stepSize = 0;
|
Chris@33
|
91 pd->apiVersion = plugin->getVampApiVersion();
|
Chris@33
|
92 pd->identifier = strdup(plugin->getIdentifier().c_str());
|
Chris@21
|
93 return (PyObject *)pd;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@33
|
96 static void
|
Chris@33
|
97 PyPluginObject_dealloc(PyPluginObject *self)
|
Chris@33
|
98 {
|
Chris@33
|
99 cerr << "PyPluginObject_dealloc" << endl;
|
Chris@33
|
100 delete self->plugin;
|
Chris@33
|
101 free(self->identifier);
|
Chris@33
|
102 PyObject_Del(self);
|
Chris@33
|
103 }
|
Chris@33
|
104
|
Chris@0
|
105 static PyObject *
|
Chris@0
|
106 vampyhost_initialise(PyObject *self, PyObject *args)
|
Chris@0
|
107 {
|
Chris@21
|
108 cerr << "vampyhost_initialise" << endl;
|
Chris@21
|
109
|
luis@7
|
110 size_t channels, blockSize, stepSize;
|
Chris@0
|
111
|
Chris@23
|
112 if (!PyArg_ParseTuple (args, "nnn",
|
luis@7
|
113 (size_t) &channels,
|
luis@7
|
114 (size_t) &stepSize,
|
Chris@23
|
115 (size_t) &blockSize)) {
|
Chris@0
|
116 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
117 "initialise() takes channel count, step size, and block size arguments");
|
Chris@16
|
118 return 0;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@23
|
121 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
122 if (!pd) return 0;
|
Chris@0
|
123
|
Chris@16
|
124 pd->channels = channels;
|
Chris@16
|
125 pd->stepSize = stepSize;
|
Chris@16
|
126 pd->blockSize = blockSize;
|
Chris@0
|
127
|
Chris@16
|
128 if (!pd->plugin->initialise(channels, stepSize, blockSize)) {
|
Chris@17
|
129 cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << " and ADAPT_ALL_SAFE set" << endl;
|
Chris@0
|
130 PyErr_SetString(PyExc_TypeError,
|
Chris@17
|
131 "Plugin initialization failed");
|
Chris@16
|
132 return 0;
|
Chris@6
|
133 }
|
Chris@0
|
134
|
Chris@16
|
135 pd->isInitialised = true;
|
luis@7
|
136
|
Chris@0
|
137 return Py_True;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 static PyObject *
|
Chris@23
|
141 vampyhost_reset(PyObject *self, PyObject *)
|
Chris@18
|
142 {
|
Chris@21
|
143 cerr << "vampyhost_reset" << endl;
|
Chris@21
|
144
|
Chris@23
|
145 PyPluginObject *pd = getPluginObject(self);
|
Chris@18
|
146 if (!pd) return 0;
|
Chris@18
|
147
|
Chris@18
|
148 if (!pd->isInitialised) {
|
Chris@18
|
149 PyErr_SetString(PyExc_StandardError,
|
Chris@18
|
150 "Plugin has not been initialised");
|
Chris@18
|
151 return 0;
|
Chris@18
|
152 }
|
Chris@18
|
153
|
Chris@18
|
154 pd->plugin->reset();
|
Chris@18
|
155 return Py_True;
|
Chris@18
|
156 }
|
Chris@18
|
157
|
Chris@18
|
158 static PyObject *
|
Chris@20
|
159 vampyhost_getParameter(PyObject *self, PyObject *args)
|
Chris@20
|
160 {
|
Chris@21
|
161 cerr << "vampyhost_getParameter" << endl;
|
Chris@21
|
162
|
Chris@20
|
163 PyObject *pyParam;
|
Chris@20
|
164
|
Chris@23
|
165 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@20
|
166 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
167 "getParameter() takes parameter id (string) argument");
|
Chris@20
|
168 return 0; }
|
Chris@20
|
169
|
Chris@23
|
170 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
171 if (!pd) return 0;
|
Chris@20
|
172
|
Chris@20
|
173 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam));
|
Chris@20
|
174 return PyFloat_FromDouble(double(value));
|
Chris@20
|
175 }
|
Chris@20
|
176
|
Chris@20
|
177 static PyObject *
|
Chris@20
|
178 vampyhost_setParameter(PyObject *self, PyObject *args)
|
Chris@20
|
179 {
|
Chris@21
|
180 cerr << "vampyhost_setParameter" << endl;
|
Chris@21
|
181
|
Chris@20
|
182 PyObject *pyParam;
|
Chris@20
|
183 float value;
|
Chris@20
|
184
|
Chris@23
|
185 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
|
Chris@20
|
186 PyErr_SetString(PyExc_TypeError,
|
Chris@23
|
187 "setParameter() takes parameter id (string), and value (float) arguments");
|
Chris@20
|
188 return 0; }
|
Chris@20
|
189
|
Chris@23
|
190 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
191 if (!pd) return 0;
|
Chris@20
|
192
|
Chris@20
|
193 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value);
|
Chris@20
|
194 return Py_True;
|
Chris@20
|
195 }
|
Chris@20
|
196
|
Chris@20
|
197 static PyObject *
|
Chris@0
|
198 vampyhost_process(PyObject *self, PyObject *args)
|
Chris@0
|
199 {
|
Chris@21
|
200 cerr << "vampyhost_process" << endl;
|
Chris@21
|
201
|
Chris@0
|
202 PyObject *pyBuffer;
|
Chris@0
|
203 PyObject *pyRealTime;
|
Chris@0
|
204
|
Chris@23
|
205 if (!PyArg_ParseTuple(args, "OO",
|
Chris@0
|
206 &pyBuffer, // Audio data
|
Chris@0
|
207 &pyRealTime)) { // TimeStamp
|
Chris@0
|
208 PyErr_SetString(PyExc_TypeError,
|
Chris@17
|
209 "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments");
|
Chris@16
|
210 return 0; }
|
Chris@0
|
211
|
Chris@0
|
212 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@0
|
213 PyErr_SetString(PyExc_TypeError,"Valid timestamp required.");
|
Chris@16
|
214 return 0; }
|
Chris@0
|
215
|
Chris@17
|
216 if (!PyList_Check(pyBuffer)) {
|
Chris@17
|
217 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input.");
|
Chris@17
|
218 return 0;
|
Chris@17
|
219 }
|
Chris@17
|
220
|
Chris@23
|
221 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
222 if (!pd) return 0;
|
Chris@0
|
223
|
Chris@0
|
224 if (!pd->isInitialised) {
|
Chris@0
|
225 PyErr_SetString(PyExc_StandardError,
|
Chris@0
|
226 "Plugin has not been initialised.");
|
Chris@16
|
227 return 0;
|
Chris@16
|
228 }
|
Chris@0
|
229
|
Chris@12
|
230 int channels = pd->channels;
|
Chris@0
|
231
|
Chris@4
|
232 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@17
|
233 cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
|
Chris@4
|
234 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@16
|
235 return 0;
|
Chris@4
|
236 }
|
Chris@0
|
237
|
Chris@4
|
238 float **inbuf = new float *[channels];
|
Chris@0
|
239
|
Chris@29
|
240 VectorConversion typeConv;
|
Chris@17
|
241
|
Chris@12
|
242 vector<vector<float> > data;
|
Chris@4
|
243 for (int c = 0; c < channels; ++c) {
|
Chris@4
|
244 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@17
|
245 data.push_back(typeConv.PyValue_To_FloatVector(cbuf));
|
Chris@12
|
246 }
|
Chris@12
|
247
|
Chris@12
|
248 for (int c = 0; c < channels; ++c) {
|
Chris@17
|
249 if (data[c].size() != pd->blockSize) {
|
Chris@17
|
250 cerr << "Wrong number of samples on channel " << c << ": expected " << pd->blockSize << " (plugin's block size), got " << data[c].size() << endl;
|
Chris@17
|
251 PyErr_SetString(PyExc_TypeError, "Wrong number of samples");
|
Chris@17
|
252 return 0;
|
Chris@17
|
253 }
|
Chris@12
|
254 inbuf[c] = &data[c][0];
|
Chris@4
|
255 }
|
Chris@0
|
256
|
Chris@12
|
257 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
|
Chris@0
|
258
|
Chris@18
|
259 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
|
Chris@0
|
260
|
Chris@4
|
261 delete[] inbuf;
|
Chris@0
|
262
|
Chris@29
|
263 VectorConversion conv;
|
Chris@18
|
264
|
Chris@18
|
265 PyObject *pyFs = PyDict_New();
|
Chris@0
|
266
|
Chris@18
|
267 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
|
Chris@18
|
268 fsi != fs.end(); ++fsi) {
|
Chris@18
|
269
|
Chris@18
|
270 int fno = fsi->first;
|
Chris@18
|
271 const Plugin::FeatureList &fl = fsi->second;
|
Chris@18
|
272
|
Chris@18
|
273 if (!fl.empty()) {
|
Chris@18
|
274
|
Chris@18
|
275 PyObject *pyFl = PyList_New(fl.size());
|
Chris@18
|
276
|
Chris@18
|
277 for (int fli = 0; fli < (int)fl.size(); ++fli) {
|
Chris@18
|
278
|
Chris@18
|
279 const Plugin::Feature &f = fl[fli];
|
Chris@18
|
280 PyObject *pyF = PyDict_New();
|
Chris@18
|
281
|
Chris@18
|
282 if (f.hasTimestamp) {
|
Chris@18
|
283 PyDict_SetItemString
|
Chris@18
|
284 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
|
Chris@18
|
285 }
|
Chris@18
|
286 if (f.hasDuration) {
|
Chris@18
|
287 PyDict_SetItemString
|
Chris@18
|
288 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
|
Chris@18
|
289 }
|
Chris@18
|
290
|
Chris@18
|
291 PyDict_SetItemString
|
Chris@18
|
292 (pyF, "label", PyString_FromString(f.label.c_str()));
|
Chris@18
|
293
|
Chris@18
|
294 if (!f.values.empty()) {
|
Chris@18
|
295 PyDict_SetItemString
|
Chris@28
|
296 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
|
Chris@18
|
297 }
|
Chris@18
|
298
|
Chris@18
|
299 PyList_SET_ITEM(pyFl, fli, pyF);
|
Chris@18
|
300 }
|
Chris@18
|
301
|
Chris@18
|
302 PyObject *pyN = PyInt_FromLong(fno);
|
Chris@18
|
303 PyDict_SetItem(pyFs, pyN, pyFl);
|
Chris@18
|
304 }
|
Chris@18
|
305 }
|
Chris@18
|
306
|
Chris@18
|
307 return pyFs;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@23
|
310 static PyObject *
|
Chris@23
|
311 vampyhost_unload(PyObject *self, PyObject *)
|
Chris@23
|
312 {
|
Chris@23
|
313 cerr << "vampyhost_unloadPlugin" << endl;
|
Chris@23
|
314
|
Chris@23
|
315 PyPluginObject *pd = getPluginObject(self);
|
Chris@23
|
316 if (!pd) return 0;
|
Chris@23
|
317
|
Chris@23
|
318 delete pd->plugin;
|
Chris@32
|
319 pd->plugin = 0; // This is checked by getPluginObject, so we avoid
|
Chris@32
|
320 // blowing up if called repeatedly
|
Chris@23
|
321
|
Chris@23
|
322 return Py_True;
|
Chris@23
|
323 }
|
Chris@23
|
324
|
Chris@33
|
325 static PyMemberDef PyPluginObject_members[] =
|
Chris@33
|
326 {
|
Chris@33
|
327 {(char *)"apiVersion", T_INT, offsetof(PyPluginObject, apiVersion), READONLY,
|
Chris@33
|
328 xx_foo_doc}, //!!! fix all these!
|
Chris@33
|
329
|
Chris@33
|
330 {(char *)"identifier", T_STRING, offsetof(PyPluginObject, identifier), READONLY,
|
Chris@33
|
331 xx_foo_doc}, //!!! fix all these!
|
Chris@33
|
332
|
Chris@33
|
333 {0, 0}
|
Chris@33
|
334 };
|
Chris@33
|
335
|
Chris@21
|
336 static PyMethodDef PyPluginObject_methods[] =
|
Chris@21
|
337 {
|
Chris@23
|
338 {"getParameter", vampyhost_getParameter, METH_VARARGS,
|
Chris@23
|
339 xx_foo_doc}, //!!! fix all these!
|
Chris@23
|
340
|
Chris@23
|
341 {"setParameter", vampyhost_setParameter, METH_VARARGS,
|
Chris@23
|
342 xx_foo_doc},
|
Chris@33
|
343
|
Chris@23
|
344 {"initialise", vampyhost_initialise, METH_VARARGS,
|
Chris@23
|
345 xx_foo_doc},
|
Chris@23
|
346
|
Chris@23
|
347 {"reset", vampyhost_reset, METH_NOARGS,
|
Chris@23
|
348 xx_foo_doc},
|
Chris@23
|
349
|
Chris@23
|
350 {"process", vampyhost_process, METH_VARARGS,
|
Chris@23
|
351 xx_foo_doc},
|
Chris@23
|
352
|
Chris@23
|
353 {"unload", vampyhost_unload, METH_NOARGS,
|
Chris@23
|
354 xx_foo_doc},
|
Chris@23
|
355
|
Chris@21
|
356 {0, 0}
|
Chris@21
|
357 };
|
Chris@21
|
358
|
Chris@21
|
359 /* Doc:: 10.3 Type Objects */ /* static */
|
Chris@21
|
360 PyTypeObject Plugin_Type =
|
Chris@21
|
361 {
|
Chris@21
|
362 PyObject_HEAD_INIT(NULL)
|
Chris@21
|
363 0, /*ob_size*/
|
Chris@21
|
364 "vampyhost.Plugin", /*tp_name*/
|
Chris@21
|
365 sizeof(PyPluginObject), /*tp_basicsize*/
|
Chris@21
|
366 0, /*tp_itemsize*/
|
Chris@21
|
367 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
|
Chris@21
|
368 0, /*tp_print*/
|
Chris@33
|
369 0, /*tp_getattr*/
|
Chris@33
|
370 0, /*tp_setattr*/
|
Chris@21
|
371 0, /*tp_compare*/
|
Chris@21
|
372 0, /*tp_repr*/
|
Chris@21
|
373 0, /*tp_as_number*/
|
Chris@21
|
374 0, /*tp_as_sequence*/
|
Chris@21
|
375 0, /*tp_as_mapping*/
|
Chris@21
|
376 0, /*tp_hash*/
|
Chris@21
|
377 0, /*tp_call*/
|
Chris@21
|
378 0, /*tp_str*/
|
Chris@33
|
379 PyObject_GenericGetAttr, /*tp_getattro*/
|
Chris@33
|
380 PyObject_GenericSetAttr, /*tp_setattro*/
|
Chris@21
|
381 0, /*tp_as_buffer*/
|
Chris@21
|
382 Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
Chris@21
|
383 "Plugin Object", /*tp_doc*/
|
Chris@21
|
384 0, /*tp_traverse*/
|
Chris@21
|
385 0, /*tp_clear*/
|
Chris@21
|
386 0, /*tp_richcompare*/
|
Chris@21
|
387 0, /*tp_weaklistoffset*/
|
Chris@21
|
388 0, /*tp_iter*/
|
Chris@21
|
389 0, /*tp_iternext*/
|
Chris@21
|
390 PyPluginObject_methods, /*tp_methods*/
|
Chris@33
|
391 PyPluginObject_members, /*tp_members*/
|
Chris@21
|
392 0, /*tp_getset*/
|
Chris@21
|
393 0, /*tp_base*/
|
Chris@21
|
394 0, /*tp_dict*/
|
Chris@21
|
395 0, /*tp_descr_get*/
|
Chris@21
|
396 0, /*tp_descr_set*/
|
Chris@21
|
397 0, /*tp_dictoffset*/
|
Chris@21
|
398 0, /*tp_init*/
|
Chris@21
|
399 PyType_GenericAlloc, /*tp_alloc*/
|
Chris@21
|
400 0, /*tp_new*/
|
Chris@21
|
401 PyObject_Del, /*tp_free*/
|
Chris@21
|
402 0, /*tp_is_gc*/
|
Chris@21
|
403 };
|
Chris@0
|
404
|