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 "./RaylibException.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
16class Music : public ::Music {
17public:
18 Music(
19 ::AudioStream stream = {nullptr, nullptr, 0, 0, 0},
20 unsigned int frameCount = 0,
21 bool looping = false,
22 int ctxType = 0,
23 void* ctxData = nullptr)
24 : ::Music{stream, frameCount, looping, ctxType, ctxData} {}
25
26 Music(const ::Music& music) : ::Music(music) { }
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) noexcept {
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
70 ~Music() { Unload(); }
71
72 GETTER(::AudioStream, Stream, stream)
73 GETTER(unsigned int, FrameCount, frameCount)
74 GETTERSETTER(bool, Looping, looping)
75 GETTER(int, CtxType, ctxType)
76 GETTER(void*, CtxData, ctxData)
77
78 Music& operator=(const ::Music& music) {
79 set(music);
80 return *this;
81 }
82
83 Music& operator=(const Music&) = delete;
84
85 Music& operator=(Music&& other) noexcept {
86 if (this == &other) {
87 return *this;
88 }
89
90 Unload();
91 set(other);
92
93 other.ctxType = 0;
94 other.ctxData = nullptr;
95 other.looping = false;
96 other.frameCount = 0;
97 other.stream = {};
98
99 return *this;
100 }
101
105 void Unload() { ::UnloadMusicStream(*this); }
106
111 ::PlayMusicStream(*this);
112 return *this;
113 }
114
119 ::UpdateMusicStream(*this);
120 return *this;
121 }
122
127 ::StopMusicStream(*this);
128 return *this;
129 }
130
135 ::PauseMusicStream(*this);
136 return *this;
137 }
138
143 ::ResumeMusicStream(*this);
144 return *this;
145 }
146
150 Music& Seek(float position) {
151 SeekMusicStream(*this, position);
152 return *this;
153 }
154
158 [[nodiscard]] bool IsPlaying() const { return ::IsMusicStreamPlaying(*this); }
159
163 Music& SetVolume(float volume) {
164 ::SetMusicVolume(*this, volume);
165 return *this;
166 }
167
171 Music& SetPitch(float pitch) {
172 ::SetMusicPitch(*this, pitch);
173 return *this;
174 }
175
179 Music& SetPan(float pan = 0.5f) {
180 ::SetMusicPan(*this, pan);
181 return *this;
182 }
183
187 [[nodiscard]] float GetTimeLength() const { return ::GetMusicTimeLength(*this); }
188
192 [[nodiscard]] float GetTimePlayed() const { return ::GetMusicTimePlayed(*this); }
193
199 void Load(const std::string_view fileName) {
200 set(::LoadMusicStream(fileName.data()));
201 if (!IsValid()) {
202 throw RaylibException(TextFormat("Failed to load Music from file: %s", fileName.data()));
203 }
204 }
205
211 void Load(const std::string_view fileType, unsigned char* data, int dataSize) {
212 set(::LoadMusicStreamFromMemory(fileType.data(), data, dataSize));
213 if (!IsValid()) {
214 throw RaylibException(TextFormat("Failed to load Music from %s file dat", fileType.data()));
215 }
216 }
217
223 void Load(const std::string_view fileType, std::span<unsigned char> data) {
224 set(::LoadMusicStreamFromMemory(fileType.data(), data.data(), static_cast<int>(data.size())));
225 if (!IsValid()) {
226 throw RaylibException(TextFormat("Failed to load Music from %s file dat", fileType.data()));
227 }
228 }
229
235 bool IsValid() const { return ::IsMusicValid(*this); }
236protected:
237 void set(const ::Music& music) {
238 stream = music.stream;
239 frameCount = music.frameCount;
240 looping = music.looping;
241 ctxType = music.ctxType;
242 ctxData = music.ctxData;
243 }
244};
245} // namespace raylib
246
247using RMusic = raylib::Music;
248
249#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:158
Music & Seek(float position)
Seek music to a position (in seconds)
Definition: Music.hpp:150
Music & SetVolume(float volume)
Set volume for music.
Definition: Music.hpp:163
void Load(const std::string_view fileType, unsigned char *data, int dataSize)
Load music stream from memory.
Definition: Music.hpp:211
Music(const std::string_view fileType, std::span< unsigned char > data)
Load music stream from memory.
Definition: Music.hpp:51
float GetTimePlayed() const
Get current music time played (in seconds)
Definition: Music.hpp:192
Music & SetPan(float pan=0.5f)
Set pan for a music (0.5 is center)
Definition: Music.hpp:179
Music & Stop()
Stop music playing.
Definition: Music.hpp:126
bool IsValid() const
Retrieve whether or not the Music has been loaded.
Definition: Music.hpp:235
Music & Play()
Start music playing.
Definition: Music.hpp:110
void Load(const std::string_view fileType, std::span< unsigned char > data)
Load music stream from memory.
Definition: Music.hpp:223
~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:142
Music & SetPitch(float pitch)
Set pitch for music.
Definition: Music.hpp:171
float GetTimeLength() const
Get music time length (in seconds)
Definition: Music.hpp:187
Music & Pause()
Pause music playing.
Definition: Music.hpp:134
Music & Update()
Updates buffers for music streaming.
Definition: Music.hpp:118
void Unload()
Unload music stream.
Definition: Music.hpp:105
void Load(const std::string_view fileName)
Load music stream from file.
Definition: Music.hpp:199
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8