comparison vampyhost.cpp @ 23:9dc444c79aa5

Pull out the functions that act on a plugin from module to plugin object scope. Also reverse previous decision to remove unload(), I think it is needed
author Chris Cannam
date Tue, 25 Nov 2014 17:39:27 +0000
parents ccb5fef13104
children 392878bea53d
comparison
equal deleted inserted replaced
22:ccb5fef13104 23:9dc444c79aa5
45 45
46 // structure for holding plugin instance data 46 // structure for holding plugin instance data
47 struct PyPluginObject 47 struct PyPluginObject
48 { 48 {
49 PyObject_HEAD 49 PyObject_HEAD
50 /*
51 PyPluginData(string k, Plugin *p, float rate) :
52 key(k),
53 plugin(p),
54 inputSampleRate(rate),
55 isInitialised(false),
56 channels(0),
57 blockSize(0),
58 stepSize(0) {
59 }
60 */
61 string *key; 50 string *key;
62 Plugin *plugin; 51 Plugin *plugin;
63 float inputSampleRate; 52 float inputSampleRate;
64 bool isInitialised; 53 bool isInitialised;
65 size_t channels; 54 size_t channels;
78 delete self->key; 67 delete self->key;
79 delete self->plugin; 68 delete self->plugin;
80 PyObject_Del(self); 69 PyObject_Del(self);
81 } 70 }
82 71
83 /* MODULE HELPER FUNCTIONS */
84 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!! 72 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
85 73
86 PyPluginObject * 74 PyPluginObject *
87 getPluginObject(PyObject *pyPluginHandle) 75 getPluginObject(PyObject *pyPluginHandle)
88 { 76 {
100 return pd; 88 return pd;
101 } 89 }
102 } 90 }
103 91
104 static PyObject * 92 static PyObject *
105 vampyhost_enumeratePlugins(PyObject *self, PyObject *args) 93 vampyhost_enumeratePlugins(PyObject *self, PyObject *)
106 { 94 {
107 cerr << "vampyhost_enumeratePlugins" << endl; 95 cerr << "vampyhost_enumeratePlugins" << endl;
108 96
109 PluginLoader *loader = PluginLoader::getInstance(); 97 PluginLoader *loader = PluginLoader::getInstance();
110 vector<PluginLoader::PluginKey> plugins = loader->listPlugins(); 98 vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
111 PyTypeConversions conv; 99 PyTypeConversions conv;
112 return conv.PyValue_From_StringVector(plugins); 100 return conv.PyValue_From_StringVector(plugins);
113 } 101 }
114 102
115 static PyObject * 103 static PyObject *
116 vampyhost_getPluginPath(PyObject *self, PyObject *args) 104 vampyhost_getPluginPath(PyObject *self, PyObject *)
117 { 105 {
118 cerr << "vampyhost_getPluginPath" << endl; 106 cerr << "vampyhost_getPluginPath" << endl;
119 107
120 vector<string> path = PluginHostAdapter::getPluginPath(); 108 vector<string> path = PluginHostAdapter::getPluginPath();
121 PyTypeConversions conv; 109 PyTypeConversions conv;
282 static PyObject * 270 static PyObject *
283 vampyhost_initialise(PyObject *self, PyObject *args) 271 vampyhost_initialise(PyObject *self, PyObject *args)
284 { 272 {
285 cerr << "vampyhost_initialise" << endl; 273 cerr << "vampyhost_initialise" << endl;
286 274
287 PyObject *pyPluginHandle;
288 size_t channels, blockSize, stepSize; 275 size_t channels, blockSize, stepSize;
289 276
290 if (!PyArg_ParseTuple (args, "Onnn", &pyPluginHandle, 277 if (!PyArg_ParseTuple (args, "nnn",
291 (size_t) &channels, 278 (size_t) &channels,
292 (size_t) &stepSize, 279 (size_t) &stepSize,
293 (size_t) &blockSize)) 280 (size_t) &blockSize)) {
294 { 281 PyErr_SetString(PyExc_TypeError,
295 PyErr_SetString(PyExc_TypeError, 282 "initialise() takes channel count, step size, and block size arguments");
296 "initialise() takes plugin handle (object), channel count, step size, and block size arguments");
297 return 0; 283 return 0;
298 } 284 }
299 285
300 PyPluginObject *pd = getPluginObject(pyPluginHandle); 286 PyPluginObject *pd = getPluginObject(self);
301 if (!pd) return 0; 287 if (!pd) return 0;
302 288
303 pd->channels = channels; 289 pd->channels = channels;
304 pd->stepSize = stepSize; 290 pd->stepSize = stepSize;
305 pd->blockSize = blockSize; 291 pd->blockSize = blockSize;
315 301
316 return Py_True; 302 return Py_True;
317 } 303 }
318 304
319 static PyObject * 305 static PyObject *
320 vampyhost_reset(PyObject *self, PyObject *args) 306 vampyhost_reset(PyObject *self, PyObject *)
321 { 307 {
322 cerr << "vampyhost_reset" << endl; 308 cerr << "vampyhost_reset" << endl;
323 309
324 PyObject *pyPluginHandle; 310 PyPluginObject *pd = getPluginObject(self);
325
326 if (!PyArg_ParseTuple (args, "O", &pyPluginHandle))
327 {
328 PyErr_SetString(PyExc_TypeError,
329 "initialise() takes plugin handle (object) argument");
330 return 0;
331 }
332
333 PyPluginObject *pd = getPluginObject(pyPluginHandle);
334 if (!pd) return 0; 311 if (!pd) return 0;
335 312
336 if (!pd->isInitialised) { 313 if (!pd->isInitialised) {
337 PyErr_SetString(PyExc_StandardError, 314 PyErr_SetString(PyExc_StandardError,
338 "Plugin has not been initialised"); 315 "Plugin has not been initialised");
346 static PyObject * 323 static PyObject *
347 vampyhost_getParameter(PyObject *self, PyObject *args) 324 vampyhost_getParameter(PyObject *self, PyObject *args)
348 { 325 {
349 cerr << "vampyhost_getParameter" << endl; 326 cerr << "vampyhost_getParameter" << endl;
350 327
351 PyObject *pyPluginHandle;
352 PyObject *pyParam; 328 PyObject *pyParam;
353 329
354 if (!PyArg_ParseTuple(args, "OS", &pyPluginHandle, &pyParam)) { 330 if (!PyArg_ParseTuple(args, "S", &pyParam)) {
355 PyErr_SetString(PyExc_TypeError, 331 PyErr_SetString(PyExc_TypeError,
356 "getParameter() takes plugin handle (object) and parameter id (string) arguments"); 332 "getParameter() takes parameter id (string) argument");
357 return 0; } 333 return 0; }
358 334
359 PyPluginObject *pd = getPluginObject(pyPluginHandle); 335 PyPluginObject *pd = getPluginObject(self);
360 if (!pd) return 0; 336 if (!pd) return 0;
361 337
362 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam)); 338 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam));
363 return PyFloat_FromDouble(double(value)); 339 return PyFloat_FromDouble(double(value));
364 } 340 }
366 static PyObject * 342 static PyObject *
367 vampyhost_setParameter(PyObject *self, PyObject *args) 343 vampyhost_setParameter(PyObject *self, PyObject *args)
368 { 344 {
369 cerr << "vampyhost_setParameter" << endl; 345 cerr << "vampyhost_setParameter" << endl;
370 346
371 PyObject *pyPluginHandle;
372 PyObject *pyParam; 347 PyObject *pyParam;
373 float value; 348 float value;
374 349
375 if (!PyArg_ParseTuple(args, "OSf", &pyPluginHandle, &pyParam, &value)) { 350 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
376 PyErr_SetString(PyExc_TypeError, 351 PyErr_SetString(PyExc_TypeError,
377 "setParameter() takes plugin handle (object), parameter id (string), and value (float) arguments"); 352 "setParameter() takes parameter id (string), and value (float) arguments");
378 return 0; } 353 return 0; }
379 354
380 PyPluginObject *pd = getPluginObject(pyPluginHandle); 355 PyPluginObject *pd = getPluginObject(self);
381 if (!pd) return 0; 356 if (!pd) return 0;
382 357
383 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value); 358 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value);
384 return Py_True; 359 return Py_True;
385 } 360 }
387 static PyObject * 362 static PyObject *
388 vampyhost_process(PyObject *self, PyObject *args) 363 vampyhost_process(PyObject *self, PyObject *args)
389 { 364 {
390 cerr << "vampyhost_process" << endl; 365 cerr << "vampyhost_process" << endl;
391 366
392 PyObject *pyPluginHandle;
393 PyObject *pyBuffer; 367 PyObject *pyBuffer;
394 PyObject *pyRealTime; 368 PyObject *pyRealTime;
395 369
396 if (!PyArg_ParseTuple(args, "OOO", 370 if (!PyArg_ParseTuple(args, "OO",
397 &pyPluginHandle, // C object holding a pointer to a plugin and its descriptor
398 &pyBuffer, // Audio data 371 &pyBuffer, // Audio data
399 &pyRealTime)) { // TimeStamp 372 &pyRealTime)) { // TimeStamp
400 PyErr_SetString(PyExc_TypeError, 373 PyErr_SetString(PyExc_TypeError,
401 "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments"); 374 "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments");
402 return 0; } 375 return 0; }
408 if (!PyList_Check(pyBuffer)) { 381 if (!PyList_Check(pyBuffer)) {
409 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input."); 382 PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input.");
410 return 0; 383 return 0;
411 } 384 }
412 385
413 PyPluginObject *pd = getPluginObject(pyPluginHandle); 386 PyPluginObject *pd = getPluginObject(self);
414 if (!pd) return 0; 387 if (!pd) return 0;
415 388
416 if (!pd->isInitialised) { 389 if (!pd->isInitialised) {
417 PyErr_SetString(PyExc_StandardError, 390 PyErr_SetString(PyExc_StandardError,
418 "Plugin has not been initialised."); 391 "Plugin has not been initialised.");
509 cerr << "no you fool, here!" << endl; 482 cerr << "no you fool, here!" << endl;
510 483
511 return pyFs; 484 return pyFs;
512 } 485 }
513 486
487 static PyObject *
488 vampyhost_unload(PyObject *self, PyObject *)
489 {
490 cerr << "vampyhost_unloadPlugin" << endl;
491
492 PyPluginObject *pd = getPluginObject(self);
493 if (!pd) return 0;
494
495 delete pd->plugin;
496 pd->plugin = 0; // This is checked by getPluginObject, so we
497 // attempt to avoid repeated calls from blowing up
498
499 return Py_True;
500 }
501
514 static PyMethodDef PyPluginObject_methods[] = 502 static PyMethodDef PyPluginObject_methods[] =
515 { 503 {
504 {"getParameter", vampyhost_getParameter, METH_VARARGS,
505 xx_foo_doc}, //!!! fix all these!
506
507 {"setParameter", vampyhost_setParameter, METH_VARARGS,
508 xx_foo_doc},
509
510 {"initialise", vampyhost_initialise, METH_VARARGS,
511 xx_foo_doc},
512
513 {"reset", vampyhost_reset, METH_NOARGS,
514 xx_foo_doc},
515
516 {"process", vampyhost_process, METH_VARARGS,
517 xx_foo_doc},
518
519 {"unload", vampyhost_unload, METH_NOARGS,
520 xx_foo_doc},
521
516 {0, 0} 522 {0, 0}
517 }; 523 };
524
525 static int
526 PyPluginObject_setattr(PyPluginObject *self, char *name, PyObject *value)
527 {
528 return -1;
529 }
530
531 static PyObject *
532 PyPluginObject_getattr(PyPluginObject *self, char *name)
533 {
534 return Py_FindMethod(PyPluginObject_methods, (PyObject *)self, name);
535 }
518 536
519 /* Doc:: 10.3 Type Objects */ /* static */ 537 /* Doc:: 10.3 Type Objects */ /* static */
520 PyTypeObject Plugin_Type = 538 PyTypeObject Plugin_Type =
521 { 539 {
522 PyObject_HEAD_INIT(NULL) 540 PyObject_HEAD_INIT(NULL)
524 "vampyhost.Plugin", /*tp_name*/ 542 "vampyhost.Plugin", /*tp_name*/
525 sizeof(PyPluginObject), /*tp_basicsize*/ 543 sizeof(PyPluginObject), /*tp_basicsize*/
526 0, /*tp_itemsize*/ 544 0, /*tp_itemsize*/
527 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/ 545 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
528 0, /*tp_print*/ 546 0, /*tp_print*/
529 0, /*tp_getattr*/ 547 (getattrfunc)PyPluginObject_getattr, /*tp_getattr*/
530 0, /*tp_setattr*/ 548 (setattrfunc)PyPluginObject_setattr, /*tp_setattr*/
531 0, /*tp_compare*/ 549 0, /*tp_compare*/
532 0, /*tp_repr*/ 550 0, /*tp_repr*/
533 0, /*tp_as_number*/ 551 0, /*tp_as_number*/
534 0, /*tp_as_sequence*/ 552 0, /*tp_as_sequence*/
535 0, /*tp_as_mapping*/ 553 0, /*tp_as_mapping*/
581 xx_foo_doc}, 599 xx_foo_doc},
582 600
583 {"loadPlugin", vampyhost_loadPlugin, METH_VARARGS, 601 {"loadPlugin", vampyhost_loadPlugin, METH_VARARGS,
584 xx_foo_doc}, 602 xx_foo_doc},
585 603
586 {"getParameter", vampyhost_getParameter, METH_VARARGS,
587 xx_foo_doc},
588
589 {"setParameter", vampyhost_setParameter, METH_VARARGS,
590 xx_foo_doc},
591
592 {"initialise", vampyhost_initialise, METH_VARARGS,
593 xx_foo_doc},
594
595 {"reset", vampyhost_reset, METH_VARARGS,
596 xx_foo_doc},
597
598 {"process", vampyhost_process, METH_VARARGS,
599 xx_foo_doc},
600
601 {0, 0} /* sentinel */ 604 {0, 0} /* sentinel */
602 }; 605 };
603 606
604 PyPluginObject * 607 PyPluginObject *
605 PyPluginObject::create_internal() 608 PyPluginObject::create_internal()