aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/sound.cpp
blob: ae70c1f5d8f3ad615cf8bdeec92665763e1c00e7 (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
/* 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 "audio/decoders/raw.h"
#include "audio/decoders/voc.h"
#include "common/config-manager.h"
#include "xeen/sound.h"
#include "xeen/xeen.h"

namespace Xeen {

Sound::Sound(Audio::Mixer *mixer) : _mixer(mixer), _fxOn(true), _musicOn(true), _subtitles(false),
		_songData(nullptr), _effectsData(nullptr), _musicSide(0), _musicPercent(100),
		_musicVolume(0), _sfxVolume(0) {
	_SoundDriver = new AdlibSoundDriver();
}

Sound::~Sound() {
	stopAllAudio();

	delete _SoundDriver;
	delete[] _effectsData;
	delete[] _songData;
}

void Sound::playSound(Common::SeekableReadStream &s, int unused) {
	stopSound();
	if (!_fxOn)
		return;

	s.seek(0);
	Common::SeekableReadStream *srcStream = s.readStream(s.size());
	Audio::SeekableAudioStream *stream = Audio::makeVOCStream(srcStream,
		Audio::FLAG_UNSIGNED, DisposeAfterUse::YES);
	_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, stream);
}

void Sound::playSound(const Common::String &name, int unused) {
	File f;
	if (!f.open(name))
		error("Could not open sound - %s", name.c_str());

	playSound(f);
}

void Sound::playSound(const Common::String &name, int ccNum, int unused) {
	File f;
	if (!f.open(name, ccNum))
		error("Could not open sound - %s", name.c_str());

	playSound(f);
}

void Sound::playVoice(const Common::String &name, int ccMode) {
	File f;
	bool result = (ccMode == -1) ? f.open(name) : f.open(name, ccMode);
	if (!result)
		error("Could not open sound - %s", name.c_str());

	stopSound();

	Common::SeekableReadStream *srcStream = f.readStream(f.size());
	Audio::SeekableAudioStream *stream = Audio::makeVOCStream(srcStream,
		Audio::FLAG_UNSIGNED, DisposeAfterUse::YES);
	_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_soundHandle, stream);
}

void Sound::stopSound() {
	_mixer->stopHandle(_soundHandle);
}

bool Sound::isSoundPlaying() const {
	return _mixer->isSoundHandleActive(_soundHandle);
}

void Sound::stopAllAudio() {
	stopSong();
	stopFX();
	stopSound();
	setMusicPercent(100);
}

void Sound::setFxOn(bool isOn) {
	ConfMan.setBool("sfx_mute", !isOn);
	if (isOn)
		ConfMan.setBool("mute", false);

	g_vm->syncSoundSettings();
}

void Sound::loadEffectsData() {
	// Stop any prior FX
	stopFX();

	if (!_effectsData) {
		// Load in an entire driver so we have quick access to the effects data that's hardcoded within it
		File file("blastmus");
		byte *effectsData = new byte[file.size()];
		file.seek(0);
		file.read(effectsData, file.size());
		file.close();
		_effectsData = effectsData;

		// Locate the playFX routine
		const byte *fx = effectsData + READ_LE_UINT16(effectsData + 10) + 12;
		assert(READ_BE_UINT16(fx + 28) == 0x81FB);
		uint numEffects = READ_LE_UINT16(fx + 30);

		assert(READ_BE_UINT16(fx + 36) == 0x8B87);
		const byte *table = effectsData + READ_LE_UINT16(fx + 38);

		// Extract the effects offsets
		_effectsOffsets.resize(numEffects);
		for (uint idx = 0; idx < numEffects; ++idx)
			_effectsOffsets[idx] = READ_LE_UINT16(&table[idx * 2]);
	}
}

void Sound::playFX(uint effectId) {
	stopFX();
	loadEffectsData();

	if (effectId < _effectsOffsets.size()) {
		const byte *dataP = &_effectsData[_effectsOffsets[effectId]];
		_SoundDriver->playFX(effectId, dataP);
	}
}

void Sound::stopFX() {
	_SoundDriver->stopFX();
}

int Sound::songCommand(uint commandId, byte musicVolume, byte sfxVolume) {
	int result = _SoundDriver->songCommand(commandId, musicVolume, sfxVolume);
	if (commandId == STOP_SONG) {
		delete[] _songData;
		_songData = nullptr;
	}

	return result;
}

void Sound::playSong(Common::SeekableReadStream &stream) {
	stopSong();
	if (!_musicOn)
		return;

	byte *songData = new byte[stream.size()];
	stream.seek(0);
	stream.read(songData, stream.size());
	_songData = songData;

	_SoundDriver->playSong(_songData);
}

void Sound::playSong(const Common::String &name, int param) {
	_priorMusic = _currentMusic;
	_currentMusic = name;

	Common::File mf;
	if (mf.open(name)) {
		playSong(mf);
	} else {
		File f(name, _musicSide);
		playSong(f);
	}
}

void Sound::setMusicOn(bool isOn) {
	ConfMan.setBool("music_mute", !isOn);
	if (isOn)
		ConfMan.setBool("mute", false);

	g_vm->syncSoundSettings();
}

bool Sound::isMusicPlaying() const {
	return _SoundDriver->isPlaying();
}

void Sound::setMusicPercent(byte percent) {
	assert(percent <= 100);
	_musicPercent = percent;

	updateVolume();
}

void Sound::updateSoundSettings() {
	_fxOn = !ConfMan.getBool("sfx_mute");
	if (!_fxOn)
		stopFX();

	_musicOn = !ConfMan.getBool("music_mute");
	if (!_musicOn)
		stopSong();

	_subtitles = ConfMan.hasKey("subtitles") ? ConfMan.getBool("subtitles") : true;
	_musicVolume = CLIP(ConfMan.getInt("music_volume"), 0, 255);
	_sfxVolume = CLIP(ConfMan.getInt("sfx_volume"), 0, 255);
	updateVolume();
}

void Sound::updateVolume() {
	songCommand(SET_VOLUME, _musicPercent * _musicVolume / 100, _sfxVolume);
}

} // End of namespace Xeen