reverb_sw.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*************************************************************************/
  2. /* reverb_sw.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. #include "reverb_sw.h"
  31. #include "core/print_string.h"
  32. #include <stdlib.h>
  33. #define SETMIN(x, y) (x) = MIN((x), (y))
  34. #define rangeloop(c, min, max) \
  35. for ((c) = (min); (c) < (max); (c)++)
  36. #define MULSHIFT_S32(Factor1, Factor2, Bits) \
  37. ((int)(((int64_t)(Factor1) * (Factor2)) >> (Bits)))
  38. struct ReverbParamsSW {
  39. unsigned int BufferSize; // Required buffer size
  40. int gLPF; // Coefficient
  41. int gEcho0; // Coefficient
  42. int gEcho1; // Coefficient
  43. int gEcho2; // Coefficient
  44. int gEcho3; // Coefficient
  45. int gWall; // Coefficient
  46. int gReva; // Coefficient
  47. int gRevb; // Coefficient
  48. int gInputL; // Coefficient
  49. int gInputR; // Coefficient
  50. unsigned int nRevaOldL; // Offset
  51. unsigned int nRevaOldR; // Offset
  52. unsigned int nRevbOldL; // Offset
  53. unsigned int nRevbOldR; // Offset
  54. unsigned int nLwlNew; // Offset
  55. unsigned int nRwrNew; // Offset
  56. unsigned int nEcho0L; // Offset
  57. unsigned int nEcho0R; // Offset
  58. unsigned int nEcho1L; // Offset
  59. unsigned int nEcho1R; // Offset
  60. unsigned int nLwlOld; // Offset
  61. unsigned int nRwrOld; // Offset
  62. unsigned int nLwrNew; // Offset
  63. unsigned int nRwlNew; // Offset
  64. unsigned int nEcho2L; // Offset
  65. unsigned int nEcho2R; // Offset
  66. unsigned int nEcho3L; // Offset
  67. unsigned int nEcho3R; // Offset
  68. unsigned int nLwrOld; // Offset
  69. unsigned int nRwlOld; // Offset
  70. unsigned int nRevaNewL; // Offset
  71. unsigned int nRevaNewR; // Offset
  72. unsigned int nRevbNewL; // Offset
  73. unsigned int nRevbNewR; // Offset
  74. };
  75. static ReverbParamsSW reverb_params_Room = {
  76. 0x26C0 / 2,
  77. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  78. 0x6D80, 0x54B8, -0x4130, 0x0000, 0x0000, -0x4580,
  79. //gReva gRevb gInputL gInputR
  80. 0x5800, 0x5300, -0x8000, -0x8000,
  81. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  82. 0x01B4 - 0x007D, 0x0136 - 0x007D, 0x00B8 - 0x005B, 0x005C - 0x005B,
  83. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  84. 0x04D6, 0x0333, 0x03F0, 0x0227, 0x0374, 0x01EF,
  85. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  86. 0x0334, 0x01B5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  87. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  88. 0x0000, 0x0000, 0x01B4, 0x0136, 0x00B8, 0x005C
  89. };
  90. static ReverbParamsSW reverb_params_StudioSmall = {
  91. 0x1F40 / 2,
  92. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  93. 0x70F0, 0x4FA8, -0x4320, 0x4410, -0x3F10, -0x6400,
  94. //gReva gRevb gInputL gInputR
  95. 0x5280, 0x4EC0, -0x8000, -0x8000,
  96. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  97. 0x00B4 - 0x0033, 0x0080 - 0x0033, 0x004C - 0x0025, 0x0026 - 0x0025,
  98. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  99. 0x03E4, 0x031B, 0x03A4, 0x02AF, 0x0372, 0x0266,
  100. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  101. 0x031C, 0x025D, 0x025C, 0x018E, 0x022F, 0x0135, 0x01D2, 0x00B7,
  102. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  103. 0x018F, 0x00B5, 0x00B4, 0x0080, 0x004C, 0x0026
  104. };
  105. static ReverbParamsSW reverb_params_StudioMedium = {
  106. 0x4840 / 2,
  107. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  108. 0x70F0, 0x4FA8, -0x4320, 0x4510, -0x4110, -0x4B40,
  109. //gReva gRevb gInputL gInputR
  110. 0x5280, 0x4EC0, -0x8000, -0x8000,
  111. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  112. 0x0264 - 0x00B1, 0x01B2 - 0x00B1, 0x0100 - 0x007F, 0x0080 - 0x007F,
  113. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  114. 0x0904, 0x076B, 0x0824, 0x065F, 0x07A2, 0x0616,
  115. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  116. 0x076C, 0x05ED, 0x05EC, 0x042E, 0x050F, 0x0305, 0x0462, 0x02B7,
  117. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  118. 0x042F, 0x0265, 0x0264, 0x01B2, 0x0100, 0x0080
  119. };
  120. static ReverbParamsSW reverb_params_StudioLarge = {
  121. 0x6FE0 / 2,
  122. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  123. 0x6F60, 0x4FA8, -0x4320, 0x4510, -0x4110, -0x5980,
  124. //gReva gRevb gInputL gInputR
  125. 0x5680, 0x52C0, -0x8000, -0x8000,
  126. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  127. 0x031C - 0x00E3, 0x0238 - 0x00E3, 0x0154 - 0x00A9, 0x00AA - 0x00A9,
  128. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  129. 0x0DFB, 0x0B58, 0x0D09, 0x0A3C, 0x0BD9, 0x0973,
  130. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  131. 0x0B59, 0x08DA, 0x08D9, 0x05E9, 0x07EC, 0x04B0, 0x06EF, 0x03D2,
  132. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  133. 0x05EA, 0x031D, 0x031C, 0x0238, 0x0154, 0x00AA
  134. };
  135. static ReverbParamsSW reverb_params_Hall = {
  136. 0xADE0 / 2,
  137. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  138. 0x6000, 0x5000, 0x4C00, -0x4800, -0x4400, -0x4000,
  139. //gReva gRevb gInputL gInputR
  140. 0x6000, 0x5C00, -0x8000, -0x8000,
  141. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  142. 0x05C0 - 0x01A5, 0x041A - 0x01A5, 0x0274 - 0x0139, 0x013A - 0x0139,
  143. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  144. 0x15BA, 0x11BB, 0x14C2, 0x10BD, 0x11BC, 0x0DC1,
  145. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  146. 0x11C0, 0x0DC3, 0x0DC0, 0x09C1, 0x0BC4, 0x07C1, 0x0A00, 0x06CD,
  147. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  148. 0x09C2, 0x05C1, 0x05C0, 0x041A, 0x0274, 0x013A
  149. };
  150. static ReverbParamsSW reverb_params_SpaceEcho = {
  151. 0xF6C0 / 2,
  152. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  153. 0x7E00, 0x5000, -0x4C00, -0x5000, 0x4C00, -0x5000,
  154. //gReva gRevb gInputL gInputR
  155. 0x6000, 0x5400, -0x8000, -0x8000,
  156. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  157. 0x0AE0 - 0x033D, 0x07A2 - 0x033D, 0x0464 - 0x0231, 0x0232 - 0x0231,
  158. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  159. 0x1ED6, 0x1A31, 0x1D14, 0x183B, 0x1BC2, 0x16B2,
  160. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  161. 0x1A32, 0x15EF, 0x15EE, 0x1055, 0x1334, 0x0F2D, 0x11F6, 0x0C5D,
  162. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  163. 0x1056, 0x0AE1, 0x0AE0, 0x07A2, 0x0464, 0x0232
  164. };
  165. static ReverbParamsSW reverb_params_Echo = {
  166. 0x18040 / 2,
  167. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  168. 0x7FFF, 0x7FFF, 0x0000, 0x0000, 0x0000, -0x7F00,
  169. //gReva gRevb gInputL gInputR
  170. 0x0000, 0x0000, -0x8000, -0x8000,
  171. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  172. 0x1004 - 0x0001, 0x1002 - 0x0001, 0x0004 - 0x0001, 0x0002 - 0x0001,
  173. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  174. 0x1FFF, 0x0FFF, 0x1005, 0x0005, 0x0000, 0x0000,
  175. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  176. 0x1005, 0x0005, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  177. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  178. 0x0000, 0x0000, 0x1004, 0x1002, 0x0004, 0x0002
  179. };
  180. static ReverbParamsSW reverb_params_Delay = {
  181. 0x18040 / 2,
  182. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  183. 0x7FFF, 0x7FFF, 0x0000, 0x0000, 0x0000, 0x0000,
  184. //gReva gRevb gInputL gInputR
  185. 0x0000, 0x0000, -0x8000, -0x8000,
  186. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  187. 0x1004 - 0x0001, 0x1002 - 0x0001, 0x0004 - 0x0001, 0x0002 - 0x0001,
  188. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  189. 0x1FFF, 0x0FFF, 0x1005, 0x0005, 0x0000, 0x0000,
  190. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  191. 0x1005, 0x0005, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  192. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  193. 0x0000, 0x0000, 0x1004, 0x1002, 0x0004, 0x0002
  194. };
  195. static ReverbParamsSW reverb_params_HalfEcho = {
  196. 0x3C00 / 2,
  197. //gLPF gEcho0 gEcho1 gEcho2 gEcho3 gWall
  198. 0x70F0, 0x4FA8, -0x4320, 0x4510, -0x4110, -0x7B00,
  199. //gReva gRevb gInputL gInputR
  200. 0x5F80, 0x54C0, -0x8000, -0x8000,
  201. //nRevaOldL nRevaOldR nRevbOldL nRevbOldR
  202. 0x0058 - 0x0017, 0x0040 - 0x0017, 0x0028 - 0x0013, 0x0014 - 0x0013,
  203. //nLwlNew nRwrNew nEcho0L nEcho0R nEcho1L nEcho1R
  204. 0x0371, 0x02AF, 0x02E5, 0x01DF, 0x02B0, 0x01D7,
  205. //nLwlOld nRwrOld nLwrNew nRwlNew nEcho2L nEcho2R nEcho3L nEcho3R
  206. 0x0358, 0x026A, 0x01D6, 0x011E, 0x012D, 0x00B1, 0x011F, 0x0059,
  207. //nLwrOld nRwlOld nRevaNewL nRevaNewR nRevbNewL nRevbNewR
  208. 0x01A0, 0x00E3, 0x0058, 0x0040, 0x0028, 0x0014
  209. };
  210. static ReverbParamsSW *reverb_param_modes[] = {
  211. &reverb_params_Room,
  212. &reverb_params_StudioSmall,
  213. &reverb_params_StudioMedium,
  214. &reverb_params_StudioLarge,
  215. &reverb_params_Hall,
  216. &reverb_params_SpaceEcho,
  217. &reverb_params_Echo,
  218. &reverb_params_Delay,
  219. &reverb_params_HalfEcho,
  220. };
  221. bool ReverbSW::process(int *p_input, int *p_output, int p_frames, int p_stereo_stride) {
  222. // p_input must point to a non-looping buffer.
  223. // BOTH p_input and p_output must be touched (use ClearModuleBuffer).
  224. if (!reverb_buffer)
  225. return false;
  226. // LOCAL MACROS
  227. #undef LM_SETSRCOFFSET
  228. #define LM_SETSRCOFFSET(x) \
  229. (x) = current_params->x + Offset; \
  230. if ((x) >= reverb_buffer_size) { \
  231. (x) -= reverb_buffer_size; \
  232. } \
  233. SETMIN(aSample, reverb_buffer_size - (x));
  234. /*
  235. #undef LM_SETSRCOFFSET2
  236. #define LM_SETSRCOFFSET2(x,y) \
  237. (x) = ((y) << 3) >> HZShift; \
  238. (x) += Offset; \
  239. if ( (x) >= reverb_buffer_size ) { \
  240. (x) -= reverb_buffer_size; \
  241. } \
  242. SETMIN ( aSample, reverb_buffer_size - (x) );
  243. */
  244. #undef LM_SRCADVANCE
  245. #define LM_SRCADVANCE(x) \
  246. (x) += aSample;
  247. #undef LM_MUL
  248. #define LM_MUL(x, y) \
  249. MULSHIFT_S32(x, current_params->y, 15)
  250. #undef LM_REVERB
  251. #define LM_REVERB(x) reverb_buffer[(x) + cSample]
  252. // LOCAL VARIABLES
  253. unsigned int Offset;
  254. int lwl, lwr, rwl, rwr;
  255. //unsigned char HZShift;
  256. // CODE
  257. lwl = state.lwl;
  258. lwr = state.lwr;
  259. rwl = state.rwl;
  260. rwr = state.rwr;
  261. Offset = state.Offset;
  262. int max = 0;
  263. while (p_frames) {
  264. // Offsets
  265. unsigned int nLwlOld;
  266. unsigned int nRwrOld;
  267. unsigned int nLwlNew;
  268. unsigned int nRwrNew;
  269. unsigned int nLwrOld;
  270. unsigned int nRwlOld;
  271. unsigned int nLwrNew;
  272. unsigned int nRwlNew;
  273. unsigned int nEcho0L;
  274. unsigned int nEcho1L;
  275. unsigned int nEcho2L;
  276. unsigned int nEcho3L;
  277. unsigned int nEcho0R;
  278. unsigned int nEcho1R;
  279. unsigned int nEcho2R;
  280. unsigned int nEcho3R;
  281. unsigned int nRevaOldL;
  282. unsigned int nRevaOldR;
  283. unsigned int nRevbOldL;
  284. unsigned int nRevbOldR;
  285. unsigned int nRevaNewL;
  286. unsigned int nRevaNewR;
  287. unsigned int nRevbNewL;
  288. unsigned int nRevbNewR;
  289. // Other variables
  290. unsigned int aSample = p_frames;
  291. // Set initial offsets
  292. LM_SETSRCOFFSET(nLwlOld);
  293. LM_SETSRCOFFSET(nRwrOld);
  294. LM_SETSRCOFFSET(nLwlNew);
  295. LM_SETSRCOFFSET(nRwrNew);
  296. LM_SETSRCOFFSET(nLwrOld);
  297. LM_SETSRCOFFSET(nRwlOld);
  298. LM_SETSRCOFFSET(nLwrNew);
  299. LM_SETSRCOFFSET(nRwlNew);
  300. LM_SETSRCOFFSET(nEcho0L);
  301. LM_SETSRCOFFSET(nEcho1L);
  302. LM_SETSRCOFFSET(nEcho2L);
  303. LM_SETSRCOFFSET(nEcho3L);
  304. LM_SETSRCOFFSET(nEcho0R);
  305. LM_SETSRCOFFSET(nEcho1R);
  306. LM_SETSRCOFFSET(nEcho2R);
  307. LM_SETSRCOFFSET(nEcho3R);
  308. LM_SETSRCOFFSET(nRevaOldL);
  309. LM_SETSRCOFFSET(nRevaOldR);
  310. LM_SETSRCOFFSET(nRevbOldL);
  311. LM_SETSRCOFFSET(nRevbOldR);
  312. LM_SETSRCOFFSET(nRevaNewL);
  313. LM_SETSRCOFFSET(nRevaNewR);
  314. LM_SETSRCOFFSET(nRevbNewL);
  315. LM_SETSRCOFFSET(nRevbNewR);
  316. //SETMIN ( aSample, p_output.Size - p_output.Offset );
  317. for (unsigned int cSample = 0; cSample < aSample; cSample++) {
  318. int tempL0, tempL1, tempR0, tempR1;
  319. tempL1 = p_input[(cSample << p_stereo_stride)] >> 8;
  320. tempR1 = p_input[(cSample << p_stereo_stride) + 1] >> 8;
  321. tempL0 = LM_MUL(tempL1, gInputL);
  322. tempR0 = LM_MUL(tempR1, gInputR);
  323. /*
  324. Left -> Wall -> Left Reflection
  325. */
  326. tempL1 = tempL0 + LM_MUL(LM_REVERB(nLwlOld), gWall);
  327. tempR1 = tempR0 + LM_MUL(LM_REVERB(nRwrOld), gWall);
  328. lwl += LM_MUL(tempL1 - lwl, gLPF);
  329. rwr += LM_MUL(tempR1 - rwr, gLPF);
  330. LM_REVERB(nLwlNew) = lwl;
  331. LM_REVERB(nRwrNew) = rwr;
  332. /*
  333. Left -> Wall -> Right Reflection
  334. */
  335. tempL1 = tempL0 + LM_MUL(LM_REVERB(nRwlOld), gWall);
  336. tempR1 = tempR0 + LM_MUL(LM_REVERB(nLwrOld), gWall);
  337. lwr += LM_MUL(tempL1 - lwr, gLPF);
  338. rwl += LM_MUL(tempR1 - rwl, gLPF);
  339. LM_REVERB(nLwrNew) = lwr;
  340. LM_REVERB(nRwlNew) = rwl;
  341. /*
  342. Early Echo(Early Reflection)
  343. */
  344. tempL0 =
  345. LM_MUL(LM_REVERB(nEcho0L), gEcho0) +
  346. LM_MUL(LM_REVERB(nEcho1L), gEcho1) +
  347. LM_MUL(LM_REVERB(nEcho2L), gEcho2) +
  348. LM_MUL(LM_REVERB(nEcho3L), gEcho3);
  349. tempR0 =
  350. LM_MUL(LM_REVERB(nEcho0R), gEcho0) +
  351. LM_MUL(LM_REVERB(nEcho1R), gEcho1) +
  352. LM_MUL(LM_REVERB(nEcho2R), gEcho2) +
  353. LM_MUL(LM_REVERB(nEcho3R), gEcho3);
  354. /*
  355. Late Reverb
  356. */
  357. tempL1 = LM_REVERB(nRevaOldL);
  358. tempR1 = LM_REVERB(nRevaOldR);
  359. tempL0 -= LM_MUL(tempL1, gReva);
  360. tempR0 -= LM_MUL(tempR1, gReva);
  361. LM_REVERB(nRevaNewL) = tempL0;
  362. LM_REVERB(nRevaNewR) = tempR0;
  363. tempL0 = LM_MUL(tempL0, gReva) + tempL1;
  364. tempR0 = LM_MUL(tempR0, gReva) + tempR1;
  365. tempL1 = LM_REVERB(nRevbOldL);
  366. tempR1 = LM_REVERB(nRevbOldR);
  367. tempL0 -= LM_MUL(tempL1, gRevb);
  368. tempR0 -= LM_MUL(tempR1, gRevb);
  369. LM_REVERB(nRevbNewL) = tempL0;
  370. LM_REVERB(nRevbNewR) = tempR0;
  371. tempL0 = LM_MUL(tempL0, gRevb) + tempL1;
  372. tempR0 = LM_MUL(tempR0, gRevb) + tempR1;
  373. /*
  374. Output
  375. */
  376. max |= abs(tempL0);
  377. max |= abs(tempR0);
  378. p_output[(cSample << p_stereo_stride)] += tempL0 << 8;
  379. p_output[(cSample << p_stereo_stride) + 1] += tempR0 << 8;
  380. }
  381. // Advance offsets
  382. Offset += aSample;
  383. if (Offset >= reverb_buffer_size) {
  384. Offset -= reverb_buffer_size;
  385. }
  386. p_input += aSample << p_stereo_stride;
  387. p_output += aSample << p_stereo_stride;
  388. p_frames -= aSample;
  389. }
  390. state.lwl = lwl;
  391. state.lwr = lwr;
  392. state.rwl = rwl;
  393. state.rwr = rwr;
  394. state.Offset = Offset;
  395. return (max & 0x7FFFFF00) != 0; // audio was mixed?
  396. }
  397. void ReverbSW::adjust_current_params() {
  398. *current_params = *reverb_param_modes[mode];
  399. uint32_t maxofs = 0;
  400. #define LM_CONFIG_PARAM(x) \
  401. current_params->x = (int)(((int64_t)current_params->x * (int64_t)mix_rate * 8L) / (int64_t)44100); \
  402. if (current_params->x > maxofs) \
  403. maxofs = current_params->x;
  404. LM_CONFIG_PARAM(nLwlOld);
  405. LM_CONFIG_PARAM(nRwrOld);
  406. LM_CONFIG_PARAM(nLwlNew);
  407. LM_CONFIG_PARAM(nRwrNew);
  408. LM_CONFIG_PARAM(nLwrOld);
  409. LM_CONFIG_PARAM(nRwlOld);
  410. LM_CONFIG_PARAM(nLwrNew);
  411. LM_CONFIG_PARAM(nRwlNew);
  412. LM_CONFIG_PARAM(nEcho0L);
  413. LM_CONFIG_PARAM(nEcho1L);
  414. LM_CONFIG_PARAM(nEcho2L);
  415. LM_CONFIG_PARAM(nEcho3L);
  416. LM_CONFIG_PARAM(nEcho0R);
  417. LM_CONFIG_PARAM(nEcho1R);
  418. LM_CONFIG_PARAM(nEcho2R);
  419. LM_CONFIG_PARAM(nEcho3R);
  420. LM_CONFIG_PARAM(nRevaOldL);
  421. LM_CONFIG_PARAM(nRevaOldR);
  422. LM_CONFIG_PARAM(nRevbOldL);
  423. LM_CONFIG_PARAM(nRevbOldR);
  424. LM_CONFIG_PARAM(nRevaNewL);
  425. LM_CONFIG_PARAM(nRevaNewR);
  426. LM_CONFIG_PARAM(nRevbNewL);
  427. LM_CONFIG_PARAM(nRevbNewR);
  428. int needed_buffer_size = maxofs + 1;
  429. if (reverb_buffer)
  430. memdelete_arr(reverb_buffer);
  431. reverb_buffer = memnew_arr(int, needed_buffer_size);
  432. reverb_buffer_size = needed_buffer_size;
  433. for (uint32_t i = 0; i < reverb_buffer_size; i++)
  434. reverb_buffer[i] = 0;
  435. state.reset();
  436. }
  437. void ReverbSW::set_mode(ReverbMode p_mode) {
  438. if (mode == p_mode)
  439. return;
  440. mode = p_mode;
  441. adjust_current_params();
  442. }
  443. void ReverbSW::set_mix_rate(int p_mix_rate) {
  444. if (p_mix_rate == mix_rate)
  445. return;
  446. mix_rate = p_mix_rate;
  447. adjust_current_params();
  448. }
  449. ReverbSW::ReverbSW() {
  450. reverb_buffer = 0;
  451. reverb_buffer_size = 0;
  452. mode = REVERB_MODE_ROOM;
  453. mix_rate = 1;
  454. current_params = memnew(ReverbParamsSW);
  455. }
  456. ReverbSW::~ReverbSW() {
  457. if (reverb_buffer)
  458. memdelete_arr(reverb_buffer);
  459. memdelete(current_params);
  460. }