-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvf.jl
192 lines (168 loc) · 4.86 KB
/
svf.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module StateVariableFilter
struct CommonProcessingResult{T<:AbstractFloat}
w::T
a::T
b::T
c1::T
c2::T
end
function process_common(freq, q)
w = 2.0 * tan(pi * freq)
a = w / q
b = w*w
c1 = (a + b) / (1 + 0.5 * a + 0.25 * b)
c2 = b / (a + b)
CommonProcessingResult(w, a, b, c1, c2)
end
mutable struct State{T<:AbstractFloat}
z1::T
z2::T
end
State() = State(0.0,0.0)
struct D1D0{T<:AbstractFloat}
d1::T
d0::T
end
function calculate_d0_high(common)
1.0 - 0.5 * common.c1 + common.c1 * common.c2 * 0.25
end
function calculate_d1d0_band(common)
d1_band = 1.0 - common.c2
d0_band = d1_band * common.c1 * 0.5
D1D0(d1_band, d0_band)
end
function calculate_d0_low(common)
common.c1 * common.c2 * 0.25
end
function process(s::State, in, highgain, bandgain, lowgain, freq, q)
common = process_common(freq, q)
d0_high = calculate_d0_high(common)
d1d0_band = calculate_d1d0_band(common)
d0_low = calculate_d0_low(common)
out = 0
x = in - s.z1 - s.z2 + 1e-20
out += highgain * d0_high * x
out += bandgain * (d1d0_band.d0 * x + d1d0_band.d1 * s.z1)
s.z2 += common.c2 * s.z1
out += lowgain * (d0_low * x + s.z2)
s.z1 += common.c1 * x
out
end
function process!(s::State, input_buffer, output_buffer, sample_count, highgain, bandgain, lowgain, freq, q)
common = process_common(freq, q)
d0_high = calculate_d0_high(common)
d1d0_band = calculate_d1d0_band(common)
d0_low = calculate_d0_low(common)
for sample_index = 1:sample_count
in = input_buffer[sample_index]
out = 0
x = in - s.z1 - s.z2 + 1e-20
out += highgain * d0_high * x
out += bandgain * (d1d0_band.d0 * x + d1d0_band.d1 * s.z1)
s.z2 += common.c2 * s.z1
out += lowgain * (d0_low * x + s.z2)
s.z1 += common.c1 * x
output_buffer[sample_index] = out;
end
output_buffer
end
#struct lowbandhighpass
#{
# state s;
#
# void process(float *input_buffer, float *output_buffer, unsigned int sample_count, const float highgain, const float bandgain, const float lowgain, float freq, float q)
# {
# const common_processing_commonult common = process_common(freq, q);
#
# const float d0_high = calculate_d0_high(common);
# d1d0 d1d0_band = calculate_d1d0_band(common);
# const float d0_low = calculate_d0_low(common);
#
# for (unsigned int sample_index = 0; sample_index < sample_count; ++sample_index)
# {
# const float in = input_buffer[sample_index];
# float out = 0;
#
# const float x = in - s.z1 - s.z2 + 1e-20f;
# out += highgain * d0_high * x;
# out += bandgain * (d1d0_band.d0 * x + d1d0_band.d1 * s.z1);
# s.z2 += common.c2 * s.z1;
# out += lowgain * (d0_low * x + s.z2);
# s.z1 += common.c1 * x;
#
# output_buffer[sample_index] = out;
# }
# }
#};
#
#struct highpass
#{
# state s;
#
# void process(float *input_buffer, float *output_buffer, unsigned int sample_count, const float highgain, float freq, float q)
# {
# const common_processing_commonult common = process_common(freq, q);
#
# const float d0_high = calculate_d0_high(common);
#
# for (unsigned int sample_index = 0; sample_index < sample_count; ++sample_index)
# {
# const float in = input_buffer[sample_index];
#
# const float x = in - s.z1 - s.z2 + 1e-20f;
# output_buffer[sample_index] = highgain * d0_high * x;
# s.z2 += common.c2 * s.z1;
# s.z1 += common.c1 * x;
# }
# }
#};
#
#struct bandpass
#{
# state s;
#
# void process(float *input_buffer, float *output_buffer, unsigned int sample_count, const float bandgain, float freq, float q)
# {
# const common_processing_commonult common = process_common(freq, q);
#
# d1d0 d1d0_band = calculate_d1d0_band(common);
#
# for (unsigned int sample_index = 0; sample_index < sample_count; ++sample_index)
# {
# const float in = input_buffer[sample_index];
#
# const float x = in - s.z1 - s.z2 + 1e-20f;
# output_buffer[sample_index] = bandgain * (d1d0_band.d0 * x + d1d0_band.d1 * s.z1);
# s.z2 += common.c2 * s.z1;
# s.z1 += common.c1 * x;
# }
# }
#};
#
#struct lowpass
#{
# state s;
#
# void process(float *input_buffer, float *output_buffer, unsigned int sample_count, const float lowgain, float freq, float q)
# {
# const common_processing_commonult common = process_common(freq, q);
#
# const float d0_low = calculate_d0_low(common);
#
# for (unsigned int sample_index = 0; sample_index < sample_count; ++sample_index)
# {
# const float in = input_buffer[sample_index];
#
# const float x = in - s.z1 - s.z2 + 1e-20f;
# s.z2 += common.c2 * s.z1;
# output_buffer[sample_index] = lowgain * (d0_low * x + s.z2);
# s.z1 += common.c1 * x;
# }
# }
#};
#
#} // namespace
#
##endif
#
end