aboutsummaryrefslogtreecommitdiff
path: root/queen/sound.cpp
blob: 791dc2cbf67fb4d304ffd73f6ea9a379691da3cc (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003-2004 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 "queen/sound.h"

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

#define	SB_HEADER_SIZE	110
#define	STOP_MUSIC	-1

namespace Queen {

Sound::Sound(SoundMixer *mixer, QueenEngine *vm) : 
	_mixer(mixer), _vm(vm), _sfxToggle(true), _speechToggle(true), _musicToggle(true), _lastOverride(0), _currentSong(0) {
}

Sound::~Sound() {
}

Sound *Sound::giveSound(SoundMixer *mixer, QueenEngine *vm, uint8 compression) {
	switch(compression) {
		case COMPRESSION_NONE:
				return new SBSound(mixer, vm);
				break;
		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
				break;
		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
				break;
		default:
				warning("Unknown compression type");
				return new SilentSound(mixer, vm);
	}
}

void Sound::waitSfxFinished() {
	while(_sfxHandle.isActive())
		_vm->input()->delay(10);
}

void Sound::playSfx(uint16 sfx) {
	if (sfx != 0) {
		char name[13];
		strcpy(name, _sfxName[sfx - 1]);
		strcat(name, ".SB");
		sfxPlay(name);
	}
}

void Sound::playSfx(const char *base) {
	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");
	sfxPlay(name);
}

void Sound::playSong(int16 songNum) {
	if (songNum <= 0) {
		_vm->music()->stopSong();
		return;
	}
	
	int16 newTune = _song[songNum - 1].tuneList[0] - 1;

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

	if (!musicOn() || _vm->resource()->isDemo())
		return;

	switch (_song[songNum - 1].override) {
		// Override all songs
		case  1:
			break;
		// Alter song settings (such as volume) and exit
		case  2:
		default:
			return;
			break;
	}
	
	_lastOverride = songNum;
	
	switch (_tune[newTune].mode) {
		//Random loop
		case  0:
			warning("Music: Random loop not yet supported (doing sequential loop instead)");
		//Sequential loop
		case  1:
			_vm->music()->loop(true);
			break;
		//Play once
		case  2:
		default:
			_vm->music()->loop(false);
			break;
	}

	int16 song = _tune[newTune].tuneNum[0] - 1;

	// Work around bug in Roland music, note that these numbers are 'one-off' from
	// the original code.
	if (/*isRoland && */ song == 88 || song == 89) {
		warning("Working around Roland music bug");
		song = 62;
	}
	
	_vm->music()->playSong(song);
}


void SBSound::playSound(byte *sound, uint32 size) {
	byte flags = SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE;
	_mixer->playRaw(&_sfxHandle, sound, size, 11025, flags);
}

void SBSound::sfxPlay(const char *name) {
	waitSfxFinished();	
	if (_vm->resource()->fileExists(name)) 
		playSound(_vm->resource()->loadFileMalloc(name, SB_HEADER_SIZE), _vm->resource()->fileSize(name) - SB_HEADER_SIZE);
}

#ifdef USE_MAD
void MP3Sound::sfxPlay(const char *name) {
	waitSfxFinished();
	if (_vm->resource()->fileExists(name)) 
		_mixer->playMP3(&_sfxHandle, _vm->resource()->giveCompressedSound(name), _vm->resource()->fileSize(name));
}
#endif

#ifdef USE_VORBIS
void OGGSound::sfxPlay(const char *name) {
	waitSfxFinished();	
	if (_vm->resource()->fileExists(name))
		_mixer->playVorbis(&_sfxHandle, _vm->resource()->giveCompressedSound(name), _vm->resource()->fileSize(name));
}
#endif

} //End of namespace Queen