1 | /**************************************************************************/ |
2 | /* audio_effect_compressor.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_compressor.h" |
32 | #include "servers/audio_server.h" |
33 | |
34 | void AudioEffectCompressorInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { |
35 | float threshold = Math::db_to_linear(base->threshold); |
36 | float sample_rate = AudioServer::get_singleton()->get_mix_rate(); |
37 | |
38 | float ratatcoef = exp(-1 / (0.00001f * sample_rate)); |
39 | float ratrelcoef = exp(-1 / (0.5f * sample_rate)); |
40 | float attime = base->attack_us / 1000000.0; |
41 | float reltime = base->release_ms / 1000.0; |
42 | float atcoef = exp(-1 / (attime * sample_rate)); |
43 | float relcoef = exp(-1 / (reltime * sample_rate)); |
44 | |
45 | float makeup = Math::db_to_linear(base->gain); |
46 | |
47 | float mix = base->mix; |
48 | float gr_meter_decay = exp(1 / (1 * sample_rate)); |
49 | |
50 | const AudioFrame *src = p_src_frames; |
51 | |
52 | if (base->sidechain != StringName() && current_channel != -1) { |
53 | int bus = AudioServer::get_singleton()->thread_find_bus_index(base->sidechain); |
54 | if (bus >= 0) { |
55 | src = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus, current_channel); |
56 | } |
57 | } |
58 | |
59 | for (int i = 0; i < p_frame_count; i++) { |
60 | AudioFrame s = src[i]; |
61 | //convert to positive |
62 | s.l = Math::abs(s.l); |
63 | s.r = Math::abs(s.r); |
64 | |
65 | float peak = MAX(s.l, s.r); |
66 | |
67 | float overdb = 2.08136898f * Math::linear_to_db(peak / threshold); |
68 | |
69 | if (overdb < 0.0) { //we only care about what goes over to compress |
70 | overdb = 0.0; |
71 | } |
72 | |
73 | if (overdb - rundb > 5) { // diffeence is too large |
74 | averatio = 4; |
75 | } |
76 | |
77 | if (overdb > rundb) { |
78 | rundb = overdb + atcoef * (rundb - overdb); |
79 | runratio = averatio + ratatcoef * (runratio - averatio); |
80 | } else { |
81 | rundb = overdb + relcoef * (rundb - overdb); |
82 | runratio = averatio + ratrelcoef * (runratio - averatio); |
83 | } |
84 | |
85 | overdb = rundb; |
86 | averatio = runratio; |
87 | |
88 | float cratio; |
89 | |
90 | if (false) { //rato all-in |
91 | cratio = 12 + averatio; |
92 | } else { |
93 | cratio = base->ratio; |
94 | } |
95 | |
96 | float gr = -overdb * (cratio - 1) / cratio; |
97 | float grv = Math::db_to_linear(gr); |
98 | |
99 | runmax = maxover + relcoef * (runmax - maxover); // highest peak for setting att/rel decays in reltime |
100 | maxover = runmax; |
101 | |
102 | if (grv < gr_meter) { |
103 | gr_meter = grv; |
104 | } else { |
105 | gr_meter *= gr_meter_decay; |
106 | if (gr_meter > 1) { |
107 | gr_meter = 1; |
108 | } |
109 | } |
110 | |
111 | p_dst_frames[i] = p_src_frames[i] * grv * makeup * mix + p_src_frames[i] * (1.0 - mix); |
112 | } |
113 | } |
114 | |
115 | Ref<AudioEffectInstance> AudioEffectCompressor::instantiate() { |
116 | Ref<AudioEffectCompressorInstance> ins; |
117 | ins.instantiate(); |
118 | ins->base = Ref<AudioEffectCompressor>(this); |
119 | ins->rundb = 0; |
120 | ins->runratio = 0; |
121 | ins->averatio = 0; |
122 | ins->runmax = 0; |
123 | ins->maxover = 0; |
124 | ins->gr_meter = 1.0; |
125 | ins->current_channel = -1; |
126 | return ins; |
127 | } |
128 | |
129 | void AudioEffectCompressor::set_threshold(float p_threshold) { |
130 | threshold = p_threshold; |
131 | } |
132 | |
133 | float AudioEffectCompressor::get_threshold() const { |
134 | return threshold; |
135 | } |
136 | |
137 | void AudioEffectCompressor::set_ratio(float p_ratio) { |
138 | ratio = p_ratio; |
139 | } |
140 | |
141 | float AudioEffectCompressor::get_ratio() const { |
142 | return ratio; |
143 | } |
144 | |
145 | void AudioEffectCompressor::set_gain(float p_gain) { |
146 | gain = p_gain; |
147 | } |
148 | |
149 | float AudioEffectCompressor::get_gain() const { |
150 | return gain; |
151 | } |
152 | |
153 | void AudioEffectCompressor::set_attack_us(float p_attack_us) { |
154 | attack_us = p_attack_us; |
155 | } |
156 | |
157 | float AudioEffectCompressor::get_attack_us() const { |
158 | return attack_us; |
159 | } |
160 | |
161 | void AudioEffectCompressor::set_release_ms(float p_release_ms) { |
162 | release_ms = p_release_ms; |
163 | } |
164 | |
165 | float AudioEffectCompressor::get_release_ms() const { |
166 | return release_ms; |
167 | } |
168 | |
169 | void AudioEffectCompressor::set_mix(float p_mix) { |
170 | mix = p_mix; |
171 | } |
172 | |
173 | float AudioEffectCompressor::get_mix() const { |
174 | return mix; |
175 | } |
176 | |
177 | void AudioEffectCompressor::set_sidechain(const StringName &p_sidechain) { |
178 | AudioServer::get_singleton()->lock(); |
179 | sidechain = p_sidechain; |
180 | AudioServer::get_singleton()->unlock(); |
181 | } |
182 | |
183 | StringName AudioEffectCompressor::get_sidechain() const { |
184 | return sidechain; |
185 | } |
186 | |
187 | void AudioEffectCompressor::_validate_property(PropertyInfo &p_property) const { |
188 | if (p_property.name == "sidechain" ) { |
189 | String buses = "" ; |
190 | for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { |
191 | buses += "," ; |
192 | buses += AudioServer::get_singleton()->get_bus_name(i); |
193 | } |
194 | |
195 | p_property.hint_string = buses; |
196 | } |
197 | } |
198 | |
199 | void AudioEffectCompressor::_bind_methods() { |
200 | ClassDB::bind_method(D_METHOD("set_threshold" , "threshold" ), &AudioEffectCompressor::set_threshold); |
201 | ClassDB::bind_method(D_METHOD("get_threshold" ), &AudioEffectCompressor::get_threshold); |
202 | |
203 | ClassDB::bind_method(D_METHOD("set_ratio" , "ratio" ), &AudioEffectCompressor::set_ratio); |
204 | ClassDB::bind_method(D_METHOD("get_ratio" ), &AudioEffectCompressor::get_ratio); |
205 | |
206 | ClassDB::bind_method(D_METHOD("set_gain" , "gain" ), &AudioEffectCompressor::set_gain); |
207 | ClassDB::bind_method(D_METHOD("get_gain" ), &AudioEffectCompressor::get_gain); |
208 | |
209 | ClassDB::bind_method(D_METHOD("set_attack_us" , "attack_us" ), &AudioEffectCompressor::set_attack_us); |
210 | ClassDB::bind_method(D_METHOD("get_attack_us" ), &AudioEffectCompressor::get_attack_us); |
211 | |
212 | ClassDB::bind_method(D_METHOD("set_release_ms" , "release_ms" ), &AudioEffectCompressor::set_release_ms); |
213 | ClassDB::bind_method(D_METHOD("get_release_ms" ), &AudioEffectCompressor::get_release_ms); |
214 | |
215 | ClassDB::bind_method(D_METHOD("set_mix" , "mix" ), &AudioEffectCompressor::set_mix); |
216 | ClassDB::bind_method(D_METHOD("get_mix" ), &AudioEffectCompressor::get_mix); |
217 | |
218 | ClassDB::bind_method(D_METHOD("set_sidechain" , "sidechain" ), &AudioEffectCompressor::set_sidechain); |
219 | ClassDB::bind_method(D_METHOD("get_sidechain" ), &AudioEffectCompressor::get_sidechain); |
220 | |
221 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "threshold" , PROPERTY_HINT_RANGE, "-60,0,0.1" ), "set_threshold" , "get_threshold" ); |
222 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio" , PROPERTY_HINT_RANGE, "1,48,0.1" ), "set_ratio" , "get_ratio" ); |
223 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gain" , PROPERTY_HINT_RANGE, "-20,20,0.1" ), "set_gain" , "get_gain" ); |
224 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attack_us" , PROPERTY_HINT_RANGE, U"20,2000,1,suffix:\u00B5s" ), "set_attack_us" , "get_attack_us" ); |
225 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "release_ms" , PROPERTY_HINT_RANGE, "20,2000,1,suffix:ms" ), "set_release_ms" , "get_release_ms" ); |
226 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mix" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_mix" , "get_mix" ); |
227 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "sidechain" , PROPERTY_HINT_ENUM), "set_sidechain" , "get_sidechain" ); |
228 | } |
229 | |
230 | AudioEffectCompressor::AudioEffectCompressor() { |
231 | threshold = 0; |
232 | ratio = 4; |
233 | gain = 0; |
234 | attack_us = 20; |
235 | release_ms = 250; |
236 | mix = 1; |
237 | } |
238 | |