1 /*
2     TiMidity -- Experimental MIDI to WAVE converter
3     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19     playmidi.c -- random stuff in need of rearrangement
20 */
21 
22 #include "config.h"
23 
24 #include "common.h"
25 #include "instrum.h"
26 #include "playmidi.h"
27 #include "loadmidi.h"
28 #include "mix.h"
29 #include "ctrlmode.h"
30 #include "timidity.h"
31 #include "tables.h"
32 
33 
34 extern resample_t *resample_buffer; /* to free it on Timidity_Close */
35 
36 
37 static int opt_expression_curve = 2;
38 static int opt_volume_curve = 2;
39 static int opt_stereo_surround = 0;
40 
41 
42 Channel channel[MAXCHAN];
43 Voice voice[MAX_VOICES];
44 
45 signed char drumvolume[MAXCHAN][MAXNOTE];
46 signed char drumpanpot[MAXCHAN][MAXNOTE];
47 signed char drumreverberation[MAXCHAN][MAXNOTE];
48 signed char drumchorusdepth[MAXCHAN][MAXNOTE];
49 
50 int control_ratio=0;
51 
52 static double amplification = 1.0;
53 static double master_volume = 1.0;
54 static int quiet_factor=1;
55 
56 
57 int drumchannels=DEFAULT_DRUMCHANNELS;
58 int adjust_panning_immediately=0;
59 
60 static int midi_playing = 0;
61 static int lost_notes, cut_notes;
62 
63 static s32_t *common_buffer;
64 static int common_buf_size = 0;
65 
66 static MidiEvent *event_list, *current_event;
67 static int sample_count, current_sample;
68 
69 int GM_System_On=0;
70 int XG_System_On=0;
71 int GS_System_On=0;
72 int XG_System_reverb_type;
73 int XG_System_chorus_type;
74 int XG_System_variation_type;
75 
76 
NeedCommonBuf(int count)77 final_volume_t * NeedCommonBuf(int count)
78 {
79 	if (count > common_buf_size)
80 	{
81 		if (common_buffer)
82 			free(common_buffer);
83 
84 		common_buf_size = count;
85 		common_buffer = (final_volume_t *)safe_malloc(common_buf_size * sizeof(final_volume_t) + 100);
86 	}
87 
88 	return common_buffer;
89 }
90 
91 
adjust_master_volume(int vol)92 static void adjust_master_volume(int vol)
93 {
94 	master_volume = vol * amplification / 16384.0;
95 
96 	if (quiet_factor == 0)
97 		master_volume *= 2.0;
98 
99 	if (quiet_factor >= 2)
100 		master_volume /= (double)(1 << (quiet_factor-1));
101 }
102 
103 
adjust_amplification(void)104 static void adjust_amplification(void)
105 {
106 	adjust_master_volume(16383);
107 }
108 
109 
reset_voices(void)110 static void reset_voices(void)
111 {
112 	for (int i=0; i < MAX_VOICES; i++)
113 		voice[i].status=VOICE_FREE;
114 }
115 
116 /* Process the Reset All Controllers event */
reset_controllers(int c)117 static void reset_controllers(int c)
118 {
119 	channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
120 	channel[c].expression=127; /* SCC-1 does this. */
121 	channel[c].sustain=0;
122 	channel[c].pitchbend=0x2000;
123 	channel[c].pitchfactor=0; /* to be computed */
124 
125 	channel[c].reverberation = 0;
126 	channel[c].chorusdepth = 0;
127 }
128 
redraw_controllers(int c)129 static void redraw_controllers(int c)
130 {
131 }
132 
reset_midi(void)133 static void reset_midi(void)
134 {
135 	for (int i=0; i < MAXCHAN; i++)
136 	{
137 		reset_controllers(i);
138 		/* The rest of these are unaffected by the Reset All Controllers event */
139 		channel[i].program=default_program;
140 		channel[i].panning=NO_PANNING;
141 		channel[i].pitchsens=2;
142 		channel[i].bank=0; /* tone bank or drum set */
143 		channel[i].harmoniccontent=64;
144 		channel[i].releasetime=64;
145 		channel[i].attacktime=64;
146 		channel[i].brightness=64;
147 		channel[i].sfx=0;
148 	}
149 
150 	reset_voices();
151 }
152 
select_sample(int v,Instrument * ip)153 static void select_sample(int v, Instrument *ip)
154 {
155 	int f, cdiff, diff, midfreq;
156 	int s,i;
157 	Sample *sp, *closest;
158 
159 	s=ip->samples;
160 	sp=ip->sample;
161 
162 	if (s==1)
163 	{
164 		voice[v].sample=sp;
165 		return;
166 	}
167 
168 	f=voice[v].orig_frequency;
169 	/*
170 	   No suitable sample found! We'll select the sample whose root
171 	   frequency is closest to the one we want. (Actually we should
172 	   probably convert the low, high, and root frequencies to MIDI note
173 	   values and compare those.) */
174 
175 	cdiff=0x7FFFFFFF;
176 	closest=sp=ip->sample;
177 	midfreq = (sp->low_freq + sp->high_freq) / 2;
178 	for(i=0; i<s; i++)
179 	{
180 		diff=sp->root_freq - f;
181 		/*  But the root freq. can perfectly well lie outside the keyrange
182 		 *  frequencies, so let's try:
183 		 */
184 		/* diff=midfreq - f; */
185 		if (diff<0) diff=-diff;
186 		if (diff<cdiff)
187 		{
188 			cdiff=diff;
189 			closest=sp;
190 		}
191 		sp++;
192 	}
193 	voice[v].sample=closest;
194 	return;
195 }
196 
197 
198 
select_stereo_samples(int v,InstrumentLayer * lp)199 static void select_stereo_samples(int v, InstrumentLayer *lp)
200 {
201 	Instrument *ip;
202 	InstrumentLayer *nlp, *bestvel;
203 	int diffvel, midvel, mindiff;
204 
205 	/* select closest velocity */
206 	bestvel = lp;
207 	mindiff = 500;
208 
209 	for (nlp = lp; nlp; nlp = nlp->next)
210 	{
211 		midvel = (nlp->hi + nlp->lo)/2;
212 		if (!midvel)
213 			diffvel = 127;
214 		else if (voice[v].velocity < nlp->lo || voice[v].velocity > nlp->hi)
215 			diffvel = 200;
216 		else
217 			diffvel = voice[v].velocity - midvel;
218 
219 		if (diffvel < 0)
220 			diffvel = -diffvel;
221 
222 		if (diffvel < mindiff)
223 		{
224 			mindiff = diffvel;
225 			bestvel = nlp;
226 		}
227 	}
228 	ip = bestvel->instrument;
229 
230 	if (ip->right_sample)
231 	{
232 		ip->sample = ip->right_sample;
233 		ip->samples = ip->right_samples;
234 		select_sample(v, ip);
235 		voice[v].right_sample = voice[v].sample;
236 	}
237 	else voice[v].right_sample = 0;
238 	ip->sample = ip->left_sample;
239 	ip->samples = ip->left_samples;
240 	select_sample(v, ip);
241 }
242 
243 
recompute_freq(int v)244 static void recompute_freq(int v)
245 {
246 	int sign=(voice[v].sample_increment < 0); /* for bidirectional loops */
247 	int pb=channel[voice[v].channel].pitchbend;
248 	double a;
249 
250 	if (!voice[v].sample->sample_rate)
251 		return;
252 
253 	if (voice[v].vibrato_control_ratio)
254 	{
255 		/* This instrument has vibrato. Invalidate any precomputed
256 		   sample_increments. */
257 
258 		for (int i=0; i < VIBRATO_SAMPLE_INCREMENTS; i++)
259 			voice[v].vibrato_sample_increment[i] = 0;
260 	}
261 
262 	if (pb==0x2000 || pb<0 || pb>0x3FFF)
263 		voice[v].frequency=voice[v].orig_frequency;
264 	else
265 	{
266 		pb-=0x2000;
267 		if (!(channel[voice[v].channel].pitchfactor))
268 		{
269 			/* Damn. Somebody bent the pitch. */
270 			int i=pb*channel[voice[v].channel].pitchsens;
271 			if (pb<0)
272 				i=-i;
273 			channel[voice[v].channel].pitchfactor=
274 				(double)(bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13]);
275 		}
276 		if (pb>0)
277 			voice[v].frequency=
278 				(int)(channel[voice[v].channel].pitchfactor *
279 						(double)(voice[v].orig_frequency));
280 		else
281 			voice[v].frequency=
282 				(int)((double)(voice[v].orig_frequency) /
283 						channel[voice[v].channel].pitchfactor);
284 	}
285 
286 	a = FSCALE(((double)(voice[v].sample->sample_rate) *
287 				(double)(voice[v].frequency)) /
288 			((double)(voice[v].sample->root_freq) *
289 			 (double)(play_mode_rate)),
290 			FRACTION_BITS);
291 
292 	if (sign)
293 		a = -a; /* need to preserve the loop direction */
294 
295 	voice[v].sample_increment = (int)(a);
296 }
297 
298 static int expr_curve[128] =
299 {
300 	7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11,
301 	11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15,
302 	15, 16, 16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 22,
303 	22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31,
304 	32, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 44,
305 	45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 59, 60, 61, 63,
306 	64, 65, 67, 68, 70, 71, 73, 75, 76, 78, 80, 82, 83, 85, 87, 89,
307 	91, 93, 95, 97, 99, 102, 104, 106, 109, 111, 113, 116, 118, 121,
308 	124, 127
309 };
310 
panf(int pan,int speaker,int separation)311 static int panf(int pan, int speaker, int separation)
312 {
313 	int val = 127 - abs(pan - speaker) * 127 / separation;
314 
315 	return expr_curve[CLAMP(0,val,128)];
316 }
317 
318 
319 static int vcurve[128] =
320 {
321 	0,0,18,29,36,42,47,51,55,58,
322 	60,63,65,67,69,71,73,74,76,77,
323 	79,80,81,82,83,84,85,86,87,88,
324 	89,90,91,92,92,93,94,95,95,96,
325 	97,97,98,99,99,100,100,101,101,102,
326 	103,103,104,104,105,105,106,106,106,107,
327 	107,108,108,109,109,109,110,110,111,111,
328 	111,112,112,112,113,113,114,114,114,115,
329 	115,115,116,116,116,116,117,117,117,118,
330 	118,118,119,119,119,119,120,120,120,120,
331 	121,121,121,122,122,122,122,123,123,123,
332 	123,123,124,124,124,124,125,125,125,125,
333 	126,126,126,126,126,127,127,127
334 };
335 
recompute_amp(int v)336 static void recompute_amp(int v)
337 {
338 	int tempamp;
339 	int chan = voice[v].channel;
340 	int panning = voice[v].panning;
341 	int vol = channel[chan].volume;
342 	int expr = channel[chan].expression;
343 	int vel = vcurve[voice[v].velocity];
344 //	int drumpan = NO_PANNING;
345 	double curved_expression, curved_volume;
346 
347 	if (channel[chan].kit)
348 	{
349 		int note = voice[v].sample->note_to_use;
350 		if (note>0 && drumvolume[chan][note]>=0) vol = drumvolume[chan][note];
351 		if (note>0 && drumpanpot[chan][note]>=0) panning = drumvolume[chan][note];
352 	}
353 
354 	if (opt_expression_curve == 2) curved_expression = 127.0 * vol_table[expr];
355 	else if (opt_expression_curve == 1) curved_expression = 127.0 * expr_table[expr];
356 	else curved_expression = (double)expr;
357 
358 	if (opt_volume_curve == 2) curved_volume = 127.0 * vol_table[vol];
359 	else if (opt_volume_curve == 1) curved_volume = 127.0 * expr_table[vol];
360 	else curved_volume = (double)vol;
361 
362 	tempamp= (int)((double)vel * curved_volume * curved_expression); /* 21 bits */
363 
364 	/* TODO: use fscale */
365 
366 	if (num_ochannels > 1)
367 	{
368 		if (panning > 60 && panning < 68)
369 		{
370 			voice[v].panned=PANNED_CENTER;
371 
372 			if (num_ochannels == 6) voice[v].left_amp =
373 				FSCALENEG((double) (tempamp) * voice[v].sample->volume *
374 						master_volume, 20);
375 			else voice[v].left_amp=
376 				FSCALENEG((double)(tempamp) * voice[v].sample->volume *
377 						master_volume, 21);
378 		}
379 		else if (panning<5)
380 		{
381 			voice[v].panned = PANNED_LEFT;
382 
383 			voice[v].left_amp=
384 				FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 20);
385 		}
386 		else if (panning>123)
387 		{
388 			voice[v].panned = PANNED_RIGHT;
389 
390 			voice[v].left_amp= /* left_amp will be used */
391 				FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 20);
392 		}
393 		else
394 		{
395 			double refv = (double)(tempamp) * voice[v].sample->volume * master_volume;
396 			int wide_panning = 64;
397 
398 			if (num_ochannels == 4) wide_panning = 95;
399 
400 			voice[v].panned = PANNED_MYSTERY;
401 			voice[v].lfe_amp = FSCALENEG(refv * 64, 27);
402 
403 			switch (num_ochannels)
404 			{
405 				case 2:
406 					voice[v].lr_amp = 0;
407 					voice[v].left_amp = FSCALENEG(refv * (128-panning), 27);
408 					voice[v].ce_amp = 0;
409 					voice[v].right_amp = FSCALENEG(refv * panning, 27);
410 					voice[v].rr_amp = 0;
411 					break;
412 				case 4:
413 					voice[v].lr_amp = FSCALENEG(refv * panf(panning, 0, wide_panning), 27);
414 					voice[v].left_amp = FSCALENEG(refv * panf(panning, 32, wide_panning), 27);
415 					voice[v].ce_amp = 0;
416 					voice[v].right_amp = FSCALENEG(refv * panf(panning, 95, wide_panning), 27);
417 					voice[v].rr_amp = FSCALENEG(refv * panf(panning, 128, wide_panning), 27);
418 					break;
419 				case 6:
420 					voice[v].lr_amp = FSCALENEG(refv * panf(panning, 0, wide_panning), 27);
421 					voice[v].left_amp = FSCALENEG(refv * panf(panning, 32, wide_panning), 27);
422 					voice[v].ce_amp = FSCALENEG(refv * panf(panning, 64, wide_panning), 27);
423 					voice[v].right_amp = FSCALENEG(refv * panf(panning, 95, wide_panning), 27);
424 					voice[v].rr_amp = FSCALENEG(refv * panf(panning, 128, wide_panning), 27);
425 					break;
426 			}
427 
428 		}
429 	}
430 	else
431 	{
432 		voice[v].panned=PANNED_CENTER;
433 
434 		voice[v].left_amp=
435 			FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 21);
436 	}
437 }
438 
439 
440 #define NOT_CLONE 0
441 #define STEREO_CLONE 1
442 #define REVERB_CLONE 2
443 #define CHORUS_CLONE 3
444 
445 
446 /* just a variant of note_on() */
vc_alloc(int j)447 static int vc_alloc(int j)
448 {
449 	for (int i=0; i < MAX_VOICES; i++)
450 	{
451 		if (i == j)
452 			continue;
453 
454 		if (voice[i].status & VOICE_FREE)
455 			return i;
456 	}
457 	return -1;
458 }
459 
460 static void kill_note(int i);
461 
kill_others(int i)462 static void kill_others(int i)
463 {
464 	if (!voice[i].sample->exclusiveClass)
465 		return;
466 
467 	for (int j=0; j < MAX_VOICES; j++)
468 	{
469 		if (voice[j].status & (VOICE_FREE|VOICE_OFF|VOICE_DIE))
470 			continue;
471 
472 		if (i == j)
473 			continue;
474 
475 		if (voice[i].channel != voice[j].channel)
476 			continue;
477 
478 		if (voice[j].sample->note_to_use)
479 		{
480 			if (voice[j].sample->exclusiveClass != voice[i].sample->exclusiveClass)
481 				continue;
482 
483 			kill_note(j);
484 		}
485 	}
486 }
487 
488 
clone_voice(Instrument * ip,int v,MidiEvent * e,int clone_type,int variationbank)489 static void clone_voice(Instrument *ip, int v, MidiEvent *e, int clone_type, int variationbank)
490 {
491 	int w, played_note, chorus=0, reverb=0, milli;
492 	int chan = voice[v].channel;
493 
494 	if (clone_type == STEREO_CLONE)
495 	{
496 		if (!voice[v].right_sample && variationbank != 3)
497 			return;
498 		if (variationbank == 6)
499 			return;
500 	}
501 
502 	if (channel[chan].kit)
503 	{
504 		reverb = drumreverberation[chan][voice[v].note];
505 		chorus = drumchorusdepth[chan][voice[v].note];
506 	}
507 	else
508 	{
509 		reverb = channel[chan].reverberation;
510 		chorus = channel[chan].chorusdepth;
511 	}
512 
513 	if (clone_type == REVERB_CLONE) chorus = 0;
514 	else if (clone_type == CHORUS_CLONE) reverb = 0;
515 	else if (clone_type == STEREO_CLONE) reverb = chorus = 0;
516 
517 	if (reverb > 127) reverb = 127;
518 	if (chorus > 127) chorus = 127;
519 
520 	if (clone_type == CHORUS_CLONE)
521 	{
522 		if (variationbank == 32) chorus = 30;
523 		else if (variationbank == 33) chorus = 60;
524 		else if (variationbank == 34) chorus = 90;
525 	}
526 
527 	chorus /= 2;  /* This is an ad hoc adjustment. */
528 
529 	if (!reverb && !chorus && clone_type != STEREO_CLONE) return;
530 
531 	if ( (w = vc_alloc(v)) < 0 ) return;
532 
533 	voice[w] = voice[v];
534 	if (clone_type==STEREO_CLONE) voice[v].clone_voice = w;
535 	voice[w].clone_voice = v;
536 	voice[w].clone_type = clone_type;
537 
538 	voice[w].sample = voice[v].right_sample;
539 	voice[w].velocity= e->b;
540 
541 	milli = play_mode_rate/1000;
542 
543 	if (clone_type == STEREO_CLONE)
544 	{
545 		int left, right, leftpan, rightpan;
546 		int panrequest = voice[v].panning;
547 		if (variationbank == 3)
548 		{
549 			voice[v].panning = 0;
550 			voice[w].panning = 127;
551 		}
552 		else
553 		{
554 			if (voice[v].sample->panning > voice[w].sample->panning)
555 			{
556 				left = w;
557 				right = v;
558 			}
559 			else
560 			{
561 				left = v;
562 				right = w;
563 			}
564 #define INSTRUMENT_SEPARATION 12
565 			leftpan = panrequest - INSTRUMENT_SEPARATION / 2;
566 			rightpan = leftpan + INSTRUMENT_SEPARATION;
567 			if (leftpan < 0)
568 			{
569 				leftpan = 0;
570 				rightpan = leftpan + INSTRUMENT_SEPARATION;
571 			}
572 			if (rightpan > 127)
573 			{
574 				rightpan = 127;
575 				leftpan = rightpan - INSTRUMENT_SEPARATION;
576 			}
577 			voice[left].panning = leftpan;
578 			voice[right].panning = rightpan;
579 			voice[right].echo_delay = 20 * milli;
580 		}
581 	}
582 
583 	voice[w].volume = voice[w].sample->volume;
584 
585 	if (reverb)
586 	{
587 		if (opt_stereo_surround)
588 		{
589 			if (voice[w].panning > 64)
590 				voice[w].panning = 127;
591 			else
592 				voice[w].panning = 0;
593 		}
594 		else
595 		{
596 			if (voice[v].panning < 64)
597 				voice[w].panning = 64 + reverb/2;
598 			else
599 				voice[w].panning = 64 - reverb/2;
600 		}
601 
602 		/* try 98->99 for melodic instruments ? (bit much for percussion) */
603 		voice[w].volume *= vol_table[(127-reverb)/8 + 98];
604 
605 		voice[w].echo_delay += reverb * milli;
606 		voice[w].envelope_rate[DECAY] *= 2;
607 		voice[w].envelope_rate[RELEASE] /= 2;
608 
609 		if (XG_System_reverb_type >= 0)
610 		{
611 			int subtype = XG_System_reverb_type & 0x07;
612 			int rtype = XG_System_reverb_type >>3;
613 
614 			switch (rtype)
615 			{
616 				case 0: /* no effect */
617 					break;
618 				case 1: /* hall */
619 					if (subtype) voice[w].echo_delay += 100 * milli;
620 					break;
621 				case 2: /* room */
622 					voice[w].echo_delay /= 2;
623 					break;
624 				case 3: /* stage */
625 					voice[w].velocity = voice[v].velocity;
626 					break;
627 				case 4: /* plate */
628 					voice[w].panning = voice[v].panning;
629 					break;
630 				case 16: /* white room */
631 					voice[w].echo_delay = 0;
632 					break;
633 				case 17: /* tunnel */
634 					voice[w].echo_delay *= 2;
635 					voice[w].velocity /= 2;
636 					break;
637 				case 18: /* canyon */
638 					voice[w].echo_delay *= 2;
639 					break;
640 				case 19: /* basement */
641 					voice[w].velocity /= 2;
642 					break;
643 
644 				default: break;
645 			}
646 		}
647 	}
648 	played_note = voice[w].sample->note_to_use;
649 
650 	if (!played_note)
651 	{
652 		played_note = e->a & 0x7f;
653 		if (variationbank == 35) played_note += 12;
654 		else if (variationbank == 36) played_note -= 12;
655 		else if (variationbank == 37) played_note += 7;
656 		else if (variationbank == 36) played_note -= 7;
657 	}
658 #if 0
659 	played_note = ( (played_note - voice[w].sample->freq_center) * voice[w].sample->freq_scale ) / 1024 +
660 		voice[w].sample->freq_center;
661 #endif
662 	voice[w].note = played_note;
663 	voice[w].orig_frequency = freq_table[played_note];
664 
665 	if (chorus)
666 	{
667 		if (opt_stereo_surround)
668 		{
669 			if (voice[v].panning < 64)
670 				voice[w].panning = voice[v].panning + 32;
671 			else
672 				voice[w].panning = voice[v].panning - 32;
673 		}
674 
675 		if (!voice[w].vibrato_control_ratio)
676 		{
677 			voice[w].vibrato_control_ratio = 100;
678 			voice[w].vibrato_depth = 6;
679 			voice[w].vibrato_sweep = 74;
680 		}
681 		voice[w].volume *= 0.40;
682 		voice[v].volume = voice[w].volume;
683 
684 		recompute_amp(v);
685 		apply_envelope_to_amp(v);
686 
687 		voice[w].vibrato_sweep = chorus/2;
688 		voice[w].vibrato_depth /= 2;
689 
690 		if (!voice[w].vibrato_depth)
691 			voice[w].vibrato_depth = 2;
692 
693 		voice[w].vibrato_control_ratio /= 2;
694 		voice[w].echo_delay += 30 * milli;
695 
696 		if (XG_System_chorus_type >= 0)
697 		{
698 			int subtype = XG_System_chorus_type & 0x07;
699 			int chtype = 0x0f & (XG_System_chorus_type >> 3);
700 
701 			switch (chtype)
702 			{
703 				case 0: /* no effect */
704 					break;
705 				case 1: /* chorus */
706 					chorus /= 3;
707 					if (channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
708 						voice[w].orig_frequency =
709 							(u32_t)( (double)voice[w].orig_frequency * bend_fine[chorus] );
710 					else voice[w].orig_frequency =
711 						(u32_t)( (double)voice[w].orig_frequency / bend_fine[chorus] );
712 					if (subtype) voice[w].vibrato_depth *= 2;
713 					break;
714 				case 2: /* celeste */
715 					voice[w].orig_frequency += (voice[w].orig_frequency/128) * chorus;
716 					break;
717 				case 3: /* flanger */
718 					voice[w].vibrato_control_ratio = 10;
719 					voice[w].vibrato_depth = 100;
720 					voice[w].vibrato_sweep = 8;
721 					voice[w].echo_delay += 200 * milli;
722 					break;
723 				case 4: /* symphonic : cf Children of the Night /128 bad, /1024 ok */
724 					voice[w].orig_frequency += (voice[w].orig_frequency/512) * chorus;
725 					voice[v].orig_frequency -= (voice[v].orig_frequency/512) * chorus;
726 					recompute_freq(v);
727 					break;
728 				case 8: /* phaser */
729 					break;
730 
731 				default:
732 					break;
733 			}
734 		}
735 		else
736 		{
737 			chorus /= 3;
738 			if (channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
739 			{
740 				voice[w].orig_frequency =
741 					(u32_t)( (double)voice[w].orig_frequency * bend_fine[chorus] );
742 			}
743 			else
744 			{
745 				voice[w].orig_frequency =
746 					(u32_t)( (double)voice[w].orig_frequency / bend_fine[chorus] );
747 			}
748 		}
749 	}
750 #if 0
751 	voice[w].loop_start = voice[w].sample->loop_start;
752 	voice[w].loop_end = voice[w].sample->loop_end;
753 #endif
754 	voice[w].echo_delay_count = voice[w].echo_delay;
755 
756 	if (reverb)
757 		voice[w].echo_delay *= 2;
758 
759 	recompute_freq(w);
760 	recompute_amp(w);
761 
762 	if (voice[w].sample->modes & MODES_ENVELOPE)
763 	{
764 		/* Ramp up from 0 */
765 		voice[w].envelope_stage=ATTACK;
766 		voice[w].modulation_stage=ATTACK;
767 		voice[w].envelope_volume=0;
768 		voice[w].modulation_volume=0;
769 		voice[w].control_counter=0;
770 		voice[w].modulation_counter=0;
771 		recompute_envelope(w);
772 		/*recompute_modulation(w);*/
773 	}
774 	else
775 	{
776 		voice[w].envelope_increment=0;
777 		voice[w].modulation_increment=0;
778 	}
779 	apply_envelope_to_amp(w);
780 }
781 
782 
xremap(int * banknumpt,int * this_notept,int this_kit)783 static void xremap(int *banknumpt, int *this_notept, int this_kit)
784 {
785 	int i, newmap;
786 	int banknum = *banknumpt;
787 	int this_note = *this_notept;
788 	int newbank, newnote;
789 
790 	if (!this_kit)
791 	{
792 		if (banknum == SFXBANK && tonebank[SFXBANK])
793 			return;
794 
795 		if (banknum == SFXBANK && tonebank[120])
796 			*banknumpt = 120;
797 		return;
798 	}
799 
800 	if (this_kit != 127 && this_kit != 126)
801 		return;
802 
803 	for (i = 0; i < XMAPMAX; i++)
804 	{
805 		newmap = xmap[i][0];
806 		if (!newmap) return;
807 		if (this_kit == 127 && newmap != XGDRUM) continue;
808 		if (this_kit == 126 && newmap != SFXDRUM1) continue;
809 		if (xmap[i][1] != banknum) continue;
810 		if (xmap[i][3] != this_note) continue;
811 		newbank = xmap[i][2];
812 		newnote = xmap[i][4];
813 		if (newbank == banknum && newnote == this_note) return;
814 		if (!drumset[newbank]) return;
815 		if (!drumset[newbank]->tone[newnote].layer) return;
816 		if (drumset[newbank]->tone[newnote].layer == MAGIC_LOAD_INSTRUMENT) return;
817 		*banknumpt = newbank;
818 		*this_notept = newnote;
819 		return;
820 	}
821 }
822 
823 
start_note(MidiEvent * e,int i)824 static void start_note(MidiEvent *e, int i)
825 {
826 	InstrumentLayer *lp;
827 	Instrument *ip;
828 
829 	int j, banknum, ch=e->channel;
830 	int played_note, drumpan=NO_PANNING;
831 	int rt;
832 	int attacktime, releasetime, decaytime, variationbank;
833 	int brightness = channel[ch].brightness;
834 	int harmoniccontent = channel[ch].harmoniccontent;
835 	int this_note = e->a;
836 	int this_velocity = e->b;
837 	int drumsflag = channel[ch].kit;
838 	int this_prog = channel[ch].program;
839 
840 	if (channel[ch].sfx)
841 		banknum=channel[ch].sfx;
842 	else
843 		banknum=channel[ch].bank;
844 
845 	voice[i].velocity=this_velocity;
846 
847 	if (XG_System_On) xremap(&banknum, &this_note, drumsflag);
848 	/*   if (current_config_pc42b) pcmap(&banknum, &this_note, &this_prog, &drumsflag); */
849 
850 	if (drumsflag)
851 	{
852 		lp = drumset[banknum]->tone[this_note].layer;
853 		if (! lp)
854 			lp = drumset[0]->tone[this_note].layer;
855 
856 		if (! lp)
857 			return; /* No instrument? Then we can't play. */
858 
859 		SYS_ASSERT(lp != MAGIC_LOAD_INSTRUMENT);
860 
861 		ip = lp->instrument;
862 		if (ip->type == INST_GUS && ip->samples != 1)
863 		{
864 			ctl_msg(CMSG_WARNING, VERB_VERBOSE,
865 					"Strange: percussion instrument with %d samples!", ip->samples);
866 		}
867 
868 		if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
869 		{
870 			voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
871 			drumpan=drumpanpot[ch][(int)ip->sample->note_to_use];
872 		}
873 		else
874 			voice[i].orig_frequency=freq_table[this_note & 0x7F];
875 
876 	}
877 	else /* melodic */
878 	{
879 		if (channel[ch].program==SPECIAL_PROGRAM)
880 			lp = default_instrument;
881 		else
882 		{
883 			lp = tonebank[banknum]->tone[this_prog].layer;
884 
885 			if (! lp)
886 				lp = tonebank[0]->tone[this_prog].layer;
887 
888 			if (! lp)
889 				return; /* No instrument? Then we can't play. */
890 		}
891 
892 		// -AJA- all instruments have been loaded already
893 		SYS_ASSERT(lp != MAGIC_LOAD_INSTRUMENT);
894 
895 		ip = lp->instrument;
896 		if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
897 			voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
898 		else
899 			voice[i].orig_frequency=freq_table[this_note & 0x7F];
900 	}
901 
902 	select_stereo_samples(i, lp);
903 
904 	voice[i].starttime = e->time;
905 	played_note = voice[i].sample->note_to_use;
906 
907 	if (!played_note || !drumsflag)
908 		played_note = this_note & 0x7f;
909 #if 0
910 	played_note = ( (played_note - voice[i].sample->freq_center) * voice[i].sample->freq_scale ) / 1024 +
911 		voice[i].sample->freq_center;
912 #endif
913 	voice[i].status=VOICE_ON;
914 	voice[i].channel=ch;
915 	voice[i].note=played_note;
916 	voice[i].velocity=this_velocity;
917 	voice[i].sample_offset=0;
918 	voice[i].sample_increment=0; /* make sure it isn't negative */
919 
920 	voice[i].tremolo_phase=0;
921 	voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment;
922 	voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment;
923 	voice[i].tremolo_sweep_position=0;
924 
925 	voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment;
926 	voice[i].vibrato_sweep_position=0;
927 	voice[i].vibrato_depth=voice[i].sample->vibrato_depth;
928 	voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio;
929 	voice[i].vibrato_control_counter=voice[i].vibrato_phase=0;
930 	voice[i].vibrato_delay = voice[i].sample->vibrato_delay;
931 
932 	kill_others(i);
933 
934 	for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
935 		voice[i].vibrato_sample_increment[j]=0;
936 
937 
938 	attacktime = channel[ch].attacktime;
939 	releasetime = channel[ch].releasetime;
940 	decaytime = 64;
941 	variationbank = channel[ch].variationbank;
942 
943 	switch (variationbank)
944 	{
945 		case  8:
946 			attacktime = 64+32;
947 			break;
948 		case 12:
949 			decaytime = 64-32;
950 			break;
951 		case 16:
952 			brightness = 64+16;
953 			break;
954 		case 17:
955 			brightness = 64+32;
956 			break;
957 		case 18:
958 			brightness = 64-16;
959 			break;
960 		case 19:
961 			brightness = 64-32;
962 			break;
963 		case 20:
964 			harmoniccontent = 64+16;
965 			break;
966 #if 0
967 		case 24:
968 			voice[i].modEnvToFilterFc=2.0;
969 			voice[i].sample->cutoff_freq = 800;
970 			break;
971 		case 25:
972 			voice[i].modEnvToFilterFc=-2.0;
973 			voice[i].sample->cutoff_freq = 800;
974 			break;
975 		case 27:
976 			voice[i].modLfoToFilterFc=2.0;
977 			voice[i].lfo_phase_increment=109;
978 			voice[i].lfo_sweep=122;
979 			voice[i].sample->cutoff_freq = 800;
980 			break;
981 		case 28:
982 			voice[i].modLfoToFilterFc=-2.0;
983 			voice[i].lfo_phase_increment=109;
984 			voice[i].lfo_sweep=122;
985 			voice[i].sample->cutoff_freq = 800;
986 			break;
987 #endif
988 		default:
989 			break;
990 	}
991 
992 
993 	for (j=ATTACK; j<MAXPOINT; j++)
994 	{
995 		voice[i].envelope_rate[j]=voice[i].sample->envelope_rate[j];
996 		voice[i].envelope_offset[j]=voice[i].sample->envelope_offset[j];
997 	}
998 
999 	voice[i].echo_delay=voice[i].envelope_rate[DELAY];
1000 	voice[i].echo_delay_count = voice[i].echo_delay;
1001 
1002 	if (attacktime!=64)
1003 	{
1004 		rt = voice[i].envelope_rate[ATTACK];
1005 		rt = rt + ( (64-attacktime)*rt ) / 100;
1006 		if (rt > 1000) voice[i].envelope_rate[ATTACK] = rt;
1007 	}
1008 	if (releasetime!=64)
1009 	{
1010 		rt = voice[i].envelope_rate[RELEASE];
1011 		rt = rt + ( (64-releasetime)*rt ) / 100;
1012 		if (rt > 1000) voice[i].envelope_rate[RELEASE] = rt;
1013 	}
1014 	if (decaytime!=64)
1015 	{
1016 		rt = voice[i].envelope_rate[DECAY];
1017 		rt = rt + ( (64-decaytime)*rt ) / 100;
1018 		if (rt > 1000) voice[i].envelope_rate[DECAY] = rt;
1019 	}
1020 
1021 	if (channel[ch].panning != NO_PANNING)
1022 		voice[i].panning=channel[ch].panning;
1023 	else
1024 		voice[i].panning=voice[i].sample->panning;
1025 	if (drumpan != NO_PANNING)
1026 		voice[i].panning=drumpan;
1027 
1028 	if (variationbank == 1)
1029 	{
1030 		int pan = voice[i].panning;
1031 		int disturb = 0;
1032 		/* If they're close up (no reverb) and you are behind the pianist,
1033 		 * high notes come from the right, so we'll spread piano etc. notes
1034 		 * out horizontally according to their pitches.
1035 		 */
1036 		if (this_prog < 21)
1037 		{
1038 			int n = voice[i].velocity - 32;
1039 			if (n < 0) n = 0;
1040 			if (n > 64) n = 64;
1041 			pan = pan/2 + n;
1042 		}
1043 		/* For other types of instruments, the music sounds more alive if
1044 		 * notes come from slightly different directions.  However, instruments
1045 		 * do drift around in a sometimes disconcerting way, so the following
1046 		 * might not be such a good idea.
1047 		 */
1048 		else disturb = (voice[i].velocity/32 % 8) +
1049 			(voice[i].note % 8); /* /16? */
1050 
1051 		if (pan < 64) pan += disturb;
1052 		else pan -= disturb;
1053 		if (pan < 0) pan = 0;
1054 		else if (pan > 127) pan = 127;
1055 		voice[i].panning = pan;
1056 	}
1057 
1058 	recompute_freq(i);
1059 	recompute_amp(i);
1060 
1061 	if (voice[i].sample->modes & MODES_ENVELOPE)
1062 	{
1063 		/* Ramp up from 0 */
1064 		voice[i].envelope_stage=ATTACK;
1065 		voice[i].envelope_volume=0;
1066 		voice[i].control_counter=0;
1067 		recompute_envelope(i);
1068 	}
1069 	else
1070 	{
1071 		voice[i].envelope_increment=0;
1072 	}
1073 	apply_envelope_to_amp(i);
1074 
1075 	voice[i].clone_voice = -1;
1076 	voice[i].clone_type = NOT_CLONE;
1077 
1078 	clone_voice(ip, i, e, STEREO_CLONE, variationbank);
1079 	clone_voice(ip, i, e, CHORUS_CLONE, variationbank);
1080 	clone_voice(ip, i, e, REVERB_CLONE, variationbank);
1081 }
1082 
kill_note(int i)1083 static void kill_note(int i)
1084 {
1085 	voice[i].status = VOICE_DIE;
1086 
1087 	int v = voice[i].clone_voice;
1088 	if (v >= 0)
1089 		voice[v].status = VOICE_DIE;
1090 }
1091 
1092 
1093 /* Only one instance of a note can be playing on a single channel. */
note_on(MidiEvent * e)1094 static void note_on(MidiEvent *e)
1095 {
1096 	int i, v;
1097 	int lowest=-1;
1098 	int lv = 0x7FFFFFFF;
1099 
1100 	for (i=0; i < MAX_VOICES; i++)
1101 	{
1102 		if (voice[i].status == VOICE_FREE)
1103 			lowest=i; /* Can't get a lower volume than silence */
1104 		else if (voice[i].channel==e->channel &&
1105 				(voice[i].note==e->a || channel[voice[i].channel].mono))
1106 			kill_note(i);
1107 	}
1108 
1109 	if (lowest != -1)
1110 	{
1111 		/* Found a free voice. */
1112 		start_note(e,lowest);
1113 		return;
1114 	}
1115 
1116 	/* Look for the decaying note with the lowest volume */
1117 	if (lowest==-1)
1118 	{
1119 		for (i=0; i < MAX_VOICES; i++)
1120 		{
1121 			if ( (voice[i].status & ~(VOICE_ON | VOICE_DIE | VOICE_FREE)) &&
1122 					(!voice[i].clone_type))
1123 			{
1124 				v=voice[i].left_mix;
1125 
1126 				if ((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v))
1127 					v=voice[i].right_mix;
1128 
1129 				if (v < lv)
1130 				{
1131 					lv=v;
1132 					lowest=i;
1133 				}
1134 			}
1135 		}
1136 	}
1137 
1138 	if (lowest != -1)
1139 	{
1140 		int cl = voice[lowest].clone_voice;
1141 
1142 		/* This can still cause a click, but if we had a free voice to
1143 		   spare for ramping down this note, we wouldn't need to kill it
1144 		   in the first place... Still, this needs to be fixed. Perhaps
1145 		   we could use a reserve of voices to play dying notes only. */
1146 
1147 		if (cl >= 0)
1148 		{
1149 			if (voice[cl].clone_type==STEREO_CLONE ||
1150 				(!voice[cl].clone_type && voice[lowest].clone_type==STEREO_CLONE))
1151 			{
1152 				voice[cl].status=VOICE_FREE;
1153 			}
1154 			else if (voice[cl].clone_voice==lowest)
1155 				voice[cl].clone_voice=-1;
1156 		}
1157 
1158 		cut_notes++;
1159 		voice[lowest].status=VOICE_FREE;
1160 		start_note(e,lowest);
1161 	}
1162 	else
1163 		lost_notes++;
1164 }
1165 
finish_note(int i)1166 static void finish_note(int i)
1167 {
1168 	if (voice[i].sample->modes & MODES_ENVELOPE)
1169 	{
1170 		/* We need to get the envelope out of Sustain stage */
1171 		voice[i].envelope_stage=3;
1172 		voice[i].status=VOICE_OFF;
1173 
1174 		recompute_envelope(i);
1175 		apply_envelope_to_amp(i);
1176 	}
1177 	else
1178 	{
1179 		/* Set status to OFF so resample_voice() will let this voice out
1180 		   of its loop, if any. In any case, this voice dies when it
1181 		   hits the end of its data (ofs>=data_length). */
1182 		voice[i].status=VOICE_OFF;
1183 	}
1184 
1185 	int v = voice[i].clone_voice;
1186 	if (v >= 0)
1187 	{
1188 		voice[i].clone_voice = -1;
1189 		finish_note(v);
1190 	}
1191 }
1192 
note_off(MidiEvent * e)1193 static void note_off(MidiEvent *e)
1194 {
1195 	for (int i=0; i < MAX_VOICES; i++)
1196 	{
1197 		if (voice[i].status == VOICE_ON &&
1198 			voice[i].channel == e->channel &&
1199 			voice[i].note == e->a)
1200 		{
1201 			if (channel[e->channel].sustain)
1202 			{
1203 				voice[i].status=VOICE_SUSTAINED;
1204 
1205 				int v = voice[i].clone_voice;
1206 				if (v >= 0)
1207 				{
1208 					if (voice[v].status == VOICE_ON)
1209 						voice[v].status = VOICE_SUSTAINED;
1210 				}
1211 			}
1212 			else
1213 				finish_note(i);
1214 
1215 			return;  // found the one we wanted
1216 		}
1217 	}
1218 }
1219 
1220 /* Process the All Notes Off event */
all_notes_off(int chan)1221 static void all_notes_off(int chan)
1222 {
1223 	ctl_msg(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", chan);
1224 
1225 	for (int i=0; i < MAX_VOICES; i++)
1226 	{
1227 		if (voice[i].status==VOICE_ON && voice[i].channel == chan)
1228 		{
1229 			if (channel[chan].sustain)
1230 				voice[i].status = VOICE_SUSTAINED;
1231 			else
1232 				finish_note(i);
1233 		}
1234 	}
1235 }
1236 
1237 /* Process the All Sounds Off event */
all_sounds_off(int chan)1238 static void all_sounds_off(int chan)
1239 {
1240 	for (int i=0; i < MAX_VOICES; i++)
1241 	{
1242 		if (voice[i].channel == chan &&
1243 			voice[i].status != VOICE_FREE &&
1244 			voice[i].status != VOICE_DIE)
1245 		{
1246 			kill_note(i);
1247 		}
1248 	}
1249 }
1250 
adjust_pressure(MidiEvent * e)1251 static void adjust_pressure(MidiEvent *e)
1252 {
1253 	for (int i=0; i < MAX_VOICES; i++)
1254 	{
1255 		if (voice[i].status == VOICE_ON &&
1256 			voice[i].channel == e->channel &&
1257 			voice[i].note == e->a)
1258 		{
1259 			voice[i].velocity=e->b;
1260 			recompute_amp(i);
1261 			apply_envelope_to_amp(i);
1262 
1263 			return;  // found the one we wanted
1264 		}
1265 	}
1266 }
1267 
adjust_panning(int chan)1268 static void adjust_panning(int chan)
1269 {
1270 	for (int i=0; i < MAX_VOICES; i++)
1271 	{
1272 		if ((voice[i].channel == chan) &&
1273 			(voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
1274 		{
1275 			if (voice[i].clone_type != NOT_CLONE)
1276 				continue;
1277 
1278 			voice[i].panning = channel[chan].panning;
1279 			recompute_amp(i);
1280 			apply_envelope_to_amp(i);
1281 		}
1282 	}
1283 }
1284 
drop_sustain(int chan)1285 static void drop_sustain(int chan)
1286 {
1287 	for (int i=0; i < MAX_VOICES; i++)
1288 		if (voice[i].status == VOICE_SUSTAINED && voice[i].channel == chan)
1289 			finish_note(i);
1290 }
1291 
adjust_pitchbend(int chan)1292 static void adjust_pitchbend(int chan)
1293 {
1294 	for (int i=0; i < MAX_VOICES; i++)
1295 	{
1296 		if (voice[i].status != VOICE_FREE && voice[i].channel == chan)
1297 			recompute_freq(i);
1298 	}
1299 }
1300 
1301 
adjust_volume(int chan)1302 static void adjust_volume(int chan)
1303 {
1304 	for (int i=0; i < MAX_VOICES; i++)
1305 	{
1306 		if (voice[i].channel == chan &&
1307 			(voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
1308 		{
1309 			recompute_amp(i);
1310 			apply_envelope_to_amp(i);
1311 		}
1312 	}
1313 }
1314 
1315 
do_compute_data(final_volume_t * buffer_pointer,int count)1316 static void do_compute_data(final_volume_t *buffer_pointer, int count)
1317 {
1318 //# I_Debugf("    do_compute_data: %d\n", count);
1319 
1320 	memset(buffer_pointer, 0, count * num_ochannels * sizeof(final_volume_t));
1321 
1322 	for (int i=0; i<MAX_VOICES; i++)
1323 	{
1324 		if (voice[i].status != VOICE_FREE)
1325 		{
1326 			if (!voice[i].sample_offset && voice[i].echo_delay_count)
1327 			{
1328 				if ((int)voice[i].echo_delay_count >= count)
1329 					voice[i].echo_delay_count -= count;
1330 				else
1331 				{
1332 					mix_voice(buffer_pointer+voice[i].echo_delay_count, i, count-voice[i].echo_delay_count);
1333 					voice[i].echo_delay_count = 0;
1334 				}
1335 			}
1336 			else
1337 				mix_voice(buffer_pointer, i, count);
1338 		}
1339 	}
1340 	current_sample += count;
1341 }
1342 
1343 
handle_recent_events(void)1344 static bool handle_recent_events(void)
1345 {
1346 	// returns true if OK, false for end-of-track
1347 
1348 	for (; current_event->time <= current_sample; current_event++)
1349 	{
1350 		switch (current_event->type)
1351 		{
1352 			/* Effects affecting a single note */
1353 
1354 			case ME_NOTEON:
1355 				current_event->a += channel[current_event->channel].transpose;
1356 				if (!(current_event->b)) /* Velocity 0? */
1357 					note_off(current_event);
1358 				else
1359 					note_on(current_event);
1360 				break;
1361 
1362 			case ME_NOTEOFF:
1363 				current_event->a += channel[current_event->channel].transpose;
1364 				note_off(current_event);
1365 				break;
1366 
1367 			case ME_KEYPRESSURE:
1368 				adjust_pressure(current_event);
1369 				break;
1370 
1371 				/* Effects affecting a single channel */
1372 
1373 			case ME_PITCH_SENS:
1374 				channel[current_event->channel].pitchsens=current_event->a;
1375 				channel[current_event->channel].pitchfactor=0;
1376 				break;
1377 
1378 			case ME_PITCHWHEEL:
1379 				channel[current_event->channel].pitchbend=
1380 					current_event->a + current_event->b * 128;
1381 				channel[current_event->channel].pitchfactor=0;
1382 				/* Adjust pitch for notes already playing */
1383 				adjust_pitchbend(current_event->channel);
1384 				break;
1385 
1386 			case ME_MAINVOLUME:
1387 				channel[current_event->channel].volume=current_event->a;
1388 				adjust_volume(current_event->channel);
1389 				break;
1390 
1391 			case ME_MASTERVOLUME:
1392 				adjust_master_volume(current_event->a + (current_event->b <<7));
1393 				break;
1394 
1395 			case ME_REVERBERATION:
1396 				channel[current_event->channel].reverberation=current_event->a;
1397 				break;
1398 
1399 			case ME_CHORUSDEPTH:
1400 				channel[current_event->channel].chorusdepth=current_event->a;
1401 				break;
1402 
1403 			case ME_PAN:
1404 				channel[current_event->channel].panning=current_event->a;
1405 				if (adjust_panning_immediately)
1406 					adjust_panning(current_event->channel);
1407 				break;
1408 
1409 			case ME_EXPRESSION:
1410 				channel[current_event->channel].expression=current_event->a;
1411 				adjust_volume(current_event->channel);
1412 				break;
1413 
1414 			case ME_PROGRAM:
1415 				/* if (ISDRUMCHANNEL(current_event->channel)) { */
1416 				if (channel[current_event->channel].kit)
1417 				{
1418 					/* Change drum set */
1419 					channel[current_event->channel].bank=current_event->a;
1420 				}
1421 				else
1422 				{
1423 					channel[current_event->channel].program=current_event->a;
1424 				}
1425 				break;
1426 
1427 			case ME_SUSTAIN:
1428 				channel[current_event->channel].sustain=current_event->a;
1429 				if (!current_event->a)
1430 					drop_sustain(current_event->channel);
1431 				break;
1432 
1433 			case ME_RESET_CONTROLLERS:
1434 				reset_controllers(current_event->channel);
1435 				redraw_controllers(current_event->channel);
1436 				break;
1437 
1438 			case ME_ALL_NOTES_OFF:
1439 				all_notes_off(current_event->channel);
1440 				break;
1441 
1442 			case ME_ALL_SOUNDS_OFF:
1443 				all_sounds_off(current_event->channel);
1444 				break;
1445 
1446 			case ME_HARMONICCONTENT:
1447 				channel[current_event->channel].harmoniccontent=current_event->a;
1448 				break;
1449 
1450 			case ME_RELEASETIME:
1451 				channel[current_event->channel].releasetime=current_event->a;
1452 				break;
1453 
1454 			case ME_ATTACKTIME:
1455 				channel[current_event->channel].attacktime=current_event->a;
1456 				break;
1457 
1458 			case ME_BRIGHTNESS:
1459 				channel[current_event->channel].brightness=current_event->a;
1460 				break;
1461 
1462 			case ME_TONE_BANK:
1463 				channel[current_event->channel].bank=current_event->a;
1464 				break;
1465 
1466 
1467 			case ME_TONE_KIT:
1468 				if (current_event->a==SFX_BANKTYPE)
1469 				{
1470 					channel[current_event->channel].sfx=SFXBANK;
1471 					channel[current_event->channel].kit=0;
1472 				}
1473 				else
1474 				{
1475 					channel[current_event->channel].sfx=0;
1476 					channel[current_event->channel].kit=current_event->a;
1477 				}
1478 				break;
1479 
1480 			case ME_EOT:
1481 				/* Give the last notes a couple of seconds to decay */
1482 				ctl_msg(CMSG_INFO, VERB_VERBOSE,
1483 						"Playing time: ~%d seconds", current_sample/play_mode_rate+2);
1484 				ctl_msg(CMSG_INFO, VERB_VERBOSE,
1485 						"Notes cut: %d", cut_notes);
1486 				ctl_msg(CMSG_INFO, VERB_VERBOSE,
1487 						"Notes lost totally: %d", lost_notes);
1488 				midi_playing = 0;
1489 
1490 				return false;
1491 		}
1492 	}
1493 
1494 	return true; // OK
1495 }
1496 
1497 //------------------------------------------------------------------------
1498 
Timidity_PlaySome(s16_t * stream,int samples)1499 int Timidity_PlaySome(s16_t *stream, int samples)
1500 {
1501 	if (! midi_playing)
1502 		return 0;
1503 
1504 //# I_Debugf("\nTimidity_PlaySome: samples=%d\n", samples);
1505 
1506 	int end_sample = current_sample + samples;
1507 	int total_done = 0;
1508 
1509 	while (current_sample < end_sample)
1510 	{
1511 		/* Handle all events that should happen at this time */
1512 		if (! handle_recent_events())
1513 			break;
1514 
1515 		int count = MIN(current_event->time, end_sample) - current_sample;
1516 		SYS_ASSERT(count > 0);
1517 
1518 		final_volume_t *buffer_pointer = NeedCommonBuf(count * num_ochannels);
1519 
1520 		do_compute_data(buffer_pointer, count);
1521 
1522 		s32tobuf(stream, buffer_pointer, count * num_ochannels);
1523 
1524 		stream += count * num_ochannels;
1525 		total_done += count;
1526 	}
1527 
1528 	return total_done;
1529 }
1530 
1531 
do_recompute_amps(void)1532 static void do_recompute_amps(void)
1533 {
1534 	if (! midi_playing)
1535 		return;
1536 
1537 	for (int i=0; i < MAX_VOICES; i++)
1538 	{
1539 		if (voice[i].status != VOICE_FREE)
1540 		{
1541 			recompute_amp(i);
1542 			apply_envelope_to_amp(i);
1543 		}
1544 	}
1545 }
1546 
Timidity_SetVolume(int volume)1547 void Timidity_SetVolume(int volume)
1548 {
1549 	amplification = CLAMP(0, volume, 1);
1550 
1551 	adjust_amplification();
1552 	do_recompute_amps();
1553 }
1554 
Timidity_QuietFactor(int factor)1555 void Timidity_QuietFactor(int factor)
1556 {
1557 	quiet_factor = factor;
1558 
1559 	adjust_amplification();
1560 	do_recompute_amps();
1561 }
1562 
Timidity_Start(struct MidiSong * song)1563 void Timidity_Start(struct MidiSong *song)
1564 {
1565 	load_missing_instruments();
1566 
1567 	adjust_amplification();
1568 
1569 	sample_count = song->samples;
1570 	event_list = song->events;
1571 	lost_notes=cut_notes=0;
1572 
1573 	current_sample=0;
1574 	current_event=event_list;
1575 
1576 	reset_midi();
1577 
1578 	midi_playing = 1;
1579 }
1580 
Timidity_Active(void)1581 int Timidity_Active(void)
1582 {
1583 	return midi_playing;
1584 }
1585 
Timidity_Stop(void)1586 void Timidity_Stop(void)
1587 {
1588 	midi_playing = 0;
1589 }
1590 
Timidity_FreeSong(struct MidiSong * song)1591 void Timidity_FreeSong(struct MidiSong *song)
1592 {
1593 	if (free_instruments_afterwards)
1594 		free_instruments();
1595 
1596 	free(song->events);
1597 	free(song);
1598 }
1599 
Timidity_Close(void)1600 void Timidity_Close(void)
1601 {
1602 	if (resample_buffer)
1603 	{
1604 		free(resample_buffer);
1605 		resample_buffer = NULL;
1606 	}
1607 	if (common_buffer)
1608 	{
1609 		free(common_buffer);
1610 		common_buffer = NULL;
1611 	}
1612 	free_instruments();
1613 	free_pathlist();
1614 }
1615 
1616 
1617 //--- editor settings ---
1618 // vi:ts=4:sw=4:noexpandtab
1619