pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_TypeChannel.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_TYPECONVERTCHANNEL_H
00040 #define PLONK_TYPECONVERTCHANNEL_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00045 
00046 
00048 template<class OutputSampleType, class InputSampleType>
00049 class TypeChannelInternal
00050 :   public ChannelInternal<OutputSampleType,ChannelInternalCore::Data>
00051 {
00052 public:
00053     typedef typename ChannelInternalCore::Data          Data;
00054 
00055     typedef TypeChannelInternal<OutputSampleType,InputSampleType> TypeInternal;
00056     
00057     typedef ChannelBase<OutputSampleType>               OutputChannelType;
00058     typedef ChannelInternal<OutputSampleType,Data>      OutputChannelInternalType;
00059     typedef ChannelInternalBase<OutputSampleType>       OutputChannelInternalBase;
00060     typedef UnitBase<OutputSampleType>                  OutputUnitType;
00061     typedef NumericalArray<OutputSampleType>            OutputBuffer;
00062 
00063     typedef ChannelBase<InputSampleType>                InputChannelType;
00064     typedef ChannelInternal<InputSampleType,Data>       InputChannelInternalType;
00065     typedef UnitBase<InputSampleType>                   InputUnitType;
00066     typedef NumericalArray<InputSampleType>             InputBuffer;
00067             
00068     typedef InputDictionary                             Inputs;
00069     
00070     TypeChannelInternal (Inputs const& inputs, 
00071                          Data const& data, 
00072                          BlockSize const& blockSize,
00073                          SampleRate const& sampleRate) throw()
00074     :   OutputChannelInternalType (inputs, data, blockSize, sampleRate)
00075     {
00076     }
00077     
00078     Text getName() const throw()
00079     {        
00080         Text name ("Type");
00081         name += " (";
00082         name += TypeUtility<InputSampleType>::getTypeName();
00083         name += " to ";
00084         name += TypeUtility<OutputSampleType>::getTypeName();
00085         name += ")";
00086         return name;
00087     }    
00088     
00089     IntArray getInputKeys() const throw()
00090     {
00091         const IntArray keys (IOKey::Generic);
00092         return keys;
00093     }    
00094     
00095     bool isTypeConverter() const throw() 
00096     { 
00097         return true; 
00098     }
00099     
00100     const Text getInputTypeName() const throw()
00101     { 
00102         return TypeUtility<InputSampleType>::getTypeName(); 
00103     }
00104     
00105     int getInputTypeCode() const throw()
00106     { 
00107         return TypeUtility<InputSampleType>::getTypeCode(); 
00108     }
00109     
00110     InputUnitType getInputUnit() const throw()
00111     {
00112         const InputUnitType& input (this->template getInputAs<InputUnitType> (IOKey::Generic));
00113         return input;
00114     }
00115     
00116     OutputChannelInternalBase* getChannel (const int index) throw()
00117     {
00118         const Inputs channelInputs = this->getInputs().getChannel (index);
00119         return new TypeInternal (channelInputs, 
00120                                  this->getState(), 
00121                                  this->getBlockSize(), 
00122                                  this->getSampleRate());
00123     }                
00124     
00125     void initChannel (const int channel) throw()
00126     {        
00127         InputUnitType& input (this->template getInputAs<InputUnitType> (IOKey::Generic));
00128 
00129         this->setBlockSize (BlockSize::decide (input.getBlockSize (channel),
00130                                                this->getBlockSize()));
00131         this->setSampleRate (SampleRate::decide (input.getSampleRate (channel),
00132                                                  this->getSampleRate()));      
00133                 
00134         this->setOverlap (input.getOverlap (channel));
00135         
00136         InputSampleType sourceValue = input.getValue (channel);
00137         this->initValue (OutputSampleType (sourceValue));
00138     }    
00139     
00140     void process (ProcessInfo& info, const int channel) throw()
00141     {
00142         InputUnitType& input (this->template getInputAs<InputUnitType> (IOKey::Generic));
00143         const InputBuffer& inputBuffer (input.process (info, channel));
00144         
00145         OutputSampleType* const outputSamples = this->getOutputSamples();
00146         const int outputBufferLength = this->getOutputBuffer().length();
00147         
00148         const InputSampleType* const inputSamples = inputBuffer.getArray();
00149         const int inputBufferLength = inputBuffer.length();
00150         
00151         int i;
00152         
00153         if (inputBufferLength == outputBufferLength)
00154         {
00155             NumericalArrayConverter<OutputSampleType,InputSampleType>::convertDirect (outputSamples, inputSamples, outputBufferLength);            
00156         }
00157         else if (inputBufferLength == 1)
00158         {
00159             OutputSampleType temp;
00160             NumericalConverter::roundCopy (inputSamples[0], temp);
00161             NumericalArrayFiller<OutputSampleType>::fill (outputSamples, temp, (UnsignedLong)outputBufferLength);
00162         }
00163         else
00164         {
00165             double inputPosition = 0.0;
00166             const double inputIncrement = double (inputBufferLength) / double (outputBufferLength);
00167             
00168             for (i = 0; i < outputBufferLength; ++i) 
00169             {                
00170                 NumericalConverter::roundCopy (inputSamples[int (inputPosition)], outputSamples[i]);
00171                 inputPosition += inputIncrement;
00172             }        
00173         }
00174     }
00175     
00176 private:
00177 };
00178 
00179 
00180 
00195 template<class OutputSampleType, class InputSampleType>
00196 class TypeUnitBase
00197 {
00198 public:
00199     typedef typename ChannelInternalCore::Data  Data;
00200 
00201     typedef ChannelBase<OutputSampleType>           OutputChannelType;
00202     typedef ChannelInternal<OutputSampleType,Data>  OutputChannelInternalType;
00203     typedef ChannelInternalBase<OutputSampleType>   OutputChannelInternalBaseType;
00204     typedef UnitBase<OutputSampleType>              OutputUnitType;    
00205     typedef UnitBase<InputSampleType>               InputUnitType;
00206     typedef InputDictionary                         Inputs;
00207     
00208     typedef TypeChannelInternal<OutputSampleType,InputSampleType> TypeInternal;
00209     
00210     static inline UnitInfos getInfo() throw()
00211     {
00212         const double blockSize = (double)BlockSize::noPreference().getValue();
00213         const double sampleRate = SampleRate::noPreference().getValue();
00214 
00215         return UnitInfo ("Type", "Resamples signals to a different sample type.",
00216                          
00217                          // output
00218                          ChannelCount::VariableChannelCount, 
00219                          IOKey::Generic,    Measure::None,      IOInfo::NoDefault,  IOLimit::None,
00220                          IOKey::End,
00221 
00222                          // inputs
00223                          IOKey::Generic,    Measure::None,      IOInfo::NoDefault,  IOLimit::None,
00224                          IOKey::BlockSize,  Measure::Samples,   blockSize,          IOLimit::Minimum,   Measure::Samples,   1.0,
00225                          IOKey::SampleRate, Measure::Hertz,     sampleRate,         IOLimit::Minimum,   Measure::Hertz,     0.0,
00226                          IOKey::End);
00227     }    
00228     
00229 
00230     static OutputUnitType ar (InputUnitType const& input, 
00231                               BlockSize const& blockSize = BlockSize::noPreference(), 
00232                               SampleRate const& sampleRate = SampleRate::noPreference()) throw()
00233     {        
00234         Inputs inputs;
00235         inputs.put (IOKey::Generic, input);
00236         
00237         Data data = { -1.0, -1.0 };
00238         
00239         return OutputUnitType::template createFromInputs<TypeInternal> (inputs, 
00240                                                                         data, 
00241                                                                         blockSize, 
00242                                                                         sampleRate);
00243     }
00244     
00245     static inline OutputUnitType kr (InputUnitType const& input) throw()
00246     {
00247         return ar (input, 
00248                    BlockSize::getControlRateBlockSize(), 
00249                    SampleRate::getControlRate());
00250     }
00251 };
00252 
00253 
00254 template<class OutputSampleType, class InputSampleType>
00255 class TypeUnit : public TypeUnitBase<OutputSampleType,InputSampleType>
00256 {
00257 };
00258 
00259 
00260 // this provides quick returns for conversions to the same type of units
00261 
00262 #define PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(SampleType)\
00263     template<> class TypeUnit<SampleType,SampleType>\
00264     {\
00265     public:\
00266         typedef UnitBase<SampleType> UnitType;\
00267         \
00268         static inline UnitType ar (UnitType const& input) throw() {\
00269             return input;\
00270         }\
00271         \
00272         static inline UnitType ar (UnitType const& input, BlockSize const& blockSize) throw() {\
00273             return TypeUnitBase<SampleType,SampleType>::ar (input, blockSize, SampleRate::noPreference());\
00274         }\
00275         \
00276         static inline UnitType ar (UnitType const& input, BlockSize const& blockSize, SampleRate const& sampleRate) throw() {\
00277             return TypeUnitBase<SampleType,SampleType>::ar (input, blockSize, sampleRate);\
00278         }\
00279     }
00280 
00281 PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(float);
00282 PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(double);
00283 PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(int);
00284 PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(short);
00285 PLONK_TYPEUNIT_SAMEIOTYPES_DEFINE(Int24);
00286 
00287 
00288 
00289 #endif // PLONK_TYPECONVERTCHANNEL_H
 All Classes Functions Typedefs Enumerations Enumerator Properties