Mercurial > hg > piper-vamp-js
changeset 69:967b0619b090
Start on plugin-library-to-json-request-response-handler adapter
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 22 Aug 2016 17:17:28 +0100 |
parents | |
children | caf75dce15e5 |
files | Makefile.example VamPipeAdapter.h VamPipePluginLibrary.cpp VamPipePluginLibrary.h example.cpp vampipe.map |
diffstat | 6 files changed, 591 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile.example Mon Aug 22 17:17:28 2016 +0100 @@ -0,0 +1,19 @@ + +example.so: example.cpp VamPipeAdapter.h VamPipePluginLibrary.h VamPipePluginLibrary.cpp + c++ -std=c++11 -fPIC \ + -I../vamp-plugin-sdk -I.. -I../json \ + -Wall -Wextra -g \ + -shared -Wl,-Bsymbolic -Wl,-soname=example.so -Wl,-z,defs -Wl,--version-script=vampipe.map \ + example.cpp \ + VamPipePluginLibrary.cpp \ + ../vamp-plugin-sdk/examples/ZeroCrossing.cpp \ + ../vamp-plugin-sdk/examples/SpectralCentroid.cpp \ + ../vamp-plugin-sdk/examples/PercussionOnsetDetector.cpp \ + ../vamp-plugin-sdk/examples/FixedTempoEstimator.cpp \ + ../vamp-plugin-sdk/examples/AmplitudeFollower.cpp \ + ../vamp-plugin-sdk/examples/PowerSpectrum.cpp \ + ../json/json11/json11.cpp \ + ../vamp-plugin-sdk/libvamp-hostsdk.a \ + ../vamp-plugin-sdk/libvamp-sdk.a \ + -o example.so +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VamPipeAdapter.h Mon Aug 22 17:17:28 2016 +0100 @@ -0,0 +1,126 @@ +/* -*- 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 2015-2016 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_ADAPTER_H +#define VAMPIPE_ADAPTER_H + +#include <vamp-hostsdk/PluginStaticData.h> +#include <vamp-hostsdk/PluginConfiguration.h> +#include <vamp-hostsdk/RequestResponse.h> + +#include <vamp-hostsdk/PluginInputDomainAdapter.h> +#include <vamp-hostsdk/PluginBufferingAdapter.h> +#include <vamp-hostsdk/PluginChannelAdapter.h> + +namespace vampipe { + +class VamPipeAdapterBase +{ +public: + virtual Vamp::HostExt::PluginStaticData getStaticData() const = 0; + virtual Vamp::HostExt::LoadResponse loadPlugin(Vamp::HostExt::LoadRequest r) const = 0; +}; + +template <typename P> +class VamPipeAdapter : public VamPipeAdapterBase +{ + const int adaptInputDomain = 0x01; + const int adaptChannelCount = 0x02; + const int adaptBufferSize = 0x04; + +public: + VamPipeAdapter(std::string libname) : + m_soname(libname) { } + + virtual Vamp::HostExt::PluginStaticData getStaticData() const { + P p(44100.f); + return Vamp::HostExt::PluginStaticData::fromPlugin + (m_soname + ":" + p.getIdentifier(), + {}, //!!! todo: category - tricky one that + &p); + } + + virtual Vamp::HostExt::LoadResponse loadPlugin(Vamp::HostExt:: + LoadRequest r) const { + + // We assume the caller has guaranteed that the request is for + // the correct plugin (so we don't have to check the plugin + // key field here) + + Vamp::Plugin *p = new P(r.inputSampleRate); + + if (r.adapterFlags & adaptInputDomain) { + if (p->getInputDomain() == Vamp::Plugin::FrequencyDomain) { + p = new Vamp::HostExt::PluginInputDomainAdapter(p); + } + } + + if (r.adapterFlags & adaptBufferSize) { + p = new Vamp::HostExt::PluginBufferingAdapter(p); + } + + if (r.adapterFlags & adaptChannelCount) { + p = new Vamp::HostExt::PluginChannelAdapter(p); + } + + Vamp::HostExt::LoadResponse response; + response.plugin = p; + + response.staticData = Vamp::HostExt::PluginStaticData::fromPlugin + (m_soname + ":" + p->getIdentifier(), + {}, //!!! todo: category - tricky one that + p); + + int defaultChannels = 0; + if (p->getMinChannelCount() == p->getMaxChannelCount()) { + defaultChannels = p->getMinChannelCount(); + } + + response.defaultConfiguration = Vamp::HostExt::PluginConfiguration::fromPlugin + (p, + defaultChannels, + p->getPreferredStepSize(), + p->getPreferredBlockSize()); + + return response; + } + +private: + std::string m_soname; +}; + +} + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VamPipePluginLibrary.cpp Mon Aug 22 17:17:28 2016 +0100 @@ -0,0 +1,279 @@ +/* -*- 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 2015-2016 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. +*/ + +#include "VamPipePluginLibrary.h" +#include "VamPipeAdapter.h" +#include "json/VampJson.h" + +using namespace std; +using namespace json11; + +namespace vampipe { + +//!!! too many explicit namespaces here + +//!!! dup with vampipe-convert +static Json +convertRequestJson(string input) +{ + string err; + Json j = Json::parse(input, err); + if (err != "") { + throw VampJson::Failure("invalid json: " + err); + } + if (!j.is_object()) { + throw VampJson::Failure("object expected at top level"); + } + if (!j["type"].is_string()) { + throw VampJson::Failure("string expected for type field"); + } + if (!j["content"].is_object()) { + throw VampJson::Failure("object expected for content field"); + } + return j; +} + +VamPipePluginLibrary::VamPipePluginLibrary(vector<VamPipeAdapterBase *> pp) +{ + for (VamPipeAdapterBase *p: pp) { + string key = p->getStaticData().pluginKey; + m_adapters[key] = p; + } +} + +RequestOrResponse +VamPipePluginLibrary::readRequest(string req) const +{ + RequestOrResponse rr; + rr.direction = RequestOrResponse::Request; + + //!!! todo: handle exceptions (can't pass through C abi) + Json j = convertRequestJson(req); + + //!!! reduce, reduce + rr.type = VampJson::getRequestResponseType(j); + + switch (rr.type) { + + case RRType::List: + VampJson::toVampRequest_List(j); // type check only + break; + case RRType::Load: + rr.loadRequest = VampJson::toVampRequest_Load(j); + break; + case RRType::Configure: + rr.configurationRequest = VampJson::toVampRequest_Configure(j, m_mapper); + break; + case RRType::Process: + rr.processRequest = VampJson::toVampRequest_Process(j, m_mapper); + break; + case RRType::Finish: + rr.finishPlugin = VampJson::toVampRequest_Finish(j, m_mapper); + break; + case RRType::NotValid: + break; + } + + return rr; +} + +string +VamPipePluginLibrary::writeResponse(const RequestOrResponse &rr) const +{ + Json j; + + switch (rr.type) { + + case RRType::List: + j = VampJson::fromVampResponse_List("", rr.listResponse); + break; + case RRType::Load: + j = VampJson::fromVampResponse_Load(rr.loadResponse, m_mapper); + break; + case RRType::Configure: + j = VampJson::fromVampResponse_Configure(rr.configurationResponse); + break; + case RRType::Process: + j = VampJson::fromVampResponse_Process(rr.processResponse); + break; + case RRType::Finish: + j = VampJson::fromVampResponse_Finish(rr.finishResponse); + break; + case RRType::NotValid: + break; + } + + return j.dump(); +} + +vector<Vamp::HostExt::PluginStaticData> +VamPipePluginLibrary::listPluginData() const +{ + vector<Vamp::HostExt::PluginStaticData> data; + for (auto a: m_adapters) { + data.push_back(a.second->getStaticData()); + } + return data; +} + +Vamp::HostExt::LoadResponse +VamPipePluginLibrary::loadPlugin(Vamp::HostExt::LoadRequest req) const +{ + string key = req.pluginKey; + if (m_adapters.find(key) != m_adapters.end()) { + return m_adapters.at(key)->loadPlugin(req); + } else { + throw runtime_error("no adapter for plugin key " + key); + } +} + +Vamp::HostExt::ConfigurationResponse +VamPipePluginLibrary::configurePlugin(Vamp::HostExt::ConfigurationRequest req) const +{ + for (Vamp::HostExt::PluginConfiguration::ParameterMap::const_iterator i = + req.configuration.parameterValues.begin(); + i != req.configuration.parameterValues.end(); ++i) { + req.plugin->setParameter(i->first, i->second); + } + + if (req.configuration.currentProgram != "") { + req.plugin->selectProgram(req.configuration.currentProgram); + } + + Vamp::HostExt::ConfigurationResponse response; + + if (req.plugin->initialise(req.configuration.channelCount, + req.configuration.stepSize, + req.configuration.blockSize)) { + response.outputs = req.plugin->getOutputDescriptors(); + } + + return response; +} + +string +VamPipePluginLibrary::requestJsonImpl(string req) +{ + RequestOrResponse request = readRequest(req); + + RequestOrResponse response; + response.direction = RequestOrResponse::Response; + response.type = request.type; + + switch (request.type) { + + case RRType::List: + response.listResponse = listPluginData(); + response.success = true; + break; + + case RRType::Load: + response.loadResponse = loadPlugin(request.loadRequest); + if (response.loadResponse.plugin) { + m_mapper.addPlugin(response.loadResponse.plugin); + response.success = true; + } + break; + + case RRType::Configure: + { + auto &creq = request.configurationRequest; + auto h = m_mapper.pluginToHandle(creq.plugin); + if (m_mapper.isConfigured(h)) { + //!!! again, can't return through C abi + throw runtime_error("plugin has already been configured"); + } + + response.configurationResponse = configurePlugin(creq); + + if (!response.configurationResponse.outputs.empty()) { + m_mapper.markConfigured + (h, creq.configuration.channelCount, creq.configuration.blockSize); + response.success = true; + } + break; + } + + case RRType::Process: + { + auto &preq = request.processRequest; + auto h = m_mapper.pluginToHandle(preq.plugin); + if (!m_mapper.isConfigured(h)) { + throw runtime_error("plugin has not been configured"); + } + + int channels = int(preq.inputBuffers.size()); + if (channels != m_mapper.getChannelCount(h)) { + throw runtime_error("wrong number of channels supplied to process"); + } + + const float **fbuffers = new const float *[channels]; + for (int i = 0; i < channels; ++i) { + if (int(preq.inputBuffers[i].size()) != m_mapper.getBlockSize(h)) { + delete[] fbuffers; + throw runtime_error("wrong block size supplied to process"); + } + fbuffers[i] = preq.inputBuffers[i].data(); + } + + response.processResponse.features = + preq.plugin->process(fbuffers, preq.timestamp); + response.success = true; + + delete[] fbuffers; + break; + } + + case RRType::Finish: + { + auto h = m_mapper.pluginToHandle(request.finishPlugin); + + response.finishResponse.features = + request.finishPlugin->getRemainingFeatures(); + + m_mapper.removePlugin(h); + delete request.finishPlugin; + response.success = true; + break; + } + + case RRType::NotValid: + break; + } + + return writeResponse(response); +} + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VamPipePluginLibrary.h Mon Aug 22 17:17:28 2016 +0100 @@ -0,0 +1,86 @@ +/* -*- 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 2015-2016 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_LIBRARY_H +#define VAMPIPE_PLUGIN_LIBRARY_H + +#include "bits/CountingPluginHandleMapper.h" +#include "bits/RequestOrResponse.h" + +#include <vamp-hostsdk/PluginStaticData.h> +#include <vamp-hostsdk/RequestResponse.h> + +#include <vector> +#include <string> +#include <cstring> + +namespace vampipe { + +class VamPipeAdapterBase; + +class VamPipePluginLibrary +{ +public: + VamPipePluginLibrary(std::vector<VamPipeAdapterBase *> pp); + + const char *requestJson(const char *request) { + return strdup(requestJsonImpl(request).c_str()); + } + + void freeJson(const char *json) { + free(const_cast<char *>(json)); + } + +private: + std::string requestJsonImpl(std::string req); + + RequestOrResponse readRequest(std::string req) const; + std::string writeResponse(const RequestOrResponse &resp) const; + + //!!! pull out these next few into another class, a sort of mini + //!!! library-level analogue to PluginLoader ? + + //!!! no type for this in RequestResponse.h in vamp sdk, should there be? + std::vector<Vamp::HostExt::PluginStaticData> listPluginData() const; + Vamp::HostExt::LoadResponse loadPlugin(Vamp::HostExt::LoadRequest r) const; + Vamp::HostExt::ConfigurationResponse configurePlugin(Vamp::HostExt:: + ConfigurationRequest r) const; + + std::map<std::string, VamPipeAdapterBase *> m_adapters; // pluginKey -> adapter + CountingPluginHandleMapper m_mapper; +}; + +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example.cpp Mon Aug 22 17:17:28 2016 +0100 @@ -0,0 +1,77 @@ +/* -*- 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 2015-2016 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. +*/ + +#include "VamPipeAdapter.h" +#include "VamPipePluginLibrary.h" + +#include "vamp-plugin-sdk/examples/ZeroCrossing.h" +#include "vamp-plugin-sdk/examples/SpectralCentroid.h" +#include "vamp-plugin-sdk/examples/PercussionOnsetDetector.h" +#include "vamp-plugin-sdk/examples/FixedTempoEstimator.h" +#include "vamp-plugin-sdk/examples/AmplitudeFollower.h" +#include "vamp-plugin-sdk/examples/PowerSpectrum.h" + +using vampipe::VamPipeAdapter; +using vampipe::VamPipePluginLibrary; + +static std::string soname("vamp-example-plugins"); + +static VamPipeAdapter<ZeroCrossing> zeroCrossingAdapter(soname); +static VamPipeAdapter<SpectralCentroid> spectralCentroidAdapter(soname); +static VamPipeAdapter<PercussionOnsetDetector> percussionOnsetAdapter(soname); +static VamPipeAdapter<FixedTempoEstimator> fixedTempoAdapter(soname); +static VamPipeAdapter<AmplitudeFollower> amplitudeAdapter(soname); +static VamPipeAdapter<PowerSpectrum> powerSpectrumAdapter(soname); + +static VamPipePluginLibrary library({ + &zeroCrossingAdapter, + &spectralCentroidAdapter, + &percussionOnsetAdapter, + &fixedTempoAdapter, + &litudeAdapter, + &powerSpectrumAdapter +}); + +extern "C" { + +const char *vampipeRequestJson(const char *request) { + return library.requestJson(request); +} + +void vampipeFreeJson(const char *json) { + return library.freeJson(json); +} + +} +