raylib-cpp
C++ object-oriented wrapper library for raylib.
Wave.hpp
1#ifndef RAYLIB_CPP_INCLUDE_WAVE_HPP_
2#define RAYLIB_CPP_INCLUDE_WAVE_HPP_
3
4#include <string>
5
6#include "./raylib.hpp"
7#include "./raylib-cpp-utils.hpp"
8#include "./RaylibException.hpp"
9
10namespace raylib {
14class Wave : public ::Wave {
15 public:
16 Wave(const ::Wave& wave) {
17 set(wave);
18 }
19
21 unsigned int frameCount = 0,
22 unsigned int sampleRate = 0,
23 unsigned int sampleSize = 0,
24 unsigned int channels = 0,
25 void *data = nullptr) : ::Wave{frameCount, sampleRate, sampleSize, channels, data} {
26 // Nothing.
27 }
28
32 Wave(const std::string& fileName) {
33 if (!Load(fileName)) {
34 throw RaylibException(TextFormat("Failed to load Wave from file: %s", fileName.c_str()));
35 }
36 }
37
41 Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) {
42 if (!Load(fileType, fileData, dataSize)) {
43 throw RaylibException("Failed to load Wave from memory");
44 }
45 }
46
47 Wave(const Wave& other) {
48 set(other.Copy());
49 }
50
51 Wave(Wave&& other) {
52 set(other);
53
54 other.frameCount = 0;
55 other.sampleRate = 0;
56 other.sampleSize = 0;
57 other.channels = 0;
58 other.data = nullptr;
59 }
60
65 Unload();
66 }
67
68 GETTERSETTER(unsigned int, FrameCount, frameCount)
69 GETTERSETTER(unsigned int, SampleRate, sampleRate)
70 GETTERSETTER(unsigned int, SampleSize, sampleSize)
71 GETTERSETTER(unsigned int, Channels, channels)
72 GETTERSETTER(void *, Data, data)
73
74 Wave& operator=(const ::Wave& wave) {
75 set(wave);
76 return *this;
77 }
78
79 Wave& operator=(const Wave& other) {
80 if (this == &other) {
81 return *this;
82 }
83
84 Unload();
85 set(other.Copy());
86
87 return *this;
88 }
89
90 Wave& operator=(Wave&& other) noexcept {
91 if (this != &other) {
92 return *this;
93 }
94
95 Unload();
96 set(other);
97
98 other.frameCount = 0;
99 other.sampleRate = 0;
100 other.sampleSize = 0;
101 other.channels = 0;
102 other.data = nullptr;
103
104 return *this;
105 }
106
110 inline Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
111 ::WaveFormat(this, SampleRate, SampleSize, Channels);
112 return *this;
113 }
114
118 inline ::Wave Copy() const {
119 return ::WaveCopy(*this);
120 }
121
125 inline Wave& Crop(int initSample, int finalSample) {
126 ::WaveCrop(this, initSample, finalSample);
127 return *this;
128 }
129
133 inline float* LoadSamples() {
134 return ::LoadWaveSamples(*this);
135 }
136
140 inline void UnloadSamples(float *samples) {
141 ::UnloadWaveSamples(samples);
142 }
143
147 inline bool Export(const std::string& fileName) {
148 // TODO(RobLoach): Throw exception on error.
149 return ::ExportWave(*this, fileName.c_str());
150 }
151
155 inline bool ExportAsCode(const std::string& fileName) {
156 // TODO(RobLoach): Throw exception on error.
157 return ::ExportWaveAsCode(*this, fileName.c_str());
158 }
159
163 void Unload() {
164 if (data != nullptr) {
165 ::UnloadWave(*this);
166 data = nullptr;
167 }
168 }
169
173 inline ::Sound LoadSound() {
174 return ::LoadSoundFromWave(*this);
175 }
176
180 inline operator ::Sound() {
181 return LoadSound();
182 }
183
189 bool Load(const std::string& fileName) {
190 set(::LoadWave(fileName.c_str()));
191 return IsReady();
192 }
193
199 bool Load(const std::string& fileType, const unsigned char *fileData, int dataSize) {
200 set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize));
201 return IsReady();
202 }
203
209 inline bool IsReady() const {
210 return data != nullptr;
211 }
212
213 private:
214 inline void set(const ::Wave& wave) {
215 frameCount = wave.frameCount;
216 sampleRate = wave.sampleRate;
217 sampleSize = wave.sampleSize;
218 channels = wave.channels;
219 data = wave.data;
220 }
221};
222
223} // namespace raylib
224
225#endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
Exception used for most raylib-related exceptions.
Wave type, defines audio wave data.
Definition: Wave.hpp:14
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:133
bool Load(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:189
inline ::Wave Copy() const
Copy a wave to a new wave.
Definition: Wave.hpp:118
Wave(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:41
Wave(const Wave &other)
Definition: Wave.hpp:47
bool ExportAsCode(const std::string &fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:155
Wave(const ::Wave &wave)
Definition: Wave.hpp:16
~Wave()
Unload wave data.
Definition: Wave.hpp:64
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:125
Wave & operator=(Wave &&other) noexcept
Definition: Wave.hpp:90
void Unload()
Unload wave data.
Definition: Wave.hpp:163
inline ::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:173
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:110
bool IsReady() const
Retrieve whether or not the Wave data has been loaded.
Definition: Wave.hpp:209
Wave(unsigned int frameCount=0, unsigned int sampleRate=0, unsigned int sampleSize=0, unsigned int channels=0, void *data=nullptr)
Definition: Wave.hpp:20
bool Export(const std::string &fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:147
Wave & operator=(const Wave &other)
Definition: Wave.hpp:79
Wave(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:32
Wave(Wave &&other)
Definition: Wave.hpp:51
void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:140
bool Load(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load wave from memory buffer, fileType refers to extension: i.e.
Definition: Wave.hpp:199
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8