some random improvements
[pcsx_rearmed.git] / plugins / dfsound / reverb.c
CommitLineData
ef79bbde
P
1/***************************************************************************\r
2 reverb.c - description\r
3 -------------------\r
4 begin : Wed May 15 2002\r
5 copyright : (C) 2002 by Pete Bernert\r
6 email : BlackDove@addcom.de\r
1775933a 7\r
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2011\r
9 Portions (C) SPU2-X, gigaherz, Pcsx2 Development Team\r
10\r
ef79bbde
P
11 ***************************************************************************/\r
12/***************************************************************************\r
13 * *\r
14 * This program is free software; you can redistribute it and/or modify *\r
15 * it under the terms of the GNU General Public License as published by *\r
16 * the Free Software Foundation; either version 2 of the License, or *\r
17 * (at your option) any later version. See also the license.txt file for *\r
18 * additional informations. *\r
19 * *\r
20 ***************************************************************************/\r
21\r
22#include "stdafx.h"\r
23\r
24#define _IN_REVERB\r
25\r
26// will be included from spu.c\r
27#ifdef _IN_SPU\r
28\r
ef79bbde
P
29////////////////////////////////////////////////////////////////////////\r
30// START REVERB\r
31////////////////////////////////////////////////////////////////////////\r
32\r
33INLINE void StartREVERB(int ch)\r
34{\r
5514a050 35 if(spu.s_chan[ch].bReverb && (spu.spuCtrl&0x80)) // reverb possible?\r
ef79bbde 36 {\r
5514a050 37 spu.s_chan[ch].bRVBActive=!!spu_config.iUseReverb;\r
ef79bbde 38 }\r
5514a050 39 else spu.s_chan[ch].bRVBActive=0; // else -> no reverb\r
ef79bbde
P
40}\r
41\r
ef79bbde
P
42////////////////////////////////////////////////////////////////////////\r
43\r
1775933a 44INLINE int rvb2ram_offs(int curr, int space, int iOff)\r
ef79bbde 45{\r
1775933a 46 iOff += curr;\r
47 if (iOff >= 0x40000) iOff -= space;\r
48 return iOff;\r
ef79bbde
P
49}\r
50\r
1775933a 51// get_buffer content helper: takes care about wraps\r
52#define g_buffer(var) \\r
3154bfab 53 ((int)(signed short)spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var)])\r
ef79bbde 54\r
1775933a 55// saturate iVal and store it as var\r
56#define s_buffer(var, iVal) \\r
57 ssat32_to_16(iVal); \\r
3154bfab 58 spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var)] = iVal\r
ef79bbde 59\r
1775933a 60#define s_buffer1(var, iVal) \\r
61 ssat32_to_16(iVal); \\r
3154bfab 62 spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var + 1)] = iVal\r
ef79bbde
P
63\r
64////////////////////////////////////////////////////////////////////////\r
65\r
1775933a 66// portions based on spu2-x from PCSX2\r
3bd31caf 67static void MixREVERB(int *SSumLR, int *RVB, int ns_to)\r
ef79bbde 68{\r
1775933a 69 int l_old = rvb.iRVBLeft;\r
70 int r_old = rvb.iRVBRight;\r
71 int curr_addr = rvb.CurrAddr;\r
72 int space = 0x40000 - rvb.StartAddr;\r
650adfd2 73 int l = 0, r = 0, ns;\r
1775933a 74\r
650adfd2 75 for (ns = 0; ns < ns_to * 2; )\r
ef79bbde 76 {\r
1775933a 77 int IIR_ALPHA = rvb.IIR_ALPHA;\r
78 int ACC0, ACC1, FB_A0, FB_A1, FB_B0, FB_B1;\r
79 int mix_dest_a0, mix_dest_a1, mix_dest_b0, mix_dest_b1;\r
80\r
3bd31caf 81 int input_L = RVB[ns] * rvb.IN_COEF_L;\r
82 int input_R = RVB[ns+1] * rvb.IN_COEF_R;\r
1775933a 83\r
84 int IIR_INPUT_A0 = ((g_buffer(IIR_SRC_A0) * rvb.IIR_COEF) + input_L) >> 15;\r
85 int IIR_INPUT_A1 = ((g_buffer(IIR_SRC_A1) * rvb.IIR_COEF) + input_R) >> 15;\r
86 int IIR_INPUT_B0 = ((g_buffer(IIR_SRC_B0) * rvb.IIR_COEF) + input_L) >> 15;\r
87 int IIR_INPUT_B1 = ((g_buffer(IIR_SRC_B1) * rvb.IIR_COEF) + input_R) >> 15;\r
88\r
89 int iir_dest_a0 = g_buffer(IIR_DEST_A0);\r
90 int iir_dest_a1 = g_buffer(IIR_DEST_A1);\r
91 int iir_dest_b0 = g_buffer(IIR_DEST_B0);\r
92 int iir_dest_b1 = g_buffer(IIR_DEST_B1);\r
93\r
94 int IIR_A0 = iir_dest_a0 + ((IIR_INPUT_A0 - iir_dest_a0) * IIR_ALPHA >> 15);\r
95 int IIR_A1 = iir_dest_a1 + ((IIR_INPUT_A1 - iir_dest_a1) * IIR_ALPHA >> 15);\r
96 int IIR_B0 = iir_dest_b0 + ((IIR_INPUT_B0 - iir_dest_b0) * IIR_ALPHA >> 15);\r
97 int IIR_B1 = iir_dest_b1 + ((IIR_INPUT_B1 - iir_dest_b1) * IIR_ALPHA >> 15);\r
98\r
99 s_buffer1(IIR_DEST_A0, IIR_A0);\r
100 s_buffer1(IIR_DEST_A1, IIR_A1);\r
101 s_buffer1(IIR_DEST_B0, IIR_B0);\r
102 s_buffer1(IIR_DEST_B1, IIR_B1);\r
103\r
104 ACC0 = (g_buffer(ACC_SRC_A0) * rvb.ACC_COEF_A +\r
105 g_buffer(ACC_SRC_B0) * rvb.ACC_COEF_B +\r
106 g_buffer(ACC_SRC_C0) * rvb.ACC_COEF_C +\r
107 g_buffer(ACC_SRC_D0) * rvb.ACC_COEF_D) >> 15;\r
108 ACC1 = (g_buffer(ACC_SRC_A1) * rvb.ACC_COEF_A +\r
109 g_buffer(ACC_SRC_B1) * rvb.ACC_COEF_B +\r
110 g_buffer(ACC_SRC_C1) * rvb.ACC_COEF_C +\r
111 g_buffer(ACC_SRC_D1) * rvb.ACC_COEF_D) >> 15;\r
112\r
113 FB_A0 = g_buffer(FB_SRC_A0);\r
114 FB_A1 = g_buffer(FB_SRC_A1);\r
115 FB_B0 = g_buffer(FB_SRC_B0);\r
116 FB_B1 = g_buffer(FB_SRC_B1);\r
117\r
118 mix_dest_a0 = ACC0 - ((FB_A0 * rvb.FB_ALPHA) >> 15);\r
119 mix_dest_a1 = ACC1 - ((FB_A1 * rvb.FB_ALPHA) >> 15);\r
120\r
121 mix_dest_b0 = FB_A0 + (((ACC0 - FB_A0) * rvb.FB_ALPHA - FB_B0 * rvb.FB_X) >> 15);\r
122 mix_dest_b1 = FB_A1 + (((ACC1 - FB_A1) * rvb.FB_ALPHA - FB_B1 * rvb.FB_X) >> 15);\r
123\r
124 s_buffer(MIX_DEST_A0, mix_dest_a0);\r
125 s_buffer(MIX_DEST_A1, mix_dest_a1);\r
126 s_buffer(MIX_DEST_B0, mix_dest_b0);\r
127 s_buffer(MIX_DEST_B1, mix_dest_b1);\r
128\r
b72f17a1 129 l = (mix_dest_a0 + mix_dest_b0) / 2;\r
130 r = (mix_dest_a1 + mix_dest_b1) / 2;\r
1775933a 131\r
b72f17a1 132 l = (l * rvb.VolLeft) >> 15; // 15?\r
133 r = (r * rvb.VolRight) >> 15;\r
134\r
135 SSumLR[ns++] += (l + l_old) / 2;\r
136 SSumLR[ns++] += (r + r_old) / 2;\r
137 SSumLR[ns++] += l;\r
138 SSumLR[ns++] += r;\r
139\r
140 l_old = l;\r
141 r_old = r;\r
142\r
143 curr_addr++;\r
144 if (curr_addr >= 0x40000) curr_addr = rvb.StartAddr;\r
145 }\r
146\r
147 rvb.iRVBLeft = l;\r
148 rvb.iRVBRight = r;\r
149 rvb.CurrAddr = curr_addr;\r
150}\r
151\r
3bd31caf 152static void MixREVERB_off(int *SSumLR, int ns_to)\r
b72f17a1 153{\r
154 int l_old = rvb.iRVBLeft;\r
155 int r_old = rvb.iRVBRight;\r
156 int curr_addr = rvb.CurrAddr;\r
157 int space = 0x40000 - rvb.StartAddr;\r
650adfd2 158 int l = 0, r = 0, ns;\r
b72f17a1 159\r
650adfd2 160 for (ns = 0; ns < ns_to * 2; )\r
b72f17a1 161 {\r
162 l = (g_buffer(MIX_DEST_A0) + g_buffer(MIX_DEST_B0)) / 2;\r
163 r = (g_buffer(MIX_DEST_A1) + g_buffer(MIX_DEST_B1)) / 2;\r
164\r
165 l = (l * rvb.VolLeft) >> 15;\r
166 r = (r * rvb.VolRight) >> 15;\r
1775933a 167\r
168 SSumLR[ns++] += (l + l_old) / 2;\r
169 SSumLR[ns++] += (r + r_old) / 2;\r
170 SSumLR[ns++] += l;\r
171 SSumLR[ns++] += r;\r
172\r
173 l_old = l;\r
174 r_old = r;\r
175\r
176 curr_addr++;\r
177 if (curr_addr >= 0x40000) curr_addr = rvb.StartAddr;\r
ef79bbde 178 }\r
1775933a 179\r
180 rvb.iRVBLeft = l;\r
181 rvb.iRVBRight = r;\r
182 rvb.CurrAddr = curr_addr;\r
ef79bbde
P
183}\r
184\r
1775933a 185static void prepare_offsets(void)\r
186{\r
187 int space = 0x40000 - rvb.StartAddr;\r
188 int t;\r
189 #define prep_offs(v) \\r
190 t = rvb.v; \\r
191 while (t >= space) \\r
192 t -= space; \\r
193 rvb.n##v = t\r
194 #define prep_offs2(d, v1, v2) \\r
195 t = rvb.v1 - rvb.v2; \\r
196 while (t >= space) \\r
197 t -= space; \\r
198 rvb.n##d = t\r
199\r
200 prep_offs(IIR_SRC_A0);\r
201 prep_offs(IIR_SRC_A1);\r
202 prep_offs(IIR_SRC_B0);\r
203 prep_offs(IIR_SRC_B1);\r
204 prep_offs(IIR_DEST_A0);\r
205 prep_offs(IIR_DEST_A1);\r
206 prep_offs(IIR_DEST_B0);\r
207 prep_offs(IIR_DEST_B1);\r
208 prep_offs(ACC_SRC_A0);\r
209 prep_offs(ACC_SRC_A1);\r
210 prep_offs(ACC_SRC_B0);\r
211 prep_offs(ACC_SRC_B1);\r
212 prep_offs(ACC_SRC_C0);\r
213 prep_offs(ACC_SRC_C1);\r
214 prep_offs(ACC_SRC_D0);\r
215 prep_offs(ACC_SRC_D1);\r
216 prep_offs(MIX_DEST_A0);\r
217 prep_offs(MIX_DEST_A1);\r
218 prep_offs(MIX_DEST_B0);\r
219 prep_offs(MIX_DEST_B1);\r
220 prep_offs2(FB_SRC_A0, MIX_DEST_A0, FB_SRC_A);\r
221 prep_offs2(FB_SRC_A1, MIX_DEST_A1, FB_SRC_A);\r
222 prep_offs2(FB_SRC_B0, MIX_DEST_B0, FB_SRC_B);\r
223 prep_offs2(FB_SRC_B1, MIX_DEST_B1, FB_SRC_B);\r
224\r
225#undef prep_offs\r
226#undef prep_offs2\r
227 rvb.dirty = 0;\r
228}\r
ef79bbde 229\r
3bd31caf 230INLINE void REVERBDo(int *SSumLR, int *RVB, int ns_to)\r
ef79bbde 231{\r
1775933a 232 if (!rvb.StartAddr) // reverb is off\r
233 {\r
234 rvb.iRVBLeft = rvb.iRVBRight = 0;\r
235 return;\r
236 }\r
237\r
3154bfab 238 if (spu.spuCtrl & 0x80) // -> reverb on? oki\r
1775933a 239 {\r
b72f17a1 240 if (unlikely(rvb.dirty))\r
1775933a 241 prepare_offsets();\r
242\r
3bd31caf 243 MixREVERB(SSumLR, RVB, ns_to);\r
1775933a 244 }\r
b72f17a1 245 else if (rvb.VolLeft || rvb.VolRight)\r
246 {\r
247 if (unlikely(rvb.dirty))\r
248 prepare_offsets();\r
249\r
3bd31caf 250 MixREVERB_off(SSumLR, ns_to);\r
b72f17a1 251 }\r
1775933a 252 else // -> reverb off\r
253 {\r
b72f17a1 254 // reverb runs anyway\r
650adfd2 255 rvb.CurrAddr += ns_to / 2;\r
1775933a 256 while (rvb.CurrAddr >= 0x40000)\r
257 rvb.CurrAddr -= 0x40000 - rvb.StartAddr;\r
258 }\r
ef79bbde
P
259}\r
260\r
261////////////////////////////////////////////////////////////////////////\r
262\r
263#endif\r
264\r
265/*\r
266-----------------------------------------------------------------------------\r
267PSX reverb hardware notes\r
268by Neill Corlett\r
269-----------------------------------------------------------------------------\r
270\r
271Yadda yadda disclaimer yadda probably not perfect yadda well it's okay anyway\r
272yadda yadda.\r
273\r
274-----------------------------------------------------------------------------\r
275\r
276Basics\r
277------\r
278\r
279- The reverb buffer is 22khz 16-bit mono PCM.\r
280- It starts at the reverb address given by 1DA2, extends to\r
281 the end of sound RAM, and wraps back to the 1DA2 address.\r
282\r
283Setting the address at 1DA2 resets the current reverb work address.\r
284\r
285This work address ALWAYS increments every 1/22050 sec., regardless of\r
286whether reverb is enabled (bit 7 of 1DAA set).\r
287\r
288And the contents of the reverb buffer ALWAYS play, scaled by the\r
289"reverberation depth left/right" volumes (1D84/1D86).\r
290(which, by the way, appear to be scaled so 3FFF=approx. 1.0, 4000=-1.0)\r
291\r
292-----------------------------------------------------------------------------\r
293\r
294Register names\r
295--------------\r
296\r
297These are probably not their real names.\r
298These are probably not even correct names.\r
299We will use them anyway, because we can.\r
300\r
3011DC0: FB_SRC_A (offset)\r
3021DC2: FB_SRC_B (offset)\r
3031DC4: IIR_ALPHA (coef.)\r
3041DC6: ACC_COEF_A (coef.)\r
3051DC8: ACC_COEF_B (coef.)\r
3061DCA: ACC_COEF_C (coef.)\r
3071DCC: ACC_COEF_D (coef.)\r
3081DCE: IIR_COEF (coef.)\r
3091DD0: FB_ALPHA (coef.)\r
3101DD2: FB_X (coef.)\r
3111DD4: IIR_DEST_A0 (offset)\r
3121DD6: IIR_DEST_A1 (offset)\r
3131DD8: ACC_SRC_A0 (offset)\r
3141DDA: ACC_SRC_A1 (offset)\r
3151DDC: ACC_SRC_B0 (offset)\r
3161DDE: ACC_SRC_B1 (offset)\r
3171DE0: IIR_SRC_A0 (offset)\r
3181DE2: IIR_SRC_A1 (offset)\r
3191DE4: IIR_DEST_B0 (offset)\r
3201DE6: IIR_DEST_B1 (offset)\r
3211DE8: ACC_SRC_C0 (offset)\r
3221DEA: ACC_SRC_C1 (offset)\r
3231DEC: ACC_SRC_D0 (offset)\r
3241DEE: ACC_SRC_D1 (offset)\r
3251DF0: IIR_SRC_B1 (offset)\r
3261DF2: IIR_SRC_B0 (offset)\r
3271DF4: MIX_DEST_A0 (offset)\r
3281DF6: MIX_DEST_A1 (offset)\r
3291DF8: MIX_DEST_B0 (offset)\r
3301DFA: MIX_DEST_B1 (offset)\r
3311DFC: IN_COEF_L (coef.)\r
3321DFE: IN_COEF_R (coef.)\r
333\r
334The coefficients are signed fractional values.\r
335-32768 would be -1.0\r
336 32768 would be 1.0 (if it were possible... the highest is of course 32767)\r
337\r
338The offsets are (byte/8) offsets into the reverb buffer.\r
339i.e. you multiply them by 8, you get byte offsets.\r
340You can also think of them as (samples/4) offsets.\r
341They appear to be signed. They can be negative.\r
342None of the documented presets make them negative, though.\r
343\r
344Yes, 1DF0 and 1DF2 appear to be backwards. Not a typo.\r
345\r
346-----------------------------------------------------------------------------\r
347\r
348What it does\r
349------------\r
350\r
351We take all reverb sources:\r
352- regular channels that have the reverb bit on\r
353- cd and external sources, if their reverb bits are on\r
354and mix them into one stereo 44100hz signal.\r
355\r
356Lowpass/downsample that to 22050hz. The PSX uses a proper bandlimiting\r
357algorithm here, but I haven't figured out the hysterically exact specifics.\r
358I use an 8-tap filter with these coefficients, which are nice but probably\r
359not the real ones:\r
360\r
3610.037828187894\r
3620.157538631280\r
3630.321159685278\r
3640.449322115345\r
3650.449322115345\r
3660.321159685278\r
3670.157538631280\r
3680.037828187894\r
369\r
370So we have two input samples (INPUT_SAMPLE_L, INPUT_SAMPLE_R) every 22050hz.\r
371\r
372* IN MY EMULATION, I divide these by 2 to make it clip less.\r
373 (and of course the L/R output coefficients are adjusted to compensate)\r
374 The real thing appears to not do this.\r
375\r
376At every 22050hz tick:\r
377- If the reverb bit is enabled (bit 7 of 1DAA), execute the reverb\r
378 steady-state algorithm described below\r
379- AFTERWARDS, retrieve the "wet out" L and R samples from the reverb buffer\r
380 (This part may not be exactly right and I guessed at the coefs. TODO: check later.)\r
381 L is: 0.333 * (buffer[MIX_DEST_A0] + buffer[MIX_DEST_B0])\r
382 R is: 0.333 * (buffer[MIX_DEST_A1] + buffer[MIX_DEST_B1])\r
383- Advance the current buffer position by 1 sample\r
384\r
385The wet out L and R are then upsampled to 44100hz and played at the\r
386"reverberation depth left/right" (1D84/1D86) volume, independent of the main\r
387volume.\r
388\r
389-----------------------------------------------------------------------------\r
390\r
391Reverb steady-state\r
392-------------------\r
393\r
394The reverb steady-state algorithm is fairly clever, and of course by\r
395"clever" I mean "batshit insane".\r
396\r
397buffer[x] is relative to the current buffer position, not the beginning of\r
398the buffer. Note that all buffer offsets must wrap around so they're\r
399contained within the reverb work area.\r
400\r
401Clipping is performed at the end... maybe also sooner, but definitely at\r
402the end.\r
403\r
404IIR_INPUT_A0 = buffer[IIR_SRC_A0] * IIR_COEF + INPUT_SAMPLE_L * IN_COEF_L;\r
405IIR_INPUT_A1 = buffer[IIR_SRC_A1] * IIR_COEF + INPUT_SAMPLE_R * IN_COEF_R;\r
406IIR_INPUT_B0 = buffer[IIR_SRC_B0] * IIR_COEF + INPUT_SAMPLE_L * IN_COEF_L;\r
407IIR_INPUT_B1 = buffer[IIR_SRC_B1] * IIR_COEF + INPUT_SAMPLE_R * IN_COEF_R;\r
408\r
409IIR_A0 = IIR_INPUT_A0 * IIR_ALPHA + buffer[IIR_DEST_A0] * (1.0 - IIR_ALPHA);\r
410IIR_A1 = IIR_INPUT_A1 * IIR_ALPHA + buffer[IIR_DEST_A1] * (1.0 - IIR_ALPHA);\r
411IIR_B0 = IIR_INPUT_B0 * IIR_ALPHA + buffer[IIR_DEST_B0] * (1.0 - IIR_ALPHA);\r
412IIR_B1 = IIR_INPUT_B1 * IIR_ALPHA + buffer[IIR_DEST_B1] * (1.0 - IIR_ALPHA);\r
413\r
414buffer[IIR_DEST_A0 + 1sample] = IIR_A0;\r
415buffer[IIR_DEST_A1 + 1sample] = IIR_A1;\r
416buffer[IIR_DEST_B0 + 1sample] = IIR_B0;\r
417buffer[IIR_DEST_B1 + 1sample] = IIR_B1;\r
418\r
419ACC0 = buffer[ACC_SRC_A0] * ACC_COEF_A +\r
420 buffer[ACC_SRC_B0] * ACC_COEF_B +\r
421 buffer[ACC_SRC_C0] * ACC_COEF_C +\r
422 buffer[ACC_SRC_D0] * ACC_COEF_D;\r
423ACC1 = buffer[ACC_SRC_A1] * ACC_COEF_A +\r
424 buffer[ACC_SRC_B1] * ACC_COEF_B +\r
425 buffer[ACC_SRC_C1] * ACC_COEF_C +\r
426 buffer[ACC_SRC_D1] * ACC_COEF_D;\r
427\r
428FB_A0 = buffer[MIX_DEST_A0 - FB_SRC_A];\r
429FB_A1 = buffer[MIX_DEST_A1 - FB_SRC_A];\r
430FB_B0 = buffer[MIX_DEST_B0 - FB_SRC_B];\r
431FB_B1 = buffer[MIX_DEST_B1 - FB_SRC_B];\r
432\r
433buffer[MIX_DEST_A0] = ACC0 - FB_A0 * FB_ALPHA;\r
434buffer[MIX_DEST_A1] = ACC1 - FB_A1 * FB_ALPHA;\r
435buffer[MIX_DEST_B0] = (FB_ALPHA * ACC0) - FB_A0 * (FB_ALPHA^0x8000) - FB_B0 * FB_X;\r
436buffer[MIX_DEST_B1] = (FB_ALPHA * ACC1) - FB_A1 * (FB_ALPHA^0x8000) - FB_B1 * FB_X;\r
437\r
438-----------------------------------------------------------------------------\r
439*/\r
440\r
1775933a 441// vim:shiftwidth=1:expandtab\r