ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

emisound.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "gui/error.h"
00024 
00025 #include "common/stream.h"
00026 #include "common/mutex.h"
00027 #include "common/timer.h"
00028 #include "audio/audiostream.h"
00029 #include "audio/decoders/raw.h"
00030 #include "audio/mixer.h"
00031 #include "engines/grim/debug.h"
00032 #include "engines/grim/sound.h"
00033 #include "engines/grim/grim.h"
00034 #include "engines/grim/resource.h"
00035 #include "engines/grim/savegame.h"
00036 #include "engines/grim/textsplit.h"
00037 #include "engines/grim/emi/sound/emisound.h"
00038 #include "engines/grim/emi/sound/track.h"
00039 #include "engines/grim/emi/sound/aifftrack.h"
00040 #include "engines/grim/emi/sound/mp3track.h"
00041 #include "engines/grim/emi/sound/scxtrack.h"
00042 #include "engines/grim/emi/sound/vimatrack.h"
00043 #include "engines/grim/movie/codecs/vima.h"
00044 
00045 namespace Grim {
00046 
00047 EMISound *g_emiSound = nullptr;
00048 
00049 extern uint16 imuseDestTable[];
00050 
00051 MusicEntry emiPS2MusicTable[] = {
00052     { 0, 0, 0, 127, 0, "", "", "" },
00053     { 0, 0, 1, 127, 1, "state", "", "1115.scx" },
00054     { 0, 0, 2, 127, 2, "state", "", "1170.scx" },
00055     { 0, 0, 2, 127, 3, "state", "", "1170.scx" },
00056     { 0, 0, 2, 127, 4, "state", "", "1170.scx" },
00057     { 0, 0, 3, 127, 5, "state", "", "1165.scx" },
00058     { 0, 0, 4, 127, 6, "state", "", "1145.scx" },
00059     { 0, 0, 4, 127, 7, "state", "", "1145.scx" },
00060     { 0, 0, 1, 127, 8, "state", "", "1115.scx" },
00061     { 0, 0, 1, 127, 9, "state", "", "1115.scx" },
00062     { 0, 0, 0, 127, 10, "episode", "", "7200.scx" },
00063     { 0, 0, 0, 127, 11, "episode", "", "1210.scx" },
00064     { 0, 0, 0, 127, 12, "state", "", "1180.scx" },
00065     { 0, 0, 0, 127, 13, "state", "", "1110.scx" },
00066     { 0, 0, 1, 127, 14, "state", "", "1115.scx" },
00067     { 0, 0, 0, 127, 15, "state", "", "1105.scx" },
00068     { 0, 0, 4, 127, 16, "state", "", "1145.scx" },
00069     { 0, 0, 0, 127, 17, "state", "", "1150.scx" },
00070     { 0, 0, 0, 127, 18, "state", "", "1100.scx" },
00071     { 0, 0, 5, 127, 19, "state", "", "1120.scx" },
00072     { 0, 0, 5, 127, 20, "state", "", "1120.scx" },
00073     { 0, 0, 5, 127, 21, "state", "", "1120.scx" },
00074     { 0, 0, 3, 127, 22, "state", "", "1165.scx" },
00075     { 0, 0, 0, 127, 23, "state", "", "1155.scx" },
00076     { 0, 0, 0, 127, 24, "state", "", "1160.scx" },
00077     { 0, 0, 0, 127, 25, "state", "", "1140.scx" },
00078     { 0, 0, 0, 127, 26, "state", "", "1140.scx" },
00079     { 0, 0, 2, 127, 27, "state", "", "1170.scx" },
00080     { 0, 0, 2, 127, 28, "state", "", "1175.scx" },
00081     { 0, 0, 0, 127, 29, "episode", "", "1205.scx" },
00082     { 0, 0, 0, 127, 30, "state", "", "1000.scx" },
00083     { 0, 0, 0, 127, 31, "state", "", "1185.scx" },
00084     { 0, 0, 0, 127, 32, "state", "", "2127.scx" },
00085     { 0, 0, 0, 127, 33, "state", "", "2119.scx" },
00086     { 0, 0, 0, 127, 34, "episode", "", "2208.scx" },
00087     { 0, 0, 0, 127, 35, "state", "", "2195.scx" },
00088     { 0, 0, 0, 127, 36, "state", "", "2190.scx" },
00089     { 0, 0, 0, 127, 37, "state", "", "2185.scx" },
00090     { 0, 0, 1, 127, 38, "state", "", "2175.scx" },
00091     { 0, 0, 0, 127, 39, "state", "", "2170.scx" },
00092     { 0, 0, 0, 127, 40, "state", "", "2165.scx" },
00093     { 0, 0, 0, 127, 41, "state", "", "2160.scx" },
00094     { 0, 0, 0, 127, 42, "state", "", "2155.scx" },
00095     { 0, 0, 0, 127, 43, "state", "", "2120.scx" },
00096     { 0, 0, 0, 127, 44, "state", "", "2150.scx" },
00097     { 0, 0, 0, 127, 45, "state", "", "2145.scx" },
00098     { 0, 0, 2, 127, 46, "state", "", "2105.scx" },
00099     { 0, 0, 0, 127, 47, "state", "", "2115.scx" },
00100     { 0, 0, 0, 127, 48, "state", "", "2125.scx" },
00101     { 0, 0, 0, 127, 49, "state", "", "2130.scx" },
00102     { 0, 0, 0, 127, 50, "state", "", "2100.scx" },
00103     { 0, 0, 0, 127, 51, "state", "", "2140.scx" },
00104     { 0, 0, 0, 127, 52, "episode", "", "2200.scx" },
00105     { 0, 0, 0, 127, 53, "state", "", "2116.scx" },
00106     { 0, 0, 0, 127, 54, "episode", "", "2207.scx" },
00107     { 0, 0, 0, 127, 55, "state", "", "2107.scx" },
00108     { 0, 0, 0, 127, 56, "episode", "", "2215.scx" },
00109     { 0, 0, 0, 127, 57, "episode", "", "2220.scx" },
00110     { 0, 0, 0, 127, 58, "episode", "", "2225.scx" },
00111     { 0, 0, 0, 127, 59, "episode", "", "2210.scx" },
00112     { 0, 0, 0, 127, 60, "state", "", "2135.scx" },
00113     { 0, 0, 2, 127, 61, "state", "", "2105.scx" },
00114     { 0, 0, 0, 127, 62, "state", "", "2108.scx" },
00115     { 0, 0, 0, 127, 63, "state", "", "2117.scx" },
00116     { 0, 0, 0, 127, 64, "state", "", "2118.scx" },
00117     { 0, 0, 1, 127, 65, "state", "", "2175.scx" },
00118     { 0, 0, 0, 127, 66, "state", "", "4120.scx" },
00119     { 0, 0, 1, 127, 67, "state", "", "3100.scx" },
00120     { 0, 0, 0, 127, 68, "state", "", "4115.scx" },
00121     { 0, 0, 2, 127, 69, "state", "", "4100.scx" },
00122     { 0, 0, 0, 127, 70, "state", "", "3150.scx" },
00123     { 0, 0, 0, 127, 71, "state", "", "3145.scx" },
00124     { 0, 0, 0, 127, 72, "state", "", "4110.scx" },
00125     { 0, 0, 0, 127, 73, "state", "", "3140.scx" },
00126     { 0, 0, 3, 127, 74, "state", "", "3135.scx" },
00127     { 0, 0, 3, 127, 75, "state", "", "3120.scx" },
00128     { 0, 0, 4, 127, 76, "state", "", "3130.scx" },
00129     { 0, 0, 4, 127, 77, "state", "", "3115.scx" },
00130     { 0, 0, 1, 127, 78, "state", "", "3100.scx" },
00131     { 0, 0, 5, 127, 79, "state", "", "3125.scx" },
00132     { 0, 0, 5, 127, 80, "state", "", "3110.scx" },
00133     { 0, 0, 6, 127, 81, "state", "", "3105.scx" },
00134     { 0, 0, 0, 127, 82, "episode", "", "3210.scx" },
00135     { 0, 0, 0, 127, 83, "episode", "", "3200.scx" },
00136     { 0, 0, 0, 127, 84, "episode", "", "3205.scx" },
00137     { 0, 0, 0, 127, 85, "state", "", "3147.scx" },
00138     { 0, 0, 0, 127, 86, "episode", "", "4215.scx" },
00139     { 0, 0, 0, 127, 87, "state", "", "4105.scx" },
00140     { 0, 0, 6, 127, 88, "state", "", "3106.scx" },
00141     { 0, 0, 6, 127, 89, "state", "", "3107.scx" },
00142     { 0, 0, 2, 127, 90, "state", "", "4100.scx" },
00143     { 0, 0, 1, 127, 91, "state", "", "5145.scx" },
00144     { 0, 0, 2, 127, 92, "state", "", "5140.scx" },
00145     { 0, 0, 2, 127, 93, "state", "", "5140.scx" },
00146     { 0, 0, 3, 127, 94, "state", "", "5135.scx" },
00147     { 0, 0, 3, 127, 95, "state", "", "5135.scx" },
00148     { 0, 0, 3, 127, 96, "state", "", "5135.scx" },
00149     { 0, 0, 0, 127, 97, "state", "", "5170.scx" },
00150     { 0, 0, 0, 127, 98, "episode", "", "5205.scx" },
00151     { 0, 0, 0, 127, 99, "state", "", "5120.scx" },
00152     { 0, 0, 0, 127, 100, "episode", "", "5215.scx" },
00153     { 0, 0, 0, 127, 101, "episode", "", "5230.scx" },
00154     { 0, 0, 0, 127, 102, "episode", "", "5225.scx" },
00155     { 0, 0, 0, 127, 103, "state", "", "5117.scx" },
00156     { 0, 0, 0, 127, 104, "state", "", "5115.scx" },
00157     { 0, 0, 0, 127, 105, "episode", "", "5220.scx" },
00158     { 0, 0, 0, 127, 106, "state", "", "6105.scx" },
00159     { 0, 0, 0, 127, 107, "state", "", "6100.scx" },
00160     { 0, 0, 0, 127, 108, "state", "", "5165.scx" },
00161     { 0, 0, 0, 127, 109, "state", "", "5160.scx" },
00162     { 0, 0, 0, 127, 110, "episode", "", "5200.scx" },
00163     { 0, 0, 2, 127, 111, "state", "", "5140.scx" },
00164     { 0, 0, 3, 127, 112, "state", "", "5135.scx" },
00165     { 0, 0, 0, 127, 113, "state", "", "5155.scx" },
00166     { 0, 0, 0, 127, 114, "state", "", "5150.scx" },
00167     { 0, 0, 0, 127, 115, "state", "", "5130.scx" },
00168     { 0, 0, 0, 127, 116, "state", "", "5125.scx" },
00169     { 0, 0, 0, 127, 117, "state", "", "5110.scx" },
00170     { 0, 0, 1, 127, 118, "state", "", "5105.scx" },
00171     { 0, 0, 0, 127, 119, "state", "", "5100.scx" },
00172     { 0, 0, 0, 127, 120, "state", "", "6110.scx" },
00173     { 0, 0, 0, 127, 121, "state", "", "5106.scx" },
00174     { 0, 0, 0, 127, 122, "episode", "", "7210.scx" },
00175     { 0, 0, 0, 127, 123, "episode", "", "1200.scx" },
00176     { 0, 0, 0, 127, 124, "state", "", "1195.scx" },
00177     { 0, 0, 0, 127, 125, "episode", "", "1215.scx" }
00178 };
00179 
00180 void EMISound::timerHandler(void *refCon) {
00181     EMISound *emiSound = (EMISound *)refCon;
00182     emiSound->callback();
00183 }
00184 
00185 EMISound::EMISound(int fps) {
00186     _curMusicState = -1;
00187     _numMusicStates = 0;
00188     _musicTrack = nullptr;
00189     _curTrackId = 0;
00190     _callbackFps = fps;
00191     vimaInit(imuseDestTable);
00192     initMusicTable();
00193     g_system->getTimerManager()->installTimerProc(timerHandler, 1000000 / _callbackFps, this, "emiSoundCallback");
00194 }
00195 
00196 EMISound::~EMISound() {
00197     g_system->getTimerManager()->removeTimerProc(timerHandler);
00198     freePlayingSounds();
00199     freeLoadedSounds();
00200     delete _musicTrack;
00201     if (g_grim->getGamePlatform() != Common::kPlatformPS2) {
00202         delete[] _musicTable;
00203     }
00204 }
00205 
00206 EMISound::TrackList::iterator EMISound::getPlayingTrackByName(const Common::String &name) {
00207     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00208         if ((*it)->getSoundName() == name) {
00209             return it;
00210         }
00211     }
00212     return _playingTracks.end();
00213 }
00214 
00215 void EMISound::freePlayingSounds() {
00216     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00217         delete (*it);
00218     }
00219     _playingTracks.clear();
00220 }
00221 
00222 void EMISound::freeLoadedSounds() {
00223     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
00224         delete it->_value;
00225     }
00226     _preloadedTrackMap.clear();
00227 }
00228 
00229 bool EMISound::startVoice(const Common::String &soundName, int volume, int pan) {
00230     return startSound(soundName, Audio::Mixer::kSpeechSoundType, volume, pan);
00231 }
00232 
00233 bool EMISound::startSfx(const Common::String &soundName, int volume, int pan) {
00234     return startSound(soundName, Audio::Mixer::kSFXSoundType, volume, pan);
00235 }
00236 
00237 bool EMISound::startSfxFrom(const Common::String &soundName, const Math::Vector3d &pos, int volume) {
00238     return startSoundFrom(soundName, Audio::Mixer::kSFXSoundType, pos, volume);
00239 }
00240 
00241 bool EMISound::startSound(const Common::String &soundName, Audio::Mixer::SoundType soundType, int volume, int pan) {
00242     Common::StackLock lock(_mutex);
00243     SoundTrack *track = initTrack(soundName, soundType);
00244     if (track) {
00245         track->setBalance(pan * 2 - 127);
00246         track->setVolume(volume);
00247         track->play();
00248         _playingTracks.push_back(track);
00249         return true;
00250     }
00251     return false;
00252 }
00253 
00254 bool EMISound::startSoundFrom(const Common::String &soundName, Audio::Mixer::SoundType soundType, const Math::Vector3d &pos, int volume) {
00255     Common::StackLock lock(_mutex);
00256     SoundTrack *track = initTrack(soundName, soundType);
00257     if (track) {
00258         track->setVolume(volume);
00259         track->setPosition(true, pos);
00260         track->play();
00261         _playingTracks.push_back(track);
00262         return true;
00263     }
00264     return false;
00265 }
00266 
00267 bool EMISound::getSoundStatus(const Common::String &soundName) {
00268     TrackList::iterator it = getPlayingTrackByName(soundName);
00269 
00270     if (it == _playingTracks.end())  // We have no such sound.
00271         return false;
00272 
00273     return (*it)->isPlaying();
00274 }
00275 
00276 void EMISound::stopSound(const Common::String &soundName) {
00277     Common::StackLock lock(_mutex);
00278     TrackList::iterator it = getPlayingTrackByName(soundName);
00279     if (it == _playingTracks.end()) {
00280         warning("Sound track '%s' could not be found to stop", soundName.c_str());
00281     } else {
00282         delete (*it);
00283         _playingTracks.erase(it);
00284     }
00285 }
00286 
00287 int32 EMISound::getPosIn16msTicks(const Common::String &soundName) {
00288     TrackList::iterator it = getPlayingTrackByName(soundName);
00289     if (it == _playingTracks.end()) {
00290         warning("Sound track '%s' could not be found to get ticks", soundName.c_str());
00291         return 0;
00292     } else {
00293         return (*it)->getPos().msecs() / 16;
00294     }
00295 }
00296 
00297 void EMISound::setVolume(const Common::String &soundName, int volume) {
00298     Common::StackLock lock(_mutex);
00299     TrackList::iterator it = getPlayingTrackByName(soundName);
00300     if (it == _playingTracks.end()) {
00301         warning("Sound track '%s' could not be found to set volume", soundName.c_str());
00302     } else {
00303         (*it)->setVolume(volume);
00304     }
00305 }
00306 
00307 void EMISound::setPan(const Common::String &soundName, int pan) {
00308     Common::StackLock lock(_mutex);
00309     TrackList::iterator it = getPlayingTrackByName(soundName);
00310     if (it == _playingTracks.end()) {
00311         warning("Sound track '%s' could not be found to set pan", soundName.c_str());
00312     } else {
00313         (*it)->setBalance(pan * 2 - 127);
00314     }
00315 }
00316 
00317 bool EMISound::loadSfx(const Common::String &soundName, int &id) {
00318     Common::StackLock lock(_mutex);
00319     SoundTrack *track = initTrack(soundName, Audio::Mixer::kSFXSoundType);
00320     if (track) {
00321         id = _curTrackId++;
00322         _preloadedTrackMap[id] = track;
00323         return true;
00324     } else {
00325         return false;
00326     }
00327 }
00328 
00329 void EMISound::playLoadedSound(int id, bool looping) {
00330     Common::StackLock lock(_mutex);
00331     TrackMap::iterator it = _preloadedTrackMap.find(id);
00332     if (it != _preloadedTrackMap.end()) {
00333         it->_value->setLooping(looping);
00334         it->_value->setPosition(false);
00335         it->_value->play();
00336     } else {
00337         warning("EMISound::playLoadedSound called with invalid sound id");
00338     }
00339 }
00340 
00341 void EMISound::playLoadedSoundFrom(int id, const Math::Vector3d &pos, bool looping) {
00342     Common::StackLock lock(_mutex);
00343     TrackMap::iterator it = _preloadedTrackMap.find(id);
00344     if (it != _preloadedTrackMap.end()) {
00345         it->_value->setLooping(looping);
00346         it->_value->setPosition(true, pos);
00347         it->_value->play();
00348     }
00349     else {
00350         warning("EMISound::playLoadedSoundFrom called with invalid sound id");
00351     }
00352 }
00353 
00354 void EMISound::setLoadedSoundLooping(int id, bool looping) {
00355     Common::StackLock lock(_mutex);
00356     TrackMap::iterator it = _preloadedTrackMap.find(id);
00357     if (it != _preloadedTrackMap.end()) {
00358         it->_value->setLooping(looping);
00359     } else {
00360         warning("EMISound::setLoadedSoundLooping called with invalid sound id");
00361     }
00362 }
00363 
00364 void EMISound::stopLoadedSound(int id) {
00365     Common::StackLock lock(_mutex);
00366     TrackMap::iterator it = _preloadedTrackMap.find(id);
00367     if (it != _preloadedTrackMap.end()) {
00368         it->_value->stop();
00369     } else {
00370         warning("EMISound::stopLoadedSound called with invalid sound id");
00371     }
00372 }
00373 
00374 void EMISound::freeLoadedSound(int id) {
00375     Common::StackLock lock(_mutex);
00376     TrackMap::iterator it = _preloadedTrackMap.find(id);
00377     if (it != _preloadedTrackMap.end()) {
00378         delete it->_value;
00379         _preloadedTrackMap.erase(it);
00380     } else {
00381         warning("EMISound::freeLoadedSound called with invalid sound id");
00382     }
00383 }
00384 
00385 void EMISound::setLoadedSoundVolume(int id, int volume) {
00386     Common::StackLock lock(_mutex);
00387     TrackMap::iterator it = _preloadedTrackMap.find(id);
00388     if (it != _preloadedTrackMap.end()) {
00389         it->_value->setVolume(volume);
00390     } else {
00391         warning("EMISound::setLoadedSoundVolume called with invalid sound id");
00392     }
00393 }
00394 
00395 void EMISound::setLoadedSoundPan(int id, int pan) {
00396     Common::StackLock lock(_mutex);
00397     TrackMap::iterator it = _preloadedTrackMap.find(id);
00398     if (it != _preloadedTrackMap.end()) {
00399         it->_value->setBalance(pan * 2 - 127);
00400     } else {
00401         warning("EMISound::setLoadedSoundPan called with invalid sound id");
00402     }
00403 }
00404 
00405 void EMISound::setLoadedSoundPosition(int id, const Math::Vector3d &pos) {
00406     Common::StackLock lock(_mutex);
00407     TrackMap::iterator it = _preloadedTrackMap.find(id);
00408     if (it != _preloadedTrackMap.end()) {
00409         it->_value->setPosition(true, pos);
00410     } else {
00411         warning("EMISound::setLoadedSoundPosition called with invalid sound id");
00412     }
00413 }
00414 
00415 bool EMISound::getLoadedSoundStatus(int id) {
00416     Common::StackLock lock(_mutex);
00417     TrackMap::iterator it = _preloadedTrackMap.find(id);
00418     if (it != _preloadedTrackMap.end()) {
00419         return it->_value->isPlaying();
00420     }
00421     warning("EMISound::getLoadedSoundStatus called with invalid sound id");
00422     return false;
00423 }
00424 
00425 int EMISound::getLoadedSoundVolume(int id) {
00426     Common::StackLock lock(_mutex);
00427     TrackMap::iterator it = _preloadedTrackMap.find(id);
00428     if (it != _preloadedTrackMap.end()) {
00429         return it->_value->getVolume();
00430     }
00431     warning("EMISound::getLoadedSoundVolume called with invalid sound id");
00432     return false;
00433 }
00434 
00435 SoundTrack *EMISound::initTrack(const Common::String &soundName, Audio::Mixer::SoundType soundType, const Audio::Timestamp *start) const {
00436     SoundTrack *track;
00437     Common::String soundNameLower(soundName);
00438     soundNameLower.toLowercase();
00439     if (soundNameLower.hasSuffix(".scx")) {
00440         track = new SCXTrack(soundType);
00441     } else if (soundNameLower.hasSuffix(".m4b") || soundNameLower.hasSuffix(".lab")) {
00442         track = new MP3Track(soundType);
00443     } else if (soundNameLower.hasSuffix(".aif")) {
00444         track = new AIFFTrack(soundType);
00445     } else {
00446         track = new VimaTrack();
00447     }
00448 
00449     Common::String filename;
00450     if (soundType == Audio::Mixer::kMusicSoundType) {
00451         filename = _musicPrefix + soundName;
00452     } else {
00453         filename = soundName;
00454     }
00455 
00456     if (track->openSound(filename, soundName, start)) {
00457         return track;
00458     }
00459     return nullptr;
00460 }
00461 
00462 bool EMISound::stateHasLooped(int stateId) {
00463     if (stateId == _curMusicState) {
00464         if (_curMusicState != 0 && _musicTrack) {
00465             return _musicTrack->hasLooped();
00466         }
00467     } else {
00468         warning("EMISound::stateHasLooped called for a different music state than the current one");
00469     }
00470     return false;
00471 }
00472 
00473 bool EMISound::stateHasEnded(int stateId) {
00474     if (stateId == _curMusicState) {
00475         if (_curMusicState != 0 && _musicTrack) {
00476             return !_musicTrack->isPlaying();
00477         }
00478     }
00479     return true;
00480 }
00481 
00482 void EMISound::setMusicState(int stateId) {
00483     Common::StackLock lock(_mutex);
00484     // The demo calls ImSetState with state id 1000, which exceeds the number of states in the
00485     // music table.
00486     if (stateId >= _numMusicStates)
00487         stateId = 0;
00488     if (stateId == _curMusicState)
00489         return;
00490 
00491     Common::String soundName = _musicTable[stateId]._filename;
00492     int sync = _musicTable[stateId]._sync;
00493     Audio::Timestamp musicPos;
00494     int prevSync = -1;
00495     if (_musicTrack) {
00496         if (_musicTrack->isPlaying()) {
00497             musicPos = _musicTrack->getPos();
00498             prevSync = _musicTrack->getSync();
00499             if (sync == prevSync && soundName == _musicTrack->getSoundName()) {
00500                 // If the previous music track is the same track as the new one, we'll just
00501                 // keep playing the previous track. This happens in the PS2 version where they
00502                 // removed some of the music variations, but kept the states associated with
00503                 // those.
00504                 _curMusicState = stateId;
00505                 return;
00506             }
00507             _musicTrack->fadeOut();
00508             _playingTracks.push_back(_musicTrack);
00509             _musicTrack = nullptr;
00510         }
00511     }
00512 
00513     bool fadeMusicIn = false;
00514     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00515         if ((*it)->isPlaying() && (*it)->getSoundType() == Audio::Mixer::kMusicSoundType) {
00516             fadeMusicIn = true;
00517             break;
00518         }
00519     }
00520     if (!fadeMusicIn) {
00521         for (uint i = 0; i < _stateStack.size(); ++i) {
00522             if (_stateStack[i]._track && _stateStack[i]._track->isPlaying() && !_stateStack[i]._track->isPaused()) {
00523                 fadeMusicIn = true;
00524                 break;
00525             }
00526         }
00527     }
00528 
00529     if (stateId == 0) {
00530         _curMusicState = 0;
00531         return;
00532     }
00533     if (_musicTable == nullptr) {
00534         Debug::debug(Debug::Sound, "No music table loaded");
00535         return;
00536     }
00537     if (_musicTable[stateId]._id != stateId) {
00538         Debug::debug(Debug::Sound, "Attempted to play track #%d, not found in music table!", stateId);
00539         return;
00540     }
00541     _curMusicState = stateId;
00542 
00543     Audio::Timestamp *start = nullptr;
00544     if (prevSync != 0 && sync != 0 && prevSync == sync)
00545         start = &musicPos;
00546 
00547     Debug::debug(Debug::Sound, "Loading music: %s", soundName.c_str());
00548     SoundTrack *music = initTrack(soundName, Audio::Mixer::kMusicSoundType, start);
00549     if (music) {
00550         music->play();
00551         music->setSync(sync);
00552         if (fadeMusicIn) {
00553             music->setFade(0.0f);
00554             music->fadeIn();
00555         }
00556         _musicTrack = music;
00557     }
00558 }
00559 
00560 uint32 EMISound::getMsPos(int stateId) {
00561     if (!_musicTrack) {
00562         Debug::debug(Debug::Sound, "EMISound::getMsPos: Music track is null", stateId);
00563         return 0;
00564     }
00565     return _musicTrack->getPos().msecs();
00566 }
00567 
00568 MusicEntry *EMISound::initMusicTableDemo(const Common::String &filename) {
00569     Common::SeekableReadStream *data = g_resourceloader->openNewStreamFile(filename);
00570 
00571     if (!data)
00572         error("Couldn't open %s", filename.c_str());
00573     // FIXME, for now we use a fixed-size table, as I haven't looked at the retail-data yet.
00574     _numMusicStates = 15;
00575     MusicEntry *musicTable = new MusicEntry[_numMusicStates];
00576     for (int i = 0; i < 15; ++i) {
00577         musicTable->_x = 0;
00578         musicTable->_y = 0;
00579         musicTable->_sync = 0;
00580         musicTable->_trim = 0;
00581         musicTable->_id = i;
00582     }
00583 
00584     TextSplitter *ts = new TextSplitter(filename, data);
00585     int id, x, y, sync;
00586     char musicfilename[64];
00587     char name[64];
00588     while (!ts->isEof()) {
00589         while (!ts->checkString("*/")) {
00590             while (!ts->checkString(".cuebutton"))
00591                 ts->nextLine();
00592 
00593             ts->scanString(".cuebutton id %d x %d y %d sync %d \"%[^\"]64s", 5, &id, &x, &y, &sync, name);
00594             ts->scanString(".playfile \"%[^\"]64s", 1, musicfilename);
00595             musicTable[id]._id = id;
00596             musicTable[id]._x = x;
00597             musicTable[id]._y = y;
00598             musicTable[id]._sync = sync;
00599             musicTable[id]._name = name;
00600             musicTable[id]._filename = musicfilename;
00601         }
00602         ts->nextLine();
00603     }
00604     delete ts;
00605     delete data;
00606     return musicTable;
00607 }
00608 
00609 void EMISound::initMusicTableRetail(MusicEntry *musicTable, const Common::String filename) {
00610     Common::SeekableReadStream *data = g_resourceloader->openNewStreamFile(filename);
00611 
00612     // Remember to check, in case we forgot to copy over those files from the CDs.
00613     if (!data) {
00614         warning("Couldn't open %s", filename.c_str());
00615         return;
00616     }
00617     
00618     TextSplitter *ts = new TextSplitter(filename, data);
00619     int id, x, y, sync, trim;
00620     char musicfilename[64];
00621     char type[16];
00622     // Every block is followed by 3 lines of commenting/uncommenting, except the last.
00623     while (!ts->isEof()) {
00624         while (!ts->checkString("*/")) {
00625             while (!ts->checkString(".cuebutton"))
00626                 ts->nextLine();
00627 
00628             ts->scanString(".cuebutton id %d x %d y %d sync %d type %16s", 5, &id, &x, &y, &sync, type);
00629             ts->scanString(".playfile trim %d \"%[^\"]64s", 2, &trim, musicfilename);
00630             if (musicfilename[1] == '\\')
00631                 musicfilename[1] = '/';
00632             musicTable[id]._id = id;
00633             musicTable[id]._x = x;
00634             musicTable[id]._y = y;
00635             musicTable[id]._sync = sync;
00636             musicTable[id]._type = type;
00637             musicTable[id]._name = "";
00638             musicTable[id]._trim = trim;
00639             musicTable[id]._filename = musicfilename;
00640         }
00641         ts->nextLine();
00642     }
00643     delete ts;
00644     delete data;
00645 }
00646 
00647 void tableLoadErrorDialog() {
00648     const char *errorMessage = nullptr;
00649     errorMessage =  "ERROR: Not enough music tracks found!\n"
00650     "Escape from Monkey Island has two versions of FullMonkeyMap.imt,\n"
00651     "you need to copy both files from both CDs to Textures/, and rename\n"
00652     "them as follows to get music-support in-game: \n"
00653     "CD 1: \"FullMonkeyMap.imt\" -> \"FullMonkeyMap1.imt\"\n"
00654     "CD 2: \"FullMonkeyMap.imt\" -> \"FullMonkeyMap2.imt\"\n"
00655     "\n"
00656     "Alternatively, a Steam or GOG copy has a combined FullMonkeyMap.int";
00657     GUI::displayErrorDialog(errorMessage);
00658 }
00659 
00660 void EMISound::initMusicTable() {
00661     if (g_grim->getGameFlags() & ADGF_DEMO) {
00662         _musicTable = initMusicTableDemo("Music/FullMonkeyMap.imt");
00663         _musicPrefix = "Music/";
00664     } else if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
00665         _musicTable = emiPS2MusicTable;
00666         _numMusicStates = ARRAYSIZE(emiPS2MusicTable);
00667         _musicPrefix = "";
00668     } else {
00669         MusicEntry *musicTable = new MusicEntry[126];
00670         for (int i = 0; i < 126; ++i) {
00671             musicTable[i]._x = 0;
00672             musicTable[i]._y = 0;
00673             musicTable[i]._sync = 0;
00674             musicTable[i]._trim = 0;
00675             musicTable[i]._id = i;
00676         }
00677 
00678         initMusicTableRetail(musicTable, "Textures/FullMonkeyMap1.imt");
00679         initMusicTableRetail(musicTable, "Textures/FullMonkeyMap2.imt");
00680         initMusicTableRetail(musicTable, "Textures/FullMonkeyMap.imt");
00681 
00682         /* There seem to be 69+60 music tracks, for a total of 125 unique tracks. */
00683         int numTracks = 0;
00684         for (int i = 0; i < 126; i++) {
00685             if (!musicTable[i]._filename.empty()) {
00686                 numTracks++;
00687             }
00688         }
00689 
00690         warning("Found %d music tracks, expected at least 100", numTracks);
00691         if (numTracks < 100) {
00692             delete[] musicTable;
00693             _numMusicStates = 0;
00694             _musicTable = nullptr;
00695             tableLoadErrorDialog();
00696         } else {
00697             _numMusicStates = 126;
00698             _musicTable = musicTable;
00699             _musicPrefix = "Textures/spago/"; // Default to high-quality music.
00700         }
00701     }
00702 }
00703 
00704 void EMISound::selectMusicSet(int setId) {
00705     if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
00706         assert(setId == 0);
00707         _musicPrefix = "";
00708         return;
00709     }
00710     if (setId == 0) {
00711         _musicPrefix = "Textures/spago/";
00712     } else if (setId == 1) {
00713         _musicPrefix = "Textures/mego/";
00714     } else {
00715         error("EMISound::selectMusicSet - Unknown setId %d", setId);
00716     }
00717 
00718     // Immediately switch all currently active music tracks to the new quality.
00719     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00720         SoundTrack *track = (*it);
00721         if (track && track->getSoundType() == Audio::Mixer::kMusicSoundType) {
00722             (*it) = restartTrack(track);
00723             delete track;
00724         }
00725     }
00726     for (uint32 i = 0; i < _stateStack.size(); ++i) {
00727         SoundTrack *track = _stateStack[i]._track;
00728         if (track) {
00729             _stateStack[i]._track = restartTrack(track);
00730             delete track;
00731         }
00732     }
00733 }
00734 
00735 SoundTrack *EMISound::restartTrack(SoundTrack *track) {
00736     Audio::Timestamp pos = track->getPos();
00737     SoundTrack *newTrack = initTrack(track->getSoundName(), track->getSoundType(), &pos);
00738     if (newTrack) {
00739         newTrack->setVolume(track->getVolume());
00740         newTrack->setBalance(track->getBalance());
00741         newTrack->setFadeMode(track->getFadeMode());
00742         newTrack->setFade(track->getFade());
00743         if (track->isPlaying()) {
00744             newTrack->play();
00745         }
00746         if (track->isPaused()) {
00747             newTrack->pause();
00748         }
00749     }
00750     return newTrack;
00751 }
00752 
00753 void EMISound::pushStateToStack() {
00754     Common::StackLock lock(_mutex);
00755     if (_musicTrack) {
00756         _musicTrack->fadeOut();
00757         StackEntry entry = { _curMusicState, _musicTrack };
00758         _stateStack.push(entry);
00759         _musicTrack = nullptr;
00760     } else {
00761         StackEntry entry = { _curMusicState, nullptr };
00762         _stateStack.push(entry);
00763     }
00764     _curMusicState = 0;
00765 }
00766 
00767 void EMISound::popStateFromStack() {
00768     Common::StackLock lock(_mutex);
00769     if (_musicTrack) {
00770         _musicTrack->fadeOut();
00771         _playingTracks.push_back(_musicTrack);
00772     }
00773 
00774     //even pop state from stack if music isn't set
00775     StackEntry entry = _stateStack.pop();
00776     SoundTrack *track = entry._track;
00777 
00778     _musicTrack = track;
00779     _curMusicState = entry._state;
00780 
00781     if (track) {
00782         if (track->isPaused()) {
00783             track->pause();
00784         }
00785         track->fadeIn();
00786     }
00787 }
00788 
00789 void EMISound::flushStack() {
00790     Common::StackLock lock(_mutex);
00791     while (!_stateStack.empty()) {
00792         SoundTrack *temp = _stateStack.pop()._track;
00793         delete temp;
00794     }
00795 }
00796 
00797 void EMISound::pause(bool paused) {
00798     Common::StackLock lock(_mutex);
00799 
00800     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00801         SoundTrack *track = (*it);
00802         if (paused == track->isPaused())
00803             continue;
00804 
00805         // Do not pause music.
00806         if (track == _musicTrack)
00807             continue;
00808 
00809         track->pause();
00810     }
00811 
00812     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
00813         SoundTrack *track = (*it)._value;
00814         if (!track->isPlaying() || paused == track->isPaused())
00815             continue;
00816 
00817         track->pause();
00818     }
00819 }
00820 
00821 void EMISound::callback() {
00822     Common::StackLock lock(_mutex);
00823 
00824     if (_musicTrack) {
00825         updateTrack(_musicTrack);
00826     }
00827 
00828     for (uint i = 0; i < _stateStack.size(); ++i) {
00829         SoundTrack *track = _stateStack[i]._track;
00830         if (track == nullptr || track->isPaused() || !track->isPlaying())
00831             continue;
00832 
00833         updateTrack(track);
00834         if (track->getFadeMode() == SoundTrack::FadeOut && track->getFade() == 0.0f) {
00835             track->pause();
00836         }
00837     }
00838 
00839     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00840         SoundTrack *track = (*it);
00841         if (track->isPaused() || !track->isPlaying())
00842             continue;
00843 
00844         updateTrack(track);
00845         if (track->getFadeMode() == SoundTrack::FadeOut && track->getFade() == 0.0f) {
00846             track->stop();
00847         }
00848     }
00849 }
00850 
00851 void EMISound::updateTrack(SoundTrack *track) {
00852     if (track->getFadeMode() != SoundTrack::FadeNone) {
00853         float fadeStep = 0.5f / _callbackFps;
00854         float fade = track->getFade();
00855         if (track->getFadeMode() == SoundTrack::FadeIn) {
00856             fade += fadeStep;
00857             if (fade > 1.0f)
00858                 fade = 1.0f;
00859             track->setFade(fade);
00860         }
00861         else {
00862             fade -= fadeStep;
00863             if (fade < 0.0f)
00864                 fade = 0.0f;
00865             track->setFade(fade);
00866         }
00867     }
00868 }
00869 
00870 void EMISound::flushTracks() {
00871     Common::StackLock lock(_mutex);
00872     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00873         SoundTrack *track = (*it);
00874         if (!track->isPlaying()) {
00875             delete track;
00876             it = _playingTracks.erase(it);
00877         }
00878     }
00879 }
00880 
00881 void EMISound::restoreState(SaveGame *savedState) {
00882     Common::StackLock lock(_mutex);
00883     // Clear any current music
00884     flushStack();
00885     setMusicState(0);
00886     freePlayingSounds();
00887     freeLoadedSounds();
00888     delete _musicTrack;
00889     _musicTrack = nullptr;
00890     // Actually load:
00891     savedState->beginSection('SOUN');
00892     _musicPrefix = savedState->readString();
00893     if (savedState->saveMinorVersion() >= 21) {
00894         _curMusicState = savedState->readLESint32();
00895     }
00896 
00897     // Stack:
00898     uint32 stackSize = savedState->readLEUint32();
00899     for (uint32 i = 0; i < stackSize; i++) {
00900         SoundTrack *track = nullptr;
00901         int state = 0;
00902         if (savedState->saveMinorVersion() >= 21) {
00903             state = savedState->readLESint32();
00904             bool hasTrack = savedState->readBool();
00905             if (hasTrack) {
00906                 track = restoreTrack(savedState);
00907             }
00908         } else {
00909             Common::String soundName = savedState->readString();
00910             track = initTrack(soundName, Audio::Mixer::kMusicSoundType);
00911             if (track) {
00912                 track->play();
00913                 track->pause();
00914             }
00915         }
00916         StackEntry entry = { state, track };
00917         _stateStack.push(entry);
00918     }
00919 
00920     // Music:
00921     if (savedState->saveMinorVersion() < 21) {
00922         uint32 hasActiveTrack = savedState->readLEUint32();
00923         if (hasActiveTrack) {
00924             Common::String soundName = savedState->readString();
00925             _musicTrack = initTrack(soundName, Audio::Mixer::kMusicSoundType);
00926             if (_musicTrack) {
00927                 _musicTrack->play();
00928             } else {
00929                 error("Couldn't reopen %s", soundName.c_str());
00930             }
00931         }
00932     } else if (savedState->saveMinorVersion() >= 21) {
00933         bool musicActive = savedState->readBool();
00934         if (musicActive) {
00935             _musicTrack = restoreTrack(savedState);
00936         }
00937     }
00938 
00939     // Effects and voices:
00940     uint32 numTracks = savedState->readLEUint32();
00941     for (uint32 i = 0; i < numTracks; i++) {
00942         bool channelIsActive = true;
00943         if (savedState->saveMinorVersion() < 21) {
00944             channelIsActive = (savedState->readLESint32() != 0);
00945         }
00946         if (channelIsActive) {
00947             SoundTrack *track = restoreTrack(savedState);
00948             _playingTracks.push_back(track);
00949         }
00950     }
00951 
00952     // Preloaded sounds:
00953     if (savedState->saveMinorVersion() >= 21) {
00954         _curTrackId = savedState->readLESint32();
00955         uint32 numLoaded = savedState->readLEUint32();
00956         for (uint32 i = 0; i < numLoaded; ++i) {
00957             int id = savedState->readLESint32();
00958             _preloadedTrackMap[id] = restoreTrack(savedState);
00959         }
00960     }
00961 
00962     savedState->endSection();
00963 }
00964 
00965 void EMISound::saveState(SaveGame *savedState) {
00966     Common::StackLock lock(_mutex);
00967     savedState->beginSection('SOUN');
00968     savedState->writeString(_musicPrefix);
00969     savedState->writeLESint32(_curMusicState);
00970 
00971     // Stack:
00972     uint32 stackSize = _stateStack.size();
00973     savedState->writeLEUint32(stackSize);
00974     for (uint32 i = 0; i < stackSize; i++) {
00975         savedState->writeLESint32(_stateStack[i]._state);
00976         if (!_stateStack[i]._track) {
00977             savedState->writeBool(false);
00978         } else {
00979             savedState->writeBool(true);
00980             saveTrack(_stateStack[i]._track, savedState);
00981         }
00982     }
00983 
00984     // Music:
00985     savedState->writeBool(_musicTrack != nullptr);
00986     if (_musicTrack) {
00987         saveTrack(_musicTrack, savedState);
00988     }
00989 
00990     // Effects and voices:
00991     savedState->writeLEUint32(_playingTracks.size());
00992     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00993         saveTrack((*it), savedState);
00994     }
00995 
00996     // Preloaded sounds:
00997     savedState->writeLESint32(_curTrackId);
00998     uint32 numLoaded = _preloadedTrackMap.size();
00999     savedState->writeLEUint32(numLoaded);
01000     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
01001         savedState->writeLESint32(it->_key);
01002         saveTrack(it->_value, savedState);
01003     }
01004 
01005     savedState->endSection();
01006 }
01007 
01008 void EMISound::saveTrack(SoundTrack *track, SaveGame *savedState) {
01009     savedState->writeString(track->getSoundName());
01010     savedState->writeLEUint32(track->getVolume());
01011     savedState->writeLEUint32(track->getBalance());
01012     savedState->writeLEUint32(track->getPos().msecs());
01013     savedState->writeBool(track->isPlaying());
01014     savedState->writeBool(track->isPaused());
01015     savedState->writeLESint32((int)track->getSoundType());
01016     savedState->writeLESint32((int)track->getFadeMode());
01017     savedState->writeFloat(track->getFade());
01018     savedState->writeLESint32(track->getSync());
01019     savedState->writeBool(track->isLooping());
01020     savedState->writeBool(track->isPositioned());
01021     savedState->writeVector3d(track->getWorldPos());
01022 }
01023 
01024 SoundTrack *EMISound::restoreTrack(SaveGame *savedState) {
01025     Common::String soundName = savedState->readString();
01026     int volume = savedState->readLESint32();
01027     int balance = savedState->readLESint32();
01028     Audio::Timestamp pos(savedState->readLESint32());
01029     bool playing = savedState->readBool();
01030     if (savedState->saveMinorVersion() < 21) {
01031         SoundTrack *track = initTrack(soundName, Audio::Mixer::kSpeechSoundType);
01032         if (track)
01033             track->play();
01034         return track;
01035     }
01036     bool paused = savedState->readBool();
01037     Audio::Mixer::SoundType soundType = (Audio::Mixer::SoundType)savedState->readLESint32();
01038     SoundTrack::FadeMode fadeMode = (SoundTrack::FadeMode)savedState->readLESint32();
01039     float fade = savedState->readFloat();
01040     int sync = savedState->readLESint32();
01041     bool looping = savedState->saveMinorVersion() >= 21 ? savedState->readBool() : false;
01042     bool positioned = false;
01043     Math::Vector3d worldPos;
01044     if (savedState->saveMinorVersion() >= 23) {
01045         positioned = savedState->readBool();
01046         worldPos = savedState->readVector3d();
01047     }
01048 
01049     SoundTrack *track = initTrack(soundName, soundType, &pos);
01050     track->setVolume(volume);
01051     track->setBalance(balance);
01052     track->setPosition(positioned, worldPos);
01053     track->setLooping(looping);
01054     track->setFadeMode(fadeMode);
01055     track->setFade(fade);
01056     track->setSync(sync);
01057     if (playing)
01058         track->play();
01059     if (paused)
01060         track->pause();
01061     return track;
01062 }
01063 
01064 void EMISound::updateSoundPositions() {
01065     Common::StackLock lock(_mutex);
01066 
01067     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
01068         SoundTrack *track = (*it);
01069         track->updatePosition();
01070     }
01071 
01072     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
01073         SoundTrack *track = (*it)._value;
01074         track->updatePosition();
01075     }
01076 }
01077 
01078 } // end of namespace Grim


Generated on Sat May 18 2019 05:01:01 for ResidualVM by doxygen 1.7.1
curved edge   curved edge