Mercurial > hg > piper-cpp
comparison vamp-support/CountingPluginHandleMapper.h @ 75:81e1c48e97f9
Rearrange and rename to Piper C++ structure
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 10 Oct 2016 16:31:09 +0100 |
parents | |
children | d9e85a65d45b |
comparison
equal
deleted
inserted
replaced
74:d45cfa25aaad | 75:81e1c48e97f9 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Piper C++ | |
5 | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 Copyright 2006-2016 Chris Cannam and 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_COUNTING_PLUGIN_HANDLE_MAPPER_H | |
36 #define PIPER_COUNTING_PLUGIN_HANDLE_MAPPER_H | |
37 | |
38 #include "PluginHandleMapper.h" | |
39 #include "PluginOutputIdMapper.h" | |
40 #include "DefaultPluginOutputIdMapper.h" | |
41 | |
42 #include <set> | |
43 #include <map> | |
44 | |
45 namespace piper { | |
46 | |
47 class CountingPluginHandleMapper : public PluginHandleMapper | |
48 { | |
49 public: | |
50 CountingPluginHandleMapper() : m_nextHandle(1) { } | |
51 | |
52 void addPlugin(Vamp::Plugin *p) { | |
53 if (!p) return; | |
54 if (m_rplugins.find(p) == m_rplugins.end()) { | |
55 Handle h = m_nextHandle++; | |
56 m_plugins[h] = p; | |
57 m_rplugins[p] = h; | |
58 m_outputMappers[h] = | |
59 std::make_shared<DefaultPluginOutputIdMapper>(p); | |
60 } | |
61 } | |
62 | |
63 void removePlugin(Handle h) { | |
64 if (m_plugins.find(h) == m_plugins.end()) return; | |
65 Vamp::Plugin *p = m_plugins[h]; | |
66 m_outputMappers.erase(h); | |
67 m_plugins.erase(h); | |
68 if (isConfigured(h)) { | |
69 m_configuredPlugins.erase(h); | |
70 m_channelCounts.erase(h); | |
71 } | |
72 m_rplugins.erase(p); | |
73 } | |
74 | |
75 Handle pluginToHandle(Vamp::Plugin *p) const noexcept { | |
76 if (m_rplugins.find(p) == m_rplugins.end()) { | |
77 return INVALID_HANDLE; | |
78 } | |
79 return m_rplugins.at(p); | |
80 } | |
81 | |
82 Vamp::Plugin *handleToPlugin(Handle h) const noexcept { | |
83 if (m_plugins.find(h) == m_plugins.end()) { | |
84 return nullptr; | |
85 } | |
86 return m_plugins.at(h); | |
87 } | |
88 | |
89 const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper | |
90 (Vamp::Plugin *p) const noexcept { | |
91 return handleToOutputIdMapper(pluginToHandle(p)); | |
92 } | |
93 | |
94 const std::shared_ptr<PluginOutputIdMapper> handleToOutputIdMapper | |
95 (Handle h) const noexcept { | |
96 if (h != INVALID_HANDLE && | |
97 m_outputMappers.find(h) != m_outputMappers.end()) { | |
98 return m_outputMappers.at(h); | |
99 } else { | |
100 return {}; | |
101 } | |
102 } | |
103 | |
104 bool isConfigured(Handle h) const noexcept { | |
105 if (h == INVALID_HANDLE) return false; | |
106 return m_configuredPlugins.find(h) != m_configuredPlugins.end(); | |
107 } | |
108 | |
109 void markConfigured(Handle h, int channelCount, int blockSize) { | |
110 if (h == INVALID_HANDLE) return; | |
111 m_configuredPlugins.insert(h); | |
112 m_channelCounts[h] = channelCount; | |
113 m_blockSizes[h] = blockSize; | |
114 } | |
115 | |
116 int getChannelCount(Handle h) const noexcept { | |
117 if (m_channelCounts.find(h) == m_channelCounts.end()) { | |
118 return 0; | |
119 } | |
120 return m_channelCounts.at(h); | |
121 } | |
122 | |
123 int getBlockSize(Handle h) const noexcept { | |
124 if (m_blockSizes.find(h) == m_blockSizes.end()) { | |
125 return 0; | |
126 } | |
127 return m_blockSizes.at(h); | |
128 } | |
129 | |
130 private: | |
131 Handle m_nextHandle; // NB plugin handle type must fit in JSON number | |
132 std::map<Handle, Vamp::Plugin *> m_plugins; | |
133 std::map<Vamp::Plugin *, Handle> m_rplugins; | |
134 std::set<Handle> m_configuredPlugins; | |
135 std::map<Handle, int> m_channelCounts; | |
136 std::map<Handle, int> m_blockSizes; | |
137 std::map<Handle, std::shared_ptr<PluginOutputIdMapper>> m_outputMappers; | |
138 }; | |
139 | |
140 } | |
141 | |
142 #endif |