pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_ReblockChannel.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_REBLOCKCHANNEL_H
00040 #define PLONK_REBLOCKCHANNEL_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00046 template<class SampleType>
00047 class ReblockChannelInternal 
00048 :   public ChannelInternal<SampleType, ChannelInternalCore::Data>
00049 {
00050 public:
00051     typedef ChannelInternalCore::Data                               Data;
00052     typedef ChannelBase<SampleType>                                 ChannelType;
00053     typedef ReblockChannelInternal<SampleType>                      ReblockInternal;
00054     typedef ChannelInternal<SampleType,Data>                        Internal;
00055     typedef ChannelInternalBase<SampleType>                         InternalBase;
00056     typedef UnitBase<SampleType>                                    UnitType;
00057     typedef InputDictionary                                         Inputs;
00058     typedef NumericalArray<SampleType>                              Buffer;
00059     
00060     ReblockChannelInternal (Inputs const& inputs, 
00061                             Data const& data,
00062                             BlockSize const& blockSize,
00063                             SampleRate const& sampleRate) throw()
00064     :   Internal (inputs, data, blockSize, sampleRate)
00065     {
00066     }
00067     
00068     Text getName() const throw()
00069     {
00070         return "Reblock";
00071     }        
00072     
00073     IntArray getInputKeys() const throw()
00074     {
00075         const IntArray keys (IOKey::Generic, IOKey::Busses);
00076         return keys;
00077     }
00078     
00079     InternalBase* getChannel (const int index) throw()
00080     {
00081         const UnitType& input = this->getInputAsUnit (IOKey::Generic);
00082         const Busses& busses = this->getInputAsBusses (IOKey::Busses);
00083 
00084         Inputs channelInputs;
00085         channelInputs.put (IOKey::Generic, input[index]);
00086         channelInputs.put (IOKey::Busses, busses); // must be all of them in this case
00087 
00088         return new ReblockChannelInternal (channelInputs, 
00089                                            this->getState(),
00090                                            this->getBlockSize(),
00091                                            this->getSampleRate());
00092     }
00093     
00094     void initChannel (const int channel) throw()
00095     {
00096         const UnitType& input = this->getInputAsUnit (IOKey::Generic);        
00097         plonk_assert (input.getOverlap (channel) == Math<DoubleVariable>::get1());
00098         this->setSampleRate (input.getSampleRate (channel));        
00099         this->initValue (input.getValue (channel));        
00100     }
00101     
00102     void process (ProcessInfo& info, const int channel) throw()
00103     {
00104         UnitType& inputUnit = this->getInputAsUnit (IOKey::Generic);
00105         Busses& busses = this->getInputAsBusses (IOKey::Busses);
00106         
00107         plonk_assert (busses.length() == inputUnit.getNumChannels());
00108         
00109         const int outputBufferLength = this->getOutputBuffer().length();
00110         SampleType* const outputSamples = this->getOutputSamples();
00111         
00112         TimeStamp infoTimeStamp = info.getTimeStamp();
00113         const TimeStamp latestTimeNeeded = infoTimeStamp + this->getSampleDurationInTicks() * outputBufferLength;
00114 
00115         while (busses.atUnchecked (0).getLatestValidTime() <= latestTimeNeeded)
00116         {
00117             const TimeStamp& thisTimeStamp = inputUnit.wrapAt (0).getNextTimeStamp();
00118             info.setTimeStamp (thisTimeStamp);
00119 
00120             for (int i = 0; i < busses.length(); ++i)
00121             {
00122                 const Buffer& inputBuffer (inputUnit.process (info, i));
00123                 const SampleType* const inputSamples = inputBuffer.getArray();
00124                 const int inputBufferLength = inputBuffer.length();
00125 
00126                 busses.atUnchecked (i).write (thisTimeStamp, inputBufferLength, inputSamples);
00127             }
00128         }
00129         
00130         info.setTimeStamp (infoTimeStamp); // ensure it is reset for parent graph
00131         
00132         // output one channel..
00133         busses.atUnchecked (channel).read (nextValidReadTime, outputBufferLength, outputSamples);
00134     }
00135     
00136 private:
00137     TimeStamp nextValidReadTime;
00138 };
00139 
00140 //------------------------------------------------------------------------------
00141 
00153 template<class SampleType>
00154 class ReblockUnit
00155 {
00156 public:    
00157     typedef ReblockChannelInternal<SampleType>          ReblockInternal;
00158     typedef typename ReblockInternal::Data              Data;
00159     typedef ChannelBase<SampleType>                     ChannelType;
00160     typedef ChannelInternal<SampleType,Data>            Internal;
00161     typedef UnitBase<SampleType>                        UnitType;
00162     typedef InputDictionary                             Inputs;    
00163     typedef NumericalArray<SampleType>                  Buffer;
00164     typedef BusBuffer<SampleType>                       Bus;
00165     typedef PLONK_BUSARRAYBASETYPE<Bus>                 Busses;
00166     
00167     static inline UnitInfos getInfo() throw()
00168     {
00169         const double blockSize = (double)BlockSize::getDefault().getValue();
00170 
00171         return UnitInfo ("Reblock", "Re-buffer to a different block size.",
00172                          
00173                          // output
00174                          ChannelCount::VariableChannelCount, 
00175                          IOKey::Generic,         Measure::None,      IOInfo::NoDefault,   IOLimit::None,      IOKey::End,
00176                          
00177                          // inputs
00178                          IOKey::Generic,         Measure::None,      IOInfo::NoDefault,   IOLimit::None,
00179                          IOKey::BlockSize,       Measure::Samples,   blockSize,           IOLimit::Minimum,   Measure::Samples,   1.0,
00180                          IOKey::End);
00181     }    
00182     
00184     static UnitType ar (UnitType const& input,
00185                         BlockSize const& preferredBlockSize = BlockSize::getDefault()) throw()
00186     {
00187         plonk_assert (input.channelsHaveSameBlockSize());
00188         plonk_assert (input.channelsHaveSameSampleRate());
00189         plonk_assert (input.channelsHaveSameOverlap());
00190         
00191         const int numChannels = input.getNumChannels();
00192         Busses busses (Busses::emptyWithAllocatedSize (numChannels));
00193         
00194         BlockSize inputBlockSize = input.getBlockSize (0);
00195         BlockSize bufferSize = preferredBlockSize.max (inputBlockSize) * 2;
00196         SampleRate sampleRate = input.getSampleRate (0);
00197         
00198         for (int i = 0; i < numChannels; ++i)
00199             busses.add (Bus (bufferSize, inputBlockSize, sampleRate));
00200         
00201         Inputs inputs;
00202         inputs.put (IOKey::Generic, input);
00203         inputs.put (IOKey::Busses, busses);
00204         
00205         Data data = { -1.0, -1.0 };
00206         
00207         return UnitType::template createFromInputs<ReblockInternal> (inputs, 
00208                                                                      data,
00209                                                                      preferredBlockSize,
00210                                                                      SampleRate::noPreference());
00211     }
00212 };
00213 
00214 typedef ReblockUnit<PLONK_TYPE_DEFAULT> Reblock;
00215 
00216 
00217 #endif // PLONK_REBLOCKCHANNEL_H
 All Classes Functions Typedefs Enumerations Enumerator Properties