-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
one_pole_filter.h
81 lines (68 loc) · 2.96 KB
/
one_pole_filter.h
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
/* Copyright 2013-2019 Matt Tytel
*
* vital is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* vital is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with vital. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "common.h"
#include "synth_constants.h"
#include "utils.h"
namespace vital {
template <poly_float (*saturate)(poly_float) = utils::pass<poly_float>>
class OnePoleFilter {
public:
OnePoleFilter() {
reset(constants::kFullMask);
}
force_inline void reset(poly_mask reset_mask) {
current_state_ = utils::maskLoad(current_state_, 0.0f, reset_mask);
filter_state_ = utils::maskLoad(filter_state_, 0.0f, reset_mask);
sat_filter_state_ = utils::maskLoad(sat_filter_state_, 0.0f, reset_mask);
}
virtual ~OnePoleFilter() { }
force_inline poly_float tickBasic(poly_float audio_in, poly_float coefficient) {
poly_float delta = coefficient * (audio_in - filter_state_);
filter_state_ += delta;
current_state_ = filter_state_;
filter_state_ += delta;
return current_state_;
}
force_inline poly_float tick(poly_float audio_in, poly_float coefficient) {
poly_float delta = coefficient * (audio_in - sat_filter_state_);
filter_state_ += delta;
current_state_ = saturate(filter_state_);
filter_state_ += delta;
sat_filter_state_ = saturate(filter_state_);
return current_state_;
}
force_inline poly_float tickDerivative(poly_float audio_in, poly_float coefficient) {
poly_float delta = coefficient * (audio_in - filter_state_);
filter_state_ = utils::mulAdd(filter_state_, saturate(filter_state_ + delta), delta);
current_state_ = filter_state_;
filter_state_ = utils::mulAdd(filter_state_, saturate(filter_state_ + delta), delta);
sat_filter_state_ = filter_state_;
return current_state_;
}
force_inline poly_float getCurrentState() { return current_state_; }
force_inline poly_float getNextSatState() { return sat_filter_state_; }
force_inline poly_float getNextState() { return filter_state_; }
static force_inline poly_float computeCoefficient(poly_float cutoff_frequency, int sample_rate) {
poly_float delta_phase = cutoff_frequency * (vital::kPi / sample_rate);
return utils::tan(delta_phase / (delta_phase + 1.0f));
}
private:
poly_float current_state_;
poly_float filter_state_;
poly_float sat_filter_state_;
};
} // namespace vital