1 | /**************************************************************************/ |
2 | /* audio_effect_distortion.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "audio_effect_distortion.h" |
32 | #include "core/math/math_funcs.h" |
33 | #include "servers/audio_server.h" |
34 | |
35 | void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { |
36 | const float *src = (const float *)p_src_frames; |
37 | float *dst = (float *)p_dst_frames; |
38 | |
39 | //float lpf_c=expf(-Math_TAU*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE)); |
40 | float lpf_c = expf(-Math_TAU * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate())); |
41 | float lpf_ic = 1.0 - lpf_c; |
42 | |
43 | float drive_f = base->drive; |
44 | float pregain_f = Math::db_to_linear(base->pre_gain); |
45 | float postgain_f = Math::db_to_linear(base->post_gain); |
46 | |
47 | float atan_mult = pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001; |
48 | float atan_div = 1.0 / (atanf(atan_mult) * (1.0 + drive_f * 8)); |
49 | |
50 | float lofi_mult = powf(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits |
51 | |
52 | for (int i = 0; i < p_frame_count * 2; i++) { |
53 | float out = undenormalize(src[i] * lpf_ic + lpf_c * h[i & 1]); |
54 | h[i & 1] = out; |
55 | float a = out; |
56 | float ha = src[i] - out; //high freqs |
57 | a *= pregain_f; |
58 | |
59 | switch (base->mode) { |
60 | case AudioEffectDistortion::MODE_CLIP: { |
61 | float a_sign = a < 0 ? -1.0f : 1.0f; |
62 | a = powf(abs(a), 1.0001 - drive_f) * a_sign; |
63 | if (a > 1.0) { |
64 | a = 1.0; |
65 | } else if (a < (-1.0)) { |
66 | a = -1.0; |
67 | } |
68 | |
69 | } break; |
70 | case AudioEffectDistortion::MODE_ATAN: { |
71 | a = atanf(a * atan_mult) * atan_div; |
72 | |
73 | } break; |
74 | case AudioEffectDistortion::MODE_LOFI: { |
75 | a = floorf(a * lofi_mult + 0.5) / lofi_mult; |
76 | |
77 | } break; |
78 | case AudioEffectDistortion::MODE_OVERDRIVE: { |
79 | const double x = a * 0.686306; |
80 | const double z = 1 + exp(sqrt(fabs(x)) * -0.75); |
81 | a = (expf(x) - expf(-x * z)) / (expf(x) + expf(-x)); |
82 | } break; |
83 | case AudioEffectDistortion::MODE_WAVESHAPE: { |
84 | float x = a; |
85 | float k = 2 * drive_f / (1.00001 - drive_f); |
86 | |
87 | a = (1.0 + k) * x / (1.0 + k * fabsf(x)); |
88 | |
89 | } break; |
90 | } |
91 | |
92 | dst[i] = a * postgain_f + ha; |
93 | } |
94 | } |
95 | |
96 | Ref<AudioEffectInstance> AudioEffectDistortion::instantiate() { |
97 | Ref<AudioEffectDistortionInstance> ins; |
98 | ins.instantiate(); |
99 | ins->base = Ref<AudioEffectDistortion>(this); |
100 | ins->h[0] = 0; |
101 | ins->h[1] = 0; |
102 | |
103 | return ins; |
104 | } |
105 | |
106 | void AudioEffectDistortion::set_mode(Mode p_mode) { |
107 | mode = p_mode; |
108 | } |
109 | |
110 | AudioEffectDistortion::Mode AudioEffectDistortion::get_mode() const { |
111 | return mode; |
112 | } |
113 | |
114 | void AudioEffectDistortion::set_pre_gain(float p_pre_gain) { |
115 | pre_gain = p_pre_gain; |
116 | } |
117 | |
118 | float AudioEffectDistortion::get_pre_gain() const { |
119 | return pre_gain; |
120 | } |
121 | |
122 | void AudioEffectDistortion::set_keep_hf_hz(float p_keep_hf_hz) { |
123 | keep_hf_hz = p_keep_hf_hz; |
124 | } |
125 | |
126 | float AudioEffectDistortion::get_keep_hf_hz() const { |
127 | return keep_hf_hz; |
128 | } |
129 | |
130 | void AudioEffectDistortion::set_drive(float p_drive) { |
131 | drive = p_drive; |
132 | } |
133 | |
134 | float AudioEffectDistortion::get_drive() const { |
135 | return drive; |
136 | } |
137 | |
138 | void AudioEffectDistortion::set_post_gain(float p_post_gain) { |
139 | post_gain = p_post_gain; |
140 | } |
141 | |
142 | float AudioEffectDistortion::get_post_gain() const { |
143 | return post_gain; |
144 | } |
145 | |
146 | void AudioEffectDistortion::_bind_methods() { |
147 | ClassDB::bind_method(D_METHOD("set_mode" , "mode" ), &AudioEffectDistortion::set_mode); |
148 | ClassDB::bind_method(D_METHOD("get_mode" ), &AudioEffectDistortion::get_mode); |
149 | |
150 | ClassDB::bind_method(D_METHOD("set_pre_gain" , "pre_gain" ), &AudioEffectDistortion::set_pre_gain); |
151 | ClassDB::bind_method(D_METHOD("get_pre_gain" ), &AudioEffectDistortion::get_pre_gain); |
152 | |
153 | ClassDB::bind_method(D_METHOD("set_keep_hf_hz" , "keep_hf_hz" ), &AudioEffectDistortion::set_keep_hf_hz); |
154 | ClassDB::bind_method(D_METHOD("get_keep_hf_hz" ), &AudioEffectDistortion::get_keep_hf_hz); |
155 | |
156 | ClassDB::bind_method(D_METHOD("set_drive" , "drive" ), &AudioEffectDistortion::set_drive); |
157 | ClassDB::bind_method(D_METHOD("get_drive" ), &AudioEffectDistortion::get_drive); |
158 | |
159 | ClassDB::bind_method(D_METHOD("set_post_gain" , "post_gain" ), &AudioEffectDistortion::set_post_gain); |
160 | ClassDB::bind_method(D_METHOD("get_post_gain" ), &AudioEffectDistortion::get_post_gain); |
161 | |
162 | ADD_PROPERTY(PropertyInfo(Variant::INT, "mode" , PROPERTY_HINT_ENUM, "Clip,ATan,LoFi,Overdrive,Wave Shape" ), "set_mode" , "get_mode" ); |
163 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pre_gain" , PROPERTY_HINT_RANGE, "-60,60,0.01,suffix:dB" ), "set_pre_gain" , "get_pre_gain" ); |
164 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "keep_hf_hz" , PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz" ), "set_keep_hf_hz" , "get_keep_hf_hz" ); |
165 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "drive" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_drive" , "get_drive" ); |
166 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "post_gain" , PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB" ), "set_post_gain" , "get_post_gain" ); |
167 | |
168 | BIND_ENUM_CONSTANT(MODE_CLIP); |
169 | BIND_ENUM_CONSTANT(MODE_ATAN); |
170 | BIND_ENUM_CONSTANT(MODE_LOFI); |
171 | BIND_ENUM_CONSTANT(MODE_OVERDRIVE); |
172 | BIND_ENUM_CONSTANT(MODE_WAVESHAPE); |
173 | } |
174 | |
175 | AudioEffectDistortion::AudioEffectDistortion() { |
176 | mode = MODE_CLIP; |
177 | pre_gain = 0; |
178 | post_gain = 0; |
179 | keep_hf_hz = 16000; |
180 | drive = 0; |
181 | } |
182 | |