aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/players/player_he.cpp
blob: 74041e5ba4bd4a549f88f60f82876a8cf55d9305 (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
/* 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.
 *
 */

#ifdef ENABLE_HE

#include "scumm/players/player_he.h"
#include "scumm/scumm.h"
#include "scumm/file.h"
#include "audio/miles.h"
#include "audio/midiparser.h"
#include "audio/mixer.h"
#include "common/memstream.h"

namespace Scumm {
Player_HE::Player_HE(ScummEngine *scumm) :
	_vm(scumm),
	_currentMusic(-1),
	_bank(NULL),
	_parser(NULL),
	_midi(NULL),
	_masterVolume(256) {

	for (int chan = 0; chan < 16; chan++)
		_channelVolume[chan] = 127;

	loadAdLibBank();

	Common::MemoryReadStream *bankStream = new Common::MemoryReadStream(_bank, _bankSize);

	_midi = Audio::MidiDriver_Miles_AdLib_create("", "", bankStream);
	if (!_midi) {
		error("Player_HE::Player_HE: could not create midi driver");
	}
	if (_midi->open() != 0) {
		error("Player_HE::Player_HE: could not open midi driver");
	}
}

Player_HE::~Player_HE() {
	if (_parser) {
		_parser->stopPlaying();
		delete _parser;
		_parser = NULL;
	}
	if (_midi) {
		_midi->setTimerCallback(0, 0);
		_midi->close();
		delete _midi;
		_midi = NULL;
	}
	if (_bank) {
		free(_bank);
	}
}

void Player_HE::setMusicVolume(int vol) {
	_masterVolume = vol;
	for (int chan = 0; chan < 16; chan++) {
		byte volume = (_channelVolume[chan] * vol) / 256;
		if (_midi)
			_midi->send(0x07b0 | chan | (volume << 16));
	}
}

void Player_HE::onTimer(void *data) {
	Player_HE *player = (Player_HE *)data;
	Common::StackLock lock(player->_mutex);
	if (player->_parser)
		player->_parser->onTimer();
}

void Player_HE::startSoundWithTrackID(int sound, int track) {
	Common::StackLock lock(_mutex);
	byte *ptr = _vm->getResourceAddress(rtSound, sound);
	if (ptr == NULL)
		return;

	if (_parser) {
		_parser->stopPlaying();
		delete _parser;
	}
	_parser = MidiParser::createParser_XMIDI();
	_parser->setMidiDriver(this);
	_parser->loadMusic(ptr + 40, 0);
	_parser->setTrack(track);
	_parser->setTimerRate(_midi->getBaseTempo());
	_midi->setTimerCallback(this, &Player_HE::onTimer);

	_currentMusic = sound;
}

void Player_HE::stopSound(int sound) {
	Common::StackLock lock(_mutex);
	if (!_parser || _currentMusic != sound)
		return;
	_parser->stopPlaying();
	delete _parser;
	_parser = NULL;
}

void Player_HE::stopAllSounds() {
	Common::StackLock lock(_mutex);
	if (!_parser)
		return;
	_parser->stopPlaying();
	delete _parser;
	_parser = NULL;
}

int Player_HE::getSoundStatus(int sound) const {
	Common::StackLock lock(_mutex);
	return (_parser && _currentMusic == sound) ? _parser->isPlaying() : 0;
}

int Player_HE::getMusicTimer() {
	Common::StackLock lock(_mutex);
	return _parser ? _parser->getTick() : 0;
}

void Player_HE::loadAdLibBank() {
	ScummFile file;
	Common::String drvName;
	char entryName[14];
	uint32 tag, entrySize, fileSize;
	Common::String bankName;

	if (_vm->_game.id == GID_PUTTMOON) {
		// Use GM bank
		bankName = "FAT.AD";
	} else {
		// Use MT32-like bank
		bankName = "MIDPAK.AD";
	}

	const char *ptr = strchr(_vm->_filenamePattern.pattern, '.');
	if (ptr) {
		drvName = Common::String(_vm->_filenamePattern.pattern, ptr - _vm->_filenamePattern.pattern + 1);
	} else {
		drvName = _vm->_filenamePattern.pattern;
		drvName += '.';
	}

	drvName += "drv";

	if (!file.open(drvName))
		error("Player_HE::loadAdLibBank(): could not open %s", drvName.c_str());

	uint32 size = (uint32)file.size();

	for (uint32 offset = 0; offset < size;) {
		file.seek(offset, SEEK_SET);
		if (size - offset < 31)
			error("Player_HE::loadAdLibBank(): unexpected end of file");

		tag = file.readUint32BE();
		entrySize = file.readUint32BE();
		if (size - offset < entrySize)
			error("Player_HE::loadAdLibBank(): unexpected end of file");
		fileSize = entrySize - 31;
		file.read(entryName, 13);
		entryName[13] = 0;

		if (tag != MKTAG('F', 'I', 'L', 'E'))
			error("Player_HE::loadAdLibBank(): unknown entry format");

		if (entryName == bankName) {
			_bank = (byte*)malloc(fileSize);
			file.read(_bank, fileSize);
			_bankSize = fileSize;
			return;
		}

		offset += entrySize;
	}
	error("Player_HE::loadAdLibBank(): could not find %s entry", bankName.c_str());
}

int Player_HE::open() {
	if (_midi)
		return _midi->open();
	return 0;
}

bool Player_HE::isOpen() const {
	if (_midi)
		return _midi->isOpen();
	return false;
}

void Player_HE::close() {
	if (_midi)
		_midi->close();
}

void Player_HE::setTimerCallback(void *timerParam, Common::TimerManager::TimerProc timerProc) {
	if (_midi)
		_midi->setTimerCallback(timerParam, timerProc);
}

uint32 Player_HE::getBaseTempo() {
	if (_midi)
		return _midi->getBaseTempo();
	return 0;
}

void Player_HE::send(uint32 b) {
	byte chan = b & 0x0f;
	byte cmd = b & 0xf0;
	byte op1 = (b >> 8) & 0x7f;
	byte op2 = (b >> 16) & 0x7f;
	if (cmd == 0xb0 && op1 == 0x07) {
		_channelVolume[chan] = op2;
		op2 = (op2 * _masterVolume) / 256;
		b = (b & 0xffff) | (op2 << 16);
	}
	if (_midi)
		_midi->send(b);
}

}

#endif