comparison vamp-client/PluginStub.h @ 94:a660dca988f8

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