annotate vampyhost.cpp @ 39:13dcfe8c7ed7

Docs and tidying
author Chris Cannam
date Wed, 26 Nov 2014 15:23:56 +0000
parents 20a9fcbc2f5f
children f12b59c022a5
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@39 38 // include for python extension module: must be first
Chris@0 39 #include <Python.h>
Chris@14 40
Chris@14 41 // define a unique API pointer
Chris@27 42 #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API
Chris@14 43 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
Chris@14 44 #include "numpy/arrayobject.h"
Chris@14 45
Chris@31 46 #include "PyRealTime.h"
Chris@31 47 #include "PyPluginObject.h"
Chris@12 48
Chris@1 49 #include "vamp-hostsdk/PluginHostAdapter.h"
Chris@1 50 #include "vamp-hostsdk/PluginChannelAdapter.h"
Chris@1 51 #include "vamp-hostsdk/PluginInputDomainAdapter.h"
Chris@1 52 #include "vamp-hostsdk/PluginLoader.h"
Chris@16 53
Chris@29 54 #include "VectorConversion.h"
Chris@16 55 #include "PyRealTime.h"
Chris@0 56
Chris@0 57 #include <iostream>
Chris@0 58 #include <string>
Chris@0 59
Chris@0 60 #include <cmath>
Chris@0 61
Chris@0 62 using namespace std;
Chris@0 63 using namespace Vamp;
Chris@31 64 using namespace Vamp::HostExt;
Chris@21 65
Chris@28 66 //!!! todo: conv errors
Chris@28 67
Chris@0 68 static PyObject *
Chris@39 69 enumeratePlugins(PyObject *self, PyObject *)
Chris@0 70 {
Chris@0 71 PluginLoader *loader = PluginLoader::getInstance();
Chris@0 72 vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
Chris@29 73 VectorConversion conv;
Chris@15 74 return conv.PyValue_From_StringVector(plugins);
Chris@0 75 }
Chris@0 76
Chris@15 77 static PyObject *
Chris@39 78 getPluginPath(PyObject *self, PyObject *)
Chris@15 79 {
Chris@15 80 vector<string> path = PluginHostAdapter::getPluginPath();
Chris@29 81 VectorConversion conv;
Chris@15 82 return conv.PyValue_From_StringVector(path);
Chris@15 83 }
Chris@0 84
Chris@15 85 static string toPluginKey(PyObject *pyPluginKey)
Chris@0 86 {
Chris@36 87 // convert to stl string
Chris@0 88 string pluginKey(PyString_AS_STRING(pyPluginKey));
Chris@0 89
Chris@36 90 // check pluginKey validity
Chris@0 91 string::size_type ki = pluginKey.find(':');
Chris@0 92 if (ki == string::npos) {
Chris@39 93 PyErr_SetString(PyExc_TypeError,
Chris@39 94 "Plugin key must be of the form library:identifier");
Chris@39 95 return "";
Chris@0 96 }
Chris@0 97
Chris@15 98 return pluginKey;
Chris@15 99 }
Chris@15 100
Chris@15 101 static PyObject *
Chris@39 102 getLibraryFor(PyObject *self, PyObject *args)
Chris@15 103 {
Chris@15 104 PyObject *pyPluginKey;
Chris@15 105
Chris@15 106 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
Chris@39 107 PyErr_SetString(PyExc_TypeError,
Chris@39 108 "getLibraryPathForPlugin() takes plugin key (string) argument");
Chris@39 109 return 0; }
Chris@15 110
Chris@15 111 string pluginKey = toPluginKey(pyPluginKey);
Chris@16 112 if (pluginKey == "") return 0;
Chris@15 113
Chris@0 114 PluginLoader *loader = PluginLoader::getInstance();
Chris@0 115 string path = loader->getLibraryPathForPlugin(pluginKey);
Chris@0 116 PyObject *pyPath = PyString_FromString(path.c_str());
Chris@0 117 return pyPath;
Chris@0 118 }
Chris@0 119
Chris@0 120 static PyObject *
Chris@39 121 getPluginCategory(PyObject *self, PyObject *args)
Chris@0 122 {
Chris@0 123 PyObject *pyPluginKey;
Chris@0 124
Chris@0 125 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
Chris@39 126 PyErr_SetString(PyExc_TypeError,
Chris@39 127 "getPluginCategory() takes plugin key (string) argument");
Chris@39 128 return 0; }
Chris@0 129
Chris@15 130 string pluginKey = toPluginKey(pyPluginKey);
Chris@16 131 if (pluginKey == "") return 0;
Chris@0 132
Chris@0 133 PluginLoader *loader = PluginLoader::getInstance();
luis@7 134 PluginLoader::PluginCategoryHierarchy
Chris@39 135 category = loader->getPluginCategory(pluginKey);
Chris@0 136
Chris@29 137 VectorConversion conv;
Chris@15 138 return conv.PyValue_From_StringVector(category);
Chris@0 139 }
Chris@0 140
Chris@0 141 static PyObject *
Chris@39 142 getOutputList(PyObject *self, PyObject *args)
Chris@0 143 {
Chris@31 144 PyObject *pyPluginKey;
Chris@31 145
Chris@31 146 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
Chris@39 147 PyErr_SetString(PyExc_TypeError,
Chris@39 148 "getOutputList() takes plugin key (string) argument");
Chris@39 149 return 0; }
Chris@31 150
Chris@15 151 Plugin::OutputList outputs;
Chris@0 152
Chris@31 153 string pluginKey = toPluginKey(pyPluginKey);
Chris@31 154 if (pluginKey == "") return 0;
Chris@31 155
Chris@31 156 PluginLoader *loader = PluginLoader::getInstance();
Chris@31 157
Chris@31 158 Plugin *plugin = loader->loadPlugin
Chris@31 159 (pluginKey, 48000, PluginLoader::ADAPT_ALL_SAFE);
Chris@31 160 if (!plugin) {
Chris@31 161 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
Chris@31 162 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
Chris@31 163 return 0;
Chris@0 164 }
Chris@0 165
Chris@31 166 outputs = plugin->getOutputDescriptors();
luis@7 167
Chris@0 168 PyObject *pyList = PyList_New(outputs.size());
Chris@0 169
Chris@0 170 for (size_t i = 0; i < outputs.size(); ++i) {
Chris@39 171 PyObject *pyOutputId =
Chris@39 172 PyString_FromString(outputs[i].identifier.c_str());
Chris@39 173 PyList_SET_ITEM(pyList, i, pyOutputId);
Chris@0 174 }
Chris@0 175
Chris@0 176 return pyList;
Chris@0 177 }
Chris@0 178
Chris@0 179 static PyObject *
Chris@39 180 loadPlugin(PyObject *self, PyObject *args)
Chris@0 181 {
Chris@0 182 PyObject *pyPluginKey;
Chris@0 183 float inputSampleRate;
Chris@0 184
luis@7 185 if (!PyArg_ParseTuple(args, "Sf",
Chris@39 186 &pyPluginKey,
Chris@39 187 &inputSampleRate)) {
Chris@39 188 PyErr_SetString(PyExc_TypeError,
Chris@39 189 "loadPlugin() takes plugin key (string) and sample rate (float) arguments");
Chris@39 190 return 0; }
Chris@0 191
Chris@15 192 string pluginKey = toPluginKey(pyPluginKey);
Chris@16 193 if (pluginKey == "") return 0;
Chris@0 194
Chris@0 195 PluginLoader *loader = PluginLoader::getInstance();
luis@7 196
Chris@15 197 Plugin *plugin = loader->loadPlugin(pluginKey, inputSampleRate,
Chris@15 198 PluginLoader::ADAPT_ALL_SAFE);
luis@7 199 if (!plugin) {
Chris@39 200 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
Chris@39 201 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
Chris@39 202 return 0;
luis@7 203 }
Chris@15 204
Chris@31 205 return PyPluginObject_From_Plugin(plugin);
Chris@0 206 }
Chris@0 207
Chris@18 208 // module methods table
Chris@0 209 static PyMethodDef vampyhost_methods[] = {
Chris@0 210
Chris@39 211 {"listPlugins", enumeratePlugins, METH_NOARGS,
Chris@39 212 "listPlugins() -> Return a list of the plugin keys of all installed Vamp plugins." },
Chris@0 213
Chris@39 214 {"getPluginPath", getPluginPath, METH_NOARGS,
Chris@39 215 "getPluginPath() -> Return a list of directories which will be searched for Vamp plugins. This may be changed by setting the VAMP_PATH environment variable."},
Chris@15 216
Chris@39 217 {"getCategoryOf", getPluginCategory, METH_VARARGS,
Chris@39 218 "getCategoryOf(pluginKey) -> Return the category of a Vamp plugin given its key, if known. The category is expressed as a list of nested types from least to most specific."},
Chris@0 219
Chris@39 220 {"getLibraryFor", getLibraryFor, METH_VARARGS,
Chris@39 221 "getLibraryFor(pluginKey) -> Return the file path of the Vamp plugin library in which the given plugin key is found, or an empty string if the plugin is not installed."},
Chris@0 222
Chris@39 223 {"getOutputsOf", getOutputList, METH_VARARGS,
Chris@39 224 "getOutputsOf(pluginKey) -> Return a list of the output identifiers of the plugin with the given key, if installed."},
Chris@0 225
Chris@39 226 {"loadPlugin", loadPlugin, METH_VARARGS,
Chris@39 227 "loadPlugin(pluginKey, samplerate) -> Load the plugin that has the given key, if installed, and return the plugin object."},
Chris@0 228
Chris@39 229 {0, 0} /* sentinel */
Chris@0 230 };
Chris@0 231
Chris@39 232 PyDoc_STRVAR(module_doc, "Load and run Vamp audio analysis plugins.");
Chris@0 233
Chris@25 234 static int
Chris@25 235 setint(PyObject *d, const char *name, int value)
Chris@25 236 {
Chris@25 237 PyObject *v;
Chris@25 238 int err;
Chris@25 239 v = PyInt_FromLong((long)value);
Chris@25 240 err = PyDict_SetItemString(d, name, v);
Chris@25 241 Py_XDECREF(v);
Chris@25 242 return err;
Chris@25 243 }
Chris@14 244
Chris@0 245 /* Initialization function for the module (*must* be called initxx) */
Chris@0 246
Chris@25 247 // module initialization (includes extern C {...} as necessary)
Chris@0 248 PyMODINIT_FUNC
Chris@0 249 initvampyhost(void)
Chris@0 250 {
Chris@0 251 PyObject *m;
Chris@0 252
Chris@25 253 if (PyType_Ready(&RealTime_Type) < 0) return;
Chris@25 254 if (PyType_Ready(&Plugin_Type) < 0) return;
Chris@0 255
Chris@0 256 m = Py_InitModule3("vampyhost", vampyhost_methods, module_doc);
Chris@25 257 if (!m) {
Chris@25 258 cerr << "ERROR: initvampyhost: Failed to initialise module" << endl;
Chris@25 259 return;
Chris@25 260 }
Chris@0 261
Chris@14 262 import_array();
Chris@14 263
Chris@17 264 PyModule_AddObject(m, "RealTime", (PyObject *)&RealTime_Type);
Chris@25 265 PyModule_AddObject(m, "Plugin", (PyObject *)&Plugin_Type);
Chris@25 266
Chris@25 267 // Some enum types
Chris@25 268 PyObject *dict = PyModule_GetDict(m);
Chris@25 269 if (!dict) {
Chris@25 270 cerr << "ERROR: initvampyhost: Failed to obtain module dictionary" << endl;
Chris@25 271 return;
Chris@25 272 }
Chris@25 273
Chris@25 274 if (setint(dict, "OneSamplePerStep",
Chris@25 275 Plugin::OutputDescriptor::OneSamplePerStep) < 0 ||
Chris@25 276 setint(dict, "FixedSampleRate",
Chris@25 277 Plugin::OutputDescriptor::FixedSampleRate) < 0 ||
Chris@25 278 setint(dict, "VariableSampleRate",
Chris@25 279 Plugin::OutputDescriptor::VariableSampleRate) < 0 ||
Chris@25 280 setint(dict, "TimeDomain",
Chris@25 281 Plugin::TimeDomain) < 0 ||
Chris@25 282 setint(dict, "FrequencyDomain",
Chris@25 283 Plugin::FrequencyDomain) < 0) {
Chris@25 284 cerr << "ERROR: initvampyhost: Failed to add enums to module dictionary" << endl;
Chris@25 285 return;
Chris@25 286 }
Chris@0 287 }