raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
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#include <span>
8
9namespace raylib {
13class AudioStream : public ::AudioStream {
14 public:
15 AudioStream(const ::AudioStream& music) {
16 set(music);
17 }
18
19 AudioStream(rAudioBuffer* buffer = nullptr,
20 rAudioProcessor *processor = nullptr,
21 unsigned int sampleRate = 0,
22 unsigned int sampleSize = 0,
23 unsigned int channels = 0) : ::AudioStream{buffer, processor, sampleRate, sampleSize, channels} {
24 // Nothing.
25 }
26
32 AudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels = 2) {
33 Load(sampleRate, sampleSize, channels);
34 }
35
36 AudioStream(const AudioStream&) = delete;
37
38 AudioStream(AudioStream&& other) {
39 set(other);
40
41 other.buffer = nullptr;
42 other.processor = nullptr;
43 other.sampleRate = 0;
44 other.sampleSize = 0;
45 other.channels = 0;
46 }
47
48 ~AudioStream() {
49 Unload();
50 }
51
52 GETTER(rAudioBuffer *, Buffer, buffer)
53 GETTER(rAudioProcessor *, Processor, processor)
54 GETTER(unsigned int, SampleRate, sampleRate)
55 GETTER(unsigned int, SampleSize, sampleSize)
56 GETTER(unsigned int, Channels, channels)
57
58 AudioStream& operator=(const ::AudioStream& stream) {
59 set(stream);
60 return *this;
61 }
62
63 AudioStream& operator=(const AudioStream&) = delete;
64
65 AudioStream& operator=(AudioStream&& other) noexcept {
66 if (this == &other) {
67 return *this;
68 }
69
70 Unload();
71 set(other);
72
73 other.buffer = nullptr;
74 other.processor = nullptr;
75 other.sampleRate = 0;
76 other.sampleSize = 0;
77 other.channels = 0;
78
79 return *this;
80 }
81
85 AudioStream& Update(const void *data, int samplesCount) {
86 ::UpdateAudioStream(*this, data, samplesCount);
87 return *this;
88 }
89
93 AudioStream& Update(std::span<std::byte> data) {
94 ::UpdateAudioStream(*this, data.data(), static_cast<int>(data.size()));
95 return *this;
96 }
97
101 void Unload() {
102 if (IsReady()) {
103 ::UnloadAudioStream(*this);
104 }
105 }
106
110 bool IsProcessed() const {
111 return ::IsAudioStreamProcessed(*this);
112 }
113
118 ::PlayAudioStream(*this);
119 return *this;
120 }
121
126 ::PauseAudioStream(*this);
127 return *this;
128 }
129
134 ::ResumeAudioStream(*this);
135 return *this;
136 }
137
141 bool IsPlaying() const {
142 return ::IsAudioStreamPlaying(*this);
143 }
144
149 ::StopAudioStream(*this);
150 return *this;
151 }
152
156 AudioStream& SetVolume(float volume = 1.0f) {
157 ::SetAudioStreamVolume(*this, volume);
158 return *this;
159 }
160
164 AudioStream& SetPitch(float pitch) {
165 ::SetAudioStreamPitch(*this, pitch);
166 return *this;
167 }
168
172 AudioStream& SetPan(float pan = 0.5f) {
173 ::SetAudioStreamPan(*this, pan);
174 return *this;
175 }
176
180 static void SetBufferSizeDefault(int size) {
181 ::SetAudioStreamBufferSizeDefault(size);
182 }
183
187 void SetCallback(::AudioCallback callback) {
188 ::SetAudioStreamCallback(*this, callback);
189 }
190
194 void AttachProcessor(::AudioCallback processor) {
195 ::AttachAudioStreamProcessor(*this, processor);
196 }
197
201 void DetachProcessor(::AudioCallback processor) {
202 ::DetachAudioStreamProcessor(*this, processor);
203 }
204
208 bool IsReady() const {
209 return ::IsAudioStreamReady(*this);
210 }
211
217 void Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) {
218 Unload();
219 set(::LoadAudioStream(SampleRate, SampleSize, Channels));
220 if (!IsReady()) {
221 throw RaylibException("Failed to load audio stream");
222 }
223 }
224
225 protected:
226 void set(const ::AudioStream& stream) {
227 buffer = stream.buffer;
228 processor = stream.processor;
229 sampleRate = stream.sampleRate;
230 sampleSize = stream.sampleSize;
231 channels = stream.channels;
232 }
233};
234} // namespace raylib
235
237
238#endif // RAYLIB_CPP_INCLUDE_AUDIOSTREAM_HPP_
AudioStream management functions.
Definition: AudioStream.hpp:13
void SetCallback(::AudioCallback callback)
Audio thread callback to request new data.
AudioStream & Stop()
Stop audio stream.
AudioStream & SetPitch(float pitch)
Set pitch for audio stream (1.0 is base level)
AudioStream & SetVolume(float volume=1.0f)
Set volume for audio stream (1.0 is max level)
bool IsProcessed() const
Check if any audio stream buffers requires refill.
AudioStream & Play()
Play audio stream.
bool IsPlaying() const
Check if audio stream is playing.
void AttachProcessor(::AudioCallback processor)
Attach audio stream processor to 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.
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:32
AudioStream & Update(std::span< std::byte > data)
Update audio stream buffers with data.
Definition: AudioStream.hpp:93
AudioStream & Pause()
Pause audio stream.
bool IsReady() const
Retrieve whether or not the audio stream is ready.
void DetachProcessor(::AudioCallback processor)
Detach audio stream processor from stream.
AudioStream & Resume()
Resume audio stream.
AudioStream & Update(const void *data, int samplesCount)
Update audio stream buffers with data.
Definition: AudioStream.hpp:85
AudioStream & SetPan(float pan=0.5f)
Set pan for audio stream (0.5 is centered)
Exception used for most raylib-related exceptions.