annotate vamp-client/PiperVampPlugin.h @ 208:c67a0a945b6b

Rename PluginStub to PiperVampPlugin and AutoPlugin to PiperAutoPlugin
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 09 Feb 2017 13:31:46 +0000
parents
children df65480a08de
rev   line source
cannam@208 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@208 2 /*
cannam@208 3 Piper C++
cannam@208 4
cannam@208 5 An API for audio analysis and feature extraction plugins.
cannam@208 6
cannam@208 7 Centre for Digital Music, Queen Mary, University of London.
cannam@208 8 Copyright 2006-2017 Chris Cannam and QMUL.
cannam@208 9
cannam@208 10 Permission is hereby granted, free of charge, to any person
cannam@208 11 obtaining a copy of this software and associated documentation
cannam@208 12 files (the "Software"), to deal in the Software without
cannam@208 13 restriction, including without limitation the rights to use, copy,
cannam@208 14 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@208 15 of the Software, and to permit persons to whom the Software is
cannam@208 16 furnished to do so, subject to the following conditions:
cannam@208 17
cannam@208 18 The above copyright notice and this permission notice shall be
cannam@208 19 included in all copies or substantial portions of the Software.
cannam@208 20
cannam@208 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@208 22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@208 23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@208 24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@208 25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@208 26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@208 27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@208 28
cannam@208 29 Except as contained in this notice, the names of the Centre for
cannam@208 30 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@208 31 shall not be used in advertising or otherwise to promote the sale,
cannam@208 32 use or other dealings in this Software without prior written
cannam@208 33 authorization.
cannam@208 34 */
cannam@208 35
cannam@208 36 #ifndef PIPER_VAMP_PLUGIN_H
cannam@208 37 #define PIPER_VAMP_PLUGIN_H
cannam@208 38
cannam@208 39 #include <vamp-hostsdk/Plugin.h>
cannam@208 40 #include <vamp-hostsdk/PluginLoader.h>
cannam@208 41
cannam@208 42 #include "vamp-support/PluginStaticData.h"
cannam@208 43 #include "vamp-support/PluginConfiguration.h"
cannam@208 44
cannam@208 45 #include "PluginClient.h"
cannam@208 46
cannam@208 47 #include <cstdint>
cannam@208 48 #include <iostream>
cannam@208 49
cannam@208 50 namespace piper_vamp {
cannam@208 51 namespace client {
cannam@208 52
cannam@208 53 class PiperVampPlugin : public Vamp::Plugin
cannam@208 54 {
cannam@208 55 enum State {
cannam@208 56 /**
cannam@208 57 * The plugin's corresponding Piper feature extractor has been
cannam@208 58 * loaded but no subsequent state change has happened. This is
cannam@208 59 * the initial state of PiperVampPlugin on construction, since
cannam@208 60 * it is associated with a pre-loaded handle.
cannam@208 61 */
cannam@208 62 Loaded,
cannam@208 63
cannam@208 64 /**
cannam@208 65 * The plugin has been configured, and the step and block size
cannam@208 66 * received from the host in its last call to initialise()
cannam@208 67 * match those that were returned in the configuration
cannam@208 68 * response (i.e. the server's desired step and block
cannam@208 69 * size). Our m_config record reflects these correct
cannam@208 70 * values. The plugin is ready to process.
cannam@208 71 */
cannam@208 72 Configured,
cannam@208 73
cannam@208 74 /**
cannam@208 75 * The plugin has been configured, but the step and block size
cannam@208 76 * received from the host in its last call to initialise()
cannam@208 77 * differ from those returned by the server in the
cannam@208 78 * configuration response. Our initialise() call therefore
cannam@208 79 * returned false, and the plugin cannot be used until the
cannam@208 80 * host calls initialise() again with the "correct" step and
cannam@208 81 * block size. Our m_config record reflects these correct
cannam@208 82 * values, so the host can retrieve them through
cannam@208 83 * getPreferredStepSize and getPreferredBlockSize.
cannam@208 84 */
cannam@208 85 Misconfigured,
cannam@208 86
cannam@208 87 /**
cannam@208 88 * The finish() function has been called and the plugin
cannam@208 89 * unloaded. No further plugin activity is possible.
cannam@208 90 */
cannam@208 91 Finished,
cannam@208 92
cannam@208 93 /**
cannam@208 94 * A call has failed unrecoverably. No further plugin activity
cannam@208 95 * is possible.
cannam@208 96 */
cannam@208 97 Failed
cannam@208 98 };
cannam@208 99
cannam@208 100 public:
cannam@208 101 PiperVampPlugin(PluginClient *client,
cannam@208 102 std::string pluginKey,
cannam@208 103 float inputSampleRate,
cannam@208 104 int adapterFlags,
cannam@208 105 PluginStaticData psd,
cannam@208 106 PluginConfiguration defaultConfig) :
cannam@208 107 Plugin(inputSampleRate),
cannam@208 108 m_client(client),
cannam@208 109 m_key(pluginKey),
cannam@208 110 m_adapterFlags(adapterFlags),
cannam@208 111 m_state(Loaded),
cannam@208 112 m_psd(psd),
cannam@208 113 m_defaultConfig(defaultConfig),
cannam@208 114 m_config(defaultConfig)
cannam@208 115 { }
cannam@208 116
cannam@208 117 virtual ~PiperVampPlugin() {
cannam@208 118 if (m_state != Finished && m_state != Failed) {
cannam@208 119 try {
cannam@208 120 (void)m_client->finish(this);
cannam@208 121 } catch (const std::exception &e) {
cannam@208 122 // Finish can throw, but our destructor must not
cannam@208 123 std::cerr << "WARNING: PiperVampPlugin::~PiperVampPlugin: caught exception from finish(): " << e.what() << std::endl;
cannam@208 124 }
cannam@208 125 }
cannam@208 126 }
cannam@208 127
cannam@208 128 virtual std::string getIdentifier() const {
cannam@208 129 return m_psd.basic.identifier;
cannam@208 130 }
cannam@208 131
cannam@208 132 virtual std::string getName() const {
cannam@208 133 return m_psd.basic.name;
cannam@208 134 }
cannam@208 135
cannam@208 136 virtual std::string getDescription() const {
cannam@208 137 return m_psd.basic.description;
cannam@208 138 }
cannam@208 139
cannam@208 140 virtual std::string getMaker() const {
cannam@208 141 return m_psd.maker;
cannam@208 142 }
cannam@208 143
cannam@208 144 virtual std::string getCopyright() const {
cannam@208 145 return m_psd.copyright;
cannam@208 146 }
cannam@208 147
cannam@208 148 virtual int getPluginVersion() const {
cannam@208 149 return m_psd.pluginVersion;
cannam@208 150 }
cannam@208 151
cannam@208 152 virtual ParameterList getParameterDescriptors() const {
cannam@208 153 return m_psd.parameters;
cannam@208 154 }
cannam@208 155
cannam@208 156 virtual float getParameter(std::string name) const {
cannam@208 157 if (m_config.parameterValues.find(name) != m_config.parameterValues.end()) {
cannam@208 158 return m_config.parameterValues.at(name);
cannam@208 159 } else {
cannam@208 160 return 0.f;
cannam@208 161 }
cannam@208 162 }
cannam@208 163
cannam@208 164 virtual void setParameter(std::string name, float value) {
cannam@208 165 if (m_state == Failed) {
cannam@208 166 throw std::logic_error("Plugin is in failed state");
cannam@208 167 }
cannam@208 168 if (m_state != Loaded) {
cannam@208 169 m_state = Failed;
cannam@208 170 throw std::logic_error("Can't set parameter after plugin initialised");
cannam@208 171 }
cannam@208 172 m_config.parameterValues[name] = value;
cannam@208 173 }
cannam@208 174
cannam@208 175 virtual ProgramList getPrograms() const {
cannam@208 176 return m_psd.programs;
cannam@208 177 }
cannam@208 178
cannam@208 179 virtual std::string getCurrentProgram() const {
cannam@208 180 return m_config.currentProgram;
cannam@208 181 }
cannam@208 182
cannam@208 183 virtual void selectProgram(std::string program) {
cannam@208 184 if (m_state == Failed) {
cannam@208 185 throw std::logic_error("Plugin is in failed state");
cannam@208 186 }
cannam@208 187 if (m_state != Loaded) {
cannam@208 188 m_state = Failed;
cannam@208 189 throw std::logic_error("Can't select program after plugin initialised");
cannam@208 190 }
cannam@208 191 m_config.currentProgram = program;
cannam@208 192 }
cannam@208 193
cannam@208 194 virtual bool initialise(size_t inputChannels,
cannam@208 195 size_t stepSize,
cannam@208 196 size_t blockSize) {
cannam@208 197
cannam@208 198 if (m_state == Failed) {
cannam@208 199 throw std::logic_error("Plugin is in failed state");
cannam@208 200 }
cannam@208 201
cannam@208 202 if (m_state == Misconfigured) {
cannam@208 203 if (int(stepSize) == m_config.framing.stepSize &&
cannam@208 204 int(blockSize) == m_config.framing.blockSize) {
cannam@208 205 m_state = Configured;
cannam@208 206 return true;
cannam@208 207 } else {
cannam@208 208 return false;
cannam@208 209 }
cannam@208 210 }
cannam@208 211
cannam@208 212 if (m_state != Loaded) {
cannam@208 213 m_state = Failed;
cannam@208 214 throw std::logic_error("Plugin has already been initialised");
cannam@208 215 }
cannam@208 216
cannam@208 217 m_config.channelCount = int(inputChannels);
cannam@208 218 m_config.framing.stepSize = int(stepSize);
cannam@208 219 m_config.framing.blockSize = int(blockSize);
cannam@208 220
cannam@208 221 try {
cannam@208 222 auto response = m_client->configure(this, m_config);
cannam@208 223 m_outputs = response.outputs;
cannam@208 224
cannam@208 225 // Update with the new preferred step and block size now
cannam@208 226 // that the plugin has taken into account its parameter
cannam@208 227 // settings. If the values passed in to initialise()
cannam@208 228 // weren't suitable, then this ensures that a subsequent
cannam@208 229 // call to getPreferredStepSize/BlockSize on this plugin
cannam@208 230 // object will at least get acceptable values from now on
cannam@208 231 m_config.framing = response.framing;
cannam@208 232
cannam@208 233 // And if they didn't match up with the passed-in ones,
cannam@208 234 // lodge ourselves in Misconfigured state and report
cannam@208 235 // failure so as to provoke the host to call initialise()
cannam@208 236 // again before any processing.
cannam@208 237 if (m_config.framing.stepSize != int(stepSize) ||
cannam@208 238 m_config.framing.blockSize != int(blockSize)) {
cannam@208 239 m_state = Misconfigured;
cannam@208 240 return false;
cannam@208 241 }
cannam@208 242
cannam@208 243 } catch (const std::exception &e) {
cannam@208 244 m_state = Failed;
cannam@208 245 throw;
cannam@208 246 }
cannam@208 247
cannam@208 248 if (!m_outputs.empty()) {
cannam@208 249 m_state = Configured;
cannam@208 250 return true;
cannam@208 251 } else {
cannam@208 252 return false;
cannam@208 253 }
cannam@208 254 }
cannam@208 255
cannam@208 256 virtual void reset() {
cannam@208 257
cannam@208 258 if (m_state == Failed) {
cannam@208 259 throw std::logic_error("Plugin is in failed state");
cannam@208 260 }
cannam@208 261 if (m_state == Loaded || m_state == Misconfigured) {
cannam@208 262 // reset is a no-op if the plugin hasn't been initialised yet
cannam@208 263 return;
cannam@208 264 }
cannam@208 265
cannam@208 266 try {
cannam@208 267 m_client->reset(this, m_config);
cannam@208 268 } catch (const std::exception &e) {
cannam@208 269 m_state = Failed;
cannam@208 270 throw;
cannam@208 271 }
cannam@208 272
cannam@208 273 m_state = Configured;
cannam@208 274 }
cannam@208 275
cannam@208 276 virtual InputDomain getInputDomain() const {
cannam@208 277 return m_psd.inputDomain;
cannam@208 278 }
cannam@208 279
cannam@208 280 virtual size_t getPreferredBlockSize() const {
cannam@208 281 // Return this from m_config instead of m_defaultConfig, so
cannam@208 282 // that it gets updated in the event of an initialise() call
cannam@208 283 // that fails for the wrong value
cannam@208 284 return m_config.framing.blockSize;
cannam@208 285 }
cannam@208 286
cannam@208 287 virtual size_t getPreferredStepSize() const {
cannam@208 288 // Return this from m_config instead of m_defaultConfig, so
cannam@208 289 // that it gets updated in the event of an initialise() call
cannam@208 290 // that fails for the wrong value
cannam@208 291 return m_config.framing.stepSize;
cannam@208 292 }
cannam@208 293
cannam@208 294 virtual size_t getMinChannelCount() const {
cannam@208 295 return m_psd.minChannelCount;
cannam@208 296 }
cannam@208 297
cannam@208 298 virtual size_t getMaxChannelCount() const {
cannam@208 299 return m_psd.maxChannelCount;
cannam@208 300 }
cannam@208 301
cannam@208 302 virtual OutputList getOutputDescriptors() const {
cannam@208 303
cannam@208 304 if (m_state == Failed) {
cannam@208 305 throw std::logic_error("Plugin is in failed state");
cannam@208 306 }
cannam@208 307 if (m_state == Configured) {
cannam@208 308 return m_outputs;
cannam@208 309 }
cannam@208 310
cannam@208 311 //!!! todo: figure out for which hosts (and adapters?) it may
cannam@208 312 //!!! be a problem that the output descriptors are incomplete
cannam@208 313 //!!! here. Any such hosts/adapters are broken, but I bet they
cannam@208 314 //!!! exist
cannam@208 315
cannam@208 316 OutputList staticOutputs;
cannam@208 317 for (const auto &o: m_psd.basicOutputInfo) {
cannam@208 318 OutputDescriptor od;
cannam@208 319 od.identifier = o.identifier;
cannam@208 320 od.name = o.name;
cannam@208 321 od.description = o.description;
cannam@208 322 staticOutputs.push_back(od);
cannam@208 323 }
cannam@208 324 return staticOutputs;
cannam@208 325 }
cannam@208 326
cannam@208 327 virtual FeatureSet process(const float *const *inputBuffers,
cannam@208 328 Vamp::RealTime timestamp) {
cannam@208 329
cannam@208 330 if (m_state == Failed) {
cannam@208 331 throw std::logic_error("Plugin is in failed state");
cannam@208 332 }
cannam@208 333 if (m_state == Loaded || m_state == Misconfigured) {
cannam@208 334 m_state = Failed;
cannam@208 335 throw std::logic_error("Plugin has not been initialised");
cannam@208 336 }
cannam@208 337 if (m_state == Finished) {
cannam@208 338 m_state = Failed;
cannam@208 339 throw std::logic_error("Plugin has already been disposed of");
cannam@208 340 }
cannam@208 341
cannam@208 342 std::vector<std::vector<float> > vecbuf;
cannam@208 343 for (int c = 0; c < m_config.channelCount; ++c) {
cannam@208 344 vecbuf.push_back(std::vector<float>
cannam@208 345 (inputBuffers[c],
cannam@208 346 inputBuffers[c] + m_config.framing.blockSize));
cannam@208 347 }
cannam@208 348
cannam@208 349 try {
cannam@208 350 return m_client->process(this, vecbuf, timestamp);
cannam@208 351 } catch (const std::exception &e) {
cannam@208 352 m_state = Failed;
cannam@208 353 throw;
cannam@208 354 }
cannam@208 355 }
cannam@208 356
cannam@208 357 virtual FeatureSet getRemainingFeatures() {
cannam@208 358
cannam@208 359 if (m_state == Failed) {
cannam@208 360 throw std::logic_error("Plugin is in failed state");
cannam@208 361 }
cannam@208 362 if (m_state == Loaded || m_state == Misconfigured) {
cannam@208 363 m_state = Failed;
cannam@208 364 throw std::logic_error("Plugin has not been configured");
cannam@208 365 }
cannam@208 366 if (m_state == Finished) {
cannam@208 367 m_state = Failed;
cannam@208 368 throw std::logic_error("Plugin has already been disposed of");
cannam@208 369 }
cannam@208 370
cannam@208 371 m_state = Finished;
cannam@208 372
cannam@208 373 try {
cannam@208 374 return m_client->finish(this);
cannam@208 375 } catch (const std::exception &e) {
cannam@208 376 m_state = Failed;
cannam@208 377 throw;
cannam@208 378 }
cannam@208 379 }
cannam@208 380
cannam@208 381 // Not Plugin methods, but needed by the PluginClient to support reloads:
cannam@208 382
cannam@208 383 virtual float getInputSampleRate() const {
cannam@208 384 return m_inputSampleRate;
cannam@208 385 }
cannam@208 386
cannam@208 387 virtual std::string getPluginKey() const {
cannam@208 388 return m_key;
cannam@208 389 }
cannam@208 390
cannam@208 391 virtual int getAdapterFlags() const {
cannam@208 392 return m_adapterFlags;
cannam@208 393 }
cannam@208 394
cannam@208 395 private:
cannam@208 396 PluginClient *m_client;
cannam@208 397 std::string m_key;
cannam@208 398 int m_adapterFlags;
cannam@208 399 State m_state;
cannam@208 400 PluginStaticData m_psd;
cannam@208 401 OutputList m_outputs;
cannam@208 402 PluginConfiguration m_defaultConfig;
cannam@208 403 PluginConfiguration m_config;
cannam@208 404 };
cannam@208 405
cannam@208 406 }
cannam@208 407 }
cannam@208 408
cannam@208 409 #endif