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 
9 namespace raylib {
13 class Wave : public ::Wave {
14  public:
15  Wave(const ::Wave& wave) {
16  set(wave);
17  }
18 
19  Wave(
20  unsigned int frameCount = 0,
21  unsigned int sampleRate = 0,
22  unsigned int sampleSize = 0,
23  unsigned int channels = 0,
24  void *data = nullptr) : ::Wave{frameCount, sampleRate, sampleSize, channels, data} { }
25 
29  Wave(const std::string& fileName) {
30  set(::LoadWave(fileName.c_str()));
31  }
32 
36  Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) {
37  set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize));
38  }
39 
40  Wave(const Wave& other) {
41  set(other.Copy());
42  }
43 
44  Wave(Wave&& other) {
45  set(other);
46 
47  other.frameCount = 0;
48  other.sampleRate = 0;
49  other.sampleSize = 0;
50  other.channels = 0;
51  other.data = nullptr;
52  }
53 
57  ~Wave() {
58  Unload();
59  }
60 
61  GETTERSETTER(unsigned int, FrameCount, frameCount)
62  GETTERSETTER(unsigned int, SampleRate, sampleRate)
63  GETTERSETTER(unsigned int, SampleSize, sampleSize)
64  GETTERSETTER(unsigned int, Channels, channels)
65  GETTERSETTER(void *, Data, data)
66 
67  Wave& operator=(const ::Wave& wave) {
68  set(wave);
69  return *this;
70  }
71 
72  Wave& operator=(const Wave& other) {
73  if (&other != this) {
74  return *this;
75  }
76 
77  Unload();
78  set(other.Copy());
79 
80  return *this;
81  }
82 
83  Wave& operator=(Wave&& other) {
84  if (this != &other) {
85  return *this;
86  }
87 
88  Unload();
89  set(other);
90 
91  other.frameCount = 0;
92  other.sampleRate = 0;
93  other.sampleSize = 0;
94  other.channels = 0;
95  other.data = nullptr;
96 
97  return *this;
98  }
99 
103  inline Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
104  ::WaveFormat(this, SampleRate, SampleSize, Channels);
105  return *this;
106  }
107 
111  inline ::Wave Copy() const {
112  return ::WaveCopy(*this);
113  }
114 
118  inline Wave& Crop(int initSample, int finalSample) {
119  ::WaveCrop(this, initSample, finalSample);
120  return *this;
121  }
122 
126  inline float* LoadSamples() {
127  return ::LoadWaveSamples(*this);
128  }
129 
133  inline void UnloadSamples(float *samples) {
134  ::UnloadWaveSamples(samples);
135  }
136 
140  inline bool Export(const std::string& fileName) {
141  // TODO(RobLoach): Throw exception on error.
142  return ::ExportWave(*this, fileName.c_str());
143  }
144 
148  inline bool ExportAsCode(const std::string& fileName) {
149  // TODO(RobLoach): Throw exception on error.
150  return ::ExportWaveAsCode(*this, fileName.c_str());
151  }
152 
156  void Unload() {
157  if (data != nullptr) {
158  ::UnloadWave(*this);
159  data = nullptr;
160  }
161  }
162 
166  inline ::Sound LoadSound() {
167  return ::LoadSoundFromWave(*this);
168  }
169 
173  inline operator ::Sound() {
174  return LoadSound();
175  }
176 
177  private:
178  inline void set(const ::Wave& wave) {
179  frameCount = wave.frameCount;
180  sampleRate = wave.sampleRate;
181  sampleSize = wave.sampleSize;
182  channels = wave.channels;
183  data = wave.data;
184  }
185 };
186 } // namespace raylib
187 
188 #endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Wave
Wave type, defines audio wave data.
Definition: Wave.hpp:13
raylib::Wave::Crop
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:118
raylib::Wave::Unload
void Unload()
Unload wave data.
Definition: Wave.hpp:156
raylib::Wave::LoadSamples
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:126
raylib::Wave::Export
bool Export(const std::string &fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:140
raylib::Wave::~Wave
~Wave()
Unload wave data.
Definition: Wave.hpp:57
raylib::Wave::Wave
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:36
raylib::Wave::Copy
inline ::Wave Copy() const
Copy a wave to a new wave.
Definition: Wave.hpp:111
raylib::Wave::Wave
Wave(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:29
raylib::Wave::ExportAsCode
bool ExportAsCode(const std::string &fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:148
raylib::Wave::UnloadSamples
void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:133
raylib::Wave::Format
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:103
raylib::Wave::LoadSound
inline ::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:166