Fireworks Engine  v2.0
Lightweight Sandbox Game Engine using OpenGL for additional Customisation and Quick Prototyping
audioclip.h
Go to the documentation of this file.
1 #pragma once
2 #include <al.h>
3 #include <alc.h>
4 #include <src/utils/wavloader.h>
5 
6 
7 namespace fireworks { namespace audio {
8 
10 #define STREAM_BUFFER_COUNT 4
11 #define STREAM_BUFFER_SIZE 65536
13 
15  class AudioClip
16  {
17  public:
19  int gain;
21  int pitch;
22  public:
27  AudioClip(const std::string& filePath);
28  ~AudioClip();
29 
31  void Play();
33  void Pause();
35  void Stop();
37  void Replay();
39  void PlayOnce();
41  void PlayOnceOnly();
43  void Loop();
44 
48  inline std::uint32_t getAudioFormat() const { return m_AudioFormat; }
50  inline std::uint32_t getSampleRate() const { return m_SampleRate; }
52  inline std::uint32_t getBPS() const { return m_BitsPerSecond; }
54  inline std::uint32_t getClipSizeinKB() const { return (m_Size / 1024); }
55 
57  inline bool isLooping() { alGetSourcei(m_Source, AL_LOOPING, &m_SourceState); return m_SourceState == 1; }
59  inline bool isPaused() { getSourceState(); return m_SourceState == AL_PAUSED; }
61  inline bool isPlaying() { getSourceState(); return m_SourceState == AL_PLAYING; }
63  inline bool isStopped() { getSourceState(); return m_SourceState == AL_STOPPED; }
65  inline bool didPlayonce() const { return m_DidPlayOnce; }
66 
68  inline ALuint getBuffer() const { return m_BufferID; }
70  inline ALuint getSource() const { return m_Source; }
71  private:
72  static ALboolean s_g_bEAX;
73  static ALCdevice* s_Device;
74  static ALCcontext* s_Context;
75 
76  std::string m_FilePath;
77  const char* m_Data;
78  utils::WavLoader m_WavLoader;
79 
80  std::uint32_t m_AudioFormat;
81  std::uint32_t m_SampleRate;
82  std::uint32_t m_BitsPerSecond;
83  std::uint32_t m_Size;
84 
85  ALuint m_BufferID;
86  ALuint m_Source;
87  ALint m_SourceState;
88 
89  bool m_EnableLooping;
90  bool m_DidPlayOnce;
91  private:
92  inline void getSourceState() { alGetSourcei(m_Source, AL_SOURCE_STATE, &m_SourceState); }
93  void init();
94  void load();
95  void enableStreamingMode();
96  };
97 } }
bool isPlaying()
Tells whether the audio clip is playing or not.
Definition: audioclip.h:61
ALuint getBuffer() const
Gets the Buffer of the audio clip data.
Definition: audioclip.h:68
void Pause()
Pause a Source.
Definition: audioclip.cpp:70
void PlayOnce()
Play the Source Once in any loop.
Definition: audioclip.cpp:51
~AudioClip()
Definition: audioclip.cpp:16
Class responsible for managing audio clips.
Definition: audioclip.h:15
void PlayOnceOnly()
Play the Source only Once in any loop.
Definition: audioclip.cpp:57
std::uint32_t getAudioFormat() const
Gets the audio format of the audio clip.
Definition: audioclip.h:48
bool isLooping()
Tells whether the audio clip is in looping state or not.
Definition: audioclip.h:57
ALuint getSource() const
Gets the Source ID of the audio source that plays the clip.
Definition: audioclip.h:70
void Loop()
Play the Source in Looping mode in any loop.
Definition: audioclip.cpp:63
std::uint32_t getClipSizeinKB() const
Gets the the clip size in Kb.
Definition: audioclip.h:54
bool isStopped()
Tells whether the audio clip is stopped or not.
Definition: audioclip.h:63
Definition: audioclip.cpp:3
std::uint32_t getSampleRate() const
Gets the sample rate of the audio clip.
Definition: audioclip.h:50
A class to load WAVE (.wav) audio files.
Definition: wavloader.h:12
AudioClip(const std::string &filePath)
Creates a audio clip to play in your application.
Definition: audioclip.cpp:9
bool isPaused()
Tells whether the audio clip is paused or not.
Definition: audioclip.h:59
void Play()
Play, Replay, or Resume a Source.
Definition: audioclip.cpp:26
int gain
Sets the gain of the audio source.
Definition: audioclip.h:19
void Replay()
Rewind a Source (set playback position to beginning)
Definition: audioclip.cpp:46
void Stop()
Stop a Source.
Definition: audioclip.cpp:41
int pitch
Sets the pitch of the audio source.
Definition: audioclip.h:21
std::uint32_t getBPS() const
Gets the bits per second of the audio clip.
Definition: audioclip.h:52
bool didPlayonce() const
Tells whether if the audio clip did play at least once.
Definition: audioclip.h:65