![]() |
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_DELAYFORMALLPASSDECAY_H 00040 #define PLONK_DELAYFORMALLPASSDECAY_H 00041 00042 #include "../channel/plonk_ChannelInternalCore.h" 00043 #include "plonk_DelayForwardDeclarations.h" 00044 00045 template<class SampleType, Interp::TypeCode InterpTypeCode> 00046 class DelayFormAllpassDecay 00047 : public DelayFormAllpassFFFB<SampleType> 00048 { 00049 public: 00050 typedef DelayFormAllpassFFFB<SampleType> Base; 00051 00052 enum InParams 00053 { 00054 DurationIn, 00055 DecayIn, 00056 NumInParams 00057 }; 00058 00059 typedef typename Base::Data Data; 00060 typedef typename Data::DelayState DelayState; 00061 typedef DelayFormAllpassDecay FormType; 00062 00063 typedef SampleType SampleDataType; 00064 typedef Delay2ParamChannelInternal<FormType> DelayInternal; 00065 typedef ChannelBase<SampleType> ChannelType; 00066 typedef ChannelInternal<SampleType,Data> Internal; 00067 typedef UnitBase<SampleType> UnitType; 00068 typedef InputDictionary Inputs; 00069 typedef NumericalArray<SampleType> Buffer; 00070 00071 typedef typename TypeUtility<SampleType>::IndexType Param1Type; 00072 typedef Param1Type DurationType; 00073 typedef UnitBase<DurationType> DurationUnitType; 00074 00075 typedef typename TypeUtility<SampleType>::IndexType Param2Type; 00076 typedef Param2Type DecayType; 00077 typedef UnitBase<DecayType> DecayUnitType; 00078 00079 typedef InterpSelect<SampleType,DurationType,InterpTypeCode> InterpSelectType; 00080 typedef typename InterpSelectType::InterpType InterpType; 00081 typedef typename InterpType::ExtensionBuffer ExtensionBuffer; 00082 00083 00084 typedef void (*InputFunction) (Data&, DelayState&); 00085 typedef void (*ReadFunction) (Data&, DelayState&); 00086 typedef void (*WriteFunction) (Data&, DelayState&); 00087 typedef void (*OutputFunction) (Data&, DelayState&); 00088 typedef void (*Param1Function) (Data&, DelayState&, Param1Type const&); 00089 typedef void (*Param2Function) (Data&, DelayState&, Param2Type const&); 00090 00091 static inline IntArray getInputKeys() throw() 00092 { 00093 const IntArray keys (IOKey::Generic, IOKey::Duration, IOKey::Decay); 00094 return keys; 00095 } 00096 00097 static inline void param1Process (Data& data, DelayState& state, DurationType const& duration) throw() 00098 { 00099 if (state.paramsIn[DurationIn] != duration) 00100 { 00101 state.paramsIn[DurationIn] = duration; 00102 state.paramsOut[Base::DurationInSamplesOut] = plonk::max (DurationType (1), DurationType (duration * data.base.sampleRate)); 00103 plonk_assert (state.paramsOut[Base::DurationInSamplesOut] > 0 && 00104 state.paramsOut[Base::DurationInSamplesOut] <= state.bufferLengthIndex); 00105 state.paramsOut[Base::CoeffOut] = plonk::decayFeedback (state.paramsIn[DurationIn], state.paramsIn[DecayIn]); 00106 } 00107 } 00108 00109 static inline void param2Ignore (Data&, DelayState&, DecayType const&) throw() { } 00110 static inline void param2Process (Data&, DelayState& state, DecayType const& decay) throw() 00111 { 00112 if (state.paramsIn[DecayIn] != decay) 00113 { 00114 state.paramsIn[DecayIn] = decay; 00115 state.paramsOut[Base::CoeffOut] = plonk::decayFeedback (state.paramsIn[DurationIn], decay); 00116 } 00117 } 00118 00119 static inline UnitType ar (UnitType const& input, 00120 DurationUnitType const& duration, 00121 DecayUnitType const& decay, 00122 const DurationType maximumDuration, 00123 UnitType const& mul, 00124 UnitType const& add, 00125 BlockSize const& preferredBlockSize, 00126 SampleRate const& preferredSampleRate) throw() 00127 { 00128 const Data data = { { -1.0, -1.0 }, maximumDuration, 0 }; 00129 00130 const int numInputChannels = input.getNumChannels(); 00131 const int numDurationChannels = duration.getNumChannels(); 00132 const int numDecayChannels = decay.getNumChannels(); 00133 const int numChannels = plonk::max (numInputChannels, numDurationChannels, numDecayChannels); 00134 00135 UnitType mainUnit = UnitType::emptyChannels (numChannels); 00136 00137 for (int i = 0; i < numChannels; ++i) 00138 { 00139 Inputs inputs; 00140 inputs.put (IOKey::Generic, input[i]); 00141 inputs.put (IOKey::Duration, duration[i]); 00142 inputs.put (IOKey::Decay, decay[i]); 00143 00144 UnitType unit = UnitType::template proxiesFromInputs<DelayInternal> (inputs, 00145 data, 00146 preferredBlockSize, 00147 preferredSampleRate); 00148 mainUnit.put (i, unit); 00149 } 00150 00151 return UnitType::applyMulAdd (mainUnit, mul, add); 00152 } 00153 }; 00154 00155 00156 //------------------------------------------------------------------------------ 00157 00158 00175 template<class SampleType, Interp::TypeCode InterpTypeCode> 00176 class AllpassDecayUnit 00177 { 00178 public: 00179 typedef DelayFormAllpassDecay<SampleType,InterpTypeCode> FormType; 00180 typedef DelayFormAllpassFFFB<SampleType,InterpTypeCode> AltFormType; 00181 00182 typedef Delay2ParamChannelInternal<FormType> DelayInternal; 00183 typedef UnitBase<SampleType> UnitType; 00184 typedef InputDictionary Inputs; 00185 00186 typedef typename DelayInternal::Param1Type DurationType; 00187 typedef UnitBase<DurationType> DurationUnitType; 00188 00189 typedef typename DelayInternal::Param2Type DecayType; 00190 typedef UnitBase<DecayType> DecayUnitType; 00191 00192 typedef AllpassDecayUnit<SampleType, Interp::Lagrange3> HQ; 00193 typedef AllpassDecayUnit<SampleType, Interp::None> N; 00194 00195 00196 static inline UnitInfos getInfo() throw() 00197 { 00198 const double blockSize = (double)BlockSize::getDefault().getValue(); 00199 const double sampleRate = SampleRate::getDefault().getValue(); 00200 00201 return UnitInfo ("AllpassDecay", "An allpass delay setting the decay as a time to decay by 60dB.", 00202 00203 // output 00204 ChannelCount::VariableChannelCount, 00205 IOKey::Generic, Measure::None, 0.0, IOLimit::None, 00206 IOKey::End, 00207 00208 // inputs 00209 IOKey::Generic, Measure::None, IOInfo::NoDefault, IOLimit::None, 00210 IOKey::Duration, Measure::Seconds, 0.5, IOLimit::Minimum, Measure::Seconds, 0.0, 00211 IOKey::Decay, Measure::Seconds, 1.0, IOLimit::None, 00212 IOKey::MaximumDuration, Measure::Seconds, 1.0, IOLimit::Minimum, Measure::Samples, 1.0, 00213 IOKey::Multiply, Measure::Factor, 1.0, IOLimit::None, 00214 IOKey::Add, Measure::None, 0.0, IOLimit::None, 00215 IOKey::BlockSize, Measure::Samples, blockSize, IOLimit::Minimum, Measure::Samples, 1.0, 00216 IOKey::SampleRate, Measure::Hertz, sampleRate, IOLimit::Minimum, Measure::Hertz, 0.0, 00217 IOKey::End); 00218 } 00219 00221 static UnitType ar (UnitType const& input, 00222 DurationUnitType const& duration = DurationType (0.5), 00223 DecayUnitType const& decay = DecayType (1.0), 00224 const DurationType maximumDuration = DurationType (1.0), 00225 UnitType const& mul = SampleType (1), 00226 UnitType const& add = SampleType (0), 00227 BlockSize const& preferredBlockSize = BlockSize::getDefault(), 00228 SampleRate const& preferredSampleRate = SampleRate::getDefault()) throw() 00229 { 00230 if (duration.isEachChannelConstant() && decay.isEachChannelConstant()) 00231 return AltFormType::ar (input, duration, duration.getValues().decayFeedback (decay.getValues()), maximumDuration, 00232 mul, add, preferredBlockSize, preferredSampleRate); 00233 else 00234 return FormType::ar (input, duration, decay, maximumDuration, 00235 mul, add, preferredBlockSize, preferredSampleRate); 00236 } 00237 00238 }; 00239 00240 typedef AllpassDecayUnit<PLONK_TYPE_DEFAULT> AllpassDecay; 00241 00242 00243 #endif // PLONK_DELAYFORMALLPASSDECAY_H 00244