Path: blob/master/servers/audio/effects/audio_effect_hard_limiter.cpp
10278 views
/**************************************************************************/1/* audio_effect_hard_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_hard_limiter.h"3132#include "servers/audio_server.h"3334void AudioEffectHardLimiterInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {35float sample_rate = AudioServer::get_singleton()->get_mix_rate();3637float ceiling = Math::db_to_linear(base->ceiling);38float release = base->release;39float attack = base->attack;40float pre_gain = Math::db_to_linear(base->pre_gain);4142for (int i = 0; i < p_frame_count; i++) {43float sample_left = p_src_frames[i].left;44float sample_right = p_src_frames[i].right;4546sample_left *= pre_gain;47sample_right *= pre_gain;4849float largest_sample = MAX(Math::abs(sample_left), Math::abs(sample_right));5051release_factor = MAX(0.0, release_factor - 1.0 / sample_rate);52release_factor = MIN(release_factor, release);5354if (release_factor > 0.0) {55gain = Math::lerp(gain_target, 1.0f, 1.0f - release_factor / release);56}5758if (largest_sample * gain > ceiling) {59gain_target = ceiling / largest_sample;60release_factor = release;61attack_factor = attack;62}6364// Lerp gain over attack time to avoid distortion.65attack_factor = MAX(0.0f, attack_factor - 1.0f / sample_rate);66if (attack_factor > 0.0) {67gain = Math::lerp(gain_target, gain, 1.0f - attack_factor / attack);68}6970int bucket_id = gain_bucket_cursor / gain_bucket_size;7172// If first item within the current bucket, reset the bucket.73if (gain_bucket_cursor % gain_bucket_size == 0) {74gain_buckets[bucket_id] = 1.0f;75}7677gain_buckets[bucket_id] = MIN(gain_buckets[bucket_id], gain);7879gain_bucket_cursor = (gain_bucket_cursor + 1) % gain_samples_to_store;8081for (int j = 0; j < (int)gain_buckets.size(); j++) {82gain = MIN(gain, gain_buckets[j]);83}8485// Introduce latency by grabbing the AudioFrame stored previously,86// then overwrite it with current audioframe, then update circular87// buffer cursor.88float dst_buffer_left = sample_buffer_left[sample_cursor];89float dst_buffer_right = sample_buffer_right[sample_cursor];9091sample_buffer_left[sample_cursor] = sample_left;92sample_buffer_right[sample_cursor] = sample_right;9394sample_cursor = (sample_cursor + 1) % sample_buffer_left.size();9596p_dst_frames[i].left = dst_buffer_left * gain;97p_dst_frames[i].right = dst_buffer_right * gain;98}99}100101Ref<AudioEffectInstance> AudioEffectHardLimiter::instantiate() {102Ref<AudioEffectHardLimiterInstance> ins;103ins.instantiate();104ins->base = Ref<AudioEffectHardLimiter>(this);105106float mix_rate = AudioServer::get_singleton()->get_mix_rate();107108for (int i = 0; i < (int)Math::ceil(mix_rate * attack) + 1; i++) {109ins->sample_buffer_left.push_back(0.0f);110ins->sample_buffer_right.push_back(0.0f);111}112113ins->gain_samples_to_store = (int)Math::ceil(mix_rate * (attack + sustain) + 1);114ins->gain_bucket_size = (int)(mix_rate * attack);115116for (int i = 0; i < ins->gain_samples_to_store; i += ins->gain_bucket_size) {117ins->gain_buckets.push_back(1.0f);118}119120return ins;121}122123void AudioEffectHardLimiter::set_ceiling_db(float p_ceiling) {124ceiling = p_ceiling;125}126127float AudioEffectHardLimiter::get_ceiling_db() const {128return ceiling;129}130131float AudioEffectHardLimiter::get_pre_gain_db() const {132return pre_gain;133}134135void AudioEffectHardLimiter::set_pre_gain_db(const float p_pre_gain) {136pre_gain = p_pre_gain;137}138139float AudioEffectHardLimiter::get_release() const {140return release;141}142143void AudioEffectHardLimiter::set_release(const float p_release) {144release = p_release;145}146147void AudioEffectHardLimiter::_bind_methods() {148ClassDB::bind_method(D_METHOD("set_ceiling_db", "ceiling"), &AudioEffectHardLimiter::set_ceiling_db);149ClassDB::bind_method(D_METHOD("get_ceiling_db"), &AudioEffectHardLimiter::get_ceiling_db);150151ClassDB::bind_method(D_METHOD("set_pre_gain_db", "p_pre_gain"), &AudioEffectHardLimiter::set_pre_gain_db);152ClassDB::bind_method(D_METHOD("get_pre_gain_db"), &AudioEffectHardLimiter::get_pre_gain_db);153154ClassDB::bind_method(D_METHOD("set_release", "p_release"), &AudioEffectHardLimiter::set_release);155ClassDB::bind_method(D_METHOD("get_release"), &AudioEffectHardLimiter::get_release);156157ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pre_gain_db", PROPERTY_HINT_RANGE, "-24,24,0.01,suffix:dB"), "set_pre_gain_db", "get_pre_gain_db");158ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ceiling_db", PROPERTY_HINT_RANGE, "-24,0.0,0.01,suffix:dB"), "set_ceiling_db", "get_ceiling_db");159ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "release", PROPERTY_HINT_RANGE, "0.01,3,0.01"), "set_release", "get_release");160}161162163