c@40
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@40
|
2
|
c@40
|
3 /*
|
c@40
|
4 Vampipe
|
c@40
|
5
|
c@40
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@40
|
7 Copyright 2006-2016 Chris Cannam and QMUL.
|
c@40
|
8
|
c@40
|
9 Permission is hereby granted, free of charge, to any person
|
c@40
|
10 obtaining a copy of this software and associated documentation
|
c@40
|
11 files (the "Software"), to deal in the Software without
|
c@40
|
12 restriction, including without limitation the rights to use, copy,
|
c@40
|
13 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@40
|
14 of the Software, and to permit persons to whom the Software is
|
c@40
|
15 furnished to do so, subject to the following conditions:
|
c@40
|
16
|
c@40
|
17 The above copyright notice and this permission notice shall be
|
c@40
|
18 included in all copies or substantial portions of the Software.
|
c@40
|
19
|
c@40
|
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@40
|
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@40
|
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@40
|
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
c@40
|
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@40
|
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@40
|
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@40
|
27
|
c@40
|
28 Except as contained in this notice, the names of the Centre for
|
c@40
|
29 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@40
|
30 shall not be used in advertising or otherwise to promote the sale,
|
c@40
|
31 use or other dealings in this Software without prior written
|
c@40
|
32 authorization.
|
c@40
|
33 */
|
c@40
|
34
|
c@40
|
35 #ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
|
c@40
|
36 #define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
|
c@40
|
37
|
c@40
|
38 #include "PluginHandleMapper.h"
|
c@51
|
39 #include "PluginOutputIdMapper.h"
|
c@51
|
40 #include "DefaultPluginOutputIdMapper.h"
|
c@40
|
41
|
c@40
|
42 #include <set>
|
c@40
|
43 #include <map>
|
c@40
|
44
|
c@40
|
45 namespace vampipe {
|
c@40
|
46
|
c@40
|
47 //!!! NB not thread-safe at present, should it be?
|
c@40
|
48 class CountingPluginHandleMapper : public PluginHandleMapper
|
c@40
|
49 {
|
c@40
|
50 public:
|
c@40
|
51 CountingPluginHandleMapper() : m_nextHandle(1) { }
|
c@40
|
52
|
c@51
|
53 ~CountingPluginHandleMapper() {
|
c@51
|
54 for (auto &o: m_outputMappers) {
|
c@51
|
55 delete o.second;
|
c@51
|
56 }
|
c@51
|
57 }
|
c@51
|
58
|
c@40
|
59 void addPlugin(Vamp::Plugin *p) {
|
c@40
|
60 if (m_rplugins.find(p) == m_rplugins.end()) {
|
c@51
|
61 Handle h = m_nextHandle++;
|
c@40
|
62 m_plugins[h] = p;
|
c@40
|
63 m_rplugins[p] = h;
|
c@51
|
64 m_outputMappers[h] = new DefaultPluginOutputIdMapper(p);
|
c@40
|
65 }
|
c@40
|
66 }
|
c@40
|
67
|
c@51
|
68 void removePlugin(Handle h) {
|
c@40
|
69 if (m_plugins.find(h) == m_plugins.end()) {
|
c@52
|
70 std::cerr << "remove: no handle " << h << std::endl;
|
c@40
|
71 throw NotFound();
|
c@40
|
72 }
|
c@40
|
73 Vamp::Plugin *p = m_plugins[h];
|
c@51
|
74 delete m_outputMappers.at(h);
|
c@51
|
75 m_outputMappers.erase(h);
|
c@40
|
76 m_plugins.erase(h);
|
c@40
|
77 if (isConfigured(h)) {
|
c@40
|
78 m_configuredPlugins.erase(h);
|
c@40
|
79 m_channelCounts.erase(h);
|
c@40
|
80 }
|
c@40
|
81 m_rplugins.erase(p);
|
c@40
|
82 }
|
c@40
|
83
|
c@51
|
84 Handle pluginToHandle(Vamp::Plugin *p) const {
|
c@40
|
85 if (m_rplugins.find(p) == m_rplugins.end()) {
|
c@52
|
86 std::cerr << "pluginToHandle: no plugin " << p << std::endl;
|
c@40
|
87 throw NotFound();
|
c@40
|
88 }
|
c@40
|
89 return m_rplugins.at(p);
|
c@40
|
90 }
|
c@40
|
91
|
c@51
|
92 Vamp::Plugin *handleToPlugin(Handle h) const {
|
c@40
|
93 if (m_plugins.find(h) == m_plugins.end()) {
|
c@52
|
94 std::cerr << "handleToPlugin: no handle " << h << std::endl;
|
c@40
|
95 throw NotFound();
|
c@40
|
96 }
|
c@40
|
97 return m_plugins.at(h);
|
c@40
|
98 }
|
c@40
|
99
|
c@51
|
100 //!!! iffy: mapper will move when another plugin is added. return by value?
|
c@51
|
101 const PluginOutputIdMapper &pluginToOutputIdMapper(Vamp::Plugin *p) const {
|
c@51
|
102 // pluginToHandle checks the plugin has been registered with us
|
c@52
|
103 std::cerr << "output id mapper requested for plugin with handle " << pluginToHandle(p) << std::endl;
|
c@51
|
104 return *m_outputMappers.at(pluginToHandle(p));
|
c@51
|
105 }
|
c@51
|
106
|
c@51
|
107 //!!! iffy: mapper will move when another plugin is added. return by value?
|
c@51
|
108 const PluginOutputIdMapper &handleToOutputIdMapper(Handle h) const {
|
c@52
|
109 std::cerr << "output id mapper requested for handle " << h << std::endl;
|
c@51
|
110 if (m_plugins.find(h) == m_plugins.end()) {
|
c@51
|
111 throw NotFound();
|
c@51
|
112 }
|
c@51
|
113 return *m_outputMappers.at(h);
|
c@51
|
114 }
|
c@51
|
115
|
c@51
|
116 bool isConfigured(Handle h) const {
|
c@40
|
117 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
|
c@40
|
118 }
|
c@40
|
119
|
c@51
|
120 void markConfigured(Handle h, int channelCount, int blockSize) {
|
c@40
|
121 m_configuredPlugins.insert(h);
|
c@40
|
122 m_channelCounts[h] = channelCount;
|
c@40
|
123 m_blockSizes[h] = blockSize;
|
c@40
|
124 }
|
c@40
|
125
|
c@51
|
126 int getChannelCount(Handle h) const {
|
c@40
|
127 if (m_channelCounts.find(h) == m_channelCounts.end()) {
|
c@40
|
128 throw NotFound();
|
c@40
|
129 }
|
c@40
|
130 return m_channelCounts.at(h);
|
c@40
|
131 }
|
c@40
|
132
|
c@51
|
133 int getBlockSize(Handle h) const {
|
c@40
|
134 if (m_blockSizes.find(h) == m_blockSizes.end()) {
|
c@40
|
135 throw NotFound();
|
c@40
|
136 }
|
c@40
|
137 return m_blockSizes.at(h);
|
c@40
|
138 }
|
c@40
|
139
|
c@40
|
140 private:
|
c@51
|
141 Handle m_nextHandle; // NB plugin handle type must fit in JSON number
|
c@51
|
142 std::map<Handle, Vamp::Plugin *> m_plugins;
|
c@51
|
143 std::map<Vamp::Plugin *, Handle> m_rplugins;
|
c@51
|
144 std::set<Handle> m_configuredPlugins;
|
c@51
|
145 std::map<Handle, int> m_channelCounts;
|
c@51
|
146 std::map<Handle, int> m_blockSizes;
|
c@51
|
147 std::map<Handle, DefaultPluginOutputIdMapper *> m_outputMappers;
|
c@40
|
148 };
|
c@40
|
149
|
c@40
|
150 }
|
c@40
|
151
|
c@40
|
152 #endif
|