2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_effect_eq.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
#include "audio_effect_eq.h"
|
|
|
|
#include "servers/audio_server.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
2017-01-21 22:00:25 +00:00
|
|
|
int band_count = bands[0].size();
|
2017-11-25 03:07:54 +00:00
|
|
|
EQ::BandProcess *proc_l = bands[0].ptrw();
|
|
|
|
EQ::BandProcess *proc_r = bands[1].ptrw();
|
|
|
|
float *bgain = gains.ptrw();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < band_count; i++) {
|
|
|
|
bgain[i] = Math::db2linear(base->gain[i]);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_frame_count; i++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
AudioFrame src = p_src_frames[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioFrame dst = AudioFrame(0, 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < band_count; j++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
float l = src.l;
|
|
|
|
float r = src.r;
|
|
|
|
|
|
|
|
proc_l[j].process_one(l);
|
|
|
|
proc_r[j].process_one(r);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
dst.l += l * bgain[j];
|
|
|
|
dst.r += r * bgain[j];
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_dst_frames[i] = dst;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioEffectInstance> AudioEffectEQ::instance() {
|
|
|
|
Ref<AudioEffectEQInstance> ins;
|
|
|
|
ins.instance();
|
2017-03-05 15:44:50 +00:00
|
|
|
ins->base = Ref<AudioEffectEQ>(this);
|
2017-01-21 22:00:25 +00:00
|
|
|
ins->gains.resize(eq.get_band_count());
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
ins->bands[i].resize(eq.get_band_count());
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < ins->bands[i].size(); j++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
ins->bands[i].write[j] = eq.get_band_processor(j);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioEffectEQ::set_band_gain_db(int p_band, float p_volume) {
|
|
|
|
ERR_FAIL_INDEX(p_band, gain.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
gain.write[p_band] = p_volume;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEffectEQ::get_band_gain_db(int p_band) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_band, gain.size(), 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
return gain[p_band];
|
|
|
|
}
|
|
|
|
int AudioEffectEQ::get_band_count() const {
|
|
|
|
return gain.size();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool AudioEffectEQ::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
const Map<StringName, int>::Element *E = prop_band_map.find(p_name);
|
2017-01-21 22:00:25 +00:00
|
|
|
if (E) {
|
2017-03-05 15:44:50 +00:00
|
|
|
set_band_gain_db(E->get(), p_value);
|
2017-01-21 22:00:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool AudioEffectEQ::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
const Map<StringName, int>::Element *E = prop_band_map.find(p_name);
|
2017-01-21 22:00:25 +00:00
|
|
|
if (E) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = get_band_gain_db(E->get());
|
2017-01-21 22:00:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioEffectEQ::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
for (int i = 0; i < band_names.size(); i++) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, band_names[i], PROPERTY_HINT_RANGE, "-60,24,0.1"));
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectEQ::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_band_gain_db", "band_idx", "volume_db"), &AudioEffectEQ::set_band_gain_db);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_band_gain_db", "band_idx"), &AudioEffectEQ::get_band_gain_db);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_band_count"), &AudioEffectEQ::get_band_count);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) {
|
2017-01-21 22:00:25 +00:00
|
|
|
eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
|
|
|
|
eq.set_preset_band_mode(p_preset);
|
|
|
|
gain.resize(eq.get_band_count());
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < gain.size(); i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
gain.write[i] = 0.0;
|
2017-03-05 15:44:50 +00:00
|
|
|
String name = "band_db/" + itos(eq.get_band_frequency(i)) + "_hz";
|
|
|
|
prop_band_map[name] = i;
|
2017-01-21 22:00:25 +00:00
|
|
|
band_names.push_back(name);
|
|
|
|
}
|
|
|
|
}
|