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 "./RaylibException.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
21class Sound : public ::Sound {
22public:
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) noexcept {
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) { Load(wave); }
57
58 ~Sound() { Unload(); }
59
60 GETTER(unsigned int, FrameCount, frameCount)
61 GETTER(::AudioStream, Stream, stream)
62
63 Sound& operator=(Sound&& other) noexcept {
64 if (this == &other) {
65 return *this;
66 }
67
68 Unload();
69 set(other);
70 other.frameCount = 0;
71 other.stream = {nullptr, nullptr, 0, 0, 0};
72
73 return *this;
74 }
75
79 Sound& Update(const void* data, int samplesCount) {
80 ::UpdateSound(*this, data, samplesCount);
81 return *this;
82 }
83
87 Sound& Update(const std::span<std::byte> data) {
88 ::UpdateSound(*this, data.data(), static_cast<int>(data.size()));
89 return *this;
90 }
91
95 Sound& Update(const void* data) {
96 ::UpdateSound(*this, data, static_cast<int>(frameCount));
97 return *this;
98 }
99
103 void Unload() {
104 // Protect against calling UnloadSound() twice.
105 if (frameCount != 0) {
106 ::UnloadSound(*this);
107 frameCount = 0;
108 }
109 }
110
115 ::PlaySound(*this);
116 return *this;
117 }
118
123 ::StopSound(*this);
124 return *this;
125 }
126
131 ::PauseSound(*this);
132 return *this;
133 }
134
139 ::ResumeSound(*this);
140 return *this;
141 }
142
146 [[nodiscard]] bool IsPlaying() const { return ::IsSoundPlaying(*this); }
147
151 Sound& SetVolume(float volume) {
152 ::SetSoundVolume(*this, volume);
153 return *this;
154 }
155
159 Sound& SetPitch(float pitch) {
160 ::SetSoundPitch(*this, pitch);
161 return *this;
162 }
163
167 Sound& SetPan(float pan = 0.5f) {
168 ::SetSoundPan(*this, pan);
169 return *this;
170 }
171
177 void Load(const std::string_view fileName) {
178 set(::LoadSound(fileName.data()));
179 if (!IsValid()) {
180 throw RaylibException("Failed to load Sound from file");
181 }
182 }
183
189 void Load(const ::Wave& wave) {
190 set(::LoadSoundFromWave(wave));
191 if (!IsValid()) {
192 throw RaylibException("Failed to load Wave");
193 }
194 }
195
201 [[nodiscard]] bool IsValid() const { return ::IsSoundValid(*this); }
202protected:
203 void set(const ::Sound& sound) {
204 frameCount = sound.frameCount;
205 stream = sound.stream;
206 }
207};
208} // namespace raylib
209
210using RSound = raylib::Sound;
211
212#endif // RAYLIB_CPP_INCLUDE_SOUND_HPP_
AudioStream management functions.
Definition: AudioStream.hpp:11
Exception used for most raylib-related exceptions.
Wave/Sound management functions.
Definition: Sound.hpp:21
void Unload()
Unload sound.
Definition: Sound.hpp:103
bool IsValid() const
Retrieve whether or not the Sound buffer is loaded.
Definition: Sound.hpp:201
void Load(const ::Wave &wave)
Loads the given Wave object into the Sound.
Definition: Sound.hpp:189
Sound & Stop()
Stop playing a sound.
Definition: Sound.hpp:122
Sound & SetVolume(float volume)
Set volume for a sound (1.0 is max level)
Definition: Sound.hpp:151
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:130
Sound & Update(const void *data)
Update sound buffer with new data, assuming it's the same sample count.
Definition: Sound.hpp:95
Sound & Update(const std::span< std::byte > data)
Update sound buffer with new data.
Definition: Sound.hpp:87
Sound & Resume()
Resume a paused sound.
Definition: Sound.hpp:138
Sound & Update(const void *data, int samplesCount)
Update sound buffer with new data.
Definition: Sound.hpp:79
Sound & SetPan(float pan=0.5f)
Set pan for a sound (0.5 is center)
Definition: Sound.hpp:167
Sound & Play()
Play a sound.
Definition: Sound.hpp:114
bool IsPlaying() const
Check if a sound is currently playing.
Definition: Sound.hpp:146
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:177
Sound & SetPitch(float pitch)
Set pitch for a sound (1.0 is base level)
Definition: Sound.hpp:159
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8