aboutsummaryrefslogtreecommitdiff
path: root/engines/queen/sound.cpp
blob: 917351f3dab9b63933529b15698a76bfafc7672c (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"
#include "common/endian.h"

#include "queen/sound.h"

#include "queen/input.h"
#include "queen/music.h"
#include "queen/queen.h"
#include "queen/resource.h"

#include "sound/flac.h"
#include "sound/mp3.h"
#include "sound/vorbis.h"

#define	SB_HEADER_SIZE_V104 110
#define	SB_HEADER_SIZE_V110 122
#define	STOP_MUSIC	-1

namespace Queen {

class SilentSound : public Sound {
public:
	SilentSound(Audio::Mixer *mixer, QueenEngine *vm) : Sound(mixer, vm) {}
protected:
	void playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle) {
		// Do nothing
	}
};

class SBSound : public Sound {
public:
	SBSound(Audio::Mixer *mixer, QueenEngine *vm) : Sound(mixer, vm) {}
protected:
	void playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle);
};

#ifdef USE_MAD
class MP3Sound : public Sound {
public:
	MP3Sound(Audio::Mixer *mixer, QueenEngine *vm) : Sound(mixer, vm) {}
protected:
	void playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle) {
		_mixer->playInputStream(Audio::Mixer::kSFXSoundType, soundHandle, Audio::makeMP3Stream(f, size));
	}
};
#endif

#ifdef USE_VORBIS
class OGGSound : public Sound {
public:
	OGGSound(Audio::Mixer *mixer, QueenEngine *vm) : Sound(mixer, vm) {}
protected:
	void playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle) {
		_mixer->playInputStream(Audio::Mixer::kSFXSoundType, soundHandle, Audio::makeVorbisStream(f, size));
	}
};
#endif

#ifdef USE_FLAC
class FLACSound : public Sound {
public:
	FLACSound(Audio::Mixer *mixer, QueenEngine *vm) : Sound(mixer, vm) {};
protected:
	void playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle) {
		_mixer->playInputStream(Audio::Mixer::kSFXSoundType, soundHandle, Audio::makeFlacStream(f, size));
	}
};
#endif // #ifdef USE_FLAC

Sound::Sound(Audio::Mixer *mixer, QueenEngine *vm) :
	_mixer(mixer), _vm(vm), _sfxToggle(true), _speechToggle(true), _musicToggle(true), _lastOverride(0) {
#ifdef ENABLE_AMIGA_MUSIC
	_lastModuleOverride = -1;
#endif
}

Sound *Sound::makeSoundInstance(Audio::Mixer *mixer, QueenEngine *vm, uint8 compression) {
	if (!mixer->isReady())
		return new SilentSound(mixer, vm);

	switch (compression) {
	case COMPRESSION_NONE:
		return new SBSound(mixer, vm);
	case COMPRESSION_MP3:
#ifndef USE_MAD
		warning("Using MP3 compressed datafile, but MP3 support not compiled in");
		return new SilentSound(mixer, vm);
#else
		return new MP3Sound(mixer, vm);
#endif
	case COMPRESSION_OGG:
#ifndef USE_VORBIS
		warning("Using OGG compressed datafile, but OGG support not compiled in");
		return new SilentSound(mixer, vm);
#else
		return new OGGSound(mixer, vm);
#endif
	case COMPRESSION_FLAC:
#ifndef USE_FLAC
		warning("Using FLAC compressed datafile, but FLAC support not compiled in");
		return new SilentSound(mixer, vm);
#else
		return new FLACSound(mixer, vm);
#endif
	default:
		warning("Unknown compression type");
		return new SilentSound(mixer, vm);
	}
}

void Sound::waitFinished(bool isSpeech) {
	while (_mixer->isSoundHandleActive(isSpeech ? _speechHandle : _sfxHandle))
		_vm->input()->delay(10);
}

void Sound::playSfx(uint16 sfx) {
	if (sfxOn() && sfx != 0) {
#ifndef PALMOS_68K
		playSound(_sfxName[sfx - 1], false);
#else
		playSound(_sfxName + 10 * (sfx - 1), false);	// saved as 8char + /0/0
#endif
	}
}

void Sound::playSpeech(const char *base) {
	if (speechOn()) {
		playSound(base, true);
	}
}

void Sound::playSound(const char *base, bool isSpeech) {
	char name[13];
	strcpy(name, base);
	// alter filename to add zeros and append ".SB"
	for (int i = 0; i < 8; i++) {
		if (name[i] == ' ')
			name[i] = '0';
	}
	strcat(name, ".SB");
	waitFinished(isSpeech);
	uint32 size;
	Common::File *f = _vm->resource()->findSound(name, &size);
	if (f) {
		playSoundData(f, size, isSpeech ? &_speechHandle : &_sfxHandle);
		_speechSfxExists = isSpeech;
	} else {
		_speechSfxExists = false;
	}
}

#ifdef ENABLE_AMIGA_MUSIC

static struct {
	const char *name;
	uint8 songNum;
	uint8 remapSongNumTable[6];
} amigaMusicData[] = {
	{ "HOTEL",    1, {   1,   2,  39,   0 } },
	{ "HOTEL",    2, {  20,  30,  34,   0 } },
	{ "HOTEL",    3, {  19,   0 } },
	{ "HOTEL",    4, {  29,  35,  36,   0 } },
	{ "JUNG",     1, {  40,   0 } },
	{ "JUNG",     2, {   3,  38,  89,   0 } },
	{ "TEMPLE",   1, {  54,   0 } },
	{ "TEMPLE",   2, {  12,   0 } },
	{ "TEMPLE",   3, {   7,   9,  10,  11, 0 } },
	{ "TEMPLE",   4, {  31,   0 } },
	{ "FLODA",    1, {  16,   0 } },
	{ "FLODA",    2, {  17,   0 } },
	{ "FLODA",    3, {  13,   0 } },
	{ "FLODA",    4, {  41,   0 } },
	{ "FLODA",    5, {  30,  43,   0 } },
	{ "TITLE",    1, {  67,  88, 203,   0 } },
	{ "AWESTRUK", 1, {  37,  52,  90, 196, 0 } },
	{ "'JUNGLE'", 1, {  91,   0 } },
	{ "FRANK",    1, {  46,   0 } },
	{ "BOB",      1, {   6,   0 } },
	{ "AZURA",    1, {  44,  53, 204, 0 } },
	{ "FORT",     1, {  21,   0 } },
	{ "ROCKET",   1, {  32, 194, 195, 0 } },
	{ "ROBOT",    1, {  92,   0 } }
};

void Sound::playSong(int16 songNum) {
	debug(2, "Sound::playSong %d override %d/%d", songNum, _lastModuleOverride, _lastOverride);

	const char *moduleName = 0;
	for (int i = 0; i < ARRAYSIZE(amigaMusicData) && !moduleName; ++i) {
		for (int j = 0; amigaMusicData[i].remapSongNumTable[j] != 0; ++j) {
			if (amigaMusicData[i].remapSongNumTable[j] == songNum) {
				moduleName = amigaMusicData[i].name;
				songNum = amigaMusicData[i].songNum;

				if (_lastModuleOverride == i && _lastOverride == songNum)
					return;

				_lastModuleOverride = i;
				_lastOverride = songNum;
				break;
			}
		}
	}
	if (!moduleName)
		return;

	_mixer->stopHandle(_musicHandle);

	debug(1, "playAmigaSong name '%s' subsong %d", moduleName, songNum);

	char buf[16];
	sprintf(buf, "%s.SNG", moduleName);
	Common::File fsng;
	if (!fsng.open(buf))
		return;

	sprintf(buf, "%s.INS", moduleName);
	Common::File fins;
	if (!fins.open(buf))
		return;

	Audio::AudioStream *rjp1Stream = Audio::makeRjp1Stream(&fsng, &fins, songNum);
	if (rjp1Stream) {
		_mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_musicHandle, rjp1Stream);
	}
}

#else

void Sound::playSong(int16 songNum) {
	if (songNum <= 0) {
		_vm->music()->stopSong();
		return;
	}

	int16 newTune;
	if (_vm->resource()->isDemo()) {
		if (songNum == 17) {
			_vm->music()->stopSong();
			return;
		}
		newTune = _songDemo[songNum - 1].tuneList[0] - 1;
	} else {
		newTune = _song[songNum - 1].tuneList[0] - 1;
	}

	if (_tune[newTune].sfx[0]) {
		playSfx(_tune[newTune].sfx[0]);
		return;
	}

	if (!musicOn())
		return;

	int override = (_vm->resource()->isDemo()) ? _songDemo[songNum - 1].override : _song[songNum - 1].override;
	switch (override) {
	// Override all songs
	case  1:
		break;
	// Alter song settings (such as volume) and exit
	case  2:
		_vm->music()->toggleVChange();
	default:
		return;
	}

	_lastOverride = songNum;

	_vm->music()->queueTuneList(newTune);
	_vm->music()->playMusic();
}

#endif // ENABLE_AMIGA_MUSIC

void Sound::saveState(byte *&ptr) {
	WRITE_BE_UINT16(ptr, _lastOverride); ptr += 2;
}

void Sound::loadState(uint32 ver, byte *&ptr) {
	_lastOverride = (int16)READ_BE_INT16(ptr); ptr += 2;
}

void SBSound::playSoundData(Common::File *f, uint32 size, Audio::SoundHandle *soundHandle) {
	int headerSize;
	f->seek(2, SEEK_CUR);
	uint16 version = f->readUint16LE();
	switch (version) {
	case 104:
		headerSize = SB_HEADER_SIZE_V104;
		break;
	case 110:
		headerSize = SB_HEADER_SIZE_V110;
		break;
	default:
		warning("Unhandled SB file version %d, defaulting to 104\n", version);
		headerSize = SB_HEADER_SIZE_V104;
		break;
	}
	f->seek(headerSize - 4, SEEK_CUR);
	size -= headerSize;
	uint8 *sound = (uint8 *)malloc(size);
	if (sound) {
		f->read(sound, size);
		byte flags = Audio::Mixer::FLAG_UNSIGNED | Audio::Mixer::FLAG_AUTOFREE;
		_mixer->playRaw(Audio::Mixer::kSFXSoundType, soundHandle, sound, size, 11025, flags);
	}
}

} //End of namespace Queen