c@69: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@69: c@69: /* c@69: VamPipe c@69: c@69: Centre for Digital Music, Queen Mary, University of London. c@69: Copyright 2015-2016 QMUL. c@69: c@69: Permission is hereby granted, free of charge, to any person c@69: obtaining a copy of this software and associated documentation c@69: files (the "Software"), to deal in the Software without c@69: restriction, including without limitation the rights to use, copy, c@69: modify, merge, publish, distribute, sublicense, and/or sell copies c@69: of the Software, and to permit persons to whom the Software is c@69: furnished to do so, subject to the following conditions: c@69: c@69: The above copyright notice and this permission notice shall be c@69: included in all copies or substantial portions of the Software. c@69: c@69: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@69: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@69: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@69: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@69: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@69: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@69: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@69: c@69: Except as contained in this notice, the names of the Centre for c@69: Digital Music; Queen Mary, University of London; and Chris Cannam c@69: shall not be used in advertising or otherwise to promote the sale, c@69: use or other dealings in this Software without prior written c@69: authorization. c@69: */ c@69: c@69: #include "VamPipePluginLibrary.h" c@69: #include "VamPipeAdapter.h" c@69: #include "json/VampJson.h" c@69: c@69: using namespace std; c@69: using namespace json11; c@69: c@69: namespace vampipe { c@69: c@69: //!!! too many explicit namespaces here c@69: c@69: //!!! dup with vampipe-convert c@69: static Json c@69: convertRequestJson(string input) c@69: { c@69: string err; c@69: Json j = Json::parse(input, err); c@69: if (err != "") { c@69: throw VampJson::Failure("invalid json: " + err); c@69: } c@69: if (!j.is_object()) { c@69: throw VampJson::Failure("object expected at top level"); c@69: } c@69: if (!j["type"].is_string()) { c@69: throw VampJson::Failure("string expected for type field"); c@69: } c@71: if (!j["content"].is_null() && !j["content"].is_object()) { c@69: throw VampJson::Failure("object expected for content field"); c@69: } c@69: return j; c@69: } c@69: c@81: VamPipePluginLibrary::VamPipePluginLibrary(vector pp) : c@81: m_useBase64(false) c@69: { c@69: for (VamPipeAdapterBase *p: pp) { c@69: string key = p->getStaticData().pluginKey; c@69: m_adapters[key] = p; c@69: } c@69: } c@69: c@69: RequestOrResponse c@81: VamPipePluginLibrary::readRequest(string req) c@69: { c@69: RequestOrResponse rr; c@69: rr.direction = RequestOrResponse::Request; c@69: c@69: Json j = convertRequestJson(req); c@69: c@69: //!!! reduce, reduce c@69: rr.type = VampJson::getRequestResponseType(j); c@81: VampJson::BufferSerialisation serialisation = VampJson::BufferSerialisation::Text; c@69: c@69: switch (rr.type) { c@69: c@69: case RRType::List: c@69: VampJson::toVampRequest_List(j); // type check only c@69: break; c@69: case RRType::Load: c@69: rr.loadRequest = VampJson::toVampRequest_Load(j); c@69: break; c@69: case RRType::Configure: c@69: rr.configurationRequest = VampJson::toVampRequest_Configure(j, m_mapper); c@69: break; c@69: case RRType::Process: c@81: rr.processRequest = VampJson::toVampRequest_Process(j, m_mapper, serialisation); c@69: break; c@69: case RRType::Finish: c@69: rr.finishPlugin = VampJson::toVampRequest_Finish(j, m_mapper); c@69: break; c@69: case RRType::NotValid: c@69: break; c@69: } c@69: c@81: if (serialisation == VampJson::BufferSerialisation::Base64) { c@81: m_useBase64 = true; c@81: } c@81: c@69: return rr; c@69: } c@69: c@69: string c@69: VamPipePluginLibrary::writeResponse(const RequestOrResponse &rr) const c@69: { c@69: Json j; c@69: c@81: VampJson::BufferSerialisation serialisation = c@81: (m_useBase64 ? c@81: VampJson::BufferSerialisation::Base64 : c@81: VampJson::BufferSerialisation::Text); c@81: c@69: switch (rr.type) { c@69: c@69: case RRType::List: c@69: j = VampJson::fromVampResponse_List("", rr.listResponse); c@69: break; c@69: case RRType::Load: c@69: j = VampJson::fromVampResponse_Load(rr.loadResponse, m_mapper); c@69: break; c@69: case RRType::Configure: c@69: j = VampJson::fromVampResponse_Configure(rr.configurationResponse); c@69: break; c@69: case RRType::Process: c@81: j = VampJson::fromVampResponse_Process(rr.processResponse, serialisation); c@69: break; c@69: case RRType::Finish: c@81: j = VampJson::fromVampResponse_Finish(rr.finishResponse, serialisation); c@69: break; c@69: case RRType::NotValid: c@69: break; c@69: } c@69: c@69: return j.dump(); c@69: } c@69: c@69: vector c@69: VamPipePluginLibrary::listPluginData() const c@69: { c@69: vector data; c@69: for (auto a: m_adapters) { c@69: data.push_back(a.second->getStaticData()); c@69: } c@69: return data; c@69: } c@69: c@69: Vamp::HostExt::LoadResponse c@69: VamPipePluginLibrary::loadPlugin(Vamp::HostExt::LoadRequest req) const c@69: { c@69: string key = req.pluginKey; c@69: if (m_adapters.find(key) != m_adapters.end()) { c@69: return m_adapters.at(key)->loadPlugin(req); c@69: } else { c@69: throw runtime_error("no adapter for plugin key " + key); c@69: } c@69: } c@69: c@69: Vamp::HostExt::ConfigurationResponse c@69: VamPipePluginLibrary::configurePlugin(Vamp::HostExt::ConfigurationRequest req) const c@69: { c@69: for (Vamp::HostExt::PluginConfiguration::ParameterMap::const_iterator i = c@69: req.configuration.parameterValues.begin(); c@69: i != req.configuration.parameterValues.end(); ++i) { c@69: req.plugin->setParameter(i->first, i->second); c@69: } c@69: c@69: if (req.configuration.currentProgram != "") { c@69: req.plugin->selectProgram(req.configuration.currentProgram); c@69: } c@69: c@69: Vamp::HostExt::ConfigurationResponse response; c@69: c@69: if (req.plugin->initialise(req.configuration.channelCount, c@69: req.configuration.stepSize, c@69: req.configuration.blockSize)) { c@69: response.outputs = req.plugin->getOutputDescriptors(); c@69: } c@69: c@69: return response; c@69: } c@69: c@69: string c@83: VamPipePluginLibrary::processRawImpl(int pluginHandle, c@83: const float *const *inputBuffers, c@83: int sec, c@83: int nsec) c@82: { c@82: RequestOrResponse response; c@82: response.direction = RequestOrResponse::Response; c@82: response.type = RRType::Process; c@82: c@82: try { c@82: if (!m_mapper.isConfigured(pluginHandle)) { c@82: throw runtime_error("plugin has not been configured"); c@82: } c@82: c@82: Vamp::Plugin *plugin = m_mapper.handleToPlugin(pluginHandle); c@82: Vamp::RealTime timestamp(sec, nsec); c@82: c@82: response.processResponse.features = plugin->process(inputBuffers, timestamp); c@82: response.success = true; c@83: c@83: m_useBase64 = true; c@82: c@82: return writeResponse(response); c@82: c@82: } catch (const std::exception &e) { c@82: return VampJson::fromException(e, RRType::Process).dump(); c@82: } c@82: } c@82: c@82: string c@69: VamPipePluginLibrary::requestJsonImpl(string req) c@69: { c@70: RequestOrResponse request; c@70: c@70: try { c@70: request = readRequest(req); c@70: } catch (const std::exception &e) { c@70: return VampJson::fromException(e, RRType::NotValid).dump(); c@70: } c@69: c@69: RequestOrResponse response; c@69: response.direction = RequestOrResponse::Response; c@69: response.type = request.type; c@69: c@70: try { c@70: switch (request.type) { c@69: c@70: case RRType::List: c@70: response.listResponse = listPluginData(); c@70: response.success = true; c@70: break; c@69: c@70: case RRType::Load: c@70: response.loadResponse = loadPlugin(request.loadRequest); c@70: if (response.loadResponse.plugin) { c@70: m_mapper.addPlugin(response.loadResponse.plugin); c@70: response.success = true; c@70: } c@70: break; c@69: c@70: case RRType::Configure: c@70: { c@70: auto &creq = request.configurationRequest; c@70: auto h = m_mapper.pluginToHandle(creq.plugin); c@70: if (m_mapper.isConfigured(h)) { c@70: throw runtime_error("plugin has already been configured"); c@70: } c@70: c@70: response.configurationResponse = configurePlugin(creq); c@70: c@70: if (!response.configurationResponse.outputs.empty()) { c@70: m_mapper.markConfigured c@70: (h, creq.configuration.channelCount, creq.configuration.blockSize); c@70: response.success = true; c@70: } c@70: break; c@69: } c@69: c@70: case RRType::Process: c@70: { c@70: auto &preq = request.processRequest; c@70: auto h = m_mapper.pluginToHandle(preq.plugin); c@70: if (!m_mapper.isConfigured(h)) { c@70: throw runtime_error("plugin has not been configured"); c@70: } c@70: c@70: int channels = int(preq.inputBuffers.size()); c@70: if (channels != m_mapper.getChannelCount(h)) { c@70: throw runtime_error("wrong number of channels supplied to process"); c@70: } c@82: c@70: const float **fbuffers = new const float *[channels]; c@70: for (int i = 0; i < channels; ++i) { c@70: if (int(preq.inputBuffers[i].size()) != m_mapper.getBlockSize(h)) { c@70: delete[] fbuffers; c@70: throw runtime_error("wrong block size supplied to process"); c@70: } c@70: fbuffers[i] = preq.inputBuffers[i].data(); c@70: } c@70: c@70: response.processResponse.features = c@81: preq.plugin->process(fbuffers, preq.timestamp); c@69: response.success = true; c@69: c@70: delete[] fbuffers; c@70: break; c@69: } c@69: c@70: case RRType::Finish: c@70: { c@70: auto h = m_mapper.pluginToHandle(request.finishPlugin); c@70: c@70: response.finishResponse.features = c@70: request.finishPlugin->getRemainingFeatures(); c@70: c@70: m_mapper.removePlugin(h); c@70: delete request.finishPlugin; c@70: response.success = true; c@70: break; c@69: } c@69: c@70: case RRType::NotValid: c@70: break; c@70: } c@70: c@70: return writeResponse(response); c@69: c@70: } catch (const std::exception &e) { c@70: return VampJson::fromException(e, request.type).dump(); c@69: } c@69: } c@69: c@69: } c@69: