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 "./RaylibException.hpp"
5#include <span>
6
7namespace raylib {
11class AudioStream : public ::AudioStream {
12public:
13 AudioStream(const ::AudioStream& music)
14 : ::AudioStream(music) {
15 // Nothing.
16 }
17
19 rAudioBuffer* buffer = nullptr,
20 rAudioProcessor* processor = nullptr,
21 unsigned int sampleRate = 0,
22 unsigned int sampleSize = 0,
23 unsigned int channels = 0)
24 : ::AudioStream{buffer, processor, sampleRate, sampleSize, channels} {
25 // Nothing.
26 }
27
33 AudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels = 2) {
34 Load(sampleRate, sampleSize, channels);
35 }
36
37 AudioStream(const AudioStream&) = delete;
38
39 AudioStream(AudioStream&& other) noexcept {
40 set(other);
41
42 other.buffer = nullptr;
43 other.processor = nullptr;
44 other.sampleRate = 0;
45 other.sampleSize = 0;
46 other.channels = 0;
47 }
48
49 ~AudioStream() { Unload(); }
50
51 GETTER(rAudioBuffer*, Buffer, buffer)
52 GETTER(rAudioProcessor*, Processor, processor)
53 GETTER(unsigned int, SampleRate, sampleRate)
54 GETTER(unsigned int, SampleSize, sampleSize)
55 GETTER(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 AudioStream& Update(const void* data, int samplesCount) {
85 ::UpdateAudioStream(*this, data, samplesCount);
86 return *this;
87 }
88
92 AudioStream& Update(std::span<std::byte> data) {
93 ::UpdateAudioStream(*this, data.data(), static_cast<int>(data.size()));
94 return *this;
95 }
96
100 void Unload() {
101 if (IsValid()) {
102 ::UnloadAudioStream(*this);
103 }
104 }
105
109 [[nodiscard]] bool IsProcessed() const { return ::IsAudioStreamProcessed(*this); }
110
115 ::PlayAudioStream(*this);
116 return *this;
117 }
118
123 ::PauseAudioStream(*this);
124 return *this;
125 }
126
131 ::ResumeAudioStream(*this);
132 return *this;
133 }
134
138 [[nodiscard]] bool IsPlaying() const { return ::IsAudioStreamPlaying(*this); }
139
144 ::StopAudioStream(*this);
145 return *this;
146 }
147
151 AudioStream& SetVolume(float volume = 1.0f) {
152 ::SetAudioStreamVolume(*this, volume);
153 return *this;
154 }
155
159 AudioStream& SetPitch(float pitch) {
160 ::SetAudioStreamPitch(*this, pitch);
161 return *this;
162 }
163
167 AudioStream& SetPan(float pan = 0.5f) {
168 ::SetAudioStreamPan(*this, pan);
169 return *this;
170 }
171
175 static void SetBufferSizeDefault(int size) { ::SetAudioStreamBufferSizeDefault(size); }
176
180 void SetCallback(::AudioCallback callback) { ::SetAudioStreamCallback(*this, callback); }
181
185 void AttachProcessor(::AudioCallback processor) { ::AttachAudioStreamProcessor(*this, processor); }
186
190 void DetachProcessor(::AudioCallback processor) { ::DetachAudioStreamProcessor(*this, processor); }
191
195 [[nodiscard]] bool IsValid() const { return ::IsAudioStreamValid(*this); }
196
202 void Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) {
203 Unload();
204 set(::LoadAudioStream(SampleRate, SampleSize, Channels));
205 if (!IsValid()) {
206 throw RaylibException("Failed to load audio stream");
207 }
208 }
209protected:
210 void set(const ::AudioStream& stream) {
211 buffer = stream.buffer;
212 processor = stream.processor;
213 sampleRate = stream.sampleRate;
214 sampleSize = stream.sampleSize;
215 channels = stream.channels;
216 }
217};
218} // namespace raylib
219
221
222#endif // RAYLIB_CPP_INCLUDE_AUDIOSTREAM_HPP_
AudioStream management functions.
Definition: AudioStream.hpp:11
void SetCallback(::AudioCallback callback)
Audio thread callback to request new data.
AudioStream & Stop()
Stop audio stream.
bool IsValid() const
Retrieve whether or not the audio stream is ready.
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:33
AudioStream & Update(std::span< std::byte > data)
Update audio stream buffers with data.
Definition: AudioStream.hpp:92
AudioStream & Pause()
Pause audio stream.
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:84
AudioStream & SetPan(float pan=0.5f)
Set pan for audio stream (0.5 is centered)
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8