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