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 "engines/engine.h"
24 #include "scumm/players/player_v4a.h"
25 #include "scumm/scumm.h"
26
27 #include "common/file.h"
28
29 namespace Scumm {
30
Player_V4A(ScummEngine * scumm,Audio::Mixer * mixer)31 Player_V4A::Player_V4A(ScummEngine *scumm, Audio::Mixer *mixer)
32 : _vm(scumm),
33 _mixer(mixer),
34 _tfmxMusic(_mixer->getOutputRate(), true),
35 _tfmxSfx(_mixer->getOutputRate(), true),
36 _musicHandle(),
37 _sfxHandle(),
38 _musicId(),
39 _sfxSlots(),
40 _initState(0),
41 _signal(0) {
42
43 assert(scumm);
44 assert(mixer);
45 assert(_vm->_game.id == GID_MONKEY_VGA);
46 _tfmxMusic.setSignalPtr(&_signal, 1);
47 }
48
init()49 bool Player_V4A::init() {
50 if (_vm->_game.id != GID_MONKEY_VGA)
51 error("player_v4a - unknown game");
52
53 Common::File fileMdat, fileSample;
54
55 if (fileMdat.open("music.dat") && fileSample.open("sample.dat")) {
56 // explicitly request that no instance delets the resources automatically
57 if (_tfmxMusic.load(fileMdat, fileSample, false)) {
58 _tfmxSfx.setModuleData(_tfmxMusic);
59 return true;
60 }
61 } else
62 warning("player_v4a: couldnt load one of the music resources: music.dat, sample.dat");
63
64 return false;
65 }
66
~Player_V4A()67 Player_V4A::~Player_V4A() {
68 _mixer->stopHandle(_musicHandle);
69 _mixer->stopHandle(_sfxHandle);
70 _tfmxMusic.freeResources();
71 }
72
setMusicVolume(int vol)73 void Player_V4A::setMusicVolume(int vol) {
74 debug(5, "player_v4a: setMusicVolume %i", vol);
75 }
76
stopAllSounds()77 void Player_V4A::stopAllSounds() {
78 debug(5, "player_v4a: stopAllSounds");
79 if (_initState > 0) {
80 _tfmxMusic.stopSong();
81 _signal = 0;
82 _musicId = 0;
83
84 _tfmxSfx.stopSong();
85 clearSfxSlots();
86 } else
87 _mixer->stopHandle(_musicHandle);
88 }
89
stopSound(int nr)90 void Player_V4A::stopSound(int nr) {
91 debug(5, "player_v4a: stopSound %d", nr);
92 if (nr == 0)
93 return;
94 if (nr == _musicId) {
95 _musicId = 0;
96 if (_initState > 0)
97 _tfmxMusic.stopSong();
98 else
99 _mixer->stopHandle(_musicHandle);
100 _signal = 0;
101 } else {
102 const int chan = getSfxChan(nr);
103 if (chan != -1) {
104 setSfxSlot(chan, 0);
105 _tfmxSfx.stopMacroEffect(chan);
106 }
107 }
108 }
109
startSound(int nr)110 void Player_V4A::startSound(int nr) {
111 static const int8 monkeyCommands[52] = {
112 -1, -2, -3, -4, -5, -6, -7, -8,
113 -9, -10, -11, -12, -13, -14, 18, 17,
114 -17, -18, -19, -20, -21, -22, -23, -24,
115 -25, -26, -27, -28, -29, -30, -31, -32,
116 -33, 16, -35, 0, 1, 2, 3, 7,
117 8, 10, 11, 4, 5, 14, 15, 12,
118 6, 13, 9, 19
119 };
120
121 const byte *ptr = _vm->getResourceAddress(rtSound, nr);
122 assert(ptr);
123
124 const int val = ptr[9];
125 if (val < 0 || val >= ARRAYSIZE(monkeyCommands)) {
126 warning("player_v4a: illegal Songnumber %i", val);
127 return;
128 }
129
130 if (!_initState)
131 _initState = init() ? 1 : -1;
132
133 if (_initState < 0)
134 return;
135
136 int index = monkeyCommands[val];
137 const byte type = ptr[6];
138 if (index < 0) { // SoundFX
139 index = -index - 1;
140 debug(3, "player_v4a: play %d: custom %i - %02X", nr, index, type);
141
142 // start an empty Song so timing is setup
143 if (_tfmxSfx.getSongIndex() < 0)
144 _tfmxSfx.doSong(0x18);
145
146 const int chan = _tfmxSfx.doSfx((uint16)index);
147 if (chan >= 0 && chan < ARRAYSIZE(_sfxSlots))
148 setSfxSlot(chan, nr, type);
149 else
150 warning("player_v4a: custom %i is not of required type", index);
151
152 // the Tfmx-player never "ends" the output by itself, so this should be threadsafe
153 if (!_mixer->isSoundHandleActive(_sfxHandle))
154 _mixer->playStream(Audio::Mixer::kSFXSoundType, &_sfxHandle, &_tfmxSfx, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO);
155
156 } else { // Song
157 debug(3, "player_v4a: play %d: song %i - %02X", nr, index, type);
158 if (ptr[6] != 0x7F)
159 warning("player_v4a: Song has wrong type");
160
161 _tfmxMusic.doSong(index);
162 _signal = 2;
163
164 // the Tfmx-player never "ends" the output by itself, so this should be threadsafe
165 if (!_mixer->isSoundHandleActive(_musicHandle))
166 _mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle, &_tfmxMusic, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO);
167 _musicId = nr;
168 }
169 }
170
getMusicTimer()171 int Player_V4A::getMusicTimer() {
172 // A workaround if the modplayer couldnt load the datafiles - just return a number big enough to pass all tests
173 if (_initState < 0)
174 return 2000;
175 if (_musicId) {
176 // The titlesong (and a few others) is running with ~70 ticks per second and the scale seems to be based on that.
177 // The Game itself doesn't get the timing from the Tfmx Player however, so we just use the elapsed time
178 // 357 ~ 1000 * 25 * (1 / 70)
179 return _mixer->getSoundElapsedTime(_musicHandle) / 357;
180 }
181 return 0;
182 }
183
getSoundStatus(int nr) const184 int Player_V4A::getSoundStatus(int nr) const {
185 // For music the game queues a variable the Tfmx Player sets through a special command.
186 // For sfx there seems to be no way to queue them, and the game doesn't try to.
187 return (nr == _musicId) ? _signal : 0;
188 }
189
190 } // End of namespace Scumm
191