comparison vamp-support/AssignedPluginHandleMapper.h @ 80:d9e85a65d45b

Inching toward a client
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 11 Oct 2016 15:49:28 +0100
parents
children db9a6ab618bc
comparison
equal deleted inserted replaced
79:f4497c1da43d 80:d9e85a65d45b
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_ASSIGNED_PLUGIN_HANDLE_MAPPER_H
36 #define PIPER_ASSIGNED_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 #include <iostream>
45
46 namespace piper {
47
48 class AssignedPluginHandleMapper : public PluginHandleMapper
49 {
50 public:
51 AssignedPluginHandleMapper() { }
52
53 void addPlugin(Vamp::Plugin *p, Handle h) {
54 if (!p) return;
55 if (m_rplugins.find(p) == m_rplugins.end()) {
56 if (m_plugins.find(h) != m_plugins.end()) {
57 std::cerr << "ERROR: Duplicate plugin handle " << h
58 << " for plugin " << p << " (already used for plugin "
59 << m_plugins[h] << ")" << std::endl;
60 throw std::logic_error("Duplicate plugin handle");
61 }
62 m_plugins[h] = p;
63 m_rplugins[p] = h;
64 m_outputMappers[h] =
65 std::make_shared<DefaultPluginOutputIdMapper>(p);
66 }
67 }
68
69 void removePlugin(Handle h) {
70 if (m_plugins.find(h) == m_plugins.end()) return;
71 Vamp::Plugin *p = m_plugins[h];
72 m_outputMappers.erase(h);
73 m_plugins.erase(h);
74 if (isConfigured(h)) {
75 m_configuredPlugins.erase(h);
76 m_channelCounts.erase(h);
77 }
78 m_rplugins.erase(p);
79 }
80
81 Handle pluginToHandle(Vamp::Plugin *p) const noexcept {
82 if (m_rplugins.find(p) == m_rplugins.end()) {
83 return INVALID_HANDLE;
84 }
85 return m_rplugins.at(p);
86 }
87
88 Vamp::Plugin *handleToPlugin(Handle h) const noexcept {
89 if (m_plugins.find(h) == m_plugins.end()) {
90 return nullptr;
91 }
92 return m_plugins.at(h);
93 }
94
95 const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper
96 (Vamp::Plugin *p) const noexcept {
97 return handleToOutputIdMapper(pluginToHandle(p));
98 }
99
100 const std::shared_ptr<PluginOutputIdMapper> handleToOutputIdMapper
101 (Handle h) const noexcept {
102 if (h != INVALID_HANDLE &&
103 m_outputMappers.find(h) != m_outputMappers.end()) {
104 return m_outputMappers.at(h);
105 } else {
106 return {};
107 }
108 }
109
110 bool isConfigured(Handle h) const noexcept {
111 if (h == INVALID_HANDLE) return false;
112 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
113 }
114
115 void markConfigured(Handle h, int channelCount, int blockSize) {
116 if (h == INVALID_HANDLE) return;
117 m_configuredPlugins.insert(h);
118 m_channelCounts[h] = channelCount;
119 m_blockSizes[h] = blockSize;
120 }
121
122 int getChannelCount(Handle h) const noexcept {
123 if (m_channelCounts.find(h) == m_channelCounts.end()) {
124 return 0;
125 }
126 return m_channelCounts.at(h);
127 }
128
129 int getBlockSize(Handle h) const noexcept {
130 if (m_blockSizes.find(h) == m_blockSizes.end()) {
131 return 0;
132 }
133 return m_blockSizes.at(h);
134 }
135
136 private:
137 std::map<Handle, Vamp::Plugin *> m_plugins;
138 std::map<Vamp::Plugin *, Handle> m_rplugins;
139 std::set<Handle> m_configuredPlugins;
140 std::map<Handle, int> m_channelCounts;
141 std::map<Handle, int> m_blockSizes;
142 std::map<Handle, std::shared_ptr<PluginOutputIdMapper>> m_outputMappers;
143 };
144
145 }
146
147 #endif