changeset 48:0e0e18629917

Set parameters in bulk
author Chris Cannam
date Tue, 13 Jan 2015 11:57:06 +0000
parents 1cb0ed230b71
children d4a3cd9dcf2c
files FloatConversion.h Makefile.inc PyPluginObject.cpp VectorConversion.cpp VectorConversion.h
diffstat 5 files changed, 130 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FloatConversion.h	Tue Jan 13 11:57:06 2015 +0000
@@ -0,0 +1,76 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+  VampyHost
+
+  Use Vamp audio analysis plugins in Python
+
+  Gyorgy Fazekas and Chris Cannam
+  Centre for Digital Music, Queen Mary, University of London
+  Copyright 2008-2014 Queen Mary, University of London
+  
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation
+  files (the "Software"), to deal in the Software without
+  restriction, including without limitation the rights to use, copy,
+  modify, merge, publish, distribute, sublicense, and/or sell copies
+  of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+  Except as contained in this notice, the names of the Centre for
+  Digital Music; Queen Mary, University of London; and the authors
+  shall not be used in advertising or otherwise to promote the sale,
+  use or other dealings in this Software without prior written
+  authorization.
+*/
+
+#ifndef VAMPYHOST_FLOAT_CONVERSION_H
+#define VAMPYHOST_FLOAT_CONVERSION_H
+
+class FloatConversion
+{
+public:
+    static bool check(PyObject *pyValue) {
+	if (pyValue && PyFloat_Check(pyValue)) {
+	    return true;
+	}
+	if (pyValue && PyLong_Check(pyValue)) {
+	    return true;
+	}
+	if (pyValue && PyInt_Check(pyValue)) {
+	    return true;
+	}
+	return false;
+    }
+	
+    static float convert(PyObject* pyValue) {
+	
+	if (pyValue && PyFloat_Check(pyValue)) {
+	    return (float) PyFloat_AS_DOUBLE(pyValue);
+	}
+
+	if (pyValue && PyLong_Check(pyValue)) {
+	    return (float) PyLong_AsDouble(pyValue);
+	}
+
+	if (pyValue && PyInt_Check(pyValue)) {
+	    return (float) PyInt_AsLong(pyValue);
+	}
+
+	return 0.0;
+    }
+};
+
+#endif
+
--- a/Makefile.inc	Tue Jan 13 11:38:29 2015 +0000
+++ b/Makefile.inc	Tue Jan 13 11:57:06 2015 +0000
@@ -4,7 +4,7 @@
 CXX		?= g++
 CC		?= gcc
 
-HEADERS		:= PyPluginObject.h PyRealTime.h VectorConversion.h
+HEADERS		:= PyPluginObject.h PyRealTime.h FloatConversion.h VectorConversion.h
 SOURCES		:= PyPluginObject.cpp PyRealTime.cpp VectorConversion.cpp vampyhost.cpp
 
 OBJECTS		:= $(SOURCES:.cpp=.o)
--- a/PyPluginObject.cpp	Tue Jan 13 11:38:29 2015 +0000
+++ b/PyPluginObject.cpp	Tue Jan 13 11:57:06 2015 +0000
@@ -45,6 +45,7 @@
 
 #include "structmember.h"
 
+#include "FloatConversion.h"
 #include "VectorConversion.h"
 #include "PyRealTime.h"
 
@@ -272,13 +273,13 @@
 }
 
 static PyObject *
-getParameter(PyObject *self, PyObject *args)
+getParameterValue(PyObject *self, PyObject *args)
 {
     PyObject *pyParam;
 
     if (!PyArg_ParseTuple(args, "S", &pyParam)) {
         PyErr_SetString(PyExc_TypeError,
-                        "getParameter() takes parameter id (string) argument");
+                        "getParameterValue() takes parameter id (string) argument");
         return 0; }
 
     PyPluginObject *pd = getPluginObject(self);
@@ -289,14 +290,14 @@
 }
 
 static PyObject *
-setParameter(PyObject *self, PyObject *args)
+setParameterValue(PyObject *self, PyObject *args)
 {
     PyObject *pyParam;
     float value;
 
     if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) {
         PyErr_SetString(PyExc_TypeError,
-                        "setParameter() takes parameter id (string), and value (float) arguments");
+                        "setParameterValue() takes parameter id (string), and value (float) arguments");
         return 0; }
 
     PyPluginObject *pd = getPluginObject(self);
@@ -307,6 +308,44 @@
 }
 
 static PyObject *
+setParameterValues(PyObject *self, PyObject *args)
+{
+    PyObject *dict;
+
+    if (!PyArg_ParseTuple(args, "O", &dict)) {
+        PyErr_SetString(PyExc_TypeError,
+                        "setParameterValues() takes dict argument");
+        return 0; }
+
+    if (!PyDict_Check(dict)) {
+        PyErr_SetString(PyExc_TypeError,
+                        "setParameterValues() takes dict argument");
+        return 0; }
+    
+    PyPluginObject *pd = getPluginObject(self);
+    if (!pd) return 0;
+
+    Py_ssize_t pos = 0;
+    PyObject *key, *value;
+    while (PyDict_Next(dict, &pos, &key, &value)) {
+        if (!key || !PyString_CheckExact(key)) {
+            PyErr_SetString(PyExc_TypeError,
+                            "Parameter dict keys must all have string type");
+            return 0;
+        }
+        if (!value || !FloatConversion::check(value)) {
+            PyErr_SetString(PyExc_TypeError,
+                            "Parameter dict values must be convertible to float");
+            return 0;
+        }
+        pd->plugin->setParameter(PyString_AS_STRING(key),
+                                 FloatConversion::convert(value));
+    }
+    
+    return Py_True;
+}
+
+static PyObject *
 selectProgram(PyObject *self, PyObject *args)
 {
     PyObject *pyParam;
@@ -556,12 +595,15 @@
     {"getOutputs", getOutputs, METH_NOARGS,
      "getOutputs() -> Obtain the output descriptors for all of the plugin's outputs."},
 
-    {"getParameterValue", getParameter, METH_VARARGS,
+    {"getParameterValue", getParameterValue, METH_VARARGS,
      "getParameterValue(identifier) -> Return the value of the parameter with the given identifier."},
 
-    {"setParameterValue", setParameter, METH_VARARGS,
+    {"setParameterValue", setParameterValue, METH_VARARGS,
      "setParameterValue(identifier, value) -> Set the parameter with the given identifier to the given value."},
 
+    {"setParameterValues", setParameterValues, METH_VARARGS,
+     "setParameterValues(dict) -> Set multiple parameters to values corresponding to the key/value pairs in the dict. Any parameters not mentioned in the dict are unchanged."},
+
     {"selectProgram", selectProgram, METH_VARARGS,
      "selectProgram(name) -> Select the processing program with the given name."},
     
--- a/VectorConversion.cpp	Tue Jan 13 11:38:29 2015 +0000
+++ b/VectorConversion.cpp	Tue Jan 13 11:57:06 2015 +0000
@@ -37,6 +37,7 @@
 
 #include <Python.h>
 
+#include "FloatConversion.h"
 #include "VectorConversion.h"
 
 #include <math.h>
@@ -61,26 +62,10 @@
 float 
 VectorConversion::PyValue_To_Float(PyObject* pyValue) const
 {
-    // convert float
-    if (pyValue && PyFloat_Check(pyValue)) {
-        return (float) PyFloat_AS_DOUBLE(pyValue);
+    if (FloatConversion::check(pyValue)) {
+        return FloatConversion::convert(pyValue);
     }
 
-    // convert long
-    if (pyValue && PyLong_Check(pyValue)) {
-        return (float) PyLong_AsDouble(pyValue);
-    }
-
-    // convert int
-    if (pyValue && PyInt_Check(pyValue)) {
-        return (float) PyInt_AsLong(pyValue);
-    }
-
-    if (pyValue == NULL) {
-        setValueError("Error while converting object " + PyValue_Get_TypeName(pyValue) + " to float. ");
-        return 0.0;		
-    }
-		
     setValueError("Conversion error: object" + PyValue_Get_TypeName(pyValue) +" is not float, int, or long.");
     return 0.0;
 }
--- a/VectorConversion.h	Tue Jan 13 11:38:29 2015 +0000
+++ b/VectorConversion.h	Tue Jan 13 11:57:06 2015 +0000
@@ -40,8 +40,8 @@
   between Python types and C++ vectors.
 */
 
-#ifndef PY_TYPE_CONVERSIONS_H
-#define PY_TYPE_CONVERSIONS_H
+#ifndef VAMPYHOST_VECTOR_CONVERSION_H
+#define VAMPYHOST_VECTOR_CONVERSION_H
 
 #include <Python.h>