Bugfix [#27157] keyframing a constrained bone does not work as before
[blender.git] / intern / audaspace / FX / AUD_EnvelopeFactory.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/FX/AUD_EnvelopeFactory.cpp
28  *  \ingroup audfx
29  */
30
31
32 #include "AUD_EnvelopeFactory.h"
33 #include "AUD_CallbackIIRFilterReader.h"
34
35 #include <cmath>
36
37 struct EnvelopeParameters
38 {
39         float attack;
40         float release;
41         float threshold;
42         float arthreshold;
43 };
44
45 sample_t AUD_EnvelopeFactory::envelopeFilter(AUD_CallbackIIRFilterReader* reader, EnvelopeParameters* param)
46 {
47         float in = fabs(reader->x(0));
48         float out = reader->y(-1);
49         if(in < param->threshold)
50                 in = 0.0f;
51         return (in > out ? param->attack : param->release) * (out - in) + in;
52 }
53
54 void AUD_EnvelopeFactory::endEnvelopeFilter(EnvelopeParameters* param)
55 {
56         delete param;
57 }
58
59 AUD_EnvelopeFactory::AUD_EnvelopeFactory(AUD_Reference<AUD_IFactory> factory, float attack,
60                                                                                  float release, float threshold,
61                                                                                  float arthreshold) :
62                 AUD_EffectFactory(factory),
63                 m_attack(attack),
64                 m_release(release),
65                 m_threshold(threshold),
66                 m_arthreshold(arthreshold)
67 {
68 }
69
70 AUD_Reference<AUD_IReader> AUD_EnvelopeFactory::createReader()
71 {
72         AUD_Reference<AUD_IReader> reader = getReader();
73
74         EnvelopeParameters* param = new EnvelopeParameters();
75         param->arthreshold = m_arthreshold;
76         param->attack = pow(m_arthreshold, 1.0f/(static_cast<float>(reader->getSpecs().rate) * m_attack));
77         param->release = pow(m_arthreshold, 1.0f/(static_cast<float>(reader->getSpecs().rate) * m_release));
78         param->threshold = m_threshold;
79
80         return new AUD_CallbackIIRFilterReader(reader, 1, 2,
81                                                                                    (doFilterIIR) envelopeFilter,
82                                                                                    (endFilterIIR) endEnvelopeFilter,
83                                                                                    param);
84 }