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 "./RaylibException.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
16class Wave : public ::Wave {
17public:
18 Wave(const ::Wave& wave) : ::Wave(wave) { }
19
20 Wave(
21 unsigned int frameCount = 0,
22 unsigned int sampleRate = 0,
23 unsigned int sampleSize = 0,
24 unsigned int channels = 0,
25 void* data = nullptr)
26 : ::Wave{frameCount, sampleRate, sampleSize, channels, data} {
27 // Nothing.
28 }
29
35 Wave(const std::string_view fileName) {
36 Load(fileName);
37 }
38
44 Wave(const std::string_view fileType, const unsigned char *fileData, int dataSize) {
45 Load(fileType, fileData, dataSize);
46 }
47
53 Wave(const std::string_view fileType, std::span<const unsigned char> fileData) {
54 Load(fileType, fileData);
55 }
56
57 Wave(const Wave& other) {
58 set(other.Copy());
59 }
60
61 Wave(Wave&& other) noexcept {
62 set(other);
63
64 other.frameCount = 0;
65 other.sampleRate = 0;
66 other.sampleSize = 0;
67 other.channels = 0;
68 other.data = nullptr;
69 }
70
74 ~Wave() { Unload(); }
75
76 GETTER(unsigned int, FrameCount, frameCount)
77 GETTER(unsigned int, SampleRate, sampleRate)
78 GETTER(unsigned int, SampleSize, sampleSize)
79 GETTER(unsigned int, Channels, channels)
80 GETTER(void*, Data, data)
81
82 Wave& operator=(const ::Wave& wave) {
83 set(wave);
84 return *this;
85 }
86
87 Wave& operator=(const Wave& other) {
88 if (this == &other) {
89 return *this;
90 }
91
92 Unload();
93 set(other.Copy());
94
95 return *this;
96 }
97
98 Wave& operator=(Wave&& other) noexcept {
99 if (this != &other) {
100 return *this;
101 }
102
103 Unload();
104 set(other);
105
106 other.frameCount = 0;
107 other.sampleRate = 0;
108 other.sampleSize = 0;
109 other.channels = 0;
110 other.data = nullptr;
111
112 return *this;
113 }
114
118 [[nodiscard]] ::Wave Copy() const { return ::WaveCopy(*this); }
119
123 Wave& Crop(int initSample, int finalSample) {
124 ::WaveCrop(this, initSample, finalSample);
125 return *this;
126 }
127
131 Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
132 ::WaveFormat(this, SampleRate, SampleSize, Channels);
133 return *this;
134 }
135
139 float* LoadSamples() { return ::LoadWaveSamples(*this); }
140
144 static void UnloadSamples(float* samples) { ::UnloadWaveSamples(samples); }
145
149 bool Export(const std::string_view fileName) {
150 // TODO(RobLoach): Throw exception on error.
151 return ::ExportWave(*this, fileName.data());
152 }
153
157 bool ExportAsCode(const std::string_view fileName) {
158 // TODO(RobLoach): Throw exception on error.
159 return ::ExportWaveAsCode(*this, fileName.data());
160 }
161
165 void Unload() {
166 // Protect against calling UnloadWave() twice.
167 if (data != nullptr) {
168 ::UnloadWave(*this);
169 data = nullptr;
170 }
171 }
172
176 ::Sound LoadSound() { return ::LoadSoundFromWave(*this); }
177
181 operator ::Sound() { return LoadSound(); }
182
188 void Load(const std::string_view fileName) {
189 set(::LoadWave(fileName.data()));
190 if (!IsValid()) {
191 throw RaylibException("Failed to load Wave from file: " + std::string(fileName));
192 }
193 }
194
200 void Load(const std::string_view fileType, const unsigned char *fileData, int dataSize) {
201 set(::LoadWaveFromMemory(fileType.data(), fileData, dataSize));
202 if (!IsValid()) {
203 throw RaylibException("Failed to load Wave from file data of type: " + std::string(fileType));
204 }
205 }
206
212 void Load(const std::string_view fileType, std::span<const unsigned char> fileData) {
213 set(::LoadWaveFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size())));
214 if (!IsValid()) {
215 throw RaylibException("Failed to load Wave from file data of type: " + std::string(fileType));
216 }
217 }
218
224 [[nodiscard]] bool IsValid() const { return ::IsWaveValid(*this); }
225protected:
226 void set(const ::Wave& wave) {
227 frameCount = wave.frameCount;
228 sampleRate = wave.sampleRate;
229 sampleSize = wave.sampleSize;
230 channels = wave.channels;
231 data = wave.data;
232 }
233};
234
235} // namespace raylib
236
237using RWave = raylib::Wave;
238
239#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:139
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:200
bool Export(const std::string_view fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:149
::Wave Copy() const
Copy a wave to a new wave.
Definition: Wave.hpp:118
bool ExportAsCode(const std::string_view fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:157
::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:176
~Wave()
Unload wave data.
Definition: Wave.hpp:74
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:123
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:44
void Unload()
Unload wave data.
Definition: Wave.hpp:165
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:131
void Load(const std::string_view fileName)
Load wave data from file.
Definition: Wave.hpp:188
operator::Sound()
Load sound from wave data.
Definition: Wave.hpp:181
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:53
Wave(const std::string_view fileName)
Load wave data from file.
Definition: Wave.hpp:35
bool IsValid() const
Retrieve whether or not the Wave data has been loaded.
Definition: Wave.hpp:224
static void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:144
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:212
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8