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 MusicEntry *EMISound::initMusicTableRetail(MusicEntry *table, 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         delete[] table;
00616         return nullptr;
00617     }
00618     
00619     MusicEntry *musicTable = table;
00620     if (!table) {
00621         _numMusicStates = 126;
00622         musicTable = new MusicEntry[_numMusicStates];
00623         for (int i = 0; i < 126; ++i) {
00624             musicTable->_x = 0;
00625             musicTable->_y = 0;
00626             musicTable->_sync = 0;
00627             musicTable->_trim = 0;
00628             musicTable->_id = i;
00629         }
00630     }
00631 
00632     TextSplitter *ts = new TextSplitter(filename, data);
00633     int id, x, y, sync, trim;
00634     char musicfilename[64];
00635     char type[16];
00636     // Every block is followed by 3 lines of commenting/uncommenting, except the last.
00637     while (!ts->isEof()) {
00638         while (!ts->checkString("*/")) {
00639             while (!ts->checkString(".cuebutton"))
00640                 ts->nextLine();
00641 
00642             ts->scanString(".cuebutton id %d x %d y %d sync %d type %16s", 5, &id, &x, &y, &sync, type);
00643             ts->scanString(".playfile trim %d \"%[^\"]64s", 2, &trim, musicfilename);
00644             if (musicfilename[1] == '\\')
00645                 musicfilename[1] = '/';
00646             musicTable[id]._id = id;
00647             musicTable[id]._x = x;
00648             musicTable[id]._y = y;
00649             musicTable[id]._sync = sync;
00650             musicTable[id]._type = type;
00651             musicTable[id]._name = "";
00652             musicTable[id]._trim = trim;
00653             musicTable[id]._filename = musicfilename;
00654         }
00655         ts->nextLine();
00656     }
00657     delete ts;
00658     delete data;
00659     return musicTable;
00660 }
00661 
00662 void tableLoadErrorDialog(const char *filename) {
00663     const char *errorMessage = nullptr;
00664     errorMessage =  "ERROR: Missing file for music-support.\n"
00665     "Escape from Monkey Island has two versions of FullMonkeyMap.imt,\n"
00666     "you need to copy both files from both CDs to Textures/, and rename\n"
00667     "them as follows to get music-support in-game: \n"
00668     "CD 1: \"FullMonkeyMap.imt\" -> \"FullMonkeyMap1.imt\"\n"
00669     "CD 2: \"FullMonkeyMap.imt\" -> \"FullMonkeyMap2.imt\"";
00670     GUI::displayErrorDialog(errorMessage);
00671     error("Missing file %s", filename);
00672 }
00673 
00674 void EMISound::initMusicTable() {
00675     if (g_grim->getGameFlags() & ADGF_DEMO) {
00676         _musicTable = initMusicTableDemo("Music/FullMonkeyMap.imt");
00677         _musicPrefix = "Music/";
00678     } else if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
00679         _musicTable = emiPS2MusicTable;
00680         _numMusicStates = ARRAYSIZE(emiPS2MusicTable);
00681         _musicPrefix = "";
00682     } else {
00683         _musicTable = nullptr;
00684         _musicTable = initMusicTableRetail(_musicTable, "Textures/FullMonkeyMap1.imt");
00685         if (_musicTable == nullptr) {
00686             tableLoadErrorDialog("Textures/FullMonkeyMap1.imt");
00687         }
00688         _musicTable = initMusicTableRetail(_musicTable, "Textures/FullMonkeyMap2.imt");
00689         if (_musicTable == nullptr) {
00690             tableLoadErrorDialog("Textures/FullMonkeyMap2.imt");
00691         }
00692         _musicPrefix = "Textures/spago/"; // Default to high-quality music.
00693     }
00694 }
00695 
00696 void EMISound::selectMusicSet(int setId) {
00697     if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
00698         assert(setId == 0);
00699         _musicPrefix = "";
00700         return;
00701     }
00702     if (setId == 0) {
00703         _musicPrefix = "Textures/spago/";
00704     } else if (setId == 1) {
00705         _musicPrefix = "Textures/mego/";
00706     } else {
00707         error("EMISound::selectMusicSet - Unknown setId %d", setId);
00708     }
00709 
00710     // Immediately switch all currently active music tracks to the new quality.
00711     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00712         SoundTrack *track = (*it);
00713         if (track && track->getSoundType() == Audio::Mixer::kMusicSoundType) {
00714             (*it) = restartTrack(track);
00715             delete track;
00716         }
00717     }
00718     for (uint32 i = 0; i < _stateStack.size(); ++i) {
00719         SoundTrack *track = _stateStack[i]._track;
00720         if (track) {
00721             _stateStack[i]._track = restartTrack(track);
00722             delete track;
00723         }
00724     }
00725 }
00726 
00727 SoundTrack *EMISound::restartTrack(SoundTrack *track) {
00728     Audio::Timestamp pos = track->getPos();
00729     SoundTrack *newTrack = initTrack(track->getSoundName(), track->getSoundType(), &pos);
00730     if (newTrack) {
00731         newTrack->setVolume(track->getVolume());
00732         newTrack->setBalance(track->getBalance());
00733         newTrack->setFadeMode(track->getFadeMode());
00734         newTrack->setFade(track->getFade());
00735         if (track->isPlaying()) {
00736             newTrack->play();
00737         }
00738         if (track->isPaused()) {
00739             newTrack->pause();
00740         }
00741     }
00742     return newTrack;
00743 }
00744 
00745 void EMISound::pushStateToStack() {
00746     Common::StackLock lock(_mutex);
00747     if (_musicTrack) {
00748         _musicTrack->fadeOut();
00749         StackEntry entry = { _curMusicState, _musicTrack };
00750         _stateStack.push(entry);
00751         _musicTrack = nullptr;
00752     } else {
00753         StackEntry entry = { _curMusicState, nullptr };
00754         _stateStack.push(entry);
00755     }
00756     _curMusicState = 0;
00757 }
00758 
00759 void EMISound::popStateFromStack() {
00760     Common::StackLock lock(_mutex);
00761     if (_musicTrack) {
00762         _musicTrack->fadeOut();
00763         _playingTracks.push_back(_musicTrack);
00764     }
00765 
00766     //even pop state from stack if music isn't set
00767     StackEntry entry = _stateStack.pop();
00768     SoundTrack *track = entry._track;
00769 
00770     _musicTrack = track;
00771     _curMusicState = entry._state;
00772 
00773     if (track) {
00774         if (track->isPaused()) {
00775             track->pause();
00776         }
00777         track->fadeIn();
00778     }
00779 }
00780 
00781 void EMISound::flushStack() {
00782     Common::StackLock lock(_mutex);
00783     while (!_stateStack.empty()) {
00784         SoundTrack *temp = _stateStack.pop()._track;
00785         delete temp;
00786     }
00787 }
00788 
00789 void EMISound::pause(bool paused) {
00790     Common::StackLock lock(_mutex);
00791 
00792     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00793         SoundTrack *track = (*it);
00794         if (paused == track->isPaused())
00795             continue;
00796 
00797         // Do not pause music.
00798         if (track == _musicTrack)
00799             continue;
00800 
00801         track->pause();
00802     }
00803 
00804     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
00805         SoundTrack *track = (*it)._value;
00806         if (!track->isPlaying() || paused == track->isPaused())
00807             continue;
00808 
00809         track->pause();
00810     }
00811 }
00812 
00813 void EMISound::callback() {
00814     Common::StackLock lock(_mutex);
00815 
00816     if (_musicTrack) {
00817         updateTrack(_musicTrack);
00818     }
00819 
00820     for (uint i = 0; i < _stateStack.size(); ++i) {
00821         SoundTrack *track = _stateStack[i]._track;
00822         if (track == nullptr || track->isPaused() || !track->isPlaying())
00823             continue;
00824 
00825         updateTrack(track);
00826         if (track->getFadeMode() == SoundTrack::FadeOut && track->getFade() == 0.0f) {
00827             track->pause();
00828         }
00829     }
00830 
00831     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00832         SoundTrack *track = (*it);
00833         if (track->isPaused() || !track->isPlaying())
00834             continue;
00835 
00836         updateTrack(track);
00837         if (track->getFadeMode() == SoundTrack::FadeOut && track->getFade() == 0.0f) {
00838             track->stop();
00839         }
00840     }
00841 }
00842 
00843 void EMISound::updateTrack(SoundTrack *track) {
00844     if (track->getFadeMode() != SoundTrack::FadeNone) {
00845         float fadeStep = 0.5f / _callbackFps;
00846         float fade = track->getFade();
00847         if (track->getFadeMode() == SoundTrack::FadeIn) {
00848             fade += fadeStep;
00849             if (fade > 1.0f)
00850                 fade = 1.0f;
00851             track->setFade(fade);
00852         }
00853         else {
00854             fade -= fadeStep;
00855             if (fade < 0.0f)
00856                 fade = 0.0f;
00857             track->setFade(fade);
00858         }
00859     }
00860 }
00861 
00862 void EMISound::flushTracks() {
00863     Common::StackLock lock(_mutex);
00864     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00865         SoundTrack *track = (*it);
00866         if (!track->isPlaying()) {
00867             delete track;
00868             it = _playingTracks.erase(it);
00869         }
00870     }
00871 }
00872 
00873 void EMISound::restoreState(SaveGame *savedState) {
00874     Common::StackLock lock(_mutex);
00875     // Clear any current music
00876     flushStack();
00877     setMusicState(0);
00878     freePlayingSounds();
00879     freeLoadedSounds();
00880     delete _musicTrack;
00881     _musicTrack = nullptr;
00882     // Actually load:
00883     savedState->beginSection('SOUN');
00884     _musicPrefix = savedState->readString();
00885     if (savedState->saveMinorVersion() >= 21) {
00886         _curMusicState = savedState->readLESint32();
00887     }
00888 
00889     // Stack:
00890     uint32 stackSize = savedState->readLEUint32();
00891     for (uint32 i = 0; i < stackSize; i++) {
00892         SoundTrack *track = nullptr;
00893         int state = 0;
00894         if (savedState->saveMinorVersion() >= 21) {
00895             state = savedState->readLESint32();
00896             bool hasTrack = savedState->readBool();
00897             if (hasTrack) {
00898                 track = restoreTrack(savedState);
00899             }
00900         } else {
00901             Common::String soundName = savedState->readString();
00902             track = initTrack(soundName, Audio::Mixer::kMusicSoundType);
00903             if (track) {
00904                 track->play();
00905                 track->pause();
00906             }
00907         }
00908         StackEntry entry = { state, track };
00909         _stateStack.push(entry);
00910     }
00911 
00912     // Music:
00913     if (savedState->saveMinorVersion() < 21) {
00914         uint32 hasActiveTrack = savedState->readLEUint32();
00915         if (hasActiveTrack) {
00916             Common::String soundName = savedState->readString();
00917             _musicTrack = initTrack(soundName, Audio::Mixer::kMusicSoundType);
00918             if (_musicTrack) {
00919                 _musicTrack->play();
00920             } else {
00921                 error("Couldn't reopen %s", soundName.c_str());
00922             }
00923         }
00924     } else if (savedState->saveMinorVersion() >= 21) {
00925         bool musicActive = savedState->readBool();
00926         if (musicActive) {
00927             _musicTrack = restoreTrack(savedState);
00928         }
00929     }
00930 
00931     // Effects and voices:
00932     uint32 numTracks = savedState->readLEUint32();
00933     for (uint32 i = 0; i < numTracks; i++) {
00934         bool channelIsActive = true;
00935         if (savedState->saveMinorVersion() < 21) {
00936             channelIsActive = (savedState->readLESint32() != 0);
00937         }
00938         if (channelIsActive) {
00939             SoundTrack *track = restoreTrack(savedState);
00940             _playingTracks.push_back(track);
00941         }
00942     }
00943 
00944     // Preloaded sounds:
00945     if (savedState->saveMinorVersion() >= 21) {
00946         _curTrackId = savedState->readLESint32();
00947         uint32 numLoaded = savedState->readLEUint32();
00948         for (uint32 i = 0; i < numLoaded; ++i) {
00949             int id = savedState->readLESint32();
00950             _preloadedTrackMap[id] = restoreTrack(savedState);
00951         }
00952     }
00953 
00954     savedState->endSection();
00955 }
00956 
00957 void EMISound::saveState(SaveGame *savedState) {
00958     Common::StackLock lock(_mutex);
00959     savedState->beginSection('SOUN');
00960     savedState->writeString(_musicPrefix);
00961     savedState->writeLESint32(_curMusicState);
00962 
00963     // Stack:
00964     uint32 stackSize = _stateStack.size();
00965     savedState->writeLEUint32(stackSize);
00966     for (uint32 i = 0; i < stackSize; i++) {
00967         savedState->writeLESint32(_stateStack[i]._state);
00968         if (!_stateStack[i]._track) {
00969             savedState->writeBool(false);
00970         } else {
00971             savedState->writeBool(true);
00972             saveTrack(_stateStack[i]._track, savedState);
00973         }
00974     }
00975 
00976     // Music:
00977     savedState->writeBool(_musicTrack != nullptr);
00978     if (_musicTrack) {
00979         saveTrack(_musicTrack, savedState);
00980     }
00981 
00982     // Effects and voices:
00983     savedState->writeLEUint32(_playingTracks.size());
00984     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
00985         saveTrack((*it), savedState);
00986     }
00987 
00988     // Preloaded sounds:
00989     savedState->writeLESint32(_curTrackId);
00990     uint32 numLoaded = _preloadedTrackMap.size();
00991     savedState->writeLEUint32(numLoaded);
00992     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
00993         savedState->writeLESint32(it->_key);
00994         saveTrack(it->_value, savedState);
00995     }
00996 
00997     savedState->endSection();
00998 }
00999 
01000 void EMISound::saveTrack(SoundTrack *track, SaveGame *savedState) {
01001     savedState->writeString(track->getSoundName());
01002     savedState->writeLEUint32(track->getVolume());
01003     savedState->writeLEUint32(track->getBalance());
01004     savedState->writeLEUint32(track->getPos().msecs());
01005     savedState->writeBool(track->isPlaying());
01006     savedState->writeBool(track->isPaused());
01007     savedState->writeLESint32((int)track->getSoundType());
01008     savedState->writeLESint32((int)track->getFadeMode());
01009     savedState->writeFloat(track->getFade());
01010     savedState->writeLESint32(track->getSync());
01011     savedState->writeBool(track->isLooping());
01012     savedState->writeBool(track->isPositioned());
01013     savedState->writeVector3d(track->getWorldPos());
01014 }
01015 
01016 SoundTrack *EMISound::restoreTrack(SaveGame *savedState) {
01017     Common::String soundName = savedState->readString();
01018     int volume = savedState->readLESint32();
01019     int balance = savedState->readLESint32();
01020     Audio::Timestamp pos(savedState->readLESint32());
01021     bool playing = savedState->readBool();
01022     if (savedState->saveMinorVersion() < 21) {
01023         SoundTrack *track = initTrack(soundName, Audio::Mixer::kSpeechSoundType);
01024         if (track)
01025             track->play();
01026         return track;
01027     }
01028     bool paused = savedState->readBool();
01029     Audio::Mixer::SoundType soundType = (Audio::Mixer::SoundType)savedState->readLESint32();
01030     SoundTrack::FadeMode fadeMode = (SoundTrack::FadeMode)savedState->readLESint32();
01031     float fade = savedState->readFloat();
01032     int sync = savedState->readLESint32();
01033     bool looping = savedState->saveMinorVersion() >= 21 ? savedState->readBool() : false;
01034     bool positioned = false;
01035     Math::Vector3d worldPos;
01036     if (savedState->saveMinorVersion() >= 23) {
01037         positioned = savedState->readBool();
01038         worldPos = savedState->readVector3d();
01039     }
01040 
01041     SoundTrack *track = initTrack(soundName, soundType, &pos);
01042     track->setVolume(volume);
01043     track->setBalance(balance);
01044     track->setPosition(positioned, worldPos);
01045     track->setLooping(looping);
01046     track->setFadeMode(fadeMode);
01047     track->setFade(fade);
01048     track->setSync(sync);
01049     if (playing)
01050         track->play();
01051     if (paused)
01052         track->pause();
01053     return track;
01054 }
01055 
01056 void EMISound::updateSoundPositions() {
01057     Common::StackLock lock(_mutex);
01058 
01059     for (TrackList::iterator it = _playingTracks.begin(); it != _playingTracks.end(); ++it) {
01060         SoundTrack *track = (*it);
01061         track->updatePosition();
01062     }
01063 
01064     for (TrackMap::iterator it = _preloadedTrackMap.begin(); it != _preloadedTrackMap.end(); ++it) {
01065         SoundTrack *track = (*it)._value;
01066         track->updatePosition();
01067     }
01068 }
01069 
01070 } // end of namespace Grim


Generated on Sat Feb 16 2019 05:00:50 for ResidualVM by doxygen 1.7.1
curved edge   curved edge