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