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@48
|
48 #include "FloatConversion.h"
|
Chris@29
|
49 #include "VectorConversion.h"
|
Chris@16
|
50 #include "PyRealTime.h"
|
Chris@0
|
51
|
Chris@0
|
52 #include <string>
|
Chris@31
|
53 #include <vector>
|
Chris@33
|
54 #include <cstddef>
|
Chris@0
|
55
|
Chris@0
|
56 using namespace std;
|
Chris@0
|
57 using namespace Vamp;
|
Chris@0
|
58
|
Chris@31
|
59 static
|
Chris@21
|
60 PyPluginObject *
|
Chris@21
|
61 getPluginObject(PyObject *pyPluginHandle)
|
Chris@21
|
62 {
|
Chris@21
|
63 PyPluginObject *pd = 0;
|
Chris@21
|
64 if (PyPlugin_Check(pyPluginHandle)) {
|
Chris@21
|
65 pd = (PyPluginObject *)pyPluginHandle;
|
Chris@16
|
66 }
|
Chris@16
|
67 if (!pd || !pd->plugin) {
|
Chris@16
|
68 PyErr_SetString(PyExc_AttributeError,
|
Chris@39
|
69 "Invalid or already deleted plugin handle.");
|
Chris@16
|
70 return 0;
|
Chris@0
|
71 } else {
|
Chris@16
|
72 return pd;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@34
|
76 static
|
Chris@34
|
77 PyObject *
|
Chris@34
|
78 pystr(const string &s)
|
Chris@34
|
79 {
|
Chris@34
|
80 return PyString_FromString(s.c_str());
|
Chris@34
|
81 }
|
Chris@34
|
82
|
Chris@31
|
83 PyObject *
|
Chris@31
|
84 PyPluginObject_From_Plugin(Plugin *plugin)
|
Chris@0
|
85 {
|
Chris@31
|
86 PyPluginObject *pd =
|
Chris@31
|
87 (PyPluginObject *)PyType_GenericAlloc(&Plugin_Type, 0);
|
Chris@21
|
88 pd->plugin = plugin;
|
Chris@21
|
89 pd->isInitialised = false;
|
Chris@21
|
90 pd->channels = 0;
|
Chris@21
|
91 pd->blockSize = 0;
|
Chris@21
|
92 pd->stepSize = 0;
|
Chris@34
|
93
|
Chris@34
|
94 PyObject *infodict = PyDict_New();
|
Chris@34
|
95 PyDict_SetItemString
|
Chris@34
|
96 (infodict, "apiVersion", PyInt_FromLong(plugin->getVampApiVersion()));
|
Chris@34
|
97 PyDict_SetItemString
|
Chris@34
|
98 (infodict, "pluginVersion", PyInt_FromLong(plugin->getPluginVersion()));
|
Chris@34
|
99 PyDict_SetItemString
|
Chris@34
|
100 (infodict, "identifier", pystr(plugin->getIdentifier()));
|
Chris@34
|
101 PyDict_SetItemString
|
Chris@34
|
102 (infodict, "name", pystr(plugin->getName()));
|
Chris@34
|
103 PyDict_SetItemString
|
Chris@34
|
104 (infodict, "description", pystr(plugin->getDescription()));
|
Chris@34
|
105 PyDict_SetItemString
|
Chris@34
|
106 (infodict, "maker", pystr(plugin->getMaker()));
|
Chris@34
|
107 PyDict_SetItemString
|
Chris@34
|
108 (infodict, "copyright", pystr(plugin->getCopyright()));
|
Chris@34
|
109 pd->info = infodict;
|
Chris@34
|
110
|
Chris@34
|
111 pd->inputDomain = plugin->getInputDomain();
|
Chris@34
|
112
|
Chris@35
|
113 VectorConversion conv;
|
Chris@35
|
114
|
Chris@34
|
115 Plugin::ParameterList pl = plugin->getParameterDescriptors();
|
Chris@34
|
116 PyObject *params = PyList_New(pl.size());
|
Chris@34
|
117
|
Chris@34
|
118 for (int i = 0; i < (int)pl.size(); ++i) {
|
Chris@34
|
119 PyObject *paramdict = PyDict_New();
|
Chris@34
|
120 PyDict_SetItemString
|
Chris@34
|
121 (paramdict, "identifier", pystr(pl[i].identifier));
|
Chris@34
|
122 PyDict_SetItemString
|
Chris@34
|
123 (paramdict, "name", pystr(pl[i].name));
|
Chris@34
|
124 PyDict_SetItemString
|
Chris@34
|
125 (paramdict, "description", pystr(pl[i].description));
|
Chris@34
|
126 PyDict_SetItemString
|
Chris@34
|
127 (paramdict, "unit", pystr(pl[i].unit));
|
Chris@34
|
128 PyDict_SetItemString
|
Chris@34
|
129 (paramdict, "minValue", PyFloat_FromDouble(pl[i].minValue));
|
Chris@34
|
130 PyDict_SetItemString
|
Chris@34
|
131 (paramdict, "maxValue", PyFloat_FromDouble(pl[i].maxValue));
|
Chris@34
|
132 PyDict_SetItemString
|
Chris@34
|
133 (paramdict, "defaultValue", PyFloat_FromDouble(pl[i].defaultValue));
|
Chris@34
|
134 if (pl[i].isQuantized) {
|
Chris@34
|
135 PyDict_SetItemString
|
Chris@34
|
136 (paramdict, "isQuantized", Py_True);
|
Chris@34
|
137 PyDict_SetItemString
|
Chris@34
|
138 (paramdict, "quantizeStep", PyFloat_FromDouble(pl[i].quantizeStep));
|
Chris@34
|
139 if (!pl[i].valueNames.empty()) {
|
Chris@34
|
140 PyDict_SetItemString
|
Chris@34
|
141 (paramdict, "valueNames", conv.PyValue_From_StringVector(pl[i].valueNames));
|
Chris@34
|
142 }
|
Chris@34
|
143 } else {
|
Chris@34
|
144 PyDict_SetItemString
|
Chris@34
|
145 (paramdict, "isQuantized", Py_False);
|
Chris@34
|
146 }
|
Chris@34
|
147
|
Chris@34
|
148 PyList_SET_ITEM(params, i, paramdict);
|
Chris@34
|
149 }
|
Chris@34
|
150
|
Chris@34
|
151 pd->parameters = params;
|
Chris@39
|
152
|
Chris@39
|
153 Plugin::ProgramList prl = plugin->getPrograms();
|
Chris@39
|
154 PyObject *progs = PyList_New(prl.size());
|
Chris@39
|
155
|
Chris@39
|
156 for (int i = 0; i < (int)prl.size(); ++i) {
|
Chris@39
|
157 PyList_SET_ITEM(progs, i, pystr(prl[i]));
|
Chris@39
|
158 }
|
Chris@39
|
159
|
Chris@39
|
160 pd->programs = progs;
|
Chris@37
|
161
|
Chris@37
|
162 return (PyObject *)pd;
|
Chris@37
|
163 }
|
Chris@35
|
164
|
Chris@37
|
165 static void
|
Chris@37
|
166 PyPluginObject_dealloc(PyPluginObject *self)
|
Chris@37
|
167 {
|
Chris@37
|
168 delete self->plugin;
|
Chris@37
|
169 PyObject_Del(self);
|
Chris@37
|
170 }
|
Chris@37
|
171
|
Chris@37
|
172 static PyObject *
|
Chris@37
|
173 getOutputs(PyObject *self, PyObject *args)
|
Chris@37
|
174 {
|
Chris@37
|
175 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
176 if (!pd) return 0;
|
Chris@37
|
177
|
Chris@37
|
178 Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
|
Chris@35
|
179 PyObject *outputs = PyList_New(ol.size());
|
Chris@35
|
180
|
Chris@35
|
181 for (int i = 0; i < (int)ol.size(); ++i) {
|
Chris@35
|
182 PyObject *outdict = PyDict_New();
|
Chris@35
|
183 PyDict_SetItemString
|
Chris@35
|
184 (outdict, "identifier", pystr(ol[i].identifier));
|
Chris@35
|
185 PyDict_SetItemString
|
Chris@35
|
186 (outdict, "name", pystr(ol[i].name));
|
Chris@35
|
187 PyDict_SetItemString
|
Chris@35
|
188 (outdict, "description", pystr(ol[i].description));
|
Chris@35
|
189 PyDict_SetItemString
|
Chris@35
|
190 (outdict, "binCount", PyInt_FromLong(ol[i].binCount));
|
Chris@35
|
191 if (ol[i].binCount > 0) {
|
Chris@35
|
192 if (ol[i].hasKnownExtents) {
|
Chris@35
|
193 PyDict_SetItemString
|
Chris@35
|
194 (outdict, "hasKnownExtents", Py_True);
|
Chris@35
|
195 PyDict_SetItemString
|
Chris@35
|
196 (outdict, "minValue", PyFloat_FromDouble(ol[i].minValue));
|
Chris@35
|
197 PyDict_SetItemString
|
Chris@35
|
198 (outdict, "maxValue", PyFloat_FromDouble(ol[i].maxValue));
|
Chris@35
|
199 } else {
|
Chris@35
|
200 PyDict_SetItemString
|
Chris@35
|
201 (outdict, "hasKnownExtents", Py_False);
|
Chris@35
|
202 }
|
Chris@35
|
203 if (ol[i].isQuantized) {
|
Chris@35
|
204 PyDict_SetItemString
|
Chris@35
|
205 (outdict, "isQuantized", Py_True);
|
Chris@35
|
206 PyDict_SetItemString
|
Chris@35
|
207 (outdict, "quantizeStep", PyFloat_FromDouble(ol[i].quantizeStep));
|
Chris@35
|
208 } else {
|
Chris@35
|
209 PyDict_SetItemString
|
Chris@35
|
210 (outdict, "isQuantized", Py_False);
|
Chris@35
|
211 }
|
Chris@35
|
212 }
|
Chris@35
|
213 PyDict_SetItemString
|
Chris@35
|
214 (outdict, "sampleType", PyInt_FromLong((int)ol[i].sampleType));
|
Chris@35
|
215 PyDict_SetItemString
|
Chris@35
|
216 (outdict, "sampleRate", PyFloat_FromDouble(ol[i].sampleRate));
|
Chris@35
|
217 PyDict_SetItemString
|
Chris@35
|
218 (outdict, "hasDuration", ol[i].hasDuration ? Py_True : Py_False);
|
Chris@35
|
219
|
Chris@35
|
220 PyList_SET_ITEM(outputs, i, outdict);
|
Chris@35
|
221 }
|
Chris@35
|
222
|
Chris@37
|
223 return outputs;
|
Chris@33
|
224 }
|
Chris@33
|
225
|
Chris@0
|
226 static PyObject *
|
Chris@39
|
227 initialise(PyObject *self, PyObject *args)
|
Chris@0
|
228 {
|
luis@7
|
229 size_t channels, blockSize, stepSize;
|
Chris@0
|
230
|
Chris@23
|
231 if (!PyArg_ParseTuple (args, "nnn",
|
Chris@39
|
232 (size_t) &channels,
|
Chris@39
|
233 (size_t) &stepSize,
|
Chris@39
|
234 (size_t) &blockSize)) {
|
Chris@39
|
235 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
236 "initialise() takes channel count, step size, and block size arguments");
|
Chris@39
|
237 return 0;
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@23
|
240 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
241 if (!pd) return 0;
|
Chris@0
|
242
|
Chris@16
|
243 pd->channels = channels;
|
Chris@16
|
244 pd->stepSize = stepSize;
|
Chris@16
|
245 pd->blockSize = blockSize;
|
Chris@0
|
246
|
Chris@16
|
247 if (!pd->plugin->initialise(channels, stepSize, blockSize)) {
|
Chris@39
|
248 cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << endl;
|
Chris@39
|
249 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
250 "Plugin initialization failed");
|
Chris@39
|
251 return 0;
|
Chris@6
|
252 }
|
Chris@0
|
253
|
Chris@16
|
254 pd->isInitialised = true;
|
luis@7
|
255
|
Chris@0
|
256 return Py_True;
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 static PyObject *
|
Chris@39
|
260 reset(PyObject *self, PyObject *)
|
Chris@18
|
261 {
|
Chris@23
|
262 PyPluginObject *pd = getPluginObject(self);
|
Chris@18
|
263 if (!pd) return 0;
|
Chris@18
|
264
|
Chris@18
|
265 if (!pd->isInitialised) {
|
Chris@18
|
266 PyErr_SetString(PyExc_StandardError,
|
Chris@18
|
267 "Plugin has not been initialised");
|
Chris@18
|
268 return 0;
|
Chris@18
|
269 }
|
Chris@18
|
270
|
Chris@18
|
271 pd->plugin->reset();
|
Chris@18
|
272 return Py_True;
|
Chris@18
|
273 }
|
Chris@18
|
274
|
Chris@18
|
275 static PyObject *
|
Chris@48
|
276 getParameterValue(PyObject *self, PyObject *args)
|
Chris@20
|
277 {
|
Chris@20
|
278 PyObject *pyParam;
|
Chris@20
|
279
|
Chris@23
|
280 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
281 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
282 "getParameterValue() takes parameter id (string) argument");
|
Chris@39
|
283 return 0; }
|
Chris@20
|
284
|
Chris@23
|
285 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
286 if (!pd) return 0;
|
Chris@20
|
287
|
Chris@20
|
288 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam));
|
Chris@20
|
289 return PyFloat_FromDouble(double(value));
|
Chris@20
|
290 }
|
Chris@20
|
291
|
Chris@20
|
292 static PyObject *
|
Chris@48
|
293 setParameterValue(PyObject *self, PyObject *args)
|
Chris@20
|
294 {
|
Chris@20
|
295 PyObject *pyParam;
|
Chris@20
|
296 float value;
|
Chris@20
|
297
|
Chris@23
|
298 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
|
Chris@39
|
299 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
300 "setParameterValue() takes parameter id (string), and value (float) arguments");
|
Chris@39
|
301 return 0; }
|
Chris@20
|
302
|
Chris@23
|
303 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
304 if (!pd) return 0;
|
Chris@20
|
305
|
Chris@20
|
306 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value);
|
Chris@20
|
307 return Py_True;
|
Chris@20
|
308 }
|
Chris@20
|
309
|
Chris@39
|
310 static PyObject *
|
Chris@48
|
311 setParameterValues(PyObject *self, PyObject *args)
|
Chris@48
|
312 {
|
Chris@48
|
313 PyObject *dict;
|
Chris@48
|
314
|
Chris@48
|
315 if (!PyArg_ParseTuple(args, "O", &dict)) {
|
Chris@48
|
316 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
317 "setParameterValues() takes dict argument");
|
Chris@48
|
318 return 0; }
|
Chris@48
|
319
|
Chris@48
|
320 if (!PyDict_Check(dict)) {
|
Chris@48
|
321 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
322 "setParameterValues() takes dict argument");
|
Chris@48
|
323 return 0; }
|
Chris@48
|
324
|
Chris@48
|
325 PyPluginObject *pd = getPluginObject(self);
|
Chris@48
|
326 if (!pd) return 0;
|
Chris@48
|
327
|
Chris@48
|
328 Py_ssize_t pos = 0;
|
Chris@48
|
329 PyObject *key, *value;
|
Chris@48
|
330 while (PyDict_Next(dict, &pos, &key, &value)) {
|
Chris@48
|
331 if (!key || !PyString_CheckExact(key)) {
|
Chris@48
|
332 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
333 "Parameter dict keys must all have string type");
|
Chris@48
|
334 return 0;
|
Chris@48
|
335 }
|
Chris@48
|
336 if (!value || !FloatConversion::check(value)) {
|
Chris@48
|
337 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
338 "Parameter dict values must be convertible to float");
|
Chris@48
|
339 return 0;
|
Chris@48
|
340 }
|
Chris@48
|
341 pd->plugin->setParameter(PyString_AS_STRING(key),
|
Chris@48
|
342 FloatConversion::convert(value));
|
Chris@48
|
343 }
|
Chris@48
|
344
|
Chris@48
|
345 return Py_True;
|
Chris@48
|
346 }
|
Chris@48
|
347
|
Chris@48
|
348 static PyObject *
|
Chris@39
|
349 selectProgram(PyObject *self, PyObject *args)
|
Chris@39
|
350 {
|
Chris@39
|
351 PyObject *pyParam;
|
Chris@39
|
352
|
Chris@39
|
353 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
354 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
355 "selectProgram() takes parameter id (string) argument");
|
Chris@39
|
356 return 0; }
|
Chris@39
|
357
|
Chris@39
|
358 PyPluginObject *pd = getPluginObject(self);
|
Chris@39
|
359 if (!pd) return 0;
|
Chris@39
|
360
|
Chris@39
|
361 pd->plugin->selectProgram(PyString_AS_STRING(pyParam));
|
Chris@39
|
362 return Py_True;
|
Chris@39
|
363 }
|
Chris@39
|
364
|
Chris@35
|
365 static
|
Chris@35
|
366 PyObject *
|
Chris@35
|
367 convertFeatureSet(const Plugin::FeatureSet &fs)
|
Chris@35
|
368 {
|
Chris@35
|
369 VectorConversion conv;
|
Chris@35
|
370
|
Chris@35
|
371 PyObject *pyFs = PyDict_New();
|
Chris@35
|
372
|
Chris@35
|
373 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
|
Chris@35
|
374 fsi != fs.end(); ++fsi) {
|
Chris@35
|
375
|
Chris@35
|
376 int fno = fsi->first;
|
Chris@35
|
377 const Plugin::FeatureList &fl = fsi->second;
|
Chris@35
|
378
|
Chris@35
|
379 if (!fl.empty()) {
|
Chris@35
|
380
|
Chris@35
|
381 PyObject *pyFl = PyList_New(fl.size());
|
Chris@35
|
382
|
Chris@35
|
383 for (int fli = 0; fli < (int)fl.size(); ++fli) {
|
Chris@35
|
384
|
Chris@35
|
385 const Plugin::Feature &f = fl[fli];
|
Chris@35
|
386 PyObject *pyF = PyDict_New();
|
Chris@35
|
387
|
Chris@35
|
388 if (f.hasTimestamp) {
|
Chris@35
|
389 PyDict_SetItemString
|
Chris@35
|
390 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
|
Chris@35
|
391 }
|
Chris@35
|
392 if (f.hasDuration) {
|
Chris@35
|
393 PyDict_SetItemString
|
Chris@35
|
394 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
|
Chris@35
|
395 }
|
Chris@35
|
396
|
Chris@35
|
397 PyDict_SetItemString
|
Chris@35
|
398 (pyF, "label", pystr(f.label));
|
Chris@35
|
399
|
Chris@35
|
400 if (!f.values.empty()) {
|
Chris@35
|
401 PyDict_SetItemString
|
Chris@35
|
402 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
|
Chris@35
|
403 }
|
Chris@35
|
404
|
Chris@35
|
405 PyList_SET_ITEM(pyFl, fli, pyF);
|
Chris@35
|
406 }
|
Chris@35
|
407
|
Chris@35
|
408 PyObject *pyN = PyInt_FromLong(fno);
|
Chris@35
|
409 PyDict_SetItem(pyFs, pyN, pyFl);
|
Chris@35
|
410 }
|
Chris@35
|
411 }
|
Chris@35
|
412
|
Chris@35
|
413 return pyFs;
|
Chris@35
|
414 }
|
Chris@35
|
415
|
Chris@40
|
416 static vector<vector<float> >
|
Chris@40
|
417 convertPluginInput(PyObject *pyBuffer, int channels, int blockSize)
|
Chris@40
|
418 {
|
Chris@40
|
419 vector<vector<float> > data;
|
Chris@40
|
420
|
Chris@40
|
421 VectorConversion conv;
|
Chris@40
|
422
|
Chris@40
|
423 if (PyArray_CheckExact(pyBuffer)) {
|
Chris@40
|
424
|
Chris@40
|
425 data = conv.Py2DArray_To_FloatVector(pyBuffer);
|
Chris@40
|
426
|
Chris@40
|
427 if (conv.error) {
|
Chris@40
|
428 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@40
|
429 return data;
|
Chris@40
|
430 }
|
Chris@40
|
431
|
Chris@41
|
432 if ((int)data.size() != channels) {
|
Chris@41
|
433 // cerr << "Wrong number of channels: got " << data.size() << ", expected " << channels << endl;
|
Chris@41
|
434 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@41
|
435 return vector<vector<float> >();
|
Chris@41
|
436 }
|
Chris@41
|
437
|
Chris@40
|
438 } else {
|
Chris@40
|
439
|
Chris@40
|
440 if (!PyList_Check(pyBuffer)) {
|
Chris@43
|
441 PyErr_SetString(PyExc_TypeError, "List of NumPy arrays or lists of numbers required for process input");
|
Chris@40
|
442 return data;
|
Chris@40
|
443 }
|
Chris@43
|
444
|
Chris@40
|
445 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@41
|
446 // cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
|
Chris@40
|
447 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@40
|
448 return data;
|
Chris@40
|
449 }
|
Chris@40
|
450
|
Chris@40
|
451 for (int c = 0; c < channels; ++c) {
|
Chris@40
|
452 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@40
|
453 data.push_back(conv.PyValue_To_FloatVector(cbuf));
|
Chris@43
|
454 if (conv.error) {
|
Chris@43
|
455 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@43
|
456 return vector<vector<float> >();
|
Chris@43
|
457 }
|
Chris@40
|
458 }
|
Chris@41
|
459 }
|
Chris@40
|
460
|
Chris@41
|
461 for (int c = 0; c < channels; ++c) {
|
Chris@41
|
462 if ((int)data[c].size() != blockSize) {
|
Chris@41
|
463 // cerr << "Wrong number of samples on channel " << c << ": expected " << blockSize << " (plugin's block size), got " << data[c].size() << endl;
|
Chris@46
|
464 PyErr_SetString(PyExc_TypeError, "Wrong number of samples for process block");
|
Chris@41
|
465 return vector<vector<float> >();
|
Chris@40
|
466 }
|
Chris@40
|
467 }
|
Chris@40
|
468
|
Chris@40
|
469 return data;
|
Chris@40
|
470 }
|
Chris@40
|
471
|
Chris@20
|
472 static PyObject *
|
Chris@39
|
473 process(PyObject *self, PyObject *args)
|
Chris@0
|
474 {
|
Chris@0
|
475 PyObject *pyBuffer;
|
Chris@0
|
476 PyObject *pyRealTime;
|
Chris@0
|
477
|
Chris@23
|
478 if (!PyArg_ParseTuple(args, "OO",
|
Chris@39
|
479 &pyBuffer, // Audio data
|
Chris@39
|
480 &pyRealTime)) { // TimeStamp
|
Chris@39
|
481 PyErr_SetString(PyExc_TypeError,
|
Chris@47
|
482 "process() takes buffer (2D array or list of arrays, one row per channel) and timestamp (RealTime) arguments");
|
Chris@39
|
483 return 0; }
|
Chris@0
|
484
|
Chris@0
|
485 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@40
|
486 PyErr_SetString(PyExc_TypeError, "Valid timestamp required.");
|
Chris@39
|
487 return 0; }
|
Chris@0
|
488
|
Chris@23
|
489 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
490 if (!pd) return 0;
|
Chris@0
|
491
|
Chris@0
|
492 if (!pd->isInitialised) {
|
Chris@39
|
493 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
494 "Plugin has not been initialised.");
|
Chris@39
|
495 return 0;
|
Chris@16
|
496 }
|
Chris@0
|
497
|
Chris@40
|
498 int channels = pd->channels;
|
Chris@40
|
499 vector<vector<float> > data =
|
Chris@40
|
500 convertPluginInput(pyBuffer, channels, pd->blockSize);
|
Chris@40
|
501 if (data.empty()) return 0;
|
Chris@0
|
502
|
Chris@4
|
503 float **inbuf = new float *[channels];
|
Chris@4
|
504 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
505 inbuf[c] = &data[c][0];
|
Chris@4
|
506 }
|
Chris@12
|
507 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
|
Chris@18
|
508 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
|
Chris@4
|
509 delete[] inbuf;
|
Chris@0
|
510
|
Chris@35
|
511 return convertFeatureSet(fs);
|
Chris@35
|
512 }
|
Chris@0
|
513
|
Chris@35
|
514 static PyObject *
|
Chris@39
|
515 getRemainingFeatures(PyObject *self, PyObject *)
|
Chris@35
|
516 {
|
Chris@35
|
517 PyPluginObject *pd = getPluginObject(self);
|
Chris@35
|
518 if (!pd) return 0;
|
Chris@18
|
519
|
Chris@35
|
520 if (!pd->isInitialised) {
|
Chris@39
|
521 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
522 "Plugin has not been initialised.");
|
Chris@39
|
523 return 0;
|
Chris@35
|
524 }
|
Chris@18
|
525
|
Chris@35
|
526 Plugin::FeatureSet fs = pd->plugin->getRemainingFeatures();
|
Chris@18
|
527
|
Chris@35
|
528 return convertFeatureSet(fs);
|
Chris@0
|
529 }
|
Chris@0
|
530
|
Chris@23
|
531 static PyObject *
|
Chris@39
|
532 getPreferredBlockSize(PyObject *self, PyObject *)
|
Chris@37
|
533 {
|
Chris@37
|
534 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
535 if (!pd) return 0;
|
Chris@37
|
536 return PyInt_FromLong(pd->plugin->getPreferredBlockSize());
|
Chris@37
|
537 }
|
Chris@37
|
538
|
Chris@37
|
539 static PyObject *
|
Chris@39
|
540 getPreferredStepSize(PyObject *self, PyObject *)
|
Chris@37
|
541 {
|
Chris@37
|
542 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
543 if (!pd) return 0;
|
Chris@37
|
544 return PyInt_FromLong(pd->plugin->getPreferredStepSize());
|
Chris@37
|
545 }
|
Chris@37
|
546
|
Chris@37
|
547 static PyObject *
|
Chris@39
|
548 getMinChannelCount(PyObject *self, PyObject *)
|
Chris@37
|
549 {
|
Chris@37
|
550 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
551 if (!pd) return 0;
|
Chris@37
|
552 return PyInt_FromLong(pd->plugin->getMinChannelCount());
|
Chris@37
|
553 }
|
Chris@37
|
554
|
Chris@37
|
555 static PyObject *
|
Chris@39
|
556 getMaxChannelCount(PyObject *self, PyObject *)
|
Chris@37
|
557 {
|
Chris@37
|
558 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
559 if (!pd) return 0;
|
Chris@37
|
560 return PyInt_FromLong(pd->plugin->getMaxChannelCount());
|
Chris@37
|
561 }
|
Chris@37
|
562
|
Chris@37
|
563 static PyObject *
|
Chris@39
|
564 unload(PyObject *self, PyObject *)
|
Chris@23
|
565 {
|
Chris@23
|
566 PyPluginObject *pd = getPluginObject(self);
|
Chris@23
|
567 if (!pd) return 0;
|
Chris@23
|
568
|
Chris@23
|
569 delete pd->plugin;
|
Chris@32
|
570 pd->plugin = 0; // This is checked by getPluginObject, so we avoid
|
Chris@32
|
571 // blowing up if called repeatedly
|
Chris@23
|
572
|
Chris@23
|
573 return Py_True;
|
Chris@23
|
574 }
|
Chris@23
|
575
|
Chris@33
|
576 static PyMemberDef PyPluginObject_members[] =
|
Chris@33
|
577 {
|
Chris@34
|
578 {(char *)"info", T_OBJECT, offsetof(PyPluginObject, info), READONLY,
|
Chris@39
|
579 (char *)"info -> A read-only dictionary of plugin metadata."},
|
Chris@34
|
580
|
Chris@34
|
581 {(char *)"inputDomain", T_INT, offsetof(PyPluginObject, inputDomain), READONLY,
|
Chris@39
|
582 (char *)"inputDomain -> The format of input audio required by the plugin, either vampyhost.TimeDomain or vampyhost.FrequencyDomain."},
|
Chris@34
|
583
|
Chris@34
|
584 {(char *)"parameters", T_OBJECT, offsetof(PyPluginObject, parameters), READONLY,
|
Chris@39
|
585 (char *)"parameters -> A list of metadata dictionaries describing the plugin's configurable parameters."},
|
Chris@39
|
586
|
Chris@39
|
587 {(char *)"programs", T_OBJECT, offsetof(PyPluginObject, programs), READONLY,
|
Chris@39
|
588 (char *)"programs -> A list of the programs available for this plugin, if any."},
|
Chris@33
|
589
|
Chris@33
|
590 {0, 0}
|
Chris@33
|
591 };
|
Chris@33
|
592
|
Chris@21
|
593 static PyMethodDef PyPluginObject_methods[] =
|
Chris@21
|
594 {
|
Chris@37
|
595 {"getOutputs", getOutputs, METH_NOARGS,
|
Chris@39
|
596 "getOutputs() -> Obtain the output descriptors for all of the plugin's outputs."},
|
Chris@37
|
597
|
Chris@48
|
598 {"getParameterValue", getParameterValue, METH_VARARGS,
|
Chris@39
|
599 "getParameterValue(identifier) -> Return the value of the parameter with the given identifier."},
|
Chris@23
|
600
|
Chris@48
|
601 {"setParameterValue", setParameterValue, METH_VARARGS,
|
Chris@39
|
602 "setParameterValue(identifier, value) -> Set the parameter with the given identifier to the given value."},
|
Chris@37
|
603
|
Chris@48
|
604 {"setParameterValues", setParameterValues, METH_VARARGS,
|
Chris@48
|
605 "setParameterValues(dict) -> Set multiple parameters to values corresponding to the key/value pairs in the dict. Any parameters not mentioned in the dict are unchanged."},
|
Chris@48
|
606
|
Chris@39
|
607 {"selectProgram", selectProgram, METH_VARARGS,
|
Chris@39
|
608 "selectProgram(name) -> Select the processing program with the given name."},
|
Chris@39
|
609
|
Chris@39
|
610 {"getPreferredBlockSize", getPreferredBlockSize, METH_VARARGS,
|
Chris@39
|
611 "getPreferredBlockSize() -> Return the plugin's preferred processing block size, or 0 if the plugin accepts any block size."},
|
Chris@37
|
612
|
Chris@39
|
613 {"getPreferredStepSize", getPreferredStepSize, METH_VARARGS,
|
Chris@39
|
614 "getPreferredStepSize() -> Return the plugin's preferred processing step size, or 0 if the plugin allows the host to select. If this is 0, the host should normally choose the same step as block size for time-domain plugins, or half the block size for frequency-domain plugins."},
|
Chris@37
|
615
|
Chris@39
|
616 {"getMinChannelCount", getMinChannelCount, METH_VARARGS,
|
Chris@39
|
617 "getMinChannelCount() -> Return the minimum number of channels of audio data the plugin accepts as input."},
|
Chris@37
|
618
|
Chris@39
|
619 {"getMaxChannelCount", getMaxChannelCount, METH_VARARGS,
|
Chris@39
|
620 "getMaxChannelCount() -> Return the maximum number of channels of audio data the plugin accepts as input."},
|
Chris@33
|
621
|
Chris@39
|
622 {"initialise", initialise, METH_VARARGS,
|
Chris@39
|
623 "initialise(channels, stepSize, blockSize) -> Initialise the plugin for the given number of channels and processing frame sizes. This must be called before process() can be used."},
|
Chris@23
|
624
|
Chris@39
|
625 {"reset", reset, METH_NOARGS,
|
Chris@39
|
626 "reset() -> Reset the plugin after processing, to prepare for another processing run with the same parameters."},
|
Chris@23
|
627
|
Chris@39
|
628 {"process", process, METH_VARARGS,
|
Chris@39
|
629 "process(block, timestamp) -> Provide one processing frame to the plugin, with its timestamp, and obtain any features that were extracted immediately from this frame."},
|
Chris@23
|
630
|
Chris@39
|
631 {"getRemainingFeatures", getRemainingFeatures, METH_NOARGS,
|
Chris@39
|
632 "getRemainingFeatures() -> Obtain any features extracted at the end of processing."},
|
Chris@35
|
633
|
Chris@39
|
634 {"unload", unload, METH_NOARGS,
|
Chris@39
|
635 "unload() -> Dispose of the plugin. You cannot use the plugin object again after calling this. Note that unloading also happens automatically when the plugin object's reference count reaches zero; this function is only necessary if you wish to ensure the native part of the plugin is disposed of before then."},
|
Chris@23
|
636
|
Chris@21
|
637 {0, 0}
|
Chris@21
|
638 };
|
Chris@21
|
639
|
Chris@21
|
640 /* Doc:: 10.3 Type Objects */ /* static */
|
Chris@21
|
641 PyTypeObject Plugin_Type =
|
Chris@21
|
642 {
|
Chris@21
|
643 PyObject_HEAD_INIT(NULL)
|
Chris@39
|
644 0, /*ob_size*/
|
Chris@39
|
645 "vampyhost.Plugin", /*tp_name*/
|
Chris@39
|
646 sizeof(PyPluginObject), /*tp_basicsize*/
|
Chris@39
|
647 0, /*tp_itemsize*/
|
Chris@21
|
648 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
|
Chris@39
|
649 0, /*tp_print*/
|
Chris@39
|
650 0, /*tp_getattr*/
|
Chris@39
|
651 0, /*tp_setattr*/
|
Chris@39
|
652 0, /*tp_compare*/
|
Chris@39
|
653 0, /*tp_repr*/
|
Chris@39
|
654 0, /*tp_as_number*/
|
Chris@39
|
655 0, /*tp_as_sequence*/
|
Chris@39
|
656 0, /*tp_as_mapping*/
|
Chris@39
|
657 0, /*tp_hash*/
|
Chris@39
|
658 0, /*tp_call*/
|
Chris@39
|
659 0, /*tp_str*/
|
Chris@39
|
660 PyObject_GenericGetAttr, /*tp_getattro*/
|
Chris@39
|
661 PyObject_GenericSetAttr, /*tp_setattro*/
|
Chris@39
|
662 0, /*tp_as_buffer*/
|
Chris@39
|
663 Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
Chris@40
|
664 "Plugin object, providing a low-level API for running a Vamp plugin.", /*tp_doc*/
|
Chris@39
|
665 0, /*tp_traverse*/
|
Chris@39
|
666 0, /*tp_clear*/
|
Chris@39
|
667 0, /*tp_richcompare*/
|
Chris@39
|
668 0, /*tp_weaklistoffset*/
|
Chris@39
|
669 0, /*tp_iter*/
|
Chris@39
|
670 0, /*tp_iternext*/
|
Chris@39
|
671 PyPluginObject_methods, /*tp_methods*/
|
Chris@39
|
672 PyPluginObject_members, /*tp_members*/
|
Chris@39
|
673 0, /*tp_getset*/
|
Chris@39
|
674 0, /*tp_base*/
|
Chris@39
|
675 0, /*tp_dict*/
|
Chris@39
|
676 0, /*tp_descr_get*/
|
Chris@39
|
677 0, /*tp_descr_set*/
|
Chris@39
|
678 0, /*tp_dictoffset*/
|
Chris@39
|
679 0, /*tp_init*/
|
Chris@39
|
680 PyType_GenericAlloc, /*tp_alloc*/
|
Chris@39
|
681 0, /*tp_new*/
|
Chris@39
|
682 PyObject_Del, /*tp_free*/
|
Chris@39
|
683 0, /*tp_is_gc*/
|
Chris@21
|
684 };
|
Chris@0
|
685
|