raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Music.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MUSIC_HPP_
2#define RAYLIB_CPP_INCLUDE_MUSIC_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 {
16class Music : public ::Music {
17 public:
18 Music(::AudioStream stream = {nullptr, nullptr, 0, 0, 0},
19 unsigned int frameCount = 0,
20 bool looping = false,
21 int ctxType = 0,
22 void *ctxData = nullptr) : ::Music{stream, frameCount, looping, ctxType, ctxData} {}
23
24 Music(const ::Music& music) {
25 set(music);
26 }
27
33 Music(const std::string_view fileName) {
34 Load(fileName);
35 }
36
42 Music(const std::string_view fileType, unsigned char* data, int dataSize) {
43 Load(fileType, data, dataSize);
44 }
45
51 Music(const std::string_view fileType, std::span<unsigned char> data) {
52 Load(fileType, data);
53 }
54
55 Music(const Music&) = delete;
56
57 Music(Music&& other) {
58 set(other);
59
60 other.stream = {};
61 other.frameCount = 0;
62 other.looping = false;
63 other.ctxType = 0;
64 other.ctxData = nullptr;
65 }
66
71 Unload();
72 }
73
74 GETTER(::AudioStream, Stream, stream)
75 GETTER(unsigned int, FrameCount, frameCount)
76 GETTERSETTER(bool, Looping, looping)
77 GETTER(int, CtxType, ctxType)
78 GETTER(void*, CtxData, ctxData)
79
80 Music& operator=(const ::Music& music) {
81 set(music);
82 return *this;
83 }
84
85 Music& operator=(const Music&) = delete;
86
87 Music& operator=(Music&& other) noexcept {
88 if (this == &other) {
89 return *this;
90 }
91
92 Unload();
93 set(other);
94
95 other.ctxType = 0;
96 other.ctxData = nullptr;
97 other.looping = false;
98 other.frameCount = 0;
99 other.stream = {};
100
101 return *this;
102 }
103
107 void Unload() {
108 ::UnloadMusicStream(*this);
109 }
110
115 ::PlayMusicStream(*this);
116 return *this;
117 }
118
123 ::UpdateMusicStream(*this);
124 return *this;
125 }
126
131 ::StopMusicStream(*this);
132 return *this;
133 }
134
139 ::PauseMusicStream(*this);
140 return *this;
141 }
142
147 ::ResumeMusicStream(*this);
148 return *this;
149 }
150
154 Music& Seek(float position) {
155 SeekMusicStream(*this, position);
156 return *this;
157 }
158
162 bool IsPlaying() const {
163 return ::IsMusicStreamPlaying(*this);
164 }
165
169 Music& SetVolume(float volume) {
170 ::SetMusicVolume(*this, volume);
171 return *this;
172 }
173
177 Music& SetPitch(float pitch) {
178 ::SetMusicPitch(*this, pitch);
179 return *this;
180 }
181
185 Music& SetPan(float pan = 0.5f) {
186 ::SetMusicPan(*this, pan);
187 return *this;
188 }
189
193 float GetTimeLength() const {
194 return ::GetMusicTimeLength(*this);
195 }
196
200 float GetTimePlayed() const {
201 return ::GetMusicTimePlayed(*this);
202 }
203
209 void Load(const std::string_view fileName) {
210 set(::LoadMusicStream(fileName.data()));
211 if (!IsReady()) {
212 throw RaylibException(TextFormat("Failed to load Music from file: %s", fileName.data()));
213 }
214 }
215
221 void Load(const std::string_view fileType, unsigned char* data, int dataSize) {
222 set(::LoadMusicStreamFromMemory(fileType.data(), data, dataSize));
223 if (!IsReady()) {
224 throw RaylibException(TextFormat("Failed to load Music from %s file dat", fileType.data()));
225 }
226 }
227
233 void Load(const std::string_view fileType, std::span<unsigned char> data) {
234 set(::LoadMusicStreamFromMemory(fileType.data(), data.data(), static_cast<int>(data.size())));
235 if (!IsReady()) {
236 throw RaylibException(TextFormat("Failed to load Music from %s file dat", fileType.data()));
237 }
238 }
239
245 bool IsReady() const {
246 return ::IsMusicReady(*this);
247 }
248
249 protected:
250 void set(const ::Music& music) {
251 stream = music.stream;
252 frameCount = music.frameCount;
253 looping = music.looping;
254 ctxType = music.ctxType;
255 ctxData = music.ctxData;
256 }
257};
258} // namespace raylib
259
260using RMusic = raylib::Music;
261
262#endif // RAYLIB_CPP_INCLUDE_MUSIC_HPP_
Music stream type (audio file streaming from memory)
Definition: Music.hpp:16
Music(const std::string_view fileName)
Load music stream from file.
Definition: Music.hpp:33
bool IsPlaying() const
Check if music is playing.
Definition: Music.hpp:162
Music & Seek(float position)
Seek music to a position (in seconds)
Definition: Music.hpp:154
Music & SetVolume(float volume)
Set volume for music.
Definition: Music.hpp:169
void Load(const std::string_view fileType, unsigned char *data, int dataSize)
Load music stream from memory.
Definition: Music.hpp:221
Music(const std::string_view fileType, std::span< unsigned char > data)
Load music stream from memory.
Definition: Music.hpp:51
bool IsReady() const
Retrieve whether or not the Music has been loaded.
Definition: Music.hpp:245
float GetTimePlayed() const
Get current music time played (in seconds)
Definition: Music.hpp:200
Music & SetPan(float pan=0.5f)
Set pan for a music (0.5 is center)
Definition: Music.hpp:185
Music & Stop()
Stop music playing.
Definition: Music.hpp:130
Music & Play()
Start music playing.
Definition: Music.hpp:114
void Load(const std::string_view fileType, std::span< unsigned char > data)
Load music stream from memory.
Definition: Music.hpp:233
~Music()
Unload music stream.
Definition: Music.hpp:70
Music(const std::string_view fileType, unsigned char *data, int dataSize)
Load music stream from memory.
Definition: Music.hpp:42
Music & Resume()
Resume music playing.
Definition: Music.hpp:146
Music & SetPitch(float pitch)
Set pitch for music.
Definition: Music.hpp:177
float GetTimeLength() const
Get music time length (in seconds)
Definition: Music.hpp:193
Music & Pause()
Pause music playing.
Definition: Music.hpp:138
Music & Update()
Updates buffers for music streaming.
Definition: Music.hpp:122
void Unload()
Unload music stream.
Definition: Music.hpp:107
void Load(const std::string_view fileName)
Load music stream from file.
Definition: Music.hpp:209
Exception used for most raylib-related exceptions.