aboutsummaryrefslogtreecommitdiff
path: root/engines/dreamweb/sound.cpp
blob: 4b636d4789cb74eee4c9b2931a94d7c6ea17f664 (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
/* 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 "common/config-manager.h"
#include "common/debug.h"
#include "common/file.h"

#include "dreamweb/dreamweb.h"
#include "dreamweb/sound.h"

namespace DreamWeb {

DreamWebSound::DreamWebSound(DreamWebEngine *vm) : _vm(vm) {
	_vm->_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, ConfMan.getInt("sfx_volume"));
	_vm->_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, ConfMan.getInt("music_volume"));
	_vm->_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, ConfMan.getInt("speech_volume"));

	_channel0 = 0;
	_channel1 = 0;

	_currentSample = 0xff;
	_channel0Playing = 0;
	_channel0Repeat = 0;
	_channel1Playing = 255;

	_volume = 0;
	_volumeTo = 0;
	_volumeDirection = 0;
	_volumeCount = 0;
}

DreamWebSound::~DreamWebSound() {
}

bool DreamWebSound::loadSpeech(byte type1, int idx1, byte type2, int idx2) {
	cancelCh1();

	Common::String name = Common::String::format("%c%02d%c%04d.RAW", type1, idx1, type2, idx2);
	debug(2, "loadSpeech() name:%s", name.c_str());
	return loadSpeech(name);
}

void DreamWebSound::volumeChange(uint8 value, int8 direction) {
	_volumeTo = value;
	_volumeDirection = direction;
}

void DreamWebSound::volumeAdjust() {
	if (_volumeDirection == 0)
		return;
	if (_volume != _volumeTo) {
		_volumeCount += 64;
		// Only modify the volume every 256/64 = 4th time around
		if (_volumeCount == 0)
			_volume += _volumeDirection;
	} else {
		_volumeDirection = 0;
	}
}

void DreamWebSound::playChannel0(uint8 index, uint8 repeat) {
	debug(1, "playChannel0(index:%d, repeat:%d)", index, repeat);
	_channel0Playing = index;
	_channel0Repeat = repeat;
}

void DreamWebSound::playChannel1(uint8 index) {
	debug(1, "playChannel1(index:%d)", index);
	if (_channel1Playing == 7)
		return;

	_channel1Playing = index;
}

void DreamWebSound::cancelCh0() {
	debug(1, "cancelCh0()");
	_channel0Playing = 255;
	_channel0Repeat = 0;
	stopSound(0);
}

void DreamWebSound::cancelCh1() {
	debug(1, "cancelCh1()");
	_channel1Playing = 255;
	stopSound(1);
}

void DreamWebSound::loadRoomsSample(uint8 sample) {
	debug(1, "loadRoomsSample(sample:%d)", sample);

	if (sample == 255 || _currentSample == sample)
		return; // loaded already

	assert(sample < 100);
	Common::String sampleSuffix = Common::String::format("V%02d", sample);
	_currentSample = sample;

	uint8 ch0 = _channel0Playing;
	if (ch0 >= 12 && ch0 != 255)
		cancelCh0();
	uint8 ch1 = _channel1Playing;
	if (ch1 >= 12)
		cancelCh1();
	loadSounds(1, sampleSuffix.c_str());
}

void DreamWebSound::playSound(uint8 channel, uint8 id, uint8 loops) {
	debug(1, "playSound(channel:%u, id:%u, loops:%u)", channel, id, loops);

	int bank = 0;
	bool speech = false;
	Audio::Mixer::SoundType type = channel == 0?
		Audio::Mixer::kMusicSoundType: Audio::Mixer::kSFXSoundType;

	if (id >= 12) {
		id -= 12;
		bank = 1;
		if (id == 50) {
			speech = true;
			type = Audio::Mixer::kSpeechSoundType;
		}
	}
	const SoundData &data = _soundData[bank];

	Audio::SeekableAudioStream *raw;
	if (!speech) {
		if (id >= data.samples.size() || data.samples[id].size == 0) {
			warning("invalid sample #%u played", id);
			return;
		}

		const Sample &sample = data.samples[id];
		uint8 *buffer = (uint8 *)malloc(sample.size);
		if (!buffer)
			error("out of memory: cannot allocate memory for sound(%u bytes)", sample.size);
		memcpy(buffer, data.data.begin() + sample.offset, sample.size);

		raw = Audio::makeRawStream(buffer, sample.size, 22050, Audio::FLAG_UNSIGNED);
	} else {
		uint8 *buffer = (uint8 *)malloc(_speechData.size());
		if (!buffer)
			error("out of memory: cannot allocate memory for sound(%u bytes)", _speechData.size());
		memcpy(buffer, _speechData.begin(), _speechData.size());
		raw = Audio::makeRawStream(buffer, _speechData.size(), 22050, Audio::FLAG_UNSIGNED);
	}

	Audio::AudioStream *stream;
	if (loops > 1) {
		stream = new Audio::LoopingAudioStream(raw, (loops < 255) ? loops : 0);
	} else
		stream = raw;

	if (_vm->_mixer->isSoundHandleActive(_channelHandle[channel]))
		_vm->_mixer->stopHandle(_channelHandle[channel]);
	_vm->_mixer->playStream(type, &_channelHandle[channel], stream);
}

void DreamWebSound::stopSound(uint8 channel) {
	debug(1, "stopSound(%u)", channel);
	assert(channel == 0 || channel == 1);
	_vm->_mixer->stopHandle(_channelHandle[channel]);
	if (channel == 0)
		_channel0 = 0;
	else
		_channel1 = 0;
}

bool DreamWebSound::loadSpeech(const Common::String &filename) {
	if (!_vm->hasSpeech())
		return false;

	Common::File file;
	if (!file.open(_vm->getSpeechDirName() + "/" + filename))
		return false;

	debug(1, "loadSpeech(%s)", filename.c_str());

	uint size = file.size();
	_speechData.resize(size);
	file.read(_speechData.begin(), size);
	file.close();
	return true;
}

void DreamWebSound::soundHandler() {
	_vm->_subtitles = ConfMan.getBool("subtitles");
	volumeAdjust();

	uint volume = _volume;
	//.vol file loaded into soundbuf:0x4000
	//volume table at (volume * 0x100 + 0x3f00)
	//volume value could be from 1 to 7
	//1 - 0x10-0xff
	//2 - 0x1f-0xdf
	//3 - 0x2f-0xd0
	//4 - 0x3e-0xc1
	//5 - 0x4d-0xb2
	//6 - 0x5d-0xa2
	//7 - 0x6f-0x91
	if (volume >= 8)
		volume = 7;
	volume = (8 - volume) * Audio::Mixer::kMaxChannelVolume / 8;
	_vm->_mixer->setChannelVolume(_channelHandle[0], volume);

	uint8 ch0 = _channel0Playing;
	if (ch0 == 255)
		ch0 = 0;
	uint8 ch1 = _channel1Playing;
	if (ch1 == 255)
		ch1 = 0;
	uint8 ch0loop = _channel0Repeat;

	if (_channel0 != ch0) {
		_channel0 = ch0;
		if (ch0) {
			playSound(0, ch0, ch0loop);
		}
	}
	if (_channel1 != ch1) {
		_channel1 = ch1;
		if (ch1) {
			playSound(1, ch1, 1);
		}
	}

	if (!_vm->_mixer->isSoundHandleActive(_channelHandle[0])) {
		_channel0Playing = 255;
		_channel0 = 0;
	}
	if (!_vm->_mixer->isSoundHandleActive(_channelHandle[1])) {
		_channel1Playing = 255;
		_channel1 = 0;
	}
}

void DreamWebSound::loadSounds(uint bank, const Common::String &suffix) {
	Common::String filename = _vm->getDatafilePrefix() + suffix;
	debug(1, "loadSounds(%u, %s)", bank, filename.c_str());
	Common::File file;
	if (!file.open(filename)) {
		warning("cannot open %s", filename.c_str());
		return;
	}

	uint8 header[96];
	file.read(header, sizeof(header));
	uint tablesize = READ_LE_UINT16(header + 50);
	debug(1, "table size = %u", tablesize);

	SoundData &soundData = _soundData[bank];
	soundData.samples.resize(tablesize / 6);
	uint total = 0;
	for (uint i = 0; i < tablesize / 6; ++i) {
		uint8 entry[6];
		Sample &sample = soundData.samples[i];
		file.read(entry, sizeof(entry));
		sample.offset = entry[0] * 16384 + READ_LE_UINT16(entry + 1);
		sample.size = READ_LE_UINT16(entry + 3) * 2048;
		total += sample.size;
		debug(1, "offset: %08x, size: %u", sample.offset, sample.size);
	}
	soundData.data.resize(total);
	file.read(soundData.data.begin(), total);
	file.close();
}

} // End of namespace DreamWeb