aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/imuse/mac_m68k.cpp
blob: 0980ef1fd2837c10bdc359d0b238af14fa67e418 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "scumm/imuse/mac_m68k.h"

#include "common/util.h"
#include "common/macresman.h"
#include "common/stream.h"

namespace Scumm {

MacM68kDriver::MacM68kDriver(Audio::Mixer *mixer)
	: MidiDriver_Emulated(mixer) {
}

MacM68kDriver::~MacM68kDriver() {
}

int MacM68kDriver::open() {
	if (_isOpen) {
		return MERR_ALREADY_OPEN;
	}

	const int error = MidiDriver_Emulated::open();
	if (error) {
		return error;
	}

	for (uint i = 0; i < ARRAYSIZE(_channels); ++i) {
		_channels[i].init(this, i);
	}

	memset(_voiceChannels, 0, sizeof(_voiceChannels));
	_lastUsedVoiceChannel = 0;

	loadAllInstruments();

	_pitchTable[116] = 1664510;
	_pitchTable[117] = 1763487;
	_pitchTable[118] = 1868350;
	_pitchTable[119] = 1979447;
	_pitchTable[120] = 2097152;
	_pitchTable[121] = 2221855;
	_pitchTable[122] = 2353973;
	_pitchTable[123] = 2493948;
	_pitchTable[124] = 2642246;
	_pitchTable[125] = 2799362;
	_pitchTable[126] = 2965820;
	_pitchTable[127] = 3142177;
	for (int i = 115; i >= 0; --i) {
		_pitchTable[i] = _pitchTable[i + 12] / 2;
	}

	_volumeTable = new byte[8192];
	for (int i = 0; i < 32; ++i) {
		for (int j = 0; j < 256; ++j) {
			_volumeTable[i * 256 + j] = ((-128 + j) * _volumeBaseTable[i]) / 127 - 128;
		}
	}

	_mixBuffer = 0;
	_mixBufferLength = 0;

	// We set the output sound type to music here to allow sound volume
	// adjustment. The drawback here is that we can not control the music and
	// sfx separately here. But the AdLib output has the same issue so it
	// should not be that bad.
	_mixer->playStream(Audio::Mixer::kMusicSoundType, &_mixerSoundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);

	return 0;
}

void MacM68kDriver::close() {
	if (!_isOpen) {
		return;
	}

	_mixer->stopHandle(_mixerSoundHandle);
	_isOpen = false;
	for (InstrumentMap::iterator i = _instruments.begin(); i != _instruments.end(); ++i) {
		delete[] i->_value.data;
	}
	_instruments.clear();
	delete[] _volumeTable;
	_volumeTable = 0;
	delete[] _mixBuffer;
	_mixBuffer = 0;
	_mixBufferLength = 0;
}

void MacM68kDriver::send(uint32 d) {
	assert(false);
}

void MacM68kDriver::sysEx_customInstrument(byte channel, uint32 type, const byte *instr) {
	assert(false);
}

MidiChannel *MacM68kDriver::allocateChannel() {
	for (uint i = 0; i < ARRAYSIZE(_channels); ++i) {
		if (_channels[i].allocate()) {
			return &_channels[i];
		}
	}

	return 0;
}

MacM68kDriver::Instrument MacM68kDriver::getInstrument(int idx) const {
	InstrumentMap::const_iterator i = _instruments.find(idx);
	if (i != _instruments.end()) {
		return i->_value;
	} else {
		return _defaultInstrument;
	}
}

void MacM68kDriver::generateSamples(int16 *buf, int len) {
	int silentChannels = 0;

	if (_mixBufferLength < len) {
		delete[] _mixBuffer;

		_mixBufferLength = len;
		_mixBuffer = new int[_mixBufferLength];
		assert(_mixBuffer);
	}
	memset(_mixBuffer, 0, sizeof(int) * _mixBufferLength);

	for (int i = 0; i < kChannelCount; ++i) {
		OutputChannel &out = _voiceChannels[i].out;
		if (out.isFinished) {
			++silentChannels;
			continue;
		}

		byte *volumeTable = &_volumeTable[(out.volume / 4) * 256];
		int *buffer = _mixBuffer;

		int samplesLeft = len;
		while (samplesLeft) {
			out.subPos += out.pitchModifier;
			while (out.subPos >= 0x10000) {
				out.subPos -= 0x10000;
				out.instrument++;
			}

			if (out.instrument >= out.end) {
				if (!out.start) {
					break;
				}

				out.instrument = out.start;
				out.subPos = 0;
			}

			*buffer++ += volumeTable[*out.instrument];
			--samplesLeft;
		}

		if (samplesLeft) {
			out.isFinished = true;
			while (samplesLeft--) {
				*buffer++ += 0x80;
			}
		}
	}

	const int *buffer = _mixBuffer;
	const int silenceAdd = silentChannels << 7;
	while (len--) {
		*buf++ = (((*buffer++ + silenceAdd) >> 3) << 8) ^ 0x8000;
	}
}

void MacM68kDriver::loadAllInstruments() {
	Common::MacResManager resource;
	if (resource.open("iMUSE Setups")) {
		if (!resource.hasResFork()) {
			error("MacM68kDriver::loadAllInstruments: \"iMUSE Setups\" loaded, but no resource fork present");
		}

		for (int i = 0x3E7; i < 0x468; ++i) {
			Common::SeekableReadStream *stream = resource.getResource(MKTAG('s', 'n', 'd', ' '), i);
			if (stream) {
				addInstrument(i, stream);
				delete stream;
			}
		}

		for (int i = 0x7D0; i < 0x8D0; ++i) {
			Common::SeekableReadStream *stream = resource.getResource(MKTAG('s', 'n', 'd', ' '), i);
			if (stream) {
				addInstrument(i, stream);
				delete stream;
			}
		}

		InstrumentMap::iterator inst = _instruments.find(kDefaultInstrument);
		if (inst != _instruments.end()) {
			_defaultInstrument = inst->_value;
		} else {
			error("MacM68kDriver::loadAllInstruments: Could not load default instrument");
		}
	} else {
		error("MacM68kDriver::loadAllInstruments: Could not load \"iMUSE Setups\"");
	}
}

void MacM68kDriver::addInstrument(int idx, Common::SeekableReadStream *data) {
	// We parse the "SND" files manually here, since we need special data
	// from their header and need to work on them raw while mixing.
	data->skip(2);
	int count = data->readUint16BE();
	data->skip(2 * (3 * count));
	count = data->readUint16BE();
	data->skip(2 * (4 * count));

	Instrument inst;
	// Skip (optional) pointer to data
	data->skip(4);
	inst.length        = data->readUint32BE();
	inst.sampleRate    = data->readUint32BE();
	inst.loopStart     = data->readUint32BE();
	inst.loopEnd       = data->readUint32BE();
	// Skip encoding
	data->skip(1);
	inst.baseFrequency = data->readByte();

	inst.data = new byte[inst.length];
	assert(inst.data);
	data->read(inst.data, inst.length);
	_instruments[idx] = inst;
}

void MacM68kDriver::setPitch(OutputChannel *out, int frequency) {
	out->frequency = frequency;
	out->isFinished = false;

	const int pitchIdx = (frequency >> 7) + 60 - out->baseFrequency;
	assert(pitchIdx >= 0);

	const int low7Bits = frequency & 0x7F;
	if (low7Bits) {
		out->pitchModifier = _pitchTable[pitchIdx] + (((_pitchTable[pitchIdx + 1] - _pitchTable[pitchIdx]) * low7Bits) >> 7);
	} else {
		out->pitchModifier = _pitchTable[pitchIdx];
	}
}

void MacM68kDriver::VoiceChannel::off() {
	if (out.start) {
		out.isFinished = true;
	}

	part->removeVoice(this);
	part = 0;
}

void MacM68kDriver::MidiChannel_MacM68k::release() {
	_allocated = false;
	while (_voice) {
		_voice->off();
	}
}

void MacM68kDriver::MidiChannel_MacM68k::send(uint32 b) {
	uint8 type = b & 0xF0;
	uint8 p1 = (b >> 8) & 0xFF;
	uint8 p2 = (b >> 16) & 0xFF;

	switch (type) {
	case 0x80:
		noteOff(p1);
		break;

	case 0x90:
		if (p2) {
			noteOn(p1, p2);
		} else {
			noteOff(p1);
		}
		break;

	case 0xB0:
		controlChange(p1, p2);
		break;

	case 0xE0:
		pitchBend((p1 | (p2 << 7)) - 0x2000);
		break;

	default:
		break;
	}
}

void MacM68kDriver::MidiChannel_MacM68k::noteOff(byte note) {
	for (VoiceChannel *i = _voice; i; i = i->next) {
		if (i->note == note) {
			if (_sustain) {
				i->sustainNoteOff = true;
			} else {
				i->off();
			}
		}
	}
}

void MacM68kDriver::MidiChannel_MacM68k::noteOn(byte note, byte velocity) {
	// Do not start a not unless there is an instrument set up
	if (!_instrument.data) {
		return;
	}

	// Allocate a voice channel
	VoiceChannel *voice = _owner->allocateVoice(_priority);
	if (!voice) {
		return;
	}
	addVoice(voice);

	voice->note = note;
	// This completly ignores the note's volume, but is in accordance
	// to the original.
	voice->out.volume = _volume;

	// Set up the instrument data
	voice->out.baseFrequency = _instrument.baseFrequency;
	voice->out.soundStart    = _instrument.data;
	voice->out.soundEnd      = _instrument.data + _instrument.length;
	if (_instrument.loopEnd && _instrument.loopEnd - 12 > _instrument.loopStart) {
		voice->out.loopStart = _instrument.data + _instrument.loopStart;
		voice->out.loopEnd   = _instrument.data + _instrument.loopEnd;
	} else {
		voice->out.loopStart = 0;
		voice->out.loopEnd   = voice->out.soundEnd;
	}

	voice->out.start = voice->out.loopStart;
	voice->out.end   = voice->out.loopEnd;

	// Set up the pitch
	_owner->setPitch(&voice->out, (note << 7) + _pitchBend);

	// Set up the sample position
	voice->out.instrument = voice->out.soundStart;
	voice->out.subPos = 0;
}

void MacM68kDriver::MidiChannel_MacM68k::programChange(byte program) {
	_instrument = _owner->getInstrument(program + kProgramChangeBase);
}

void MacM68kDriver::MidiChannel_MacM68k::pitchBend(int16 bend) {
	_pitchBend = (bend * _pitchBendFactor) >> 6;
	for (VoiceChannel *i = _voice; i; i = i->next) {
		_owner->setPitch(&i->out, (i->note << 7) + _pitchBend);
	}
}

void MacM68kDriver::MidiChannel_MacM68k::controlChange(byte control, byte value) {
	switch (control) {
	// volume change
	case 7:
		_volume = value;
		for (VoiceChannel *i = _voice; i; i = i->next) {
			i->out.volume = value;
			i->out.isFinished = false;
		}
		break;

	// sustain
	case 64:
		_sustain = value;
		if (!_sustain) {
			for (VoiceChannel *i = _voice; i; i = i->next) {
				if (i->sustainNoteOff) {
					i->off();
				}
			}
		}
		break;

	// all notes off
	case 123:
		for (VoiceChannel *i = _voice; i; i = i->next) {
			i->off();
		}
		break;

	default:
		break;
	}
}

void MacM68kDriver::MidiChannel_MacM68k::pitchBendFactor(byte value) {
	_pitchBendFactor = value;
}

void MacM68kDriver::MidiChannel_MacM68k::priority(byte value) {
	_priority = value;
}

void MacM68kDriver::MidiChannel_MacM68k::sysEx_customInstrument(uint32 type, const byte *instr) {
	assert(instr);
	if (type == 'MAC ') {
		_instrument = _owner->getInstrument(*instr + kSysExBase);
	}
}

void MacM68kDriver::MidiChannel_MacM68k::init(MacM68kDriver *owner, byte channel) {
	_owner = owner;
	_number = channel;
	_allocated = false;
}

bool MacM68kDriver::MidiChannel_MacM68k::allocate() {
	if (_allocated) {
		return false;
	}

	_allocated = true;
	_voice = 0;
	_priority = 0;
	memset(&_instrument, 0, sizeof(_instrument));
	_pitchBend = 0;
	_pitchBendFactor = 0;
	_volume = 0;
	return true;
}

void MacM68kDriver::MidiChannel_MacM68k::addVoice(VoiceChannel *voice) {
	voice->next = _voice;
	voice->prev = 0;
	voice->part = this;
	if (_voice) {
		_voice->prev = voice;
	}
	_voice = voice;
}

void MacM68kDriver::MidiChannel_MacM68k::removeVoice(VoiceChannel *voice) {
	VoiceChannel *i = _voice;
	while (i && i != voice) {
		i = i->next;
	}

	if (i) {
		if (i->next) {
			i->next->prev = i->prev;
		}

		if (i->prev) {
			i->prev->next = i->next;
		} else {
			_voice = i->next;
		}
	}
}

MacM68kDriver::VoiceChannel *MacM68kDriver::allocateVoice(int priority) {
	VoiceChannel *channel = 0;
	for (int i = 0; i < kChannelCount; ++i) {
		if (++_lastUsedVoiceChannel == kChannelCount) {
			_lastUsedVoiceChannel = 0;
		}

		VoiceChannel *cur = &_voiceChannels[_lastUsedVoiceChannel];
		if (!cur->part) {
			memset(cur, 0, sizeof(*cur));
			return cur;
		} else if (!cur->next) {
			if (cur->part->_priority <= priority) {
				priority = cur->part->_priority;
				channel = cur;
			}
		}
	}

	if (channel) {
		channel->off();
		memset(channel, 0, sizeof(*channel));
	}

	return channel;
}

const int MacM68kDriver::_volumeBaseTable[32] = {
	  0,   0,   1,   1,   2,   3,   5,   6,
	  8,  11,  13,  16,  19,  22,  26,  30,
	 34,  38,  43,  48,  53,  58,  64,  70,
	 76,  83,  89,  96, 104, 111, 119, 127
};

} // End of namespace Scumm