reverb.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************/
  2. /* reverb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. // Author: Juan Linietsky <[email protected]>, (C) 2006
  31. #include "reverb.h"
  32. #include "core/math/math_funcs.h"
  33. #include <math.h>
  34. const float Reverb::comb_tunings[MAX_COMBS] = {
  35. //freeverb comb tunings
  36. 0.025306122448979593f,
  37. 0.026938775510204082f,
  38. 0.028956916099773241f,
  39. 0.03074829931972789f,
  40. 0.032244897959183672f,
  41. 0.03380952380952381f,
  42. 0.035306122448979592f,
  43. 0.036666666666666667f
  44. };
  45. const float Reverb::allpass_tunings[MAX_ALLPASS] = {
  46. //freeverb allpass tunings
  47. 0.0051020408163265302f,
  48. 0.007732426303854875f,
  49. 0.01f,
  50. 0.012607709750566893f
  51. };
  52. void Reverb::process(float *p_src, float *p_dst, int p_frames) {
  53. if (p_frames > INPUT_BUFFER_MAX_SIZE)
  54. p_frames = INPUT_BUFFER_MAX_SIZE;
  55. int predelay_frames = lrint((params.predelay / 1000.0) * params.mix_rate);
  56. if (predelay_frames < 10)
  57. predelay_frames = 10;
  58. if (predelay_frames >= echo_buffer_size)
  59. predelay_frames = echo_buffer_size - 1;
  60. for (int i = 0; i < p_frames; i++) {
  61. if (echo_buffer_pos >= echo_buffer_size)
  62. echo_buffer_pos = 0;
  63. int read_pos = echo_buffer_pos - predelay_frames;
  64. while (read_pos < 0)
  65. read_pos += echo_buffer_size;
  66. float in = undenormalise(echo_buffer[read_pos] * params.predelay_fb + p_src[i]);
  67. echo_buffer[echo_buffer_pos] = in;
  68. input_buffer[i] = in;
  69. p_dst[i] = 0; //take the chance and clear this
  70. echo_buffer_pos++;
  71. }
  72. if (params.hpf > 0) {
  73. float hpaux = expf(-2.0 * Math_PI * params.hpf * 6000 / params.mix_rate);
  74. float hp_a1 = (1.0 + hpaux) / 2.0;
  75. float hp_a2 = -(1.0 + hpaux) / 2.0;
  76. float hp_b1 = hpaux;
  77. for (int i = 0; i < p_frames; i++) {
  78. float in = input_buffer[i];
  79. input_buffer[i] = in * hp_a1 + hpf_h1 * hp_a2 + hpf_h2 * hp_b1;
  80. hpf_h2 = input_buffer[i];
  81. hpf_h1 = in;
  82. }
  83. }
  84. for (int i = 0; i < MAX_COMBS; i++) {
  85. Comb &c = comb[i];
  86. int size_limit = c.size - lrintf((float)c.extra_spread_frames * (1.0 - params.extra_spread));
  87. for (int j = 0; j < p_frames; j++) {
  88. if (c.pos >= size_limit) //reset this now just in case
  89. c.pos = 0;
  90. float out = undenormalise(c.buffer[c.pos] * c.feedback);
  91. out = out * (1.0 - c.damp) + c.damp_h * c.damp; //lowpass
  92. c.damp_h = out;
  93. c.buffer[c.pos] = input_buffer[j] + out;
  94. p_dst[j] += out;
  95. c.pos++;
  96. }
  97. }
  98. static const float allpass_feedback = 0.7;
  99. /* this one works, but the other version is just nicer....
  100. int ap_size_limit[MAX_ALLPASS];
  101. for (int i=0;i<MAX_ALLPASS;i++) {
  102. AllPass &a=allpass[i];
  103. ap_size_limit[i]=a.size-lrintf((float)a.extra_spread_frames*(1.0-params.extra_spread));
  104. }
  105. for (int i=0;i<p_frames;i++) {
  106. float sample=p_dst[i];
  107. float aux,in;
  108. float AllPass*ap;
  109. #define PROCESS_ALLPASS(m_ap) \
  110. ap=&allpass[m_ap]; \
  111. if (ap->pos>=ap_size_limit[m_ap]) \
  112. ap->pos=0; \
  113. aux=undenormalise(ap->buffer[ap->pos]); \
  114. in=sample; \
  115. sample=-in+aux; \
  116. ap->pos++;
  117. PROCESS_ALLPASS(0);
  118. PROCESS_ALLPASS(1);
  119. PROCESS_ALLPASS(2);
  120. PROCESS_ALLPASS(3);
  121. p_dst[i]=sample;
  122. }
  123. */
  124. for (int i = 0; i < MAX_ALLPASS; i++) {
  125. AllPass &a = allpass[i];
  126. int size_limit = a.size - lrintf((float)a.extra_spread_frames * (1.0 - params.extra_spread));
  127. for (int j = 0; j < p_frames; j++) {
  128. if (a.pos >= size_limit)
  129. a.pos = 0;
  130. float aux = a.buffer[a.pos];
  131. a.buffer[a.pos] = undenormalise(allpass_feedback * aux + p_dst[j]);
  132. p_dst[j] = aux - allpass_feedback * a.buffer[a.pos];
  133. a.pos++;
  134. }
  135. }
  136. static const float wet_scale = 0.6;
  137. for (int i = 0; i < p_frames; i++) {
  138. p_dst[i] = p_dst[i] * params.wet * wet_scale + p_src[i] * params.dry;
  139. }
  140. }
  141. void Reverb::set_room_size(float p_size) {
  142. params.room_size = p_size;
  143. update_parameters();
  144. }
  145. void Reverb::set_damp(float p_damp) {
  146. params.damp = p_damp;
  147. update_parameters();
  148. }
  149. void Reverb::set_wet(float p_wet) {
  150. params.wet = p_wet;
  151. }
  152. void Reverb::set_dry(float p_dry) {
  153. params.dry = p_dry;
  154. }
  155. void Reverb::set_predelay(float p_predelay) {
  156. params.predelay = p_predelay;
  157. }
  158. void Reverb::set_predelay_feedback(float p_predelay_fb) {
  159. params.predelay_fb = p_predelay_fb;
  160. }
  161. void Reverb::set_highpass(float p_frq) {
  162. if (p_frq > 1)
  163. p_frq = 1;
  164. if (p_frq < 0)
  165. p_frq = 0;
  166. params.hpf = p_frq;
  167. }
  168. void Reverb::set_extra_spread(float p_spread) {
  169. params.extra_spread = p_spread;
  170. }
  171. void Reverb::set_mix_rate(float p_mix_rate) {
  172. params.mix_rate = p_mix_rate;
  173. configure_buffers();
  174. }
  175. void Reverb::set_extra_spread_base(float p_sec) {
  176. params.extra_spread_base = p_sec;
  177. configure_buffers();
  178. }
  179. void Reverb::configure_buffers() {
  180. clear_buffers(); //clear if necessary
  181. for (int i = 0; i < MAX_COMBS; i++) {
  182. Comb &c = comb[i];
  183. c.extra_spread_frames = lrint(params.extra_spread_base * params.mix_rate);
  184. int len = lrint(comb_tunings[i] * params.mix_rate) + c.extra_spread_frames;
  185. if (len < 5)
  186. len = 5; //may this happen?
  187. c.buffer = memnew_arr(float, len);
  188. c.pos = 0;
  189. for (int j = 0; j < len; j++)
  190. c.buffer[j] = 0;
  191. c.size = len;
  192. }
  193. for (int i = 0; i < MAX_ALLPASS; i++) {
  194. AllPass &a = allpass[i];
  195. a.extra_spread_frames = lrint(params.extra_spread_base * params.mix_rate);
  196. int len = lrint(allpass_tunings[i] * params.mix_rate) + a.extra_spread_frames;
  197. if (len < 5)
  198. len = 5; //may this happen?
  199. a.buffer = memnew_arr(float, len);
  200. a.pos = 0;
  201. for (int j = 0; j < len; j++)
  202. a.buffer[j] = 0;
  203. a.size = len;
  204. }
  205. echo_buffer_size = (int)(((float)MAX_ECHO_MS / 1000.0) * params.mix_rate + 1.0);
  206. echo_buffer = memnew_arr(float, echo_buffer_size);
  207. for (int i = 0; i < echo_buffer_size; i++) {
  208. echo_buffer[i] = 0;
  209. }
  210. echo_buffer_pos = 0;
  211. }
  212. void Reverb::update_parameters() {
  213. //more freeverb derived constants
  214. static const float room_scale = 0.28f;
  215. static const float room_offset = 0.7f;
  216. for (int i = 0; i < MAX_COMBS; i++) {
  217. Comb &c = comb[i];
  218. c.feedback = room_offset + params.room_size * room_scale;
  219. if (c.feedback < room_offset)
  220. c.feedback = room_offset;
  221. else if (c.feedback > (room_offset + room_scale))
  222. c.feedback = (room_offset + room_scale);
  223. float auxdmp = params.damp / 2.0 + 0.5; //only half the range (0.5 .. 1.0 is enough)
  224. auxdmp *= auxdmp;
  225. c.damp = expf(-2.0 * Math_PI * auxdmp * 10000 / params.mix_rate); // 0 .. 10khz
  226. }
  227. }
  228. void Reverb::clear_buffers() {
  229. if (echo_buffer)
  230. memdelete_arr(echo_buffer);
  231. for (int i = 0; i < MAX_COMBS; i++) {
  232. if (comb[i].buffer)
  233. memdelete_arr(comb[i].buffer);
  234. comb[i].buffer = 0;
  235. }
  236. for (int i = 0; i < MAX_ALLPASS; i++) {
  237. if (allpass[i].buffer)
  238. memdelete_arr(allpass[i].buffer);
  239. allpass[i].buffer = 0;
  240. }
  241. }
  242. Reverb::Reverb() {
  243. params.room_size = 0.8;
  244. params.damp = 0.5;
  245. params.dry = 1.0;
  246. params.wet = 0.0;
  247. params.mix_rate = 44100;
  248. params.extra_spread_base = 0;
  249. params.extra_spread = 1.0;
  250. params.predelay = 150;
  251. params.predelay_fb = 0.4;
  252. params.hpf = 0;
  253. hpf_h1 = 0;
  254. hpf_h2 = 0;
  255. input_buffer = memnew_arr(float, INPUT_BUFFER_MAX_SIZE);
  256. echo_buffer = 0;
  257. configure_buffers();
  258. update_parameters();
  259. }
  260. Reverb::~Reverb() {
  261. memdelete_arr(input_buffer);
  262. clear_buffers();
  263. }