BARE2D
AudioManager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Sound.hpp"
4 #include "Music.hpp"
5 
6 namespace BARE2D
7 {
8 
9  /**
10  * @class AudioManager
11  * @file AudioManager.hpp
12  * @brief The AudioManager acts as a friendly wrapper for the SDL_Mixer library. It basically just plays sounds and music, and allows a user to adjust sound levels, etc.
13  */
15  {
16  public:
17  static AudioManager* getInstance();
18  static void release();
19 
20  /**
21  * @brief Initializes all necessary bits and pieces.
22  */
23  void init();
24 
25  /**
26  * @brief Destroys/quits all necessary bits and pieces.
27  */
28  void destroy();
29 
30  /**
31  * @brief Plays a sound at a position.
32  * @param sound A reference to the sound that is to be played.
33  * @param fadeTime The number of milliseconds that the sound will take to fade in. Defaults to 0 (no fade).
34  * @param timeOut the number of milliseconds before the sound times out. Defaults to 0 (never).
35  * @param angle The angle (clockwise in degrees, where 0 is directly forward) that the sound is to be played at. Defaults to 0 (directly in front)
36  * @param distance The distance from the camera that the sound should be played (from 0 (very near, loud) to 255 (very distant, quiet))
37  * @return The unsigned integer that represents the sound's ID.
38  */
39  unsigned int playSound(Sound& sound, unsigned int fadeTime = 0, unsigned int timeOut = 0, int angle = 0, unsigned char distance = 128);
40  /**
41  * @brief Resets the sound's position for positional audio.
42  * @param soundID A reference to the ID that represents the sound to move.
43  * @param angle The angle (clockwise in degrees, where 0 is directly forward) that the sound is to be played at.
44  * @param distance The distance from the camera that the sound should be played (from 0 (very near, loud) to 255 (very distant, quiet))
45  */
46  void moveSound(unsigned int& soundID, int angle, unsigned char distance = 128);
47  /**
48  * @brief Stops a sound
49  * @param fadeTime The number of milliseconds the sound will take to fade out. Defaults to 0 (no fade).
50  * @param soundID The ID of the sound to stop, given by playSound(...) calls.
51  */
52  void stopSound(unsigned int& soundID, unsigned int fadeTime = 0);
53  /**
54  * @brief Pauses a sound
55  * @param soundID The ID of the sound to pause, given by playSound(...) calls.
56  */
57  void pauseSound(unsigned int& soundID);
58  /**
59  * @brief Resumes a paused sound
60  * @param soundID The ID of the sound to resume, given by playSound(...) calls.
61  */
62  void resumeSound(unsigned int& soundID);
63 
64  /**
65  * @brief Plays some music! Only one "music" can be played at a time.
66  * @param music The music to play
67  * @param fadeTime The number of milliseconds that the sound will take to fade in. Defaults to 0 (no fade).
68  */
69  void playMusic(Music& music, unsigned int fadeTime = 0);
70  /**
71  * @brief Stops the music
72  * @param fadeTime The number of milliseconds the sound will take to fade out. Defaults to 0 (no fade).
73  */
74  void stopMusic(unsigned int fadeTime = 0);
75  /**
76  * @brief Pauses the music
77  */
78  void pauseMusic();
79  /**
80  * @brief Resumes the paused music
81  */
82  void resumeMusic();
83 
84  /**
85  * @brief Mutes all sounds and music
86  */
87  void mute();
88  /**
89  * @brief Unmutes all sounds and music
90  */
91  void unmute();
92 
93  /**
94  * @brief Sets the volume level of all sounds
95  * @param volumeLevel The level of the volume (clamped from 0 (muted) to 255(loud))
96  */
97  void setSoundVolume(unsigned char volumeLevel);
98  /**
99  * @brief Sets the volume level of all music
100  * @param volumeLevel The level of the volume - 0 is muted, 255 is very loud.
101  */
102  void setMusicVolume(unsigned char volumeLevel);
103  /**
104  * @brief Sets the modifier of all sound levels - eg., if sounds were set at 40% and music at 60%, setting the master modifier to 50% would set sounds to 20% and music to 30%
105  * @param masterLevel The level of the volume - 0 is muted, 255 is very loud.
106  */
107  void setMasterVolumeModifier(unsigned char masterLevel);
108 
109  /**
110  * @return True if there is music currently playing, false otherwise.
111  */
112  bool isMusicPlaying();
113 
114  private:
115  AudioManager();
116  ~AudioManager();
117 
119 
120  unsigned char m_soundVolume = 255, m_musicVolume = 255, m_masterVolume = 255;
121  bool m_muted = false;
122 
123  };
124 
125 }
BARE2D::Sound
Definition: Sound.hpp:7
utf8::distance
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition: checked.h:198
BARE2D::AudioManager::resumeMusic
void resumeMusic()
Resumes the paused music.
Definition: AudioManager.cpp:174
BARE2D::AudioManager::m_masterVolume
unsigned char m_masterVolume
Definition: AudioManager.hpp:120
BARE2D::AudioManager::resumeSound
void resumeSound(unsigned int &soundID)
Resumes a paused sound.
Definition: AudioManager.cpp:140
BARE2D
Definition: App.cpp:13
BARE2D::AudioManager::playMusic
void playMusic(Music &music, unsigned int fadeTime=0)
Plays some music! Only one "music" can be played at a time.
Definition: AudioManager.cpp:145
BARE2D::AudioManager::m_instance
static AudioManager * m_instance
Definition: AudioManager.hpp:118
BARE2D::Music
Definition: Music.hpp:7
BARE2D::AudioManager::setMusicVolume
void setMusicVolume(unsigned char volumeLevel)
Sets the volume level of all music.
Definition: AudioManager.cpp:204
BARE2D::AudioManager::isMusicPlaying
bool isMusicPlaying()
Definition: AudioManager.cpp:223
BARE2D::AudioManager::setSoundVolume
void setSoundVolume(unsigned char volumeLevel)
Sets the volume level of all sounds.
Definition: AudioManager.cpp:198
BARE2D::AudioManager::pauseSound
void pauseSound(unsigned int &soundID)
Pauses a sound.
Definition: AudioManager.cpp:135
Music.hpp
BARE2D::AudioManager::~AudioManager
~AudioManager()
Definition: AudioManager.cpp:32
BARE2D::AudioManager::release
static void release()
Definition: AudioManager.cpp:20
BARE2D::AudioManager::init
void init()
Initializes all necessary bits and pieces.
Definition: AudioManager.cpp:37
BARE2D::AudioManager::pauseMusic
void pauseMusic()
Pauses the music.
Definition: AudioManager.cpp:169
BARE2D::AudioManager::unmute
void unmute()
Unmutes all sounds and music.
Definition: AudioManager.cpp:189
BARE2D::AudioManager::stopSound
void stopSound(unsigned int &soundID, unsigned int fadeTime=0)
Stops a sound.
Definition: AudioManager.cpp:126
Sound.hpp
BARE2D::AudioManager::playSound
unsigned int playSound(Sound &sound, unsigned int fadeTime=0, unsigned int timeOut=0, int angle=0, unsigned char distance=128)
Plays a sound at a position.
Definition: AudioManager.cpp:92
BARE2D::AudioManager::AudioManager
AudioManager()
Definition: AudioManager.cpp:28
BARE2D::AudioManager::getInstance
static AudioManager * getInstance()
Definition: AudioManager.cpp:12
BARE2D::AudioManager::setMasterVolumeModifier
void setMasterVolumeModifier(unsigned char masterLevel)
Sets the modifier of all sound levels - eg., if sounds were set at 40% and music at 60%,...
Definition: AudioManager.cpp:210
BARE2D::AudioManager::stopMusic
void stopMusic(unsigned int fadeTime=0)
Stops the music.
Definition: AudioManager.cpp:160
BARE2D::AudioManager::m_muted
bool m_muted
Definition: AudioManager.hpp:121
BARE2D::AudioManager::m_soundVolume
unsigned char m_soundVolume
Definition: AudioManager.hpp:120
BARE2D::AudioManager
Definition: AudioManager.hpp:14
BARE2D::AudioManager::mute
void mute()
Mutes all sounds and music.
Definition: AudioManager.cpp:179
BARE2D::AudioManager::destroy
void destroy()
Destroys/quits all necessary bits and pieces.
Definition: AudioManager.cpp:71
BARE2D::AudioManager::moveSound
void moveSound(unsigned int &soundID, int angle, unsigned char distance=128)
Resets the sound's position for positional audio.
Definition: AudioManager.cpp:119
BARE2D::AudioManager::m_musicVolume
unsigned char m_musicVolume
Definition: AudioManager.hpp:120