diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index f5cdcd87..2673d6e8 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -3983,6 +3983,11 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) { m_audioTrack = OpInt(line, "track", 0); m_audioRepeat = OpInt(line, "repeat", 1); + if(m_audioTrack != 0) { + std::stringstream filename; + filename << "music" << std::setfill('0') << std::setw(3) << m_audioTrack << ".ogg"; + m_sound->CacheMusic(filename.str()); + } } if (Cmd(line, "AudioChange") && !resetObject && m_version >= 2) @@ -3998,6 +4003,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) OpString(line, "filename", m_audioChange[i].music); m_audioChange[i].repeat = OpInt(line, "repeat", 1); m_audioChange[i].changed = false; + m_sound->CacheMusic(m_audioChange[i].music); m_audioChangeTotal ++; } } diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 48fcc15e..50564e93 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -52,6 +52,10 @@ void ALSound::CleanUp() delete item.second; } + for (auto item : mMusic) { + delete item.second; + } + mEnabled = false; mCurrentMusic->FreeBuffer(); @@ -160,6 +164,18 @@ bool ALSound::Cache(Sound sound, std::string filename) return false; } +bool ALSound::CacheMusic(std::string filename) +{ + Buffer *buffer = new Buffer(); + std::stringstream file; + file << m_soundPath << "/" << filename; + if (buffer->LoadFromFile(file.str(), static_cast(-1))) { + mMusic[filename] = buffer; + return true; + } + return false; +} + int ALSound::GetPriority(Sound sound) { @@ -555,16 +571,20 @@ bool ALSound::PlayMusic(std::string filename, bool bRepeat) std::stringstream file; file << m_soundPath << "/" << filename; - if (!boost::filesystem::exists(file.str())) { - GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str()); - return false; + // check if we have music in cache + if (mMusic.find(filename) == mMusic.end()) { + GetLogger()->Warn("Music %s was not cached!\n", filename.c_str()); + if (!boost::filesystem::exists(file.str())) { + GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str()); + return false; + } + Buffer *buffer = new Buffer(); + buffer->LoadFromFile(file.str(), static_cast(-1)); + mCurrentMusic->SetBuffer(buffer); + } else { + GetLogger()->Debug("Music loaded from cache\n"); + mCurrentMusic->SetBuffer(mMusic[filename]); } - - // TODO: Cache - - Buffer *buffer = new Buffer(); - buffer->LoadFromFile(file.str(), static_cast(-1)); - mCurrentMusic->SetBuffer(buffer); mCurrentMusic->SetVolume(mMusicVolume); mCurrentMusic->SetLoop(bRepeat); diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 7e0503af..b8afd4d1 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -40,6 +40,7 @@ class ALSound : public CSoundInterface bool Create(bool b3D); bool Cache(Sound, std::string); + bool CacheMusic(std::string); bool GetEnable(); @@ -91,6 +92,7 @@ class ALSound : public CSoundInterface ALCdevice* mDevice; ALCcontext* mContext; std::map mSounds; + std::map mMusic; std::map mChannels; Channel *mCurrentMusic; Math::Vector mEye; diff --git a/src/sound/sound.h b/src/sound/sound.h index f1015186..9d0e9a05 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -178,6 +178,8 @@ class CSoundInterface /** Function called to add all music files to list */ inline void AddMusicFiles(std::string path) { m_soundPath = path; + CacheMusic("sound010.ogg"); + CacheMusic("sound011.ogg"); }; /** Function called to cache sound effect file. @@ -188,6 +190,13 @@ class CSoundInterface */ inline virtual bool Cache(Sound bSound, std::string bFile) { return true; }; + /** Function called to cache music file. + * This function is called by CRobotMain for each file used in the mission. + * \param bFile - file to load + * \return return true on success + */ + inline virtual bool CacheMusic(std::string bFile) { return true; }; + /** Return if plugin is enabled * \return return true if plugin is enabled */