1 | /**************************************************************************/ |
2 | /* audio_filter_sw.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef AUDIO_FILTER_SW_H |
32 | #define AUDIO_FILTER_SW_H |
33 | |
34 | #include "core/math/math_funcs.h" |
35 | |
36 | class AudioFilterSW { |
37 | public: |
38 | struct Coeffs { |
39 | float a1 = 0.0f; |
40 | float a2 = 0.0f; |
41 | float b0 = 0.0f; |
42 | float b1 = 0.0f; |
43 | float b2 = 0.0f; |
44 | }; |
45 | |
46 | enum Mode { |
47 | BANDPASS, |
48 | HIGHPASS, |
49 | LOWPASS, |
50 | NOTCH, |
51 | PEAK, |
52 | BANDLIMIT, |
53 | LOWSHELF, |
54 | HIGHSHELF |
55 | }; |
56 | |
57 | class Processor { // Simple filter processor. |
58 | AudioFilterSW *filter = nullptr; |
59 | Coeffs coeffs; |
60 | // History. |
61 | float ha1 = 0.0f; |
62 | float ha2 = 0.0f; |
63 | float hb1 = 0.0f; |
64 | float hb2 = 0.0f; |
65 | Coeffs incr_coeffs; |
66 | |
67 | public: |
68 | void set_filter(AudioFilterSW *p_filter, bool p_clear_history = true); |
69 | void process(float *p_samples, int p_amount, int p_stride = 1, bool p_interpolate = false); |
70 | void update_coeffs(int p_interp_buffer_len = 0); |
71 | _ALWAYS_INLINE_ void process_one(float &p_sample); |
72 | _ALWAYS_INLINE_ void process_one_interp(float &p_sample); |
73 | |
74 | Processor(); |
75 | }; |
76 | |
77 | private: |
78 | float cutoff = 5000.0f; |
79 | float resonance = 0.5f; |
80 | float gain = 1.0f; |
81 | float sampling_rate = 44100.0f; |
82 | int stages = 1; |
83 | Mode mode = LOWPASS; |
84 | |
85 | public: |
86 | float get_response(float p_freq, Coeffs *p_coeffs); |
87 | |
88 | void set_mode(Mode p_mode); |
89 | void set_cutoff(float p_cutoff); |
90 | void set_resonance(float p_resonance); |
91 | void set_gain(float p_gain); |
92 | void set_sampling_rate(float p_srate); |
93 | void set_stages(int p_stages); //adjust for multiple stages |
94 | |
95 | void prepare_coefficients(Coeffs *p_coeffs); |
96 | |
97 | AudioFilterSW() {} |
98 | }; |
99 | |
100 | /* inline methods */ |
101 | |
102 | void AudioFilterSW::Processor::process_one(float &p_sample) { |
103 | float pre = p_sample; |
104 | p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2); |
105 | ha2 = ha1; |
106 | hb2 = hb1; |
107 | hb1 = pre; |
108 | ha1 = p_sample; |
109 | } |
110 | |
111 | void AudioFilterSW::Processor::process_one_interp(float &p_sample) { |
112 | float pre = p_sample; |
113 | p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2); |
114 | ha2 = ha1; |
115 | hb2 = hb1; |
116 | hb1 = pre; |
117 | ha1 = p_sample; |
118 | |
119 | coeffs.b0 += incr_coeffs.b0; |
120 | coeffs.b1 += incr_coeffs.b1; |
121 | coeffs.b2 += incr_coeffs.b2; |
122 | coeffs.a1 += incr_coeffs.a1; |
123 | coeffs.a2 += incr_coeffs.a2; |
124 | } |
125 | |
126 | #endif // AUDIO_FILTER_SW_H |
127 | |