Mercurial > hg > piper-cpp
comparison json/VampJson.h @ 49:f3f7561233d6
Begin plugin output id / index mapping for use in feature sets
| author | Chris Cannam <c.cannam@qmul.ac.uk> |
|---|---|
| date | Fri, 16 Sep 2016 14:13:21 +0100 |
| parents | fa2edfabf829 |
| children | f4244a2d55ac |
comparison
equal
deleted
inserted
replaced
| 48:ce6cb3308bd7 | 49:f3f7561233d6 |
|---|---|
| 45 | 45 |
| 46 #include <vamp-hostsdk/Plugin.h> | 46 #include <vamp-hostsdk/Plugin.h> |
| 47 #include <vamp-hostsdk/PluginLoader.h> | 47 #include <vamp-hostsdk/PluginLoader.h> |
| 48 | 48 |
| 49 #include "bits/PluginHandleMapper.h" | 49 #include "bits/PluginHandleMapper.h" |
| 50 #include "bits/PluginOutputIdMapper.h" | |
| 50 #include "bits/RequestResponseType.h" | 51 #include "bits/RequestResponseType.h" |
| 51 | 52 |
| 52 namespace vampipe { | 53 namespace vampipe { |
| 53 | 54 |
| 54 /** | 55 /** |
| 390 return f; | 391 return f; |
| 391 } | 392 } |
| 392 | 393 |
| 393 static json11::Json | 394 static json11::Json |
| 394 fromFeatureSet(const Vamp::Plugin::FeatureSet &fs, | 395 fromFeatureSet(const Vamp::Plugin::FeatureSet &fs, |
| 396 const PluginOutputIdMapper &omapper, | |
| 395 BufferSerialisation serialisation) { | 397 BufferSerialisation serialisation) { |
| 396 | 398 |
| 397 json11::Json::object jo; | 399 json11::Json::object jo; |
| 398 for (const auto &fsi : fs) { | 400 for (const auto &fsi : fs) { |
| 399 std::vector<json11::Json> fj; | 401 std::vector<json11::Json> fj; |
| 400 for (const Vamp::Plugin::Feature &f: fsi.second) { | 402 for (const Vamp::Plugin::Feature &f: fsi.second) { |
| 401 fj.push_back(fromFeature(f, serialisation)); | 403 fj.push_back(fromFeature(f, serialisation)); |
| 402 } | 404 } |
| 403 std::stringstream sstr; | 405 jo[omapper.indexToId(fsi.first)] = fj; |
| 404 sstr << fsi.first; | |
| 405 std::string n = sstr.str(); | |
| 406 jo[n] = fj; | |
| 407 } | 406 } |
| 408 return json11::Json(jo); | 407 return json11::Json(jo); |
| 409 } | 408 } |
| 410 | 409 |
| 411 static Vamp::Plugin::FeatureList | 410 static Vamp::Plugin::FeatureList |
| 421 } | 420 } |
| 422 return fl; | 421 return fl; |
| 423 } | 422 } |
| 424 | 423 |
| 425 static Vamp::Plugin::FeatureSet | 424 static Vamp::Plugin::FeatureSet |
| 426 toFeatureSet(json11::Json j, BufferSerialisation &serialisation) { | 425 toFeatureSet(json11::Json j, |
| 426 const PluginOutputIdMapper &omapper, | |
| 427 BufferSerialisation &serialisation) { | |
| 427 | 428 |
| 428 Vamp::Plugin::FeatureSet fs; | 429 Vamp::Plugin::FeatureSet fs; |
| 429 if (!j.is_object()) { | 430 if (!j.is_object()) { |
| 430 throw Failure("object expected for feature set"); | 431 throw Failure("object expected for feature set"); |
| 431 } | 432 } |
| 432 for (auto &entry : j.object_items()) { | 433 for (auto &entry : j.object_items()) { |
| 433 std::string nstr = entry.first; | 434 int n = omapper.idToIndex(entry.first); |
| 434 size_t count = 0; | 435 if (fs.find(n) != fs.end()) { |
| 435 int n = stoi(nstr, &count); | 436 throw Failure("duplicate numerical index for output"); |
| 436 if (n < 0 || fs.find(n) != fs.end() || count < nstr.size()) { | |
| 437 throw Failure("invalid or duplicate numerical index for output"); | |
| 438 } | 437 } |
| 439 fs[n] = toFeatureList(entry.second, serialisation); | 438 fs[n] = toFeatureList(entry.second, serialisation); |
| 440 } | 439 } |
| 441 return fs; | 440 return fs; |
| 442 } | 441 } |
| 742 return req; | 741 return req; |
| 743 } | 742 } |
| 744 | 743 |
| 745 static json11::Json | 744 static json11::Json |
| 746 fromLoadResponse(const Vamp::HostExt::LoadResponse &resp, | 745 fromLoadResponse(const Vamp::HostExt::LoadResponse &resp, |
| 747 const PluginHandleMapper &mapper) { | 746 const PluginHandleMapper &pmapper) { |
| 748 | 747 |
| 749 json11::Json::object jo; | 748 json11::Json::object jo; |
| 750 jo["pluginHandle"] = double(mapper.pluginToHandle(resp.plugin)); | 749 jo["pluginHandle"] = double(pmapper.pluginToHandle(resp.plugin)); |
| 751 jo["staticData"] = fromPluginStaticData(resp.staticData); | 750 jo["staticData"] = fromPluginStaticData(resp.staticData); |
| 752 jo["defaultConfiguration"] = | 751 jo["defaultConfiguration"] = |
| 753 fromPluginConfiguration(resp.defaultConfiguration); | 752 fromPluginConfiguration(resp.defaultConfiguration); |
| 754 return json11::Json(jo); | 753 return json11::Json(jo); |
| 755 } | 754 } |
| 756 | 755 |
| 757 static Vamp::HostExt::LoadResponse | 756 static Vamp::HostExt::LoadResponse |
| 758 toLoadResponse(json11::Json j, | 757 toLoadResponse(json11::Json j, |
| 759 const PluginHandleMapper &mapper) { | 758 const PluginHandleMapper &pmapper) { |
| 760 | 759 |
| 761 std::string err; | 760 std::string err; |
| 762 | 761 |
| 763 if (!j.has_shape({ | 762 if (!j.has_shape({ |
| 764 { "pluginHandle", json11::Json::NUMBER }, | 763 { "pluginHandle", json11::Json::NUMBER }, |
| 766 { "defaultConfiguration", json11::Json::OBJECT } }, err)) { | 765 { "defaultConfiguration", json11::Json::OBJECT } }, err)) { |
| 767 throw Failure("malformed load response: " + err); | 766 throw Failure("malformed load response: " + err); |
| 768 } | 767 } |
| 769 | 768 |
| 770 Vamp::HostExt::LoadResponse resp; | 769 Vamp::HostExt::LoadResponse resp; |
| 771 resp.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value()); | 770 resp.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); |
| 772 resp.staticData = toPluginStaticData(j["staticData"]); | 771 resp.staticData = toPluginStaticData(j["staticData"]); |
| 773 resp.defaultConfiguration = toPluginConfiguration(j["defaultConfiguration"]); | 772 resp.defaultConfiguration = toPluginConfiguration(j["defaultConfiguration"]); |
| 774 return resp; | 773 return resp; |
| 775 } | 774 } |
| 776 | 775 |
| 777 static json11::Json | 776 static json11::Json |
| 778 fromConfigurationRequest(const Vamp::HostExt::ConfigurationRequest &cr, | 777 fromConfigurationRequest(const Vamp::HostExt::ConfigurationRequest &cr, |
| 779 const PluginHandleMapper &mapper) { | 778 const PluginHandleMapper &pmapper) { |
| 780 | 779 |
| 781 json11::Json::object jo; | 780 json11::Json::object jo; |
| 782 | 781 |
| 783 jo["pluginHandle"] = mapper.pluginToHandle(cr.plugin); | 782 jo["pluginHandle"] = pmapper.pluginToHandle(cr.plugin); |
| 784 jo["configuration"] = fromPluginConfiguration(cr.configuration); | 783 jo["configuration"] = fromPluginConfiguration(cr.configuration); |
| 785 | 784 |
| 786 return json11::Json(jo); | 785 return json11::Json(jo); |
| 787 } | 786 } |
| 788 | 787 |
| 789 static Vamp::HostExt::ConfigurationRequest | 788 static Vamp::HostExt::ConfigurationRequest |
| 790 toConfigurationRequest(json11::Json j, | 789 toConfigurationRequest(json11::Json j, |
| 791 const PluginHandleMapper &mapper) { | 790 const PluginHandleMapper &pmapper) { |
| 792 | 791 |
| 793 std::string err; | 792 std::string err; |
| 794 | 793 |
| 795 if (!j.has_shape({ | 794 if (!j.has_shape({ |
| 796 { "pluginHandle", json11::Json::NUMBER }, | 795 { "pluginHandle", json11::Json::NUMBER }, |
| 797 { "configuration", json11::Json::OBJECT } }, err)) { | 796 { "configuration", json11::Json::OBJECT } }, err)) { |
| 798 throw Failure("malformed configuration request: " + err); | 797 throw Failure("malformed configuration request: " + err); |
| 799 } | 798 } |
| 800 | 799 |
| 801 Vamp::HostExt::ConfigurationRequest cr; | 800 Vamp::HostExt::ConfigurationRequest cr; |
| 802 cr.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value()); | 801 cr.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); |
| 803 cr.configuration = toPluginConfiguration(j["configuration"]); | 802 cr.configuration = toPluginConfiguration(j["configuration"]); |
| 804 return cr; | 803 return cr; |
| 805 } | 804 } |
| 806 | 805 |
| 807 static json11::Json | 806 static json11::Json |
| 834 return cr; | 833 return cr; |
| 835 } | 834 } |
| 836 | 835 |
| 837 static json11::Json | 836 static json11::Json |
| 838 fromProcessRequest(const Vamp::HostExt::ProcessRequest &r, | 837 fromProcessRequest(const Vamp::HostExt::ProcessRequest &r, |
| 839 const PluginHandleMapper &mapper, | 838 const PluginHandleMapper &pmapper, |
| 840 BufferSerialisation serialisation) { | 839 BufferSerialisation serialisation) { |
| 841 | 840 |
| 842 json11::Json::object jo; | 841 json11::Json::object jo; |
| 843 jo["pluginHandle"] = mapper.pluginToHandle(r.plugin); | 842 jo["pluginHandle"] = pmapper.pluginToHandle(r.plugin); |
| 844 | 843 |
| 845 json11::Json::object io; | 844 json11::Json::object io; |
| 846 io["timestamp"] = fromRealTime(r.timestamp); | 845 io["timestamp"] = fromRealTime(r.timestamp); |
| 847 | 846 |
| 848 json11::Json::array chans; | 847 json11::Json::array chans; |
| 863 return json11::Json(jo); | 862 return json11::Json(jo); |
| 864 } | 863 } |
| 865 | 864 |
| 866 static Vamp::HostExt::ProcessRequest | 865 static Vamp::HostExt::ProcessRequest |
| 867 toProcessRequest(json11::Json j, | 866 toProcessRequest(json11::Json j, |
| 868 const PluginHandleMapper &mapper, | 867 const PluginHandleMapper &pmapper, |
| 869 BufferSerialisation &serialisation) { | 868 BufferSerialisation &serialisation) { |
| 870 | 869 |
| 871 std::string err; | 870 std::string err; |
| 872 | 871 |
| 873 if (!j.has_shape({ | 872 if (!j.has_shape({ |
| 883 { "inputBuffers", json11::Json::ARRAY } }, err)) { | 882 { "inputBuffers", json11::Json::ARRAY } }, err)) { |
| 884 throw Failure("malformed process request: " + err); | 883 throw Failure("malformed process request: " + err); |
| 885 } | 884 } |
| 886 | 885 |
| 887 Vamp::HostExt::ProcessRequest r; | 886 Vamp::HostExt::ProcessRequest r; |
| 888 r.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value()); | 887 r.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); |
| 889 | 888 |
| 890 r.timestamp = toRealTime(input["timestamp"]); | 889 r.timestamp = toRealTime(input["timestamp"]); |
| 891 | 890 |
| 892 for (auto a: input["inputBuffers"].array_items()) { | 891 for (auto a: input["inputBuffers"].array_items()) { |
| 893 | 892 |
| 949 return json11::Json(jo); | 948 return json11::Json(jo); |
| 950 } | 949 } |
| 951 | 950 |
| 952 static json11::Json | 951 static json11::Json |
| 953 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, | 952 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, |
| 954 const PluginHandleMapper &mapper) { | 953 const PluginHandleMapper &pmapper) { |
| 955 | 954 |
| 956 json11::Json::object jo; | 955 json11::Json::object jo; |
| 957 jo["type"] = "load"; | 956 jo["type"] = "load"; |
| 958 jo["success"] = (resp.plugin != 0); | 957 jo["success"] = (resp.plugin != 0); |
| 959 jo["errorText"] = ""; | 958 jo["errorText"] = ""; |
| 960 jo["content"] = fromLoadResponse(resp, mapper); | 959 jo["content"] = fromLoadResponse(resp, pmapper); |
| 961 return json11::Json(jo); | 960 return json11::Json(jo); |
| 962 } | 961 } |
| 963 | 962 |
| 964 static json11::Json | 963 static json11::Json |
| 965 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, | 964 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, |
| 966 const PluginHandleMapper &mapper) { | 965 const PluginHandleMapper &pmapper) { |
| 967 | 966 |
| 968 json11::Json::object jo; | 967 json11::Json::object jo; |
| 969 jo["type"] = "configure"; | 968 jo["type"] = "configure"; |
| 970 jo["content"] = fromConfigurationRequest(req, mapper); | 969 jo["content"] = fromConfigurationRequest(req, pmapper); |
| 971 return json11::Json(jo); | 970 return json11::Json(jo); |
| 972 } | 971 } |
| 973 | 972 |
| 974 static json11::Json | 973 static json11::Json |
| 975 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { | 974 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { |
| 982 return json11::Json(jo); | 981 return json11::Json(jo); |
| 983 } | 982 } |
| 984 | 983 |
| 985 static json11::Json | 984 static json11::Json |
| 986 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, | 985 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, |
| 987 const PluginHandleMapper &mapper, | 986 const PluginHandleMapper &pmapper, |
| 988 BufferSerialisation serialisation) { | 987 BufferSerialisation serialisation) { |
| 989 | 988 |
| 990 json11::Json::object jo; | 989 json11::Json::object jo; |
| 991 jo["type"] = "process"; | 990 jo["type"] = "process"; |
| 992 jo["content"] = fromProcessRequest(req, mapper, serialisation); | 991 jo["content"] = fromProcessRequest(req, pmapper, serialisation); |
| 993 return json11::Json(jo); | 992 return json11::Json(jo); |
| 994 } | 993 } |
| 995 | 994 |
| 996 static json11::Json | 995 static json11::Json |
| 997 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp, | 996 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp, |
| 997 const PluginOutputIdMapper &omapper, | |
| 998 BufferSerialisation serialisation) { | 998 BufferSerialisation serialisation) { |
| 999 | 999 |
| 1000 json11::Json::object jo; | 1000 json11::Json::object jo; |
| 1001 jo["type"] = "process"; | 1001 jo["type"] = "process"; |
| 1002 jo["success"] = true; | 1002 jo["success"] = true; |
| 1003 jo["errorText"] = ""; | 1003 jo["errorText"] = ""; |
| 1004 jo["content"] = fromFeatureSet(resp.features, serialisation); | 1004 jo["content"] = fromFeatureSet(resp.features, omapper, serialisation); |
| 1005 return json11::Json(jo); | 1005 return json11::Json(jo); |
| 1006 } | 1006 } |
| 1007 | 1007 |
| 1008 static json11::Json | 1008 static json11::Json |
| 1009 fromVampRequest_Finish(Vamp::Plugin *p, | 1009 fromVampRequest_Finish(Vamp::Plugin *p, |
| 1010 const PluginHandleMapper &mapper) { | 1010 const PluginHandleMapper &pmapper) { |
| 1011 | 1011 |
| 1012 json11::Json::object jo; | 1012 json11::Json::object jo; |
| 1013 jo["type"] = "finish"; | 1013 jo["type"] = "finish"; |
| 1014 json11::Json::object fo; | 1014 json11::Json::object fo; |
| 1015 fo["pluginHandle"] = mapper.pluginToHandle(p); | 1015 fo["pluginHandle"] = pmapper.pluginToHandle(p); |
| 1016 jo["content"] = fo; | 1016 jo["content"] = fo; |
| 1017 return json11::Json(jo); | 1017 return json11::Json(jo); |
| 1018 } | 1018 } |
| 1019 | 1019 |
| 1020 static json11::Json | 1020 static json11::Json |
| 1021 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp, | 1021 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp, |
| 1022 const PluginOutputIdMapper &omapper, | |
| 1022 BufferSerialisation serialisation) { | 1023 BufferSerialisation serialisation) { |
| 1023 | 1024 |
| 1024 json11::Json::object jo; | 1025 json11::Json::object jo; |
| 1025 jo["type"] = "finish"; | 1026 jo["type"] = "finish"; |
| 1026 jo["success"] = true; | 1027 jo["success"] = true; |
| 1027 jo["errorText"] = ""; | 1028 jo["errorText"] = ""; |
| 1028 jo["content"] = fromFeatureSet(resp.features, serialisation); | 1029 jo["content"] = fromFeatureSet(resp.features, omapper, serialisation); |
| 1029 return json11::Json(jo); | 1030 return json11::Json(jo); |
| 1030 } | 1031 } |
| 1031 | 1032 |
| 1032 static json11::Json | 1033 static json11::Json |
| 1033 fromException(const std::exception &e, RRType responseType) { | 1034 fromException(const std::exception &e, RRType responseType) { |
| 1114 checkTypeField(j, "load"); | 1115 checkTypeField(j, "load"); |
| 1115 return toLoadRequest(j["content"]); | 1116 return toLoadRequest(j["content"]); |
| 1116 } | 1117 } |
| 1117 | 1118 |
| 1118 static Vamp::HostExt::LoadResponse | 1119 static Vamp::HostExt::LoadResponse |
| 1119 toVampResponse_Load(json11::Json j, const PluginHandleMapper &mapper) { | 1120 toVampResponse_Load(json11::Json j, const PluginHandleMapper &pmapper) { |
| 1120 | 1121 |
| 1121 Vamp::HostExt::LoadResponse resp; | 1122 Vamp::HostExt::LoadResponse resp; |
| 1122 if (successful(j)) { | 1123 if (successful(j)) { |
| 1123 resp = toLoadResponse(j["content"], mapper); | 1124 resp = toLoadResponse(j["content"], pmapper); |
| 1124 } | 1125 } |
| 1125 return resp; | 1126 return resp; |
| 1126 } | 1127 } |
| 1127 | 1128 |
| 1128 static Vamp::HostExt::ConfigurationRequest | 1129 static Vamp::HostExt::ConfigurationRequest |
| 1129 toVampRequest_Configure(json11::Json j, const PluginHandleMapper &mapper) { | 1130 toVampRequest_Configure(json11::Json j, const PluginHandleMapper &pmapper) { |
| 1130 | 1131 |
| 1131 checkTypeField(j, "configure"); | 1132 checkTypeField(j, "configure"); |
| 1132 return toConfigurationRequest(j["content"], mapper); | 1133 return toConfigurationRequest(j["content"], pmapper); |
| 1133 } | 1134 } |
| 1134 | 1135 |
| 1135 static Vamp::HostExt::ConfigurationResponse | 1136 static Vamp::HostExt::ConfigurationResponse |
| 1136 toVampResponse_Configure(json11::Json j) { | 1137 toVampResponse_Configure(json11::Json j) { |
| 1137 | 1138 |
| 1141 } | 1142 } |
| 1142 return resp; | 1143 return resp; |
| 1143 } | 1144 } |
| 1144 | 1145 |
| 1145 static Vamp::HostExt::ProcessRequest | 1146 static Vamp::HostExt::ProcessRequest |
| 1146 toVampRequest_Process(json11::Json j, const PluginHandleMapper &mapper, | 1147 toVampRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, |
| 1147 BufferSerialisation &serialisation) { | 1148 BufferSerialisation &serialisation) { |
| 1148 | 1149 |
| 1149 checkTypeField(j, "process"); | 1150 checkTypeField(j, "process"); |
| 1150 return toProcessRequest(j["content"], mapper, serialisation); | 1151 return toProcessRequest(j["content"], pmapper, serialisation); |
| 1151 } | 1152 } |
| 1152 | 1153 |
| 1153 static Vamp::HostExt::ProcessResponse | 1154 static Vamp::HostExt::ProcessResponse |
| 1154 toVampResponse_Process(json11::Json j, BufferSerialisation &serialisation) { | 1155 toVampResponse_Process(json11::Json j, |
| 1156 const PluginOutputIdMapper &omapper, | |
| 1157 BufferSerialisation &serialisation) { | |
| 1155 | 1158 |
| 1156 Vamp::HostExt::ProcessResponse resp; | 1159 Vamp::HostExt::ProcessResponse resp; |
| 1157 if (successful(j)) { | 1160 if (successful(j)) { |
| 1158 resp.features = toFeatureSet(j["content"], serialisation); | 1161 resp.features = toFeatureSet(j["content"], omapper, serialisation); |
| 1159 } | 1162 } |
| 1160 return resp; | 1163 return resp; |
| 1161 } | 1164 } |
| 1162 | 1165 |
| 1163 static Vamp::Plugin * | 1166 static Vamp::Plugin * |
| 1164 toVampRequest_Finish(json11::Json j, const PluginHandleMapper &mapper) { | 1167 toVampRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper) { |
| 1165 | 1168 |
| 1166 checkTypeField(j, "finish"); | 1169 checkTypeField(j, "finish"); |
| 1167 return mapper.handleToPlugin(j["content"]["pluginHandle"].int_value()); | 1170 return pmapper.handleToPlugin(j["content"]["pluginHandle"].int_value()); |
| 1168 } | 1171 } |
| 1169 | 1172 |
| 1170 static Vamp::HostExt::ProcessResponse | 1173 static Vamp::HostExt::ProcessResponse |
| 1171 toVampResponse_Finish(json11::Json j, BufferSerialisation &serialisation) { | 1174 toVampResponse_Finish(json11::Json j, |
| 1175 const PluginOutputIdMapper &omapper, | |
| 1176 BufferSerialisation &serialisation) { | |
| 1172 | 1177 |
| 1173 Vamp::HostExt::ProcessResponse resp; | 1178 Vamp::HostExt::ProcessResponse resp; |
| 1174 if (successful(j)) { | 1179 if (successful(j)) { |
| 1175 resp.features = toFeatureSet(j["content"], serialisation); | 1180 resp.features = toFeatureSet(j["content"], omapper, serialisation); |
| 1176 } | 1181 } |
| 1177 return resp; | 1182 return resp; |
| 1178 } | 1183 } |
| 1179 }; | 1184 }; |
| 1180 | 1185 |
