changeset 10:c8451896c40e

Some conversions for plugin handles
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 17 May 2016 09:58:15 +0100
parents 197440dc8296
children aa61cb5c5754
files Makefile bits/PluginHandleMapper.h capnproto/VampnProto.h capnproto/vamp.capnp json/VampJson.h utilities/json-cli.cpp
diffstat 6 files changed, 122 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon May 16 15:46:22 2016 +0100
+++ b/Makefile	Tue May 17 09:58:15 2016 +0100
@@ -1,6 +1,6 @@
 
 CXXFLAGS	:= -Wall -Werror -std=c++11
-INCFLAGS	:= -Ivamp-plugin-sdk -Ijson -Icapnproto
+INCFLAGS	:= -Ivamp-plugin-sdk -Ijson -Icapnproto -I.
 LDFLAGS		:= -Lvamp-plugin-sdk -Wl,-Bstatic -lvamp-hostsdk -Wl,-Bdynamic -lcapnp -lkj -ldl
 
 all:	bin/vamp-json-cli bin/vamp-json-to-capnp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bits/PluginHandleMapper.h	Tue May 17 09:58:15 2016 +0100
@@ -0,0 +1,55 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Vampipe
+
+    Centre for Digital Music, Queen Mary, University of London.
+    Copyright 2006-2016 Chris Cannam and QMUL.
+  
+    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 Chris Cannam
+    shall not be used in advertising or otherwise to promote the sale,
+    use or other dealings in this Software without prior written
+    authorization.
+*/
+
+#ifndef VAMPIPE_PLUGIN_HANDLE_MAPPER_H
+#define VAMPIPE_PLUGIN_HANDLE_MAPPER_H
+
+#include <vamp-hostsdk/Plugin.h>
+
+namespace vampipe {
+class PluginHandleMapper
+{
+public:
+    class NotFound : virtual public std::runtime_error {
+    public:
+        NotFound() : runtime_error("plugin or handle not found in mapper") { }
+    };
+    
+    virtual uint32_t pluginToHandle(Vamp::Plugin *) = 0; // may throw NotFound
+    virtual Vamp::Plugin *handleToPlugin(uint32_t)  = 0; // may throw NotFound
+};
+}
+
+#endif
+
--- a/capnproto/VampnProto.h	Mon May 16 15:46:22 2016 +0100
+++ b/capnproto/VampnProto.h	Tue May 17 09:58:15 2016 +0100
@@ -9,6 +9,8 @@
 #include <vamp-hostsdk/PluginLoader.h>
 #include <vamp-hostsdk/PluginStaticData.h>
 
+#include "bits/PluginHandleMapper.h"
+
 namespace vampipe
 {
 
@@ -468,6 +470,29 @@
         }
         req.adapterFlags = flags;
     }
+
+    static void
+    buildLoadResponse(LoadResponse::Builder &b,
+                      const Vamp::HostExt::LoadResponse &resp,
+                      PluginHandleMapper &mapper) {
+
+        b.setPluginHandle(mapper.pluginToHandle(resp.plugin));
+        auto sd = b.initStaticData();
+        buildPluginStaticData(sd, resp.staticData);
+        auto conf = b.initDefaultConfiguration();
+        buildPluginConfiguration(conf, resp.defaultConfiguration);
+    }
+
+    static void
+    readLoadResponse(Vamp::HostExt::LoadResponse &resp,
+                     const LoadResponse::Reader &r,
+                     PluginHandleMapper &mapper) {
+
+        resp.plugin = mapper.handleToPlugin(r.getPluginHandle());
+        readPluginStaticData(resp.staticData, r.getStaticData());
+        readPluginConfiguration(resp.defaultConfiguration,
+                                r.getDefaultConfiguration());
+    }
 };
 
 }
--- a/capnproto/vamp.capnp	Mon May 16 15:46:22 2016 +0100
+++ b/capnproto/vamp.capnp	Tue May 17 09:58:15 2016 +0100
@@ -121,13 +121,13 @@
 }
 
 struct LoadResponse {
-    pluginHandle       @0  :Int64;
+    pluginHandle       @0  :Int32;
     staticData         @1  :PluginStaticData;
     defaultConfiguration @2  :PluginConfiguration;
 }
 
 struct ConfigurationRequest {
-    pluginHandle       @0  :Int64;
+    pluginHandle       @0  :Int32;
     configuration      @1  :PluginConfiguration;
 }
 
@@ -136,7 +136,7 @@
 }
 
 struct ProcessRequest {
-    pluginHandle       @0  :Int64;
+    pluginHandle       @0  :Int32;
     timestamp          @1  :RealTime;
     input              @2  :List(List(Float32));
 }
--- a/json/VampJson.h	Mon May 16 15:46:22 2016 +0100
+++ b/json/VampJson.h	Tue May 17 09:58:15 2016 +0100
@@ -14,6 +14,10 @@
 #include <vamp-hostsdk/Plugin.h>
 #include <vamp-hostsdk/PluginLoader.h>
 
+#include "bits/PluginHandleMapper.h"
+
+namespace vampipe {
+
 /**
  * Convert the structures laid out in the Vamp SDK classes into JSON
  * (and back again) following the schema in the vamp-json-schema
@@ -663,7 +667,40 @@
         req.adapterFlags = toAdapterFlags(j["adapterFlags"]);
         return req;
     }
+
+    static json11::Json
+    fromLoadResponse(Vamp::HostExt::LoadResponse resp,
+                     PluginHandleMapper &mapper) {
+
+        json11::Json::object jo;
+        jo["pluginHandle"] = double(mapper.pluginToHandle(resp.plugin));
+        jo["staticData"] = fromPluginStaticData(resp.staticData);
+        jo["defaultConfiguration"] =
+            fromPluginConfiguration(resp.defaultConfiguration);
+        return json11::Json(jo);
+    }
+
+    static Vamp::HostExt::LoadResponse
+    toLoadResponse(json11::Json j,
+                   PluginHandleMapper &mapper) {
+
+        std::string err;
+
+        if (!j.has_shape({
+                    { "pluginHandle", json11::Json::NUMBER },
+                    { "staticData", json11::Json::OBJECT },
+                    { "defaultConfiguration", json11::Json::OBJECT } }, err)) {
+            throw VampJson::Failure("malformed load response: " + err);
+        }
+
+        Vamp::HostExt::LoadResponse resp;
+        resp.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value());
+        resp.staticData = toPluginStaticData(j["staticData"]);
+        resp.defaultConfiguration = toPluginConfiguration(j["defaultConfiguration"]);
+        return resp;
+    }
 };
 
+}
 
 #endif
--- a/utilities/json-cli.cpp	Mon May 16 15:46:22 2016 +0100
+++ b/utilities/json-cli.cpp	Tue May 17 09:58:15 2016 +0100
@@ -12,6 +12,7 @@
 using namespace Vamp;
 using namespace Vamp::HostExt;
 using namespace json11;
+using namespace vampipe;
 
 static map<uint32_t, Plugin *> loadedPlugins;
 static set<uint32_t> initialisedPlugins;