Audaspace (GSoC): First merging commit
[blender.git] / intern / audaspace / FX / AUD_EnvelopeFactory.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 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 Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include "AUD_EnvelopeFactory.h"
27 #include "AUD_CallbackIIRFilterReader.h"
28
29 #include <cmath>
30
31 struct EnvelopeParameters
32 {
33         float attack;
34         float release;
35         float threshold;
36         float arthreshold;
37 };
38
39 sample_t envelopeFilter(AUD_CallbackIIRFilterReader* reader, EnvelopeParameters* param)
40 {
41         float in = fabs(reader->x(0));
42         float out = reader->y(-1);
43         if(in < param->threshold)
44                 in = 0.0f;
45         return (in > out ? param->attack : param->release) * (out - in) + in;
46 }
47
48 void endEnvelopeFilter(EnvelopeParameters* param)
49 {
50         delete param;
51 }
52
53 AUD_EnvelopeFactory::AUD_EnvelopeFactory(AUD_IFactory* factory, float attack,
54                                                                                  float release, float threshold,
55                                                                                  float arthreshold) :
56                 AUD_EffectFactory(factory),
57                 m_attack(attack),
58                 m_release(release),
59                 m_threshold(threshold),
60                 m_arthreshold(arthreshold)
61 {
62 }
63
64 AUD_IReader* AUD_EnvelopeFactory::createReader() const
65 {
66         AUD_IReader* reader = getReader();
67
68         EnvelopeParameters* param = new EnvelopeParameters();
69         param->arthreshold = m_arthreshold;
70         param->attack = pow(m_arthreshold, 1.0f/(reader->getSpecs().rate * m_attack));
71         param->release = pow(m_arthreshold, 1.0f/(reader->getSpecs().rate * m_release));
72         param->threshold = m_threshold;
73
74         return new AUD_CallbackIIRFilterReader(reader, 1, 2,
75                                                                                    (doFilterIIR) envelopeFilter,
76                                                                                    (endFilterIIR) endEnvelopeFilter,
77                                                                                    param);
78 }