Mercurial > hg > vampy-host
comparison vampyhost.cpp @ 15:8b264cabbc28
Start to tidy & rationalise the API
author | Chris Cannam |
---|---|
date | Mon, 24 Nov 2014 11:02:28 +0000 |
parents | 8565ec421f9c |
children | 7987e3123909 |
comparison
equal
deleted
inserted
replaced
14:8565ec421f9c | 15:8b264cabbc28 |
---|---|
103 /* | 103 /* |
104 VAMPYHOST MAIN | 104 VAMPYHOST MAIN |
105 --------------------------------------------------------------------- | 105 --------------------------------------------------------------------- |
106 */ | 106 */ |
107 | 107 |
108 /* ENUMERATE PLUGINS*/ | |
109 | |
110 static PyObject * | 108 static PyObject * |
111 vampyhost_enumeratePlugins(PyObject *self, PyObject *args) | 109 vampyhost_enumeratePlugins(PyObject *self, PyObject *args) |
112 { | 110 { |
113 string retType; | |
114 | |
115 if (!PyArg_ParseTuple(args, "|s:enumeratePlugins", &retType)) | |
116 return NULL; | |
117 | |
118 //list available plugins | |
119 PluginLoader *loader = PluginLoader::getInstance(); | 111 PluginLoader *loader = PluginLoader::getInstance(); |
120 vector<PluginLoader::PluginKey> plugins = loader->listPlugins(); | 112 vector<PluginLoader::PluginKey> plugins = loader->listPlugins(); |
121 | 113 PyTypeConversions conv; |
122 //library Map | 114 return conv.PyValue_From_StringVector(plugins); |
123 typedef multimap<string, PluginLoader::PluginKey> LibraryMap; | 115 } |
124 LibraryMap libraryMap; | 116 |
125 | 117 static PyObject * |
126 //New list object | 118 vampyhost_getPluginPath(PyObject *self, PyObject *args) |
127 PyObject *pyList = PyList_New(plugins.size()); | 119 { |
128 | 120 vector<string> path = PluginHostAdapter::getPluginPath(); |
129 for (size_t i = 0; i < plugins.size(); ++i) { | 121 PyTypeConversions conv; |
130 string path = loader->getLibraryPathForPlugin(plugins[i]); | 122 return conv.PyValue_From_StringVector(path); |
131 libraryMap.insert(LibraryMap::value_type(path, plugins[i])); | 123 } |
132 | 124 |
133 PyObject *pyPluginKey = PyString_FromString(plugins[i].c_str()); | 125 static string toPluginKey(PyObject *pyPluginKey) |
134 PyList_SET_ITEM(pyList,i,pyPluginKey); | 126 { |
135 | |
136 } | |
137 | |
138 PyList_Sort(pyList); | |
139 return pyList; | |
140 } | |
141 | |
142 | |
143 /* GET PLUGIN LIBRARY PATH*/ | |
144 | |
145 static PyObject * | |
146 vampyhost_getLibraryPath(PyObject *self, PyObject *args) | |
147 { | |
148 PyObject *pyPluginKey; | |
149 | |
150 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) { | |
151 PyErr_SetString(PyExc_TypeError, | |
152 "String input argument required: pluginKey"); | |
153 return NULL; } | |
154 | |
155 //convert to stl string | 127 //convert to stl string |
156 string pluginKey(PyString_AS_STRING(pyPluginKey)); | 128 string pluginKey(PyString_AS_STRING(pyPluginKey)); |
157 | 129 |
158 //check pluginKey Validity | 130 //check pluginKey Validity |
159 string::size_type ki = pluginKey.find(':'); | 131 string::size_type ki = pluginKey.find(':'); |
160 if (ki == string::npos) { | 132 if (ki == string::npos) { |
161 PyErr_SetString(PyExc_TypeError, | 133 PyErr_SetString(PyExc_TypeError, |
162 "String input argument required: pluginLibrary:Identifier"); | 134 "Plugin key must be of the form library:identifier"); |
163 return NULL; | 135 return ""; |
164 } | 136 } |
165 | 137 |
138 return pluginKey; | |
139 } | |
140 | |
141 static PyObject * | |
142 vampyhost_getLibraryFor(PyObject *self, PyObject *args) | |
143 { | |
144 PyObject *pyPluginKey; | |
145 | |
146 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) { | |
147 PyErr_SetString(PyExc_TypeError, | |
148 "getLibraryPathForPlugin() takes plugin key (string) argument"); | |
149 return NULL; } | |
150 | |
151 string pluginKey = toPluginKey(pyPluginKey); | |
152 if (pluginKey == "") return NULL; | |
153 | |
166 PluginLoader *loader = PluginLoader::getInstance(); | 154 PluginLoader *loader = PluginLoader::getInstance(); |
167 string path = loader->getLibraryPathForPlugin(pluginKey); | 155 string path = loader->getLibraryPathForPlugin(pluginKey); |
168 PyObject *pyPath = PyString_FromString(path.c_str()); | 156 PyObject *pyPath = PyString_FromString(path.c_str()); |
169 return pyPath; | 157 return pyPath; |
170 } | 158 } |
171 | 159 |
172 | |
173 /* GET PLUGIN CATEGORY*/ | |
174 | |
175 static PyObject * | 160 static PyObject * |
176 vampyhost_getPluginCategory(PyObject *self, PyObject *args) | 161 vampyhost_getPluginCategory(PyObject *self, PyObject *args) |
177 { | 162 { |
178 PyObject *pyPluginKey; | 163 PyObject *pyPluginKey; |
179 | 164 |
180 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) { | 165 if (!PyArg_ParseTuple(args, "S", &pyPluginKey)) { |
181 PyErr_SetString(PyExc_TypeError, | 166 PyErr_SetString(PyExc_TypeError, |
182 "String input argument required: pluginKey"); | 167 "getPluginCategory() takes plugin key (string) argument"); |
183 return NULL; } | 168 return NULL; } |
184 | 169 |
185 //convert to stl string | 170 string pluginKey = toPluginKey(pyPluginKey); |
186 string pluginKey(PyString_AS_STRING(pyPluginKey)); | 171 if (pluginKey == "") return NULL; |
187 | |
188 //check pluginKey Validity | |
189 string::size_type ki = pluginKey.find(':'); | |
190 if (ki == string::npos) { | |
191 PyErr_SetString(PyExc_TypeError, | |
192 "String input argument required: pluginLibrary:Identifier"); | |
193 return NULL; | |
194 } | |
195 | 172 |
196 PluginLoader *loader = PluginLoader::getInstance(); | 173 PluginLoader *loader = PluginLoader::getInstance(); |
197 PluginLoader::PluginCategoryHierarchy | 174 PluginLoader::PluginCategoryHierarchy |
198 category = loader->getPluginCategory(pluginKey); | 175 category = loader->getPluginCategory(pluginKey); |
199 string catstring; | 176 |
200 | 177 PyTypeConversions conv; |
201 if (!category.empty()) { | 178 return conv.PyValue_From_StringVector(category); |
202 catstring = ""; | 179 } |
203 for (size_t ci = 0; ci < category.size(); ++ci) { | 180 |
204 catstring.append(category[ci]); | 181 static PyObject * |
205 catstring.append(" "); | 182 vampyhost_getOutputList(PyObject *self, PyObject *args) |
183 { | |
184 PyObject *pyPluginHandle; | |
185 Plugin::OutputList outputs; | |
186 | |
187 if (!PyArg_ParseTuple(args, "O", &pyPluginHandle)) { | |
188 PyErr_SetString(PyExc_TypeError, | |
189 "getOutputList() takes plugin handle (object) or plugin key (string) argument"); | |
190 return NULL; | |
191 } | |
192 | |
193 if (PyString_Check(pyPluginHandle) ) { | |
194 | |
195 // we have a plugin key | |
196 | |
197 string pluginKey = toPluginKey(pyPluginHandle); | |
198 if (pluginKey == "") return NULL; | |
199 | |
200 PluginLoader *loader = PluginLoader::getInstance(); | |
201 | |
202 Plugin *plugin = loader->loadPlugin | |
203 (pluginKey, 48000, PluginLoader::ADAPT_ALL_SAFE); | |
204 if (!plugin) { | |
205 string pyerr("Failed to load plugin: "); pyerr += pluginKey; | |
206 PyErr_SetString(PyExc_TypeError,pyerr.c_str()); | |
207 return NULL; | |
206 } | 208 } |
207 PyObject *pyCat = PyString_FromString(catstring.c_str()); | 209 |
208 return pyCat; | 210 outputs = plugin->getOutputDescriptors(); |
209 } | 211 |
210 PyObject *pyCat = PyString_FromString(""); | 212 delete plugin; |
211 return pyCat; | 213 |
212 } | |
213 | |
214 | |
215 | |
216 /* GET PLUGIN OUTPUT LIST*/ | |
217 | |
218 static PyObject * | |
219 vampyhost_getOutputList(PyObject *self, PyObject *args) | |
220 { | |
221 PyObject *pyPluginHandle; | |
222 string pluginKey; | |
223 | |
224 if (!PyArg_ParseTuple(args, "O", &pyPluginHandle)) { | |
225 PyErr_SetString(PyExc_TypeError, | |
226 "Invalid argument: plugin handle or plugin key required."); | |
227 return NULL; | |
228 } | |
229 | |
230 //check if we have a plugin key string or a handle object | |
231 if (PyString_Check(pyPluginHandle) ) { | |
232 | |
233 pluginKey.assign(PyString_AS_STRING(pyPluginHandle)); | |
234 //check pluginKey Validity | |
235 string::size_type ki = pluginKey.find(':'); | |
236 if (ki == string::npos) { | |
237 PyErr_SetString(PyExc_TypeError, | |
238 "String input argument required: pluginLibrary:Identifier"); | |
239 return NULL; | |
240 } | |
241 | |
242 } else { | 214 } else { |
243 | 215 |
216 // we have a loaded plugin handle | |
217 | |
244 string *key; | 218 string *key; |
245 Plugin *plugin; | 219 Plugin *plugin; |
246 | 220 |
247 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) { | 221 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) { |
248 PyErr_SetString(PyExc_TypeError, | 222 PyErr_SetString(PyExc_TypeError, |
249 "Invalid or deleted plugin handle."); | 223 "Invalid or deleted plugin handle."); |
250 return NULL; } | 224 return NULL; } |
251 pluginKey.assign(*key); | 225 |
252 } | 226 outputs = plugin->getOutputDescriptors(); |
253 | 227 } |
254 //This code creates new instance of the plugin anyway | 228 |
229 PyObject *pyList = PyList_New(outputs.size()); | |
230 | |
231 for (size_t i = 0; i < outputs.size(); ++i) { | |
232 PyObject *pyOutputId = | |
233 PyString_FromString(outputs[i].identifier.c_str()); | |
234 PyList_SET_ITEM(pyList, i, pyOutputId); | |
235 } | |
236 | |
237 return pyList; | |
238 } | |
239 | |
240 static PyObject * | |
241 vampyhost_loadPlugin(PyObject *self, PyObject *args) | |
242 { | |
243 PyObject *pyPluginKey; | |
244 float inputSampleRate; | |
245 | |
246 if (!PyArg_ParseTuple(args, "Sf", | |
247 &pyPluginKey, | |
248 &inputSampleRate)) { | |
249 PyErr_SetString(PyExc_TypeError, | |
250 "loadPlugin() takes plugin key (string) and sample rate (number) arguments"); | |
251 return NULL; } | |
252 | |
253 string pluginKey = toPluginKey(pyPluginKey); | |
254 if (pluginKey == "") return NULL; | |
255 | |
255 PluginLoader *loader = PluginLoader::getInstance(); | 256 PluginLoader *loader = PluginLoader::getInstance(); |
256 | 257 |
257 //load plugin | 258 Plugin *plugin = loader->loadPlugin(pluginKey, inputSampleRate, |
258 Plugin *plugin = loader->loadPlugin | 259 PluginLoader::ADAPT_ALL_SAFE); |
259 (pluginKey, 48000, PluginLoader::ADAPT_ALL_SAFE); | |
260 if (!plugin) { | 260 if (!plugin) { |
261 string pyerr("Failed to load plugin: "); pyerr += pluginKey; | 261 string pyerr("Failed to load plugin: "); pyerr += pluginKey; |
262 PyErr_SetString(PyExc_TypeError,pyerr.c_str()); | 262 PyErr_SetString(PyExc_TypeError,pyerr.c_str()); |
263 return NULL; | 263 return NULL; |
264 } | 264 } |
265 | 265 |
266 Plugin::OutputList outputs = plugin->getOutputDescriptors(); | |
267 //Plugin::OutputDescriptor od; | |
268 | |
269 if (outputs.size()<1) { | |
270 string pyerr("Plugin has no output: "); pyerr += pluginKey; | |
271 PyErr_SetString(PyExc_TypeError,pyerr.c_str()); | |
272 return NULL; | |
273 } | |
274 | |
275 //New list object | |
276 PyObject *pyList = PyList_New(outputs.size()); | |
277 | |
278 for (size_t i = 0; i < outputs.size(); ++i) { | |
279 PyObject *pyOutputId = | |
280 PyString_FromString(outputs[i].identifier.c_str()); | |
281 PyList_SET_ITEM(pyList,i,pyOutputId); | |
282 } | |
283 | |
284 delete plugin; | |
285 return pyList; | |
286 } | |
287 | |
288 | |
289 | |
290 /* LOAD PLUGIN */ | |
291 | |
292 static PyObject * | |
293 vampyhost_loadPlugin(PyObject *self, PyObject *args) | |
294 { | |
295 PyObject *pyPluginKey; | |
296 float inputSampleRate; | |
297 | |
298 if (!PyArg_ParseTuple(args, "Sf", | |
299 &pyPluginKey, | |
300 &inputSampleRate)) { | |
301 PyErr_SetString(PyExc_TypeError, | |
302 "String input argument required: pluginKey"); | |
303 return NULL; } | |
304 | |
305 //convert to stl string | |
306 string pluginKey(PyString_AS_STRING(pyPluginKey)); | |
307 | |
308 //check pluginKey Validity | |
309 string::size_type ki = pluginKey.find(':'); | |
310 if (ki == string::npos) { | |
311 PyErr_SetString(PyExc_TypeError, | |
312 "String input argument required: pluginLibrary:Identifier"); | |
313 return NULL; | |
314 } | |
315 | |
316 PluginLoader *loader = PluginLoader::getInstance(); | |
317 | |
318 //load plugin | |
319 Plugin *plugin = loader->loadPlugin (pluginKey, inputSampleRate, | |
320 PluginLoader::ADAPT_ALL_SAFE); | |
321 if (!plugin) { | |
322 string pyerr("Failed to load plugin: "); pyerr += pluginKey; | |
323 PyErr_SetString(PyExc_TypeError,pyerr.c_str()); | |
324 return NULL; | |
325 } | |
326 //void *identifier = (void*) new string(pluginKey); | |
327 PyPluginDescriptor *pd = new PyPluginDescriptor; | 266 PyPluginDescriptor *pd = new PyPluginDescriptor; |
328 | 267 |
329 pd->key = pluginKey; | 268 pd->key = pluginKey; |
330 pd->isInitialised = false; | 269 pd->isInitialised = false; |
331 pd->inputSampleRate = inputSampleRate; | 270 pd->inputSampleRate = inputSampleRate; |
332 | |
333 //New PyCObject | |
334 //PyObject *pyPluginHandle = PyCObject_FromVoidPtrAndDesc( | |
335 //(void*) plugin, identifier, NULL); | |
336 | 271 |
337 PyObject *pyPluginHandle = PyCObject_FromVoidPtrAndDesc( | 272 PyObject *pyPluginHandle = PyCObject_FromVoidPtrAndDesc( |
338 (void*) plugin, (void*) pd, NULL); | 273 (void*) plugin, (void*) pd, NULL); |
339 | 274 |
340 return pyPluginHandle; | 275 return pyPluginHandle; |
588 | 523 |
589 /* List of functions defined in this module */ | 524 /* List of functions defined in this module */ |
590 //module methods table | 525 //module methods table |
591 static PyMethodDef vampyhost_methods[] = { | 526 static PyMethodDef vampyhost_methods[] = { |
592 | 527 |
593 {"enumeratePlugins", vampyhost_enumeratePlugins, METH_VARARGS, | 528 {"enumeratePlugins", vampyhost_enumeratePlugins, METH_NOARGS, |
594 xx_foo_doc}, | 529 xx_foo_doc}, |
595 | 530 |
596 {"getLibraryPath", vampyhost_getLibraryPath, METH_VARARGS, | 531 {"getPluginPath", vampyhost_getPluginPath, METH_NOARGS, |
532 xx_foo_doc}, | |
533 | |
534 {"getLibraryForPlugin", vampyhost_getLibraryFor, METH_VARARGS, | |
597 xx_foo_doc}, | 535 xx_foo_doc}, |
598 | 536 |
599 {"getPluginCategory", vampyhost_getPluginCategory, METH_VARARGS, | 537 {"getPluginCategory", vampyhost_getPluginCategory, METH_VARARGS, |
600 xx_foo_doc}, | 538 xx_foo_doc}, |
601 | 539 |