audio_effect_compressor.cpp 8.5 KB

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