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