audio_effect_reverb.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* audio_effect_reverb.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_reverb.h"
  31. #include "servers/audio_server.h"
  32. void AudioEffectReverbInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  33. for (int i = 0; i < 2; i++) {
  34. Reverb &r = reverb[i];
  35. r.set_predelay(base->predelay);
  36. r.set_predelay_feedback(base->predelay_fb);
  37. r.set_highpass(base->hpf);
  38. r.set_room_size(base->room_size);
  39. r.set_damp(base->damping);
  40. r.set_extra_spread(base->spread);
  41. r.set_wet(base->wet);
  42. r.set_dry(base->dry);
  43. }
  44. int todo = p_frame_count;
  45. int offset = 0;
  46. while (todo) {
  47. int to_mix = MIN(todo, Reverb::INPUT_BUFFER_MAX_SIZE);
  48. for (int j = 0; j < to_mix; j++) {
  49. tmp_src[j] = p_src_frames[offset + j].left;
  50. }
  51. reverb[0].process(tmp_src, tmp_dst, to_mix);
  52. for (int j = 0; j < to_mix; j++) {
  53. p_dst_frames[offset + j].left = tmp_dst[j];
  54. tmp_src[j] = p_src_frames[offset + j].right;
  55. }
  56. reverb[1].process(tmp_src, tmp_dst, to_mix);
  57. for (int j = 0; j < to_mix; j++) {
  58. p_dst_frames[offset + j].right = tmp_dst[j];
  59. }
  60. offset += to_mix;
  61. todo -= to_mix;
  62. }
  63. }
  64. AudioEffectReverbInstance::AudioEffectReverbInstance() {
  65. reverb[0].set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  66. reverb[0].set_extra_spread_base(0);
  67. reverb[1].set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  68. reverb[1].set_extra_spread_base(0.000521); //for stereo effect
  69. }
  70. Ref<AudioEffectInstance> AudioEffectReverb::instantiate() {
  71. Ref<AudioEffectReverbInstance> ins;
  72. ins.instantiate();
  73. ins->base = Ref<AudioEffectReverb>(this);
  74. return ins;
  75. }
  76. void AudioEffectReverb::set_predelay_msec(float p_msec) {
  77. predelay = p_msec;
  78. }
  79. void AudioEffectReverb::set_predelay_feedback(float p_feedback) {
  80. predelay_fb = CLAMP(p_feedback, 0, 0.98);
  81. }
  82. void AudioEffectReverb::set_room_size(float p_size) {
  83. room_size = p_size;
  84. }
  85. void AudioEffectReverb::set_damping(float p_damping) {
  86. damping = p_damping;
  87. }
  88. void AudioEffectReverb::set_spread(float p_spread) {
  89. spread = p_spread;
  90. }
  91. void AudioEffectReverb::set_dry(float p_dry) {
  92. dry = p_dry;
  93. }
  94. void AudioEffectReverb::set_wet(float p_wet) {
  95. wet = p_wet;
  96. }
  97. void AudioEffectReverb::set_hpf(float p_hpf) {
  98. hpf = p_hpf;
  99. }
  100. float AudioEffectReverb::get_predelay_msec() const {
  101. return predelay;
  102. }
  103. float AudioEffectReverb::get_predelay_feedback() const {
  104. return predelay_fb;
  105. }
  106. float AudioEffectReverb::get_room_size() const {
  107. return room_size;
  108. }
  109. float AudioEffectReverb::get_damping() const {
  110. return damping;
  111. }
  112. float AudioEffectReverb::get_spread() const {
  113. return spread;
  114. }
  115. float AudioEffectReverb::get_dry() const {
  116. return dry;
  117. }
  118. float AudioEffectReverb::get_wet() const {
  119. return wet;
  120. }
  121. float AudioEffectReverb::get_hpf() const {
  122. return hpf;
  123. }
  124. void AudioEffectReverb::_bind_methods() {
  125. ClassDB::bind_method(D_METHOD("set_predelay_msec", "msec"), &AudioEffectReverb::set_predelay_msec);
  126. ClassDB::bind_method(D_METHOD("get_predelay_msec"), &AudioEffectReverb::get_predelay_msec);
  127. ClassDB::bind_method(D_METHOD("set_predelay_feedback", "feedback"), &AudioEffectReverb::set_predelay_feedback);
  128. ClassDB::bind_method(D_METHOD("get_predelay_feedback"), &AudioEffectReverb::get_predelay_feedback);
  129. ClassDB::bind_method(D_METHOD("set_room_size", "size"), &AudioEffectReverb::set_room_size);
  130. ClassDB::bind_method(D_METHOD("get_room_size"), &AudioEffectReverb::get_room_size);
  131. ClassDB::bind_method(D_METHOD("set_damping", "amount"), &AudioEffectReverb::set_damping);
  132. ClassDB::bind_method(D_METHOD("get_damping"), &AudioEffectReverb::get_damping);
  133. ClassDB::bind_method(D_METHOD("set_spread", "amount"), &AudioEffectReverb::set_spread);
  134. ClassDB::bind_method(D_METHOD("get_spread"), &AudioEffectReverb::get_spread);
  135. ClassDB::bind_method(D_METHOD("set_dry", "amount"), &AudioEffectReverb::set_dry);
  136. ClassDB::bind_method(D_METHOD("get_dry"), &AudioEffectReverb::get_dry);
  137. ClassDB::bind_method(D_METHOD("set_wet", "amount"), &AudioEffectReverb::set_wet);
  138. ClassDB::bind_method(D_METHOD("get_wet"), &AudioEffectReverb::get_wet);
  139. ClassDB::bind_method(D_METHOD("set_hpf", "amount"), &AudioEffectReverb::set_hpf);
  140. ClassDB::bind_method(D_METHOD("get_hpf"), &AudioEffectReverb::get_hpf);
  141. ADD_GROUP("Predelay", "predelay_");
  142. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "predelay_msec", PROPERTY_HINT_RANGE, "20,500,1,suffix:ms"), "set_predelay_msec", "get_predelay_msec");
  143. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "predelay_feedback", PROPERTY_HINT_RANGE, "0,0.98,0.01"), "set_predelay_feedback", "get_predelay_feedback");
  144. ADD_GROUP("", "");
  145. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "room_size", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_room_size", "get_room_size");
  146. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_damping", "get_damping");
  147. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_spread", "get_spread");
  148. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "hipass", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_hpf", "get_hpf");
  149. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dry", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dry", "get_dry");
  150. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wet", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_wet", "get_wet");
  151. }
  152. AudioEffectReverb::AudioEffectReverb() {
  153. predelay = 150;
  154. predelay_fb = 0.4;
  155. hpf = 0;
  156. room_size = 0.8;
  157. damping = 0.5;
  158. spread = 1.0;
  159. dry = 1.0;
  160. wet = 0.5;
  161. }