pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_BlockChannel.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_BLOCKCHANNEL_H
00040 #define PLONK_BLOCKCHANNEL_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00047 template<class SampleType>
00048 class BlockChannelInternal 
00049 :   public ChannelInternal<SampleType, ChannelInternalCore::Data>
00050 {
00051 public:
00052     typedef ChannelInternalCore::Data                           Data;
00053     typedef ChannelBase<SampleType>                             ChannelType;
00054     typedef BlockChannelInternal<SampleType>                    BlockInternal;
00055     typedef ChannelInternal<SampleType,Data>                    Internal;
00056     typedef ChannelInternalBase<SampleType>                     InternalBase;
00057     typedef UnitBase<SampleType>                                UnitType;
00058     typedef InputDictionary                                     Inputs;
00059     typedef NumericalArray<SampleType>                          Buffer;
00060     
00061     BlockChannelInternal (Inputs const& inputs, 
00062                           Data const& data, 
00063                           BlockSize const& blockSize,
00064                           SampleRate const& sampleRate) throw()
00065     :   Internal (inputs, data, blockSize, sampleRate)
00066     {
00067     }
00068     
00069     Text getName() const throw()
00070     {        
00071         return "Block";
00072     }        
00073     
00074     IntArray getInputKeys() const throw()
00075     {
00076         const IntArray keys (IOKey::Buffer);
00077         return keys;
00078     }    
00079     
00080     InternalBase* getChannel (const int /*index*/) throw()
00081     {
00082         return this;
00083     }        
00084     
00085     void initChannel (const int /*channel*/) throw()
00086     {
00087         const Buffer& buffer = this->getInputAsBuffer (IOKey::Buffer);
00088         
00089         if (buffer.length() == BlockSize::getDefault().getValue())
00090             this->setBlockSize (BlockSize::getDefault());
00091         else if (this->getBlockSize() == BlockSize::noPreference())
00092             this->setBlockSize (buffer.length());
00093         
00094         this->initValue (SampleType (0));
00095         this->copyBlock();        
00096     }    
00097     
00098     inline void copyBlock() throw()
00099     {
00100         SampleType* const outputSamples = this->getOutputSamples();
00101         const int outputBufferLength = this->getOutputBuffer().length();
00102         
00103         const Buffer& buffer = this->getInputAsBuffer (IOKey::Buffer);
00104         const SampleType* const bufferSamples = buffer.getArray();
00105         
00106         plonk_assert (outputBufferLength == buffer.length());
00107         
00108         Buffer::copyData (outputSamples, bufferSamples, outputBufferLength);
00109     }
00110         
00111     void process (ProcessInfo& /*info*/, const int /*channel*/) throw()
00112     {                
00113         this->copyBlock(); // can avoid doing this every time...
00114     }
00115 
00116 private:
00117 };
00118 
00119 
00120 
00121 //------------------------------------------------------------------------------
00122 
00139 template<class SampleType>
00140 class BlockUnit
00141 {
00142 public:    
00143     typedef BlockChannelInternal<SampleType>        BlockInternal;
00144     typedef typename BlockInternal::Data            Data;
00145     typedef ChannelBase<SampleType>                 ChannelType;
00146     typedef ChannelInternal<SampleType,Data>        Internal;
00147     typedef ChannelInternalBase<SampleType>         InternaBase;
00148     typedef UnitBase<SampleType>                    UnitType;
00149     typedef InputDictionary                         Inputs;
00150     typedef NumericalArray<SampleType>              Buffer;
00151     typedef NumericalArray2D<SampleType>            BufferArray;
00152     
00153     static inline UnitInfos getInfo() throw()
00154     {
00155         const double blockSize = (double)BlockSize::getDefault().getValue();
00156         const double sampleRate = SampleRate::getDefault().getValue();
00157 
00158         return UnitInfo ("Block", "Copies data from a buffer to an output block. Useful for windowing functions.",
00159                          
00160                          // output
00161                          1, 
00162                          IOKey::Generic,     Measure::None,      IOInfo::NoDefault,  IOLimit::None, 
00163                          IOKey::End,
00164                          
00165                          // inputs
00166                          IOKey::Buffer,         Measure::None,
00167                          IOKey::OverlapMake,    Measure::Factor,    0.5,        IOLimit::Clipped, Measure::NormalisedUnipolar,  0.0, 1.0,
00168                          IOKey::BlockSize,      Measure::Samples,   blockSize,  IOLimit::Minimum, Measure::Samples,             1.0,
00169                          IOKey::SampleRate,     Measure::Hertz,     sampleRate, IOLimit::Minimum, Measure::Hertz,               0.0,
00170                          IOKey::End);
00171     }    
00172     
00174     static inline UnitType ar (Buffer const& buffer,
00175                                DoubleVariable const& overlap = Math<DoubleVariable>::get1(),
00176                                BlockSize const& preferredBlockSize = BlockSize::noPreference(),
00177                                SampleRate const& preferredSampleRate = SampleRate::getDefault()) throw()
00178     {        
00179         Inputs inputs;
00180         inputs.put (IOKey::Buffer, buffer);
00181         inputs.put (IOKey::OverlapMake, overlap);
00182         
00183         Data data = { -1.0, -1.0 };
00184         
00185         return UnitType::template createFromInputs<BlockInternal> (inputs, 
00186                                                                    data, 
00187                                                                    preferredBlockSize, 
00188                                                                    preferredSampleRate);
00189     }
00190     
00192     static inline UnitType ar (BufferArray const& buffers,
00193                                DoubleVariable const& overlap = Math<DoubleVariable>::get1(),
00194                                BlockSize const& preferredBlockSize = BlockSize::noPreference(),
00195                                SampleRate const& preferredSampleRate = SampleRate::getDefault()) throw()
00196     {
00197         const int numChannels = buffers.length();
00198         UnitType result (UnitType::emptyWithAllocatedSize (numChannels));
00199         Data data = { -1.0, -1.0 };
00200         
00201         for (int i = 0; i < numChannels; ++i)
00202         {
00203             Inputs inputs;
00204             inputs.put (IOKey::Buffer, buffers[i]);
00205             inputs.put (IOKey::OverlapMake, overlap);
00206             
00207             result.add (UnitType::template createFromInputs<BlockInternal> (inputs,
00208                                                                             data,
00209                                                                             preferredBlockSize,
00210                                                                             preferredSampleRate));
00211         }
00212         
00213         return result;
00214     }
00215     
00216     static inline UnitType kr (Buffer const& buffer,                                
00217                                DoubleVariable const& overlap = Math<DoubleVariable>::get1()) throw()
00218     {
00219         return ar (buffer, 
00220                    overlap,
00221                    BlockSize::noPreference(), // ??
00222                    SampleRate::getControlRate());
00223     }
00224       
00225 };
00226 
00227 typedef BlockUnit<PLONK_TYPE_DEFAULT> Block;
00228 
00229 
00230 
00231 
00232 #endif // PLONK_BLOCKCHANNEL_H
00233 
00234 
 All Classes Functions Typedefs Enumerations Enumerator Properties