2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_effect_distortion.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 15:47:28 +01: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-05 00:50:27 +01:00
|
|
|
|
2017-01-22 00:57:48 +01:00
|
|
|
#include "audio_effect_distortion.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "servers/audio_server.h"
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
|
|
|
const float *src = (const float *)p_src_frames;
|
|
|
|
float *dst = (float *)p_dst_frames;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2020-04-03 11:50:40 +02:00
|
|
|
//float lpf_c=expf(-Math_TAU*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE));
|
|
|
|
float lpf_c = expf(-Math_TAU * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate()));
|
2017-03-05 16:44:50 +01:00
|
|
|
float lpf_ic = 1.0 - lpf_c;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float drive_f = base->drive;
|
2022-08-13 17:45:42 +02:00
|
|
|
float pregain_f = Math::db_to_linear(base->pre_gain);
|
|
|
|
float postgain_f = Math::db_to_linear(base->post_gain);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float atan_mult = pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001;
|
|
|
|
float atan_div = 1.0 / (atanf(atan_mult) * (1.0 + drive_f * 8));
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float lofi_mult = powf(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_frame_count * 2; i++) {
|
|
|
|
float out = undenormalise(src[i] * lpf_ic + lpf_c * h[i & 1]);
|
|
|
|
h[i & 1] = out;
|
|
|
|
float a = out;
|
|
|
|
float ha = src[i] - out; //high freqs
|
|
|
|
a *= pregain_f;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
switch (base->mode) {
|
|
|
|
case AudioEffectDistortion::MODE_CLIP: {
|
2021-02-19 01:53:26 +01:00
|
|
|
float a_sign = a < 0 ? -1.0f : 1.0f;
|
|
|
|
a = powf(abs(a), 1.0001 - drive_f) * a_sign;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (a > 1.0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
a = 1.0;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (a < (-1.0)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
a = -1.0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case AudioEffectDistortion::MODE_ATAN: {
|
2017-03-05 16:44:50 +01:00
|
|
|
a = atanf(a * atan_mult) * atan_div;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case AudioEffectDistortion::MODE_LOFI: {
|
2017-03-05 16:44:50 +01:00
|
|
|
a = floorf(a * lofi_mult + 0.5) / lofi_mult;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case AudioEffectDistortion::MODE_OVERDRIVE: {
|
|
|
|
const double x = a * 0.686306;
|
2017-03-05 16:44:50 +01:00
|
|
|
const double z = 1 + exp(sqrt(fabs(x)) * -0.75);
|
2017-01-22 00:57:48 +01:00
|
|
|
a = (expf(x) - expf(-x * z)) / (expf(x) + expf(-x));
|
|
|
|
} break;
|
|
|
|
case AudioEffectDistortion::MODE_WAVESHAPE: {
|
|
|
|
float x = a;
|
2017-03-05 16:44:50 +01:00
|
|
|
float k = 2 * drive_f / (1.00001 - drive_f);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
a = (1.0 + k) * x / (1.0 + k * fabsf(x));
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dst[i] = a * postgain_f + ha;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
Ref<AudioEffectInstance> AudioEffectDistortion::instantiate() {
|
2017-01-22 00:57:48 +01:00
|
|
|
Ref<AudioEffectDistortionInstance> ins;
|
2021-06-18 00:03:09 +02:00
|
|
|
ins.instantiate();
|
2017-03-05 16:44:50 +01:00
|
|
|
ins->base = Ref<AudioEffectDistortion>(this);
|
|
|
|
ins->h[0] = 0;
|
|
|
|
ins->h[1] = 0;
|
2017-01-22 00:57:48 +01:00
|
|
|
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectDistortion::set_mode(Mode p_mode) {
|
2017-03-05 16:44:50 +01:00
|
|
|
mode = p_mode;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioEffectDistortion::Mode AudioEffectDistortion::get_mode() const {
|
2017-01-22 00:57:48 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectDistortion::set_pre_gain(float p_pre_gain) {
|
|
|
|
pre_gain = p_pre_gain;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectDistortion::get_pre_gain() const {
|
2017-01-22 00:57:48 +01:00
|
|
|
return pre_gain;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectDistortion::set_keep_hf_hz(float p_keep_hf_hz) {
|
|
|
|
keep_hf_hz = p_keep_hf_hz;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectDistortion::get_keep_hf_hz() const {
|
2017-01-22 00:57:48 +01:00
|
|
|
return keep_hf_hz;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectDistortion::set_drive(float p_drive) {
|
|
|
|
drive = p_drive;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectDistortion::get_drive() const {
|
2017-01-22 00:57:48 +01:00
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectDistortion::set_post_gain(float p_post_gain) {
|
|
|
|
post_gain = p_post_gain;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectDistortion::get_post_gain() const {
|
2017-01-22 00:57:48 +01:00
|
|
|
return post_gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectDistortion::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &AudioEffectDistortion::set_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mode"), &AudioEffectDistortion::get_mode);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pre_gain", "pre_gain"), &AudioEffectDistortion::set_pre_gain);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pre_gain"), &AudioEffectDistortion::get_pre_gain);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_keep_hf_hz", "keep_hf_hz"), &AudioEffectDistortion::set_keep_hf_hz);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_keep_hf_hz"), &AudioEffectDistortion::get_keep_hf_hz);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_drive", "drive"), &AudioEffectDistortion::set_drive);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_drive"), &AudioEffectDistortion::get_drive);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_post_gain", "post_gain"), &AudioEffectDistortion::set_post_gain);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_post_gain"), &AudioEffectDistortion::get_post_gain);
|
2017-01-22 00:57:48 +01:00
|
|
|
|
2021-05-22 04:30:58 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Clip,ATan,LoFi,Overdrive,Wave Shape"), "set_mode", "get_mode");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pre_gain", PROPERTY_HINT_RANGE, "-60,60,0.01"), "set_pre_gain", "get_pre_gain");
|
2022-05-20 07:24:41 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "keep_hf_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_keep_hf_hz", "get_keep_hf_hz");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drive", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drive", "get_drive");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "post_gain", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_post_gain", "get_post_gain");
|
2017-09-12 21:09:06 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(MODE_CLIP);
|
|
|
|
BIND_ENUM_CONSTANT(MODE_ATAN);
|
|
|
|
BIND_ENUM_CONSTANT(MODE_LOFI);
|
|
|
|
BIND_ENUM_CONSTANT(MODE_OVERDRIVE);
|
|
|
|
BIND_ENUM_CONSTANT(MODE_WAVESHAPE);
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioEffectDistortion::AudioEffectDistortion() {
|
|
|
|
mode = MODE_CLIP;
|
|
|
|
pre_gain = 0;
|
|
|
|
post_gain = 0;
|
|
|
|
keep_hf_hz = 16000;
|
|
|
|
drive = 0;
|
2017-01-22 00:57:48 +01:00
|
|
|
}
|