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_IMPULSES_H 00040 #define PLONK_IMPULSES_H 00041 00042 #include "../channel/plonk_ChannelInternalCore.h" 00043 #include "../plonk_GraphForwardDeclarations.h" 00044 #include "plonk_Saw.h" 00045 00046 template<class SampleType> class ImpulseChannelInternal; 00047 00048 PLONK_CHANNELDATA_DECLARE(ImpulseChannelInternal,SampleType) 00049 { 00050 typedef typename TypeUtility<SampleType>::IndexType FrequencyType; 00051 00052 ChannelInternalCore::Data base; 00053 00054 FrequencyType currentValue; 00055 LongLong peak; 00056 LongLong peak2peak; 00057 }; 00058 00059 PLONK_CHANNELDATA_SPECIAL(ImpulseChannelInternal,float) 00060 { 00061 ChannelInternalCore::Data base; 00062 00063 float currentValue; 00064 float peak; 00065 float peak2peak; 00066 }; 00067 00068 PLONK_CHANNELDATA_SPECIAL(ImpulseChannelInternal,double) 00069 { 00070 ChannelInternalCore::Data base; 00071 00072 double currentValue; 00073 double peak; 00074 double peak2peak; 00075 }; 00076 00077 PLONK_CHANNELDATA_SPECIAL(ImpulseChannelInternal,short) 00078 { 00079 typedef TypeUtility<short>::IndexType FrequencyType; 00080 00081 ChannelInternalCore::Data base; 00082 00083 FrequencyType currentValue; 00084 short peak; 00085 int peak2peak; 00086 }; 00087 00088 PLONK_CHANNELDATA_SPECIAL(ImpulseChannelInternal,int) 00089 { 00090 typedef TypeUtility<int>::IndexType FrequencyType; 00091 00092 ChannelInternalCore::Data base; 00093 00094 FrequencyType currentValue; 00095 int peak; 00096 LongLong peak2peak; 00097 }; 00098 00099 //------------------------------------------------------------------------------ 00100 00102 template<class SampleType> 00103 class ImpulseChannelInternal 00104 : public ChannelInternal<SampleType, PLONK_CHANNELDATA_NAME(ImpulseChannelInternal,SampleType)> 00105 { 00106 public: 00107 typedef PLONK_CHANNELDATA_NAME(ImpulseChannelInternal,SampleType) Data; 00108 typedef InputDictionary Inputs; 00109 00110 typedef ChannelBase<SampleType> ChannelType; 00111 typedef ImpulseChannelInternal<SampleType> ImpulseInternal; 00112 typedef ChannelInternal<SampleType,Data> Internal; 00113 typedef ChannelInternalBase<SampleType> InternalBase; 00114 typedef UnitBase<SampleType> UnitType; 00115 00116 typedef typename TypeUtility<SampleType>::IndexType FrequencyType; 00117 typedef UnitBase<FrequencyType> FrequencyUnitType; 00118 typedef NumericalArray<FrequencyType> FrequencyBufferType; 00119 00120 ImpulseChannelInternal (Inputs const& inputs, 00121 Data const& data, 00122 BlockSize const& blockSize, 00123 SampleRate const& sampleRate) throw() 00124 : Internal (inputs, data, blockSize, sampleRate) 00125 { 00126 } 00127 00128 Text getName() const throw() 00129 { 00130 return "Impulse"; 00131 } 00132 00133 IntArray getInputKeys() const throw() 00134 { 00135 const IntArray keys (IOKey::Frequency); 00136 return keys; 00137 } 00138 00139 InternalBase* getChannel (const int index) throw() 00140 { 00141 const Inputs channelInputs = this->getInputs().getChannel (index); 00142 return new ImpulseInternal (channelInputs, 00143 this->getState(), 00144 this->getBlockSize(), 00145 this->getSampleRate()); 00146 } 00147 00148 void initChannel (const int channel) throw() 00149 { 00150 const FrequencyUnitType& frequencyUnit = ChannelInternalCore::getInputAs<FrequencyUnitType> (IOKey::Frequency); 00151 00152 this->setBlockSize (BlockSize::decide (frequencyUnit.getBlockSize (channel), 00153 this->getBlockSize())); 00154 this->setSampleRate (SampleRate::decide (frequencyUnit.getSampleRate (channel), 00155 this->getSampleRate())); 00156 00157 this->setOverlap (frequencyUnit.getOverlap (channel)); 00158 00159 this->initValue (SampleType (1)); 00160 } 00161 00162 void process (ProcessInfo& info, const int channel) throw() 00163 { 00164 Data& data = this->getState(); 00165 const double sampleDuration = data.base.sampleDuration; 00166 const double factor = data.peak2peak * sampleDuration; 00167 00168 FrequencyUnitType& frequencyUnit = ChannelInternalCore::getInputAs<FrequencyUnitType> (IOKey::Frequency); 00169 00170 const FrequencyBufferType& frequencyBuffer (frequencyUnit.process (info, channel)); 00171 const FrequencyType* const frequencySamples = frequencyBuffer.getArray(); 00172 const int frequencyBufferLength = frequencyBuffer.length(); 00173 00174 SampleType* const outputSamples = this->getOutputSamples(); 00175 const int outputBufferLength = this->getOutputBuffer().length(); 00176 00177 int i; 00178 00179 if (frequencyBufferLength == outputBufferLength) 00180 { 00181 for (i = 0; i < outputBufferLength; ++i) 00182 { 00183 if (data.currentValue >= data.peak) 00184 { 00185 data.currentValue -= data.peak2peak; 00186 outputSamples[i] = data.peak; 00187 } 00188 else 00189 { 00190 outputSamples[i] = SampleType (0); 00191 } 00192 00193 data.currentValue += frequencySamples[i] * factor; 00194 } 00195 } 00196 else if (frequencyBufferLength == 1) 00197 { 00198 const FrequencyType valueIncrement (frequencySamples[0] * factor); 00199 00200 for (i = 0; i < outputBufferLength; ++i) 00201 { 00202 if (data.currentValue >= data.peak) 00203 { 00204 data.currentValue -= data.peak2peak; 00205 outputSamples[i] = data.peak; 00206 } 00207 else 00208 { 00209 outputSamples[i] = SampleType (0); 00210 } 00211 00212 data.currentValue += valueIncrement; 00213 } 00214 } 00215 else 00216 { 00217 double frequencyPosition = 0.0; 00218 const double frequencyIncrement = double (frequencyBufferLength) / double (outputBufferLength); 00219 00220 for (i = 0; i < outputBufferLength; ++i) 00221 { 00222 if (data.currentValue >= data.peak) 00223 { 00224 data.currentValue -= data.peak2peak; 00225 outputSamples[i] = data.peak; 00226 } 00227 else 00228 { 00229 outputSamples[i] = SampleType (0); 00230 } 00231 00232 frequencyPosition += frequencyIncrement; 00233 data.currentValue += frequencySamples[int (frequencyPosition)] * factor; 00234 } 00235 } 00236 } 00237 00238 private: 00239 }; 00240 00241 00242 00243 //------------------------------------------------------------------------------ 00244 00259 template<class SampleType> 00260 class ImpulseUnit 00261 { 00262 public: 00263 typedef ImpulseChannelInternal<SampleType> ImpulseInternal; 00264 typedef typename ImpulseInternal::Data Data; 00265 typedef InputDictionary Inputs; 00266 typedef ChannelBase<SampleType> ChannelType; 00267 typedef ChannelInternal<SampleType,Data> Internal; 00268 typedef UnitBase<SampleType> UnitType; 00269 00270 typedef typename ImpulseInternal::FrequencyType FrequencyType; 00271 typedef typename ImpulseInternal::FrequencyUnitType FrequencyUnitType; 00272 typedef typename ImpulseInternal::FrequencyBufferType FrequencyBufferType; 00273 00274 00275 static inline UnitInfos getInfo() throw() 00276 { 00277 const double blockSize = (double)BlockSize::getDefault().getValue(); 00278 const double sampleRate = SampleRate::getDefault().getValue(); 00279 const double peak = (double)TypeUtility<SampleType>::getTypePeak(); // will be innaccurate for LongLong 00280 00281 return UnitInfo ("Impulse", "An impulse generator.", 00282 00283 // output 00284 ChannelCount::VariableChannelCount, 00285 IOKey::Generic, Measure::None, 0.0, IOLimit::Clipped, Measure::NormalisedBipolar, -peak, peak, 00286 IOKey::End, 00287 00288 // inputs 00289 IOKey::Frequency, Measure::Hertz, 440.0, IOLimit::Clipped, Measure::SampleRateRatio, 0.0, 0.5, 00290 IOKey::Multiply, Measure::Factor, 1.0, IOLimit::None, 00291 IOKey::Add, Measure::None, 0.0, IOLimit::None, 00292 IOKey::BlockSize, Measure::Samples, blockSize, IOLimit::Minimum, Measure::Samples, 1.0, 00293 IOKey::SampleRate, Measure::Hertz, sampleRate, IOLimit::Minimum, Measure::Hertz, 0.0, 00294 IOKey::End); 00295 } 00296 00298 static UnitType ar (FrequencyUnitType const& frequency = FrequencyType (440), 00299 UnitType const& mul = SampleType (1), 00300 UnitType const& add = SampleType (0), 00301 BlockSize const& preferredBlockSize = BlockSize::getDefault(), 00302 SampleRate const& preferredSampleRate = SampleRate::getDefault()) throw() 00303 { 00304 Inputs inputs; 00305 inputs.put (IOKey::Frequency, frequency); 00306 inputs.put (IOKey::Multiply, mul); 00307 inputs.put (IOKey::Add, add); 00308 00309 const LongLong peak = SampleType (TypeUtility<SampleType>::getTypePeak()); 00310 00311 Data data = { { -1.0, -1.0 }, 0, peak, peak * 2 }; 00312 00313 return UnitType::template createFromInputs<ImpulseInternal> (inputs, 00314 data, 00315 preferredBlockSize, 00316 preferredSampleRate); 00317 } 00318 00320 static UnitType kr (FrequencyUnitType const& frequency, 00321 UnitType const& mul = SampleType (1), 00322 UnitType const& add = SampleType (0)) throw() 00323 { 00324 return ar (frequency, mul, add, 00325 BlockSize::getControlRateBlockSize(), 00326 SampleRate::getControlRate()); 00327 } 00328 }; 00329 00330 typedef ImpulseUnit<PLONK_TYPE_DEFAULT> Impulse; 00331 00332 00333 #endif // PLONK_IMPULSES_H 00334