Path: blob/master/servers/audio/effects/audio_effect_distortion.cpp
10278 views
/**************************************************************************/1/* audio_effect_distortion.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_distortion.h"31#include "core/math/math_funcs.h"32#include "servers/audio_server.h"3334void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {35const float *src = (const float *)p_src_frames;36float *dst = (float *)p_dst_frames;3738//float lpf_c=std::exp(-Math::TAU*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE));39float lpf_c = std::exp(-Math::TAU * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate()));40float lpf_ic = 1.0 - lpf_c;4142float drive_f = base->drive;43float pregain_f = Math::db_to_linear(base->pre_gain);44float postgain_f = Math::db_to_linear(base->post_gain);4546float atan_mult = std::pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001;47float atan_div = 1.0 / (std::atan(atan_mult) * (1.0 + drive_f * 8));4849float lofi_mult = std::pow(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits5051for (int i = 0; i < p_frame_count * 2; i++) {52float out = undenormalize(src[i] * lpf_ic + lpf_c * h[i & 1]);53h[i & 1] = out;54float a = out;55float ha = src[i] - out; //high freqs56a *= pregain_f;5758switch (base->mode) {59case AudioEffectDistortion::MODE_CLIP: {60float a_sign = a < 0 ? -1.0f : 1.0f;61a = std::pow(std::abs(a), 1.0001 - drive_f) * a_sign;62if (a > 1.0) {63a = 1.0;64} else if (a < (-1.0)) {65a = -1.0;66}6768} break;69case AudioEffectDistortion::MODE_ATAN: {70a = std::atan(a * atan_mult) * atan_div;7172} break;73case AudioEffectDistortion::MODE_LOFI: {74a = std::floor(a * lofi_mult + 0.5) / lofi_mult;7576} break;77case AudioEffectDistortion::MODE_OVERDRIVE: {78const double x = a * 0.686306;79const double z = 1 + std::exp(std::sqrt(std::abs(x)) * -0.75);80a = (std::exp(x) - std::exp(-x * z)) / (std::exp(x) + std::exp(-x));81} break;82case AudioEffectDistortion::MODE_WAVESHAPE: {83float x = a;84float k = 2 * drive_f / (1.00001 - drive_f);8586a = (1.0 + k) * x / (1.0 + k * std::abs(x));8788} break;89}9091dst[i] = a * postgain_f + ha;92}93}9495Ref<AudioEffectInstance> AudioEffectDistortion::instantiate() {96Ref<AudioEffectDistortionInstance> ins;97ins.instantiate();98ins->base = Ref<AudioEffectDistortion>(this);99ins->h[0] = 0;100ins->h[1] = 0;101102return ins;103}104105void AudioEffectDistortion::set_mode(Mode p_mode) {106mode = p_mode;107}108109AudioEffectDistortion::Mode AudioEffectDistortion::get_mode() const {110return mode;111}112113void AudioEffectDistortion::set_pre_gain(float p_pre_gain) {114pre_gain = p_pre_gain;115}116117float AudioEffectDistortion::get_pre_gain() const {118return pre_gain;119}120121void AudioEffectDistortion::set_keep_hf_hz(float p_keep_hf_hz) {122keep_hf_hz = p_keep_hf_hz;123}124125float AudioEffectDistortion::get_keep_hf_hz() const {126return keep_hf_hz;127}128129void AudioEffectDistortion::set_drive(float p_drive) {130drive = p_drive;131}132133float AudioEffectDistortion::get_drive() const {134return drive;135}136137void AudioEffectDistortion::set_post_gain(float p_post_gain) {138post_gain = p_post_gain;139}140141float AudioEffectDistortion::get_post_gain() const {142return post_gain;143}144145void AudioEffectDistortion::_bind_methods() {146ClassDB::bind_method(D_METHOD("set_mode", "mode"), &AudioEffectDistortion::set_mode);147ClassDB::bind_method(D_METHOD("get_mode"), &AudioEffectDistortion::get_mode);148149ClassDB::bind_method(D_METHOD("set_pre_gain", "pre_gain"), &AudioEffectDistortion::set_pre_gain);150ClassDB::bind_method(D_METHOD("get_pre_gain"), &AudioEffectDistortion::get_pre_gain);151152ClassDB::bind_method(D_METHOD("set_keep_hf_hz", "keep_hf_hz"), &AudioEffectDistortion::set_keep_hf_hz);153ClassDB::bind_method(D_METHOD("get_keep_hf_hz"), &AudioEffectDistortion::get_keep_hf_hz);154155ClassDB::bind_method(D_METHOD("set_drive", "drive"), &AudioEffectDistortion::set_drive);156ClassDB::bind_method(D_METHOD("get_drive"), &AudioEffectDistortion::get_drive);157158ClassDB::bind_method(D_METHOD("set_post_gain", "post_gain"), &AudioEffectDistortion::set_post_gain);159ClassDB::bind_method(D_METHOD("get_post_gain"), &AudioEffectDistortion::get_post_gain);160161ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Clip,ATan,LoFi,Overdrive,Wave Shape"), "set_mode", "get_mode");162ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pre_gain", PROPERTY_HINT_RANGE, "-60,60,0.01,suffix:dB"), "set_pre_gain", "get_pre_gain");163ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "keep_hf_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_keep_hf_hz", "get_keep_hf_hz");164ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drive", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drive", "get_drive");165ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "post_gain", PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB"), "set_post_gain", "get_post_gain");166167BIND_ENUM_CONSTANT(MODE_CLIP);168BIND_ENUM_CONSTANT(MODE_ATAN);169BIND_ENUM_CONSTANT(MODE_LOFI);170BIND_ENUM_CONSTANT(MODE_OVERDRIVE);171BIND_ENUM_CONSTANT(MODE_WAVESHAPE);172}173174AudioEffectDistortion::AudioEffectDistortion() {175mode = MODE_CLIP;176pre_gain = 0;177post_gain = 0;178keep_hf_hz = 16000;179drive = 0;180}181182183