From 2fe66319fe6598c08727a084c535455714a911ea Mon Sep 17 00:00:00 2001 From: Jason Hooks Date: Sun, 12 Jun 2011 16:36:18 -0400 Subject: [PATCH] streammusic strict --- apps/openmw/mwsound/soundmanager.cpp | 119 ++++++++++++++++++--------- apps/openmw/mwsound/soundmanager.hpp | 9 +- 2 files changed, 85 insertions(+), 43 deletions(-) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 72e62421f..abff70664 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -87,15 +87,18 @@ namespace MWSound bool FSstrict; FileFinder::FileFinder files; FileFinder::FileFinderStrict strict; + FileFinder::FileFinder musicpath; + FileFinder::FileFinderStrict musicpathStrict; SoundImpl(Ogre::Root *root, Ogre::Camera *camera, const ESMS::ESMStore &str, - const std::string &soundDir, bool fsstrict) + const std::string &soundDir, const std::string &musicDir, bool fsstrict) : mgr(new OEManager(SoundFactoryPtr(new SOUND_FACTORY))) , updater(mgr) , cameraTracker(mgr) , store(str) , files(soundDir), strict(soundDir) + ,musicpath(musicDir), musicpathStrict(musicDir) { FSstrict = fsstrict; cout << "Sound output: " << SOUND_OUT << endl; @@ -113,6 +116,8 @@ namespace MWSound cameraTracker.unfollowCamera(); } + + static std::string toMp3(std::string str) { std::string::size_type i = str.rfind('.'); @@ -124,44 +129,70 @@ namespace MWSound return str; } - bool hasFile(const std::string &str) + bool hasFile(const std::string &str, bool music = false) { if(FSstrict == false){ - if(files.has(str)) return true; - // Not found? Try with .mp3 - return files.has(toMp3(str)); + if(music){ + if(musicpath.has(str)) return true; + // Not found? Try with .mp3 + return musicpath.has(toMp3(str)); + } + else + { + if(files.has(str)) return true; + return files.has(toMp3(str)); + } + } + else + { + if(music){ + if(musicpathStrict.has(str)) return true; + // Not found? Try with .mp3 + return musicpathStrict.has(toMp3(str)); + } + else + { + if(strict.has(str)) return true; + return strict.has(toMp3(str)); + } + } - else{ - if(strict.has(str)) return true; - // Not found? Try with .mp3 - return files.has(toMp3(str)); - } } // Convert a Morrowind sound path (eg. Fx\funny.wav) to full path // with proper slash conversion (eg. datadir/Sound/Fx/funny.wav) - std::string convertPath(const std::string &str) + std::string convertPath(const std::string &str, bool music = false) { if(FSstrict == false){ // Search and return - if(files.has(str)) - return files.lookup(str); + if(music && musicpath.has(str)) + return musicpath.lookup(str); + else if(files.has(str)) + return files.lookup(str); + // Try mp3 if the wav wasn't found std::string mp3 = toMp3(str); - if(files.has(mp3)) - return files.lookup(mp3); + if(music && musicpath.has(mp3)) + return musicpath.lookup(mp3); + else if(files.has(mp3)) + return files.lookup(mp3); } else { - if(files.has(str)) - return files.lookup(str); + if(music && musicpathStrict.has(str)) + return musicpathStrict.lookup(str); + else if(strict.has(str)) + return strict.lookup(str); + - // Try mp3 if the wav wasn't found - std::string mp3 = toMp3(str); - if(files.has(mp3)) - return files.lookup(mp3); + // Try mp3 if the wav wasn't found + std::string mp3 = toMp3(str); + if(music && musicpathStrict.has(mp3)) + return musicpathStrict.lookup(mp3); + else if(strict.has(str)) + return strict.lookup(mp3); } // Give up @@ -326,6 +357,22 @@ namespace MWSound } }; + void SoundManager::streamMusicFull (const std::string& filename) + { + if(!mData) return; + + // Play the sound and tell it to stream, if possible. TODO: + // Store the reference, the jukebox will need to check status, + // control volume etc. + if (mData->music) + mData->music->stop(); + mData->music = mData->mgr->load(filename); + mData->music->setStreaming(true); + mData->music->setVolume(0.4); + mData->music->play(); + + } + SoundManager::SoundManager(Ogre::Root *root, Ogre::Camera *camera, const ESMS::ESMStore &store, boost::filesystem::path dataDir, @@ -335,7 +382,7 @@ namespace MWSound fsStrict = fsstrict; MP3Lookup(dataDir / "Music/Explore/"); if(useSound) - mData = new SoundImpl(root, camera, store, (dataDir / "Sound").string(), fsstrict); + mData = new SoundImpl(root, camera, store, (dataDir / "Sound").string(), (dataDir / "Music").string(), fsstrict); } SoundManager::~SoundManager() @@ -344,6 +391,15 @@ namespace MWSound delete mData; } + void SoundManager::streamMusic(const std::string& filename){ + if(mData->hasFile(filename, true)) + { + std::string fullpath = mData->convertPath(filename, true); + streamMusicFull(fullpath); + } + } + + void SoundManager::MP3Lookup(boost::filesystem::path dir) { boost::filesystem::directory_iterator dir_iter(dir), dir_end; @@ -376,7 +432,7 @@ namespace MWSound try { std::cout << "Playing " << music << "\n"; - streamMusic(music); + streamMusicFull(music); } catch(std::exception &e) { @@ -420,22 +476,7 @@ namespace MWSound return !mData->isPlaying(ptr, "_say_sound"); } - void SoundManager::streamMusic (const std::string& filename) - { - if(!mData) return; - - // Play the sound and tell it to stream, if possible. TODO: - // Store the reference, the jukebox will need to check status, - // control volume etc. - if (mData->music) - mData->music->stop(); - mData->music = mData->mgr->load(filename); - mData->music->setStreaming(true); - mData->music->setVolume(0.4); - mData->music->play(); - - } - + void SoundManager::playSound (const std::string& soundId, float volume, float pitch) { if(!mData) return; diff --git a/apps/openmw/mwsound/soundmanager.hpp b/apps/openmw/mwsound/soundmanager.hpp index b76e71e39..70e6babf9 100644 --- a/apps/openmw/mwsound/soundmanager.hpp +++ b/apps/openmw/mwsound/soundmanager.hpp @@ -31,6 +31,9 @@ namespace MWSound SoundImpl *mData; std::vector files; bool fsStrict; + void streamMusicFull (const std::string& filename); + ///< Play a soundifle + /// \param filename name of a sound file in "Music/" in the data directory. public: @@ -39,7 +42,7 @@ namespace MWSound boost::filesystem::path dataDir, bool useSound, bool fsstrict); ~SoundManager(); - + void streamMusic(const std::string& filename); void startRandomTitle(); void MP3Lookup(boost::filesystem::path dir); //struct SoundImpl; @@ -54,9 +57,7 @@ namespace MWSound bool sayDone (MWWorld::Ptr reference) const; ///< Is actor not speaking? - void streamMusic (const std::string& filename); - ///< Play a soundifle - /// \param filename name of a sound file in "Music/" in the data directory. + void playSound (const std::string& soundId, float volume, float pitch); ///< Play a sound, independently of 3D-position