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