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 
10 namespace raylib {
14 class Wave : public ::Wave {
15  public:
16  Wave(const ::Wave& wave) {
17  set(wave);
18  }
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) : ::Wave{frameCount, sampleRate, sampleSize, channels, data} {
26  // Nothing.
27  }
28 
34  Wave(const std::string& fileName) {
35  Load(fileName);
36  }
37 
43  Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) {
44  Load(fileType, fileData, dataSize);
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 
64  ~Wave() {
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 Copy() const {
111  return ::WaveCopy(*this);
112  }
113 
117  inline Wave& Crop(int initSample, int finalSample) {
118  ::WaveCrop(this, initSample, finalSample);
119  return *this;
120  }
121 
125  inline Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
126  ::WaveFormat(this, SampleRate, SampleSize, Channels);
127  return *this;
128  }
129 
133  inline float* LoadSamples() {
134  return ::LoadWaveSamples(*this);
135  }
136 
140  inline static 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  void Load(const std::string& fileName) {
190  set(::LoadWave(fileName.c_str()));
191  if (!IsReady()) {
192  throw RaylibException("Failed to load Wave from file: " + fileName);
193  }
194  }
195 
201  void Load(const std::string& fileType, const unsigned char *fileData, int dataSize) {
202  set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize));
203  if (!IsReady()) {
204  throw RaylibException("Failed to load Wave from file data of type: " + fileType);
205  }
206  }
207 
213  inline bool IsReady() const {
214  return data != nullptr;
215  }
216 
217  private:
218  void set(const ::Wave& wave) {
219  frameCount = wave.frameCount;
220  sampleRate = wave.sampleRate;
221  sampleSize = wave.sampleSize;
222  channels = wave.channels;
223  data = wave.data;
224  }
225 };
226 
227 } // namespace raylib
228 
229 using RWave = raylib::Wave;
230 
231 #endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
Exception used for most raylib-related exceptions.
Wave type, defines audio wave data.
Definition: Wave.hpp:14
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:117
inline ::Wave Copy() const
Copy a wave to a new wave.
Definition: Wave.hpp:110
void 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:201
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:43
bool ExportAsCode(const std::string &fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:155
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:125
~Wave()
Unload wave data.
Definition: Wave.hpp:64
void Unload()
Unload wave data.
Definition: Wave.hpp:163
inline ::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:173
bool IsReady() const
Retrieve whether or not the Wave data has been loaded.
Definition: Wave.hpp:213
bool Export(const std::string &fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:147
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:133
static void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:140
Wave(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:34
void Load(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:189
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8