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