audio_effect_compressor.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*************************************************************************/
  2. /* audio_effect_compressor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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::db2linear(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::db2linear(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.l = Math::abs(s.l);
  55. s.r = Math::abs(s.r);
  56. float peak = MAX(s.l, s.r);
  57. float overdb = 2.08136898f * Math::linear2db(peak / threshold);
  58. if (overdb < 0.0) //we only care about what goes over to compress
  59. overdb = 0.0;
  60. if (overdb - rundb > 5) // diffeence is too large
  61. averatio = 4;
  62. if (overdb > rundb) {
  63. rundb = overdb + atcoef * (rundb - overdb);
  64. runratio = averatio + ratatcoef * (runratio - averatio);
  65. } else {
  66. rundb = overdb + relcoef * (rundb - overdb);
  67. runratio = averatio + ratrelcoef * (runratio - averatio);
  68. }
  69. overdb = rundb;
  70. averatio = runratio;
  71. float cratio;
  72. if (false) { //rato all-in
  73. cratio = 12 + averatio;
  74. } else {
  75. cratio = base->ratio;
  76. }
  77. float gr = -overdb * (cratio - 1) / cratio;
  78. float grv = Math::db2linear(gr);
  79. runmax = maxover + relcoef * (runmax - maxover); // highest peak for setting att/rel decays in reltime
  80. maxover = runmax;
  81. if (grv < gr_meter) {
  82. gr_meter = grv;
  83. } else {
  84. gr_meter *= gr_meter_decay;
  85. if (gr_meter > 1)
  86. gr_meter = 1;
  87. }
  88. p_dst_frames[i] = p_src_frames[i] * grv * makeup * mix + p_src_frames[i] * (1.0 - mix);
  89. }
  90. }
  91. Ref<AudioEffectInstance> AudioEffectCompressor::instance() {
  92. Ref<AudioEffectCompressorInstance> ins;
  93. ins.instance();
  94. ins->base = Ref<AudioEffectCompressor>(this);
  95. ins->rundb = 0;
  96. ins->runratio = 0;
  97. ins->averatio = 0;
  98. ins->runmax = 0;
  99. ins->maxover = 0;
  100. ins->gr_meter = 1.0;
  101. ins->current_channel = -1;
  102. return ins;
  103. }
  104. void AudioEffectCompressor::set_threshold(float p_threshold) {
  105. threshold = p_threshold;
  106. }
  107. float AudioEffectCompressor::get_threshold() const {
  108. return threshold;
  109. }
  110. void AudioEffectCompressor::set_ratio(float p_ratio) {
  111. ratio = p_ratio;
  112. }
  113. float AudioEffectCompressor::get_ratio() const {
  114. return ratio;
  115. }
  116. void AudioEffectCompressor::set_gain(float p_gain) {
  117. gain = p_gain;
  118. }
  119. float AudioEffectCompressor::get_gain() const {
  120. return gain;
  121. }
  122. void AudioEffectCompressor::set_attack_us(float p_attack_us) {
  123. attack_us = p_attack_us;
  124. }
  125. float AudioEffectCompressor::get_attack_us() const {
  126. return attack_us;
  127. }
  128. void AudioEffectCompressor::set_release_ms(float p_release_ms) {
  129. release_ms = p_release_ms;
  130. }
  131. float AudioEffectCompressor::get_release_ms() const {
  132. return release_ms;
  133. }
  134. void AudioEffectCompressor::set_mix(float p_mix) {
  135. mix = p_mix;
  136. }
  137. float AudioEffectCompressor::get_mix() const {
  138. return mix;
  139. }
  140. void AudioEffectCompressor::set_sidechain(const StringName &p_sidechain) {
  141. AudioServer::get_singleton()->lock();
  142. sidechain = p_sidechain;
  143. AudioServer::get_singleton()->unlock();
  144. }
  145. StringName AudioEffectCompressor::get_sidechain() const {
  146. return sidechain;
  147. }
  148. void AudioEffectCompressor::_validate_property(PropertyInfo &property) const {
  149. if (property.name == "sidechain") {
  150. String buses = "";
  151. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  152. buses += ",";
  153. buses += AudioServer::get_singleton()->get_bus_name(i);
  154. }
  155. property.hint_string = buses;
  156. }
  157. }
  158. void AudioEffectCompressor::_bind_methods() {
  159. ClassDB::bind_method(D_METHOD("set_threshold", "threshold"), &AudioEffectCompressor::set_threshold);
  160. ClassDB::bind_method(D_METHOD("get_threshold"), &AudioEffectCompressor::get_threshold);
  161. ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AudioEffectCompressor::set_ratio);
  162. ClassDB::bind_method(D_METHOD("get_ratio"), &AudioEffectCompressor::get_ratio);
  163. ClassDB::bind_method(D_METHOD("set_gain", "gain"), &AudioEffectCompressor::set_gain);
  164. ClassDB::bind_method(D_METHOD("get_gain"), &AudioEffectCompressor::get_gain);
  165. ClassDB::bind_method(D_METHOD("set_attack_us", "attack_us"), &AudioEffectCompressor::set_attack_us);
  166. ClassDB::bind_method(D_METHOD("get_attack_us"), &AudioEffectCompressor::get_attack_us);
  167. ClassDB::bind_method(D_METHOD("set_release_ms", "release_ms"), &AudioEffectCompressor::set_release_ms);
  168. ClassDB::bind_method(D_METHOD("get_release_ms"), &AudioEffectCompressor::get_release_ms);
  169. ClassDB::bind_method(D_METHOD("set_mix", "mix"), &AudioEffectCompressor::set_mix);
  170. ClassDB::bind_method(D_METHOD("get_mix"), &AudioEffectCompressor::get_mix);
  171. ClassDB::bind_method(D_METHOD("set_sidechain", "sidechain"), &AudioEffectCompressor::set_sidechain);
  172. ClassDB::bind_method(D_METHOD("get_sidechain"), &AudioEffectCompressor::get_sidechain);
  173. ADD_PROPERTY(PropertyInfo(Variant::REAL, "threshold", PROPERTY_HINT_RANGE, "-60,0,0.1"), "set_threshold", "get_threshold");
  174. ADD_PROPERTY(PropertyInfo(Variant::REAL, "ratio", PROPERTY_HINT_RANGE, "1,48,0.1"), "set_ratio", "get_ratio");
  175. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gain", PROPERTY_HINT_RANGE, "-20,20,0.1"), "set_gain", "get_gain");
  176. ADD_PROPERTY(PropertyInfo(Variant::REAL, "attack_us", PROPERTY_HINT_RANGE, "20,2000,1"), "set_attack_us", "get_attack_us");
  177. ADD_PROPERTY(PropertyInfo(Variant::REAL, "release_ms", PROPERTY_HINT_RANGE, "20,2000,1"), "set_release_ms", "get_release_ms");
  178. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mix", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_mix", "get_mix");
  179. ADD_PROPERTY(PropertyInfo(Variant::STRING, "sidechain", PROPERTY_HINT_ENUM), "set_sidechain", "get_sidechain");
  180. }
  181. AudioEffectCompressor::AudioEffectCompressor() {
  182. threshold = 0;
  183. ratio = 4;
  184. gain = 0;
  185. attack_us = 20;
  186. release_ms = 250;
  187. mix = 1;
  188. }