1 /*************************************************************************/
2 /*  audio_filter_sw.h                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 #ifndef AUDIO_FILTER_SW_H
31 #define AUDIO_FILTER_SW_H
32 
33 #include "math_funcs.h"
34 
35 class AudioFilterSW {
36 public:
37 	struct Coeffs {
38 
39 		float a1, a2;
40 		float b0, b1, b2;
41 
42 		//bool operator==(const Coeffs &p_rv) { return (FLOATS_EQ(a1,p_rv.a1) && FLOATS_EQ(a2,p_rv.a2) && FLOATS_EQ(b1,p_rv.b1) && FLOATS_EQ(b2,p_rv.b2) && FLOATS_EQ(b0,p_rv.b0) ); }
CoeffsCoeffs43 		Coeffs() { a1 = a2 = b0 = b1 = b2 = 0.0; }
44 	};
45 
46 	enum Mode {
47 		BANDPASS,
48 		HIGHPASS,
49 		LOWPASS,
50 		NOTCH,
51 		PEAK,
52 		BANDLIMIT,
53 		LOWSHELF,
54 		HIGHSHELF
55 
56 	};
57 
58 	class Processor { // simple filter processor
59 
60 		AudioFilterSW *filter;
61 		Coeffs coeffs;
62 		float ha1, ha2, hb1, hb2; //history
63 	public:
64 		void set_filter(AudioFilterSW *p_filter);
65 		void process(float *p_samples, int p_amount, int p_stride = 1);
66 		void update_coeffs();
67 		inline void process_one(float &p_sample);
68 
69 		Processor();
70 	};
71 
72 private:
73 	float cutoff;
74 	float resonance;
75 	float gain;
76 	float sampling_rate;
77 	int stages;
78 	Mode mode;
79 
80 public:
81 	float get_response(float p_freq, Coeffs *p_coeffs);
82 
83 	void set_mode(Mode p_mode);
84 	void set_cutoff(float p_cutoff);
85 	void set_resonance(float p_resonance);
86 	void set_gain(float p_gain);
87 	void set_sampling_rate(float p_srate);
88 	void set_stages(int p_stages); //adjust for multiple stages
89 
90 	void prepare_coefficients(Coeffs *p_coeffs);
91 
92 	AudioFilterSW();
93 };
94 
95 /* inline methods */
96 
process_one(float & p_val)97 void AudioFilterSW::Processor::process_one(float &p_val) {
98 
99 	float pre = p_val;
100 	p_val = (p_val * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2);
101 	ha2 = ha1;
102 	hb2 = hb1;
103 	hb1 = pre;
104 	ha1 = p_val;
105 }
106 
107 #endif // AUDIO_FILTER_SW_H
108