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@0
|
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@2
|
66 PyDoc_STRVAR(xx_foo_doc, "Some description"); //!!!
|
Chris@0
|
67
|
Chris@28
|
68 //!!! todo: conv errors
|
Chris@28
|
69
|
Chris@0
|
70 static PyObject *
|
Chris@23
|
71 vampyhost_enumeratePlugins(PyObject *self, PyObject *)
|
Chris@0
|
72 {
|
Chris@0
|
73 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
74 vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
|
Chris@29
|
75 VectorConversion conv;
|
Chris@15
|
76 return conv.PyValue_From_StringVector(plugins);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@15
|
79 static PyObject *
|
Chris@23
|
80 vampyhost_getPluginPath(PyObject *self, PyObject *)
|
Chris@15
|
81 {
|
Chris@15
|
82 vector<string> path = PluginHostAdapter::getPluginPath();
|
Chris@29
|
83 VectorConversion conv;
|
Chris@15
|
84 return conv.PyValue_From_StringVector(path);
|
Chris@15
|
85 }
|
Chris@0
|
86
|
Chris@15
|
87 static string toPluginKey(PyObject *pyPluginKey)
|
Chris@0
|
88 {
|
Chris@36
|
89 // convert to stl string
|
Chris@0
|
90 string pluginKey(PyString_AS_STRING(pyPluginKey));
|
Chris@0
|
91
|
Chris@36
|
92 // check pluginKey validity
|
Chris@0
|
93 string::size_type ki = pluginKey.find(':');
|
Chris@0
|
94 if (ki == string::npos) {
|
Chris@0
|
95 PyErr_SetString(PyExc_TypeError,
|
Chris@15
|
96 "Plugin key must be of the form library:identifier");
|
Chris@15
|
97 return "";
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@15
|
100 return pluginKey;
|
Chris@15
|
101 }
|
Chris@15
|
102
|
Chris@15
|
103 static PyObject *
|
Chris@15
|
104 vampyhost_getLibraryFor(PyObject *self, PyObject *args)
|
Chris@15
|
105 {
|
Chris@15
|
106 PyObject *pyPluginKey;
|
Chris@15
|
107
|
Chris@15
|
108 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
|
Chris@15
|
109 PyErr_SetString(PyExc_TypeError,
|
Chris@15
|
110 "getLibraryPathForPlugin() takes plugin key (string) argument");
|
Chris@16
|
111 return 0; }
|
Chris@15
|
112
|
Chris@15
|
113 string pluginKey = toPluginKey(pyPluginKey);
|
Chris@16
|
114 if (pluginKey == "") return 0;
|
Chris@15
|
115
|
Chris@0
|
116 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@0
|
117 string path = loader->getLibraryPathForPlugin(pluginKey);
|
Chris@0
|
118 PyObject *pyPath = PyString_FromString(path.c_str());
|
Chris@0
|
119 return pyPath;
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 static PyObject *
|
Chris@0
|
123 vampyhost_getPluginCategory(PyObject *self, PyObject *args)
|
Chris@0
|
124 {
|
Chris@0
|
125 PyObject *pyPluginKey;
|
Chris@0
|
126
|
Chris@0
|
127 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
|
Chris@0
|
128 PyErr_SetString(PyExc_TypeError,
|
Chris@15
|
129 "getPluginCategory() takes plugin key (string) argument");
|
Chris@16
|
130 return 0; }
|
Chris@0
|
131
|
Chris@15
|
132 string pluginKey = toPluginKey(pyPluginKey);
|
Chris@16
|
133 if (pluginKey == "") return 0;
|
Chris@0
|
134
|
Chris@0
|
135 PluginLoader *loader = PluginLoader::getInstance();
|
luis@7
|
136 PluginLoader::PluginCategoryHierarchy
|
Chris@0
|
137 category = loader->getPluginCategory(pluginKey);
|
Chris@0
|
138
|
Chris@29
|
139 VectorConversion conv;
|
Chris@15
|
140 return conv.PyValue_From_StringVector(category);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 static PyObject *
|
Chris@0
|
144 vampyhost_getOutputList(PyObject *self, PyObject *args)
|
Chris@0
|
145 {
|
Chris@31
|
146 PyObject *pyPluginKey;
|
Chris@31
|
147
|
Chris@31
|
148 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) {
|
Chris@31
|
149 PyErr_SetString(PyExc_TypeError,
|
Chris@31
|
150 "getOutputList() takes plugin key (string) argument");
|
Chris@31
|
151 return 0; }
|
Chris@31
|
152
|
Chris@15
|
153 Plugin::OutputList outputs;
|
Chris@0
|
154
|
Chris@31
|
155 string pluginKey = toPluginKey(pyPluginKey);
|
Chris@31
|
156 if (pluginKey == "") return 0;
|
Chris@31
|
157
|
Chris@31
|
158 PluginLoader *loader = PluginLoader::getInstance();
|
Chris@31
|
159
|
Chris@31
|
160 Plugin *plugin = loader->loadPlugin
|
Chris@31
|
161 (pluginKey, 48000, PluginLoader::ADAPT_ALL_SAFE);
|
Chris@31
|
162 if (!plugin) {
|
Chris@31
|
163 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
|
Chris@31
|
164 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@31
|
165 return 0;
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@31
|
168 outputs = plugin->getOutputDescriptors();
|
luis@7
|
169
|
Chris@0
|
170 PyObject *pyList = PyList_New(outputs.size());
|
Chris@0
|
171
|
Chris@0
|
172 for (size_t i = 0; i < outputs.size(); ++i) {
|
luis@7
|
173 PyObject *pyOutputId =
|
Chris@0
|
174 PyString_FromString(outputs[i].identifier.c_str());
|
Chris@15
|
175 PyList_SET_ITEM(pyList, i, pyOutputId);
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 return pyList;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 static PyObject *
|
Chris@0
|
182 vampyhost_loadPlugin(PyObject *self, PyObject *args)
|
Chris@0
|
183 {
|
Chris@0
|
184 PyObject *pyPluginKey;
|
Chris@0
|
185 float inputSampleRate;
|
Chris@0
|
186
|
luis@7
|
187 if (!PyArg_ParseTuple(args, "Sf",
|
Chris@0
|
188 &pyPluginKey,
|
Chris@0
|
189 &inputSampleRate)) {
|
Chris@0
|
190 PyErr_SetString(PyExc_TypeError,
|
Chris@20
|
191 "loadPlugin() takes plugin key (string) and sample rate (float) arguments");
|
Chris@16
|
192 return 0; }
|
Chris@0
|
193
|
Chris@15
|
194 string pluginKey = toPluginKey(pyPluginKey);
|
Chris@16
|
195 if (pluginKey == "") return 0;
|
Chris@0
|
196
|
Chris@0
|
197 PluginLoader *loader = PluginLoader::getInstance();
|
luis@7
|
198
|
Chris@15
|
199 Plugin *plugin = loader->loadPlugin(pluginKey, inputSampleRate,
|
Chris@15
|
200 PluginLoader::ADAPT_ALL_SAFE);
|
luis@7
|
201 if (!plugin) {
|
Chris@0
|
202 string pyerr("Failed to load plugin: "); pyerr += pluginKey;
|
luis@7
|
203 PyErr_SetString(PyExc_TypeError,pyerr.c_str());
|
Chris@16
|
204 return 0;
|
luis@7
|
205 }
|
Chris@15
|
206
|
Chris@31
|
207 return PyPluginObject_From_Plugin(plugin);
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@18
|
210 // module methods table
|
Chris@0
|
211 static PyMethodDef vampyhost_methods[] = {
|
Chris@0
|
212
|
Chris@18
|
213 {"listPlugins", vampyhost_enumeratePlugins, METH_NOARGS,
|
Chris@0
|
214 xx_foo_doc},
|
Chris@0
|
215
|
Chris@15
|
216 {"getPluginPath", vampyhost_getPluginPath, METH_NOARGS,
|
Chris@15
|
217 xx_foo_doc},
|
Chris@15
|
218
|
Chris@18
|
219 {"getCategoryOf", vampyhost_getPluginCategory, METH_VARARGS,
|
Chris@0
|
220 xx_foo_doc},
|
Chris@0
|
221
|
Chris@18
|
222 {"getLibraryFor", vampyhost_getLibraryFor, METH_VARARGS,
|
Chris@0
|
223 xx_foo_doc},
|
Chris@0
|
224
|
Chris@18
|
225 {"getOutputsOf", vampyhost_getOutputList, METH_VARARGS,
|
Chris@0
|
226 xx_foo_doc},
|
Chris@0
|
227
|
Chris@0
|
228 {"loadPlugin", vampyhost_loadPlugin, METH_VARARGS,
|
Chris@0
|
229 xx_foo_doc},
|
Chris@0
|
230
|
Chris@16
|
231 {0, 0} /* sentinel */
|
Chris@0
|
232 };
|
Chris@0
|
233
|
Chris@0
|
234 //Documentation for our new module
|
Chris@0
|
235 PyDoc_STRVAR(module_doc, "This is a template module just for instruction.");
|
Chris@0
|
236
|
Chris@25
|
237 static int
|
Chris@25
|
238 setint(PyObject *d, const char *name, int value)
|
Chris@25
|
239 {
|
Chris@25
|
240 PyObject *v;
|
Chris@25
|
241 int err;
|
Chris@25
|
242 v = PyInt_FromLong((long)value);
|
Chris@25
|
243 err = PyDict_SetItemString(d, name, v);
|
Chris@25
|
244 Py_XDECREF(v);
|
Chris@25
|
245 return err;
|
Chris@25
|
246 }
|
Chris@14
|
247
|
Chris@0
|
248 /* Initialization function for the module (*must* be called initxx) */
|
Chris@0
|
249
|
Chris@25
|
250 // module initialization (includes extern C {...} as necessary)
|
Chris@0
|
251 PyMODINIT_FUNC
|
Chris@0
|
252 initvampyhost(void)
|
Chris@0
|
253 {
|
Chris@0
|
254 PyObject *m;
|
Chris@0
|
255
|
Chris@25
|
256 if (PyType_Ready(&RealTime_Type) < 0) return;
|
Chris@25
|
257 if (PyType_Ready(&Plugin_Type) < 0) return;
|
Chris@0
|
258
|
Chris@0
|
259 m = Py_InitModule3("vampyhost", vampyhost_methods, module_doc);
|
Chris@25
|
260 if (!m) {
|
Chris@25
|
261 cerr << "ERROR: initvampyhost: Failed to initialise module" << endl;
|
Chris@25
|
262 return;
|
Chris@25
|
263 }
|
Chris@0
|
264
|
Chris@14
|
265 import_array();
|
Chris@14
|
266
|
Chris@17
|
267 PyModule_AddObject(m, "RealTime", (PyObject *)&RealTime_Type);
|
Chris@25
|
268 PyModule_AddObject(m, "Plugin", (PyObject *)&Plugin_Type);
|
Chris@25
|
269
|
Chris@25
|
270 // Some enum types
|
Chris@25
|
271 PyObject *dict = PyModule_GetDict(m);
|
Chris@25
|
272 if (!dict) {
|
Chris@25
|
273 cerr << "ERROR: initvampyhost: Failed to obtain module dictionary" << endl;
|
Chris@25
|
274 return;
|
Chris@25
|
275 }
|
Chris@25
|
276
|
Chris@25
|
277 if (setint(dict, "OneSamplePerStep",
|
Chris@25
|
278 Plugin::OutputDescriptor::OneSamplePerStep) < 0 ||
|
Chris@25
|
279 setint(dict, "FixedSampleRate",
|
Chris@25
|
280 Plugin::OutputDescriptor::FixedSampleRate) < 0 ||
|
Chris@25
|
281 setint(dict, "VariableSampleRate",
|
Chris@25
|
282 Plugin::OutputDescriptor::VariableSampleRate) < 0 ||
|
Chris@25
|
283 setint(dict, "TimeDomain",
|
Chris@25
|
284 Plugin::TimeDomain) < 0 ||
|
Chris@25
|
285 setint(dict, "FrequencyDomain",
|
Chris@25
|
286 Plugin::FrequencyDomain) < 0) {
|
Chris@25
|
287 cerr << "ERROR: initvampyhost: Failed to add enums to module dictionary" << endl;
|
Chris@25
|
288 return;
|
Chris@25
|
289 }
|
Chris@0
|
290 }
|