1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2004 Masanao Izumo <iz@onicos.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     effect.c - To apply sound effects.
21     Mainly written by Masanao Izumo <iz@onicos.co.jp>
22 
23     Interfaces:
24     void init_effect(void);
25     do_effect(int32* buf, int32 count);
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif /* HAVE_CONFIG_H */
31 
32 #ifndef NO_STRING_H
33 #include <string.h>
34 #else
35 #include <strings.h>
36 #endif
37 
38 #include <stdlib.h>
39 
40 #include "mt19937ar.h"
41 
42 #include "timidity.h"
43 #include "instrum.h"
44 #include "playmidi.h"
45 #include "output.h"
46 #include "reverb.h"
47 
48 #define SIDE_CONTI_SEC	10
49 #define CHANGE_SEC		2.0
50 
51 #define NS_AMP_MAX	((int32) 0x0fffffff)
52 #define NS_AMP_MIN	((int32)-0x0fffffff)
53 
54 static void effect_left_right_delay(int32 *, int32);
55 static void init_mtrand(void);
56 static void init_ns_tap(void);
57 static void init_ns_tap16(void);
58 static void ns_shaping8(int32 *, int32);
59 static void ns_shaping16(int32 *, int32);
60 static void ns_shaping16_trad(int32 *, int32);
61 static void do_soft_clipping1(int32 *, int32);
62 static void do_soft_clipping2(int32 *, int32);
63 static void ns_shaping16_9(int32 *, int32);
64 static inline unsigned long frand(void);
65 static inline int32 my_mod(int32, int32);
66 
67 static int32 ns_z0[4];
68 static int32 ns_z1[4];
69 static const int32 ns9_order = 9;
70 static int32 ns9_histposl, ns9_histposr;
71 static int32 ns9_ehl[18] = {0};
72 static int32 ns9_ehr[18] = {0};
73 //static int32 ns9_ehl[18];
74 //static int32 ns9_ehr[18];
75 static uint32 ns9_r1l, ns9_r2l, ns9_r1r, ns9_r2r;
76 static const float ns9_coef[9] = {
77 	2.412f, -3.370f, 3.937f, -4.174f, 3.353f,
78 	-2.205f, 1.281f, -0.569f, 0.0847f
79 };
80 static int32 ns9_c[9];
81 int noise_sharp_type = 4;
82 
init_effect(void)83 void init_effect(void)
84 {
85 	effect_left_right_delay(NULL, 0);
86 	init_mtrand();
87 	init_pink_noise(&global_pink_noise_light);
88 	init_ns_tap();
89 	init_reverb();
90 	init_ch_delay();
91 	init_ch_chorus();
92 	init_eq_gs();
93 }
94 
95 /*
96  * Left & Right Delay Effect
97  */
effect_left_right_delay(int32 * buff,int32 count)98 static void effect_left_right_delay(int32 *buff, int32 count)
99 {
100 	static int32 prev[AUDIO_BUFFER_SIZE * 2];
101 	int32 save[AUDIO_BUFFER_SIZE * 2];
102 	int32 pi, i, j, k, v, backoff;
103 	int b;
104 	static int turn_counter = 0, tc;
105 	static int status;
106 	static double rate0, rate1, dr;
107 	int32 *p;
108 
109 	if (buff == NULL) {
110 		memset(prev, 0, sizeof(prev));
111 		return;
112 	}
113 	if (play_mode->encoding & PE_MONO)
114 		return;
115 	if (effect_lr_mode == 0 || effect_lr_mode == 1 || effect_lr_mode == 2)
116 		b = effect_lr_mode;
117 	else
118 		return;
119 	count *= 2;
120 	backoff = 2 * (int) (play_mode->rate * effect_lr_delay_msec / 1000.0);
121 	if (backoff == 0)
122 		return;
123 	if (backoff > count)
124 		backoff = count;
125 	if (count < audio_buffer_size * 2) {
126 		memset(buff + count, 0, 4 * (audio_buffer_size * 2 - count));
127 		count = audio_buffer_size * 2;
128 	}
129 	memcpy(save, buff, 4 * count);
130 	pi = count - backoff;
131 	if (b == 2) {
132 		if (turn_counter == 0) {
133 			turn_counter = SIDE_CONTI_SEC * play_mode->rate;
134 			/* status: 0 -> 2 -> 3 -> 1 -> 4 -> 5 -> 0 -> ...
135 			 * status left   right
136 			 * 0      -      +		(right)
137 			 * 1      +      -		(left)
138 			 * 2     -> +    +		(right -> center)
139 			 * 3      +     -> -	(center -> left)
140 			 * 4     -> -    -		(left -> center)
141 			 * 5      -     -> +	(center -> right)
142 			 */
143 			status = 0;
144 			tc = 0;
145 		}
146 		p = prev;
147 		for (i = 0; i < count; i += 2, pi += 2) {
148 			if (i < backoff)
149 				p = prev;
150 			else if (p == prev) {
151 				pi = 0;
152 				p = save;
153 			}
154 			if (status < 2)
155 				buff[i + status] = p[pi + status];
156 			else if (status < 4) {
157 				j = (status & 1);
158 				v = (int32) (rate0 * buff[i + j] + rate1 * p[pi + j]);
159 				buff[i + j] = v;
160 				rate0 += dr, rate1 -= dr;
161 			} else {
162 				j = (status & 1);
163 				k = ! j;
164 				v = (int32) (rate0 * buff[i + j] + rate1 * p[pi + j]);
165 				buff[i + j] = v;
166 				buff[i + k] = p[pi + k];
167 				rate0 += dr, rate1 -= dr;
168 			}
169 			tc++;
170 			if (tc == turn_counter) {
171 				tc = 0;
172 				switch (status) {
173 				case 0:
174 					status = 2;
175 					turn_counter = (CHANGE_SEC / 2.0) * play_mode->rate;
176 					rate0 = 0.0;
177 					rate1 = 1.0;
178 					dr = 1.0 / turn_counter;
179 					break;
180 				case 2:
181 					status = 3;
182 					rate0 = 1.0;
183 					rate1 = 0.0;
184 					dr = -1.0 / turn_counter;
185 					break;
186 				case 3:
187 					status = 1;
188 					turn_counter = SIDE_CONTI_SEC * play_mode->rate;
189 					break;
190 				case 1:
191 					status = 4;
192 					turn_counter = (CHANGE_SEC / 2.0) * play_mode->rate;
193 					rate0 = 1.0;
194 					rate1 = 0.0;
195 					dr = -1.0 / turn_counter;
196 					break;
197 				case 4:
198 					status = 5;
199 					turn_counter = (CHANGE_SEC / 2.0) * play_mode->rate;
200 					rate0 = 0.0;
201 					rate1 = 1.0;
202 					dr = 1.0 / turn_counter;
203 					break;
204 				case 5:
205 					status = 0;
206 					turn_counter = SIDE_CONTI_SEC * play_mode->rate;
207 					break;
208 				}
209 			}
210 		}
211 	} else {
212 		for (i = 0; i < backoff; i += 2, pi += 2)
213 			buff[b + i] = prev[b + pi];
214 		for (pi = 0; i < count; i += 2, pi += 2)
215 			buff[b + i] = save[b + pi];
216 	}
217 	memcpy(prev + count - backoff, save + count - backoff, 4 * backoff);
218 }
219 
init_mtrand(void)220 static void init_mtrand(void)
221 {
222 	unsigned long init[4] = { 0x123, 0x234, 0x345, 0x456 };
223 	unsigned long length = 4;
224 	init_by_array(init, length);
225 }
226 
227 /* Noise Shaping filter from
228  * Kunihiko IMAI <imai@leo.ec.t.kanazawa-u.ac.jp>
229  * (Modified by Masanao Izumo <mo@goice.co.jp>)
230  */
init_ns_tap(void)231 static void init_ns_tap(void)
232 {
233 	memset(ns_z0, 0, sizeof(ns_z0));
234 	memset(ns_z1, 0, sizeof(ns_z1));
235 	if (play_mode->encoding & PE_16BIT)
236 		init_ns_tap16();
237 }
238 
init_ns_tap16(void)239 static void init_ns_tap16(void)
240 {
241 	int i;
242 
243 	for (i = 0; i < ns9_order; i++)
244 		ns9_c[i] = TIM_FSCALE(ns9_coef[i], 24);
245 	memset(ns9_ehl, 0, sizeof(ns9_ehl));
246 	memset(ns9_ehr, 0, sizeof(ns9_ehr));
247 	ns9_histposl = ns9_histposr = 8;
248 	ns9_r1l = ns9_r2l = ns9_r1r = ns9_r2r = 0;
249 }
250 
do_effect(int32 * buf,int32 count)251 void do_effect(int32 *buf, int32 count)
252 {
253 	int32 nsamples = (play_mode->encoding & PE_MONO)
254 			? count : count * 2;
255 	int reverb_level = (opt_reverb_control < 0)
256 			? -opt_reverb_control & 0x7f : DEFAULT_REVERB_SEND_LEVEL;
257 
258 	/* reverb in mono */
259 	if (opt_reverb_control && play_mode->encoding & PE_MONO)
260 		do_mono_reverb(buf, count);
261 	/* for static reverb / chorus level */
262 	if (opt_reverb_control == 2 || opt_reverb_control == 4
263 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x80))
264 			|| opt_chorus_control < 0) {
265 		set_dry_signal(buf, nsamples);
266 		/* chorus sounds horrible
267 		 * if applied globally on top of channel chorus
268 		 */
269 #if 0
270 		if (opt_chorus_control < 0)
271 			set_ch_chorus(buf, nsamples, -opt_chorus_control);
272 #endif
273 		if (opt_reverb_control == 2 || opt_reverb_control == 4
274 			|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x80)))
275 			set_ch_reverb(buf, nsamples, reverb_level);
276 		mix_dry_signal(buf, nsamples);
277 			/* chorus sounds horrible
278 			 * if applied globally on top of channel chorus
279 			 */
280 #if 0
281 		if (opt_chorus_control < 0 && !opt_surround_chorus)
282 			do_ch_chorus(buf, nsamples);
283 #endif
284 		if (opt_reverb_control == 2 || opt_reverb_control == 4
285 				|| (opt_reverb_control < 0 && ! (opt_reverb_control & 0x80)))
286 			do_ch_reverb(buf, nsamples);
287 	}
288 	/* L/R Delay */
289 	effect_left_right_delay(buf, count);
290 	/* Noise shaping filter must apply at last */
291 	if (play_mode->encoding & PE_24BIT) {}
292 	else if (! (play_mode->encoding & (PE_16BIT | PE_ULAW | PE_ALAW)))
293 		ns_shaping8(buf, count);
294 	else if (play_mode->encoding & PE_16BIT)
295 		ns_shaping16(buf, count);
296 }
297 
ns_shaping8(int32 * lp,int32 c)298 static void ns_shaping8(int32 *lp, int32 c)
299 {
300 	int32 l, i, ll;
301 	int32 ns_tap_0, ns_tap_1, ns_tap_2, ns_tap_3;
302 
303 	switch (noise_sharp_type) {
304 	case 1:
305 		ns_tap_0 = 1;
306 		ns_tap_1 = 0;
307 		ns_tap_2 = 0;
308 		ns_tap_3 = 0;
309 		break;
310 	case 2:
311 		ns_tap_0 = -2;
312 		ns_tap_1 = 1;
313 		ns_tap_2 = 0;
314 		ns_tap_3 = 0;
315 		break;
316 	case 3:
317 		ns_tap_0 = 3;
318 		ns_tap_1 = -3;
319 		ns_tap_2 = 1;
320 		ns_tap_3 = 0;
321 		break;
322 	case 4:
323 		ns_tap_0 = -4;
324 		ns_tap_1 = 6;
325 		ns_tap_2 = -4;
326 		ns_tap_3 = 1;
327 		break;
328 	default:
329 		return;
330 	}
331 	if (! (play_mode->encoding & PE_MONO))
332 		c *= 2;
333 	for (i = 0; i < c; i++) {
334 		/* applied noise-shaping filter */
335 		if (lp[i] > NS_AMP_MAX)
336 			lp[i] = NS_AMP_MAX;
337 		else if (lp[i] < NS_AMP_MIN)
338 			lp[i] = NS_AMP_MIN;
339 		ll = lp[i] + ns_tap_0 * ns_z0[0] + ns_tap_1 * ns_z0[1]
340 				+ ns_tap_2 * ns_z0[2] + ns_tap_3 * ns_z0[3];
341 		l = ll >> (32 - 8 - GUARD_BITS);
342 		lp[i] = l << (32 - 8 - GUARD_BITS);
343 		ns_z0[3] = ns_z0[2], ns_z0[2] = ns_z0[1], ns_z0[1] = ns_z0[0];
344 		ns_z0[0] = ll - l * (1U << (32 - 8 - GUARD_BITS));
345 		if (play_mode->encoding & PE_MONO)
346 			continue;
347 		i++;
348 		if (lp[i] > NS_AMP_MAX)
349 			lp[i] = NS_AMP_MAX;
350 		else if (lp[i] < NS_AMP_MIN)
351 			lp[i] = NS_AMP_MIN;
352 		ll = lp[i] + ns_tap_0 * ns_z1[0] + ns_tap_1 * ns_z1[1]
353 				+ ns_tap_2 * ns_z1[2] + ns_tap_3 * ns_z1[3];
354 		l = ll >> (32 - 8 - GUARD_BITS);
355 		lp[i] = l << (32 - 8 - GUARD_BITS);
356 		ns_z1[3] = ns_z1[2], ns_z1[2] = ns_z1[1], ns_z1[1] = ns_z1[0];
357 		ns_z1[0] = ll - l * (1U << (32 - 8 - GUARD_BITS));
358 	}
359 }
360 
ns_shaping16(int32 * lp,int32 c)361 static void ns_shaping16(int32 *lp, int32 c)
362 {
363 	if (! (play_mode->encoding & PE_MONO))
364 		c *= 2;
365 	switch (noise_sharp_type) {
366 	case 1:
367 		ns_shaping16_trad(lp, c);
368 		break;
369 	case 2:
370 		do_soft_clipping1(lp, c);
371 		ns_shaping16_9(lp, c);
372 		break;
373 	case 3:
374 		do_soft_clipping2(lp, c);
375 		ns_shaping16_9(lp, c);
376 		break;
377 	case 4:
378 		ns_shaping16_9(lp, c);
379 		break;
380 	default:
381 		break;
382 	}
383 }
384 
ns_shaping16_trad(int32 * lp,int32 c)385 static void ns_shaping16_trad(int32 *lp, int32 c)
386 {
387 	int32 l, i, ll;
388 	int32 ns_tap_0, ns_tap_1, ns_tap_2, ns_tap_3;
389 
390 	ns_tap_0 = -4;
391 	ns_tap_1 = 6;
392 	ns_tap_2 = -4;
393 	ns_tap_3 = 1;
394 	for (i = 0; i < c; i++) {
395 		/* applied noise-shaping filter */
396 		if (lp[i] > NS_AMP_MAX)
397 			lp[i] = NS_AMP_MAX;
398 		else if (lp[i] < NS_AMP_MIN)
399 			lp[i] = NS_AMP_MIN;
400 		ll = lp[i] + ns_tap_0 * ns_z0[0] + ns_tap_1 * ns_z0[1]
401 				+ ns_tap_2 * ns_z0[2] + ns_tap_3 * ns_z0[3];
402 		l = ll >> (32 - 16 - GUARD_BITS);
403 		lp[i] = l << (32 - 16 - GUARD_BITS);
404 		ns_z0[3] = ns_z0[2], ns_z0[2] = ns_z0[1], ns_z0[1] = ns_z0[0];
405 		ns_z0[0] = ll - l * (1U << (32 - 16 - GUARD_BITS));
406 		if (play_mode->encoding & PE_MONO)
407 			continue;
408 		i++;
409 		if (lp[i] > NS_AMP_MAX)
410 			lp[i] = NS_AMP_MAX;
411 		else if (lp[i] < NS_AMP_MIN)
412 			lp[i] = NS_AMP_MIN;
413 		ll = lp[i] + ns_tap_0 * ns_z1[0] + ns_tap_1 * ns_z1[1]
414 				+ ns_tap_2 * ns_z1[2] + ns_tap_3 * ns_z1[3];
415 		l = ll >> (32 - 16 - GUARD_BITS);
416 		lp[i] = l << (32 - 16 - GUARD_BITS);
417 		ns_z1[3] = ns_z1[2], ns_z1[2] = ns_z1[1], ns_z1[1] = ns_z1[0];
418 		ns_z1[0] = ll - l * (1U << (32 - 16 - GUARD_BITS));
419 	}
420 }
421 
422 #define WS_AMP_MAX	((int32) 0x0fffffff)
423 #define WS_AMP_MIN	((int32)-0x0fffffff)
424 
do_soft_clipping1(int32 * buf,int32 count)425 static void do_soft_clipping1(int32 *buf, int32 count)
426 {
427 	int32 i, x;
428 	int32 ai = TIM_FSCALE(1.5f, 24), bi = TIM_FSCALE(0.5f, 24);
429 
430 	for (i = 0; i < count; i++) {
431 		x = buf[i];
432 		x = (x > WS_AMP_MAX) ? WS_AMP_MAX
433 				: ((x < WS_AMP_MIN) ? WS_AMP_MIN : x);
434 		x = imuldiv24(x, ai) - imuldiv24(imuldiv28(imuldiv28(x, x), x), bi);
435 		buf[i] = x;
436 	}
437 }
438 
do_soft_clipping2(int32 * buf,int32 count)439 static void do_soft_clipping2(int32 *buf, int32 count)
440 {
441 	int32 i, x;
442 
443 	for (i = 0; i < count; i++) {
444 		x = buf[i];
445 		x = (x > WS_AMP_MAX) ? WS_AMP_MAX
446 				: ((x < WS_AMP_MIN) ? WS_AMP_MIN : x);
447 		x = signlong(x) * ((abs(x) << 1) - imuldiv28(x, x));
448 		buf[i] = x;
449 	}
450 }
451 
ns_shaping16_9(int32 * lp,int32 c)452 static void ns_shaping16_9(int32 *lp, int32 c)
453 {
454 	int32 i, l, sample, output;
455 
456 	for (i = 0; i < c; i++) {
457 		/* left channel */
458 		ns9_r2l = ns9_r1l;
459 		ns9_r1l = frand();
460 		lp[i] = (lp[i] > NS_AMP_MAX) ? NS_AMP_MAX
461 				: (lp[i] < NS_AMP_MIN) ? NS_AMP_MIN : lp[i];
462 #if OPT_MODE != 0
463 		sample = lp[i] - imuldiv24(ns9_c[8], ns9_ehl[ns9_histposl + 8])
464 				- imuldiv24(ns9_c[7], ns9_ehl[ns9_histposl + 7])
465 				- imuldiv24(ns9_c[6], ns9_ehl[ns9_histposl + 6])
466 				- imuldiv24(ns9_c[5], ns9_ehl[ns9_histposl + 5])
467 				- imuldiv24(ns9_c[4], ns9_ehl[ns9_histposl + 4])
468 				- imuldiv24(ns9_c[3], ns9_ehl[ns9_histposl + 3])
469 				- imuldiv24(ns9_c[2], ns9_ehl[ns9_histposl + 2])
470 				- imuldiv24(ns9_c[1], ns9_ehl[ns9_histposl + 1])
471 				- imuldiv24(ns9_c[0], ns9_ehl[ns9_histposl]);
472 #else
473 		sample = lp[i] - ns9_coef[8] * ns9_ehl[ns9_histposl + 8]
474 				- ns9_coef[7] * ns9_ehl[ns9_histposl + 7]
475 				- ns9_coef[6] * ns9_ehl[ns9_histposl + 6]
476 				- ns9_coef[5] * ns9_ehl[ns9_histposl + 5]
477 				- ns9_coef[4] * ns9_ehl[ns9_histposl + 4]
478 				- ns9_coef[3] * ns9_ehl[ns9_histposl + 3]
479 				- ns9_coef[2] * ns9_ehl[ns9_histposl + 2]
480 				- ns9_coef[1] * ns9_ehl[ns9_histposl + 1]
481 				- ns9_coef[0] * ns9_ehl[ns9_histposl];
482 #endif
483 		l = sample >> (32 - 16 - GUARD_BITS);
484 		output = l * (1U << (32 - 16 - GUARD_BITS))
485 				+ ((ns9_r1l - ns9_r2l) >> 30);
486 		ns9_histposl = my_mod((ns9_histposl + 8), ns9_order);
487 		ns9_ehl[ns9_histposl + 9] = ns9_ehl[ns9_histposl] = output - sample;
488 		lp[i] = output;
489 		/* right channel */
490 		i++;
491 		ns9_r2r = ns9_r1r;
492 		ns9_r1r = frand();
493 		lp[i] = (lp[i] > NS_AMP_MAX) ? NS_AMP_MAX
494 				: (lp[i] < NS_AMP_MIN) ? NS_AMP_MIN : lp[i];
495 #if OPT_MODE != 0
496 		sample = lp[i] - imuldiv24(ns9_c[8], ns9_ehr[ns9_histposr + 8])
497 				- imuldiv24(ns9_c[7], ns9_ehr[ns9_histposr + 7])
498 				- imuldiv24(ns9_c[6], ns9_ehr[ns9_histposr + 6])
499 				- imuldiv24(ns9_c[5], ns9_ehr[ns9_histposr + 5])
500 				- imuldiv24(ns9_c[4], ns9_ehr[ns9_histposr + 4])
501 				- imuldiv24(ns9_c[3], ns9_ehr[ns9_histposr + 3])
502 				- imuldiv24(ns9_c[2], ns9_ehr[ns9_histposr + 2])
503 				- imuldiv24(ns9_c[1], ns9_ehr[ns9_histposr + 1])
504 				- imuldiv24(ns9_c[0], ns9_ehr[ns9_histposr]);
505 #else
506 		sample = lp[i] - ns9_coef[8] * ns9_ehr[ns9_histposr + 8]
507 				- ns9_coef[7] * ns9_ehr[ns9_histposr + 7]
508 				- ns9_coef[6] * ns9_ehr[ns9_histposr + 6]
509 				- ns9_coef[5] * ns9_ehr[ns9_histposr + 5]
510 				- ns9_coef[4] * ns9_ehr[ns9_histposr + 4]
511 				- ns9_coef[3] * ns9_ehr[ns9_histposr + 3]
512 				- ns9_coef[2] * ns9_ehr[ns9_histposr + 2]
513 				- ns9_coef[1] * ns9_ehr[ns9_histposr + 1]
514 				- ns9_coef[0] * ns9_ehr[ns9_histposr];
515 #endif
516 		l = sample >> (32 - 16 - GUARD_BITS);
517 		output = l * (1U << (32 - 16 - GUARD_BITS))
518 				+ ((ns9_r1r - ns9_r2r) >> 30);
519 		ns9_histposr = my_mod((ns9_histposr + 8), ns9_order);
520 		ns9_ehr[ns9_histposr + 9] = ns9_ehr[ns9_histposr] = output - sample;
521 		lp[i] = output;
522 	}
523 }
524 
frand(void)525 static inline unsigned long frand(void)
526 {
527 	return genrand_int32();
528 }
529 
my_mod(int32 x,int32 n)530 static inline int32 my_mod(int32 x, int32 n)
531 {
532 	if (x >= n)
533 		x -= n;
534 	return x;
535 }
536 
537