annotate vamp-client/qt/AutoPlugin.h @ 173:492970caa62b

Merge branch 'master' of https://github.com/piper-audio/piper-cpp
author Lucas Thompson <dev@lucas.im>
date Tue, 31 Jan 2017 22:21:51 +0000
parents 590b1a1fd955
children 52322dde68ea
rev   line source
cannam@150 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@150 2 /*
cannam@150 3 Piper C++
cannam@150 4
cannam@150 5 An API for audio analysis and feature extraction plugins.
cannam@150 6
cannam@150 7 Centre for Digital Music, Queen Mary, University of London.
cannam@150 8 Copyright 2006-2016 Chris Cannam and QMUL.
cannam@150 9
cannam@150 10 Permission is hereby granted, free of charge, to any person
cannam@150 11 obtaining a copy of this software and associated documentation
cannam@150 12 files (the "Software"), to deal in the Software without
cannam@150 13 restriction, including without limitation the rights to use, copy,
cannam@150 14 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@150 15 of the Software, and to permit persons to whom the Software is
cannam@150 16 furnished to do so, subject to the following conditions:
cannam@150 17
cannam@150 18 The above copyright notice and this permission notice shall be
cannam@150 19 included in all copies or substantial portions of the Software.
cannam@150 20
cannam@150 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@150 22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@150 23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@150 24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@150 25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@150 26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@150 27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@150 28
cannam@150 29 Except as contained in this notice, the names of the Centre for
cannam@150 30 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@150 31 shall not be used in advertising or otherwise to promote the sale,
cannam@150 32 use or other dealings in this Software without prior written
cannam@150 33 authorization.
cannam@150 34 */
cannam@150 35
cannam@150 36 #ifndef PIPER_AUTO_PLUGIN_H
cannam@150 37 #define PIPER_AUTO_PLUGIN_H
cannam@150 38
cannam@150 39 #include "ProcessQtTransport.h"
cannam@152 40 #include "../CapnpRRClient.h"
cannam@170 41 #include "../Exceptions.h"
cannam@150 42
cannam@150 43 #include <cstdint>
cannam@150 44
cannam@150 45 namespace piper_vamp {
cannam@150 46 namespace client {
cannam@150 47
cannam@170 48 /**
cannam@170 49 * This "plugin" make the Piper client abstraction behave like a local
cannam@170 50 * Vamp plugin, with its own server that lasts only for the lifetime
cannam@170 51 * of this plugin and serves only it.
cannam@170 52 *
cannam@170 53 * Note that any method may throw ServerCrashed, RequestTimedOut or
cannam@170 54 * ProtocolError exceptions.
cannam@170 55 */
cannam@150 56 class AutoPlugin : public Vamp::Plugin
cannam@150 57 {
cannam@150 58 public:
cannam@150 59 AutoPlugin(std::string serverName,
cannam@150 60 std::string pluginKey,
cannam@150 61 float inputSampleRate,
cannam@150 62 int adapterFlags,
cannam@150 63 LogCallback *logger) : // logger may be nullptr for cerr
cannam@150 64 Vamp::Plugin(inputSampleRate),
cannam@150 65 m_logger(logger),
cannam@150 66 m_transport(serverName, "capnp", logger),
cannam@150 67 m_client(&m_transport, logger)
cannam@150 68 {
cannam@150 69 LoadRequest req;
cannam@150 70 req.pluginKey = pluginKey;
cannam@150 71 req.inputSampleRate = inputSampleRate;
cannam@150 72 req.adapterFlags = adapterFlags;
cannam@150 73 try {
cannam@150 74 LoadResponse resp = m_client.loadPlugin(req);
cannam@150 75 m_plugin = resp.plugin;
cannam@150 76 } catch (ServerCrashed c) {
cannam@150 77 log(std::string("AutoPlugin: Server crashed: ") + c.what());
cannam@150 78 m_plugin = 0;
cannam@150 79 }
cannam@150 80 }
cannam@150 81
cannam@150 82 virtual ~AutoPlugin() {
cannam@150 83 delete m_plugin;
cannam@150 84 }
cannam@150 85
cannam@150 86 bool isOK() const {
cannam@150 87 return (m_plugin != nullptr);
cannam@150 88 }
cannam@150 89
cannam@150 90 virtual std::string getIdentifier() const {
cannam@150 91 return getPlugin()->getIdentifier();
cannam@150 92 }
cannam@150 93
cannam@150 94 virtual std::string getName() const {
cannam@150 95 return getPlugin()->getName();
cannam@150 96 }
cannam@150 97
cannam@150 98 virtual std::string getDescription() const {
cannam@150 99 return getPlugin()->getDescription();
cannam@150 100 }
cannam@150 101
cannam@150 102 virtual std::string getMaker() const {
cannam@150 103 return getPlugin()->getMaker();
cannam@150 104 }
cannam@150 105
cannam@150 106 virtual std::string getCopyright() const {
cannam@150 107 return getPlugin()->getCopyright();
cannam@150 108 }
cannam@150 109
cannam@150 110 virtual int getPluginVersion() const {
cannam@150 111 return getPlugin()->getPluginVersion();
cannam@150 112 }
cannam@150 113
cannam@150 114 virtual ParameterList getParameterDescriptors() const {
cannam@150 115 return getPlugin()->getParameterDescriptors();
cannam@150 116 }
cannam@150 117
cannam@150 118 virtual float getParameter(std::string name) const {
cannam@150 119 return getPlugin()->getParameter(name);
cannam@150 120 }
cannam@150 121
cannam@150 122 virtual void setParameter(std::string name, float value) {
cannam@150 123 getPlugin()->setParameter(name, value);
cannam@150 124 }
cannam@150 125
cannam@150 126 virtual ProgramList getPrograms() const {
cannam@150 127 return getPlugin()->getPrograms();
cannam@150 128 }
cannam@150 129
cannam@150 130 virtual std::string getCurrentProgram() const {
cannam@150 131 return getPlugin()->getCurrentProgram();
cannam@150 132 }
cannam@150 133
cannam@150 134 virtual void selectProgram(std::string program) {
cannam@150 135 getPlugin()->selectProgram(program);
cannam@150 136 }
cannam@150 137
cannam@150 138 virtual bool initialise(size_t inputChannels,
cannam@150 139 size_t stepSize,
cannam@150 140 size_t blockSize) {
cannam@170 141 try {
cannam@170 142 return getPlugin()->initialise(inputChannels, stepSize, blockSize);
cannam@170 143 } catch (const ServiceError &e) {
cannam@170 144 // Sadly, the Vamp API has taught hosts to try to divine
cannam@170 145 // initialisation problems from a bool return value alone
cannam@170 146 log(std::string("AutoPlugin: initialise failed: ") + e.what());
cannam@170 147 return false;
cannam@170 148 }
cannam@150 149 }
cannam@150 150
cannam@150 151 virtual void reset() {
cannam@150 152 getPlugin()->reset();
cannam@150 153 }
cannam@150 154
cannam@150 155 virtual InputDomain getInputDomain() const {
cannam@150 156 return getPlugin()->getInputDomain();
cannam@150 157 }
cannam@150 158
cannam@150 159 virtual size_t getPreferredBlockSize() const {
cannam@150 160 return getPlugin()->getPreferredBlockSize();
cannam@150 161 }
cannam@150 162
cannam@150 163 virtual size_t getPreferredStepSize() const {
cannam@150 164 return getPlugin()->getPreferredStepSize();
cannam@150 165 }
cannam@150 166
cannam@150 167 virtual size_t getMinChannelCount() const {
cannam@150 168 return getPlugin()->getMinChannelCount();
cannam@150 169 }
cannam@150 170
cannam@150 171 virtual size_t getMaxChannelCount() const {
cannam@150 172 return getPlugin()->getMaxChannelCount();
cannam@150 173 }
cannam@150 174
cannam@150 175 virtual OutputList getOutputDescriptors() const {
cannam@150 176 return getPlugin()->getOutputDescriptors();
cannam@150 177 }
cannam@150 178
cannam@150 179 virtual FeatureSet process(const float *const *inputBuffers,
cannam@150 180 Vamp::RealTime timestamp) {
cannam@150 181 return getPlugin()->process(inputBuffers, timestamp);
cannam@150 182 }
cannam@150 183
cannam@150 184 virtual FeatureSet getRemainingFeatures() {
cannam@150 185 return getPlugin()->getRemainingFeatures();
cannam@150 186 }
cannam@150 187
cannam@150 188 private:
cannam@150 189 LogCallback *m_logger;
cannam@150 190 ProcessQtTransport m_transport;
cannam@150 191 CapnpRRClient m_client;
cannam@150 192 Vamp::Plugin *m_plugin;
cannam@150 193 Vamp::Plugin *getPlugin() const {
cannam@150 194 if (!m_plugin) {
cannam@150 195 log("AutoPlugin: getPlugin() failed (caller should have called AutoPlugin::isOK)");
cannam@150 196 throw std::logic_error("Plugin load failed");
cannam@150 197 }
cannam@150 198 return m_plugin;
cannam@150 199 }
cannam@150 200
cannam@150 201 void log(std::string message) const {
cannam@150 202 if (m_logger) m_logger->log(message);
cannam@150 203 else std::cerr << message << std::endl;
cannam@150 204 }
cannam@150 205 };
cannam@150 206
cannam@150 207 }
cannam@150 208 }
cannam@150 209
cannam@150 210 #endif
cannam@150 211
cannam@150 212