![]() |
pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
|
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_FILTER_H 00040 #define PLONK_FILTER_H 00041 00042 #include "../channel/plonk_ChannelInternalCore.h" 00043 #include "plonk_FilterForwardDeclarations.h" 00044 00045 00047 template<class FormType> 00048 class FilterChannelInternal 00049 : public ChannelInternal<typename FormType::SampleDataType, 00050 typename FormType::Data> 00051 { 00052 public: 00053 typedef typename FormType::SampleDataType SampleType; 00054 typedef typename FormType::Data Data; 00055 typedef ChannelBase<SampleType> ChannelType; 00056 typedef FilterChannelInternal<FormType> FilterInternal; 00057 typedef ChannelInternal<SampleType,Data> Internal; 00058 typedef ChannelInternalBase<SampleType> InternalBase; 00059 typedef UnitBase<SampleType> UnitType; 00060 typedef NumericalArray2D<ChannelType,UnitType> UnitsType; 00061 typedef InputDictionary Inputs; 00062 typedef NumericalArray<SampleType> Buffer; 00063 00064 FilterChannelInternal (Inputs const& inputs, 00065 Data const& data, 00066 BlockSize const& blockSize, 00067 SampleRate const& sampleRate) throw() 00068 : Internal (inputs, data, blockSize, sampleRate) 00069 { 00070 } 00071 00072 Text getName() const throw() 00073 { 00074 return "Filter (" + FormType::getName() + ")"; 00075 } 00076 00077 IntArray getInputKeys() const throw() 00078 { 00079 const IntArray keys (IOKey::Generic, IOKey::Coeffs); 00080 return keys; 00081 } 00082 00083 InternalBase* getChannel (const int index) throw() 00084 { 00085 Inputs channelInputs; 00086 00087 channelInputs.put (IOKey::Generic, 00088 this->getInputAsUnit (IOKey::Generic).getChannel (index)); 00089 00090 const UnitType& coeffs = this->getInputAsUnit (IOKey::Coeffs); 00091 UnitsType groupedCoeffs = coeffs.group (FormType::NumCoeffs); 00092 UnitType channelCoeffs = groupedCoeffs.wrapAt (index); 00093 00094 channelInputs.put (IOKey::Coeffs, 00095 channelCoeffs); 00096 00097 return new FilterChannelInternal (channelInputs, 00098 this->getState(), 00099 this->getBlockSize(), 00100 this->getSampleRate()); 00101 } 00102 00103 void initChannel (const int channel) throw() 00104 { 00105 const UnitType& inputUnit = this->getInputAsUnit (IOKey::Generic); 00106 00107 this->setBlockSize (BlockSize::decide (inputUnit.getBlockSize (channel), 00108 this->getBlockSize())); 00109 this->setSampleRate (SampleRate::decide (inputUnit.getSampleRate (channel), 00110 this->getSampleRate())); 00111 00112 this->setOverlap (inputUnit.getOverlap (channel)); 00113 00114 this->initValue (SampleType (0)); 00115 } 00116 00117 void process (ProcessInfo& info, const int channel) throw() 00118 { 00119 FormType::process (this->getOutputSamples(), 00120 this->getOutputBuffer().length(), 00121 this->getInputAsUnit (IOKey::Generic), 00122 this->getInputAsUnit (IOKey::Coeffs), 00123 this->getState(), 00124 info, 00125 channel); 00126 } 00127 00128 private: 00129 }; 00130 00131 //------------------------------------------------------------------------------ 00132 00137 template<class FormType> 00138 class FilterUnit 00139 { 00140 public: 00141 typedef typename FormType::SampleDataType SampleType; 00142 00143 typedef FilterChannelInternal<FormType> FilterInternal; 00144 typedef typename FilterInternal::Data Data; 00145 typedef ChannelBase<SampleType> ChannelType; 00146 typedef ChannelInternal<SampleType,Data> Internal; 00147 typedef ChannelInternalBase<SampleType> ChannelInternalType; 00148 typedef UnitBase<SampleType> UnitType; 00149 typedef NumericalArray2D<ChannelType,UnitType> UnitsType; 00150 typedef InputDictionary Inputs; 00151 00152 static inline UnitInfos getInfo() throw() 00153 { 00154 const double blockSize = (double)BlockSize::noPreference().getValue(); 00155 const double sampleRate = SampleRate::noPreference().getValue(); 00156 00157 return UnitInfo ("Filter", "A generic IIR filter.", 00158 00159 // output 00160 ChannelCount::VariableChannelCount, 00161 IOKey::Generic, Measure::None, 0.0, IOLimit::None, 00162 IOKey::End, 00163 00164 // inputs 00165 IOKey::Generic, Measure::None, IOInfo::NoDefault, IOLimit::None, 00166 IOKey::Coeffs, Measure::Coeffs, IOInfo::NoDefault, IOLimit::None, 00167 IOKey::Multiply, Measure::Factor, 1.0, IOLimit::None, 00168 IOKey::Add, Measure::None, 0.0, IOLimit::None, 00169 IOKey::BlockSize, Measure::Samples, blockSize, IOLimit::Minimum, Measure::Samples, 1.0, 00170 IOKey::SampleRate, Measure::Hertz, sampleRate, IOLimit::Minimum, Measure::Hertz, 0.0, 00171 IOKey::End); 00172 } 00173 00175 static UnitType ar (UnitType const& input, 00176 UnitType const& coeffs, 00177 UnitType const& mul = SampleType (1), 00178 UnitType const& add = SampleType (0), 00179 BlockSize const& preferredBlockSize = BlockSize::noPreference(), 00180 SampleRate const& preferredSampleRate = SampleRate::noPreference()) throw() 00181 { 00182 // re: full templating - could say that filters are only supported with float or double types? 00183 00184 const int requiredCoeffs = FormType::NumCoeffs; 00185 const int numCoeffChannels = coeffs.getNumChannels(); 00186 00187 plonk_assert (numCoeffChannels > 0); 00188 plonk_assert ((numCoeffChannels % requiredCoeffs) == 0); 00189 00190 const int numOutputChannels = plonk::max (input.getNumChannels(), numCoeffChannels / requiredCoeffs); 00191 UnitType result (UnitType::withSize (numOutputChannels)); 00192 00193 Data data; 00194 Memory::zero (data); 00195 data.base.sampleRate = -1.0; 00196 data.base.sampleDuration = -1.0; 00197 00198 for (int i = 0; i < numOutputChannels; ++i) 00199 { 00200 Inputs inputs; 00201 inputs.put (IOKey::Generic, input); 00202 inputs.put (IOKey::Coeffs, coeffs); 00203 00204 ChannelInternalType* internal = new FilterInternal (inputs, 00205 data, 00206 preferredBlockSize, 00207 preferredSampleRate); 00208 internal->initChannel (i); 00209 00210 result.put (i, ChannelType (internal)); 00211 } 00212 00213 return UnitType::applyMulAdd (result, mul, add); 00214 } 00215 00216 }; 00217 00218 00219 typedef FilterForm<PLONK_TYPE_DEFAULT,FilterFormType::P2Z2> FilterFormP2Z2; 00220 typedef FilterUnit<FilterFormP2Z2> FilterP2Z2; 00221 00222 00223 00224 00225 #endif // PLONK_FILTER_H 00226