raylib-cpp
C++ object-oriented wrapper library for raylib.
Wave.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_WAVE_HPP_
26 #define RAYLIB_CPP_INCLUDE_WAVE_HPP_
27 
28 #include <string>
29 
30 #include "./raylib.hpp"
31 #include "./raylib-cpp-utils.hpp"
32 
33 namespace raylib {
34 class Wave : public ::Wave {
35  public:
36  Wave(const ::Wave& wave) {
37  set(wave);
38  }
39 
41  unsigned int SampleCount = 0,
42  unsigned int SampleRate = 0,
43  unsigned int SampleSize = 0,
44  unsigned int Channels = 0) {
45  sampleCount = SampleCount;
46  sampleRate = SampleRate;
47  sampleSize = SampleSize;
48  channels = Channels;
49  }
50 
54  Wave(const std::string& fileName) {
55  set(::LoadWave(fileName.c_str()));
56  }
57 
61  Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) {
62  set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize));
63  }
64 
65  ~Wave() {
66  Unload();
67  }
68 
69  GETTERSETTER(unsigned int, SampleCount, sampleCount)
70  GETTERSETTER(unsigned int, SampleRate, sampleRate)
71  GETTERSETTER(unsigned int, SampleSize, sampleSize)
72  GETTERSETTER(unsigned int, Channels, channels)
73  GETTERSETTER(void *, Data, data)
74 
75  Wave& operator=(const ::Wave& wave) {
76  set(wave);
77  return *this;
78  }
79 
83  inline Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
84  ::WaveFormat(this, SampleRate, SampleSize, Channels);
85  return *this;
86  }
87 
91  inline ::Wave Copy() {
92  return ::WaveCopy(*this);
93  }
94 
98  inline Wave& Crop(int initSample, int finalSample) {
99  ::WaveCrop(this, initSample, finalSample);
100  return *this;
101  }
102 
106  inline float* LoadSamples() {
107  return ::LoadWaveSamples(*this);
108  }
109 
113  inline void UnloadSamples(float *samples) {
114  ::UnloadWaveSamples(samples);
115  }
116 
120  inline bool Export(const std::string& fileName) {
121  // TODO(RobLoach): Throw exception on error.
122  return ::ExportWave(*this, fileName.c_str());
123  }
124 
128  inline bool ExportAsCode(const std::string& fileName) {
129  // TODO(RobLoach): Throw exception on error.
130  return ::ExportWaveAsCode(*this, fileName.c_str());
131  }
132 
136  void Unload() {
137  if (data != NULL) {
138  ::UnloadWave(*this);
139  data = NULL;
140  }
141  }
142 
146  inline ::Sound LoadSound() {
147  return ::LoadSoundFromWave(*this);
148  }
149 
153  inline operator ::Sound() {
154  return LoadSound();
155  }
156 
160  inline operator Sound() {
161  return LoadSound();
162  }
163 
164  protected:
165  inline void set(const ::Wave& wave) {
166  sampleCount = wave.sampleCount;
167  sampleRate = wave.sampleRate;
168  sampleSize = wave.sampleSize;
169  channels = wave.channels;
170  data = wave.data;
171  }
172 };
173 } // namespace raylib
174 
175 #endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
raylib::Wave::Copy
inline ::Wave Copy()
Copy a wave to a new wave.
Definition: Wave.hpp:91
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Wave
Definition: Wave.hpp:34
raylib::Wave::Crop
Wave & Crop(int initSample, int finalSample)
Crop a wave to defined samples range.
Definition: Wave.hpp:98
raylib::Wave::Unload
void Unload()
Unload wave data.
Definition: Wave.hpp:136
raylib::Wave::LoadSamples
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:106
raylib::Sound
Wave/Sound management functions.
Definition: Sound.hpp:37
raylib::Wave::Wave
Wave(const ::Wave &wave)
Definition: Wave.hpp:36
raylib::Wave::Wave
Wave(unsigned int SampleCount=0, unsigned int SampleRate=0, unsigned int SampleSize=0, unsigned int Channels=0)
Definition: Wave.hpp:40
raylib::Wave::Export
bool Export(const std::string &fileName)
Export wave data to file, returns true on success.
Definition: Wave.hpp:120
raylib::Wave::~Wave
~Wave()
Definition: Wave.hpp:65
raylib::Wave::set
void set(const ::Wave &wave)
Definition: Wave.hpp:165
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:61
raylib::Wave::Wave
Wave(const std::string &fileName)
Load wave data from file.
Definition: Wave.hpp:54
raylib::Wave::ExportAsCode
bool ExportAsCode(const std::string &fileName)
Export wave sample data to code (.h), returns true on success.
Definition: Wave.hpp:128
raylib::Wave::UnloadSamples
void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:113
raylib::Wave::Format
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:83
raylib::Wave::LoadSound
inline ::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:146