raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Sound.hpp
1#ifndef RAYLIB_CPP_INCLUDE_SOUND_HPP_
2#define RAYLIB_CPP_INCLUDE_SOUND_HPP_
3
4#include <span>
5#include <string>
6#include <string_view>
7
8#include "./raylib.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./RaylibException.hpp"
11
12namespace raylib {
21class Sound : public ::Sound {
22 public:
23 Sound(const Sound&) = delete;
24 Sound& operator=(const Sound&) = delete;
25
26 Sound() {
27 stream = { nullptr, nullptr, 0, 0, 0 };
28 frameCount = 0;
29 }
30
31 Sound(::AudioStream stream, unsigned int frameCount) : ::Sound{stream, frameCount} {
32 // Nothing.
33 }
34
35 Sound(Sound&& other) {
36 set(other);
37
38 other.stream = { nullptr, nullptr, 0, 0, 0 };
39 other.frameCount = 0;
40 }
41
47 Sound(const std::string_view fileName) {
48 Load(fileName);
49 }
50
56 Sound(const ::Wave& wave) {
57 Load(wave);
58 }
59
60 ~Sound() {
61 Unload();
62 }
63
64 GETTER(unsigned int, FrameCount, frameCount)
65 GETTER(::AudioStream, Stream, stream)
66
67 Sound& operator=(Sound&& other) noexcept {
68 if (this == &other) {
69 return *this;
70 }
71
72 Unload();
73 set(other);
74 other.frameCount = 0;
75 other.stream = { nullptr, nullptr, 0, 0, 0 };
76
77 return *this;
78 }
79
83 Sound& Update(const void *data, int samplesCount) {
84 ::UpdateSound(*this, data, samplesCount);
85 return *this;
86 }
87
91 Sound& Update(const std::span<std::byte> data) {
92 ::UpdateSound(*this, data.data(), static_cast<int>(data.size()));
93 return *this;
94 }
95
99 Sound& Update(const void *data) {
100 ::UpdateSound(*this, data, static_cast<int>(frameCount));
101 return *this;
102 }
103
107 void Unload() {
108 // Protect against calling UnloadSound() twice.
109 if (frameCount != 0) {
110 ::UnloadSound(*this);
111 frameCount = 0;
112 }
113 }
114
119 ::PlaySound(*this);
120 return *this;
121 }
122
127 ::StopSound(*this);
128 return *this;
129 }
130
135 ::PauseSound(*this);
136 return *this;
137 }
138
143 ::ResumeSound(*this);
144 return *this;
145 }
146
150 bool IsPlaying() const {
151 return ::IsSoundPlaying(*this);
152 }
153
157 Sound& SetVolume(float volume) {
158 ::SetSoundVolume(*this, volume);
159 return *this;
160 }
161
165 Sound& SetPitch(float pitch) {
166 ::SetSoundPitch(*this, pitch);
167 return *this;
168 }
169
173 Sound& SetPan(float pan = 0.5f) {
174 ::SetSoundPan(*this, pan);
175 return *this;
176 }
177
183 void Load(const std::string_view fileName) {
184 set(::LoadSound(fileName.data()));
185 if (!IsReady()) {
186 throw RaylibException("Failed to load Sound from file");
187 }
188 }
189
195 void Load(const ::Wave& wave) {
196 set(::LoadSoundFromWave(wave));
197 if (!IsReady()) {
198 throw RaylibException("Failed to load Wave");
199 }
200 }
201
207 bool IsReady() const {
208 return ::IsSoundReady(*this);
209 }
210
211 protected:
212 void set(const ::Sound& sound) {
213 frameCount = sound.frameCount;
214 stream = sound.stream;
215 }
216};
217} // namespace raylib
218
219using RSound = raylib::Sound;
220
221#endif // RAYLIB_CPP_INCLUDE_SOUND_HPP_
AudioStream management functions.
Definition: AudioStream.hpp:13
Exception used for most raylib-related exceptions.
Wave/Sound management functions.
Definition: Sound.hpp:21
void Unload()
Unload sound.
Definition: Sound.hpp:107
void Load(const ::Wave &wave)
Loads the given Wave object into the Sound.
Definition: Sound.hpp:195
Sound & Stop()
Stop playing a sound.
Definition: Sound.hpp:126
Sound & SetVolume(float volume)
Set volume for a sound (1.0 is max level)
Definition: Sound.hpp:157
Sound(const std::string_view fileName)
Loads a sound from the given file.
Definition: Sound.hpp:47
Sound & Pause()
Pause a sound.
Definition: Sound.hpp:134
bool IsReady() const
Retrieve whether or not the Sound buffer is loaded.
Definition: Sound.hpp:207
Sound & Update(const void *data)
Update sound buffer with new data, assuming it's the same sample count.
Definition: Sound.hpp:99
Sound & Update(const std::span< std::byte > data)
Update sound buffer with new data.
Definition: Sound.hpp:91
Sound & Resume()
Resume a paused sound.
Definition: Sound.hpp:142
Sound & Update(const void *data, int samplesCount)
Update sound buffer with new data.
Definition: Sound.hpp:83
Sound & SetPan(float pan=0.5f)
Set pan for a sound (0.5 is center)
Definition: Sound.hpp:173
Sound & Play()
Play a sound.
Definition: Sound.hpp:118
bool IsPlaying() const
Check if a sound is currently playing.
Definition: Sound.hpp:150
Sound(const ::Wave &wave)
Loads a sound from the given Wave.
Definition: Sound.hpp:56
void Load(const std::string_view fileName)
Load a sound from the given file.
Definition: Sound.hpp:183
Sound & SetPitch(float pitch)
Set pitch for a sound (1.0 is base level)
Definition: Sound.hpp:165