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