raylib-cpp
C++ object-oriented wrapper library for raylib.
AudioStream.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_AUDIOSTREAM_HPP_
2 #define RAYLIB_CPP_INCLUDE_AUDIOSTREAM_HPP_
3 
4 #include "./raylib.hpp"
5 #include "./raylib-cpp-utils.hpp"
6 #include "./RaylibException.hpp"
7 
8 namespace raylib {
12 class AudioStream : public ::AudioStream {
13  public:
14  AudioStream(const ::AudioStream& music) {
15  set(music);
16  }
17 
18  AudioStream(rAudioBuffer* buffer = nullptr,
19  rAudioProcessor *processor = nullptr,
20  unsigned int sampleRate = 0,
21  unsigned int sampleSize = 0,
22  unsigned int channels = 0) : ::AudioStream{buffer, processor, sampleRate, sampleSize, channels} {
23  // Nothing.
24  }
25 
31  AudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels = 2) {
32  Load(sampleRate, sampleSize, channels);
33  }
34 
35  AudioStream(const AudioStream&) = delete;
36 
37  AudioStream(AudioStream&& other) {
38  set(other);
39 
40  other.buffer = nullptr;
41  other.processor = nullptr;
42  other.sampleRate = 0;
43  other.sampleSize = 0;
44  other.channels = 0;
45  }
46 
47  ~AudioStream() {
48  Unload();
49  }
50 
51  GETTERSETTER(rAudioBuffer *, Buffer, buffer)
52  GETTERSETTER(rAudioProcessor *, Processor, processor)
53  GETTERSETTER(unsigned int, SampleRate, sampleRate)
54  GETTERSETTER(unsigned int, SampleSize, sampleSize)
55  GETTERSETTER(unsigned int, Channels, channels)
56 
57  AudioStream& operator=(const ::AudioStream& stream) {
58  set(stream);
59  return *this;
60  }
61 
62  AudioStream& operator=(const AudioStream&) = delete;
63 
64  AudioStream& operator=(AudioStream&& other) noexcept {
65  if (this == &other) {
66  return *this;
67  }
68 
69  Unload();
70  set(other);
71 
72  other.buffer = nullptr;
73  other.processor = nullptr;
74  other.sampleRate = 0;
75  other.sampleSize = 0;
76  other.channels = 0;
77 
78  return *this;
79  }
80 
84  inline AudioStream& Update(const void *data, int samplesCount) {
85  ::UpdateAudioStream(*this, data, samplesCount);
86  return *this;
87  }
88 
92  inline void Unload() {
93  ::UnloadAudioStream(*this);
94  }
95 
99  inline bool IsProcessed() const {
100  return ::IsAudioStreamProcessed(*this);
101  }
102 
106  inline AudioStream& Play() {
107  ::PlayAudioStream(*this);
108  return *this;
109  }
110 
114  inline AudioStream& Pause() {
115  ::PauseAudioStream(*this);
116  return *this;
117  }
118 
122  inline AudioStream& Resume() {
123  ::ResumeAudioStream(*this);
124  return *this;
125  }
126 
130  inline bool IsPlaying() const {
131  return ::IsAudioStreamPlaying(*this);
132  }
133 
137  inline AudioStream& Stop() {
138  ::StopAudioStream(*this);
139  return *this;
140  }
141 
145  inline AudioStream& SetVolume(float volume = 1.0f) {
146  ::SetAudioStreamVolume(*this, volume);
147  return *this;
148  }
149 
153  inline AudioStream& SetPitch(float pitch) {
154  ::SetAudioStreamPitch(*this, pitch);
155  return *this;
156  }
157 
161  inline AudioStream& SetPan(float pan = 0.5f) {
162  ::SetAudioStreamPitch(*this, pan);
163  return *this;
164  }
165 
169  inline static void SetBufferSizeDefault(int size) {
170  ::SetAudioStreamBufferSizeDefault(size);
171  }
172 
176  inline void SetCallback(::AudioCallback callback) {
177  ::SetAudioStreamCallback(*this, callback);
178  }
179 
183  inline void AttachProcessor(::AudioCallback processor) {
184  ::SetAudioStreamCallback(*this, processor);
185  }
186 
190  inline void DetachProcessor(::AudioCallback processor) {
191  ::SetAudioStreamCallback(*this, processor);
192  }
193 
197  bool IsReady() {
198  return channels > 0;
199  }
200 
206  void Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) {
207  Unload();
208  set(::LoadAudioStream(SampleRate, SampleSize, Channels));
209  if (!IsReady()) {
210  throw RaylibException("Failed to load audio stream");
211  }
212  }
213 
214  private:
215  void set(const ::AudioStream& stream) {
216  buffer = stream.buffer;
217  processor = stream.processor;
218  sampleRate = stream.sampleRate;
219  sampleSize = stream.sampleSize;
220  channels = stream.channels;
221  }
222 };
223 } // namespace raylib
224 
226 
227 #endif // RAYLIB_CPP_INCLUDE_AUDIOSTREAM_HPP_
AudioStream management functions.
Definition: AudioStream.hpp:12
void SetCallback(::AudioCallback callback)
Audio thread callback to request new data.
bool IsProcessed() const
Check if any audio stream buffers requires refill.
Definition: AudioStream.hpp:99
AudioStream & Stop()
Stop audio stream.
AudioStream & SetPitch(float pitch)
Set pitch for audio stream (1.0 is base level)
bool IsPlaying() const
Check if audio stream is playing.
void AttachProcessor(::AudioCallback processor)
Attach audio stream processor to stream.
AudioStream & SetVolume(float volume=1.0f)
Set volume for audio stream (1.0 is max level)
AudioStream & Play()
Play audio stream.
void Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels=2)
Load audio stream (to stream raw audio pcm data)
void Unload()
Unload audio stream and free memory.
Definition: AudioStream.hpp:92
static void SetBufferSizeDefault(int size)
Default size for new audio streams.
AudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels=2)
Init audio stream (to stream raw audio pcm data)
Definition: AudioStream.hpp:31
AudioStream & Pause()
Pause audio stream.
AudioStream & Resume()
Resume audio stream.
AudioStream & SetPan(float pan=0.5f)
Set pan for audio stream (0.5 is centered)
AudioStream & Update(const void *data, int samplesCount)
Update audio stream buffers with data.
Definition: AudioStream.hpp:84
bool IsReady()
Retrieve whether or not the audio stream is ready.
void DetachProcessor(::AudioCallback processor)
Detach audio stream processor from stream.
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8