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