Path: blob/master/servers/audio/effects/audio_effect_limiter.cpp
10278 views
/**************************************************************************/1/* audio_effect_limiter.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_limiter.h"3132void AudioEffectLimiterInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {33float threshdb = base->threshold;34float ceiling = Math::db_to_linear(base->ceiling);35float ceildb = base->ceiling;36float makeup = Math::db_to_linear(ceildb - threshdb);37float sc = -base->soft_clip;38float scv = Math::db_to_linear(sc);39float peakdb = ceildb + 25;40float scmult = Math::abs((ceildb - sc) / (peakdb - sc));4142for (int i = 0; i < p_frame_count; i++) {43float spl0 = p_src_frames[i].left;44float spl1 = p_src_frames[i].right;45spl0 = spl0 * makeup;46spl1 = spl1 * makeup;47float sign0 = (spl0 < 0.0 ? -1.0 : 1.0);48float sign1 = (spl1 < 0.0 ? -1.0 : 1.0);49float abs0 = Math::abs(spl0);50float abs1 = Math::abs(spl1);51float overdb0 = Math::linear_to_db(abs0) - ceildb;52float overdb1 = Math::linear_to_db(abs1) - ceildb;5354if (abs0 > scv) {55spl0 = sign0 * (scv + Math::db_to_linear(overdb0 * scmult));56}57if (abs1 > scv) {58spl1 = sign1 * (scv + Math::db_to_linear(overdb1 * scmult));59}6061spl0 = MIN(ceiling, Math::abs(spl0)) * (spl0 < 0.0 ? -1.0 : 1.0);62spl1 = MIN(ceiling, Math::abs(spl1)) * (spl1 < 0.0 ? -1.0 : 1.0);6364p_dst_frames[i].left = spl0;65p_dst_frames[i].right = spl1;66}67}6869Ref<AudioEffectInstance> AudioEffectLimiter::instantiate() {70Ref<AudioEffectLimiterInstance> ins;71ins.instantiate();72ins->base = Ref<AudioEffectLimiter>(this);7374return ins;75}7677void AudioEffectLimiter::set_threshold_db(float p_threshold) {78threshold = p_threshold;79}8081float AudioEffectLimiter::get_threshold_db() const {82return threshold;83}8485void AudioEffectLimiter::set_ceiling_db(float p_ceiling) {86ceiling = p_ceiling;87}8889float AudioEffectLimiter::get_ceiling_db() const {90return ceiling;91}9293void AudioEffectLimiter::set_soft_clip_db(float p_soft_clip) {94soft_clip = p_soft_clip;95}9697float AudioEffectLimiter::get_soft_clip_db() const {98return soft_clip;99}100101void AudioEffectLimiter::set_soft_clip_ratio(float p_soft_clip) {102soft_clip_ratio = p_soft_clip;103}104105float AudioEffectLimiter::get_soft_clip_ratio() const {106return soft_clip_ratio;107}108109void AudioEffectLimiter::_bind_methods() {110ClassDB::bind_method(D_METHOD("set_ceiling_db", "ceiling"), &AudioEffectLimiter::set_ceiling_db);111ClassDB::bind_method(D_METHOD("get_ceiling_db"), &AudioEffectLimiter::get_ceiling_db);112113ClassDB::bind_method(D_METHOD("set_threshold_db", "threshold"), &AudioEffectLimiter::set_threshold_db);114ClassDB::bind_method(D_METHOD("get_threshold_db"), &AudioEffectLimiter::get_threshold_db);115116ClassDB::bind_method(D_METHOD("set_soft_clip_db", "soft_clip"), &AudioEffectLimiter::set_soft_clip_db);117ClassDB::bind_method(D_METHOD("get_soft_clip_db"), &AudioEffectLimiter::get_soft_clip_db);118119ClassDB::bind_method(D_METHOD("set_soft_clip_ratio", "soft_clip"), &AudioEffectLimiter::set_soft_clip_ratio);120ClassDB::bind_method(D_METHOD("get_soft_clip_ratio"), &AudioEffectLimiter::get_soft_clip_ratio);121122ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ceiling_db", PROPERTY_HINT_RANGE, "-20,-0.1,0.1,suffix:dB"), "set_ceiling_db", "get_ceiling_db");123ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "threshold_db", PROPERTY_HINT_RANGE, "-30,0,0.1,suffix:dB"), "set_threshold_db", "get_threshold_db");124ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "soft_clip_db", PROPERTY_HINT_RANGE, "0,6,0.1,suffix:dB"), "set_soft_clip_db", "get_soft_clip_db");125ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "soft_clip_ratio", PROPERTY_HINT_RANGE, "3,20,0.1"), "set_soft_clip_ratio", "get_soft_clip_ratio");126}127128AudioEffectLimiter::AudioEffectLimiter() {129threshold = 0;130ceiling = -0.1;131soft_clip = 2;132soft_clip_ratio = 10;133}134135136