File: sound.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (360 lines) | stat: -rw-r--r-- 9,664 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */
#include "audio/decoders/raw.h"
#include "audio/decoders/voc.h"
#include "backends/audiocd/audiocd.h"
#include "common/config-manager.h"
#include "mm/shared/xeen/sound.h"
#include "mm/shared/xeen/sound_driver_adlib.h"
#include "mm/shared/xeen/sound_driver_mt32.h"
#include "mm/xeen/xeen.h"
#include "mm/mm.h"
#ifdef ENABLE_MM1
#include "mm/mm1/mm1.h"
#endif

namespace MM {
namespace Shared {
namespace Xeen {

Sound::Sound(Audio::Mixer *mixer) : _mixer(mixer), _fxOn(true), _musicOn(true), _subtitles(false),
_songData(nullptr), _SoundDriver(nullptr), _effectsData(nullptr), _musicSide(0), _musicPercent(100),
_musicVolume(0), _sfxVolume(0) {
	MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_ADLIB | MDT_MIDI | MDT_PREFER_MT32);
	musicType = MidiDriver::getMusicType(dev);

	switch (musicType) {
	case MT_MT32:
		_SoundDriver = new SoundDriverMT32();
		debugC(1, "Selected mt32 sound driver\n");
		break;
	case MT_ADLIB:
	default:
		_SoundDriver = new SoundDriverAdlib();
		debugC(1, "Selected adlib sound driver\n");
		break;
	}

	if (g_engine->getGameID() != GType_MightAndMagic1
#ifdef ENABLE_MM1
		|| static_cast<MM::MM1::MM1Engine *>(g_engine)->isEnhanced()
#endif
		)
		// Force load effects early so custom instruments for mt32 are loaded before sound is played.
		loadEffectsData();

	assert(_SoundDriver);

	if (g_engine->getIsCD())
		g_system->getAudioCDManager()->open();
}

Sound::~Sound() {
	stopAllAudio();
	if (g_engine->getIsCD())
		g_system->getAudioCDManager()->close();

	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::Path &name, int unused) {
	File f;
	if (!f.open(name))
		error("Could not open sound - %s", name.toString().c_str());

	playSound(f);
}

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

	playSound(f);
}
#endif

#ifdef ENABLE_XEEN
void Sound::playVoice(const Common::Path &name, int ccMode) {
#else
void Sound::playVoice(const Common::Path &name) {
#endif
	stopSound();
	if (!_fxOn)
		return;
	File f;
#ifdef ENABLE_XEEN
	bool result = (ccMode == -1) ? f.open(name) : f.open(name, ccMode);
#else
	bool result = f.open(name);
#endif

	if (!result)
		error("Could not open sound - %s", name.toString().c_str());
	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(true);
	stopSound();
	setMusicPercent(100);
}

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

	g_engine->syncSoundSettings();
}

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

	// Skip if the sound driver hasn't been loaded, or effects have already been loaded
	if (!_SoundDriver || _effectsData)
		return;

	if (musicType == MT_ADLIB) {
		// Load in an entire driver so we have quick access to the effects data that's hardcoded within it

		const char *name = "blastmus";
		File file(name);
		size_t size = file.size();
		byte *effectsData = new byte[size];

		if (file.read(effectsData, size) != size) {
			delete[] effectsData;
			error("Failed to read %zu bytes from '%s'", size, name);
		}

		_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]);
	} else if (musicType == MT_MT32) {
		// Load in an entire driver so we have quick access to the effects data that's hardcoded within it
		const char *name = "rolmus";
		File file(name);
		size_t size = file.size();
		byte *effectsData = new byte[size];

		if (file.read(effectsData, size) != size) {
			delete[] effectsData;
			error("Failed to read %zu bytes from '%s'", size, name);
		}

		_effectsData = effectsData;
		// Locate the playFX routine
		const byte *fx = effectsData + READ_LE_UINT16(effectsData + 10) + 12;
		assert(READ_BE_UINT16(fx + 36) == 0x81FB);
		// TODO: Investigate, additional 10 effects seem to exist in the table beyond the base 180. Not unique to rolmus as at least admus, blastmus and canmus have them too.
		uint numEffects = READ_LE_UINT16(fx + 38);

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

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

		// rolmus in intro.cc
		if (effectsData[1] == 0xBD) {
			_patchesOffsetsMT32 = {0x0A86, 0x0ABC, 0x10A3, 0x0BC1, 0x10AF, 0x0CBB, 0x10BB, 0x0DB5, 0x10C7, 0x0EAF, 0x10D3, 0x0FA9, 0x10DF};
			debugC(3, "intro rolmus");
		// rolmus in xeen.cc
		} else if (effectsData[1] == 0xB9) {
			_patchesOffsetsMT32 = {0x09A7, 0x09DD, 0x0FC4, 0x0AE2, 0x0FD0, 0x0BDC, 0x0FDC, 0x0CD6, 0x0FE8, 0x0DD0, 0x0FF4, 0x0ECA, 0x1000};
			debugC(3, "xeen rolmus");
		}

		assert(_patchesOffsetsMT32.size() == 13);

		for (uint idx = 0; idx < 13; idx++) {
			const byte *ptr = &effectsData[_patchesOffsetsMT32[idx]];

			_SoundDriver->sysExMessage(ptr);
		}
	}
}

void Sound::playFX(uint effectId) {
	stopFX();
	if (!_fxOn)
		return;
	loadEffectsData();

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

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

int Sound::songCommand(uint commandId, byte musicVolume, byte sfxVolume) {
	int result = 0;

	if (_SoundDriver)
		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;

	if (!stream.seek(0))
		error("Failed to seek to 0 for song data");

	size_t size = stream.size();
	byte *songData = new byte[size];

	if (stream.read(songData, size) != size) {
		delete[] songData;
		error("Failed to read %zu bytes of song data", size);
	}

	assert(!_songData);
	_songData = songData;

	if (_SoundDriver)
		_SoundDriver->playSong(_songData);
}

void Sound::playSong(const Common::Path &name, int param) {
	if (isMusicPlaying() && name == _currentMusic)
		return;
	_currentMusic = name;

	Common::File mf;
	if (mf.open(name)) {
		playSong(mf);
#ifdef ENABLE_XEEN
	} else if (dynamic_cast<MM::Xeen::XeenEngine *>(g_engine)) {
		File f(name, _musicSide);
		playSong(f);
#endif
	} else {
		File f(name);
		playSong(f);
	}
}

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

	g_engine->syncSoundSettings();
}

bool Sound::isMusicPlaying() const {
	return _SoundDriver && _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();
	else if (!_currentMusic.empty())
		playSong(_currentMusic);

	_subtitles = ConfMan.hasKey("subtitles") ? ConfMan.getBool("subtitles") : true;
	if (ConfMan.getBool("mute")) {
		_musicVolume = 0;
		_sfxVolume = 0;
	} else {
		_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);
}

} // namespace Xeen
} // namespace Shared
} // namespace MM