raylib-cpp
C++ object-oriented wrapper library for raylib.
Sound.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_SOUND_HPP_
2 #define RAYLIB_CPP_INCLUDE_SOUND_HPP_
3 
4 #include <string>
5 
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 #include "./RaylibException.hpp"
9 
10 namespace raylib {
19 class Sound : public ::Sound {
20  public:
21  Sound(const Sound&) = delete;
22  Sound& operator=(const Sound&) = delete;
23 
24  Sound() {
25  frameCount = 0;
26  stream.buffer = nullptr;
27  }
28 
29  Sound(::AudioStream stream, unsigned int frameCount) : ::Sound{stream, frameCount} {
30  // Nothing.
31  }
32 
33  Sound(Sound&& other) {
34  set(other);
35 
36  other.frameCount = 0;
37  other.stream = { 0, 0, 0, 0 };
38  }
39 
45  Sound(const std::string& fileName) {
46  if (!Load(fileName)) {
47  throw RaylibException(TextFormat("Failed to load Sound from file: %s", fileName.c_str()));
48  }
49  }
50 
56  Sound(const ::Wave& wave) {
57  if (!Load(wave)) {
58  throw RaylibException("Failed to load Sound from Wave");
59  }
60  }
61 
62  ~Sound() {
63  Unload();
64  }
65 
66  GETTERSETTER(unsigned int, FrameCount, frameCount)
67  GETTERSETTER(::AudioStream, Stream, stream)
68 
69  Sound& operator=(Sound&& other) {
70  if (this == &other) {
71  return *this;
72  }
73 
74  Unload();
75  set(other);
76  other.frameCount = 0;
77  other.stream = { 0, 0, 0, 0 };
78 
79  return *this;
80  }
81 
85  inline Sound& Update(const void *data, int samplesCount) {
86  ::UpdateSound(*this, data, samplesCount);
87  return *this;
88  }
89 
93  inline Sound& Update(const void *data) {
94  ::UpdateSound(*this, data, static_cast<int>(frameCount));
95  return *this;
96  }
97 
101  inline void Unload() {
102  ::UnloadSound(*this);
103  }
104 
108  inline Sound& Play() {
109  ::PlaySound(*this);
110  return *this;
111  }
112 
116  inline Sound& Stop() {
117  ::StopSound(*this);
118  return *this;
119  }
120 
124  inline Sound& Pause() {
125  ::PauseSound(*this);
126  return *this;
127  }
128 
132  inline Sound& Resume() {
133  ::ResumeSound(*this);
134  return *this;
135  }
136 
140  inline Sound& PlayMulti() {
141  ::PlaySoundMulti(*this);
142  return *this;
143  }
144 
148  inline Sound& StopMulti() {
149  ::StopSoundMulti();
150  return *this;
151  }
152 
156  inline bool IsPlaying() const {
157  return ::IsSoundPlaying(*this);
158  }
159 
163  inline Sound& SetVolume(float volume) {
164  ::SetSoundVolume(*this, volume);
165  return *this;
166  }
167 
171  inline Sound& SetPitch(float pitch) {
172  ::SetSoundPitch(*this, pitch);
173  return *this;
174  }
175 
181  bool Load(const std::string& fileName) {
182  set(::LoadSound(fileName.c_str()));
183  return IsReady();
184  }
185 
189  bool Load(const ::Wave& wave) {
190  set(::LoadSoundFromWave(wave));
191  return IsReady();
192  }
193 
194 
200  bool IsReady() const {
201  return stream.buffer != nullptr;
202  }
203 
204  private:
205  inline void set(const ::Sound& sound) {
206  frameCount = sound.frameCount;
207  stream = sound.stream;
208  }
209 };
210 } // namespace raylib
211 
212 #endif // RAYLIB_CPP_INCLUDE_SOUND_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Sound::Load
bool Load(const ::Wave &wave)
Loads the given Wave object into the Sound.
Definition: Sound.hpp:189
raylib::Sound::StopMulti
Sound & StopMulti()
Stop any sound playing (using multichannel buffer pool)
Definition: Sound.hpp:148
raylib::Sound::SetVolume
Sound & SetVolume(float volume)
Set volume for a sound (1.0 is max level)
Definition: Sound.hpp:163
raylib::Sound::Update
Sound & Update(const void *data, int samplesCount)
Update sound buffer with new data.
Definition: Sound.hpp:85
raylib::Sound
Wave/Sound management functions.
Definition: Sound.hpp:19
raylib::Sound::Pause
Sound & Pause()
Pause a sound.
Definition: Sound.hpp:124
raylib::Sound::IsReady
bool IsReady() const
Retrieve whether or not the Sound buffer is loaded.
Definition: Sound.hpp:200
raylib::Sound::Resume
Sound & Resume()
Resume a paused sound.
Definition: Sound.hpp:132
raylib::Sound::Stop
Sound & Stop()
Stop playing a sound.
Definition: Sound.hpp:116
raylib::Sound::Load
bool Load(const std::string &fileName)
Load a sound from the given file.
Definition: Sound.hpp:181
raylib::Sound::IsPlaying
bool IsPlaying() const
Check if a sound is currently playing.
Definition: Sound.hpp:156
raylib::Sound::Unload
void Unload()
Unload sound.
Definition: Sound.hpp:101
raylib::AudioStream
AudioStream management functions.
Definition: AudioStream.hpp:12
raylib::Sound::Sound
Sound(const ::Wave &wave)
Loads a sound from the given Wave.
Definition: Sound.hpp:56
raylib::Sound::Update
Sound & Update(const void *data)
Update sound buffer with new data, assuming it's the same sample count.
Definition: Sound.hpp:93
raylib::Sound::SetPitch
Sound & SetPitch(float pitch)
Set pitch for a sound (1.0 is base level)
Definition: Sound.hpp:171
raylib::RaylibException
Exception used for most raylib-related exceptions.
Definition: RaylibException.hpp:13
raylib::Sound::Play
Sound & Play()
Play a sound.
Definition: Sound.hpp:108
raylib::Sound::PlayMulti
Sound & PlayMulti()
Play a sound (using multichannel buffer pool)
Definition: Sound.hpp:140
raylib::Sound::Sound
Sound(const std::string &fileName)
Loads a sound from the given file.
Definition: Sound.hpp:45