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 
80  Wave& operator=(const Wave& wave) {
81  set(wave);
82  return *this;
83  }
84 
88  inline Wave& Format(int SampleRate, int SampleSize, int Channels = 2) {
89  ::WaveFormat(this, SampleRate, SampleSize, Channels);
90  return *this;
91  }
92 
96  inline ::Wave Copy() {
97  return ::WaveCopy(*this);
98  }
99 
103  inline Wave& Crop(int initSample, int finalSample) {
104  ::WaveCrop(this, initSample, finalSample);
105  return *this;
106  }
107 
111  inline float* LoadSamples() {
112  return ::LoadWaveSamples(*this);
113  }
114 
118  inline void UnloadSamples(float *samples) {
119  ::UnloadWaveSamples(samples);
120  }
121 
125  inline bool Export(const std::string& fileName) {
126  // TODO(RobLoach): Throw exception on error.
127  return ::ExportWave(*this, fileName.c_str());
128  }
129 
133  inline bool ExportAsCode(const std::string& fileName) {
134  // TODO(RobLoach): Throw exception on error.
135  return ::ExportWaveAsCode(*this, fileName.c_str());
136  }
137 
141  void Unload() {
142  if (data != NULL) {
143  ::UnloadWave(*this);
144  data = NULL;
145  }
146  }
147 
151  inline ::Sound LoadSound() {
152  return ::LoadSoundFromWave(*this);
153  }
154 
158  inline operator ::Sound() {
159  return LoadSound();
160  }
161 
165  inline operator Sound() {
166  return LoadSound();
167  }
168 
169  protected:
170  inline void set(const ::Wave& wave) {
171  sampleCount = wave.sampleCount;
172  sampleRate = wave.sampleRate;
173  sampleSize = wave.sampleSize;
174  channels = wave.channels;
175  data = wave.data;
176  }
177 };
178 } // namespace raylib
179 
180 #endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_
raylib::Wave::Copy
inline ::Wave Copy()
Copy a wave to a new wave.
Definition: Wave.hpp:96
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:103
raylib::Wave::operator=
Wave & operator=(const Wave &wave)
Definition: Wave.hpp:80
raylib::Wave::Unload
void Unload()
Unload wave data.
Definition: Wave.hpp:141
raylib::Wave::LoadSamples
float * LoadSamples()
Load samples data from wave as a floats array.
Definition: Wave.hpp:111
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:125
raylib::Wave::~Wave
~Wave()
Definition: Wave.hpp:65
raylib::Wave::set
void set(const ::Wave &wave)
Definition: Wave.hpp:170
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:133
raylib::Wave::UnloadSamples
void UnloadSamples(float *samples)
Unload samples data loaded with LoadWaveSamples()
Definition: Wave.hpp:118
raylib::Wave::Format
Wave & Format(int SampleRate, int SampleSize, int Channels=2)
Convert wave data to desired format.
Definition: Wave.hpp:88
raylib::Wave::LoadSound
inline ::Sound LoadSound()
Load sound from wave data.
Definition: Wave.hpp:151