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 #include "kyra/sound/sound_intern.h"
24 #include "kyra/resource/resource.h"
25 
26 #include "audio/mixer.h"
27 #include "audio/mods/maxtrax.h"
28 
29 namespace Kyra {
30 
SoundAmiga_LoK(KyraEngine_v1 * vm,Audio::Mixer * mixer)31 SoundAmiga_LoK::SoundAmiga_LoK(KyraEngine_v1 *vm, Audio::Mixer *mixer)
32 	: Sound(vm, mixer),
33 	  _driver(0),
34 	  _musicHandle(),
35 	  _fileLoaded(kFileNone),
36 	  _tableSfxIntro(0),
37 	  _tableSfxGame(0),
38 	  _tableSfxIntro_Size(0),
39 	  _tableSfxGame_Size(0) {
40 }
41 
~SoundAmiga_LoK()42 SoundAmiga_LoK::~SoundAmiga_LoK() {
43 	_mixer->stopHandle(_musicHandle);
44 	delete _driver;
45 }
46 
init()47 bool SoundAmiga_LoK::init() {
48 	_driver = new Audio::MaxTrax(_mixer->getOutputRate(), true);
49 
50 	_tableSfxIntro = _vm->staticres()->loadAmigaSfxTable(k1AmigaIntroSFXTable, _tableSfxIntro_Size);
51 	_tableSfxGame = _vm->staticres()->loadAmigaSfxTable(k1AmigaGameSFXTable, _tableSfxGame_Size);
52 
53 	return _driver != 0 && _tableSfxIntro && _tableSfxGame;
54 }
55 
initAudioResourceInfo(int set,void * info)56 void SoundAmiga_LoK::initAudioResourceInfo(int set, void *info) {
57 	// See comment below
58 }
59 
selectAudioResourceSet(int set)60 void SoundAmiga_LoK::selectAudioResourceSet(int set) {
61 	// It seems that loadSoundFile() is doing what would normally be done in here.
62 	// As long as this driver is only required for one single target (Kyra 1 Amiga)
63 	// this doesn't matter much.
64 }
65 
hasSoundFile(uint file) const66 bool SoundAmiga_LoK::hasSoundFile(uint file) const {
67 	if (file < 3)
68 		return true;
69 	return false;
70 }
71 
loadSoundFile(uint file)72 void SoundAmiga_LoK::loadSoundFile(uint file) {
73 	debugC(5, kDebugLevelSound, "SoundAmiga_LoK::loadSoundFile(%d)", file);
74 
75 	static const char *const tableFilenames[3][2] = {
76 		{ "introscr.mx",  "introinst.mx" },
77 		{ "kyramusic.mx", 0 },
78 		{ "finalescr.mx", "introinst.mx" }
79 	};
80 	assert(file < ARRAYSIZE(tableFilenames));
81 	if (_fileLoaded == (FileType)file)
82 		return;
83 	const char *scoreName = tableFilenames[file][0];
84 	const char *sampleName = tableFilenames[file][1];
85 	bool loaded = false;
86 
87 	Common::SeekableReadStream *scoreIn = _vm->resource()->createReadStream(scoreName);
88 	if (sampleName) {
89 		Common::SeekableReadStream *sampleIn = _vm->resource()->createReadStream(sampleName);
90 		if (scoreIn && sampleIn) {
91 			_fileLoaded = kFileNone;
92 			loaded = _driver->load(*scoreIn, true, false);
93 			loaded = loaded && _driver->load(*sampleIn, false, true);
94 		} else
95 			warning("SoundAmiga_LoK: missing atleast one of those music files: %s, %s", scoreName, sampleName);
96 		delete sampleIn;
97 	} else {
98 		if (scoreIn) {
99 			_fileLoaded = kFileNone;
100 			loaded = _driver->load(*scoreIn);
101 		} else
102 			warning("SoundAmiga_LoK: missing music file: %s", scoreName);
103 	}
104 	delete scoreIn;
105 
106 	if (loaded)
107 		_fileLoaded = (FileType)file;
108 }
109 
playTrack(uint8 track)110 void SoundAmiga_LoK::playTrack(uint8 track) {
111 	debugC(5, kDebugLevelSound, "SoundAmiga_LoK::playTrack(%d)", track);
112 
113 	static const byte tempoIntro[] = { 0x46, 0x55, 0x3C, 0x41 };
114 	static const byte tempoFinal[] = { 0x78, 0x50 };
115 	static const byte tempoIngame[] = {
116 		0x64, 0x64, 0x64, 0x64, 0x64, 0x73, 0x4B, 0x64,
117 		0x64, 0x64, 0x55, 0x9C, 0x6E, 0x91, 0x78, 0x84,
118 		0x32, 0x64, 0x64, 0x6E, 0x3C, 0xD8, 0xAF
119 	};
120 	static const byte loopIngame[] = {
121 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 		0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01,
123 		0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00
124 	};
125 
126 	int score = -1;
127 	bool loop = false;
128 	byte volume = 0x40;
129 	byte tempo = 0;
130 
131 
132 	switch (_fileLoaded) {
133 	case kFileIntro:
134 		if (track >= 2 && track < ARRAYSIZE(tempoIntro) + 2) {
135 			score = track - 2;
136 			tempo = tempoIntro[score];
137 		}
138 		break;
139 
140 	case kFileGame:
141 		if (track >= 11 && track < ARRAYSIZE(tempoIngame) + 11) {
142 			score = track - 11;
143 			loop = loopIngame[score] != 0;
144 			tempo = tempoIngame[score];
145 		}
146 		break;
147 
148 	case kFileFinal:
149 		// score 0 gets started immediately after loading the music-files with different tempo.
150 		// we need to define a track-value for the fake call of this function
151 		if (track >= 2 && track < ARRAYSIZE(tempoFinal) + 2) {
152 			score = track - 2;
153 			loop = true;
154 			tempo = tempoFinal[score];
155 		}
156 		break;
157 
158 	default:
159 		return;
160 	}
161 
162 	if (score >= 0) {
163 		if (_musicEnabled && _driver->playSong(score, loop)) {
164 			_driver->setVolume(volume);
165 			_driver->setTempo(tempo << 4);
166 			if (!_mixer->isSoundHandleActive(_musicHandle))
167 				_mixer->playStream(Audio::Mixer::kPlainSoundType, &_musicHandle, _driver, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO);
168 		}
169 	} else if (track == 0)
170 		_driver->stopMusic();
171 	else if (track == 1)
172 		beginFadeOut();
173 }
174 
haltTrack()175 void SoundAmiga_LoK::haltTrack() {
176 	debugC(5, kDebugLevelSound, "SoundAmiga_LoK::haltTrack()");
177 	_driver->stopMusic();
178 }
179 
beginFadeOut()180 void SoundAmiga_LoK::beginFadeOut() {
181 	debugC(5, kDebugLevelSound, "SoundAmiga_LoK::beginFadeOut()");
182 	for (int i = 0x3F; i >= 0; --i) {
183 		_driver->setVolume((byte)i);
184 		_vm->delay(_vm->tickLength());
185 	}
186 
187 	_driver->stopMusic();
188 	_vm->delay(_vm->tickLength());
189 	_driver->setVolume(0x40);
190 }
191 
playSoundEffect(uint16 track,uint8)192 void SoundAmiga_LoK::playSoundEffect(uint16 track, uint8) {
193 	debugC(5, kDebugLevelSound, "SoundAmiga_LoK::playSoundEffect(%d)", track);
194 	const AmigaSfxTable *sfx = 0;
195 	bool pan = false;
196 
197 	switch (_fileLoaded) {
198 	case kFileFinal:
199 	case kFileIntro:
200 		// We only allow playing of sound effects, which are included in the table.
201 		if (track < _tableSfxIntro_Size) {
202 			sfx = &_tableSfxIntro[track];
203 			pan = (sfx->pan != 0);
204 		}
205 		break;
206 
207 	case kFileGame:
208 		if (0x61 <= track && track <= 0x63)
209 			playTrack(track - 0x4F);
210 
211 		if (track >= _tableSfxGame_Size)
212 			return;
213 
214 		if (_tableSfxGame[track].note) {
215 			sfx = &_tableSfxGame[track];
216 			pan = (sfx->pan != 0) && (sfx->pan != 2);
217 		}
218 
219 		break;
220 
221 	default:
222 		return;
223 	}
224 
225 	if (_sfxEnabled && sfx) {
226 		const bool success = _driver->playNote(sfx->note, sfx->patch, sfx->duration, sfx->volume, pan);
227 		if (success && !_mixer->isSoundHandleActive(_musicHandle))
228 			_mixer->playStream(Audio::Mixer::kPlainSoundType, &_musicHandle, _driver, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO);
229 	}
230 }
231 
232 } // End of namespace Kyra
233