One of my responsibilities during my final project at Full Sail was to wrap an OpenAL implementation to use sound files. We used OGG Vorbis and Wave files, so I wrote a system that would give us only the power that we needed, as my responsibilities were stretched across the board. This included a SoundManager which contained instances of the Sound class and StreamingSound class through the ISound interface. The SoundManager was the entry point into the sound system and could Load, Play, Stop, and Fade sounds and was responsible for Initializing the Device.
CSoundManager.h
- ///////////////////////////////////////////////////////////////////////////////
- // Name : CSoundManager.h
- //
- // Date : 2007.02.08
- //
- // Purpose : Maintains the creation of all of the Sound objects in the game.
- ///////////////////////////////////////////////////////////////////////////////
- #ifndef _IL_SOUNDMANAGER_H_
- #define _IL_SOUNDMANAGER_H_
- #include "../types.h"
- #include "../core/IUnknown.h"
- #include "ISound.h"
- #include "tChannel.h"
- #include "tBuffer.h"
- #include <vector>
- using std::vector;
- namespace IL
- {
- namespace CORE
- {
- class CStaticEntity3D;
- };
- namespace SOUND
- {
- //class ISound;
- class CSound;
- class CStreamingSound;
- class CSoundManager :
- public IL::CORE::IUnknown
- {
- public:
- //////////////////////////////////////////////////////////////////////////
- // Name : Constructor
- //////////////////////////////////////////////////////////////////////////
- CSoundManager() : m_UniqueIDGenerator(0) {}
- //////////////////////////////////////////////////////////////////////////
- // Name : Destructor
- //////////////////////////////////////////////////////////////////////////
- virtual ~CSoundManager(){clear();}
- //////////////////////////////////////////////////////////////////////////
- // Name : clear
- //
- // Date : 2007.02.08
- //
- // Author : Chad Stewart
- //
- // Purpose : Frees all allocated memory and returns the object to its
- // default state.
- //////////////////////////////////////////////////////////////////////////
- virtual void clear();
- ///////////////////////////////////////////////////////////////////////////////
- // Name : getInstance
- //
- // Date : 2007.02.12
- //
- // Author : Chad Stewart
- //
- // Purpose : "Returns the only instance of this class. If this is the
- // first call to getInstance, it will create the instance of
- // this class prior to returning it." - Rob Walker
- ///////////////////////////////////////////////////////////////////////////////
- static CSoundManager* getInstance(void);
- //////////////////////////////////////////////////////////////////////////
- // Name : deleteInstance
- //
- // Date : 2007.02.12
- //
- // Author : Chad Stewart
- //
- // Purpose : Deletes the only instance of this class and sets it to NULL
- //////////////////////////////////////////////////////////////////////////
- void deleteInstance(void);
- //////////////////////////////////////////////////////////////////////////
- // Name : handleMessage
- //
- // Date : 2006.04.15
- //
- // Author : Chad Stewart
- //
- // Purpose : Performs the handling of any messages the sound manager
- // might be given.
- //////////////////////////////////////////////////////////////////////////
- void handleMessage(const IL::CORE::tMessage * vMessage);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : initialize
- //
- // Date : 2007.02.12
- //
- // Author : Chad Stewart
- //
- // Purpose : Set up OpenAL
- ///////////////////////////////////////////////////////////////////////////////
- ILENUM initialize(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : changeDevice
- //
- // Date : 2007.06.15
- //
- // Author : Chad Stewart
- //
- // Purpose : This changes the device that OpenAL uses.
- ///////////////////////////////////////////////////////////////////////////////
- ILENUM changeDevice(const char* vDeviceName);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : getFreeSound
- //
- // Date : 2007.06.16
- //
- // Author : Chad Stewart
- //
- // Purpose : Gets or creates a sound from memory.
- ///////////////////////////////////////////////////////////////////////////////
- IL::SOUND::CSound* getFreeSound(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : createSound
- //
- // Date : 2007.02.16
- //
- // Author : Chad Stewart
- //
- // Purpose : Makes a sound and adds it into the vector of sounds.
- // Returns an integer ID.
- ///////////////////////////////////////////////////////////////////////////////
- ILINT loadSoundFromFile(const ILCHAR* vFilename = NULL, ILBOOL vStream = false);
- ILINT loadSoundFromFile(const ILWCHAR* vFilename = NULL, ILBOOL vStream = false);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : playSound
- //
- // Date : 2007.02.16
- //
- // Author : Chad Stewart
- //
- // Purpose : Plays the sound specified.
- //
- // Note : Returns the ID of the song if it is played correctly. Otherwise,
- // it is -1 for an error.
- ///////////////////////////////////////////////////////////////////////////////
- ILINT playSound(ILINT vID, ILENUM vPlayMode = NULL, ILFLOAT vFadeInLength = 0.0f, ILFLOAT vMaxVolume = 1.0f,
- ILFLOAT vFadeOutLength = 0.0f, ILFLOAT vFadeOutStartTime = 0.0f, ILFLOAT vReferenceDistance = 50.0f);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : stopSound
- //
- // Date : 2007.02.26
- //
- // Author : Chad Stewart
- //
- // Purpose : Stop a currently playing sound.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL stopSound(ILINT vID);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : stopSounds
- //
- // Date : 2007.05.03
- //
- // Author : Chad Stewart
- //
- // Purpose : OK, all of the sounds playing? You're done!
- ///////////////////////////////////////////////////////////////////////////////
- void stopAllSounds(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : pauseSound
- //
- // Date : 2007.02.26
- //
- // Author : Chad Stewart
- //
- // Purpose : Pause a currently playing sound.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL pauseSound(ILINT vID);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : unpauseSound
- //
- // Date : 2007.06.02
- //
- // Author : Chad Stewart
- //
- // Purpose : Un-pause a currently playing sound.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL unpauseSound(ILINT vID);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : pauseAllSounds
- //
- // Date : 2007.06.01
- //
- // Author : Chad Stewart
- //
- // Purpose : OK, all of the sounds playing? You're done!
- ///////////////////////////////////////////////////////////////////////////////
- vector<ILINT>& pauseAllSounds(ILBOOL vSavePlayingSoundsIDs = false);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : update
- //
- // Date : 2007.02.17
- //
- // Author : Chad Stewart
- //
- // Purpose : Update all of the sounds contained in the manager.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL update(ILFLOAT vEllapsedTime, IL::CORE::CStaticEntity3D* vListener = NULL);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : fadeSoundOut
- //
- // Date : 2007.03.10
- //
- // Author : Chad Stewart
- //
- // Purpose : Fade a sound out.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL fadeSoundOut(ILINT vID, ILFLOAT vFadeOutLength = 0.0f, ILFLOAT vFadeOutStartTime = 0.0f);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : crossFadeSounds
- //
- // Date : 2007.03.10
- //
- // Author : Chad Stewart
- //
- // Purpose : Fades one sound out while another sound is introduced.
- ///////////////////////////////////////////////////////////////////////////////
- ILBOOL crossFadeSounds(ILINT vFirstID, ILFLOAT vFadeOutLength, ILINT vSecondID,
- ILFLOAT vFadeInLength, ILENUM vPlayMode = NULL, ILFLOAT vReferenceDistance = 1.0f);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : deleteSound
- //
- // Date : 2007.02.19
- //
- // Author : Chad Stewart
- //
- // Purpose : Completely trod all over the functionality of a factory.
- ///////////////////////////////////////////////////////////////////////////////
- void deleteSound(ILINT vID);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : clearSounds
- //
- // Date : 2007.02.19
- //
- // Author : Chad Stewart
- //
- // Purpose : If delete wasn't bad enough, you can clear them all.
- ///////////////////////////////////////////////////////////////////////////////
- void clearSounds(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : recycleSound
- //
- // Date : 2007.02.17
- //
- // Author : Chad Stewart
- //
- // Purpose : Clears the sound and puts it in the list for re-use.
- ///////////////////////////////////////////////////////////////////////////////
- void recycleSound(ILINT vID);
- void recycleSound(ISound* vSound);
- //////////////////////////////////////////////////////////////////////////
- // Name : Accessors
- //////////////////////////////////////////////////////////////////////////
- ISound* getSound(ILINT vID);
- void getPosition(ILINT vID, IL::CORE::ILVECTOR3DF& vPosition) const;
- void getPosition(ILINT vID, ILFLOAT* vPosition) const;
- void getVelocity(ILINT vID, IL::CORE::ILVECTOR3DF& vVelocity) const;
- void getVelocity(ILINT vID, ILFLOAT* vVelocity) const;
- ILULONG getFormat(ILINT vID) const;
- ILULONG getChannels(ILINT vID) const;
- ILULONG getFrequency(ILINT vID) const;
- ILFLOAT getVolume(ILINT vID) const;
- ILUINT getSourceID(ILINT vID) const;
- ILUINT getUniqueID(ILINT vID) const;
- void getDeviceUsed(char* vDeviceString) const;
- void getDevicesPossible(std::vector<IL::CORE::CString>& vDeviceStrings) const;
- ILBOOL isPaused(ILINT vID) const;
- ILBOOL isStream(ILINT vID) const;
- //////////////////////////////////////////////////////////////////////////
- // Name : Mutators
- //////////////////////////////////////////////////////////////////////////
- void setPosition(ILINT vID, IL::CORE::ILVECTOR3DF& vPosition);
- void setPosition(ILINT vID, ILFLOAT* vPosition);
- void setPosition(ILINT vID, ILFLOAT vX, ILFLOAT vY, ILFLOAT vZ);
- void setPosition2D(ILINT vID, ILFLOAT vX, ILFLOAT vY);
- void setVelocity(ILINT vID, IL::CORE::ILVECTOR3DF& vVelocity);
- void setVelocity(ILINT vID, ILFLOAT* vVelocity);
- void setFormat(ILINT vID, ILULONG vFormat);
- void setChannels(ILINT vID, ILULONG vChannels);
- void setFrequency(ILINT vID, ILULONG vFrequency);
- void setMaxVolume(ILINT vID, ILFLOAT vGain);
- void setVolume(ILINT vID, ILFLOAT vGain);
- private:
- //////////////////////////////////////////////////////////////////////////
- // Name : Copy Constructor
- //////////////////////////////////////////////////////////////////////////
- CSoundManager(const CSoundManager& vObject){*this = vObject;}
- //////////////////////////////////////////////////////////////////////////
- // Name : Operator=
- //////////////////////////////////////////////////////////////////////////
- CSoundManager& operator=(const CSoundManager& vObject){return *this;}
- ///////////////////////////////////////////////////////////////////////////////
- // Name : cloneSound
- //
- // Date : 2007.03.05
- //
- // Author : Chad Stewart
- //
- // Purpose : Create a new sound from an old one. This is how overlap works,
- // because otherwise each ISound would be a vector of sounds playing
- // instead of a single sound.
- ///////////////////////////////////////////////////////////////////////////////
- ILUINT cloneSound(ILINT vID, ISound** vCloneSound);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : getFreeChannel
- //
- // Date : 2007.04.16
- //
- // Author : Chad Stewart
- //
- // Purpose : Looks through the channels for one that is not in use and
- // returns the channel's location in the vector.
- ///////////////////////////////////////////////////////////////////////////////
- ILINT getFreeChannel(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : getFreeBufferi
- //
- // Date : 2007.04.21
- //
- // Author : Chad Stewart
- //
- // Purpose : Get a single free buffer from OpenAL
- ///////////////////////////////////////////////////////////////////////////////
- ILINT getFreeBufferi(void);
- ///////////////////////////////////////////////////////////////////////////////
- // Name : cloneSound
- //
- // Date : 2007.03.05
- //
- // Author : Chad Stewart
- //
- // Purpose : The ISound's clone sound.
- //
- // Note : This is a friend of the ISound. It's a friend because it's
- // private and we don't want people going around and calling
- // the ISound's clone all over the place.
- ///////////////////////////////////////////////////////////////////////////////
- friend ISound* ISound::cloneSound(void);
- private:
- static IL::SOUND::CSoundManager* m_pOnlyInstance;
- vector<tChannel> m_vSoundChannels;
- vector<tBuffer> m_vSoundBuffers;
- vector<ISound*> m_Sounds;
- vector<CSound*> m_UnusedSounds;
- vector<CStreamingSound*> m_UnusedStreams;
- ILINT m_UniqueIDGenerator;
- };
- };
- };
- #endif