Blender V2.61 - r43446

AUD_EnvelopeFactory.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include "AUD_EnvelopeFactory.h"
00031 #include "AUD_CallbackIIRFilterReader.h"
00032 
00033 #include <cmath>
00034 
00035 struct EnvelopeParameters
00036 {
00037     float attack;
00038     float release;
00039     float threshold;
00040     float arthreshold;
00041 };
00042 
00043 sample_t AUD_EnvelopeFactory::envelopeFilter(AUD_CallbackIIRFilterReader* reader, EnvelopeParameters* param)
00044 {
00045     float in = fabs(reader->x(0));
00046     float out = reader->y(-1);
00047     if(in < param->threshold)
00048         in = 0.0f;
00049     return (in > out ? param->attack : param->release) * (out - in) + in;
00050 }
00051 
00052 void AUD_EnvelopeFactory::endEnvelopeFilter(EnvelopeParameters* param)
00053 {
00054     delete param;
00055 }
00056 
00057 AUD_EnvelopeFactory::AUD_EnvelopeFactory(AUD_Reference<AUD_IFactory> factory, float attack,
00058                                          float release, float threshold,
00059                                          float arthreshold) :
00060         AUD_EffectFactory(factory),
00061         m_attack(attack),
00062         m_release(release),
00063         m_threshold(threshold),
00064         m_arthreshold(arthreshold)
00065 {
00066 }
00067 
00068 AUD_Reference<AUD_IReader> AUD_EnvelopeFactory::createReader()
00069 {
00070     AUD_Reference<AUD_IReader> reader = getReader();
00071 
00072     EnvelopeParameters* param = new EnvelopeParameters();
00073     param->arthreshold = m_arthreshold;
00074     param->attack = pow(m_arthreshold, 1.0f/(static_cast<float>(reader->getSpecs().rate) * m_attack));
00075     param->release = pow(m_arthreshold, 1.0f/(static_cast<float>(reader->getSpecs().rate) * m_release));
00076     param->threshold = m_threshold;
00077 
00078     return new AUD_CallbackIIRFilterReader(reader, 1, 2,
00079                                            (doFilterIIR) envelopeFilter,
00080                                            (endFilterIIR) endEnvelopeFilter,
00081                                            param);
00082 }