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/softsynth/fmtowns_pc98/towns_pc98_driver.h"
27 
28 #include "common/config-manager.h"
29 
30 namespace Kyra {
31 
SoundPC98_LoK(KyraEngine_v1 * vm,Audio::Mixer * mixer)32 SoundPC98_LoK::SoundPC98_LoK(KyraEngine_v1 *vm, Audio::Mixer *mixer) :
33 	Sound(vm, mixer), _musicTrackData(0), _sfxTrackData(0), _lastTrack(-1), _driver(0), _currentResourceSet(0) {
34 	memset(&_resInfo, 0, sizeof(_resInfo));
35 }
36 
~SoundPC98_LoK()37 SoundPC98_LoK::~SoundPC98_LoK() {
38 	delete[] _musicTrackData;
39 	delete[] _sfxTrackData;
40 	delete _driver;
41 	for (int i = 0; i < 3; i++)
42 		initAudioResourceInfo(i, 0);
43 }
44 
init()45 bool SoundPC98_LoK::init() {
46 	_driver = new TownsPC98_AudioDriver(_mixer, TownsPC98_AudioDriver::kType26);
47 	bool reslt = _driver->init();
48 	updateVolumeSettings();
49 
50 	return reslt;
51 }
52 
initAudioResourceInfo(int set,void * info)53 void SoundPC98_LoK::initAudioResourceInfo(int set, void *info) {
54 	if (set >= kMusicIntro && set <= kMusicFinale) {
55 		delete _resInfo[set];
56 		_resInfo[set] = info ? new Common::String(((SoundResourceInfo_PC98*)info)->pattern) : 0;
57 	}
58 }
59 
selectAudioResourceSet(int set)60 void SoundPC98_LoK::selectAudioResourceSet(int set) {
61 	if (set >= kMusicIntro && set <= kMusicFinale) {
62 		if (_resInfo[set])
63 			_currentResourceSet = set;
64 	}
65 }
66 
hasSoundFile(uint file) const67 bool SoundPC98_LoK::hasSoundFile(uint file) const {
68 	return true;
69 }
70 
loadSoundFile(uint)71 void SoundPC98_LoK::loadSoundFile(uint) {
72 	if (_currentResourceSet == kMusicIntro) {
73 		delete[] _sfxTrackData;
74 		_sfxTrackData = 0;
75 
76 		int dataSize = 0;
77 		const uint8 *tmp = _vm->staticres()->loadRawData(k1PC98IntroSfx, dataSize);
78 
79 		if (!tmp) {
80 			warning("Could not load static intro sound effects data\n");
81 			return;
82 		}
83 
84 		_sfxTrackData = new uint8[dataSize];
85 		memcpy(_sfxTrackData, tmp, dataSize);
86 	}
87 }
88 
loadSoundFile(Common::String file)89 void SoundPC98_LoK::loadSoundFile(Common::String file) {
90 	delete[] _sfxTrackData;
91 	_sfxTrackData = _vm->resource()->fileData(file.c_str(), 0);
92 }
93 
playTrack(uint8 track)94 void SoundPC98_LoK::playTrack(uint8 track) {
95 	track -= 1;
96 
97 	if (track == _lastTrack && _musicEnabled)
98 		return;
99 
100 	beginFadeOut();
101 
102 	Common::String musicFile = Common::String::format(resPattern(), track);
103 	delete[] _musicTrackData;
104 	_musicTrackData = _vm->resource()->fileData(musicFile.c_str(), 0);
105 	if (_musicEnabled)
106 		_driver->loadMusicData(_musicTrackData);
107 
108 	_lastTrack = track;
109 }
110 
haltTrack()111 void SoundPC98_LoK::haltTrack() {
112 	_lastTrack = -1;
113 	_driver->reset();
114 }
115 
beginFadeOut()116 void SoundPC98_LoK::beginFadeOut() {
117 	if (!_driver->musicPlaying())
118 		return;
119 
120 	for (int i = 0; i < 20; i++) {
121 		_driver->fadeStep();
122 		_vm->delay(32);
123 	}
124 	haltTrack();
125 }
126 
playSoundEffect(uint16 track,uint8)127 void SoundPC98_LoK::playSoundEffect(uint16 track, uint8) {
128 	if (!_sfxTrackData)
129 		return;
130 
131 	_driver->loadSoundEffectData(_sfxTrackData, track);
132 }
133 
updateVolumeSettings()134 void SoundPC98_LoK::updateVolumeSettings() {
135 	if (!_driver)
136 		return;
137 
138 	bool mute = false;
139 	if (ConfMan.hasKey("mute"))
140 		mute = ConfMan.getBool("mute");
141 
142 	_driver->setMusicVolume((mute ? 0 : ConfMan.getInt("music_volume")));
143 	_driver->setSoundEffectVolume((mute ? 0 : ConfMan.getInt("sfx_volume")));
144 }
145 
146 } // End of namespace Kyra
147