| RecursivePowerFunction.h | String.h | StringReverse.h | CSoundManager.h | CSoundManager.cpp |

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
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    Name    : CSoundManager.h
  3. //
  4. //    Date    : 2007.02.08
  5. //
  6. //    Purpose : Maintains the creation of all of the Sound objects in the game.
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef _IL_SOUNDMANAGER_H_
  9. #define _IL_SOUNDMANAGER_H_
  10.  
  11. #include "../types.h"
  12. #include "../core/IUnknown.h"
  13. #include "ISound.h"
  14. #include "tChannel.h"
  15. #include "tBuffer.h"
  16.  
  17. #include <vector>
  18. using std::vector;
  19.  
  20.  
  21. namespace IL
  22. {
  23.  
  24.     namespace CORE
  25.     {
  26.         class CStaticEntity3D;
  27.     };
  28.  
  29.     namespace SOUND
  30.     {
  31.         //class ISound;
  32.         class CSound;
  33.         class CStreamingSound;
  34.         class CSoundManager :
  35.             public IL::CORE::IUnknown
  36.         {
  37.             public:
  38.                 //////////////////////////////////////////////////////////////////////////
  39.                 //    Name    : Constructor
  40.                 //////////////////////////////////////////////////////////////////////////
  41.                 CSoundManager() : m_UniqueIDGenerator(0) {}
  42.  
  43.                 //////////////////////////////////////////////////////////////////////////
  44.                 //    Name    : Destructor
  45.                 //////////////////////////////////////////////////////////////////////////
  46.                 virtual ~CSoundManager(){clear();}
  47.  
  48.                 //////////////////////////////////////////////////////////////////////////
  49.                 //    Name    : clear
  50.                 //
  51.                 //    Date    : 2007.02.08
  52.                 //
  53.                 //    Author  : Chad Stewart
  54.                 //
  55.                 //    Purpose : Frees all allocated memory and returns the object to its
  56.                 //                default state.
  57.                 //////////////////////////////////////////////////////////////////////////
  58.                 virtual void clear();
  59.  
  60.                 ///////////////////////////////////////////////////////////////////////////////
  61.                 //    Name    : getInstance
  62.                 //
  63.                 //    Date    : 2007.02.12
  64.                 //
  65.                 //    Author  : Chad Stewart
  66.                 //
  67.                 //    Purpose : "Returns the only instance of this class. If this is the
  68.                 //                first call to getInstance, it will create the instance of
  69.                 //                this class prior to returning it." - Rob Walker
  70.                 ///////////////////////////////////////////////////////////////////////////////
  71.                 static CSoundManager* getInstance(void);
  72.  
  73.                 //////////////////////////////////////////////////////////////////////////
  74.                 //    Name    : deleteInstance
  75.                 //
  76.                 //    Date    : 2007.02.12
  77.                 //
  78.                 //    Author  : Chad Stewart
  79.                 //
  80.                 //    Purpose : Deletes the only instance of this class and sets it to NULL
  81.                 //////////////////////////////////////////////////////////////////////////
  82.                 void deleteInstance(void);
  83.  
  84.                 //////////////////////////////////////////////////////////////////////////
  85.                 //    Name    : handleMessage
  86.                 //
  87.                 //    Date    : 2006.04.15
  88.                 //
  89.                 //    Author  : Chad Stewart
  90.                 //
  91.                 //    Purpose : Performs the handling of any messages the sound manager
  92.                 //                might be given.
  93.                 //////////////////////////////////////////////////////////////////////////
  94.                 void handleMessage(const IL::CORE::tMessage * vMessage);
  95.  
  96.                 ///////////////////////////////////////////////////////////////////////////////
  97.                 //    Name    : initialize
  98.                 //
  99.                 //    Date    : 2007.02.12
  100.                 //
  101.                 //    Author  : Chad Stewart
  102.                 //
  103.                 //    Purpose : Set up OpenAL
  104.                 ///////////////////////////////////////////////////////////////////////////////
  105.                 ILENUM initialize(void);
  106.  
  107.                 ///////////////////////////////////////////////////////////////////////////////
  108.                 //    Name    : changeDevice
  109.                 //
  110.                 //    Date    : 2007.06.15
  111.                 //
  112.                 //    Author  : Chad Stewart
  113.                 //
  114.                 //    Purpose : This changes the device that OpenAL uses.
  115.                 ///////////////////////////////////////////////////////////////////////////////
  116.                 ILENUM changeDevice(const char* vDeviceName);
  117.  
  118.                 ///////////////////////////////////////////////////////////////////////////////
  119.                 //    Name    : getFreeSound
  120.                 //
  121.                 //    Date    : 2007.06.16
  122.                 //
  123.                 //    Author  : Chad Stewart
  124.                 //
  125.                 //    Purpose : Gets or creates a sound from memory.
  126.                 ///////////////////////////////////////////////////////////////////////////////
  127.                 IL::SOUND::CSound* getFreeSound(void);
  128.  
  129.                 ///////////////////////////////////////////////////////////////////////////////
  130.                 //    Name    : createSound
  131.                 //
  132.                 //    Date    : 2007.02.16
  133.                 //
  134.                 //    Author  : Chad Stewart
  135.                 //
  136.                 //    Purpose : Makes a sound and adds it into the vector of sounds.
  137.                 //                Returns an integer ID.
  138.                 ///////////////////////////////////////////////////////////////////////////////
  139.                 ILINT loadSoundFromFile(const ILCHAR*  vFilename = NULL, ILBOOL vStream = false);
  140.                 ILINT loadSoundFromFile(const ILWCHAR* vFilename = NULL, ILBOOL vStream = false);
  141.  
  142.                 ///////////////////////////////////////////////////////////////////////////////
  143.                 //    Name    : playSound
  144.                 //
  145.                 //    Date    : 2007.02.16
  146.                 //
  147.                 //    Author  : Chad Stewart
  148.                 //
  149.                 //    Purpose : Plays the sound specified.
  150.                 //
  151.                 //    Note    : Returns the ID of the song if it is played correctly. Otherwise,
  152.                 //                it is -1 for an error.
  153.                 ///////////////////////////////////////////////////////////////////////////////
  154.                 ILINT playSound(ILINT vID, ILENUM vPlayMode = NULL, ILFLOAT vFadeInLength = 0.0f, ILFLOAT vMaxVolume = 1.0f,
  155.                     ILFLOAT vFadeOutLength = 0.0f, ILFLOAT vFadeOutStartTime = 0.0f, ILFLOAT vReferenceDistance = 50.0f);
  156.  
  157.                 ///////////////////////////////////////////////////////////////////////////////
  158.                 //    Name    : stopSound
  159.                 //
  160.                 //    Date    : 2007.02.26
  161.                 //
  162.                 //    Author  : Chad Stewart
  163.                 //
  164.                 //    Purpose : Stop a currently playing sound.
  165.                 ///////////////////////////////////////////////////////////////////////////////
  166.                 ILBOOL stopSound(ILINT vID);
  167.  
  168.                 ///////////////////////////////////////////////////////////////////////////////
  169.                 //    Name    : stopSounds
  170.                 //
  171.                 //    Date    : 2007.05.03
  172.                 //
  173.                 //    Author  : Chad Stewart
  174.                 //
  175.                 //    Purpose : OK, all of the sounds playing? You're done!
  176.                 ///////////////////////////////////////////////////////////////////////////////
  177.                 void stopAllSounds(void);
  178.  
  179.                 ///////////////////////////////////////////////////////////////////////////////
  180.                 //    Name    : pauseSound
  181.                 //
  182.                 //    Date    : 2007.02.26
  183.                 //
  184.                 //    Author  : Chad Stewart
  185.                 //
  186.                 //    Purpose : Pause a currently playing sound.
  187.                 ///////////////////////////////////////////////////////////////////////////////
  188.                 ILBOOL pauseSound(ILINT vID);
  189.  
  190.                 ///////////////////////////////////////////////////////////////////////////////
  191.                 //    Name    : unpauseSound
  192.                 //
  193.                 //    Date    : 2007.06.02
  194.                 //
  195.                 //    Author  : Chad Stewart
  196.                 //
  197.                 //    Purpose : Un-pause a currently playing sound.
  198.                 ///////////////////////////////////////////////////////////////////////////////
  199.                 ILBOOL unpauseSound(ILINT vID);
  200.  
  201.                 ///////////////////////////////////////////////////////////////////////////////
  202.                 //    Name    : pauseAllSounds
  203.                 //
  204.                 //    Date    : 2007.06.01
  205.                 //
  206.                 //    Author  : Chad Stewart
  207.                 //
  208.                 //    Purpose : OK, all of the sounds playing? You're done!
  209.                 ///////////////////////////////////////////////////////////////////////////////
  210.                 vector<ILINT>& pauseAllSounds(ILBOOL vSavePlayingSoundsIDs = false);
  211.  
  212.                 ///////////////////////////////////////////////////////////////////////////////
  213.                 //    Name    : update
  214.                 //
  215.                 //    Date    : 2007.02.17
  216.                 //
  217.                 //    Author  : Chad Stewart
  218.                 //
  219.                 //    Purpose : Update all of the sounds contained in the manager.
  220.                 ///////////////////////////////////////////////////////////////////////////////
  221.                 ILBOOL update(ILFLOAT vEllapsedTime, IL::CORE::CStaticEntity3D* vListener = NULL);
  222.  
  223.                 ///////////////////////////////////////////////////////////////////////////////
  224.                 //    Name    : fadeSoundOut
  225.                 //
  226.                 //    Date    : 2007.03.10
  227.                 //
  228.                 //    Author  : Chad Stewart
  229.                 //
  230.                 //    Purpose : Fade a sound out.
  231.                 ///////////////////////////////////////////////////////////////////////////////
  232.                 ILBOOL fadeSoundOut(ILINT vID, ILFLOAT vFadeOutLength = 0.0f, ILFLOAT vFadeOutStartTime = 0.0f);
  233.  
  234.                 ///////////////////////////////////////////////////////////////////////////////
  235.                 //    Name    : crossFadeSounds
  236.                 //
  237.                 //    Date    : 2007.03.10
  238.                 //
  239.                 //    Author  : Chad Stewart
  240.                 //
  241.                 //    Purpose : Fades one sound out while another sound is introduced.
  242.                 ///////////////////////////////////////////////////////////////////////////////
  243.                 ILBOOL crossFadeSounds(ILINT vFirstID, ILFLOAT vFadeOutLength, ILINT vSecondID,
  244.                                 ILFLOAT vFadeInLength, ILENUM vPlayMode = NULL, ILFLOAT vReferenceDistance = 1.0f);
  245.  
  246.                 ///////////////////////////////////////////////////////////////////////////////
  247.                 //    Name    : deleteSound
  248.                 //
  249.                 //    Date    : 2007.02.19
  250.                 //
  251.                 //    Author  : Chad Stewart
  252.                 //
  253.                 //    Purpose : Completely trod all over the functionality of a factory.
  254.                 ///////////////////////////////////////////////////////////////////////////////
  255.                 void deleteSound(ILINT vID);
  256.  
  257.                 ///////////////////////////////////////////////////////////////////////////////
  258.                 //    Name    : clearSounds
  259.                 //
  260.                 //    Date    : 2007.02.19
  261.                 //
  262.                 //    Author  : Chad Stewart
  263.                 //
  264.                 //    Purpose : If delete wasn't bad enough, you can clear them all.
  265.                 ///////////////////////////////////////////////////////////////////////////////
  266.                 void clearSounds(void);
  267.  
  268.                 ///////////////////////////////////////////////////////////////////////////////
  269.                 //    Name    : recycleSound
  270.                 //
  271.                 //    Date    : 2007.02.17
  272.                 //
  273.                 //    Author  : Chad Stewart
  274.                 //
  275.                 //    Purpose : Clears the sound and puts it in the list for re-use.
  276.                 ///////////////////////////////////////////////////////////////////////////////
  277.                 void recycleSound(ILINT vID);
  278.                 void recycleSound(ISound* vSound);
  279.  
  280.                 //////////////////////////////////////////////////////////////////////////
  281.                 //    Name    : Accessors
  282.                 //////////////////////////////////////////////////////////////////////////
  283.                 ISound* getSound(ILINT vID);
  284.                 void    getPosition(ILINT vID, IL::CORE::ILVECTOR3DF& vPosition) const;
  285.                 void    getPosition(ILINT vID, ILFLOAT* vPosition) const;
  286.                 void    getVelocity(ILINT vID, IL::CORE::ILVECTOR3DF& vVelocity) const;
  287.                 void    getVelocity(ILINT vID, ILFLOAT* vVelocity) const;
  288.                 ILULONG    getFormat(ILINT vID) const;
  289.                 ILULONG    getChannels(ILINT vID) const;
  290.                 ILULONG    getFrequency(ILINT vID) const;
  291.                 ILFLOAT    getVolume(ILINT vID) const;
  292.                 ILUINT    getSourceID(ILINT vID) const;
  293.                 ILUINT    getUniqueID(ILINT vID) const;
  294.                 void    getDeviceUsed(char* vDeviceString) const;
  295.                 void    getDevicesPossible(std::vector<IL::CORE::CString>& vDeviceStrings) const;
  296.                 ILBOOL    isPaused(ILINT vID) const;
  297.                 ILBOOL    isStream(ILINT vID) const;
  298.  
  299.                 //////////////////////////////////////////////////////////////////////////
  300.                 //    Name    : Mutators
  301.                 //////////////////////////////////////////////////////////////////////////
  302.                 void setPosition(ILINT vID, IL::CORE::ILVECTOR3DF& vPosition);
  303.                 void setPosition(ILINT vID, ILFLOAT* vPosition);
  304.                 void setPosition(ILINT vID, ILFLOAT vX, ILFLOAT vY, ILFLOAT vZ);
  305.                 void setPosition2D(ILINT vID, ILFLOAT vX, ILFLOAT vY);
  306.                 void setVelocity(ILINT vID, IL::CORE::ILVECTOR3DF& vVelocity);
  307.                 void setVelocity(ILINT vID, ILFLOAT* vVelocity);
  308.                 void setFormat(ILINT vID, ILULONG vFormat);
  309.                 void setChannels(ILINT vID, ILULONG vChannels);
  310.                 void setFrequency(ILINT vID, ILULONG vFrequency);
  311.                 void setMaxVolume(ILINT vID, ILFLOAT vGain);
  312.                 void setVolume(ILINT vID, ILFLOAT vGain);
  313.  
  314.             private:
  315.                 //////////////////////////////////////////////////////////////////////////
  316.                 //    Name    : Copy Constructor
  317.                 //////////////////////////////////////////////////////////////////////////
  318.                 CSoundManager(const CSoundManager& vObject){*this = vObject;}
  319.  
  320.                 //////////////////////////////////////////////////////////////////////////
  321.                 //    Name    : Operator=
  322.                 //////////////////////////////////////////////////////////////////////////
  323.                 CSoundManager& operator=(const CSoundManager& vObject){return *this;}
  324.  
  325.                 ///////////////////////////////////////////////////////////////////////////////
  326.                 //    Name    : cloneSound
  327.                 //
  328.                 //    Date    : 2007.03.05
  329.                 //
  330.                 //    Author  : Chad Stewart
  331.                 //
  332.                 //    Purpose : Create a new sound from an old one. This is how overlap works,
  333.                 //                because otherwise each ISound would be a vector of sounds playing
  334.                 //                instead of a single sound.
  335.                 ///////////////////////////////////////////////////////////////////////////////
  336.                 ILUINT cloneSound(ILINT vID, ISound** vCloneSound);
  337.  
  338.                 ///////////////////////////////////////////////////////////////////////////////
  339.                 //    Name    : getFreeChannel
  340.                 //
  341.                 //    Date    : 2007.04.16
  342.                 //
  343.                 //    Author  : Chad Stewart
  344.                 //
  345.                 //    Purpose : Looks through the channels for one that is not in use and
  346.                 //                returns the channel's location in the vector.
  347.                 ///////////////////////////////////////////////////////////////////////////////
  348.                 ILINT getFreeChannel(void);
  349.  
  350.                 ///////////////////////////////////////////////////////////////////////////////
  351.                 //    Name    : getFreeBufferi
  352.                 //
  353.                 //    Date    : 2007.04.21
  354.                 //
  355.                 //    Author  : Chad Stewart
  356.                 //
  357.                 //    Purpose : Get a single free buffer from OpenAL
  358.                 ///////////////////////////////////////////////////////////////////////////////
  359.                 ILINT getFreeBufferi(void);
  360.  
  361.                 ///////////////////////////////////////////////////////////////////////////////
  362.                 //    Name    : cloneSound
  363.                 //
  364.                 //    Date    : 2007.03.05
  365.                 //
  366.                 //    Author  : Chad Stewart
  367.                 //
  368.                 //    Purpose : The ISound's clone sound.
  369.                 //
  370.                 //    Note    : This is a friend of the ISound. It's a friend because it's
  371.                 //                private and we don't want people going around and calling
  372.                 //                the ISound's clone all over the place.
  373.                 ///////////////////////////////////////////////////////////////////////////////
  374.                 friend ISound* ISound::cloneSound(void);
  375.  
  376.             private:
  377.                 static IL::SOUND::CSoundManager*    m_pOnlyInstance;
  378.                 vector<tChannel>                    m_vSoundChannels;
  379.                 vector<tBuffer>                        m_vSoundBuffers;
  380.                 vector<ISound*>                        m_Sounds;
  381.                 vector<CSound*>                        m_UnusedSounds;
  382.                 vector<CStreamingSound*>            m_UnusedStreams;
  383.                 ILINT                                m_UniqueIDGenerator;
  384.         };
  385.     };
  386. };
  387.  
  388. #endif