pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_LinearPanChannel.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_LINEARPAN_H
00040 #define PLONK_LINEARPAN_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "../plonk_GraphForwardDeclarations.h"
00044 
00045 
00048 template<class SampleType>
00049 class LinearPanChannelInternal 
00050 :   public ProxyOwnerChannelInternal<SampleType, ChannelInternalCore::Data>
00051 {
00052 public:
00053     typedef typename ChannelInternalCore::Data              Data;
00054 
00055     typedef ChannelBase<SampleType>                         ChannelType;
00056     typedef ObjectArray<ChannelType>                        ChannelArrayType;
00057     typedef ProxyOwnerChannelInternal<SampleType,Data>      Internal;
00058     typedef UnitBase<SampleType>                            UnitType;
00059     typedef InputDictionary                                 Inputs;
00060     typedef NumericalArray<SampleType>                      Buffer;
00061 
00062     enum OutputIndices { LeftOutput, RightOutput, NumOutputs };
00063     
00064     LinearPanChannelInternal (Inputs const& inputs, 
00065                               Data const& data, 
00066                               BlockSize const& blockSize,
00067                               SampleRate const& sampleRate,
00068                               ChannelArrayType& channels) throw()
00069     :   Internal (NumOutputs, inputs, data, blockSize, sampleRate, channels)
00070     {
00071     }
00072         
00073     Text getName() const throw()
00074     {
00075         return "Linear Pan";
00076     }    
00077     
00078     IntArray getInputKeys() const throw()
00079     {
00080         const IntArray keys (IOKey::Generic,
00081                              IOKey::Position);
00082         return keys;
00083     }    
00084     
00085     void initChannel (const int channel) throw()
00086     {        
00087         const UnitType& input = this->getInputAsUnit (IOKey::Generic);
00088         const UnitType& position = this->getInputAsUnit (IOKey::Position);
00089                 
00090         if ((channel % this->getNumChannels()) == 0)
00091         {
00092             // use sample rate and block size of the input unless another 
00093             // preference has been indicated on construction
00094             this->setBlockSize (BlockSize::decide (input.getBlockSize (0),
00095                                                    this->getBlockSize()));
00096             this->setSampleRate (SampleRate::decide (input.getSampleRate (0),
00097                                                      this->getSampleRate()));      
00098         }
00099         
00100         plonk_assert (input.getOverlap (0) == Math<DoubleVariable>::get1());
00101         plonk_assert (position.getOverlap (0) == Math<DoubleVariable>::get1());
00102 
00103         const SampleType inputValue = input.getValue (0);        
00104         const SampleType mulAdd (0.5);
00105         const SampleType rightLevel = position.getValue (0) * mulAdd + mulAdd;
00106         
00107         SampleType value (0);
00108         
00109         switch (channel)
00110         {
00111             case 0: {
00112                 const SampleType leftLevel = SampleType (1.0) - rightLevel;
00113                 value = inputValue * leftLevel;
00114             } break;
00115             case 1: {
00116                 value = inputValue * rightLevel;
00117             } break;
00118             default: {
00119                 plonk_assertfalse;
00120             } break;
00121         }
00122         
00123         this->initProxyValue (channel, value);
00124     }    
00125     
00126     void process (ProcessInfo& info, const int /*channel*/) throw()
00127     {                
00128         UnitType& inputUnit (this->getInputAsUnit (IOKey::Generic));
00129         UnitType& positionUnit (this->getInputAsUnit (IOKey::Position));
00130         
00131         const Buffer& inputBuffer (inputUnit.process (info, 0));
00132         const Buffer& positionBuffer (positionUnit.process (info, 0));
00133         
00134         const SampleType mulAdd (0.5);
00135         const SampleType rightLevel = positionBuffer.atUnchecked (0) * mulAdd + mulAdd;
00136         const SampleType leftLevel = SampleType (1.0) - rightLevel;
00137         
00138         const SampleType* const inputSamples = inputBuffer.getArray();
00139         const int inputBufferLength = inputBuffer.length();
00140 
00141         SampleType* const leftOutputSamples = this->getOutputSamples (LeftOutput);
00142         SampleType* const rightOutputSamples = this->getOutputSamples (RightOutput);
00143         const int outputBufferLength = this->getOutputBuffer (0).length();
00144                 
00145         int i;
00146         
00147         if (inputBufferLength == outputBufferLength)
00148         {        
00149             for (i = 0; i < outputBufferLength; ++i)
00150             {
00151                 const SampleType inputValue = inputSamples[i];
00152                 leftOutputSamples[i] = leftLevel * inputValue;
00153                 rightOutputSamples[i] = rightLevel * inputValue;
00154             }
00155         }
00156         else 
00157         {
00158             double inputPosition = 0.0;
00159             const double inputIncrement = double (inputBufferLength) / double (outputBufferLength);
00160 
00161             for (i = 0; i < outputBufferLength; ++i)
00162             {
00163                 const SampleType inputValue = inputSamples[int(inputPosition)];
00164                 
00165                 leftOutputSamples[i] = leftLevel * inputValue;
00166                 rightOutputSamples[i] = rightLevel * inputValue;
00167                 
00168                 inputPosition += inputIncrement;
00169             }
00170         }
00171     }    
00172 };
00173 
00174 
00175 
00176 //------------------------------------------------------------------------------
00177 
00190 template<class SampleType>
00191 class LinearPanUnit
00192 {
00193 public:
00194     typedef typename ChannelInternalCore::Data      Data;
00195 
00196     typedef LinearPanChannelInternal<SampleType>    LinearPanInternal;
00197     typedef ChannelBase<SampleType>                 ChannelType;
00198     typedef ChannelInternal<SampleType,Data>        Internal;
00199     typedef UnitBase<SampleType>                    UnitType;
00200     typedef InputDictionary                         Inputs;
00201     
00202     static inline UnitInfos getInfo() throw()
00203     {
00204         const double blockSize = (double)BlockSize::noPreference().getValue();
00205         const double sampleRate = SampleRate::noPreference().getValue();
00206         const double peak = (double)TypeUtility<SampleType>::getTypePeak(); // will be innaccurate for LongLong
00207 
00208         return UnitInfo ("LinearPan", "Pan a mono signal across two channels using a linear pan.",
00209                         
00210                          // outputs
00211                          2, 
00212                          IOKey::LeftOperand,    Measure::None,          IOInfo::NoDefault,  IOLimit::None,
00213                          IOKey::RightOperand,   Measure::None,          IOInfo::NoDefault,  IOLimit::None,
00214                          IOKey::End,
00215                          
00216                          // inputs
00217                          IOKey::Generic,    Measure::None,              IOInfo::NoDefault,  IOLimit::None,
00218                          IOKey::Position,   Measure::NormalisedBipolar, 0.0,                IOLimit::Clipped, Measure::NormalisedBipolar,  -peak, peak,
00219                          IOKey::BlockSize,  Measure::Samples,           blockSize,          IOLimit::Minimum, Measure::Samples,             1.0,
00220                          IOKey::SampleRate, Measure::Hertz,             sampleRate,         IOLimit::Minimum, Measure::Hertz,               0.0,
00221                          IOKey::End);
00222     }        
00223     
00224     static UnitType ar (UnitType const& input,
00225                         UnitType const& position = SampleType (0),
00226                         BlockSize const& preferredBlockSize = BlockSize::noPreference(),
00227                         SampleRate const& preferredSampleRate = SampleRate::noPreference()) throw()
00228     {               
00229         /*
00230          for full templating we'd need to add template params for position unit types possibly not for this type though
00231          */
00232 
00233         const int numInputChannels = plonk::max (input.getNumChannels(), position.getNumChannels());
00234         UnitType result (UnitType::emptyWithAllocatedSize (numInputChannels * 2));
00235         Data data = { -1.0, -1.0 };
00236 
00237         for (int i = 0; i < numInputChannels; ++i)
00238         {
00239             Inputs inputs;
00240             inputs.put (IOKey::Generic, input[i]);
00241             inputs.put (IOKey::Position, position[i]);
00242 
00243             result.add (UnitType::template proxiesFromInputs<LinearPanInternal> (inputs, 
00244                                                                                  data, 
00245                                                                                  preferredBlockSize, 
00246                                                                                  preferredSampleRate));
00247         }
00248         
00249         return result;
00250     }
00251         
00252 };
00253 
00254 typedef LinearPanUnit<PLONK_TYPE_DEFAULT> LinearPan;
00255 
00256 
00257 #endif // PLONK_LINEARPAN_H
 All Classes Functions Typedefs Enumerations Enumerator Properties