Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@26: /* Chris@26: VampyHost Chris@26: Chris@26: Use Vamp audio analysis plugins in Python Chris@26: Chris@26: Gyorgy Fazekas and Chris Cannam Chris@26: Centre for Digital Music, Queen Mary, University of London Chris@26: Copyright 2008-2014 Queen Mary, University of London Chris@26: Chris@26: Permission is hereby granted, free of charge, to any person Chris@26: obtaining a copy of this software and associated documentation Chris@26: files (the "Software"), to deal in the Software without Chris@26: restriction, including without limitation the rights to use, copy, Chris@26: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@26: of the Software, and to permit persons to whom the Software is Chris@26: furnished to do so, subject to the following conditions: Chris@26: Chris@26: The above copyright notice and this permission notice shall be Chris@26: included in all copies or substantial portions of the Software. Chris@26: Chris@26: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@26: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@26: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@26: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@26: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@26: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@26: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@26: Chris@26: Except as contained in this notice, the names of the Centre for Chris@26: Digital Music; Queen Mary, University of London; and the authors Chris@26: shall not be used in advertising or otherwise to promote the sale, Chris@26: use or other dealings in this Software without prior written Chris@26: authorization. Chris@26: */ Chris@26: Chris@31: #include "PyPluginObject.h" Chris@14: Chris@14: // define a unique API pointer Chris@27: #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API Chris@14: #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION Chris@31: #define NO_IMPORT_ARRAY Chris@14: #include "numpy/arrayobject.h" Chris@14: Chris@29: #include "VectorConversion.h" Chris@16: #include "PyRealTime.h" Chris@0: Chris@0: #include Chris@31: #include Chris@0: Chris@0: using namespace std; Chris@0: using namespace Vamp; Chris@0: Chris@21: static void Chris@21: PyPluginObject_dealloc(PyPluginObject *self) Chris@21: { Chris@21: cerr << "PyPluginObject_dealloc" << endl; Chris@21: delete self->plugin; Chris@21: PyObject_Del(self); Chris@21: } Chris@21: Chris@2: PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!! Chris@0: Chris@28: //!!! todo: conv errors Chris@28: Chris@31: static Chris@21: PyPluginObject * Chris@21: getPluginObject(PyObject *pyPluginHandle) Chris@21: { Chris@21: cerr << "getPluginObject" << endl; Chris@0: Chris@21: PyPluginObject *pd = 0; Chris@21: if (PyPlugin_Check(pyPluginHandle)) { Chris@21: pd = (PyPluginObject *)pyPluginHandle; Chris@16: } Chris@16: if (!pd || !pd->plugin) { Chris@16: PyErr_SetString(PyExc_AttributeError, Chris@16: "Invalid or already deleted plugin handle."); Chris@16: return 0; Chris@0: } else { Chris@16: return pd; Chris@0: } Chris@0: } Chris@0: Chris@31: PyObject * Chris@31: PyPluginObject_From_Plugin(Plugin *plugin) Chris@0: { Chris@31: PyPluginObject *pd = Chris@31: (PyPluginObject *)PyType_GenericAlloc(&Plugin_Type, 0); Chris@21: pd->plugin = plugin; Chris@21: pd->isInitialised = false; Chris@21: pd->channels = 0; Chris@21: pd->blockSize = 0; Chris@21: pd->stepSize = 0; Chris@21: return (PyObject *)pd; Chris@0: } Chris@0: Chris@0: static PyObject * Chris@0: vampyhost_initialise(PyObject *self, PyObject *args) Chris@0: { Chris@21: cerr << "vampyhost_initialise" << endl; Chris@21: luis@7: size_t channels, blockSize, stepSize; Chris@0: Chris@23: if (!PyArg_ParseTuple (args, "nnn", luis@7: (size_t) &channels, luis@7: (size_t) &stepSize, Chris@23: (size_t) &blockSize)) { Chris@0: PyErr_SetString(PyExc_TypeError, Chris@23: "initialise() takes channel count, step size, and block size arguments"); Chris@16: return 0; Chris@0: } Chris@0: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@16: if (!pd) return 0; Chris@0: Chris@16: pd->channels = channels; Chris@16: pd->stepSize = stepSize; Chris@16: pd->blockSize = blockSize; Chris@0: Chris@16: if (!pd->plugin->initialise(channels, stepSize, blockSize)) { Chris@17: cerr << "Failed to initialise native plugin adapter with channels = " << channels << ", stepSize = " << stepSize << ", blockSize = " << blockSize << " and ADAPT_ALL_SAFE set" << endl; Chris@0: PyErr_SetString(PyExc_TypeError, Chris@17: "Plugin initialization failed"); Chris@16: return 0; Chris@6: } Chris@0: Chris@16: pd->isInitialised = true; luis@7: Chris@0: return Py_True; Chris@0: } Chris@0: Chris@0: static PyObject * Chris@23: vampyhost_reset(PyObject *self, PyObject *) Chris@18: { Chris@21: cerr << "vampyhost_reset" << endl; Chris@21: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@18: if (!pd) return 0; Chris@18: Chris@18: if (!pd->isInitialised) { Chris@18: PyErr_SetString(PyExc_StandardError, Chris@18: "Plugin has not been initialised"); Chris@18: return 0; Chris@18: } Chris@18: Chris@18: pd->plugin->reset(); Chris@18: return Py_True; Chris@18: } Chris@18: Chris@18: static PyObject * Chris@20: vampyhost_getParameter(PyObject *self, PyObject *args) Chris@20: { Chris@21: cerr << "vampyhost_getParameter" << endl; Chris@21: Chris@20: PyObject *pyParam; Chris@20: Chris@23: if (!PyArg_ParseTuple(args, "S", &pyParam)) { Chris@20: PyErr_SetString(PyExc_TypeError, Chris@23: "getParameter() takes parameter id (string) argument"); Chris@20: return 0; } Chris@20: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@20: if (!pd) return 0; Chris@20: Chris@20: float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam)); Chris@20: return PyFloat_FromDouble(double(value)); Chris@20: } Chris@20: Chris@20: static PyObject * Chris@20: vampyhost_setParameter(PyObject *self, PyObject *args) Chris@20: { Chris@21: cerr << "vampyhost_setParameter" << endl; Chris@21: Chris@20: PyObject *pyParam; Chris@20: float value; Chris@20: Chris@23: if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) { Chris@20: PyErr_SetString(PyExc_TypeError, Chris@23: "setParameter() takes parameter id (string), and value (float) arguments"); Chris@20: return 0; } Chris@20: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@20: if (!pd) return 0; Chris@20: Chris@20: pd->plugin->setParameter(PyString_AS_STRING(pyParam), value); Chris@20: return Py_True; Chris@20: } Chris@20: Chris@20: static PyObject * Chris@0: vampyhost_process(PyObject *self, PyObject *args) Chris@0: { Chris@21: cerr << "vampyhost_process" << endl; Chris@21: Chris@0: PyObject *pyBuffer; Chris@0: PyObject *pyRealTime; Chris@0: Chris@23: if (!PyArg_ParseTuple(args, "OO", Chris@0: &pyBuffer, // Audio data Chris@0: &pyRealTime)) { // TimeStamp Chris@0: PyErr_SetString(PyExc_TypeError, Chris@17: "process() takes plugin handle (object), buffer (2D array of channels * samples floats) and timestamp (RealTime) arguments"); Chris@16: return 0; } Chris@0: Chris@0: if (!PyRealTime_Check(pyRealTime)) { Chris@0: PyErr_SetString(PyExc_TypeError,"Valid timestamp required."); Chris@16: return 0; } Chris@0: Chris@17: if (!PyList_Check(pyBuffer)) { Chris@17: PyErr_SetString(PyExc_TypeError, "List of NumPy Array required for process input."); Chris@17: return 0; Chris@17: } Chris@17: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@16: if (!pd) return 0; Chris@0: Chris@0: if (!pd->isInitialised) { Chris@0: PyErr_SetString(PyExc_StandardError, Chris@0: "Plugin has not been initialised."); Chris@16: return 0; Chris@16: } Chris@0: Chris@12: int channels = pd->channels; Chris@0: Chris@4: if (PyList_GET_SIZE(pyBuffer) != channels) { Chris@17: cerr << "Wrong number of channels: got " << PyList_GET_SIZE(pyBuffer) << ", expected " << channels << endl; Chris@4: PyErr_SetString(PyExc_TypeError, "Wrong number of channels"); Chris@16: return 0; Chris@4: } Chris@0: Chris@4: float **inbuf = new float *[channels]; Chris@0: Chris@29: VectorConversion typeConv; Chris@17: Chris@12: vector > data; Chris@4: for (int c = 0; c < channels; ++c) { Chris@4: PyObject *cbuf = PyList_GET_ITEM(pyBuffer, c); Chris@17: data.push_back(typeConv.PyValue_To_FloatVector(cbuf)); Chris@12: } Chris@12: Chris@12: for (int c = 0; c < channels; ++c) { Chris@17: if (data[c].size() != pd->blockSize) { Chris@17: cerr << "Wrong number of samples on channel " << c << ": expected " << pd->blockSize << " (plugin's block size), got " << data[c].size() << endl; Chris@17: PyErr_SetString(PyExc_TypeError, "Wrong number of samples"); Chris@17: return 0; Chris@17: } Chris@12: inbuf[c] = &data[c][0]; Chris@4: } Chris@0: Chris@12: RealTime timeStamp = *PyRealTime_AsRealTime(pyRealTime); Chris@0: Chris@18: Plugin::FeatureSet fs = pd->plugin->process(inbuf, timeStamp); Chris@0: Chris@4: delete[] inbuf; Chris@0: Chris@29: VectorConversion conv; Chris@18: Chris@18: PyObject *pyFs = PyDict_New(); Chris@0: Chris@18: for (Plugin::FeatureSet::const_iterator fsi = fs.begin(); Chris@18: fsi != fs.end(); ++fsi) { Chris@18: Chris@18: int fno = fsi->first; Chris@18: const Plugin::FeatureList &fl = fsi->second; Chris@18: Chris@18: if (!fl.empty()) { Chris@18: Chris@18: PyObject *pyFl = PyList_New(fl.size()); Chris@18: Chris@18: for (int fli = 0; fli < (int)fl.size(); ++fli) { Chris@18: Chris@18: const Plugin::Feature &f = fl[fli]; Chris@18: PyObject *pyF = PyDict_New(); Chris@18: Chris@18: if (f.hasTimestamp) { Chris@18: PyDict_SetItemString Chris@18: (pyF, "timestamp", PyRealTime_FromRealTime(f.timestamp)); Chris@18: } Chris@18: if (f.hasDuration) { Chris@18: PyDict_SetItemString Chris@18: (pyF, "duration", PyRealTime_FromRealTime(f.duration)); Chris@18: } Chris@18: Chris@18: PyDict_SetItemString Chris@18: (pyF, "label", PyString_FromString(f.label.c_str())); Chris@18: Chris@18: if (!f.values.empty()) { Chris@18: PyDict_SetItemString Chris@28: (pyF, "values", conv.PyArray_From_FloatVector(f.values)); Chris@18: } Chris@18: Chris@18: PyList_SET_ITEM(pyFl, fli, pyF); Chris@18: } Chris@18: Chris@18: PyObject *pyN = PyInt_FromLong(fno); Chris@18: PyDict_SetItem(pyFs, pyN, pyFl); Chris@18: } Chris@18: } Chris@18: Chris@18: return pyFs; Chris@0: } Chris@0: Chris@23: static PyObject * Chris@23: vampyhost_unload(PyObject *self, PyObject *) Chris@23: { Chris@23: cerr << "vampyhost_unloadPlugin" << endl; Chris@23: Chris@23: PyPluginObject *pd = getPluginObject(self); Chris@23: if (!pd) return 0; Chris@23: Chris@23: delete pd->plugin; Chris@32: pd->plugin = 0; // This is checked by getPluginObject, so we avoid Chris@32: // blowing up if called repeatedly Chris@23: Chris@23: return Py_True; Chris@23: } Chris@23: Chris@21: static PyMethodDef PyPluginObject_methods[] = Chris@21: { Chris@23: {"getParameter", vampyhost_getParameter, METH_VARARGS, Chris@23: xx_foo_doc}, //!!! fix all these! Chris@23: Chris@23: {"setParameter", vampyhost_setParameter, METH_VARARGS, Chris@23: xx_foo_doc}, Chris@23: Chris@23: {"initialise", vampyhost_initialise, METH_VARARGS, Chris@23: xx_foo_doc}, Chris@23: Chris@23: {"reset", vampyhost_reset, METH_NOARGS, Chris@23: xx_foo_doc}, Chris@23: Chris@23: {"process", vampyhost_process, METH_VARARGS, Chris@23: xx_foo_doc}, Chris@23: Chris@23: {"unload", vampyhost_unload, METH_NOARGS, Chris@23: xx_foo_doc}, Chris@23: Chris@21: {0, 0} Chris@21: }; Chris@21: Chris@23: static int Chris@23: PyPluginObject_setattr(PyPluginObject *self, char *name, PyObject *value) Chris@23: { Chris@23: return -1; Chris@23: } Chris@23: Chris@23: static PyObject * Chris@23: PyPluginObject_getattr(PyPluginObject *self, char *name) Chris@23: { Chris@23: return Py_FindMethod(PyPluginObject_methods, (PyObject *)self, name); Chris@23: } Chris@23: Chris@21: /* Doc:: 10.3 Type Objects */ /* static */ Chris@21: PyTypeObject Plugin_Type = Chris@21: { Chris@21: PyObject_HEAD_INIT(NULL) Chris@21: 0, /*ob_size*/ Chris@21: "vampyhost.Plugin", /*tp_name*/ Chris@21: sizeof(PyPluginObject), /*tp_basicsize*/ Chris@21: 0, /*tp_itemsize*/ Chris@21: (destructor)PyPluginObject_dealloc, /*tp_dealloc*/ Chris@21: 0, /*tp_print*/ Chris@23: (getattrfunc)PyPluginObject_getattr, /*tp_getattr*/ Chris@23: (setattrfunc)PyPluginObject_setattr, /*tp_setattr*/ Chris@21: 0, /*tp_compare*/ Chris@21: 0, /*tp_repr*/ Chris@21: 0, /*tp_as_number*/ Chris@21: 0, /*tp_as_sequence*/ Chris@21: 0, /*tp_as_mapping*/ Chris@21: 0, /*tp_hash*/ Chris@21: 0, /*tp_call*/ Chris@21: 0, /*tp_str*/ Chris@21: 0, /*tp_getattro*/ Chris@21: 0, /*tp_setattro*/ Chris@21: 0, /*tp_as_buffer*/ Chris@21: Py_TPFLAGS_DEFAULT, /*tp_flags*/ Chris@21: "Plugin Object", /*tp_doc*/ Chris@21: 0, /*tp_traverse*/ Chris@21: 0, /*tp_clear*/ Chris@21: 0, /*tp_richcompare*/ Chris@21: 0, /*tp_weaklistoffset*/ Chris@21: 0, /*tp_iter*/ Chris@21: 0, /*tp_iternext*/ Chris@21: PyPluginObject_methods, /*tp_methods*/ Chris@21: 0, /*tp_members*/ Chris@21: 0, /*tp_getset*/ Chris@21: 0, /*tp_base*/ Chris@21: 0, /*tp_dict*/ Chris@21: 0, /*tp_descr_get*/ Chris@21: 0, /*tp_descr_set*/ Chris@21: 0, /*tp_dictoffset*/ Chris@21: 0, /*tp_init*/ Chris@21: PyType_GenericAlloc, /*tp_alloc*/ Chris@21: 0, /*tp_new*/ Chris@21: PyObject_Del, /*tp_free*/ Chris@21: 0, /*tp_is_gc*/ Chris@21: }; Chris@0: