Advertisement
diamondandplatinum3

AudioManager.h ~ C++

Oct 8th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.98 KB | None | 0 0
  1. //#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. //#                 Audio Manager
  3. //#                 Author: DiamondandPlatinum3
  4. //#                 Date: August 6, 2013
  5. //#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. //#   Description:
  7. //#    
  8. //#   ~ This class handles all audio in this project. Audio is split into three
  9. //#     categories:
  10. //#
  11. //#         * BGM: Background Music, Only one of these can be playing at a time.
  12. //#                
  13. //#         * BGS: Background Sound, Only one of these can be playing at a time.
  14. //#                 These sounds files are typically used as a constant noise,
  15. //#                 Such as a fire sound, or people talking.
  16. //#
  17. //#         * SFX: Sound Effects, can use more than one at a time.
  18. //#
  19. //#
  20. //#   ~ So far this class can only play Sounds as 2D.
  21. //#
  22. //#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  23. #ifndef _AUDIOMANAGER_H_
  24. #define _AUDIOMANAGER_H_
  25.  
  26.  
  27. #include <fmod.hpp>
  28. #include <string>
  29. #include <list>
  30. #include <vector>
  31.  
  32.  
  33. #define NUMBER_OF_AUDIO_CHANNELS             5
  34. #define AUDIO_ROOT_DIRECTORY_FOLDER_NAME    "Audio/"
  35. #define BACKGROUND_MUSIC_FOLDER_NAME        "BGM/"
  36. #define BACKGROUND_SOUNDS_FOLDER_NAME       "BGS/"
  37. #define SOUND_EFFECTS_FOLDER_NAME           "SE/"
  38.  
  39.  
  40. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  41. //              Class: Audio Manager
  42. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  43. class AudioManager
  44. {
  45. //////////////////////////////////////////
  46. public:
  47. ////////////////////////////////////////// 
  48.  
  49.     //===============================================
  50.     //          Public Declarations
  51.     //===============================================
  52.     enum AudioTypes
  53.     {
  54.         BGM                 = 0,
  55.         BACKGROUND_MUSIC    = 0,
  56.         STAGE_MUSIC         = 0,
  57.         BGS                 = 1,
  58.         BACKGROUND_SOUND    = 1,
  59.         ENVIRONMENTAL_SOUND = 1,
  60.         SE                  = 2,
  61.         SFX                 = 2,
  62.         SOUND_EFFECT        = 2,
  63.        
  64.         NONE                = -1,
  65.     };
  66.  
  67.  
  68.     //===============================================
  69.     //          Constructor & Destructor
  70.     //===============================================
  71.     AudioManager();
  72.     ~AudioManager();
  73.  
  74.  
  75.  
  76.  
  77.     //===============================================
  78.     //          Static Functions
  79.     //===============================================
  80.  
  81.     // Get the one time instance of the AudioManager class
  82.     static AudioManager* GetInstance();
  83.  
  84.  
  85.     //===============================================
  86.     //          Public Functions
  87.     //===============================================
  88.     void OnLoad();
  89.     void OnUnload();
  90.  
  91.     // As a rule, update must be called every frame
  92.     void Update();
  93.  
  94.     // Play a Sound, pass in the alias name of a sound you have imported, if you know what soundtype it is (BGM, BGS, SFX) you can pass that in as an argument to speed up the process
  95.     void PlaySound( std::string& AliasNameofSound, AudioTypes SoundType = NONE );
  96.     void PlaySound( unsigned int AudioID );
  97.  
  98.     // Stop a Channel (including any and all sounds it is playing), pass in the alias name of a sound you have imported, Channel Types: BGM, BGS, SFX
  99.     void StopSound(AudioTypes ChannelType);
  100.  
  101.     // Fadein Sound, pass in the amount of time you want to use until the sound is fully faded in. Use this function after using the PlaySound function
  102.     void FadeinSound(AudioTypes ChannelType, float SecondsToFadeIn);
  103.  
  104.     // Fadeout Sound, pass in the amount of time you want to use until the sound is fully faded in. Use this function after using the PlaySound function
  105.     void FadeoutSound(AudioTypes ChannelType, float SecondsToFadeOut);
  106.  
  107.     // Import New Audio Files into the AudioManager
  108.     // Filename: Filename (including extension) of the Audio File you are importing, path is included with the directory string you have set earlier
  109.     // AliasName: You may give this sound file an alias name, this alias name will be used to identify the sound for other functions. For Example name the file "Title Theme"
  110.     // Volume: Volume of the sound file, can be between 5..100. The volume is offset by the channel volume, so 80 volume for this sound and 80 volume for the channel volume will yield a 64 total volume when played
  111.     // Tempo: Speed of the Sound File, can be between 5..200. This ONLY affects MIDI files, does not affect other sound types, they will still play at normal speeds
  112.     // LoopStart: Position to start Looping Audio via SampleRates, if you don't know what this is, best leave it as 0
  113.     // LoopEnd: Position to end Audio and restart from Loopstart, again if you don't know what this is, leave it as 0
  114.     unsigned int ImportAudio( std::string& sFileName, AudioTypes AudioType, unsigned int Volume = 80, unsigned int Tempo = 100, unsigned int LoopStart = 0, unsigned int LoopEnd = 0 );
  115.     unsigned int ImportAudio( std::string& sFileName, std::string& sAliasName, AudioTypes AudioType, unsigned int Volume = 80, unsigned int Tempo = 100, unsigned int LoopStart = 0, unsigned int LoopEnd = 0 );
  116.    
  117.     // Restore Default Sound Options, sets all channel volumes back to 80
  118.     void RestoreDefaults();
  119.  
  120.     // Prints out Audio Loop Information. Use for debugging
  121.     void PrintOutAudioLoopInfo();
  122.  
  123.     //===============================================
  124.     //          Getter Functions
  125.     //===============================================
  126.  
  127.     // Get Volume for a Sound, pass in the alias name of a sound you have imported, if you know what soundtype it is (BGM, BGS, SFX) you can pass that in as an argument to speed up the process
  128.     unsigned int GetSoundVolume( std::string& AliasName, AudioTypes SoundType = NONE );
  129.     unsigned int GetSoundVolume( unsigned int AudioID );
  130.  
  131.     // Get Tempo for a Sound, pass in the alias name of a sound you have imported, if you know what soundtype it is (BGM, BGS, SFX) you can pass that in as an argument to speed up the process
  132.     unsigned int GetSoundTempo( std::string& AliasName, AudioTypes SoundType = NONE );
  133.     unsigned int GetSoundTempo( unsigned int AudioID );
  134.  
  135.     // See if a channel is mute. Channel Types: BGM, BGS, SFX
  136.     bool IsChannelMuted( AudioTypes ChannelType );
  137.  
  138.     //===============================================
  139.     //          Setter Functions
  140.     //===============================================
  141.  
  142.     // Set Volume For Entire System, including BGM, BGS & SFX
  143.     void SetMasterVolume( unsigned int Volume );   
  144.  
  145.     // Set Individual Channel Volume, Channel Types: BGM, BGS, SFX
  146.     void SetChannelVolume( AudioTypes ChannelType, unsigned int Volume );
  147.  
  148.     // Set Volume for an individual sound, the channel sound still reflects off of this, so an 80 volume sound mixed in with an 80 channel volume sound will yield a 64 volume sound when played
  149.     // If you know what soundtype it is (BGM, BGS, SFX) you can pass that in as an argument to speed up the process
  150.     void SetSoundVolume( std::string& AliasName, unsigned int Volume, AudioTypes SoundType );
  151.     void SetSoundVolume( unsigned int AudioID, unsigned int Volume );
  152.  
  153.     // Set Tempo for an individual sound, only works for MIDI Files, if you know what soundtype it is (BGM, BGS, SFX) you can pass that in as an argument to speed up the process
  154.     void SetSoundTempo( std::string& AliasName, unsigned int Tempo, AudioTypes SoundType );
  155.     void SetSoundTempo( unsigned int AudioID, unsigned int Tempo );
  156.    
  157.     // Set Sound Pause for a Channel, basically you can mute all sounds in that channel
  158.     void SetChannelPause( AudioTypes ChannelType, bool SoundPaused );
  159.  
  160.     // Set Mute, you may mute a channel with this function. Channel Types: BGM, BGS, SFX
  161.     void ToggleMute( AudioTypes ChannelType );
  162.  
  163.  
  164. //////////////////////////////////////////
  165. private:
  166. //////////////////////////////////////////
  167.    
  168.    
  169.     //===============================================
  170.     //          Private Declarations
  171.     //===============================================
  172.     typedef std::pair<bool, unsigned int> ImportedAndID;
  173.  
  174.  
  175.     struct AudioInfo
  176.     {
  177.         unsigned int            AudioID;
  178.         std::string             FilePath;
  179.         AudioTypes              SoundType;
  180.         unsigned int            Volume;
  181.         unsigned int            Tempo;
  182.         float                   ActualVolume;
  183.         float                   ActualTempo;
  184.         unsigned int            LoopStart;
  185.         unsigned int            LoopEnd;
  186.         FMOD::Sound*            Sound_ptr;
  187.         std::list<std::string>  AliasNames;
  188.  
  189.         void AddAlias( std::string& Alias );
  190.         bool DoesAliasesInclude( std::string& Alias );
  191.     };
  192.  
  193.     struct AudioHolder
  194.     {
  195.         std::vector< AudioInfo* > ImportedAudio;
  196.         unsigned int              EmptySlotID;
  197.  
  198.         AudioInfo* operator [] ( unsigned int index ) { return ImportedAudio[index]; }
  199.     };
  200.  
  201.     struct ChannelInfo
  202.     {
  203.         FMOD::Channel**         ChannelPTR;
  204.         AudioInfo*              CurrentSound;
  205.         AudioTypes              ChannelType;
  206.         float                   Volume;
  207.         bool                    Paused;
  208.         bool                    Fadein;
  209.         bool                    Fadeout;
  210.         float                   FadeTime;
  211.         float                   CurrentFadeTime;
  212.         std::list<AudioInfo*>   ImportedAudioList;
  213.     };
  214.  
  215.  
  216.     //===============================================
  217.     //         Private Instance Variables
  218.     //===============================================
  219.     ChannelInfo          m_ChannelInfos[3];
  220.     AudioHolder          m_AudioHolder;
  221.  
  222.     AudioInfo*           m_pBGMReservedAudio;
  223.     AudioInfo*           m_pBGSReservedAudio;
  224.  
  225.     bool                 m_bMuteSoundEffects;
  226.  
  227.  
  228.     FMOD_RESULT          FMOD_Result;
  229.     FMOD::System*        m_pFMODSystem;
  230.     FMOD::Channel*       m_pBGMChannel;
  231.     FMOD::Channel*       m_pBGSChannel;
  232.     FMOD::Channel*       m_pSEChannel;
  233.  
  234.  
  235.     //===============================================
  236.     //          Static Declarations
  237.     //===============================================
  238.     static AudioManager* m_Instance;
  239.  
  240.  
  241.     //===============================================
  242.     //          Private Functions
  243.     //===============================================
  244.     void            InitSound                           (                                                   );
  245.     void            DeleteSounds                        (                                                   );
  246.     void            SetupAudioHolder                    (                                                   );
  247.     void            SetupAudioChannels                  (                                                   );
  248.     void            AddNewAudioInfoToHolder             ( AudioInfo* pAudInfo                               );
  249.     void            CorrectChannelVolume                ( ChannelInfo&  a_Channel, float fVolume            );
  250.     void            UpdateChannelFade                   ( ChannelInfo&  a_Channel                           );
  251.  
  252.     bool            BGMWasFoundAndPlaying               ( std::string&  AliasName                           );
  253.     bool            BGSWasFoundAndPlaying               ( std::string&  AliasName                           );
  254.     bool            SEWasFoundAndPlaying                ( std::string&  AliasName                           );
  255.  
  256.     void            PlayBGMSound                        ( AudioInfo* pAudInfo                               );
  257.     void            PlayBGSSound                        ( AudioInfo* pAudInfo                               );
  258.     void            PlaySFXSound                        ( AudioInfo* pAudInfo                               );
  259.    
  260.     ImportedAndID   CheckIfAlreadyImported              ( std::string&  sFilePath, AudioTypes SoundType     );
  261.     unsigned int    GetValidVolume                      ( unsigned int  iVolume                             );
  262.     unsigned int    GetValidTempo                       ( unsigned int  iTempo                              );
  263.  
  264.     AudioInfo*      FindSoundFromPTR                    ( FMOD::Sound*  pSound, AudioTypes SoundType        );
  265.     AudioInfo*      FindAudioInfoInBGMImportedList      ( std::string&  AliasName                           );
  266.     AudioInfo*      FindAudioInfoInBGSImportedList      ( std::string&  AliasName                           );
  267.     AudioInfo*      FindAudioInfoInSEImportedList       ( std::string&  AliasName                           );
  268.     AudioInfo*      ConvertAliasIntoAudioInfoObject     ( std::string&  AliasName, AudioTypes SoundType     );
  269.     ChannelInfo*    ConvertChannelTypeToChannelInfo     ( AudioTypes ChannelType                            );
  270.     std::string     ConvertSoundTypeToAudioFolder       ( AudioTypes AudioType                              );
  271.  
  272.     bool            IsBGMArgument                       ( AudioTypes SoundType                              );
  273.     bool            IsBGSArgument                       ( AudioTypes SoundType                              );
  274.     bool            IsSFXArgument                       ( AudioTypes SoundType                              );
  275. };
  276.  
  277. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement