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.h
28  *
29  */
30 #ifndef ___REVERB_H_
31 #define ___REVERB_H_
32 
33 #ifndef REVERB_PRIVATE
34 #define EXTERN extern
35 #else
36 #define EXTERN
37 #endif
38 
39 #define DEFAULT_REVERB_SEND_LEVEL 40
40 
41 extern int opt_reverb_control;
42 extern FLOAT_T reverb_predelay_factor;
43 extern FLOAT_T freeverb_scaleroom;
44 extern FLOAT_T freeverb_offsetroom;
45 
46 extern void set_dry_signal(int32 *, int32);
47 extern void set_dry_signal_xg(int32 *, int32, int32);
48 extern void mix_dry_signal(int32 *, int32);
49 extern void free_effect_buffers(void);
50 
51 /*                    */
52 /*  Effect Utitities  */
53 /*                    */
54 /*! simple delay */
55 typedef struct {
56 	int32 *buf, size, index;
57 } simple_delay;
58 
59 /*! Pink Noise Generator */
60 typedef struct {
61 	float b0, b1, b2, b3, b4, b5, b6;
62 } pink_noise;
63 
64 extern void init_pink_noise(pink_noise *);
65 extern float get_pink_noise(pink_noise *);
66 extern float get_pink_noise_light(pink_noise *);
67 
68 #ifndef SINE_CYCLE_LENGTH
69 #define SINE_CYCLE_LENGTH 1024
70 #endif
71 
72 /*! LFO */
73 typedef struct {
74 	int32 buf[SINE_CYCLE_LENGTH];
75 	int32 count, cycle;	/* in samples */
76 	int32 icycle;	/* proportional to (SINE_CYCLE_LENGTH / cycle) */
77 	int type;	/* current content of its buffer */
78 	double freq;	/* in Hz */
79 } lfo;
80 
81 enum {
82 	LFO_NONE = 0,
83 	LFO_SINE,
84 	LFO_TRIANGULAR,
85 };
86 
87 /*! modulated delay with allpass interpolation */
88 typedef struct {
89 	int32 *buf, size, rindex, windex, hist;
90 	int32 ndelay, depth;	/* in samples */
91 } mod_delay;
92 
93 /*! modulated allpass filter with allpass interpolation */
94 typedef struct {
95 	int32 *buf, size, rindex, windex, hist;
96 	int32 ndelay, depth;	/* in samples */
97 	double feedback;
98 	int32 feedbacki;
99 } mod_allpass;
100 
101 /*! Moog VCF (resonant IIR state variable filter) */
102 typedef struct {
103 	int16 freq, last_freq;	/* in Hz */
104 	double res_dB, last_res_dB; /* in dB */
105 	int32 f, q, p;	/* coefficients in fixed-point */
106 	int32 b0, b1, b2, b3, b4;
107 } filter_moog;
108 
109 /*! Moog VCF (resonant IIR state variable filter with distortion) */
110 typedef struct {
111 	int16 freq, last_freq;	/* in Hz */
112 	double res_dB, last_res_dB; /* in dB */
113 	double dist, last_dist, f, q, p, d, b0, b1, b2, b3, b4;
114 } filter_moog_dist;
115 
116 /*! LPF18 (resonant IIR lowpass filter with waveshaping) */
117 typedef struct {
118 	int16 freq, last_freq;	/* in Hz */
119 	double dist, res, last_dist, last_res; /* in linear */
120 	double ay1, ay2, aout, lastin, kres, value, kp, kp1h;
121 } filter_lpf18;
122 
123 /*! 1st order lowpass filter */
124 typedef struct {
125 	double a;
126 	int32 ai, iai;	/* coefficients in fixed-point */
127 	int32 x1l, x1r;
128 } filter_lowpass1;
129 
130 extern void init_filter_lowpass1(filter_lowpass1 *);
131 
132 /*! lowpass / highpass filter */
133 typedef struct {
134 	double freq, q, last_freq, last_q;
135 	int32 x1l, x2l, y1l, y2l, x1r, x2r, y1r, y2r;
136 	int32 a1, a2, b1, b02;
137 } filter_biquad;
138 
139 #ifndef PART_EQ_XG
140 #define PART_EQ_XG
141 /*! shelving filter */
142 typedef struct {
143 	double freq, gain, q;
144 	int32 x1l, x2l, y1l, y2l, x1r, x2r, y1r, y2r;
145 	int32 a1, a2, b0, b1, b2;
146 } filter_shelving;
147 
148 struct part_eq_xg {
149 	int8 bass, treble, bass_freq, treble_freq;
150 	filter_shelving basss, trebles;
151 	int8 valid;
152 };
153 #endif /* PART_EQ_XG */
154 
155 extern void calc_filter_shelving_high(filter_shelving *);
156 extern void calc_filter_shelving_low(filter_shelving *);
157 
158 /*! peaking filter */
159 typedef struct {
160 	double freq, gain, q;
161 	int32 x1l, x2l, y1l, y2l, x1r, x2r, y1r, y2r;
162 	int32 ba1, a2, b0, b2;
163 } filter_peaking;
164 
165 extern void calc_filter_peaking(filter_peaking *);
166 
167 /*! allpass filter */
168 typedef struct _allpass {
169 	int32 *buf, size, index;
170 	double feedback;
171 	int32 feedbacki;
172 } allpass;
173 
174 /*! comb filter */
175 typedef struct _comb {
176 	int32 *buf, filterstore, size, index;
177 	double feedback, damp1, damp2;
178 	int32 feedbacki, damp1i, damp2i;
179 } comb;
180 
181 /*                                  */
182 /*  Insertion and Variation Effect  */
183 /*                                  */
184 struct effect_xg_t {
185 	int8 use_msb, type_msb, type_lsb, param_lsb[16], param_msb[10],
186 		ret, pan, send_reverb, send_chorus, connection, part,
187 		mw_depth, bend_depth, cat_depth, ac1_depth, ac2_depth, cbc1_depth,
188 		cbc2_depth;
189 	struct _EffectList *ef;
190 };
191 
192 extern void do_insertion_effect_gs(int32*, int32);
193 extern void do_insertion_effect_xg(int32*, int32, struct effect_xg_t *);
194 extern void do_variation_effect1_xg(int32*, int32);
195 extern void init_ch_effect_xg(void);
196 
197 enum {
198 	EFFECT_NONE,
199 	EFFECT_EQ2,
200 	EFFECT_EQ3,
201 	EFFECT_STEREO_EQ,
202 	EFFECT_OVERDRIVE1,
203 	EFFECT_DISTORTION1,
204 	EFFECT_OD1OD2,
205 	EFFECT_CHORUS,
206 	EFFECT_FLANGER,
207 	EFFECT_SYMPHONIC,
208 	EFFECT_CHORUS_EQ3,
209 	EFFECT_STEREO_OVERDRIVE,
210 	EFFECT_STEREO_DISTORTION,
211 	EFFECT_STEREO_AMP_SIMULATOR,
212 	EFFECT_OD_EQ3,
213 	EFFECT_HEXA_CHORUS,
214 	EFFECT_DELAY_LCR,
215 	EFFECT_DELAY_LR,
216 	EFFECT_ECHO,
217 	EFFECT_CROSS_DELAY,
218 	EFFECT_DELAY_EQ2,
219 	EFFECT_LOFI,
220 	EFFECT_LOFI1,
221 	EFFECT_LOFI2,
222 	EFFECT_XG_AUTO_WAH,
223 	EFFECT_XG_AUTO_WAH_EQ2,
224 	EFFECT_XG_AUTO_WAH_OD,
225 	EFFECT_XG_AUTO_WAH_OD_EQ3,
226 };
227 
228 #define MAGIC_INIT_EFFECT_INFO -1
229 #define MAGIC_FREE_EFFECT_INFO -2
230 
231 EXTERN struct insertion_effect_gs_t {
232 	int32 type;
233 	int8 type_lsb, type_msb, parameter[20], send_reverb,
234 		send_chorus, send_delay, control_source1, control_depth1,
235 		control_source2, control_depth2, send_eq_switch;
236 	struct _EffectList *ef;
237 } insertion_effect_gs;
238 
239 enum {
240 	XG_CONN_INSERTION = 0,
241 	XG_CONN_SYSTEM = 1,
242 	XG_CONN_SYSTEM_CHORUS,
243 	XG_CONN_SYSTEM_REVERB,
244 };
245 
246 #define XG_INSERTION_EFFECT_NUM 2
247 #define XG_VARIATION_EFFECT_NUM 1
248 
249 EXTERN struct effect_xg_t insertion_effect_xg[XG_INSERTION_EFFECT_NUM],
250 	variation_effect_xg[XG_VARIATION_EFFECT_NUM], reverb_status_xg, chorus_status_xg;
251 
252 typedef struct _EffectList {
253 	int type;
254 	void *info;
255 	struct _EffectEngine *engine;
256 	struct _EffectList *next_ef;
257 } EffectList;
258 
259 struct _EffectEngine {
260 	int type;
261 	char *name;
262 	void (*do_effect)(int32 *, int32, struct _EffectList *);
263 	void (*conv_gs)(struct insertion_effect_gs_t *, struct _EffectList *);
264 	void (*conv_xg)(struct effect_xg_t *, struct _EffectList *);
265 	int info_size;
266 };
267 
268 extern struct _EffectEngine effect_engine[];
269 
270 struct effect_parameter_gs_t {
271 	int8 type_msb, type_lsb;
272 	char *name;
273 	int8 param[20];
274 	int8 control1, control2;
275 };
276 
277 extern struct effect_parameter_gs_t effect_parameter_gs[];
278 
279 struct effect_parameter_xg_t {
280 	int8 type_msb, type_lsb;
281 	char *name;
282 	int8 param_msb[10], param_lsb[16];
283 	int8 control;
284 };
285 
286 extern struct effect_parameter_xg_t effect_parameter_xg[];
287 
288 extern EffectList *push_effect(EffectList *, int);
289 extern void do_effect_list(int32 *, int32, EffectList *);
290 extern void free_effect_list(EffectList *);
291 
292 /*! 2-Band EQ */
293 typedef struct {
294     int16 low_freq, high_freq;		/* in Hz */
295 	int16 low_gain, high_gain;		/* in dB */
296 	filter_shelving hsf, lsf;
297 } InfoEQ2;
298 
299 /*! 3-Band EQ */
300 typedef struct {
301     int16 low_freq, high_freq, mid_freq;		/* in Hz */
302 	int16 low_gain, high_gain, mid_gain;		/* in dB */
303 	double mid_width;
304 	filter_shelving hsf, lsf;
305 	filter_peaking peak;
306 } InfoEQ3;
307 
308 /*! Stereo EQ */
309 typedef struct {
310     int16 low_freq, high_freq, m1_freq, m2_freq;		/* in Hz */
311 	int16 low_gain, high_gain, m1_gain, m2_gain;		/* in dB */
312 	double m1_q, m2_q, level;
313 	int32 leveli;
314 	filter_shelving hsf, lsf;
315 	filter_peaking m1, m2;
316 } InfoStereoEQ;
317 
318 /*! Overdrive 1 / Distortion 1 */
319 typedef struct {
320 	double level;
321 	int32 leveli, di;	/* in fixed-point */
322 	int8 drive, pan, amp_sw, amp_type;
323 	filter_moog svf;
324 	filter_biquad lpf1;
325 	void (*amp_sim)(int32 *, int32);
326 } InfoOverdrive1;
327 
328 /*! OD1 / OD2 */
329 typedef struct {
330 	double level, levell, levelr;
331 	int32 levelli, levelri, dli, dri;	/* in fixed-point */
332 	int8 drivel, driver, panl, panr, typel, typer, amp_swl, amp_swr, amp_typel, amp_typer;
333 	filter_moog svfl, svfr;
334 	filter_biquad lpf1;
335 	void (*amp_siml)(int32 *, int32), (*amp_simr)(int32 *, int32);
336 	void (*odl)(int32 *, int32), (*odr)(int32 *, int32);
337 } InfoOD1OD2;
338 
339 /*! HEXA-CHORUS */
340 typedef struct {
341 	simple_delay buf0;
342 	lfo lfo0;
343 	double dry, wet, level;
344 	int32 pdelay, depth;	/* in samples */
345 	int8 pdelay_dev, depth_dev, pan_dev;
346 	int32 dryi, weti;	/* in fixed-point */
347 	int32 pan0, pan1, pan2, pan3, pan4, pan5;
348 	int32 depth0, depth1, depth2, depth3, depth4, depth5,
349 		pdelay0, pdelay1, pdelay2, pdelay3, pdelay4, pdelay5;
350 	int32 spt0, spt1, spt2, spt3, spt4, spt5,
351 		hist0, hist1, hist2, hist3, hist4, hist5;
352 } InfoHexaChorus;
353 
354 /*! Plate Reverb */
355 typedef struct {
356 	simple_delay pd, od1l, od2l, od3l, od4l, od5l, od6l, od7l,
357 		od1r, od2r, od3r, od4r, od5r, od6r, od7r,
358 		td1, td2, td1d, td2d;
359 	lfo lfo1, lfo1d;
360 	allpass ap1, ap2, ap3, ap4, ap6, ap6d;
361 	mod_allpass ap5, ap5d;
362 	filter_lowpass1 lpf1, lpf2;
363 	int32 t1, t1d;
364 	double decay, ddif1, ddif2, idif1, idif2, dry, wet;
365 	int32 decayi, ddif1i, ddif2i, idif1i, idif2i, dryi, weti;
366 } InfoPlateReverb;
367 
368 /*! Standard Reverb */
369 typedef struct {
370 	int32 spt0, spt1, spt2, spt3, rpt0, rpt1, rpt2, rpt3;
371 	int32 ta, tb, HPFL, HPFR, LPFL, LPFR, EPFL, EPFR;
372 	simple_delay buf0_L, buf0_R, buf1_L, buf1_R, buf2_L, buf2_R, buf3_L, buf3_R;
373 	double fbklev, nmixlev, cmixlev, monolev, hpflev, lpflev, lpfinp, epflev, epfinp, width, wet;
374 	int32 fbklevi, nmixlevi, cmixlevi, monolevi, hpflevi, lpflevi, lpfinpi, epflevi, epfinpi, widthi, weti;
375 } InfoStandardReverb;
376 
377 /*! Freeverb */
378 #define numcombs 8
379 #define numallpasses 4
380 
381 typedef struct {
382 	simple_delay pdelay;
383 	double roomsize, roomsize1, damp, damp1, wet, wet1, wet2, width;
384 	comb combL[numcombs], combR[numcombs];
385 	allpass allpassL[numallpasses], allpassR[numallpasses];
386 	int32 wet1i, wet2i;
387 	int8 alloc_flag;
388 } InfoFreeverb;
389 
390 /*! 3-Tap Stereo Delay Effect */
391 typedef struct {
392 	simple_delay delayL, delayR;
393 	int32 size[3], index[3];
394 	double level[3], feedback, send_reverb;
395 	int32 leveli[3], feedbacki, send_reverbi;
396 } InfoDelay3;
397 
398 /*! Stereo Chorus Effect */
399 typedef struct {
400 	simple_delay delayL, delayR;
401 	lfo lfoL, lfoR;
402 	int32 wpt0, spt0, spt1, hist0, hist1;
403 	int32 rpt0, depth, pdelay;
404 	double level, feedback, send_reverb, send_delay;
405 	int32 leveli, feedbacki, send_reverbi, send_delayi;
406 } InfoStereoChorus;
407 
408 /*! Chorus */
409 typedef struct {
410 	simple_delay delayL, delayR;
411 	lfo lfoL, lfoR;
412 	int32 wpt0, spt0, spt1, hist0, hist1;
413 	int32 rpt0, depth, pdelay;
414 	double dry, wet, feedback, pdelay_ms, depth_ms, rate, phase_diff;
415 	int32 dryi, weti, feedbacki;
416 } InfoChorus;
417 
418 /*! Stereo Overdrive / Distortion */
419 typedef struct {
420 	double level, dry, wet, drive, cutoff;
421 	int32 dryi, weti, di;
422 	filter_moog svfl, svfr;
423 	filter_biquad lpf1;
424 	void (*od)(int32 *, int32);
425 } InfoStereoOD;
426 
427 /*! Delay L,C,R */
428 typedef struct {
429 	simple_delay delayL, delayR;
430 	int32 index[3], size[3];	/* L,C,R */
431 	double rdelay, ldelay, cdelay, fdelay;	/* in ms */
432 	double dry, wet, feedback, clevel, high_damp;
433 	int32 dryi, weti, feedbacki, cleveli;
434 	filter_lowpass1 lpf;
435 } InfoDelayLCR;
436 
437 /*! Delay L,R */
438 typedef struct {
439 	simple_delay delayL, delayR;
440 	int32 index[2], size[2];	/* L,R */
441 	double rdelay, ldelay, fdelay1, fdelay2;	/* in ms */
442 	double dry, wet, feedback, high_damp;
443 	int32 dryi, weti, feedbacki;
444 	filter_lowpass1 lpf;
445 } InfoDelayLR;
446 
447 /*! Echo */
448 typedef struct {
449 	simple_delay delayL, delayR;
450 	int32 index[2], size[2];	/* L1,R1 */
451 	double rdelay1, ldelay1, rdelay2, ldelay2;	/* in ms */
452 	double dry, wet, lfeedback, rfeedback, high_damp, level;
453 	int32 dryi, weti, lfeedbacki, rfeedbacki, leveli;
454 	filter_lowpass1 lpf;
455 } InfoEcho;
456 
457 /*! Cross Delay */
458 typedef struct {
459 	simple_delay delayL, delayR;
460 	double lrdelay, rldelay;	/* in ms */
461 	double dry, wet, feedback, high_damp;
462 	int32 dryi, weti, feedbacki, input_select;
463 	filter_lowpass1 lpf;
464 } InfoCrossDelay;
465 
466 /*! Lo-Fi 1 */
467 typedef struct {
468 	int8 lofi_type, pan, pre_filter, post_filter;
469 	double level, dry, wet;
470 	int32 bit_mask, level_shift, dryi, weti;
471 	filter_biquad pre_fil, post_fil;
472 } InfoLoFi1;
473 
474 /*! Lo-Fi 2 */
475 typedef struct {
476 	int8 wp_sel, disc_type, hum_type, ms, pan, rdetune, lofi_type, fil_type;
477 	double wp_level, rnz_lev, discnz_lev, hum_level, dry, wet, level;
478 	int32 bit_mask, level_shift, wp_leveli, rnz_levi, discnz_levi, hum_keveki, dryi, weti;
479 	filter_biquad fil, wp_lpf, hum_lpf, disc_lpf;
480 } InfoLoFi2;
481 
482 /*! LO-FI */
483 typedef struct {
484 	int8 output_gain, word_length, filter_type, bit_assign, emphasis;
485 	double dry, wet;
486 	int32 bit_mask, level_shift, dryi, weti;
487 	filter_biquad lpf, srf;
488 } InfoLoFi;
489 
490 /*! XG: Auto Wah */
491 typedef struct {
492 	int8 lfo_depth, drive;
493 	double resonance, lfo_freq, offset_freq, dry, wet;
494 	int32 dryi, weti, fil_count, fil_cycle;
495 	lfo lfo;
496 	filter_moog_dist fil0, fil1;
497 } InfoXGAutoWah;
498 
499 typedef struct {
500 	double level;
501 	int32 leveli;
502 	filter_biquad lpf;
503 } InfoXGAutoWahOD;
504 
505 /*                             */
506 /*        System Effect        */
507 /*                             */
508 /* Reverb Effect */
509 extern void do_ch_reverb(int32 *, int32);
510 extern void do_mono_reverb(int32 *, int32);
511 extern void set_ch_reverb(int32 *, int32, int32);
512 extern void init_reverb(void);
513 extern void reverb_rc_event(int, int32);
514 extern void do_ch_reverb_xg(int32 *, int32);
515 
516 /* Chorus Effect */
517 extern void do_ch_chorus(int32 *, int32);
518 extern void set_ch_chorus(int32 *, int32, int32);
519 extern void init_ch_chorus(void);
520 extern void do_ch_chorus_xg(int32 *, int32);
521 
522 /* Delay (Celeste) Effect */
523 extern void do_ch_delay(int32 *, int32);
524 extern void set_ch_delay(int32 *, int32, int32);
525 extern void init_ch_delay(void);
526 
527 /* EQ */
528 extern void init_eq_gs(void);
529 extern void set_ch_eq_gs(int32 *, int32);
530 extern void do_ch_eq_gs(int32 *, int32);
531 extern void do_ch_eq_xg(int32 *, int32, struct part_eq_xg *);
532 extern void do_multi_eq_xg(int32 *, int32);
533 
534 /* GS parameters of reverb effect */
535 EXTERN struct reverb_status_gs_t
536 {
537 	/* GS parameters */
538 	int8 character, pre_lpf, level, time, delay_feedback, pre_delay_time;
539 
540 	InfoStandardReverb info_standard_reverb;
541 	InfoPlateReverb info_plate_reverb;
542 	InfoFreeverb info_freeverb;
543 	InfoDelay3 info_reverb_delay;
544 	filter_lowpass1 lpf;
545 } reverb_status_gs;
546 
547 struct chorus_text_gs_t
548 {
549     int status;
550     uint8 voice_reserve[18], macro[3], pre_lpf[3], level[3], feed_back[3],
551 		delay[3], rate[3], depth[3], send_level[3];
552 };
553 
554 /* GS parameters of chorus effect */
555 EXTERN struct chorus_status_gs_t
556 {
557 	/* GS parameters */
558 	int8 macro, pre_lpf, level, feedback, delay, rate, depth, send_reverb, send_delay;
559 
560 	struct chorus_text_gs_t text;
561 
562 	InfoStereoChorus info_stereo_chorus;
563 	filter_lowpass1 lpf;
564 } chorus_status_gs;
565 
566 /* GS parameters of delay effect */
567 EXTERN struct delay_status_gs_t
568 {
569 	/* GS parameters */
570 	int8 type, level, level_center, level_left, level_right,
571 		feedback, pre_lpf, send_reverb, time_c, time_l, time_r;
572     double time_center;			/* in ms */
573     double time_ratio_left, time_ratio_right;		/* in pct */
574 
575 	/* for pre-calculation */
576 	int32 sample[3];	/* center, left, right */
577 	double level_ratio[3];	/* center, left, right */
578 	double feedback_ratio, send_reverb_ratio;
579 
580 	filter_lowpass1 lpf;
581 	InfoDelay3 info_delay;
582 } delay_status_gs;
583 
584 /* GS parameters of channel EQ */
585 EXTERN struct eq_status_gs_t
586 {
587 	/* GS parameters */
588     int8 low_freq, high_freq, low_gain, high_gain;
589 
590 	filter_shelving hsf, lsf;
591 } eq_status_gs;
592 
593 /* XG parameters of Multi EQ */
594 EXTERN struct multi_eq_xg_t
595 {
596 	/* XG parameters */
597 	int8 type, gain1, gain2, gain3, gain4, gain5,
598 		freq1, freq2, freq3, freq4, freq5,
599 		q1, q2, q3, q4, q5, shape1, shape5;
600 
601 	int8 valid, valid1, valid2, valid3, valid4, valid5;
602 	filter_shelving eq1s, eq5s;
603 	filter_peaking eq1p, eq2p, eq3p, eq4p, eq5p;
604 } multi_eq_xg;
605 
606 EXTERN pink_noise global_pink_noise_light;
607 
608 #undef EXTERN
609 
610 #endif /* ___REVERB_H_ */
611