aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/audio.cpp
blob: 8f33f2224318c3935fa9358103667d9fefc1b9e6 (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
/* 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 "mads/audio.h"
#include "mads/compression.h"

#include "common/stream.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/raw.h"

namespace MADS {

AudioPlayer::AudioPlayer(Audio::Mixer *mixer, uint32 gameID) : _mixer(mixer), _gameID(gameID) {
	setVolume(Audio::Mixer::kMaxChannelVolume);
	setDefaultSoundGroup();
}

AudioPlayer::~AudioPlayer() {
	_dsrEntries.clear();
	_filename = "";
}

bool AudioPlayer::isPlaying() const {
	return _mixer->isSoundHandleActive(_handle);
}

void AudioPlayer::setVolume(int volume) {
	_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume);
	_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, volume);
}

void AudioPlayer::setDefaultSoundGroup() {
	switch (_gameID) {
	case GType_RexNebular:
		setSoundGroup("rex009.dsr");
		break;
	case GType_Dragonsphere:
		setSoundGroup("drag009.dsr");
		break;
	case GType_Phantom:
		setSoundGroup("phan009.dsr");
		break;
	default:
		error("setDefaultSoundGroup: Unknown game");
	}
}

void AudioPlayer::setSoundGroup(const Common::String &filename) {
	if (_filename != filename) {
		_dsrEntries.clear();

		_filename = filename;
		_dsrFile.open(filename);

		// Read header
		uint16 entryCount = _dsrFile.readUint16LE();

		for (uint16 i = 0; i < entryCount; i++) {
			DSREntry newEntry;
			newEntry.frequency = _dsrFile.readUint16LE();
			newEntry.channels = _dsrFile.readUint32LE();
			newEntry.compSize = _dsrFile.readUint32LE();
			newEntry.uncompSize = _dsrFile.readUint32LE();
			newEntry.offset = _dsrFile.readUint32LE();
			_dsrEntries.push_back(newEntry);
		}

		_dsrFile.close();
	}
}

void AudioPlayer::playSound(int soundIndex, bool loop) {
	if (_dsrEntries.empty()) {
		warning("DSR file not loaded, not playing sound");
		return;
	}

	if (soundIndex < 0 || soundIndex > (int)_dsrEntries.size() - 1) {
		warning("Invalid sound index: %i (max %i), not playing sound", soundIndex, _dsrEntries.size() - 1);
		return;
	}

	// Get sound data
	FabDecompressor fab;
	int32 compSize = _dsrEntries[soundIndex].compSize;
	int32 uncompSize = _dsrEntries[soundIndex].uncompSize;
	int32 offset = _dsrEntries[soundIndex].offset;
	int16 frequency = _dsrEntries[soundIndex].frequency;
	byte *compData = new byte[compSize];
	byte *buffer = new byte[uncompSize];
	_dsrFile.open(_filename);
	_dsrFile.seek(offset, SEEK_SET);
	_dsrFile.read(compData, compSize);
	_dsrFile.close();

	fab.decompress(compData, compSize, buffer, uncompSize);

	// Play sound
	Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
				Audio::makeRawStream(buffer, uncompSize, frequency, Audio::FLAG_UNSIGNED),
				loop ? 0 : 1);
	_mixer->playStream(Audio::Mixer::kSFXSoundType, &_handle, stream, -1,  Audio::Mixer::kMaxChannelVolume);

	/*
	// Dump the sound file
	FILE *destFile = fopen("sound.raw", "wb");
	fwrite(_dsrFile.dsrEntries[soundIndex]->data, _dsrFile.dsrEntries[soundIndex].uncompSize, 1, destFile);
	fclose(destFile);
	*/
}

void AudioPlayer::stop() {
	_mixer->stopHandle(_handle);
}

} // End of namespace M4