Audaspace:
[blender.git] / intern / audaspace / FX / AUD_ButterworthFactory.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * Copyright 2009-2011 Jörg Hermann Müller
5  *
6  * This file is part of AudaSpace.
7  *
8  * Audaspace is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * AudaSpace is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Audaspace; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file audaspace/FX/AUD_ButterworthFactory.cpp
26  *  \ingroup audfx
27  */
28
29
30 #include "AUD_ButterworthFactory.h"
31 #include "AUD_IIRFilterReader.h"
32
33 #include <cmath>
34
35 #ifndef M_PI
36 #define M_PI 3.14159265358979323846
37 #endif
38
39 #define BWPB41 0.76536686473
40 #define BWPB42 1.84775906502
41
42 AUD_ButterworthFactory::AUD_ButterworthFactory(boost::shared_ptr<AUD_IFactory> factory,
43                                                                                            float frequency) :
44                 AUD_DynamicIIRFilterFactory(factory),
45                 m_frequency(frequency)
46 {
47 }
48
49 void AUD_ButterworthFactory::recalculateCoefficients(AUD_SampleRate rate,
50                                                                                                          std::vector<float> &b,
51                                                                                                          std::vector<float> &a)
52 {
53         float omega = 2 * tan(m_frequency * M_PI / rate);
54         float o2 = omega * omega;
55         float o4 = o2 * o2;
56         float x1 = o2 + 2.0f * (float)BWPB41 * omega + 4.0f;
57         float x2 = o2 + 2.0f * (float)BWPB42 * omega + 4.0f;
58         float y1 = o2 - 2.0f * (float)BWPB41 * omega + 4.0f;
59         float y2 = o2 - 2.0f * (float)BWPB42 * omega + 4.0f;
60         float o228 = 2.0f * o2 - 8.0f;
61         float norm = x1 * x2;
62         a.push_back(1);
63         a.push_back((x1 + x2) * o228 / norm);
64         a.push_back((x1 * y2 + x2 * y1 + o228 * o228) / norm);
65         a.push_back((y1 + y2) * o228 / norm);
66         a.push_back(y1 * y2 / norm);
67         b.push_back(o4 / norm);
68         b.push_back(4 * o4 / norm);
69         b.push_back(6 * o4 / norm);
70         b.push_back(b[1]);
71         b.push_back(b[0]);
72 }