raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Wave.hpp
1#ifndef RAYLIB_CPP_INCLUDE_WAVE_HPP_
2#define RAYLIB_CPP_INCLUDE_WAVE_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 Wave : public ::Wave {
17 public:
18 Wave(const ::Wave& wave) {
19 set(wave);
20 }
21
22 Wave(
23 unsigned int frameCount = 0,
24 unsigned int sampleRate = 0,
25 unsigned int sampleSize = 0,
26 unsigned int channels = 0,
27 void *data = nullptr) : ::Wave{frameCount, sampleRate, sampleSize, channels, data} {
28 // Nothing.
29 }
30
36 Wave(const std::string_view fileName) {
37 Load(fileName);
38 }
39
45 Wave(const std::string_view fileType, const unsigned char *fileData, int dataSize) {
46 Load(fileType, fileData, dataSize);
47 }
48
54 Wave(const std::string_view fileType, std::span<const unsigned char> fileData) {
55 Load(fileType, fileData);
56 }
57
58 Wave(const Wave& other) {
59 set(other.Copy());
60 }
61
62 Wave(Wave&& other) {
63 set(other);
64
65 other.frameCount = 0;
66 other.sampleRate = 0;
67 other.sampleSize = 0;
68 other.channels = 0;
69 other.data = nullptr;
70 }
71
76 Unload();
77 }
78
79 GETTER(unsigned int, FrameCount, frameCount)
80 GETTER(unsigned int, SampleRate, sampleRate)
81 GETTER(unsigned int, SampleSize, sampleSize)
82 GETTER(unsigned int, Channels, channels)
83 GETTER(void *, Data, data)
84
85 Wave& operator=(const ::Wave& wave) {
86 set(wave);
87 return *this;
88 }
89
90 Wave& operator=(const Wave& other) {
91 if (this == &other) {
92 return *this;
93 }
94
95 Unload();
96 set(other.Copy());
97
98 return *this;
99 }
100
101 Wave& operator=(Wave&& other) noexcept {
102 if (this != &other) {
103 return *this;
104 }
105
106 Unload();
107 set(other);
108
109 other.frameCount = 0;
110 other.sampleRate = 0;
111 other.sampleSize = 0;
112 other.channels = 0;
113 other.data = nullptr;
114
115 return *this;
116 }
117
121 ::Wave Copy() const {
122 return ::WaveCopy(*this);
123 }
124
128 Wave& Crop(int initSample, int finalSample) {
129 ::WaveCrop(this, initSample, finalSample);
130 return *this;
131 }
132
136 Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
137 ::WaveFormat(this, SampleRate, SampleSize, Channels);
138 return *this;
139 }
140
144 float* LoadSamples() {
145 return ::LoadWaveSamples(*this);
146 }
147
151 static void UnloadSamples(float *samples) {
152 ::UnloadWaveSamples(samples);
153 }
154
158 bool Export(const std::string_view fileName) {
159 // TODO(RobLoach): Throw exception on error.
160 return ::ExportWave(*this, fileName.data());
161 }
162
166 bool ExportAsCode(const std::string_view fileName) {
167 // TODO(RobLoach): Throw exception on error.
168 return ::ExportWaveAsCode(*this, fileName.data());
169 }
170
174 void Unload() {
175 // Protect against calling UnloadWave() twice.
176 if (data != nullptr) {
177 ::UnloadWave(*this);
178 data = nullptr;
179 }
180 }
181
186 return ::LoadSoundFromWave(*this);
187 }
188
192 operator ::Sound() {
193 return LoadSound();
194 }
195
201 void Load(const std::string_view fileName) {
202 set(::LoadWave(fileName.data()));
203 if (!IsReady()) {
204 throw RaylibException("Failed to load Wave from file: " + std::string(fileName));
205 }
206 }
207
213 void Load(const std::string_view fileType, const unsigned char *fileData, int dataSize) {
214 set(::LoadWaveFromMemory(fileType.data(), fileData, dataSize));
215 if (!IsReady()) {
216 throw RaylibException("Failed to load Wave from file data of type: " + std::string(fileType));
217 }
218 }
219
225 void Load(const std::string_view fileType, std::span<const unsigned char> fileData) {
226 set(::LoadWaveFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size())));
227 if (!IsReady()) {
228 throw RaylibException("Failed to load Wave from file data of type: " + std::string(fileType));
229 }
230 }
231
237 bool IsReady() const {
238 return ::IsWaveReady(*this);
239 }
240
241 protected:
242 void set(const ::Wave& wave) {
243 frameCount = wave.frameCount;
244 sampleRate = wave.sampleRate;
245 sampleSize = wave.sampleSize;
246 channels = wave.channels;
247 data = wave.data;
248 }
249};
250
251} // namespace raylib
252
253using RWave = raylib::Wave;
254
255#endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
Exception used for most raylib-related exceptions.
Wave type, defines audio wave data.
Definition: Wave.hpp:16
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:144
void Load(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:213
bool Export(const std::string_view fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:158
::Wave Copy() const
Copy a wave to a new wave.
Definition: Wave.hpp:121
bool ExportAsCode(const std::string_view fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:166
::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:185
~Wave()
Unload wave data.
Definition: Wave.hpp:75
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:128
Wave(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:45
void Unload()
Unload wave data.
Definition: Wave.hpp:174
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:136
void Load(const std::string_view fileName)
Load wave data from file.
Definition: Wave.hpp:201
operator::Sound()
Load sound from wave data.
Definition: Wave.hpp:192
Wave(const std::string_view fileType, std::span< const unsigned char > fileData)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:54
Wave(const std::string_view fileName)
Load wave data from file.
Definition: Wave.hpp:36
bool IsReady() const
Retrieve whether or not the Wave data has been loaded.
Definition: Wave.hpp:237
static void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:151
void Load(const std::string_view fileType, std::span< const unsigned char > fileData)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:225