Mercurial > hg > piper-cpp
comparison json/VampJson.h @ 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 |
comparison
equal
deleted
inserted
replaced
16:913fc1d3710a | 17:3ef01276e15e |
---|---|
638 | 638 |
639 return Vamp::HostExt::PluginLoader::AdapterFlags(flags); | 639 return Vamp::HostExt::PluginLoader::AdapterFlags(flags); |
640 } | 640 } |
641 | 641 |
642 static json11::Json | 642 static json11::Json |
643 fromLoadRequest(Vamp::HostExt::LoadRequest req) { | 643 fromLoadRequest(const Vamp::HostExt::LoadRequest &req) { |
644 | 644 |
645 json11::Json::object jo; | 645 json11::Json::object jo; |
646 jo["pluginKey"] = req.pluginKey; | 646 jo["pluginKey"] = req.pluginKey; |
647 jo["inputSampleRate"] = req.inputSampleRate; | 647 jo["inputSampleRate"] = req.inputSampleRate; |
648 jo["adapterFlags"] = fromAdapterFlags(req.adapterFlags); | 648 jo["adapterFlags"] = fromAdapterFlags(req.adapterFlags); |
667 req.adapterFlags = toAdapterFlags(j["adapterFlags"]); | 667 req.adapterFlags = toAdapterFlags(j["adapterFlags"]); |
668 return req; | 668 return req; |
669 } | 669 } |
670 | 670 |
671 static json11::Json | 671 static json11::Json |
672 fromLoadResponse(Vamp::HostExt::LoadResponse resp, | 672 fromLoadResponse(const Vamp::HostExt::LoadResponse &resp, |
673 PluginHandleMapper &mapper) { | 673 PluginHandleMapper &mapper) { |
674 | 674 |
675 json11::Json::object jo; | 675 json11::Json::object jo; |
676 jo["pluginHandle"] = double(mapper.pluginToHandle(resp.plugin)); | 676 jo["pluginHandle"] = double(mapper.pluginToHandle(resp.plugin)); |
677 jo["staticData"] = fromPluginStaticData(resp.staticData); | 677 jo["staticData"] = fromPluginStaticData(resp.staticData); |
780 io["inputBuffers"] = chans; | 780 io["inputBuffers"] = chans; |
781 | 781 |
782 jo["processInput"] = io; | 782 jo["processInput"] = io; |
783 return json11::Json(jo); | 783 return json11::Json(jo); |
784 } | 784 } |
785 | |
786 static Vamp::HostExt::ProcessRequest | |
787 toProcessRequest(json11::Json j, PluginHandleMapper &mapper) { | |
788 | |
789 std::string err; | |
790 | |
791 if (!j.has_shape({ | |
792 { "pluginHandle", json11::Json::NUMBER }, | |
793 { "processInput", json11::Json::OBJECT } }, err)) { | |
794 throw Failure("malformed process request: " + err); | |
795 } | |
796 | |
797 auto input = j["processInput"]; | |
798 | |
799 if (!input.has_shape({ | |
800 { "timestamp", json11::Json::OBJECT }, | |
801 { "inputBuffers", json11::Json::ARRAY } }, err)) { | |
802 throw Failure("malformed process request: " + err); | |
803 } | |
804 | |
805 Vamp::HostExt::ProcessRequest r; | |
806 r.plugin = mapper.handleToPlugin(j["pluginHandle"].int_value()); | |
807 | |
808 r.timestamp = toRealTime(input["timestamp"]); | |
809 | |
810 for (auto a: input["inputBuffers"].array_items()) { | |
811 if (a["b64values"].is_string()) { | |
812 r.inputBuffers.push_back(toFloatBuffer | |
813 (a["b64values"].string_value())); | |
814 } else if (a["values"].is_array()) { | |
815 std::vector<float> buf; | |
816 for (auto v : a["values"].array_items()) { | |
817 buf.push_back(v.number_value()); | |
818 } | |
819 r.inputBuffers.push_back(buf); | |
820 } else { | |
821 throw Failure("expected values or b64values in inputBuffers object"); | |
822 } | |
823 } | |
824 | |
825 return r; | |
826 } | |
827 | |
828 static json11::Json | |
829 fromVampRequest_List() { | |
830 | |
831 json11::Json::object jo; | |
832 jo["type"] = "list"; | |
833 return json11::Json(jo); | |
834 } | |
835 | |
836 static json11::Json | |
837 fromVampResponse_List(std::string errorText, | |
838 const std::vector<Vamp::HostExt::PluginStaticData> &d) { | |
839 | |
840 json11::Json::object jo; | |
841 jo["success"] = (errorText == ""); | |
842 jo["errorText"] = errorText; | |
843 | |
844 json11::Json::array arr; | |
845 for (const auto &a: d) { | |
846 arr.push_back(fromPluginStaticData(a)); | |
847 } | |
848 jo["response"] = arr; | |
849 return json11::Json(jo); | |
850 } | |
851 | |
852 static json11::Json | |
853 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { | |
854 | |
855 json11::Json::object jo; | |
856 jo["type"] = "load"; | |
857 jo["content"] = fromLoadRequest(req); | |
858 return json11::Json(jo); | |
859 } | |
860 | |
861 static json11::Json | |
862 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, | |
863 PluginHandleMapper &mapper) { | |
864 | |
865 json11::Json::object jo; | |
866 jo["success"] = (resp.plugin != 0); | |
867 jo["errorText"] = ""; | |
868 jo["response"] = fromLoadResponse(resp, mapper); | |
869 return json11::Json(jo); | |
870 } | |
871 | |
872 static json11::Json | |
873 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, | |
874 PluginHandleMapper &mapper) { | |
875 | |
876 json11::Json::object jo; | |
877 jo["type"] = "configure"; | |
878 jo["content"] = fromConfigurationRequest(req, mapper); | |
879 return json11::Json(jo); | |
880 } | |
881 | |
882 static json11::Json | |
883 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { | |
884 | |
885 json11::Json::object jo; | |
886 jo["success"] = (!resp.outputs.empty()); | |
887 jo["errorText"] = ""; | |
888 jo["response"] = fromConfigurationResponse(resp); | |
889 return json11::Json(jo); | |
890 } | |
891 | |
892 static json11::Json | |
893 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, | |
894 PluginHandleMapper &mapper) { | |
895 | |
896 json11::Json::object jo; | |
897 jo["type"] = "process"; | |
898 jo["content"] = fromProcessRequest(req, mapper); | |
899 return json11::Json(jo); | |
900 } | |
901 | |
902 static json11::Json | |
903 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp) { | |
904 | |
905 json11::Json::object jo; | |
906 jo["success"] = true; | |
907 jo["errorText"] = ""; | |
908 jo["response"] = fromFeatureSet(resp.features); | |
909 return json11::Json(jo); | |
910 } | |
911 | |
912 static json11::Json | |
913 fromVampRequest_Finish() { | |
914 | |
915 json11::Json::object jo; | |
916 jo["type"] = "finish"; | |
917 return json11::Json(jo); | |
918 } | |
919 | |
920 static json11::Json | |
921 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp) { | |
922 | |
923 json11::Json::object jo; | |
924 jo["success"] = true; | |
925 jo["errorText"] = ""; | |
926 jo["response"] = fromFeatureSet(resp.features); | |
927 return json11::Json(jo); | |
928 } | |
785 }; | 929 }; |
786 | 930 |
787 } | 931 } |
788 | 932 |
789 #endif | 933 #endif |