1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #include "fx/ButterworthCalculator.h"
18 
19 #include <cmath>
20 
21 #define BWPB41 0.76536686473
22 #define BWPB42 1.84775906502
23 
24 AUD_NAMESPACE_BEGIN
25 
ButterworthCalculator(float frequency)26 ButterworthCalculator::ButterworthCalculator(float frequency) :
27 	m_frequency(frequency)
28 {
29 }
30 
recalculateCoefficients(SampleRate rate,std::vector<float> & b,std::vector<float> & a)31 void ButterworthCalculator::recalculateCoefficients(SampleRate rate, std::vector<float> &b, std::vector<float> &a)
32 {
33 	float omega = 2 * std::tan(m_frequency * M_PI / rate);
34 	float o2 = omega * omega;
35 	float o4 = o2 * o2;
36 	float x1 = o2 + 2.0f * (float)BWPB41 * omega + 4.0f;
37 	float x2 = o2 + 2.0f * (float)BWPB42 * omega + 4.0f;
38 	float y1 = o2 - 2.0f * (float)BWPB41 * omega + 4.0f;
39 	float y2 = o2 - 2.0f * (float)BWPB42 * omega + 4.0f;
40 	float o228 = 2.0f * o2 - 8.0f;
41 	float norm = x1 * x2;
42 	a.push_back(1);
43 	a.push_back((x1 + x2) * o228 / norm);
44 	a.push_back((x1 * y2 + x2 * y1 + o228 * o228) / norm);
45 	a.push_back((y1 + y2) * o228 / norm);
46 	a.push_back(y1 * y2 / norm);
47 	b.push_back(o4 / norm);
48 	b.push_back(4 * o4 / norm);
49 	b.push_back(6 * o4 / norm);
50 	b.push_back(b[1]);
51 	b.push_back(b[0]);
52 }
53 
54 AUD_NAMESPACE_END
55