1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20 
21 /*
22  * REVERB EFFECT FOR TIMIDITY++-1.X (Version 0.06e  1999/1/28)
23  *
24  * Copyright (C) 1997,1998,1999  Masaki Kiryu <mkiryu@usa.net>
25  *                           (http://w3mb.kcom.ne.jp/~mkiryu/)
26  *
27  * reverb.c  -- main reverb engine.
28  *
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33 
34 #ifndef NO_STRING_H
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39 #include "timidity.h"
40 #include "controls.h"
41 #include "tables.h"
42 #include "common.h"
43 #include "output.h"
44 #define REVERB_PRIVATE 1
45 #include "reverb.h"
46 #include "mt19937ar.h"
47 #include <math.h>
48 #include <stdlib.h>
49 
50 #define SYS_EFFECT_PRE_LPF
51 
52 /* #define SYS_EFFECT_CLIP */
53 #ifdef SYS_EFFECT_CLIP
54 #define CLIP_AMP_MAX (1L << (32 - GUARD_BITS))
55 #define CLIP_AMP_MIN (-1L << (32 - GUARD_BITS))
56 #endif /* SYS_EFFECT_CLIP */
57 
58 FLOAT_T reverb_predelay_factor = 1.0;
59 
60 static double REV_INP_LEV = 1.0;
61 #define MASTER_CHORUS_LEVEL 1.7
62 #define MASTER_DELAY_LEVEL 3.25
63 
64 /*              */
65 /*  Dry Signal  */
66 /*              */
67 static int32 direct_buffer[AUDIO_BUFFER_SIZE * 2];
68 static int32 direct_bufsize = sizeof(direct_buffer);
69 
70 #if OPT_MODE != 0 && !defined(_AMD64_) && ( defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 1380)) )
set_dry_signal(int32 * buf,int32 count)71 void set_dry_signal(int32 *buf, int32 count)
72 {
73 	int32 *dbuf = direct_buffer;
74 	_asm {
75 		mov		ecx, [count]
76 		mov		esi, [buf]
77 		test	ecx, ecx
78 		jz		short L2
79 		mov		edi, [dbuf]
80 L1:		mov		eax, [esi]
81 		mov		ebx, [edi]
82 		add		esi, 4
83 		add		ebx, eax
84 		mov		[edi], ebx
85 		add		edi, 4
86 		dec		ecx
87 		jnz		L1
88 L2:
89 	}
90 }
91 #else
set_dry_signal(register int32 * buf,int32 n)92 void set_dry_signal(register int32 *buf, int32 n)
93 {
94 #if USE_ALTIVEC
95   if(is_altivec_available()) {
96     v_set_dry_signal(direct_buffer, buf, n);
97   } else {
98 #endif
99     register int32 i;
100 	register int32 *dbuf = direct_buffer;
101 
102     for(i = n - 1; i >= 0; i--)
103     {
104         dbuf[i] += buf[i];
105     }
106 #if USE_ALTIVEC
107   }
108 #endif
109 }
110 #endif
111 
112 /* XG has "dry level". */
113 #if OPT_MODE != 0 	/* fixed-point implementation */
114 #if  !defined(_AMD64_) && (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 1380)) )
set_dry_signal_xg(int32 * buf,int32 count,int32 level)115 void set_dry_signal_xg(int32 *buf, int32 count, int32 level)
116 {
117 	int32 *dbuf = direct_buffer;
118 	if(!level) {return;}
119 	level = level * 65536 / 127;
120 
121 	_asm {
122 		mov		ecx, [count]
123 		mov		esi, [buf]
124 		mov		ebx, [level]
125 		test	ecx, ecx
126 		jz		short L2
127 		mov		edi, [dbuf]
128 L1:		mov		eax, [esi]
129 		imul	ebx
130 		shr		eax, 16
131 		shl		edx, 16
132 		or		eax, edx	/* u */
133 		mov		edx, [edi]	/* v */
134 		add		esi, 4		/* u */
135 		add		edx, eax	/* v */
136 		mov		[edi], edx	/* u */
137 		add		edi, 4		/* v */
138 		dec		ecx			/* u */
139 		jnz		L1			/* v */
140 L2:
141 	}
142 }
143 #else
set_dry_signal_xg(register int32 * sbuffer,int32 n,int32 level)144 void set_dry_signal_xg(register int32 *sbuffer, int32 n, int32 level)
145 {
146     register int32 i;
147 	int32 *buf = direct_buffer;
148 	if(!level) {return;}
149 	level = level * 65536 / 127;
150 
151 	for(i = n - 1; i >= 0; i--) {buf[i] += imuldiv16(sbuffer[i], level);}
152 }
153 #endif	/* _MSC_VER */
154 #else	/* floating-point implementation */
set_dry_signal_xg(register int32 * sbuffer,int32 n,int32 level)155 void set_dry_signal_xg(register int32 *sbuffer, int32 n, int32 level)
156 {
157     register int32 i;
158     register int32 count = n;
159 	if(!level) {return;}
160     FLOAT_T send_level = (FLOAT_T)level / 127.0;
161 
162     for(i = 0; i < count; i++)
163     {
164 		direct_buffer[i] += sbuffer[i] * send_level;
165     }
166 }
167 #endif /* OPT_MODE != 0 */
168 
169 #ifdef SYS_EFFECT_CLIP
mix_dry_signal(int32 * buf,int32 n)170 void mix_dry_signal(int32 *buf, int32 n)
171 {
172 	int32 i, x;
173 	for (i = 0; i < n; i++) {
174 		x = direct_buffer[i];
175 		buf[i] = (x > CLIP_AMP_MAX) ? CLIP_AMP_MAX
176 				: (x < CLIP_AMP_MIN) ? CLIP_AMP_MIN : x;
177 	}
178 	memset(direct_buffer, 0, sizeof(int32) * n);
179 }
180 #else /* SYS_EFFECT_CLIP */
mix_dry_signal(int32 * buf,int32 n)181 void mix_dry_signal(int32 *buf, int32 n)
182 {
183  	memcpy(buf, direct_buffer, sizeof(int32) * n);
184 	memset(direct_buffer, 0, sizeof(int32) * n);
185 }
186 #endif /* SYS_EFFECT_CLIP */
187 
188 /*                    */
189 /*  Effect Utilities  */
190 /*                    */
191 #ifdef __BORLANDC__
isprime(int val)192 static int isprime(int val)
193 #else
194 static inline int isprime(int val)
195 #endif
196 {
197 	int i;
198 	if (val == 2) {return 1;}
199 	if (val & 1) {
200 		for (i = 3; i < (int)sqrt((double)val) + 1; i += 2) {
201 			if ((val % i) == 0) {return 0;}
202 		}
203 		return 1; /* prime */
204 	} else {return 0;} /* even */
205 }
206 
207 /*! delay */
free_delay(simple_delay * delay)208 static void free_delay(simple_delay *delay)
209 {
210 	if(delay->buf != NULL) {
211 		free(delay->buf);
212 		delay->buf = NULL;
213 	}
214 }
215 
set_delay(simple_delay * delay,int32 size)216 static void set_delay(simple_delay *delay, int32 size)
217 {
218 	if(size < 1) {size = 1;}
219 	free_delay(delay);
220 	delay->buf = (int32 *)safe_malloc(sizeof(int32) * size);
221 	if(delay->buf == NULL) {return;}
222 	delay->index = 0;
223 	delay->size = size;
224 	memset(delay->buf, 0, sizeof(int32) * delay->size);
225 }
226 
do_delay(int32 * stream,int32 * buf,int32 size,int32 * index)227 static inline void do_delay(int32 *stream, int32 *buf, int32 size, int32 *index)
228 {
229 	int32 output;
230 	output = buf[*index];
231 	buf[*index] = *stream;
232 	if (++*index >= size) {*index = 0;}
233 	*stream = output;
234 }
235 
236 /*! LFO (low frequency oscillator) */
init_lfo(lfo * lfo,double freq,int type,double phase)237 static void init_lfo(lfo *lfo, double freq, int type, double phase)
238 {
239 	int32 i, cycle, diff;
240 
241 	lfo->count = 0;
242 	lfo->freq = freq;
243 	if (lfo->freq < 0.05) {lfo->freq = 0.05;}
244 	cycle = (double)play_mode->rate / lfo->freq;
245 	if (cycle < 1) {cycle = 1;}
246 	lfo->cycle = cycle;
247 	lfo->icycle = TIM_FSCALE((SINE_CYCLE_LENGTH - 1) / (double)cycle, 24) - 0.5;
248 	diff = SINE_CYCLE_LENGTH * phase / 360.0;
249 
250 	if(lfo->type != type) {	/* generate LFO waveform */
251 		switch(type) {
252 		case LFO_SINE:
253 			for(i = 0; i < SINE_CYCLE_LENGTH; i++)
254 				lfo->buf[i] = TIM_FSCALE((lookup_sine(i + diff) + 1.0) / 2.0, 16);
255 			break;
256 		case LFO_TRIANGULAR:
257 			for(i = 0; i < SINE_CYCLE_LENGTH; i++)
258 				lfo->buf[i] = TIM_FSCALE((lookup_triangular(i + diff) + 1.0) / 2.0, 16);
259 			break;
260 		default:
261 			for(i = 0; i < SINE_CYCLE_LENGTH; i++) {lfo->buf[i] = TIM_FSCALE(0.5, 16);}
262 			break;
263 		}
264 	}
265 	lfo->type = type;
266 }
267 
268 /* returned value is between 0 and (1 << 16) */
do_lfo(lfo * lfo)269 static inline int32 do_lfo(lfo *lfo)
270 {
271 	int32 val;
272 	val = lfo->buf[imuldiv24(lfo->count, lfo->icycle)];
273 	if(++lfo->count == lfo->cycle) {lfo->count = 0;}
274 	return val;
275 }
276 
277 #if 0
278 /*! modulated delay with allpass interpolation (for Chorus Effect,...) */
279 static void free_mod_delay(mod_delay *delay)
280 {
281 	if(delay->buf != NULL) {
282 		free(delay->buf);
283 		delay->buf = NULL;
284 	}
285 }
286 #endif
287 
288 #if 0
289 static void set_mod_delay(mod_delay *delay, int32 ndelay, int32 depth)
290 {
291 	int32 size = ndelay + depth + 1;
292 	free_mod_delay(delay);
293 	delay->buf = (int32 *)safe_malloc(sizeof(int32) * size);
294 	if(delay->buf == NULL) {return;}
295 	delay->rindex = 0;
296 	delay->windex = 0;
297 	delay->hist = 0;
298 	delay->ndelay = ndelay;
299 	delay->depth = depth;
300 	delay->size = size;
301 	memset(delay->buf, 0, sizeof(int32) * delay->size);
302 }
303 #endif
304 
do_mod_delay(int32 * stream,int32 * buf,int32 size,int32 * rindex,int32 * windex,int32 ndelay,int32 depth,int32 lfoval,int32 * hist)305 static inline void do_mod_delay(int32 *stream, int32 *buf, int32 size, int32 *rindex, int32 *windex,
306 								int32 ndelay, int32 depth, int32 lfoval, int32 *hist)
307 {
308 	int32 t1, t2;
309 	if (++*windex == size) {*windex = 0;}
310 	t1 = buf[*rindex];
311 	t2 = imuldiv24(lfoval, depth);
312 	*rindex = *windex - ndelay - (t2 >> 8);
313 	if (*rindex < 0) {*rindex += size;}
314 	t2 = 0xFF - (t2 & 0xFF);
315 	*hist = t1 + imuldiv8(buf[*rindex] - *hist, t2);
316 	buf[*windex] = *stream;
317 	*stream = *hist;
318 }
319 
320 /*! modulated allpass filter with allpass interpolation (for Plate Reverberator,...) */
free_mod_allpass(mod_allpass * delay)321 static void free_mod_allpass(mod_allpass *delay)
322 {
323 	if(delay->buf != NULL) {
324 		free(delay->buf);
325 		delay->buf = NULL;
326 	}
327 }
328 
set_mod_allpass(mod_allpass * delay,int32 ndelay,int32 depth,double feedback)329 static void set_mod_allpass(mod_allpass *delay, int32 ndelay, int32 depth, double feedback)
330 {
331 	int32 size = ndelay + depth + 1;
332 	free_mod_allpass(delay);
333 	delay->buf = (int32 *)safe_malloc(sizeof(int32) * size);
334 	if(delay->buf == NULL) {return;}
335 	delay->rindex = 0;
336 	delay->windex = 0;
337 	delay->hist = 0;
338 	delay->ndelay = ndelay;
339 	delay->depth = depth;
340 	delay->size = size;
341 	delay->feedback = feedback;
342 	delay->feedbacki = TIM_FSCALE(feedback, 24);
343 	memset(delay->buf, 0, sizeof(int32) * delay->size);
344 }
345 
do_mod_allpass(int32 * stream,int32 * buf,int32 size,int32 * rindex,int32 * windex,int32 ndelay,int32 depth,int32 lfoval,int32 * hist,int32 feedback)346 static inline void do_mod_allpass(int32 *stream, int32 *buf, int32 size, int32 *rindex, int32 *windex,
347 								  int32 ndelay, int32 depth, int32 lfoval, int32 *hist, int32 feedback)
348 {
349 	int t1, t2, t3;
350 	if (++*windex == size) {*windex = 0;}
351 	t3 = *stream + imuldiv24(*hist, feedback);
352 	t1 = buf[*rindex];
353 	t2 = imuldiv24(lfoval, depth);
354 	*rindex = *windex - ndelay - (t2 >> 8);
355 	if (*rindex < 0) {*rindex += size;}
356 	t2 = 0xFF - (t2 & 0xFF);
357 	*hist = t1 + imuldiv8(buf[*rindex] - *hist, t2);
358 	buf[*windex] = t3;
359 	*stream = *hist - imuldiv24(t3, feedback);
360 }
361 
362 /* allpass filter */
free_allpass(allpass * allpass)363 static void free_allpass(allpass *allpass)
364 {
365 	if(allpass->buf != NULL) {
366 		free(allpass->buf);
367 		allpass->buf = NULL;
368 	}
369 }
370 
set_allpass(allpass * allpass,int32 size,double feedback)371 static void set_allpass(allpass *allpass, int32 size, double feedback)
372 {
373 	if(allpass->buf != NULL) {
374 		free(allpass->buf);
375 		allpass->buf = NULL;
376 	}
377 	allpass->buf = (int32 *)safe_malloc(sizeof(int32) * size);
378 	if(allpass->buf == NULL) {return;}
379 	allpass->index = 0;
380 	allpass->size = size;
381 	allpass->feedback = feedback;
382 	allpass->feedbacki = TIM_FSCALE(feedback, 24);
383 	memset(allpass->buf, 0, sizeof(int32) * allpass->size);
384 }
385 
do_allpass(int32 * stream,int32 * buf,int32 size,int32 * index,int32 feedback)386 static inline void do_allpass(int32 *stream, int32 *buf, int32 size, int32 *index, int32 feedback)
387 {
388 	int32 bufout, output;
389 	bufout = buf[*index];
390 	output = *stream - imuldiv24(bufout, feedback);
391 	buf[*index] = output;
392 	if (++*index >= size) {*index = 0;}
393 	*stream = bufout + imuldiv24(output, feedback);
394 }
395 
init_filter_moog(filter_moog * svf)396 static void init_filter_moog(filter_moog *svf)
397 {
398 	svf->b0 = svf->b1 = svf->b2 = svf->b3 = svf->b4 = 0;
399 }
400 
401 /*! calculate Moog VCF coefficients */
calc_filter_moog(filter_moog * svf)402 void calc_filter_moog(filter_moog *svf)
403 {
404 	double res, fr, p, q, f;
405 
406 	if (svf->freq > play_mode->rate / 2) {svf->freq = play_mode->rate / 2;}
407 	else if(svf->freq < 20) {svf->freq = 20;}
408 
409 	if(svf->freq != svf->last_freq || svf->res_dB != svf->last_res_dB) {
410 		if(svf->last_freq == 0) {init_filter_moog(svf);}
411 		svf->last_freq = svf->freq, svf->last_res_dB = svf->res_dB;
412 
413 		res = pow(10, (svf->res_dB - 96) / 20);
414 		fr = 2.0 * (double)svf->freq / (double)play_mode->rate;
415 		q = 1.0 - fr;
416 		p = fr + 0.8 * fr * q;
417 		f = p + p - 1.0;
418 		q = res * (1.0 + 0.5 * q * (1.0 - q + 5.6 * q * q));
419 		svf->f = TIM_FSCALE(f, 24);
420 		svf->p = TIM_FSCALE(p, 24);
421 		svf->q = TIM_FSCALE(q, 24);
422 	}
423 }
424 
do_filter_moog(int32 * stream,int32 * high,int32 f,int32 p,int32 q,int32 * b0,int32 * b1,int32 * b2,int32 * b3,int32 * b4)425 static inline void do_filter_moog(int32 *stream, int32 *high, int32 f, int32 p, int32 q,
426 								  int32 *b0, int32 *b1, int32 *b2, int32 *b3, int32 *b4)
427 {
428 	int32 t1, t2, t3, tb0 = *b0, tb1 = *b1, tb2 = *b2, tb3 = *b3, tb4 = *b4;
429 	t3 = *stream - imuldiv24(q, tb4);
430 	t1 = tb1;	tb1 = imuldiv24(t3 + tb0, p) - imuldiv24(tb1, f);
431 	t2 = tb2;	tb2 = imuldiv24(tb1 + t1, p) - imuldiv24(tb2, f);
432 	t1 = tb3;	tb3 = imuldiv24(tb2 + t2, p) - imuldiv24(tb3, f);
433 	*stream = tb4 = imuldiv24(tb3 + t1, p) - imuldiv24(tb4, f);
434 	tb0 = t3;
435 	*stream = tb4;
436 	*high = t3 - tb4;
437 	*b0 = tb0, *b1 = tb1, *b2 = tb2, *b3 = tb3, *b4 = tb4;
438 }
439 
init_filter_moog_dist(filter_moog_dist * svf)440 static void init_filter_moog_dist(filter_moog_dist *svf)
441 {
442 	svf->b0 = svf->b1 = svf->b2 = svf->b3 = svf->b4 = 0.0;
443 }
444 
445 /*! calculate Moog VCF coefficients */
calc_filter_moog_dist(filter_moog_dist * svf)446 void calc_filter_moog_dist(filter_moog_dist *svf)
447 {
448 	double res, fr, p, q, f;
449 
450 	if (svf->freq > play_mode->rate / 2) {svf->freq = play_mode->rate / 2;}
451 	else if (svf->freq < 20) {svf->freq = 20;}
452 
453 	if (svf->freq != svf->last_freq || svf->res_dB != svf->last_res_dB
454 		 || svf->dist != svf->last_dist) {
455 		if (svf->last_freq == 0) {init_filter_moog_dist(svf);}
456 		svf->last_freq = svf->freq, svf->last_res_dB = svf->res_dB,
457 			svf->last_dist = svf->dist;
458 
459 		res = pow(10.0, (svf->res_dB - 96.0) / 20.0);
460 		fr = 2.0 * (double)svf->freq / (double)play_mode->rate;
461 		q = 1.0 - fr;
462 		p = fr + 0.8 * fr * q;
463 		f = p + p - 1.0;
464 		q = res * (1.0 + 0.5 * q * (1.0 - q + 5.6 * q * q));
465 		svf->f = f;
466 		svf->p = p;
467 		svf->q = q;
468 		svf->d = 1.0 + svf->dist;
469 	}
470 }
471 
do_filter_moog_dist(double * stream,double * high,double * band,double f,double p,double q,double d,double * b0,double * b1,double * b2,double * b3,double * b4)472 static inline void do_filter_moog_dist(double *stream, double *high, double *band, double f, double p, double q, double d,
473 								   double *b0, double *b1, double *b2, double *b3, double *b4)
474 {
475 	double t1, t2, in, tb0 = *b0, tb1 = *b1, tb2 = *b2, tb3 = *b3, tb4 = *b4;
476 	in = *stream;
477 	in -= q * tb4;
478 	t1 = tb1;  tb1 = (in + tb0) * p - tb1 * f;
479 	t2 = tb2;  tb2 = (tb1 + t1) * p - tb2 * f;
480 	t1 = tb3;  tb3 = (tb2 + t2) * p - tb3 * f;
481 	tb4 = (tb3 + t1) * p - tb4 * f;
482 	tb4 *= d;
483 	tb4 = tb4 - tb4 * tb4 * tb4 * 0.166667;
484 	tb0 = in;
485 	*stream = tb4;
486 	*high = in - tb4;
487 	*band = 3.0 * (tb3 - tb4);
488 	*b0 = tb0, *b1 = tb1, *b2 = tb2, *b3 = tb3, *b4 = tb4;
489 }
490 
do_filter_moog_dist_band(double * stream,double f,double p,double q,double d,double * b0,double * b1,double * b2,double * b3,double * b4)491 static inline void do_filter_moog_dist_band(double *stream, double f, double p, double q, double d,
492 								   double *b0, double *b1, double *b2, double *b3, double *b4)
493 {
494 	double t1, t2, in, tb0 = *b0, tb1 = *b1, tb2 = *b2, tb3 = *b3, tb4 = *b4;
495 	in = *stream;
496 	in -= q * tb4;
497 	t1 = tb1;  tb1 = (in + tb0) * p - tb1 * f;
498 	t2 = tb2;  tb2 = (tb1 + t1) * p - tb2 * f;
499 	t1 = tb3;  tb3 = (tb2 + t2) * p - tb3 * f;
500 	tb4 = (tb3 + t1) * p - tb4 * f;
501 	tb4 *= d;
502 	tb4 = tb4 - tb4 * tb4 * tb4 * 0.166667;
503 	tb0 = in;
504 	*stream = 3.0 * (tb3 - tb4);
505 	*b0 = tb0, *b1 = tb1, *b2 = tb2, *b3 = tb3, *b4 = tb4;
506 }
507 
init_filter_lpf18(filter_lpf18 * p)508 static void init_filter_lpf18(filter_lpf18 *p)
509 {
510 	p->ay1 = p->ay2 = p->aout = p->lastin = 0;
511 }
512 
513 /*! calculate LPF18 coefficients */
calc_filter_lpf18(filter_lpf18 * p)514 void calc_filter_lpf18(filter_lpf18 *p)
515 {
516 	double kfcn, kp, kp1, kp1h, kres, value;
517 
518 	if(p->freq != p->last_freq || p->dist != p->last_dist
519 		|| p->res != p->last_res) {
520 		if(p->last_freq == 0) {init_filter_lpf18(p);}
521 		p->last_freq = p->freq, p->last_dist = p->dist, p->last_res = p->res;
522 
523 		kfcn = 2.0 * (double)p->freq / (double)play_mode->rate;
524 		kp = ((-2.7528 * kfcn + 3.0429) * kfcn + 1.718) * kfcn - 0.9984;
525 		kp1 = kp + 1.0;
526 		kp1h = 0.5 * kp1;
527 		kres = p->res * (((-2.7079 * kp1 + 10.963) * kp1 - 14.934) * kp1 + 8.4974);
528 		value = 1.0 + (p->dist * (1.5 + 2.0 * kres * (1.0 - kfcn)));
529 
530 		p->kp = kp, p->kp1h = kp1h, p->kres = kres, p->value = value;
531 	}
532 }
533 
do_filter_lpf18(double * stream,double * ay1,double * ay2,double * aout,double * lastin,double kres,double value,double kp,double kp1h)534 static inline void do_filter_lpf18(double *stream, double *ay1, double *ay2, double *aout, double *lastin,
535 								   double kres, double value, double kp, double kp1h)
536 {
537 	double ax1, ay11, ay31;
538 	ax1 = *lastin;
539 	ay11 = *ay1;
540 	ay31 = *ay2;
541 	*lastin = *stream - tanh(kres * *aout);
542 	*ay1 = kp1h * (*lastin + ax1) - kp * *ay1;
543 	*ay2 = kp1h * (*ay1 + ay11) - kp * *ay2;
544 	*aout = kp1h * (*ay2 + ay31) - kp * *aout;
545 	*stream = tanh(*aout * value);
546 }
547 
548 #define WS_AMP_MAX	((int32) 0x0fffffff)
549 #define WS_AMP_MIN	((int32)-0x0fffffff)
550 
do_dummy_clipping(int32 * stream,int32 d)551 static void do_dummy_clipping(int32 *stream, int32 d) {}
552 
do_hard_clipping(int32 * stream,int32 d)553 static void do_hard_clipping(int32 *stream, int32 d)
554 {
555 	int32 x;
556 	x = imuldiv24(*stream, d);
557 	x = (x > WS_AMP_MAX) ? WS_AMP_MAX
558 			: (x < WS_AMP_MIN) ? WS_AMP_MIN : x;
559 	*stream = x;
560 }
561 
do_soft_clipping1(int32 * stream,int32 d)562 static void do_soft_clipping1(int32 *stream, int32 d)
563 {
564 	int32 x, ai = TIM_FSCALE(1.5, 24), bi = TIM_FSCALE(0.5, 24);
565 	x = imuldiv24(*stream, d);
566 	x = (x > WS_AMP_MAX) ? WS_AMP_MAX
567 			: (x < WS_AMP_MIN) ? WS_AMP_MIN : x;
568 	x = imuldiv24(x, ai) - imuldiv24(imuldiv28(imuldiv28(x, x), x), bi);
569 	*stream = x;
570 }
571 
do_soft_clipping2(int32 * stream,int32 d)572 static void do_soft_clipping2(int32 *stream, int32 d)
573 {
574 	int32 x;
575 	x = imuldiv24(*stream, d);
576 	x = (x > WS_AMP_MAX) ? WS_AMP_MAX
577 			: (x < WS_AMP_MIN) ? WS_AMP_MIN : x;
578 	x = signlong(x) * ((abs(x) << 1) - imuldiv28(x, x));
579 	*stream = x;
580 }
581 
582 /*! 1st order lowpass filter */
init_filter_lowpass1(filter_lowpass1 * p)583 void init_filter_lowpass1(filter_lowpass1 *p)
584 {
585 	if (p->a > 1.0) {p->a = 1.0;}
586 	p->x1l = p->x1r = 0;
587 	p->ai = TIM_FSCALE(p->a, 24);
588 	p->iai = TIM_FSCALE(1.0 - p->a, 24);
589 }
590 
do_filter_lowpass1(int32 * stream,int32 * x1,int32 a,int32 ia)591 static inline void do_filter_lowpass1(int32 *stream, int32 *x1, int32 a, int32 ia)
592 {
593 	*stream = *x1 = imuldiv24(*x1, ia) + imuldiv24(*stream, a);
594 }
595 
do_filter_lowpass1_stereo(int32 * buf,int32 count,filter_lowpass1 * p)596 void do_filter_lowpass1_stereo(int32 *buf, int32 count, filter_lowpass1 *p)
597 {
598 	int32 i, a = p->ai, ia = p->iai, x1l = p->x1l, x1r = p->x1r;
599 
600 	for(i = 0; i < count; i++) {
601 		do_filter_lowpass1(&buf[i], &x1l, a, ia);
602 		++i;
603 		do_filter_lowpass1(&buf[i], &x1r, a, ia);
604 	}
605 	p->x1l = x1l, p->x1r = x1r;
606 }
607 
init_filter_biquad(filter_biquad * p)608 void init_filter_biquad(filter_biquad *p)
609 {
610 	p->x1l = 0, p->x2l = 0, p->y1l = 0, p->y2l = 0, p->x1r = 0,
611 		p->x2r = 0, p->y1r = 0, p->y2r = 0;
612 }
613 
614 /*! biquad lowpass filter */
calc_filter_biquad_low(filter_biquad * p)615 void calc_filter_biquad_low(filter_biquad *p)
616 {
617 	double a0, a1, a2, b1, b02, omega, sn, cs, alpha;
618 
619 	if(p->freq != p->last_freq || p->q != p->last_q) {
620 		if (p->last_freq == 0) {init_filter_biquad(p);}
621 		p->last_freq = p->freq, p->last_q = p->q;
622 		omega = 2.0 * M_PI * (double)p->freq / (double)play_mode->rate;
623 		sn = sin(omega);
624 		cs = cos(omega);
625 		if (p->q == 0 || p->freq < 0 || p->freq > play_mode->rate / 2) {
626 			p->b02 = TIM_FSCALE(1.0, 24);
627 			p->a1 = p->a2 = p->b1 = 0;
628 			return;
629 		} else {alpha = sn / (2.0 * p->q);}
630 
631 		a0 = 1.0 / (1.0 + alpha);
632 		b02 = ((1.0 - cs) / 2.0) * a0;
633 		b1 = (1.0 - cs) * a0;
634 		a1 = (-2.0 * cs) * a0;
635 		a2 = (1.0 - alpha) * a0;
636 
637 		p->b1 = TIM_FSCALE(b1, 24);
638 		p->a2 = TIM_FSCALE(a2, 24);
639 		p->a1 = TIM_FSCALE(a1, 24);
640 		p->b02 = TIM_FSCALE(b02, 24);
641 	}
642 }
643 
644 /*! biquad highpass filter */
calc_filter_biquad_high(filter_biquad * p)645 void calc_filter_biquad_high(filter_biquad *p)
646 {
647 	double a0, a1, a2, b1, b02, omega, sn, cs, alpha;
648 
649 	if(p->freq != p->last_freq || p->q != p->last_q) {
650 		if (p->last_freq == 0) {init_filter_biquad(p);}
651 		p->last_freq = p->freq, p->last_q = p->q;
652 		omega = 2.0 * M_PI * (double)p->freq / (double)play_mode->rate;
653 		sn = sin(omega);
654 		cs = cos(omega);
655 		if (p->q == 0 || p->freq < 0 || p->freq > play_mode->rate / 2) {
656 			p->b02 = TIM_FSCALE(1.0, 24);
657 			p->a1 = p->a2 = p->b1 = 0;
658 			return;
659 		} else {alpha = sn / (2.0 * p->q);}
660 
661 		a0 = 1.0 / (1.0 + alpha);
662 		b02 = ((1.0 + cs) / 2.0) * a0;
663 		b1 = (-(1.0 + cs)) * a0;
664 		a1 = (-2.0 * cs) * a0;
665 		a2 = (1.0 - alpha) * a0;
666 
667 		p->b1 = TIM_FSCALE(b1, 24);
668 		p->a2 = TIM_FSCALE(a2, 24);
669 		p->a1 = TIM_FSCALE(a1, 24);
670 		p->b02 = TIM_FSCALE(b02, 24);
671 	}
672 }
673 
do_filter_biquad(int32 * stream,int32 a1,int32 a2,int32 b1,int32 b02,int32 * x1,int32 * x2,int32 * y1,int32 * y2)674 static inline void do_filter_biquad(int32 *stream, int32 a1, int32 a2, int32 b1,
675 									int32 b02, int32 *x1, int32 *x2, int32 *y1, int32 *y2)
676 {
677 	int32 t1;
678 	t1 = imuldiv24(*stream + *x2, b02) + imuldiv24(*x1, b1) - imuldiv24(*y1, a1) - imuldiv24(*y2, a2);
679 	*x2 = *x1;
680 	*x1 = *stream;
681 	*y2 = *y1;
682 	*y1 = t1;
683 	*stream = t1;
684 }
685 
686 #if 0
687 static void do_biquad_filter_stereo(int32* buf, int32 count, filter_biquad *p)
688 {
689 	int32 i;
690 	int32 x1l = p->x1l, x2l = p->x2l, y1l = p->y1l, y2l = p->y2l,
691 		x1r = p->x1r, x2r = p->x2r, y1r = p->y1r, y2r = p->y2r;
692 	int32 a1 = p->a1, a2 = p->a2, b02 = p->b02, b1 = p->b1;
693 
694 	for(i = 0; i < count; i++) {
695 		do_filter_biquad(&(buf[i]), a1, a2, b1, b02, &x1l, &x2l, &y1l, &y2l);
696 		++i;
697 		do_filter_biquad(&(buf[i]), a1, a2, b1, b02, &x1r, &x2r, &y1r, &y2r);
698 	}
699 	p->x1l = x1l, p->x2l = x2l, p->y1l = y1l, p->y2l = y2l,
700 		p->x1r = x1r, p->x2r = x2r, p->y1r = y1r, p->y2r = y2r;
701 }
702 #endif
703 
init_filter_shelving(filter_shelving * p)704 void init_filter_shelving(filter_shelving *p)
705 {
706 	p->x1l = 0, p->x2l = 0, p->y1l = 0, p->y2l = 0, p->x1r = 0,
707 		p->x2r = 0, p->y1r = 0, p->y2r = 0;
708 }
709 
710 /*! shelving filter */
calc_filter_shelving_low(filter_shelving * p)711 void calc_filter_shelving_low(filter_shelving *p)
712 {
713 	double a0, a1, a2, b0, b1, b2, omega, sn, cs, A, beta;
714 
715 	init_filter_shelving(p);
716 
717 	A = pow(10, p->gain / 40);
718 	omega = 2.0 * M_PI * (double)p->freq / (double)play_mode->rate;
719 	sn = sin(omega);
720 	cs = cos(omega);
721 	if (p->freq < 0 || p->freq > play_mode->rate / 2) {
722 		p->b0 = TIM_FSCALE(1.0, 24);
723 		p->a1 = p->b1 = p->a2 = p->b2 = 0;
724 		return;
725 	}
726 	if (p->q == 0) {beta = sqrt(A + A);}
727 	else {beta = sqrt(A) / p->q;}
728 
729 	a0 = 1.0 / ((A + 1) + (A - 1) * cs + beta * sn);
730 	a1 = 2.0 * ((A - 1) + (A + 1) * cs);
731 	a2 = -((A + 1) + (A - 1) * cs - beta * sn);
732 	b0 = A * ((A + 1) - (A - 1) * cs + beta * sn);
733 	b1 = 2.0 * A * ((A - 1) - (A + 1) * cs);
734 	b2 = A * ((A + 1) - (A - 1) * cs - beta * sn);
735 
736 	a1 *= a0;
737 	a2 *= a0;
738 	b1 *= a0;
739 	b2 *= a0;
740 	b0 *= a0;
741 
742 	p->a1 = TIM_FSCALE(a1, 24);
743 	p->a2 = TIM_FSCALE(a2, 24);
744 	p->b0 = TIM_FSCALE(b0, 24);
745 	p->b1 = TIM_FSCALE(b1, 24);
746 	p->b2 = TIM_FSCALE(b2, 24);
747 }
748 
calc_filter_shelving_high(filter_shelving * p)749 void calc_filter_shelving_high(filter_shelving *p)
750 {
751 	double a0, a1, a2, b0, b1, b2, omega, sn, cs, A, beta;
752 
753 	init_filter_shelving(p);
754 
755 	A = pow(10, p->gain / 40);
756 	omega = 2.0 * M_PI * (double)p->freq / (double)play_mode->rate;
757 	sn = sin(omega);
758 	cs = cos(omega);
759 	if (p->freq < 0 || p->freq > play_mode->rate / 2) {
760 		p->b0 = TIM_FSCALE(1.0, 24);
761 		p->a1 = p->b1 = p->a2 = p->b2 = 0;
762 		return;
763 	}
764 	if (p->q == 0) {beta = sqrt(A + A);}
765 	else {beta = sqrt(A) / p->q;}
766 
767 	a0 = 1.0 / ((A + 1) - (A - 1) * cs + beta * sn);
768 	a1 = (-2 * ((A - 1) - (A + 1) * cs));
769 	a2 = -((A + 1) - (A - 1) * cs - beta * sn);
770 	b0 = A * ((A + 1) + (A - 1) * cs + beta * sn);
771 	b1 = -2 * A * ((A - 1) + (A + 1) * cs);
772 	b2 = A * ((A + 1) + (A - 1) * cs - beta * sn);
773 
774 	a1 *= a0;
775 	a2 *= a0;
776 	b0 *= a0;
777 	b1 *= a0;
778 	b2 *= a0;
779 
780 	p->a1 = TIM_FSCALE(a1, 24);
781 	p->a2 = TIM_FSCALE(a2, 24);
782 	p->b0 = TIM_FSCALE(b0, 24);
783 	p->b1 = TIM_FSCALE(b1, 24);
784 	p->b2 = TIM_FSCALE(b2, 24);
785 }
786 
do_shelving_filter_stereo(int32 * buf,int32 count,filter_shelving * p)787 static void do_shelving_filter_stereo(int32* buf, int32 count, filter_shelving *p)
788 {
789 	int32 i;
790 	int32 x1l = p->x1l, x2l = p->x2l, y1l = p->y1l, y2l = p->y2l,
791 		x1r = p->x1r, x2r = p->x2r, y1r = p->y1r, y2r = p->y2r, yout;
792 	int32 a1 = p->a1, a2 = p->a2, b0 = p->b0, b1 = p->b1, b2 = p->b2;
793 
794 	for(i = 0; i < count; i++) {
795 		yout = imuldiv24(buf[i], b0) + imuldiv24(x1l, b1) + imuldiv24(x2l, b2) + imuldiv24(y1l, a1) + imuldiv24(y2l, a2);
796 		x2l = x1l;
797 		x1l = buf[i];
798 		y2l = y1l;
799 		y1l = yout;
800 		buf[i] = yout;
801 
802 		yout = imuldiv24(buf[++i], b0) + imuldiv24(x1r, b1) + imuldiv24(x2r, b2) + imuldiv24(y1r, a1) + imuldiv24(y2r, a2);
803 		x2r = x1r;
804 		x1r = buf[i];
805 		y2r = y1r;
806 		y1r = yout;
807 		buf[i] = yout;
808 	}
809 	p->x1l = x1l, p->x2l = x2l, p->y1l = y1l, p->y2l = y2l,
810 		p->x1r = x1r, p->x2r = x2r, p->y1r = y1r, p->y2r = y2r;
811 }
812 
init_filter_peaking(filter_peaking * p)813 void init_filter_peaking(filter_peaking *p)
814 {
815 	p->x1l = 0, p->x2l = 0, p->y1l = 0, p->y2l = 0, p->x1r = 0,
816 		p->x2r = 0, p->y1r = 0, p->y2r = 0;
817 }
818 
819 /*! peaking filter */
calc_filter_peaking(filter_peaking * p)820 void calc_filter_peaking(filter_peaking *p)
821 {
822 	double a0, ba1, a2, b0, b2, omega, sn, cs, A, alpha;
823 
824 	init_filter_peaking(p);
825 
826 	A = pow(10, p->gain / 40);
827 	omega = 2.0 * M_PI * (double)p->freq / (double)play_mode->rate;
828 	sn = sin(omega);
829 	cs = cos(omega);
830 	if (p->q == 0 || p->freq < 0 || p->freq > play_mode->rate / 2) {
831 		p->b0 = TIM_FSCALE(1.0, 24);
832 		p->ba1 = p->a2 = p->b2 = 0;
833 		return;
834 	} else {alpha = sn / (2.0 * p->q);}
835 
836 	a0 = 1.0 / (1.0 + alpha / A);
837 	ba1 = -2.0 * cs;
838 	a2 = 1.0 - alpha / A;
839 	b0 = 1.0 + alpha * A;
840 	b2 = 1.0 - alpha * A;
841 
842 	ba1 *= a0;
843 	a2 *= a0;
844 	b0 *= a0;
845 	b2 *= a0;
846 
847 	p->ba1 = TIM_FSCALE(ba1, 24);
848 	p->a2 = TIM_FSCALE(a2, 24);
849 	p->b0 = TIM_FSCALE(b0, 24);
850 	p->b2 = TIM_FSCALE(b2, 24);
851 }
852 
do_peaking_filter_stereo(int32 * buf,int32 count,filter_peaking * p)853 static void do_peaking_filter_stereo(int32* buf, int32 count, filter_peaking *p)
854 {
855 	int32 i;
856 	int32 x1l = p->x1l, x2l = p->x2l, y1l = p->y1l, y2l = p->y2l,
857 		x1r = p->x1r, x2r = p->x2r, y1r = p->y1r, y2r = p->y2r, yout;
858 	int32 ba1 = p->ba1, a2 = p->a2, b0 = p->b0, b2 = p->b2;
859 
860 	for(i = 0; i < count; i++) {
861 		yout = imuldiv24(buf[i], b0) + imuldiv24(x1l - y1l, ba1) + imuldiv24(x2l, b2) - imuldiv24(y2l, a2);
862 		x2l = x1l;
863 		x1l = buf[i];
864 		y2l = y1l;
865 		y1l = yout;
866 		buf[i] = yout;
867 
868 		yout = imuldiv24(buf[++i], b0) + imuldiv24(x1r - y1r, ba1) + imuldiv24(x2r, b2) - imuldiv24(y2r, a2);
869 		x2r = x1r;
870 		x1r = buf[i];
871 		y2r = y1r;
872 		y1r = yout;
873 		buf[i] = yout;
874 	}
875 	p->x1l = x1l, p->x2l = x2l, p->y1l = y1l, p->y2l = y2l,
876 		p->x1r = x1r, p->x2r = x2r, p->y1r = y1r, p->y2r = y2r;
877 }
878 
init_pink_noise(pink_noise * p)879 void init_pink_noise(pink_noise *p)
880 {
881 	p->b0 = p->b1 = p->b2 = p->b3 = p->b4 = p->b5 = p->b6 = 0;
882 }
883 
get_pink_noise(pink_noise * p)884 float get_pink_noise(pink_noise *p)
885 {
886 	float b0 = p->b0, b1 = p->b1, b2 = p->b2, b3 = p->b3,
887 	   b4 = p->b4, b5 = p->b5, b6 = p->b6, pink, white;
888 
889 	white = genrand_real1() * 2.0 - 1.0;
890 	b0 = 0.99886 * b0 + white * 0.0555179;
891 	b1 = 0.99332 * b1 + white * 0.0750759;
892 	b2 = 0.96900 * b2 + white * 0.1538520;
893 	b3 = 0.86650 * b3 + white * 0.3104856;
894 	b4 = 0.55000 * b4 + white * 0.5329522;
895 	b5 = -0.7616 * b5 - white * 0.0168980;
896 	pink = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
897 	b6 = white * 0.115926;
898 	pink *= 0.22;
899 	pink = (pink > 1.0) ? 1.0 : (pink < -1.0) ? -1.0 : pink;
900 
901 	p->b0 = b0, p->b1 = b1, p->b2 = b2, p->b3 = b3,
902 		p->b4 = b4, p->b5 = b5, p->b6 = b6;
903 
904 	return pink;
905 }
906 
get_pink_noise_light(pink_noise * p)907 float get_pink_noise_light(pink_noise *p)
908 {
909 	float b0 = p->b0, b1 = p->b1, b2 = p->b2, pink, white;
910 
911 	white = genrand_real1() * 2.0 - 1.0;
912 	b0 = 0.99765 * b0 + white * 0.0990460;
913 	b1 = 0.96300 * b1 + white * 0.2965164;
914 	b2 = 0.57000 * b2 + white * 1.0526913;
915 	pink = b0 + b1 + b2 + white * 0.1848;
916 	pink *= 0.22;
917 	pink = (pink > 1.0) ? 1.0 : (pink < -1.0) ? -1.0 : pink;
918 
919 	p->b0 = b0, p->b1 = b1, p->b2 = b2;
920 
921 	return pink;
922 }
923 
924 /*                          */
925 /*  Standard Reverb Effect  */
926 /*                          */
927 #define REV_VAL0         5.3
928 #define REV_VAL1        10.5
929 #define REV_VAL2        44.12
930 #define REV_VAL3        21.0
931 
932 static int32  reverb_effect_buffer[AUDIO_BUFFER_SIZE * 2];
933 static int32  reverb_effect_bufsize = sizeof(reverb_effect_buffer);
934 
935 #if OPT_MODE != 0
936 #if !defined(_AMD64_) && ( defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || ( defined(__BORLANDC__) &&(__BORLANDC__ >= 1380) ) )
set_ch_reverb(int32 * buf,int32 count,int32 level)937 void set_ch_reverb(int32 *buf, int32 count, int32 level)
938 {
939 	int32 *dbuf = reverb_effect_buffer;
940 	if(!level) {return;}
941 	level = TIM_FSCALE(level / 127.0 * REV_INP_LEV, 24);
942 
943 	_asm {
944 		mov		ecx, [count]
945 		mov		esi, [buf]
946 		mov		ebx, [level]
947 		test	ecx, ecx
948 		jz		short L2
949 		mov		edi, [dbuf]
950 L1:		mov		eax, [esi]
951 		imul	ebx
952 		shr		eax, 24
953 		shl		edx, 8
954 		or		eax, edx	/* u */
955 		mov		edx, [edi]	/* v */
956 		add		esi, 4		/* u */
957 		add		edx, eax	/* v */
958 		mov		[edi], edx	/* u */
959 		add		edi, 4		/* v */
960 		dec		ecx			/* u */
961 		jnz		L1			/* v */
962 L2:
963 	}
964 }
965 #else
set_ch_reverb(int32 * buf,int32 count,int32 level)966 void set_ch_reverb(int32 *buf, int32 count, int32 level)
967 {
968     int32 i, *dbuf = reverb_effect_buffer;
969 	if(!level) {return;}
970     level = TIM_FSCALE(level / 127.0 * REV_INP_LEV, 24);
971 
972 	for(i = count - 1; i >= 0; i--) {dbuf[i] += imuldiv24(buf[i], level);}
973 }
974 #endif	/* _MSC_VER */
975 #else
set_ch_reverb(register int32 * sbuffer,int32 n,int32 level)976 void set_ch_reverb(register int32 *sbuffer, int32 n, int32 level)
977 {
978     register int32  i;
979 	if(!level) {return;}
980     FLOAT_T send_level = (FLOAT_T)level / 127.0 * REV_INP_LEV;
981 
982 	for(i = 0; i < n; i++)
983     {
984         reverb_effect_buffer[i] += sbuffer[i] * send_level;
985     }
986 }
987 #endif /* OPT_MODE != 0 */
988 
gs_revchar_to_roomsize(int character)989 static double gs_revchar_to_roomsize(int character)
990 {
991 	double rs;
992 	switch(character) {
993 	case 0: rs = 1.0;	break;	/* Room 1 */
994 	case 1: rs = 0.94;	break;	/* Room 2 */
995 	case 2: rs = 0.97;	break;	/* Room 3 */
996 	case 3: rs = 0.90;	break;	/* Hall 1 */
997 	case 4: rs = 0.85;	break;	/* Hall 2 */
998 	default: rs = 1.0;	break;	/* Plate, Delay, Panning Delay */
999 	}
1000 	return rs;
1001 }
1002 
gs_revchar_to_level(int character)1003 static double gs_revchar_to_level(int character)
1004 {
1005 	double level;
1006 	switch(character) {
1007 	case 0: level = 0.744025605;	break;	/* Room 1 */
1008 	case 1: level = 1.224309745;	break;	/* Room 2 */
1009 	case 2: level = 0.858592403;	break;	/* Room 3 */
1010 	case 3: level = 1.0471802;	break;	/* Hall 1 */
1011 	case 4: level = 1.0;	break;	/* Hall 2 */
1012 	case 5: level = 0.865335496;	break;	/* Plate */
1013 	default: level = 1.0;	break;	/* Delay, Panning Delay */
1014 	}
1015 	return level;
1016 }
1017 
gs_revchar_to_rt(int character)1018 static double gs_revchar_to_rt(int character)
1019 {
1020 	double rt;
1021 	switch(character) {
1022 	case 0: rt = 0.516850262;	break;	/* Room 1 */
1023 	case 1: rt = 1.004226004;	break;	/* Room 2 */
1024 	case 2: rt = 0.691046825;	break;	/* Room 3 */
1025 	case 3: rt = 0.893006004;	break;	/* Hall 1 */
1026 	case 4: rt = 1.0;	break;	/* Hall 2 */
1027 	case 5: rt = 0.538476488;	break;	/* Plate */
1028 	default: rt = 1.0;	break;	/* Delay, Panning Delay */
1029 	}
1030 	return rt;
1031 }
1032 
init_standard_reverb(InfoStandardReverb * info)1033 static void init_standard_reverb(InfoStandardReverb *info)
1034 {
1035 	double time;
1036 	info->ta = info->tb = 0;
1037 	info->HPFL = info->HPFR = info->LPFL = info->LPFR = info->EPFL = info->EPFR = 0;
1038 	info->spt0 = info->spt1 = info->spt2 = info->spt3 = 0;
1039 	time = reverb_time_table[reverb_status_gs.time] * gs_revchar_to_rt(reverb_status_gs.character)
1040 		/ reverb_time_table[64] * 0.8;
1041 	info->rpt0 = REV_VAL0 * play_mode->rate / 1000.0 * time;
1042 	info->rpt1 = REV_VAL1 * play_mode->rate / 1000.0 * time;
1043 	info->rpt2 = REV_VAL2 * play_mode->rate / 1000.0 * time;
1044 	info->rpt3 = REV_VAL3 * play_mode->rate / 1000.0 * time;
1045 	while (!isprime(info->rpt0)) {info->rpt0++;}
1046 	while (!isprime(info->rpt1)) {info->rpt1++;}
1047 	while (!isprime(info->rpt2)) {info->rpt2++;}
1048 	while (!isprime(info->rpt3)) {info->rpt3++;}
1049 	set_delay(&(info->buf0_L), info->rpt0 + 1);
1050 	set_delay(&(info->buf0_R), info->rpt0 + 1);
1051 	set_delay(&(info->buf1_L), info->rpt1 + 1);
1052 	set_delay(&(info->buf1_R), info->rpt1 + 1);
1053 	set_delay(&(info->buf2_L), info->rpt2 + 1);
1054 	set_delay(&(info->buf2_R), info->rpt2 + 1);
1055 	set_delay(&(info->buf3_L), info->rpt3 + 1);
1056 	set_delay(&(info->buf3_R), info->rpt3 + 1);
1057 	info->fbklev = 0.12;
1058 	info->nmixlev = 0.7;
1059 	info->cmixlev = 0.9;
1060 	info->monolev = 0.7;
1061 	info->hpflev = 0.5;
1062 	info->lpflev = 0.45;
1063 	info->lpfinp = 0.55;
1064 	info->epflev = 0.4;
1065 	info->epfinp = 0.48;
1066 	info->width = 0.125;
1067 	info->wet = 2.0 * (double)reverb_status_gs.level / 127.0 * gs_revchar_to_level(reverb_status_gs.character);
1068 	info->fbklevi = TIM_FSCALE(info->fbklev, 24);
1069 	info->nmixlevi = TIM_FSCALE(info->nmixlev, 24);
1070 	info->cmixlevi = TIM_FSCALE(info->cmixlev, 24);
1071 	info->monolevi = TIM_FSCALE(info->monolev, 24);
1072 	info->hpflevi = TIM_FSCALE(info->hpflev, 24);
1073 	info->lpflevi = TIM_FSCALE(info->lpflev, 24);
1074 	info->lpfinpi = TIM_FSCALE(info->lpfinp, 24);
1075 	info->epflevi = TIM_FSCALE(info->epflev, 24);
1076 	info->epfinpi = TIM_FSCALE(info->epfinp, 24);
1077 	info->widthi = TIM_FSCALE(info->width, 24);
1078 	info->weti = TIM_FSCALE(info->wet, 24);
1079 }
1080 
free_standard_reverb(InfoStandardReverb * info)1081 static void free_standard_reverb(InfoStandardReverb *info)
1082 {
1083 	free_delay(&(info->buf0_L));
1084 	free_delay(&(info->buf0_R));
1085 	free_delay(&(info->buf1_L));
1086 	free_delay(&(info->buf1_R));
1087 	free_delay(&(info->buf2_L));
1088 	free_delay(&(info->buf2_R));
1089 	free_delay(&(info->buf3_L));
1090 	free_delay(&(info->buf3_R));
1091 }
1092 
1093 /*! Standard Reverberator; this implementation is specialized for system effect. */
1094 #if OPT_MODE != 0 /* fixed-point implementation */
do_ch_standard_reverb(int32 * buf,int32 count,InfoStandardReverb * info)1095 static void do_ch_standard_reverb(int32 *buf, int32 count, InfoStandardReverb *info)
1096 {
1097 	int32 i, fixp, s, t;
1098 	int32 spt0 = info->spt0, spt1 = info->spt1, spt2 = info->spt2, spt3 = info->spt3,
1099 		ta = info->ta, tb = info->tb, HPFL = info->HPFL, HPFR = info->HPFR,
1100 		LPFL = info->LPFL, LPFR = info->LPFR, EPFL = info->EPFL, EPFR = info->EPFR;
1101 	int32 *buf0_L = info->buf0_L.buf, *buf0_R = info->buf0_R.buf,
1102 		*buf1_L = info->buf1_L.buf, *buf1_R = info->buf1_R.buf,
1103 		*buf2_L = info->buf2_L.buf, *buf2_R = info->buf2_R.buf,
1104 		*buf3_L = info->buf3_L.buf, *buf3_R = info->buf3_R.buf;
1105 	int32 fbklevi = info->fbklevi, cmixlevi = info->cmixlevi,
1106 		hpflevi = info->hpflevi, lpflevi = info->lpflevi, lpfinpi = info->lpfinpi,
1107 		epflevi = info->epflevi, epfinpi = info->epfinpi, widthi = info->widthi,
1108 		rpt0 = info->rpt0, rpt1 = info->rpt1, rpt2 = info->rpt2, rpt3 = info->rpt3, weti = info->weti;
1109 
1110 	if(count == MAGIC_INIT_EFFECT_INFO) {
1111 		init_standard_reverb(info);
1112 		return;
1113 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1114 		free_standard_reverb(info);
1115 		return;
1116 	}
1117 
1118 	for (i = 0; i < count; i++)
1119 	{
1120         /* L */
1121         fixp = reverb_effect_buffer[i];
1122 
1123         LPFL = imuldiv24(LPFL, lpflevi) + imuldiv24(buf2_L[spt2] + tb, lpfinpi) + imuldiv24(ta, widthi);
1124         ta = buf3_L[spt3];
1125         s  = buf3_L[spt3] = buf0_L[spt0];
1126         buf0_L[spt0] = -LPFL;
1127 
1128         t = imuldiv24(HPFL + fixp, hpflevi);
1129         HPFL = t - fixp;
1130 
1131         buf2_L[spt2] = imuldiv24(s - imuldiv24(fixp, fbklevi), cmixlevi);
1132         tb = buf1_L[spt1];
1133         buf1_L[spt1] = t;
1134 
1135         EPFL = imuldiv24(EPFL, epflevi) + imuldiv24(ta, epfinpi);
1136         buf[i] += imuldiv24(ta + EPFL, weti);
1137 
1138         /* R */
1139         fixp = reverb_effect_buffer[++i];
1140 
1141         LPFR = imuldiv24(LPFR, lpflevi) + imuldiv24(buf2_R[spt2] + tb, lpfinpi) + imuldiv24(ta, widthi);
1142         ta = buf3_R[spt3];
1143         s  = buf3_R[spt3] = buf0_R[spt0];
1144         buf0_R[spt0] = LPFR;
1145 
1146         t = imuldiv24(HPFR + fixp, hpflevi);
1147         HPFR = t - fixp;
1148 
1149         buf2_R[spt2] = imuldiv24(s - imuldiv24(fixp, fbklevi), cmixlevi);
1150         tb = buf1_R[spt1];
1151         buf1_R[spt1] = t;
1152 
1153         EPFR = imuldiv24(EPFR, epflevi) + imuldiv24(ta, epfinpi);
1154         buf[i] += imuldiv24(ta + EPFR, weti);
1155 
1156 		if (++spt0 == rpt0) {spt0 = 0;}
1157 		if (++spt1 == rpt1) {spt1 = 0;}
1158 		if (++spt2 == rpt2) {spt2 = 0;}
1159 		if (++spt3 == rpt3) {spt3 = 0;}
1160 	}
1161 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
1162 	info->spt0 = spt0, info->spt1 = spt1, info->spt2 = spt2, info->spt3 = spt3,
1163 	info->ta = ta, info->tb = tb, info->HPFL = HPFL, info->HPFR = HPFR,
1164 	info->LPFL = LPFL, info->LPFR = LPFR, info->EPFL = EPFL, info->EPFR = EPFR;
1165 }
1166 #else /* floating-point implementation */
do_ch_standard_reverb(int32 * buf,int32 count,InfoStandardReverb * info)1167 static void do_ch_standard_reverb(int32 *buf, int32 count, InfoStandardReverb *info)
1168 {
1169 	int32 i, fixp, s, t;
1170 	int32 spt0 = info->spt0, spt1 = info->spt1, spt2 = info->spt2, spt3 = info->spt3,
1171 		ta = info->ta, tb = info->tb, HPFL = info->HPFL, HPFR = info->HPFR,
1172 		LPFL = info->LPFL, LPFR = info->LPFR, EPFL = info->EPFL, EPFR = info->EPFR;
1173 	int32 *buf0_L = info->buf0_L.buf, *buf0_R = info->buf0_R.buf,
1174 		*buf1_L = info->buf1_L.buf, *buf1_R = info->buf1_R.buf,
1175 		*buf2_L = info->buf2_L.buf, *buf2_R = info->buf2_R.buf,
1176 		*buf3_L = info->buf3_L.buf, *buf3_R = info->buf3_R.buf;
1177 	FLOAT_T fbklev = info->fbklev, cmixlev = info->cmixlev,
1178 		hpflev = info->hpflev, lpflev = info->lpflev, lpfinp = info->lpfinp,
1179 		epflev = info->epflev, epfinp = info->epfinp, width = info->width,
1180 		rpt0 = info->rpt0, rpt1 = info->rpt1, rpt2 = info->rpt2, rpt3 = info->rpt3, wet = info->wet;
1181 
1182 	if(count == MAGIC_INIT_EFFECT_INFO) {
1183 		init_standard_reverb(info);
1184 		return;
1185 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1186 		free_standard_reverb(info);
1187 		return;
1188 	}
1189 
1190 	for (i = 0; i < count; i++)
1191 	{
1192         /* L */
1193         fixp = reverb_effect_buffer[i];
1194 
1195         LPFL = LPFL * lpflev + (buf2_L[spt2] + tb) * lpfinp + ta * width;
1196         ta = buf3_L[spt3];
1197         s  = buf3_L[spt3] = buf0_L[spt0];
1198         buf0_L[spt0] = -LPFL;
1199 
1200         t = (HPFL + fixp) * hpflev;
1201         HPFL = t - fixp;
1202 
1203         buf2_L[spt2] = (s - fixp * fbklev) * cmixlev;
1204         tb = buf1_L[spt1];
1205         buf1_L[spt1] = t;
1206 
1207         EPFL = EPFL * epflev + ta * epfinp;
1208         buf[i] += (ta + EPFL) * wet;
1209 
1210         /* R */
1211         fixp = reverb_effect_buffer[++i];
1212 
1213         LPFR = LPFR * lpflev + (buf2_R[spt2] + tb) * lpfinp + ta * width;
1214         ta = buf3_R[spt3];
1215         s  = buf3_R[spt3] = buf0_R[spt0];
1216         buf0_R[spt0] = LPFR;
1217 
1218         t = (HPFR + fixp) * hpflev;
1219         HPFR = t - fixp;
1220 
1221         buf2_R[spt2] = (s - fixp * fbklev) * cmixlev;
1222         tb = buf1_R[spt1];
1223         buf1_R[spt1] = t;
1224 
1225         EPFR = EPFR * epflev + ta * epfinp;
1226         buf[i] += (ta + EPFR) * wet;
1227 
1228 		if (++spt0 == rpt0) {spt0 = 0;}
1229 		if (++spt1 == rpt1) {spt1 = 0;}
1230 		if (++spt2 == rpt2) {spt2 = 0;}
1231 		if (++spt3 == rpt3) {spt3 = 0;}
1232 	}
1233 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
1234 	info->spt0 = spt0, info->spt1 = spt1, info->spt2 = spt2, info->spt3 = spt3,
1235 	info->ta = ta, info->tb = tb, info->HPFL = HPFL, info->HPFR = HPFR,
1236 	info->LPFL = LPFL, info->LPFR = LPFR, info->EPFL = EPFL, info->EPFR = EPFR;
1237 }
1238 #endif /* OPT_MODE != 0 */
1239 
1240 /*! Standard Monoral Reverberator; this implementation is specialized for system effect. */
do_ch_standard_reverb_mono(int32 * buf,int32 count,InfoStandardReverb * info)1241 static void do_ch_standard_reverb_mono(int32 *buf, int32 count, InfoStandardReverb *info)
1242 {
1243 	int32 i, fixp, s, t;
1244 	int32 spt0 = info->spt0, spt1 = info->spt1, spt2 = info->spt2, spt3 = info->spt3,
1245 		ta = info->ta, tb = info->tb, HPFL = info->HPFL, HPFR = info->HPFR,
1246 		LPFL = info->LPFL, LPFR = info->LPFR, EPFL = info->EPFL, EPFR = info->EPFR;
1247 	int32 *buf0_L = info->buf0_L.buf, *buf0_R = info->buf0_R.buf,
1248 		*buf1_L = info->buf1_L.buf, *buf1_R = info->buf1_R.buf,
1249 		*buf2_L = info->buf2_L.buf, *buf2_R = info->buf2_R.buf,
1250 		*buf3_L = info->buf3_L.buf, *buf3_R = info->buf3_R.buf;
1251 	FLOAT_T fbklev = info->fbklev, nmixlev = info->nmixlev, monolev = info->monolev,
1252 		hpflev = info->hpflev, lpflev = info->lpflev, lpfinp = info->lpfinp,
1253 		epflev = info->epflev, epfinp = info->epfinp, width = info->width,
1254 		rpt0 = info->rpt0, rpt1 = info->rpt1, rpt2 = info->rpt2, rpt3 = info->rpt3, wet = info->wet;
1255 
1256 	if(count == MAGIC_INIT_EFFECT_INFO) {
1257 		init_standard_reverb(info);
1258 		return;
1259 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1260 		free_standard_reverb(info);
1261 		return;
1262 	}
1263 
1264 	for (i = 0; i < count; i++)
1265 	{
1266         /* L */
1267         fixp = buf[i] * monolev;
1268 
1269         LPFL = LPFL * lpflev + (buf2_L[spt2] + tb) * lpfinp + ta * width;
1270         ta = buf3_L[spt3];
1271         s  = buf3_L[spt3] = buf0_L[spt0];
1272         buf0_L[spt0] = -LPFL;
1273 
1274         t = (HPFL + fixp) * hpflev;
1275         HPFL = t - fixp;
1276 
1277         buf2_L[spt2] = (s - fixp * fbklev) * nmixlev;
1278         tb = buf1_L[spt1];
1279         buf1_L[spt1] = t;
1280 
1281         /* R */
1282         LPFR = LPFR * lpflev + (buf2_R[spt2] + tb) * lpfinp + ta * width;
1283         ta = buf3_R[spt3];
1284         s  = buf3_R[spt3] = buf0_R[spt0];
1285         buf0_R[spt0] = LPFR;
1286 
1287         t = (HPFR + fixp) * hpflev;
1288         HPFR = t - fixp;
1289 
1290         buf2_R[spt2] = (s - fixp * fbklev) * nmixlev;
1291         tb = buf1_R[spt1];
1292         buf1_R[spt1] = t;
1293 
1294         EPFR = EPFR * epflev + ta * epfinp;
1295         buf[i] = (ta + EPFR) * wet + fixp;
1296 
1297 		if (++spt0 == rpt0) {spt0 = 0;}
1298 		if (++spt1 == rpt1) {spt1 = 0;}
1299 		if (++spt2 == rpt2) {spt2 = 0;}
1300 		if (++spt3 == rpt3) {spt3 = 0;}
1301 	}
1302 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
1303 	info->spt0 = spt0, info->spt1 = spt1, info->spt2 = spt2, info->spt3 = spt3,
1304 	info->ta = ta, info->tb = tb, info->HPFL = HPFL, info->HPFR = HPFR,
1305 	info->LPFL = LPFL, info->LPFR = LPFR, info->EPFL = EPFL, info->EPFR = EPFR;
1306 }
1307 
1308 /* dummy */
reverb_rc_event(int rc,int32 val)1309 void reverb_rc_event(int rc, int32 val)
1310 {
1311     switch(rc)
1312     {
1313       case RC_CHANGE_REV_EFFB:
1314         break;
1315       case RC_CHANGE_REV_TIME:
1316         break;
1317     }
1318 }
1319 
1320 /*             */
1321 /*  Freeverb   */
1322 /*             */
set_freeverb_allpass(allpass * allpass,int32 size)1323 static void set_freeverb_allpass(allpass *allpass, int32 size)
1324 {
1325 	if(allpass->buf != NULL) {
1326 		free(allpass->buf);
1327 		allpass->buf = NULL;
1328 	}
1329 	allpass->buf = (int32 *)safe_malloc(sizeof(int32) * size);
1330 	if(allpass->buf == NULL) {return;}
1331 	allpass->index = 0;
1332 	allpass->size = size;
1333 }
1334 
init_freeverb_allpass(allpass * allpass)1335 static void init_freeverb_allpass(allpass *allpass)
1336 {
1337 	memset(allpass->buf, 0, sizeof(int32) * allpass->size);
1338 }
1339 
set_freeverb_comb(comb * comb,int32 size)1340 static void set_freeverb_comb(comb *comb, int32 size)
1341 {
1342 	if(comb->buf != NULL) {
1343 		free(comb->buf);
1344 		comb->buf = NULL;
1345 	}
1346 	comb->buf = (int32 *)safe_malloc(sizeof(int32) * size);
1347 	if(comb->buf == NULL) {return;}
1348 	comb->index = 0;
1349 	comb->size = size;
1350 	comb->filterstore = 0;
1351 }
1352 
init_freeverb_comb(comb * comb)1353 static void init_freeverb_comb(comb *comb)
1354 {
1355 	memset(comb->buf, 0, sizeof(int32) * comb->size);
1356 }
1357 
1358 FLOAT_T freeverb_scaleroom = 0.28;
1359 FLOAT_T freeverb_offsetroom = 0.7;
1360 
1361 #define scalewet 0.06
1362 #define scaledamp 0.4
1363 #define scaleroom freeverb_scaleroom
1364 #define offsetroom freeverb_offsetroom
1365 #define initialroom 0.5
1366 #define initialdamp 0.5
1367 #define initialwet 1 / scalewet
1368 #define initialdry 0
1369 #define initialwidth 0.5
1370 #define initialallpassfbk 0.65
1371 #define stereospread 23
1372 static int combtunings[numcombs] = {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617};
1373 static int allpasstunings[numallpasses] = {225, 341, 441, 556};
1374 #define fixedgain 0.025
1375 #define combfbk 3.0
1376 
realloc_freeverb_buf(InfoFreeverb * rev)1377 static void realloc_freeverb_buf(InfoFreeverb *rev)
1378 {
1379 	int i;
1380 	int32 tmpL, tmpR;
1381 	double time, samplerate = play_mode->rate;
1382 
1383 	time = reverb_time_table[reverb_status_gs.time] * gs_revchar_to_rt(reverb_status_gs.character) * combfbk
1384 		/ (60 * combtunings[numcombs - 1] / (-20 * log10(rev->roomsize1) * 44100.0));
1385 
1386 	for(i = 0; i < numcombs; i++)
1387 	{
1388 		tmpL = combtunings[i] * samplerate * time / 44100.0;
1389 		tmpR = (combtunings[i] + stereospread) * samplerate * time / 44100.0;
1390 		if(tmpL < 10) tmpL = 10;
1391 		if(tmpR < 10) tmpR = 10;
1392 		while(!isprime(tmpL)) tmpL++;
1393 		while(!isprime(tmpR)) tmpR++;
1394 		rev->combL[i].size = tmpL;
1395 		rev->combR[i].size = tmpR;
1396 		set_freeverb_comb(&rev->combL[i], rev->combL[i].size);
1397 		set_freeverb_comb(&rev->combR[i], rev->combR[i].size);
1398 	}
1399 
1400 	for(i = 0; i < numallpasses; i++)
1401 	{
1402 		tmpL = allpasstunings[i] * samplerate * time / 44100.0;
1403 		tmpR = (allpasstunings[i] + stereospread) * samplerate * time / 44100.0;
1404 		if(tmpL < 10) tmpL = 10;
1405 		if(tmpR < 10) tmpR = 10;
1406 		while(!isprime(tmpL)) tmpL++;
1407 		while(!isprime(tmpR)) tmpR++;
1408 		rev->allpassL[i].size = tmpL;
1409 		rev->allpassR[i].size = tmpR;
1410 		set_freeverb_allpass(&rev->allpassL[i], rev->allpassL[i].size);
1411 		set_freeverb_allpass(&rev->allpassR[i], rev->allpassR[i].size);
1412 	}
1413 }
1414 
update_freeverb(InfoFreeverb * rev)1415 static void update_freeverb(InfoFreeverb *rev)
1416 {
1417 	int i;
1418 	double allpassfbk = 0.55, rtbase, rt;
1419 
1420 	rev->wet = (double)reverb_status_gs.level / 127.0 * gs_revchar_to_level(reverb_status_gs.character) * fixedgain;
1421 	rev->roomsize = gs_revchar_to_roomsize(reverb_status_gs.character) * scaleroom + offsetroom;
1422 	rev->width = 0.5;
1423 
1424 	rev->wet1 = rev->width / 2.0 + 0.5;
1425 	rev->wet2 = (1.0 - rev->width) / 2.0;
1426 	rev->roomsize1 = rev->roomsize;
1427 	rev->damp1 = rev->damp;
1428 
1429 	realloc_freeverb_buf(rev);
1430 
1431 	rtbase = 1.0 / (44100.0 * reverb_time_table[reverb_status_gs.time] * gs_revchar_to_rt(reverb_status_gs.character));
1432 
1433 	for(i = 0; i < numcombs; i++)
1434 	{
1435 		rt = pow(10.0, -combfbk * (double)combtunings[i] * rtbase);
1436 		rev->combL[i].feedback = rt;
1437 		rev->combR[i].feedback = rt;
1438 		rev->combL[i].damp1 = rev->damp1;
1439 		rev->combR[i].damp1 = rev->damp1;
1440 		rev->combL[i].damp2 = 1 - rev->damp1;
1441 		rev->combR[i].damp2 = 1 - rev->damp1;
1442 		rev->combL[i].damp1i = TIM_FSCALE(rev->combL[i].damp1, 24);
1443 		rev->combR[i].damp1i = TIM_FSCALE(rev->combR[i].damp1, 24);
1444 		rev->combL[i].damp2i = TIM_FSCALE(rev->combL[i].damp2, 24);
1445 		rev->combR[i].damp2i = TIM_FSCALE(rev->combR[i].damp2, 24);
1446 		rev->combL[i].feedbacki = TIM_FSCALE(rev->combL[i].feedback, 24);
1447 		rev->combR[i].feedbacki = TIM_FSCALE(rev->combR[i].feedback, 24);
1448 	}
1449 
1450 	for(i = 0; i < numallpasses; i++)
1451 	{
1452 		rev->allpassL[i].feedback = allpassfbk;
1453 		rev->allpassR[i].feedback = allpassfbk;
1454 		rev->allpassL[i].feedbacki = TIM_FSCALE(rev->allpassL[i].feedback, 24);
1455 		rev->allpassR[i].feedbacki = TIM_FSCALE(rev->allpassR[i].feedback, 24);
1456 	}
1457 
1458 	rev->wet1i = TIM_FSCALE(rev->wet1, 24);
1459 	rev->wet2i = TIM_FSCALE(rev->wet2, 24);
1460 
1461 	set_delay(&(rev->pdelay), (int32)((FLOAT_T)reverb_status_gs.pre_delay_time * reverb_predelay_factor * play_mode->rate / 1000.0));
1462 }
1463 
init_freeverb(InfoFreeverb * rev)1464 static void init_freeverb(InfoFreeverb *rev)
1465 {
1466 	int i;
1467 	for(i = 0; i < numcombs; i++) {
1468 		init_freeverb_comb(&rev->combL[i]);
1469 		init_freeverb_comb(&rev->combR[i]);
1470 	}
1471 	for(i = 0; i < numallpasses; i++) {
1472 		init_freeverb_allpass(&rev->allpassL[i]);
1473 		init_freeverb_allpass(&rev->allpassR[i]);
1474 	}
1475 }
1476 
alloc_freeverb_buf(InfoFreeverb * rev)1477 static void alloc_freeverb_buf(InfoFreeverb *rev)
1478 {
1479 	int i;
1480 	if(rev->alloc_flag) {return;}
1481 	for (i = 0; i < numcombs; i++) {
1482 		set_freeverb_comb(&rev->combL[i], combtunings[i]);
1483 		set_freeverb_comb(&rev->combR[i], combtunings[i] + stereospread);
1484 	}
1485 	for (i = 0; i < numallpasses; i++) {
1486 		set_freeverb_allpass(&rev->allpassL[i], allpasstunings[i]);
1487 		set_freeverb_allpass(&rev->allpassR[i], allpasstunings[i] + stereospread);
1488 		rev->allpassL[i].feedback = initialallpassfbk;
1489 		rev->allpassR[i].feedback = initialallpassfbk;
1490 	}
1491 
1492 	rev->wet = initialwet * scalewet;
1493 	rev->damp = initialdamp * scaledamp;
1494 	rev->width = initialwidth;
1495 	rev->roomsize = initialroom * scaleroom + offsetroom;
1496 
1497 	rev->alloc_flag = 1;
1498 }
1499 
free_freeverb_buf(InfoFreeverb * rev)1500 static void free_freeverb_buf(InfoFreeverb *rev)
1501 {
1502 	int i;
1503 
1504 	for(i = 0; i < numcombs; i++)
1505 	{
1506 		if(rev->combL[i].buf != NULL) {
1507 			free(rev->combL[i].buf);
1508 			rev->combL[i].buf = NULL;
1509 		}
1510 		if(rev->combR[i].buf != NULL) {
1511 			free(rev->combR[i].buf);
1512 			rev->combR[i].buf = NULL;
1513 		}
1514 	}
1515 	for(i = 0; i < numallpasses; i++)
1516 	{
1517 		if(rev->allpassL[i].buf != NULL) {
1518 			free(rev->allpassL[i].buf);
1519 			rev->allpassL[i].buf = NULL;
1520 		}
1521 		if(rev->allpassR[i].buf != NULL) {
1522 			free(rev->allpassR[i].buf);
1523 			rev->allpassR[i].buf = NULL;
1524 		}
1525 	}
1526 	free_delay(&(rev->pdelay));
1527 }
1528 
do_freeverb_allpass(int32 * stream,int32 * buf,int32 size,int32 * index,int32 feedback)1529 static inline void do_freeverb_allpass(int32 *stream, int32 *buf, int32 size, int32 *index, int32 feedback)
1530 {
1531 	int32 bufout, output;
1532 	bufout = buf[*index];
1533 	output = -*stream + bufout;
1534 	buf[*index] = *stream + imuldiv24(bufout, feedback);
1535 	if (++*index >= size) {*index = 0;}
1536 	*stream = output;
1537 }
1538 
do_freeverb_comb(int32 input,int32 * stream,int32 * buf,int32 size,int32 * index,int32 damp1,int32 damp2,int32 * fs,int32 feedback)1539 static inline void do_freeverb_comb(int32 input, int32 *stream, int32 *buf, int32 size, int32 *index,
1540 									int32 damp1, int32 damp2, int32 *fs, int32 feedback)
1541 {
1542 	int32 output;
1543 	output = buf[*index];
1544 	*fs = imuldiv24(output, damp2) + imuldiv24(*fs, damp1);
1545 	buf[*index] = input + imuldiv24(*fs, feedback);
1546 	if (++*index >= size) {*index = 0;}
1547 	*stream += output;
1548 }
1549 
do_ch_freeverb(int32 * buf,int32 count,InfoFreeverb * rev)1550 static void do_ch_freeverb(int32 *buf, int32 count, InfoFreeverb *rev)
1551 {
1552 	int32 i, k = 0;
1553 	int32 outl, outr, input;
1554 	comb *combL = rev->combL, *combR = rev->combR;
1555 	allpass *allpassL = rev->allpassL, *allpassR = rev->allpassR;
1556 	simple_delay *pdelay = &(rev->pdelay);
1557 
1558 	if(count == MAGIC_INIT_EFFECT_INFO) {
1559 		alloc_freeverb_buf(rev);
1560 		update_freeverb(rev);
1561 		init_freeverb(rev);
1562 		return;
1563 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1564 		free_freeverb_buf(rev);
1565 		return;
1566 	}
1567 
1568 	for (k = 0; k < count; k++)
1569 	{
1570 		input = reverb_effect_buffer[k] + reverb_effect_buffer[k + 1];
1571 		outl = outr = reverb_effect_buffer[k] = reverb_effect_buffer[k + 1] = 0;
1572 
1573 		do_delay(&input, pdelay->buf, pdelay->size, &pdelay->index);
1574 
1575 		for (i = 0; i < numcombs; i++) {
1576 			do_freeverb_comb(input, &outl, combL[i].buf, combL[i].size, &combL[i].index,
1577 				combL[i].damp1i, combL[i].damp2i, &combL[i].filterstore, combL[i].feedbacki);
1578 			do_freeverb_comb(input, &outr, combR[i].buf, combR[i].size, &combR[i].index,
1579 				combR[i].damp1i, combR[i].damp2i, &combR[i].filterstore, combR[i].feedbacki);
1580 		}
1581 		for (i = 0; i < numallpasses; i++) {
1582 			do_freeverb_allpass(&outl, allpassL[i].buf, allpassL[i].size, &allpassL[i].index, allpassL[i].feedbacki);
1583 			do_freeverb_allpass(&outr, allpassR[i].buf, allpassR[i].size, &allpassR[i].index, allpassR[i].feedbacki);
1584 		}
1585 		buf[k] += imuldiv24(outl, rev->wet1i) + imuldiv24(outr, rev->wet2i);
1586 		buf[k + 1] += imuldiv24(outr, rev->wet1i) + imuldiv24(outl, rev->wet2i);
1587 		++k;
1588 	}
1589 }
1590 
1591 /*                                 */
1592 /*  Reverb: Delay & Panning Delay  */
1593 /*                                 */
1594 /*! initialize Reverb: Delay Effect; this implementation is specialized for system effect. */
init_ch_reverb_delay(InfoDelay3 * info)1595 static void init_ch_reverb_delay(InfoDelay3 *info)
1596 {
1597 	int32 x;
1598 	info->size[0] = (double)reverb_status_gs.time * 3.75 * play_mode->rate / 1000.0;
1599 	x = info->size[0] + 1;	/* allowance */
1600 	set_delay(&(info->delayL), x);
1601 	set_delay(&(info->delayR), x);
1602 	info->index[0] = x - info->size[0];
1603 	if (info->index[0] >= info->size[0]) {
1604 		info->index[0] = (info->size[0] == 0) ? 0 : info->size[0] - 1;
1605 	}
1606 	info->level[0] = (double)reverb_status_gs.level * 1.82 / 127.0;
1607 	info->feedback = sqrt((double)reverb_status_gs.delay_feedback / 127.0) * 0.98;
1608 	info->leveli[0] = TIM_FSCALE(info->level[0], 24);
1609 	info->feedbacki = TIM_FSCALE(info->feedback, 24);
1610 }
1611 
free_ch_reverb_delay(InfoDelay3 * info)1612 static void free_ch_reverb_delay(InfoDelay3 *info)
1613 {
1614 	free_delay(&(info->delayL));
1615 	free_delay(&(info->delayR));
1616 }
1617 
1618 /*! Reverb: Panning Delay Effect; this implementation is specialized for system effect. */
do_ch_reverb_panning_delay(int32 * buf,int32 count,InfoDelay3 * info)1619 static void do_ch_reverb_panning_delay(int32 *buf, int32 count, InfoDelay3 *info)
1620 {
1621 	int32 i, l, r;
1622 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
1623 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
1624 	int32 buf_index = delayL->index, buf_size = delayL->size;
1625 	int32 index0 = info->index[0], level0i = info->leveli[0],
1626 		feedbacki = info->feedbacki;
1627 
1628 	if(count == MAGIC_INIT_EFFECT_INFO) {
1629 		init_ch_reverb_delay(info);
1630 		return;
1631 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1632 		free_ch_reverb_delay(info);
1633 		return;
1634 	}
1635 
1636 	for (i = 0; i < count; i++)
1637 	{
1638 		bufL[buf_index] = reverb_effect_buffer[i] + imuldiv24(bufR[index0], feedbacki);
1639 		l = imuldiv24(bufL[index0], level0i);
1640 		bufR[buf_index] = reverb_effect_buffer[i + 1] + imuldiv24(bufL[index0], feedbacki);
1641 		r = imuldiv24(bufR[index0], level0i);
1642 
1643 		buf[i] += r;
1644 		buf[++i] += l;
1645 
1646 		if (++index0 == buf_size) {index0 = 0;}
1647 		if (++buf_index == buf_size) {buf_index = 0;}
1648 	}
1649 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
1650 	info->index[0] = index0;
1651 	delayL->index = delayR->index = buf_index;
1652 }
1653 
1654 /*! Reverb: Normal Delay Effect; this implementation is specialized for system effect. */
do_ch_reverb_normal_delay(int32 * buf,int32 count,InfoDelay3 * info)1655 static void do_ch_reverb_normal_delay(int32 *buf, int32 count, InfoDelay3 *info)
1656 {
1657 	int32 i;
1658 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
1659 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
1660 	int32 buf_index = delayL->index, buf_size = delayL->size;
1661 	int32 index0 = info->index[0], level0i = info->leveli[0],
1662 		feedbacki = info->feedbacki;
1663 
1664 	if(count == MAGIC_INIT_EFFECT_INFO) {
1665 		init_ch_reverb_delay(info);
1666 		return;
1667 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1668 		free_ch_reverb_delay(info);
1669 		return;
1670 	}
1671 
1672 	for (i = 0; i < count; i++)
1673 	{
1674 		bufL[buf_index] = reverb_effect_buffer[i] + imuldiv24(bufL[index0], feedbacki);
1675 		buf[i] += imuldiv24(bufL[index0], level0i);
1676 
1677 		bufR[buf_index] = reverb_effect_buffer[++i] + imuldiv24(bufR[index0], feedbacki);
1678 		buf[i] += imuldiv24(bufR[index0], level0i);
1679 
1680 		if (++index0 == buf_size) {index0 = 0;}
1681 		if (++buf_index == buf_size) {buf_index = 0;}
1682 	}
1683 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
1684 	info->index[0] = index0;
1685 	delayL->index = delayR->index = buf_index;
1686 }
1687 
1688 /*                      */
1689 /*  Plate Reverberator  */
1690 /*                      */
1691 #define PLATE_SAMPLERATE 29761.0
1692 #define PLATE_DECAY 0.50
1693 #define PLATE_DECAY_DIFFUSION1 0.70
1694 #define PLATE_DECAY_DIFFUSION2 0.50
1695 #define PLATE_INPUT_DIFFUSION1 0.750
1696 #define PLATE_INPUT_DIFFUSION2 0.625
1697 #define PLATE_BANDWIDTH 0.9955
1698 #define PLATE_DAMPING 0.0005
1699 #define PLATE_WET 0.25
1700 
1701 /*! calculate delay sample in current sample-rate */
get_plate_delay(double delay,double t)1702 static inline int32 get_plate_delay(double delay, double t)
1703 {
1704 	return (int32)(delay * play_mode->rate * t / PLATE_SAMPLERATE);
1705 }
1706 
1707 /*! Plate Reverberator; this implementation is specialized for system effect. */
do_ch_plate_reverb(int32 * buf,int32 count,InfoPlateReverb * info)1708 static void do_ch_plate_reverb(int32 *buf, int32 count, InfoPlateReverb *info)
1709 {
1710 	int32 i;
1711 	int32 x, xd, val, outl, outr, temp1, temp2, temp3;
1712 	simple_delay *pd = &(info->pd), *od1l = &(info->od1l), *od2l = &(info->od2l),
1713 		*od3l = &(info->od3l), *od4l = &(info->od4l), *od5l = &(info->od5l),
1714 		*od6l = &(info->od6l), *od1r = &(info->od1r), *od2r = &(info->od2r),
1715 		*od3r = &(info->od3r), *od4r = &(info->od4r), *od5r = &(info->od5r),
1716 		*od7r = &(info->od7r), *od7l = &(info->od7l), *od6r = &(info->od6r),
1717 		*td1 = &(info->td1), *td2 = &(info->td2), *td1d = &(info->td1d), *td2d = &(info->td2d);
1718 	allpass *ap1 = &(info->ap1), *ap2 = &(info->ap2), *ap3 = &(info->ap3),
1719 		*ap4 = &(info->ap4), *ap6 = &(info->ap6), *ap6d = &(info->ap6d);
1720 	mod_allpass *ap5 = &(info->ap5), *ap5d = &(info->ap5d);
1721 	lfo *lfo1 = &(info->lfo1), *lfo1d = &(info->lfo1d);
1722 	filter_lowpass1 *lpf1 = &(info->lpf1), *lpf2 = &(info->lpf2);
1723 	int32 t1 = info->t1, t1d = info->t1d;
1724 	int32 decayi = info->decayi, ddif1i = info->ddif1i, ddif2i = info->ddif2i,
1725 		idif1i = info->idif1i, idif2i = info->idif2i;
1726 	double t;
1727 
1728 	if(count == MAGIC_INIT_EFFECT_INFO) {
1729 		init_lfo(lfo1, 1.30, LFO_SINE, 0);
1730 		init_lfo(lfo1d, 1.30, LFO_SINE, 0);
1731 		t = reverb_time_table[reverb_status_gs.time] / reverb_time_table[64] - 1.0;
1732 		t = 1.0 + t / 2;
1733 		set_delay(pd, reverb_status_gs.pre_delay_time * play_mode->rate / 1000);
1734 		set_delay(td1, get_plate_delay(4453, t)),
1735 		set_delay(td1d, get_plate_delay(4217, t));
1736 		set_delay(td2, get_plate_delay(3720, t));
1737 		set_delay(td2d, get_plate_delay(3163, t));
1738 		set_delay(od1l, get_plate_delay(266, t));
1739 		set_delay(od2l, get_plate_delay(2974, t));
1740 		set_delay(od3l, get_plate_delay(1913, t));
1741 		set_delay(od4l, get_plate_delay(1996, t));
1742 		set_delay(od5l, get_plate_delay(1990, t));
1743 		set_delay(od6l, get_plate_delay(187, t));
1744 		set_delay(od7l, get_plate_delay(1066, t));
1745 		set_delay(od1r, get_plate_delay(353, t));
1746 		set_delay(od2r, get_plate_delay(3627, t));
1747 		set_delay(od3r, get_plate_delay(1228, t));
1748 		set_delay(od4r, get_plate_delay(2673, t));
1749 		set_delay(od5r, get_plate_delay(2111, t));
1750 		set_delay(od6r, get_plate_delay(335, t));
1751 		set_delay(od7r, get_plate_delay(121, t));
1752 		set_allpass(ap1, get_plate_delay(142, t), PLATE_INPUT_DIFFUSION1);
1753 		set_allpass(ap2, get_plate_delay(107, t), PLATE_INPUT_DIFFUSION1);
1754 		set_allpass(ap3, get_plate_delay(379, t), PLATE_INPUT_DIFFUSION2);
1755 		set_allpass(ap4, get_plate_delay(277, t), PLATE_INPUT_DIFFUSION2);
1756 		set_allpass(ap6, get_plate_delay(1800, t), PLATE_DECAY_DIFFUSION2);
1757 		set_allpass(ap6d, get_plate_delay(2656, t), PLATE_DECAY_DIFFUSION2);
1758 		set_mod_allpass(ap5, get_plate_delay(672, t), get_plate_delay(16, t), PLATE_DECAY_DIFFUSION1);
1759 		set_mod_allpass(ap5d, get_plate_delay(908, t), get_plate_delay(16, t), PLATE_DECAY_DIFFUSION1);
1760 		lpf1->a = PLATE_BANDWIDTH, lpf2->a = 1.0 - PLATE_DAMPING;
1761 		init_filter_lowpass1(lpf1);
1762 		init_filter_lowpass1(lpf2);
1763 		info->t1 = info->t1d = 0;
1764 		info->decay = PLATE_DECAY;
1765 		info->decayi = TIM_FSCALE(info->decay, 24);
1766 		info->ddif1 = PLATE_DECAY_DIFFUSION1;
1767 		info->ddif1i = TIM_FSCALE(info->ddif1, 24);
1768 		info->ddif2 = PLATE_DECAY_DIFFUSION2;
1769 		info->ddif2i = TIM_FSCALE(info->ddif2, 24);
1770 		info->idif1 = PLATE_INPUT_DIFFUSION1;
1771 		info->idif1i = TIM_FSCALE(info->idif1, 24);
1772 		info->idif2 = PLATE_INPUT_DIFFUSION2;
1773 		info->idif2i = TIM_FSCALE(info->idif2, 24);
1774 		info->wet = PLATE_WET * (double)reverb_status_gs.level / 127.0;
1775 		return;
1776 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
1777 		free_delay(pd);	free_delay(td1); free_delay(td1d); free_delay(td2);
1778 		free_delay(td2d); free_delay(od1l); free_delay(od2l); free_delay(od3l);
1779 		free_delay(od4l); free_delay(od5l); free_delay(od6l); free_delay(od7l);
1780 		free_delay(od1r); free_delay(od2r);	free_delay(od3r); free_delay(od4r);
1781 		free_delay(od5r); free_delay(od6r);	free_delay(od7r); free_allpass(ap1);
1782 		free_allpass(ap2); free_allpass(ap3); free_allpass(ap4); free_allpass(ap6);
1783 		free_allpass(ap6d);	free_mod_allpass(ap5); free_mod_allpass(ap5d);
1784 		return;
1785 	}
1786 
1787 	for (i = 0; i < count; i++)
1788 	{
1789 		outr = outl = 0;
1790 		x = (reverb_effect_buffer[i] + reverb_effect_buffer[i + 1]) >> 1;
1791 		reverb_effect_buffer[i] = reverb_effect_buffer[i + 1] = 0;
1792 
1793 		do_delay(&x, pd->buf, pd->size, &pd->index);
1794 		do_filter_lowpass1(&x, &lpf1->x1l, lpf1->ai, lpf1->iai);
1795 		do_allpass(&x, ap1->buf, ap1->size, &ap1->index, idif1i);
1796 		do_allpass(&x, ap2->buf, ap2->size, &ap2->index, idif1i);
1797 		do_allpass(&x, ap3->buf, ap3->size, &ap3->index, idif2i);
1798 		do_allpass(&x, ap4->buf, ap4->size, &ap4->index, idif2i);
1799 
1800 		/* tank structure */
1801 		xd = x;
1802 		x += imuldiv24(t1d, decayi);
1803 		val = do_lfo(lfo1);
1804 		do_mod_allpass(&x, ap5->buf, ap5->size, &ap5->rindex, &ap5->windex,
1805 			ap5->ndelay, ap5->depth, val, &ap5->hist, ddif1i);
1806 		temp1 = temp2 = temp3 = x;	/* n_out_1 */
1807 		do_delay(&temp1, od5l->buf, od5l->size, &od5l->index);
1808 		outl -= temp1;	/* left output 5 */
1809 		do_delay(&temp2, od1r->buf, od1r->size, &od1r->index);
1810 		outr += temp2;	/* right output 1 */
1811 		do_delay(&temp3, od2r->buf, od2r->size, &od2r->index);
1812 		outr += temp3;	/* right output 2 */
1813 		do_delay(&x, td1->buf, td1->size, &td1->index);
1814 		do_filter_lowpass1(&x, &lpf2->x1l, lpf2->ai, lpf2->iai);
1815 		temp1 = temp2 = x;	/* n_out_2 */
1816 		do_delay(&temp1, od6l->buf, od6l->size, &od6l->index);
1817 		outl -= temp1;	/* left output 6 */
1818 		do_delay(&temp2, od3r->buf, od3r->size, &od3r->index);
1819 		outr -= temp2;	/* right output 3 */
1820 		x = imuldiv24(x, decayi);
1821 		do_allpass(&x, ap6->buf, ap6->size, &ap6->index, ddif2i);
1822 		temp1 = temp2 = x;	/* n_out_3 */
1823 		do_delay(&temp1, od7l->buf, od7l->size, &od7l->index);
1824 		outl -= temp1;	/* left output 7 */
1825 		do_delay(&temp2, od4r->buf, od4r->size, &od4r->index);
1826 		outr += temp2;	/* right output 4 */
1827 		do_delay(&x, td2->buf, td2->size, &td2->index);
1828 		t1 = x;
1829 
1830 		xd += imuldiv24(t1, decayi);
1831 		val = do_lfo(lfo1d);
1832 		do_mod_allpass(&x, ap5d->buf, ap5d->size, &ap5d->rindex, &ap5d->windex,
1833 			ap5d->ndelay, ap5d->depth, val, &ap5d->hist, ddif1i);
1834 		temp1 = temp2 = temp3 = xd;	/* n_out_4 */
1835 		do_delay(&temp1, od1l->buf, od1l->size, &od1l->index);
1836 		outl += temp1;	/* left output 1 */
1837 		do_delay(&temp2, od2l->buf, od2l->size, &od2l->index);
1838 		outl += temp2;	/* left output 2 */
1839 		do_delay(&temp3, od6r->buf, od6r->size, &od6r->index);
1840 		outr -= temp3;	/* right output 6 */
1841 		do_delay(&xd, td1d->buf, td1d->size, &td1d->index);
1842 		do_filter_lowpass1(&xd, &lpf2->x1r, lpf2->ai, lpf2->iai);
1843 		temp1 = temp2 = xd;	/* n_out_5 */
1844 		do_delay(&temp1, od3l->buf, od3l->size, &od3l->index);
1845 		outl -= temp1;	/* left output 3 */
1846 		do_delay(&temp2, od6r->buf, od6r->size, &od6r->index);
1847 		outr -= temp2;	/* right output 6 */
1848 		xd = imuldiv24(xd, decayi);
1849 		do_allpass(&xd, ap6d->buf, ap6d->size, &ap6d->index, ddif2i);
1850 		temp1 = temp2 = xd;	/* n_out_6 */
1851 		do_delay(&temp1, od4l->buf, od4l->size, &od4l->index);
1852 		outl += temp1;	/* left output 4 */
1853 		do_delay(&temp2, od7r->buf, od7r->size, &od7r->index);
1854 		outr -= temp2;	/* right output 7 */
1855 		do_delay(&xd, td2d->buf, td2d->size, &td2d->index);
1856 		t1d = xd;
1857 
1858 		buf[i] += outl;
1859 		buf[i + 1] += outr;
1860 
1861 		++i;
1862 	}
1863 	info->t1 = t1, info->t1d = t1d;
1864 }
1865 
1866 /*! initialize Reverb Effect */
init_reverb(void)1867 void init_reverb(void)
1868 {
1869 	init_filter_lowpass1(&(reverb_status_gs.lpf));
1870 	/* Only initialize freeverb if stereo output */
1871 	/* Old non-freeverb must be initialized for mono reverb not to crash */
1872 	if (! (play_mode->encoding & PE_MONO)
1873 			&& (opt_reverb_control == 3 || opt_reverb_control == 4
1874 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x100)))) {
1875 		switch(reverb_status_gs.character) {	/* select reverb algorithm */
1876 		case 5:	/* Plate Reverb */
1877 			do_ch_plate_reverb(NULL, MAGIC_INIT_EFFECT_INFO, &(reverb_status_gs.info_plate_reverb));
1878 			REV_INP_LEV = reverb_status_gs.info_plate_reverb.wet;
1879 			break;
1880 		case 6:	/* Delay */
1881 			do_ch_reverb_normal_delay(NULL, MAGIC_INIT_EFFECT_INFO, &(reverb_status_gs.info_reverb_delay));
1882 			REV_INP_LEV = 1.0;
1883 			break;
1884 		case 7: /* Panning Delay */
1885 			do_ch_reverb_panning_delay(NULL, MAGIC_INIT_EFFECT_INFO, &(reverb_status_gs.info_reverb_delay));
1886 			REV_INP_LEV = 1.0;
1887 			break;
1888 		default: /* Freeverb */
1889 			do_ch_freeverb(NULL, MAGIC_INIT_EFFECT_INFO, &(reverb_status_gs.info_freeverb));
1890 			REV_INP_LEV = reverb_status_gs.info_freeverb.wet;
1891 			break;
1892 		}
1893 	} else {	/* Old Reverb */
1894 		do_ch_standard_reverb(NULL, MAGIC_INIT_EFFECT_INFO, &(reverb_status_gs.info_standard_reverb));
1895 		REV_INP_LEV = 1.0;
1896 	}
1897 	memset(reverb_effect_buffer, 0, reverb_effect_bufsize);
1898 	memset(direct_buffer, 0, direct_bufsize);
1899 }
1900 
do_ch_reverb(int32 * buf,int32 count)1901 void do_ch_reverb(int32 *buf, int32 count)
1902 {
1903 #ifdef SYS_EFFECT_PRE_LPF
1904 	if ((opt_reverb_control == 3 || opt_reverb_control == 4
1905 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x100))) && reverb_status_gs.pre_lpf)
1906 		do_filter_lowpass1_stereo(reverb_effect_buffer, count, &(reverb_status_gs.lpf));
1907 #endif /* SYS_EFFECT_PRE_LPF */
1908 	if (opt_reverb_control == 3 || opt_reverb_control == 4
1909 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x100))) {
1910 		switch(reverb_status_gs.character) {	/* select reverb algorithm */
1911 		case 5:	/* Plate Reverb */
1912 			do_ch_plate_reverb(buf, count, &(reverb_status_gs.info_plate_reverb));
1913 			REV_INP_LEV = reverb_status_gs.info_plate_reverb.wet;
1914 			break;
1915 		case 6:	/* Delay */
1916 			do_ch_reverb_normal_delay(buf, count, &(reverb_status_gs.info_reverb_delay));
1917 			REV_INP_LEV = 1.0;
1918 			break;
1919 		case 7: /* Panning Delay */
1920 			do_ch_reverb_panning_delay(buf, count, &(reverb_status_gs.info_reverb_delay));
1921 			REV_INP_LEV = 1.0;
1922 			break;
1923 		default: /* Freeverb */
1924 			do_ch_freeverb(buf, count, &(reverb_status_gs.info_freeverb));
1925 			REV_INP_LEV = reverb_status_gs.info_freeverb.wet;
1926 			break;
1927 		}
1928 	} else {	/* Old Reverb */
1929 		do_ch_standard_reverb(buf, count, &(reverb_status_gs.info_standard_reverb));
1930 	}
1931 }
1932 
do_mono_reverb(int32 * buf,int32 count)1933 void do_mono_reverb(int32 *buf, int32 count)
1934 {
1935 	do_ch_standard_reverb_mono(buf, count, &(reverb_status_gs.info_standard_reverb));
1936 }
1937 
1938 /*                   */
1939 /*   Delay Effect    */
1940 /*                   */
1941 static int32 delay_effect_buffer[AUDIO_BUFFER_SIZE * 2];
1942 static void do_ch_3tap_delay(int32 *, int32, InfoDelay3 *);
1943 static void do_ch_cross_delay(int32 *, int32, InfoDelay3 *);
1944 static void do_ch_normal_delay(int32 *, int32, InfoDelay3 *);
1945 
init_ch_delay(void)1946 void init_ch_delay(void)
1947 {
1948 	memset(delay_effect_buffer, 0, sizeof(delay_effect_buffer));
1949 	init_filter_lowpass1(&(delay_status_gs.lpf));
1950 	do_ch_3tap_delay(NULL, MAGIC_INIT_EFFECT_INFO, &(delay_status_gs.info_delay));
1951 }
1952 
do_ch_delay(int32 * buf,int32 count)1953 void do_ch_delay(int32 *buf, int32 count)
1954 {
1955 #ifdef SYS_EFFECT_PRE_LPF
1956 	if ((opt_reverb_control == 3 || opt_reverb_control == 4
1957 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x100))) && delay_status_gs.pre_lpf)
1958 		do_filter_lowpass1_stereo(delay_effect_buffer, count, &(delay_status_gs.lpf));
1959 #endif /* SYS_EFFECT_PRE_LPF */
1960 	switch (delay_status_gs.type) {
1961 	case 1:
1962 		do_ch_3tap_delay(buf, count, &(delay_status_gs.info_delay));
1963 		break;
1964 	case 2:
1965 		do_ch_cross_delay(buf, count, &(delay_status_gs.info_delay));
1966 		break;
1967 	default:
1968 		do_ch_normal_delay(buf, count, &(delay_status_gs.info_delay));
1969 		break;
1970 	}
1971 }
1972 
1973 #if OPT_MODE != 0
1974 #if !defined(_AMD64_) && ( defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 1380) ) )
set_ch_delay(int32 * buf,int32 count,int32 level)1975 void set_ch_delay(int32 *buf, int32 count, int32 level)
1976 {
1977 	int32 *dbuf = delay_effect_buffer;
1978 	if(!level) {return;}
1979 	level = level * 65536 / 127;
1980 
1981 	_asm {
1982 		mov		ecx, [count]
1983 		mov		esi, [buf]
1984 		mov		ebx, [level]
1985 		test	ecx, ecx
1986 		jz		short L2
1987 		mov		edi, [dbuf]
1988 L1:		mov		eax, [esi]
1989 		imul	ebx
1990 		shr		eax, 16
1991 		shl		edx, 16
1992 		or		eax, edx	/* u */
1993 		mov		edx, [edi]	/* v */
1994 		add		esi, 4		/* u */
1995 		add		edx, eax	/* v */
1996 		mov		[edi], edx	/* u */
1997 		add		edi, 4		/* v */
1998 		dec		ecx			/* u */
1999 		jnz		L1			/* v */
2000 L2:
2001 	}
2002 }
2003 #else
set_ch_delay(register int32 * sbuffer,int32 n,int32 level)2004 void set_ch_delay(register int32 *sbuffer, int32 n, int32 level)
2005 {
2006     register int32 i;
2007 	int32 *buf = delay_effect_buffer;
2008 	if(!level) {return;}
2009 	level = level * 65536 / 127;
2010 
2011 	for(i = n - 1; i >= 0; i--) {buf[i] += imuldiv16(sbuffer[i], level);}
2012 }
2013 #endif	/* _MSC_VER */
2014 #else
set_ch_delay(register int32 * sbuffer,int32 n,int32 level)2015 void set_ch_delay(register int32 *sbuffer, int32 n, int32 level)
2016 {
2017     register int32 i;
2018 	if(!level) {return;}
2019     FLOAT_T send_level = (FLOAT_T)level / 127.0;
2020 
2021     for(i = 0; i < n; i++)
2022     {
2023         delay_effect_buffer[i] += sbuffer[i] * send_level;
2024     }
2025 }
2026 #endif /* OPT_MODE != 0 */
2027 
2028 /*! initialize Delay Effect; this implementation is specialized for system effect. */
init_ch_3tap_delay(InfoDelay3 * info)2029 static void init_ch_3tap_delay(InfoDelay3 *info)
2030 {
2031 	int32 i, x;
2032 
2033 	for (i = 0; i < 3; i++) {
2034 		info->size[i] = delay_status_gs.sample[i];
2035 	}
2036 	x = info->size[0];	/* find maximum value */
2037 	for (i = 1; i < 3; i++) {
2038 		if (info->size[i] > x) {x = info->size[i];}
2039 	}
2040 	x += 1;	/* allowance */
2041 	set_delay(&(info->delayL), x);
2042 	set_delay(&(info->delayR), x);
2043 	for (i = 0; i < 3; i++) {
2044 		info->index[i] = (x - info->size[i]) % x;	/* set start-point */
2045 		info->level[i] = delay_status_gs.level_ratio[i] * MASTER_DELAY_LEVEL;
2046 		info->leveli[i] = TIM_FSCALE(info->level[i], 24);
2047 	}
2048 	info->feedback = delay_status_gs.feedback_ratio;
2049 	info->send_reverb = delay_status_gs.send_reverb_ratio * REV_INP_LEV;
2050 	info->feedbacki = TIM_FSCALE(info->feedback, 24);
2051 	info->send_reverbi = TIM_FSCALE(info->send_reverb, 24);
2052 }
2053 
free_ch_3tap_delay(InfoDelay3 * info)2054 static void free_ch_3tap_delay(InfoDelay3 *info)
2055 {
2056 	free_delay(&(info->delayL));
2057 	free_delay(&(info->delayR));
2058 }
2059 
2060 /*! 3-Tap Stereo Delay Effect; this implementation is specialized for system effect. */
do_ch_3tap_delay(int32 * buf,int32 count,InfoDelay3 * info)2061 static void do_ch_3tap_delay(int32 *buf, int32 count, InfoDelay3 *info)
2062 {
2063 	int32 i, x;
2064 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
2065 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
2066 	int32 buf_index = delayL->index, buf_size = delayL->size;
2067 	int32 index0 = info->index[0], index1 = info->index[1], index2 = info->index[2];
2068 	int32 level0i = info->leveli[0], level1i = info->leveli[1], level2i = info->leveli[2],
2069 		feedbacki = info->feedbacki, send_reverbi = info->send_reverbi;
2070 
2071 	if(count == MAGIC_INIT_EFFECT_INFO) {
2072 		init_ch_3tap_delay(info);
2073 		return;
2074 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2075 		free_ch_3tap_delay(info);
2076 		return;
2077 	}
2078 
2079 	for (i = 0; i < count; i++)
2080 	{
2081 		bufL[buf_index] = delay_effect_buffer[i] + imuldiv24(bufL[index0], feedbacki);
2082 		x = imuldiv24(bufL[index0], level0i) + imuldiv24(bufL[index1] + bufR[index1], level1i);
2083 		buf[i] += x;
2084 		reverb_effect_buffer[i] += imuldiv24(x, send_reverbi);
2085 
2086 		bufR[buf_index] = delay_effect_buffer[++i] + imuldiv24(bufR[index0], feedbacki);
2087 		x = imuldiv24(bufR[index0], level0i) + imuldiv24(bufL[index2] + bufR[index2], level2i);
2088 		buf[i] += x;
2089 		reverb_effect_buffer[i] += imuldiv24(x, send_reverbi);
2090 
2091 		if (++index0 == buf_size) {index0 = 0;}
2092 		if (++index1 == buf_size) {index1 = 0;}
2093 		if (++index2 == buf_size) {index2 = 0;}
2094 		if (++buf_index == buf_size) {buf_index = 0;}
2095 	}
2096 	memset(delay_effect_buffer, 0, sizeof(int32) * count);
2097 	info->index[0] = index0, info->index[1] = index1, info->index[2] = index2;
2098 	delayL->index = delayR->index = buf_index;
2099 }
2100 
2101 /*! Cross Delay Effect; this implementation is specialized for system effect. */
do_ch_cross_delay(int32 * buf,int32 count,InfoDelay3 * info)2102 static void do_ch_cross_delay(int32 *buf, int32 count, InfoDelay3 *info)
2103 {
2104 	int32 i, l, r;
2105 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
2106 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
2107 	int32 buf_index = delayL->index, buf_size = delayL->size;
2108 	int32 index0 = info->index[0], level0i = info->leveli[0],
2109 		feedbacki = info->feedbacki, send_reverbi = info->send_reverbi;
2110 
2111 	if(count == MAGIC_INIT_EFFECT_INFO) {
2112 		init_ch_3tap_delay(info);
2113 		return;
2114 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2115 		free_ch_3tap_delay(info);
2116 		return;
2117 	}
2118 
2119 	for (i = 0; i < count; i++)
2120 	{
2121 		bufL[buf_index] = delay_effect_buffer[i] + imuldiv24(bufR[index0], feedbacki);
2122 		l = imuldiv24(bufL[index0], level0i);
2123 		bufR[buf_index] = delay_effect_buffer[i + 1] + imuldiv24(bufL[index0], feedbacki);
2124 		r = imuldiv24(bufR[index0], level0i);
2125 
2126 		buf[i] += r;
2127 		reverb_effect_buffer[i] += imuldiv24(r, send_reverbi);
2128 		buf[++i] += l;
2129 		reverb_effect_buffer[i] += imuldiv24(l, send_reverbi);
2130 
2131 		if (++index0 == buf_size) {index0 = 0;}
2132 		if (++buf_index == buf_size) {buf_index = 0;}
2133 	}
2134 	memset(delay_effect_buffer, 0, sizeof(int32) * count);
2135 	info->index[0] = index0;
2136 	delayL->index = delayR->index = buf_index;
2137 }
2138 
2139 /*! Normal Delay Effect; this implementation is specialized for system effect. */
do_ch_normal_delay(int32 * buf,int32 count,InfoDelay3 * info)2140 static void do_ch_normal_delay(int32 *buf, int32 count, InfoDelay3 *info)
2141 {
2142 	int32 i, x;
2143 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
2144 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
2145 	int32 buf_index = delayL->index, buf_size = delayL->size;
2146 	int32 index0 = info->index[0], level0i = info->leveli[0],
2147 		feedbacki = info->feedbacki, send_reverbi = info->send_reverbi;
2148 
2149 	if(count == MAGIC_INIT_EFFECT_INFO) {
2150 		init_ch_3tap_delay(info);
2151 		return;
2152 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2153 		free_ch_3tap_delay(info);
2154 		return;
2155 	}
2156 
2157 	for (i = 0; i < count; i++)
2158 	{
2159 		bufL[buf_index] = delay_effect_buffer[i] + imuldiv24(bufL[index0], feedbacki);
2160 		x = imuldiv24(bufL[index0], level0i);
2161 		buf[i] += x;
2162 		reverb_effect_buffer[i] += imuldiv24(x, send_reverbi);
2163 
2164 		bufR[buf_index] = delay_effect_buffer[++i] + imuldiv24(bufR[index0], feedbacki);
2165 		x = imuldiv24(bufR[index0], level0i);
2166 		buf[i] += x;
2167 		reverb_effect_buffer[i] += imuldiv24(x, send_reverbi);
2168 
2169 		if (++index0 == buf_size) {index0 = 0;}
2170 		if (++buf_index == buf_size) {buf_index = 0;}
2171 	}
2172 	memset(delay_effect_buffer, 0, sizeof(int32) * count);
2173 	info->index[0] = index0;
2174 	delayL->index = delayR->index = buf_index;
2175 }
2176 
2177 /*                             */
2178 /*        Chorus Effect        */
2179 /*                             */
2180 static int32 chorus_effect_buffer[AUDIO_BUFFER_SIZE * 2];
2181 
2182 /*! Stereo Chorus; this implementation is specialized for system effect. */
do_ch_stereo_chorus(int32 * buf,int32 count,InfoStereoChorus * info)2183 static void do_ch_stereo_chorus(int32 *buf, int32 count, InfoStereoChorus *info)
2184 {
2185 	int32 i, output, f0, f1, v0, v1;
2186 	int32 *bufL = info->delayL.buf, *bufR = info->delayR.buf,
2187 		*lfobufL = info->lfoL.buf, *lfobufR = info->lfoR.buf,
2188 		icycle = info->lfoL.icycle, cycle = info->lfoL.cycle,
2189 		leveli = info->leveli, feedbacki = info->feedbacki,
2190 		send_reverbi = info->send_reverbi, send_delayi = info->send_delayi,
2191 		depth = info->depth, pdelay = info->pdelay, rpt0 = info->rpt0;
2192 	int32 wpt0 = info->wpt0, spt0 = info->spt0, spt1 = info->spt1,
2193 		hist0 = info->hist0, hist1 = info->hist1, lfocnt = info->lfoL.count;
2194 
2195 	if(count == MAGIC_INIT_EFFECT_INFO) {
2196 		init_lfo(&(info->lfoL), (double)chorus_status_gs.rate * 0.122, LFO_TRIANGULAR, 0);
2197 		init_lfo(&(info->lfoR), (double)chorus_status_gs.rate * 0.122, LFO_TRIANGULAR, 90);
2198 		info->pdelay = chorus_delay_time_table[chorus_status_gs.delay] * (double)play_mode->rate / 1000.0;
2199 		info->depth = (double)(chorus_status_gs.depth + 1) / 3.2 * (double)play_mode->rate / 1000.0;
2200 		info->pdelay -= info->depth / 2;	/* NOMINAL_DELAY to delay */
2201 		if (info->pdelay < 1) {info->pdelay = 1;}
2202 		info->rpt0 = info->pdelay + info->depth + 2;	/* allowance */
2203 		set_delay(&(info->delayL), info->rpt0);
2204 		set_delay(&(info->delayR), info->rpt0);
2205 		info->feedback = (double)chorus_status_gs.feedback * 0.763 / 100.0;
2206 		info->level = (double)chorus_status_gs.level / 127.0 * MASTER_CHORUS_LEVEL;
2207 		info->send_reverb = (double)chorus_status_gs.send_reverb * 0.787 / 100.0 * REV_INP_LEV;
2208 		info->send_delay = (double)chorus_status_gs.send_delay * 0.787 / 100.0;
2209 		info->feedbacki = TIM_FSCALE(info->feedback, 24);
2210 		info->leveli = TIM_FSCALE(info->level, 24);
2211 		info->send_reverbi = TIM_FSCALE(info->send_reverb, 24);
2212 		info->send_delayi = TIM_FSCALE(info->send_delay, 24);
2213 		info->wpt0 = info->spt0 = info->spt1 = info->hist0 = info->hist1 = 0;
2214 		return;
2215 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2216 		free_delay(&(info->delayL));
2217 		free_delay(&(info->delayR));
2218 		return;
2219 	}
2220 
2221 	/* LFO */
2222 	f0 = imuldiv24(lfobufL[imuldiv24(lfocnt, icycle)], depth);
2223 	spt0 = wpt0 - pdelay - (f0 >> 8);	/* integral part of delay */
2224 	f0 = 0xFF - (f0 & 0xFF);	/* (1 - frac) * 256 */
2225 	if(spt0 < 0) {spt0 += rpt0;}
2226 	f1 = imuldiv24(lfobufR[imuldiv24(lfocnt, icycle)], depth);
2227 	spt1 = wpt0 - pdelay - (f1 >> 8);	/* integral part of delay */
2228 	f1 = 0xFF - (f1 & 0xFF);	/* (1 - frac) * 256 */
2229 	if(spt1 < 0) {spt1 += rpt0;}
2230 
2231 	for(i = 0; i < count; i++) {
2232 		v0 = bufL[spt0];
2233 		v1 = bufR[spt1];
2234 
2235 		/* LFO */
2236 		if(++wpt0 == rpt0) {wpt0 = 0;}
2237 		f0 = imuldiv24(lfobufL[imuldiv24(lfocnt, icycle)], depth);
2238 		spt0 = wpt0 - pdelay - (f0 >> 8);	/* integral part of delay */
2239 		f0 = 0xFF - (f0 & 0xFF);	/* (1 - frac) * 256 */
2240 		if(spt0 < 0) {spt0 += rpt0;}
2241 		f1 = imuldiv24(lfobufR[imuldiv24(lfocnt, icycle)], depth);
2242 		spt1 = wpt0 - pdelay - (f1 >> 8);	/* integral part of delay */
2243 		f1 = 0xFF - (f1 & 0xFF);	/* (1 - frac) * 256 */
2244 		if(spt1 < 0) {spt1 += rpt0;}
2245 		if(++lfocnt == cycle) {lfocnt = 0;}
2246 
2247 		/* left */
2248 		/* delay with all-pass interpolation */
2249 		output = hist0 = v0 + imuldiv8(bufL[spt0] - hist0, f0);
2250 		bufL[wpt0] = chorus_effect_buffer[i] + imuldiv24(output, feedbacki);
2251 		output = imuldiv24(output, leveli);
2252 		buf[i] += output;
2253 		/* send to other system effects (it's peculiar to GS) */
2254 		reverb_effect_buffer[i] += imuldiv24(output, send_reverbi);
2255 		delay_effect_buffer[i] += imuldiv24(output, send_delayi);
2256 
2257 		/* right */
2258 		/* delay with all-pass interpolation */
2259 		output = hist1 = v1 + imuldiv8(bufR[spt1] - hist1, f1);
2260 		bufR[wpt0] = chorus_effect_buffer[++i] + imuldiv24(output, feedbacki);
2261 		output = imuldiv24(output, leveli);
2262 		buf[i] += output;
2263 		/* send to other system effects (it's peculiar to GS) */
2264 		reverb_effect_buffer[i] += imuldiv24(output, send_reverbi);
2265 		delay_effect_buffer[i] += imuldiv24(output, send_delayi);
2266 	}
2267 	memset(chorus_effect_buffer, 0, sizeof(int32) * count);
2268 	info->wpt0 = wpt0, info->spt0 = spt0, info->spt1 = spt1,
2269 		info->hist0 = hist0, info->hist1 = hist1;
2270 	info->lfoL.count = info->lfoR.count = lfocnt;
2271 }
2272 
init_ch_chorus(void)2273 void init_ch_chorus(void)
2274 {
2275 	/* clear delay-line of LPF */
2276 	init_filter_lowpass1(&(chorus_status_gs.lpf));
2277 	do_ch_stereo_chorus(NULL, MAGIC_INIT_EFFECT_INFO, &(chorus_status_gs.info_stereo_chorus));
2278 	memset(chorus_effect_buffer, 0, sizeof(chorus_effect_buffer));
2279 }
2280 
2281 #if OPT_MODE != 0	/* fixed-point implementation */
2282 #if !defined(_AMD64_) && ( defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || ( defined(__BORLANDC__) && (__BORLANDC__ >= 1380) ) )
set_ch_chorus(int32 * buf,int32 count,int32 level)2283 void set_ch_chorus(int32 *buf, int32 count, int32 level)
2284 {
2285 	int32 *dbuf = chorus_effect_buffer;
2286 	if(!level) {return;}
2287 	level = level * 65536 / 127;
2288 
2289 	_asm {
2290 		mov		ecx, [count]
2291 		mov		esi, [buf]
2292 		mov		ebx, [level]
2293 		test	ecx, ecx
2294 		jz		short L2
2295 		mov		edi, [dbuf]
2296 L1:		mov		eax, [esi]
2297 		imul	ebx
2298 		shr		eax, 16
2299 		shl		edx, 16
2300 		or		eax, edx	/* u */
2301 		mov		edx, [edi]	/* v */
2302 		add		esi, 4		/* u */
2303 		add		edx, eax	/* v */
2304 		mov		[edi], edx	/* u */
2305 		add		edi, 4		/* v */
2306 		dec		ecx			/* u */
2307 		jnz		L1			/* v */
2308 L2:
2309 	}
2310 }
2311 #else
set_ch_chorus(register int32 * sbuffer,int32 n,int32 level)2312 void set_ch_chorus(register int32 *sbuffer,int32 n, int32 level)
2313 {
2314     register int32 i;
2315 	int32 *buf = chorus_effect_buffer;
2316 	if(!level) {return;}
2317 	level = level * 65536 / 127;
2318 
2319 	for(i = n - 1; i >= 0; i--) {buf[i] += imuldiv16(sbuffer[i], level);}
2320 }
2321 #endif	/* _MSC_VER */
2322 #else	/* floating-point implementation */
set_ch_chorus(register int32 * sbuffer,int32 n,int32 level)2323 void set_ch_chorus(register int32 *sbuffer,int32 n, int32 level)
2324 {
2325     register int32 i;
2326     register int32 count = n;
2327 	if(!level) {return;}
2328     FLOAT_T send_level = (FLOAT_T)level / 127.0;
2329 
2330     for(i = 0; i < count; i++)
2331     {
2332 		chorus_effect_buffer[i] += sbuffer[i] * send_level;
2333     }
2334 }
2335 #endif /* OPT_MODE != 0 */
2336 
do_ch_chorus(int32 * buf,int32 count)2337 void do_ch_chorus(int32 *buf, int32 count)
2338 {
2339 #ifdef SYS_EFFECT_PRE_LPF
2340 	if ((opt_reverb_control == 3 || opt_reverb_control == 4
2341 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x100))) && chorus_status_gs.pre_lpf)
2342 		do_filter_lowpass1_stereo(chorus_effect_buffer, count, &(chorus_status_gs.lpf));
2343 #endif /* SYS_EFFECT_PRE_LPF */
2344 
2345 	do_ch_stereo_chorus(buf, count, &(chorus_status_gs.info_stereo_chorus));
2346 }
2347 
2348 /*                             */
2349 /*       EQ (Equalizer)        */
2350 /*                             */
2351 static int32 eq_buffer[AUDIO_BUFFER_SIZE * 2];
2352 
init_eq_gs()2353 void init_eq_gs()
2354 {
2355 	memset(eq_buffer, 0, sizeof(eq_buffer));
2356 	calc_filter_shelving_low(&(eq_status_gs.lsf));
2357 	calc_filter_shelving_high(&(eq_status_gs.hsf));
2358 }
2359 
do_ch_eq_gs(int32 * buf,int32 count)2360 void do_ch_eq_gs(int32* buf, int32 count)
2361 {
2362 	register int32 i;
2363 
2364 	do_shelving_filter_stereo(eq_buffer, count, &(eq_status_gs.lsf));
2365 	do_shelving_filter_stereo(eq_buffer, count, &(eq_status_gs.hsf));
2366 
2367 	for(i = 0; i < count; i++) {
2368 		buf[i] += eq_buffer[i];
2369 		eq_buffer[i] = 0;
2370 	}
2371 }
2372 
do_ch_eq_xg(int32 * buf,int32 count,struct part_eq_xg * p)2373 void do_ch_eq_xg(int32* buf, int32 count, struct part_eq_xg *p)
2374 {
2375 	if(p->bass - 0x40 != 0) {
2376 		do_shelving_filter_stereo(buf, count, &(p->basss));
2377 	}
2378 	if(p->treble - 0x40 != 0) {
2379 		do_shelving_filter_stereo(buf, count, &(p->trebles));
2380 	}
2381 }
2382 
do_multi_eq_xg(int32 * buf,int32 count)2383 void do_multi_eq_xg(int32* buf, int32 count)
2384 {
2385 	if(multi_eq_xg.valid1) {
2386 		if(multi_eq_xg.shape1) {	/* peaking */
2387 			do_peaking_filter_stereo(buf, count, &(multi_eq_xg.eq1p));
2388 		} else {	/* shelving */
2389 			do_shelving_filter_stereo(buf, count, &(multi_eq_xg.eq1s));
2390 		}
2391 	}
2392 	if(multi_eq_xg.valid2) {
2393 		do_peaking_filter_stereo(buf, count, &(multi_eq_xg.eq2p));
2394 	}
2395 	if(multi_eq_xg.valid3) {
2396 		do_peaking_filter_stereo(buf, count, &(multi_eq_xg.eq3p));
2397 	}
2398 	if(multi_eq_xg.valid4) {
2399 		do_peaking_filter_stereo(buf, count, &(multi_eq_xg.eq4p));
2400 	}
2401 	if(multi_eq_xg.valid5) {
2402 		if(multi_eq_xg.shape5) {	/* peaking */
2403 			do_peaking_filter_stereo(buf, count, &(multi_eq_xg.eq5p));
2404 		} else {	/* shelving */
2405 			do_shelving_filter_stereo(buf, count, &(multi_eq_xg.eq5s));
2406 		}
2407 	}
2408 }
2409 
2410 #if OPT_MODE != 0
2411 #if !defined(_AMD64_) && ( defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || ( defined(__BORLANDC__) && (__BORLANDC__ >= 1380) ) )
set_ch_eq_gs(int32 * buf,int32 count)2412 void set_ch_eq_gs(int32 *buf, int32 count)
2413 {
2414 	int32 *dbuf = eq_buffer;
2415 	_asm {
2416 		mov		ecx, [count]
2417 		mov		esi, [buf]
2418 		test	ecx, ecx
2419 		jz		short L2
2420 		mov		edi, [dbuf]
2421 L1:		mov		eax, [esi]
2422 		mov		ebx, [edi]
2423 		add		esi, 4
2424 		add		ebx, eax
2425 		mov		[edi], ebx
2426 		add		edi, 4
2427 		dec		ecx
2428 		jnz		L1
2429 L2:
2430 	}
2431 }
2432 #else
set_ch_eq_gs(register int32 * buf,int32 n)2433 void set_ch_eq_gs(register int32 *buf, int32 n)
2434 {
2435     register int32 i;
2436 
2437     for(i = n-1; i >= 0; i--)
2438     {
2439         eq_buffer[i] += buf[i];
2440     }
2441 }
2442 #endif	/* _MSC_VER */
2443 #else
set_ch_eq_gs(register int32 * sbuffer,int32 n)2444 void set_ch_eq_gs(register int32 *sbuffer, int32 n)
2445 {
2446     register int32  i;
2447 
2448 	for(i = 0; i < n; i++)
2449     {
2450         eq_buffer[i] += sbuffer[i];
2451     }
2452 }
2453 #endif /* OPT_MODE != 0 */
2454 
2455 
2456 /*                                  */
2457 /*  Insertion and Variation Effect  */
2458 /*                                  */
do_insertion_effect_gs(int32 * buf,int32 count)2459 void do_insertion_effect_gs(int32 *buf, int32 count)
2460 {
2461 	do_effect_list(buf, count, insertion_effect_gs.ef);
2462 }
2463 
do_insertion_effect_xg(int32 * buf,int32 count,struct effect_xg_t * st)2464 void do_insertion_effect_xg(int32 *buf, int32 count, struct effect_xg_t *st)
2465 {
2466 	do_effect_list(buf, count, st->ef);
2467 }
2468 
do_variation_effect1_xg(int32 * buf,int32 count)2469 void do_variation_effect1_xg(int32 *buf, int32 count)
2470 {
2471 	int32 i, x;
2472 	int32 send_reverbi = TIM_FSCALE((double)variation_effect_xg[0].send_reverb * (0.787 / 100.0 * REV_INP_LEV), 24),
2473 		send_chorusi = TIM_FSCALE((double)variation_effect_xg[0].send_chorus * (0.787 / 100.0), 24);
2474 	if (variation_effect_xg[0].connection == XG_CONN_SYSTEM) {
2475 		do_effect_list(delay_effect_buffer, count, variation_effect_xg[0].ef);
2476 		for (i = 0; i < count; i++) {
2477 			x = delay_effect_buffer[i];
2478 			buf[i] += x;
2479 			reverb_effect_buffer[i] += imuldiv24(x, send_reverbi);
2480 			chorus_effect_buffer[i] += imuldiv24(x, send_chorusi);
2481 		}
2482 	}
2483 	memset(delay_effect_buffer, 0, sizeof(int32) * count);
2484 }
2485 
do_ch_chorus_xg(int32 * buf,int32 count)2486 void do_ch_chorus_xg(int32 *buf, int32 count)
2487 {
2488 	int32 i;
2489 	int32 send_reverbi = TIM_FSCALE((double)chorus_status_xg.send_reverb * (0.787 / 100.0 * REV_INP_LEV), 24);
2490 
2491 	do_effect_list(chorus_effect_buffer, count, chorus_status_xg.ef);
2492 	for (i = 0; i < count; i++) {
2493 		buf[i] += chorus_effect_buffer[i];
2494 		reverb_effect_buffer[i] += imuldiv24(chorus_effect_buffer[i], send_reverbi);
2495 	}
2496 	memset(chorus_effect_buffer, 0, sizeof(int32) * count);
2497 }
2498 
do_ch_reverb_xg(int32 * buf,int32 count)2499 void do_ch_reverb_xg(int32 *buf, int32 count)
2500 {
2501 	int32 i;
2502 
2503 	do_effect_list(reverb_effect_buffer, count, reverb_status_xg.ef);
2504 	for (i = 0; i < count; i++) {
2505 		buf[i] += reverb_effect_buffer[i];
2506 	}
2507 	memset(reverb_effect_buffer, 0, sizeof(int32) * count);
2508 }
2509 
init_ch_effect_xg(void)2510 void init_ch_effect_xg(void)
2511 {
2512 	memset(reverb_effect_buffer, 0, sizeof(reverb_effect_buffer));
2513 	memset(chorus_effect_buffer, 0, sizeof(chorus_effect_buffer));
2514 	memset(delay_effect_buffer, 0, sizeof(delay_effect_buffer));
2515 }
2516 
alloc_effect(EffectList * ef)2517 void alloc_effect(EffectList *ef)
2518 {
2519 	int i;
2520 
2521 	ef->engine = NULL;
2522 	for(i = 0; effect_engine[i].type != -1; i++) {
2523 		if (effect_engine[i].type == ef->type) {
2524 			ef->engine = &(effect_engine[i]);
2525 			break;
2526 		}
2527 	}
2528 	if (ef->engine == NULL) {return;}
2529 
2530 	if (ef->info != NULL) {
2531 		free(ef->info);
2532 		ef->info = NULL;
2533 	}
2534 	ef->info = safe_malloc(ef->engine->info_size);
2535 	memset(ef->info, 0, ef->engine->info_size);
2536 
2537 /*	ctl->cmsg(CMSG_INFO, VERB_NOISY, "Effect Engine: %s", ef->engine->name); */
2538 }
2539 
2540 /*! allocate new effect item and add it into the tail of effect list.
2541     EffectList *efc: pointer to the top of effect list.
2542     int8 type: type of new effect item.
2543     void *info: pointer to infomation of new effect item. */
push_effect(EffectList * efc,int type)2544 EffectList *push_effect(EffectList *efc, int type)
2545 {
2546 	EffectList *eft, *efn;
2547 	if (type == EFFECT_NONE) {return NULL;}
2548 	efn = (EffectList *)safe_malloc(sizeof(EffectList));
2549 	memset(efn, 0, sizeof(EffectList));
2550 	efn->type = type;
2551 	efn->next_ef = NULL;
2552 	efn->info = NULL;
2553 	alloc_effect(efn);
2554 
2555 	if(efc == NULL) {
2556 		efc = efn;
2557 	} else {
2558 		eft = efc;
2559 		while(eft->next_ef != NULL) {
2560 			eft = eft->next_ef;
2561 		}
2562 		eft->next_ef = efn;
2563 	}
2564 	return efc;
2565 }
2566 
2567 /*! process all items of effect list. */
do_effect_list(int32 * buf,int32 count,EffectList * ef)2568 void do_effect_list(int32 *buf, int32 count, EffectList *ef)
2569 {
2570 	EffectList *efc = ef;
2571 	if(ef == NULL) {return;}
2572 	while(efc != NULL && efc->engine->do_effect != NULL)
2573 	{
2574 		(*efc->engine->do_effect)(buf, count, efc);
2575 		efc = efc->next_ef;
2576 	}
2577 }
2578 
2579 /*! free all items of effect list. */
free_effect_list(EffectList * ef)2580 void free_effect_list(EffectList *ef)
2581 {
2582 	EffectList *efc, *efn;
2583 	efc = ef;
2584 	if (efc == NULL) {return;}
2585 	do {
2586 		efn = efc->next_ef;
2587 		if(efc->info != NULL) {
2588 			(*efc->engine->do_effect)(NULL, MAGIC_FREE_EFFECT_INFO, efc);
2589 			free(efc->info);
2590 			efc->info = NULL;
2591 		}
2592 		efc->engine = NULL;
2593 		free(efc);
2594 		efc = NULL;
2595 	} while ((efc = efn) != NULL);
2596 }
2597 
2598 /*! 2-Band EQ */
do_eq2(int32 * buf,int32 count,EffectList * ef)2599 void do_eq2(int32 *buf, int32 count, EffectList *ef)
2600 {
2601 	InfoEQ2 *eq = (InfoEQ2 *)ef->info;
2602 	if(count == MAGIC_INIT_EFFECT_INFO) {
2603 		eq->lsf.q = 0;
2604 		eq->lsf.freq = eq->low_freq;
2605 		eq->lsf.gain = eq->low_gain;
2606 		calc_filter_shelving_low(&(eq->lsf));
2607 		eq->hsf.q = 0;
2608 		eq->hsf.freq = eq->high_freq;
2609 		eq->hsf.gain = eq->high_gain;
2610 		calc_filter_shelving_high(&(eq->hsf));
2611 		return;
2612 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2613 		return;
2614 	}
2615 	if(eq->low_gain != 0) {
2616 		do_shelving_filter_stereo(buf, count, &(eq->lsf));
2617 	}
2618 	if(eq->high_gain != 0) {
2619 		do_shelving_filter_stereo(buf, count, &(eq->hsf));
2620 	}
2621 }
2622 
2623 /*! panning (pan = [0, 127]) */
do_left_panning(int32 sample,int32 pan)2624 static inline int32 do_left_panning(int32 sample, int32 pan)
2625 {
2626 	return imuldiv8(sample, 256 - pan - pan);
2627 }
2628 
do_right_panning(int32 sample,int32 pan)2629 static inline int32 do_right_panning(int32 sample, int32 pan)
2630 {
2631 	return imuldiv8(sample, pan + pan);
2632 }
2633 
2634 #define OD_BITS 28
2635 #define OD_MAX_NEG (1.0 / (double)(1L << OD_BITS))
2636 #define OD_DRIVE_GS 4.0
2637 #define OD_LEVEL_GS 0.5
2638 #define STEREO_OD_BITS 27
2639 #define STEREO_OD_MAX_NEG (1.0 / (double)(1L << STEREO_OD_BITS))
2640 #define OVERDRIVE_DIST 4.0
2641 #define OVERDRIVE_RES 0.1
2642 #define OVERDRIVE_LEVEL 1.0
2643 #define OVERDRIVE_OFFSET 0
2644 #define DISTORTION_DIST 40.0
2645 #define DISTORTION_RES 0.2
2646 #define DISTORTION_LEVEL 0.2
2647 #define DISTORTION_OFFSET 0
2648 
calc_gs_drive(int val)2649 static inline double calc_gs_drive(int val)
2650 {
2651 	return (OD_DRIVE_GS * (double)val / 127.0 + 1.0);
2652 }
2653 
2654 /*! GS 0x0110: Overdrive 1 */
do_overdrive1(int32 * buf,int32 count,EffectList * ef)2655 void do_overdrive1(int32 *buf, int32 count, EffectList *ef)
2656 {
2657 	InfoOverdrive1 *info = (InfoOverdrive1 *)ef->info;
2658 	filter_moog *svf = &(info->svf);
2659 	filter_biquad *lpf1 = &(info->lpf1);
2660 	void (*do_amp_sim)(int32 *, int32) = info->amp_sim;
2661 	int32 i, input, high, leveli = info->leveli, di = info->di,
2662 		pan = info->pan, asdi = TIM_FSCALE(1.0, 24);
2663 
2664 	if(count == MAGIC_INIT_EFFECT_INFO) {
2665 		/* decompositor */
2666 		svf->freq = 500;
2667 		svf->res_dB = 0;
2668 		calc_filter_moog(svf);
2669 		init_filter_moog(svf);
2670 		/* amp simulator */
2671 		info->amp_sim = do_dummy_clipping;
2672 		if (info->amp_sw == 1) {
2673 			if (info->amp_type <= 3) {info->amp_sim = do_soft_clipping2;}
2674 		}
2675 		/* waveshaper */
2676 		info->di = TIM_FSCALE(calc_gs_drive(info->drive), 24);
2677 		info->leveli = TIM_FSCALE(info->level * OD_LEVEL_GS, 24);
2678 		/* anti-aliasing */
2679 		lpf1->freq = 8000.0;
2680 		lpf1->q = 1.0;
2681 		calc_filter_biquad_low(lpf1);
2682 		return;
2683 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2684 		return;
2685 	}
2686 	for(i = 0; i < count; i++) {
2687 		input = (buf[i] + buf[i + 1]) >> 1;
2688 		/* amp simulation */
2689 		do_amp_sim(&input, asdi);
2690 		/* decomposition */
2691 		do_filter_moog(&input, &high, svf->f, svf->p, svf->q,
2692 			&svf->b0, &svf->b1, &svf->b2, &svf->b3, &svf->b4);
2693 		/* waveshaping */
2694 		do_soft_clipping1(&high, di);
2695 		/* anti-aliasing */
2696 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1l, &lpf1->x2l, &lpf1->y1l, &lpf1->y2l);
2697 		/* mixing */
2698 		input = imuldiv24(high + input, leveli);
2699 		buf[i] = do_left_panning(input, pan);
2700 		buf[i + 1] = do_right_panning(input, pan);
2701 		++i;
2702 	}
2703 }
2704 
2705 /*! GS 0x0111: Distortion 1 */
do_distortion1(int32 * buf,int32 count,EffectList * ef)2706 void do_distortion1(int32 *buf, int32 count, EffectList *ef)
2707 {
2708 	InfoOverdrive1 *info = (InfoOverdrive1 *)ef->info;
2709 	filter_moog *svf = &(info->svf);
2710 	filter_biquad *lpf1 = &(info->lpf1);
2711 	void (*do_amp_sim)(int32 *, int32) = info->amp_sim;
2712 	int32 i, input, high, leveli = info->leveli, di = info->di,
2713 		pan = info->pan, asdi = TIM_FSCALE(1.0, 24);
2714 
2715 	if(count == MAGIC_INIT_EFFECT_INFO) {
2716 		/* decompositor */
2717 		svf->freq = 500;
2718 		svf->res_dB = 0;
2719 		calc_filter_moog(svf);
2720 		init_filter_moog(svf);
2721 		/* amp simulator */
2722 		info->amp_sim = do_dummy_clipping;
2723 		if (info->amp_sw == 1) {
2724 			if (info->amp_type <= 3) {info->amp_sim = do_soft_clipping2;}
2725 		}
2726 		/* waveshaper */
2727 		info->di = TIM_FSCALE(calc_gs_drive(info->drive), 24);
2728 		info->leveli = TIM_FSCALE(info->level * OD_LEVEL_GS, 24);
2729 		/* anti-aliasing */
2730 		lpf1->freq = 8000.0;
2731 		lpf1->q = 1.0;
2732 		calc_filter_biquad_low(lpf1);
2733 		return;
2734 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2735 		return;
2736 	}
2737 	for(i = 0; i < count; i++) {
2738 		input = (buf[i] + buf[i + 1]) >> 1;
2739 		/* amp simulation */
2740 		do_amp_sim(&input, asdi);
2741 		/* decomposition */
2742 		do_filter_moog(&input, &high, svf->f, svf->p, svf->q,
2743 			&svf->b0, &svf->b1, &svf->b2, &svf->b3, &svf->b4);
2744 		/* waveshaping */
2745 		do_hard_clipping(&high, di);
2746 		/* anti-aliasing */
2747 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1l, &lpf1->x2l, &lpf1->y1l, &lpf1->y2l);
2748 		/* mixing */
2749 		input = imuldiv24(high + input, leveli);
2750 		buf[i] = do_left_panning(input, pan);
2751 		buf[i + 1] = do_right_panning(input, pan);
2752 		++i;
2753 	}
2754 }
2755 
2756 /*! GS 0x1103: OD1 / OD2 */
do_dual_od(int32 * buf,int32 count,EffectList * ef)2757 void do_dual_od(int32 *buf, int32 count, EffectList *ef)
2758 {
2759 	InfoOD1OD2 *info = (InfoOD1OD2 *)ef->info;
2760 	filter_moog *svfl = &(info->svfl), *svfr = &(info->svfr);
2761 	filter_biquad *lpf1 = &(info->lpf1);
2762 	void (*do_amp_siml)(int32 *, int32) = info->amp_siml,
2763 		(*do_odl)(int32 *, int32) = info->odl,
2764 		(*do_odr)(int32 *, int32) = info->odr;
2765 	int32 i, inputl, inputr, high, levelli = info->levelli, levelri = info->levelri,
2766 		dli = info->dli, dri = info->dri, panl = info->panl, panr = info->panr, asdi = TIM_FSCALE(1.0, 24);
2767 
2768 	if(count == MAGIC_INIT_EFFECT_INFO) {
2769 		/* left */
2770 		/* decompositor */
2771 		svfl->freq = 500;
2772 		svfl->res_dB = 0;
2773 		calc_filter_moog(svfl);
2774 		init_filter_moog(svfl);
2775 		/* amp simulator */
2776 		info->amp_siml = do_dummy_clipping;
2777 		if (info->amp_swl == 1) {
2778 			if (info->amp_typel <= 3) {info->amp_siml = do_soft_clipping2;}
2779 		}
2780 		/* waveshaper */
2781 		if(info->typel == 0) {info->odl = do_soft_clipping1;}
2782 		else {info->odl = do_hard_clipping;}
2783 		info->dli = TIM_FSCALE(calc_gs_drive(info->drivel), 24);
2784 		info->levelli = TIM_FSCALE(info->levell * OD_LEVEL_GS, 24);
2785 		/* right */
2786 		/* decompositor */
2787 		svfr->freq = 500;
2788 		svfr->res_dB = 0;
2789 		calc_filter_moog(svfr);
2790 		init_filter_moog(svfr);
2791 		/* amp simulator */
2792 		info->amp_simr = do_dummy_clipping;
2793 		if (info->amp_swr == 1) {
2794 			if (info->amp_typer <= 3) {info->amp_simr = do_soft_clipping2;}
2795 		}
2796 		/* waveshaper */
2797 		if(info->typer == 0) {info->odr = do_soft_clipping1;}
2798 		else {info->odr = do_hard_clipping;}
2799 		info->dri = TIM_FSCALE(calc_gs_drive(info->driver), 24);
2800 		info->levelri = TIM_FSCALE(info->levelr * OD_LEVEL_GS, 24);
2801 		/* anti-aliasing */
2802 		lpf1->freq = 8000.0;
2803 		lpf1->q = 1.0;
2804 		calc_filter_biquad_low(lpf1);
2805 		return;
2806 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2807 		return;
2808 	}
2809 	for(i = 0; i < count; i++) {
2810 		/* left */
2811 		inputl = buf[i];
2812 		/* amp simulation */
2813 		do_amp_siml(&inputl, asdi);
2814 		/* decomposition */
2815 		do_filter_moog(&inputl, &high, svfl->f, svfl->p, svfl->q,
2816 			&svfl->b0, &svfl->b1, &svfl->b2, &svfl->b3, &svfl->b4);
2817 		/* waveshaping */
2818 		do_odl(&high, dli);
2819 		/* anti-aliasing */
2820 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1l, &lpf1->x2l, &lpf1->y1l, &lpf1->y2l);
2821 		inputl = imuldiv24(high + inputl, levelli);
2822 
2823 		/* right */
2824 		inputr = buf[++i];
2825 		/* amp simulation */
2826 		do_amp_siml(&inputr, asdi);
2827 		/* decomposition */
2828 		do_filter_moog(&inputr, &high, svfr->f, svfr->p, svfr->q,
2829 			&svfr->b0, &svfr->b1, &svfr->b2, &svfr->b3, &svfr->b4);
2830 		/* waveshaping */
2831 		do_odr(&high, dri);
2832 		/* anti-aliasing */
2833 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1r, &lpf1->x2r, &lpf1->y1r, &lpf1->y2r);
2834 		inputr = imuldiv24(high + inputr, levelri);
2835 
2836 		/* panning */
2837 		buf[i - 1] = do_left_panning(inputl, panl) + do_left_panning(inputr, panr);
2838 		buf[i] = do_right_panning(inputl, panl) + do_right_panning(inputr, panr);
2839 	}
2840 }
2841 
2842 #define HEXA_CHORUS_WET_LEVEL 0.2
2843 #define HEXA_CHORUS_DEPTH_DEV (1.0 / (20.0 + 1.0))
2844 #define HEXA_CHORUS_DELAY_DEV (1.0 / (20.0 * 3.0))
2845 
2846 /*! GS 0x0140: HEXA-CHORUS */
do_hexa_chorus(int32 * buf,int32 count,EffectList * ef)2847 void do_hexa_chorus(int32 *buf, int32 count, EffectList *ef)
2848 {
2849 	InfoHexaChorus *info = (InfoHexaChorus *)ef->info;
2850 	lfo *lfo = &(info->lfo0);
2851 	simple_delay *buf0 = &(info->buf0);
2852 	int32 *ebuf = buf0->buf, size = buf0->size, index = buf0->index;
2853 	int32 spt0 = info->spt0, spt1 = info->spt1, spt2 = info->spt2,
2854 		spt3 = info->spt3, spt4 = info->spt4, spt5 = info->spt5,
2855 		hist0 = info->hist0, hist1 = info->hist1, hist2 = info->hist2,
2856 		hist3 = info->hist3, hist4 = info->hist4, hist5 = info->hist5;
2857 	int32 dryi = info->dryi, weti = info->weti;
2858 	int32 pan0 = info->pan0, pan1 = info->pan1, pan2 = info->pan2,
2859 		pan3 = info->pan3, pan4 = info->pan4, pan5 = info->pan5;
2860 	int32 depth0 = info->depth0, depth1 = info->depth1, depth2 = info->depth2,
2861 		depth3 = info->depth3, depth4 = info->depth4, depth5 = info->depth5,
2862 		pdelay0 = info->pdelay0, pdelay1 = info->pdelay1, pdelay2 = info->pdelay2,
2863 		pdelay3 = info->pdelay3, pdelay4 = info->pdelay4, pdelay5 = info->pdelay5;
2864 	int32 i, lfo_val,
2865 		v0, v1, v2, v3, v4, v5, f0, f1, f2, f3, f4, f5;
2866 
2867 	if(count == MAGIC_INIT_EFFECT_INFO) {
2868 		set_delay(buf0, (int32)(9600.0 * play_mode->rate / 44100.0));
2869 		init_lfo(lfo, lfo->freq, LFO_TRIANGULAR, 0);
2870 		info->dryi = TIM_FSCALE(info->level * info->dry, 24);
2871 		info->weti = TIM_FSCALE(info->level * info->wet * HEXA_CHORUS_WET_LEVEL, 24);
2872 		v0 = info->depth * ((double)info->depth_dev * HEXA_CHORUS_DEPTH_DEV);
2873 		info->depth0 = info->depth - v0;
2874 		info->depth1 = info->depth;
2875 		info->depth2 = info->depth + v0;
2876 		info->depth3 = info->depth + v0;
2877 		info->depth4 = info->depth;
2878 		info->depth5 = info->depth - v0;
2879 		v0 = info->pdelay * ((double)info->pdelay_dev * HEXA_CHORUS_DELAY_DEV);
2880 		info->pdelay0 = info->pdelay + v0;
2881 		info->pdelay1 = info->pdelay + v0 * 2;
2882 		info->pdelay2 = info->pdelay + v0 * 3;
2883 		info->pdelay3 = info->pdelay + v0 * 3;
2884 		info->pdelay4 = info->pdelay + v0 * 2;
2885 		info->pdelay5 = info->pdelay + v0;
2886 		/* in this part, validation check may be necessary. */
2887 		info->pan0 = 64 - info->pan_dev * 3;
2888 		info->pan1 = 64 - info->pan_dev * 2;
2889 		info->pan2 = 64 - info->pan_dev;
2890 		info->pan3 = 64 + info->pan_dev;
2891 		info->pan4 = 64 + info->pan_dev * 2;
2892 		info->pan5 = 64 + info->pan_dev * 3;
2893 		info->hist0 = info->hist1 = info->hist2
2894 			= info->hist3 = info->hist4 = info->hist5 = 0;
2895 		info->spt0 = info->spt1 = info->spt2
2896 			= info->spt3 = info->spt4 = info->spt5 = 0;
2897 		return;
2898 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
2899 		free_delay(buf0);
2900 		return;
2901 	}
2902 
2903 	/* LFO */
2904 	lfo_val = lfo->buf[imuldiv24(lfo->count, lfo->icycle)];
2905 	f0 = imuldiv24(lfo_val, depth0);
2906 	spt0 = index - pdelay0 - (f0 >> 8);	/* integral part of delay */
2907 	if(spt0 < 0) {spt0 += size;}
2908 	f1 = imuldiv24(lfo_val, depth1);
2909 	spt1 = index - pdelay1 - (f1 >> 8);	/* integral part of delay */
2910 	if(spt1 < 0) {spt1 += size;}
2911 	f2 = imuldiv24(lfo_val, depth2);
2912 	spt2 = index - pdelay2 - (f2 >> 8);	/* integral part of delay */
2913 	if(spt2 < 0) {spt2 += size;}
2914 	f3 = imuldiv24(lfo_val, depth3);
2915 	spt3 = index - pdelay3 - (f3 >> 8);	/* integral part of delay */
2916 	if(spt3 < 0) {spt3 += size;}
2917 	f4 = imuldiv24(lfo_val, depth4);
2918 	spt4 = index - pdelay4 - (f4 >> 8);	/* integral part of delay */
2919 	if(spt4 < 0) {spt4 += size;}
2920 	f5 = imuldiv24(lfo_val, depth5);
2921 	spt5 = index - pdelay5 - (f5 >> 8);	/* integral part of delay */
2922 	if(spt5 < 0) {spt5 += size;}
2923 
2924 	for(i = 0; i < count; i++) {
2925 		v0 = ebuf[spt0], v1 = ebuf[spt1], v2 = ebuf[spt2],
2926 		v3 = ebuf[spt3], v4 = ebuf[spt4], v5 = ebuf[spt5];
2927 
2928 		/* LFO */
2929 		if(++index == size) {index = 0;}
2930 		lfo_val = do_lfo(lfo);
2931 		f0 = imuldiv24(lfo_val, depth0);
2932 		spt0 = index - pdelay0 - (f0 >> 8);	/* integral part of delay */
2933 		f0 = 0xFF - (f0 & 0xFF);	/* (1 - frac) * 256 */
2934 		if(spt0 < 0) {spt0 += size;}
2935 		f1 = imuldiv24(lfo_val, depth1);
2936 		spt1 = index - pdelay1 - (f1 >> 8);	/* integral part of delay */
2937 		f1 = 0xFF - (f1 & 0xFF);	/* (1 - frac) * 256 */
2938 		if(spt1 < 0) {spt1 += size;}
2939 		f2 = imuldiv24(lfo_val, depth2);
2940 		spt2 = index - pdelay2 - (f2 >> 8);	/* integral part of delay */
2941 		f2 = 0xFF - (f2 & 0xFF);	/* (1 - frac) * 256 */
2942 		if(spt2 < 0) {spt2 += size;}
2943 		f3 = imuldiv24(lfo_val, depth3);
2944 		spt3 = index - pdelay3 - (f3 >> 8);	/* integral part of delay */
2945 		f3 = 0xFF - (f3 & 0xFF);	/* (1 - frac) * 256 */
2946 		if(spt3 < 0) {spt3 += size;}
2947 		f4 = imuldiv24(lfo_val, depth4);
2948 		spt4 = index - pdelay4 - (f4 >> 8);	/* integral part of delay */
2949 		f4 = 0xFF - (f4 & 0xFF);	/* (1 - frac) * 256 */
2950 		if(spt4 < 0) {spt4 += size;}
2951 		f5 = imuldiv24(lfo_val, depth5);
2952 		spt5 = index - pdelay5 - (f5 >> 8);	/* integral part of delay */
2953 		f5 = 0xFF - (f5 & 0xFF);	/* (1 - frac) * 256 */
2954 		if(spt5 < 0) {spt5 += size;}
2955 
2956 		/* chorus effect */
2957 		/* all-pass interpolation */
2958 		hist0 = v0 + imuldiv8(ebuf[spt0] - hist0, f0);
2959 		hist1 = v1 + imuldiv8(ebuf[spt1] - hist1, f1);
2960 		hist2 = v2 + imuldiv8(ebuf[spt2] - hist2, f2);
2961 		hist3 = v3 + imuldiv8(ebuf[spt3] - hist3, f3);
2962 		hist4 = v4 + imuldiv8(ebuf[spt4] - hist4, f4);
2963 		hist5 = v5 + imuldiv8(ebuf[spt5] - hist5, f5);
2964 		ebuf[index] = imuldiv24(buf[i] + buf[i + 1], weti);
2965 
2966 		/* mixing */
2967 		buf[i] = do_left_panning(hist0, pan0) + do_left_panning(hist1, pan1)
2968 			+ do_left_panning(hist2, pan2) + do_left_panning(hist3, pan3)
2969 			+ do_left_panning(hist4, pan4) + do_left_panning(hist5, pan5)
2970 			+ imuldiv24(buf[i], dryi);
2971 		buf[i + 1] = do_right_panning(hist0, pan0) + do_right_panning(hist1, pan1)
2972 			+ do_right_panning(hist2, pan2) + do_right_panning(hist3, pan3)
2973 			+ do_right_panning(hist4, pan4) + do_right_panning(hist5, pan5)
2974 			+ imuldiv24(buf[i + 1], dryi);
2975 
2976 		++i;
2977 	}
2978 	buf0->size = size, buf0->index = index;
2979 	info->spt0 = spt0, info->spt1 = spt1, info->spt2 = spt2,
2980 	info->spt3 = spt3, info->spt4 = spt4, info->spt5 = spt5,
2981 	info->hist0 = hist0, info->hist1 = hist1, info->hist2 = hist2,
2982 	info->hist3 = hist3, info->hist4 = hist4, info->hist5 = hist5;
2983 }
2984 
free_effect_xg(struct effect_xg_t * st)2985 static void free_effect_xg(struct effect_xg_t *st)
2986 {
2987 	free_effect_list(st->ef);
2988 	st->ef = NULL;
2989 }
2990 
free_effect_buffers(void)2991 void free_effect_buffers(void)
2992 {
2993 	int i;
2994 	/* free GM/GS/GM2 effects */
2995 	do_ch_standard_reverb(NULL, MAGIC_FREE_EFFECT_INFO, &(reverb_status_gs.info_standard_reverb));
2996 	do_ch_freeverb(NULL, MAGIC_FREE_EFFECT_INFO, &(reverb_status_gs.info_freeverb));
2997 	do_ch_plate_reverb(NULL, MAGIC_FREE_EFFECT_INFO, &(reverb_status_gs.info_plate_reverb));
2998 	do_ch_reverb_normal_delay(NULL, MAGIC_FREE_EFFECT_INFO, &(reverb_status_gs.info_reverb_delay));
2999 	do_ch_stereo_chorus(NULL, MAGIC_FREE_EFFECT_INFO, &(chorus_status_gs.info_stereo_chorus));
3000 	do_ch_3tap_delay(NULL, MAGIC_FREE_EFFECT_INFO, &(delay_status_gs.info_delay));
3001 	free_effect_list(insertion_effect_gs.ef);
3002 	insertion_effect_gs.ef = NULL;
3003 	/* free XG effects */
3004 	free_effect_xg(&reverb_status_xg);
3005 	free_effect_xg(&chorus_status_xg);
3006 	for (i = 0; i < XG_VARIATION_EFFECT_NUM; i++) {
3007 		free_effect_xg(&variation_effect_xg[i]);
3008 	}
3009 	for (i = 0; i < XG_INSERTION_EFFECT_NUM; i++) {
3010 		free_effect_xg(&insertion_effect_xg[i]);
3011 	}
3012 }
3013 
clip_int(int val,int min,int max)3014 static inline int clip_int(int val, int min, int max)
3015 {
3016 	return ((val > max) ? max : (val < min) ? min : val);
3017 }
3018 
conv_gs_eq2(struct insertion_effect_gs_t * ieffect,EffectList * ef)3019 static void conv_gs_eq2(struct insertion_effect_gs_t *ieffect, EffectList *ef)
3020 {
3021 	InfoEQ2 *eq = (InfoEQ2 *)ef->info;
3022 
3023 	eq->high_freq = 4000;
3024 	eq->high_gain = clip_int(ieffect->parameter[16] - 0x40, -12, 12);
3025 	eq->low_freq = 400;
3026 	eq->low_gain = clip_int(ieffect->parameter[17] - 0x40, -12, 12);
3027 }
3028 
conv_gs_overdrive1(struct insertion_effect_gs_t * ieffect,EffectList * ef)3029 static void conv_gs_overdrive1(struct insertion_effect_gs_t *ieffect, EffectList *ef)
3030 {
3031 	InfoOverdrive1 *od = (InfoOverdrive1 *)ef->info;
3032 
3033 	od->drive = ieffect->parameter[0];
3034 	od->amp_type = ieffect->parameter[1];
3035 	od->amp_sw = ieffect->parameter[2];
3036 	od->pan = ieffect->parameter[18];
3037 	od->level = (double)ieffect->parameter[19] / 127.0;
3038 }
3039 
conv_gs_dual_od(struct insertion_effect_gs_t * ieffect,EffectList * ef)3040 static void conv_gs_dual_od(struct insertion_effect_gs_t *ieffect, EffectList *ef)
3041 {
3042 	InfoOD1OD2 *od = (InfoOD1OD2 *)ef->info;
3043 
3044 	od->typel = ieffect->parameter[0];
3045 	od->drivel = ieffect->parameter[1];
3046 	od->amp_typel = ieffect->parameter[2];
3047 	od->amp_swl = ieffect->parameter[3];
3048 	od->typer = ieffect->parameter[5];
3049 	od->driver = ieffect->parameter[6];
3050 	od->amp_typer = ieffect->parameter[7];
3051 	od->amp_swr = ieffect->parameter[8];
3052 	od->panl = ieffect->parameter[15];
3053 	od->levell = (double)ieffect->parameter[16] / 127.0;
3054 	od->panr = ieffect->parameter[17];
3055 	od->levelr = (double)ieffect->parameter[18] / 127.0;
3056 	od->level = (double)ieffect->parameter[19] / 127.0;
3057 }
3058 
calc_dry_gs(int val)3059 static double calc_dry_gs(int val)
3060 {
3061 	return ((double)(127 - val) / 127.0);
3062 }
3063 
calc_wet_gs(int val)3064 static double calc_wet_gs(int val)
3065 {
3066 	return ((double)val / 127.0);
3067 }
3068 
conv_gs_hexa_chorus(struct insertion_effect_gs_t * ieffect,EffectList * ef)3069 static void conv_gs_hexa_chorus(struct insertion_effect_gs_t *ieffect, EffectList *ef)
3070 {
3071 	InfoHexaChorus *info = (InfoHexaChorus *)ef->info;
3072 
3073 	info->level = (double)ieffect->parameter[19] / 127.0;
3074 	info->pdelay = pre_delay_time_table[ieffect->parameter[0]] * (double)play_mode->rate / 1000.0;
3075 	info->depth = (double)(ieffect->parameter[2] + 1) / 3.2  * (double)play_mode->rate / 1000.0;
3076 	info->pdelay -= info->depth / 2;
3077 	if(info->pdelay <= 1) {info->pdelay = 1;}
3078 	info->lfo0.freq = rate1_table[ieffect->parameter[1]];
3079 	info->pdelay_dev = ieffect->parameter[3];
3080 	info->depth_dev = ieffect->parameter[4] - 64;
3081 	info->pan_dev = ieffect->parameter[5];
3082 	info->dry = calc_dry_gs(ieffect->parameter[15]);
3083 	info->wet = calc_wet_gs(ieffect->parameter[15]);
3084 }
3085 
calc_dry_xg(int val,struct effect_xg_t * st)3086 static double calc_dry_xg(int val, struct effect_xg_t *st)
3087 {
3088 	if (st->connection) {return 0.0;}
3089 	else {return ((double)(127 - val) / 127.0);}
3090 }
3091 
calc_wet_xg(int val,struct effect_xg_t * st)3092 static double calc_wet_xg(int val, struct effect_xg_t *st)
3093 {
3094 	switch(st->connection) {
3095 	case XG_CONN_SYSTEM:
3096 		return ((double)st->ret / 127.0);
3097 	case XG_CONN_SYSTEM_CHORUS:
3098 		return ((double)st->ret / 127.0);
3099 	case XG_CONN_SYSTEM_REVERB:
3100 		return ((double)st->ret / 127.0);
3101 	default:
3102 		return ((double)val / 127.0);
3103 	}
3104 }
3105 
3106 /*! 3-Band EQ */
do_eq3(int32 * buf,int32 count,EffectList * ef)3107 static void do_eq3(int32 *buf, int32 count, EffectList *ef)
3108 {
3109 	InfoEQ3 *eq = (InfoEQ3 *)ef->info;
3110 	if (count == MAGIC_INIT_EFFECT_INFO) {
3111 		eq->lsf.q = 0;
3112 		eq->lsf.freq = eq->low_freq;
3113 		eq->lsf.gain = eq->low_gain;
3114 		calc_filter_shelving_low(&(eq->lsf));
3115 		eq->hsf.q = 0;
3116 		eq->hsf.freq = eq->high_freq;
3117 		eq->hsf.gain = eq->high_gain;
3118 		calc_filter_shelving_high(&(eq->hsf));
3119 		eq->peak.q = 1.0 / eq->mid_width;
3120 		eq->peak.freq = eq->mid_freq;
3121 		eq->peak.gain = eq->mid_gain;
3122 		calc_filter_peaking(&(eq->peak));
3123 		return;
3124 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3125 		return;
3126 	}
3127 	if (eq->low_gain != 0) {
3128 		do_shelving_filter_stereo(buf, count, &(eq->lsf));
3129 	}
3130 	if (eq->high_gain != 0) {
3131 		do_shelving_filter_stereo(buf, count, &(eq->hsf));
3132 	}
3133 	if (eq->mid_gain != 0) {
3134 		do_peaking_filter_stereo(buf, count, &(eq->peak));
3135 	}
3136 }
3137 
3138 /*! Stereo EQ */
do_stereo_eq(int32 * buf,int32 count,EffectList * ef)3139 static void do_stereo_eq(int32 *buf, int32 count, EffectList *ef)
3140 {
3141 	InfoStereoEQ *eq = (InfoStereoEQ *)ef->info;
3142 	int32 i, leveli = eq->leveli;
3143 	if (count == MAGIC_INIT_EFFECT_INFO) {
3144 		eq->lsf.q = 0;
3145 		eq->lsf.freq = eq->low_freq;
3146 		eq->lsf.gain = eq->low_gain;
3147 		calc_filter_shelving_low(&(eq->lsf));
3148 		eq->hsf.q = 0;
3149 		eq->hsf.freq = eq->high_freq;
3150 		eq->hsf.gain = eq->high_gain;
3151 		calc_filter_shelving_high(&(eq->hsf));
3152 		eq->m1.q = eq->m1_q;
3153 		eq->m1.freq = eq->m1_freq;
3154 		eq->m1.gain = eq->m1_gain;
3155 		calc_filter_peaking(&(eq->m1));
3156 		eq->m2.q = eq->m2_q;
3157 		eq->m2.freq = eq->m2_freq;
3158 		eq->m2.gain = eq->m2_gain;
3159 		calc_filter_peaking(&(eq->m2));
3160 		eq->leveli = TIM_FSCALE(eq->level, 24);
3161 		return;
3162 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3163 		return;
3164 	}
3165 	if (eq->level != 1.0) {
3166 		for (i = 0; i < count; i++) {
3167 			buf[i] = imuldiv24(buf[i], leveli);
3168 		}
3169 	}
3170 	if (eq->low_gain != 0) {
3171 		do_shelving_filter_stereo(buf, count, &(eq->lsf));
3172 	}
3173 	if (eq->high_gain != 0) {
3174 		do_shelving_filter_stereo(buf, count, &(eq->hsf));
3175 	}
3176 	if (eq->m1_gain != 0) {
3177 		do_peaking_filter_stereo(buf, count, &(eq->m1));
3178 	}
3179 	if (eq->m2_gain != 0) {
3180 		do_peaking_filter_stereo(buf, count, &(eq->m2));
3181 	}
3182 }
3183 
conv_xg_eq2(struct effect_xg_t * st,EffectList * ef)3184 static void conv_xg_eq2(struct effect_xg_t *st, EffectList *ef)
3185 {
3186 	InfoEQ2 *info = (InfoEQ2 *)ef->info;
3187 
3188 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[0], 4, 40)];
3189 	info->low_gain = clip_int(st->param_lsb[1] - 64, -12, 12);
3190 	info->high_freq = eq_freq_table_xg[clip_int(st->param_lsb[2], 28, 58)];
3191 	info->high_gain = clip_int(st->param_lsb[3] - 64, -12, 12);
3192 }
3193 
conv_xg_eq3(struct effect_xg_t * st,EffectList * ef)3194 static void conv_xg_eq3(struct effect_xg_t *st, EffectList *ef)
3195 {
3196 	InfoEQ3 *info = (InfoEQ3 *)ef->info;
3197 
3198 	info->low_gain = clip_int(st->param_lsb[0] - 64, -12, 12);
3199 	info->mid_freq = eq_freq_table_xg[clip_int(st->param_lsb[1], 14, 54)];
3200 	info->mid_gain = clip_int(st->param_lsb[2] - 64, -12, 12);
3201 	info->mid_width = (double)clip_int(st->param_lsb[3], 10, 120) / 10.0;
3202 	info->high_gain = clip_int(st->param_lsb[4] - 64, -12, 12);
3203 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[5], 4, 40)];
3204 	info->high_freq = eq_freq_table_xg[clip_int(st->param_lsb[6], 28, 58)];
3205 }
3206 
3207 static float eq_q_table_gs[] =
3208 {
3209 	0.5, 1.0, 2.0, 4.0, 9.0,
3210 };
3211 
conv_gs_stereo_eq(struct insertion_effect_gs_t * st,EffectList * ef)3212 static void conv_gs_stereo_eq(struct insertion_effect_gs_t *st, EffectList *ef)
3213 {
3214 	InfoStereoEQ *info = (InfoStereoEQ *)ef->info;
3215 
3216 	info->low_freq = (st->parameter[0] == 0) ? 200 : 400;
3217 	info->low_gain = clip_int(st->parameter[1] - 64, -12, 12);
3218 	info->high_freq = (st->parameter[2] == 0) ? 4000 : 8000;
3219 	info->high_gain = clip_int(st->parameter[3] - 64, -12, 12);
3220 	info->m1_freq = eq_freq_table_gs[st->parameter[4]];
3221 	info->m1_q = eq_q_table_gs[clip_int(st->parameter[5], 0, 4)];
3222 	info->m1_gain = clip_int(st->parameter[6] - 64, -12, 12);
3223 	info->m2_freq = eq_freq_table_gs[st->parameter[7]];
3224 	info->m2_q = eq_q_table_gs[clip_int(st->parameter[8], 0, 4)];
3225 	info->m2_gain = clip_int(st->parameter[9] - 64, -12, 12);
3226 	info->level = (double)st->parameter[19] / 127.0;
3227 }
3228 
conv_xg_chorus_eq3(struct effect_xg_t * st,EffectList * ef)3229 static void conv_xg_chorus_eq3(struct effect_xg_t *st, EffectList *ef)
3230 {
3231 	InfoEQ3 *info = (InfoEQ3 *)ef->info;
3232 
3233 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[5], 4, 40)];
3234 	info->low_gain = clip_int(st->param_lsb[6] - 64, -12, 12);
3235 	info->high_freq = eq_freq_table_xg[clip_int(st->param_lsb[7], 28, 58)];
3236 	info->high_gain = clip_int(st->param_lsb[8] - 64, -12, 12);
3237 	info->mid_freq = eq_freq_table_xg[clip_int(st->param_lsb[10], 14, 54)];
3238 	info->mid_gain = clip_int(st->param_lsb[11] - 64, -12, 12);
3239 	info->mid_width = (double)clip_int(st->param_lsb[12], 10, 120) / 10.0;
3240 }
3241 
conv_xg_chorus(struct effect_xg_t * st,EffectList * ef)3242 static void conv_xg_chorus(struct effect_xg_t *st, EffectList *ef)
3243 {
3244 	InfoChorus *info = (InfoChorus *)ef->info;
3245 
3246 	info->rate = lfo_freq_table_xg[st->param_lsb[0]];
3247 	info->depth_ms = (double)(st->param_lsb[1] + 1) / 3.2 / 2.0;
3248 	info->feedback = (double)(st->param_lsb[2] - 64) * (0.763 * 2.0 / 100.0);
3249 	info->pdelay_ms = mod_delay_offset_table_xg[st->param_lsb[3]];
3250 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3251 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3252 	info->phase_diff = 90.0;
3253 }
3254 
conv_xg_flanger(struct effect_xg_t * st,EffectList * ef)3255 static void conv_xg_flanger(struct effect_xg_t *st, EffectList *ef)
3256 {
3257 	InfoChorus *info = (InfoChorus *)ef->info;
3258 
3259 	info->rate = lfo_freq_table_xg[st->param_lsb[0]];
3260 	info->depth_ms = (double)(st->param_lsb[1] + 1) / 3.2 / 2.0;
3261 	info->feedback = (double)(st->param_lsb[2] - 64) * (0.763 * 2.0 / 100.0);
3262 	info->pdelay_ms = mod_delay_offset_table_xg[st->param_lsb[2]];
3263 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3264 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3265 	info->phase_diff = (double)(clip_int(st->param_lsb[13], 4, 124) - 64) * 3.0;
3266 }
3267 
conv_xg_symphonic(struct effect_xg_t * st,EffectList * ef)3268 static void conv_xg_symphonic(struct effect_xg_t *st, EffectList *ef)
3269 {
3270 	InfoChorus *info = (InfoChorus *)ef->info;
3271 
3272 	info->rate = lfo_freq_table_xg[st->param_lsb[0]];
3273 	info->depth_ms = (double)(st->param_lsb[1] + 1) / 3.2 / 2.0;
3274 	info->feedback = 0.0;
3275 	info->pdelay_ms = mod_delay_offset_table_xg[st->param_lsb[3]];
3276 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3277 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3278 	info->phase_diff = 90.0;
3279 }
3280 
do_chorus(int32 * buf,int32 count,EffectList * ef)3281 static void do_chorus(int32 *buf, int32 count, EffectList *ef)
3282 {
3283 	InfoChorus *info = (InfoChorus *)ef->info;
3284 	int32 i, output, f0, f1, v0, v1;
3285 	int32 *bufL = info->delayL.buf, *bufR = info->delayR.buf,
3286 		*lfobufL = info->lfoL.buf, *lfobufR = info->lfoR.buf,
3287 		icycle = info->lfoL.icycle, cycle = info->lfoL.cycle,
3288 		dryi = info->dryi, weti = info->weti, feedbacki = info->feedbacki,
3289 		depth = info->depth, pdelay = info->pdelay, rpt0 = info->rpt0;
3290 	int32 wpt0 = info->wpt0, spt0 = info->spt0, spt1 = info->spt1,
3291 		hist0 = info->hist0, hist1 = info->hist1, lfocnt = info->lfoL.count;
3292 
3293 	if (count == MAGIC_INIT_EFFECT_INFO) {
3294 		init_lfo(&(info->lfoL), info->rate, LFO_TRIANGULAR, 0);
3295 		init_lfo(&(info->lfoR), info->rate, LFO_TRIANGULAR, info->phase_diff);
3296 		info->pdelay = info->pdelay_ms * (double)play_mode->rate / 1000.0;
3297 		info->depth = info->depth_ms * (double)play_mode->rate / 1000.0;
3298 		info->pdelay -= info->depth / 2;	/* NOMINAL_DELAY to delay */
3299 		if (info->pdelay < 1) {info->pdelay = 1;}
3300 		info->rpt0 = info->pdelay + info->depth + 2;	/* allowance */
3301 		set_delay(&(info->delayL), info->rpt0);
3302 		set_delay(&(info->delayR), info->rpt0);
3303 		info->feedbacki = TIM_FSCALE(info->feedback, 24);
3304 		info->dryi = TIM_FSCALE(info->dry, 24);
3305 		info->weti = TIM_FSCALE(info->wet, 24);
3306 		info->wpt0 = info->spt0 = info->spt1 = info->hist0 = info->hist1 = 0;
3307 		return;
3308 	} else if (count == MAGIC_FREE_EFFECT_INFO) {
3309 		free_delay(&(info->delayL));
3310 		free_delay(&(info->delayR));
3311 		return;
3312 	}
3313 
3314 	/* LFO */
3315 	f0 = imuldiv24(lfobufL[imuldiv24(lfocnt, icycle)], depth);
3316 	spt0 = wpt0 - pdelay - (f0 >> 8);	/* integral part of delay */
3317 	f0 = 0xFF - (f0 & 0xFF);	/* (1 - frac) * 256 */
3318 	if (spt0 < 0) {spt0 += rpt0;}
3319 	f1 = imuldiv24(lfobufR[imuldiv24(lfocnt, icycle)], depth);
3320 	spt1 = wpt0 - pdelay - (f1 >> 8);	/* integral part of delay */
3321 	f1 = 0xFF - (f1 & 0xFF);	/* (1 - frac) * 256 */
3322 	if (spt1 < 0) {spt1 += rpt0;}
3323 
3324 	for (i = 0; i < count; i++) {
3325 		v0 = bufL[spt0];
3326 		v1 = bufR[spt1];
3327 
3328 		/* LFO */
3329 		if(++wpt0 == rpt0) {wpt0 = 0;}
3330 		f0 = imuldiv24(lfobufL[imuldiv24(lfocnt, icycle)], depth);
3331 		spt0 = wpt0 - pdelay - (f0 >> 8);	/* integral part of delay */
3332 		f0 = 0xFF - (f0 & 0xFF);	/* (1 - frac) * 256 */
3333 		if(spt0 < 0) {spt0 += rpt0;}
3334 		f1 = imuldiv24(lfobufR[imuldiv24(lfocnt, icycle)], depth);
3335 		spt1 = wpt0 - pdelay - (f1 >> 8);	/* integral part of delay */
3336 		f1 = 0xFF - (f1 & 0xFF);	/* (1 - frac) * 256 */
3337 		if(spt1 < 0) {spt1 += rpt0;}
3338 		if(++lfocnt == cycle) {lfocnt = 0;}
3339 
3340 		/* left */
3341 		/* delay with all-pass interpolation */
3342 		output = hist0 = v0 + imuldiv8(bufL[spt0] - hist0, f0);
3343 		bufL[wpt0] = buf[i] + imuldiv24(output, feedbacki);
3344 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(output, weti);
3345 
3346 		/* right */
3347 		/* delay with all-pass interpolation */
3348 		output = hist1 = v1 + imuldiv8(bufR[spt1] - hist1, f1);
3349 		bufR[wpt0] = buf[++i] + imuldiv24(output, feedbacki);
3350 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(output, weti);
3351 	}
3352 	info->wpt0 = wpt0, info->spt0 = spt0, info->spt1 = spt1,
3353 		info->hist0 = hist0, info->hist1 = hist1;
3354 	info->lfoL.count = info->lfoR.count = lfocnt;
3355 }
3356 
conv_xg_od_eq3(struct effect_xg_t * st,EffectList * ef)3357 static void conv_xg_od_eq3(struct effect_xg_t *st, EffectList *ef)
3358 {
3359 	InfoEQ3 *info = (InfoEQ3 *)ef->info;
3360 
3361 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[1], 4, 40)];
3362 	info->low_gain = clip_int(st->param_lsb[2] - 64, -12, 12);
3363 	info->mid_freq = eq_freq_table_xg[clip_int(st->param_lsb[6], 14, 54)];
3364 	info->mid_gain = clip_int(st->param_lsb[7] - 64, -12, 12);
3365 	info->mid_width = (double)clip_int(st->param_lsb[8], 10, 120) / 10.0;
3366 	info->high_freq = 0;
3367 	info->high_gain = 0;
3368 }
3369 
conv_xg_overdrive(struct effect_xg_t * st,EffectList * ef)3370 static void conv_xg_overdrive(struct effect_xg_t *st, EffectList *ef)
3371 {
3372 	InfoStereoOD *info = (InfoStereoOD *)ef->info;
3373 
3374 	info->od = do_soft_clipping1;
3375 	info->drive = (double)st->param_lsb[0] / 127.0;
3376 	info->cutoff = eq_freq_table_xg[clip_int(st->param_lsb[3], 34, 60)];
3377 	info->level = (double)st->param_lsb[4] / 127.0;
3378 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3379 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3380 }
3381 
conv_xg_distortion(struct effect_xg_t * st,EffectList * ef)3382 static void conv_xg_distortion(struct effect_xg_t *st, EffectList *ef)
3383 {
3384 	InfoStereoOD *info = (InfoStereoOD *)ef->info;
3385 
3386 	info->od = do_hard_clipping;
3387 	info->drive = (double)st->param_lsb[0] / 127.0;
3388 	info->cutoff = eq_freq_table_xg[clip_int(st->param_lsb[3], 34, 60)];
3389 	info->level = (double)st->param_lsb[4] / 127.0;
3390 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3391 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3392 }
3393 
conv_xg_amp_simulator(struct effect_xg_t * st,EffectList * ef)3394 static void conv_xg_amp_simulator(struct effect_xg_t *st, EffectList *ef)
3395 {
3396 	InfoStereoOD *info = (InfoStereoOD *)ef->info;
3397 
3398 	info->od = do_soft_clipping2;
3399 	info->drive = (double)st->param_lsb[0] / 127.0;
3400 	info->cutoff = eq_freq_table_xg[clip_int(st->param_lsb[2], 34, 60)];
3401 	info->level = (double)st->param_lsb[3] / 127.0;
3402 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3403 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3404 }
3405 
do_stereo_od(int32 * buf,int32 count,EffectList * ef)3406 static void do_stereo_od(int32 *buf, int32 count, EffectList *ef)
3407 {
3408 	InfoStereoOD *info = (InfoStereoOD *)ef->info;
3409 	filter_moog *svfl = &(info->svfl), *svfr = &(info->svfr);
3410 	filter_biquad *lpf1 = &(info->lpf1);
3411 	void (*do_od)(int32 *, int32) = info->od;
3412 	int32 i, inputl, inputr, high, weti = info->weti, dryi = info->dryi, di = info->di;
3413 
3414 	if(count == MAGIC_INIT_EFFECT_INFO) {
3415 		/* decompositor */
3416 		svfl->freq = 500;
3417 		svfl->res_dB = 0;
3418 		calc_filter_moog(svfl);
3419 		init_filter_moog(svfl);
3420 		svfr->freq = 500;
3421 		svfr->res_dB = 0;
3422 		calc_filter_moog(svfr);
3423 		init_filter_moog(svfr);
3424 		/* anti-aliasing */
3425 		lpf1->freq = info->cutoff;
3426 		lpf1->q = 1.0;
3427 		calc_filter_biquad_low(lpf1);
3428 		info->weti = TIM_FSCALE(info->wet * info->level, 24);
3429 		info->dryi = TIM_FSCALE(info->dry * info->level, 24);
3430 		info->di = TIM_FSCALE(calc_gs_drive(info->drive), 24);
3431 		return;
3432 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3433 		return;
3434 	}
3435 	for(i = 0; i < count; i++) {
3436 		/* left */
3437 		inputl = buf[i];
3438 		/* decomposition */
3439 		do_filter_moog(&inputl, &high, svfl->f, svfl->p, svfl->q,
3440 			&svfl->b0, &svfl->b1, &svfl->b2, &svfl->b3, &svfl->b4);
3441 		/* waveshaping */
3442 		do_od(&high, di);
3443 		/* anti-aliasing */
3444 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1l, &lpf1->x2l, &lpf1->y1l, &lpf1->y2l);
3445 		buf[i] = imuldiv24(high + inputl, weti) + imuldiv24(buf[i], dryi);
3446 
3447 		/* right */
3448 		inputr = buf[++i];
3449 		/* decomposition */
3450 		do_filter_moog(&inputr, &high, svfr->f, svfr->p, svfr->q,
3451 			&svfr->b0, &svfr->b1, &svfr->b2, &svfr->b3, &svfr->b4);
3452 		/* waveshaping */
3453 		do_od(&high, di);
3454 		/* anti-aliasing */
3455 		do_filter_biquad(&high, lpf1->a1, lpf1->a2, lpf1->b1, lpf1->b02, &lpf1->x1r, &lpf1->x2r, &lpf1->y1r, &lpf1->y2r);
3456 		buf[i] = imuldiv24(high + inputr, weti) + imuldiv24(buf[i], dryi);
3457 	}
3458 }
3459 
do_delay_lcr(int32 * buf,int32 count,EffectList * ef)3460 static void do_delay_lcr(int32 *buf, int32 count, EffectList *ef)
3461 {
3462 	int32 i, x;
3463 	InfoDelayLCR *info = (InfoDelayLCR *)ef->info;
3464 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
3465 	filter_lowpass1 *lpf = &(info->lpf);
3466 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
3467 	int32 buf_index = delayL->index, buf_size = delayL->size;
3468 	int32 index0 = info->index[0], index1 = info->index[1], index2 = info->index[2],
3469 		x1l = lpf->x1l, x1r = lpf->x1r;
3470 	int32 cleveli = info->cleveli, feedbacki = info->feedbacki,
3471 		dryi = info->dryi, weti = info->weti, ai = lpf->ai, iai = lpf->iai;
3472 
3473 	if(count == MAGIC_INIT_EFFECT_INFO) {
3474 		info->size[0] = info->ldelay * play_mode->rate / 1000.0;
3475 		info->size[1] = info->cdelay * play_mode->rate / 1000.0;
3476 		info->size[2] = info->rdelay * play_mode->rate / 1000.0;
3477 		x = info->fdelay * play_mode->rate / 1000.0;
3478 		for (i = 0; i < 3; i++) {
3479 			if (info->size[i] > x) {info->size[i] = x;}
3480 		}
3481 		x += 1;	/* allowance */
3482 		set_delay(&(info->delayL), x);
3483 		set_delay(&(info->delayR), x);
3484 		for (i = 0; i < 3; i++) {	/* set start-point */
3485 			info->index[i] = x - info->size[i];
3486 		}
3487 		info->feedbacki = TIM_FSCALE(info->feedback, 24);
3488 		info->cleveli = TIM_FSCALE(info->clevel, 24);
3489 		info->dryi = TIM_FSCALE(info->dry, 24);
3490 		info->weti = TIM_FSCALE(info->wet, 24);
3491 		lpf->a = (1.0 - info->high_damp) * 44100.0 / play_mode->rate;
3492 		init_filter_lowpass1(lpf);
3493 		return;
3494 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3495 		free_delay(&(info->delayL));
3496 		free_delay(&(info->delayR));
3497 		return;
3498 	}
3499 
3500 	for (i = 0; i < count; i++)
3501 	{
3502 		x = imuldiv24(bufL[buf_index], feedbacki);
3503 		do_filter_lowpass1(&x, &x1l, ai, iai);
3504 		bufL[buf_index] = buf[i] + x;
3505 		x = bufL[index0] + imuldiv24(bufL[index1], cleveli);
3506 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(x, weti);
3507 
3508 		x = imuldiv24(bufR[buf_index], feedbacki);
3509 		do_filter_lowpass1(&x, &x1r, ai, iai);
3510 		bufR[buf_index] = buf[++i] + x;
3511 		x = bufR[index2] + imuldiv24(bufR[index1], cleveli);
3512 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(x, weti);
3513 
3514 		if (++index0 == buf_size) {index0 = 0;}
3515 		if (++index1 == buf_size) {index1 = 0;}
3516 		if (++index2 == buf_size) {index2 = 0;}
3517 		if (++buf_index == buf_size) {buf_index = 0;}
3518 	}
3519 	info->index[0] = index0, info->index[1] = index1, info->index[2] = index2;
3520 	lpf->x1l = x1l, lpf->x1r = x1r;
3521 	delayL->index = delayR->index = buf_index;
3522 }
3523 
conv_xg_delay_eq2(struct effect_xg_t * st,EffectList * ef)3524 static void conv_xg_delay_eq2(struct effect_xg_t *st, EffectList *ef)
3525 {
3526 	InfoEQ2 *info = (InfoEQ2 *)ef->info;
3527 
3528 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[12], 4, 40)];
3529 	info->low_gain = clip_int(st->param_lsb[13] - 64, -12, 12);
3530 	info->high_freq = eq_freq_table_xg[clip_int(st->param_lsb[14], 28, 58)];
3531 	info->high_gain = clip_int(st->param_lsb[15] - 64, -12, 12);
3532 }
3533 
conv_xg_delay_lcr(struct effect_xg_t * st,EffectList * ef)3534 static void conv_xg_delay_lcr(struct effect_xg_t *st, EffectList *ef)
3535 {
3536 	InfoDelayLCR *info = (InfoDelayLCR *)ef->info;
3537 
3538 	info->ldelay = (double)clip_int(st->param_msb[0] * 128 + st->param_lsb[0], 1, 14860) / 10.0;
3539 	info->rdelay = (double)clip_int(st->param_msb[1] * 128 + st->param_lsb[1], 1, 14860) / 10.0;
3540 	info->cdelay = (double)clip_int(st->param_msb[2] * 128 + st->param_lsb[2], 1, 14860) / 10.0;
3541 	info->fdelay = (double)clip_int(st->param_msb[3] * 128 + st->param_lsb[3], 1, 14860) / 10.0;
3542 	info->feedback = (double)(st->param_lsb[4] - 64) * (0.763 * 2.0 / 100.0);
3543 	info->clevel = (double)st->param_lsb[5] / 127.0;
3544 	info->high_damp = (double)clip_int(st->param_lsb[6], 1, 10) / 10.0;
3545 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3546 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3547 }
3548 
conv_xg_delay_lr(struct effect_xg_t * st,EffectList * ef)3549 static void conv_xg_delay_lr(struct effect_xg_t *st, EffectList *ef)
3550 {
3551 	InfoDelayLR *info = (InfoDelayLR *)ef->info;
3552 
3553 	info->ldelay = (double)clip_int(st->param_msb[0] * 128 + st->param_lsb[0], 1, 14860) / 10.0;
3554 	info->rdelay = (double)clip_int(st->param_msb[1] * 128 + st->param_lsb[1], 1, 14860) / 10.0;
3555 	info->fdelay1 = (double)clip_int(st->param_msb[2] * 128 + st->param_lsb[2], 1, 14860) / 10.0;
3556 	info->fdelay2 = (double)clip_int(st->param_msb[3] * 128 + st->param_lsb[3], 1, 14860) / 10.0;
3557 	info->feedback = (double)(st->param_lsb[4] - 64) * (0.763 * 2.0 / 100.0);
3558 	info->high_damp = (double)clip_int(st->param_lsb[5], 1, 10) / 10.0;
3559 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3560 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3561 }
3562 
do_delay_lr(int32 * buf,int32 count,EffectList * ef)3563 static void do_delay_lr(int32 *buf, int32 count, EffectList *ef)
3564 {
3565 	int32 i, x;
3566 	InfoDelayLR *info = (InfoDelayLR *)ef->info;
3567 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
3568 	filter_lowpass1 *lpf = &(info->lpf);
3569 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
3570 	int32 indexl = delayL->index, sizel = delayL->size,
3571 		indexr = delayR->index, sizer = delayR->size;
3572 	int32 index0 = info->index[0], index1 = info->index[1],
3573 		x1l = lpf->x1l, x1r = lpf->x1r;
3574 	int32 feedbacki = info->feedbacki,
3575 		dryi = info->dryi, weti = info->weti, ai = lpf->ai, iai = lpf->iai;
3576 
3577 	if(count == MAGIC_INIT_EFFECT_INFO) {
3578 		info->size[0] = info->ldelay * play_mode->rate / 1000.0;
3579 		x = info->fdelay1 * play_mode->rate / 1000.0;
3580 		if (info->size[0] > x) {info->size[0] = x;}
3581 		x++;
3582 		set_delay(&(info->delayL), x);
3583 		info->index[0] = x - info->size[0];
3584 		info->size[1] = info->rdelay * play_mode->rate / 1000.0;
3585 		x = info->fdelay2 * play_mode->rate / 1000.0;
3586 		if (info->size[1] > x) {info->size[1] = x;}
3587 		x++;
3588 		set_delay(&(info->delayR), x);
3589 		info->index[1] = x - info->size[1];
3590 		info->feedbacki = TIM_FSCALE(info->feedback, 24);
3591 		info->dryi = TIM_FSCALE(info->dry, 24);
3592 		info->weti = TIM_FSCALE(info->wet, 24);
3593 		lpf->a = (1.0 - info->high_damp) * 44100.0 / play_mode->rate;
3594 		init_filter_lowpass1(lpf);
3595 		return;
3596 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3597 		free_delay(&(info->delayL));
3598 		free_delay(&(info->delayR));
3599 		return;
3600 	}
3601 
3602 	for (i = 0; i < count; i++)
3603 	{
3604 		x = imuldiv24(bufL[indexl], feedbacki);
3605 		do_filter_lowpass1(&x, &x1l, ai, iai);
3606 		bufL[indexl] = buf[i] + x;
3607 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(bufL[index0], weti);
3608 
3609 		x = imuldiv24(bufR[indexr], feedbacki);
3610 		do_filter_lowpass1(&x, &x1r, ai, iai);
3611 		bufR[indexr] = buf[++i] + x;
3612 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(bufR[index1], weti);
3613 
3614 		if (++index0 == sizel) {index0 = 0;}
3615 		if (++index1 == sizer) {index1 = 0;}
3616 		if (++indexl == sizel) {indexl = 0;}
3617 		if (++indexr == sizer) {indexr = 0;}
3618 	}
3619 	info->index[0] = index0, info->index[1] = index1;
3620 	lpf->x1l = x1l, lpf->x1r = x1r;
3621 	delayL->index = indexl, delayR->index = indexr;
3622 }
3623 
conv_xg_echo(struct effect_xg_t * st,EffectList * ef)3624 static void conv_xg_echo(struct effect_xg_t *st, EffectList *ef)
3625 {
3626 	InfoEcho *info = (InfoEcho *)ef->info;
3627 
3628 	info->ldelay1 = (double)clip_int(st->param_msb[0] * 128 + st->param_lsb[0], 1, 7430) / 10.0;
3629 	info->lfeedback = (double)(st->param_lsb[1] - 64) * (0.763 * 2.0 / 100.0);
3630 	info->rdelay1 = (double)clip_int(st->param_msb[2] * 128 + st->param_lsb[2], 1, 7430) / 10.0;
3631 	info->rfeedback = (double)(st->param_lsb[3] - 64) * (0.763 * 2.0 / 100.0);
3632 	info->high_damp = (double)clip_int(st->param_lsb[4], 1, 10) / 10.0;
3633 	info->ldelay2 = (double)clip_int(st->param_msb[5] * 128 + st->param_lsb[5], 1, 7430) / 10.0;
3634 	info->rdelay2 = (double)clip_int(st->param_msb[6] * 128 + st->param_lsb[6], 1, 7430) / 10.0;
3635 	info->level = (double)st->param_lsb[7] / 127.0;
3636 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3637 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3638 }
3639 
do_echo(int32 * buf,int32 count,EffectList * ef)3640 static void do_echo(int32 *buf, int32 count, EffectList *ef)
3641 {
3642 	int32 i, x, y;
3643 	InfoEcho *info = (InfoEcho *)ef->info;
3644 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
3645 	filter_lowpass1 *lpf = &(info->lpf);
3646 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
3647 	int32 indexl = delayL->index, sizel = delayL->size,
3648 		indexr = delayR->index, sizer = delayR->size;
3649 	int32 index0 = info->index[0], index1 = info->index[1],
3650 		x1l = lpf->x1l, x1r = lpf->x1r;
3651 	int32 lfeedbacki = info->lfeedbacki, rfeedbacki = info->rfeedbacki, leveli = info->leveli,
3652 		dryi = info->dryi, weti = info->weti, ai = lpf->ai, iai = lpf->iai;
3653 
3654 	if(count == MAGIC_INIT_EFFECT_INFO) {
3655 		info->size[0] = info->ldelay2 * play_mode->rate / 1000.0;
3656 		x = info->ldelay1 * play_mode->rate / 1000.0;
3657 		if (info->size[0] > x) {info->size[0] = x;}
3658 		x++;
3659 		set_delay(&(info->delayL), x);
3660 		info->index[0] = x - info->size[0];
3661 		info->size[1] = info->rdelay2 * play_mode->rate / 1000.0;
3662 		x = info->rdelay1 * play_mode->rate / 1000.0;
3663 		if (info->size[1] > x) {info->size[1] = x;}
3664 		x++;
3665 		set_delay(&(info->delayR), x);
3666 		info->index[1] = x - info->size[1];
3667 		info->lfeedbacki = TIM_FSCALE(info->lfeedback, 24);
3668 		info->rfeedbacki = TIM_FSCALE(info->rfeedback, 24);
3669 		info->leveli = TIM_FSCALE(info->level, 24);
3670 		info->dryi = TIM_FSCALE(info->dry, 24);
3671 		info->weti = TIM_FSCALE(info->wet, 24);
3672 		lpf->a = (1.0 - info->high_damp) * 44100.0 / play_mode->rate;
3673 		init_filter_lowpass1(lpf);
3674 		return;
3675 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3676 		free_delay(&(info->delayL));
3677 		free_delay(&(info->delayR));
3678 		return;
3679 	}
3680 
3681 	for (i = 0; i < count; i++)
3682 	{
3683 		y = bufL[indexl] + imuldiv24(bufL[index0], leveli);
3684 		x = imuldiv24(bufL[indexl], lfeedbacki);
3685 		do_filter_lowpass1(&x, &x1l, ai, iai);
3686 		bufL[indexl] = buf[i] + x;
3687 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(y, weti);
3688 
3689 		y = bufR[indexr] + imuldiv24(bufR[index1], leveli);
3690 		x = imuldiv24(bufR[indexr], rfeedbacki);
3691 		do_filter_lowpass1(&x, &x1r, ai, iai);
3692 		bufR[indexr] = buf[++i] + x;
3693 		buf[i] = imuldiv24(buf[i], dryi) + imuldiv24(y, weti);
3694 
3695 		if (++index0 == sizel) {index0 = 0;}
3696 		if (++index1 == sizer) {index1 = 0;}
3697 		if (++indexl == sizel) {indexl = 0;}
3698 		if (++indexr == sizer) {indexr = 0;}
3699 	}
3700 	info->index[0] = index0, info->index[1] = index1;
3701 	lpf->x1l = x1l, lpf->x1r = x1r;
3702 	delayL->index = indexl, delayR->index = indexr;
3703 }
3704 
conv_xg_cross_delay(struct effect_xg_t * st,EffectList * ef)3705 static void conv_xg_cross_delay(struct effect_xg_t *st, EffectList *ef)
3706 {
3707 	InfoCrossDelay *info = (InfoCrossDelay *)ef->info;
3708 
3709 	info->lrdelay = (double)clip_int(st->param_msb[0] * 128 + st->param_lsb[0], 1, 7430) / 10.0;
3710 	info->rldelay = (double)clip_int(st->param_msb[1] * 128 + st->param_lsb[1], 1, 7430) / 10.0;
3711 	info->feedback = (double)(st->param_lsb[2] - 64) * (0.763 * 2.0 / 100.0);
3712 	info->input_select = st->param_lsb[3];
3713 	info->high_damp = (double)clip_int(st->param_lsb[4], 1, 10) / 10.0;
3714 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3715 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3716 }
3717 
do_cross_delay(int32 * buf,int32 count,EffectList * ef)3718 static void do_cross_delay(int32 *buf, int32 count, EffectList *ef)
3719 {
3720 	int32 i, lfb, rfb, lout, rout;
3721 	InfoCrossDelay *info = (InfoCrossDelay *)ef->info;
3722 	simple_delay *delayL = &(info->delayL), *delayR = &(info->delayR);
3723 	filter_lowpass1 *lpf = &(info->lpf);
3724 	int32 *bufL = delayL->buf, *bufR = delayR->buf;
3725 	int32 indexl = delayL->index, sizel = delayL->size,
3726 		indexr = delayR->index, sizer = delayR->size,
3727 		x1l = lpf->x1l, x1r = lpf->x1r;
3728 	int32 feedbacki = info->feedbacki,
3729 		dryi = info->dryi, weti = info->weti, ai = lpf->ai, iai = lpf->iai;
3730 
3731 	if(count == MAGIC_INIT_EFFECT_INFO) {
3732 		set_delay(&(info->delayL), (int32)(info->lrdelay * play_mode->rate / 1000.0));
3733 		set_delay(&(info->delayR), (int32)(info->rldelay * play_mode->rate / 1000.0));
3734 		info->feedbacki = TIM_FSCALE(info->feedback, 24);
3735 		info->dryi = TIM_FSCALE(info->dry, 24);
3736 		info->weti = TIM_FSCALE(info->wet, 24);
3737 		lpf->a = (1.0 - info->high_damp) * 44100.0 / play_mode->rate;
3738 		init_filter_lowpass1(lpf);
3739 		return;
3740 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3741 		free_delay(&(info->delayL));
3742 		free_delay(&(info->delayR));
3743 		return;
3744 	}
3745 
3746 	for (i = 0; i < count; i++)
3747 	{
3748 		lfb = imuldiv24(bufL[indexl], feedbacki);
3749 		do_filter_lowpass1(&lfb, &x1l, ai, iai);
3750 		lout = imuldiv24(buf[i], dryi) + imuldiv24(bufL[indexl], weti);
3751 		rfb = imuldiv24(bufR[indexr], feedbacki);
3752 		do_filter_lowpass1(&rfb, &x1r, ai, iai);
3753 		rout = imuldiv24(buf[i + 1], dryi) + imuldiv24(bufR[indexr], weti);
3754 		bufL[indexl] = buf[i] + rfb;
3755 		buf[i] = lout;
3756 		bufR[indexr] = buf[++i] + lfb;
3757 		buf[i] = rout;
3758 
3759 		if (++indexl == sizel) {indexl = 0;}
3760 		if (++indexr == sizer) {indexr = 0;}
3761 	}
3762 	lpf->x1l = x1l, lpf->x1r = x1r;
3763 	delayL->index = indexl, delayR->index = indexr;
3764 }
3765 
conv_gs_lofi1(struct insertion_effect_gs_t * st,EffectList * ef)3766 static void conv_gs_lofi1(struct insertion_effect_gs_t *st, EffectList *ef)
3767 {
3768 	InfoLoFi1 *info = (InfoLoFi1 *)ef->info;
3769 
3770 	info->pre_filter = st->parameter[0];
3771 	info->lofi_type = 1 + clip_int(st->parameter[1], 0, 8);
3772 	info->post_filter = st->parameter[2];
3773 	info->dry = calc_dry_gs(st->parameter[15] & 0x7F);
3774 	info->wet = calc_wet_gs(st->parameter[15] & 0x7F);
3775 	info->pan = st->parameter[18];
3776 	info->level = (st->parameter[19] & 0x7F) / 127.0;
3777 }
3778 
apply_lofi(int32 input,int32 bit_mask,int32 level_shift)3779 static inline int32 apply_lofi(int32 input, int32 bit_mask, int32 level_shift)
3780 {
3781 	return (input + level_shift) & bit_mask;
3782 }
3783 
do_lofi1(int32 * buf,int32 count,EffectList * ef)3784 static void do_lofi1(int32 *buf, int32 count, EffectList *ef)
3785 {
3786 	int32 i, x, y;
3787 	InfoLoFi1 *info = (InfoLoFi1 *)ef->info;
3788 	int32 bit_mask = info->bit_mask, dryi = info->dryi,	weti = info->weti;
3789 	const int32 level_shift = info->level_shift;
3790 
3791 	if(count == MAGIC_INIT_EFFECT_INFO) {
3792 		info->bit_mask = ~0L << (info->lofi_type * 2);
3793 		info->level_shift = ~info->bit_mask >> 1;
3794 		info->dryi = TIM_FSCALE(info->dry * info->level, 24);
3795 		info->weti = TIM_FSCALE(info->wet * info->level, 24);
3796 		return;
3797 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3798 		return;
3799 	}
3800 
3801 	for (i = 0; i < count; i++)
3802 	{
3803 		x = buf[i];
3804 		y = apply_lofi(x, bit_mask, level_shift);
3805 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3806 
3807 		x = buf[++i];
3808 		y = apply_lofi(x, bit_mask, level_shift);
3809 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3810 	}
3811 }
3812 
conv_gs_lofi2(struct insertion_effect_gs_t * st,EffectList * ef)3813 static void conv_gs_lofi2(struct insertion_effect_gs_t *st, EffectList *ef)
3814 {
3815 	InfoLoFi2 *info = (InfoLoFi2 *)ef->info;
3816 
3817 	info->lofi_type = 1 + clip_int(st->parameter[0], 0, 5);
3818 	info->fil_type = clip_int(st->parameter[1], 0, 2);
3819 	info->fil.freq = cutoff_freq_table_gs[st->parameter[2]];
3820 	info->rdetune = st->parameter[3];
3821 	info->rnz_lev = (double)st->parameter[4] / 127.0;
3822 	info->wp_sel = clip_int(st->parameter[5], 0, 1);
3823 	info->wp_lpf.freq = lpf_table_gs[st->parameter[6]];
3824 	info->wp_level = (double)st->parameter[7] / 127.0;
3825 	info->disc_type = clip_int(st->parameter[8], 0, 3);
3826 	info->disc_lpf.freq = lpf_table_gs[st->parameter[9]];
3827 	info->discnz_lev = (double)st->parameter[10] / 127.0;
3828 	info->hum_type = clip_int(st->parameter[11], 0, 1);
3829 	info->hum_lpf.freq = lpf_table_gs[st->parameter[12]];
3830 	info->hum_level = (double)st->parameter[13] / 127.0;
3831 	info->ms = clip_int(st->parameter[14], 0, 1);
3832 	info->dry = calc_dry_gs(st->parameter[15] & 0x7F);
3833 	info->wet = calc_wet_gs(st->parameter[15] & 0x7F);
3834 	info->pan = st->parameter[18];
3835 	info->level = (st->parameter[19] & 0x7F) / 127.0;
3836 }
3837 
do_lofi2(int32 * buf,int32 count,EffectList * ef)3838 static void do_lofi2(int32 *buf, int32 count, EffectList *ef)
3839 {
3840 	int32 i, x, y;
3841 	InfoLoFi2 *info = (InfoLoFi2 *)ef->info;
3842 	filter_biquad *fil = &(info->fil);
3843 	int32 bit_mask = info->bit_mask, dryi = info->dryi,	weti = info->weti;
3844 	const int32 level_shift = info->level_shift;
3845 
3846 	if(count == MAGIC_INIT_EFFECT_INFO) {
3847 		fil->q = 1.0;
3848 		if (info->fil_type == 1) {calc_filter_biquad_low(fil);}
3849 		else if (info->fil_type == 2) {calc_filter_biquad_high(fil);}
3850 		else {
3851 			fil->freq = -1;	/* bypass */
3852 			calc_filter_biquad_low(fil);
3853 		}
3854 		info->bit_mask = ~0L << (info->lofi_type * 2);
3855 		info->level_shift = ~info->bit_mask >> 1;
3856 		info->dryi = TIM_FSCALE(info->dry * info->level, 24);
3857 		info->weti = TIM_FSCALE(info->wet * info->level, 24);
3858 		return;
3859 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3860 		return;
3861 	}
3862 	for (i = 0; i < count; i++)
3863 	{
3864 		x = buf[i];
3865 		y = apply_lofi(x, bit_mask, level_shift);
3866 		do_filter_biquad(&y, fil->a1, fil->a2, fil->b1, fil->b02, &fil->x1l, &fil->x2l, &fil->y1l, &fil->y2l);
3867 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3868 
3869 		x = buf[++i];
3870 		y = apply_lofi(x, bit_mask, level_shift);
3871 		do_filter_biquad(&y, fil->a1, fil->a2, fil->b1, fil->b02, &fil->x1r, &fil->x2r, &fil->y1r, &fil->y2r);
3872 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3873 	}
3874 }
3875 
conv_xg_lofi(struct effect_xg_t * st,EffectList * ef)3876 static void conv_xg_lofi(struct effect_xg_t *st, EffectList *ef)
3877 {
3878 	InfoLoFi *info = (InfoLoFi *)ef->info;
3879 
3880 	info->srf.freq = lofi_sampling_freq_table_xg[st->param_lsb[0]] / 2.0;
3881 	info->word_length = st->param_lsb[1];
3882 	info->output_gain = clip_int(st->param_lsb[2], 0, 18);
3883 	info->lpf.freq = eq_freq_table_xg[clip_int(st->param_lsb[3], 10, 80)];
3884 	info->filter_type = st->param_lsb[4];
3885 	info->lpf.q = (double)clip_int(st->param_lsb[5], 10, 120) / 10.0;
3886 	info->bit_assign = clip_int(st->param_lsb[6], 0, 6);
3887 	info->emphasis = st->param_lsb[7];
3888 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3889 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3890 }
3891 
do_lofi(int32 * buf,int32 count,EffectList * ef)3892 static void do_lofi(int32 *buf, int32 count, EffectList *ef)
3893 {
3894 	int32 i, x, y;
3895 	InfoLoFi *info = (InfoLoFi *)ef->info;
3896 	filter_biquad *lpf = &(info->lpf), *srf = &(info->srf);
3897 	int32 bit_mask = info->bit_mask, dryi = info->dryi,	weti = info->weti;
3898 	const int32 level_shift = info->level_shift;
3899 
3900 	if(count == MAGIC_INIT_EFFECT_INFO) {
3901 		srf->q = 1.0;
3902 		calc_filter_biquad_low(srf);
3903 		calc_filter_biquad_low(lpf);
3904 		info->bit_mask = ~((1L << (info->bit_assign + 22 - GUARD_BITS)) - 1L);
3905 		info->level_shift = ~info->bit_mask >> 1;
3906 		info->dryi = TIM_FSCALE(info->dry * pow(10.0, (double)info->output_gain / 20.0), 24);
3907 		info->weti = TIM_FSCALE(info->wet * pow(10.0, (double)info->output_gain / 20.0), 24);
3908 		return;
3909 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
3910 		return;
3911 	}
3912 
3913 	for (i = 0; i < count; i++)
3914 	{
3915 		x = buf[i];
3916 		y = apply_lofi(x, bit_mask, level_shift);
3917 		do_filter_biquad(&y, srf->a1, srf->a2, srf->b1, srf->b02, &srf->x1l, &srf->x2l, &srf->y1l, &srf->y2l);
3918 		do_filter_biquad(&y, lpf->a1, lpf->a2, lpf->b1, lpf->b02, &lpf->x1l, &lpf->x2l, &lpf->y1l, &lpf->y2l);
3919 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3920 
3921 		x = buf[++i];
3922 		y = apply_lofi(x, bit_mask, level_shift);
3923 		do_filter_biquad(&y, srf->a1, srf->a2, srf->b1, srf->b02, &srf->x1r, &srf->x2r, &srf->y1r, &srf->y2r);
3924 		do_filter_biquad(&y, lpf->a1, lpf->a2, lpf->b1, lpf->b02, &lpf->x1r, &lpf->x2r, &lpf->y1r, &lpf->y2r);
3925 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
3926 	}
3927 }
3928 
conv_xg_auto_wah_od(struct effect_xg_t * st,EffectList * ef)3929 static void conv_xg_auto_wah_od(struct effect_xg_t *st, EffectList *ef)
3930 {
3931 	InfoXGAutoWahOD *info = (InfoXGAutoWahOD *)ef->info;
3932 
3933 	info->lpf.freq = eq_freq_table_xg[clip_int(st->param_lsb[13], 34, 80)];
3934 	info->level = (double)st->param_lsb[14] / 127.0;
3935 }
3936 
conv_xg_auto_wah_od_eq3(struct effect_xg_t * st,EffectList * ef)3937 static void conv_xg_auto_wah_od_eq3(struct effect_xg_t *st, EffectList *ef)
3938 {
3939 	InfoEQ3 *info = (InfoEQ3 *)ef->info;
3940 
3941 	info->low_freq = eq_freq_table_xg[24];
3942 	info->low_gain = clip_int(st->param_lsb[11] - 64, -12, 12);
3943 	info->mid_freq = eq_freq_table_xg[41];
3944 	info->mid_gain = clip_int(st->param_lsb[12] - 64, -12, 12);
3945 	info->mid_width = 1.0;
3946 	info->high_freq = 0;
3947 	info->high_gain = 0;
3948 }
3949 
conv_xg_auto_wah_eq2(struct effect_xg_t * st,EffectList * ef)3950 static void conv_xg_auto_wah_eq2(struct effect_xg_t *st, EffectList *ef)
3951 {
3952 	InfoEQ2 *info = (InfoEQ2 *)ef->info;
3953 
3954 	info->low_freq = eq_freq_table_xg[clip_int(st->param_lsb[5], 4, 40)];
3955 	info->low_gain = clip_int(st->param_lsb[6] - 64, -12, 12);
3956 	info->high_freq = eq_freq_table_xg[clip_int(st->param_lsb[7], 28, 58)];
3957 	info->high_gain = clip_int(st->param_lsb[8] - 64, -12, 12);
3958 }
3959 
conv_xg_auto_wah(struct effect_xg_t * st,EffectList * ef)3960 static void conv_xg_auto_wah(struct effect_xg_t *st, EffectList *ef)
3961 {
3962 	InfoXGAutoWah *info = (InfoXGAutoWah *)ef->info;
3963 
3964 	info->lfo_freq = lfo_freq_table_xg[st->param_lsb[0]];
3965 	info->lfo_depth = st->param_lsb[1];
3966 	info->offset_freq = (double)(st->param_lsb[2]) * 3900.0 / 127.0 + 100.0;
3967 	info->resonance = (double)clip_int(st->param_lsb[3], 10, 120) / 10.0;
3968 	info->dry = calc_dry_xg(st->param_lsb[9], st);
3969 	info->wet = calc_wet_xg(st->param_lsb[9], st);
3970 	info->drive = st->param_lsb[10];
3971 }
3972 
calc_xg_auto_wah_freq(int32 lfo_val,double offset_freq,int8 depth)3973 static inline double calc_xg_auto_wah_freq(int32 lfo_val, double offset_freq, int8 depth)
3974 {
3975 	double freq;
3976 	int32 fine;
3977 	fine = ((lfo_val - (1L << 15)) * depth) >> 7;	/* max: +-2^8 fine */
3978 	if (fine >= 0) {
3979 		freq = offset_freq * bend_fine[fine & 0xff]
3980 			* bend_coarse[fine >> 8 & 0x7f];
3981 	} else {
3982 		freq = offset_freq / (bend_fine[(-fine) & 0xff]
3983 			* bend_coarse[(-fine) >> 8 & 0x7f]);
3984 	}
3985 	return freq;
3986 }
3987 
3988 #define XG_AUTO_WAH_BITS (32 - GUARD_BITS)
3989 #define XG_AUTO_WAH_MAX_NEG (1.0 / (double)(1L << XG_AUTO_WAH_BITS))
3990 
do_xg_auto_wah(int32 * buf,int32 count,EffectList * ef)3991 static void do_xg_auto_wah(int32 *buf, int32 count, EffectList *ef)
3992 {
3993 	int32 i, x, y, val;
3994 	InfoXGAutoWah *info = (InfoXGAutoWah *)ef->info;
3995 	filter_moog_dist *fil0 = &(info->fil0), *fil1 = &(info->fil1);
3996 	lfo *lfo = &(info->lfo);
3997 	int32 dryi = info->dryi, weti = info->weti, fil_cycle = info->fil_cycle;
3998 	int8 lfo_depth = info->lfo_depth;
3999 	double yf, offset_freq = info->offset_freq;
4000 	int32 fil_count = info->fil_count;
4001 
4002 	if(count == MAGIC_INIT_EFFECT_INFO) {
4003 		init_lfo(lfo, info->lfo_freq, LFO_TRIANGULAR, 0);
4004 		fil0->res_dB = fil1->res_dB = (info->resonance - 1.0) * 12.0 / 11.0;
4005 		fil0->dist = fil1->dist = 4.0 * sqrt((double)info->drive / 127.0);
4006 		val = do_lfo(lfo);
4007 		fil0->freq = fil1->freq = calc_xg_auto_wah_freq(val, info->offset_freq, info->lfo_depth);
4008 		calc_filter_moog_dist(fil0);
4009 		init_filter_moog_dist(fil0);
4010 		calc_filter_moog_dist(fil1);
4011 		init_filter_moog_dist(fil1);
4012 		info->fil_count = 0;
4013 		info->fil_cycle = (int32)(44.0 * play_mode->rate / 44100.0);
4014 		info->dryi = TIM_FSCALE(info->dry, 24);
4015 		info->weti = TIM_FSCALE(info->wet, 24);
4016 		return;
4017 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
4018 		return;
4019 	}
4020 
4021 	for (i = 0; i < count; i++)
4022 	{
4023 		x = y = buf[i];
4024 		yf = (double)y * XG_AUTO_WAH_MAX_NEG;
4025 		do_filter_moog_dist_band(&yf, fil0->f, fil0->p, fil0->q, fil0->d,
4026 								   &fil0->b0, &fil0->b1, &fil0->b2, &fil0->b3, &fil0->b4);
4027 		y = TIM_FSCALE(yf, XG_AUTO_WAH_BITS);
4028 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
4029 
4030 		x = y = buf[++i];
4031 		yf = (double)y * XG_AUTO_WAH_MAX_NEG;
4032 		do_filter_moog_dist_band(&yf, fil0->f, fil0->p, fil0->q, fil0->d,
4033 								   &fil1->b0, &fil1->b1, &fil1->b2, &fil1->b3, &fil1->b4);
4034 		y = TIM_FSCALE(yf, XG_AUTO_WAH_BITS);
4035 		buf[i] = imuldiv24(x, dryi) + imuldiv24(y, weti);
4036 
4037 		val = do_lfo(lfo);
4038 
4039 		if (++fil_count == fil_cycle) {
4040 			fil_count = 0;
4041 			fil0->freq = calc_xg_auto_wah_freq(val, offset_freq, lfo_depth);
4042 			calc_filter_moog_dist(fil0);
4043 		}
4044 	}
4045 	info->fil_count = fil_count;
4046 }
4047 
do_xg_auto_wah_od(int32 * buf,int32 count,EffectList * ef)4048 static void do_xg_auto_wah_od(int32 *buf, int32 count, EffectList *ef)
4049 {
4050 	int32 i, x;
4051 	InfoXGAutoWahOD *info = (InfoXGAutoWahOD *)ef->info;
4052 	filter_biquad *lpf = &(info->lpf);
4053 	int32 leveli = info->leveli;
4054 
4055 	if(count == MAGIC_INIT_EFFECT_INFO) {
4056 		lpf->q = 1.0;
4057 		calc_filter_biquad_low(lpf);
4058 		info->leveli = TIM_FSCALE(info->level, 24);
4059 		return;
4060 	} else if(count == MAGIC_FREE_EFFECT_INFO) {
4061 		return;
4062 	}
4063 
4064 	for (i = 0; i < count; i++)
4065 	{
4066 		x = buf[i];
4067 		do_filter_biquad(&x, lpf->a1, lpf->a2, lpf->b1, lpf->b02, &lpf->x1l, &lpf->x2l, &lpf->y1l, &lpf->y2l);
4068 		buf[i] = imuldiv24(x, leveli);
4069 
4070 		x = buf[++i];
4071 		do_filter_biquad(&x, lpf->a1, lpf->a2, lpf->b1, lpf->b02, &lpf->x1r, &lpf->x2r, &lpf->y1r, &lpf->y2r);
4072 		buf[i] = imuldiv24(x, leveli);
4073 	}
4074 }
4075 
4076 enum {	/* width * length * height */
4077 	ER_SMALL_ROOM,	/* 5m * 5m * 3m */
4078 	ER_MEDIUM_ROOM,	/* 10m * 10m * 5m */
4079 	ER_LARGE_ROOM,	/* 12m * 12m * 8m */
4080 	ER_MEDIUM_HALL,	/* 15m * 20m * 10m */
4081 	ER_LARGE_HALL,	/* 20m * 40m * 18m */
4082 	ER_EOF,
4083 };
4084 
4085 #if 0
4086 struct early_reflection_param_t {
4087 	int8 character;
4088 	int16 time_left[20];	/* in ms */
4089 	float weight_left[20];
4090 	int16 time_right[20];	/* in ms */
4091 	float weight_right[20];
4092 };
4093 
4094 static struct early_reflection_param_t early_reflection_param[] = {
4095 	{ ER_SMALL_ROOM,
4096 		{ 461, 487, 505, 530, 802, 818, 927, 941, 1047, 1058, 1068, 1070, 1076, 1096, 1192, 1203, 1236, 1261, 1321, 1344, },
4097 		{ 0.1594, 0.1328, 0.1197, 0.1025, 0.0285, 0.0267, 0.0182, 0.0173, 0.0098, 0.0121, 0.0092, 0.0116, 0.0090, 0.0085, 0.0084, 0.0081, 0.0059, 0.0055, 0.0048, 0.0045, },
4098 		{ 381, 524, 621, 636, 718, 803, 890, 906, 975, 1016, 1026, 1040, 1137, 1166, 1212, 1220, 1272, 1304, 1315, 1320,},
4099 		{ 0.1896, 0.0706, 0.0467, 0.0388, 0.0298, 0.0211, 0.0137, 0.0181, 0.0144, 0.0101, 0.0088, 0.0118, 0.0072, 0.0081, 0.0073, 0.0071, 0.0062, 0.0042, 0.0057, 0.0024, },
4100 	},
4101 	{ ER_MEDIUM_ROOM,
4102 		{ 461, 802, 927, 1047, 1236, 1321, 1345, 1468, 1497, 1568, 1609, 1642, 1713, 1744, 1768, 1828, 1835, 1864, 1893, 1923,},
4103 		{ 0.3273, 0.0586, 0.0374, 0.0201, 0.0120, 0.0098, 0.0152, 0.0090, 0.0109, 0.0095, 0.0068, 0.0034, 0.0073, 0.0041, 0.0027, 0.0024, 0.0059, 0.0034, 0.0054, 0.0035,},
4104 		{ 381, 636, 906, 1026, 1040, 1315, 1320, 1415, 1445, 1556, 1588, 1628, 1637, 1663, 1693, 1768, 1788, 1824, 1882, 1920,},
4105 		{ 0.1896, 0.0706, 0.0467, 0.0388, 0.0298, 0.0211, 0.0137, 0.0181, 0.0144, 0.0101, 0.0088, 0.0118, 0.0072, 0.0081, 0.0073, 0.0071, 0.0062, 0.0042, 0.0057, 0.0024, },
4106 	},
4107 	{ ER_LARGE_ROOM,
4108 		{ 577, 875, 1665, 1713, 1784, 1790, 1835, 1913, 1954, 2023, 2062, 2318, 2349, 2371, 2405, 2409, 2469, 2492, 2534, 2552, },
4109 		{ 0.2727, 0.0752, 0.0070, 0.0110, 0.0083, 0.0056, 0.0089, 0.0079, 0.0051, 0.0066, 0.0043, 0.0019, 0.0035, 0.0023, 0.0038, 0.0017, 0.0015, 0.0029, 0.0027, 0.0032,},
4110 		{ 381, 636, 1485, 1570, 1693, 1768, 1875, 1895, 1963, 2066, 2128, 2220, 2277, 2309, 2361, 2378, 2432, 2454, 2497, 2639,},
4111 		{ 0.5843, 0.1197, 0.0117, 0.0098, 0.0032, 0.0028, 0.0042, 0.0022, 0.0020, 0.0038, 0.0035, 0.0043, 0.0040, 0.0022, 0.0028, 0.0035, 0.0033, 0.0018, 0.0010, 0.0008, },
4112 	},
4113 	{ ER_MEDIUM_HALL,
4114 		{ 1713, 1835, 2534, 2618, 2780, 2849, 2857, 3000, 3071, 3159, 3226, 3338, 3349, 3407, 3413, 3465, 3594, 3669, 3714, 3728,},
4115 		{ 0.0146, 0.0118, 0.0036, 0.0033, 0.0033, 0.0030, 0.0031, 0.0013, 0.0012, 0.0023, 0.0021, 0.0018, 0.0016, 0.0015, 0.0016, 0.0016, 0.0015, 0.0013, 0.0006, 0.0012,},
4116 		{ 381, 636, 1693, 1768, 2066, 2128, 2362, 2416, 2454, 2644, 2693, 2881, 2890, 2926, 2957, 3036, 3185, 3328, 3385, 3456,},
4117 		{ 0.7744, 0.1586, 0.0042, 0.0037, 0.0051, 0.0046, 0.0036, 0.0034, 0.0024, 0.0018, 0.0017, 0.0024, 0.0015, 0.0023, 0.0008, 0.0012, 0.0013, 0.0005, 0.0012, 0.0005, },
4118 	},
4119 	{ ER_LARGE_HALL,
4120 		{ 1713, 1835, 2534, 2618, 3159, 3226, 3669, 3728, 4301, 4351, 4936, 5054, 5097, 5278, 5492, 5604, 5631, 5714, 5751, 5800,},
4121 		{ 0.0150, 0.0121, 0.0037, 0.0034, 0.0023, 0.0022, 0.0013, 0.0012, 0.0005, 0.0005, 0.0006, 0.0002, 0.0002, 0.0004, 0.0004, 0.0004, 0.0004, 0.0002, 0.0002, 0.0003,},
4122 		{ 381, 636, 1693, 1768, 2066, 2128, 2644, 2693, 3828, 3862, 4169, 4200, 4792, 5068, 5204, 5231, 5378, 5460, 5485, 5561,},
4123 		{ 0.7936, 0.1625, 0.0043, 0.0038, 0.0052, 0.0047, 0.0018, 0.0017, 0.0008, 0.0008, 0.0007, 0.0007, 0.0003, 0.0001, 0.0003, 0.0002, 0.0002, 0.0002, 0.0001, 0.0003, },
4124 	},
4125 	{ ER_EOF, { 0, }, { 0, }, { 0, }, { 0, }, },
4126 };
4127 #endif
4128 
4129 struct _EffectEngine effect_engine[] = {
4130 {	EFFECT_NONE, "None", NULL, NULL, NULL, 0,},
4131 {	EFFECT_STEREO_EQ, "Stereo-EQ", do_stereo_eq, conv_gs_stereo_eq, NULL, sizeof(InfoStereoEQ),},
4132 {	EFFECT_EQ2, "2-Band EQ", do_eq2, conv_gs_eq2, conv_xg_eq2, sizeof(InfoEQ2),},
4133 {	EFFECT_EQ3, "3-Band EQ", do_eq3, NULL, conv_xg_eq3, sizeof(InfoEQ3),},
4134 {	EFFECT_OVERDRIVE1, "Overdrive", do_overdrive1, conv_gs_overdrive1, NULL, sizeof(InfoOverdrive1),},
4135 {	EFFECT_DISTORTION1, "Distortion", do_distortion1, conv_gs_overdrive1, NULL, sizeof(InfoOverdrive1),},
4136 {	EFFECT_OD1OD2, "OD1/OD2", do_dual_od, conv_gs_dual_od, NULL, sizeof(InfoOD1OD2),},
4137 {	EFFECT_HEXA_CHORUS, "Hexa-Chorus", do_hexa_chorus, conv_gs_hexa_chorus, NULL, sizeof(InfoHexaChorus),},
4138 {	EFFECT_CHORUS, "Chorus", do_chorus, NULL, conv_xg_chorus, sizeof(InfoChorus),},
4139 {	EFFECT_FLANGER, "Flanger", do_chorus, NULL, conv_xg_flanger, sizeof(InfoChorus),},
4140 {	EFFECT_SYMPHONIC, "Symphonic", do_chorus, NULL, conv_xg_symphonic, sizeof(InfoChorus),},
4141 {	EFFECT_CHORUS_EQ3, "3-Band EQ (XG Chorus built-in)", do_eq3, NULL, conv_xg_chorus_eq3, sizeof(InfoEQ3),},
4142 {	EFFECT_STEREO_OVERDRIVE, "Stereo Overdrive", do_stereo_od, NULL, conv_xg_overdrive, sizeof(InfoStereoOD),},
4143 {	EFFECT_STEREO_DISTORTION, "Stereo Distortion", do_stereo_od, NULL, conv_xg_distortion, sizeof(InfoStereoOD),},
4144 {	EFFECT_STEREO_AMP_SIMULATOR, "Amp Simulator", do_stereo_od, NULL, conv_xg_amp_simulator, sizeof(InfoStereoOD),},
4145 {	EFFECT_OD_EQ3, "2-Band EQ (XG OD built-in)", do_eq3, NULL, conv_xg_od_eq3, sizeof(InfoEQ3),},
4146 {	EFFECT_DELAY_LCR, "Delay L,C,R", do_delay_lcr, NULL, conv_xg_delay_lcr, sizeof(InfoDelayLCR),},
4147 {	EFFECT_DELAY_LR, "Delay L,R", do_delay_lr, NULL, conv_xg_delay_lr, sizeof(InfoDelayLR),},
4148 {	EFFECT_ECHO, "Echo", do_echo, NULL, conv_xg_echo, sizeof(InfoEcho),},
4149 {	EFFECT_CROSS_DELAY, "Cross Delay", do_cross_delay, NULL, conv_xg_cross_delay, sizeof(InfoCrossDelay),},
4150 {	EFFECT_DELAY_EQ2, "2-Band EQ (XG Delay built-in)", do_eq2, NULL, conv_xg_delay_eq2, sizeof(InfoEQ2),},
4151 {	EFFECT_LOFI, "Lo-Fi", do_lofi, NULL, conv_xg_lofi, sizeof(InfoLoFi),},
4152 {	EFFECT_LOFI1, "Lo-Fi 1", do_lofi1, conv_gs_lofi1, NULL, sizeof(InfoLoFi1),},
4153 {	EFFECT_LOFI2, "Lo-Fi 2", do_lofi2, conv_gs_lofi2, NULL, sizeof(InfoLoFi2),},
4154 {	EFFECT_XG_AUTO_WAH, "Auto Wah", do_xg_auto_wah, NULL, conv_xg_auto_wah, sizeof(InfoXGAutoWah),},
4155 {	EFFECT_XG_AUTO_WAH_EQ2, "2-Band EQ (Auto Wah built-in)", do_eq2, NULL, conv_xg_auto_wah_eq2, sizeof(InfoEQ2),},
4156 {	EFFECT_XG_AUTO_WAH_OD, "OD (Auto Wah built-in)", do_xg_auto_wah_od, NULL, conv_xg_auto_wah_od, sizeof(InfoXGAutoWahOD),},
4157 {	EFFECT_XG_AUTO_WAH_OD_EQ3, "2-Band EQ (Auto Wah OD built-in)", do_eq3, NULL, conv_xg_auto_wah_od_eq3, sizeof(InfoEQ3),},
4158 {	-1, "EOF", NULL, NULL, NULL, 0, },
4159 };
4160 
4161 struct effect_parameter_xg_t effect_parameter_xg[] = {
4162 {	0, 0, "NO EFFECT",
4163 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
4164 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 0, },
4165 
4166 {	0x05, 0, "DELAY L,C,R", { 0x1A, 0x0D, 0x27, 0x27, 0, 0, 0, 0, 0, 0,},{
4167 	0x05, 0x03, 0x08, 0x08, 74, 100, 10, 0, 0, 32, 0, 0, 28, 64, 46, 64,}, 9,},
4168 
4169 {	0x06, 0, "DELAY L,R", { 0x13, 0x1D, 0x1D, 0x1D, 0, 0, 0, 0, 0, 0,},{
4170 	0x44, 0x26, 0x28, 0x26, 87, 10, 0, 0, 0, 32, 0, 0, 28, 64, 46, 64, },9,},
4171 
4172 {	0x07, 0, "ECHO", { 0x0D, 0, 0x0D, 0, 0, 0x0D, 0x0D, 0, 0, 0,},{
4173 	0x24, 80, 0x74, 80, 10, 0x24, 0x74, 0, 0, 40, 0, 0, 28, 64, 46, 64,}, 9,},
4174 
4175 {	0x08, 0, "CROSS DELAY", { 0x0D, 0x0D, 0, 0, 0, 0, 0, 0, 0, 0,},{
4176 	0x24, 0x56, 111, 1, 10, 0, 0, 0, 0, 32, 0, 0, 28, 64, 46, 64,}, 9,},
4177 
4178 {	0x41, 0, "CHORUS 1", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4179 	6, 54, 77, 106, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4180 
4181 {	0x41, 0x01, "CHORUS 2",{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4182 	8, 63, 64, 30, 0, 28, 62, 42, 58, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4183 
4184 {	0x41, 0x02, "CHORUS 3",{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4185 	4, 44, 64, 110, 0, 28, 64, 46, 66, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4186 
4187 {	0x41, 0x03, "GM CHORUS 1",{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4188 	9, 10, 64, 109, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4189 
4190 {	0x41, 0x04, "GM CHORUS 2", {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4191 	28, 34, 67, 105, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4192 
4193 {	0x41, 0x05, "GM CHORUS 3", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4194 	9, 34, 69, 105, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4195 
4196 {	0x41, 0x06, "GM CHORUS 4", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4197 	26, 29, 75, 102, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4198 
4199 {	0x41, 0x07, "FB CHORUS", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4200 	6, 43, 107, 111, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 0, 0,}, 9,},
4201 
4202 {	0x41, 0x08, "CHORUS 4", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4203 	9, 32, 69, 104, 0, 28, 64, 46, 64, 64, 46, 64, 10, 0, 1, 0,}, 9,},
4204 
4205 {	0x42, 0, "CELESTE 1", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4206 	12, 32, 64, 0, 0, 28, 64, 46, 64, 127, 40, 68, 10, 0, 0, 0,}, 9,},
4207 
4208 {	0x42, 0x01, "CELESTE 2", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4209 	28, 18, 90, 2, 0, 28, 62, 42, 60, 84, 40, 68, 10, 0, 0, 0,}, 9,},
4210 
4211 {	0x42, 0x02, "CELESTE 3", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4212 	4, 63, 44, 2, 0, 28, 64, 46, 68, 127, 40, 68, 10, 0, 0,0,}, 9,},
4213 
4214 {	0x42, 0x08, "CELESTE 4", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4215 	8, 29, 64, 0, 0, 28, 64, 51, 66, 127, 40, 68, 10, 0, 1, 0,}, 9,},
4216 
4217 {	0x43, 0, "FLANGER 1", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4218 	14, 14, 104, 2, 0, 28, 64, 46, 64, 96, 40, 64, 10, 4, 0, 0,}, 9, },
4219 
4220 {	0x43, 0x01, "FLANGER 2", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4221 	32, 17, 26, 2, 0, 28, 64, 46, 60, 96, 40, 64, 10, 4, 0, 0,}, 9, },
4222 
4223 {	0x43, 0x07, "GM FLANGER", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4224 	3, 21, 120, 1, 0, 28, 64, 46, 64, 96, 40, 64, 10, 4, 0, 0,}, 9, },
4225 
4226 {	0x43, 0x08, "FLANGER 3", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4227 	4, 109, 109, 2, 0, 28, 64, 46, 64, 127, 40, 64, 10, 4, 0, 0,}, 9, },
4228 
4229 {	0x44, 0, "SYMPHONIC", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4230 	12, 25, 16, 0, 0, 28, 64, 46, 64, 127, 46, 64, 10, 0, 0, 0,}, 9,},
4231 
4232 {	0x49, 0, "DISTORTION", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4233 	40, 20, 72, 53, 48, 0, 43, 74, 10, 127, 120, 0, 0, 0, 0,0,}, 0,},
4234 
4235 {	0x49, 0x08, "STEREO DISTORTION", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4236 	18, 27, 71, 48, 84, 0, 32, 66, 10, 127, 105, 0, 0, 0, 0, 0,}, 0,},
4237 
4238 {	0x4A, 0, "OVERDRIVE", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4239 	29, 24, 68, 45, 55, 0, 41, 72, 10, 127, 104, 0, 0, 0, 0, 0,}, 0,},
4240 
4241 {	0x4A, 0x08, "STEREO OVERDRIVE", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4242 	10, 24, 69, 46, 105, 0, 41, 66, 10, 127, 104, 0, 0, 0, 0, 0,}, 0,},
4243 
4244 {	0x4B, 0, "AMP SIMULATOR", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4245 	39, 1, 48, 55, 0, 0, 0, 0, 0, 127, 112, 0, 0, 0, 0, 0,}, 0,},
4246 
4247 {	0x4B, 0x08, "STEREO AMP SIMULATOR", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4248 	16, 2, 46, 119, 0, 0, 0, 0, 0, 127, 106, 0, 0, 0, 0, 0,}, 0,},
4249 
4250 {	0x4C, 0, "3-BAND EQ", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4251 	70, 34, 60, 10, 70, 28, 46, 0, 0, 127, 0, 0, 0, 0, 0, 0,}, -1,},
4252 
4253 {	0x4D, 0, "2-BAND EQ", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4254 	28, 70, 46, 70, 0, 0, 0, 0, 0, 127, 0, 0, 0, 0, 0, 0,}, -1,},
4255 
4256 {	0x4E, 0, "AUTO WAH", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4257 	70, 56, 39, 25, 0, 28, 66, 46, 64, 127, 0, 0, 0, 0, 0, 0,}, 2, },
4258 
4259 {	0x4E, 0x01, "AUTO WAH+DISTORTION", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4260 	40, 73, 26, 29, 0, 28, 66, 46, 64, 127, 30, 72, 74, 53, 48, 0,}, 2, },
4261 
4262 {	0x4E, 0x02, "AUTO WAH+OVERDRIVE", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4263 	48, 64, 32, 23, 0, 28, 66, 46, 64, 127, 29, 68, 72, 45, 55, 0,}, 2, },
4264 
4265 {	0x5E, 0, "LO-FI",{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4266 	2, 60, 6, 54, 5, 10, 1, 1, 0, 127, 0, 0, 0, 0, 1, 0,}, 9, },
4267 
4268 {	-1, -1, "EOF",{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},{
4269 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 0,},
4270 };
4271 
4272 struct effect_parameter_gs_t effect_parameter_gs[] = {
4273 {	0, 0, "None", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4274 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 0, 0,},
4275 {	0x01, 0x00, "Stereo-EQ", { 1, 0x45, 1, 0x34, 0x48, 0, 0x48, 0x38, 0, 0x48,
4276 	0, 0, 0, 0, 0, 0, 0, 0, 0, 127,}, 19, -1,},
4277 {	0x01, 0x10, "Overdrive",{  48, 1, 1, 0, 0, 0, 0, 0, 0, 0,
4278 	0, 0, 0, 0, 0, 0, 0x40, 0x40, 0x40, 96,}, 0, 18,},
4279 {	0x01, 0x11, "Distrotion",{  76, 3, 1, 0, 0, 0, 0, 0, 0, 0,
4280 	0, 0, 0, 0, 0, 0, 0x40, 0x38, 0x40, 84,}, 0, 18, },
4281 {	0x11, 0x03, "OD1/OD2",{  0, 48, 1, 1, 0, 1, 76, 3, 1, 0,
4282 	0, 0, 0, 0, 0, 0x40, 96, 0x40, 84, 127,}, 1, 6, },
4283 {	0x01, 0x40, "Hexa Chorus",{  0x18, 0x08, 127, 5, 66, 16, 0, 0, 0, 0,
4284 	0, 0, 0, 0, 0, 0, 0x40, 0x40, 64, 112,}, 1, 15, },
4285 {	0x01, 0x72, "Lo-Fi 1",{  2, 6, 2, 0, 0, 0, 0, 0, 0, 0,
4286 	0, 0, 0, 0, 0, 127, 0x40, 0x40, 64, 127,}, 15, 18, },
4287 {	0x01, 0x73, "Lo-Fi 2",{  2, 1, 0x20, 0, 64, 1, 127, 0, 0, 127,
4288 	0, 0, 127, 0, 1, 127, 0x40, 0x40, 64, 127,}, 3, 15, },
4289 {	-1, -1, "EOF",{  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4290 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 0, 0,},
4291 };
4292