pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_OverlapMakeChannel.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_OVERLAPMAKECHANNEL_H
00040 #define PLONK_OVERLAPMAKECHANNEL_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00045 
00046 template<class SampleType> class OverlapMakeChannelInternal;
00047 
00048 PLONK_CHANNELDATA_DECLARE(OverlapMakeChannelInternal,SampleType)
00049 {
00050     ChannelInternalCore::Data base;
00051     bool zeroPad;
00052 };
00053 
00055 template<class SampleType>
00056 class OverlapMakeChannelInternal 
00057 :   public ProxyOwnerChannelInternal<SampleType, PLONK_CHANNELDATA_NAME(OverlapMakeChannelInternal,SampleType)>
00058 {
00059 public:
00060     typedef PLONK_CHANNELDATA_NAME(OverlapMakeChannelInternal,SampleType)   Data;
00061     typedef ChannelBase<SampleType>                                         ChannelType;
00062     typedef ObjectArray<ChannelType>                                        ChannelArrayType;
00063     typedef OverlapMakeChannelInternal<SampleType>                          OverlapMakeInternal;
00064     typedef ProxyOwnerChannelInternal<SampleType,Data>                      Internal;
00065     typedef ChannelInternalBase<SampleType>                                 InternalBase;
00066     typedef UnitBase<SampleType>                                            UnitType;
00067     typedef InputDictionary                                                 Inputs;
00068     typedef NumericalArray<SampleType>                                      Buffer;
00069     typedef ObjectArray<Buffer>                                             BufferArray;
00070     
00071     OverlapMakeChannelInternal (Inputs const& inputs, 
00072                                 Data const& data, 
00073                                 BlockSize const& blockSize,
00074                                 SampleRate const& sampleRate,
00075                                 ChannelArrayType& channels) throw()
00076     :   Internal (inputs.getMaxNumChannels(), inputs, data, blockSize, sampleRate, channels),
00077         tempBuffers (BufferArray::withSize (inputs.getMaxNumChannels())),
00078         tempBufferPos (0),
00079         tempBufferFill (0),
00080         nextInputTimeStamp (TimeStamp::getZero())
00081     {
00082     }
00083     
00084     Text getName() const throw()
00085     {
00086         return "Overlap Make";
00087     }        
00088     
00089     IntArray getInputKeys() const throw()
00090     {
00091         const IntArray keys (IOKey::Generic);
00092         return keys;
00093     }
00094         
00095     void initChannel (const int channel) throw()
00096     {        
00097         const UnitType& input = this->getInputAsUnit (IOKey::Generic);
00098         const SampleType sourceValue = input.getValue (channel);
00099 
00100         if ((channel % this->getNumChannels()) == 0)
00101         {
00102             // all need to be the same input BS and SR
00103             this->setBlockSize (input.getBlockSize (0));
00104             this->setSampleRate (input.getSampleRate (0));
00105             
00106             for (int i = 0; i < this->getNumChannels(); ++i)
00107             {
00108                 tempBuffers.atUnchecked (i).setSize (this->getBlockSize().getValue() * 2, false);
00109                 tempBuffers.atUnchecked (i).zero();
00110                 tempBufferPos = 0;
00111                 tempBufferFill = 0;
00112             }
00113         }
00114         
00115         this->initProxyValue (channel, sourceValue);        
00116     }    
00117     
00118     inline void process (ProcessInfo& info, const int /*channel*/) throw()
00119     {                
00120         /* Be careful optimising this with the new NumericalArray vector stuff */
00121         const Data& data = this->getState();
00122 
00123         const int numChannels = this->getNumChannels();
00124 
00125         UnitType& inputUnit (this->getInputAsUnit (IOKey::Generic));
00126                 
00127         const int outputBufferLength = this->getOutputBuffer (0).length();
00128         const int tempBufferLength = this->tempBuffers.atUnchecked (0).length();
00129         
00130         const int overlapHop = int (this->getOverlap().getValue() * outputBufferLength + 0.5);
00131         int i, channel, channelBufferPos, channelBufferFill;
00132         
00133         if (overlapHop < outputBufferLength)
00134         {
00135             const TimeStamp infoTimeStamp = info.getTimeStamp();
00136             
00137             // ensure we have enough buffered to output the buffer
00138             while ((tempBufferPos + outputBufferLength) >= tempBufferFill)
00139             {
00140                 info.setTimeStamp (nextInputTimeStamp);
00141                 
00142                 for (channel = 0; channel < numChannels; ++channel)
00143                 {
00144                     channelBufferFill = tempBufferFill;
00145 
00146                     SampleType* const tempBufferSamples = this->tempBuffers.atUnchecked (channel).getArray();
00147 
00148                     const Buffer& inputBuffer (inputUnit.process (info, channel));
00149                     const SampleType* const inputSamples = inputBuffer.getArray();
00150                     const int inputBufferLength = inputBuffer.length();
00151                     
00152                     plonk_assert (outputBufferLength == inputBufferLength);
00153                     
00154                     for (i = 0; i < inputBufferLength; ++i)
00155                         tempBufferSamples[channelBufferFill++] = inputSamples[i];
00156                 }
00157                 
00158                 tempBufferFill = channelBufferFill;
00159                 nextInputTimeStamp = inputUnit.getNextTimeStamp (0);
00160                 
00161                 plonk_assert (tempBufferFill <= tempBufferLength);
00162             }
00163             
00164             // output one buffer
00165             if (data.zeroPad == false)
00166             {
00167                 // filled with overlapping data
00168                 
00169                 for (channel = 0; channel < numChannels; ++channel)
00170                 {
00171                     channelBufferPos = tempBufferPos;
00172 
00173                     SampleType* const outputSamples = this->getOutputSamples (channel);
00174                     SampleType* const tempBufferSamples = this->tempBuffers.atUnchecked (channel).getArray();
00175 
00176                     for (i = 0; i < outputBufferLength; ++i)
00177                         outputSamples[i] = tempBufferSamples[channelBufferPos++];
00178                 }
00179                 
00180                 tempBufferPos = channelBufferPos;
00181                 
00182                 // adjust the position pointer
00183                 tempBufferPos -= outputBufferLength;
00184                 tempBufferPos += overlapHop;
00185             }
00186             else
00187             {
00188                 // overlaps are zero-padded
00189                 
00190                 const SampleType& zero = Math<SampleType>::get0();
00191                 
00192                 for (channel = 0; channel < numChannels; ++channel)
00193                 {
00194                     channelBufferPos = tempBufferPos;
00195 
00196                     SampleType* const outputSamples = this->getOutputSamples (channel);
00197                     SampleType* const tempBufferSamples = this->tempBuffers.atUnchecked (channel).getArray();
00198 
00199                     for (i = 0; i < overlapHop; ++i)
00200                         outputSamples[i] = tempBufferSamples[channelBufferPos++];
00201 
00202                     for (i = overlapHop; i < outputBufferLength; ++i)
00203                         outputSamples[i] = zero;
00204                 }
00205                 
00206                 tempBufferPos = channelBufferPos;
00207             }
00208             
00209             // if we're over 1/2 way move the 2nd half to the start 
00210             // (the 2nd half will be re-filled next time if needed)
00211             if (tempBufferPos >= outputBufferLength)
00212             {                
00213                 for (channel = 0; channel < numChannels; ++channel)
00214                 {
00215                     channelBufferFill = 0;
00216 
00217                     SampleType* const tempBufferSamples = this->tempBuffers.atUnchecked (channel).getArray();
00218 
00219                     for (i = outputBufferLength; i < tempBufferLength; ++i)
00220                         tempBufferSamples[channelBufferFill++] = tempBufferSamples[i];
00221                 }
00222                 
00223                 tempBufferFill = channelBufferFill;
00224                 tempBufferPos -= outputBufferLength;
00225             }
00226             
00227             info.setTimeStamp (infoTimeStamp); // reset for the parent graph        
00228         }
00229         else
00230         {
00231             for (channel = 0; channel < numChannels; ++channel)
00232             {
00233                 SampleType* const outputSamples = this->getOutputSamples (channel);
00234                 const Buffer& inputBuffer (inputUnit.process (info, channel));
00235                 const SampleType* const inputSamples = inputBuffer.getArray();
00236                 
00237                 plonk_assert (outputBufferLength == inputBuffer.length());
00238                 
00239                 Buffer::copyData (outputSamples, inputSamples, outputBufferLength);
00240             }
00241         }
00242     }
00243     
00244     
00245 private:
00246     BufferArray tempBuffers;
00247     int tempBufferPos;
00248     int tempBufferFill;
00249     TimeStamp nextInputTimeStamp;
00250 };
00251 
00252 //------------------------------------------------------------------------------
00253 
00266 template<class SampleType>
00267 class OverlapMakeUnit
00268 {
00269 public:    
00270     typedef OverlapMakeChannelInternal<SampleType>      OverlapMakeInternal;
00271     typedef typename OverlapMakeInternal::Data          Data;
00272     typedef ChannelBase<SampleType>                     ChannelType;
00273     typedef ChannelInternal<SampleType,Data>            Internal;
00274     typedef UnitBase<SampleType>                        UnitType;
00275     typedef InputDictionary                             Inputs;    
00276     
00277     static inline UnitInfos getInfo() throw()
00278     {        
00279         const double minOverlap = TypeUtility<double>::getTypeEpsilon();
00280 
00281         return UnitInfo ("OverlapMake", "Resamples an incoming signal into overlapping blocks.",
00282                          
00283                          // output
00284                          ChannelCount::VariableChannelCount, 
00285                          IOKey::Generic,         Measure::None,      IOInfo::NoDefault,   IOLimit::None,      IOKey::End,
00286                          
00287                          // inputs
00288                          IOKey::Generic,         Measure::None,      IOInfo::NoDefault,   IOLimit::None,
00289                          IOKey::OverlapMake,     Measure::Factor,    0.5,                 IOLimit::Clipped,   Measure::Factor,     minOverlap, 1.0,
00290                          IOKey::End);
00291     }    
00292     
00294     static UnitType ar (UnitType const& input, 
00295                         DoubleVariable const& overlap = Math<DoubleVariable>::get0_5(),
00296                         const bool zeroPad = false) throw()
00297     {                        
00298         plonk_assert (overlap.getValue() >= TypeUtility<double>::getTypeEpsilon());
00299         plonk_assert (overlap.getValue() <= 1.0);
00300         
00301         Inputs inputs;
00302         inputs.put (IOKey::Generic, input);
00303         inputs.put (IOKey::OverlapMake, overlap);
00304         
00305         Data data = { { -1.0, -1.0 }, zeroPad };
00306         
00307         return UnitType::template proxiesFromInputs<OverlapMakeInternal> (inputs,
00308                                                                           data,
00309                                                                           BlockSize::noPreference(),
00310                                                                           SampleRate::noPreference());
00311     }
00312         
00313 };
00314 
00315 typedef OverlapMakeUnit<PLONK_TYPE_DEFAULT> OverlapMake;
00316 
00317 
00318 
00319 #endif // PLONK_OVERLAPMAKECHANNEL_H
 All Classes Functions Typedefs Enumerations Enumerator Properties