comparison src/PiperAdapter.h @ 117:2e8aacb7f883

Move some things around (have not yet updated builds)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 08 Nov 2016 12:02:57 +0000
parents
children 523047216129
comparison
equal deleted inserted replaced
116:286c8e57abd0 117:2e8aacb7f883
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Piper Vamp JSON Adapter
5
6 Centre for Digital Music, Queen Mary, University of London.
7 Copyright 2015-2016 QMUL.
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use, copy,
13 modify, merge, publish, distribute, sublicense, and/or sell copies
14 of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the names of the Centre for
29 Digital Music; Queen Mary, University of London; and Chris Cannam
30 shall not be used in advertising or otherwise to promote the sale,
31 use or other dealings in this Software without prior written
32 authorization.
33 */
34
35 #ifndef PIPER_ADAPTER_H
36 #define PIPER_ADAPTER_H
37
38 #include "vamp-support/PluginStaticData.h"
39 #include "vamp-support/PluginConfiguration.h"
40 #include "vamp-support/RequestResponse.h"
41
42 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
43 #include <vamp-hostsdk/PluginBufferingAdapter.h>
44 #include <vamp-hostsdk/PluginChannelAdapter.h>
45
46 namespace piper_vamp_js { //!!! not a good name for this namespace
47
48 class PiperAdapterInterface
49 {
50 public:
51 virtual std::string getLibraryName() const = 0;
52 virtual piper_vamp::PluginStaticData getStaticData() const = 0;
53 virtual piper_vamp::LoadResponse loadPlugin(piper_vamp::LoadRequest r) const = 0;
54 virtual Vamp::Plugin *createPlugin(float inputSampleRate) const = 0;
55 };
56
57 template <typename P>
58 class PiperAdapterBase : public PiperAdapterInterface
59 {
60 const int adaptInputDomain = 0x01;
61 const int adaptChannelCount = 0x02;
62 const int adaptBufferSize = 0x04;
63
64 protected:
65 PiperAdapterBase(std::string libname) : m_soname(libname) { }
66
67 public:
68 virtual Vamp::Plugin *createPlugin(float inputSampleRate) const = 0;
69
70 virtual std::string getLibraryName() const override {
71 return m_soname;
72 }
73
74 virtual piper_vamp::PluginStaticData getStaticData() const override {
75 Vamp::Plugin *p = createPlugin(44100.f);
76 auto data = piper_vamp::PluginStaticData::fromPlugin
77 (m_soname + ":" + p->getIdentifier(),
78 {}, //!!! todo: category - tricky one that
79 p);
80 delete p;
81 return data;
82 }
83
84 virtual piper_vamp::LoadResponse loadPlugin(piper_vamp::LoadRequest r)
85 const override {
86
87 // We assume the caller has guaranteed that the request is for
88 // the correct plugin (so we don't have to check the plugin
89 // key field here)
90
91 Vamp::Plugin *p = createPlugin(r.inputSampleRate);
92
93 if (r.adapterFlags & adaptInputDomain) {
94 if (p->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
95 p = new Vamp::HostExt::PluginInputDomainAdapter(p);
96 }
97 }
98
99 if (r.adapterFlags & adaptBufferSize) {
100 p = new Vamp::HostExt::PluginBufferingAdapter(p);
101 }
102
103 if (r.adapterFlags & adaptChannelCount) {
104 p = new Vamp::HostExt::PluginChannelAdapter(p);
105 }
106
107 piper_vamp::LoadResponse response;
108 response.plugin = p;
109
110 response.staticData = piper_vamp::PluginStaticData::fromPlugin
111 (m_soname + ":" + p->getIdentifier(),
112 {}, //!!! todo: category - tricky one that
113 p);
114
115 int defaultChannels = 0;
116 if (p->getMinChannelCount() == p->getMaxChannelCount()) {
117 defaultChannels = p->getMinChannelCount();
118 }
119
120 response.defaultConfiguration = piper_vamp::PluginConfiguration::fromPlugin
121 (p,
122 defaultChannels,
123 p->getPreferredStepSize(),
124 p->getPreferredBlockSize());
125
126 return response;
127 }
128
129 private:
130 std::string m_soname;
131 };
132
133 template <typename P>
134 class PiperAdapter : public PiperAdapterBase<P>
135 {
136 public:
137 PiperAdapter(std::string libname) : PiperAdapterBase<P>(libname) { }
138
139 virtual Vamp::Plugin *createPlugin(float inputSampleRate) const override {
140 return new P(inputSampleRate);
141 }
142 };
143
144 }
145
146 #endif
147