pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_Lookup.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_LOOKUP_H
00040 #define PLONK_LOOKUP_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00045 template<class SampleType> class LookupChannelInternal;
00046 
00047 PLONK_CHANNELDATA_DECLARE(LookupChannelInternal,SampleType)
00048 {    
00049     typedef typename TypeUtility<SampleType>::IndexType PositionType;
00050     ChannelInternalCore::Data base;
00051 };      
00052 
00053 //------------------------------------------------------------------------------
00054 
00056 template<class SampleType>
00057 class LookupChannelInternal
00058 :   public ChannelInternal<SampleType, PLONK_CHANNELDATA_NAME(LookupChannelInternal,SampleType)>
00059 {
00060 public:
00061 
00062     typedef PLONK_CHANNELDATA_NAME(LookupChannelInternal,SampleType)  Data;
00063     typedef ChannelBase<SampleType>                                 ChannelType;
00064     typedef LookupChannelInternal<SampleType>                       LookupInternal;
00065     typedef ChannelInternal<SampleType,Data>                        Internal;
00066     typedef ChannelInternalBase<SampleType>                         InternalBase;
00067     typedef UnitBase<SampleType>                                    UnitType;
00068     typedef InputDictionary                                         Inputs;
00069     typedef NumericalArray<SampleType>                              Buffer;
00070     typedef WavetableBase<SampleType>                               WavetableType;
00071 
00072     typedef typename TypeUtility<SampleType>::IndexType             PositionType;
00073     typedef UnitBase<PositionType>                                  PositionUnitType;
00074     typedef NumericalArray<PositionType>                            PositionBufferType;
00075     typedef InterpLinear<SampleType,PositionType>                   InterpType;
00076 
00077     LookupChannelInternal (Inputs const& inputs, 
00078                            Data const& data,
00079                            BlockSize const& blockSize,
00080                            SampleRate const& sampleRate) throw()
00081     :   Internal (inputs, data, blockSize, sampleRate)
00082     {
00083     }
00084             
00085     Text getName() const throw()
00086     {
00087         return "Lookup";
00088     }       
00089     
00090     IntArray getInputKeys() const throw()
00091     {
00092         const IntArray keys (IOKey::Wavetable,
00093                              IOKey::Position);
00094         return keys;
00095     }    
00096     
00097     InternalBase* getChannel (const int index) throw()
00098     {
00099         const Inputs channelInputs = this->getInputs().getChannel (index);
00100         return new LookupChannelInternal (channelInputs, 
00101                                           this->getState(), 
00102                                           this->getBlockSize(), 
00103                                           this->getSampleRate());
00104     }
00105     
00106     void initChannel (const int channel) throw()
00107     {        
00108         const PositionUnitType& positionUnit = ChannelInternalCore::getInputAs<PositionUnitType> (IOKey::Time);
00109         
00110         this->setBlockSize (BlockSize::decide (positionUnit.getBlockSize (channel),
00111                                                this->getBlockSize()));
00112         this->setSampleRate (SampleRate::decide (positionUnit.getSampleRate (channel),
00113                                                  this->getSampleRate()));
00114         
00115         this->setOverlap (positionUnit.getOverlap (channel));
00116         
00117         this->initValue (0);//
00118     }    
00119     
00120     void process (ProcessInfo& info, const int channel) throw()
00121     {                
00122         PositionUnitType& positionUnit = ChannelInternalCore::getInputAs<PositionUnitType> (IOKey::Time);
00123         const PositionBufferType& positionBuffer (positionUnit.process (info, channel));
00124         
00125         SampleType* const outputSamples = this->getOutputSamples();
00126         const int outputBufferLength = this->getOutputBuffer().length();
00127         
00128         const PositionType* const positionSamples = positionBuffer.getArray();
00129         const int positionBufferLength = positionBuffer.length();
00130         
00131         const WavetableType& table (this->getInputAsWavetable (IOKey::Wavetable));
00132         const SampleType* const tableSamples = table.getArray();
00133         
00134         const PositionType positionScale (table.length());
00135         
00136         int i;
00137         
00138         if (positionBufferLength == outputBufferLength)
00139         {
00140             for (i = 0; i < outputBufferLength; ++i) 
00141             {
00142                 const PositionType currentPosition = plonk::wrap (positionSamples[i], PositionType (0), PositionType (1)) * positionScale;
00143                 outputSamples[i] = InterpType::lookup (tableSamples, currentPosition);
00144             }
00145         }
00146         else if (positionBufferLength == 1)
00147         {
00148             const PositionType currentPosition = plonk::wrap (positionSamples[0], PositionType (0), PositionType (1)) * positionScale;
00149             const SampleType value = InterpType::lookup (tableSamples, currentPosition);
00150             NumericalArrayFiller<SampleType>::fill (outputSamples, value, outputBufferLength);
00151         }
00152         else
00153         {
00154             double positionPosition = 0.0;
00155             const double positionIncrement = double (positionBufferLength) / double (outputBufferLength);
00156             
00157             for (i = 0; i < outputBufferLength; ++i) 
00158             {
00159                 const PositionType currentPosition = plonk::wrap (positionSamples[int (positionPosition)], PositionType (0), PositionType (1)) * positionScale;
00160                 outputSamples[i] = InterpType::lookup (tableSamples, currentPosition);
00161                 positionPosition += positionIncrement;
00162             }                    
00163         }
00164         
00165     }
00166     
00167 private:
00168 };
00169 
00170 //------------------------------------------------------------------------------
00171 
00187 template<class SampleType>
00188 class LookupUnit
00189 {
00190 public:    
00191     typedef LookupChannelInternal<SampleType>           LookupInternal;
00192     typedef typename LookupInternal::Data               Data;
00193     typedef ChannelBase<SampleType>                     ChannelType;
00194     typedef ChannelInternal<SampleType,Data>            Internal;
00195     typedef UnitBase<SampleType>                        UnitType;
00196     typedef InputDictionary                             Inputs;
00197     typedef WavetableBase<SampleType>                   WavetableType;
00198     
00199     typedef typename LookupInternal::PositionType         PositionType;
00200     typedef typename LookupInternal::PositionUnitType     PositionUnitType;
00201     typedef typename LookupInternal::PositionBufferType   PositionBufferType;
00202     
00203     static inline UnitInfos getInfo() throw()
00204     {
00205         const double blockSize = (double)BlockSize::getDefault().getValue();
00206         const double sampleRate = SampleRate::getDefault().getValue();
00207         
00208         return UnitInfo ("Lookup", "A wavetable lookup generator",
00209                          
00210                          // output
00211                          ChannelCount::VariableChannelCount, 
00212                          IOKey::Generic,    Measure::None,                  0.0,                    IOLimit::None,
00213                          IOKey::End,
00214                          
00215                          // inputs
00216                          IOKey::Wavetable,  Measure::None,
00217                          IOKey::Position,   Measure::NormalisedUnipolar,    IOInfo::NoDefault,      IOLimit::None,
00218                          IOKey::Multiply,   Measure::Factor,                1.0,                    IOLimit::None,
00219                          IOKey::Add,        Measure::None,                  0.0,                    IOLimit::None,
00220                          IOKey::BlockSize,  Measure::Samples,               blockSize,              IOLimit::Minimum,   Measure::Samples,               1.0,
00221                          IOKey::SampleRate, Measure::Hertz,                 sampleRate,             IOLimit::Minimum,   Measure::Hertz,                 0.0,
00222                          IOKey::End);
00223     }
00224     
00226     static UnitType ar (WavetableType const& table,
00227                         PositionUnitType const& position,
00228                         UnitType const& mul = SampleType (1),
00229                         UnitType const& add = SampleType (0),
00230                         BlockSize const& preferredBlockSize = BlockSize::getDefault(),
00231                         SampleRate const& preferredSampleRate = SampleRate::getDefault()) throw()
00232     {             
00233         Inputs inputs;
00234         inputs.put (IOKey::Wavetable, table);
00235         inputs.put (IOKey::Time, position);
00236         inputs.put (IOKey::Multiply, mul);
00237         inputs.put (IOKey::Add, add);
00238                         
00239         Data data = { { -1.0, -1.0 } };
00240         
00241         return UnitType::template createFromInputs<LookupInternal> (inputs, 
00242                                                                     data,
00243                                                                     preferredBlockSize,
00244                                                                     preferredSampleRate);
00245     }
00246     
00248     static UnitType kr (WavetableType const& table,
00249                         PositionUnitType const& position,
00250                         UnitType const& mul = SampleType (1),
00251                         UnitType const& add = SampleType (0)) throw()
00252     {
00253         return ar (table, position, mul, add,
00254                    BlockSize::getControlRateBlockSize(), 
00255                    SampleRate::getControlRate());
00256     }        
00257 };
00258 
00259 typedef LookupUnit<PLONK_TYPE_DEFAULT> Lookup;
00260 
00261 
00262 #endif // PLONK_LOOKUP_H
00263 
 All Classes Functions Typedefs Enumerations Enumerator Properties