History: Another FMOD SoundManager
Preview of version: 3
The interface
Copy to clipboard
#pragma once class ISoundManager{ public: virtual ~ISoundManager(){} virtual void Load2DSoundsFromFile(const Ogre::String &fileName)=0; virtual void Load3DSoundsFromFile(const Ogre::String &fileName)=0; virtual void UnloadSoundsFromFile(const Ogre::String &fileName)=0; virtual void Load2DPlaylistFromFile(const Ogre::String &fileName)=0; virtual void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity)=0; virtual void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward)=0; virtual void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward, const Ogre::Vector3 &up)=0; virtual void System3DSettings(const Ogre::Real &dopplerScale, const Ogre::Real &distanceFactor, const Ogre::Real rolloffScale)=0; virtual void SoundGroupMaxAudible(const Ogre::String &name, const int &maxAudible)=0; virtual void SoundGroupMuteFadeSpeed(const Ogre::String &name, const Ogre::Real &speed)=0; virtual void SoundGroupStop(const Ogre::String &name)=0; virtual void SoundGroupStopAll()=0; virtual void SoundGroupEmpty(const Ogre::String &name)=0; virtual void SoundGroupEmptyAll()=0; virtual void SoundGroupDestroy(const Ogre::String &name)=0; virtual void SoundGroupDestroyAll()=0; virtual void SoundGroupVolume(const Ogre::String &name, const Ogre::Real &volume)=0; virtual Ogre::Real GetSoundGroupVolume(const Ogre::String &name)=0; virtual void Sound2DLoopCount(const Ogre::String &name, const int &loopCount)=0; virtual void Sound3DLoopCount(const Ogre::String &name, const int &loopCount)=0; virtual void Sound3DConeSettings(const Ogre::String &name, const Ogre::Real &insideAngle, const Ogre::Real &outsideAngle, const Ogre::Real &outsideVolume)=0; virtual void Sound3DMinMaxDistance(const Ogre::String &name, const Ogre::Real &minDistance, const Ogre::Real &maxDistance)=0; virtual void ChannelGroupCreate(const Ogre::String &name)=0; virtual void ChannelGroupPause(const Ogre::String &name, const bool &isPaused)=0; virtual void ChannelGroupPauseAll(const bool &isPaused)=0; virtual void ChannelGroupMute(const Ogre::String &name, const bool &isMuted)=0; virtual void ChannelGroupMuteAll(const bool &isMuted)=0; virtual void ChannelGroupVolume(const Ogre::String &name, const Ogre::Real &volume)=0; virtual void ChannelGroupVolumeAll(const Ogre::Real &volume)=0; virtual void ChannelGroupStop(const Ogre::String &name)=0; virtual void ChannelGroupStopAll()=0; virtual void ChannelGroupDestroy(const Ogre::String &name)=0; virtual void ChannelGroupDestroyAll()=0; virtual void ChannelPause(const int &index, const bool &isPaused)=0; virtual void ChannelMute(const int &index, const bool &isMuted)=0; virtual void ChannelVolume(const int &index, const Ogre::Real &volume)=0; virtual void ChannelLoop(const int &index, const int &loopCount)=0; virtual void ChannelStop(const int &index)=0; virtual void Channel3DAttributes(const int &index, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity)=0; virtual void Channel3DConeOrientation(const int &index, const Ogre::Vector3 &orientation)=0; virtual void Channel3DConeSettings(const int &index, const Ogre::Real &insideAngle, const Ogre::Real &outsideAngle, const Ogre::Real &outsideVolume)=0; virtual void Channel3DDopplerLevel(const int &index, const Ogre::Real &level)=0; virtual void Channel3DMinMaxDistance(const int &index, const Ogre::Real &minDistance, const Ogre::Real &maxDistance)=0; virtual void PlaylistPlay(const Ogre::String &playlistName)=0; virtual void PlaylistPlayAll()=0; virtual void PlaylistSkip(const Ogre::String &playlistName)=0; virtual void PlaylistPause(const Ogre::String &playlistName)=0; virtual void PlaylistPauseAll()=0; virtual void PlaylistResume(const Ogre::String &playlistName)=0; virtual void PlaylistResumeAll()=0; virtual void PlaylistStop(const Ogre::String &playlistName)=0; virtual void PlatlistStopAll()=0; virtual void PlaylistDestroy(const Ogre::String &playlistName)=0; virtual void PlaylistDestroyAll()=0; virtual int Play2DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName)=0; virtual int Play2DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const int &loopCount)=0; virtual int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position)=0; virtual int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const int &loopCount)=0; virtual int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward)=0; virtual int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward, const int &loopCount)=0; };
The header
Copy to clipboard
#pragma once #include "ISoundManager.h" #include <fmod.hpp> #include "FMOD\fmod_errors.h" #include <hash_map> #include <fstream> #define SOUND_LOG_NAME "SoundManager.log" #define SOUND_LOG(x) Ogre::LogManager::getSingleton().getLog(SOUND_LOG_NAME)->logMessage(x) #define MAX_CHAR_ARRAY_SIZE 255 #define MAX_SOUND_CHANNELS 200 #define SONG_PRIORITY 0 #define SOUND_PRIORITY_2D 1 #define SOUND_PATH "..\\..\\res\\Audio\\" class SoundInstance{ public: SoundInstance(Ogre::String soundName, FMOD::Sound* sound): _soundName(soundName), _sound(sound) {} ~SoundInstance(){ _sound->release(); } Ogre::String _soundName; FMOD::Sound* _sound; }; class SoundManager : public ISoundManager{ public: void Load2DSoundsFromFile(const Ogre::String &fileName); void Load3DSoundsFromFile(const Ogre::String &fileName); void UnloadSoundsFromFile(const Ogre::String &fileName); void Load2DPlaylistFromFile(const Ogre::String &fileName); void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity); void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward); void System3DAttributes(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward, const Ogre::Vector3 &up); void System3DSettings(const Ogre::Real &dopplerScale, const Ogre::Real &distanceFactor, const Ogre::Real rolloffScale); void SoundGroupCreate(const Ogre::String &name); void SoundGroupMaxAudible(const Ogre::String &name, const int &maxAudible); void SoundGroupMuteFadeSpeed(const Ogre::String &name, const Ogre::Real &speed); void SoundGroupStop(const Ogre::String &name); void SoundGroupStopAll(); void SoundGroupEmpty(const Ogre::String &name); void SoundGroupEmptyAll(); void SoundGroupDestroy(const Ogre::String &name); void SoundGroupDestroyAll(); void SoundGroupVolume(const Ogre::String &name, const Ogre::Real &volume); Ogre::Real GetSoundGroupVolume(const Ogre::String &name); void Sound2DLoopCount(const Ogre::String &name, const int &loopCount); void Sound3DLoopCount(const Ogre::String &name, const int &loopCount); void Sound3DConeSettings(const Ogre::String &name, const Ogre::Real &insideAngle, const Ogre::Real &outsideAngle, const Ogre::Real &outsideVolume); void Sound3DMinMaxDistance(const Ogre::String &name, const Ogre::Real &minDistance, const Ogre::Real &maxDistance); void ChannelGroupCreate(const Ogre::String &name); void ChannelGroupPause(const Ogre::String &name, const bool &isPaused); void ChannelGroupPauseAll(const bool &isPaused); void ChannelGroupMute(const Ogre::String &name, const bool &isMuted); void ChannelGroupMuteAll(const bool &isMuted); void ChannelGroupVolume(const Ogre::String &name, const Ogre::Real &volume); void ChannelGroupVolumeAll(const Ogre::Real &volume); void ChannelGroupStop(const Ogre::String &name); void ChannelGroupStopAll(); void ChannelGroupDestroy(const Ogre::String &name); void ChannelGroupDestroyAll(); void ChannelPause(const int &index, const bool &isPaused); void ChannelMute(const int &index, const bool &isMuted); void ChannelVolume(const int &index, const Ogre::Real &volume); void ChannelLoop(const int &index, const int &loopCount); void ChannelStop(const int &index); void Channel3DAttributes(const int &index, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity); void Channel3DConeOrientation(const int &index, const Ogre::Vector3 &orientation); void Channel3DConeSettings(const int &index, const Ogre::Real &insideAngle, const Ogre::Real &outsideAngle, const Ogre::Real &outsideVolume); void Channel3DDopplerLevel(const int &index, const Ogre::Real &level); void Channel3DMinMaxDistance(const int &index, const Ogre::Real &minDistance, const Ogre::Real &maxDistance); void PlaylistPlay(const Ogre::String &playlistName); void PlaylistPlayAll(); void PlaylistSkip(const Ogre::String &playlistName); void PlaylistPause(const Ogre::String &playlistName); void PlaylistPauseAll(); void PlaylistResume(const Ogre::String &playlistName); void PlaylistResumeAll(); void PlaylistStop(const Ogre::String &playlistName); void PlatlistStopAll(); void PlaylistDestroy(const Ogre::String &playlistName); void PlaylistDestroyAll(); int Play2DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName="MASTER"); int Play2DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName="MASTER", const int &loopCount=0); int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position); int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const int &loopCount); int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward); int Play3DSound(const Ogre::String &soundName, const Ogre::String &channelGroupName, const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Vector3 &forward, const int &loopCount); private: static int SOUND_2D; static int SOUND_3D; SoundManager(); virtual ~SoundManager(); void Initialize(); void UpdateStarted(const Ogre::FrameEvent &gameTime); void UpdateEnded(const Ogre::FrameEvent &gameTime); void Cleanup(); void UnloadSound(const Ogre::String &name, const void *type); static void OpenFile(const Ogre::String fileName, std::ifstream &infile); static const FMOD_VECTOR OgreToFmodVec(const Ogre::Vector3 &vector); static const Ogre::Vector3 FmodToOgreVec(const FMOD_VECTOR &vector); FMOD::System* _system; Ogre::Real _timeSinceLastFrame; FMOD_VECTOR _listenerPosition; FMOD_VECTOR _velocityVector; FMOD_VECTOR _forwardVector; FMOD_VECTOR _upVector; stdext::hash_map<const Ogre::String, SoundInstance*>* _2DsoundMap; stdext::hash_map<const Ogre::String, SoundInstance*>* _3DsoundMap; stdext::hash_map<const Ogre::String, FMOD::SoundGroup*>* _soundGroupMap; stdext::hash_map<const Ogre::String, FMOD::SoundGroup*>::iterator _SGMitr; stdext::hash_map<const Ogre::String, FMOD::ChannelGroup*>* _channelGroupMap; stdext::hash_map<const Ogre::String, FMOD::ChannelGroup*>::iterator _CGMitr; class Sound2DPlaylist; stdext::hash_map<const Ogre::String, Sound2DPlaylist*>* _playlistMap; stdext::hash_map<const Ogre::String, Sound2DPlaylist*>::iterator _playlistItr; class Sound2DPlaylist{ public: Sound2DPlaylist(const Ogre::String &name) : _name(name) {} ~Sound2DPlaylist() { _soundQueue.clear(); } void Update(FMOD::System *system){ FMOD::Channel *channel; if(system->getChannel(_channel, &channel) == FMOD_OK) return; Skip(); } void AddSound(const Ogre::String soundName) { _soundQueue.push_back(soundName); } void Play() { Stop(); _channel = SoundManager::getSingleton()->Play2DSound(_soundQueue.front(), _name); } void Pause() { SoundManager::getSingleton()->ChannelPause(_channel, true); } void Resume() { SoundManager::getSingleton()->ChannelPause(_channel, false); } void Stop() { SoundManager::getSingleton()->ChannelStop(_channel); _channel = -1; } void Skip() { _soundQueue.push_back(_soundQueue.front()); _soundQueue.pop_front(); Stop(); Play(); } Ogre::String _name; FMOD::ChannelGroup* _channelGroup; std::deque<const Ogre::String> _soundQueue; std::deque<const Ogre::String>::iterator _itr; int _channel; }; };