pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_BinaryOpPlink.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_BINARYOPPLINK_H
00040 #define PLONK_BINARYOPPLINK_H
00041 
00042 #include "plonk_BinaryOpChannel.h"
00043 
00044 #define PLONK_PLINK_BINARYOPCHANNEL_COMMON_START(PLONKOP) \
00045     template<>\
00046     class BinaryOpChannelInternal<float, BinaryOpFunctionsHelper<float>::BinaryOpFunctionsType::PLONKOP>\
00047     : public ChannelInternal<float, ChannelInternalCore::Data>\
00048     {\
00049     public:\
00050         typedef ChannelInternalCore::Data                                       Data;\
00051         typedef BinaryOpFunctionsHelper<float>::BinaryOpFunctionsType           BinaryOpFunctionsType;\
00052         typedef ChannelBase<float>                                              ChannelType;\
00053         typedef BinaryOpChannelInternal<float,BinaryOpFunctionsType::PLONKOP>   BinaryOpInternal;\
00054         typedef ChannelInternal<float,Data>                                     Internal;\
00055         typedef ChannelInternalBase<float>                                      InternalBase;\
00056         typedef UnitBase<float>                                                 UnitType;\
00057         typedef InputDictionary                                                 Inputs;\
00058         typedef NumericalArray<float>                                           Buffer;\
00059         typedef BinaryOpUtility<float>                                          UtilityType;\
00060         \
00061         enum InputIndices { LeftOperand, RightOperand, NumInputs };\
00062         enum Outputs { Output, NumOutputs };\
00063         enum Buffers { OutputBuffer, LeftOperandBuffer, RightOperandBuffer, NumBuffers };\
00064         typedef PlinkProcess<NumBuffers> Process;\
00065         \
00066         BinaryOpChannelInternal (Inputs const& inputs, Data const& data,\
00067                                  BlockSize const& blockSize, SampleRate const& sampleRate) throw()\
00068         :   Internal (inputs, data, blockSize, sampleRate)\
00069         {\
00070             plonk_staticassert (NumBuffers == (NumInputs + NumOutputs));\
00071             Process::init (&p, this, NumOutputs, NumInputs);\
00072         }
00073         
00074 
00075 #define PLONK_PLINK_BINARYOPCHANNEL_COMMON_END\
00076     private:\
00077         Process p;\
00078     };
00079 
00080 #define PLONK_PLINK_BINARYOPCHANNEL_FUNCTION(PLANKOP,TYPECODE) plink_BinaryOpProcess##PLANKOP##TYPECODE
00081 
00082 #define PLONK_PLINK_BINARYOPCHANNEL_PROCESS(PLONKOP, PLANKOP) \
00083     Text getName() const throw() {\
00084         Text variant = UtilityType::global().getName (BinaryOpFunctionsType::PLONKOP);\
00085         if (variant.length() < 1) variant = "unknown type";\
00086         return "Binary Operator (" + variant + ")";\
00087     }\
00088     \
00089     IntArray getInputKeys() const throw() {\
00090         const IntArray keys (IOKey::LeftOperand, IOKey::RightOperand);\
00091         return keys;\
00092     }\
00093     \
00094     InternalBase* getChannel (const int index) throw() {\
00095         const Inputs channelInputs = this->getInputs().getChannel (index);\
00096         return new BinaryOpInternal (channelInputs, this->getState(), this->getBlockSize(), this->getSampleRate());\
00097     }\
00098     \
00099     void initChannel (const int channel) throw() {\
00100         const UnitType& leftUnit = this->getInputAsUnit (IOKey::LeftOperand);\
00101         const float leftValue = leftUnit.getValue (channel);\
00102         const UnitType& rightUnit = this->getInputAsUnit (IOKey::RightOperand);\
00103         const float rightValue = rightUnit.getValue (channel);\
00104         this->setBlockSize (BlockSize::decide (leftUnit.getBlockSize (channel).selectMax (rightUnit.getBlockSize (channel)),\
00105                                                this->getBlockSize()));\
00106         this->setSampleRate (SampleRate::decide (leftUnit.getSampleRate (channel).selectMax (rightUnit.getSampleRate (channel)),\
00107                                                  this->getSampleRate()));\
00108         const DoubleVariable leftOverlap = leftUnit.getOverlap (channel);\
00109         const DoubleVariable rightOverlap = rightUnit.getOverlap (channel);\
00110         if (leftOverlap == rightOverlap)            this->setOverlap (leftOverlap);\
00111         else if (leftUnit.isConstant (channel))     this->setOverlap (rightOverlap);\
00112         else if (rightUnit.isConstant (channel))    this->setOverlap (leftOverlap);\
00113         else plonk_assertfalse;\
00114         this->initValue (pl_##PLANKOP##F (leftValue, rightValue));\
00115     }\
00116     \
00117     void process (ProcessInfo& info, const int channel) throw() {\
00118         UnitType& leftUnit (this->getInputAsUnit (IOKey::LeftOperand));\
00119         UnitType& rightUnit (this->getInputAsUnit  (IOKey::RightOperand));\
00120         \
00121         const Buffer& leftBuffer (leftUnit.process (info, channel));\
00122         const Buffer& rightBuffer (rightUnit.process (info, channel));\
00123         \
00124         p.buffers[0].bufferSize = this->getOutputBuffer().length();\
00125         p.buffers[0].buffer = this->getOutputSamples();\
00126         p.buffers[1].bufferSize = leftBuffer.length();\
00127         p.buffers[1].buffer = leftBuffer.getArray();\
00128         p.buffers[2].bufferSize = rightBuffer.length();\
00129         p.buffers[2].buffer = rightBuffer.getArray();\
00130         \
00131         PLONK_PLINK_BINARYOPCHANNEL_FUNCTION(PLANKOP,F) (&p,0);\
00132     }
00133 
00134 #define PLONK_PLINK_BINARYOPCHANNEL(PLONKOP, PLANKOP)\
00135     PLONK_PLINK_BINARYOPCHANNEL_COMMON_START(PLONKOP)\
00136     PLONK_PLINK_BINARYOPCHANNEL_PROCESS(PLONKOP, PLANKOP)\
00137     PLONK_PLINK_BINARYOPCHANNEL_COMMON_END
00138 
00139 PLONK_PLINK_BINARYOPCHANNEL(addop, Add);
00140 PLONK_PLINK_BINARYOPCHANNEL(subop, Sub);
00141 PLONK_PLINK_BINARYOPCHANNEL(mulop, Mul);
00142 PLONK_PLINK_BINARYOPCHANNEL(divop, Div);
00143 PLONK_PLINK_BINARYOPCHANNEL(modop, Mod);
00144 PLONK_PLINK_BINARYOPCHANNEL(min, Min);
00145 PLONK_PLINK_BINARYOPCHANNEL(max, Max);
00146 PLONK_PLINK_BINARYOPCHANNEL(pow, Pow);
00147 PLONK_PLINK_BINARYOPCHANNEL(isEqualTo, IsEqualTo);
00148 PLONK_PLINK_BINARYOPCHANNEL(isNotEqualTo, IsNotEqualTo);
00149 PLONK_PLINK_BINARYOPCHANNEL(isGreaterThan, IsGreaterThan);
00150 PLONK_PLINK_BINARYOPCHANNEL(isGreaterThanOrEqualTo, IsGreaterThanOrEqualTo);
00151 PLONK_PLINK_BINARYOPCHANNEL(isLessThan, IsLessThan);
00152 PLONK_PLINK_BINARYOPCHANNEL(isLessThanOrEqualTo, IsLessThanOrEqualTo);
00153 PLONK_PLINK_BINARYOPCHANNEL(hypot, Hypot);
00154 PLONK_PLINK_BINARYOPCHANNEL(atan2, Atan2);
00155 PLONK_PLINK_BINARYOPCHANNEL(sumsqr, SumSqr);
00156 PLONK_PLINK_BINARYOPCHANNEL(difsqr, DifSqr);
00157 PLONK_PLINK_BINARYOPCHANNEL(sqrsum, SqrSum);
00158 PLONK_PLINK_BINARYOPCHANNEL(sqrdif, SqrDif);
00159 PLONK_PLINK_BINARYOPCHANNEL(absdif, AbsDif);
00160 PLONK_PLINK_BINARYOPCHANNEL(thresh, Thresh);
00161 
00162 
00163 #endif // PLONK_BINARYOPPLINK_H
 All Classes Functions Typedefs Enumerations Enumerator Properties