1 /*************************************************************************/
2 /*  audio_effect_eq.cpp                                                  */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "audio_effect_eq.h"
32 #include "servers/audio_server.h"
33 
process(const AudioFrame * p_src_frames,AudioFrame * p_dst_frames,int p_frame_count)34 void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
35 
36 	int band_count = bands[0].size();
37 	EQ::BandProcess *proc_l = bands[0].ptrw();
38 	EQ::BandProcess *proc_r = bands[1].ptrw();
39 	float *bgain = gains.ptrw();
40 	for (int i = 0; i < band_count; i++) {
41 		bgain[i] = Math::db2linear(base->gain[i]);
42 	}
43 
44 	for (int i = 0; i < p_frame_count; i++) {
45 
46 		AudioFrame src = p_src_frames[i];
47 		AudioFrame dst = AudioFrame(0, 0);
48 
49 		for (int j = 0; j < band_count; j++) {
50 
51 			float l = src.l;
52 			float r = src.r;
53 
54 			proc_l[j].process_one(l);
55 			proc_r[j].process_one(r);
56 
57 			dst.l += l * bgain[j];
58 			dst.r += r * bgain[j];
59 		}
60 
61 		p_dst_frames[i] = dst;
62 	}
63 }
64 
instance()65 Ref<AudioEffectInstance> AudioEffectEQ::instance() {
66 	Ref<AudioEffectEQInstance> ins;
67 	ins.instance();
68 	ins->base = Ref<AudioEffectEQ>(this);
69 	ins->gains.resize(eq.get_band_count());
70 	for (int i = 0; i < 2; i++) {
71 		ins->bands[i].resize(eq.get_band_count());
72 		for (int j = 0; j < ins->bands[i].size(); j++) {
73 			ins->bands[i].write[j] = eq.get_band_processor(j);
74 		}
75 	}
76 
77 	return ins;
78 }
79 
set_band_gain_db(int p_band,float p_volume)80 void AudioEffectEQ::set_band_gain_db(int p_band, float p_volume) {
81 	ERR_FAIL_INDEX(p_band, gain.size());
82 	gain.write[p_band] = p_volume;
83 }
84 
get_band_gain_db(int p_band) const85 float AudioEffectEQ::get_band_gain_db(int p_band) const {
86 	ERR_FAIL_INDEX_V(p_band, gain.size(), 0);
87 
88 	return gain[p_band];
89 }
get_band_count() const90 int AudioEffectEQ::get_band_count() const {
91 	return gain.size();
92 }
93 
_set(const StringName & p_name,const Variant & p_value)94 bool AudioEffectEQ::_set(const StringName &p_name, const Variant &p_value) {
95 
96 	const Map<StringName, int>::Element *E = prop_band_map.find(p_name);
97 	if (E) {
98 		set_band_gain_db(E->get(), p_value);
99 		return true;
100 	}
101 
102 	return false;
103 }
104 
_get(const StringName & p_name,Variant & r_ret) const105 bool AudioEffectEQ::_get(const StringName &p_name, Variant &r_ret) const {
106 
107 	const Map<StringName, int>::Element *E = prop_band_map.find(p_name);
108 	if (E) {
109 		r_ret = get_band_gain_db(E->get());
110 		return true;
111 	}
112 
113 	return false;
114 }
115 
_get_property_list(List<PropertyInfo> * p_list) const116 void AudioEffectEQ::_get_property_list(List<PropertyInfo> *p_list) const {
117 
118 	for (int i = 0; i < band_names.size(); i++) {
119 
120 		p_list->push_back(PropertyInfo(Variant::REAL, band_names[i], PROPERTY_HINT_RANGE, "-60,24,0.1"));
121 	}
122 }
123 
_bind_methods()124 void AudioEffectEQ::_bind_methods() {
125 
126 	ClassDB::bind_method(D_METHOD("set_band_gain_db", "band_idx", "volume_db"), &AudioEffectEQ::set_band_gain_db);
127 	ClassDB::bind_method(D_METHOD("get_band_gain_db", "band_idx"), &AudioEffectEQ::get_band_gain_db);
128 	ClassDB::bind_method(D_METHOD("get_band_count"), &AudioEffectEQ::get_band_count);
129 }
130 
AudioEffectEQ(EQ::Preset p_preset)131 AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) {
132 
133 	eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
134 	eq.set_preset_band_mode(p_preset);
135 	gain.resize(eq.get_band_count());
136 	for (int i = 0; i < gain.size(); i++) {
137 		gain.write[i] = 0.0;
138 		String name = "band_db/" + itos(eq.get_band_frequency(i)) + "_hz";
139 		prop_band_map[name] = i;
140 		band_names.push_back(name);
141 	}
142 }
143