Mercurial > hg > piper-cpp
changeset 17:3ef01276e15e
More request/response encodings
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 18 May 2016 15:31:22 +0100 |
parents | 913fc1d3710a |
children | 071c55f52c7d |
files | capnproto/VampnProto.h json/VampJson.h |
diffstat | 2 files changed, 167 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/capnproto/VampnProto.h Wed May 18 14:38:27 2016 +0100 +++ b/capnproto/VampnProto.h Wed May 18 15:31:22 2016 +0100 @@ -622,8 +622,8 @@ static void buildVampResponse_List(VampResponse::Builder &b, - const std::vector<Vamp::HostExt::PluginStaticData> &d, - std::string errorText = "") { + std::string errorText, + const std::vector<Vamp::HostExt::PluginStaticData> &d) { b.setSuccess(errorText == ""); b.setErrorText(errorText); auto r = b.getResponse().initList(d.size()); @@ -644,6 +644,8 @@ buildVampResponse_Load(VampResponse::Builder &b, const Vamp::HostExt::LoadResponse &resp, PluginHandleMapper &mapper) { + b.setSuccess(resp.plugin != 0); + b.setErrorText(""); auto u = b.getResponse().initLoad(); buildLoadResponse(u, resp, mapper); } @@ -659,6 +661,8 @@ static void buildVampResponse_Configure(VampResponse::Builder &b, const Vamp::HostExt::ConfigurationResponse &cr) { + b.setSuccess(!cr.outputs.empty()); + b.setErrorText(""); auto u = b.getResponse().initConfigure(); buildConfigurationResponse(u, cr); } @@ -674,9 +678,24 @@ static void buildVampResponse_Process(VampResponse::Builder &b, const Vamp::HostExt::ProcessResponse &pr) { + b.setSuccess(true); + b.setErrorText(""); auto u = b.getResponse().initProcess(); buildProcessResponse(u, pr); } + + static void + buildVampRequest_Finish(VampRequest::Builder &b) { + + b.getRequest().setFinish(); + } + + static void + buildVampResponse_Finish(VampResponse::Builder &b, + const Vamp::HostExt::ProcessResponse &pr) { + + buildVampResponse_Process(b, pr); + } }; }
--- a/json/VampJson.h Wed May 18 14:38:27 2016 +0100 +++ b/json/VampJson.h Wed May 18 15:31:22 2016 +0100 @@ -640,7 +640,7 @@ } static json11::Json - fromLoadRequest(Vamp::HostExt::LoadRequest req) { + fromLoadRequest(const Vamp::HostExt::LoadRequest &req) { json11::Json::object jo; jo["pluginKey"] = req.pluginKey; @@ -669,7 +669,7 @@ } static json11::Json - fromLoadResponse(Vamp::HostExt::LoadResponse resp, + fromLoadResponse(const Vamp::HostExt::LoadResponse &resp, PluginHandleMapper &mapper) { json11::Json::object jo; @@ -782,6 +782,150 @@ jo["processInput"] = io; return json11::Json(jo); } + + static Vamp::HostExt::ProcessRequest + toProcessRequest(json11::Json j, PluginHandleMapper &mapper) { + + std::string err; + + if (!j.has_shape({ + { "pluginHandle", json11::Json::NUMBER }, + { "processInput", json11::Json::OBJECT } }, err)) { + throw Failure("malformed process request: " + err); + } + + auto input = j["processInput"]; + + if (!input.has_shape({ + { "timestamp", json11::Json::OBJECT }, + { "inputBuffers", json11::Json::ARRAY } }, err)) { + throw Failure("malformed process request: " + err); + } + + Vamp::HostExt::ProcessRequest r; + r.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value()); + + r.timestamp = toRealTime(input["timestamp"]); + + for (auto a: input["inputBuffers"].array_items()) { + if (a["b64values"].is_string()) { + r.inputBuffers.push_back(toFloatBuffer + (a["b64values"].string_value())); + } else if (a["values"].is_array()) { + std::vector<float> buf; + for (auto v : a["values"].array_items()) { + buf.push_back(v.number_value()); + } + r.inputBuffers.push_back(buf); + } else { + throw Failure("expected values or b64values in inputBuffers object"); + } + } + + return r; + } + + static json11::Json + fromVampRequest_List() { + + json11::Json::object jo; + jo["type"] = "list"; + return json11::Json(jo); + } + + static json11::Json + fromVampResponse_List(std::string errorText, + const std::vector<Vamp::HostExt::PluginStaticData> &d) { + + json11::Json::object jo; + jo["success"] = (errorText == ""); + jo["errorText"] = errorText; + + json11::Json::array arr; + for (const auto &a: d) { + arr.push_back(fromPluginStaticData(a)); + } + jo["response"] = arr; + return json11::Json(jo); + } + + static json11::Json + fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { + + json11::Json::object jo; + jo["type"] = "load"; + jo["content"] = fromLoadRequest(req); + return json11::Json(jo); + } + + static json11::Json + fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, + PluginHandleMapper &mapper) { + + json11::Json::object jo; + jo["success"] = (resp.plugin != 0); + jo["errorText"] = ""; + jo["response"] = fromLoadResponse(resp, mapper); + return json11::Json(jo); + } + + static json11::Json + fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, + PluginHandleMapper &mapper) { + + json11::Json::object jo; + jo["type"] = "configure"; + jo["content"] = fromConfigurationRequest(req, mapper); + return json11::Json(jo); + } + + static json11::Json + fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { + + json11::Json::object jo; + jo["success"] = (!resp.outputs.empty()); + jo["errorText"] = ""; + jo["response"] = fromConfigurationResponse(resp); + return json11::Json(jo); + } + + static json11::Json + fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, + PluginHandleMapper &mapper) { + + json11::Json::object jo; + jo["type"] = "process"; + jo["content"] = fromProcessRequest(req, mapper); + return json11::Json(jo); + } + + static json11::Json + fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp) { + + json11::Json::object jo; + jo["success"] = true; + jo["errorText"] = ""; + jo["response"] = fromFeatureSet(resp.features); + return json11::Json(jo); + } + + static json11::Json + fromVampRequest_Finish() { + + json11::Json::object jo; + jo["type"] = "finish"; + return json11::Json(jo); + } + + static json11::Json + fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp) { + + json11::Json::object jo; + jo["success"] = true; + jo["errorText"] = ""; + jo["response"] = fromFeatureSet(resp.features); + return json11::Json(jo); + } }; }