pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_FilterCoeffs1Param.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_FILTERCOEFFS1PARAM_H
00040 #define PLONK_FILTERCOEFFS1PARAM_H
00041 
00042 #include "../channel/plonk_ChannelInternalCore.h"
00043 #include "plonk_FilterForwardDeclarations.h"
00044 
00045 
00046 
00048 template<class ShapeType>
00049 class FilterCoeffs1ParamChannelInternal 
00050 :   public ProxyOwnerChannelInternal<typename ShapeType::SampleDataType, 
00051                                      typename ShapeType::Data>
00052 {
00053 public:
00054     typedef typename ShapeType::SampleDataType                  SampleType;
00055     typedef typename ShapeType::Data                            Data;
00056     typedef typename ShapeType::FormType                        FormType;
00057     
00058     typedef ChannelBase<SampleType>                             ChannelType;
00059     typedef ObjectArray<ChannelType>                            ChannelArrayType;
00060         
00061     typedef ProxyOwnerChannelInternal<SampleType,Data>          Internal;
00062     typedef UnitBase<SampleType>                                UnitType;
00063     typedef InputDictionary                                     Inputs;
00064     typedef NumericalArray<SampleType>                          Buffer;
00065         
00066     FilterCoeffs1ParamChannelInternal (Inputs const& inputs, 
00067                                        Data const& data, 
00068                                        BlockSize const& blockSize,
00069                                        SampleRate const& sampleRate,
00070                                        ChannelArrayType& channels) throw()
00071     :   Internal (FormType::NumCoeffs, inputs, data, blockSize, sampleRate, channels),
00072         inputKeys (ShapeType::getInputKeys())
00073     {
00074         plonk_assert (ShapeType::NumParams == 1);
00075 
00076         SampleRate& filterSampleRate = this->getInputAsSampleRate (IOKey::FilterSampleRate);
00077         filterSampleRate.addReceiver (this);
00078         
00079         updateFilterSampleRateInData();
00080     }
00081     
00082     ~FilterCoeffs1ParamChannelInternal()
00083     {
00084         SampleRate& filterSampleRate = this->getInputAsSampleRate (IOKey::FilterSampleRate);
00085         filterSampleRate.removeReceiver (this);
00086     }
00087             
00088     Text getName() const throw()
00089     {
00090         return "Filter Coeffs 1 Param (" + ShapeType::getFormName() + " / " + ShapeType::getShapeName() + ")";
00091     }       
00092     
00093     IntArray getInputKeys() const throw()
00094     {
00095         return inputKeys;
00096     }    
00097         
00098     void initChannel (const int channel) throw()
00099     {        
00100         plonk_assert (ShapeType::NumParams == 1);
00101 
00102         UnitType& param0Unit = this->getInputAsUnit (inputKeys.atUnchecked (0));
00103         plonk_assert (param0Unit.getOverlap (0) == Math<DoubleVariable>::get1());
00104 
00105         if ((channel % this->getNumChannels()) == 0)
00106         {
00107             this->setBlockSize (BlockSize::decide (param0Unit.getBlockSize (0),
00108                                                    this->getBlockSize()));
00109             this->setSampleRate (SampleRate::decide (param0Unit.getSampleRate (0),
00110                                                      this->getSampleRate()));
00111             
00112             Data& data = this->getState();
00113             data.params[0] = param0Unit.getValue (0);
00114             
00115             ShapeType::calculate (data);
00116             
00117             for (int i = 0; i < FormType::NumCoeffs; ++i)
00118                 this->initProxyValue (i, data.coeffs[i]);            
00119         }
00120     }    
00121     
00122     void changed (DoubleVariable::Sender const& source, Text const& message, Dynamic const& payload) throw()
00123     {        
00124         SampleRate sampleRateSource = static_cast<SampleRate> (source);
00125         
00126         if (sampleRateSource == this->getInputAsSampleRate (IOKey::FilterSampleRate))
00127         {
00128             this->updateFilterSampleRateInData();
00129             return;
00130         }
00131                 
00132         Internal::changed (source, message, payload); // process others
00133     }    
00134     
00135     void updateFilterSampleRateInData() throw()
00136     {
00137         const SampleRate& filterSampleRate = this->getInputAsSampleRate (IOKey::FilterSampleRate);
00138         Data& data = this->getState();
00139 
00140         const double sampleRate = filterSampleRate.getValue();
00141         
00142         data.filterSampleRate = sampleRate;
00143         data.filterSampleDuration = 1.0 / sampleRate;
00144     }
00145     
00146     void process (ProcessInfo& info, const int /*channel*/) throw()
00147     {   
00148         int i, j;
00149 
00150         Data& data = this->getState();
00151                             
00152         const int outputLength = this->getOutputBuffer (0).length();
00153             
00154         UnitType& param0Unit = this->getInputAsUnit (inputKeys.atUnchecked (0));
00155         
00156         const Buffer& param0Buffer (param0Unit.process (info, 0));
00157         const SampleType* const param0Samples = param0Buffer.getArray();
00158         const int param0Length = param0Buffer.length();
00159         
00160         if (outputLength == param0Length)
00161         {
00162             for (i = 0; i < outputLength; ++i)
00163             {
00164                 data.params[0] = param0Samples[i];
00165                 
00166                 ShapeType::calculate (data);
00167                 
00168                 for (j = 0; j < FormType::NumCoeffs; ++j)
00169                     this->getOutputSamples (j) [i] = data.coeffs[j];
00170             }
00171         }
00172         else if (param0Length == 1)
00173         {
00174             data.params[0] = param0Samples[0];
00175 
00176             ShapeType::calculate (data);
00177 
00178             for (j = 0; j < FormType::NumCoeffs; ++j)
00179             {
00180                 SampleType* const samples = this->getOutputSamples (j);
00181                 const SampleType coeff = data.coeffs[j];
00182                 
00183                 for (i = 0; i < outputLength; ++i)
00184                     samples[i] = coeff;                
00185             }            
00186         }
00187         else
00188         {
00189             double param0Position = 0.0;
00190             const double param0Increment = double (param0Length) / double (outputLength);
00191             
00192             for (i = 0; i < outputLength; ++i)
00193             {
00194                 data.params[0] = param0Samples[int (param0Position)];
00195                 
00196                 ShapeType::calculate (data);
00197                 
00198                 for (j = 0; j < FormType::NumCoeffs; ++j)
00199                     this->getOutputSamples (j) [i] = data.coeffs[j];
00200                 
00201                 param0Position += param0Increment;
00202             }
00203         }
00204     }
00205     
00206 private:
00207     IntArray inputKeys;
00208 };
00209 
00210 
00211 
00212 
00213 
00216 template<class ShapeType>
00217 class FilterCoeffs1ParamUnit
00218 {
00219 public:        
00220     typedef typename ShapeType::SampleDataType              SampleType;
00221     typedef typename ShapeType::Data                        Data;
00222     typedef typename ShapeType::FormType                    FormType;
00223     
00224     typedef FilterCoeffs1ParamChannelInternal<ShapeType>    FilterCoeffsInternal;
00225     typedef ChannelBase<SampleType>                         ChannelType;
00226     typedef ChannelInternal<SampleType,Data>                Internal;
00227     typedef UnitBase<SampleType>                            UnitType;
00228     typedef InputDictionary                                 Inputs;
00229     
00230     static inline UnitInfos getInfo() throw()
00231     {
00232         const double sampleRate = SampleRate::getDefault().getValue();
00233         
00234         return UnitInfo ("Filter Coeffs 1 Param", "Filter coefficients generator for an IIR filter using one control parameter.",
00235                          
00236                          // output
00237                          FormType::NumCoeffs, 
00238                          IOKey::Coeffs,             Measure::Coeffs,    0.0,                IOLimit::None,
00239                          IOKey::End,
00240                          
00241                          // inputs
00242                          IOKey::Generic,            Measure::Unknown,   IOInfo::NoDefault,  IOLimit::None,
00243                          IOKey::FilterSampleRate,   Measure::Hertz,     sampleRate,         IOLimit::Minimum,   Measure::Hertz,             0.0,
00244                          IOKey::BlockSize,          Measure::Samples,   0.0,                IOLimit::Minimum,   Measure::Samples,           1.0,
00245                          IOKey::SampleRate,         Measure::Hertz,     -1.0,               IOLimit::Minimum,   Measure::Hertz,             0.0,
00246                          IOKey::End);
00247     }
00248     
00249     
00251     static UnitType ar (UnitType const& param0,
00252                         SampleRates const filterSampleRates = SampleRate::getDefault(),
00253                         BlockSize const& preferredBlockSize = BlockSize::noPreference(),
00254                         SampleRate const& preferredSampleRate = SampleRate::noPreference()) throw()
00255     {                
00256         const IntArray inputKeys = ShapeType::getInputKeys();
00257         
00258         plonk_assert (inputKeys.length() == 1);
00259         
00260         const int numInputChannels = param0.getNumChannels();
00261         const int numSampleRates = filterSampleRates.length();
00262         const int numChannels = filterSampleRates.areAllEqual() ? numInputChannels : plonk::max (numInputChannels, numSampleRates);
00263         UnitType result (UnitType::emptyWithAllocatedSize (numChannels * FormType::NumCoeffs));
00264         
00265         Data data;
00266         Memory::zero (data);
00267         data.base.sampleRate = -1.0;
00268         data.base.sampleDuration = -1.0;
00269         
00270         for (int i = 0; i < numChannels; ++i)
00271         {
00272             Inputs inputs;
00273             inputs.put (inputKeys.atUnchecked (0), param0[i]);
00274             inputs.put (IOKey::FilterSampleRate, filterSampleRates.wrapAt (i));
00275             
00276             result.add (UnitType::template proxiesFromInputs<FilterCoeffsInternal> (inputs, 
00277                                                                                     data, 
00278                                                                                     preferredBlockSize, 
00279                                                                                     preferredSampleRate));
00280         }
00281         
00282         return result;
00283     }
00284 };
00285 
00286 
00287 
00288 
00289 #endif // PLONK_FILTERCOEFFS1PARAM_H
00290 
 All Classes Functions Typedefs Enumerations Enumerator Properties