1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifdef ENABLE_EOB
24 
25 #include "kyra/sound/sound_intern.h"
26 #include "kyra/resource/resource.h"
27 
28 #include "common/config-manager.h"
29 
30 #include "backends/audiocd/audiocd.h"
31 
32 #include "audio/audiostream.h"
33 #include "audio/decoders/raw.h"
34 
35 namespace Kyra {
36 
SoundTowns_Darkmoon(KyraEngine_v1 * vm,Audio::Mixer * mixer)37 SoundTowns_Darkmoon::SoundTowns_Darkmoon(KyraEngine_v1 *vm, Audio::Mixer *mixer) : Sound(vm, mixer) {
38 	_intf = new TownsAudioInterface(mixer, this);
39 	_pcmData = 0;
40 	_pcmVol = 0;
41 	_timer = 0;
42 	_timerSwitch = 0;
43 	memset(_resource, 0, sizeof(_resource));
44 }
45 
~SoundTowns_Darkmoon()46 SoundTowns_Darkmoon::~SoundTowns_Darkmoon() {
47 	for (int i = 0; i < 3; i++)
48 		initAudioResourceInfo(i, 0);
49 	delete _intf;
50 	delete[] _pcmData;
51 }
52 
init()53 bool SoundTowns_Darkmoon::init() {
54 	if (!_intf->init())
55 		return false;
56 
57 	_intf->callback(21, 255, 1);
58 	_intf->callback(21, 0, 1);
59 	_intf->callback(22, 255, 221);
60 
61 	_intf->callback(70, 0x31);
62 	_intf->callback(33, 1);
63 	_intf->callback(8, 0x47, 127);
64 	_intf->callback(67, 1, 127, 127);
65 
66 	_intf->setSoundEffectChanMask(-1);
67 
68 	_lastSfxChan = 0x46;
69 	_lastEnvChan = 0x40;
70 
71 	updateVolumeSettings();
72 
73 	return true;
74 }
75 
timerCallback(int timerId)76 void SoundTowns_Darkmoon::timerCallback(int timerId) {
77 	switch (timerId) {
78 	case 1:
79 		_timerSwitch = (_timerSwitch + 1) % 4;
80 		if (!_timerSwitch)
81 			_timer++;
82 		break;
83 	default:
84 		break;
85 	}
86 }
87 
initAudioResourceInfo(int set,void * info)88 void SoundTowns_Darkmoon::initAudioResourceInfo(int set, void *info) {
89 	delete _resource[set];
90 	_resource[set] = info ? new SoundResourceInfo_TownsEoB(*(SoundResourceInfo_TownsEoB*)info) : 0;
91 }
92 
selectAudioResourceSet(int set)93 void SoundTowns_Darkmoon::selectAudioResourceSet(int set) {
94 	delete[] _pcmData;
95 
96 	if (!_resource[set] || !_resource[kMusicIngame])
97 		return;
98 
99 	_fileList = _resource[set]->fileList;
100 	_fileListLen = _resource[set]->numFiles;
101 
102 	_pcmDataSize = _resource[kMusicIngame]->pcmDataSize;
103 	_pcmData = new uint8[_pcmDataSize];
104 	_pcmVol = _resource[set]->pcmVolume;
105 	memcpy(_pcmData, _resource[kMusicIngame]->pcmData, _pcmDataSize);
106 
107 	if (set == kMusicIngame)
108 		return;
109 
110 	memcpy(_pcmData, _resource[set]->pcmData, _resource[set]->pcmDataSize);
111 }
112 
hasSoundFile(uint file) const113 bool SoundTowns_Darkmoon::hasSoundFile(uint file) const {
114 	return true;
115 }
116 
loadSoundFile(uint file)117 void SoundTowns_Darkmoon::loadSoundFile(uint file) {
118 	if (file < _fileListLen)
119 		loadSoundFile(_fileList[file]);
120 }
121 
loadSoundFile(Common::String name)122 void SoundTowns_Darkmoon::loadSoundFile(Common::String name) {
123 	Common::SeekableReadStream *s = _vm->resource()->createReadStream(Common::String::format("%s.SDT", name.c_str()));
124 	if (!s)
125 		error("Failed to load sound file '%s.SDT'", name.c_str());
126 
127 	for (int i = 0; i < 120; i++) {
128 		_soundTable[i].type = s->readSByte();
129 		_soundTable[i].para1 = s->readSint32LE();
130 		_soundTable[i].para2 = s->readSint16LE();
131 	}
132 
133 	delete s;
134 
135 	uint32 bytesLeft;
136 	uint8 *pmb = _vm->resource()->fileData(Common::String::format("%s.PMB", name.c_str()).c_str(), &bytesLeft);
137 
138 	_vm->delay(300);
139 
140 	if (pmb) {
141 		uint8 *src = pmb + 8;
142 		for (int i = 0; i < 32; i++)
143 			_intf->callback(5, 0x40, i, &src[i << 7]);
144 
145 		_intf->callback(35, -1);
146 		src += 0x1000;
147 		bytesLeft -= 0x1008;
148 
149 		while (bytesLeft) {
150 			_intf->callback(34, src);
151 			uint32 len = READ_LE_UINT16(&src[12]) + 32;
152 			src = src + len;
153 			bytesLeft -= len;
154 		}
155 
156 		delete[] pmb;
157 	} else {
158 		warning("Sound file '%s.PMB' not found.", name.c_str());
159 		// TODO
160 	}
161 }
162 
playTrack(uint8 track)163 void SoundTowns_Darkmoon::playTrack(uint8 track) {
164 	if (track >= 120 || !_sfxEnabled)
165 		return;
166 
167 	uint8 *pcm = 0;
168 
169 	switch (_soundTable[track].type) {
170 	case -1:
171 		if (track == 0)
172 			haltTrack();
173 		else if (track == 2)
174 			beginFadeOut();
175 		break;
176 
177 	case 0:
178 		if (_soundTable[track].para1 == -1 || (uint32)_soundTable[track].para1 > _pcmDataSize)
179 			return;
180 
181 		pcm = _pcmData + _soundTable[track].para1;
182 		WRITE_LE_UINT16(&pcm[24], _soundTable[track].para2 * 98 / 1000);
183 
184 		_intf->callback(39, 0x47);
185 		_intf->callback(37, 0x47, 60, track == 11 ? 127 : _pcmVol, pcm);
186 		break;
187 
188 	case 2:
189 		resetTrigger();
190 		g_system->getAudioCDManager()->play(_soundTable[track].para1 - 1, 1, 0, 0);
191 		break;
192 
193 	case 3:
194 		_lastSfxChan ^= 3;
195 		_intf->callback(39, _lastSfxChan);
196 		_intf->callback(4, _lastSfxChan, _soundTable[track].para1);
197 		_intf->callback(1, _lastSfxChan, _soundTable[track].para2, 127);
198 		break;
199 
200 	default:
201 		break;
202 	}
203 }
204 
haltTrack()205 void SoundTowns_Darkmoon::haltTrack() {
206 	_intf->callback(39, 0x47);
207 	_intf->callback(39, 0x46);
208 	_intf->callback(39, 0x45);
209 
210 	g_system->getAudioCDManager()->stop();
211 }
212 
isPlaying() const213 bool SoundTowns_Darkmoon::isPlaying() const {
214 	return g_system->getAudioCDManager()->isPlaying();
215 }
216 
playSoundEffect(uint16 track,uint8 volume)217 void SoundTowns_Darkmoon::playSoundEffect(uint16 track, uint8 volume) {
218 	if (!_sfxEnabled)
219 		return;
220 
221 	if (volume == 255)
222 		return playTrack(track);
223 
224 	uint8 *pcm = 0;
225 
226 	switch (_soundTable[track].type) {
227 	case 0:
228 		if (_soundTable[track].para1 == -1 || (uint32)_soundTable[track].para1 > _pcmDataSize)
229 			return;
230 
231 		pcm = _pcmData + _soundTable[track].para1;
232 		WRITE_LE_UINT16(&pcm[24], _soundTable[track].para2 * 98 / 1000);
233 
234 		_intf->callback(39, 0x47);
235 		_intf->callback(37, 0x47, 60, volume, pcm);
236 		break;
237 
238 	case 3:
239 		_intf->callback(2, _lastEnvChan);
240 		_intf->callback(4, _lastEnvChan, _soundTable[track].para1);
241 		_intf->callback(1, _lastEnvChan, _soundTable[track].para2, volume);
242 		break;
243 
244 	default:
245 		break;
246 	}
247 
248 	if (++_lastEnvChan == 0x43)
249 		_lastEnvChan = 0x40;
250 }
251 
stopAllSoundEffects()252 void SoundTowns_Darkmoon::stopAllSoundEffects() {
253 	_intf->callback(39, 0x42);
254 	_intf->callback(39, 0x41);
255 	_intf->callback(39, 0x40);
256 }
257 
beginFadeOut()258 void SoundTowns_Darkmoon::beginFadeOut() {
259 	for (int vol = 127; vol >= 0; vol -= 2) {
260 		_intf->callback(67, 1, vol, vol);
261 		_vm->delay(16);
262 	}
263 
264 	_intf->callback(67, 1, 0, 0);
265 	_intf->callback(70, 1);
266 
267 	g_system->getAudioCDManager()->stop();
268 
269 	_intf->callback(70, 0x31);
270 	_intf->callback(67, 1, 127, 127);
271 }
272 
updateVolumeSettings()273 void SoundTowns_Darkmoon::updateVolumeSettings() {
274 	bool mute = (ConfMan.hasKey("mute")) ? ConfMan.getBool("mute") : false;
275 	_intf->setSoundEffectVolume((mute ? 0 : ConfMan.getInt("sfx_volume")));
276 }
277 
checkTrigger()278 int SoundTowns_Darkmoon::checkTrigger() {
279 	return _timer;
280 }
281 
resetTrigger()282 void SoundTowns_Darkmoon::resetTrigger() {
283 	_timer = 0;
284 	_timerSwitch = 0;
285 }
286 
287 } // End of namespace Kyra
288 
289 #endif
290