aboutsummaryrefslogtreecommitdiff
path: root/sound/gmidi.cpp
blob: ecd4e560c1575c8c4ad4338122c41a9ece95c863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "scumm.h"
#include "gmidi.h"

void MidiSoundDriver::midiSetDriver(MidiDriver *driver)
{
	_md = driver;

	int result = _md->open(MidiDriver::MO_SIMPLE);
	if (result)
		error("MidiSoundDriver::error = %s", MidiDriver::get_error_name(result));
}

/****************** Common midi code (SCUMM specific) *****************/
void MidiSoundDriver::midiPitchBend(byte chan, int16 pitchbend)
{
	uint16 tmp;

	if (_midi_pitchbend_last[chan] != pitchbend) {
		_midi_pitchbend_last[chan] = pitchbend;
		tmp = (pitchbend << 2) + 0x2000;
		_md->send(((tmp >> 7) & 0x7F) << 16 | (tmp & 0x7F) << 8 | 0xE0 | chan);
	}
}

void MidiSoundDriver::midiVolume(byte chan, byte volume)
{
	if (_midi_volume_last[chan] != volume) {
		_midi_volume_last[chan] = volume;
		_md->send(volume << 16 | 7 << 8 | 0xB0 | chan);
	}
}
void MidiSoundDriver::midiPedal(byte chan, bool pedal)
{
	if (_midi_pedal_last[chan] != pedal) {
		_midi_pedal_last[chan] = pedal;
		_md->send(pedal << 16 | 64 << 8 | 0xB0 | chan);
	}
}

void MidiSoundDriver::midiModWheel(byte chan, byte modwheel)
{
	if (_midi_modwheel_last[chan] != modwheel) {
		_midi_modwheel_last[chan] = modwheel;
		_md->send(modwheel << 16 | 1 << 8 | 0xB0 | chan);
	}
}

void MidiSoundDriver::midiEffectLevel(byte chan, byte level)
{
	if (_midi_effectlevel_last[chan] != level) {
		_midi_effectlevel_last[chan] = level;
		_md->send(level << 16 | 91 << 8 | 0xB0 | chan);
	}
}

void MidiSoundDriver::midiChorus(byte chan, byte chorus)
{
	if (_midi_chorus_last[chan] != chorus) {
		_midi_chorus_last[chan] = chorus;
		_md->send(chorus << 16 | 93 << 8 | 0xB0 | chan);
	}
}

void MidiSoundDriver::midiControl0(byte chan, byte value)
{
	_md->send(value << 16 | 0 << 8 | 0xB0 | chan);
}

void MidiSoundDriver::midiProgram(byte chan, byte program)
{
	if ((chan + 1) != 10) {				/* Ignore percussion prededed by patch change */
		if (_se->_mt32emulate)
			program = mt32_to_gmidi[program];

		_md->send(program << 8 | 0xC0 | chan);
	}
}

void MidiSoundDriver::midiPan(byte chan, int8 pan)
{
	if (_midi_pan_last[chan] != pan) {
		_midi_pan_last[chan] = pan;
		_md->send(((pan - 64) & 0x7F) << 16 | 10 << 8 | 0xB0 | chan);
	}
}

void MidiSoundDriver::midiNoteOn(byte chan, byte note, byte velocity)
{
	_md->send(velocity << 16 | note << 8 | 0x90 | chan);
}

void MidiSoundDriver::midiNoteOff(byte chan, byte note)
{
	_md->send(note << 8 | 0x80 | chan);
}

void MidiSoundDriver::midiSilence(byte chan)
{
	_md->send((64 << 8) | 0xB0 | chan);
	_md->send((123 << 8) | 0xB0 | chan);
}


void MidiSoundDriver::part_key_on(Part *part, byte note, byte velocity)
{
	MidiChannelGM *mc = part->_mc->gm();

	if (mc) {
		mc->_actives[note >> 4] |= (1 << (note & 0xF));
		midiNoteOn(mc->_chan, note, velocity);
	} else if (part->_percussion) {
		midiVolume(SPECIAL_CHANNEL, part->_vol_eff);
		midiProgram(SPECIAL_CHANNEL, part->_bank);
		midiNoteOn(SPECIAL_CHANNEL, note, velocity);
	}
}

void MidiSoundDriver::part_key_off(Part *part, byte note)
{
	MidiChannelGM *mc = part->_mc->gm();

	if (mc) {
		mc->_actives[note >> 4] &= ~(1 << (note & 0xF));
		midiNoteOff(mc->_chan, note);
	} else if (part->_percussion) {
		midiNoteOff(SPECIAL_CHANNEL, note);
	}
}

int MidiSoundDriver::midi_driver_thread(void *param) {
	MidiSoundDriver *mid = (MidiSoundDriver*) param;
	int old_time, cur_time;

	old_time = mid->_system->get_msecs();

	for(;;) {
		mid->_system->delay_msecs(10);

		cur_time = mid->_system->get_msecs();
		while (old_time < cur_time) {
			old_time += 10;
			mid->_se->on_timer();
		}
	}
}

void MidiSoundDriver::init(SoundEngine *eng, OSystem *syst)
{
	int i;
	MidiChannelGM *mc;

	_system = syst;

	/* Install the on_timer thread */
	syst->create_thread(midi_driver_thread, this);
	_se = eng;

	for (i = 0, mc = _midi_channels; i != ARRAYSIZE(_midi_channels); i++, mc++)
		mc->_chan = i;
}

void MidiSoundDriver::update_pris()
{
	Part *part, *hipart;
	int i;
	byte hipri, lopri;
	MidiChannelGM *mc, *lomc;

	while (true) {
		hipri = 0;
		hipart = NULL;
		for (i = 32, part = _se->parts_ptr(); i; i--, part++) {
			if (part->_player && !part->_percussion && part->_on && !part->_mc
					&& part->_pri_eff >= hipri) {
				hipri = part->_pri_eff;
				hipart = part;
			}
		}

		if (!hipart)
			return;

		lopri = 255;
		lomc = NULL;
		for (i = ARRAYSIZE(_midi_channels), mc = _midi_channels;; mc++) {
			if (!mc->_part) {
				lomc = mc;
				break;
			}
			if (mc->_part->_pri_eff <= lopri) {
				lopri = mc->_part->_pri_eff;
				lomc = mc;
			}

			if (!--i) {
				if (lopri >= hipri)
					return;
				lomc->_part->off();
				break;
			}
		}

		hipart->_mc = lomc;
		lomc->_part = hipart;
		hipart->changed(pcAll);
	}
}

int MidiSoundDriver::part_update_active(Part *part, uint16 *active)
{
	int i, j;
	uint16 *act, mask, bits;
	int count = 0;

	bits = 1 << part->_chan;

	act = part->_mc->gm()->_actives;

	for (i = 8; i; i--) {
		mask = *act++;
		if (mask) {
			for (j = 16; j; j--, mask >>= 1, active++) {
				if (mask & 1 && !(*active & bits)) {
					*active |= bits;
					count++;
				}
			}
		} else {
			active += 16;
		}
	}
	return count;
}

void MidiSoundDriver::part_changed(Part *part, byte what)
{
	MidiChannelGM *mc;

	/* Mark for re-schedule if program changed when in pre-state */
	if (what & pcProgram && part->_percussion) {
		part->_percussion = false;
		update_pris();
	}

	if (!(mc = part->_mc->gm()))
		return;

	if (what & pcMod)
		midiPitchBend(mc->_chan,
									clamp(part->_pitchbend + part->_detune_eff +
												(part->_transpose_eff << 7), -2048, 2047));

	if (what & pcVolume)
		midiVolume(mc->_chan, part->_vol_eff);

	if (what & pcPedal)
		midiPedal(mc->_chan, part->_pedal);

	if (what & pcModwheel)
		midiModWheel(mc->_chan, part->_modwheel);

	if (what & pcPan)
		midiPan(mc->_chan, part->_pan_eff);

	if (what & pcEffectLevel)
		midiEffectLevel(mc->_chan, part->_effect_level);

	if (what & pcProgram) {
		if (part->_bank) {
			midiControl0(mc->_chan, part->_bank);
			midiProgram(mc->_chan, part->_program);
			midiControl0(mc->_chan, 0);
		} else {
			midiProgram(mc->_chan, part->_program);
		}
	}

	if (what & pcChorus)
		midiChorus(mc->_chan, part->_effect_level);
}


void MidiSoundDriver::part_off(Part *part)
{
	MidiChannelGM *mc = part->_mc->gm();
	if (mc) {
		part->_mc = NULL;
		mc->_part = NULL;
		memset(mc->_actives, 0, sizeof(mc->_actives));
		midiSilence(mc->_chan);
	}
}