c@75: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@75: c@75: /* c@75: Piper C++ c@75: c@75: Centre for Digital Music, Queen Mary, University of London. c@75: Copyright 2006-2016 Chris Cannam and QMUL. c@75: c@75: Permission is hereby granted, free of charge, to any person c@75: obtaining a copy of this software and associated documentation c@75: files (the "Software"), to deal in the Software without c@75: restriction, including without limitation the rights to use, copy, c@75: modify, merge, publish, distribute, sublicense, and/or sell copies c@75: of the Software, and to permit persons to whom the Software is c@75: furnished to do so, subject to the following conditions: c@75: c@75: The above copyright notice and this permission notice shall be c@75: included in all copies or substantial portions of the Software. c@75: c@75: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@75: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@75: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@75: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@75: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@75: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@75: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@75: c@75: Except as contained in this notice, the names of the Centre for c@75: Digital Music; Queen Mary, University of London; and Chris Cannam c@75: shall not be used in advertising or otherwise to promote the sale, c@75: use or other dealings in this Software without prior written c@75: authorization. c@75: */ c@75: c@75: #ifndef PIPER_COUNTING_PLUGIN_HANDLE_MAPPER_H c@75: #define PIPER_COUNTING_PLUGIN_HANDLE_MAPPER_H c@75: c@75: #include "PluginHandleMapper.h" c@75: #include "PluginOutputIdMapper.h" c@75: #include "DefaultPluginOutputIdMapper.h" c@75: c@75: #include c@75: #include c@75: c@75: namespace piper { c@75: c@75: class CountingPluginHandleMapper : public PluginHandleMapper c@75: { c@75: public: c@75: CountingPluginHandleMapper() : m_nextHandle(1) { } c@75: c@75: void addPlugin(Vamp::Plugin *p) { c@75: if (!p) return; c@75: if (m_rplugins.find(p) == m_rplugins.end()) { c@75: Handle h = m_nextHandle++; c@75: m_plugins[h] = p; c@75: m_rplugins[p] = h; c@75: m_outputMappers[h] = c@75: std::make_shared(p); c@75: } c@75: } c@75: c@75: void removePlugin(Handle h) { c@75: if (m_plugins.find(h) == m_plugins.end()) return; c@75: Vamp::Plugin *p = m_plugins[h]; c@75: m_outputMappers.erase(h); c@75: m_plugins.erase(h); c@75: if (isConfigured(h)) { c@75: m_configuredPlugins.erase(h); c@75: m_channelCounts.erase(h); c@75: } c@75: m_rplugins.erase(p); c@75: } c@75: c@75: Handle pluginToHandle(Vamp::Plugin *p) const noexcept { c@75: if (m_rplugins.find(p) == m_rplugins.end()) { c@75: return INVALID_HANDLE; c@75: } c@75: return m_rplugins.at(p); c@75: } c@75: c@75: Vamp::Plugin *handleToPlugin(Handle h) const noexcept { c@75: if (m_plugins.find(h) == m_plugins.end()) { c@75: return nullptr; c@75: } c@75: return m_plugins.at(h); c@75: } c@75: c@75: const std::shared_ptr pluginToOutputIdMapper c@75: (Vamp::Plugin *p) const noexcept { c@75: return handleToOutputIdMapper(pluginToHandle(p)); c@75: } c@75: c@75: const std::shared_ptr handleToOutputIdMapper c@75: (Handle h) const noexcept { c@75: if (h != INVALID_HANDLE && c@75: m_outputMappers.find(h) != m_outputMappers.end()) { c@75: return m_outputMappers.at(h); c@75: } else { c@75: return {}; c@75: } c@75: } c@75: c@75: bool isConfigured(Handle h) const noexcept { c@75: if (h == INVALID_HANDLE) return false; c@75: return m_configuredPlugins.find(h) != m_configuredPlugins.end(); c@75: } c@75: c@75: void markConfigured(Handle h, int channelCount, int blockSize) { c@75: if (h == INVALID_HANDLE) return; c@75: m_configuredPlugins.insert(h); c@75: m_channelCounts[h] = channelCount; c@75: m_blockSizes[h] = blockSize; c@75: } c@75: c@75: int getChannelCount(Handle h) const noexcept { c@75: if (m_channelCounts.find(h) == m_channelCounts.end()) { c@75: return 0; c@75: } c@75: return m_channelCounts.at(h); c@75: } c@75: c@75: int getBlockSize(Handle h) const noexcept { c@75: if (m_blockSizes.find(h) == m_blockSizes.end()) { c@75: return 0; c@75: } c@75: return m_blockSizes.at(h); c@75: } c@75: c@75: private: c@75: Handle m_nextHandle; // NB plugin handle type must fit in JSON number c@75: std::map m_plugins; c@75: std::map m_rplugins; c@75: std::set m_configuredPlugins; c@75: std::map m_channelCounts; c@75: std::map m_blockSizes; c@75: std::map> m_outputMappers; c@75: }; c@75: c@75: } c@75: c@75: #endif