| 1 | /**************************************************************************/ |
| 2 | /* eq_filter.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 EQ_FILTER_H |
| 32 | #define EQ_FILTER_H |
| 33 | |
| 34 | #include "core/templates/vector.h" |
| 35 | #include "core/typedefs.h" |
| 36 | |
| 37 | class EQ { |
| 38 | public: |
| 39 | enum Preset { |
| 40 | PRESET_6_BANDS, |
| 41 | PRESET_8_BANDS, |
| 42 | PRESET_10_BANDS, |
| 43 | PRESET_21_BANDS, |
| 44 | PRESET_31_BANDS |
| 45 | }; |
| 46 | |
| 47 | class BandProcess { |
| 48 | friend class EQ; |
| 49 | float c1, c2, c3; |
| 50 | struct History { |
| 51 | float a1, a2, a3; |
| 52 | float b1, b2, b3; |
| 53 | |
| 54 | } history; |
| 55 | |
| 56 | public: |
| 57 | inline void process_one(float &p_data); |
| 58 | |
| 59 | BandProcess(); |
| 60 | }; |
| 61 | |
| 62 | private: |
| 63 | struct Band { |
| 64 | float freq; |
| 65 | float c1, c2, c3; |
| 66 | }; |
| 67 | |
| 68 | Vector<Band> band; |
| 69 | |
| 70 | float mix_rate; |
| 71 | |
| 72 | void recalculate_band_coefficients(); |
| 73 | |
| 74 | public: |
| 75 | void set_mix_rate(float p_mix_rate); |
| 76 | |
| 77 | int get_band_count() const; |
| 78 | void set_preset_band_mode(Preset p_preset); |
| 79 | void set_bands(const Vector<float> &p_bands); |
| 80 | BandProcess get_band_processor(int p_band) const; |
| 81 | float get_band_frequency(int p_band); |
| 82 | |
| 83 | EQ(); |
| 84 | ~EQ(); |
| 85 | }; |
| 86 | |
| 87 | /* Inline Function */ |
| 88 | |
| 89 | inline void EQ::BandProcess::process_one(float &p_data) { |
| 90 | history.a1 = p_data; |
| 91 | |
| 92 | history.b1 = c1 * (history.a1 - history.a3) + c3 * history.b2 - c2 * history.b3; |
| 93 | |
| 94 | p_data = history.b1; |
| 95 | |
| 96 | history.a3 = history.a2; |
| 97 | history.a2 = history.a1; |
| 98 | history.b3 = history.b2; |
| 99 | history.b2 = history.b1; |
| 100 | } |
| 101 | |
| 102 | #endif // EQ_FILTER_H |
| 103 | |