cannam@233: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
cannam@233: 
cannam@233: /*
cannam@233:     Vamp
cannam@233: 
cannam@233:     An API for audio analysis and feature extraction plugins.
cannam@233: 
cannam@233:     Centre for Digital Music, Queen Mary, University of London.
cannam@290:     Copyright 2006-2009 Chris Cannam and QMUL.
cannam@233:   
cannam@233:     Permission is hereby granted, free of charge, to any person
cannam@233:     obtaining a copy of this software and associated documentation
cannam@233:     files (the "Software"), to deal in the Software without
cannam@233:     restriction, including without limitation the rights to use, copy,
cannam@233:     modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@233:     of the Software, and to permit persons to whom the Software is
cannam@233:     furnished to do so, subject to the following conditions:
cannam@233: 
cannam@233:     The above copyright notice and this permission notice shall be
cannam@233:     included in all copies or substantial portions of the Software.
cannam@233: 
cannam@233:     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@233:     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@233:     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@233:     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@233:     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@233:     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@233:     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@233: 
cannam@233:     Except as contained in this notice, the names of the Centre for
cannam@233:     Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@233:     shall not be used in advertising or otherwise to promote the sale,
cannam@233:     use or other dealings in this Software without prior written
cannam@233:     authorization.
cannam@233: */
cannam@233: 
cannam@233: #include <vamp-hostsdk/PluginChannelAdapter.h>
cannam@233: 
cannam@263: _VAMP_SDK_HOSTSPACE_BEGIN(PluginChannelAdapter.cpp)
cannam@263: 
cannam@233: namespace Vamp {
cannam@233: 
cannam@233: namespace HostExt {
cannam@233: 
cannam@233: class PluginChannelAdapter::Impl
cannam@233: {
cannam@233: public:
cannam@233:     Impl(Plugin *plugin);
cannam@233:     ~Impl();
cannam@233: 
cannam@233:     bool initialise(size_t channels, size_t stepSize, size_t blockSize);
cannam@233: 
cannam@233:     FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@233:     FeatureSet processInterleaved(const float *inputBuffers, RealTime timestamp);
cannam@233: 
cannam@233: protected:
cannam@233:     Plugin *m_plugin;
cannam@233:     size_t m_blockSize;
cannam@233:     size_t m_inputChannels;
cannam@233:     size_t m_pluginChannels;
cannam@233:     float **m_buffer;
cannam@233:     float **m_deinterleave;
cannam@233:     const float **m_forwardPtrs;
cannam@233: };
cannam@233: 
cannam@233: PluginChannelAdapter::PluginChannelAdapter(Plugin *plugin) :
cannam@233:     PluginWrapper(plugin)
cannam@233: {
cannam@233:     m_impl = new Impl(plugin);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::~PluginChannelAdapter()
cannam@233: {
cannam@233:     delete m_impl;
cannam@233: }
cannam@233: 
cannam@233: bool
cannam@233: PluginChannelAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@233: {
cannam@233:     return m_impl->initialise(channels, stepSize, blockSize);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::FeatureSet
cannam@233: PluginChannelAdapter::process(const float *const *inputBuffers,
cannam@233:                               RealTime timestamp)
cannam@233: {
cannam@233:     return m_impl->process(inputBuffers, timestamp);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::FeatureSet
cannam@233: PluginChannelAdapter::processInterleaved(const float *inputBuffers,
cannam@233:                                          RealTime timestamp)
cannam@233: {
cannam@233:     return m_impl->processInterleaved(inputBuffers, timestamp);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::Impl::Impl(Plugin *plugin) :
cannam@233:     m_plugin(plugin),
cannam@233:     m_blockSize(0),
cannam@233:     m_inputChannels(0),
cannam@233:     m_pluginChannels(0),
cannam@233:     m_buffer(0),
cannam@233:     m_deinterleave(0),
cannam@233:     m_forwardPtrs(0)
cannam@233: {
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::Impl::~Impl()
cannam@233: {
cannam@233:     // the adapter will delete the plugin
cannam@233: 
cannam@233:     if (m_buffer) {
cannam@233:         if (m_inputChannels > m_pluginChannels) {
cannam@233:             delete[] m_buffer[0];
cannam@233:         } else {
cannam@233:             for (size_t i = 0; i < m_pluginChannels - m_inputChannels; ++i) {
cannam@233:                 delete[] m_buffer[i];
cannam@233:             }
cannam@233:         }
cannam@233:         delete[] m_buffer;
cannam@233:         m_buffer = 0;
cannam@233:     }
cannam@233: 
cannam@233:     if (m_deinterleave) {
cannam@233:         for (size_t i = 0; i < m_inputChannels; ++i) {
cannam@233:             delete[] m_deinterleave[i];
cannam@233:         }
cannam@233:         delete[] m_deinterleave;
cannam@233:         m_deinterleave = 0;
cannam@233:     }
cannam@233: 
cannam@233:     if (m_forwardPtrs) {
cannam@233:         delete[] m_forwardPtrs;
cannam@233:         m_forwardPtrs = 0;
cannam@233:     }
cannam@233: }
cannam@233: 
cannam@233: bool
cannam@233: PluginChannelAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@233: {
cannam@233:     m_blockSize = blockSize;
cannam@233: 
cannam@233:     size_t minch = m_plugin->getMinChannelCount();
cannam@233:     size_t maxch = m_plugin->getMaxChannelCount();
cannam@233: 
cannam@233:     m_inputChannels = channels;
cannam@233: 
cannam@233:     if (m_inputChannels < minch) {
cannam@233: 
cannam@233:         m_forwardPtrs = new const float *[minch];
cannam@233: 
cannam@233:         if (m_inputChannels > 1) {
cannam@233:             // We need a set of zero-valued buffers to add to the
cannam@233:             // forwarded pointers
cannam@233:             m_buffer = new float*[minch - channels];
cannam@233:             for (size_t i = 0; i < minch; ++i) {
cannam@233:                 m_buffer[i] = new float[blockSize];
cannam@233:                 for (size_t j = 0; j < blockSize; ++j) {
cannam@233:                     m_buffer[i][j] = 0.f;
cannam@233:                 }
cannam@233:             }
cannam@233:         }
cannam@233: 
cannam@233:         m_pluginChannels = minch;
cannam@233: 
cannam@282: //        std::cerr << "PluginChannelAdapter::initialise: expanding " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
cannam@233: 
cannam@233:     } else if (m_inputChannels > maxch) {
cannam@233: 
cannam@233:         // We only need m_buffer if we are mixing down to a single
cannam@233:         // channel -- otherwise we can just forward the same float* as
cannam@233:         // passed in to process(), expecting the excess to be ignored
cannam@233: 
cannam@233:         if (maxch == 1) {
cannam@233:             m_buffer = new float *[1];
cannam@233:             m_buffer[0] = new float[blockSize];
cannam@233: 
cannam@282: //            std::cerr << "PluginChannelAdapter::initialise: mixing " << m_inputChannels << " to mono for plugin" << std::endl;
cannam@233: 
cannam@233:         } else {
cannam@233:             
cannam@282: //            std::cerr << "PluginChannelAdapter::initialise: reducing " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
cannam@233:         }
cannam@233: 
cannam@233:         m_pluginChannels = maxch;
cannam@233: 
cannam@233:     } else {
cannam@233:  
cannam@282: //        std::cerr << "PluginChannelAdapter::initialise: accepting given number of channels (" << m_inputChannels << ")" << std::endl;
cannam@233:         m_pluginChannels = m_inputChannels;
cannam@233:     }
cannam@233: 
cannam@233:     return m_plugin->initialise(m_pluginChannels, stepSize, blockSize);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::FeatureSet
cannam@233: PluginChannelAdapter::Impl::processInterleaved(const float *inputBuffers,
cannam@233:                                                RealTime timestamp)
cannam@233: {
cannam@233:     if (!m_deinterleave) {
cannam@233:         m_deinterleave = new float *[m_inputChannels];
cannam@233:         for (size_t i = 0; i < m_inputChannels; ++i) {
cannam@233:             m_deinterleave[i] = new float[m_blockSize];
cannam@233:         }
cannam@233:     }
cannam@233: 
cannam@233:     for (size_t i = 0; i < m_inputChannels; ++i) {
cannam@233:         for (size_t j = 0; j < m_blockSize; ++j) {
cannam@233:             m_deinterleave[i][j] = inputBuffers[j * m_inputChannels + i];
cannam@233:         }
cannam@233:     }
cannam@233: 
cannam@233:     return process(m_deinterleave, timestamp);
cannam@233: }
cannam@233: 
cannam@233: PluginChannelAdapter::FeatureSet
cannam@233: PluginChannelAdapter::Impl::process(const float *const *inputBuffers,
cannam@233:                                     RealTime timestamp)
cannam@233: {
cannam@233: //    std::cerr << "PluginChannelAdapter::process: " << m_inputChannels << " -> " << m_pluginChannels << " channels" << std::endl;
cannam@233: 
cannam@233:     if (m_inputChannels < m_pluginChannels) {
cannam@233: 
cannam@233:         if (m_inputChannels == 1) {
cannam@233:             for (size_t i = 0; i < m_pluginChannels; ++i) {
cannam@233:                 m_forwardPtrs[i] = inputBuffers[0];
cannam@233:             }
cannam@233:         } else {
cannam@233:             for (size_t i = 0; i < m_inputChannels; ++i) {
cannam@233:                 m_forwardPtrs[i] = inputBuffers[i];
cannam@233:             }
cannam@233:             for (size_t i = m_inputChannels; i < m_pluginChannels; ++i) {
cannam@233:                 m_forwardPtrs[i] = m_buffer[i - m_inputChannels];
cannam@233:             }
cannam@233:         }
cannam@233: 
cannam@233:         return m_plugin->process(m_forwardPtrs, timestamp);
cannam@233: 
cannam@233:     } else if (m_inputChannels > m_pluginChannels) {
cannam@233: 
cannam@233:         if (m_pluginChannels == 1) {
cannam@233:             for (size_t j = 0; j < m_blockSize; ++j) {
cannam@233:                 m_buffer[0][j] = inputBuffers[0][j];
cannam@233:             }
cannam@233:             for (size_t i = 1; i < m_inputChannels; ++i) {
cannam@233:                 for (size_t j = 0; j < m_blockSize; ++j) {
cannam@233:                     m_buffer[0][j] += inputBuffers[i][j];
cannam@233:                 }
cannam@233:             }
cannam@233:             for (size_t j = 0; j < m_blockSize; ++j) {
cannam@233:                 m_buffer[0][j] /= m_inputChannels;
cannam@233:             }
cannam@233:             return m_plugin->process(m_buffer, timestamp);
cannam@233:         } else {
cannam@233:             return m_plugin->process(inputBuffers, timestamp);
cannam@233:         }
cannam@233: 
cannam@233:     } else {
cannam@233: 
cannam@233:         return m_plugin->process(inputBuffers, timestamp);
cannam@233:     }
cannam@233: }
cannam@233: 
cannam@233: }
cannam@233: 
cannam@233: }
cannam@233: 
cannam@263: _VAMP_SDK_HOSTSPACE_END(PluginChannelAdapter.cpp)
cannam@233: 
cannam@263: