summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/spureverb.c
blob: c5107c010fbe076c81b592190f775a72250a9706 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "emu.h"
#include "spureverb.h"

//
//
//

//#define use_intrinsics

#ifdef use_intrinsics
	#include <mmintrin.h>
#endif

//
//
//

static inline int clamp(int v)
{
	if (v<-32768) return -32768;
	if (v>32767) return 32767;
	return v;
}

//
//
//

reverb::reverb(const int hz, const int maxdelay)
	:  yp(0),
		max_delay(maxdelay),
		sound_hz(hz)
{
	for (int c=0; c<2; c++)
	{
		for (int f=0; f<4; f++)
			y[c][f]=new signed short [maxdelay];
		x[c]=new signed short [maxdelay];
		ax[c]=new signed short [maxdelay];
		ay[c]=new signed short [maxdelay];
	}
	memset(bx1,0,sizeof(bx1));
	memset(by1,0,sizeof(by1));
}

//
//
//

reverb::~reverb()
{
	for (int c=0; c<2; c++)
	{
		for (int f=0; f<4; f++)
			global_free(y[c][f]);
		global_free(x[c]);
		global_free(ax[c]);
		global_free(ay[c]);
	}
}

//
//
//

void reverb::bandpass(signed short *sp,
											const reverb_params *rp,
											const unsigned int sz)
{
	int band_pole=(int)(rp->band_pole*32767),
			band_gain=(int)(rp->band_gain*32767);

	// Bandpass

	int xp=yp;
	for (unsigned int i=0; i<(sz>>2); i++, sp+=2)
	{
		for (int c=0; c<2; c++)
		{
			int x1=(xp-1)&(max_delay-1),
					bv;

			bv=sp[c]+bx1[c][1]+((band_pole*x[c][x1])>>15);
			bv=(bv*band_gain)>>15;
			x[c][xp]=clamp(bv);
			bx1[c][1]=bx1[c][0];
			bx1[c][0]=sp[c];
		}

		xp=(xp+1)&(max_delay-1);
	}
}

void reverb::comb_allpass1(signed short *sp,
														signed short *dp,
														const comb_param &comb_delay,
														const int comb_gain,
														const int allpass_delay,
														const int allpass_gain,
														const int *rvol,
														const unsigned int sz)
{
	for (unsigned int i=0; i<(sz>>2); i++, sp+=2, dp+=2)
	{
		for (int c=0; c<2; c++)
		{
			// Comb

			int v=0;

			for (int f=0; f<4; f++)
			{
				int yck=(yp-comb_delay[c][f])&(max_delay-1);
				y[c][f][yp]=clamp(x[c][yck]+((comb_gain*y[c][f][yck])>>15));
				v+=y[c][f][yp];
			}

			v>>=2;

			// Allpass

			if (allpass_delay)
			{
				ax[c][yp]=v;

				int ypa=(yp-allpass_delay)&(max_delay-1);
				v=((allpass_gain*(ay[c][ypa]-x[c][yp]))>>15)+ax[c][ypa];
				v=clamp(v);
				ay[c][yp]=v;
			}

			// Output

			dp[c]=clamp(((v*rvol[c])>>15)+dp[c]+sp[c]);
		}
		yp=(yp+1)&(max_delay-1);
	}
}

//
//
//

void reverb::comb_allpass4(signed short *sp,
														signed short *dp,
														const comb_param &comb_delay,
														const int comb_gain,
														const int allpass_delay,
														const int allpass_gain,
														const int *rvol,
														const unsigned int sz)
{
#ifdef use_intrinsics
	__m64   cg=_mm_set1_pi16(comb_gain),
				ag=_mm_set1_pi16(allpass_gain),
				rv[2];
	rv[0]=_mm_set1_pi16(rvol[0]);
	rv[1]=_mm_set1_pi16(rvol[1]);

	for (unsigned int i=0; i<(sz>>4); i++, sp+=2<<2, dp+=2<<2)
	{
		__m64 dv[2];

		for (int c=0; c<2; c++)
		{
			// Comb

			__m64 v=_mm_setzero_si64();

			for (int f=0; f<4; f++)
			{
				int yck=(yp-comb_delay[c][f])&(max_delay-1);
				__m64 xv=*(__m64 *)(&x[c][yck]),
							yv=*(__m64 *)(&y[c][f][yck]);
				yv=_mm_mulhi_pi16(yv,cg);
				yv=_mm_adds_pi16(yv,yv);
				yv=_mm_adds_pi16(xv,yv);
				*((__m64 *)&y[c][f][yp])=yv;
				yv=_mm_srai_pi16(yv,2);
				v=_mm_adds_pi16(v,yv);
			}

			// Allpass

			if (allpass_delay)
			{
				*((__m64 *)&ax[c][yp])=v;

				int ypa=(yp-allpass_delay)&(max_delay-1);
				__m64 ayv=*(__m64 *)&ay[c][ypa],
								xv=*(__m64 *)&x[c][yp],
								axv=*(__m64 *)&ax[c][ypa];

				ayv=_mm_subs_pi16(ayv,xv);
				ayv=_mm_mulhi_pi16(ayv,ag);
				ayv=_mm_adds_pi16(ayv,ayv);
				v=_mm_adds_pi16(ayv,axv);
				*((__m64 *)&ay[c][yp])=v;
			}

			// Output

			dv[c]=_mm_mulhi_pi16(v,rv[c]);
			dv[c]=_mm_adds_pi16(dv[c],dv[c]);
		}

		__m64 dv1=_mm_unpacklo_pi16(dv[0],dv[1]),
					dv2=_mm_unpackhi_pi16(dv[0],dv[1]),
					d1=*(__m64 *)&dp[0],
					d2=*(__m64 *)&dp[4],
					s1=*(__m64 *)&sp[0],
					s2=*(__m64 *)&sp[4];
		d1=_mm_adds_pi16(d1,s1);
		d2=_mm_adds_pi16(d2,s2);
		d1=_mm_adds_pi16(d1,dv1);
		d2=_mm_adds_pi16(d2,dv2);
		*(__m64 *)&dp[0]=d1;
		*(__m64 *)&dp[4]=d2;

		yp=(yp+4)&(max_delay-1);
	}

	_mm_empty();
#endif
}

//
//
//

void reverb::comb_allpass(signed short *sp,
													signed short *dp,
													const reverb_params *rp,
													const int wetvol_l,
													const int wetvol_r,
													const unsigned int _sz)
{
	unsigned int sz=_sz;
	comb_param comb_delay;
	int comb_gain=(int)(rp->comb_gain*32767),
			allpass_delay=(int)(((rp->allpass_delay/1000.0f)*sound_hz))&~3,
			allpass_gain=(int)(rp->allpass_gain*32767),
			rvol[2]={ (signed short)wetvol_l,
								(signed short)wetvol_r };

	for (int i=0; i<4; i++)
		for (int c=0; c<2; c++)
			comb_delay[c][i]=(int)(((rp->comb_delay[c][i]/1000.0f)*sound_hz))&~3;

	#ifdef use_intrinsics

	if (yp&3)
	{
		unsigned int n=min(sz,(unsigned int)(4-(yp&3))<<2);
		comb_allpass1(sp,dp,
									(const comb_param &)comb_delay,
									comb_gain,
									allpass_delay,
									allpass_gain,
									rvol,
									n);
		sp+=(n>>1);
		dp+=(n>>1);
		sz-=n;
	}

	if (sz>=16)
	{
		unsigned int n=sz&~15;
		comb_allpass4(sp,dp,
									(const comb_param &)comb_delay,
									comb_gain,
									allpass_delay,
									allpass_gain,
									rvol,
									n);
		sp+=n>>1;
		dp+=n>>1;
		sz-=n;
	}

	if (sz)
	{
		comb_allpass1(sp,dp,
									(const comb_param &)comb_delay,
									comb_gain,
									allpass_delay,
									allpass_gain,
									rvol,
									sz);
	}

	#else
		comb_allpass1(sp,dp,
									(const comb_param &)comb_delay,
									comb_gain,
									allpass_delay,
									allpass_gain,
									rvol,
									sz);
	#endif
}

//
//
//

void reverb::process(signed short *output,
										signed short *reverb_input,
										const reverb_params *rp,
										const int wetvol_l,
										const int wetvol_r,
											const unsigned int sz)
{
	signed short *sp=(signed short *)reverb_input,
								*dp=(signed short *)output;

	if (rp->band_gain>0.0f)
	{
		// Do reverb processing

		bandpass(sp,rp,sz);
		comb_allpass(sp,dp,rp,wetvol_l,wetvol_r,sz);
	} else
	{
		// Reverb disabled - just mix the input to the output

		for (unsigned int i=0; i<(sz>>2); i++)
		{
			output[0]=clamp(output[0]+reverb_input[0]);
			output[1]=clamp(output[1]+reverb_input[1]);
			output+=2;
			reverb_input+=2;
		}
	}
}