annotate vamp-client/PluginStub.h @ 116:d15cb1151d76

Add JSON support directly to the server. Had hoped to avoid this (using Capnp as canonical in the server and then converting externally as necessary) but it's just too useful for debugging purposes when bundled with client app
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 27 Oct 2016 11:39:41 +0100
parents cd438188e3f9
children ff3fd8d1b2dc
rev   line source
c@94 1
c@94 2 #ifndef PIPER_PLUGIN_STUB_H
c@94 3 #define PIPER_PLUGIN_STUB_H
c@94 4
c@94 5 #include <vamp-hostsdk/Plugin.h>
c@94 6 #include <vamp-hostsdk/PluginLoader.h>
c@97 7
c@97 8 #include "vamp-support/PluginStaticData.h"
c@97 9 #include "vamp-support/PluginConfiguration.h"
c@94 10
c@94 11 #include <cstdint>
c@94 12
c@94 13 #include "PluginClient.h"
c@94 14
c@97 15 namespace piper_vamp {
c@97 16 namespace client {
c@94 17
c@94 18 class PluginStub : public Vamp::Plugin
c@94 19 {
c@94 20 enum State {
c@94 21 Loaded, Configured, Finished
c@94 22 };
c@94 23
c@94 24 public:
c@94 25 PluginStub(PluginClient *client,
c@94 26 std::string pluginKey,
c@94 27 float inputSampleRate,
c@94 28 int adapterFlags,
c@97 29 PluginStaticData psd,
c@97 30 PluginConfiguration defaultConfig) :
c@94 31 Plugin(inputSampleRate),
c@94 32 m_client(client),
c@94 33 m_key(pluginKey),
c@94 34 m_adapterFlags(adapterFlags),
c@94 35 m_state(Loaded),
c@94 36 m_psd(psd),
c@94 37 m_defaultConfig(defaultConfig),
c@94 38 m_config(defaultConfig)
c@94 39 { }
c@94 40
c@94 41 virtual ~PluginStub() {
c@94 42 if (m_state != Finished) {
c@94 43 (void)m_client->finish(this);
c@94 44 }
c@94 45 }
c@94 46
c@94 47 virtual std::string getIdentifier() const {
c@94 48 return m_psd.basic.identifier;
c@94 49 }
c@94 50
c@94 51 virtual std::string getName() const {
c@94 52 return m_psd.basic.name;
c@94 53 }
c@94 54
c@94 55 virtual std::string getDescription() const {
c@94 56 return m_psd.basic.description;
c@94 57 }
c@94 58
c@94 59 virtual std::string getMaker() const {
c@94 60 return m_psd.maker;
c@94 61 }
c@94 62
c@94 63 virtual std::string getCopyright() const {
c@94 64 return m_psd.copyright;
c@94 65 }
c@94 66
c@94 67 virtual int getPluginVersion() const {
c@94 68 return m_psd.pluginVersion;
c@94 69 }
c@94 70
c@94 71 virtual ParameterList getParameterDescriptors() const {
c@94 72 return m_psd.parameters;
c@94 73 }
c@94 74
c@94 75 virtual float getParameter(std::string name) const {
c@94 76 if (m_config.parameterValues.find(name) != m_config.parameterValues.end()) {
c@94 77 return m_config.parameterValues.at(name);
c@94 78 } else {
c@94 79 return 0.f;
c@94 80 }
c@94 81 }
c@94 82
c@94 83 virtual void setParameter(std::string name, float value) {
c@94 84 if (m_state != Loaded) {
c@94 85 throw std::logic_error("Can't set parameter after plugin initialised");
c@94 86 }
c@94 87 m_config.parameterValues[name] = value;
c@94 88 }
c@94 89
c@94 90 virtual ProgramList getPrograms() const {
c@94 91 return m_psd.programs;
c@94 92 }
c@94 93
c@94 94 virtual std::string getCurrentProgram() const {
c@94 95 return m_config.currentProgram;
c@94 96 }
c@94 97
c@94 98 virtual void selectProgram(std::string program) {
c@94 99 if (m_state != Loaded) {
c@94 100 throw std::logic_error("Can't select program after plugin initialised");
c@94 101 }
c@94 102 m_config.currentProgram = program;
c@94 103 }
c@94 104
c@94 105 virtual bool initialise(size_t inputChannels,
c@94 106 size_t stepSize,
c@94 107 size_t blockSize) {
c@94 108
c@94 109 if (m_state != Loaded) {
c@94 110 throw std::logic_error("Plugin has already been initialised");
c@94 111 }
c@94 112
c@102 113 m_config.channelCount = int(inputChannels);
c@102 114 m_config.stepSize = int(stepSize);
c@102 115 m_config.blockSize = int(blockSize);
c@94 116
c@94 117 m_outputs = m_client->configure(this, m_config);
c@94 118
c@94 119 if (!m_outputs.empty()) {
c@94 120 m_state = Configured;
c@94 121 return true;
c@94 122 } else {
c@94 123 return false;
c@94 124 }
c@94 125 }
c@94 126
c@94 127 virtual void reset() {
c@94 128
c@94 129 if (m_state == Loaded) {
c@94 130 // reset is a no-op if the plugin hasn't been initialised yet
c@94 131 return;
c@94 132 }
c@94 133
c@94 134 m_client->reset(this, m_config);
c@94 135
c@94 136 m_state = Configured;
c@94 137 }
c@94 138
c@94 139 virtual InputDomain getInputDomain() const {
c@94 140 return m_psd.inputDomain;
c@94 141 }
c@94 142
c@94 143 virtual size_t getPreferredBlockSize() const {
c@94 144 return m_defaultConfig.blockSize;
c@94 145 }
c@94 146
c@94 147 virtual size_t getPreferredStepSize() const {
c@94 148 return m_defaultConfig.stepSize;
c@94 149 }
c@94 150
c@94 151 virtual size_t getMinChannelCount() const {
c@94 152 return m_psd.minChannelCount;
c@94 153 }
c@94 154
c@94 155 virtual size_t getMaxChannelCount() const {
c@94 156 return m_psd.maxChannelCount;
c@94 157 }
c@94 158
c@94 159 virtual OutputList getOutputDescriptors() const {
c@94 160 if (m_state == Configured) {
c@94 161 return m_outputs;
c@94 162 }
c@94 163
c@94 164 //!!! todo: figure out for which hosts (and adapters?) it may
c@94 165 //!!! be a problem that the output descriptors are incomplete
c@94 166 //!!! here. Any such hosts/adapters are broken, but I bet they
c@94 167 //!!! exist
c@94 168
c@94 169 OutputList staticOutputs;
c@94 170 for (const auto &o: m_psd.basicOutputInfo) {
c@94 171 OutputDescriptor od;
c@94 172 od.identifier = o.identifier;
c@94 173 od.name = o.name;
c@94 174 od.description = o.description;
c@94 175 staticOutputs.push_back(od);
c@94 176 }
c@94 177 return staticOutputs;
c@94 178 }
c@94 179
c@94 180 virtual FeatureSet process(const float *const *inputBuffers,
c@94 181 Vamp::RealTime timestamp) {
c@94 182
c@94 183 if (m_state == Loaded) {
c@94 184 throw std::logic_error("Plugin has not been initialised");
c@94 185 }
c@94 186 if (m_state == Finished) {
c@94 187 throw std::logic_error("Plugin has already been disposed of");
c@94 188 }
c@94 189
c@94 190 //!!! ew
c@94 191 std::vector<std::vector<float> > vecbuf;
c@94 192 for (int c = 0; c < m_config.channelCount; ++c) {
c@94 193 vecbuf.push_back(std::vector<float>
c@94 194 (inputBuffers[c],
c@94 195 inputBuffers[c] + m_config.blockSize));
c@94 196 }
c@94 197
c@94 198 return m_client->process(this, vecbuf, timestamp);
c@94 199 }
c@94 200
c@94 201 virtual FeatureSet getRemainingFeatures() {
c@94 202
c@94 203 if (m_state == Loaded) {
c@94 204 throw std::logic_error("Plugin has not been configured");
c@94 205 }
c@94 206 if (m_state == Finished) {
c@94 207 throw std::logic_error("Plugin has already been disposed of");
c@94 208 }
c@94 209
c@94 210 m_state = Finished;
c@94 211
c@94 212 return m_client->finish(this);
c@94 213 }
c@94 214
c@94 215 // Not Plugin methods, but needed by the PluginClient to support reloads:
c@94 216
c@94 217 virtual float getInputSampleRate() const {
c@94 218 return m_inputSampleRate;
c@94 219 }
c@94 220
c@94 221 virtual std::string getPluginKey() const {
c@94 222 return m_key;
c@94 223 }
c@94 224
c@94 225 virtual int getAdapterFlags() const {
c@94 226 return m_adapterFlags;
c@94 227 }
c@94 228
c@94 229 private:
c@94 230 PluginClient *m_client;
c@94 231 std::string m_key;
c@94 232 int m_adapterFlags;
c@94 233 State m_state;
c@97 234 PluginStaticData m_psd;
c@94 235 OutputList m_outputs;
c@97 236 PluginConfiguration m_defaultConfig;
c@97 237 PluginConfiguration m_config;
c@94 238 };
c@94 239
c@94 240 }
c@94 241 }
c@94 242
c@94 243 #endif