Debian Codegraph @

servers/audio/audio_filter_sw.h

raw symbols SCIP
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 #pragma once
32
33 #include "core/typedefs.h"
34
35 class AudioFilterSW {
36 public:
37 struct Coeffs {
38 double a1 = 0.0;
39 double a2 = 0.0;
40 double b0 = 0.0;
41 double b1 = 0.0;
42 double b2 = 0.0;
43 };
44
45 enum Mode {
46 BANDPASS,
47 HIGHPASS,
48 LOWPASS,
49 NOTCH,
50 PEAK,
51 BANDLIMIT,
52 LOWSHELF,
53 HIGHSHELF
54 };
55
56 class Processor { // Simple filter processor.
57 AudioFilterSW *filter = nullptr;
58 Coeffs coeffs;
59 // History.
60 float ha1 = 0.0f;
61 float ha2 = 0.0f;
62 float hb1 = 0.0f;
63 float hb2 = 0.0f;
64 Coeffs incr_coeffs;
65
66 public:
67 void set_filter(AudioFilterSW *p_filter, bool p_clear_history = true);
68 void process(float *p_samples, int p_amount, int p_stride = 1, bool p_interpolate = false);
69 void update_coeffs(int p_interp_buffer_len = 0);
70 _ALWAYS_INLINE_ void process_one(float &p_sample);
71 _ALWAYS_INLINE_ void process_one_interp(float &p_sample);
72
73 Processor();
74 };
75
76 private:
77 float cutoff = 5000.0f;
78 float resonance = 0.5f;
79 float gain = 1.0f;
80 float sampling_rate = 44100.0f;
81 int stages = 1;
82 Mode mode = LOWPASS;
83
84 public:
85 float get_response(float p_freq, Coeffs *p_coeffs);
86
87 void set_mode(Mode p_mode);
88 void set_cutoff(float p_cutoff);
89 void set_resonance(float p_resonance);
90 void set_gain(float p_gain);
91 void set_sampling_rate(float p_srate);
92 void set_stages(int p_stages); //adjust for multiple stages
93
94 void prepare_coefficients(Coeffs *p_coeffs);
95
96 AudioFilterSW() {}
97 };
98
99 /* inline methods */
100
101 void AudioFilterSW::Processor::process_one(float &p_sample) {
102 float pre = p_sample;
103 p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2);
104 ha2 = ha1;
105 hb2 = hb1;
106 hb1 = pre;
107 ha1 = p_sample;
108 }
109
110 void AudioFilterSW::Processor::process_one_interp(float &p_sample) {
111 float pre = p_sample;
112 p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2);
113 ha2 = ha1;
114 hb2 = hb1;
115 hb1 = pre;
116 ha1 = p_sample;
117
118 coeffs.b0 += incr_coeffs.b0;
119 coeffs.b1 += incr_coeffs.b1;
120 coeffs.b2 += incr_coeffs.b2;
121 coeffs.a1 += incr_coeffs.a1;
122 coeffs.a2 += incr_coeffs.a2;
123 }