c@97: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@97: c@97: /* c@97: Piper C++ c@97: c@97: An API for audio analysis and feature extraction plugins. c@97: c@97: Centre for Digital Music, Queen Mary, University of London. c@97: Copyright 2006-2016 Chris Cannam and QMUL. c@97: c@97: Permission is hereby granted, free of charge, to any person c@97: obtaining a copy of this software and associated documentation c@97: files (the "Software"), to deal in the Software without c@97: restriction, including without limitation the rights to use, copy, c@97: modify, merge, publish, distribute, sublicense, and/or sell copies c@97: of the Software, and to permit persons to whom the Software is c@97: furnished to do so, subject to the following conditions: c@97: c@97: The above copyright notice and this permission notice shall be c@97: included in all copies or substantial portions of the Software. c@97: c@97: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@97: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@97: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@97: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@97: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@97: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@97: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@97: c@97: Except as contained in this notice, the names of the Centre for c@97: Digital Music; Queen Mary, University of London; and Chris Cannam c@97: shall not be used in advertising or otherwise to promote the sale, c@97: use or other dealings in this Software without prior written c@97: authorization. c@97: */ c@97: c@97: #ifndef PIPER_LOADER_REQUESTS_H c@97: #define PIPER_LOADER_REQUESTS_H c@97: c@97: #include "PluginStaticData.h" c@97: #include "PluginConfiguration.h" cannam@222: #include "RdfTypes.h" c@97: c@97: #include c@97: c@97: #include c@97: #include c@134: #include c@97: c@97: namespace piper_vamp { c@97: c@97: class LoaderRequests c@97: { c@97: public: c@97: ListResponse c@127: listPluginData(ListRequest req) { c@97: c@97: auto loader = Vamp::HostExt::PluginLoader::getInstance(); c@134: cannam@150: // std::cerr << "listPluginData: about to ask loader to list plugins" << std::endl; c@127: c@127: std::vector keys; c@127: if (req.from.empty()) { c@127: keys = loader->listPlugins(); c@127: } else { c@127: keys = loader->listPluginsIn(req.from); c@127: } c@127: cannam@150: // std::cerr << "listPluginData: loader listed " << keys.size() << " plugins" << std::endl; c@134: c@97: ListResponse response; c@97: for (std::string key: keys) { cannam@150: // std::cerr << "listPluginData: loading plugin and querying static data: " << key << std::endl; c@97: Vamp::Plugin *p = loader->loadPlugin(key, 44100, 0); c@97: if (!p) continue; c@97: auto category = loader->getPluginCategory(key); cannam@222: PluginStaticData psd = cannam@222: PluginStaticData::fromPlugin(key, category, p); cannam@222: psd.staticOutputInfo = RdfTypes().loadStaticOutputInfo(key); cannam@222: response.available.push_back(psd); c@97: delete p; c@97: } c@97: c@97: return response; c@97: } c@97: c@97: LoadResponse c@97: loadPlugin(LoadRequest req) { c@97: c@97: auto loader = Vamp::HostExt::PluginLoader::getInstance(); c@97: c@97: Vamp::Plugin *plugin = loader->loadPlugin(req.pluginKey, c@97: req.inputSampleRate, c@97: req.adapterFlags); c@97: c@97: LoadResponse response; c@97: response.plugin = plugin; c@97: if (!plugin) return response; c@97: c@97: response.plugin = plugin; c@97: response.staticData = PluginStaticData::fromPlugin c@97: (req.pluginKey, c@97: loader->getPluginCategory(req.pluginKey), c@97: plugin); cannam@222: cannam@222: response.staticData.staticOutputInfo = cannam@222: RdfTypes().loadStaticOutputInfo(req.pluginKey); c@97: c@97: int defaultChannels = 0; c@97: if (plugin->getMinChannelCount() == plugin->getMaxChannelCount()) { c@108: defaultChannels = int(plugin->getMinChannelCount()); c@97: } c@97: c@97: response.defaultConfiguration = PluginConfiguration::fromPlugin c@97: (plugin, c@97: defaultChannels, c@108: int(plugin->getPreferredStepSize()), c@108: int(plugin->getPreferredBlockSize())); c@97: c@97: return response; c@97: } c@97: c@97: ConfigurationResponse c@97: configurePlugin(ConfigurationRequest req) { c@97: c@97: for (PluginConfiguration::ParameterMap::const_iterator i = c@97: req.configuration.parameterValues.begin(); c@97: i != req.configuration.parameterValues.end(); ++i) { c@97: req.plugin->setParameter(i->first, i->second); c@97: } c@97: c@97: if (req.configuration.currentProgram != "") { c@97: req.plugin->selectProgram(req.configuration.currentProgram); c@97: } c@97: c@97: ConfigurationResponse response; c@97: c@97: response.plugin = req.plugin; cannam@185: cannam@185: if (req.configuration.framing.stepSize == 0 || cannam@185: req.configuration.framing.blockSize == 0) { cannam@185: return response; cannam@185: } cannam@185: cannam@186: Framing pluginPreferredFraming; cannam@186: pluginPreferredFraming.stepSize = req.plugin->getPreferredStepSize(); cannam@186: pluginPreferredFraming.blockSize = req.plugin->getPreferredBlockSize(); cannam@186: c@97: if (req.plugin->initialise(req.configuration.channelCount, cannam@185: req.configuration.framing.stepSize, cannam@185: req.configuration.framing.blockSize)) { cannam@185: c@97: response.outputs = req.plugin->getOutputDescriptors(); cannam@185: cannam@185: // If the Vamp plugin initialise() call succeeds, then by cannam@185: // definition it is accepting the step and block size cannam@185: // passed in cannam@185: response.framing = req.configuration.framing; cannam@185: cannam@185: } else { cannam@186: cannam@186: // If initialise() fails, one reason could be that it cannam@186: // didn't like the passed-in framing (step and block cannam@186: // size). cannam@186: // cannam@186: // Vamp and Piper have quite different mechanisms for cannam@186: // negotiating step and block size: cannam@186: // cannam@186: // - If a Vamp plugin doesn't like the step and block size cannam@186: // passed to initialise(), it fails the initialise() call, cannam@186: // returning false from it. The host is expected to have cannam@186: // called getPreferredStepSize()/BlockSize() after it made cannam@186: // any parameter changes that might have affected these cannam@186: // preferences (but before calling initialise). cannam@186: // cannam@186: // - If a Piper server doesn't like the step and block cannam@186: // size passed in a configure request, but if everything cannam@186: // else about the configure request is OK, then it returns cannam@186: // a successful configure response including its preferred cannam@186: // step and block sizes in the response (which the host cannam@186: // must then use). The important thing to note is that cannam@186: // this is still a successful response, something we do cannam@186: // not yet have here. cannam@186: // cannam@186: // We need to check whether the passed-in framing differs cannam@186: // from the plugin's preferences; if so, then we form a cannam@186: // working supposition that initialise() failed because of cannam@186: // this. Vamp contains nothing to allow us to test this, cannam@186: // except to try initialise() again with different cannam@186: // values. So we try again with the values the plugin told cannam@186: // us it would prefer and, if that succeeds, return them cannam@186: // in a successful response in the Piper manner. cannam@186: // cannam@186: // Note that if the "other side" (i.e. the client) wants cannam@186: // to interpret this as if it were dealing with a Vamp cannam@186: // plugin, then it's going to need some equal-but-opposite cannam@186: // acrobatics. cannam@185: cannam@186: if (req.plugin->initialise(req.configuration.channelCount, cannam@186: pluginPreferredFraming.stepSize, cannam@186: pluginPreferredFraming.blockSize)) { cannam@186: cannam@186: response.outputs = req.plugin->getOutputDescriptors(); cannam@186: response.framing = pluginPreferredFraming; cannam@186: cannam@186: } // ... else we return no outputs, which is the error cannam@186: // case (presumably to be converted to Piper error cannam@186: // response). cannam@185: } c@97: c@97: return response; c@97: } c@97: }; c@97: c@97: } c@97: c@97: #endif