audio_effect_hard_limiter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* audio_effect_hard_limiter.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_hard_limiter.h"
  31. #include "servers/audio_server.h"
  32. void AudioEffectHardLimiterInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  33. float sample_rate = AudioServer::get_singleton()->get_mix_rate();
  34. float ceiling = Math::db_to_linear(base->ceiling);
  35. float release = base->release;
  36. float attack = base->attack;
  37. float pre_gain = Math::db_to_linear(base->pre_gain);
  38. for (int i = 0; i < p_frame_count; i++) {
  39. float sample_left = p_src_frames[i].left;
  40. float sample_right = p_src_frames[i].right;
  41. sample_left *= pre_gain;
  42. sample_right *= pre_gain;
  43. float largest_sample = MAX(ABS(sample_left), ABS(sample_right));
  44. release_factor = MAX(0.0, release_factor - 1.0 / sample_rate);
  45. release_factor = MIN(release_factor, release);
  46. if (release_factor > 0.0) {
  47. gain = Math::lerp(gain_target, 1.0f, 1.0f - release_factor / release);
  48. }
  49. if (largest_sample * gain > ceiling) {
  50. gain_target = ceiling / largest_sample;
  51. release_factor = release;
  52. attack_factor = attack;
  53. }
  54. // Lerp gain over attack time to avoid distortion.
  55. attack_factor = MAX(0.0f, attack_factor - 1.0f / sample_rate);
  56. if (attack_factor > 0.0) {
  57. gain = Math::lerp(gain_target, gain, 1.0f - attack_factor / attack);
  58. }
  59. int bucket_id = gain_bucket_cursor / gain_bucket_size;
  60. // If first item within the current bucket, reset the bucket.
  61. if (gain_bucket_cursor % gain_bucket_size == 0) {
  62. gain_buckets[bucket_id] = 1.0f;
  63. }
  64. gain_buckets[bucket_id] = MIN(gain_buckets[bucket_id], gain);
  65. gain_bucket_cursor = (gain_bucket_cursor + 1) % gain_samples_to_store;
  66. for (int j = 0; j < (int)gain_buckets.size(); j++) {
  67. gain = MIN(gain, gain_buckets[j]);
  68. }
  69. // Introduce latency by grabbing the AudioFrame stored previously,
  70. // then overwrite it with current audioframe, then update circular
  71. // buffer cursor.
  72. float dst_buffer_left = sample_buffer_left[sample_cursor];
  73. float dst_buffer_right = sample_buffer_right[sample_cursor];
  74. sample_buffer_left[sample_cursor] = sample_left;
  75. sample_buffer_right[sample_cursor] = sample_right;
  76. sample_cursor = (sample_cursor + 1) % sample_buffer_left.size();
  77. p_dst_frames[i].left = dst_buffer_left * gain;
  78. p_dst_frames[i].right = dst_buffer_right * gain;
  79. }
  80. }
  81. Ref<AudioEffectInstance> AudioEffectHardLimiter::instantiate() {
  82. Ref<AudioEffectHardLimiterInstance> ins;
  83. ins.instantiate();
  84. ins->base = Ref<AudioEffectHardLimiter>(this);
  85. float mix_rate = AudioServer::get_singleton()->get_mix_rate();
  86. for (int i = 0; i < (int)Math::ceil(mix_rate * attack) + 1; i++) {
  87. ins->sample_buffer_left.push_back(0.0f);
  88. ins->sample_buffer_right.push_back(0.0f);
  89. }
  90. ins->gain_samples_to_store = (int)Math::ceil(mix_rate * (attack + sustain) + 1);
  91. ins->gain_bucket_size = (int)(mix_rate * attack);
  92. for (int i = 0; i < ins->gain_samples_to_store; i += ins->gain_bucket_size) {
  93. ins->gain_buckets.push_back(1.0f);
  94. }
  95. return ins;
  96. }
  97. void AudioEffectHardLimiter::set_ceiling_db(float p_ceiling) {
  98. ceiling = p_ceiling;
  99. }
  100. float AudioEffectHardLimiter::get_ceiling_db() const {
  101. return ceiling;
  102. }
  103. float AudioEffectHardLimiter::get_pre_gain_db() const {
  104. return pre_gain;
  105. }
  106. void AudioEffectHardLimiter::set_pre_gain_db(const float p_pre_gain) {
  107. pre_gain = p_pre_gain;
  108. }
  109. float AudioEffectHardLimiter::get_release() const {
  110. return release;
  111. }
  112. void AudioEffectHardLimiter::set_release(const float p_release) {
  113. release = p_release;
  114. }
  115. void AudioEffectHardLimiter::_bind_methods() {
  116. ClassDB::bind_method(D_METHOD("set_ceiling_db", "ceiling"), &AudioEffectHardLimiter::set_ceiling_db);
  117. ClassDB::bind_method(D_METHOD("get_ceiling_db"), &AudioEffectHardLimiter::get_ceiling_db);
  118. ClassDB::bind_method(D_METHOD("set_pre_gain_db", "p_pre_gain"), &AudioEffectHardLimiter::set_pre_gain_db);
  119. ClassDB::bind_method(D_METHOD("get_pre_gain_db"), &AudioEffectHardLimiter::get_pre_gain_db);
  120. ClassDB::bind_method(D_METHOD("set_release", "p_release"), &AudioEffectHardLimiter::set_release);
  121. ClassDB::bind_method(D_METHOD("get_release"), &AudioEffectHardLimiter::get_release);
  122. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pre_gain_db", PROPERTY_HINT_RANGE, "-24,24,0.01,suffix:dB"), "set_pre_gain_db", "get_pre_gain_db");
  123. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ceiling_db", PROPERTY_HINT_RANGE, "-24,0.0,0.01,suffix:dB"), "set_ceiling_db", "get_ceiling_db");
  124. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "release", PROPERTY_HINT_RANGE, "0.01,3,0.01"), "set_release", "get_release");
  125. }