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@83
|
97 (infodict, "api_version", PyInt_FromLong(plugin->getVampApiVersion()));
|
Chris@34
|
98 PyDict_SetItemString
|
Chris@83
|
99 (infodict, "plugin_version", 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@82
|
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@83
|
130 (paramdict, "min_value", PyFloat_FromDouble(pl[i].minValue));
|
Chris@34
|
131 PyDict_SetItemString
|
Chris@83
|
132 (paramdict, "max_value", PyFloat_FromDouble(pl[i].maxValue));
|
Chris@34
|
133 PyDict_SetItemString
|
Chris@83
|
134 (paramdict, "default_value", PyFloat_FromDouble(pl[i].defaultValue));
|
Chris@34
|
135 if (pl[i].isQuantized) {
|
Chris@34
|
136 PyDict_SetItemString
|
Chris@83
|
137 (paramdict, "is_quantized", Py_True);
|
Chris@34
|
138 PyDict_SetItemString
|
Chris@83
|
139 (paramdict, "quantize_step", PyFloat_FromDouble(pl[i].quantizeStep));
|
Chris@34
|
140 if (!pl[i].valueNames.empty()) {
|
Chris@34
|
141 PyDict_SetItemString
|
Chris@83
|
142 (paramdict, "value_names", conv.PyValue_From_StringVector(pl[i].valueNames));
|
Chris@34
|
143 }
|
Chris@34
|
144 } else {
|
Chris@34
|
145 PyDict_SetItemString
|
Chris@83
|
146 (paramdict, "is_quantized", 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@86
|
174 convertOutput(const Plugin::OutputDescriptor &desc)
|
Chris@86
|
175 {
|
Chris@86
|
176 PyObject *outdict = PyDict_New();
|
Chris@86
|
177 PyDict_SetItemString
|
Chris@86
|
178 (outdict, "identifier", pystr(desc.identifier));
|
Chris@86
|
179 PyDict_SetItemString
|
Chris@86
|
180 (outdict, "name", pystr(desc.name));
|
Chris@86
|
181 PyDict_SetItemString
|
Chris@86
|
182 (outdict, "description", pystr(desc.description));
|
Chris@86
|
183 PyDict_SetItemString
|
Chris@86
|
184 (outdict, "bin_count", PyInt_FromLong(desc.binCount));
|
Chris@86
|
185 if (desc.binCount > 0) {
|
Chris@86
|
186 if (desc.hasKnownExtents) {
|
Chris@86
|
187 PyDict_SetItemString
|
Chris@86
|
188 (outdict, "has_known_extents", Py_True);
|
Chris@86
|
189 PyDict_SetItemString
|
Chris@86
|
190 (outdict, "min_value", PyFloat_FromDouble(desc.minValue));
|
Chris@86
|
191 PyDict_SetItemString
|
Chris@86
|
192 (outdict, "max_value", PyFloat_FromDouble(desc.maxValue));
|
Chris@86
|
193 } else {
|
Chris@86
|
194 PyDict_SetItemString
|
Chris@86
|
195 (outdict, "has_known_extents", Py_False);
|
Chris@86
|
196 }
|
Chris@86
|
197 if (desc.isQuantized) {
|
Chris@86
|
198 PyDict_SetItemString
|
Chris@86
|
199 (outdict, "is_quantized", Py_True);
|
Chris@86
|
200 PyDict_SetItemString
|
Chris@86
|
201 (outdict, "quantize_step", PyFloat_FromDouble(desc.quantizeStep));
|
Chris@86
|
202 } else {
|
Chris@86
|
203 PyDict_SetItemString
|
Chris@86
|
204 (outdict, "is_quantized", Py_False);
|
Chris@86
|
205 }
|
Chris@86
|
206 }
|
Chris@86
|
207 PyDict_SetItemString
|
Chris@86
|
208 (outdict, "sample_type", PyInt_FromLong((int)desc.sampleType));
|
Chris@86
|
209 PyDict_SetItemString
|
Chris@86
|
210 (outdict, "sample_rate", PyFloat_FromDouble(desc.sampleRate));
|
Chris@86
|
211 PyDict_SetItemString
|
Chris@86
|
212 (outdict, "has_duration", desc.hasDuration ? Py_True : Py_False);
|
Chris@86
|
213 return outdict;
|
Chris@86
|
214 }
|
Chris@86
|
215
|
Chris@86
|
216 static PyObject *
|
Chris@86
|
217 get_output(PyObject *self, PyObject *args)
|
Chris@86
|
218 {
|
Chris@86
|
219 PyPluginObject *pd = getPluginObject(self);
|
Chris@86
|
220 if (!pd) return 0;
|
Chris@86
|
221
|
Chris@86
|
222 int n = -1;
|
Chris@86
|
223 PyObject *pyId = 0;
|
Chris@86
|
224
|
Chris@86
|
225 if (!PyArg_ParseTuple(args, "n", &n) &&
|
Chris@86
|
226 !PyArg_ParseTuple(args, "S", &pyId)) {
|
Chris@86
|
227 PyErr_SetString(PyExc_TypeError,
|
Chris@86
|
228 "get_output takes either output id (string) or output index (int) argument");
|
Chris@86
|
229 return 0;
|
Chris@86
|
230 }
|
Chris@86
|
231
|
Chris@86
|
232 Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
|
Chris@86
|
233
|
Chris@86
|
234 if (pyId) {
|
Chris@86
|
235 string id = PyString_AS_STRING(pyId);
|
Chris@86
|
236 for (int i = 0; i < int(ol.size()); ++i) {
|
Chris@86
|
237 if (ol[i].identifier == id) {
|
Chris@86
|
238 return convertOutput(ol[i]);
|
Chris@86
|
239 }
|
Chris@86
|
240 }
|
Chris@86
|
241 } else {
|
Chris@86
|
242 if (n >= 0 && n < int(ol.size())) {
|
Chris@86
|
243 return convertOutput(ol[n]);
|
Chris@86
|
244 }
|
Chris@86
|
245 }
|
Chris@86
|
246
|
Chris@86
|
247 PyErr_SetString(PyExc_StandardError,
|
Chris@86
|
248 "unknown output id or output index out of range");
|
Chris@86
|
249 return 0;
|
Chris@86
|
250 }
|
Chris@86
|
251
|
Chris@86
|
252 static PyObject *
|
Chris@80
|
253 get_outputs(PyObject *self, PyObject *args)
|
Chris@37
|
254 {
|
Chris@37
|
255 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
256 if (!pd) return 0;
|
Chris@37
|
257 Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
|
Chris@35
|
258 PyObject *outputs = PyList_New(ol.size());
|
Chris@35
|
259
|
Chris@35
|
260 for (int i = 0; i < (int)ol.size(); ++i) {
|
Chris@86
|
261 PyObject *outdict = convertOutput(ol[i]);
|
Chris@35
|
262 PyList_SET_ITEM(outputs, i, outdict);
|
Chris@35
|
263 }
|
Chris@35
|
264
|
Chris@37
|
265 return outputs;
|
Chris@33
|
266 }
|
Chris@33
|
267
|
Chris@0
|
268 static PyObject *
|
Chris@39
|
269 initialise(PyObject *self, PyObject *args)
|
Chris@0
|
270 {
|
luis@7
|
271 size_t channels, blockSize, stepSize;
|
Chris@0
|
272
|
Chris@23
|
273 if (!PyArg_ParseTuple (args, "nnn",
|
Chris@39
|
274 (size_t) &channels,
|
Chris@39
|
275 (size_t) &stepSize,
|
Chris@39
|
276 (size_t) &blockSize)) {
|
Chris@39
|
277 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
278 "initialise() takes channel count, step size, and block size arguments");
|
Chris@39
|
279 return 0;
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@23
|
282 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
283 if (!pd) return 0;
|
Chris@0
|
284
|
Chris@16
|
285 pd->channels = channels;
|
Chris@16
|
286 pd->stepSize = stepSize;
|
Chris@16
|
287 pd->blockSize = blockSize;
|
Chris@0
|
288
|
Chris@16
|
289 if (!pd->plugin->initialise(channels, stepSize, blockSize)) {
|
Chris@39
|
290 cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << endl;
|
Chris@39
|
291 PyErr_SetString(PyExc_TypeError,
|
Chris@39
|
292 "Plugin initialization failed");
|
Chris@39
|
293 return 0;
|
Chris@6
|
294 }
|
Chris@0
|
295
|
Chris@16
|
296 pd->isInitialised = true;
|
luis@7
|
297
|
Chris@0
|
298 return Py_True;
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 static PyObject *
|
Chris@39
|
302 reset(PyObject *self, PyObject *)
|
Chris@18
|
303 {
|
Chris@23
|
304 PyPluginObject *pd = getPluginObject(self);
|
Chris@18
|
305 if (!pd) return 0;
|
Chris@18
|
306
|
Chris@18
|
307 if (!pd->isInitialised) {
|
Chris@18
|
308 PyErr_SetString(PyExc_StandardError,
|
Chris@18
|
309 "Plugin has not been initialised");
|
Chris@18
|
310 return 0;
|
Chris@18
|
311 }
|
Chris@18
|
312
|
Chris@18
|
313 pd->plugin->reset();
|
Chris@18
|
314 return Py_True;
|
Chris@18
|
315 }
|
Chris@18
|
316
|
Chris@49
|
317 static bool
|
Chris@49
|
318 hasParameter(PyPluginObject *pd, string id)
|
Chris@49
|
319 {
|
Chris@49
|
320 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
|
Chris@49
|
321 for (int i = 0; i < (int)pl.size(); ++i) {
|
Chris@49
|
322 if (pl[i].identifier == id) {
|
Chris@49
|
323 return true;
|
Chris@49
|
324 }
|
Chris@49
|
325 }
|
Chris@49
|
326 return false;
|
Chris@49
|
327 }
|
Chris@49
|
328
|
Chris@18
|
329 static PyObject *
|
Chris@80
|
330 get_parameter_value(PyObject *self, PyObject *args)
|
Chris@20
|
331 {
|
Chris@20
|
332 PyObject *pyParam;
|
Chris@20
|
333
|
Chris@23
|
334 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
335 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
336 "get_parameter_value() takes parameter id (string) argument");
|
Chris@39
|
337 return 0; }
|
Chris@20
|
338
|
Chris@23
|
339 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
340 if (!pd) return 0;
|
Chris@20
|
341
|
Chris@49
|
342 string param = PyString_AS_STRING(pyParam);
|
Chris@49
|
343
|
Chris@49
|
344 if (!hasParameter(pd, param)) {
|
Chris@49
|
345 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
346 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
347 return 0;
|
Chris@49
|
348 }
|
Chris@49
|
349
|
Chris@49
|
350 float value = pd->plugin->getParameter(param);
|
Chris@20
|
351 return PyFloat_FromDouble(double(value));
|
Chris@20
|
352 }
|
Chris@20
|
353
|
Chris@20
|
354 static PyObject *
|
Chris@80
|
355 set_parameter_value(PyObject *self, PyObject *args)
|
Chris@20
|
356 {
|
Chris@20
|
357 PyObject *pyParam;
|
Chris@20
|
358 float value;
|
Chris@20
|
359
|
Chris@23
|
360 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
|
Chris@39
|
361 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
362 "set_parameter_value() takes parameter id (string), and value (float) arguments");
|
Chris@39
|
363 return 0; }
|
Chris@20
|
364
|
Chris@23
|
365 PyPluginObject *pd = getPluginObject(self);
|
Chris@20
|
366 if (!pd) return 0;
|
Chris@20
|
367
|
Chris@49
|
368 string param = PyString_AS_STRING(pyParam);
|
Chris@49
|
369
|
Chris@49
|
370 if (!hasParameter(pd, param)) {
|
Chris@49
|
371 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
372 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
373 return 0;
|
Chris@49
|
374 }
|
Chris@49
|
375
|
Chris@49
|
376 pd->plugin->setParameter(param, value);
|
Chris@20
|
377 return Py_True;
|
Chris@20
|
378 }
|
Chris@20
|
379
|
Chris@39
|
380 static PyObject *
|
Chris@80
|
381 set_parameter_values(PyObject *self, PyObject *args)
|
Chris@48
|
382 {
|
Chris@48
|
383 PyObject *dict;
|
Chris@48
|
384
|
Chris@48
|
385 if (!PyArg_ParseTuple(args, "O", &dict)) {
|
Chris@48
|
386 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
387 "set_parameter_values() takes dict argument");
|
Chris@48
|
388 return 0; }
|
Chris@48
|
389
|
Chris@48
|
390 if (!PyDict_Check(dict)) {
|
Chris@48
|
391 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
392 "set_parameter_values() takes dict argument");
|
Chris@48
|
393 return 0; }
|
Chris@48
|
394
|
Chris@48
|
395 PyPluginObject *pd = getPluginObject(self);
|
Chris@48
|
396 if (!pd) return 0;
|
Chris@48
|
397
|
Chris@49
|
398 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
|
Chris@49
|
399 set<string> paramIds;
|
Chris@49
|
400 for (int i = 0; i < (int)pl.size(); ++i) {
|
Chris@49
|
401 paramIds.insert(pl[i].identifier);
|
Chris@49
|
402 }
|
Chris@49
|
403
|
Chris@48
|
404 Py_ssize_t pos = 0;
|
Chris@48
|
405 PyObject *key, *value;
|
Chris@48
|
406 while (PyDict_Next(dict, &pos, &key, &value)) {
|
Chris@48
|
407 if (!key || !PyString_CheckExact(key)) {
|
Chris@48
|
408 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
409 "Parameter dict keys must all have string type");
|
Chris@48
|
410 return 0;
|
Chris@48
|
411 }
|
Chris@48
|
412 if (!value || !FloatConversion::check(value)) {
|
Chris@48
|
413 PyErr_SetString(PyExc_TypeError,
|
Chris@48
|
414 "Parameter dict values must be convertible to float");
|
Chris@48
|
415 return 0;
|
Chris@48
|
416 }
|
Chris@49
|
417 string param = PyString_AS_STRING(key);
|
Chris@49
|
418 if (paramIds.find(param) == paramIds.end()) {
|
Chris@49
|
419 PyErr_SetString(PyExc_StandardError,
|
Chris@49
|
420 (string("Unknown parameter id \"") + param + "\"").c_str());
|
Chris@49
|
421 return 0;
|
Chris@49
|
422 }
|
Chris@49
|
423 pd->plugin->setParameter(param, FloatConversion::convert(value));
|
Chris@48
|
424 }
|
Chris@48
|
425
|
Chris@48
|
426 return Py_True;
|
Chris@48
|
427 }
|
Chris@48
|
428
|
Chris@48
|
429 static PyObject *
|
Chris@80
|
430 select_program(PyObject *self, PyObject *args)
|
Chris@39
|
431 {
|
Chris@39
|
432 PyObject *pyParam;
|
Chris@39
|
433
|
Chris@39
|
434 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
|
Chris@39
|
435 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
436 "select_program() takes parameter id (string) argument");
|
Chris@39
|
437 return 0; }
|
Chris@39
|
438
|
Chris@39
|
439 PyPluginObject *pd = getPluginObject(self);
|
Chris@39
|
440 if (!pd) return 0;
|
Chris@39
|
441
|
Chris@39
|
442 pd->plugin->selectProgram(PyString_AS_STRING(pyParam));
|
Chris@39
|
443 return Py_True;
|
Chris@39
|
444 }
|
Chris@39
|
445
|
Chris@35
|
446 static
|
Chris@35
|
447 PyObject *
|
Chris@35
|
448 convertFeatureSet(const Plugin::FeatureSet &fs)
|
Chris@35
|
449 {
|
Chris@35
|
450 VectorConversion conv;
|
Chris@35
|
451
|
Chris@35
|
452 PyObject *pyFs = PyDict_New();
|
Chris@35
|
453
|
Chris@35
|
454 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
|
Chris@35
|
455 fsi != fs.end(); ++fsi) {
|
Chris@35
|
456
|
Chris@35
|
457 int fno = fsi->first;
|
Chris@35
|
458 const Plugin::FeatureList &fl = fsi->second;
|
Chris@35
|
459
|
Chris@35
|
460 if (!fl.empty()) {
|
Chris@35
|
461
|
Chris@35
|
462 PyObject *pyFl = PyList_New(fl.size());
|
Chris@35
|
463
|
Chris@35
|
464 for (int fli = 0; fli < (int)fl.size(); ++fli) {
|
Chris@35
|
465
|
Chris@35
|
466 const Plugin::Feature &f = fl[fli];
|
Chris@35
|
467 PyObject *pyF = PyDict_New();
|
Chris@35
|
468
|
Chris@35
|
469 if (f.hasTimestamp) {
|
Chris@35
|
470 PyDict_SetItemString
|
Chris@35
|
471 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
|
Chris@35
|
472 }
|
Chris@35
|
473 if (f.hasDuration) {
|
Chris@35
|
474 PyDict_SetItemString
|
Chris@35
|
475 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
|
Chris@35
|
476 }
|
Chris@35
|
477
|
Chris@35
|
478 PyDict_SetItemString
|
Chris@35
|
479 (pyF, "label", pystr(f.label));
|
Chris@35
|
480
|
Chris@35
|
481 if (!f.values.empty()) {
|
Chris@35
|
482 PyDict_SetItemString
|
Chris@35
|
483 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
|
Chris@35
|
484 }
|
Chris@35
|
485
|
Chris@35
|
486 PyList_SET_ITEM(pyFl, fli, pyF);
|
Chris@35
|
487 }
|
Chris@35
|
488
|
Chris@35
|
489 PyObject *pyN = PyInt_FromLong(fno);
|
Chris@35
|
490 PyDict_SetItem(pyFs, pyN, pyFl);
|
Chris@35
|
491 }
|
Chris@35
|
492 }
|
Chris@35
|
493
|
Chris@35
|
494 return pyFs;
|
Chris@35
|
495 }
|
Chris@35
|
496
|
Chris@40
|
497 static vector<vector<float> >
|
Chris@40
|
498 convertPluginInput(PyObject *pyBuffer, int channels, int blockSize)
|
Chris@40
|
499 {
|
Chris@40
|
500 vector<vector<float> > data;
|
Chris@40
|
501
|
Chris@40
|
502 VectorConversion conv;
|
Chris@40
|
503
|
Chris@40
|
504 if (PyArray_CheckExact(pyBuffer)) {
|
Chris@40
|
505
|
Chris@40
|
506 data = conv.Py2DArray_To_FloatVector(pyBuffer);
|
Chris@40
|
507
|
Chris@40
|
508 if (conv.error) {
|
Chris@40
|
509 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@40
|
510 return data;
|
Chris@40
|
511 }
|
Chris@40
|
512
|
Chris@41
|
513 if ((int)data.size() != channels) {
|
Chris@41
|
514 // cerr << "Wrong number of channels: got " << data.size() << ", expected " << channels << endl;
|
Chris@41
|
515 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@41
|
516 return vector<vector<float> >();
|
Chris@41
|
517 }
|
Chris@41
|
518
|
Chris@40
|
519 } else {
|
Chris@40
|
520
|
Chris@40
|
521 if (!PyList_Check(pyBuffer)) {
|
Chris@43
|
522 PyErr_SetString(PyExc_TypeError, "List of NumPy arrays or lists of numbers required for process input");
|
Chris@40
|
523 return data;
|
Chris@40
|
524 }
|
Chris@43
|
525
|
Chris@40
|
526 if (PyList_GET_SIZE(pyBuffer) != channels) {
|
Chris@41
|
527 // cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
|
Chris@40
|
528 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
|
Chris@40
|
529 return data;
|
Chris@40
|
530 }
|
Chris@40
|
531
|
Chris@40
|
532 for (int c = 0; c < channels; ++c) {
|
Chris@40
|
533 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
|
Chris@40
|
534 data.push_back(conv.PyValue_To_FloatVector(cbuf));
|
Chris@43
|
535 if (conv.error) {
|
Chris@43
|
536 PyErr_SetString(PyExc_TypeError, conv.getError().str().c_str());
|
Chris@43
|
537 return vector<vector<float> >();
|
Chris@43
|
538 }
|
Chris@40
|
539 }
|
Chris@41
|
540 }
|
Chris@40
|
541
|
Chris@41
|
542 for (int c = 0; c < channels; ++c) {
|
Chris@41
|
543 if ((int)data[c].size() != blockSize) {
|
Chris@41
|
544 // cerr << "Wrong number of samples on channel " << c << ": expected " << blockSize << " (plugin's block size), got " << data[c].size() << endl;
|
Chris@46
|
545 PyErr_SetString(PyExc_TypeError, "Wrong number of samples for process block");
|
Chris@41
|
546 return vector<vector<float> >();
|
Chris@40
|
547 }
|
Chris@40
|
548 }
|
Chris@40
|
549
|
Chris@40
|
550 return data;
|
Chris@40
|
551 }
|
Chris@40
|
552
|
Chris@20
|
553 static PyObject *
|
Chris@80
|
554 process_block(PyObject *self, PyObject *args)
|
Chris@0
|
555 {
|
Chris@0
|
556 PyObject *pyBuffer;
|
Chris@0
|
557 PyObject *pyRealTime;
|
Chris@0
|
558
|
Chris@23
|
559 if (!PyArg_ParseTuple(args, "OO",
|
Chris@39
|
560 &pyBuffer, // Audio data
|
Chris@39
|
561 &pyRealTime)) { // TimeStamp
|
Chris@39
|
562 PyErr_SetString(PyExc_TypeError,
|
Chris@80
|
563 "process_block() takes buffer (2D array or list of arrays, one row per channel) and timestamp (RealTime) arguments");
|
Chris@39
|
564 return 0; }
|
Chris@0
|
565
|
Chris@0
|
566 if (!PyRealTime_Check(pyRealTime)) {
|
Chris@40
|
567 PyErr_SetString(PyExc_TypeError, "Valid timestamp required.");
|
Chris@39
|
568 return 0; }
|
Chris@0
|
569
|
Chris@23
|
570 PyPluginObject *pd = getPluginObject(self);
|
Chris@16
|
571 if (!pd) return 0;
|
Chris@0
|
572
|
Chris@0
|
573 if (!pd->isInitialised) {
|
Chris@39
|
574 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
575 "Plugin has not been initialised.");
|
Chris@39
|
576 return 0;
|
Chris@16
|
577 }
|
Chris@0
|
578
|
Chris@40
|
579 int channels = pd->channels;
|
Chris@40
|
580 vector<vector<float> > data =
|
Chris@40
|
581 convertPluginInput(pyBuffer, channels, pd->blockSize);
|
Chris@40
|
582 if (data.empty()) return 0;
|
Chris@0
|
583
|
Chris@4
|
584 float **inbuf = new float *[channels];
|
Chris@4
|
585 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
586 inbuf[c] = &data[c][0];
|
Chris@4
|
587 }
|
Chris@12
|
588 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
|
Chris@18
|
589 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
|
Chris@4
|
590 delete[] inbuf;
|
Chris@0
|
591
|
Chris@35
|
592 return convertFeatureSet(fs);
|
Chris@35
|
593 }
|
Chris@0
|
594
|
Chris@35
|
595 static PyObject *
|
Chris@80
|
596 get_remaining_features(PyObject *self, PyObject *)
|
Chris@35
|
597 {
|
Chris@35
|
598 PyPluginObject *pd = getPluginObject(self);
|
Chris@35
|
599 if (!pd) return 0;
|
Chris@18
|
600
|
Chris@35
|
601 if (!pd->isInitialised) {
|
Chris@39
|
602 PyErr_SetString(PyExc_StandardError,
|
Chris@39
|
603 "Plugin has not been initialised.");
|
Chris@39
|
604 return 0;
|
Chris@35
|
605 }
|
Chris@18
|
606
|
Chris@35
|
607 Plugin::FeatureSet fs = pd->plugin->getRemainingFeatures();
|
Chris@18
|
608
|
Chris@35
|
609 return convertFeatureSet(fs);
|
Chris@0
|
610 }
|
Chris@0
|
611
|
Chris@23
|
612 static PyObject *
|
Chris@80
|
613 get_preferred_block_size(PyObject *self, PyObject *)
|
Chris@37
|
614 {
|
Chris@37
|
615 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
616 if (!pd) return 0;
|
Chris@37
|
617 return PyInt_FromLong(pd->plugin->getPreferredBlockSize());
|
Chris@37
|
618 }
|
Chris@37
|
619
|
Chris@37
|
620 static PyObject *
|
Chris@80
|
621 get_preferred_step_size(PyObject *self, PyObject *)
|
Chris@37
|
622 {
|
Chris@37
|
623 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
624 if (!pd) return 0;
|
Chris@37
|
625 return PyInt_FromLong(pd->plugin->getPreferredStepSize());
|
Chris@37
|
626 }
|
Chris@37
|
627
|
Chris@37
|
628 static PyObject *
|
Chris@80
|
629 get_min_channel_count(PyObject *self, PyObject *)
|
Chris@37
|
630 {
|
Chris@37
|
631 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
632 if (!pd) return 0;
|
Chris@37
|
633 return PyInt_FromLong(pd->plugin->getMinChannelCount());
|
Chris@37
|
634 }
|
Chris@37
|
635
|
Chris@37
|
636 static PyObject *
|
Chris@80
|
637 get_max_channel_count(PyObject *self, PyObject *)
|
Chris@37
|
638 {
|
Chris@37
|
639 PyPluginObject *pd = getPluginObject(self);
|
Chris@37
|
640 if (!pd) return 0;
|
Chris@37
|
641 return PyInt_FromLong(pd->plugin->getMaxChannelCount());
|
Chris@37
|
642 }
|
Chris@37
|
643
|
Chris@37
|
644 static PyObject *
|
Chris@39
|
645 unload(PyObject *self, PyObject *)
|
Chris@23
|
646 {
|
Chris@23
|
647 PyPluginObject *pd = getPluginObject(self);
|
Chris@23
|
648 if (!pd) return 0;
|
Chris@23
|
649
|
Chris@23
|
650 delete pd->plugin;
|
Chris@32
|
651 pd->plugin = 0; // This is checked by getPluginObject, so we avoid
|
Chris@32
|
652 // blowing up if called repeatedly
|
Chris@23
|
653
|
Chris@23
|
654 return Py_True;
|
Chris@23
|
655 }
|
Chris@23
|
656
|
Chris@33
|
657 static PyMemberDef PyPluginObject_members[] =
|
Chris@33
|
658 {
|
Chris@34
|
659 {(char *)"info", T_OBJECT, offsetof(PyPluginObject, info), READONLY,
|
Chris@39
|
660 (char *)"info -> A read-only dictionary of plugin metadata."},
|
Chris@34
|
661
|
Chris@82
|
662 {(char *)"input_domain", T_INT, offsetof(PyPluginObject, inputDomain), READONLY,
|
Chris@81
|
663 (char *)"input_domain -> The format of input audio required by the plugin, either vampyhost.TIME_DOMAIN or vampyhost.FREQUENCY_DOMAIN."},
|
Chris@34
|
664
|
Chris@34
|
665 {(char *)"parameters", T_OBJECT, offsetof(PyPluginObject, parameters), READONLY,
|
Chris@39
|
666 (char *)"parameters -> A list of metadata dictionaries describing the plugin's configurable parameters."},
|
Chris@39
|
667
|
Chris@39
|
668 {(char *)"programs", T_OBJECT, offsetof(PyPluginObject, programs), READONLY,
|
Chris@39
|
669 (char *)"programs -> A list of the programs available for this plugin, if any."},
|
Chris@33
|
670
|
Chris@33
|
671 {0, 0}
|
Chris@33
|
672 };
|
Chris@33
|
673
|
Chris@21
|
674 static PyMethodDef PyPluginObject_methods[] =
|
Chris@21
|
675 {
|
Chris@80
|
676 {"get_outputs", get_outputs, METH_NOARGS,
|
Chris@80
|
677 "get_outputs() -> Obtain the output descriptors for all of the plugin's outputs."},
|
Chris@37
|
678
|
Chris@86
|
679 {"get_output", get_output, METH_VARARGS,
|
Chris@86
|
680 "get_output(out) -> Obtain the output descriptor for a single output, by either id (string) or index (int)."},
|
Chris@86
|
681
|
Chris@80
|
682 {"get_parameter_value", get_parameter_value, METH_VARARGS,
|
Chris@80
|
683 "get_parameter_value(identifier) -> Return the value of the parameter with the given identifier."},
|
Chris@23
|
684
|
Chris@80
|
685 {"set_parameter_value", set_parameter_value, METH_VARARGS,
|
Chris@80
|
686 "set_parameter_value(identifier, value) -> Set the parameter with the given identifier to the given value."},
|
Chris@37
|
687
|
Chris@80
|
688 {"set_parameter_values", set_parameter_values, METH_VARARGS,
|
Chris@80
|
689 "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
|
690
|
Chris@80
|
691 {"select_program", select_program, METH_VARARGS,
|
Chris@80
|
692 "select_program(name) -> Select the processing program with the given name."},
|
Chris@39
|
693
|
Chris@80
|
694 {"get_preferred_block_size", get_preferred_block_size, METH_VARARGS,
|
Chris@80
|
695 "get_preferred_block_size() -> Return the plugin's preferred processing block size, or 0 if the plugin accepts any block size."},
|
Chris@37
|
696
|
Chris@80
|
697 {"get_preferred_step_size", get_preferred_step_size, METH_VARARGS,
|
Chris@80
|
698 "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
|
699
|
Chris@80
|
700 {"get_min_channel_count", get_min_channel_count, METH_VARARGS,
|
Chris@80
|
701 "get_min_channel_count() -> Return the minimum number of channels of audio data the plugin accepts as input."},
|
Chris@37
|
702
|
Chris@80
|
703 {"get_max_channel_count", get_max_channel_count, METH_VARARGS,
|
Chris@80
|
704 "get_max_channel_count() -> Return the maximum number of channels of audio data the plugin accepts as input."},
|
Chris@33
|
705
|
Chris@39
|
706 {"initialise", initialise, METH_VARARGS,
|
Chris@80
|
707 "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
|
708
|
Chris@39
|
709 {"reset", reset, METH_NOARGS,
|
Chris@39
|
710 "reset() -> Reset the plugin after processing, to prepare for another processing run with the same parameters."},
|
Chris@23
|
711
|
Chris@80
|
712 {"process_block", process_block, METH_VARARGS,
|
Chris@80
|
713 "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
|
714
|
Chris@80
|
715 {"get_remaining_features", get_remaining_features, METH_NOARGS,
|
Chris@80
|
716 "get_remaining_features() -> Obtain any features extracted at the end of processing."},
|
Chris@35
|
717
|
Chris@39
|
718 {"unload", unload, METH_NOARGS,
|
Chris@39
|
719 "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
|
720
|
Chris@21
|
721 {0, 0}
|
Chris@21
|
722 };
|
Chris@21
|
723
|
Chris@21
|
724 /* Doc:: 10.3 Type Objects */ /* static */
|
Chris@21
|
725 PyTypeObject Plugin_Type =
|
Chris@21
|
726 {
|
Chris@21
|
727 PyObject_HEAD_INIT(NULL)
|
Chris@39
|
728 0, /*ob_size*/
|
Chris@39
|
729 "vampyhost.Plugin", /*tp_name*/
|
Chris@39
|
730 sizeof(PyPluginObject), /*tp_basicsize*/
|
Chris@39
|
731 0, /*tp_itemsize*/
|
Chris@21
|
732 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
|
Chris@39
|
733 0, /*tp_print*/
|
Chris@39
|
734 0, /*tp_getattr*/
|
Chris@39
|
735 0, /*tp_setattr*/
|
Chris@39
|
736 0, /*tp_compare*/
|
Chris@39
|
737 0, /*tp_repr*/
|
Chris@39
|
738 0, /*tp_as_number*/
|
Chris@39
|
739 0, /*tp_as_sequence*/
|
Chris@39
|
740 0, /*tp_as_mapping*/
|
Chris@39
|
741 0, /*tp_hash*/
|
Chris@39
|
742 0, /*tp_call*/
|
Chris@39
|
743 0, /*tp_str*/
|
Chris@39
|
744 PyObject_GenericGetAttr, /*tp_getattro*/
|
Chris@39
|
745 PyObject_GenericSetAttr, /*tp_setattro*/
|
Chris@39
|
746 0, /*tp_as_buffer*/
|
Chris@39
|
747 Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
Chris@40
|
748 "Plugin object, providing a low-level API for running a Vamp plugin.", /*tp_doc*/
|
Chris@39
|
749 0, /*tp_traverse*/
|
Chris@39
|
750 0, /*tp_clear*/
|
Chris@39
|
751 0, /*tp_richcompare*/
|
Chris@39
|
752 0, /*tp_weaklistoffset*/
|
Chris@39
|
753 0, /*tp_iter*/
|
Chris@39
|
754 0, /*tp_iternext*/
|
Chris@39
|
755 PyPluginObject_methods, /*tp_methods*/
|
Chris@39
|
756 PyPluginObject_members, /*tp_members*/
|
Chris@39
|
757 0, /*tp_getset*/
|
Chris@39
|
758 0, /*tp_base*/
|
Chris@39
|
759 0, /*tp_dict*/
|
Chris@39
|
760 0, /*tp_descr_get*/
|
Chris@39
|
761 0, /*tp_descr_set*/
|
Chris@39
|
762 0, /*tp_dictoffset*/
|
Chris@39
|
763 0, /*tp_init*/
|
Chris@39
|
764 PyType_GenericAlloc, /*tp_alloc*/
|
Chris@39
|
765 0, /*tp_new*/
|
Chris@39
|
766 PyObject_Del, /*tp_free*/
|
Chris@39
|
767 0, /*tp_is_gc*/
|
Chris@21
|
768 };
|
Chris@0
|
769
|