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