annotate PyPluginObject.cpp @ 31:f565f4b5cbaa

Pull out plugin object implementation into separate source file
author Chris Cannam
date Wed, 26 Nov 2014 11:12:00 +0000
parents vampyhost.cpp@7e7f2f7d9542
children d5aba4c3c229
rev   line source
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@29 46 #include "VectorConversion.h"
Chris@16 47 #include "PyRealTime.h"
Chris@0 48
Chris@0 49 #include <string>
Chris@31 50 #include <vector>
Chris@0 51
Chris@0 52 using namespace std;
Chris@0 53 using namespace Vamp;
Chris@0 54
Chris@21 55 static void
Chris@21 56 PyPluginObject_dealloc(PyPluginObject *self)
Chris@21 57 {
Chris@21 58 cerr << "PyPluginObject_dealloc" << endl;
Chris@21 59 delete self->plugin;
Chris@21 60 PyObject_Del(self);
Chris@21 61 }
Chris@21 62
Chris@2 63 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
Chris@0 64
Chris@28 65 //!!! todo: conv errors
Chris@28 66
Chris@31 67 static
Chris@21 68 PyPluginObject *
Chris@21 69 getPluginObject(PyObject *pyPluginHandle)
Chris@21 70 {
Chris@21 71 cerr << "getPluginObject" << endl;
Chris@0 72
Chris@21 73 PyPluginObject *pd = 0;
Chris@21 74 if (PyPlugin_Check(pyPluginHandle)) {
Chris@21 75 pd = (PyPluginObject *)pyPluginHandle;
Chris@16 76 }
Chris@16 77 if (!pd || !pd->plugin) {
Chris@16 78 PyErr_SetString(PyExc_AttributeError,
Chris@16 79 "Invalid or already deleted plugin handle.");
Chris@16 80 return 0;
Chris@0 81 } else {
Chris@16 82 return pd;
Chris@0 83 }
Chris@0 84 }
Chris@0 85
Chris@31 86 PyObject *
Chris@31 87 PyPluginObject_From_Plugin(Plugin *plugin)
Chris@0 88 {
Chris@31 89 PyPluginObject *pd =
Chris@31 90 (PyPluginObject *)PyType_GenericAlloc(&Plugin_Type, 0);
Chris@21 91 pd->plugin = plugin;
Chris@21 92 pd->isInitialised = false;
Chris@21 93 pd->channels = 0;
Chris@21 94 pd->blockSize = 0;
Chris@21 95 pd->stepSize = 0;
Chris@21 96 return (PyObject *)pd;
Chris@0 97 }
Chris@0 98
Chris@0 99 static PyObject *
Chris@0 100 vampyhost_initialise(PyObject *self, PyObject *args)
Chris@0 101 {
Chris@21 102 cerr << "vampyhost_initialise" << endl;
Chris@21 103
luis@7 104 size_t channels, blockSize, stepSize;
Chris@0 105
Chris@23 106 if (!PyArg_ParseTuple (args, "nnn",
luis@7 107 (size_t) &channels,
luis@7 108 (size_t) &stepSize,
Chris@23 109 (size_t) &blockSize)) {
Chris@0 110 PyErr_SetString(PyExc_TypeError,
Chris@23 111 "initialise() takes channel count, step size, and block size arguments");
Chris@16 112 return 0;
Chris@0 113 }
Chris@0 114
Chris@23 115 PyPluginObject *pd = getPluginObject(self);
Chris@16 116 if (!pd) return 0;
Chris@0 117
Chris@16 118 pd->channels = channels;
Chris@16 119 pd->stepSize = stepSize;
Chris@16 120 pd->blockSize = blockSize;
Chris@0 121
Chris@16 122 if (!pd->plugin->initialise(channels, stepSize, blockSize)) {
Chris@17 123 cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << " and ADAPT_ALL_SAFE set" << endl;
Chris@0 124 PyErr_SetString(PyExc_TypeError,
Chris@17 125 "Plugin initialization failed");
Chris@16 126 return 0;
Chris@6 127 }
Chris@0 128
Chris@16 129 pd->isInitialised = true;
luis@7 130
Chris@0 131 return Py_True;
Chris@0 132 }
Chris@0 133
Chris@0 134 static PyObject *
Chris@23 135 vampyhost_reset(PyObject *self, PyObject *)
Chris@18 136 {
Chris@21 137 cerr << "vampyhost_reset" << endl;
Chris@21 138
Chris@23 139 PyPluginObject *pd = getPluginObject(self);
Chris@18 140 if (!pd) return 0;
Chris@18 141
Chris@18 142 if (!pd->isInitialised) {
Chris@18 143 PyErr_SetString(PyExc_StandardError,
Chris@18 144 "Plugin has not been initialised");
Chris@18 145 return 0;
Chris@18 146 }
Chris@18 147
Chris@18 148 pd->plugin->reset();
Chris@18 149 return Py_True;
Chris@18 150 }
Chris@18 151
Chris@18 152 static PyObject *
Chris@20 153 vampyhost_getParameter(PyObject *self, PyObject *args)
Chris@20 154 {
Chris@21 155 cerr << "vampyhost_getParameter" << endl;
Chris@21 156
Chris@20 157 PyObject *pyParam;
Chris@20 158
Chris@23 159 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
Chris@20 160 PyErr_SetString(PyExc_TypeError,
Chris@23 161 "getParameter() takes parameter id (string) argument");
Chris@20 162 return 0; }
Chris@20 163
Chris@23 164 PyPluginObject *pd = getPluginObject(self);
Chris@20 165 if (!pd) return 0;
Chris@20 166
Chris@20 167 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam));
Chris@20 168 return PyFloat_FromDouble(double(value));
Chris@20 169 }
Chris@20 170
Chris@20 171 static PyObject *
Chris@20 172 vampyhost_setParameter(PyObject *self, PyObject *args)
Chris@20 173 {
Chris@21 174 cerr << "vampyhost_setParameter" << endl;
Chris@21 175
Chris@20 176 PyObject *pyParam;
Chris@20 177 float value;
Chris@20 178
Chris@23 179 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
Chris@20 180 PyErr_SetString(PyExc_TypeError,
Chris@23 181 "setParameter() takes parameter id (string), and value (float) arguments");
Chris@20 182 return 0; }
Chris@20 183
Chris@23 184 PyPluginObject *pd = getPluginObject(self);
Chris@20 185 if (!pd) return 0;
Chris@20 186
Chris@20 187 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value);
Chris@20 188 return Py_True;
Chris@20 189 }
Chris@20 190
Chris@20 191 static PyObject *
Chris@0 192 vampyhost_process(PyObject *self, PyObject *args)
Chris@0 193 {
Chris@21 194 cerr << "vampyhost_process" << endl;
Chris@21 195
Chris@0 196 PyObject *pyBuffer;
Chris@0 197 PyObject *pyRealTime;
Chris@0 198
Chris@23 199 if (!PyArg_ParseTuple(args, "OO",
Chris@0 200 &pyBuffer, // Audio data
Chris@0 201 &pyRealTime)) { // TimeStamp
Chris@0 202 PyErr_SetString(PyExc_TypeError,
Chris@17 203 "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments");
Chris@16 204 return 0; }
Chris@0 205
Chris@0 206 if (!PyRealTime_Check(pyRealTime)) {
Chris@0 207 PyErr_SetString(PyExc_TypeError,"Valid timestamp required.");
Chris@16 208 return 0; }
Chris@0 209
Chris@17 210 if (!PyList_Check(pyBuffer)) {
Chris@17 211 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input.");
Chris@17 212 return 0;
Chris@17 213 }
Chris@17 214
Chris@23 215 PyPluginObject *pd = getPluginObject(self);
Chris@16 216 if (!pd) return 0;
Chris@0 217
Chris@0 218 if (!pd->isInitialised) {
Chris@0 219 PyErr_SetString(PyExc_StandardError,
Chris@0 220 "Plugin has not been initialised.");
Chris@16 221 return 0;
Chris@16 222 }
Chris@0 223
Chris@12 224 int channels = pd->channels;
Chris@0 225
Chris@4 226 if (PyList_GET_SIZE(pyBuffer) != channels) {
Chris@17 227 cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl;
Chris@4 228 PyErr_SetString(PyExc_TypeError, "Wrong number of channels");
Chris@16 229 return 0;
Chris@4 230 }
Chris@0 231
Chris@4 232 float **inbuf = new float *[channels];
Chris@0 233
Chris@29 234 VectorConversion typeConv;
Chris@17 235
Chris@17 236 cerr << "here!" << endl;
Chris@12 237
Chris@12 238 vector<vector<float> > data;
Chris@4 239 for (int c = 0; c < channels; ++c) {
Chris@4 240 PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c);
Chris@17 241 data.push_back(typeConv.PyValue_To_FloatVector(cbuf));
Chris@12 242 }
Chris@12 243
Chris@12 244 for (int c = 0; c < channels; ++c) {
Chris@17 245 if (data[c].size() != pd->blockSize) {
Chris@17 246 cerr << "Wrong number of samples on channel " << c << ": expected " << pd->blockSize << " (plugin's block size), got " << data[c].size() << endl;
Chris@17 247 PyErr_SetString(PyExc_TypeError, "Wrong number of samples");
Chris@17 248 return 0;
Chris@17 249 }
Chris@12 250 inbuf[c] = &data[c][0];
Chris@4 251 }
Chris@0 252
Chris@17 253 cerr << "no, here!" << endl;
Chris@17 254
Chris@12 255 RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime);
Chris@0 256
Chris@18 257 cerr << "no no, here!" << endl;
Chris@0 258
Chris@18 259 Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp);
Chris@0 260
Chris@4 261 delete[] inbuf;
Chris@0 262
Chris@18 263 cerr << "no no no, here!" << endl;
Chris@18 264
Chris@29 265 VectorConversion conv;
Chris@18 266
Chris@18 267 PyObject *pyFs = PyDict_New();
Chris@0 268
Chris@18 269 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
Chris@18 270 fsi != fs.end(); ++fsi) {
Chris@18 271
Chris@18 272 int fno = fsi->first;
Chris@18 273 const Plugin::FeatureList &fl = fsi->second;
Chris@18 274
Chris@18 275 if (!fl.empty()) {
Chris@18 276
Chris@18 277 PyObject *pyFl = PyList_New(fl.size());
Chris@18 278
Chris@18 279 for (int fli = 0; fli < (int)fl.size(); ++fli) {
Chris@18 280
Chris@18 281 const Plugin::Feature &f = fl[fli];
Chris@18 282 PyObject *pyF = PyDict_New();
Chris@18 283
Chris@18 284 if (f.hasTimestamp) {
Chris@18 285 PyDict_SetItemString
Chris@18 286 (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp));
Chris@18 287 }
Chris@18 288 if (f.hasDuration) {
Chris@18 289 PyDict_SetItemString
Chris@18 290 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
Chris@18 291 }
Chris@18 292
Chris@18 293 PyDict_SetItemString
Chris@18 294 (pyF, "label", PyString_FromString(f.label.c_str()));
Chris@18 295
Chris@18 296 if (!f.values.empty()) {
Chris@18 297 PyDict_SetItemString
Chris@28 298 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
Chris@18 299 }
Chris@18 300
Chris@18 301 PyList_SET_ITEM(pyFl, fli, pyF);
Chris@18 302 }
Chris@18 303
Chris@18 304 PyObject *pyN = PyInt_FromLong(fno);
Chris@18 305 PyDict_SetItem(pyFs, pyN, pyFl);
Chris@18 306 }
Chris@18 307 }
Chris@18 308
Chris@18 309 cerr << "no you fool, here!" << endl;
Chris@18 310
Chris@18 311 return pyFs;
Chris@0 312 }
Chris@0 313
Chris@23 314 static PyObject *
Chris@23 315 vampyhost_unload(PyObject *self, PyObject *)
Chris@23 316 {
Chris@23 317 cerr << "vampyhost_unloadPlugin" << endl;
Chris@23 318
Chris@23 319 PyPluginObject *pd = getPluginObject(self);
Chris@23 320 if (!pd) return 0;
Chris@23 321
Chris@23 322 delete pd->plugin;
Chris@23 323 pd->plugin = 0; // This is checked by getPluginObject, so we
Chris@23 324 // attempt to avoid repeated calls from blowing up
Chris@23 325
Chris@23 326 return Py_True;
Chris@23 327 }
Chris@23 328
Chris@21 329 static PyMethodDef PyPluginObject_methods[] =
Chris@21 330 {
Chris@23 331 {"getParameter", vampyhost_getParameter, METH_VARARGS,
Chris@23 332 xx_foo_doc}, //!!! fix all these!
Chris@23 333
Chris@23 334 {"setParameter", vampyhost_setParameter, METH_VARARGS,
Chris@23 335 xx_foo_doc},
Chris@23 336
Chris@23 337 {"initialise", vampyhost_initialise, METH_VARARGS,
Chris@23 338 xx_foo_doc},
Chris@23 339
Chris@23 340 {"reset", vampyhost_reset, METH_NOARGS,
Chris@23 341 xx_foo_doc},
Chris@23 342
Chris@23 343 {"process", vampyhost_process, METH_VARARGS,
Chris@23 344 xx_foo_doc},
Chris@23 345
Chris@23 346 {"unload", vampyhost_unload, METH_NOARGS,
Chris@23 347 xx_foo_doc},
Chris@23 348
Chris@21 349 {0, 0}
Chris@21 350 };
Chris@21 351
Chris@23 352 static int
Chris@23 353 PyPluginObject_setattr(PyPluginObject *self, char *name, PyObject *value)
Chris@23 354 {
Chris@23 355 return -1;
Chris@23 356 }
Chris@23 357
Chris@23 358 static PyObject *
Chris@23 359 PyPluginObject_getattr(PyPluginObject *self, char *name)
Chris@23 360 {
Chris@23 361 return Py_FindMethod(PyPluginObject_methods, (PyObject *)self, name);
Chris@23 362 }
Chris@23 363
Chris@21 364 /* Doc:: 10.3 Type Objects */ /* static */
Chris@21 365 PyTypeObject Plugin_Type =
Chris@21 366 {
Chris@21 367 PyObject_HEAD_INIT(NULL)
Chris@21 368 0, /*ob_size*/
Chris@21 369 "vampyhost.Plugin", /*tp_name*/
Chris@21 370 sizeof(PyPluginObject), /*tp_basicsize*/
Chris@21 371 0, /*tp_itemsize*/
Chris@21 372 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
Chris@21 373 0, /*tp_print*/
Chris@23 374 (getattrfunc)PyPluginObject_getattr, /*tp_getattr*/
Chris@23 375 (setattrfunc)PyPluginObject_setattr, /*tp_setattr*/
Chris@21 376 0, /*tp_compare*/
Chris@21 377 0, /*tp_repr*/
Chris@21 378 0, /*tp_as_number*/
Chris@21 379 0, /*tp_as_sequence*/
Chris@21 380 0, /*tp_as_mapping*/
Chris@21 381 0, /*tp_hash*/
Chris@21 382 0, /*tp_call*/
Chris@21 383 0, /*tp_str*/
Chris@21 384 0, /*tp_getattro*/
Chris@21 385 0, /*tp_setattro*/
Chris@21 386 0, /*tp_as_buffer*/
Chris@21 387 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Chris@21 388 "Plugin Object", /*tp_doc*/
Chris@21 389 0, /*tp_traverse*/
Chris@21 390 0, /*tp_clear*/
Chris@21 391 0, /*tp_richcompare*/
Chris@21 392 0, /*tp_weaklistoffset*/
Chris@21 393 0, /*tp_iter*/
Chris@21 394 0, /*tp_iternext*/
Chris@21 395 PyPluginObject_methods, /*tp_methods*/
Chris@21 396 0, /*tp_members*/
Chris@21 397 0, /*tp_getset*/
Chris@21 398 0, /*tp_base*/
Chris@21 399 0, /*tp_dict*/
Chris@21 400 0, /*tp_descr_get*/
Chris@21 401 0, /*tp_descr_set*/
Chris@21 402 0, /*tp_dictoffset*/
Chris@21 403 0, /*tp_init*/
Chris@21 404 PyType_GenericAlloc, /*tp_alloc*/
Chris@21 405 0, /*tp_new*/
Chris@21 406 PyObject_Del, /*tp_free*/
Chris@21 407 0, /*tp_is_gc*/
Chris@21 408 };
Chris@0 409