annotate vamp-support/LoaderRequests.h @ 215:8dff3213f77c

Minor renaming updates
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 10 Feb 2017 14:04:22 +0000
parents 52322dde68ea
children e0e3d9efa774
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"
c@97 42
c@97 43 #include <vamp-hostsdk/PluginLoader.h>
c@97 44
c@97 45 #include <map>
c@97 46 #include <string>
c@134 47 #include <iostream>
c@97 48
c@97 49 namespace piper_vamp {
c@97 50
c@97 51 class LoaderRequests
c@97 52 {
c@97 53 public:
c@97 54 ListResponse
c@127 55 listPluginData(ListRequest req) {
c@97 56
c@97 57 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@134 58
cannam@150 59 // std::cerr << "listPluginData: about to ask loader to list plugins" << std::endl;
c@127 60
c@127 61 std::vector<std::string> keys;
c@127 62 if (req.from.empty()) {
c@127 63 keys = loader->listPlugins();
c@127 64 } else {
c@127 65 keys = loader->listPluginsIn(req.from);
c@127 66 }
c@127 67
cannam@150 68 // std::cerr << "listPluginData: loader listed " << keys.size() << " plugins" << std::endl;
c@134 69
c@97 70 ListResponse response;
c@97 71 for (std::string key: keys) {
cannam@150 72 // std::cerr << "listPluginData: loading plugin and querying static data: " << key << std::endl;
c@97 73 Vamp::Plugin *p = loader->loadPlugin(key, 44100, 0);
c@97 74 if (!p) continue;
c@97 75 auto category = loader->getPluginCategory(key);
c@97 76 response.available.push_back
c@97 77 (PluginStaticData::fromPlugin(key, category, p));
c@97 78 delete p;
c@97 79 }
c@97 80
c@97 81 return response;
c@97 82 }
c@97 83
c@97 84 LoadResponse
c@97 85 loadPlugin(LoadRequest req) {
c@97 86
c@97 87 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@97 88
c@97 89 Vamp::Plugin *plugin = loader->loadPlugin(req.pluginKey,
c@97 90 req.inputSampleRate,
c@97 91 req.adapterFlags);
c@97 92
c@97 93 LoadResponse response;
c@97 94 response.plugin = plugin;
c@97 95 if (!plugin) return response;
c@97 96
c@97 97 response.plugin = plugin;
c@97 98 response.staticData = PluginStaticData::fromPlugin
c@97 99 (req.pluginKey,
c@97 100 loader->getPluginCategory(req.pluginKey),
c@97 101 plugin);
c@97 102
c@97 103 int defaultChannels = 0;
c@97 104 if (plugin->getMinChannelCount() == plugin->getMaxChannelCount()) {
c@108 105 defaultChannels = int(plugin->getMinChannelCount());
c@97 106 }
c@97 107
c@97 108 response.defaultConfiguration = PluginConfiguration::fromPlugin
c@97 109 (plugin,
c@97 110 defaultChannels,
c@108 111 int(plugin->getPreferredStepSize()),
c@108 112 int(plugin->getPreferredBlockSize()));
c@97 113
c@97 114 return response;
c@97 115 }
c@97 116
c@97 117 ConfigurationResponse
c@97 118 configurePlugin(ConfigurationRequest req) {
c@97 119
c@97 120 for (PluginConfiguration::ParameterMap::const_iterator i =
c@97 121 req.configuration.parameterValues.begin();
c@97 122 i != req.configuration.parameterValues.end(); ++i) {
c@97 123 req.plugin->setParameter(i->first, i->second);
c@97 124 }
c@97 125
c@97 126 if (req.configuration.currentProgram != "") {
c@97 127 req.plugin->selectProgram(req.configuration.currentProgram);
c@97 128 }
c@97 129
c@97 130 ConfigurationResponse response;
c@97 131
c@97 132 response.plugin = req.plugin;
cannam@185 133
cannam@185 134 if (req.configuration.framing.stepSize == 0 ||
cannam@185 135 req.configuration.framing.blockSize == 0) {
cannam@185 136 return response;
cannam@185 137 }
cannam@185 138
cannam@186 139 Framing pluginPreferredFraming;
cannam@186 140 pluginPreferredFraming.stepSize = req.plugin->getPreferredStepSize();
cannam@186 141 pluginPreferredFraming.blockSize = req.plugin->getPreferredBlockSize();
cannam@186 142
c@97 143 if (req.plugin->initialise(req.configuration.channelCount,
cannam@185 144 req.configuration.framing.stepSize,
cannam@185 145 req.configuration.framing.blockSize)) {
cannam@185 146
c@97 147 response.outputs = req.plugin->getOutputDescriptors();
cannam@185 148
cannam@185 149 // If the Vamp plugin initialise() call succeeds, then by
cannam@185 150 // definition it is accepting the step and block size
cannam@185 151 // passed in
cannam@185 152 response.framing = req.configuration.framing;
cannam@185 153
cannam@185 154 } else {
cannam@186 155
cannam@186 156 // If initialise() fails, one reason could be that it
cannam@186 157 // didn't like the passed-in framing (step and block
cannam@186 158 // size).
cannam@186 159 //
cannam@186 160 // Vamp and Piper have quite different mechanisms for
cannam@186 161 // negotiating step and block size:
cannam@186 162 //
cannam@186 163 // - If a Vamp plugin doesn't like the step and block size
cannam@186 164 // passed to initialise(), it fails the initialise() call,
cannam@186 165 // returning false from it. The host is expected to have
cannam@186 166 // called getPreferredStepSize()/BlockSize() after it made
cannam@186 167 // any parameter changes that might have affected these
cannam@186 168 // preferences (but before calling initialise).
cannam@186 169 //
cannam@186 170 // - If a Piper server doesn't like the step and block
cannam@186 171 // size passed in a configure request, but if everything
cannam@186 172 // else about the configure request is OK, then it returns
cannam@186 173 // a successful configure response including its preferred
cannam@186 174 // step and block sizes in the response (which the host
cannam@186 175 // must then use). The important thing to note is that
cannam@186 176 // this is still a successful response, something we do
cannam@186 177 // not yet have here.
cannam@186 178 //
cannam@186 179 // We need to check whether the passed-in framing differs
cannam@186 180 // from the plugin's preferences; if so, then we form a
cannam@186 181 // working supposition that initialise() failed because of
cannam@186 182 // this. Vamp contains nothing to allow us to test this,
cannam@186 183 // except to try initialise() again with different
cannam@186 184 // values. So we try again with the values the plugin told
cannam@186 185 // us it would prefer and, if that succeeds, return them
cannam@186 186 // in a successful response in the Piper manner.
cannam@186 187 //
cannam@186 188 // Note that if the "other side" (i.e. the client) wants
cannam@186 189 // to interpret this as if it were dealing with a Vamp
cannam@186 190 // plugin, then it's going to need some equal-but-opposite
cannam@186 191 // acrobatics.
cannam@185 192
cannam@186 193 if (req.plugin->initialise(req.configuration.channelCount,
cannam@186 194 pluginPreferredFraming.stepSize,
cannam@186 195 pluginPreferredFraming.blockSize)) {
cannam@186 196
cannam@186 197 response.outputs = req.plugin->getOutputDescriptors();
cannam@186 198 response.framing = pluginPreferredFraming;
cannam@186 199
cannam@186 200 } // ... else we return no outputs, which is the error
cannam@186 201 // case (presumably to be converted to Piper error
cannam@186 202 // response).
cannam@185 203 }
c@97 204
c@97 205 return response;
c@97 206 }
c@97 207 };
c@97 208
c@97 209 }
c@97 210
c@97 211 #endif