1/**************************************************************************/
2/* audio_effect_eq.cpp */
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#include "audio_effect_eq.h"
32
33#include "servers/audio_server.h"
34
35void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
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::db_to_linear(base->gain[i]);
42 }
43
44 for (int i = 0; i < p_frame_count; i++) {
45 AudioFrame src = p_src_frames[i];
46 AudioFrame dst = AudioFrame(0, 0);
47
48 for (int j = 0; j < band_count; j++) {
49 float l = src.l;
50 float r = src.r;
51
52 proc_l[j].process_one(l);
53 proc_r[j].process_one(r);
54
55 dst.l += l * bgain[j];
56 dst.r += r * bgain[j];
57 }
58
59 p_dst_frames[i] = dst;
60 }
61}
62
63Ref<AudioEffectInstance> AudioEffectEQ::instantiate() {
64 Ref<AudioEffectEQInstance> ins;
65 ins.instantiate();
66 ins->base = Ref<AudioEffectEQ>(this);
67 ins->gains.resize(eq.get_band_count());
68 for (int i = 0; i < 2; i++) {
69 ins->bands[i].resize(eq.get_band_count());
70 for (int j = 0; j < ins->bands[i].size(); j++) {
71 ins->bands[i].write[j] = eq.get_band_processor(j);
72 }
73 }
74
75 return ins;
76}
77
78void AudioEffectEQ::set_band_gain_db(int p_band, float p_volume) {
79 ERR_FAIL_INDEX(p_band, gain.size());
80 gain.write[p_band] = p_volume;
81}
82
83float AudioEffectEQ::get_band_gain_db(int p_band) const {
84 ERR_FAIL_INDEX_V(p_band, gain.size(), 0);
85
86 return gain[p_band];
87}
88
89int AudioEffectEQ::get_band_count() const {
90 return gain.size();
91}
92
93bool AudioEffectEQ::_set(const StringName &p_name, const Variant &p_value) {
94 HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);
95 if (E) {
96 set_band_gain_db(E->value, p_value);
97 return true;
98 }
99
100 return false;
101}
102
103bool AudioEffectEQ::_get(const StringName &p_name, Variant &r_ret) const {
104 HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);
105 if (E) {
106 r_ret = get_band_gain_db(E->value);
107 return true;
108 }
109
110 return false;
111}
112
113void AudioEffectEQ::_get_property_list(List<PropertyInfo> *p_list) const {
114 for (int i = 0; i < band_names.size(); i++) {
115 p_list->push_back(PropertyInfo(Variant::FLOAT, band_names[i], PROPERTY_HINT_RANGE, "-60,24,0.1"));
116 }
117}
118
119void AudioEffectEQ::_bind_methods() {
120 ClassDB::bind_method(D_METHOD("set_band_gain_db", "band_idx", "volume_db"), &AudioEffectEQ::set_band_gain_db);
121 ClassDB::bind_method(D_METHOD("get_band_gain_db", "band_idx"), &AudioEffectEQ::get_band_gain_db);
122 ClassDB::bind_method(D_METHOD("get_band_count"), &AudioEffectEQ::get_band_count);
123}
124
125AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) {
126 eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
127 eq.set_preset_band_mode(p_preset);
128 gain.resize(eq.get_band_count());
129 for (int i = 0; i < gain.size(); i++) {
130 gain.write[i] = 0.0;
131 String band_name = "band_db/" + itos(eq.get_band_frequency(i)) + "_hz";
132 prop_band_map[band_name] = i;
133 band_names.push_back(band_name);
134 }
135}
136