PluginChannelAdapter.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     Copyright 2006-2007 Chris Cannam and QMUL.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 #include "PluginChannelAdapter.h"
00038 
00039 namespace Vamp {
00040 
00041 namespace HostExt {
00042 
00043 class PluginChannelAdapter::Impl
00044 {
00045 public:
00046     Impl(Plugin *plugin);
00047     ~Impl();
00048 
00049     bool initialise(size_t channels, size_t stepSize, size_t blockSize);
00050 
00051     FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
00052 
00053 protected:
00054     Plugin *m_plugin;
00055     size_t m_blockSize;
00056     size_t m_inputChannels;
00057     size_t m_pluginChannels;
00058     float **m_buffer;
00059     const float **m_forwardPtrs;
00060 };
00061 
00062 PluginChannelAdapter::PluginChannelAdapter(Plugin *plugin) :
00063     PluginWrapper(plugin)
00064 {
00065     m_impl = new Impl(plugin);
00066 }
00067 
00068 PluginChannelAdapter::~PluginChannelAdapter()
00069 {
00070     delete m_impl;
00071 }
00072 
00073 bool
00074 PluginChannelAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
00075 {
00076     return m_impl->initialise(channels, stepSize, blockSize);
00077 }
00078 
00079 PluginChannelAdapter::FeatureSet
00080 PluginChannelAdapter::process(const float *const *inputBuffers,
00081                               RealTime timestamp)
00082 {
00083     return m_impl->process(inputBuffers, timestamp);
00084 }
00085 
00086 PluginChannelAdapter::Impl::Impl(Plugin *plugin) :
00087     m_plugin(plugin),
00088     m_blockSize(0),
00089     m_inputChannels(0),
00090     m_pluginChannels(0),
00091     m_buffer(0),
00092     m_forwardPtrs(0)
00093 {
00094 }
00095 
00096 PluginChannelAdapter::Impl::~Impl()
00097 {
00098     // the adapter will delete the plugin
00099 
00100     if (m_buffer) {
00101         if (m_inputChannels > m_pluginChannels) {
00102             delete[] m_buffer[0];
00103         } else {
00104             for (size_t i = 0; i < m_pluginChannels - m_inputChannels; ++i) {
00105                 delete[] m_buffer[i];
00106             }
00107         }
00108         delete[] m_buffer;
00109         m_buffer = 0;
00110     }
00111 
00112     if (m_forwardPtrs) {
00113         delete[] m_forwardPtrs;
00114         m_forwardPtrs = 0;
00115     }
00116 }
00117 
00118 bool
00119 PluginChannelAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
00120 {
00121     m_blockSize = blockSize;
00122 
00123     size_t minch = m_plugin->getMinChannelCount();
00124     size_t maxch = m_plugin->getMaxChannelCount();
00125 
00126     m_inputChannels = channels;
00127 
00128     if (m_inputChannels < minch) {
00129 
00130         m_forwardPtrs = new const float *[minch];
00131 
00132         if (m_inputChannels > 1) {
00133             // We need a set of zero-valued buffers to add to the
00134             // forwarded pointers
00135             m_buffer = new float*[minch - channels];
00136             for (size_t i = 0; i < minch; ++i) {
00137                 m_buffer[i] = new float[blockSize];
00138                 for (size_t j = 0; j < blockSize; ++j) {
00139                     m_buffer[i][j] = 0.f;
00140                 }
00141             }
00142         }
00143 
00144         m_pluginChannels = minch;
00145 
00146         std::cerr << "PluginChannelAdapter::initialise: expanding " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
00147 
00148     } else if (m_inputChannels > maxch) {
00149 
00150         // We only need m_buffer if we are mixing down to a single
00151         // channel -- otherwise we can just forward the same float* as
00152         // passed in to process(), expecting the excess to be ignored
00153 
00154         if (maxch == 1) {
00155             m_buffer = new float *[1];
00156             m_buffer[0] = new float[blockSize];
00157 
00158             std::cerr << "PluginChannelAdapter::initialise: mixing " << m_inputChannels << " to mono for plugin" << std::endl;
00159 
00160         } else {
00161             
00162             std::cerr << "PluginChannelAdapter::initialise: reducing " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
00163         }
00164 
00165         m_pluginChannels = maxch;
00166 
00167     } else {
00168  
00169         std::cerr << "PluginChannelAdapter::initialise: accepting given number of channels (" << m_inputChannels << ")" << std::endl;
00170         m_pluginChannels = m_inputChannels;
00171     }
00172 
00173     return m_plugin->initialise(m_pluginChannels, stepSize, blockSize);
00174 }
00175 
00176 PluginChannelAdapter::FeatureSet
00177 PluginChannelAdapter::Impl::process(const float *const *inputBuffers,
00178                                     RealTime timestamp)
00179 {
00180 //    std::cerr << "PluginChannelAdapter::process: " << m_inputChannels << " -> " << m_pluginChannels << " channels" << std::endl;
00181 
00182     if (m_inputChannels < m_pluginChannels) {
00183 
00184         if (m_inputChannels == 1) {
00185             for (size_t i = 0; i < m_pluginChannels; ++i) {
00186                 m_forwardPtrs[i] = inputBuffers[0];
00187             }
00188         } else {
00189             for (size_t i = 0; i < m_inputChannels; ++i) {
00190                 m_forwardPtrs[i] = inputBuffers[i];
00191             }
00192             for (size_t i = m_inputChannels; i < m_pluginChannels; ++i) {
00193                 m_forwardPtrs[i] = m_buffer[i - m_inputChannels];
00194             }
00195         }
00196 
00197         return m_plugin->process(m_forwardPtrs, timestamp);
00198 
00199     } else if (m_inputChannels > m_pluginChannels) {
00200 
00201         if (m_pluginChannels == 1) {
00202             for (size_t j = 0; j < m_blockSize; ++j) {
00203                 m_buffer[0][j] = inputBuffers[0][j];
00204             }
00205             for (size_t i = 1; i < m_inputChannels; ++i) {
00206                 for (size_t j = 0; j < m_blockSize; ++j) {
00207                     m_buffer[0][j] += inputBuffers[i][j];
00208                 }
00209             }
00210             for (size_t j = 0; j < m_blockSize; ++j) {
00211                 m_buffer[0][j] /= m_inputChannels;
00212             }
00213             return m_plugin->process(m_buffer, timestamp);
00214         } else {
00215             return m_plugin->process(inputBuffers, timestamp);
00216         }
00217 
00218     } else {
00219 
00220         return m_plugin->process(inputBuffers, timestamp);
00221     }
00222 }
00223 
00224 }
00225 
00226 }
00227 
00228 

Generated on Thu Jun 19 14:01:48 2008 for VampPluginSDK by  doxygen 1.5.5