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@49
|
55 #include <set>
|
Chris@0
|
56
|
Chris@0
|
57 using namespace std;
|
Chris@0
|
58 using namespace Vamp;
|
Chris@0
|
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@49
|
276 static bool
|
Chris@49
|
277 hasParameter(PyPluginObject *pd, string id)
|
Chris@49
|
278 {
|
Chris@49
|
279 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
|
Chris@49
|
280 for (int i = 0; i < (int)pl.size(); ++i) {
|
Chris@49
|
281 if (pl[i].identifier == id) {
|
Chris@49
|
282 return true;
|
Chris@49
|
283 }
|
Chris@49
|
284 }
|
Chris@49
|
285 return false;
|
Chris@49
|
286 }
|
Chris@49
|
287
|
Chris@18
|
288 static PyObject *
|
Chris@48
|
289 getParameterValue(PyObject *self, PyObject *args)
|
Chris@20
|
290 {
|
Chris@20
|
291 PyObject *pyParam;
|
Chris@20
|
292
|
Chris@23
|
293 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
294 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
295 "getParameterValue() takes parameter id (string) argument");
|
Chris@39
|
296 return 0; }
|
Chris@20
|
297
|
Chris@23
|
298 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
299 if (!pd) return 0;
|
Chris@20
|
300
|
Chris@49
|
301 string param = PyString_AS_STRING(pyParam);
|
Chris@49
|
302
|
Chris@49
|
303 if (!hasParameter(pd, param)) {
|
Chris@49
|
304 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
305 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
306 return 0;
|
Chris@49
|
307 }
|
Chris@49
|
308
|
Chris@49
|
309 float value = pd->plugin->getParameter(param);
|
Chris@20
|
310 return PyFloat_FromDouble(double(value));
|
Chris@20
|
311 }
|
Chris@20
|
312
|
Chris@20
|
313 static PyObject *
|
Chris@48
|
314 setParameterValue(PyObject *self, PyObject *args)
|
Chris@20
|
315 {
|
Chris@20
|
316 PyObject *pyParam;
|
Chris@20
|
317 float value;
|
Chris@20
|
318
|
Chris@23
|
319 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
|
Chris@39
|
320 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
321 "setParameterValue() takes parameter id (string), and value (float) arguments");
|
Chris@39
|
322 return 0; }
|
Chris@20
|
323
|
Chris@23
|
324 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
325 if (!pd) return 0;
|
Chris@20
|
326
|
Chris@49
|
327 string param = PyString_AS_STRING(pyParam);
|
Chris@49
|
328
|
Chris@49
|
329 if (!hasParameter(pd, param)) {
|
Chris@49
|
330 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
331 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
332 return 0;
|
Chris@49
|
333 }
|
Chris@49
|
334
|
Chris@49
|
335 pd->plugin->setParameter(param, value);
|
Chris@20
|
336 return Py_True;
|
Chris@20
|
337 }
|
Chris@20
|
338
|
Chris@39
|
339 static PyObject *
|
Chris@48
|
340 setParameterValues(PyObject *self, PyObject *args)
|
Chris@48
|
341 {
|
Chris@48
|
342 PyObject *dict;
|
Chris@48
|
343
|
Chris@48
|
344 if (!PyArg_ParseTuple(args, "O", &dict)) {
|
Chris@48
|
345 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
346 "setParameterValues() takes dict argument");
|
Chris@48
|
347 return 0; }
|
Chris@48
|
348
|
Chris@48
|
349 if (!PyDict_Check(dict)) {
|
Chris@48
|
350 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
351 "setParameterValues() takes dict argument");
|
Chris@48
|
352 return 0; }
|
Chris@48
|
353
|
Chris@48
|
354 PyPluginObject *pd = getPluginObject(self);
|
Chris@48
|
355 if (!pd) return 0;
|
Chris@48
|
356
|
Chris@49
|
357 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
|
Chris@49
|
358 set<string> paramIds;
|
Chris@49
|
359 for (int i = 0; i < (int)pl.size(); ++i) {
|
Chris@49
|
360 paramIds.insert(pl[i].identifier);
|
Chris@49
|
361 }
|
Chris@49
|
362
|
Chris@48
|
363 Py_ssize_t pos = 0;
|
Chris@48
|
364 PyObject *key, *value;
|
Chris@48
|
365 while (PyDict_Next(dict, &pos, &key, &value)) {
|
Chris@48
|
366 if (!key || !PyString_CheckExact(key)) {
|
Chris@48
|
367 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
368 "Parameter dict keys must all have string type");
|
Chris@48
|
369 return 0;
|
Chris@48
|
370 }
|
Chris@48
|
371 if (!value || !FloatConversion::check(value)) {
|
Chris@48
|
372 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
373 "Parameter dict values must be convertible to float");
|
Chris@48
|
374 return 0;
|
Chris@48
|
375 }
|
Chris@49
|
376 string param = PyString_AS_STRING(key);
|
Chris@49
|
377 if (paramIds.find(param) == paramIds.end()) {
|
Chris@49
|
378 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
379 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
380 return 0;
|
Chris@49
|
381 }
|
Chris@49
|
382 pd->plugin->setParameter(param, FloatConversion::convert(value));
|
Chris@48
|
383 }
|
Chris@48
|
384
|
Chris@48
|
385 return Py_True;
|
Chris@48
|
386 }
|
Chris@48
|
387
|
Chris@48
|
388 static PyObject *
|
Chris@39
|
389 selectProgram(PyObject *self, PyObject *args)
|
Chris@39
|
390 {
|
Chris@39
|
391 PyObject *pyParam;
|
Chris@39
|
392
|
Chris@39
|
393 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
394 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
395 "selectProgram() takes parameter id (string) argument");
|
Chris@39
|
396 return 0; }
|
Chris@39
|
397
|
Chris@39
|
398 PyPluginObject *pd = getPluginObject(self);
|
Chris@39
|
399 if (!pd) return 0;
|
Chris@39
|
400
|
Chris@39
|
401 pd->plugin->selectProgram(PyString_AS_STRING(pyParam));
|
Chris@39
|
402 return Py_True;
|
Chris@39
|
403 }
|
Chris@39
|
404
|
Chris@35
|
405 static
|
Chris@35
|
406 PyObject *
|
Chris@35
|
407 convertFeatureSet(const Plugin::FeatureSet &fs)
|
Chris@35
|
408 {
|
Chris@35
|
409 VectorConversion conv;
|
Chris@35
|
410
|
Chris@35
|
411 PyObject *pyFs = PyDict_New();
|
Chris@35
|
412
|
Chris@35
|
413 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
|
Chris@35
|
414 fsi != fs.end(); ++fsi) {
|
Chris@35
|
415
|
Chris@35
|
416 int fno = fsi->first;
|
Chris@35
|
417 const Plugin::FeatureList &fl = fsi->second;
|
Chris@35
|
418
|
Chris@35
|
419 if (!fl.empty()) {
|
Chris@35
|
420
|
Chris@35
|
421 PyObject *pyFl = PyList_New(fl.size());
|
Chris@35
|
422
|
Chris@35
|
423 for (int fli = 0; fli < (int)fl.size(); ++fli) {
|
Chris@35
|
424
|
Chris@35
|
425 const Plugin::Feature &f = fl[fli];
|
Chris@35
|
426 PyObject *pyF = PyDict_New();
|
Chris@35
|
427
|
Chris@35
|
428 if (f.hasTimestamp) {
|
Chris@35
|
429 PyDict_SetItemString
|
Chris@35
|
430 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
|
Chris@35
|
431 }
|
Chris@35
|
432 if (f.hasDuration) {
|
Chris@35
|
433 PyDict_SetItemString
|
Chris@35
|
434 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
|
Chris@35
|
435 }
|
Chris@35
|
436
|
Chris@35
|
437 PyDict_SetItemString
|
Chris@35
|
438 (pyF, "label", pystr(f.label));
|
Chris@35
|
439
|
Chris@35
|
440 if (!f.values.empty()) {
|
Chris@35
|
441 PyDict_SetItemString
|
Chris@35
|
442 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
|
Chris@35
|
443 }
|
Chris@35
|
444
|
Chris@35
|
445 PyList_SET_ITEM(pyFl, fli, pyF);
|
Chris@35
|
446 }
|
Chris@35
|
447
|
Chris@35
|
448 PyObject *pyN = PyInt_FromLong(fno);
|
Chris@35
|
449 PyDict_SetItem(pyFs, pyN, pyFl);
|
Chris@35
|
450 }
|
Chris@35
|
451 }
|
Chris@35
|
452
|
Chris@35
|
453 return pyFs;
|
Chris@35
|
454 }
|
Chris@35
|
455
|
Chris@40
|
456 static vector<vector<float> >
|
Chris@40
|
457 convertPluginInput(PyObject *pyBuffer, int channels, int blockSize)
|
Chris@40
|
458 {
|
Chris@40
|
459 vector<vector<float> > data;
|
Chris@40
|
460
|
Chris@40
|
461 VectorConversion conv;
|
Chris@40
|
462
|
Chris@40
|
463 if (PyArray_CheckExact(pyBuffer)) {
|
Chris@40
|
464
|
Chris@40
|
465 data = conv.Py2DArray_To_FloatVector(pyBuffer);
|
Chris@40
|
466
|
Chris@40
|
467 if (conv.error) {
|
Chris@40
|
468 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@40
|
469 return data;
|
Chris@40
|
470 }
|
Chris@40
|
471
|
Chris@41
|
472 if ((int)data.size() != channels) {
|
Chris@41
|
473 // cerr << "Wrong number of channels: got " << data.size() << ", expected " << channels << endl;
|
Chris@41
|
474 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@41
|
475 return vector<vector<float> >();
|
Chris@41
|
476 }
|
Chris@41
|
477
|
Chris@40
|
478 } else {
|
Chris@40
|
479
|
Chris@40
|
480 if (!PyList_Check(pyBuffer)) {
|
Chris@43
|
481 PyErr_SetString(PyExc_TypeError, "List of NumPy arrays or lists of numbers required for process input");
|
Chris@40
|
482 return data;
|
Chris@40
|
483 }
|
Chris@43
|
484
|
Chris@40
|
485 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@41
|
486 // cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
|
Chris@40
|
487 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@40
|
488 return data;
|
Chris@40
|
489 }
|
Chris@40
|
490
|
Chris@40
|
491 for (int c = 0; c < channels; ++c) {
|
Chris@40
|
492 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@40
|
493 data.push_back(conv.PyValue_To_FloatVector(cbuf));
|
Chris@43
|
494 if (conv.error) {
|
Chris@43
|
495 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@43
|
496 return vector<vector<float> >();
|
Chris@43
|
497 }
|
Chris@40
|
498 }
|
Chris@41
|
499 }
|
Chris@40
|
500
|
Chris@41
|
501 for (int c = 0; c < channels; ++c) {
|
Chris@41
|
502 if ((int)data[c].size() != blockSize) {
|
Chris@41
|
503 // cerr << "Wrong number of samples on channel " << c << ": expected " << blockSize << " (plugin's block size), got " << data[c].size() << endl;
|
Chris@46
|
504 PyErr_SetString(PyExc_TypeError, "Wrong number of samples for process block");
|
Chris@41
|
505 return vector<vector<float> >();
|
Chris@40
|
506 }
|
Chris@40
|
507 }
|
Chris@40
|
508
|
Chris@40
|
509 return data;
|
Chris@40
|
510 }
|
Chris@40
|
511
|
Chris@20
|
512 static PyObject *
|
Chris@39
|
513 process(PyObject *self, PyObject *args)
|
Chris@0
|
514 {
|
Chris@0
|
515 PyObject *pyBuffer;
|
Chris@0
|
516 PyObject *pyRealTime;
|
Chris@0
|
517
|
Chris@23
|
518 if (!PyArg_ParseTuple(args, "OO",
|
Chris@39
|
519 &pyBuffer, // Audio data
|
Chris@39
|
520 &pyRealTime)) { // TimeStamp
|
Chris@39
|
521 PyErr_SetString(PyExc_TypeError,
|
Chris@47
|
522 "process() takes buffer (2D array or list of arrays, one row per channel) and timestamp (RealTime) arguments");
|
Chris@39
|
523 return 0; }
|
Chris@0
|
524
|
Chris@0
|
525 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@40
|
526 PyErr_SetString(PyExc_TypeError, "Valid timestamp required.");
|
Chris@39
|
527 return 0; }
|
Chris@0
|
528
|
Chris@23
|
529 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
530 if (!pd) return 0;
|
Chris@0
|
531
|
Chris@0
|
532 if (!pd->isInitialised) {
|
Chris@39
|
533 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
534 "Plugin has not been initialised.");
|
Chris@39
|
535 return 0;
|
Chris@16
|
536 }
|
Chris@0
|
537
|
Chris@40
|
538 int channels = pd->channels;
|
Chris@40
|
539 vector<vector<float> > data =
|
Chris@40
|
540 convertPluginInput(pyBuffer, channels, pd->blockSize);
|
Chris@40
|
541 if (data.empty()) return 0;
|
Chris@0
|
542
|
Chris@4
|
543 float **inbuf = new float *[channels];
|
Chris@4
|
544 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
545 inbuf[c] = &data[c][0];
|
Chris@4
|
546 }
|
Chris@12
|
547 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
|
Chris@18
|
548 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
|
Chris@4
|
549 delete[] inbuf;
|
Chris@0
|
550
|
Chris@35
|
551 return convertFeatureSet(fs);
|
Chris@35
|
552 }
|
Chris@0
|
553
|
Chris@35
|
554 static PyObject *
|
Chris@39
|
555 getRemainingFeatures(PyObject *self, PyObject *)
|
Chris@35
|
556 {
|
Chris@35
|
557 PyPluginObject *pd = getPluginObject(self);
|
Chris@35
|
558 if (!pd) return 0;
|
Chris@18
|
559
|
Chris@35
|
560 if (!pd->isInitialised) {
|
Chris@39
|
561 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
562 "Plugin has not been initialised.");
|
Chris@39
|
563 return 0;
|
Chris@35
|
564 }
|
Chris@18
|
565
|
Chris@35
|
566 Plugin::FeatureSet fs = pd->plugin->getRemainingFeatures();
|
Chris@18
|
567
|
Chris@35
|
568 return convertFeatureSet(fs);
|
Chris@0
|
569 }
|
Chris@0
|
570
|
Chris@23
|
571 static PyObject *
|
Chris@39
|
572 getPreferredBlockSize(PyObject *self, PyObject *)
|
Chris@37
|
573 {
|
Chris@37
|
574 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
575 if (!pd) return 0;
|
Chris@37
|
576 return PyInt_FromLong(pd->plugin->getPreferredBlockSize());
|
Chris@37
|
577 }
|
Chris@37
|
578
|
Chris@37
|
579 static PyObject *
|
Chris@39
|
580 getPreferredStepSize(PyObject *self, PyObject *)
|
Chris@37
|
581 {
|
Chris@37
|
582 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
583 if (!pd) return 0;
|
Chris@37
|
584 return PyInt_FromLong(pd->plugin->getPreferredStepSize());
|
Chris@37
|
585 }
|
Chris@37
|
586
|
Chris@37
|
587 static PyObject *
|
Chris@39
|
588 getMinChannelCount(PyObject *self, PyObject *)
|
Chris@37
|
589 {
|
Chris@37
|
590 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
591 if (!pd) return 0;
|
Chris@37
|
592 return PyInt_FromLong(pd->plugin->getMinChannelCount());
|
Chris@37
|
593 }
|
Chris@37
|
594
|
Chris@37
|
595 static PyObject *
|
Chris@39
|
596 getMaxChannelCount(PyObject *self, PyObject *)
|
Chris@37
|
597 {
|
Chris@37
|
598 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
599 if (!pd) return 0;
|
Chris@37
|
600 return PyInt_FromLong(pd->plugin->getMaxChannelCount());
|
Chris@37
|
601 }
|
Chris@37
|
602
|
Chris@37
|
603 static PyObject *
|
Chris@39
|
604 unload(PyObject *self, PyObject *)
|
Chris@23
|
605 {
|
Chris@23
|
606 PyPluginObject *pd = getPluginObject(self);
|
Chris@23
|
607 if (!pd) return 0;
|
Chris@23
|
608
|
Chris@23
|
609 delete pd->plugin;
|
Chris@32
|
610 pd->plugin = 0; // This is checked by getPluginObject, so we avoid
|
Chris@32
|
611 // blowing up if called repeatedly
|
Chris@23
|
612
|
Chris@23
|
613 return Py_True;
|
Chris@23
|
614 }
|
Chris@23
|
615
|
Chris@33
|
616 static PyMemberDef PyPluginObject_members[] =
|
Chris@33
|
617 {
|
Chris@34
|
618 {(char *)"info", T_OBJECT, offsetof(PyPluginObject, info), READONLY,
|
Chris@39
|
619 (char *)"info -> A read-only dictionary of plugin metadata."},
|
Chris@34
|
620
|
Chris@34
|
621 {(char *)"inputDomain", T_INT, offsetof(PyPluginObject, inputDomain), READONLY,
|
Chris@39
|
622 (char *)"inputDomain -> The format of input audio required by the plugin, either vampyhost.TimeDomain or vampyhost.FrequencyDomain."},
|
Chris@34
|
623
|
Chris@34
|
624 {(char *)"parameters", T_OBJECT, offsetof(PyPluginObject, parameters), READONLY,
|
Chris@39
|
625 (char *)"parameters -> A list of metadata dictionaries describing the plugin's configurable parameters."},
|
Chris@39
|
626
|
Chris@39
|
627 {(char *)"programs", T_OBJECT, offsetof(PyPluginObject, programs), READONLY,
|
Chris@39
|
628 (char *)"programs -> A list of the programs available for this plugin, if any."},
|
Chris@33
|
629
|
Chris@33
|
630 {0, 0}
|
Chris@33
|
631 };
|
Chris@33
|
632
|
Chris@21
|
633 static PyMethodDef PyPluginObject_methods[] =
|
Chris@21
|
634 {
|
Chris@37
|
635 {"getOutputs", getOutputs, METH_NOARGS,
|
Chris@39
|
636 "getOutputs() -> Obtain the output descriptors for all of the plugin's outputs."},
|
Chris@37
|
637
|
Chris@48
|
638 {"getParameterValue", getParameterValue, METH_VARARGS,
|
Chris@39
|
639 "getParameterValue(identifier) -> Return the value of the parameter with the given identifier."},
|
Chris@23
|
640
|
Chris@48
|
641 {"setParameterValue", setParameterValue, METH_VARARGS,
|
Chris@39
|
642 "setParameterValue(identifier, value) -> Set the parameter with the given identifier to the given value."},
|
Chris@37
|
643
|
Chris@48
|
644 {"setParameterValues", setParameterValues, METH_VARARGS,
|
Chris@48
|
645 "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
|
646
|
Chris@39
|
647 {"selectProgram", selectProgram, METH_VARARGS,
|
Chris@39
|
648 "selectProgram(name) -> Select the processing program with the given name."},
|
Chris@39
|
649
|
Chris@39
|
650 {"getPreferredBlockSize", getPreferredBlockSize, METH_VARARGS,
|
Chris@39
|
651 "getPreferredBlockSize() -> Return the plugin's preferred processing block size, or 0 if the plugin accepts any block size."},
|
Chris@37
|
652
|
Chris@39
|
653 {"getPreferredStepSize", getPreferredStepSize, METH_VARARGS,
|
Chris@39
|
654 "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
|
655
|
Chris@39
|
656 {"getMinChannelCount", getMinChannelCount, METH_VARARGS,
|
Chris@39
|
657 "getMinChannelCount() -> Return the minimum number of channels of audio data the plugin accepts as input."},
|
Chris@37
|
658
|
Chris@39
|
659 {"getMaxChannelCount", getMaxChannelCount, METH_VARARGS,
|
Chris@39
|
660 "getMaxChannelCount() -> Return the maximum number of channels of audio data the plugin accepts as input."},
|
Chris@33
|
661
|
Chris@39
|
662 {"initialise", initialise, METH_VARARGS,
|
Chris@39
|
663 "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
|
664
|
Chris@39
|
665 {"reset", reset, METH_NOARGS,
|
Chris@39
|
666 "reset() -> Reset the plugin after processing, to prepare for another processing run with the same parameters."},
|
Chris@23
|
667
|
Chris@39
|
668 {"process", process, METH_VARARGS,
|
Chris@39
|
669 "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
|
670
|
Chris@39
|
671 {"getRemainingFeatures", getRemainingFeatures, METH_NOARGS,
|
Chris@39
|
672 "getRemainingFeatures() -> Obtain any features extracted at the end of processing."},
|
Chris@35
|
673
|
Chris@39
|
674 {"unload", unload, METH_NOARGS,
|
Chris@39
|
675 "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
|
676
|
Chris@21
|
677 {0, 0}
|
Chris@21
|
678 };
|
Chris@21
|
679
|
Chris@21
|
680 /* Doc:: 10.3 Type Objects */ /* static */
|
Chris@21
|
681 PyTypeObject Plugin_Type =
|
Chris@21
|
682 {
|
Chris@21
|
683 PyObject_HEAD_INIT(NULL)
|
Chris@39
|
684 0, /*ob_size*/
|
Chris@39
|
685 "vampyhost.Plugin", /*tp_name*/
|
Chris@39
|
686 sizeof(PyPluginObject), /*tp_basicsize*/
|
Chris@39
|
687 0, /*tp_itemsize*/
|
Chris@21
|
688 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
|
Chris@39
|
689 0, /*tp_print*/
|
Chris@39
|
690 0, /*tp_getattr*/
|
Chris@39
|
691 0, /*tp_setattr*/
|
Chris@39
|
692 0, /*tp_compare*/
|
Chris@39
|
693 0, /*tp_repr*/
|
Chris@39
|
694 0, /*tp_as_number*/
|
Chris@39
|
695 0, /*tp_as_sequence*/
|
Chris@39
|
696 0, /*tp_as_mapping*/
|
Chris@39
|
697 0, /*tp_hash*/
|
Chris@39
|
698 0, /*tp_call*/
|
Chris@39
|
699 0, /*tp_str*/
|
Chris@39
|
700 PyObject_GenericGetAttr, /*tp_getattro*/
|
Chris@39
|
701 PyObject_GenericSetAttr, /*tp_setattro*/
|
Chris@39
|
702 0, /*tp_as_buffer*/
|
Chris@39
|
703 Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
Chris@40
|
704 "Plugin object, providing a low-level API for running a Vamp plugin.", /*tp_doc*/
|
Chris@39
|
705 0, /*tp_traverse*/
|
Chris@39
|
706 0, /*tp_clear*/
|
Chris@39
|
707 0, /*tp_richcompare*/
|
Chris@39
|
708 0, /*tp_weaklistoffset*/
|
Chris@39
|
709 0, /*tp_iter*/
|
Chris@39
|
710 0, /*tp_iternext*/
|
Chris@39
|
711 PyPluginObject_methods, /*tp_methods*/
|
Chris@39
|
712 PyPluginObject_members, /*tp_members*/
|
Chris@39
|
713 0, /*tp_getset*/
|
Chris@39
|
714 0, /*tp_base*/
|
Chris@39
|
715 0, /*tp_dict*/
|
Chris@39
|
716 0, /*tp_descr_get*/
|
Chris@39
|
717 0, /*tp_descr_set*/
|
Chris@39
|
718 0, /*tp_dictoffset*/
|
Chris@39
|
719 0, /*tp_init*/
|
Chris@39
|
720 PyType_GenericAlloc, /*tp_alloc*/
|
Chris@39
|
721 0, /*tp_new*/
|
Chris@39
|
722 PyObject_Del, /*tp_free*/
|
Chris@39
|
723 0, /*tp_is_gc*/
|
Chris@21
|
724 };
|
Chris@0
|
725
|