-
Notifications
You must be signed in to change notification settings - Fork 2
/
fir.cpp
216 lines (176 loc) · 6.02 KB
/
fir.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "fir.h"
/*
FIR filter designed with
http://t-filter.appspot.com
sampling frequency: 2000 Hz
* 0 Hz - 90 Hz
gain = 1
desired ripple = 5 dB
actual ripple = 3.986475904421452 dB
* 200 Hz - 1000 Hz
gain = 0
desired attenuation = -40 dB
actual attenuation = -40.38887353005501 dB
*/
/*
FIR filter designed with
http://t-filter.appspot.com
sampling frequency: 2000 Hz
* 0 Hz - 90 Hz
gain = 2
desired ripple = 5 dB
actual ripple = 3.576397969398726 dB
* 200 Hz - 1000 Hz
gain = 0
desired attenuation = -40 dB
actual attenuation = -41.302631617010256 dB
*/
#define FIR_LEN 90
vrok::EffectFIR::EffectFIR()
: _meter("out"), _buffer(new float[(FIR_LEN + GetBufferConfig()->frames) * GetBufferConfig()->channels]) {
int len = (FIR_LEN + GetBufferConfig()->frames) * GetBufferConfig()->channels;
for (int i = 0; i < len; i++) {
_buffer[i] = 0.0;
}
_dist[0].activate();
_dist[1].activate();
ComponentManager *c = ComponentManager::GetSingleton();
c->RegisterComponent(this);
c->RegisterProperty(this, "blend", &_blend);
c->RegisterProperty(this, "drive", &_drive);
c->RegisterProperty(this, "lp_freq", &_lp_freq);
c->RegisterProperty(this, "hp_freq", &_hp_freq);
c->RegisterProperty(this, "dry_vol", &_dry_vol);
c->RegisterProperty(this, "wet_vol", &_wet_vol);
_blend.Set(9);
_drive.Set(1);
_lp_freq.Set(150.0f);
_hp_freq.Set(50.0f);
_wet_vol.Set(0.5);
_dry_vol.Set(0.5);
_f32_dry_vol = _dry_vol.Get();
_f32_wet_vol = _wet_vol.Get();
_lp[0][0].set_lp_rbj(_lp_freq.Get(), 0.707, (float)GetBufferConfig()->samplerate);
_lp[0][1].copy_coeffs(_lp[0][0]);
_lp[0][2].copy_coeffs(_lp[0][0]);
_lp[0][3].copy_coeffs(_lp[0][0]);
_lp[1][0].copy_coeffs(_lp[0][0]);
_lp[1][1].copy_coeffs(_lp[0][0]);
_lp[1][2].copy_coeffs(_lp[0][0]);
_lp[1][3].copy_coeffs(_lp[0][0]);
_hp[0][0].set_hp_rbj(_hp_freq.Get(), 0.707, (float)GetBufferConfig()->samplerate);
_hp[0][1].copy_coeffs(_hp[0][0]);
_hp[1][0].copy_coeffs(_hp[0][0]);
_hp[1][1].copy_coeffs(_hp[0][0]);
_dist[0].set_sample_rate(GetBufferConfig()->samplerate);
_dist[0].set_params(_blend.Get(), _drive.Get());
_dist[1].set_sample_rate(GetBufferConfig()->samplerate);
_dist[1].set_params(_blend.Get(), _drive.Get());
}
bool vrok::EffectFIR::EffectRun(Buffer *out_buffer, Buffer **in_buffer_set, int buffer_count) {
BufferConfig *bc = GetBufferConfig();
int fir_len = FIR_LEN * bc->channels;
int len = bc->frames * bc->channels;
for (int i = 0; i < len; i++) {
out_buffer->GetData()[i] = (in_buffer_set[0]->GetData()[i]);
}
/*double *proc=in_buffer_set[0]->GetData();
double *proc_out=out_buffer->GetData();
proc_out[0]=proc[0];
for (int j=0;j<bc->frames;j++) {
for (int i=0;i<bc->channels;i++)
{
proc_out[i] = proc[i];
}
for (int i=0;i<bc->channels;i++)
{
proc_out[i] = _lp[i][1].process(_lp[i][0].process(proc_out[i]));
proc_out[i] = _dist[i].process(proc_out[i]);
proc_out[i] = _lp[i][2].process(_lp[i][3].process(proc_out[i]));
proc_out[i] = (proc_out[i]*_f32_wet_vol + proc[i])*_f32_dry_vol;
}
proc+=bc->channels;
proc_out+=bc->channels;
}
_lp[0][0].sanitize();
_lp[1][0].sanitize();
_lp[0][1].sanitize();
_lp[1][1].sanitize();
_lp[0][2].sanitize();
_lp[1][2].sanitize();
_lp[0][3].sanitize();
_lp[1][3].sanitize();
_hp[0][0].sanitize();
_hp[1][0].sanitize();
_hp[0][1].sanitize();
_hp[1][1].sanitize();
_meter.Process(out_buffer);
// for (int i=0;i<len;i++)
// {
// _buffer[i+fir_len] = in_buffer_set[0]->GetData()[i];
// }
// for (int i=0;i<bc->frames*bc->channels;i++)
// {
// out_buffer->GetData()[i] =0 ;
// for (int j=0;j<fir_len;j++)
// {
// out_buffer->GetData()[i] += coeffs[j]*_buffer[fir_len + i - j ];
// }
// out_buffer->GetData()[i]=CLIP(tap.process(out_buffer->GetData()[i]));
// }
// for (int i=0;i<fir_len;i++)
// {
// _buffer[i]=in_buffer_set[0]->GetData()[bc->frames*bc->channels - fir_len +i];
// }
*/
return true;
}
void vrok::EffectFIR::PropertyChanged(PropertyBase *property) {
if (!(_blend.Get() < 10.0 && _blend.Get() > -10.0)) {
WARN(9, "invalid value for blend");
return;
}
if (!(_drive.Get() < 10.0 && _drive.Get() > 0.0)) {
WARN(9, "invalid value for drive");
return;
}
if (!(_hp_freq.Get() < 20000.0 && _hp_freq.Get() > 0.0)) {
WARN(9, "invalid value for hp_freq");
return;
}
if (!(_lp_freq.Get() < 20000.0 && _lp_freq.Get() > 0.0)) {
WARN(9, "invalid value for lp_freq");
return;
}
if (!(_dry_vol.Get() < 20.0 && _dry_vol.Get() > 0.0)) {
WARN(9, "invalid value for dry vol");
return;
}
if (!(_wet_vol.Get() < 20.0 && _wet_vol.Get() > 0.0)) {
WARN(9, "invalid value for wet vol");
return;
}
_lp[0][0].set_lp_rbj(_lp_freq.Get(), 0.707, (float)GetBufferConfig()->samplerate);
_lp[0][1].copy_coeffs(_lp[0][0]);
_lp[0][2].copy_coeffs(_lp[0][0]);
_lp[0][3].copy_coeffs(_lp[0][0]);
_lp[1][0].copy_coeffs(_lp[0][0]);
_lp[1][1].copy_coeffs(_lp[0][0]);
_lp[1][2].copy_coeffs(_lp[0][0]);
_lp[1][3].copy_coeffs(_lp[0][0]);
_hp[0][0].set_hp_rbj(_hp_freq.Get(), 0.707, (float)GetBufferConfig()->samplerate);
_hp[0][1].copy_coeffs(_hp[0][0]);
_hp[1][0].copy_coeffs(_hp[0][0]);
_hp[1][1].copy_coeffs(_hp[0][0]);
_dist[0].set_sample_rate((float)GetBufferConfig()->samplerate);
_dist[0].set_params(_blend.Get(), _drive.Get());
_dist[1].set_sample_rate((float)GetBufferConfig()->samplerate);
_dist[1].set_params(_blend.Get(), _drive.Get());
_f32_dry_vol = _dry_vol.Get();
_f32_wet_vol = _wet_vol.Get();
}
bool vrok::EffectFIR::BufferConfigChange(BufferConfig *config) {
DBG(0, "bufferchange fot vu");
_meter.SetBufferConfig(config);
return true;
}