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