Aspen
2D C++ Class-Based Object Oriented Game Engine
Audio.hpp
1 #ifndef __AUDIO_HPP
2 #define __AUDIO_HPP
3 
4 #include <SDL2/SDL_mixer.h>
5 #include "Object.hpp"
6 
8 namespace Aspen
9 {
11 namespace Audio
12 {
15 {
17  std::string _path;
19  Mix_Chunk *_sound;
21  std::vector<unsigned> _channels;
22 
23 public:
28  SoundEffect(Object *parent = nullptr, std::string name = "SoundEffect");
35  SoundEffect(std::string path, Object *parent = nullptr, std::string name = "SoundEffect");
36 
39  void End();
40 
43  void SetPath(std::string path);
46  std::string GetPath();
47 
50  Mix_Chunk *GetSound();
51 
54  bool Load();
55 
59  void Play(int channel = -1);
61  void Stop();
66  bool IsPlayingOn(int channel);
70  bool IsPlaying();
71 
74  void OnDeactivate();
75 };
76 
78 class Music : public Object::Object
79 {
81  static Music *_lastPlayed;
83  std::string _path;
85  Mix_Music *_music;
86 
87 public:
92  Music(Object *parent = nullptr, std::string name = "Music");
99  Music(std::string path, Object *parent = nullptr, std::string name = "Music");
100 
103  void End();
104 
107  void SetPath(std::string path);
110  std::string GetPath();
111 
114  Mix_Music *GetMusic();
115 
118  bool Load();
123  void Play(bool loop = false, double fadeIn = 0.0);
126  void Stop(double fadeOut = 0.0);
130  bool IsPlaying();
131 
133  void OnDeactivate();
134 };
135 
137 class Audio : public Object::Object
138 {
140  static unsigned _acount;
141 
142 public:
148  Audio(Object *parent = nullptr, std::string name = "Audio");
150  ~Audio();
151 
154  void End();
155 
159  bool IsPlayingMusic();
162  void StopMusic(double fadeOut = 0.0);
163 };
164 } // namespace Audio
165 } // namespace Aspen
166 
167 #endif
void SetPath(std::string path)
Sets the file path and loads the new file.
bool Load()
Loads the music from the current path.
~Audio()
Destructor.
void Stop()
Stops any instance of the sound currently playing.
void Play(bool loop=false, double fadeIn=0.0)
Plays this music.
Object(Object *parent=nullptr, std::string name="Object")
Constructor Derived classes should call this in their constructors' initialization list.
Mix_Music * _music
loaded music file
Definition: Audio.hpp:85
void Stop(double fadeOut=0.0)
Stops this music if is currently playing.
Audio class.
Definition: Audio.hpp:137
General base class Allows for parent/child relationship trees.
Definition: Object.hpp:33
bool IsPlaying()
Determines if this music is currently playing.
SoundEffect(Object *parent=nullptr, std::string name="SoundEffect")
Constructor.
std::vector< unsigned > _channels
Channels the sound has been played on.
Definition: Audio.hpp:21
SoundEffect class.
Definition: Audio.hpp:14
bool Load()
Loads the current file path.
static Music * _lastPlayed
Tracks the last music file that was played.
Definition: Audio.hpp:81
std::string GetPath()
Gets the file path.
bool IsPlayingOn(int channel)
Determines if the sound is playing on a given channel.
void End()
Shuts down and invalidates Object and all of its children An invalid child Object will be deleted by ...
Mix_Music * GetMusic()
Gets the unwrapped SDL_Mixer music.
Audio(Object *parent=nullptr, std::string name="Audio")
Constructor Derived classes should call this in their constructors' initialization list.
Mix_Chunk * _sound
Loaded sound chunk.
Definition: Audio.hpp:19
bool IsPlayingMusic()
Determines if any music is currently playing.
void End()
Shuts down and invalidates Object and all of its children An invalid child Object will be deleted by ...
void End()
Shuts down and invalidates Object and all of its children An invalid child Object will be deleted by ...
void Play(int channel=-1)
Plays the sound on a given channel.
bool IsPlaying()
Determines if the sound is playing on any channel.
void SetPath(std::string path)
Sets the file path and loads the new file.
Music(Object *parent=nullptr, std::string name="Music")
Constructor.
static unsigned _acount
Total number of Audio classes in existence.
Definition: Audio.hpp:140
std::string _path
Path of this music file.
Definition: Audio.hpp:83
Mix_Chunk * GetSound()
Gets the unwrapped SDL_Mixer sound chunk.
void OnDeactivate()
Stops the music from playing if it's currently playing.
Aspen engine namespace.
Definition: Audio.hpp:8
void StopMusic(double fadeOut=0.0)
Stops any music currently playing.
std::string _path
Path to the soundeffect file.
Definition: Audio.hpp:17
std::string GetPath()
Gets the file path.
Music class.
Definition: Audio.hpp:78
void OnDeactivate()
Stops any instance of the sound on deactivation This should be called or reimplemented by derived cla...