Mercurial > hg > piper-cpp
comparison vamp-client/qt/PiperAutoPlugin.h @ 214:0906984b9496
Merge pull request #5 from piper-audio/dev/rename-pluginstub
Dev/rename pluginstub
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 09 Feb 2017 14:41:29 +0000 |
parents | df65480a08de |
children | 3db4c7998faf |
comparison
equal
deleted
inserted
replaced
211:8183c3be5592 | 214:0906984b9496 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 /* | |
3 Piper C++ | |
4 | |
5 An API for audio analysis and feature extraction plugins. | |
6 | |
7 Centre for Digital Music, Queen Mary, University of London. | |
8 Copyright 2006-2017 Chris Cannam and QMUL. | |
9 | |
10 Permission is hereby granted, free of charge, to any person | |
11 obtaining a copy of this software and associated documentation | |
12 files (the "Software"), to deal in the Software without | |
13 restriction, including without limitation the rights to use, copy, | |
14 modify, merge, publish, distribute, sublicense, and/or sell copies | |
15 of the Software, and to permit persons to whom the Software is | |
16 furnished to do so, subject to the following conditions: | |
17 | |
18 The above copyright notice and this permission notice shall be | |
19 included in all copies or substantial portions of the Software. | |
20 | |
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR | |
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
28 | |
29 Except as contained in this notice, the names of the Centre for | |
30 Digital Music; Queen Mary, University of London; and Chris Cannam | |
31 shall not be used in advertising or otherwise to promote the sale, | |
32 use or other dealings in this Software without prior written | |
33 authorization. | |
34 */ | |
35 | |
36 #ifndef PIPER_AUTO_PLUGIN_H | |
37 #define PIPER_AUTO_PLUGIN_H | |
38 | |
39 #include "ProcessQtTransport.h" | |
40 #include "../CapnpRRClient.h" | |
41 #include "../Exceptions.h" | |
42 | |
43 #include <cstdint> | |
44 | |
45 namespace piper_vamp { | |
46 namespace client { | |
47 | |
48 /** | |
49 * AutoPlugin presents a Piper feature extractor in the form of a Vamp | |
50 * plugin, managing its own single-use server instance. That is, the | |
51 * distinguishing quality of AutoPlugin (in comparison with | |
52 * PluginStub) is that it runs and terminates its own Piper server, | |
53 * whose lifetime matches that of the plugin. | |
54 * | |
55 * Example usage: | |
56 * | |
57 * Vamp::Plugin *plugin = | |
58 * new AutoPlugin("piper-server-name.exe", | |
59 * "vamp-example-plugins:zerocrossing", | |
60 * 44100.0f, | |
61 * Vamp::HostExt::PluginLoader::ADAPT_ALL_SAFE, | |
62 * nullptr); | |
63 * plugin->initialise(...); | |
64 * plugin->process(...); <-- in the normal way for a Vamp plugin | |
65 * delete plugin; <-- causes the server to exit | |
66 * | |
67 * AutoPlugin makes use of the Loader and PluginClient interfaces, | |
68 * providing them its own transport layer object for its single server. | |
69 * | |
70 * Note that any method may throw ServerCrashed, RequestTimedOut or | |
71 * ProtocolError exceptions. | |
72 */ | |
73 class PiperAutoPlugin : public Vamp::Plugin | |
74 { | |
75 public: | |
76 /** | |
77 * Construct a PiperAutoPlugin that runs an instance of the Piper | |
78 * server with the given server name (executable path), requesting | |
79 * the given plugin key from the server. | |
80 * | |
81 * \param adapterFlags a bitwise OR of the values in the | |
82 * Vamp::HostExt::PluginLoader::AdapterFlags enumeration | |
83 * | |
84 * \param logger an optional callback for log messages. Pass a | |
85 * null pointer to use cerr instead. | |
86 */ | |
87 PiperAutoPlugin(std::string serverName, | |
88 std::string pluginKey, | |
89 float inputSampleRate, | |
90 int adapterFlags, | |
91 LogCallback *logger) : // logger may be nullptr for cerr | |
92 Vamp::Plugin(inputSampleRate), | |
93 m_logger(logger), | |
94 m_transport(serverName, "capnp", logger), | |
95 m_client(&m_transport, logger) | |
96 { | |
97 LoadRequest req; | |
98 req.pluginKey = pluginKey; | |
99 req.inputSampleRate = inputSampleRate; | |
100 req.adapterFlags = adapterFlags; | |
101 try { | |
102 LoadResponse resp = m_client.load(req); | |
103 m_plugin = resp.plugin; | |
104 } catch (ServerCrashed c) { | |
105 log(std::string("PiperAutoPlugin: Server crashed: ") + c.what()); | |
106 m_plugin = 0; | |
107 } | |
108 } | |
109 | |
110 virtual ~PiperAutoPlugin() { | |
111 delete m_plugin; | |
112 // The transport is a plain data member and will be deleted | |
113 // here, which will have the effect of terminating the server | |
114 } | |
115 | |
116 bool isOK() const { | |
117 return (m_plugin != nullptr); | |
118 } | |
119 | |
120 virtual std::string getIdentifier() const { | |
121 return getPlugin()->getIdentifier(); | |
122 } | |
123 | |
124 virtual std::string getName() const { | |
125 return getPlugin()->getName(); | |
126 } | |
127 | |
128 virtual std::string getDescription() const { | |
129 return getPlugin()->getDescription(); | |
130 } | |
131 | |
132 virtual std::string getMaker() const { | |
133 return getPlugin()->getMaker(); | |
134 } | |
135 | |
136 virtual std::string getCopyright() const { | |
137 return getPlugin()->getCopyright(); | |
138 } | |
139 | |
140 virtual int getPluginVersion() const { | |
141 return getPlugin()->getPluginVersion(); | |
142 } | |
143 | |
144 virtual ParameterList getParameterDescriptors() const { | |
145 return getPlugin()->getParameterDescriptors(); | |
146 } | |
147 | |
148 virtual float getParameter(std::string name) const { | |
149 return getPlugin()->getParameter(name); | |
150 } | |
151 | |
152 virtual void setParameter(std::string name, float value) { | |
153 getPlugin()->setParameter(name, value); | |
154 } | |
155 | |
156 virtual ProgramList getPrograms() const { | |
157 return getPlugin()->getPrograms(); | |
158 } | |
159 | |
160 virtual std::string getCurrentProgram() const { | |
161 return getPlugin()->getCurrentProgram(); | |
162 } | |
163 | |
164 virtual void selectProgram(std::string program) { | |
165 getPlugin()->selectProgram(program); | |
166 } | |
167 | |
168 virtual bool initialise(size_t inputChannels, | |
169 size_t stepSize, | |
170 size_t blockSize) { | |
171 return getPlugin()->initialise(inputChannels, stepSize, blockSize); | |
172 } | |
173 | |
174 virtual void reset() { | |
175 getPlugin()->reset(); | |
176 } | |
177 | |
178 virtual InputDomain getInputDomain() const { | |
179 return getPlugin()->getInputDomain(); | |
180 } | |
181 | |
182 virtual size_t getPreferredBlockSize() const { | |
183 return getPlugin()->getPreferredBlockSize(); | |
184 } | |
185 | |
186 virtual size_t getPreferredStepSize() const { | |
187 return getPlugin()->getPreferredStepSize(); | |
188 } | |
189 | |
190 virtual size_t getMinChannelCount() const { | |
191 return getPlugin()->getMinChannelCount(); | |
192 } | |
193 | |
194 virtual size_t getMaxChannelCount() const { | |
195 return getPlugin()->getMaxChannelCount(); | |
196 } | |
197 | |
198 virtual OutputList getOutputDescriptors() const { | |
199 return getPlugin()->getOutputDescriptors(); | |
200 } | |
201 | |
202 virtual FeatureSet process(const float *const *inputBuffers, | |
203 Vamp::RealTime timestamp) { | |
204 return getPlugin()->process(inputBuffers, timestamp); | |
205 } | |
206 | |
207 virtual FeatureSet getRemainingFeatures() { | |
208 return getPlugin()->getRemainingFeatures(); | |
209 } | |
210 | |
211 private: | |
212 LogCallback *m_logger; | |
213 ProcessQtTransport m_transport; | |
214 CapnpRRClient m_client; | |
215 Vamp::Plugin *m_plugin; | |
216 Vamp::Plugin *getPlugin() const { | |
217 if (!m_plugin) { | |
218 log("PiperAutoPlugin: getPlugin() failed (caller should have called PiperAutoPlugin::isOK)"); | |
219 throw std::logic_error("Plugin load failed"); | |
220 } | |
221 return m_plugin; | |
222 } | |
223 | |
224 void log(std::string message) const { | |
225 if (m_logger) m_logger->log(message); | |
226 else std::cerr << message << std::endl; | |
227 } | |
228 }; | |
229 | |
230 } | |
231 } | |
232 | |
233 #endif | |
234 | |
235 |