annotate vamp-support/LoaderRequests.h @ 293:ae7397deffaa

Improve error handling (i.e. add some) for failure to initialise
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 05 Jan 2022 14:02:19 +0000
parents 09753ad777db
children
rev   line source
c@97 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@97 2
c@97 3 /*
c@97 4 Piper C++
c@97 5
c@97 6 An API for audio analysis and feature extraction plugins.
c@97 7
c@97 8 Centre for Digital Music, Queen Mary, University of London.
c@97 9 Copyright 2006-2016 Chris Cannam and QMUL.
c@97 10
c@97 11 Permission is hereby granted, free of charge, to any person
c@97 12 obtaining a copy of this software and associated documentation
c@97 13 files (the "Software"), to deal in the Software without
c@97 14 restriction, including without limitation the rights to use, copy,
c@97 15 modify, merge, publish, distribute, sublicense, and/or sell copies
c@97 16 of the Software, and to permit persons to whom the Software is
c@97 17 furnished to do so, subject to the following conditions:
c@97 18
c@97 19 The above copyright notice and this permission notice shall be
c@97 20 included in all copies or substantial portions of the Software.
c@97 21
c@97 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@97 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@97 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@97 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
c@97 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@97 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@97 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@97 29
c@97 30 Except as contained in this notice, the names of the Centre for
c@97 31 Digital Music; Queen Mary, University of London; and Chris Cannam
c@97 32 shall not be used in advertising or otherwise to promote the sale,
c@97 33 use or other dealings in this Software without prior written
c@97 34 authorization.
c@97 35 */
c@97 36
c@97 37 #ifndef PIPER_LOADER_REQUESTS_H
c@97 38 #define PIPER_LOADER_REQUESTS_H
c@97 39
c@97 40 #include "PluginStaticData.h"
c@97 41 #include "PluginConfiguration.h"
cannam@287 42 #include "PluginProgramParameters.h"
cannam@244 43 #include "StaticOutputRdf.h"
cannam@287 44 #include "RequestResponse.h"
c@97 45
c@97 46 #include <vamp-hostsdk/PluginLoader.h>
c@97 47
c@97 48 #include <map>
c@97 49 #include <string>
c@134 50 #include <iostream>
c@97 51
c@97 52 namespace piper_vamp {
c@97 53
c@97 54 class LoaderRequests
c@97 55 {
c@97 56 public:
c@97 57 ListResponse
c@127 58 listPluginData(ListRequest req) {
c@97 59
c@97 60 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@134 61
cannam@150 62 // std::cerr << "listPluginData: about to ask loader to list plugins" << std::endl;
c@127 63
c@127 64 std::vector<std::string> keys;
c@127 65 if (req.from.empty()) {
c@127 66 keys = loader->listPlugins();
c@127 67 } else {
c@127 68 keys = loader->listPluginsIn(req.from);
c@127 69 }
c@127 70
cannam@150 71 // std::cerr << "listPluginData: loader listed " << keys.size() << " plugins" << std::endl;
c@134 72
c@97 73 ListResponse response;
c@97 74 for (std::string key: keys) {
cannam@150 75 // std::cerr << "listPluginData: loading plugin and querying static data: " << key << std::endl;
c@97 76 Vamp::Plugin *p = loader->loadPlugin(key, 44100, 0);
c@97 77 if (!p) continue;
c@97 78 auto category = loader->getPluginCategory(key);
cannam@222 79 PluginStaticData psd =
cannam@222 80 PluginStaticData::fromPlugin(key, category, p);
cannam@244 81 psd.staticOutputInfo = StaticOutputRdf().loadStaticOutputInfo(key);
cannam@222 82 response.available.push_back(psd);
c@97 83 delete p;
c@97 84 }
c@97 85
c@97 86 return response;
c@97 87 }
c@97 88
c@97 89 LoadResponse
c@97 90 loadPlugin(LoadRequest req) {
c@97 91
c@97 92 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@97 93
c@97 94 Vamp::Plugin *plugin = loader->loadPlugin(req.pluginKey,
c@97 95 req.inputSampleRate,
c@97 96 req.adapterFlags);
c@97 97
c@97 98 LoadResponse response;
c@97 99 response.plugin = plugin;
c@97 100 if (!plugin) return response;
c@97 101
c@97 102 response.plugin = plugin;
c@97 103 response.staticData = PluginStaticData::fromPlugin
c@97 104 (req.pluginKey,
c@97 105 loader->getPluginCategory(req.pluginKey),
c@97 106 plugin);
cannam@222 107
cannam@222 108 response.staticData.staticOutputInfo =
cannam@244 109 StaticOutputRdf().loadStaticOutputInfo(req.pluginKey);
c@97 110
c@97 111 int defaultChannels = 0;
c@97 112 if (plugin->getMinChannelCount() == plugin->getMaxChannelCount()) {
c@108 113 defaultChannels = int(plugin->getMinChannelCount());
c@97 114 }
c@97 115
c@97 116 response.defaultConfiguration = PluginConfiguration::fromPlugin
c@97 117 (plugin,
c@97 118 defaultChannels,
c@108 119 int(plugin->getPreferredStepSize()),
c@108 120 int(plugin->getPreferredBlockSize()));
cannam@287 121
cannam@287 122 response.programParameters = PluginProgramParameters::fromPlugin
cannam@287 123 (plugin, response.defaultConfiguration);
cannam@287 124
c@97 125 return response;
c@97 126 }
c@97 127
c@97 128 ConfigurationResponse
c@97 129 configurePlugin(ConfigurationRequest req) {
c@97 130
c@97 131 for (PluginConfiguration::ParameterMap::const_iterator i =
c@97 132 req.configuration.parameterValues.begin();
c@97 133 i != req.configuration.parameterValues.end(); ++i) {
c@97 134 req.plugin->setParameter(i->first, i->second);
c@97 135 }
c@97 136
c@97 137 if (req.configuration.currentProgram != "") {
c@97 138 req.plugin->selectProgram(req.configuration.currentProgram);
c@97 139 }
c@97 140
c@97 141 ConfigurationResponse response;
c@97 142
c@97 143 response.plugin = req.plugin;
cannam@185 144
cannam@185 145 if (req.configuration.framing.stepSize == 0 ||
cannam@185 146 req.configuration.framing.blockSize == 0) {
cannam@185 147 return response;
cannam@185 148 }
cannam@185 149
cannam@186 150 Framing pluginPreferredFraming;
cannam@186 151 pluginPreferredFraming.stepSize = req.plugin->getPreferredStepSize();
cannam@186 152 pluginPreferredFraming.blockSize = req.plugin->getPreferredBlockSize();
cannam@186 153
c@97 154 if (req.plugin->initialise(req.configuration.channelCount,
cannam@185 155 req.configuration.framing.stepSize,
cannam@185 156 req.configuration.framing.blockSize)) {
cannam@185 157
c@97 158 response.outputs = req.plugin->getOutputDescriptors();
cannam@185 159
cannam@185 160 // If the Vamp plugin initialise() call succeeds, then by
cannam@185 161 // definition it is accepting the step and block size
cannam@185 162 // passed in
cannam@185 163 response.framing = req.configuration.framing;
cannam@185 164
cannam@185 165 } else {
cannam@186 166
cannam@186 167 // If initialise() fails, one reason could be that it
cannam@186 168 // didn't like the passed-in framing (step and block
cannam@186 169 // size).
cannam@186 170 //
cannam@186 171 // Vamp and Piper have quite different mechanisms for
cannam@186 172 // negotiating step and block size:
cannam@186 173 //
cannam@186 174 // - If a Vamp plugin doesn't like the step and block size
cannam@186 175 // passed to initialise(), it fails the initialise() call,
cannam@186 176 // returning false from it. The host is expected to have
cannam@186 177 // called getPreferredStepSize()/BlockSize() after it made
cannam@186 178 // any parameter changes that might have affected these
cannam@186 179 // preferences (but before calling initialise).
cannam@186 180 //
cannam@186 181 // - If a Piper server doesn't like the step and block
cannam@186 182 // size passed in a configure request, but if everything
cannam@186 183 // else about the configure request is OK, then it returns
cannam@186 184 // a successful configure response including its preferred
cannam@186 185 // step and block sizes in the response (which the host
cannam@186 186 // must then use). The important thing to note is that
cannam@186 187 // this is still a successful response, something we do
cannam@186 188 // not yet have here.
cannam@186 189 //
cannam@186 190 // We need to check whether the passed-in framing differs
cannam@186 191 // from the plugin's preferences; if so, then we form a
cannam@186 192 // working supposition that initialise() failed because of
cannam@186 193 // this. Vamp contains nothing to allow us to test this,
cannam@186 194 // except to try initialise() again with different
cannam@186 195 // values. So we try again with the values the plugin told
cannam@186 196 // us it would prefer and, if that succeeds, return them
cannam@186 197 // in a successful response in the Piper manner.
cannam@186 198 //
cannam@186 199 // Note that if the "other side" (i.e. the client) wants
cannam@186 200 // to interpret this as if it were dealing with a Vamp
cannam@186 201 // plugin, then it's going to need some equal-but-opposite
cannam@186 202 // acrobatics.
cannam@185 203
cannam@186 204 if (req.plugin->initialise(req.configuration.channelCount,
cannam@186 205 pluginPreferredFraming.stepSize,
cannam@186 206 pluginPreferredFraming.blockSize)) {
cannam@186 207
cannam@186 208 response.outputs = req.plugin->getOutputDescriptors();
cannam@186 209 response.framing = pluginPreferredFraming;
cannam@186 210
cannam@186 211 } // ... else we return no outputs, which is the error
cannam@186 212 // case (presumably to be converted to Piper error
cannam@186 213 // response).
cannam@185 214 }
c@97 215
c@97 216 return response;
c@97 217 }
c@97 218 };
c@97 219
c@97 220 }
c@97 221
c@97 222 #endif