Path: blob/master/servers/audio/effects/audio_effect_eq.cpp
10278 views
/**************************************************************************/1/* audio_effect_eq.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_effect_eq.h"3132#include "servers/audio_server.h"3334void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {35int band_count = bands[0].size();36EQ::BandProcess *proc_l = bands[0].ptrw();37EQ::BandProcess *proc_r = bands[1].ptrw();38float *bgain = gains.ptrw();39for (int i = 0; i < band_count; i++) {40bgain[i] = Math::db_to_linear(base->gain[i]);41}4243for (int i = 0; i < p_frame_count; i++) {44AudioFrame src = p_src_frames[i];45AudioFrame dst = AudioFrame(0, 0);4647for (int j = 0; j < band_count; j++) {48float l = src.left;49float r = src.right;5051proc_l[j].process_one(l);52proc_r[j].process_one(r);5354dst.left += l * bgain[j];55dst.right += r * bgain[j];56}5758p_dst_frames[i] = dst;59}60}6162Ref<AudioEffectInstance> AudioEffectEQ::instantiate() {63Ref<AudioEffectEQInstance> ins;64ins.instantiate();65ins->base = Ref<AudioEffectEQ>(this);66ins->gains.resize(eq.get_band_count());67for (int i = 0; i < 2; i++) {68ins->bands[i].resize(eq.get_band_count());69for (int j = 0; j < ins->bands[i].size(); j++) {70ins->bands[i].write[j] = eq.get_band_processor(j);71}72}7374return ins;75}7677void AudioEffectEQ::set_band_gain_db(int p_band, float p_volume) {78ERR_FAIL_INDEX(p_band, gain.size());79gain.write[p_band] = p_volume;80}8182float AudioEffectEQ::get_band_gain_db(int p_band) const {83ERR_FAIL_INDEX_V(p_band, gain.size(), 0);8485return gain[p_band];86}8788int AudioEffectEQ::get_band_count() const {89return gain.size();90}9192bool AudioEffectEQ::_set(const StringName &p_name, const Variant &p_value) {93HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);94if (E) {95set_band_gain_db(E->value, p_value);96return true;97}9899return false;100}101102bool AudioEffectEQ::_get(const StringName &p_name, Variant &r_ret) const {103HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);104if (E) {105r_ret = get_band_gain_db(E->value);106return true;107}108109return false;110}111112void AudioEffectEQ::_get_property_list(List<PropertyInfo> *p_list) const {113for (int i = 0; i < band_names.size(); i++) {114p_list->push_back(PropertyInfo(Variant::FLOAT, band_names[i], PROPERTY_HINT_RANGE, "-60,24,0.1"));115}116}117118void AudioEffectEQ::_bind_methods() {119ClassDB::bind_method(D_METHOD("set_band_gain_db", "band_idx", "volume_db"), &AudioEffectEQ::set_band_gain_db);120ClassDB::bind_method(D_METHOD("get_band_gain_db", "band_idx"), &AudioEffectEQ::get_band_gain_db);121ClassDB::bind_method(D_METHOD("get_band_count"), &AudioEffectEQ::get_band_count);122}123124AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) {125eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate());126eq.set_preset_band_mode(p_preset);127gain.resize(eq.get_band_count());128for (int i = 0; i < gain.size(); i++) {129gain.write[i] = 0.0;130String band_name = "band_db/" + itos(eq.get_band_frequency(i)) + "_hz";131prop_band_map[band_name] = i;132band_names.push_back(band_name);133}134}135136137