1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 2018 Christoph Oelckers
4     Copyright (C) 1999-2009 Masanao Izumo <iz@onicos.co.jp>
5     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21     playmidi.c -- random stuff in need of rearrangement
22 */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <algorithm>
27 
28 #include <string.h>
29 #include <math.h>
30 #include "timidity.h"
31 #include "common.h"
32 #include "instrum.h"
33 #include "playmidi.h"
34 #include "mix.h"
35 #include "recache.h"
36 #include "reverb.h"
37 #include "freq.h"
38 #include "quantity.h"
39 #include "tables.h"
40 #include "effect.h"
41 
42 
43 namespace TimidityPlus
44 {
45 	FCriticalSection ConfigMutex;
46 	int timidity_modulation_wheel = true;
47 	int timidity_portamento = false;
48 	int timidity_reverb = 0;
49 	int timidity_chorus = 0;
50 	int timidity_surround_chorus = false;	// requires restart!
51 	int timidity_channel_pressure = false;
52 	int timidity_lpf_def = true;
53 	int timidity_temper_control = true;
54 	int timidity_modulation_envelope = true;
55 	int timidity_overlap_voice_allow = true;
56 	int timidity_drum_effect = false;
57 	int timidity_pan_delay = false;
58 	float timidity_drum_power = 1.f;
59 	int timidity_key_adjust = 0;
60 	float timidity_tempo_adjust = 1.f;
61 	float min_sustain_time = 5000;
62 
63 	// The following options have no generic use and are only meaningful for some SYSEX events not normally found in common MIDIs.
64 	// For now they are kept as unchanging global variables
65 	static bool opt_eq_control = false;
66 	static bool op_nrpn_vibrato = true;
67 	static bool opt_tva_attack = false;
68 	static bool opt_tva_decay = false;
69 	static bool opt_tva_release = false;
70 	static bool opt_insertion_effect = false;
71 	static bool opt_delay_control = false;
72 
73 
74 // These two variables need to remain global or things will get messy because they get accessed from non-class code.
75 int32_t control_ratio = 22;
76 int32_t playback_rate = 22050;
77 
78 #define PLAY_INTERLEAVE_SEC			1.0
79 #define PORTAMENTO_TIME_TUNING		(1.0 / 5000.0)
80 #define PORTAMENTO_CONTROL_RATIO	256	/* controls per sec */
81 #define DEFAULT_CHORUS_DELAY1		0.02
82 #define DEFAULT_CHORUS_DELAY2		0.003
83 #define CHORUS_OPPOSITE_THRESHOLD	32
84 #define EOT_PRESEARCH_LEN			32
85 #define SPEED_CHANGE_RATE			1.0594630943592953  /* 2^(1/12) */
86 #define DEFAULT_AMPLIFICATION 		70
87 #define VIBRATO_DEPTH_MAX 384	/* 600 cent */
88 
set_playback_rate(int freq)89 void set_playback_rate(int freq)
90 {
91 	const int CONTROLS_PER_SECOND = 1000;
92 	const int MAX_CONTROL_RATIO = 255;
93 
94 	playback_rate = freq;
95 	control_ratio = playback_rate / CONTROLS_PER_SECOND;
96 	if (control_ratio < 1)
97 		control_ratio = 1;
98 	else if (control_ratio > MAX_CONTROL_RATIO)
99 		control_ratio = MAX_CONTROL_RATIO;
100 }
101 
102 
Player(Instruments * instr)103 Player::Player(Instruments *instr)
104 {
105 	last_reverb_setting = timidity_reverb;
106 	memset(this, 0, sizeof(*this));
107 
108 	// init one-time global stuff - this should go to the device class once it exists.
109 	instruments = instr;
110 	initialize_resampler_coeffs();
111 	init_tables();
112 
113 	new_midi_file_info();
114 	init_mblock(&playmidi_pool);
115 
116 	reverb = new Reverb;
117 	reverb->init_effect_status(play_system_mode);
118 	effect = new Effect(reverb);
119 
120 
121 	mixer = new Mixer(this);
122 	recache = new Recache(this);
123 
124 	for (int i = 0; i < MAX_CHANNELS; i++)
125 		init_channel_layer(i);
126 
127 	instruments->init_userdrum();
128 	instruments->init_userinst();
129 
130 	master_volume_ratio = 0xFFFF;
131 	vol_table = def_vol_table;
132 
133 	play_system_mode = DEFAULT_SYSTEM_MODE;
134 	midi_streaming = 0;
135 	stream_max_compute = 500; /* compute time limit (in msec) when streaming */
136 	current_keysig = 0;
137 	current_temper_keysig = 0;
138 	temper_adj = 0;
139 	current_play_tempo = 500000;
140 	opt_realtime_playing = 0;
141 	check_eot_flag = 0;
142 	playmidi_seek_flag = 0;
143 	opt_pure_intonation = 0;
144 	current_freq_table = 0;
145 	current_temper_freq_table = 0;
146 	master_tuning = 0;
147 
148 	make_rvid_flag = 0; /* For reverb optimization */
149 
150 	voices = DEFAULT_VOICES;
151 	amplification = DEFAULT_AMPLIFICATION;
152 
153 
154 	static const int drums[] = { 10, -1 };
155 
156 	CLEAR_CHANNELMASK(default_drumchannels);
157 	for (int i = 0; drums[i] > 0; i++)
158 	{
159 		SET_CHANNELMASK(default_drumchannels, drums[i] - 1);
160 	}
161 	for (int i = 16; i < MAX_CHANNELS; i++)
162 	{
163 		if (IS_SET_CHANNELMASK(default_drumchannels, i & 0xF))
164 			SET_CHANNELMASK(default_drumchannels, i);
165 	}
166 	COPY_CHANNELMASK(drumchannels, default_drumchannels);
167 	COPY_CHANNELMASK(drumchannel_mask, default_drumchannel_mask);
168 
169 }
170 
~Player()171 Player::~Player()
172 {
173 	reuse_mblock(&playmidi_pool);
174 	if (reverb_buffer != nullptr) free(reverb_buffer);
175 	for (int i = 0; i < MAX_CHANNELS; i++) free_drum_effect(i);
176 	delete mixer;
177 	delete recache;
178 	delete effect;
179 	delete reverb;
180 }
181 
182 
IS_SYSEX_EVENT_TYPE(MidiEvent * event)183 bool Player::IS_SYSEX_EVENT_TYPE(MidiEvent *event)
184 {
185 	return ((event)->type == ME_NONE || (event)->type >= ME_RANDOM_PAN || (event)->b == SYSEX_TAG);
186 }
187 
188 
init_freq_table_user(void)189 void Player::init_freq_table_user(void)
190 {
191 	int p, i, j, k, l;
192 	double f;
193 
194 	for (p = 0; p < 4; p++)
195 		for (i = 0; i < 12; i++)
196 			for (j = -1; j < 11; j++) {
197 				f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
198 				for (k = 0; k < 12; k++) {
199 					l = i + j * 12 + k;
200 					if (l < 0 || l >= 128)
201 						continue;
202 					freq_table_user[p][i][l] = f * 1000 + 0.5;
203 					freq_table_user[p][i + 12][l] = f * 1000 + 0.5;
204 					freq_table_user[p][i + 24][l] = f * 1000 + 0.5;
205 					freq_table_user[p][i + 36][l] = f * 1000 + 0.5;
206 				}
207 			}
208 }
209 
210 
211 /*! convert Hz to internal vibrato control ratio. */
cnv_Hz_to_vib_ratio(double freq)212 double Player::cnv_Hz_to_vib_ratio(double freq)
213 {
214 	return ((double)(playback_rate) / (freq * 2.0f * VIBRATO_SAMPLE_INCREMENTS));
215 }
216 
adjust_amplification(void)217 void Player::adjust_amplification(void)
218 {
219 	static const double compensation_ratio = 1.0;
220     /* compensate master volume */
221     master_volume = (double)(amplification) / 100.0 *
222 	((double)master_volume_ratio * (compensation_ratio/0xFFFF));
223 }
224 
new_vidq(int ch,int note)225 int Player::new_vidq(int ch, int note)
226 {
227     int i;
228 
229     if(timidity_overlap_voice_allow)
230     {
231 	i = ch * 128 + note;
232 	return vidq_head[i]++;
233     }
234     return 0;
235 }
236 
last_vidq(int ch,int note)237 int Player::last_vidq(int ch, int note)
238 {
239     int i;
240 
241     if(timidity_overlap_voice_allow)
242     {
243 	i = ch * 128 + note;
244 	if(vidq_head[i] == vidq_tail[i])
245 	{
246 	    return -1;
247 	}
248 	return vidq_tail[i]++;
249     }
250     return 0;
251 }
252 
reset_voices(void)253 void Player::reset_voices(void)
254 {
255     int i;
256     for(i = 0; i < max_voices; i++)
257     {
258 	voice[i].status = VOICE_FREE;
259 	voice[i].temper_instant = 0;
260 	voice[i].chorus_link = i;
261     }
262     upper_voices = 0;
263     memset(vidq_head, 0, sizeof(vidq_head));
264     memset(vidq_tail, 0, sizeof(vidq_tail));
265 }
266 
kill_note(int i)267 void Player::kill_note(int i)
268 {
269     voice[i].status = VOICE_DIE;
270 }
271 
kill_all_voices(void)272 void Player::kill_all_voices(void)
273 {
274     int i, uv = upper_voices;
275 
276     for(i = 0; i < uv; i++)
277 	if(voice[i].status & ~(VOICE_FREE | VOICE_DIE))
278 	    kill_note(i);
279     memset(vidq_head, 0, sizeof(vidq_head));
280     memset(vidq_tail, 0, sizeof(vidq_tail));
281 }
282 
reset_drum_controllers(struct DrumParts * d[],int note)283 void Player::reset_drum_controllers(struct DrumParts *d[], int note)
284 {
285 	int i, j;
286 
287 	if (note == -1)
288 	{
289 		for (i = 0; i < 128; i++)
290 			if (d[i] != NULL)
291 			{
292 				d[i]->drum_panning = NO_PANNING;
293 				for (j = 0; j < 6; j++) { d[i]->drum_envelope_rate[j] = -1; }
294 				d[i]->pan_random = 0;
295 				d[i]->drum_level = 1.0f;
296 				d[i]->coarse = 0;
297 				d[i]->fine = 0;
298 				d[i]->delay_level = -1;
299 				d[i]->chorus_level = -1;
300 				d[i]->reverb_level = -1;
301 				d[i]->play_note = -1;
302 				d[i]->drum_cutoff_freq = 0;
303 				d[i]->drum_resonance = 0;
304 				init_rx_drum(d[i]);
305 			}
306 	}
307 	else
308 	{
309 		d[note]->drum_panning = NO_PANNING;
310 		for (j = 0; j < 6; j++) { d[note]->drum_envelope_rate[j] = -1; }
311 		d[note]->pan_random = 0;
312 		d[note]->drum_level = 1.0f;
313 		d[note]->coarse = 0;
314 		d[note]->fine = 0;
315 		d[note]->delay_level = -1;
316 		d[note]->chorus_level = -1;
317 		d[note]->reverb_level = -1;
318 		d[note]->play_note = -1;
319 		d[note]->drum_cutoff_freq = 0;
320 		d[note]->drum_resonance = 0;
321 		init_rx_drum(d[note]);
322 	}
323 }
324 
reset_nrpn_controllers(int c)325 void Player::reset_nrpn_controllers(int c)
326 {
327 	int i;
328 
329 	/* NRPN */
330 	reset_drum_controllers(channel[c].drums, -1);
331 	channel[c].vibrato_ratio = 1.0;
332 	channel[c].vibrato_depth = 0;
333 	channel[c].vibrato_delay = 0;
334 	channel[c].param_cutoff_freq = 0;
335 	channel[c].param_resonance = 0;
336 	channel[c].cutoff_freq_coef = 1.0;
337 	channel[c].resonance_dB = 0;
338 
339 	/* System Exclusive */
340 	channel[c].dry_level = 127;
341 	channel[c].eq_gs = 1;
342 	channel[c].insertion_effect = 0;
343 	channel[c].velocity_sense_depth = 0x40;
344 	channel[c].velocity_sense_offset = 0x40;
345 	channel[c].pitch_offset_fine = 0;
346 	if (play_system_mode == GS_SYSTEM_MODE) { channel[c].assign_mode = 1; }
347 	else {
348 		if (ISDRUMCHANNEL(c)) { channel[c].assign_mode = 1; }
349 		else { channel[c].assign_mode = 2; }
350 	}
351 	for (i = 0; i < 12; i++)
352 		channel[c].scale_tuning[i] = 0;
353 	channel[c].prev_scale_tuning = 0;
354 	channel[c].temper_type = 0;
355 
356 	init_channel_layer(c);
357 	init_part_eq_xg(&(channel[c].eq_xg));
358 
359 	/* channel pressure & polyphonic key pressure control */
360 	init_midi_controller(&(channel[c].mod));
361 	init_midi_controller(&(channel[c].bend));
362 	init_midi_controller(&(channel[c].caf));
363 	init_midi_controller(&(channel[c].paf));
364 	init_midi_controller(&(channel[c].cc1));
365 	init_midi_controller(&(channel[c].cc2));
366 	channel[c].bend.pitch = 2;
367 
368 	init_rx(c);
369 	channel[c].note_limit_high = 127;
370 	channel[c].note_limit_low = 0;
371 	channel[c].vel_limit_high = 127;
372 	channel[c].vel_limit_low = 0;
373 
374 	free_drum_effect(c);
375 
376 	channel[c].legato = 0;
377 	channel[c].damper_mode = 0;
378 	channel[c].loop_timeout = 0;
379 
380 	channel[c].sysex_gs_msb_addr = channel[c].sysex_gs_msb_val =
381 		channel[c].sysex_xg_msb_addr = channel[c].sysex_xg_msb_val =
382 		channel[c].sysex_msb_addr = channel[c].sysex_msb_val = 0;
383 }
384 
385 /* Process the Reset All Controllers event */
reset_controllers(int c)386 void Player::reset_controllers(int c)
387 {
388 	int j;
389 	/* Some standard says, although the SCC docs say 0. */
390 
391 	if (play_system_mode == XG_SYSTEM_MODE)
392 		channel[c].volume = 100;
393 	else
394 		channel[c].volume = 90;
395 
396 	channel[c].expression = 127; /* SCC-1 does this. */
397 	channel[c].sustain = 0;
398 	channel[c].sostenuto = 0;
399 	channel[c].pitchbend = 0x2000;
400 	channel[c].pitchfactor = 0; /* to be computed */
401 	channel[c].mod.val = 0;
402 	channel[c].bend.val = 0;
403 	channel[c].caf.val = 0;
404 	channel[c].paf.val = 0;
405 	channel[c].cc1.val = 0;
406 	channel[c].cc2.val = 0;
407 	channel[c].portamento_time_lsb = 0;
408 	channel[c].portamento_time_msb = 0;
409 	channel[c].porta_control_ratio = 0;
410 	channel[c].portamento = 0;
411 	channel[c].last_note_fine = -1;
412 	for (j = 0; j < 6; j++) { channel[c].envelope_rate[j] = -1; }
413 	update_portamento_controls(c);
414 	set_reverb_level(c, -1);
415 	if (timidity_chorus == 1)
416 		channel[c].chorus_level = 0;
417 	else
418 		channel[c].chorus_level = -timidity_chorus;
419 	channel[c].mono = 0;
420 	channel[c].delay_level = 0;
421 }
422 
get_default_mapID(int ch)423 int Player::get_default_mapID(int ch)
424 {
425 	if (play_system_mode == XG_SYSTEM_MODE)
426 		return ISDRUMCHANNEL(ch) ? XG_DRUM_MAP : XG_NORMAL_MAP;
427 	return INST_NO_MAP;
428 }
429 
reset_midi(int playing)430 void Player::reset_midi(int playing)
431 {
432 	int i;
433 
434 	for (i = 0; i < MAX_CHANNELS; i++) {
435 		reset_controllers(i);
436 		reset_nrpn_controllers(i);
437 		channel[i].tone_map0_number = 0;
438 		channel[i].mod.lfo1_pitch_depth = 50;
439 		/* The rest of these are unaffected
440 		 * by the Reset All Controllers event
441 		 */
442 		channel[i].program = instruments->defaultProgram(i);
443 		channel[i].panning = NO_PANNING;
444 		channel[i].pan_random = 0;
445 		/* tone bank or drum set */
446 		if (ISDRUMCHANNEL(i)) {
447 			channel[i].bank = 0;
448 			channel[i].altassign = instruments->drumSet(0)->alt;
449 		} else {
450 			if (special_tonebank >= 0)
451 				channel[i].bank = special_tonebank;
452 			else
453 				channel[i].bank = default_tonebank;
454 		}
455 		channel[i].bank_lsb = channel[i].bank_msb = 0;
456 		if (play_system_mode == XG_SYSTEM_MODE && i % 16 == 9)
457 			channel[i].bank_msb = 127;	/* Use MSB=127 for XG */
458 		update_rpn_map(i, RPN_ADDR_FFFF, 0);
459 		channel[i].special_sample = 0;
460 		channel[i].key_shift = 0;
461 		channel[i].mapID = get_default_mapID(i);
462 		channel[i].lasttime = 0;
463 	}
464 	if (playing) {
465 		kill_all_voices();
466 		if (temper_type_mute) {
467 			if (temper_type_mute & 1)
468 				FILL_CHANNELMASK(channel_mute);
469 			else
470 				CLEAR_CHANNELMASK(channel_mute);
471 		}
472 	} else
473 		reset_voices();
474 	master_volume_ratio = 0xffff;
475 	adjust_amplification();
476 	master_tuning = 0;
477 	if (current_file_info) {
478 		COPY_CHANNELMASK(drumchannels, current_file_info->drumchannels);
479 		COPY_CHANNELMASK(drumchannel_mask,
480 				current_file_info->drumchannel_mask);
481 	} else {
482 		COPY_CHANNELMASK(drumchannels, default_drumchannels);
483 		COPY_CHANNELMASK(drumchannel_mask, default_drumchannel_mask);
484 	}
485 }
486 
recompute_freq(int v)487 void Player::recompute_freq(int v)
488 {
489 	int i;
490 	int ch = voice[v].channel;
491 	int note = voice[v].note;
492 	int32_t tuning = 0;
493 	int8_t st = channel[ch].scale_tuning[note % 12];
494 	int8_t tt = channel[ch].temper_type;
495 	uint8_t tp = channel[ch].rpnmap[RPN_ADDR_0003];
496 	int32_t f;
497 	int pb = channel[ch].pitchbend;
498 	int32_t tmp;
499 	double pf, root_freq;
500 	int32_t a;
501 	Voice *vp = &(voice[v]);
502 
503 	if (! voice[v].sample->sample_rate)
504 		return;
505 	if (! timidity_modulation_wheel)
506 		channel[ch].mod.val = 0;
507 	if (! timidity_portamento)
508 		voice[v].porta_control_ratio = 0;
509 	voice[v].vibrato_control_ratio = voice[v].orig_vibrato_control_ratio;
510 	if (voice[v].vibrato_control_ratio || channel[ch].mod.val > 0) {
511 		/* This instrument has vibrato. Invalidate any precomputed
512 		 * sample_increments.
513 		 */
514 
515 		/* MIDI controllers LFO pitch depth */
516 		if (timidity_channel_pressure || timidity_modulation_wheel) {
517 			vp->vibrato_depth = vp->sample->vibrato_depth + channel[ch].vibrato_depth;
518 			vp->vibrato_depth += get_midi_controller_pitch_depth(&(channel[ch].mod))
519 				+ get_midi_controller_pitch_depth(&(channel[ch].bend))
520 				+ get_midi_controller_pitch_depth(&(channel[ch].caf))
521 				+ get_midi_controller_pitch_depth(&(channel[ch].paf))
522 				+ get_midi_controller_pitch_depth(&(channel[ch].cc1))
523 				+ get_midi_controller_pitch_depth(&(channel[ch].cc2));
524 			if (vp->vibrato_depth > VIBRATO_DEPTH_MAX) {vp->vibrato_depth = VIBRATO_DEPTH_MAX;}
525 			else if (vp->vibrato_depth < 1) {vp->vibrato_depth = 1;}
526 			if (vp->sample->vibrato_depth < 0) {	/* in opposite phase */
527 				vp->vibrato_depth = -vp->vibrato_depth;
528 			}
529 		}
530 
531 		/* fill parameters for modulation wheel */
532 		if (channel[ch].mod.val > 0) {
533 			if(vp->vibrato_control_ratio == 0) {
534 				vp->vibrato_control_ratio =
535 					vp->orig_vibrato_control_ratio = (int)(cnv_Hz_to_vib_ratio(5.0) * channel[ch].vibrato_ratio);
536 			}
537 			vp->vibrato_delay = 0;
538 		}
539 
540 		for (i = 0; i < VIBRATO_SAMPLE_INCREMENTS; i++)
541 			vp->vibrato_sample_increment[i] = 0;
542 		vp->cache = NULL;
543 	}
544 	/* At least for GM2, it's recommended not to apply master_tuning for drum channels */
545 	tuning = ISDRUMCHANNEL(ch) ? 0 : master_tuning;
546 	/* fine: [0..128] => [-256..256]
547 	 * 1 coarse = 256 fine (= 1 note)
548 	 * 1 fine = 2^5 tuning
549 	 */
550 	tuning += (channel[ch].rpnmap[RPN_ADDR_0001] - 0x40
551 			+ (channel[ch].rpnmap[RPN_ADDR_0002] - 0x40) * 64) << 7;
552 	/* for NRPN Coarse Pitch of Drum (GS) & Fine Pitch of Drum (XG) */
553 	if (ISDRUMCHANNEL(ch) && channel[ch].drums[note] != NULL
554 			&& (channel[ch].drums[note]->fine
555 			|| channel[ch].drums[note]->coarse)) {
556 		tuning += (channel[ch].drums[note]->fine
557 				+ channel[ch].drums[note]->coarse * 64) << 7;
558 	}
559 	/* MIDI controllers pitch control */
560 	if (timidity_channel_pressure) {
561 		tuning += get_midi_controller_pitch(&(channel[ch].mod))
562 			+ get_midi_controller_pitch(&(channel[ch].bend))
563 			+ get_midi_controller_pitch(&(channel[ch].caf))
564 			+ get_midi_controller_pitch(&(channel[ch].paf))
565 			+ get_midi_controller_pitch(&(channel[ch].cc1))
566 			+ get_midi_controller_pitch(&(channel[ch].cc2));
567 	}
568 	if (timidity_modulation_envelope) {
569 		if (voice[v].sample->tremolo_to_pitch) {
570 			tuning += lookup_triangular(voice[v].tremolo_phase >> RATE_SHIFT)
571 					* (voice[v].sample->tremolo_to_pitch << 13) / 100.0 + 0.5;
572 			channel[ch].pitchfactor = 0;
573 		}
574 		if (voice[v].sample->modenv_to_pitch) {
575 			tuning += voice[v].last_modenv_volume
576 					* (voice[v].sample->modenv_to_pitch << 13) / 100.0 + 0.5;
577 			channel[ch].pitchfactor = 0;
578 		}
579 	}
580 	/* GS/XG - Scale Tuning */
581 	if (! ISDRUMCHANNEL(ch)) {
582 		tuning += ((st << 13) + 50) / 100;
583 		if (st != channel[ch].prev_scale_tuning) {
584 			channel[ch].pitchfactor = 0;
585 			channel[ch].prev_scale_tuning = st;
586 		}
587 	}
588 	if (! opt_pure_intonation
589 			&& timidity_temper_control && voice[v].temper_instant) {
590 		switch (tt) {
591 		case 0:
592 			f = freq_table_tuning[tp][note];
593 			break;
594 		case 1:
595 			if (current_temper_keysig < 8)
596 				f = freq_table_pytha[current_temper_freq_table][note];
597 			else
598 				f = freq_table_pytha[current_temper_freq_table + 12][note];
599 			break;
600 		case 2:
601 			if (current_temper_keysig < 8)
602 				f = freq_table_meantone[current_temper_freq_table
603 						+ ((temper_adj) ? 36 : 0)][note];
604 			else
605 				f = freq_table_meantone[current_temper_freq_table
606 						+ ((temper_adj) ? 24 : 12)][note];
607 			break;
608 		case 3:
609 			if (current_temper_keysig < 8)
610 				f = freq_table_pureint[current_temper_freq_table
611 						+ ((temper_adj) ? 36 : 0)][note];
612 			else
613 				f = freq_table_pureint[current_temper_freq_table
614 						+ ((temper_adj) ? 24 : 12)][note];
615 			break;
616 		default:	/* user-defined temperament */
617 			if ((tt -= 0x40) >= 0 && tt < 4) {
618 				if (current_temper_keysig < 8)
619 					f = freq_table_user[tt][current_temper_freq_table
620 							+ ((temper_adj) ? 36 : 0)][note];
621 				else
622 					f = freq_table_user[tt][current_temper_freq_table
623 							+ ((temper_adj) ? 24 : 12)][note];
624 			} else
625 				f = freq_table[note];
626 			break;
627 		}
628 		voice[v].orig_frequency = f;
629 	}
630 	if (! voice[v].porta_control_ratio) {
631 		if (tuning == 0 && pb == 0x2000)
632 			voice[v].frequency = voice[v].orig_frequency;
633 		else {
634 			pb -= 0x2000;
635 			if (! channel[ch].pitchfactor) {
636 				/* Damn.  Somebody bent the pitch. */
637 				tmp = pb * channel[ch].rpnmap[RPN_ADDR_0000] + tuning;
638 				if (tmp >= 0)
639 					channel[ch].pitchfactor = bend_fine[tmp >> 5 & 0xff]
640 							* bend_coarse[tmp >> 13 & 0x7f];
641 				else
642 					channel[ch].pitchfactor = 1.0 /
643 							(bend_fine[-tmp >> 5 & 0xff]
644 							* bend_coarse[-tmp >> 13 & 0x7f]);
645 			}
646 			voice[v].frequency =
647 					voice[v].orig_frequency * channel[ch].pitchfactor;
648 			if (voice[v].frequency != voice[v].orig_frequency)
649 				voice[v].cache = NULL;
650 		}
651 	} else {	/* Portamento */
652 		pb -= 0x2000;
653 		tmp = pb * channel[ch].rpnmap[RPN_ADDR_0000]
654 				+ (voice[v].porta_pb << 5) + tuning;
655 		if (tmp >= 0)
656 			pf = bend_fine[tmp >> 5 & 0xff]
657 					* bend_coarse[tmp >> 13 & 0x7f];
658 		else
659 			pf = 1.0 / (bend_fine[-tmp >> 5 & 0xff]
660 					* bend_coarse[-tmp >> 13 & 0x7f]);
661 		voice[v].frequency = voice[v].orig_frequency * pf;
662 		voice[v].cache = NULL;
663 	}
664 	root_freq = voice[v].sample->root_freq;
665 	a = TIM_FSCALE(((double) voice[v].sample->sample_rate
666 			* ((double)voice[v].frequency + channel[ch].pitch_offset_fine))
667 			/ (root_freq * playback_rate), FRACTION_BITS) + 0.5;
668 	/* need to preserve the loop direction */
669 	voice[v].sample_increment = (voice[v].sample_increment >= 0) ? a : -a;
670 }
671 
calc_velocity(int32_t ch,int32_t vel)672 int32_t Player::calc_velocity(int32_t ch,int32_t vel)
673 {
674 	int32_t velocity;
675 	velocity = channel[ch].velocity_sense_depth * vel / 64 + (channel[ch].velocity_sense_offset - 64) * 2;
676 	if(velocity > 127) {velocity = 127;}
677 	return velocity;
678 }
679 
recompute_voice_tremolo(int v)680 void Player::recompute_voice_tremolo(int v)
681 {
682 	Voice *vp = &(voice[v]);
683 	int ch = vp->channel;
684 	int32_t depth = vp->sample->tremolo_depth;
685 	depth += get_midi_controller_amp_depth(&(channel[ch].mod))
686 		+ get_midi_controller_amp_depth(&(channel[ch].bend))
687 		+ get_midi_controller_amp_depth(&(channel[ch].caf))
688 		+ get_midi_controller_amp_depth(&(channel[ch].paf))
689 		+ get_midi_controller_amp_depth(&(channel[ch].cc1))
690 		+ get_midi_controller_amp_depth(&(channel[ch].cc2));
691 	if(depth > 256) {depth = 256;}
692 	vp->tremolo_depth = depth;
693 }
694 
recompute_amp(int v)695 void Player::recompute_amp(int v)
696 {
697 	double tempamp;
698 	int ch = voice[v].channel;
699 
700 	/* master_volume and sample->volume are percentages, used to scale
701 	 *  amplitude directly, NOT perceived volume
702 	 *
703 	 * all other MIDI volumes are linear in perceived volume, 0-127
704 	 * use a lookup table for the non-linear scalings
705 	 */
706 	if (play_system_mode == GM2_SYSTEM_MODE) {
707 		tempamp = master_volume *
708 			voice[v].sample->volume *
709 			gm2_vol_table[calc_velocity(ch, voice[v].velocity)] *	/* velocity: not in GM2 standard */
710 			gm2_vol_table[channel[ch].volume] *
711 			gm2_vol_table[channel[ch].expression]; /* 21 bits */
712 	}
713 	else if (play_system_mode == GS_SYSTEM_MODE) {	/* use measured curve */
714 		tempamp = master_volume *
715 			voice[v].sample->volume *
716 			sc_vel_table[calc_velocity(ch, voice[v].velocity)] *
717 			sc_vol_table[channel[ch].volume] *
718 			sc_vol_table[channel[ch].expression]; /* 21 bits */
719 	}
720 	else {	/* use generic exponential curve */
721 		tempamp = master_volume *
722 			voice[v].sample->volume *
723 			perceived_vol_table[calc_velocity(ch, voice[v].velocity)] *
724 			perceived_vol_table[channel[ch].volume] *
725 			perceived_vol_table[channel[ch].expression]; /* 21 bits */
726 	}
727 
728 	/* every digital effect increases amplitude,
729 	 * so that it must be reduced in advance.
730 	 */
731 	if (
732 		(timidity_reverb || timidity_chorus || opt_delay_control
733 			|| (opt_eq_control && (reverb->eq_status_gs.low_gain != 0x40
734 				|| reverb->eq_status_gs.high_gain != 0x40))
735 			|| opt_insertion_effect))
736 		tempamp *= 1.35f * 0.55f;
737 	else
738 		tempamp *= 1.35f;
739 
740 	/* Reduce amplitude for chorus partners.
741 	 * 2x voices -> 2x power -> sqrt(2)x amplitude.
742 	 * 1 / sqrt(2) = ~0.7071, which is very close to the old
743 	 * CHORUS_VELOCITY_TUNING1 value of 0.7.
744 	 *
745 	 * The previous amp scaling for the various digital effects should
746 	 * really be redone to split them into separate scalings for each
747 	 * effect, rather than a single scaling if any one of them is used
748 	 * (which is NOT correct).  As it is now, if partner chorus is the
749 	 * only effect in use, then it is reduced in volume twice and winds
750 	 * up too quiet.  Compare the output of "-EFreverb=0 -EFchorus=0",
751 	 * "-EFreverb=0 -EFchorus=2", "-EFreverb=4 -EFchorus=2", and
752 	 * "-EFreverb=4 -EFchorus=0" to see how the digital effect volumes
753 	 * are not scaled properly.  Idealy, all the resulting output should
754 	 * have the same volume, regardless of effects used.  This will
755 	 * require empirically determining the amount to scale for each
756 	 * individual effect.
757 	 */
758 	if (voice[v].chorus_link != v)
759 		tempamp *= 0.7071067811865f;
760 
761 	/* NRPN - drum instrument tva level */
762 	if (ISDRUMCHANNEL(ch)) {
763 		if (channel[ch].drums[voice[v].note] != NULL) {
764 			tempamp *= channel[ch].drums[voice[v].note]->drum_level;
765 		}
766 		tempamp *= (double)timidity_drum_power;	/* global drum power */
767 	}
768 
769 	/* MIDI controllers amplitude control */
770 	if (timidity_channel_pressure) {
771 		tempamp *= get_midi_controller_amp(&(channel[ch].mod))
772 			* get_midi_controller_amp(&(channel[ch].bend))
773 			* get_midi_controller_amp(&(channel[ch].caf))
774 			* get_midi_controller_amp(&(channel[ch].paf))
775 			* get_midi_controller_amp(&(channel[ch].cc1))
776 			* get_midi_controller_amp(&(channel[ch].cc2));
777 		recompute_voice_tremolo(v);
778 	}
779 
780 	if (voice[v].fc.type != 0) {
781 		tempamp *= voice[v].fc.gain;	/* filter gain */
782 	}
783 
784 	/* applying panning to amplitude */
785 	if (true)
786 	{
787 		if (voice[v].panning == 64)
788 		{
789 			voice[v].panned = PANNED_CENTER;
790 			voice[v].left_amp = voice[v].right_amp = TIM_FSCALENEG(tempamp * pan_table[64], 27);
791 		}
792 		else if (voice[v].panning < 2)
793 		{
794 			voice[v].panned = PANNED_LEFT;
795 			voice[v].left_amp = TIM_FSCALENEG(tempamp, 20);
796 			voice[v].right_amp = 0;
797 		}
798 		else if (voice[v].panning == 127)
799 		{
800 			if (voice[v].panned == PANNED_MYSTERY) {
801 				voice[v].old_left_mix = voice[v].old_right_mix;
802 				voice[v].old_right_mix = 0;
803 			}
804 			voice[v].panned = PANNED_RIGHT;
805 			voice[v].left_amp = TIM_FSCALENEG(tempamp, 20);
806 			voice[v].right_amp = 0;
807 		}
808 		else
809 		{
810 			if (voice[v].panned == PANNED_RIGHT) {
811 				voice[v].old_right_mix = voice[v].old_left_mix;
812 				voice[v].old_left_mix = 0;
813 			}
814 			voice[v].panned = PANNED_MYSTERY;
815 			voice[v].left_amp = TIM_FSCALENEG(tempamp * pan_table[128 - voice[v].panning], 27);
816 			voice[v].right_amp = TIM_FSCALENEG(tempamp * pan_table[voice[v].panning], 27);
817 		}
818 	}
819 	else
820 	{
821 		voice[v].panned = PANNED_CENTER;
822 		voice[v].left_amp = TIM_FSCALENEG(tempamp, 21);
823 	}
824 }
825 
826 #define RESONANCE_COEFF 0.2393
827 
recompute_channel_filter(int ch,int note)828 void Player::recompute_channel_filter(int ch, int note)
829 {
830 	double coef = 1.0f, reso = 0;
831 
832 	if(channel[ch].special_sample > 0) {return;}
833 
834 	/* Soft Pedal */
835 	if(channel[ch].soft_pedal != 0) {
836 		if(note > 49) {	/* tre corde */
837 			coef *= 1.0 - 0.20 * ((double)channel[ch].soft_pedal) / 127.0f;
838 		} else {	/* una corda (due corde) */
839 			coef *= 1.0 - 0.25 * ((double)channel[ch].soft_pedal) / 127.0f;
840 		}
841 	}
842 
843 	if(!ISDRUMCHANNEL(ch)) {
844 		/* NRPN Filter Cutoff */
845 		coef *= pow(1.26, (double)(channel[ch].param_cutoff_freq) / 8.0f);
846 		/* NRPN Resonance */
847 		reso = (double)channel[ch].param_resonance * RESONANCE_COEFF;
848 	}
849 
850 	channel[ch].cutoff_freq_coef = coef;
851 	channel[ch].resonance_dB = reso;
852 }
853 
init_voice_filter(int i)854 void Player::init_voice_filter(int i)
855 {
856   memset(&(voice[i].fc), 0, sizeof(FilterCoefficients));
857   if(timidity_lpf_def && voice[i].sample->cutoff_freq) {
858 	  voice[i].fc.orig_freq = voice[i].sample->cutoff_freq;
859 	  voice[i].fc.orig_reso_dB = (double)voice[i].sample->resonance / 10.0f - 3.01f;
860 	  if (voice[i].fc.orig_reso_dB < 0.0f) {voice[i].fc.orig_reso_dB = 0.0f;}
861 	  if (timidity_lpf_def == 2) {
862 		  voice[i].fc.gain = 1.0;
863 		  voice[i].fc.type = 2;
864 	  } else if(timidity_lpf_def == 1) {
865 		  voice[i].fc.gain = pow(10.0f, -voice[i].fc.orig_reso_dB / 2.0f / 20.0f);
866 		  voice[i].fc.type = 1;
867 	  }
868 	  voice[i].fc.start_flag = 0;
869   } else {
870 	  voice[i].fc.type = 0;
871   }
872 }
873 
874 #define CHAMBERLIN_RESONANCE_MAX 24.0
875 
recompute_voice_filter(int v)876 void Player::recompute_voice_filter(int v)
877 {
878 	int ch = voice[v].channel, note = voice[v].note;
879 	double coef = 1.0, reso = 0, cent = 0, depth_cent = 0, freq;
880 	FilterCoefficients *fc = &(voice[v].fc);
881 	Sample *sp = (Sample *) &voice[v].sample;
882 
883 	if(fc->type == 0) {return;}
884 	coef = channel[ch].cutoff_freq_coef;
885 
886 	if(ISDRUMCHANNEL(ch) && channel[ch].drums[note] != NULL) {
887 		/* NRPN Drum Instrument Filter Cutoff */
888 		coef *= pow(1.26, (double)(channel[ch].drums[note]->drum_cutoff_freq) / 8.0f);
889 		/* NRPN Drum Instrument Filter Resonance */
890 		reso += (double)channel[ch].drums[note]->drum_resonance * RESONANCE_COEFF;
891 	}
892 
893 	/* MIDI controllers filter cutoff control and LFO filter depth */
894 	if(timidity_channel_pressure) {
895 		cent += get_midi_controller_filter_cutoff(&(channel[ch].mod))
896 			+ get_midi_controller_filter_cutoff(&(channel[ch].bend))
897 			+ get_midi_controller_filter_cutoff(&(channel[ch].caf))
898 			+ get_midi_controller_filter_cutoff(&(channel[ch].paf))
899 			+ get_midi_controller_filter_cutoff(&(channel[ch].cc1))
900 			+ get_midi_controller_filter_cutoff(&(channel[ch].cc2));
901 		depth_cent += get_midi_controller_filter_depth(&(channel[ch].mod))
902 			+ get_midi_controller_filter_depth(&(channel[ch].bend))
903 			+ get_midi_controller_filter_depth(&(channel[ch].caf))
904 			+ get_midi_controller_filter_depth(&(channel[ch].paf))
905 			+ get_midi_controller_filter_depth(&(channel[ch].cc1))
906 			+ get_midi_controller_filter_depth(&(channel[ch].cc2));
907 	}
908 
909 	if(sp->vel_to_fc) {	/* velocity to filter cutoff frequency */
910 		if(voice[v].velocity > sp->vel_to_fc_threshold)
911 			cent += sp->vel_to_fc * (double)(127 - voice[v].velocity) / 127.0f;
912 		else
913 			coef += sp->vel_to_fc * (double)(127 - sp->vel_to_fc_threshold) / 127.0f;
914 	}
915 	if(sp->vel_to_resonance) {	/* velocity to filter resonance */
916 		reso += (double)voice[v].velocity * sp->vel_to_resonance / 127.0f / 10.0f;
917 	}
918 	if(sp->key_to_fc) {	/* filter cutoff key-follow */
919 		cent += sp->key_to_fc * (double)(voice[v].note - sp->key_to_fc_bpo);
920 	}
921 
922 	if(timidity_modulation_envelope) {
923 		if(voice[v].sample->tremolo_to_fc + (int16_t)depth_cent) {
924 			cent += ((double)voice[v].sample->tremolo_to_fc + depth_cent) * lookup_triangular(voice[v].tremolo_phase >> RATE_SHIFT);
925 		}
926 		if(voice[v].sample->modenv_to_fc) {
927 			cent += (double)voice[v].sample->modenv_to_fc * voice[v].last_modenv_volume;
928 		}
929 	}
930 
931 	if(cent != 0) {coef *= pow(2.0, cent / 1200.0f);}
932 
933 	freq = (double)fc->orig_freq * coef;
934 
935 	if (freq > playback_rate / 2) {freq = playback_rate / 2;}
936 	else if(freq < 5) {freq = 5;}
937 	fc->freq = (int32_t)freq;
938 
939 	fc->reso_dB = fc->orig_reso_dB + channel[ch].resonance_dB + reso;
940 	if(fc->reso_dB < 0.0f) {fc->reso_dB = 0.0f;}
941 	else if(fc->reso_dB > 96.0f) {fc->reso_dB = 96.0f;}
942 
943 	if(fc->type == 1) {	/* Chamberlin filter */
944 		if(fc->freq > playback_rate / 6) {
945 			if (fc->start_flag == 0) {fc->type = 0;}	/* turn off. */
946 			else {fc->freq = playback_rate / 6;}
947 		}
948 		if(fc->reso_dB > CHAMBERLIN_RESONANCE_MAX) {fc->reso_dB = CHAMBERLIN_RESONANCE_MAX;}
949 	} else if(fc->type == 2) {	/* Moog VCF */
950 		if(fc->reso_dB > fc->orig_reso_dB / 2) {
951 			fc->gain = pow(10.0f, (fc->reso_dB - fc->orig_reso_dB / 2) / 20.0f);
952 		}
953 	}
954 	fc->start_flag = 1;	/* filter is started. */
955 }
956 
calc_drum_tva_level(int ch,int note,int level)957 float Player::calc_drum_tva_level(int ch, int note, int level)
958 {
959 	int def_level, nbank, nprog;
960 	const ToneBank *bank;
961 
962 	if(channel[ch].special_sample > 0) {return 1.0;}
963 
964 	nbank = channel[ch].bank;
965 	nprog = note;
966 	instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
967 
968 	if(ISDRUMCHANNEL(ch)) {
969 		bank = instruments->drumSet(nbank);
970 		if(bank == NULL) {bank = instruments->drumSet(0);}
971 	} else {
972 		return 1.0;
973 	}
974 
975 	def_level = bank->tone[nprog].tva_level;
976 
977 	if(def_level == -1 || def_level == 0) {def_level = 127;}
978 	else if(def_level > 127) {def_level = 127;}
979 
980 	return (sc_drum_level_table[level] / sc_drum_level_table[def_level]);
981 }
982 
calc_random_delay(int ch,int note)983 int32_t Player::calc_random_delay(int ch, int note)
984 {
985 	int nbank, nprog;
986 	const ToneBank *bank;
987 
988 	if(channel[ch].special_sample > 0) {return 0;}
989 
990 	nbank = channel[ch].bank;
991 
992 	if(ISDRUMCHANNEL(ch)) {
993 		nprog = note;
994 		instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
995 		bank = instruments->drumSet(nbank);
996 		if (bank == NULL) {bank = instruments->drumSet(0);}
997 	} else {
998 		nprog = channel[ch].program;
999 		if(nprog == SPECIAL_PROGRAM) {return 0;}
1000 		instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
1001 		bank = instruments->toneBank(nbank);
1002 		if(bank == NULL) {bank = instruments->toneBank(0);}
1003 	}
1004 
1005 	if (bank->tone[nprog].rnddelay == 0) {return 0;}
1006 	else {return (int32_t)((double)bank->tone[nprog].rnddelay * playback_rate / 1000.0
1007 		* (reverb->get_pink_noise_light(&reverb->global_pink_noise_light) + 1.0f) * 0.5);}
1008 }
1009 
recompute_bank_parameter(int ch,int note)1010 void Player::recompute_bank_parameter(int ch, int note)
1011 {
1012 	int nbank, nprog;
1013 	const ToneBank *bank;
1014 	struct DrumParts *drum;
1015 
1016 	if(channel[ch].special_sample > 0) {return;}
1017 
1018 	nbank = channel[ch].bank;
1019 
1020 	if(ISDRUMCHANNEL(ch)) {
1021 		nprog = note;
1022 		instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
1023 		bank = instruments->drumSet(nbank);
1024 		if (bank == NULL) {bank = instruments->drumSet(0);}
1025 		if (channel[ch].drums[note] == NULL)
1026 				play_midi_setup_drums(ch, note);
1027 		drum = channel[ch].drums[note];
1028 		if (drum->reverb_level == -1 && bank->tone[nprog].reverb_send != -1) {
1029 			drum->reverb_level = bank->tone[nprog].reverb_send;
1030 		}
1031 		if (drum->chorus_level == -1 && bank->tone[nprog].chorus_send != -1) {
1032 			drum->chorus_level = bank->tone[nprog].chorus_send;
1033 		}
1034 		if (drum->delay_level == -1 && bank->tone[nprog].delay_send != -1) {
1035 			drum->delay_level = bank->tone[nprog].delay_send;
1036 		}
1037 	} else {
1038 		nprog = channel[ch].program;
1039 		if (nprog == SPECIAL_PROGRAM) {return;}
1040 		instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
1041 		bank = instruments->toneBank(nbank);
1042 		if (bank == NULL) {bank = instruments->toneBank(0);}
1043 		channel[ch].legato = bank->tone[nprog].legato;
1044 		channel[ch].damper_mode = bank->tone[nprog].damper_mode;
1045 		channel[ch].loop_timeout = bank->tone[nprog].loop_timeout;
1046 	}
1047 }
1048 
1049 
1050 /* this reduces voices while maintaining sound quality */
reduce_voice(void)1051 int Player::reduce_voice(void)
1052 {
1053     int32_t lv, v;
1054     int i, j, lowest=-0x7FFFFFFF;
1055 
1056     i = upper_voices;
1057     lv = 0x7FFFFFFF;
1058 
1059     /* Look for the decaying note with the smallest volume */
1060     /* Protect drum decays.  Truncating them early sounds bad, especially on
1061        snares and cymbals */
1062     for(j = 0; j < i; j++)
1063     {
1064 	if(voice[j].status & VOICE_FREE ||
1065 	   (voice[j].sample->note_to_use && ISDRUMCHANNEL(voice[j].channel)))
1066 	    continue;
1067 
1068 	if(voice[j].status & ~(VOICE_ON | VOICE_DIE | VOICE_SUSTAINED))
1069 	{
1070 	    /* find lowest volume */
1071 	    v = voice[j].left_mix;
1072 	    if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1073 	    	v = voice[j].right_mix;
1074 	    if(v < lv)
1075 	    {
1076 		lv = v;
1077 		lowest = j;
1078 	    }
1079 	}
1080     }
1081     if(lowest != -0x7FFFFFFF)
1082     {
1083 	/* This can still cause a click, but if we had a free voice to
1084 	   spare for ramping down this note, we wouldn't need to kill it
1085 	   in the first place... Still, this needs to be fixed. Perhaps
1086 	   we could use a reserve of voices to play dying notes only. */
1087 
1088 	cut_notes++;
1089 	free_voice(lowest);
1090 	return lowest;
1091     }
1092 
1093     /* try to remove VOICE_DIE before VOICE_ON */
1094     lv = 0x7FFFFFFF;
1095     lowest = -1;
1096     for(j = 0; j < i; j++)
1097     {
1098       if(voice[j].status & VOICE_FREE)
1099 	    continue;
1100       if(voice[j].status & ~(VOICE_ON | VOICE_SUSTAINED))
1101       {
1102 	/* continue protecting drum decays */
1103 	if (voice[j].status & ~(VOICE_DIE) &&
1104 	    (voice[j].sample->note_to_use && ISDRUMCHANNEL(voice[j].channel)))
1105 		continue;
1106 	/* find lowest volume */
1107 	v = voice[j].left_mix;
1108 	if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1109 	    v = voice[j].right_mix;
1110 	if(v < lv)
1111 	{
1112 	    lv = v;
1113 	    lowest = j;
1114 	}
1115       }
1116     }
1117     if(lowest != -1)
1118     {
1119 	cut_notes++;
1120 	free_voice(lowest);
1121 	return lowest;
1122     }
1123 
1124     /* try to remove VOICE_SUSTAINED before VOICE_ON */
1125     lv = 0x7FFFFFFF;
1126     lowest = -0x7FFFFFFF;
1127     for(j = 0; j < i; j++)
1128     {
1129       if(voice[j].status & VOICE_FREE)
1130 	    continue;
1131       if(voice[j].status & VOICE_SUSTAINED)
1132       {
1133 	/* find lowest volume */
1134 	v = voice[j].left_mix;
1135 	if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1136 	    v = voice[j].right_mix;
1137 	if(v < lv)
1138 	{
1139 	    lv = v;
1140 	    lowest = j;
1141 	}
1142       }
1143     }
1144     if(lowest != -0x7FFFFFFF)
1145     {
1146 	cut_notes++;
1147 	free_voice(lowest);
1148 	return lowest;
1149     }
1150 
1151     /* try to remove chorus before VOICE_ON */
1152     lv = 0x7FFFFFFF;
1153     lowest = -0x7FFFFFFF;
1154     for(j = 0; j < i; j++)
1155     {
1156       if(voice[j].status & VOICE_FREE)
1157 	    continue;
1158       if(voice[j].chorus_link < j)
1159       {
1160 	/* find lowest volume */
1161 	v = voice[j].left_mix;
1162 	if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1163 	    v = voice[j].right_mix;
1164 	if(v < lv)
1165 	{
1166 	    lv = v;
1167 	    lowest = j;
1168 	}
1169       }
1170     }
1171     if(lowest != -0x7FFFFFFF)
1172     {
1173 	cut_notes++;
1174 
1175 	/* fix pan */
1176 	j = voice[lowest].chorus_link;
1177     	voice[j].panning = channel[voice[lowest].channel].panning;
1178     	recompute_amp(j);
1179     	mixer->apply_envelope_to_amp(j);
1180 
1181 	free_voice(lowest);
1182 	return lowest;
1183     }
1184 
1185     lost_notes++;
1186 
1187     /* remove non-drum VOICE_ON */
1188     lv = 0x7FFFFFFF;
1189     lowest = -0x7FFFFFFF;
1190     for(j = 0; j < i; j++)
1191     {
1192         if(voice[j].status & VOICE_FREE ||
1193 	   (voice[j].sample->note_to_use && ISDRUMCHANNEL(voice[j].channel)))
1194 	   	continue;
1195 
1196 	/* find lowest volume */
1197 	v = voice[j].left_mix;
1198 	if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1199 	    v = voice[j].right_mix;
1200 	if(v < lv)
1201 	{
1202 	    lv = v;
1203 	    lowest = j;
1204 	}
1205     }
1206     if(lowest != -0x7FFFFFFF)
1207     {
1208 	free_voice(lowest);
1209 	return lowest;
1210     }
1211 
1212     /* remove all other types of notes */
1213     lv = 0x7FFFFFFF;
1214     lowest = 0;
1215     for(j = 0; j < i; j++)
1216     {
1217 	if(voice[j].status & VOICE_FREE)
1218 	    continue;
1219 	/* find lowest volume */
1220 	v = voice[j].left_mix;
1221 	if(voice[j].panned == PANNED_MYSTERY && voice[j].right_mix > v)
1222 	    v = voice[j].right_mix;
1223 	if(v < lv)
1224 	{
1225 	    lv = v;
1226 	    lowest = j;
1227 	}
1228     }
1229 
1230     free_voice(lowest);
1231     return lowest;
1232 }
1233 
free_voice(int v1)1234 void Player::free_voice(int v1)
1235 {
1236     int v2;
1237 
1238 #ifdef ENABLE_PAN_DELAY
1239 	if (voice[v1].pan_delay_buf != NULL) {
1240 		free(voice[v1].pan_delay_buf);
1241 		voice[v1].pan_delay_buf = NULL;
1242 	}
1243 #endif /* ENABLE_PAN_DELAY */
1244 
1245     v2 = voice[v1].chorus_link;
1246     if(v1 != v2)
1247     {
1248 	/* Unlink chorus link */
1249 	voice[v1].chorus_link = v1;
1250 	voice[v2].chorus_link = v2;
1251     }
1252     voice[v1].status = VOICE_FREE;
1253     voice[v1].temper_instant = 0;
1254 }
1255 
find_free_voice(void)1256 int Player::find_free_voice(void)
1257 {
1258     int i, nv = voices, lowest;
1259     int32_t lv, v;
1260 
1261     for(i = 0; i < nv; i++)
1262 	if(voice[i].status == VOICE_FREE)
1263 	{
1264 	    if(upper_voices <= i)
1265 		upper_voices = i + 1;
1266 	    return i;
1267 	}
1268 
1269     upper_voices = voices;
1270 
1271     /* Look for the decaying note with the lowest volume */
1272     lv = 0x7FFFFFFF;
1273     lowest = -1;
1274     for(i = 0; i < nv; i++)
1275     {
1276 	if(voice[i].status & ~(VOICE_ON | VOICE_DIE) &&
1277 	   !(voice[i].sample && voice[i].sample->note_to_use && ISDRUMCHANNEL(voice[i].channel)))
1278 	{
1279 	    v = voice[i].left_mix;
1280 	    if((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v))
1281 		v = voice[i].right_mix;
1282 	    if(v<lv)
1283 	    {
1284 		lv = v;
1285 		lowest = i;
1286 	    }
1287 	}
1288     }
1289     return lowest;
1290 }
1291 
find_samples(MidiEvent * e,int * vlist)1292 int Player::find_samples(MidiEvent *e, int *vlist)
1293 {
1294 	int i, j, ch, bank, prog, note, nv;
1295 	const SpecialPatch *s;
1296 	Instrument *ip;
1297 
1298 	ch = e->channel;
1299 	if (channel[ch].special_sample > 0) {
1300 		if ((s = instruments->specialPatch(channel[ch].special_sample)) == NULL) {
1301 			return 0;
1302 		}
1303 		note = e->a + channel[ch].key_shift + note_key_offset;
1304 		note = (note < 0) ? 0 : ((note > 127) ? 127 : note);
1305 		return select_play_sample(s->sample, s->samples, &note, vlist, e);
1306 	}
1307 	bank = channel[ch].bank;
1308 	if (ISDRUMCHANNEL(ch)) {
1309 		note = e->a & 0x7f;
1310 		instruments->instrument_map(channel[ch].mapID, &bank, &note);
1311 		if (! (ip = play_midi_load_instrument(1, bank, note)))
1312 			return 0;	/* No instrument? Then we can't play. */
1313 
1314 		/* "keynum" of SF2, and patch option "note=" */
1315 		if (ip->sample->note_to_use)
1316 			note = ip->sample->note_to_use;
1317 	} else {
1318 		if ((prog = channel[ch].program) == SPECIAL_PROGRAM)
1319 			ip = instruments->defaultInstrument();
1320 		else {
1321 			instruments->instrument_map(channel[ch].mapID, &bank, &prog);
1322 			if (! (ip = play_midi_load_instrument(0, bank, prog)))
1323 				return 0;	/* No instrument? Then we can't play. */
1324 		}
1325 		note = ((ip->sample->note_to_use) ? ip->sample->note_to_use : e->a)
1326 				+ channel[ch].key_shift + note_key_offset;
1327 		note = (note < 0) ? 0 : ((note > 127) ? 127 : note);
1328 	}
1329 	nv = select_play_sample(ip->sample, ip->samples, &note, vlist, e);
1330 	/* Replace the sample if the sample is cached. */
1331 	if (ip->sample->note_to_use)
1332 		note = MIDI_EVENT_NOTE(e);
1333 	for (i = 0; i < nv; i++) {
1334 		j = vlist[i];
1335 		if (! opt_realtime_playing && allocate_cache_size > 0
1336 				&& ! channel[ch].portamento) {
1337 			voice[j].cache = recache->resamp_cache_fetch(voice[j].sample, note);
1338 			if (voice[j].cache)	/* cache hit */
1339 				voice[j].sample = voice[j].cache->resampled;
1340 		} else
1341 			voice[j].cache = NULL;
1342 	}
1343 	return nv;
1344 }
1345 
select_play_sample(Sample * splist,int nsp,int * note,int * vlist,MidiEvent * e)1346 int Player::select_play_sample(Sample *splist, int nsp, int *note, int *vlist, MidiEvent *e)
1347 {
1348 	int ch = e->channel, kn = e->a & 0x7f, vel = e->b;
1349 	int32_t f, fs, ft, fst, fc, fr, cdiff, diff, sample_link;
1350 	int8_t tt = channel[ch].temper_type;
1351 	uint8_t tp = channel[ch].rpnmap[RPN_ADDR_0003];
1352 	Sample *sp, *spc, *spr;
1353 	int16_t sf, sn;
1354 	double ratio;
1355 	int i, j, k, nv, nvc;
1356 
1357 	if (ISDRUMCHANNEL(ch))
1358 		f = fs = freq_table[*note];
1359 	else {
1360 		if (opt_pure_intonation) {
1361 			if (current_keysig < 8)
1362 				f = freq_table_pureint[current_freq_table][*note];
1363 			else
1364 				f = freq_table_pureint[current_freq_table + 12][*note];
1365 		} else if (timidity_temper_control)
1366 			switch (tt) {
1367 			case 0:
1368 				f = freq_table_tuning[tp][*note];
1369 				break;
1370 			case 1:
1371 				if (current_temper_keysig < 8)
1372 					f = freq_table_pytha[
1373 							current_temper_freq_table][*note];
1374 				else
1375 					f = freq_table_pytha[
1376 							current_temper_freq_table + 12][*note];
1377 				break;
1378 			case 2:
1379 				if (current_temper_keysig < 8)
1380 					f = freq_table_meantone[current_temper_freq_table
1381 							+ ((temper_adj) ? 36 : 0)][*note];
1382 				else
1383 					f = freq_table_meantone[current_temper_freq_table
1384 							+ ((temper_adj) ? 24 : 12)][*note];
1385 				break;
1386 			case 3:
1387 				if (current_temper_keysig < 8)
1388 					f = freq_table_pureint[current_temper_freq_table
1389 							+ ((temper_adj) ? 36 : 0)][*note];
1390 				else
1391 					f = freq_table_pureint[current_temper_freq_table
1392 							+ ((temper_adj) ? 24 : 12)][*note];
1393 				break;
1394 			default:	/* user-defined temperament */
1395 				if ((tt -= 0x40) >= 0 && tt < 4) {
1396 					if (current_temper_keysig < 8)
1397 						f = freq_table_user[tt][current_temper_freq_table
1398 								+ ((temper_adj) ? 36 : 0)][*note];
1399 					else
1400 						f = freq_table_user[tt][current_temper_freq_table
1401 								+ ((temper_adj) ? 24 : 12)][*note];
1402 				} else
1403 					f = freq_table[*note];
1404 				break;
1405 			}
1406 		else
1407 			f = freq_table[*note];
1408 		if (! opt_pure_intonation && timidity_temper_control
1409 				&& tt == 0 && f != freq_table[*note]) {
1410 			*note = log(f / 440000.0) / log(2) * 12 + 69.5;
1411 			*note = (*note < 0) ? 0 : ((*note > 127) ? 127 : *note);
1412 			fs = freq_table[*note];
1413 		} else
1414 			fs = freq_table[*note];
1415 	}
1416 	nv = 0;
1417 	for (i = 0, sp = splist; i < nsp; i++, sp++) {
1418 		/* GUS/SF2 - Scale Tuning */
1419 		if ((sf = sp->scale_factor) != 1024) {
1420 			sn = sp->scale_freq;
1421 			ratio = pow(2.0, (*note - sn) * (sf - 1024) / 12288.0);
1422 			ft = f * ratio + 0.5, fst = fs * ratio + 0.5;
1423 		} else
1424 			ft = f, fst = fs;
1425 		if (ISDRUMCHANNEL(ch) && channel[ch].drums[kn] != NULL)
1426 			if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)
1427 				ft = ft * ratio + 0.5, fst = fst * ratio + 0.5;
1428 		if (sp->low_freq <= fst && sp->high_freq >= fst
1429 				&& sp->low_vel <= vel && sp->high_vel >= vel
1430 				&& ! (sp->inst_type == INST_SF2
1431 				&& sp->sample_type == SF_SAMPLETYPE_RIGHT)) {
1432 			j = vlist[nv] = find_voice(e);
1433 			voice[j].orig_frequency = ft;
1434 			voice[j].sample = sp;
1435 			voice[j].status = VOICE_ON;
1436 			nv++;
1437 		}
1438 	}
1439 	if (nv == 0) {	/* we must select at least one sample. */
1440 		fr = fc = 0;
1441 		spc = spr = NULL;
1442 		cdiff = 0x7fffffff;
1443 		for (i = 0, sp = splist; i < nsp; i++, sp++) {
1444 			/* GUS/SF2 - Scale Tuning */
1445 			if ((sf = sp->scale_factor) != 1024) {
1446 				sn = sp->scale_freq;
1447 				ratio = pow(2.0, (*note - sn) * (sf - 1024) / 12288.0);
1448 				ft = f * ratio + 0.5, fst = fs * ratio + 0.5;
1449 			} else
1450 				ft = f, fst = fs;
1451 			if (ISDRUMCHANNEL(ch) && channel[ch].drums[kn] != NULL)
1452 				if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)
1453 					ft = ft * ratio + 0.5, fst = fst * ratio + 0.5;
1454 			diff = abs(sp->root_freq - fst);
1455 			if (diff < cdiff) {
1456 				if (sp->inst_type == INST_SF2
1457 						&& sp->sample_type == SF_SAMPLETYPE_RIGHT) {
1458 					fr = ft;	/* reserve */
1459 					spr = sp;	/* reserve */
1460 				} else {
1461 					fc = ft;
1462 					spc = sp;
1463 					cdiff = diff;
1464 				}
1465 			}
1466 		}
1467 		/* If spc is not NULL, a makeshift sample is found. */
1468 		/* Otherwise, it's a lonely right sample, but better than nothing. */
1469 		j = vlist[nv] = find_voice(e);
1470 		voice[j].orig_frequency = (spc) ? fc : fr;
1471 		voice[j].sample = (spc) ? spc : spr;
1472 		voice[j].status = VOICE_ON;
1473 		nv++;
1474 	}
1475 	nvc = nv;
1476 	for (i = 0; i < nvc; i++) {
1477 		spc = voice[vlist[i]].sample;
1478 		/* If it's left sample, there must be right sample. */
1479 		if (spc->inst_type == INST_SF2
1480 				&& spc->sample_type == SF_SAMPLETYPE_LEFT) {
1481 			sample_link = spc->sf_sample_link;
1482 			for (j = 0, sp = splist; j < nsp; j++, sp++)
1483 				if (sp->inst_type == INST_SF2
1484 						&& sp->sample_type == SF_SAMPLETYPE_RIGHT
1485 						&& sp->sf_sample_index == sample_link) {
1486 					/* right sample is found. */
1487 					/* GUS/SF2 - Scale Tuning */
1488 					if ((sf = sp->scale_factor) != 1024) {
1489 						sn = sp->scale_freq;
1490 						ratio = pow(2.0, (*note - sn) * (sf - 1024) / 12288.0);
1491 						ft = f * ratio + 0.5;
1492 					} else
1493 						ft = f;
1494 					if (ISDRUMCHANNEL(ch) && channel[ch].drums[kn] != NULL)
1495 						if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)
1496 							ft = ft * ratio + 0.5;
1497 					k = vlist[nv] = find_voice(e);
1498 					voice[k].orig_frequency = ft;
1499 					voice[k].sample = sp;
1500 					voice[k].status = VOICE_ON;
1501 					nv++;
1502 					break;
1503 				}
1504 		}
1505 	}
1506 	return nv;
1507 }
1508 
get_play_note_ratio(int ch,int note)1509 double Player::get_play_note_ratio(int ch, int note)
1510 {
1511 	int play_note = channel[ch].drums[note]->play_note;
1512 	int bank = channel[ch].bank;
1513 	const ToneBank *dbank;
1514 	int def_play_note;
1515 
1516 	if (play_note == -1)
1517 		return 1.0;
1518 	instruments->instrument_map(channel[ch].mapID, &bank, &note);
1519 	dbank = (instruments->drumSet(bank)) ? instruments->drumSet(bank) : instruments->drumSet(0);
1520 	if ((def_play_note = dbank->tone[note].play_note) == -1)
1521 		return 1.0;
1522 	if (play_note >= def_play_note)
1523 		return bend_coarse[(play_note - def_play_note) & 0x7f];
1524 	else
1525 		return 1 / bend_coarse[(def_play_note - play_note) & 0x7f];
1526 }
1527 
1528 /* Only one instance of a note can be playing on a single channel. */
find_voice(MidiEvent * e)1529 int Player::find_voice(MidiEvent *e)
1530 {
1531 	int ch = e->channel;
1532 	int note = MIDI_EVENT_NOTE(e);
1533 	int status_check, mono_check;
1534 	AlternateAssign *altassign;
1535 	int i, lowest = -1;
1536 
1537 	status_check = (timidity_overlap_voice_allow)
1538 			? (VOICE_OFF | VOICE_SUSTAINED) : 0xff;
1539 	mono_check = channel[ch].mono;
1540 	altassign = instruments->find_altassign(channel[ch].altassign, note);
1541 	for (i = 0; i < upper_voices; i++)
1542 		if (voice[i].status == VOICE_FREE) {
1543 			lowest = i;	/* lower volume */
1544 			break;
1545 		}
1546 	for (i = 0; i < upper_voices; i++)
1547 		if (voice[i].status != VOICE_FREE && voice[i].channel == ch) {
1548 			if (voice[i].note == note && (voice[i].status & status_check))
1549 				kill_note(i);
1550 			else if (mono_check)
1551 				kill_note(i);
1552 			else if (altassign && instruments->find_altassign(altassign, voice[i].note))
1553 				kill_note(i);
1554 			else if (voice[i].note == note && (channel[ch].assign_mode == 0
1555 					|| (channel[ch].assign_mode == 1 &&
1556 					    voice[i].proximate_flag == 0)))
1557 				kill_note(i);
1558 		}
1559 	for (i = 0; i < upper_voices; i++)
1560 		if (voice[i].channel == ch && voice[i].note == note)
1561 			voice[i].proximate_flag = 0;
1562 	if (lowest != -1)	/* Found a free voice. */
1563 		return lowest;
1564 	if (upper_voices < voices)
1565 		return upper_voices++;
1566 	return reduce_voice();
1567 }
1568 
get_panning(int ch,int note,int v)1569 int Player::get_panning(int ch, int note,int v)
1570 {
1571     int pan;
1572 
1573 	if(channel[ch].panning != NO_PANNING) {pan = (int)channel[ch].panning - 64;}
1574 	else {pan = 0;}
1575 	if(ISDRUMCHANNEL(ch) &&
1576 	 channel[ch].drums[note] != NULL &&
1577 	 channel[ch].drums[note]->drum_panning != NO_PANNING) {
1578 		pan += channel[ch].drums[note]->drum_panning;
1579 	} else {
1580 		pan += voice[v].sample->panning;
1581 	}
1582 
1583 	if (pan > 127) pan = 127;
1584 	else if (pan < 0) pan = 0;
1585 
1586 	return pan;
1587 }
1588 
1589 /*! initialize vibrato parameters for a voice. */
init_voice_vibrato(int v)1590 void Player::init_voice_vibrato(int v)
1591 {
1592 	Voice *vp = &(voice[v]);
1593 	int ch = vp->channel, j, nrpn_vib_flag;
1594 	double ratio;
1595 
1596 	/* if NRPN vibrato is set, it's believed that there must be vibrato. */
1597 	nrpn_vib_flag = op_nrpn_vibrato
1598 		&& (channel[ch].vibrato_ratio != 1.0 || channel[ch].vibrato_depth != 0);
1599 
1600 	/* vibrato sweep */
1601 	vp->vibrato_sweep = vp->sample->vibrato_sweep_increment;
1602 	vp->vibrato_sweep_position = 0;
1603 
1604 	/* vibrato rate */
1605 	if (nrpn_vib_flag) {
1606 		if(vp->sample->vibrato_control_ratio == 0) {
1607 			ratio = cnv_Hz_to_vib_ratio(5.0) * channel[ch].vibrato_ratio;
1608 		} else {
1609 			ratio = (double)vp->sample->vibrato_control_ratio * channel[ch].vibrato_ratio;
1610 		}
1611 		if (ratio < 0) {ratio = 0;}
1612 		vp->vibrato_control_ratio = (int)ratio;
1613 	} else {
1614 		vp->vibrato_control_ratio = vp->sample->vibrato_control_ratio;
1615 	}
1616 
1617 	/* vibrato depth */
1618 	if (nrpn_vib_flag) {
1619 		vp->vibrato_depth = vp->sample->vibrato_depth + channel[ch].vibrato_depth;
1620 		if (vp->vibrato_depth > VIBRATO_DEPTH_MAX) {vp->vibrato_depth = VIBRATO_DEPTH_MAX;}
1621 		else if (vp->vibrato_depth < 1) {vp->vibrato_depth = 1;}
1622 		if (vp->sample->vibrato_depth < 0) {	/* in opposite phase */
1623 			vp->vibrato_depth = -vp->vibrato_depth;
1624 		}
1625 	} else {
1626 		vp->vibrato_depth = vp->sample->vibrato_depth;
1627 	}
1628 
1629 	/* vibrato delay */
1630 	vp->vibrato_delay = vp->sample->vibrato_delay + channel[ch].vibrato_delay;
1631 
1632 	/* internal parameters */
1633 	vp->orig_vibrato_control_ratio = vp->vibrato_control_ratio;
1634 	vp->vibrato_control_counter = vp->vibrato_phase = 0;
1635 	for (j = 0; j < VIBRATO_SAMPLE_INCREMENTS; j++) {
1636 		vp->vibrato_sample_increment[j] = 0;
1637 	}
1638 }
1639 
1640 /*! initialize panning-delay for a voice. */
init_voice_pan_delay(int v)1641 void Player::init_voice_pan_delay(int v)
1642 {
1643 #ifdef ENABLE_PAN_DELAY
1644 	Voice *vp = &(voice[v]);
1645 	int ch = vp->channel;
1646 	double pan_delay_diff;
1647 
1648 	if (vp->pan_delay_buf != NULL) {
1649 		free(vp->pan_delay_buf);
1650 		vp->pan_delay_buf = NULL;
1651 	}
1652 	vp->pan_delay_rpt = 0;
1653 	if (timidity_pan_delay && channel[ch].insertion_effect == 0 && !timidity_surround_chorus) {
1654 		if (vp->panning == 64) {vp->delay += pan_delay_table[64] * playback_rate / 1000;}
1655 		else {
1656 			if(pan_delay_table[vp->panning] > pan_delay_table[127 - vp->panning]) {
1657 				pan_delay_diff = pan_delay_table[vp->panning] - pan_delay_table[127 - vp->panning];
1658 				vp->delay += (pan_delay_table[vp->panning] - pan_delay_diff) * playback_rate / 1000;
1659 			} else {
1660 				pan_delay_diff = pan_delay_table[127 - vp->panning] - pan_delay_table[vp->panning];
1661 				vp->delay += (pan_delay_table[127 - vp->panning] - pan_delay_diff) * playback_rate / 1000;
1662 			}
1663 			vp->pan_delay_rpt = pan_delay_diff * playback_rate / 1000;
1664 		}
1665 		if(vp->pan_delay_rpt < 1) {vp->pan_delay_rpt = 0;}
1666 		vp->pan_delay_wpt = 0;
1667 		vp->pan_delay_spt = vp->pan_delay_wpt - vp->pan_delay_rpt;
1668 		if (vp->pan_delay_spt < 0) {vp->pan_delay_spt += PAN_DELAY_BUF_MAX;}
1669 		vp->pan_delay_buf = (int32_t *)safe_malloc(sizeof(int32_t) * PAN_DELAY_BUF_MAX);
1670 		memset(vp->pan_delay_buf, 0, sizeof(int32_t) * PAN_DELAY_BUF_MAX);
1671 	}
1672 #endif	/* ENABLE_PAN_DELAY */
1673 }
1674 
1675 /*! initialize portamento or legato for a voice. */
init_voice_portamento(int v)1676 void Player::init_voice_portamento(int v)
1677 {
1678 	Voice *vp = &(voice[v]);
1679 	int ch = vp->channel;
1680 
1681 	vp->porta_control_counter = 0;
1682 	if (channel[ch].legato && channel[ch].legato_flag) {
1683 		update_legato_controls(ch);
1684 	}
1685 	else if (channel[ch].portamento && !channel[ch].porta_control_ratio) {
1686 		update_portamento_controls(ch);
1687 	}
1688 	vp->porta_control_ratio = 0;
1689 	if (channel[ch].porta_control_ratio)
1690 	{
1691 		if (channel[ch].last_note_fine == -1) {
1692 			/* first on */
1693 			channel[ch].last_note_fine = vp->note * 256;
1694 			channel[ch].porta_control_ratio = 0;
1695 		}
1696 		else {
1697 			vp->porta_control_ratio = channel[ch].porta_control_ratio;
1698 			vp->porta_dpb = channel[ch].porta_dpb;
1699 			vp->porta_pb = channel[ch].last_note_fine -
1700 				vp->note * 256;
1701 			if (vp->porta_pb == 0) { vp->porta_control_ratio = 0; }
1702 		}
1703 	}
1704 }
1705 
1706 /*! initialize tremolo for a voice. */
init_voice_tremolo(int v)1707 void Player::init_voice_tremolo(int v)
1708 {
1709 	Voice *vp = &(voice[v]);
1710 
1711   vp->tremolo_delay = vp->sample->tremolo_delay;
1712   vp->tremolo_phase = 0;
1713   vp->tremolo_phase_increment = vp->sample->tremolo_phase_increment;
1714   vp->tremolo_sweep = vp->sample->tremolo_sweep_increment;
1715   vp->tremolo_sweep_position = 0;
1716   vp->tremolo_depth = vp->sample->tremolo_depth;
1717 }
1718 
start_note(MidiEvent * e,int i,int vid,int cnt)1719 void Player::start_note(MidiEvent *e, int i, int vid, int cnt)
1720 {
1721 	int j, ch, note;
1722 
1723 	ch = e->channel;
1724 
1725 	note = MIDI_EVENT_NOTE(e);
1726 	voice[i].status = VOICE_ON;
1727 	voice[i].channel = ch;
1728 	voice[i].note = note;
1729 	voice[i].velocity = e->b;
1730 	voice[i].chorus_link = i;	/* No link */
1731 	voice[i].proximate_flag = 1;
1732 
1733 	j = channel[ch].special_sample;
1734 	if (j == 0 || instruments->specialPatch(j) == NULL)
1735 		voice[i].sample_offset = 0;
1736 	else
1737 	{
1738 		voice[i].sample_offset = instruments->specialPatch(j)->sample_offset << FRACTION_BITS;
1739 		if (voice[i].sample->modes & MODES_LOOPING)
1740 		{
1741 			if (voice[i].sample_offset > voice[i].sample->loop_end)
1742 				voice[i].sample_offset = voice[i].sample->loop_start;
1743 		}
1744 		else if (voice[i].sample_offset > voice[i].sample->data_length)
1745 		{
1746 			free_voice(i);
1747 			return;
1748 		}
1749 	}
1750 	voice[i].sample_increment = 0; /* make sure it isn't negative */
1751 	voice[i].vid = vid;
1752 	voice[i].delay = voice[i].sample->envelope_delay;
1753 	voice[i].modenv_delay = voice[i].sample->modenv_delay;
1754 	voice[i].delay_counter = 0;
1755 
1756 	init_voice_tremolo(i);	/* tremolo */
1757 	init_voice_filter(i);		/* resonant lowpass filter */
1758 	init_voice_vibrato(i);	/* vibrato */
1759 	voice[i].panning = get_panning(ch, note, i);	/* pan */
1760 	init_voice_pan_delay(i);	/* panning-delay */
1761 	init_voice_portamento(i);	/* portamento or legato */
1762 
1763 	if (cnt == 0)
1764 		channel[ch].last_note_fine = voice[i].note * 256;
1765 
1766 	/* initialize modulation envelope */
1767 	if (voice[i].sample->modes & MODES_ENVELOPE)
1768 	{
1769 		voice[i].modenv_stage = EG_GUS_ATTACK;
1770 		voice[i].modenv_volume = 0;
1771 		mixer->recompute_modulation_envelope(i);
1772 		mixer->apply_modulation_envelope(i);
1773 	}
1774 	else
1775 	{
1776 		voice[i].modenv_increment = 0;
1777 		mixer->apply_modulation_envelope(i);
1778 	}
1779 	recompute_freq(i);
1780 	recompute_voice_filter(i);
1781 
1782 	recompute_amp(i);
1783 	/* initialize volume envelope */
1784 	if (voice[i].sample->modes & MODES_ENVELOPE)
1785 	{
1786 		/* Ramp up from 0 */
1787 		voice[i].envelope_stage = EG_GUS_ATTACK;
1788 		voice[i].envelope_volume = 0;
1789 		voice[i].control_counter = 0;
1790 		mixer->recompute_envelope(i);
1791 		mixer->apply_envelope_to_amp(i);
1792 	}
1793 	else
1794 	{
1795 		voice[i].envelope_increment = 0;
1796 		mixer->apply_envelope_to_amp(i);
1797 	}
1798 
1799 	voice[i].timeout = -1;
1800 }
1801 
finish_note(int i)1802 void Player::finish_note(int i)
1803 {
1804     if (voice[i].sample->modes & MODES_ENVELOPE)
1805     {
1806 		/* We need to get the envelope out of Sustain stage. */
1807 		/* Note that voice[i].envelope_stage < EG_GUS_RELEASE1 */
1808 		voice[i].status = VOICE_OFF;
1809 		voice[i].envelope_stage = EG_GUS_RELEASE1;
1810 		mixer->recompute_envelope(i);
1811 		voice[i].modenv_stage = EG_GUS_RELEASE1;
1812 		mixer->recompute_modulation_envelope(i);
1813 		mixer->apply_modulation_envelope(i);
1814 		mixer->apply_envelope_to_amp(i);
1815 	}
1816     else
1817     {
1818 		/* Set status to OFF so resample_voice() will let this voice out
1819 		of its loop, if any. In any case, this voice dies when it
1820 			hits the end of its data (ofs>=data_length). */
1821 		if(voice[i].status != VOICE_OFF)
1822 		{
1823 		voice[i].status = VOICE_OFF;
1824 		}
1825     }
1826 }
1827 
set_envelope_time(int ch,int val,int stage)1828 void Player::set_envelope_time(int ch, int val, int stage)
1829 {
1830 	val = val & 0x7F;
1831 #if 0
1832 	switch(stage) {
1833 	case EG_ATTACK:	/* Attack */
1834 		//printMessage(CMSG_INFO,VERB_NOISY,"Attack Time (CH:%d VALUE:%d)", ch, val);
1835 		break;
1836 	case EG_DECAY: /* Decay */
1837 		//printMessage(CMSG_INFO,VERB_NOISY,"Decay Time (CH:%d VALUE:%d)", ch, val);
1838 		break;
1839 	case EG_RELEASE:	/* Release */
1840 		//printMessage(CMSG_INFO,VERB_NOISY,"Release Time (CH:%d VALUE:%d)", ch, val);
1841 		break;
1842 	default:
1843 		//printMessage(CMSG_INFO,VERB_NOISY,"? Time (CH:%d VALUE:%d)", ch, val);
1844 	}
1845 #endif
1846 	channel[ch].envelope_rate[stage] = val;
1847 }
1848 
1849 
1850 
1851 /* Yet another chorus implementation
1852  *	by Eric A. Welsh <ewelsh@gpc.wustl.edu>.
1853  */
new_chorus_voice_alternate(int v1,int level)1854 void Player::new_chorus_voice_alternate(int v1, int level)
1855 {
1856     int v2, ch, panlevel;
1857     uint8_t pan;
1858     double delay;
1859     double freq, frac;
1860     int note_adjusted;
1861 
1862     if((v2 = find_free_voice()) == -1)
1863 	return;
1864     ch = voice[v1].channel;
1865     voice[v2] = voice[v1];
1866 
1867     /* NRPN Chorus Send Level of Drum */
1868     if(ISDRUMCHANNEL(ch) && channel[ch].drums[voice[v1].note] != NULL) {
1869 	level *= (double)channel[ch].drums[voice[v1].note]->chorus_level / 127.0;
1870     }
1871 
1872     /* for our purposes, hard left will be equal to 1 instead of 0 */
1873     pan = voice[v1].panning;
1874     if (!pan) pan = 1;
1875 
1876     /* Choose lower voice index for base voice (v1) */
1877     if(v1 > v2)
1878     {
1879     	v1 ^= v2;
1880     	v2 ^= v1;
1881     	v1 ^= v2;
1882     }
1883 
1884     /* Make doubled link v1 and v2 */
1885     voice[v1].chorus_link = v2;
1886     voice[v2].chorus_link = v1;
1887 
1888     /* detune notes for chorus effect */
1889     level >>= 2;		/* scale to a "better" value */
1890     if (level)
1891     {
1892         if(channel[ch].pitchbend + level < 0x2000)
1893             voice[v2].orig_frequency *= bend_fine[level];
1894         else
1895 	    voice[v2].orig_frequency /= bend_fine[level];
1896         voice[v2].cache = NULL;
1897     }
1898 
1899     delay = 0.003;
1900 
1901     /* Try to keep the delayed voice from cancelling out the other voice */
1902     /* Pitch detection is used to find the real pitches for drums and MODs */
1903     note_adjusted = voice[v1].note + voice[v1].sample->transpose_detected;
1904     if (note_adjusted > 127) note_adjusted = 127;
1905     else if (note_adjusted < 0) note_adjusted = 0;
1906     freq = pitch_freq_table[note_adjusted];
1907     delay *= freq;
1908     frac = delay - floor(delay);
1909 
1910     /* force the delay away from 0.5 period */
1911     if (frac < 0.5 && frac > 0.40)
1912     {
1913     	delay = (floor(delay) + 0.40) / freq;
1914    	    delay += (0.5 - frac) * (1.0 - labs(64 - pan) / 63.0) / freq;
1915     }
1916     else if (frac >= 0.5 && frac < 0.60)
1917     {
1918     	delay = (floor(delay) + 0.60) / freq;
1919    	    delay += (0.5 - frac) * (1.0 - labs(64 - pan) / 63.0) / freq;
1920     }
1921     else
1922 	delay = 0.003;
1923 
1924     /* set panning & delay for pseudo-surround effect */
1925     {
1926         panlevel = 63;
1927         if (pan - panlevel < 1) panlevel = pan - 1;
1928         if (pan + panlevel > 127) panlevel = 127 - pan;
1929         voice[v1].panning -= panlevel;
1930         voice[v2].panning += panlevel;
1931 
1932         /* choose which voice is delayed based on panning */
1933         if (voice[v1].panned == PANNED_CENTER) {
1934             /* randomly choose which voice is delayed */
1935             if (int_rand(2))
1936                 voice[v1].delay += (int)(playback_rate * delay);
1937             else
1938                 voice[v2].delay += (int)(playback_rate * delay);
1939         }
1940         else if (pan - 64 < 0) {
1941             voice[v2].delay += (int)(playback_rate * delay);
1942         }
1943         else {
1944             voice[v1].delay += (int)(playback_rate * delay);
1945         }
1946     }
1947 
1948     /* check for similar drums playing simultaneously with center pans */
1949     if (ISDRUMCHANNEL(ch) && voice[v1].panned == PANNED_CENTER)
1950     {
1951     	int i, j;
1952 
1953     	/* force Rimshot (37), Snare1 (38), Snare2 (40), and XG #34 to have
1954     	 * the same delay, otherwise there will be bad voice cancellation.
1955     	 */
1956     	if (voice[v1].note == 37 ||
1957     	    voice[v1].note == 38 ||
1958     	    voice[v1].note == 40 ||
1959     	    (voice[v1].note == 34 && play_system_mode == XG_SYSTEM_MODE))
1960     	{
1961     	    for (i = 0; i < upper_voices; i++)
1962     	    {
1963     	    	if (voice[i].status & (VOICE_DIE | VOICE_FREE))
1964     	    	    continue;
1965 
1966     	    	if (!ISDRUMCHANNEL(voice[i].channel))
1967     	    	    continue;
1968 
1969 	    	if (i == v1 || i == v2)
1970 	    	    continue;
1971 
1972 	    	if (voice[i].note == 37 ||
1973 	    	    voice[i].note == 38 ||
1974 	    	    voice[i].note == 40 ||
1975 	    	    (voice[i].note == 34 &&
1976 	    	     play_system_mode == XG_SYSTEM_MODE))
1977 	    	{
1978 	    	    j = voice[i].chorus_link;
1979 
1980 	    	    if (voice[i].panned == PANNED_LEFT &&
1981 	    	        voice[j].panned == PANNED_RIGHT)
1982 	    	    {
1983 	    	    	voice[v1].delay = voice[i].delay;
1984 	    	    	voice[v2].delay = voice[j].delay;
1985 
1986 	    	    	break;
1987 	    	    }
1988 	    	}
1989     	    }
1990     	}
1991 
1992     	/* force Kick1 (35), Kick2 (36), and XG Kick #33 to have the same
1993     	 * delay, otherwise there will be bad voice cancellation.
1994     	 */
1995     	if (voice[v1].note == 35 ||
1996     	    voice[v1].note == 36 ||
1997     	    (voice[v1].note == 33 && play_system_mode == XG_SYSTEM_MODE))
1998     	{
1999     	    for (i = 0; i < upper_voices; i++)
2000     	    {
2001     	    	if (voice[i].status & (VOICE_DIE | VOICE_FREE))
2002     	    	    continue;
2003 
2004     	    	if (!ISDRUMCHANNEL(voice[i].channel))
2005     	    	    continue;
2006 
2007 	    	if (i == v1 || i == v2)
2008 	    	    continue;
2009 
2010 	    	if (voice[i].note == 35 ||
2011 	    	    voice[i].note == 36 ||
2012 	    	    (voice[i].note == 33 &&
2013 	    	     play_system_mode == XG_SYSTEM_MODE))
2014 	    	{
2015 	    	    j = voice[i].chorus_link;
2016 
2017 	    	    if (voice[i].panned == PANNED_LEFT &&
2018 	    	        voice[j].panned == PANNED_RIGHT)
2019 	    	    {
2020 	    	    	voice[v1].delay = voice[i].delay;
2021 	    	    	voice[v2].delay = voice[j].delay;
2022 
2023 	    	    	break;
2024 	    	    }
2025 	    	}
2026     	    }
2027     	}
2028     }
2029 
2030     init_voice_pan_delay(v1);
2031     init_voice_pan_delay(v2);
2032 
2033     recompute_amp(v1);
2034 	mixer->apply_envelope_to_amp(v1);
2035     recompute_amp(v2);
2036 	mixer->apply_envelope_to_amp(v2);
2037     if (level) recompute_freq(v2);
2038 }
2039 
note_on(MidiEvent * e)2040 void Player::note_on(MidiEvent *e)
2041 {
2042     int i, nv, v, ch, note;
2043     int vlist[32];
2044     int vid;
2045 	int32_t random_delay = 0;
2046 
2047 	ch = e->channel;
2048 	note = MIDI_EVENT_NOTE(e);
2049 
2050 	if(ISDRUMCHANNEL(ch) &&
2051 	   channel[ch].drums[note] != NULL &&
2052 	   !get_rx_drum(channel[ch].drums[note], RX_NOTE_ON)) {	/* Rx. Note On */
2053 		return;
2054 	}
2055 	if(channel[ch].note_limit_low > note ||
2056 		channel[ch].note_limit_high < note ||
2057 		channel[ch].vel_limit_low > e->b ||
2058 		channel[ch].vel_limit_high < e->b) {
2059 		return;
2060 	}
2061     if((nv = find_samples(e, vlist)) == 0)
2062 	return;
2063 
2064     vid = new_vidq(e->channel, note);
2065 
2066 	recompute_bank_parameter(ch, note);
2067 	recompute_channel_filter(ch, note);
2068 	random_delay = calc_random_delay(ch, note);
2069 
2070     for(i = 0; i < nv; i++)
2071     {
2072 	v = vlist[i];
2073 	if(ISDRUMCHANNEL(ch) &&
2074 	   channel[ch].drums[note] != NULL &&
2075 	   channel[ch].drums[note]->pan_random)
2076 	    channel[ch].drums[note]->drum_panning = int_rand(128);
2077 	else if(channel[ch].pan_random)
2078 	{
2079 	    channel[ch].panning = int_rand(128);
2080 	}
2081 	start_note(e, v, vid, nv - i - 1);
2082 	voice[v].delay += random_delay;
2083 	voice[v].modenv_delay += random_delay;
2084 	voice[v].old_left_mix = voice[v].old_right_mix =
2085 	voice[v].left_mix_inc = voice[v].left_mix_offset =
2086 	voice[v].right_mix_inc = voice[v].right_mix_offset = 0;
2087 	if(timidity_surround_chorus)
2088 	    new_chorus_voice_alternate(v, 0);
2089     }
2090 
2091     channel[ch].legato_flag = 1;
2092 }
2093 
2094 /*! sostenuto is now implemented as an instant sustain */
update_sostenuto_controls(int ch)2095 void Player::update_sostenuto_controls(int ch)
2096 {
2097   int uv = upper_voices, i;
2098 
2099   if(ISDRUMCHANNEL(ch) || channel[ch].sostenuto == 0) {return;}
2100 
2101   for(i = 0; i < uv; i++)
2102   {
2103 	if ((voice[i].status & (VOICE_ON | VOICE_OFF))
2104 			&& voice[i].channel == ch)
2105 	 {
2106 		  voice[i].status = VOICE_SUSTAINED;
2107 		  voice[i].envelope_stage = EG_GUS_RELEASE1;
2108 		  mixer->recompute_envelope(i);
2109 	 }
2110   }
2111 }
2112 
2113 /*! redamper effect for piano instruments */
update_redamper_controls(int ch)2114 void Player::update_redamper_controls(int ch)
2115 {
2116   int uv = upper_voices, i;
2117 
2118   if(ISDRUMCHANNEL(ch) || channel[ch].damper_mode == 0) {return;}
2119 
2120   for(i = 0; i < uv; i++)
2121   {
2122 	if ((voice[i].status & (VOICE_ON | VOICE_OFF))
2123 			&& voice[i].channel == ch)
2124 	  {
2125 		  voice[i].status = VOICE_SUSTAINED;
2126 		  voice[i].envelope_stage = EG_GUS_RELEASE1;
2127 		  mixer->recompute_envelope(i);
2128 	  }
2129   }
2130 }
2131 
note_off(MidiEvent * e)2132 void Player::note_off(MidiEvent *e)
2133 {
2134   int uv = upper_voices, i;
2135   int ch, note, vid, sustain;
2136 
2137   ch = e->channel;
2138   note = MIDI_EVENT_NOTE(e);
2139 
2140   if(ISDRUMCHANNEL(ch))
2141   {
2142       int nbank, nprog;
2143 
2144       nbank = channel[ch].bank;
2145       nprog = note;
2146       instruments->instrument_map(channel[ch].mapID, &nbank, &nprog);
2147 
2148       if (channel[ch].drums[nprog] != NULL &&
2149           get_rx_drum(channel[ch].drums[nprog], RX_NOTE_OFF))
2150       {
2151           auto bank = instruments->drumSet(nbank);
2152           if(bank == NULL) bank = instruments->drumSet(0);
2153 
2154           /* uh oh, this drum doesn't have an instrument loaded yet */
2155           if (bank->tone[nprog].instrument == NULL)
2156               return;
2157 
2158           /* this drum is not loaded for some reason (error occured?) */
2159           if (IS_MAGIC_INSTRUMENT(bank->tone[nprog].instrument))
2160               return;
2161 
2162           /* only disallow Note Off if the drum sample is not looped */
2163           if (!(bank->tone[nprog].instrument->sample->modes & MODES_LOOPING))
2164               return;	/* Note Off is not allowed. */
2165       }
2166   }
2167 
2168   if ((vid = last_vidq(ch, note)) == -1)
2169       return;
2170   sustain = channel[ch].sustain;
2171   for (i = 0; i < uv; i++)
2172   {
2173       if(voice[i].status == VOICE_ON &&
2174 	 voice[i].channel == ch &&
2175 	 voice[i].note == note &&
2176 	 voice[i].vid == vid)
2177       {
2178 	  if(sustain)
2179 	  {
2180 	      voice[i].status = VOICE_SUSTAINED;
2181 	  }
2182 	  else
2183 	      finish_note(i);
2184       }
2185   }
2186 
2187   channel[ch].legato_flag = 0;
2188 }
2189 
2190 /* Process the All Notes Off event */
all_notes_off(int c)2191 void Player::all_notes_off(int c)
2192 {
2193   int i, uv = upper_voices;
2194   printMessage(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", c);
2195   for(i = 0; i < uv; i++)
2196     if (voice[i].status==VOICE_ON &&
2197 	voice[i].channel==c)
2198       {
2199 	if (channel[c].sustain)
2200 	  {
2201 	    voice[i].status=VOICE_SUSTAINED;
2202 	  }
2203 	else
2204 	  finish_note(i);
2205       }
2206   for(i = 0; i < 128; i++)
2207       vidq_head[c * 128 + i] = vidq_tail[c * 128 + i] = 0;
2208 }
2209 
2210 /* Process the All Sounds Off event */
all_sounds_off(int c)2211 void Player::all_sounds_off(int c)
2212 {
2213   int i, uv = upper_voices;
2214   for(i = 0; i < uv; i++)
2215     if (voice[i].channel==c &&
2216 	(voice[i].status & ~(VOICE_FREE | VOICE_DIE)))
2217       {
2218 	kill_note(i);
2219       }
2220   for(i = 0; i < 128; i++)
2221       vidq_head[c * 128 + i] = vidq_tail[c * 128 + i] = 0;
2222 }
2223 
2224 /*! adjust polyphonic key pressure (PAf, PAT) */
adjust_pressure(MidiEvent * e)2225 void Player::adjust_pressure(MidiEvent *e)
2226 {
2227     int i, uv = upper_voices;
2228     int note, ch;
2229 
2230     if(timidity_channel_pressure)
2231     {
2232 	ch = e->channel;
2233     note = MIDI_EVENT_NOTE(e);
2234 	channel[ch].paf.val = e->b;
2235 	if(channel[ch].paf.pitch != 0) {channel[ch].pitchfactor = 0;}
2236 
2237     for(i = 0; i < uv; i++)
2238     if(voice[i].status == VOICE_ON &&
2239        voice[i].channel == ch &&
2240        voice[i].note == note)
2241     {
2242 		recompute_amp(i);
2243 		mixer->apply_envelope_to_amp(i);
2244 		recompute_freq(i);
2245 		recompute_voice_filter(i);
2246     }
2247 	}
2248 }
2249 
2250 /*! adjust channel pressure (channel aftertouch, CAf, CAT) */
adjust_channel_pressure(MidiEvent * e)2251 void Player::adjust_channel_pressure(MidiEvent *e)
2252 {
2253     if(timidity_channel_pressure)
2254     {
2255 	int i, uv = upper_voices;
2256 	int ch;
2257 
2258 	ch = e->channel;
2259 	channel[ch].caf.val = e->a;
2260 	if(channel[ch].caf.pitch != 0) {channel[ch].pitchfactor = 0;}
2261 
2262 	for(i = 0; i < uv; i++)
2263 	{
2264 	    if(voice[i].status == VOICE_ON && voice[i].channel == ch)
2265 	    {
2266 		recompute_amp(i);
2267 		mixer->apply_envelope_to_amp(i);
2268 		recompute_freq(i);
2269 		recompute_voice_filter(i);
2270 		}
2271 	}
2272     }
2273 }
2274 
adjust_panning(int c)2275 void Player::adjust_panning(int c)
2276 {
2277     int i, uv = upper_voices, pan = channel[c].panning;
2278     for(i = 0; i < uv; i++)
2279     {
2280 	if ((voice[i].channel==c) &&
2281 	    (voice[i].status & (VOICE_ON | VOICE_SUSTAINED)))
2282 	{
2283             /* adjust pan to include drum/sample pan offsets */
2284             pan = get_panning(c, voice[i].note, i);
2285 
2286 	    /* Hack to handle -EFchorus=2 in a "reasonable" way */
2287 	    if(timidity_surround_chorus && voice[i].chorus_link != i)
2288 	    {
2289 		int v1, v2;
2290 
2291 		if(i >= voice[i].chorus_link)
2292 		    /* `i' is not base chorus voice.
2293 		     *  This sub voice is already updated.
2294 		     */
2295 		    continue;
2296 
2297 		v1 = i;				/* base voice */
2298 		v2 = voice[i].chorus_link;	/* sub voice (detuned) */
2299 
2300 		if(timidity_surround_chorus) /* Surround chorus mode by Eric. */
2301 		{
2302 		    int panlevel;
2303 
2304 		    if (!pan) pan = 1;	/* make hard left be 1 instead of 0 */
2305 		    panlevel = 63;
2306 		    if (pan - panlevel < 1) panlevel = pan - 1;
2307 		    if (pan + panlevel > 127) panlevel = 127 - pan;
2308 		    voice[v1].panning = pan - panlevel;
2309 		    voice[v2].panning = pan + panlevel;
2310 		}
2311 		else
2312 		{
2313 		    voice[v1].panning = pan;
2314 		    if(pan > 60 && pan < 68) /* PANNED_CENTER */
2315 			voice[v2].panning =
2316 			    64 + int_rand(40) - 20; /* 64 +- rand(20) */
2317 		    else if(pan < CHORUS_OPPOSITE_THRESHOLD)
2318 			voice[v2].panning = 127;
2319 		    else if(pan > 127 - CHORUS_OPPOSITE_THRESHOLD)
2320 			voice[v2].panning = 0;
2321 		    else
2322 			voice[v2].panning = (pan < 64 ? 0 : 127);
2323 		}
2324 		recompute_amp(v2);
2325 		mixer->apply_envelope_to_amp(v2);
2326 		/* v1 == i, so v1 will be updated next */
2327 	    }
2328 	    else
2329 		voice[i].panning = pan;
2330 
2331 		recompute_amp(i);
2332 		mixer->apply_envelope_to_amp(i);
2333 	}
2334     }
2335 }
2336 
play_midi_setup_drums(int ch,int note)2337 void Player::play_midi_setup_drums(int ch, int note)
2338 {
2339     channel[ch].drums[note] = (struct DrumParts *)
2340 	new_segment(&playmidi_pool, sizeof(struct DrumParts));
2341     reset_drum_controllers(channel[ch].drums, note);
2342 }
2343 
adjust_drum_panning(int ch,int note)2344 void Player::adjust_drum_panning(int ch, int note)
2345 {
2346     int i, uv = upper_voices;
2347 
2348     for(i = 0; i < uv; i++) {
2349 		if(voice[i].channel == ch &&
2350 		   voice[i].note == note &&
2351 		   (voice[i].status & (VOICE_ON | VOICE_SUSTAINED)))
2352 		{
2353 			voice[i].panning = get_panning(ch, note, i);
2354 			recompute_amp(i);
2355 			mixer->apply_envelope_to_amp(i);
2356 		}
2357 	}
2358 }
2359 
drop_sustain(int c)2360 void Player::drop_sustain(int c)
2361 {
2362   int i, uv = upper_voices;
2363   for(i = 0; i < uv; i++)
2364     if (voice[i].status == VOICE_SUSTAINED && voice[i].channel == c)
2365       finish_note(i);
2366 }
2367 
adjust_all_pitch(void)2368 void Player::adjust_all_pitch(void)
2369 {
2370 	int ch, i, uv = upper_voices;
2371 
2372 	for (ch = 0; ch < MAX_CHANNELS; ch++)
2373 		channel[ch].pitchfactor = 0;
2374 	for (i = 0; i < uv; i++)
2375 		if (voice[i].status != VOICE_FREE)
2376 			recompute_freq(i);
2377 }
2378 
adjust_pitch(int c)2379 void Player::adjust_pitch(int c)
2380 {
2381   int i, uv = upper_voices;
2382   for(i = 0; i < uv; i++)
2383     if (voice[i].status != VOICE_FREE && voice[i].channel == c)
2384 	recompute_freq(i);
2385 }
2386 
adjust_volume(int c)2387 void Player::adjust_volume(int c)
2388 {
2389   int i, uv = upper_voices;
2390   for(i = 0; i < uv; i++)
2391     if (voice[i].channel == c &&
2392 	(voice[i].status & (VOICE_ON | VOICE_SUSTAINED)))
2393       {
2394 	recompute_amp(i);
2395 	mixer->apply_envelope_to_amp(i);
2396       }
2397 }
2398 
set_reverb_level(int ch,int level)2399 void Player::set_reverb_level(int ch, int level)
2400 {
2401 	if (level == -1) {
2402 		channel[ch].reverb_level = channel[ch].reverb_id =
2403 				(timidity_reverb < 0)
2404 				? -timidity_reverb & 0x7f : DEFAULT_REVERB_SEND_LEVEL;
2405 		make_rvid_flag = 1;
2406 		return;
2407 	}
2408 	channel[ch].reverb_level = level;
2409 	make_rvid_flag = 0;	/* to update reverb_id */
2410 }
2411 
get_reverb_level(int ch)2412 int Player::get_reverb_level(int ch)
2413 {
2414 	if (channel[ch].reverb_level == -1)
2415 		return (timidity_reverb < 0)
2416 			? -timidity_reverb & 0x7f : DEFAULT_REVERB_SEND_LEVEL;
2417 	return channel[ch].reverb_level;
2418 }
2419 
get_chorus_level(int ch)2420 int Player::get_chorus_level(int ch)
2421 {
2422 #ifdef DISALLOW_DRUM_BENDS
2423     if(ISDRUMCHANNEL(ch))
2424 	return 0; /* Not supported drum channel chorus */
2425 #endif
2426     if(timidity_chorus == 1)
2427 	return channel[ch].chorus_level;
2428     return -timidity_chorus;
2429 }
2430 
2431 
free_drum_effect(int ch)2432 void Player::free_drum_effect(int ch)
2433 {
2434 	int i;
2435 	if (channel[ch].drum_effect != NULL) {
2436 		for (i = 0; i < channel[ch].drum_effect_num; i++) {
2437 			if (channel[ch].drum_effect[i].buf != NULL) {
2438 				free(channel[ch].drum_effect[i].buf);
2439 				channel[ch].drum_effect[i].buf = NULL;
2440 			}
2441 		}
2442 		free(channel[ch].drum_effect);
2443 		channel[ch].drum_effect = NULL;
2444 	}
2445 	channel[ch].drum_effect_num = 0;
2446 	channel[ch].drum_effect_flag = 0;
2447 }
2448 
make_drum_effect(int ch)2449 void Player::make_drum_effect(int ch)
2450 {
2451 	int i, note, num = 0;
2452 	int8_t note_table[128];
2453 	struct DrumParts *drum;
2454 	struct DrumPartEffect *de;
2455 
2456 	if (channel[ch].drum_effect_flag == 0) {
2457 		free_drum_effect(ch);
2458 		memset(note_table, 0, sizeof(int8_t) * 128);
2459 
2460 		for(i = 0; i < 128; i++) {
2461 			if ((drum = channel[ch].drums[i]) != NULL)
2462 			{
2463 				if (drum->reverb_level != -1
2464 				|| drum->chorus_level != -1 || drum->delay_level != -1) {
2465 					note_table[num++] = i;
2466 				}
2467 			}
2468 		}
2469 
2470 		channel[ch].drum_effect = (struct DrumPartEffect *)safe_malloc(sizeof(struct DrumPartEffect) * num);
2471 
2472 		for(i = 0; i < num; i++) {
2473 			de = &(channel[ch].drum_effect[i]);
2474 			de->note = note = note_table[i];
2475 			drum = channel[ch].drums[note];
2476 			de->reverb_send = (int32_t)drum->reverb_level * (int32_t)get_reverb_level(ch) / 127;
2477 			de->chorus_send = (int32_t)drum->chorus_level * (int32_t)channel[ch].chorus_level / 127;
2478 			de->delay_send = (int32_t)drum->delay_level * (int32_t)channel[ch].delay_level / 127;
2479 			de->buf = (int32_t *)safe_malloc(AUDIO_BUFFER_SIZE * 8);
2480 			memset(de->buf, 0, AUDIO_BUFFER_SIZE * 8);
2481 		}
2482 
2483 		channel[ch].drum_effect_num = num;
2484 		channel[ch].drum_effect_flag = 1;
2485 	}
2486 }
2487 
adjust_master_volume(void)2488 void Player::adjust_master_volume(void)
2489 {
2490   int i, uv = upper_voices;
2491   adjust_amplification();
2492   for(i = 0; i < uv; i++)
2493       if(voice[i].status & (VOICE_ON | VOICE_SUSTAINED))
2494       {
2495 	  recompute_amp(i);
2496 	  mixer->apply_envelope_to_amp(i);
2497       }
2498 }
2499 
midi_drumpart_change(int ch,int isdrum)2500 int Player::midi_drumpart_change(int ch, int isdrum)
2501 {
2502 	if (IS_SET_CHANNELMASK(drumchannel_mask, ch))
2503 		return 0;
2504 	if (isdrum) {
2505 		SET_CHANNELMASK(drumchannels, ch);
2506 		SET_CHANNELMASK(current_file_info->drumchannels, ch);
2507 	} else {
2508 		UNSET_CHANNELMASK(drumchannels, ch);
2509 		UNSET_CHANNELMASK(current_file_info->drumchannels, ch);
2510 	}
2511 	return 1;
2512 }
2513 
midi_program_change(int ch,int prog)2514 void Player::midi_program_change(int ch, int prog)
2515 {
2516 	int dr = ISDRUMCHANNEL(ch);
2517 	int newbank, b, p, map;
2518 
2519 	switch (play_system_mode) {
2520 	case GS_SYSTEM_MODE:	/* GS */
2521 		if ((map = channel[ch].bank_lsb) == 0) {
2522 			map = channel[ch].tone_map0_number;
2523 		}
2524 		switch (map) {
2525 		case 0:		/* No change */
2526 			break;
2527 		case 1:
2528 			channel[ch].mapID = (dr) ? SC_55_DRUM_MAP : SC_55_TONE_MAP;
2529 			break;
2530 		case 2:
2531 			channel[ch].mapID = (dr) ? SC_88_DRUM_MAP : SC_88_TONE_MAP;
2532 			break;
2533 		case 3:
2534 			channel[ch].mapID = (dr) ? SC_88PRO_DRUM_MAP : SC_88PRO_TONE_MAP;
2535 			break;
2536 		case 4:
2537 			channel[ch].mapID = (dr) ? SC_8850_DRUM_MAP : SC_8850_TONE_MAP;
2538 			break;
2539 		default:
2540 			break;
2541 		}
2542 		newbank = channel[ch].bank_msb;
2543 		break;
2544 	case XG_SYSTEM_MODE:	/* XG */
2545 		switch (channel[ch].bank_msb) {
2546 		case 0:		/* Normal */
2547 #if 0
2548 			if (ch == 9 && channel[ch].bank_lsb == 127
2549 					&& channel[ch].mapID == XG_DRUM_MAP)
2550 				/* FIXME: Why this part is drum?  Is this correct? */
2551 				break;
2552 #endif
2553 /* Eric's explanation for the FIXME (March 2004):
2554  *
2555  * I don't have the original email from my archived inbox, but I found a
2556  * reply I made in my archived sent-mail from 1999.  A September 5th message
2557  * to Masanao Izumo is discussing a problem with a "reapxg.mid", a file which
2558  * I still have, and how it issues an MSB=0 with a program change on ch 9,
2559  * thus turning it into a melodic channel.  The strange thing is, this doesn't
2560  * happen on XG hardware, nor on the XG softsynth.  It continues to play as a
2561  * normal drum.  The author of the midi file obviously intended it to be
2562  * drumset 16 too.  The original fix was to detect LSB == -1, then break so
2563  * as to not set it to a melodic channel.  I'm guessing that this somehow got
2564  * mutated into checking for 127 instead, and the current FIXME is related to
2565  * the original hack from Sept 1999.  The Sept 5th email discusses patches
2566  * being applied to version 2.5.1 to get XG drums to work properly, and a
2567  * Sept 7th email to someone else discusses the fixes being part of the
2568  * latest 2.6.0-beta3.  A September 23rd email to Masanao Izumo specifically
2569  * mentions the LSB == -1 hack (and reapxg.mid not playing "correctly"
2570  * anymore), as well as new changes in 2.6.0 that broke a lot of other XG
2571  * files (XG drum support was extremely buggy in 1999 and we were still trying
2572  * to figure out how to initialize things to reproduce hardware behavior).  An
2573  * October 5th email says that 2.5.1 was correct, 2.6.0 had very broken XG
2574  * drum changes, and 2.6.1 still has problems.  Further discussions ensued
2575  * over what was "correct": to follow the XG spec, or to reproduce
2576  * "features" / bugs in the hardware.  I can't find the rest of the
2577  * discussions, but I think it ended with us agreeing to just follow the spec
2578  * and not try to reproduce the hardware strangeness.  I don't know how the
2579  * current FIXME wound up the way it is now.  I'm still going to guess it is
2580  * related to the old reapxg.mid hack.
2581  *
2582  * Now that reset_midi() initializes channel[ch].bank_lsb to 0 instead of -1,
2583  * checking for LSB == -1 won't do anything anymore, so changing the above
2584  * FIXME to the original == -1 won't do any good.  It is best to just #if 0
2585  * it out and leave it here as a reminder that there is at least one XG
2586  * hardware / softsynth "bug" that is not reproduced by timidity at the
2587  * moment.
2588  *
2589  * If the current FIXME actually reproduces some other XG hadware bug that
2590  * I don't know about, then it may have a valid purpose.  I just don't know
2591  * what that purpose is at the moment.  Perhaps someone else does?  I still
2592  * have src going back to 2.10.4, and the FIXME comment was already there by
2593  * then.  I don't see any entries in the Changelog that could explain it
2594  * either.  If someone has src from 2.5.1 through 2.10.3 and wants to
2595  * investigate this further, go for it :)
2596  */
2597 			midi_drumpart_change(ch, 0);
2598 			channel[ch].mapID = XG_NORMAL_MAP;
2599 			dr = ISDRUMCHANNEL(ch);
2600 			break;
2601 		case 64:	/* SFX voice */
2602 			midi_drumpart_change(ch, 0);
2603 			channel[ch].mapID = XG_SFX64_MAP;
2604 			dr = ISDRUMCHANNEL(ch);
2605 			break;
2606 		case 126:	/* SFX kit */
2607 			midi_drumpart_change(ch, 1);
2608 			channel[ch].mapID = XG_SFX126_MAP;
2609 			dr = ISDRUMCHANNEL(ch);
2610 			break;
2611 		case 127:	/* Drum kit */
2612 			midi_drumpart_change(ch, 1);
2613 			channel[ch].mapID = XG_DRUM_MAP;
2614 			dr = ISDRUMCHANNEL(ch);
2615 			break;
2616 		default:
2617 			break;
2618 		}
2619 		newbank = channel[ch].bank_lsb;
2620 		break;
2621 	case GM2_SYSTEM_MODE:	/* GM2 */
2622 		if ((channel[ch].bank_msb & 0xfe) == 0x78) {	/* 0x78/0x79 */
2623 			midi_drumpart_change(ch, channel[ch].bank_msb == 0x78);
2624 			dr = ISDRUMCHANNEL(ch);
2625 		}
2626 		channel[ch].mapID = (dr) ? GM2_DRUM_MAP : GM2_TONE_MAP;
2627 		newbank = channel[ch].bank_lsb;
2628 		break;
2629 	default:
2630 		newbank = channel[ch].bank_msb;
2631 		break;
2632 	}
2633 	if (dr) {
2634 		channel[ch].bank = prog;	/* newbank is ignored */
2635 		channel[ch].program = prog;
2636 		if (instruments->drumSet(prog) == NULL || instruments->drumSet(prog)->alt == NULL)
2637 			channel[ch].altassign = instruments->drumSet(0)->alt;
2638 		else
2639 			channel[ch].altassign = instruments->drumSet(prog)->alt;
2640 	} else {
2641 		channel[ch].bank = (special_tonebank >= 0)
2642 				? special_tonebank : newbank;
2643 		channel[ch].program = (instruments->defaultProgram(ch) == SPECIAL_PROGRAM)
2644 				? SPECIAL_PROGRAM : prog;
2645 		channel[ch].altassign = NULL;
2646 		if (opt_realtime_playing)
2647 		{
2648 			b = channel[ch].bank, p = prog;
2649 			instruments->instrument_map(channel[ch].mapID, &b, &p);
2650 			play_midi_load_instrument(0, b, p);
2651 		}
2652 	}
2653 }
2654 
2655 
2656 /*! add a new layer. */
add_channel_layer(int to_ch,int from_ch)2657 void Player::add_channel_layer(int to_ch, int from_ch)
2658 {
2659 	if (to_ch >= MAX_CHANNELS || from_ch >= MAX_CHANNELS)
2660 		return;
2661 	/* add a channel layer */
2662 	UNSET_CHANNELMASK(channel[to_ch].channel_layer, to_ch);
2663 	SET_CHANNELMASK(channel[to_ch].channel_layer, from_ch);
2664 	//printMessage(CMSG_INFO,VERB_NOISY,"Channel Layer (CH:%d -> CH:%d)", from_ch, to_ch);
2665 }
2666 
2667 /*! remove all layers for this channel. */
remove_channel_layer(int ch)2668 void Player::remove_channel_layer(int ch)
2669 {
2670 	int i, offset;
2671 
2672 	if (ch >= MAX_CHANNELS)
2673 		return;
2674 	/* remove channel layers */
2675 	offset = ch & ~0xf;
2676 	for (i = offset; i < offset + REDUCE_CHANNELS; i++)
2677 		UNSET_CHANNELMASK(channel[i].channel_layer, ch);
2678 	SET_CHANNELMASK(channel[ch].channel_layer, ch);
2679 }
2680 
2681 /*! process system exclusive sent from parse_sysex_event_multi(). */
process_sysex_event(int ev,int ch,int val,int b)2682 void Player::process_sysex_event(int ev, int ch, int val, int b)
2683 {
2684 	int temp, msb, note;
2685 
2686 	if (ch >= MAX_CHANNELS)
2687 		return;
2688 	if (ev == ME_SYSEX_MSB) {
2689 		channel[ch].sysex_msb_addr = b;
2690 		channel[ch].sysex_msb_val = val;
2691 	} else if(ev == ME_SYSEX_GS_MSB) {
2692 		channel[ch].sysex_gs_msb_addr = b;
2693 		channel[ch].sysex_gs_msb_val = val;
2694 	} else if(ev == ME_SYSEX_XG_MSB) {
2695 		channel[ch].sysex_xg_msb_addr = b;
2696 		channel[ch].sysex_xg_msb_val = val;
2697 	} else if(ev == ME_SYSEX_LSB) {	/* Universal system exclusive message */
2698 		msb = channel[ch].sysex_msb_addr;
2699 		note = channel[ch].sysex_msb_val;
2700 		channel[ch].sysex_msb_addr = channel[ch].sysex_msb_val = 0;
2701 		switch(b)
2702 		{
2703 		case 0x00:	/* CAf Pitch Control */
2704 			if(val > 0x58) {val = 0x58;}
2705 			else if(val < 0x28) {val = 0x28;}
2706 			channel[ch].caf.pitch = val - 64;
2707 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf Pitch Control (CH:%d %d semitones)", ch, channel[ch].caf.pitch);
2708 			break;
2709 		case 0x01:	/* CAf Filter Cutoff Control */
2710 			channel[ch].caf.cutoff = (val - 64) * 150;
2711 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].caf.cutoff);
2712 			break;
2713 		case 0x02:	/* CAf Amplitude Control */
2714 			channel[ch].caf.amp = (float)val / 64.0f - 1.0f;
2715 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf Amplitude Control (CH:%d %.2f)", ch, channel[ch].caf.amp);
2716 			break;
2717 		case 0x03:	/* CAf LFO1 Rate Control */
2718 			channel[ch].caf.lfo1_rate = (float)(val - 64) / 6.4f;
2719 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].caf.lfo1_rate);
2720 			break;
2721 		case 0x04:	/* CAf LFO1 Pitch Depth */
2722 			channel[ch].caf.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2723 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].caf.lfo1_pitch_depth);
2724 			break;
2725 		case 0x05:	/* CAf LFO1 Filter Depth */
2726 			channel[ch].caf.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2727 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].caf.lfo1_tvf_depth);
2728 			break;
2729 		case 0x06:	/* CAf LFO1 Amplitude Depth */
2730 			channel[ch].caf.lfo1_tva_depth = (float)val / 127.0f;
2731 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].caf.lfo1_tva_depth);
2732 			break;
2733 		case 0x07:	/* CAf LFO2 Rate Control */
2734 			channel[ch].caf.lfo2_rate = (float)(val - 64) / 6.4f;
2735 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].caf.lfo2_rate);
2736 			break;
2737 		case 0x08:	/* CAf LFO2 Pitch Depth */
2738 			channel[ch].caf.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2739 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].caf.lfo2_pitch_depth);
2740 			break;
2741 		case 0x09:	/* CAf LFO2 Filter Depth */
2742 			channel[ch].caf.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2743 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].caf.lfo2_tvf_depth);
2744 			break;
2745 		case 0x0A:	/* CAf LFO2 Amplitude Depth */
2746 			channel[ch].caf.lfo2_tva_depth = (float)val / 127.0f;
2747 			//printMessage(CMSG_INFO,VERB_NOISY,"CAf LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].caf.lfo2_tva_depth);
2748 			break;
2749 		case 0x0B:	/* PAf Pitch Control */
2750 			if(val > 0x58) {val = 0x58;}
2751 			else if(val < 0x28) {val = 0x28;}
2752 			channel[ch].paf.pitch = val - 64;
2753 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf Pitch Control (CH:%d %d semitones)", ch, channel[ch].paf.pitch);
2754 			break;
2755 		case 0x0C:	/* PAf Filter Cutoff Control */
2756 			channel[ch].paf.cutoff = (val - 64) * 150;
2757 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].paf.cutoff);
2758 			break;
2759 		case 0x0D:	/* PAf Amplitude Control */
2760 			channel[ch].paf.amp = (float)val / 64.0f - 1.0f;
2761 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf Amplitude Control (CH:%d %.2f)", ch, channel[ch].paf.amp);
2762 			break;
2763 		case 0x0E:	/* PAf LFO1 Rate Control */
2764 			channel[ch].paf.lfo1_rate = (float)(val - 64) / 6.4f;
2765 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].paf.lfo1_rate);
2766 			break;
2767 		case 0x0F:	/* PAf LFO1 Pitch Depth */
2768 			channel[ch].paf.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2769 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].paf.lfo1_pitch_depth);
2770 			break;
2771 		case 0x10:	/* PAf LFO1 Filter Depth */
2772 			channel[ch].paf.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2773 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].paf.lfo1_tvf_depth);
2774 			break;
2775 		case 0x11:	/* PAf LFO1 Amplitude Depth */
2776 			channel[ch].paf.lfo1_tva_depth = (float)val / 127.0f;
2777 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].paf.lfo1_tva_depth);
2778 			break;
2779 		case 0x12:	/* PAf LFO2 Rate Control */
2780 			channel[ch].paf.lfo2_rate = (float)(val - 64) / 6.4f;
2781 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].paf.lfo2_rate);
2782 			break;
2783 		case 0x13:	/* PAf LFO2 Pitch Depth */
2784 			channel[ch].paf.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2785 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].paf.lfo2_pitch_depth);
2786 			break;
2787 		case 0x14:	/* PAf LFO2 Filter Depth */
2788 			channel[ch].paf.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2789 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].paf.lfo2_tvf_depth);
2790 			break;
2791 		case 0x15:	/* PAf LFO2 Amplitude Depth */
2792 			channel[ch].paf.lfo2_tva_depth = (float)val / 127.0f;
2793 			//printMessage(CMSG_INFO,VERB_NOISY,"PAf LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].paf.lfo2_tva_depth);
2794 			break;
2795 		case 0x16:	/* MOD Pitch Control */
2796 			if(val > 0x58) {val = 0x58;}
2797 			else if(val < 0x28) {val = 0x28;}
2798 			channel[ch].mod.pitch = val - 64;
2799 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD Pitch Control (CH:%d %d semitones)", ch, channel[ch].mod.pitch);
2800 			break;
2801 		case 0x17:	/* MOD Filter Cutoff Control */
2802 			channel[ch].mod.cutoff = (val - 64) * 150;
2803 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].mod.cutoff);
2804 			break;
2805 		case 0x18:	/* MOD Amplitude Control */
2806 			channel[ch].mod.amp = (float)val / 64.0f - 1.0f;
2807 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD Amplitude Control (CH:%d %.2f)", ch, channel[ch].mod.amp);
2808 			break;
2809 		case 0x19:	/* MOD LFO1 Rate Control */
2810 			channel[ch].mod.lfo1_rate = (float)(val - 64) / 6.4f;
2811 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].mod.lfo1_rate);
2812 			break;
2813 		case 0x1A:	/* MOD LFO1 Pitch Depth */
2814 			channel[ch].mod.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2815 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].mod.lfo1_pitch_depth);
2816 			break;
2817 		case 0x1B:	/* MOD LFO1 Filter Depth */
2818 			channel[ch].mod.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2819 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].mod.lfo1_tvf_depth);
2820 			break;
2821 		case 0x1C:	/* MOD LFO1 Amplitude Depth */
2822 			channel[ch].mod.lfo1_tva_depth = (float)val / 127.0f;
2823 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].mod.lfo1_tva_depth);
2824 			break;
2825 		case 0x1D:	/* MOD LFO2 Rate Control */
2826 			channel[ch].mod.lfo2_rate = (float)(val - 64) / 6.4f;
2827 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].mod.lfo2_rate);
2828 			break;
2829 		case 0x1E:	/* MOD LFO2 Pitch Depth */
2830 			channel[ch].mod.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2831 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].mod.lfo2_pitch_depth);
2832 			break;
2833 		case 0x1F:	/* MOD LFO2 Filter Depth */
2834 			channel[ch].mod.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2835 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].mod.lfo2_tvf_depth);
2836 			break;
2837 		case 0x20:	/* MOD LFO2 Amplitude Depth */
2838 			channel[ch].mod.lfo2_tva_depth = (float)val / 127.0f;
2839 			//printMessage(CMSG_INFO,VERB_NOISY,"MOD LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].mod.lfo2_tva_depth);
2840 			break;
2841 		case 0x21:	/* BEND Pitch Control */
2842 			if(val > 0x58) {val = 0x58;}
2843 			else if(val < 0x28) {val = 0x28;}
2844 			channel[ch].bend.pitch = val - 64;
2845 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND Pitch Control (CH:%d %d semitones)", ch, channel[ch].bend.pitch);
2846 			break;
2847 		case 0x22:	/* BEND Filter Cutoff Control */
2848 			channel[ch].bend.cutoff = (val - 64) * 150;
2849 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].bend.cutoff);
2850 			break;
2851 		case 0x23:	/* BEND Amplitude Control */
2852 			channel[ch].bend.amp = (float)val / 64.0f - 1.0f;
2853 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND Amplitude Control (CH:%d %.2f)", ch, channel[ch].bend.amp);
2854 			break;
2855 		case 0x24:	/* BEND LFO1 Rate Control */
2856 			channel[ch].bend.lfo1_rate = (float)(val - 64) / 6.4f;
2857 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].bend.lfo1_rate);
2858 			break;
2859 		case 0x25:	/* BEND LFO1 Pitch Depth */
2860 			channel[ch].bend.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2861 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].bend.lfo1_pitch_depth);
2862 			break;
2863 		case 0x26:	/* BEND LFO1 Filter Depth */
2864 			channel[ch].bend.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2865 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].bend.lfo1_tvf_depth);
2866 			break;
2867 		case 0x27:	/* BEND LFO1 Amplitude Depth */
2868 			channel[ch].bend.lfo1_tva_depth = (float)val / 127.0f;
2869 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].bend.lfo1_tva_depth);
2870 			break;
2871 		case 0x28:	/* BEND LFO2 Rate Control */
2872 			channel[ch].bend.lfo2_rate = (float)(val - 64) / 6.4f;
2873 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].bend.lfo2_rate);
2874 			break;
2875 		case 0x29:	/* BEND LFO2 Pitch Depth */
2876 			channel[ch].bend.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2877 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].bend.lfo2_pitch_depth);
2878 			break;
2879 		case 0x2A:	/* BEND LFO2 Filter Depth */
2880 			channel[ch].bend.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2881 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].bend.lfo2_tvf_depth);
2882 			break;
2883 		case 0x2B:	/* BEND LFO2 Amplitude Depth */
2884 			channel[ch].bend.lfo2_tva_depth = (float)val / 127.0f;
2885 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].bend.lfo2_tva_depth);
2886 			break;
2887 		case 0x2C:	/* CC1 Pitch Control */
2888 			if(val > 0x58) {val = 0x58;}
2889 			else if(val < 0x28) {val = 0x28;}
2890 			channel[ch].cc1.pitch = val - 64;
2891 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 Pitch Control (CH:%d %d semitones)", ch, channel[ch].cc1.pitch);
2892 			break;
2893 		case 0x2D:	/* CC1 Filter Cutoff Control */
2894 			channel[ch].cc1.cutoff = (val - 64) * 150;
2895 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].cc1.cutoff);
2896 			break;
2897 		case 0x2E:	/* CC1 Amplitude Control */
2898 			channel[ch].cc1.amp = (float)val / 64.0f - 1.0f;
2899 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 Amplitude Control (CH:%d %.2f)", ch, channel[ch].cc1.amp);
2900 			break;
2901 		case 0x2F:	/* CC1 LFO1 Rate Control */
2902 			channel[ch].cc1.lfo1_rate = (float)(val - 64) / 6.4f;
2903 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].cc1.lfo1_rate);
2904 			break;
2905 		case 0x30:	/* CC1 LFO1 Pitch Depth */
2906 			channel[ch].cc1.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2907 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].cc1.lfo1_pitch_depth);
2908 			break;
2909 		case 0x31:	/* CC1 LFO1 Filter Depth */
2910 			channel[ch].cc1.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2911 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].cc1.lfo1_tvf_depth);
2912 			break;
2913 		case 0x32:	/* CC1 LFO1 Amplitude Depth */
2914 			channel[ch].cc1.lfo1_tva_depth = (float)val / 127.0f;
2915 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].cc1.lfo1_tva_depth);
2916 			break;
2917 		case 0x33:	/* CC1 LFO2 Rate Control */
2918 			channel[ch].cc1.lfo2_rate = (float)(val - 64) / 6.4f;
2919 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].cc1.lfo2_rate);
2920 			break;
2921 		case 0x34:	/* CC1 LFO2 Pitch Depth */
2922 			channel[ch].cc1.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2923 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].cc1.lfo2_pitch_depth);
2924 			break;
2925 		case 0x35:	/* CC1 LFO2 Filter Depth */
2926 			channel[ch].cc1.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2927 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].cc1.lfo2_tvf_depth);
2928 			break;
2929 		case 0x36:	/* CC1 LFO2 Amplitude Depth */
2930 			channel[ch].cc1.lfo2_tva_depth = (float)val / 127.0f;
2931 			//printMessage(CMSG_INFO,VERB_NOISY,"CC1 LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].cc1.lfo2_tva_depth);
2932 			break;
2933 		case 0x37:	/* CC2 Pitch Control */
2934 			if(val > 0x58) {val = 0x58;}
2935 			else if(val < 0x28) {val = 0x28;}
2936 			channel[ch].cc2.pitch = val - 64;
2937 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 Pitch Control (CH:%d %d semitones)", ch, channel[ch].cc2.pitch);
2938 			break;
2939 		case 0x38:	/* CC2 Filter Cutoff Control */
2940 			channel[ch].cc2.cutoff = (val - 64) * 150;
2941 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 Filter Cutoff Control (CH:%d %d cents)", ch, channel[ch].cc2.cutoff);
2942 			break;
2943 		case 0x39:	/* CC2 Amplitude Control */
2944 			channel[ch].cc2.amp = (float)val / 64.0f - 1.0f;
2945 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 Amplitude Control (CH:%d %.2f)", ch, channel[ch].cc2.amp);
2946 			break;
2947 		case 0x3A:	/* CC2 LFO1 Rate Control */
2948 			channel[ch].cc2.lfo1_rate = (float)(val - 64) / 6.4f;
2949 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO1 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].cc2.lfo1_rate);
2950 			break;
2951 		case 0x3B:	/* CC2 LFO1 Pitch Depth */
2952 			channel[ch].cc2.lfo1_pitch_depth = conv_lfo_pitch_depth(val);
2953 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO1 Pitch Depth (CH:%d %d cents)", ch, channel[ch].cc2.lfo1_pitch_depth);
2954 			break;
2955 		case 0x3C:	/* CC2 LFO1 Filter Depth */
2956 			channel[ch].cc2.lfo1_tvf_depth = conv_lfo_filter_depth(val);
2957 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO1 Filter Depth (CH:%d %d cents)", ch, channel[ch].cc2.lfo1_tvf_depth);
2958 			break;
2959 		case 0x3D:	/* CC2 LFO1 Amplitude Depth */
2960 			channel[ch].cc2.lfo1_tva_depth = (float)val / 127.0f;
2961 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO1 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].cc2.lfo1_tva_depth);
2962 			break;
2963 		case 0x3E:	/* CC2 LFO2 Rate Control */
2964 			channel[ch].cc2.lfo2_rate = (float)(val - 64) / 6.4f;
2965 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO2 Rate Control (CH:%d %.1f Hz)", ch, channel[ch].cc2.lfo2_rate);
2966 			break;
2967 		case 0x3F:	/* CC2 LFO2 Pitch Depth */
2968 			channel[ch].cc2.lfo2_pitch_depth = conv_lfo_pitch_depth(val);
2969 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO2 Pitch Depth (CH:%d %d cents)", ch, channel[ch].cc2.lfo2_pitch_depth);
2970 			break;
2971 		case 0x40:	/* CC2 LFO2 Filter Depth */
2972 			channel[ch].cc2.lfo2_tvf_depth = conv_lfo_filter_depth(val);
2973 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO2 Filter Depth (CH:%d %d cents)", ch, channel[ch].cc2.lfo2_tvf_depth);
2974 			break;
2975 		case 0x41:	/* CC2 LFO2 Amplitude Depth */
2976 			channel[ch].cc2.lfo2_tva_depth = (float)val / 127.0f;
2977 			//printMessage(CMSG_INFO,VERB_NOISY,"CC2 LFO2 Amplitude Depth (CH:%d %.2f)", ch, channel[ch].cc2.lfo2_tva_depth);
2978 			break;
2979 		case 0x42:	/* Note Limit Low */
2980 			channel[ch].note_limit_low = val;
2981 			//printMessage(CMSG_INFO,VERB_NOISY,"Note Limit Low (CH:%d VAL:%d)", ch, val);
2982 			break;
2983 		case 0x43:	/* Note Limit High */
2984 			channel[ch].note_limit_high = val;
2985 			//printMessage(CMSG_INFO,VERB_NOISY,"Note Limit High (CH:%d VAL:%d)", ch, val);
2986 			break;
2987 		case 0x44:	/* Velocity Limit Low */
2988 			channel[ch].vel_limit_low = val;
2989 			//printMessage(CMSG_INFO,VERB_NOISY,"Velocity Limit Low (CH:%d VAL:%d)", ch, val);
2990 			break;
2991 		case 0x45:	/* Velocity Limit High */
2992 			channel[ch].vel_limit_high = val;
2993 			//printMessage(CMSG_INFO,VERB_NOISY,"Velocity Limit High (CH:%d VAL:%d)", ch, val);
2994 			break;
2995 		case 0x46:	/* Rx. Note Off */
2996 			if (channel[ch].drums[note] == NULL)
2997 				play_midi_setup_drums(ch, note);
2998 			set_rx_drum(channel[ch].drums[note], RX_NOTE_OFF, val);
2999 			printMessage(CMSG_INFO, VERB_NOISY,
3000 				"Drum Instrument Rx. Note Off (CH:%d NOTE:%d VAL:%d)",
3001 				ch, note, val);
3002 			break;
3003 		case 0x47:	/* Rx. Note On */
3004 			if (channel[ch].drums[note] == NULL)
3005 				play_midi_setup_drums(ch, note);
3006 			set_rx_drum(channel[ch].drums[note], RX_NOTE_ON, val);
3007 			printMessage(CMSG_INFO, VERB_NOISY,
3008 				"Drum Instrument Rx. Note On (CH:%d NOTE:%d VAL:%d)",
3009 				ch, note, val);
3010 			break;
3011 		case 0x48:	/* Rx. Pitch Bend */
3012 			set_rx(ch, RX_PITCH_BEND, val);
3013 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Pitch Bend (CH:%d VAL:%d)", ch, val);
3014 			break;
3015 		case 0x49:	/* Rx. Channel Pressure */
3016 			set_rx(ch, RX_CH_PRESSURE, val);
3017 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Channel Pressure (CH:%d VAL:%d)", ch, val);
3018 			break;
3019 		case 0x4A:	/* Rx. Program Change */
3020 			set_rx(ch, RX_PROGRAM_CHANGE, val);
3021 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Program Change (CH:%d VAL:%d)", ch, val);
3022 			break;
3023 		case 0x4B:	/* Rx. Control Change */
3024 			set_rx(ch, RX_CONTROL_CHANGE, val);
3025 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Control Change (CH:%d VAL:%d)", ch, val);
3026 			break;
3027 		case 0x4C:	/* Rx. Poly Pressure */
3028 			set_rx(ch, RX_POLY_PRESSURE, val);
3029 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Poly Pressure (CH:%d VAL:%d)", ch, val);
3030 			break;
3031 		case 0x4D:	/* Rx. Note Message */
3032 			set_rx(ch, RX_NOTE_MESSAGE, val);
3033 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Note Message (CH:%d VAL:%d)", ch, val);
3034 			break;
3035 		case 0x4E:	/* Rx. RPN */
3036 			set_rx(ch, RX_RPN, val);
3037 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. RPN (CH:%d VAL:%d)", ch, val);
3038 			break;
3039 		case 0x4F:	/* Rx. NRPN */
3040 			set_rx(ch, RX_NRPN, val);
3041 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. NRPN (CH:%d VAL:%d)", ch, val);
3042 			break;
3043 		case 0x50:	/* Rx. Modulation */
3044 			set_rx(ch, RX_MODULATION, val);
3045 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Modulation (CH:%d VAL:%d)", ch, val);
3046 			break;
3047 		case 0x51:	/* Rx. Volume */
3048 			set_rx(ch, RX_VOLUME, val);
3049 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Volume (CH:%d VAL:%d)", ch, val);
3050 			break;
3051 		case 0x52:	/* Rx. Panpot */
3052 			set_rx(ch, RX_PANPOT, val);
3053 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Panpot (CH:%d VAL:%d)", ch, val);
3054 			break;
3055 		case 0x53:	/* Rx. Expression */
3056 			set_rx(ch, RX_EXPRESSION, val);
3057 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Expression (CH:%d VAL:%d)", ch, val);
3058 			break;
3059 		case 0x54:	/* Rx. Hold1 */
3060 			set_rx(ch, RX_HOLD1, val);
3061 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Hold1 (CH:%d VAL:%d)", ch, val);
3062 			break;
3063 		case 0x55:	/* Rx. Portamento */
3064 			set_rx(ch, RX_PORTAMENTO, val);
3065 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Portamento (CH:%d VAL:%d)", ch, val);
3066 			break;
3067 		case 0x56:	/* Rx. Sostenuto */
3068 			set_rx(ch, RX_SOSTENUTO, val);
3069 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Sostenuto (CH:%d VAL:%d)", ch, val);
3070 			break;
3071 		case 0x57:	/* Rx. Soft */
3072 			set_rx(ch, RX_SOFT, val);
3073 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Soft (CH:%d VAL:%d)", ch, val);
3074 			break;
3075 		case 0x58:	/* Rx. Bank Select */
3076 			set_rx(ch, RX_BANK_SELECT, val);
3077 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Bank Select (CH:%d VAL:%d)", ch, val);
3078 			break;
3079 		case 0x59:	/* Rx. Bank Select LSB */
3080 			set_rx(ch, RX_BANK_SELECT_LSB, val);
3081 			//printMessage(CMSG_INFO,VERB_NOISY,"Rx. Bank Select LSB (CH:%d VAL:%d)", ch, val);
3082 			break;
3083 		case 0x60:	/* Reverb Type (GM2) */
3084 			if (val > 8) {val = 8;}
3085 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Type (%d)", val);
3086 			reverb->set_reverb_macro_gm2(val);
3087 			reverb->recompute_reverb_status_gs();
3088 			reverb->init_reverb();
3089 			break;
3090 		case 0x61:	/* Chorus Type (GM2) */
3091 			if (val > 5) {val = 5;}
3092 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Type (%d)", val);
3093 			reverb->set_chorus_macro_gs(val);
3094 			reverb->recompute_chorus_status_gs();
3095 			reverb->init_ch_chorus();
3096 			break;
3097 		default:
3098 			break;
3099 		}
3100 		return;
3101 	} else if(ev == ME_SYSEX_GS_LSB) {	/* GS system exclusive message */
3102 		msb = channel[ch].sysex_gs_msb_addr;
3103 		note = channel[ch].sysex_gs_msb_val;
3104 		channel[ch].sysex_gs_msb_addr = channel[ch].sysex_gs_msb_val = 0;
3105 		switch(b)
3106 		{
3107 		case 0x00:	/* EQ ON/OFF */
3108 			if(!opt_eq_control) {break;}
3109 			channel[ch].eq_gs = val;
3110 			break;
3111 		case 0x01:	/* EQ LOW FREQ */
3112 			if(!opt_eq_control) {break;}
3113 			reverb->eq_status_gs.low_freq = val;
3114 			reverb->recompute_eq_status_gs();
3115 			break;
3116 		case 0x02:	/* EQ LOW GAIN */
3117 			if(!opt_eq_control) {break;}
3118 			reverb->eq_status_gs.low_gain = val;
3119 			reverb->recompute_eq_status_gs();
3120 			break;
3121 		case 0x03:	/* EQ HIGH FREQ */
3122 			if(!opt_eq_control) {break;}
3123 			reverb->eq_status_gs.high_freq = val;
3124 			reverb->recompute_eq_status_gs();
3125 			break;
3126 		case 0x04:	/* EQ HIGH GAIN */
3127 			if(!opt_eq_control) {break;}
3128 			reverb->eq_status_gs.high_gain = val;
3129 			reverb->recompute_eq_status_gs();
3130 			break;
3131 		case 0x05:	/* Reverb Macro */
3132 			if (val > 7) {val = 7;}
3133 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Macro (%d)",val);
3134 			reverb->set_reverb_macro_gs(val);
3135 			reverb->recompute_reverb_status_gs();
3136 			reverb->init_reverb();
3137 			break;
3138 		case 0x06:	/* Reverb Character */
3139 			if (val > 7) {val = 7;}
3140 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Character (%d)",val);
3141 			if (reverb->reverb_status_gs.character != val) {
3142 				reverb->reverb_status_gs.character = val;
3143 				reverb->recompute_reverb_status_gs();
3144 				reverb->init_reverb();
3145 			}
3146 			break;
3147 		case 0x07:	/* Reverb Pre-LPF */
3148 			if (val > 7) {val = 7;}
3149 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Pre-LPF (%d)",val);
3150 			if(reverb->reverb_status_gs.pre_lpf != val) {
3151 				reverb->reverb_status_gs.pre_lpf = val;
3152 				reverb->recompute_reverb_status_gs();
3153 			}
3154 			break;
3155 		case 0x08:	/* Reverb Level */
3156 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Level (%d)",val);
3157 			if(reverb->reverb_status_gs.level != val) {
3158 				reverb->reverb_status_gs.level = val;
3159 				reverb->recompute_reverb_status_gs();
3160 				reverb->init_reverb();
3161 			}
3162 			break;
3163 		case 0x09:	/* Reverb Time */
3164 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Time (%d)",val);
3165 			if(reverb->reverb_status_gs.time != val) {
3166 				reverb->reverb_status_gs.time = val;
3167 				reverb->recompute_reverb_status_gs();
3168 				reverb->init_reverb();
3169 			}
3170 			break;
3171 		case 0x0A:	/* Reverb Delay Feedback */
3172 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Delay Feedback (%d)",val);
3173 			if(reverb->reverb_status_gs.delay_feedback != val) {
3174 				reverb->reverb_status_gs.delay_feedback = val;
3175 				reverb->recompute_reverb_status_gs();
3176 				reverb->init_reverb();
3177 			}
3178 			break;
3179 		case 0x0C:	/* Reverb Predelay Time */
3180 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Predelay Time (%d)",val);
3181 			if(reverb->reverb_status_gs.pre_delay_time != val) {
3182 				reverb->reverb_status_gs.pre_delay_time = val;
3183 				reverb->recompute_reverb_status_gs();
3184 				reverb->init_reverb();
3185 			}
3186 			break;
3187 		case 0x0D:	/* Chorus Macro */
3188 			if (val > 7) {val = 7;}
3189 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Macro (%d)",val);
3190 			reverb->set_chorus_macro_gs(val);
3191 			reverb->recompute_chorus_status_gs();
3192 			reverb->init_ch_chorus();
3193 			break;
3194 		case 0x0E:	/* Chorus Pre-LPF */
3195 			if (val > 7) {val = 7;}
3196 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Pre-LPF (%d)",val);
3197 			if (reverb->chorus_status_gs.pre_lpf != val) {
3198 				reverb->chorus_status_gs.pre_lpf = val;
3199 				reverb->recompute_chorus_status_gs();
3200 			}
3201 			break;
3202 		case 0x0F:	/* Chorus Level */
3203 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Level (%d)",val);
3204 			if (reverb->chorus_status_gs.level != val) {
3205 				reverb->chorus_status_gs.level = val;
3206 				reverb->recompute_chorus_status_gs();
3207 				reverb->init_ch_chorus();
3208 			}
3209 			break;
3210 		case 0x10:	/* Chorus Feedback */
3211 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Feedback (%d)",val);
3212 			if (reverb->chorus_status_gs.feedback != val) {
3213 				reverb->chorus_status_gs.feedback = val;
3214 				reverb->recompute_chorus_status_gs();
3215 				reverb->init_ch_chorus();
3216 			}
3217 			break;
3218 		case 0x11:	/* Chorus Delay */
3219 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Delay (%d)",val);
3220 			if (reverb->chorus_status_gs.delay != val) {
3221 				reverb->chorus_status_gs.delay = val;
3222 				reverb->recompute_chorus_status_gs();
3223 				reverb->init_ch_chorus();
3224 			}
3225 			break;
3226 		case 0x12:	/* Chorus Rate */
3227 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Rate (%d)",val);
3228 			if (reverb->chorus_status_gs.rate != val) {
3229 				reverb->chorus_status_gs.rate = val;
3230 				reverb->recompute_chorus_status_gs();
3231 				reverb->init_ch_chorus();
3232 			}
3233 			break;
3234 		case 0x13:	/* Chorus Depth */
3235 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Depth (%d)",val);
3236 			if (reverb->chorus_status_gs.depth != val) {
3237 				reverb->chorus_status_gs.depth = val;
3238 				reverb->recompute_chorus_status_gs();
3239 				reverb->init_ch_chorus();
3240 			}
3241 			break;
3242 		case 0x14:	/* Chorus Send Level to Reverb */
3243 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Send Level to Reverb (%d)",val);
3244 			if (reverb->chorus_status_gs.send_reverb != val) {
3245 				reverb->chorus_status_gs.send_reverb = val;
3246 				reverb->recompute_chorus_status_gs();
3247 				reverb->init_ch_chorus();
3248 			}
3249 			break;
3250 		case 0x15:	/* Chorus Send Level to Delay */
3251 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Send Level to Delay (%d)",val);
3252 			if (reverb->chorus_status_gs.send_delay != val) {
3253 				reverb->chorus_status_gs.send_delay = val;
3254 				reverb->recompute_chorus_status_gs();
3255 				reverb->init_ch_chorus();
3256 			}
3257 			break;
3258 		case 0x16:	/* Delay Macro */
3259 			if (val > 7) {val = 7;}
3260 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Macro (%d)",val);
3261 			reverb->set_delay_macro_gs(val);
3262 			reverb->recompute_delay_status_gs();
3263 			reverb->init_ch_delay();
3264 			break;
3265 		case 0x17:	/* Delay Pre-LPF */
3266 			if (val > 7) {val = 7;}
3267 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Pre-LPF (%d)",val);
3268 			val &= 0x7;
3269 			if (reverb->delay_status_gs.pre_lpf != val) {
3270 				reverb->delay_status_gs.pre_lpf = val;
3271 				reverb->recompute_delay_status_gs();
3272 			}
3273 			break;
3274 		case 0x18:	/* Delay Time Center */
3275 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Time Center (%d)",val);
3276 			if (reverb->delay_status_gs.time_c != val) {
3277 				reverb->delay_status_gs.time_c = val;
3278 				reverb->recompute_delay_status_gs();
3279 				reverb->init_ch_delay();
3280 			}
3281 			break;
3282 		case 0x19:	/* Delay Time Ratio Left */
3283 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Time Ratio Left (%d)",val);
3284 			if (val == 0) {val = 1;}
3285 			if (reverb->delay_status_gs.time_l != val) {
3286 				reverb->delay_status_gs.time_l = val;
3287 				reverb->recompute_delay_status_gs();
3288 				reverb->init_ch_delay();
3289 			}
3290 			break;
3291 		case 0x1A:	/* Delay Time Ratio Right */
3292 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Time Ratio Right (%d)",val);
3293 			if (val == 0) {val = 1;}
3294 			if (reverb->delay_status_gs.time_r != val) {
3295 				reverb->delay_status_gs.time_r = val;
3296 				reverb->recompute_delay_status_gs();
3297 				reverb->init_ch_delay();
3298 			}
3299 			break;
3300 		case 0x1B:	/* Delay Level Center */
3301 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Level Center (%d)",val);
3302 			if (reverb->delay_status_gs.level_center != val) {
3303 				reverb->delay_status_gs.level_center = val;
3304 				reverb->recompute_delay_status_gs();
3305 				reverb->init_ch_delay();
3306 			}
3307 			break;
3308 		case 0x1C:	/* Delay Level Left */
3309 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Level Left (%d)",val);
3310 			if (reverb->delay_status_gs.level_left != val) {
3311 				reverb->delay_status_gs.level_left = val;
3312 				reverb->recompute_delay_status_gs();
3313 				reverb->init_ch_delay();
3314 			}
3315 			break;
3316 		case 0x1D:	/* Delay Level Right */
3317 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Level Right (%d)",val);
3318 			if (reverb->delay_status_gs.level_right != val) {
3319 				reverb->delay_status_gs.level_right = val;
3320 				reverb->recompute_delay_status_gs();
3321 				reverb->init_ch_delay();
3322 			}
3323 			break;
3324 		case 0x1E:	/* Delay Level */
3325 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Level (%d)",val);
3326 			if (reverb->delay_status_gs.level != val) {
3327 				reverb->delay_status_gs.level = val;
3328 				reverb->recompute_delay_status_gs();
3329 				reverb->init_ch_delay();
3330 			}
3331 			break;
3332 		case 0x1F:	/* Delay Feedback */
3333 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Feedback (%d)",val);
3334 			if (reverb->delay_status_gs.feedback != val) {
3335 				reverb->delay_status_gs.feedback = val;
3336 				reverb->recompute_delay_status_gs();
3337 				reverb->init_ch_delay();
3338 			}
3339 			break;
3340 		case 0x20:	/* Delay Send Level to Reverb */
3341 			//printMessage(CMSG_INFO,VERB_NOISY,"Delay Send Level to Reverb (%d)",val);
3342 			if (reverb->delay_status_gs.send_reverb != val) {
3343 				reverb->delay_status_gs.send_reverb = val;
3344 				reverb->recompute_delay_status_gs();
3345 				reverb->init_ch_delay();
3346 			}
3347 			break;
3348 		case 0x21:	/* Velocity Sense Depth */
3349 			channel[ch].velocity_sense_depth = val;
3350 			//printMessage(CMSG_INFO,VERB_NOISY,"Velocity Sense Depth (CH:%d VAL:%d)",ch,val);
3351 			break;
3352 		case 0x22:	/* Velocity Sense Offset */
3353 			channel[ch].velocity_sense_offset = val;
3354 			//printMessage(CMSG_INFO,VERB_NOISY,"Velocity Sense Offset (CH:%d VAL:%d)",ch,val);
3355 			break;
3356 		case 0x23:	/* Insertion Effect ON/OFF */
3357 			if(!opt_insertion_effect) {break;}
3358 			if(channel[ch].insertion_effect != val) {
3359 				//if(val) {//printMessage(CMSG_INFO,VERB_NOISY,"EFX ON (CH:%d)",ch);}
3360 				//else {//printMessage(CMSG_INFO,VERB_NOISY,"EFX OFF (CH:%d)",ch);}
3361 			}
3362 			channel[ch].insertion_effect = val;
3363 			break;
3364 		case 0x24:	/* Assign Mode */
3365 			channel[ch].assign_mode = val;
3366 			if(val == 0) {
3367 				//printMessage(CMSG_INFO,VERB_NOISY,"Assign Mode: Single (CH:%d)",ch);
3368 			} else if(val == 1) {
3369 				//printMessage(CMSG_INFO,VERB_NOISY,"Assign Mode: Limited-Multi (CH:%d)",ch);
3370 			} else if(val == 2) {
3371 				//printMessage(CMSG_INFO,VERB_NOISY,"Assign Mode: Full-Multi (CH:%d)",ch);
3372 			}
3373 			break;
3374 		case 0x25:	/* TONE MAP-0 NUMBER */
3375 			channel[ch].tone_map0_number = val;
3376 			//printMessage(CMSG_INFO,VERB_NOISY,"Tone Map-0 Number (CH:%d VAL:%d)",ch,val);
3377 			break;
3378 		case 0x26:	/* Pitch Offset Fine */
3379 			channel[ch].pitch_offset_fine = (double)((((int32_t)val << 4) | (int32_t)val) - 0x80) / 10.0;
3380 			//printMessage(CMSG_INFO,VERB_NOISY,"Pitch Offset Fine (CH:%d %3fHz)",ch,channel[ch].pitch_offset_fine);
3381 			break;
3382 		case 0x27:	/* Insertion Effect Parameter */
3383 			if(!opt_insertion_effect) {break;}
3384 			temp = reverb->insertion_effect_gs.type;
3385 			reverb->insertion_effect_gs.type_msb = val;
3386 			reverb->insertion_effect_gs.type = ((int32_t)reverb->insertion_effect_gs.type_msb << 8) | (int32_t)reverb->insertion_effect_gs.type_lsb;
3387 			if(temp == reverb->insertion_effect_gs.type) {
3388 				reverb->recompute_insertion_effect_gs();
3389 			} else {
3390 				reverb->realloc_insertion_effect_gs();
3391 			}
3392 			break;
3393 		case 0x28:	/* Insertion Effect Parameter */
3394 			if(!opt_insertion_effect) {break;}
3395 			temp = reverb->insertion_effect_gs.type;
3396 			reverb->insertion_effect_gs.type_lsb = val;
3397 			reverb->insertion_effect_gs.type = ((int32_t)reverb->insertion_effect_gs.type_msb << 8) | (int32_t)reverb->insertion_effect_gs.type_lsb;
3398 			if(temp == reverb->insertion_effect_gs.type) {
3399 				reverb->recompute_insertion_effect_gs();
3400 			} else {
3401 				reverb->realloc_insertion_effect_gs();
3402 			}
3403 			break;
3404 		case 0x29:
3405 			reverb->insertion_effect_gs.parameter[0] = val;
3406 			reverb->recompute_insertion_effect_gs();
3407 			break;
3408 		case 0x2A:
3409 			reverb->insertion_effect_gs.parameter[1] = val;
3410 			reverb->recompute_insertion_effect_gs();
3411 			break;
3412 		case 0x2B:
3413 			reverb->insertion_effect_gs.parameter[2] = val;
3414 			reverb->recompute_insertion_effect_gs();
3415 			break;
3416 		case 0x2C:
3417 			reverb->insertion_effect_gs.parameter[3] = val;
3418 			reverb->recompute_insertion_effect_gs();
3419 			break;
3420 		case 0x2D:
3421 			reverb->insertion_effect_gs.parameter[4] = val;
3422 			reverb->recompute_insertion_effect_gs();
3423 			break;
3424 		case 0x2E:
3425 			reverb->insertion_effect_gs.parameter[5] = val;
3426 			reverb->recompute_insertion_effect_gs();
3427 			break;
3428 		case 0x2F:
3429 			reverb->insertion_effect_gs.parameter[6] = val;
3430 			reverb->recompute_insertion_effect_gs();
3431 			break;
3432 		case 0x30:
3433 			reverb->insertion_effect_gs.parameter[7] = val;
3434 			reverb->recompute_insertion_effect_gs();
3435 			break;
3436 		case 0x31:
3437 			reverb->insertion_effect_gs.parameter[8] = val;
3438 			reverb->recompute_insertion_effect_gs();
3439 			break;
3440 		case 0x32:
3441 			reverb->insertion_effect_gs.parameter[9] = val;
3442 			reverb->recompute_insertion_effect_gs();
3443 			break;
3444 		case 0x33:
3445 			reverb->insertion_effect_gs.parameter[10] = val;
3446 			reverb->recompute_insertion_effect_gs();
3447 			break;
3448 		case 0x34:
3449 			reverb->insertion_effect_gs.parameter[11] = val;
3450 			reverb->recompute_insertion_effect_gs();
3451 			break;
3452 		case 0x35:
3453 			reverb->insertion_effect_gs.parameter[12] = val;
3454 			reverb->recompute_insertion_effect_gs();
3455 			break;
3456 		case 0x36:
3457 			reverb->insertion_effect_gs.parameter[13] = val;
3458 			reverb->recompute_insertion_effect_gs();
3459 			break;
3460 		case 0x37:
3461 			reverb->insertion_effect_gs.parameter[14] = val;
3462 			reverb->recompute_insertion_effect_gs();
3463 			break;
3464 		case 0x38:
3465 			reverb->insertion_effect_gs.parameter[15] = val;
3466 			reverb->recompute_insertion_effect_gs();
3467 			break;
3468 		case 0x39:
3469 			reverb->insertion_effect_gs.parameter[16] = val;
3470 			reverb->recompute_insertion_effect_gs();
3471 			break;
3472 		case 0x3A:
3473 			reverb->insertion_effect_gs.parameter[17] = val;
3474 			reverb->recompute_insertion_effect_gs();
3475 			break;
3476 		case 0x3B:
3477 			reverb->insertion_effect_gs.parameter[18] = val;
3478 			reverb->recompute_insertion_effect_gs();
3479 			break;
3480 		case 0x3C:
3481 			reverb->insertion_effect_gs.parameter[19] = val;
3482 			reverb->recompute_insertion_effect_gs();
3483 			break;
3484 		case 0x3D:
3485 			reverb->insertion_effect_gs.send_reverb = val;
3486 			reverb->recompute_insertion_effect_gs();
3487 			break;
3488 		case 0x3E:
3489 			reverb->insertion_effect_gs.send_chorus = val;
3490 			reverb->recompute_insertion_effect_gs();
3491 			break;
3492 		case 0x3F:
3493 			reverb->insertion_effect_gs.send_delay = val;
3494 			reverb->recompute_insertion_effect_gs();
3495 			break;
3496 		case 0x40:
3497 			reverb->insertion_effect_gs.control_source1 = val;
3498 			reverb->recompute_insertion_effect_gs();
3499 			break;
3500 		case 0x41:
3501 			reverb->insertion_effect_gs.control_depth1 = val;
3502 			reverb->recompute_insertion_effect_gs();
3503 			break;
3504 		case 0x42:
3505 			reverb->insertion_effect_gs.control_source2 = val;
3506 			reverb->recompute_insertion_effect_gs();
3507 			break;
3508 		case 0x43:
3509 			reverb->insertion_effect_gs.control_depth2 = val;
3510 			reverb->recompute_insertion_effect_gs();
3511 			break;
3512 		case 0x44:
3513 			reverb->insertion_effect_gs.send_eq_switch = val;
3514 			reverb->recompute_insertion_effect_gs();
3515 			break;
3516 		case 0x45:	/* Rx. Channel */
3517 			reset_controllers(ch);
3518 			all_notes_off(ch);
3519 			if (val == 0x80)
3520 				remove_channel_layer(ch);
3521 			else
3522 				add_channel_layer(ch, val);
3523 			break;
3524 		case 0x46:	/* Channel Msg Rx Port */
3525 			reset_controllers(ch);
3526 			all_notes_off(ch);
3527 			channel[ch].port_select = val;
3528 			break;
3529 		case 0x47:	/* Play Note Number */
3530 			if (channel[ch].drums[note] == NULL)
3531 				play_midi_setup_drums(ch, note);
3532 			channel[ch].drums[note]->play_note = val;
3533 			printMessage(CMSG_INFO, VERB_NOISY,
3534 				"Drum Instrument Play Note (CH:%d NOTE:%d VAL:%d)",
3535 				ch, note, channel[ch].drums[note]->play_note);
3536 			channel[ch].pitchfactor = 0;
3537 			break;
3538 		default:
3539 			break;
3540 		}
3541 		return;
3542 	} else if(ev == ME_SYSEX_XG_LSB) {	/* XG system exclusive message */
3543 		msb = channel[ch].sysex_xg_msb_addr;
3544 		note = channel[ch].sysex_xg_msb_val;
3545 		if (note == 3 && msb == 0) {	/* Effect 2 */
3546 		note = 0;	/* force insertion effect num 0 ?? */
3547 		if (note >= XG_INSERTION_EFFECT_NUM || note < 0) {return;}
3548 		switch(b)
3549 		{
3550 		case 0x00:	/* Insertion Effect Type MSB */
3551 			if (reverb->insertion_effect_xg[note].type_msb != val) {
3552 				//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Type MSB (%d %02X)", note, val);
3553 				reverb->insertion_effect_xg[note].type_msb = val;
3554 				reverb->realloc_effect_xg(&reverb->insertion_effect_xg[note]);
3555 			}
3556 			break;
3557 		case 0x01:	/* Insertion Effect Type LSB */
3558 			if (reverb->insertion_effect_xg[note].type_lsb != val) {
3559 				//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Type LSB (%d %02X)", note, val);
3560 				reverb->insertion_effect_xg[note].type_lsb = val;
3561 				reverb->realloc_effect_xg(&reverb->insertion_effect_xg[note]);
3562 			}
3563 			break;
3564 		case 0x02:	/* Insertion Effect Parameter 1 - 10 */
3565 		case 0x03:
3566 		case 0x04:
3567 		case 0x05:
3568 		case 0x06:
3569 		case 0x07:
3570 		case 0x08:
3571 		case 0x09:
3572 		case 0x0A:
3573 		case 0x0B:
3574 			if (reverb->insertion_effect_xg[note].use_msb) {break;}
3575 			temp = b - 0x02;
3576 			//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Parameter %d (%d %d)", temp + 1, note, val);
3577 			if (reverb->insertion_effect_xg[note].param_lsb[temp] != val) {
3578 				reverb->insertion_effect_xg[note].param_lsb[temp] = val;
3579 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3580 			}
3581 			break;
3582 		case 0x0C:	/* Insertion Effect Part */
3583 			//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Part (%d %d)", note, val);
3584 			if (reverb->insertion_effect_xg[note].part != val) {
3585 				reverb->insertion_effect_xg[note].part = val;
3586 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3587 			}
3588 			break;
3589 		case 0x0D:	/* MW Insertion Control Depth */
3590 			//printMessage(CMSG_INFO,VERB_NOISY,"MW Insertion Control Depth (%d %d)", note, val);
3591 			if (reverb->insertion_effect_xg[note].mw_depth != val) {
3592 				reverb->insertion_effect_xg[note].mw_depth = val;
3593 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3594 			}
3595 			break;
3596 		case 0x0E:	/* BEND Insertion Control Depth */
3597 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND Insertion Control Depth (%d %d)", note, val);
3598 			if (reverb->insertion_effect_xg[note].bend_depth != val) {
3599 				reverb->insertion_effect_xg[note].bend_depth = val;
3600 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3601 			}
3602 			break;
3603 		case 0x0F:	/* CAT Insertion Control Depth */
3604 			//printMessage(CMSG_INFO,VERB_NOISY,"CAT Insertion Control Depth (%d %d)", note, val);
3605 			if (reverb->insertion_effect_xg[note].cat_depth != val) {
3606 				reverb->insertion_effect_xg[note].cat_depth = val;
3607 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3608 			}
3609 			break;
3610 		case 0x10:	/* AC1 Insertion Control Depth */
3611 			//printMessage(CMSG_INFO,VERB_NOISY,"AC1 Insertion Control Depth (%d %d)", note, val);
3612 			if (reverb->insertion_effect_xg[note].ac1_depth != val) {
3613 				reverb->insertion_effect_xg[note].ac1_depth = val;
3614 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3615 			}
3616 			break;
3617 		case 0x11:	/* AC2 Insertion Control Depth */
3618 			//printMessage(CMSG_INFO,VERB_NOISY,"AC2 Insertion Control Depth (%d %d)", note, val);
3619 			if (reverb->insertion_effect_xg[note].ac2_depth != val) {
3620 				reverb->insertion_effect_xg[note].ac2_depth = val;
3621 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3622 			}
3623 			break;
3624 		case 0x12:	/* CBC1 Insertion Control Depth */
3625 			//printMessage(CMSG_INFO,VERB_NOISY,"CBC1 Insertion Control Depth (%d %d)", note, val);
3626 			if (reverb->insertion_effect_xg[note].cbc1_depth != val) {
3627 				reverb->insertion_effect_xg[note].cbc1_depth = val;
3628 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3629 			}
3630 			break;
3631 		case 0x13:	/* CBC2 Insertion Control Depth */
3632 			//printMessage(CMSG_INFO,VERB_NOISY,"CBC2 Insertion Control Depth (%d %d)", note, val);
3633 			if (reverb->insertion_effect_xg[note].cbc2_depth != val) {
3634 				reverb->insertion_effect_xg[note].cbc2_depth = val;
3635 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3636 			}
3637 			break;
3638 		case 0x20:	/* Insertion Effect Parameter 11 - 16 */
3639 		case 0x21:
3640 		case 0x22:
3641 		case 0x23:
3642 		case 0x24:
3643 		case 0x25:
3644 			temp = b - 0x20 + 10;
3645 			//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Parameter %d (%d %d)", temp + 1, note, val);
3646 			if (reverb->insertion_effect_xg[note].param_lsb[temp] != val) {
3647 				reverb->insertion_effect_xg[note].param_lsb[temp] = val;
3648 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3649 			}
3650 			break;
3651 		case 0x30:	/* Insertion Effect Parameter 1 - 10 MSB */
3652 		case 0x32:
3653 		case 0x34:
3654 		case 0x36:
3655 		case 0x38:
3656 		case 0x3A:
3657 		case 0x3C:
3658 		case 0x3E:
3659 		case 0x40:
3660 		case 0x42:
3661 			if (!reverb->insertion_effect_xg[note].use_msb) {break;}
3662 			temp = (b - 0x30) / 2;
3663 			//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Parameter %d MSB (%d %d)", temp + 1, note, val);
3664 			if (reverb->insertion_effect_xg[note].param_msb[temp] != val) {
3665 				reverb->insertion_effect_xg[note].param_msb[temp] = val;
3666 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3667 			}
3668 			break;
3669 		case 0x31:	/* Insertion Effect Parameter 1 - 10 LSB */
3670 		case 0x33:
3671 		case 0x35:
3672 		case 0x37:
3673 		case 0x39:
3674 		case 0x3B:
3675 		case 0x3D:
3676 		case 0x3F:
3677 		case 0x41:
3678 		case 0x43:
3679 			if (!reverb->insertion_effect_xg[note].use_msb) {break;}
3680 			temp = (b - 0x31) / 2;
3681 			//printMessage(CMSG_INFO,VERB_NOISY,"Insertion Effect Parameter %d LSB (%d %d)", temp + 1, note, val);
3682 			if (reverb->insertion_effect_xg[note].param_lsb[temp] != val) {
3683 				reverb->insertion_effect_xg[note].param_lsb[temp] = val;
3684 				reverb->recompute_effect_xg(&reverb->insertion_effect_xg[note]);
3685 			}
3686 			break;
3687 		default:
3688 			break;
3689 		}
3690 		} else if (note == 2 && msb == 1) {	/* Effect 1 */
3691 		note = 0;	/* force variation effect num 0 ?? */
3692 		switch(b)
3693 		{
3694 		case 0x00:	/* Reverb Type MSB */
3695 			if (reverb->reverb_status_xg.type_msb != val) {
3696 				//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Type MSB (%02X)", val);
3697 				reverb->reverb_status_xg.type_msb = val;
3698 				reverb->realloc_effect_xg(&reverb->reverb_status_xg);
3699 			}
3700 			break;
3701 		case 0x01:	/* Reverb Type LSB */
3702 			if (reverb->reverb_status_xg.type_lsb != val) {
3703 				//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Type LSB (%02X)", val);
3704 				reverb->reverb_status_xg.type_lsb = val;
3705 				reverb->realloc_effect_xg(&reverb->reverb_status_xg);
3706 			}
3707 			break;
3708 		case 0x02:	/* Reverb Parameter 1 - 10 */
3709 		case 0x03:
3710 		case 0x04:
3711 		case 0x05:
3712 		case 0x06:
3713 		case 0x07:
3714 		case 0x08:
3715 		case 0x09:
3716 		case 0x0A:
3717 		case 0x0B:
3718 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Parameter %d (%d)", b - 0x02 + 1, val);
3719 			if (reverb->reverb_status_xg.param_lsb[b - 0x02] != val) {
3720 				reverb->reverb_status_xg.param_lsb[b - 0x02] = val;
3721 				reverb->recompute_effect_xg(&reverb->reverb_status_xg);
3722 			}
3723 			break;
3724 		case 0x0C:	/* Reverb Return */
3725 #if 0	/* XG specific reverb is not currently implemented */
3726 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Return (%d)", val);
3727 			if (reverb->reverb_status_xg.ret != val) {
3728 				reverb->reverb_status_xg.ret = val;
3729 				reverb->recompute_effect_xg(&reverb->reverb_status_xg);
3730 			}
3731 #else	/* use GS reverb instead */
3732 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Return (%d)", val);
3733 			if (reverb->reverb_status_gs.level != val) {
3734 				reverb->reverb_status_gs.level = val;
3735 				reverb->recompute_reverb_status_gs();
3736 				reverb->init_reverb();
3737 			}
3738 #endif
3739 			break;
3740 		case 0x0D:	/* Reverb Pan */
3741 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Pan (%d)", val);
3742 			if (reverb->reverb_status_xg.pan != val) {
3743 				reverb->reverb_status_xg.pan = val;
3744 				reverb->recompute_effect_xg(&reverb->reverb_status_xg);
3745 			}
3746 			break;
3747 		case 0x10:	/* Reverb Parameter 11 - 16 */
3748 		case 0x11:
3749 		case 0x12:
3750 		case 0x13:
3751 		case 0x14:
3752 		case 0x15:
3753 			temp = b - 0x10 + 10;
3754 			//printMessage(CMSG_INFO,VERB_NOISY,"Reverb Parameter %d (%d)", temp + 1, val);
3755 			if (reverb->reverb_status_xg.param_lsb[temp] != val) {
3756 				reverb->reverb_status_xg.param_lsb[temp] = val;
3757 				reverb->recompute_effect_xg(&reverb->reverb_status_xg);
3758 			}
3759 			break;
3760 		case 0x20:	/* Chorus Type MSB */
3761 			if (reverb->chorus_status_xg.type_msb != val) {
3762 				//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Type MSB (%02X)", val);
3763 				reverb->chorus_status_xg.type_msb = val;
3764 				reverb->realloc_effect_xg(&reverb->chorus_status_xg);
3765 			}
3766 			break;
3767 		case 0x21:	/* Chorus Type LSB */
3768 			if (reverb->chorus_status_xg.type_lsb != val) {
3769 				//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Type LSB (%02X)", val);
3770 				reverb->chorus_status_xg.type_lsb = val;
3771 				reverb->realloc_effect_xg(&reverb->chorus_status_xg);
3772 			}
3773 			break;
3774 		case 0x22:	/* Chorus Parameter 1 - 10 */
3775 		case 0x23:
3776 		case 0x24:
3777 		case 0x25:
3778 		case 0x26:
3779 		case 0x27:
3780 		case 0x28:
3781 		case 0x29:
3782 		case 0x2A:
3783 		case 0x2B:
3784 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Parameter %d (%d)", b - 0x22 + 1, val);
3785 			if (reverb->chorus_status_xg.param_lsb[b - 0x22] != val) {
3786 				reverb->chorus_status_xg.param_lsb[b - 0x22] = val;
3787 				reverb->recompute_effect_xg(&reverb->chorus_status_xg);
3788 			}
3789 			break;
3790 		case 0x2C:	/* Chorus Return */
3791 #if 0	/* XG specific chorus is not currently implemented */
3792 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Return (%d)", val);
3793 			if (reverb->chorus_status_xg.ret != val) {
3794 				reverb->chorus_status_xg.ret = val;
3795 				reverb->recompute_effect_xg(&reverb->chorus_status_xg);
3796 			}
3797 #else	/* use GS chorus instead */
3798 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Return (%d)", val);
3799 			if (reverb->chorus_status_gs.level != val) {
3800 				reverb->chorus_status_gs.level = val;
3801 				reverb->recompute_chorus_status_gs();
3802 				reverb->init_ch_chorus();
3803 			}
3804 #endif
3805 			break;
3806 		case 0x2D:	/* Chorus Pan */
3807 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Pan (%d)", val);
3808 			if (reverb->chorus_status_xg.pan != val) {
3809 				reverb->chorus_status_xg.pan = val;
3810 				reverb->recompute_effect_xg(&reverb->chorus_status_xg);
3811 			}
3812 			break;
3813 		case 0x2E:	/* Send Chorus To Reverb */
3814 			//printMessage(CMSG_INFO,VERB_NOISY,"Send Chorus To Reverb (%d)", val);
3815 			if (reverb->chorus_status_xg.send_reverb != val) {
3816 				reverb->chorus_status_xg.send_reverb = val;
3817 				reverb->recompute_effect_xg(&reverb->chorus_status_xg);
3818 			}
3819 			break;
3820 		case 0x30:	/* Chorus Parameter 11 - 16 */
3821 		case 0x31:
3822 		case 0x32:
3823 		case 0x33:
3824 		case 0x34:
3825 		case 0x35:
3826 			temp = b - 0x30 + 10;
3827 			//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Parameter %d (%d)", temp + 1, val);
3828 			if (reverb->chorus_status_xg.param_lsb[temp] != val) {
3829 				reverb->chorus_status_xg.param_lsb[temp] = val;
3830 				reverb->recompute_effect_xg(&reverb->chorus_status_xg);
3831 			}
3832 			break;
3833 		case 0x40:	/* Variation Type MSB */
3834 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3835 			if (reverb->variation_effect_xg[note].type_msb != val) {
3836 				//printMessage(CMSG_INFO,VERB_NOISY,"Variation Type MSB (%02X)", val);
3837 				reverb->variation_effect_xg[note].type_msb = val;
3838 				reverb->realloc_effect_xg(&reverb->variation_effect_xg[note]);
3839 			}
3840 			break;
3841 		case 0x41:	/* Variation Type LSB */
3842 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3843 			if (reverb->variation_effect_xg[note].type_lsb != val) {
3844 				//printMessage(CMSG_INFO,VERB_NOISY,"Variation Type LSB (%02X)", val);
3845 				reverb->variation_effect_xg[note].type_lsb = val;
3846 				reverb->realloc_effect_xg(&reverb->variation_effect_xg[note]);
3847 			}
3848 			break;
3849 		case 0x42:	/* Variation Parameter 1 - 10 MSB */
3850 		case 0x44:
3851 		case 0x46:
3852 		case 0x48:
3853 		case 0x4A:
3854 		case 0x4C:
3855 		case 0x4E:
3856 		case 0x50:
3857 		case 0x52:
3858 		case 0x54:
3859 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3860 			temp = (b - 0x42) / 2;
3861 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Parameter %d MSB (%d)", temp, val);
3862 			if (reverb->variation_effect_xg[note].param_msb[temp] != val) {
3863 				reverb->variation_effect_xg[note].param_msb[temp] = val;
3864 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3865 			}
3866 			break;
3867 		case 0x43:	/* Variation Parameter 1 - 10 LSB */
3868 		case 0x45:
3869 		case 0x47:
3870 		case 0x49:
3871 		case 0x4B:
3872 		case 0x4D:
3873 		case 0x4F:
3874 		case 0x51:
3875 		case 0x53:
3876 		case 0x55:
3877 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3878 			temp = (b - 0x43) / 2;
3879 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Parameter %d LSB (%d)", temp, val);
3880 			if (reverb->variation_effect_xg[note].param_lsb[temp] != val) {
3881 				reverb->variation_effect_xg[note].param_lsb[temp] = val;
3882 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3883 			}
3884 			break;
3885 		case 0x56:	/* Variation Return */
3886 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3887 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Return (%d)", val);
3888 			if (reverb->variation_effect_xg[note].ret != val) {
3889 				reverb->variation_effect_xg[note].ret = val;
3890 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3891 			}
3892 			break;
3893 		case 0x57:	/* Variation Pan */
3894 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3895 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Pan (%d)", val);
3896 			if (reverb->variation_effect_xg[note].pan != val) {
3897 				reverb->variation_effect_xg[note].pan = val;
3898 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3899 			}
3900 			break;
3901 		case 0x58:	/* Send Variation To Reverb */
3902 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3903 			//printMessage(CMSG_INFO,VERB_NOISY,"Send Variation To Reverb (%d)", val);
3904 			if (reverb->variation_effect_xg[note].send_reverb != val) {
3905 				reverb->variation_effect_xg[note].send_reverb = val;
3906 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3907 			}
3908 			break;
3909 		case 0x59:	/* Send Variation To Chorus */
3910 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3911 			//printMessage(CMSG_INFO,VERB_NOISY,"Send Variation To Chorus (%d)", val);
3912 			if (reverb->variation_effect_xg[note].send_chorus != val) {
3913 				reverb->variation_effect_xg[note].send_chorus = val;
3914 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3915 			}
3916 			break;
3917 		case 0x5A:	/* Variation Connection */
3918 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3919 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Connection (%d)", val);
3920 			if (reverb->variation_effect_xg[note].connection != val) {
3921 				reverb->variation_effect_xg[note].connection = val;
3922 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3923 			}
3924 			break;
3925 		case 0x5B:	/* Variation Part */
3926 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3927 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Part (%d)", val);
3928 			if (reverb->variation_effect_xg[note].part != val) {
3929 				reverb->variation_effect_xg[note].part = val;
3930 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3931 			}
3932 			break;
3933 		case 0x5C:	/* MW Variation Control Depth */
3934 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3935 			//printMessage(CMSG_INFO,VERB_NOISY,"MW Variation Control Depth (%d)", val);
3936 			if (reverb->variation_effect_xg[note].mw_depth != val) {
3937 				reverb->variation_effect_xg[note].mw_depth = val;
3938 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3939 			}
3940 			break;
3941 		case 0x5D:	/* BEND Variation Control Depth */
3942 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3943 			//printMessage(CMSG_INFO,VERB_NOISY,"BEND Variation Control Depth (%d)", val);
3944 			if (reverb->variation_effect_xg[note].bend_depth != val) {
3945 				reverb->variation_effect_xg[note].bend_depth = val;
3946 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3947 			}
3948 			break;
3949 		case 0x5E:	/* CAT Variation Control Depth */
3950 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3951 			//printMessage(CMSG_INFO,VERB_NOISY,"CAT Variation Control Depth (%d)", val);
3952 			if (reverb->variation_effect_xg[note].cat_depth != val) {
3953 				reverb->variation_effect_xg[note].cat_depth = val;
3954 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3955 			}
3956 			break;
3957 		case 0x5F:	/* AC1 Variation Control Depth */
3958 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3959 			//printMessage(CMSG_INFO,VERB_NOISY,"AC1 Variation Control Depth (%d)", val);
3960 			if (reverb->variation_effect_xg[note].ac1_depth != val) {
3961 				reverb->variation_effect_xg[note].ac1_depth = val;
3962 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3963 			}
3964 			break;
3965 		case 0x60:	/* AC2 Variation Control Depth */
3966 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3967 			//printMessage(CMSG_INFO,VERB_NOISY,"AC2 Variation Control Depth (%d)", val);
3968 			if (reverb->variation_effect_xg[note].ac2_depth != val) {
3969 				reverb->variation_effect_xg[note].ac2_depth = val;
3970 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3971 			}
3972 			break;
3973 		case 0x61:	/* CBC1 Variation Control Depth */
3974 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3975 			//printMessage(CMSG_INFO,VERB_NOISY,"CBC1 Variation Control Depth (%d)", val);
3976 			if (reverb->variation_effect_xg[note].cbc1_depth != val) {
3977 				reverb->variation_effect_xg[note].cbc1_depth = val;
3978 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3979 			}
3980 			break;
3981 		case 0x62:	/* CBC2 Variation Control Depth */
3982 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3983 			//printMessage(CMSG_INFO,VERB_NOISY,"CBC2 Variation Control Depth (%d)", val);
3984 			if (reverb->variation_effect_xg[note].cbc2_depth != val) {
3985 				reverb->variation_effect_xg[note].cbc2_depth = val;
3986 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
3987 			}
3988 			break;
3989 		case 0x70:	/* Variation Parameter 11 - 16 */
3990 		case 0x71:
3991 		case 0x72:
3992 		case 0x73:
3993 		case 0x74:
3994 		case 0x75:
3995 			temp = b - 0x70 + 10;
3996 			if (note >= XG_VARIATION_EFFECT_NUM || note < 0) {break;}
3997 			//printMessage(CMSG_INFO,VERB_NOISY,"Variation Parameter %d (%d)", temp + 1, val);
3998 			if (reverb->variation_effect_xg[note].param_lsb[temp] != val) {
3999 				reverb->variation_effect_xg[note].param_lsb[temp] = val;
4000 				reverb->recompute_effect_xg(&reverb->variation_effect_xg[note]);
4001 			}
4002 			break;
4003 		default:
4004 			break;
4005 		}
4006 		} else if (note == 2 && msb == 40) {	/* Multi EQ */
4007 		switch(b)
4008 		{
4009 		case 0x00:	/* EQ type */
4010 			if(opt_eq_control) {
4011 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ type (%d)", val);
4012 				reverb->multi_eq_xg.type = val;
4013 				reverb->set_multi_eq_type_xg(val);
4014 				reverb->recompute_multi_eq_xg();
4015 			}
4016 			break;
4017 		case 0x01:	/* EQ gain1 */
4018 			if(opt_eq_control) {
4019 				if(val > 0x4C) {val = 0x4C;}
4020 				else if(val < 0x34) {val = 0x34;}
4021 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ gain1 (%d dB)", val - 0x40);
4022 				reverb->multi_eq_xg.gain1 = val;
4023 				reverb->recompute_multi_eq_xg();
4024 			}
4025 			break;
4026 		case 0x02:	/* EQ frequency1 */
4027 			if(opt_eq_control) {
4028 				if(val > 60) {val = 60;}
4029 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ frequency1 (%d Hz)", (int32_t)eq_freq_table_xg[val]);
4030 				reverb->multi_eq_xg.freq1 = val;
4031 				reverb->recompute_multi_eq_xg();
4032 			}
4033 			break;
4034 		case 0x03:	/* EQ Q1 */
4035 			if(opt_eq_control) {
4036 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ Q1 (%f)", (double)val / 10.0);
4037 				reverb->multi_eq_xg.q1 = val;
4038 				reverb->recompute_multi_eq_xg();
4039 			}
4040 			break;
4041 		case 0x04:	/* EQ shape1 */
4042 			if(opt_eq_control) {
4043 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ shape1 (%d)", val);
4044 				reverb->multi_eq_xg.shape1 = val;
4045 				reverb->recompute_multi_eq_xg();
4046 			}
4047 			break;
4048 		case 0x05:	/* EQ gain2 */
4049 			if(opt_eq_control) {
4050 				if(val > 0x4C) {val = 0x4C;}
4051 				else if(val < 0x34) {val = 0x34;}
4052 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ gain2 (%d dB)", val - 0x40);
4053 				reverb->multi_eq_xg.gain2 = val;
4054 				reverb->recompute_multi_eq_xg();
4055 			}
4056 			break;
4057 		case 0x06:	/* EQ frequency2 */
4058 			if(opt_eq_control) {
4059 				if(val > 60) {val = 60;}
4060 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ frequency2 (%d Hz)", (int32_t)eq_freq_table_xg[val]);
4061 				reverb->multi_eq_xg.freq2 = val;
4062 				reverb->recompute_multi_eq_xg();
4063 			}
4064 			break;
4065 		case 0x07:	/* EQ Q2 */
4066 			if(opt_eq_control) {
4067 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ Q2 (%f)", (double)val / 10.0);
4068 				reverb->multi_eq_xg.q2 = val;
4069 				reverb->recompute_multi_eq_xg();
4070 			}
4071 			break;
4072 		case 0x09:	/* EQ gain3 */
4073 			if(opt_eq_control) {
4074 				if(val > 0x4C) {val = 0x4C;}
4075 				else if(val < 0x34) {val = 0x34;}
4076 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ gain3 (%d dB)", val - 0x40);
4077 				reverb->multi_eq_xg.gain3 = val;
4078 				reverb->recompute_multi_eq_xg();
4079 			}
4080 			break;
4081 		case 0x0A:	/* EQ frequency3 */
4082 			if(opt_eq_control) {
4083 				if(val > 60) {val = 60;}
4084 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ frequency3 (%d Hz)", (int32_t)eq_freq_table_xg[val]);
4085 				reverb->multi_eq_xg.freq3 = val;
4086 				reverb->recompute_multi_eq_xg();
4087 			}
4088 			break;
4089 		case 0x0B:	/* EQ Q3 */
4090 			if(opt_eq_control) {
4091 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ Q3 (%f)", (double)val / 10.0);
4092 				reverb->multi_eq_xg.q3 = val;
4093 				reverb->recompute_multi_eq_xg();
4094 			}
4095 			break;
4096 		case 0x0D:	/* EQ gain4 */
4097 			if(opt_eq_control) {
4098 				if(val > 0x4C) {val = 0x4C;}
4099 				else if(val < 0x34) {val = 0x34;}
4100 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ gain4 (%d dB)", val - 0x40);
4101 				reverb->multi_eq_xg.gain4 = val;
4102 				reverb->recompute_multi_eq_xg();
4103 			}
4104 			break;
4105 		case 0x0E:	/* EQ frequency4 */
4106 			if(opt_eq_control) {
4107 				if(val > 60) {val = 60;}
4108 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ frequency4 (%d Hz)", (int32_t)eq_freq_table_xg[val]);
4109 				reverb->multi_eq_xg.freq4 = val;
4110 				reverb->recompute_multi_eq_xg();
4111 			}
4112 			break;
4113 		case 0x0F:	/* EQ Q4 */
4114 			if(opt_eq_control) {
4115 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ Q4 (%f)", (double)val / 10.0);
4116 				reverb->multi_eq_xg.q4 = val;
4117 				reverb->recompute_multi_eq_xg();
4118 			}
4119 			break;
4120 		case 0x11:	/* EQ gain5 */
4121 			if(opt_eq_control) {
4122 				if(val > 0x4C) {val = 0x4C;}
4123 				else if(val < 0x34) {val = 0x34;}
4124 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ gain5 (%d dB)", val - 0x40);
4125 				reverb->multi_eq_xg.gain5 = val;
4126 				reverb->recompute_multi_eq_xg();
4127 			}
4128 			break;
4129 		case 0x12:	/* EQ frequency5 */
4130 			if(opt_eq_control) {
4131 				if(val > 60) {val = 60;}
4132 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ frequency5 (%d Hz)", (int32_t)eq_freq_table_xg[val]);
4133 				reverb->multi_eq_xg.freq5 = val;
4134 				reverb->recompute_multi_eq_xg();
4135 			}
4136 			break;
4137 		case 0x13:	/* EQ Q5 */
4138 			if(opt_eq_control) {
4139 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ Q5 (%f)", (double)val / 10.0);
4140 				reverb->multi_eq_xg.q5 = val;
4141 				reverb->recompute_multi_eq_xg();
4142 			}
4143 			break;
4144 		case 0x14:	/* EQ shape5 */
4145 			if(opt_eq_control) {
4146 				//printMessage(CMSG_INFO,VERB_NOISY,"EQ shape5 (%d)", val);
4147 				reverb->multi_eq_xg.shape5 = val;
4148 				reverb->recompute_multi_eq_xg();
4149 			}
4150 			break;
4151 		}
4152 		} else if (note == 8 && msb == 0) {	/* Multi Part */
4153 		switch(b)
4154 		{
4155 		case 0x99:	/* Rcv CHANNEL, remapped from 0x04 */
4156 			reset_controllers(ch);
4157 			all_notes_off(ch);
4158 			if (val == 0x7f)
4159 				remove_channel_layer(ch);
4160 			else {
4161 				if((ch < REDUCE_CHANNELS) != (val < REDUCE_CHANNELS)) {
4162 					channel[ch].port_select = ch < REDUCE_CHANNELS ? 1 : 0;
4163 				}
4164 				if((ch % REDUCE_CHANNELS) != (val % REDUCE_CHANNELS)) {
4165 					add_channel_layer(ch, val);
4166 				}
4167 			}
4168 			break;
4169 		case 0x06:	/* Same Note Number Key On Assign */
4170 			if(val == 0) {
4171 				channel[ch].assign_mode = 0;
4172 				//printMessage(CMSG_INFO,VERB_NOISY,"Same Note Number Key On Assign: Single (CH:%d)",ch);
4173 			} else if(val == 1) {
4174 				channel[ch].assign_mode = 2;
4175 				//printMessage(CMSG_INFO,VERB_NOISY,"Same Note Number Key On Assign: Multi (CH:%d)",ch);
4176 			} else if(val == 2) {
4177 				//printMessage(CMSG_INFO,VERB_NOISY,"Same Note Number Key On Assign: Inst is not supported. (CH:%d)",ch);
4178 			}
4179 			break;
4180 		case 0x11:	/* Dry Level */
4181 			channel[ch].dry_level = val;
4182 			//printMessage(CMSG_INFO,VERB_NOISY,"Dry Level (CH:%d VAL:%d)", ch, val);
4183 			break;
4184 		}
4185 		} else if ((note & 0xF0) == 0x30) {	/* Drum Setup */
4186 		note = msb;
4187 		switch(b)
4188 		{
4189 		case 0x0E:	/* EG Decay1 */
4190 			if (channel[ch].drums[note] == NULL)
4191 				play_midi_setup_drums(ch, note);
4192 			//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument EG Decay1 (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4193 			channel[ch].drums[note]->drum_envelope_rate[EG_DECAY1] = val;
4194 			break;
4195 		case 0x0F:	/* EG Decay2 */
4196 			if (channel[ch].drums[note] == NULL)
4197 				play_midi_setup_drums(ch, note);
4198 			//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument EG Decay2 (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4199 			channel[ch].drums[note]->drum_envelope_rate[EG_DECAY2] = val;
4200 			break;
4201 		default:
4202 			break;
4203 		}
4204 		}
4205 		return;
4206 	}
4207 }
4208 
4209 /*! convert GS NRPN to vibrato rate ratio. */
4210 /* from 0 to 3.0. */
gs_cnv_vib_rate(int rate)4211 double Player::gs_cnv_vib_rate(int rate)
4212 {
4213 	double ratio;
4214 
4215 	if(rate == 0) {
4216 		ratio = 1.6 / 100.0;
4217 	} else if(rate == 64) {
4218 		ratio = 1.0;
4219 	} else if(rate <= 100) {
4220 		ratio = (double)rate * 1.6 / 100.0;
4221 	} else {
4222 		ratio = (double)(rate - 101) * 1.33 / 26.0 + 1.67;
4223 	}
4224 	return (1.0 / ratio);
4225 }
4226 
4227 /*! convert GS NRPN to vibrato depth. */
4228 /* from -9.6 cents to +9.45 cents. */
gs_cnv_vib_depth(int depth)4229 int32_t Player::gs_cnv_vib_depth(int depth)
4230 {
4231 	double cent;
4232 	cent = (double)(depth - 64) * 0.15;
4233 
4234 	return (int32_t)(cent * 256.0 / 400.0);
4235 }
4236 
4237 /*! convert GS NRPN to vibrato delay. */
4238 /* from 0 ms to 5074 ms. */
gs_cnv_vib_delay(int delay)4239 int32_t Player::gs_cnv_vib_delay(int delay)
4240 {
4241 	double ms;
4242 	ms = 0.2092 * exp(0.0795 * (double)delay);
4243 	if(delay == 0) {ms = 0;}
4244 	return (int32_t)((double)playback_rate * ms * 0.001);
4245 }
4246 
last_rpn_addr(int ch)4247 int Player::last_rpn_addr(int ch)
4248 {
4249 	int lsb, msb, addr, i;
4250 	const struct rpn_tag_map_t *addrmap;
4251 	struct rpn_tag_map_t {
4252 		int addr, mask, tag;
4253 	};
4254 	static const struct rpn_tag_map_t nrpn_addr_map[] = {
4255 		{0x0108, 0xffff, NRPN_ADDR_0108},
4256 		{0x0109, 0xffff, NRPN_ADDR_0109},
4257 		{0x010a, 0xffff, NRPN_ADDR_010A},
4258 		{0x0120, 0xffff, NRPN_ADDR_0120},
4259 		{0x0121, 0xffff, NRPN_ADDR_0121},
4260 		{0x0130, 0xffff, NRPN_ADDR_0130},
4261 		{0x0131, 0xffff, NRPN_ADDR_0131},
4262 		{0x0134, 0xffff, NRPN_ADDR_0134},
4263 		{0x0135, 0xffff, NRPN_ADDR_0135},
4264 		{0x0163, 0xffff, NRPN_ADDR_0163},
4265 		{0x0164, 0xffff, NRPN_ADDR_0164},
4266 		{0x0166, 0xffff, NRPN_ADDR_0166},
4267 		{0x1400, 0xff00, NRPN_ADDR_1400},
4268 		{0x1500, 0xff00, NRPN_ADDR_1500},
4269 		{0x1600, 0xff00, NRPN_ADDR_1600},
4270 		{0x1700, 0xff00, NRPN_ADDR_1700},
4271 		{0x1800, 0xff00, NRPN_ADDR_1800},
4272 		{0x1900, 0xff00, NRPN_ADDR_1900},
4273 		{0x1a00, 0xff00, NRPN_ADDR_1A00},
4274 		{0x1c00, 0xff00, NRPN_ADDR_1C00},
4275 		{0x1d00, 0xff00, NRPN_ADDR_1D00},
4276 		{0x1e00, 0xff00, NRPN_ADDR_1E00},
4277 		{0x1f00, 0xff00, NRPN_ADDR_1F00},
4278 		{0x3000, 0xff00, NRPN_ADDR_3000},
4279 		{0x3100, 0xff00, NRPN_ADDR_3100},
4280 		{0x3400, 0xff00, NRPN_ADDR_3400},
4281 		{0x3500, 0xff00, NRPN_ADDR_3500},
4282 		{-1, -1, 0}
4283 	};
4284 	static const struct rpn_tag_map_t rpn_addr_map[] = {
4285 		{0x0000, 0xffff, RPN_ADDR_0000},
4286 		{0x0001, 0xffff, RPN_ADDR_0001},
4287 		{0x0002, 0xffff, RPN_ADDR_0002},
4288 		{0x0003, 0xffff, RPN_ADDR_0003},
4289 		{0x0004, 0xffff, RPN_ADDR_0004},
4290 		{0x0005, 0xffff, RPN_ADDR_0005},
4291 		{0x7f7f, 0xffff, RPN_ADDR_7F7F},
4292 		{0xffff, 0xffff, RPN_ADDR_FFFF},
4293 		{-1, -1}
4294 	};
4295 
4296 	if (channel[ch].nrpn == -1)
4297 		return -1;
4298 	lsb = channel[ch].lastlrpn;
4299 	msb = channel[ch].lastmrpn;
4300 	if (lsb == 0xff || msb == 0xff)
4301 		return -1;
4302 	addr = (msb << 8 | lsb);
4303 	if (channel[ch].nrpn)
4304 		addrmap = nrpn_addr_map;
4305 	else
4306 		addrmap = rpn_addr_map;
4307 	for (i = 0; addrmap[i].addr != -1; i++)
4308 		if (addrmap[i].addr == (addr & addrmap[i].mask))
4309 			return addrmap[i].tag;
4310 	return -1;
4311 }
4312 
update_rpn_map(int ch,int addr,int update_now)4313 void Player::update_rpn_map(int ch, int addr, int update_now)
4314 {
4315 	int val, drumflag, i, note;
4316 
4317 	val = channel[ch].rpnmap[addr];
4318 	drumflag = 0;
4319 	switch (addr) {
4320 	case NRPN_ADDR_0108:	/* Vibrato Rate */
4321 		if (op_nrpn_vibrato) {
4322 			//printMessage(CMSG_INFO, VERB_NOISY,	"Vibrato Rate (CH:%d VAL:%d)", ch, val - 64);
4323 			channel[ch].vibrato_ratio = gs_cnv_vib_rate(val);
4324 		}
4325 		if (update_now)
4326 			adjust_pitch(ch);
4327 		break;
4328 	case NRPN_ADDR_0109:	/* Vibrato Depth */
4329 		if (op_nrpn_vibrato) {
4330 			//printMessage(CMSG_INFO, VERB_NOISY,	"Vibrato Depth (CH:%d VAL:%d)", ch, val - 64);
4331 			channel[ch].vibrato_depth = gs_cnv_vib_depth(val);
4332 		}
4333 		if (update_now)
4334 			adjust_pitch(ch);
4335 		break;
4336 	case NRPN_ADDR_010A:	/* Vibrato Delay */
4337 		if (op_nrpn_vibrato) {
4338 			//printMessage(CMSG_INFO,VERB_NOISY,"Vibrato Delay (CH:%d VAL:%d)", ch, val);
4339 			channel[ch].vibrato_delay = gs_cnv_vib_delay(val);
4340 		}
4341 		if (update_now)
4342 			adjust_pitch(ch);
4343 		break;
4344 	case NRPN_ADDR_0120:	/* Filter Cutoff Frequency */
4345 		if (timidity_lpf_def) {
4346 			//printMessage(CMSG_INFO, VERB_NOISY,	"Filter Cutoff (CH:%d VAL:%d)", ch, val - 64);
4347 			channel[ch].param_cutoff_freq = val - 64;
4348 		}
4349 		break;
4350 	case NRPN_ADDR_0121:	/* Filter Resonance */
4351 		if (timidity_lpf_def) {
4352 			//printMessage(CMSG_INFO,VERB_NOISY,"Filter Resonance (CH:%d VAL:%d)", ch, val - 64);
4353 			channel[ch].param_resonance = val - 64;
4354 		}
4355 		break;
4356 	case NRPN_ADDR_0130:	/* EQ BASS */
4357 		if (opt_eq_control) {
4358 			//printMessage(CMSG_INFO,VERB_NOISY,"EQ BASS (CH:%d %.2f dB)", ch, 0.19 * (double)(val - 0x40));
4359 			channel[ch].eq_xg.bass = val;
4360 			recompute_part_eq_xg(&(channel[ch].eq_xg));
4361 		}
4362 		break;
4363 	case NRPN_ADDR_0131:	/* EQ TREBLE */
4364 		if (opt_eq_control) {
4365 			//printMessage(CMSG_INFO,VERB_NOISY,"EQ TREBLE (CH:%d %.2f dB)", ch, 0.19 * (double)(val - 0x40));
4366 			channel[ch].eq_xg.treble = val;
4367 			recompute_part_eq_xg(&(channel[ch].eq_xg));
4368 		}
4369 		break;
4370 	case NRPN_ADDR_0134:	/* EQ BASS frequency */
4371 		if (opt_eq_control) {
4372 			if(val < 4) {val = 4;}
4373 			else if(val > 40) {val = 40;}
4374 			//printMessage(CMSG_INFO,VERB_NOISY,"EQ BASS frequency (CH:%d %d Hz)", ch, (int32_t)eq_freq_table_xg[val]);
4375 			channel[ch].eq_xg.bass_freq = val;
4376 			recompute_part_eq_xg(&(channel[ch].eq_xg));
4377 		}
4378 		break;
4379 	case NRPN_ADDR_0135:	/* EQ TREBLE frequency */
4380 		if (opt_eq_control) {
4381 			if(val < 28) {val = 28;}
4382 			else if(val > 58) {val = 58;}
4383 			//printMessage(CMSG_INFO,VERB_NOISY,"EQ TREBLE frequency (CH:%d %d Hz)", ch, (int32_t)eq_freq_table_xg[val]);
4384 			channel[ch].eq_xg.treble_freq = val;
4385 			recompute_part_eq_xg(&(channel[ch].eq_xg));
4386 		}
4387 		break;
4388 	case NRPN_ADDR_0163:	/* Attack Time */
4389 		if (opt_tva_attack) {set_envelope_time(ch, val, EG_ATTACK);}
4390 		break;
4391 	case NRPN_ADDR_0164:	/* EG Decay Time */
4392 		if (opt_tva_decay) {set_envelope_time(ch, val, EG_DECAY);}
4393 		break;
4394 	case NRPN_ADDR_0166:	/* EG Release Time */
4395 		if (opt_tva_release) {set_envelope_time(ch, val, EG_RELEASE);}
4396 		break;
4397 	case NRPN_ADDR_1400:	/* Drum Filter Cutoff (XG) */
4398 		drumflag = 1;
4399 		note = channel[ch].lastlrpn;
4400 		if (channel[ch].drums[note] == NULL)
4401 			play_midi_setup_drums(ch, note);
4402 		//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument Filter Cutoff (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4403 		channel[ch].drums[note]->drum_cutoff_freq = val - 64;
4404 		break;
4405 	case NRPN_ADDR_1500:	/* Drum Filter Resonance (XG) */
4406 		drumflag = 1;
4407 		note = channel[ch].lastlrpn;
4408 		if (channel[ch].drums[note] == NULL)
4409 			play_midi_setup_drums(ch, note);
4410 		//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument Filter Resonance (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4411 		channel[ch].drums[note]->drum_resonance = val - 64;
4412 		break;
4413 	case NRPN_ADDR_1600:	/* Drum EG Attack Time (XG) */
4414 		drumflag = 1;
4415 		if (opt_tva_attack) {
4416 			val = val & 0x7f;
4417 			note = channel[ch].lastlrpn;
4418 			if (channel[ch].drums[note] == NULL)
4419 				play_midi_setup_drums(ch, note);
4420 			val	-= 64;
4421 			//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument Attack Time (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4422 			channel[ch].drums[note]->drum_envelope_rate[EG_ATTACK] = val;
4423 		}
4424 		break;
4425 	case NRPN_ADDR_1700:	/* Drum EG Decay Time (XG) */
4426 		drumflag = 1;
4427 		if (opt_tva_decay) {
4428 			val = val & 0x7f;
4429 			note = channel[ch].lastlrpn;
4430 			if (channel[ch].drums[note] == NULL)
4431 				play_midi_setup_drums(ch, note);
4432 			val	-= 64;
4433 			//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument Decay Time (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4434 			channel[ch].drums[note]->drum_envelope_rate[EG_DECAY1] =
4435 				channel[ch].drums[note]->drum_envelope_rate[EG_DECAY2] = val;
4436 		}
4437 		break;
4438 	case NRPN_ADDR_1800:	/* Coarse Pitch of Drum (GS) */
4439 		drumflag = 1;
4440 		note = channel[ch].lastlrpn;
4441 		if (channel[ch].drums[note] == NULL)
4442 			play_midi_setup_drums(ch, note);
4443 		channel[ch].drums[note]->coarse = val - 64;
4444 		//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument Pitch Coarse (CH:%d NOTE:%d VAL:%d)", ch, note, channel[ch].drums[note]->coarse);
4445 		channel[ch].pitchfactor = 0;
4446 		break;
4447 	case NRPN_ADDR_1900:	/* Fine Pitch of Drum (XG) */
4448 		drumflag = 1;
4449 		note = channel[ch].lastlrpn;
4450 		if (channel[ch].drums[note] == NULL)
4451 			play_midi_setup_drums(ch, note);
4452 		channel[ch].drums[note]->fine = val - 64;
4453 		//printMessage(CMSG_INFO, VERB_NOISY,	"Drum Instrument Pitch Fine (CH:%d NOTE:%d VAL:%d)", ch, note, channel[ch].drums[note]->fine);
4454 		channel[ch].pitchfactor = 0;
4455 		break;
4456 	case NRPN_ADDR_1A00:	/* Level of Drum */
4457 		drumflag = 1;
4458 		note = channel[ch].lastlrpn;
4459 		if (channel[ch].drums[note] == NULL)
4460 			play_midi_setup_drums(ch, note);
4461 		//printMessage(CMSG_INFO,VERB_NOISY,"Drum Instrument TVA Level (CH:%d NOTE:%d VAL:%d)", ch, note, val);
4462 		channel[ch].drums[note]->drum_level =
4463 				calc_drum_tva_level(ch, note, val);
4464 		break;
4465 	case NRPN_ADDR_1C00:	/* Panpot of Drum */
4466 		drumflag = 1;
4467 		note = channel[ch].lastlrpn;
4468 		if (channel[ch].drums[note] == NULL)
4469 			play_midi_setup_drums(ch, note);
4470 		if(val == 0) {
4471 			val = int_rand(128);
4472 			channel[ch].drums[note]->pan_random = 1;
4473 		} else
4474 			channel[ch].drums[note]->pan_random = 0;
4475 		channel[ch].drums[note]->drum_panning = val;
4476 		if (update_now && adjust_panning_immediately && ! channel[ch].pan_random)
4477 			adjust_drum_panning(ch, note);
4478 		break;
4479 	case NRPN_ADDR_1D00:	/* Reverb Send Level of Drum */
4480 		drumflag = 1;
4481 		note = channel[ch].lastlrpn;
4482 		if (channel[ch].drums[note] == NULL)
4483 			play_midi_setup_drums(ch, note);
4484 		//printMessage(CMSG_INFO, VERB_NOISY,	"Reverb Send Level of Drum (CH:%d NOTE:%d VALUE:%d)", ch, note, val);
4485 		if (channel[ch].drums[note]->reverb_level != val) {
4486 			channel[ch].drum_effect_flag = 0;
4487 		}
4488 		channel[ch].drums[note]->reverb_level = val;
4489 		break;
4490 	case NRPN_ADDR_1E00:	/* Chorus Send Level of Drum */
4491 		drumflag = 1;
4492 		note = channel[ch].lastlrpn;
4493 		if (channel[ch].drums[note] == NULL)
4494 			play_midi_setup_drums(ch, note);
4495 		//printMessage(CMSG_INFO, VERB_NOISY,	"Chorus Send Level of Drum (CH:%d NOTE:%d VALUE:%d)", ch, note, val);
4496 		if (channel[ch].drums[note]->chorus_level != val) {
4497 			channel[ch].drum_effect_flag = 0;
4498 		}
4499 		channel[ch].drums[note]->chorus_level = val;
4500 
4501 		break;
4502 	case NRPN_ADDR_1F00:	/* Variation Send Level of Drum */
4503 		drumflag = 1;
4504 		note = channel[ch].lastlrpn;
4505 		if (channel[ch].drums[note] == NULL)
4506 			play_midi_setup_drums(ch, note);
4507 		//printMessage(CMSG_INFO,VERB_NOISY,"Delay Send Level of Drum (CH:%d NOTE:%d VALUE:%d)", ch, note, val);
4508 		if (channel[ch].drums[note]->delay_level != val) {
4509 			channel[ch].drum_effect_flag = 0;
4510 		}
4511 		channel[ch].drums[note]->delay_level = val;
4512 		break;
4513 	case NRPN_ADDR_3000:	/* Drum EQ BASS */
4514 		drumflag = 1;
4515 		note = channel[ch].lastlrpn;
4516 		if (channel[ch].drums[note] == NULL)
4517 			play_midi_setup_drums(ch, note);
4518 		break;
4519 	case NRPN_ADDR_3100:	/* Drum EQ TREBLE */
4520 		drumflag = 1;
4521 		note = channel[ch].lastlrpn;
4522 		if (channel[ch].drums[note] == NULL)
4523 			play_midi_setup_drums(ch, note);
4524 		break;
4525 	case NRPN_ADDR_3400:	/* Drum EQ BASS frequency */
4526 		drumflag = 1;
4527 		note = channel[ch].lastlrpn;
4528 		if (channel[ch].drums[note] == NULL)
4529 			play_midi_setup_drums(ch, note);
4530 		break;
4531 	case NRPN_ADDR_3500:	/* Drum EQ TREBLE frequency */
4532 		drumflag = 1;
4533 		note = channel[ch].lastlrpn;
4534 		if (channel[ch].drums[note] == NULL)
4535 			play_midi_setup_drums(ch, note);
4536 		break;
4537 	case RPN_ADDR_0000:		/* Pitch bend sensitivity */
4538 		//printMessage(CMSG_INFO, VERB_DEBUG,	"Pitch Bend Sensitivity (CH:%d VALUE:%d)", ch, val);
4539 		/* for mod2mid.c, arpeggio */
4540 		if (channel[ch].rpnmap[RPN_ADDR_0000] > 24)
4541 			channel[ch].rpnmap[RPN_ADDR_0000] = 24;
4542 		channel[ch].pitchfactor = 0;
4543 		break;
4544 	case RPN_ADDR_0001:		/* Master Fine Tuning */
4545 		//printMessage(CMSG_INFO, VERB_DEBUG, "Master Fine Tuning (CH:%d VALUE:%d)", ch, val);
4546 		channel[ch].pitchfactor = 0;
4547 		break;
4548 	case RPN_ADDR_0002:		/* Master Coarse Tuning */
4549 		//printMessage(CMSG_INFO, VERB_DEBUG, "Master Coarse Tuning (CH:%d VALUE:%d)", ch, val);
4550 		channel[ch].pitchfactor = 0;
4551 		break;
4552 	case RPN_ADDR_0003:		/* Tuning Program Select */
4553 		//printMessage(CMSG_INFO, VERB_DEBUG, "Tuning Program Select (CH:%d VALUE:%d)", ch, val);
4554 		for (i = 0; i < upper_voices; i++)
4555 			if (voice[i].status != VOICE_FREE) {
4556 				voice[i].temper_instant = 1;
4557 				recompute_freq(i);
4558 			}
4559 		break;
4560 	case RPN_ADDR_0004:		/* Tuning Bank Select */
4561 		//printMessage(CMSG_INFO, VERB_DEBUG, "Tuning Bank Select (CH:%d VALUE:%d)", ch, val);
4562 		for (i = 0; i < upper_voices; i++)
4563 			if (voice[i].status != VOICE_FREE) {
4564 				voice[i].temper_instant = 1;
4565 				recompute_freq(i);
4566 			}
4567 		break;
4568 	case RPN_ADDR_0005:		/* GM2: Modulation Depth Range */
4569 		channel[ch].mod.lfo1_pitch_depth = (((int32_t)channel[ch].rpnmap[RPN_ADDR_0005] << 7) | channel[ch].rpnmap_lsb[RPN_ADDR_0005]) * 100 / 128;
4570 		//printMessage(CMSG_INFO,VERB_NOISY,"Modulation Depth Range (CH:%d VALUE:%d)", ch, channel[ch].rpnmap[RPN_ADDR_0005]);
4571 		break;
4572 	case RPN_ADDR_7F7F:		/* RPN reset */
4573 		channel[ch].rpn_7f7f_flag = 1;
4574 		break;
4575 	case RPN_ADDR_FFFF:		/* RPN initialize */
4576 		/* All reset to defaults */
4577 		channel[ch].rpn_7f7f_flag = 0;
4578 		memset(channel[ch].rpnmap, 0, sizeof(channel[ch].rpnmap));
4579 		channel[ch].lastlrpn = channel[ch].lastmrpn = 0;
4580 		channel[ch].nrpn = 0;
4581 		channel[ch].rpnmap[RPN_ADDR_0000] = 2;
4582 		channel[ch].rpnmap[RPN_ADDR_0001] = 0x40;
4583 		channel[ch].rpnmap[RPN_ADDR_0002] = 0x40;
4584 		channel[ch].rpnmap_lsb[RPN_ADDR_0005] = 0x40;
4585 		channel[ch].rpnmap[RPN_ADDR_0005] = 0;	/* +- 50 cents */
4586 		channel[ch].pitchfactor = 0;
4587 		break;
4588 	}
4589 	drumflag = 0;
4590 	if (drumflag && midi_drumpart_change(ch, 1)) {
4591 		midi_program_change(ch, channel[ch].program);
4592 	}
4593 }
4594 
voice_increment(int n)4595 void Player::voice_increment(int n)
4596 {
4597     int i;
4598     for(i = 0; i < n; i++)
4599     {
4600 	if(voices == max_voices)
4601 	    break;
4602 	voice[voices].status = VOICE_FREE;
4603 	voice[voices].temper_instant = 0;
4604 	voice[voices].chorus_link = voices;
4605 	voices++;
4606     }
4607 }
4608 
voice_decrement(int n)4609 void Player::voice_decrement(int n)
4610 {
4611     int i, j, lowest;
4612     int32_t lv, v;
4613 
4614     /* decrease voice */
4615     for(i = 0; i < n && voices > 0; i++)
4616     {
4617 	voices--;
4618 	if(voice[voices].status == VOICE_FREE)
4619 	    continue;	/* found */
4620 
4621 	for(j = 0; j < voices; j++)
4622 	    if(voice[j].status == VOICE_FREE)
4623 		break;
4624 	if(j != voices)
4625 	{
4626 	    voice[j] = voice[voices];
4627 	    continue;	/* found */
4628 	}
4629 
4630 	/* Look for the decaying note with the lowest volume */
4631 	lv = 0x7FFFFFFF;
4632 	lowest = -1;
4633 	for(j = 0; j <= voices; j++)
4634 	{
4635 	    if(voice[j].status & ~(VOICE_ON | VOICE_DIE))
4636 	    {
4637 		v = voice[j].left_mix;
4638 		if((voice[j].panned==PANNED_MYSTERY) &&
4639 		   (voice[j].right_mix > v))
4640 		    v = voice[j].right_mix;
4641 		if(v < lv)
4642 		{
4643 		    lv = v;
4644 		    lowest = j;
4645 		}
4646 	    }
4647 	}
4648 
4649 	if(lowest != -1)
4650 	{
4651 	    cut_notes++;
4652 	    free_voice(lowest);
4653 	    voice[lowest] = voice[voices];
4654 	}
4655 	else
4656 	    lost_notes++;
4657     }
4658     if(upper_voices > voices)
4659 	upper_voices = voices;
4660 }
4661 
4662 /* EAW -- do not throw away good notes, stop decrementing */
voice_decrement_conservative(int n)4663 void Player::voice_decrement_conservative(int n)
4664 {
4665     int i, j, lowest, finalnv;
4666     int32_t lv, v;
4667 
4668     /* decrease voice */
4669     finalnv = voices - n;
4670     for(i = 1; i <= n && voices > 0; i++)
4671     {
4672 	if(voice[voices-1].status == VOICE_FREE) {
4673 	    voices--;
4674 	    continue;	/* found */
4675 	}
4676 
4677 	for(j = 0; j < finalnv; j++)
4678 	    if(voice[j].status == VOICE_FREE)
4679 		break;
4680 	if(j != finalnv)
4681 	{
4682 	    voice[j] = voice[voices-1];
4683 	    voices--;
4684 	    continue;	/* found */
4685 	}
4686 
4687 	/* Look for the decaying note with the lowest volume */
4688 	lv = 0x7FFFFFFF;
4689 	lowest = -1;
4690 	for(j = 0; j < voices; j++)
4691 	{
4692 	    if(voice[j].status & ~(VOICE_ON | VOICE_DIE) &&
4693 	       !(voice[j].sample->note_to_use &&
4694 	         ISDRUMCHANNEL(voice[j].channel)))
4695 	    {
4696 		v = voice[j].left_mix;
4697 		if((voice[j].panned==PANNED_MYSTERY) &&
4698 		   (voice[j].right_mix > v))
4699 		    v = voice[j].right_mix;
4700 		if(v < lv)
4701 		{
4702 		    lv = v;
4703 		    lowest = j;
4704 		}
4705 	    }
4706 	}
4707 
4708 	if(lowest != -1)
4709 	{
4710 	    voices--;
4711 	    cut_notes++;
4712 	    free_voice(lowest);
4713 	    voice[lowest] = voice[voices];
4714 	}
4715 	else break;
4716     }
4717     if(upper_voices > voices)
4718 	upper_voices = voices;
4719 }
4720 
mix_signal(int32_t * dest,int32_t * src,int32_t count)4721 void Player::mix_signal(int32_t *dest, int32_t *src, int32_t count)
4722 {
4723 	int32_t i;
4724 	for (i = 0; i < count; i++) {
4725 		dest[i] += src[i];
4726 	}
4727 }
4728 
is_insertion_effect_xg(int ch)4729 int Player::is_insertion_effect_xg(int ch)
4730 {
4731 	int i;
4732 	for (i = 0; i < XG_INSERTION_EFFECT_NUM; i++) {
4733 		if (reverb->insertion_effect_xg[i].part == ch) {
4734 			return 1;
4735 		}
4736 	}
4737 	for (i = 0; i < XG_VARIATION_EFFECT_NUM; i++) {
4738 		if (reverb->variation_effect_xg[i].connection == XG_CONN_INSERTION
4739 			&& reverb->variation_effect_xg[i].part == ch) {
4740 			return 1;
4741 		}
4742 	}
4743 	return 0;
4744 }
4745 
4746 /* do_compute_data_midi() with DSP Effect */
do_compute_data(int32_t count)4747 void Player::do_compute_data(int32_t count)
4748 {
4749 	int i, j, uv, stereo, n, ch, note;
4750 	int32_t *vpblist[MAX_CHANNELS];
4751 	int channel_effect, channel_reverb, channel_chorus, channel_delay, channel_eq;
4752 	int32_t cnt = count * 2, rev_max_delay_out;
4753 	struct DrumPartEffect *de;
4754 
4755 	stereo = true;
4756 	n = count * ((stereo) ? 8 : 4); /* in bytes */
4757 
4758 	memset(buffer_pointer, 0, n);
4759 	memset(insertion_effect_buffer, 0, n);
4760 
4761 	if (timidity_reverb == 3) {
4762 		rev_max_delay_out = 0x7fffffff;	/* disable */
4763 	} else {
4764 		rev_max_delay_out = REVERB_MAX_DELAY_OUT;
4765 	}
4766 
4767 	/* are effects valid? / don't supported in mono */
4768 	channel_reverb = (stereo && (timidity_reverb == 1
4769 			|| timidity_reverb == 3
4770 			|| (timidity_reverb < 0 && timidity_reverb & 0x80)));
4771 	channel_chorus = (stereo && timidity_chorus && !timidity_surround_chorus);
4772 	channel_delay = 0;
4773 
4774 	/* is EQ valid? */
4775 	channel_eq = 0;
4776 
4777 	channel_effect = (stereo && (channel_reverb || channel_chorus
4778 			|| channel_delay || channel_eq || opt_insertion_effect));
4779 
4780 	uv = upper_voices;
4781 	for(i = 0; i < uv; i++) {
4782 		if(voice[i].status != VOICE_FREE) {
4783 			channel[voice[i].channel].lasttime = current_sample + count;
4784 		}
4785 	}
4786 
4787 	/* appropriate buffers for channels */
4788 	if(channel_effect) {
4789 		int buf_index = 0;
4790 
4791 		if(reverb_buffer == NULL) {	/* allocating buffer for channel effect */
4792 			reverb_buffer = (char *)safe_malloc(MAX_CHANNELS * AUDIO_BUFFER_SIZE * 8);
4793 		}
4794 
4795 		for(i = 0; i < MAX_CHANNELS; i++) {
4796 			if(opt_insertion_effect && channel[i].insertion_effect) {
4797 				vpblist[i] = insertion_effect_buffer;
4798 			} else if(channel[i].eq_gs || (get_reverb_level(i) != DEFAULT_REVERB_SEND_LEVEL
4799 					&& current_sample - channel[i].lasttime < rev_max_delay_out)
4800 					|| channel[i].chorus_level > 0 || channel[i].delay_level > 0
4801 					|| channel[i].eq_xg.valid
4802 					|| channel[i].dry_level != 127
4803 					|| (timidity_drum_effect && ISDRUMCHANNEL(i))
4804 					|| is_insertion_effect_xg(i)) {
4805 				vpblist[i] = (int32_t*)(reverb_buffer + buf_index);
4806 				buf_index += n;
4807 			} else {
4808 				vpblist[i] = buffer_pointer;
4809 			}
4810 			/* clear buffers of drum-part effect */
4811 			if (timidity_drum_effect && ISDRUMCHANNEL(i)) {
4812 				for (j = 0; j < channel[i].drum_effect_num; j++) {
4813 					if (channel[i].drum_effect[j].buf != NULL) {
4814 						memset(channel[i].drum_effect[j].buf, 0, n);
4815 					}
4816 				}
4817 			}
4818 		}
4819 
4820 		if(buf_index) {memset(reverb_buffer, 0, buf_index);}
4821 	}
4822 
4823 	for (i = 0; i < uv; i++) {
4824 		if (voice[i].status != VOICE_FREE) {
4825 			int32_t *vpb = NULL;
4826 			int8_t flag;
4827 
4828 			if (channel_effect) {
4829 				flag = 0;
4830 				ch = voice[i].channel;
4831 				if (timidity_drum_effect && ISDRUMCHANNEL(ch)) {
4832 					make_drum_effect(ch);
4833 					note = voice[i].note;
4834 					for (j = 0; j < channel[ch].drum_effect_num; j++) {
4835 						if (channel[ch].drum_effect[j].note == note) {
4836 							vpb = channel[ch].drum_effect[j].buf;
4837 							flag = 1;
4838 						}
4839 					}
4840 					if (flag == 0) {vpb = vpblist[ch];}
4841 				} else {
4842 					vpb = vpblist[ch];
4843 				}
4844 			} else {
4845 				vpb = buffer_pointer;
4846 			}
4847 
4848 			if(!IS_SET_CHANNELMASK(channel_mute, voice[i].channel)) {
4849 				mixer->mix_voice(vpb, i, count);
4850 			} else {
4851 				free_voice(i);
4852 			}
4853 
4854 			if(voice[i].timeout == 1 && voice[i].timeout < current_sample) {
4855 				free_voice(i);
4856 			}
4857 		}
4858 	}
4859 
4860 	while(uv > 0 && voice[uv - 1].status == VOICE_FREE)	{uv--;}
4861 	upper_voices = uv;
4862 
4863 	if(play_system_mode == XG_SYSTEM_MODE && channel_effect) {	/* XG */
4864 		if (opt_insertion_effect) { 	/* insertion effect */
4865 			for (i = 0; i < XG_INSERTION_EFFECT_NUM; i++) {
4866 				if (reverb->insertion_effect_xg[i].part <= MAX_CHANNELS) {
4867 					reverb->do_insertion_effect_xg(vpblist[reverb->insertion_effect_xg[i].part], cnt, &reverb->insertion_effect_xg[i]);
4868 				}
4869 			}
4870 			for (i = 0; i < XG_VARIATION_EFFECT_NUM; i++) {
4871 				if (reverb->variation_effect_xg[i].part <= MAX_CHANNELS) {
4872 					reverb->do_insertion_effect_xg(vpblist[reverb->variation_effect_xg[i].part], cnt, &reverb->variation_effect_xg[i]);
4873 				}
4874 			}
4875 		}
4876 		for(i = 0; i < MAX_CHANNELS; i++) {	/* system effects */
4877 			int32_t *p;
4878 			p = vpblist[i];
4879 			if(p != buffer_pointer) {
4880 				if (timidity_drum_effect && ISDRUMCHANNEL(i)) {
4881 					for (j = 0; j < channel[i].drum_effect_num; j++) {
4882 						de = &(channel[i].drum_effect[j]);
4883 						if (de->reverb_send > 0) {
4884 							reverb->set_ch_reverb(de->buf, cnt, de->reverb_send);
4885 						}
4886 						if (de->chorus_send > 0) {
4887 							reverb->set_ch_chorus(de->buf, cnt, de->chorus_send);
4888 						}
4889 						if (de->delay_send > 0) {
4890 							reverb->set_ch_delay(de->buf, cnt, de->delay_send);
4891 						}
4892 						mix_signal(p, de->buf, cnt);
4893 					}
4894 				} else {
4895 					if(channel_eq && channel[i].eq_xg.valid) {
4896 						reverb->do_ch_eq_xg(p, cnt, &(channel[i].eq_xg));
4897 					}
4898 					if(channel_chorus && channel[i].chorus_level > 0) {
4899 						reverb->set_ch_chorus(p, cnt, channel[i].chorus_level);
4900 					}
4901 					if(channel_delay && channel[i].delay_level > 0) {
4902 						reverb->set_ch_delay(p, cnt, channel[i].delay_level);
4903 					}
4904 					if(channel_reverb && channel[i].reverb_level > 0
4905 						&& current_sample - channel[i].lasttime < rev_max_delay_out) {
4906 						reverb->set_ch_reverb(p, cnt, channel[i].reverb_level);
4907 					}
4908 				}
4909 				if(channel[i].dry_level == 127) {
4910 					reverb->set_dry_signal(p, cnt);
4911 				} else {
4912 					reverb->set_dry_signal_xg(p, cnt, channel[i].dry_level);
4913 				}
4914 			}
4915 		}
4916 
4917 		if(channel_reverb) {
4918 			reverb->set_ch_reverb(buffer_pointer, cnt, DEFAULT_REVERB_SEND_LEVEL);
4919 		}
4920 		reverb->set_dry_signal(buffer_pointer, cnt);
4921 
4922 		/* mixing signal and applying system effects */
4923 		reverb->mix_dry_signal(buffer_pointer, cnt);
4924 		if(channel_delay) { reverb->do_variation_effect1_xg(buffer_pointer, cnt);}
4925 		if(channel_chorus) { reverb->do_ch_chorus_xg(buffer_pointer, cnt);}
4926 		if(channel_reverb) { reverb->do_ch_reverb(buffer_pointer, cnt);}
4927 		if(reverb->multi_eq_xg.valid) { reverb->do_multi_eq_xg(buffer_pointer, cnt);}
4928 	} else if(channel_effect) {	/* GM & GS */
4929 		if(opt_insertion_effect) { 	/* insertion effect */
4930 			/* applying insertion effect */
4931 			reverb->do_insertion_effect_gs(insertion_effect_buffer, cnt);
4932 			/* sending insertion effect voice to channel effect */
4933 			reverb->set_ch_chorus(insertion_effect_buffer, cnt, reverb->insertion_effect_gs.send_chorus);
4934 			reverb->set_ch_delay(insertion_effect_buffer, cnt, reverb->insertion_effect_gs.send_delay);
4935 			reverb->set_ch_reverb(insertion_effect_buffer, cnt, reverb->insertion_effect_gs.send_reverb);
4936 			if(reverb->insertion_effect_gs.send_eq_switch && channel_eq) {
4937 				reverb->set_ch_eq_gs(insertion_effect_buffer, cnt);
4938 			} else {
4939 				reverb->set_dry_signal(insertion_effect_buffer, cnt);
4940 			}
4941 		}
4942 
4943 		for(i = 0; i < MAX_CHANNELS; i++) {	/* system effects */
4944 			int32_t *p;
4945 			p = vpblist[i];
4946 			if(p != buffer_pointer && p != insertion_effect_buffer) {
4947 				if (timidity_drum_effect && ISDRUMCHANNEL(i)) {
4948 					for (j = 0; j < channel[i].drum_effect_num; j++) {
4949 						de = &(channel[i].drum_effect[j]);
4950 						if (de->reverb_send > 0) {
4951 							reverb->set_ch_reverb(de->buf, cnt, de->reverb_send);
4952 						}
4953 						if (de->chorus_send > 0) {
4954 							reverb->set_ch_chorus(de->buf, cnt, de->chorus_send);
4955 						}
4956 						if (de->delay_send > 0) {
4957 							reverb->set_ch_delay(de->buf, cnt, de->delay_send);
4958 						}
4959 						mix_signal(p, de->buf, cnt);
4960 					}
4961 				} else {
4962 					if(channel_chorus && channel[i].chorus_level > 0) {
4963 						reverb->set_ch_chorus(p, cnt, channel[i].chorus_level);
4964 					}
4965 					if(channel_delay && channel[i].delay_level > 0) {
4966 						reverb->set_ch_delay(p, cnt, channel[i].delay_level);
4967 					}
4968 					if(channel_reverb && channel[i].reverb_level > 0
4969 						&& current_sample - channel[i].lasttime < rev_max_delay_out) {
4970 						reverb->set_ch_reverb(p, cnt, channel[i].reverb_level);
4971 					}
4972 				}
4973 				if(channel_eq && channel[i].eq_gs) {
4974 					reverb->set_ch_eq_gs(p, cnt);
4975 				} else {
4976 					reverb->set_dry_signal(p, cnt);
4977 				}
4978 			}
4979 		}
4980 
4981 		if(channel_reverb) {
4982 			reverb->set_ch_reverb(buffer_pointer, cnt, DEFAULT_REVERB_SEND_LEVEL);
4983 		}
4984 		reverb->set_dry_signal(buffer_pointer, cnt);
4985 
4986 		/* mixing signal and applying system effects */
4987 		reverb->mix_dry_signal(buffer_pointer, cnt);
4988 		if(channel_eq) { reverb->do_ch_eq_gs(buffer_pointer, cnt);}
4989 		if(channel_chorus) { reverb->do_ch_chorus(buffer_pointer, cnt);}
4990 		if(channel_delay) { reverb->do_ch_delay(buffer_pointer, cnt);}
4991 		if(channel_reverb) { reverb->do_ch_reverb(buffer_pointer, cnt);}
4992 	}
4993 
4994 	current_sample += count;
4995 }
4996 
compute_data(float * buffer,int32_t count)4997 int Player::compute_data(float *buffer, int32_t count)
4998 {
4999 	if (count == 0) return RC_OK;
5000 
5001 	std::lock_guard<FCriticalSection> lock(ConfigMutex);
5002 
5003 	if (last_reverb_setting != timidity_reverb)
5004 	{
5005 		// If the reverb mode has changed some buffers need to be reallocated before doing any sound generation.
5006 		reverb->free_effect_buffers();
5007 		reverb->init_reverb();
5008 		last_reverb_setting = timidity_reverb;
5009 	}
5010 
5011 	buffer_pointer = common_buffer;
5012 	computed_samples += count;
5013 
5014 	while (count > 0)
5015 	{
5016 		int process = std::min(count, AUDIO_BUFFER_SIZE);
5017 		do_compute_data(process);
5018 		count -= process;
5019 
5020 		effect->do_effect(common_buffer, process);
5021 		// pass to caller
5022 		for (int i = 0; i < process*2; i++)
5023 		{
5024 			*buffer++ = (common_buffer[i])*(5.f / 0x80000000u);
5025 		}
5026 	}
5027 	return RC_OK;
5028 }
5029 
update_modulation_wheel(int ch)5030 void Player::update_modulation_wheel(int ch)
5031 {
5032     int i, uv = upper_voices;
5033 	channel[ch].pitchfactor = 0;
5034     for(i = 0; i < uv; i++)
5035 	if(voice[i].status != VOICE_FREE && voice[i].channel == ch)
5036 	{
5037 	    /* Set/Reset mod-wheel */
5038 		voice[i].vibrato_control_counter = voice[i].vibrato_phase = 0;
5039 	    recompute_amp(i);
5040 		mixer->apply_envelope_to_amp(i);
5041 	    recompute_freq(i);
5042 		recompute_voice_filter(i);
5043 	}
5044 }
5045 
drop_portamento(int ch)5046 void Player::drop_portamento(int ch)
5047 {
5048     int i, uv = upper_voices;
5049 
5050     channel[ch].porta_control_ratio = 0;
5051     for(i = 0; i < uv; i++)
5052 	if(voice[i].status != VOICE_FREE &&
5053 	   voice[i].channel == ch &&
5054 	   voice[i].porta_control_ratio)
5055 	{
5056 	    voice[i].porta_control_ratio = 0;
5057 	    recompute_freq(i);
5058 	}
5059     channel[ch].last_note_fine = -1;
5060 }
5061 
update_portamento_controls(int ch)5062 void Player::update_portamento_controls(int ch)
5063 {
5064     if(!channel[ch].portamento ||
5065        (channel[ch].portamento_time_msb | channel[ch].portamento_time_lsb)
5066        == 0)
5067 	drop_portamento(ch);
5068     else
5069     {
5070 	double mt, dc;
5071 	int d;
5072 
5073 	mt = midi_time_table[channel[ch].portamento_time_msb & 0x7F] *
5074 	    midi_time_table2[channel[ch].portamento_time_lsb & 0x7F] *
5075 		PORTAMENTO_TIME_TUNING;
5076 	dc = playback_rate * mt;
5077 	d = (int)(1.0 / (mt * PORTAMENTO_CONTROL_RATIO));
5078 	d++;
5079 	channel[ch].porta_control_ratio = (int)(d * dc + 0.5);
5080 	channel[ch].porta_dpb = d;
5081     }
5082 }
5083 
update_portamento_time(int ch)5084 void Player::update_portamento_time(int ch)
5085 {
5086     int i, uv = upper_voices;
5087     int dpb;
5088     int32_t ratio;
5089 
5090     update_portamento_controls(ch);
5091     dpb = channel[ch].porta_dpb;
5092     ratio = channel[ch].porta_control_ratio;
5093 
5094     for(i = 0; i < uv; i++)
5095     {
5096 	if(voice[i].status != VOICE_FREE &&
5097 	   voice[i].channel == ch &&
5098 	   voice[i].porta_control_ratio)
5099 	{
5100 	    voice[i].porta_control_ratio = ratio;
5101 	    voice[i].porta_dpb = dpb;
5102 	    recompute_freq(i);
5103 	}
5104     }
5105 }
5106 
update_legato_controls(int ch)5107 void Player::update_legato_controls(int ch)
5108 {
5109 	double mt, dc;
5110 	int d;
5111 
5112 	mt = 0.06250 * PORTAMENTO_TIME_TUNING * 0.3;
5113 	dc = playback_rate * mt;
5114 	d = (int)(1.0 / (mt * PORTAMENTO_CONTROL_RATIO));
5115 	d++;
5116 	channel[ch].porta_control_ratio = (int)(d * dc + 0.5);
5117 	channel[ch].porta_dpb = d;
5118 }
5119 
play_event(MidiEvent * ev)5120 int Player::play_event(MidiEvent *ev)
5121 {
5122 	int32_t i, j;
5123 	int k, l, ch, orig_ch, port_ch, offset, layered;
5124 
5125 	current_event = ev;
5126 
5127 #ifndef SUPPRESS_CHANNEL_LAYER
5128 	orig_ch = ev->channel;
5129 	layered = !IS_SYSEX_EVENT_TYPE(ev);
5130 	for (k = 0; k < MAX_CHANNELS; k += 16) {
5131 		port_ch = (orig_ch + k) % MAX_CHANNELS;
5132 		offset = port_ch & ~0xf;
5133 		for (l = offset; l < offset + 16; l++) {
5134 			if (!layered && (k || l != offset))
5135 				continue;
5136 			if (layered) {
5137 				if (!IS_SET_CHANNELMASK(channel[l].channel_layer, port_ch)
5138 					|| channel[l].port_select != (orig_ch >> 4))
5139 					continue;
5140 				ev->channel = l;
5141 			}
5142 #endif
5143 			ch = ev->channel;
5144 
5145 			switch (ev->type)
5146 			{
5147 				/* MIDI Events */
5148 			case ME_NOTEOFF:
5149 				note_off(ev);
5150 				break;
5151 
5152 			case ME_NOTEON:
5153 				note_on(ev);
5154 				break;
5155 
5156 			case ME_KEYPRESSURE:
5157 				adjust_pressure(ev);
5158 				break;
5159 
5160 			case ME_PROGRAM:
5161 				midi_program_change(ch, ev->a);
5162 				break;
5163 
5164 			case ME_CHANNEL_PRESSURE:
5165 				adjust_channel_pressure(ev);
5166 				break;
5167 
5168 			case ME_PITCHWHEEL:
5169 				channel[ch].pitchbend = ev->a + ev->b * 128;
5170 				channel[ch].pitchfactor = 0;
5171 				/* Adjust pitch for notes already playing */
5172 				adjust_pitch(ch);
5173 				break;
5174 
5175 				/* Controls */
5176 			case ME_TONE_BANK_MSB:
5177 				channel[ch].bank_msb = ev->a;
5178 				break;
5179 
5180 			case ME_TONE_BANK_LSB:
5181 				channel[ch].bank_lsb = ev->a;
5182 				break;
5183 
5184 			case ME_MODULATION_WHEEL:
5185 				channel[ch].mod.val = ev->a;
5186 				update_modulation_wheel(ch);
5187 				break;
5188 
5189 			case ME_MAINVOLUME:
5190 				channel[ch].volume = ev->a;
5191 				adjust_volume(ch);
5192 				break;
5193 
5194 			case ME_PAN:
5195 				channel[ch].panning = ev->a;
5196 				channel[ch].pan_random = 0;
5197 				if (adjust_panning_immediately && !channel[ch].pan_random)
5198 					adjust_panning(ch);
5199 				break;
5200 
5201 			case ME_EXPRESSION:
5202 				channel[ch].expression = ev->a;
5203 				adjust_volume(ch);
5204 				break;
5205 
5206 			case ME_SUSTAIN:
5207 				if (channel[ch].sustain == 0 && ev->a >= 64) {
5208 					update_redamper_controls(ch);
5209 				}
5210 				channel[ch].sustain = ev->a;
5211 				if (channel[ch].damper_mode == 0) {	/* half-damper is not allowed. */
5212 					if (channel[ch].sustain >= 64) { channel[ch].sustain = 127; }
5213 					else { channel[ch].sustain = 0; }
5214 				}
5215 				if (channel[ch].sustain == 0 && channel[ch].sostenuto == 0)
5216 					drop_sustain(ch);
5217 				break;
5218 
5219 			case ME_SOSTENUTO:
5220 				channel[ch].sostenuto = (ev->a >= 64);
5221 				if (channel[ch].sustain == 0 && channel[ch].sostenuto == 0)
5222 					drop_sustain(ch);
5223 				else { update_sostenuto_controls(ch); }
5224 				break;
5225 
5226 			case ME_LEGATO_FOOTSWITCH:
5227 				channel[ch].legato = (ev->a >= 64);
5228 				break;
5229 
5230 			case ME_HOLD2:
5231 				break;
5232 
5233 			case ME_BREATH:
5234 				break;
5235 
5236 			case ME_FOOT:
5237 				break;
5238 
5239 			case ME_BALANCE:
5240 				break;
5241 
5242 			case ME_PORTAMENTO_TIME_MSB:
5243 				channel[ch].portamento_time_msb = ev->a;
5244 				update_portamento_time(ch);
5245 				break;
5246 
5247 			case ME_PORTAMENTO_TIME_LSB:
5248 				channel[ch].portamento_time_lsb = ev->a;
5249 				update_portamento_time(ch);
5250 				break;
5251 
5252 			case ME_PORTAMENTO:
5253 				channel[ch].portamento = (ev->a >= 64);
5254 				if (!channel[ch].portamento)
5255 					drop_portamento(ch);
5256 				break;
5257 
5258 			case ME_SOFT_PEDAL:
5259 				if (timidity_lpf_def) {
5260 					channel[ch].soft_pedal = ev->a;
5261 					//printMessage(CMSG_INFO,VERB_NOISY,"Soft Pedal (CH:%d VAL:%d)", ch, channel[ch].soft_pedal);
5262 				}
5263 				break;
5264 
5265 			case ME_HARMONIC_CONTENT:
5266 				if (timidity_lpf_def) {
5267 					channel[ch].param_resonance = ev->a - 64;
5268 					//printMessage(CMSG_INFO,VERB_NOISY,"Harmonic Content (CH:%d VAL:%d)", ch, channel[ch].param_resonance);
5269 				}
5270 				break;
5271 
5272 			case ME_BRIGHTNESS:
5273 				if (timidity_lpf_def) {
5274 					channel[ch].param_cutoff_freq = ev->a - 64;
5275 					//printMessage(CMSG_INFO,VERB_NOISY,"Brightness (CH:%d VAL:%d)", ch, channel[ch].param_cutoff_freq);
5276 				}
5277 				break;
5278 
5279 			case ME_DATA_ENTRY_MSB:
5280 				if (channel[ch].rpn_7f7f_flag) /* disable */
5281 					break;
5282 				if ((i = last_rpn_addr(ch)) >= 0)
5283 				{
5284 					channel[ch].rpnmap[i] = ev->a;
5285 					update_rpn_map(ch, i, 1);
5286 				}
5287 				break;
5288 
5289 			case ME_DATA_ENTRY_LSB:
5290 				if (channel[ch].rpn_7f7f_flag) /* disable */
5291 					break;
5292 				if ((i = last_rpn_addr(ch)) >= 0)
5293 				{
5294 					channel[ch].rpnmap_lsb[i] = ev->a;
5295 				}
5296 				break;
5297 
5298 			case ME_REVERB_EFFECT:
5299 				if (timidity_reverb) {
5300 					if (ISDRUMCHANNEL(ch) && get_reverb_level(ch) != ev->a) { channel[ch].drum_effect_flag = 0; }
5301 					set_reverb_level(ch, ev->a);
5302 				}
5303 				break;
5304 
5305 			case ME_CHORUS_EFFECT:
5306 				if (timidity_chorus)
5307 				{
5308 					if (timidity_chorus == 1) {
5309 						if (ISDRUMCHANNEL(ch) && channel[ch].chorus_level != ev->a) { channel[ch].drum_effect_flag = 0; }
5310 						channel[ch].chorus_level = ev->a;
5311 					}
5312 					else {
5313 						channel[ch].chorus_level = -timidity_chorus;
5314 					}
5315 					if (ev->a) {
5316 						//printMessage(CMSG_INFO,VERB_NOISY,"Chorus Send (CH:%d LEVEL:%d)", ch, ev->a);
5317 					}
5318 				}
5319 				break;
5320 
5321 			case ME_TREMOLO_EFFECT:
5322 				//printMessage(CMSG_INFO,VERB_NOISY,"Tremolo Send (CH:%d LEVEL:%d)", ch, ev->a);
5323 				break;
5324 
5325 			case ME_CELESTE_EFFECT:
5326 				if (opt_delay_control) {
5327 					if (ISDRUMCHANNEL(ch) && channel[ch].delay_level != ev->a) { channel[ch].drum_effect_flag = 0; }
5328 					channel[ch].delay_level = ev->a;
5329 					if (play_system_mode == XG_SYSTEM_MODE) {
5330 						//printMessage(CMSG_INFO,VERB_NOISY,"Variation Send (CH:%d LEVEL:%d)", ch, ev->a);
5331 					}
5332 					else {
5333 						//printMessage(CMSG_INFO,VERB_NOISY,"Delay Send (CH:%d LEVEL:%d)", ch, ev->a);
5334 					}
5335 				}
5336 				break;
5337 
5338 			case ME_ATTACK_TIME:
5339 				if (!opt_tva_attack) { break; }
5340 				set_envelope_time(ch, ev->a, EG_ATTACK);
5341 				break;
5342 
5343 			case ME_RELEASE_TIME:
5344 				if (!opt_tva_release) { break; }
5345 				set_envelope_time(ch, ev->a, EG_RELEASE);
5346 				break;
5347 
5348 			case ME_PHASER_EFFECT:
5349 				//printMessage(CMSG_INFO,VERB_NOISY,"Phaser Send (CH:%d LEVEL:%d)", ch, ev->a);
5350 				break;
5351 
5352 			case ME_RPN_INC:
5353 				if (channel[ch].rpn_7f7f_flag) /* disable */
5354 					break;
5355 				if ((i = last_rpn_addr(ch)) >= 0)
5356 				{
5357 					if (channel[ch].rpnmap[i] < 127)
5358 						channel[ch].rpnmap[i]++;
5359 					update_rpn_map(ch, i, 1);
5360 				}
5361 				break;
5362 
5363 			case ME_RPN_DEC:
5364 				if (channel[ch].rpn_7f7f_flag) /* disable */
5365 					break;
5366 				if ((i = last_rpn_addr(ch)) >= 0)
5367 				{
5368 					if (channel[ch].rpnmap[i] > 0)
5369 						channel[ch].rpnmap[i]--;
5370 					update_rpn_map(ch, i, 1);
5371 				}
5372 				break;
5373 
5374 			case ME_NRPN_LSB:
5375 				channel[ch].lastlrpn = ev->a;
5376 				channel[ch].nrpn = 1;
5377 				break;
5378 
5379 			case ME_NRPN_MSB:
5380 				channel[ch].lastmrpn = ev->a;
5381 				channel[ch].nrpn = 1;
5382 				break;
5383 
5384 			case ME_RPN_LSB:
5385 				channel[ch].lastlrpn = ev->a;
5386 				channel[ch].nrpn = 0;
5387 				break;
5388 
5389 			case ME_RPN_MSB:
5390 				channel[ch].lastmrpn = ev->a;
5391 				channel[ch].nrpn = 0;
5392 				break;
5393 
5394 			case ME_ALL_SOUNDS_OFF:
5395 				all_sounds_off(ch);
5396 				break;
5397 
5398 			case ME_RESET_CONTROLLERS:
5399 				reset_controllers(ch);
5400 				break;
5401 
5402 			case ME_ALL_NOTES_OFF:
5403 				all_notes_off(ch);
5404 				break;
5405 
5406 			case ME_MONO:
5407 				channel[ch].mono = 1;
5408 				all_notes_off(ch);
5409 				break;
5410 
5411 			case ME_POLY:
5412 				channel[ch].mono = 0;
5413 				all_notes_off(ch);
5414 				break;
5415 
5416 				/* TiMidity Extensionals */
5417 			case ME_RANDOM_PAN:
5418 				channel[ch].panning = int_rand(128);
5419 				channel[ch].pan_random = 1;
5420 				if (adjust_panning_immediately && !channel[ch].pan_random)
5421 					adjust_panning(ch);
5422 				break;
5423 
5424 			case ME_SET_PATCH:
5425 				i = channel[ch].special_sample = current_event->a;
5426 				instruments->setSpecialPatchOffset(i, 0);
5427 				break;
5428 
5429 			case ME_TEMPO:
5430 				current_play_tempo = ch + ev->b * 256 + ev->a * 65536;
5431 				break;
5432 
5433 			case ME_CHORUS_TEXT:
5434 			case ME_LYRIC:
5435 			case ME_MARKER:
5436 			case ME_INSERT_TEXT:
5437 			case ME_TEXT:
5438 			case ME_KARAOKE_LYRIC:
5439 			case ME_GSLCD:
5440 				break;
5441 
5442 			case ME_MASTER_VOLUME:
5443 				master_volume_ratio = (int32_t)ev->a + 256 * (int32_t)ev->b;
5444 				adjust_master_volume();
5445 				break;
5446 
5447 			case ME_RESET:
5448 				change_system_mode(ev->a);
5449 				reset_midi(1);
5450 				break;
5451 
5452 			case ME_PATCH_OFFS:
5453 				i = channel[ch].special_sample;
5454 				instruments->setSpecialPatchOffset(i, current_event->a | (256 * current_event->b));
5455 				break;
5456 
5457 			case ME_WRD:
5458 				break;
5459 
5460 			case ME_SHERRY:
5461 				break;
5462 
5463 			case ME_DRUMPART:
5464 				if (midi_drumpart_change(ch, current_event->a))
5465 				{
5466 					/* Update bank information */
5467 					midi_program_change(ch, channel[ch].program);
5468 				}
5469 				break;
5470 
5471 			case ME_KEYSHIFT:
5472 				i = (int)current_event->a - 0x40;
5473 				if (i != channel[ch].key_shift)
5474 				{
5475 					all_sounds_off(ch);
5476 					channel[ch].key_shift = (int8_t)i;
5477 				}
5478 				break;
5479 
5480 			case ME_KEYSIG:
5481 				if (opt_init_keysig != 8)
5482 					break;
5483 				current_keysig = current_event->a + current_event->b * 16;
5484 				if (opt_force_keysig != 8) {
5485 					i = current_keysig - ((current_keysig < 8) ? 0 : 16), j = 0;
5486 					while (i != opt_force_keysig && i != opt_force_keysig + 12)
5487 						i += (i > 0) ? -5 : 7, j++;
5488 					while (abs(j - note_key_offset) > 7)
5489 						j += (j > note_key_offset) ? -12 : 12;
5490 					if (abs(j - timidity_key_adjust) >= 12)
5491 						j += (j > timidity_key_adjust) ? -12 : 12;
5492 					note_key_offset = j;
5493 					kill_all_voices();
5494 				}
5495 				i = current_keysig + ((current_keysig < 8) ? 7 : -9), j = 0;
5496 				while (i != 7)
5497 					i += (i < 7) ? 5 : -7, j++;
5498 				j += note_key_offset, j -= floor(j / 12.0) * 12;
5499 				current_freq_table = j;
5500 				break;
5501 
5502 			case ME_MASTER_TUNING:
5503 				set_master_tuning((ev->b << 8) | ev->a);
5504 				adjust_all_pitch();
5505 				break;
5506 
5507 			case ME_SCALE_TUNING:
5508 				recache->resamp_cache_refer_alloff(ch, computed_samples);
5509 				channel[ch].scale_tuning[current_event->a] = current_event->b;
5510 				adjust_pitch(ch);
5511 				break;
5512 
5513 			case ME_BULK_TUNING_DUMP:
5514 				set_single_note_tuning(ch, current_event->a, current_event->b, 0);
5515 				break;
5516 
5517 			case ME_SINGLE_NOTE_TUNING:
5518 				set_single_note_tuning(ch, current_event->a, current_event->b, 1);
5519 				break;
5520 
5521 			case ME_TEMPER_KEYSIG:
5522 				current_temper_keysig = (current_event->a + 8) % 32 - 8;
5523 				temper_adj = ((current_event->a + 8) & 0x20) ? 1 : 0;
5524 				i = current_temper_keysig + ((current_temper_keysig < 8) ? 7 : -9);
5525 				j = 0;
5526 				while (i != 7)
5527 					i += (i < 7) ? 5 : -7, j++;
5528 				j += note_key_offset, j -= floor(j / 12.0) * 12;
5529 				current_temper_freq_table = j;
5530 				if (current_event->b)
5531 					for (i = 0; i < upper_voices; i++)
5532 						if (voice[i].status != VOICE_FREE) {
5533 							voice[i].temper_instant = 1;
5534 							recompute_freq(i);
5535 						}
5536 				break;
5537 
5538 			case ME_TEMPER_TYPE:
5539 				channel[ch].temper_type = current_event->a;
5540 				if (temper_type_mute) {
5541 					if (temper_type_mute & (1 << (current_event->a
5542 						- ((current_event->a >= 0x40) ? 0x3c : 0)))) {
5543 						SET_CHANNELMASK(channel_mute, ch);
5544 					}
5545 					else {
5546 						UNSET_CHANNELMASK(channel_mute, ch);
5547 					}
5548 				}
5549 				if (current_event->b)
5550 					for (i = 0; i < upper_voices; i++)
5551 						if (voice[i].status != VOICE_FREE) {
5552 							voice[i].temper_instant = 1;
5553 							recompute_freq(i);
5554 						}
5555 				break;
5556 
5557 			case ME_MASTER_TEMPER_TYPE:
5558 				for (i = 0; i < MAX_CHANNELS; i++) {
5559 					channel[i].temper_type = current_event->a;
5560 				}
5561 				if (temper_type_mute) {
5562 					if (temper_type_mute & (1 << (current_event->a
5563 						- ((current_event->a >= 0x40) ? 0x3c : 0)))) {
5564 						FILL_CHANNELMASK(channel_mute);
5565 					}
5566 					else {
5567 						CLEAR_CHANNELMASK(channel_mute);
5568 					}
5569 				}
5570 				if (current_event->b)
5571 					for (i = 0; i < upper_voices; i++)
5572 						if (voice[i].status != VOICE_FREE) {
5573 							voice[i].temper_instant = 1;
5574 							recompute_freq(i);
5575 						}
5576 				break;
5577 
5578 			case ME_USER_TEMPER_ENTRY:
5579 				set_user_temper_entry(ch, current_event->a, current_event->b);
5580 				break;
5581 
5582 			case ME_SYSEX_LSB:
5583 				process_sysex_event(ME_SYSEX_LSB, ch, current_event->a, current_event->b);
5584 				break;
5585 
5586 			case ME_SYSEX_MSB:
5587 				process_sysex_event(ME_SYSEX_MSB, ch, current_event->a, current_event->b);
5588 				break;
5589 
5590 			case ME_SYSEX_GS_LSB:
5591 				process_sysex_event(ME_SYSEX_GS_LSB, ch, current_event->a, current_event->b);
5592 				break;
5593 
5594 			case ME_SYSEX_GS_MSB:
5595 				process_sysex_event(ME_SYSEX_GS_MSB, ch, current_event->a, current_event->b);
5596 				break;
5597 
5598 			case ME_SYSEX_XG_LSB:
5599 				process_sysex_event(ME_SYSEX_XG_LSB, ch, current_event->a, current_event->b);
5600 				break;
5601 
5602 			case ME_SYSEX_XG_MSB:
5603 				process_sysex_event(ME_SYSEX_XG_MSB, ch, current_event->a, current_event->b);
5604 				break;
5605 
5606 			case ME_NOTE_STEP:
5607 				break;
5608 
5609 			case ME_EOT:
5610 				break;
5611 			}
5612 #ifndef SUPPRESS_CHANNEL_LAYER
5613 		}
5614 	}
5615 	ev->channel = orig_ch;
5616 #endif
5617 
5618 	return RC_OK;
5619 }
5620 
set_master_tuning(int tune)5621 void Player::set_master_tuning(int tune)
5622 {
5623 	if (tune & 0x4000)	/* 1/8192 semitones + 0x2000 | 0x4000 */
5624 		tune = (tune & 0x3FFF) - 0x2000;
5625 	else if (tune & 0x8000)	/* 1 semitones | 0x8000 */
5626 		tune = ((tune & 0x7F) - 0x40) << 13;
5627 	else	/* millisemitones + 0x400 */
5628 		tune = (((tune - 0x400) << 13) + 500) / 1000;
5629 	master_tuning = tune;
5630 }
5631 
set_single_note_tuning(int part,int a,int b,int rt)5632 void Player::set_single_note_tuning(int part, int a, int b, int rt)
5633 {
5634 	static int tp;	/* tuning program number */
5635 	static int kn;	/* MIDI key number */
5636 	static int st;	/* the nearest equal-tempered semitone */
5637 	double f, fst;	/* fraction of semitone */
5638 	int i;
5639 
5640 	switch (part) {
5641 	case 0:
5642 		tp = a;
5643 		break;
5644 	case 1:
5645 		kn = a, st = b;
5646 		break;
5647 	case 2:
5648 		if (st == 0x7f && a == 0x7f && b == 0x7f)	/* no change */
5649 			break;
5650 		f = 440 * pow(2.0, (st - 69) / 12.0);
5651 		fst = pow(2.0, (a << 7 | b) / 196608.0);
5652 		freq_table_tuning[tp][kn] = f * fst * 1000 + 0.5;
5653 		if (rt)
5654 			for (i = 0; i < upper_voices; i++)
5655 				if (voice[i].status != VOICE_FREE) {
5656 					voice[i].temper_instant = 1;
5657 					recompute_freq(i);
5658 				}
5659 		break;
5660 	}
5661 }
5662 
set_user_temper_entry(int part,int a,int b)5663 void Player::set_user_temper_entry(int part, int a, int b)
5664 {
5665 	static int tp;		/* temperament program number */
5666 	static int ll;		/* number of formula */
5667 	static int fh, fl;	/* applying pitch bit mask (forward) */
5668 	static int bh, bl;	/* applying pitch bit mask (backward) */
5669 	static int aa, bb;	/* fraction (aa/bb) */
5670 	static int cc, dd;	/* power (cc/dd)^(ee/ff) */
5671 	static int ee, ff;
5672 	static int ifmax, ibmax, count;
5673 	static double rf[11], rb[11];
5674 	int i, j, k, l, n, m;
5675 	double ratio[12], f, sc;
5676 
5677 	switch (part) {
5678 	case 0:
5679 		for (i = 0; i < 11; i++)
5680 			rf[i] = rb[i] = 1;
5681 		ifmax = ibmax = 0;
5682 		count = 0;
5683 		tp = a, ll = b;
5684 		break;
5685 	case 1:
5686 		fh = a, fl = b;
5687 		break;
5688 	case 2:
5689 		bh = a, bl = b;
5690 		break;
5691 	case 3:
5692 		aa = a, bb = b;
5693 		break;
5694 	case 4:
5695 		cc = a, dd = b;
5696 		break;
5697 	case 5:
5698 		ee = a, ff = b;
5699 		for (i = 0; i < 11; i++) {
5700 			if (((fh & 0xf) << 7 | fl) & 1 << i) {
5701 				rf[i] *= (double) aa / bb
5702 						* pow((double) cc / dd, (double) ee / ff);
5703 				if (ifmax < i + 1)
5704 					ifmax = i + 1;
5705 			}
5706 			if (((bh & 0xf) << 7 | bl) & 1 << i) {
5707 				rb[i] *= (double) aa / bb
5708 						* pow((double) cc / dd, (double) ee / ff);
5709 				if (ibmax < i + 1)
5710 					ibmax = i + 1;
5711 			}
5712 		}
5713 		if (++count < ll)
5714 			break;
5715 		ratio[0] = 1;
5716 		for (i = n = m = 0; i < ifmax; i++, m = n) {
5717 			n += (n > 4) ? -5 : 7;
5718 			ratio[n] = ratio[m] * rf[i];
5719 			if (ratio[n] > 2)
5720 				ratio[n] /= 2;
5721 		}
5722 		for (i = n = m = 0; i < ibmax; i++, m = n) {
5723 			n += (n > 6) ? -7 : 5;
5724 			ratio[n] = ratio[m] / rb[i];
5725 			if (ratio[n] < 1)
5726 				ratio[n] *= 2;
5727 		}
5728 		sc = 27 / ratio[9] / 16;	/* syntonic comma */
5729 		for (i = 0; i < 12; i++)
5730 			for (j = -1; j < 11; j++) {
5731 				f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
5732 				for (k = 0; k < 12; k++) {
5733 					l = i + j * 12 + k;
5734 					if (l < 0 || l >= 128)
5735 						continue;
5736 					if (! (fh & 0x40)) {	/* major */
5737 						freq_table_user[tp][i][l] =
5738 								f * ratio[k] * 1000 + 0.5;
5739 						freq_table_user[tp][i + 36][l] =
5740 								f * ratio[k] * sc * 1000 + 0.5;
5741 					}
5742 					if (! (bh & 0x40)) {	/* minor */
5743 						freq_table_user[tp][i + 12][l] =
5744 								f * ratio[k] * sc * 1000 + 0.5;
5745 						freq_table_user[tp][i + 24][l] =
5746 								f * ratio[k] * 1000 + 0.5;
5747 					}
5748 				}
5749 			}
5750 		break;
5751 	}
5752 }
5753 
5754 
5755 
5756 
new_midi_file_info()5757 struct midi_file_info *Player::new_midi_file_info()
5758 {
5759 	struct midi_file_info *p = &midifileinfo;
5760 
5761 	/* Initialize default members */
5762 	memset(p, 0, sizeof(struct midi_file_info));
5763 	p->hdrsiz = -1;
5764 	p->format = -1;
5765 	p->tracks = -1;
5766 	p->divisions = -1;
5767 	p->time_sig_n = p->time_sig_d = -1;
5768 	p->samples = -1;
5769 	p->max_channel = -1;
5770 	COPY_CHANNELMASK(p->drumchannels, default_drumchannels);
5771 	COPY_CHANNELMASK(p->drumchannel_mask, default_drumchannel_mask);
5772 	return p;
5773 }
5774 
5775 
5776 
5777 /*
5778  * For MIDI stream player.
5779  */
playmidi_stream_init(void)5780 void Player::playmidi_stream_init(void)
5781 {
5782     int i;
5783     static int first = 1;
5784 
5785     note_key_offset = timidity_key_adjust;
5786     midi_time_ratio = timidity_tempo_adjust;
5787     CLEAR_CHANNELMASK(channel_mute);
5788 	if (temper_type_mute & 1)
5789 		FILL_CHANNELMASK(channel_mute);
5790 	if (first)
5791 	{
5792 		first = 0;
5793 		init_mblock(&playmidi_pool);
5794 		midi_streaming = 1;
5795 	}
5796     else
5797         reuse_mblock(&playmidi_pool);
5798 
5799     /* Fill in current_file_info */
5800 	current_file_info = &midifileinfo;
5801     current_file_info->readflag = 1;
5802     current_file_info->hdrsiz = 0;
5803     current_file_info->format = 0;
5804     current_file_info->tracks = 0;
5805     current_file_info->divisions = 192; /* ?? */
5806     current_file_info->time_sig_n = 4; /* 4/ */
5807     current_file_info->time_sig_d = 4; /* /4 */
5808     current_file_info->time_sig_c = 24; /* clock */
5809     current_file_info->time_sig_b = 8;  /* q.n. */
5810     current_file_info->samples = 0;
5811     current_file_info->max_channel = MAX_CHANNELS;
5812     current_file_info->compressed = 0;
5813 
5814     current_play_tempo = 500000;
5815     check_eot_flag = 0;
5816 
5817     /* Setup default drums */
5818 	COPY_CHANNELMASK(current_file_info->drumchannels, default_drumchannels);
5819 	COPY_CHANNELMASK(current_file_info->drumchannel_mask, default_drumchannel_mask);
5820     for(i = 0; i < MAX_CHANNELS; i++)
5821 	memset(channel[i].drums, 0, sizeof(channel[i].drums));
5822     change_system_mode(DEFAULT_SYSTEM_MODE);
5823     reset_midi(0);
5824 
5825     playmidi_tmr_reset();
5826 }
5827 
playmidi_tmr_reset(void)5828 void Player::playmidi_tmr_reset(void)
5829 {
5830     int i;
5831 
5832     current_sample = 0;
5833     buffer_pointer = common_buffer;
5834     for(i = 0; i < MAX_CHANNELS; i++)
5835 	channel[i].lasttime = 0;
5836 }
5837 
5838 
5839 /*! initialize Part EQ (XG) */
init_part_eq_xg(struct part_eq_xg * p)5840 void Player::init_part_eq_xg(struct part_eq_xg *p)
5841 {
5842 	p->bass = 0x40;
5843 	p->treble = 0x40;
5844 	p->bass_freq = 0x0C;
5845 	p->treble_freq = 0x36;
5846 	p->valid = 0;
5847 }
5848 
5849 /*! recompute Part EQ (XG) */
recompute_part_eq_xg(struct part_eq_xg * p)5850 void Player::recompute_part_eq_xg(struct part_eq_xg *p)
5851 {
5852 	int8_t vbass, vtreble;
5853 
5854 	if(p->bass_freq >= 4 && p->bass_freq <= 40 && p->bass != 0x40) {
5855 		vbass = 1;
5856 		p->basss.q = 0.7;
5857 		p->basss.freq = eq_freq_table_xg[p->bass_freq];
5858 		if(p->bass == 0) {p->basss.gain = -12.0;}
5859 		else {p->basss.gain = 0.19 * (double)(p->bass - 0x40);}
5860 		reverb->calc_filter_shelving_low(&(p->basss));
5861 	} else {vbass = 0;}
5862 	if(p->treble_freq >= 28 && p->treble_freq <= 58 && p->treble != 0x40) {
5863 		vtreble = 1;
5864 		p->trebles.q = 0.7;
5865 		p->trebles.freq = eq_freq_table_xg[p->treble_freq];
5866 		if(p->treble == 0) {p->trebles.gain = -12.0;}
5867 		else {p->trebles.gain = 0.19 * (double)(p->treble - 0x40);}
5868 		reverb->calc_filter_shelving_high(&(p->trebles));
5869 	} else {vtreble = 0;}
5870 	p->valid = vbass || vtreble;
5871 }
5872 
init_midi_controller(midi_controller * p)5873 void Player::init_midi_controller(midi_controller *p)
5874 {
5875 	p->val = 0;
5876 	p->pitch = 0;
5877 	p->cutoff = 0;
5878 	p->amp = 0.0;
5879 	p->lfo1_rate = p->lfo2_rate = p->lfo1_tva_depth = p->lfo2_tva_depth = 0;
5880 	p->lfo1_pitch_depth = p->lfo2_pitch_depth = p->lfo1_tvf_depth = p->lfo2_tvf_depth = 0;
5881 	p->variation_control_depth = p->insertion_control_depth = 0;
5882 }
5883 
get_midi_controller_amp(midi_controller * p)5884 float Player::get_midi_controller_amp(midi_controller *p)
5885 {
5886 	return (1.0 + (float)p->val * (1.0f / 127.0f) * p->amp);
5887 }
5888 
get_midi_controller_filter_cutoff(midi_controller * p)5889 float Player::get_midi_controller_filter_cutoff(midi_controller *p)
5890 {
5891 	return ((float)p->val * (1.0f / 127.0f) * (float)p->cutoff);
5892 }
5893 
get_midi_controller_filter_depth(midi_controller * p)5894 float Player::get_midi_controller_filter_depth(midi_controller *p)
5895 {
5896 	return ((float)p->val * (1.0f / 127.0f) * (float)p->lfo1_tvf_depth);
5897 }
5898 
get_midi_controller_pitch(midi_controller * p)5899 int32_t Player::get_midi_controller_pitch(midi_controller *p)
5900 {
5901 	return ((int32_t)(p->val * p->pitch) << 6);
5902 }
5903 
get_midi_controller_pitch_depth(midi_controller * p)5904 int16_t Player::get_midi_controller_pitch_depth(midi_controller *p)
5905 {
5906 	return (int16_t)((float)p->val * (float)p->lfo1_pitch_depth * (1.0f / 127.0f * 256.0 / 400.0));
5907 }
5908 
get_midi_controller_amp_depth(midi_controller * p)5909 int16_t Player::get_midi_controller_amp_depth(midi_controller *p)
5910 {
5911 	return (int16_t)((float)p->val * (float)p->lfo1_tva_depth * (1.0f / 127.0f * 256.0));
5912 }
5913 
init_rx(int ch)5914 void Player::init_rx(int ch)
5915 {
5916 	channel[ch].rx = 0xFFFFFFFF;	/* all on */
5917 }
5918 
set_rx(int ch,int32_t rx,int flag)5919 void Player::set_rx(int ch, int32_t rx, int flag)
5920 {
5921 	if(ch > MAX_CHANNELS) {return;}
5922 	if(flag) {channel[ch].rx |= rx;}
5923 	else {channel[ch].rx &= ~rx;}
5924 }
5925 
init_rx_drum(struct DrumParts * p)5926 void Player::init_rx_drum(struct DrumParts *p)
5927 {
5928 	p->rx = 0xFFFFFFFF;	/* all on */
5929 }
5930 
set_rx_drum(struct DrumParts * p,int32_t rx,int flag)5931 void Player::set_rx_drum(struct DrumParts *p, int32_t rx, int flag)
5932 {
5933 	if(flag) {p->rx |= rx;}
5934 	else {p->rx &= ~rx;}
5935 }
5936 
get_rx_drum(struct DrumParts * p,int32_t rx)5937 int32_t Player::get_rx_drum(struct DrumParts *p, int32_t rx)
5938 {
5939 	return (p->rx & rx);
5940 }
5941 
play_midi_load_instrument(int dr,int bk,int prog)5942 Instrument *Player::play_midi_load_instrument(int dr, int bk, int prog)
5943 {
5944 	bool load_success;
5945 	// The inner workings of this function which alters the instrument data has been put into the Instruments class.
5946 	auto instr = instruments->play_midi_load_instrument(dr, bk, prog, &load_success);
5947 	//if (load_success) send_output(NULL, 0);	/* Update software buffer */
5948 	return instr;
5949 }
5950 
change_system_mode(int mode)5951 void Player::change_system_mode(int mode)
5952 {
5953 	pan_table = sc_pan_table;
5954 	switch (mode)
5955 	{
5956 	case GM_SYSTEM_MODE:
5957 		if (play_system_mode == DEFAULT_SYSTEM_MODE)
5958 		{
5959 			play_system_mode = GM_SYSTEM_MODE;
5960 			vol_table = def_vol_table;
5961 		}
5962 		break;
5963 	case GM2_SYSTEM_MODE:
5964 		play_system_mode = GM2_SYSTEM_MODE;
5965 		vol_table = def_vol_table;
5966 		pan_table = gm2_pan_table;
5967 		break;
5968 	case GS_SYSTEM_MODE:
5969 		play_system_mode = GS_SYSTEM_MODE;
5970 		vol_table = gs_vol_table;
5971 		break;
5972 	case XG_SYSTEM_MODE:
5973 		if (play_system_mode != XG_SYSTEM_MODE) { reverb->init_all_effect_xg(); }
5974 		play_system_mode = XG_SYSTEM_MODE;
5975 		vol_table = xg_vol_table;
5976 		break;
5977 	default:
5978 		play_system_mode = DEFAULT_SYSTEM_MODE;
5979 		vol_table = def_vol_table;
5980 		break;
5981 	}
5982 }
5983 
5984 
5985 /*! initialize channel layers. */
init_channel_layer(int ch)5986 void Player::init_channel_layer(int ch)
5987 {
5988 	if (ch >= MAX_CHANNELS)
5989 		return;
5990 	CLEAR_CHANNELMASK(channel[ch].channel_layer);
5991 	SET_CHANNELMASK(channel[ch].channel_layer, ch);
5992 	channel[ch].port_select = ch >> 4;
5993 }
5994 
5995 
5996 static const struct ctl_chg_types {
5997 	unsigned char mtype;
5998 	int ttype;
5999 } ctl_chg_list[] = {
6000 	{ 0, ME_TONE_BANK_MSB },
6001 { 1, ME_MODULATION_WHEEL },
6002 { 2, ME_BREATH },
6003 { 4, ME_FOOT },
6004 { 5, ME_PORTAMENTO_TIME_MSB },
6005 { 6, ME_DATA_ENTRY_MSB },
6006 { 7, ME_MAINVOLUME },
6007 { 8, ME_BALANCE },
6008 { 10, ME_PAN },
6009 { 11, ME_EXPRESSION },
6010 { 32, ME_TONE_BANK_LSB },
6011 { 37, ME_PORTAMENTO_TIME_LSB },
6012 { 38, ME_DATA_ENTRY_LSB },
6013 { 64, ME_SUSTAIN },
6014 { 65, ME_PORTAMENTO },
6015 { 66, ME_SOSTENUTO },
6016 { 67, ME_SOFT_PEDAL },
6017 { 68, ME_LEGATO_FOOTSWITCH },
6018 { 69, ME_HOLD2 },
6019 { 71, ME_HARMONIC_CONTENT },
6020 { 72, ME_RELEASE_TIME },
6021 { 73, ME_ATTACK_TIME },
6022 { 74, ME_BRIGHTNESS },
6023 { 84, ME_PORTAMENTO_CONTROL },
6024 { 91, ME_REVERB_EFFECT },
6025 { 92, ME_TREMOLO_EFFECT },
6026 { 93, ME_CHORUS_EFFECT },
6027 { 94, ME_CELESTE_EFFECT },
6028 { 95, ME_PHASER_EFFECT },
6029 { 96, ME_RPN_INC },
6030 { 97, ME_RPN_DEC },
6031 { 98, ME_NRPN_LSB },
6032 { 99, ME_NRPN_MSB },
6033 { 100, ME_RPN_LSB },
6034 { 101, ME_RPN_MSB },
6035 { 120, ME_ALL_SOUNDS_OFF },
6036 { 121, ME_RESET_CONTROLLERS },
6037 { 123, ME_ALL_NOTES_OFF },
6038 { 126, ME_MONO },
6039 { 127, ME_POLY },
6040 };
6041 
convert_midi_control_change(int chn,int type,int val,MidiEvent * ev_ret)6042 int Player::convert_midi_control_change(int chn, int type, int val, MidiEvent *ev_ret)
6043 {
6044 	for (auto &t : ctl_chg_list)
6045 	{
6046 		if (t.mtype == type)
6047 		{
6048 			if (val > 127) val = 127;
6049 			ev_ret->type = t.ttype;
6050 			ev_ret->channel = chn;
6051 			ev_ret->a = val;
6052 			ev_ret->b = 0;
6053 			return 1;
6054 		}
6055 	}
6056 	return 0;
6057 }
6058 
6059 
send_event(int status,int parm1,int parm2)6060 int Player::send_event(int status, int parm1, int parm2)
6061 {
6062 	MidiEvent ev;
6063 
6064 	ev.type = ME_NONE;
6065 	ev.channel = status & 0x0000000f;
6066 	//ev.channel = ev.channel + port * 16;
6067 	ev.a = (uint8_t)parm1;
6068 	ev.b = (uint8_t)parm2;
6069 	switch ((int)(status & 0x000000f0))
6070 	{
6071 	case 0x80:
6072 		ev.type = ME_NOTEOFF;
6073 		break;
6074 	case 0x90:
6075 		ev.type = (ev.b) ? ME_NOTEON : ME_NOTEOFF;
6076 		break;
6077 	case 0xa0:
6078 		ev.type = ME_KEYPRESSURE;
6079 		break;
6080 	case 0xb0:
6081 		if (!convert_midi_control_change(ev.channel, ev.a, ev.b, &ev))
6082 			ev.type = ME_NONE;
6083 		break;
6084 	case 0xc0:
6085 		ev.type = ME_PROGRAM;
6086 		break;
6087 	case 0xd0:
6088 		ev.type = ME_CHANNEL_PRESSURE;
6089 		break;
6090 	case 0xe0:
6091 		ev.type = ME_PITCHWHEEL;
6092 		break;
6093 	/*
6094 	case 0xf0:
6095 		if ((status & 0x000000ff) == 0xf2)
6096 		{
6097 			ev.type = ME_PROGRAM;
6098 		}
6099 		break;
6100 	*/
6101 	default:
6102 		break;
6103 	}
6104 	if (ev.type != ME_NONE)
6105 	{
6106 		play_event(&ev);
6107 	}
6108 	return 0;
6109 }
6110 
send_long_event(const uint8_t * sysexbuffer,int exlen)6111 void Player::send_long_event(const uint8_t *sysexbuffer, int exlen)
6112 {
6113 	int i, ne;
6114 	MidiEvent ev;
6115 	MidiEvent evm[260];
6116 	SysexConvert sc;
6117 
6118 	if ((sysexbuffer[0] != 0xf0) && (sysexbuffer[0] != 0xf7)) return;
6119 
6120 	if (sc.parse_sysex_event(sysexbuffer + 1, exlen - 1, &ev, instruments))
6121 	{
6122 		if (ev.type == ME_RESET)
6123 		{
6124 			kill_all_voices();
6125 			for (int i = 0; i < MAX_CHANNELS; i++)
6126 				init_channel_layer(i);
6127 
6128 			/* initialize effect status */
6129 			reverb->init_effect_status(play_system_mode);
6130 			effect->init_effect();
6131 			instruments->init_userdrum();
6132 			instruments->init_userinst();
6133 			playmidi_stream_init();
6134 		}
6135 		play_event(&ev);
6136 		return;
6137 	}
6138 	if ((ne = sc.parse_sysex_event_multi(sysexbuffer + 1, exlen - 1, evm, instruments)))
6139 	{
6140 		for (i = 0; i < ne; i++)
6141 		{
6142 			play_event(&evm[i]);
6143 		}
6144 	}
6145 }
6146 
6147 
6148 
6149 }
6150