Merging with trunk up to r38631.
[blender.git] / intern / audaspace / FX / AUD_ButterworthFactory.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_ButterworthFactory.cpp
28  *  \ingroup audfx
29  */
30
31
32 #include "AUD_ButterworthFactory.h"
33 #include "AUD_IIRFilterReader.h"
34
35 #include <cmath>
36
37 #ifndef M_PI
38 #define M_PI 3.14159265358979323846
39 #endif
40
41 #define BWPB41 0.76536686473
42 #define BWPB42 1.84775906502
43
44 AUD_ButterworthFactory::AUD_ButterworthFactory(AUD_Reference<AUD_IFactory> factory,
45                                                                                            float frequency) :
46                 AUD_DynamicIIRFilterFactory(factory),
47                 m_frequency(frequency)
48 {
49 }
50
51 void AUD_ButterworthFactory::recalculateCoefficients(AUD_SampleRate rate,
52                                                                                                          std::vector<float> &b,
53                                                                                                          std::vector<float> &a)
54 {
55         float omega = 2 * tan(m_frequency * M_PI / rate);
56         float o2 = omega * omega;
57         float o4 = o2 * o2;
58         float x1 = o2 + 2 * BWPB41 * omega + 4;
59         float x2 = o2 + 2 * BWPB42 * omega + 4;
60         float y1 = o2 - 2 * BWPB41 * omega + 4;
61         float y2 = o2 - 2 * BWPB42 * omega + 4;
62         float o228 = 2 * o2 - 8;
63         float norm = x1 * x2;
64         a.push_back(1);
65         a.push_back((x1 + x2) * o228 / norm);
66         a.push_back((x1 * y2 + x2 * y1 + o228 * o228) / norm);
67         a.push_back((y1 + y2) * o228 / norm);
68         a.push_back(y1 * y2 / norm);
69         b.push_back(o4 / norm);
70         b.push_back(4 * o4 / norm);
71         b.push_back(6 * o4 / norm);
72         b.push_back(b[1]);
73         b.push_back(b[0]);
74 }