SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_audio.h
1#ifndef SDL3PP_AUDIO_H_
2#define SDL3PP_AUDIO_H_
3
4#include <SDL3/SDL_audio.h>
5#include "SDL3pp_iostream.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
127using AudioSpec = SDL_AudioSpec;
128
129// Forward decl
130struct AudioDeviceBase;
131
132// Forward decl
133struct AudioDeviceRef;
134
135// Forward decl
136struct AudioDevice;
137
138// Forward decl
139struct AudioStreamBase;
140
141// Forward decl
142struct AudioStreamRef;
143
144// Forward decl
145struct AudioStream;
146
154
155// Forward decl
156struct AudioStreamLock;
157
158#ifdef SDL3PP_DOC
159
168#define SDL_AUDIO_MASK_BITSIZE (0xFFu)
169
177#define SDL_AUDIO_MASK_FLOAT (1u << 8)
178
187#define SDL_AUDIO_MASK_BIG_ENDIAN (1u << 12)
188
196#define SDL_AUDIO_MASK_SIGNED (1u << 15)
197
198#endif // SDL3PP_DOC
199
215{
216 SDL_AudioFormat m_audioFormat;
217
218public:
224 constexpr AudioFormat(SDL_AudioFormat audioFormat = {})
225 : m_audioFormat(audioFormat)
226 {
227 }
228
252 constexpr AudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
253 : m_audioFormat(
254 SDL_AudioFormat(SDL_DEFINE_AUDIO_FORMAT(sign, bigendian, flt, size)))
255 {
256 }
257
261 constexpr bool operator==(const AudioFormat& other) const = default;
262
266 constexpr bool operator==(SDL_AudioFormat audioFormat) const
267 {
268 return operator==(AudioFormat(audioFormat));
269 }
270
276 constexpr operator SDL_AudioFormat() const { return m_audioFormat; }
277
283 constexpr explicit operator bool() const { return m_audioFormat != 0; }
284
296 constexpr Uint16 GetBitSize() { return SDL_AUDIO_BITSIZE(m_audioFormat); }
297
309 constexpr Uint16 GetByteSize() { return SDL_AUDIO_BYTESIZE(m_audioFormat); }
310
322 constexpr bool IsFloat() { return SDL_AUDIO_ISFLOAT(m_audioFormat); }
323
335 constexpr bool IsBigEndian() { return SDL_AUDIO_ISBIGENDIAN(m_audioFormat); }
336
348 constexpr bool IsLittleEndian()
349 {
350 return SDL_AUDIO_ISLITTLEENDIAN(m_audioFormat);
351 }
352
364 constexpr bool IsSigned() { return SDL_AUDIO_ISSIGNED(m_audioFormat); }
365
377 constexpr bool IsInt() { return SDL_AUDIO_ISINT(m_audioFormat); }
378
390 constexpr bool IsUnsigned() { return SDL_AUDIO_ISUNSIGNED(m_audioFormat); }
391
402 const char* GetName() const { return SDL_GetAudioFormatName(m_audioFormat); }
403
417 int GetSilenceValue() const
418 {
419 return SDL_GetSilenceValueForFormat(m_audioFormat);
420 }
421};
422
423// Unfortunate name clash with SDL_oldnames.h
424#undef AUDIO_U8
425#undef AUDIO_S8
426#undef AUDIO_S16
427#undef AUDIO_S32
428#undef AUDIO_F32
429
430constexpr SDL_AudioFormat AUDIO_UNKNOWN =
431 SDL_AUDIO_UNKNOWN;
432
433constexpr SDL_AudioFormat AUDIO_U8 = SDL_AUDIO_U8;
434
435constexpr SDL_AudioFormat AUDIO_S8 = SDL_AUDIO_S8;
436
437constexpr SDL_AudioFormat AUDIO_S16LE =
438 SDL_AUDIO_S16LE;
439
440constexpr SDL_AudioFormat AUDIO_S16BE =
441 SDL_AUDIO_S16BE;
442
443constexpr SDL_AudioFormat AUDIO_S32LE =
444 SDL_AUDIO_S32LE;
445
446constexpr SDL_AudioFormat AUDIO_S32BE =
447 SDL_AUDIO_S32BE;
448
449constexpr SDL_AudioFormat AUDIO_F32LE =
450 SDL_AUDIO_F32LE;
451
452constexpr SDL_AudioFormat AUDIO_F32BE =
453 SDL_AUDIO_F32BE;
454
455constexpr SDL_AudioFormat AUDIO_S16 = SDL_AUDIO_S16;
456
457constexpr SDL_AudioFormat AUDIO_S32 = SDL_AUDIO_S32;
458
459constexpr SDL_AudioFormat AUDIO_F32 = SDL_AUDIO_F32;
460
496using AudioPostmixCallback = SDL_AudioPostmixCallback;
497
532 std::function<void(const AudioSpec& spec, std::span<float> buffer)>;
533
546struct AudioDeviceBase : Resource<SDL_AudioDeviceID>
547{
548 using Resource::Resource;
549
626 : Resource(CheckError(SDL_OpenAudioDevice(devid.get(), spec)))
627 {
628 }
629
631 constexpr auto operator<=>(const AudioDeviceBase& other) const
632 {
633 return get() <=> other.get();
634 }
635
649 const char* GetName() const { return SDL_GetAudioDeviceName(get()); }
650
682 AudioSpec GetFormat(int* sample_frames = nullptr) const
683 {
684 AudioSpec spec;
685 CheckError(SDL_GetAudioDeviceFormat(get(), &spec, sample_frames));
686 return spec;
687 }
688
708 {
709 int count;
710 auto data = SDL_GetAudioDeviceChannelMap(get(), &count);
711 return OwnArray<int>{data, size_t(count)};
712 }
713
737 bool IsPhysical() const { return SDL_IsAudioDevicePhysical(get()); }
738
750 bool IsPlayback() const { return SDL_IsAudioDevicePlayback(get()); }
751
779 void Pause() { CheckError(SDL_PauseAudioDevice(get())); }
780
805 void Resume() { CheckError(SDL_ResumeAudioDevice(get())); }
806
826 bool Paused() const { return SDL_AudioDevicePaused(get()); }
827
848 float GetGain() const { return SDL_GetAudioDeviceGain(get()); }
849
881 void SetGain(float gain) { CheckError(SDL_SetAudioDeviceGain(get(), gain)); }
882
915 void BindAudioStreams(std::span<AudioStreamRef> streams);
916
934 void BindAudioStream(AudioStreamBase& stream);
935
985 void SetPostmixCallback(AudioPostmixCB callback);
986
1037 void SetPostmixCallback(AudioPostmixCallback callback, void* userdata)
1038 {
1039 CheckError(SDL_SetAudioPostmixCallback(get(), callback, userdata));
1040 }
1041};
1042
1052{
1054
1058 constexpr AudioDeviceRef(const AudioDeviceRef& other)
1059 : AudioDeviceBase(other.get())
1060 {
1061 }
1062
1067 : AudioDeviceBase(other.release())
1068 {
1069 }
1070
1074 constexpr ~AudioDeviceRef() = default;
1075
1080 {
1081 release(other.release());
1082 return *this;
1083 }
1084
1101 void reset(SDL_AudioDeviceID newResource = {})
1102 {
1104 SDL_CloseAudioDevice(release(newResource));
1105 }
1106};
1107
1117{
1119
1123 constexpr explicit AudioDevice(SDL_AudioDeviceID resource = {})
1124 : AudioDeviceRef(resource)
1125 {
1126 }
1127
1128 constexpr AudioDevice(const AudioDevice& other) = delete;
1129
1133 constexpr AudioDevice(AudioDevice&& other) = default;
1134
1139
1144 {
1145 reset(other.release());
1146 return *this;
1147 }
1148};
1149
1160 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
1161
1172 SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
1173
1187constexpr int AudioFrameSize(const AudioSpec& x)
1188{
1189 return SDL_AUDIO_FRAMESIZE(x);
1190}
1191
1232using AudioStreamCallback = SDL_AudioStreamCallback;
1233
1273using AudioStreamCB = std::function<
1274 void(AudioStreamRef stream, int additional_amount, int total_amount)>;
1275
1305struct AudioStreamBase : Resource<SDL_AudioStream*>
1306{
1307 using Resource::Resource;
1308
1330 : Resource(CheckError(SDL_CreateAudioStream(src_spec, dst_spec)))
1331 {
1332 }
1333
1393 OptionalRef<const AudioSpec> spec = std::nullopt,
1394 AudioStreamCallback callback = nullptr,
1395 void* userdata = nullptr)
1397 SDL_OpenAudioDeviceStream(devid.get(), spec, callback, userdata)))
1398 {
1399 }
1400
1457 AudioStreamCB callback)
1458 : AudioStreamBase(devid, std::move(spec))
1459 {
1460 if (devid.IsPlayback()) {
1461 SetGetCallback(std::move(callback));
1462 } else {
1463 SetPutCallback(std::move(callback));
1464 }
1465 }
1466
1478 {
1479 return CheckError(SDL_GetAudioStreamProperties(get()));
1480 }
1481
1496 {
1497 AudioSpec spec;
1498 GetFormat(&spec, nullptr);
1499 return spec;
1500 }
1501
1516 {
1517 AudioSpec spec;
1518 GetFormat(nullptr, &spec);
1519 return spec;
1520 }
1521
1536 void GetFormat(AudioSpec* src_spec, AudioSpec* dst_spec) const
1537 {
1538 CheckError(SDL_GetAudioStreamFormat(get(), src_spec, dst_spec));
1539 }
1540
1571 void SetInputFormat(const AudioSpec& spec) { SetFormat(spec, std::nullopt); }
1572
1603 void SetOutputFormat(const AudioSpec& spec) { SetFormat(std::nullopt, spec); }
1604
1640 {
1641 CheckError(SDL_SetAudioStreamFormat(get(), src_spec, dst_spec));
1642 }
1643
1657 float GetFrequencyRatio() const
1658 {
1659 return SDL_GetAudioStreamFrequencyRatio(get());
1660 }
1661
1686 void SetFrequencyRatio(float ratio)
1687 {
1688 CheckError(SDL_SetAudioStreamFrequencyRatio(get(), ratio));
1689 }
1690
1709 float GetGain() const { return SDL_GetAudioStreamGain(get()); }
1710
1732 void SetGain(float gain) { CheckError(SDL_SetAudioStreamGain(get(), gain)); }
1733
1754 {
1755 int count;
1756 auto data = SDL_GetAudioStreamInputChannelMap(get(), &count);
1757 if (!data) return {};
1758 return OwnArray<int>{data, size_t(count)};
1759 }
1760
1781 {
1782 int count;
1783 auto data = SDL_GetAudioStreamOutputChannelMap(get(), &count);
1784 if (!data) return {};
1785 return OwnArray<int>{data, size_t(count)};
1786 }
1787
1843 void SetInputChannelMap(std::span<int> chmap)
1844 {
1845 SDL_assert_paranoid(chmap.size() < MAX_SINT32);
1846 CheckError(
1847 SDL_SetAudioStreamInputChannelMap(get(), chmap.data(), chmap.size()));
1848 }
1849
1903 void SetOutputChannelMap(std::span<int> chmap)
1904 {
1905 SDL_assert_paranoid(chmap.size() < MAX_SINT32);
1906 CheckError(
1907 SDL_SetAudioStreamOutputChannelMap(get(), chmap.data(), chmap.size()));
1908 }
1909
1936 {
1937 SDL_assert_paranoid(buf.size_bytes < MAX_SINT32);
1938 CheckError(SDL_PutAudioStreamData(get(), buf.data, buf.size_bytes));
1939 }
1940
1968 {
1969 return SDL_GetAudioStreamData(get(), buf.data, buf.size_bytes);
1970 }
1971
1995 int GetAvailable() const { return SDL_GetAudioStreamAvailable(get()); }
1996
2032 int GetQueued() const { return SDL_GetAudioStreamQueued(get()); }
2033
2050 void Flush() { CheckError(SDL_FlushAudioStream(get())); }
2051
2069 void Clear() { CheckError(SDL_ClearAudioStream(get())); }
2070
2091 void PauseDevice() { CheckError(SDL_PauseAudioStreamDevice(get())); }
2092
2113 void ResumeDevice() { CheckError(SDL_ResumeAudioStreamDevice(get())); }
2114
2131 bool DevicePaused() const { return SDL_AudioStreamDevicePaused(get()); }
2132
2198 void SetGetCallback(AudioStreamCB callback);
2199
2242 void SetGetCallback(AudioStreamCallback callback, void* userdata)
2243 {
2244 CheckError(SDL_SetAudioStreamGetCallback(get(), callback, userdata));
2245 }
2246
2290 void SetPutCallback(AudioStreamCB callback);
2291
2337 void SetPutCallback(AudioStreamCallback callback, void* userdata)
2338 {
2339 CheckError(SDL_SetAudioStreamPutCallback(get(), callback, userdata));
2340 }
2341
2360 void Bind(AudioDeviceBase& devid) { devid.BindAudioStream(*this); }
2361
2376 void Unbind() { SDL_UnbindAudioStream(get()); }
2377
2395 AudioDeviceRef GetDevice() const { return SDL_GetAudioStreamDevice(get()); }
2396};
2397
2407{
2409
2413 constexpr AudioStreamRef(const AudioStreamRef& other)
2414 : AudioStreamBase(other.get())
2415 {
2416 }
2417
2422 : AudioStreamBase(other.release())
2423 {
2424 }
2425
2429 constexpr ~AudioStreamRef() = default;
2430
2435 {
2436 release(other.release());
2437 return *this;
2438 }
2439
2457 void reset(SDL_AudioStream* newResource = {})
2458 {
2461 SDL_DestroyAudioStream(release(newResource));
2462 }
2463};
2464
2474{
2476
2480 constexpr explicit AudioStream(SDL_AudioStream* resource = {})
2481 : AudioStreamRef(resource)
2482 {
2483 }
2484
2485 constexpr AudioStream(const AudioStream& other) = delete;
2486
2490 constexpr AudioStream(AudioStream&& other) = default;
2491
2496
2501 {
2502 reset(other.release());
2503 return *this;
2504 }
2505};
2506
2510struct AudioStreamLock : LockBase<AudioStreamRef>
2511{
2515 constexpr AudioStreamLock() = default;
2516
2521 : LockBase(other.release())
2522 {
2523 }
2524
2551 : LockBase(stream.get())
2552 {
2553 CheckError(SDL_LockAudioStream(stream.get()));
2554 }
2555
2560
2576 void Unlock() { CheckError(SDL_UnlockAudioStream(release().get())); }
2577
2581 void reset() { Unlock(); }
2582};
2583
2605inline int GetNumAudioDrivers() { return SDL_GetNumAudioDrivers(); }
2606
2629inline const char* GetAudioDriver(int index)
2630{
2631 return SDL_GetAudioDriver(index);
2632}
2633
2648inline const char* GetCurrentAudioDriver()
2649{
2650 return SDL_GetCurrentAudioDriver();
2651}
2652
2679{
2680 int count;
2681 auto data = CheckError(SDL_GetAudioPlaybackDevices(&count));
2682 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
2683 size_t(count)};
2684}
2685
2712{
2713 int count;
2714 auto data = CheckError(SDL_GetAudioRecordingDevices(&count));
2715 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
2716 size_t(count)};
2717}
2718
2719inline void AudioDeviceBase::BindAudioStreams(std::span<AudioStreamRef> streams)
2720{
2721 SDL_assert_paranoid(streams.size() < SDL_MAX_SINT32);
2722 CheckError(SDL_BindAudioStreams(
2723 get(),
2724 reinterpret_cast<SDL_AudioStream* const*>(streams.data()),
2725 streams.size()));
2726}
2727
2729{
2730 CheckError(SDL_BindAudioStream(get(), stream.get()));
2731}
2732
2751inline void UnbindAudioStreams(std::span<AudioStreamRef> streams)
2752{
2753 SDL_assert_paranoid(streams.size() < SDL_MAX_SINT32);
2754 SDL_UnbindAudioStreams(
2755 reinterpret_cast<SDL_AudioStream* const*>(streams.data()), streams.size());
2756}
2757
2759{
2760 return AudioStreamLock(*this);
2761}
2762
2830{
2831 Uint8* buf;
2832 Uint32 len;
2833 if (!SDL_LoadWAV_IO(src.get(), false, spec, &buf, &len)) return {};
2834 return OwnArray<Uint8>{buf, size_t(len)};
2835}
2836
2862{
2863 Uint8* buf;
2864 Uint32 len;
2865 if (!SDL_LoadWAV(path, spec, &buf, &len)) return {};
2866 return OwnArray<Uint8>{buf, size_t(len)};
2867}
2868
2901inline void MixAudio(Uint8* dst,
2902 SourceBytes src,
2903 AudioFormat format,
2904 float volume)
2905{
2906 SDL_assert_paranoid(src.size_bytes < SDL_MAX_SINT32);
2907 CheckError(SDL_MixAudio(
2908 dst, static_cast<const Uint8*>(src.data), format, src.size_bytes, volume));
2909}
2910
2943inline void MixAudio(TargetBytes dst,
2944 SourceBytes src,
2945 AudioFormat format,
2946 float volume)
2947{
2948 if (dst.size_bytes < src.size_bytes) src.size_bytes = dst.size_bytes;
2949 MixAudio(static_cast<Uint8*>(dst.data), src, format, volume);
2950}
2951
2976 SourceBytes src_data,
2977 const AudioSpec& dst_spec)
2978{
2979 SDL_assert_paranoid(src_data.size_bytes < SDL_MAX_SINT32);
2980 Uint8* buf;
2981 int len;
2982 CheckError(SDL_ConvertAudioSamples(&src_spec,
2983 static_cast<const Uint8*>(src_data.data),
2984 src_data.size_bytes,
2985 &dst_spec,
2986 &buf,
2987 &len));
2988 return OwnArray<Uint8>{buf, size_t(len)};
2989}
2990#pragma region impl
2992
2994{
2996
2997 auto cb = Wrapper::Wrap(get(), std::move(callback));
2998 if (!SDL_SetAudioPostmixCallback(
2999 get(),
3000 [](void* userdata, const AudioSpec* spec, float* buffer, int buflen) {
3001 Wrapper::Call(userdata, *spec, std::span{buffer, size_t(buflen)});
3002 },
3003 cb)) {
3004 Wrapper::release(get());
3005 throw Error{};
3006 }
3007}
3008
3010{
3012 if (!SDL_SetAudioStreamGetCallback(
3013 get(),
3014 [](void* userdata,
3015 SDL_AudioStream* stream,
3016 int additional_amount,
3017 int total_amount) {
3018 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3019 },
3020 Wrapper::Wrap(get(), std::move(callback)))) {
3021 Wrapper::release(get());
3022 throw Error{};
3023 }
3024}
3025
3027{
3029 if (!SDL_SetAudioStreamPutCallback(
3030 get(),
3031 [](void* userdata,
3032 SDL_AudioStream* stream,
3033 int additional_amount,
3034 int total_amount) {
3035 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3036 },
3037 Wrapper::Wrap(get(), std::move(callback)))) {
3038 Wrapper::release(get());
3039 throw Error{};
3040 }
3041}
3042
3043#pragma endregion impl
3044} // namespace SDL
3045
3046#endif /* SDL3PP_AUDIO_H_ */
Audio format.
Definition SDL3pp_audio.h:215
constexpr bool IsUnsigned()
Determine if an AudioFormat represents unsigned data.
Definition SDL3pp_audio.h:390
constexpr Uint16 GetByteSize()
Retrieve the size, in bytes, from an AudioFormat.
Definition SDL3pp_audio.h:309
constexpr bool IsInt()
Determine if an AudioFormat represents integer data.
Definition SDL3pp_audio.h:377
constexpr bool IsBigEndian()
Determine if an AudioFormat represents bigendian data.
Definition SDL3pp_audio.h:335
constexpr bool IsSigned()
Determine if an AudioFormat represents signed data.
Definition SDL3pp_audio.h:364
constexpr bool IsFloat()
Determine if an AudioFormat represents floating point data.
Definition SDL3pp_audio.h:322
constexpr bool IsLittleEndian()
Determine if an AudioFormat represents littleendian data.
Definition SDL3pp_audio.h:348
int GetSilenceValue() const
Get the appropriate memset value for silencing an audio format.
Definition SDL3pp_audio.h:417
constexpr Uint16 GetBitSize()
Retrieve the size, in bits, from an AudioFormat.
Definition SDL3pp_audio.h:296
constexpr AudioFormat(SDL_AudioFormat audioFormat={})
Wraps AudioFormat.
Definition SDL3pp_audio.h:224
constexpr bool operator==(SDL_AudioFormat audioFormat) const
Compares with the underlying type.
Definition SDL3pp_audio.h:266
constexpr bool operator==(const AudioFormat &other) const =default
Default comparison operator.
constexpr AudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
Define an AudioFormat value.
Definition SDL3pp_audio.h:252
const char * GetName() const
Get the human readable name of an audio format.
Definition SDL3pp_audio.h:402
An exception that returns GetError()
Definition SDL3pp_error.h:167
Base class for locks.
Definition SDL3pp_lockBase.h:19
AudioStreamRef release()
Release locked resource without unlocking it.
Definition SDL3pp_lockBase.h:54
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
A optional reference to resource.
Definition SDL3pp_resource.h:88
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_AudioDeviceID release(SDL_AudioDeviceID newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_AudioDeviceID get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:374
void BindAudioStream(AudioStreamBase &stream)
Bind a single audio stream to an audio device.
Definition SDL3pp_audio.h:2728
OwnArray< Uint8 > ConvertAudioSamples(const AudioSpec &src_spec, SourceBytes src_data, const AudioSpec &dst_spec)
Convert some audio data of one format to another format.
Definition SDL3pp_audio.h:2975
constexpr SDL_AudioFormat AUDIO_S16BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:440
SDL_AudioSpec AudioSpec
Format specifier for audio data.
Definition SDL3pp_audio.h:127
SDL_AudioPostmixCallback AudioPostmixCallback
A callback that fires when data is about to be fed to an audio device.
Definition SDL3pp_audio.h:496
constexpr AudioDeviceRef AUDIO_DEVICE_DEFAULT_PLAYBACK
A value used to request a default playback audio device.
Definition SDL3pp_audio.h:1159
const char * GetCurrentAudioDriver()
Get the name of the current audio driver.
Definition SDL3pp_audio.h:2648
void BindAudioStreams(std::span< AudioStreamRef > streams)
Bind a list of audio streams to an audio device.
Definition SDL3pp_audio.h:2719
constexpr SDL_AudioFormat AUDIO_S32LE
32-bit integer samples
Definition SDL3pp_audio.h:443
constexpr SDL_AudioFormat AUDIO_S32BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:446
constexpr AudioDeviceRef AUDIO_DEVICE_DEFAULT_RECORDING
A value used to request a default recording audio device.
Definition SDL3pp_audio.h:1171
constexpr SDL_AudioFormat AUDIO_S16LE
Signed 16-bit samples.
Definition SDL3pp_audio.h:437
std::function< void(AudioStreamRef stream, int additional_amount, int total_amount)> AudioStreamCB
A callback that fires when data passes through an AudioStreamBase.
Definition SDL3pp_audio.h:1274
OwnArray< Uint8 > LoadWAV(IOStreamBase &src, AudioSpec *spec)
Load the audio data of a WAVE file into memory.
Definition SDL3pp_audio.h:2829
OwnArray< AudioDeviceRef > GetAudioRecordingDevices()
Get a list of currently-connected audio recording devices.
Definition SDL3pp_audio.h:2711
const char * GetAudioDriver(int index)
Use this function to get the name of a built in audio driver.
Definition SDL3pp_audio.h:2629
constexpr SDL_AudioFormat AUDIO_S16
AUDIO_S16.
Definition SDL3pp_audio.h:455
std::function< void(const AudioSpec &spec, std::span< float > buffer)> AudioPostmixCB
A callback that fires when data is about to be fed to an audio device.
Definition SDL3pp_audio.h:532
void MixAudio(Uint8 *dst, SourceBytes src, AudioFormat format, float volume)
Mix audio data in a specified format.
Definition SDL3pp_audio.h:2901
constexpr SDL_AudioFormat AUDIO_F32
AUDIO_F32.
Definition SDL3pp_audio.h:459
constexpr SDL_AudioFormat AUDIO_F32LE
32-bit floating point samples
Definition SDL3pp_audio.h:449
int GetNumAudioDrivers()
Use this function to get the number of built-in audio drivers.
Definition SDL3pp_audio.h:2605
void UnbindAudioStreams(std::span< AudioStreamRef > streams)
Unbind a list of audio streams from their audio devices.
Definition SDL3pp_audio.h:2751
constexpr SDL_AudioFormat AUDIO_U8
Unsigned 8-bit samples.
Definition SDL3pp_audio.h:433
constexpr SDL_AudioFormat AUDIO_S32
AUDIO_S32.
Definition SDL3pp_audio.h:457
OwnArray< AudioDeviceRef > GetAudioPlaybackDevices()
Get a list of currently-connected audio playback devices.
Definition SDL3pp_audio.h:2678
constexpr SDL_AudioFormat AUDIO_S8
Signed 8-bit samples.
Definition SDL3pp_audio.h:435
AudioStreamLock Lock()
Lock an audio stream for serialized access.
Definition SDL3pp_audio.h:2758
SDL_AudioStreamCallback AudioStreamCallback
A callback that fires when data passes through an AudioStreamBase.
Definition SDL3pp_audio.h:1232
constexpr SDL_AudioFormat AUDIO_F32BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:452
constexpr int AudioFrameSize(const AudioSpec &x)
Calculate the size of each audio frame (in bytes) from an AudioSpec.
Definition SDL3pp_audio.h:1187
constexpr SDL_AudioFormat AUDIO_UNKNOWN
Unspecified audio format.
Definition SDL3pp_audio.h:430
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
SDL Audio Device instance IDs.
Definition SDL3pp_audio.h:547
constexpr auto operator<=>(const AudioDeviceBase &other) const
Comparison.
Definition SDL3pp_audio.h:631
void Pause()
Use this function to pause audio playback on a specified device.
Definition SDL3pp_audio.h:779
OwnArray< int > GetChannelMap() const
Get the current channel map of an audio device.
Definition SDL3pp_audio.h:707
const char * GetName() const
Get the human-readable name of a specific audio device.
Definition SDL3pp_audio.h:649
AudioDeviceBase(const AudioDeviceBase &devid, OptionalRef< const SDL_AudioSpec > spec)
Open a specific audio device.
Definition SDL3pp_audio.h:624
void SetGain(float gain)
Change the gain of an audio device.
Definition SDL3pp_audio.h:881
void SetPostmixCallback(AudioPostmixCB callback)
Set a callback that fires when data is about to be fed to an audio device.
Definition SDL3pp_audio.h:2993
bool Paused() const
Use this function to query if an audio device is paused.
Definition SDL3pp_audio.h:826
void Resume()
Use this function to unpause audio playback on a specified device.
Definition SDL3pp_audio.h:805
void SetPostmixCallback(AudioPostmixCallback callback, void *userdata)
Set a callback that fires when data is about to be fed to an audio device.
Definition SDL3pp_audio.h:1037
bool IsPlayback() const
Determine if an audio device is a playback device (instead of recording).
Definition SDL3pp_audio.h:750
bool IsPhysical() const
Determine if an audio device is physical (instead of logical).
Definition SDL3pp_audio.h:737
float GetGain() const
Get the gain of an audio device.
Definition SDL3pp_audio.h:848
AudioSpec GetFormat(int *sample_frames=nullptr) const
Get the current audio format of a specific audio device.
Definition SDL3pp_audio.h:682
Handle to a non owned audioDevice.
Definition SDL3pp_audio.h:1052
constexpr ~AudioDeviceRef()=default
Default constructor.
void reset(SDL_AudioDeviceID newResource={})
Close a previously-opened audio device.
Definition SDL3pp_audio.h:1101
constexpr AudioDeviceRef(const AudioDeviceRef &other)
Copy constructor.
Definition SDL3pp_audio.h:1058
AudioDeviceRef & operator=(AudioDeviceRef other)
Assignment operator.
Definition SDL3pp_audio.h:1079
constexpr AudioDeviceRef(AudioDeviceRef &&other)
Move constructor.
Definition SDL3pp_audio.h:1066
Handle to an owned audioDevice.
Definition SDL3pp_audio.h:1117
AudioDevice & operator=(AudioDevice other)
Assignment operator.
Definition SDL3pp_audio.h:1143
constexpr AudioDevice(AudioDevice &&other)=default
Move constructor.
~AudioDevice()
Frees up resource when object goes out of scope.
Definition SDL3pp_audio.h:1138
constexpr AudioDevice(SDL_AudioDeviceID resource={})
Constructs from the underlying resource.
Definition SDL3pp_audio.h:1123
The opaque handle that represents an audio stream.
Definition SDL3pp_audio.h:1306
void PutData(SourceBytes buf)
Add data to the stream.
Definition SDL3pp_audio.h:1935
AudioSpec GetOutputFormat() const
Query the current output format of an audio stream.
Definition SDL3pp_audio.h:1515
float GetGain() const
Get the gain of an audio stream.
Definition SDL3pp_audio.h:1709
int GetData(TargetBytes buf)
Get converted/resampled data from the stream.
Definition SDL3pp_audio.h:1967
void Clear()
Clear any pending data in the stream.
Definition SDL3pp_audio.h:2069
void SetOutputFormat(const AudioSpec &spec)
Change the output format of an audio stream.
Definition SDL3pp_audio.h:1603
AudioStreamBase(OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Create a new audio stream.
Definition SDL3pp_audio.h:1328
void SetInputChannelMap(std::span< int > chmap)
Set the current input channel map of an audio stream.
Definition SDL3pp_audio.h:1843
AudioDeviceRef GetDevice() const
Query an audio stream for its currently-bound device.
Definition SDL3pp_audio.h:2395
AudioStreamBase(const AudioDeviceBase &devid, OptionalRef< const AudioSpec > spec, AudioStreamCB callback)
Convenience function for straightforward audio init for the common case.
Definition SDL3pp_audio.h:1455
void SetFormat(OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Change the input and output formats of an audio stream.
Definition SDL3pp_audio.h:1638
int GetQueued() const
Get the number of bytes currently queued.
Definition SDL3pp_audio.h:2032
OwnArray< int > GetInputChannelMap() const
Get the current input channel map of an audio stream.
Definition SDL3pp_audio.h:1753
void SetGain(float gain)
Change the gain of an audio stream.
Definition SDL3pp_audio.h:1732
void GetFormat(AudioSpec *src_spec, AudioSpec *dst_spec) const
Query the current format of an audio stream.
Definition SDL3pp_audio.h:1536
void SetInputFormat(const AudioSpec &spec)
Change the input format of an audio stream.
Definition SDL3pp_audio.h:1571
void SetGetCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is requested from an audio stream.
Definition SDL3pp_audio.h:2242
void Flush()
Tell the stream that you're done sending data, and anything being buffered should be converted/resamp...
Definition SDL3pp_audio.h:2050
void SetPutCallback(AudioStreamCB callback)
Set a callback that runs when data is added to an audio stream.
Definition SDL3pp_audio.h:3026
OwnArray< int > GetOutputChannelMap() const
Get the current output channel map of an audio stream.
Definition SDL3pp_audio.h:1780
PropertiesRef GetProperties() const
Get the properties associated with an audio stream.
Definition SDL3pp_audio.h:1477
float GetFrequencyRatio() const
Get the frequency ratio of an audio stream.
Definition SDL3pp_audio.h:1657
void SetFrequencyRatio(float ratio)
Change the frequency ratio of an audio stream.
Definition SDL3pp_audio.h:1686
void SetPutCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is added to an audio stream.
Definition SDL3pp_audio.h:2337
void ResumeDevice()
Use this function to unpause audio playback on the audio device associated with an audio stream.
Definition SDL3pp_audio.h:2113
void SetOutputChannelMap(std::span< int > chmap)
Set the current output channel map of an audio stream.
Definition SDL3pp_audio.h:1903
void SetGetCallback(AudioStreamCB callback)
Set a callback that runs when data is requested from an audio stream.
Definition SDL3pp_audio.h:3009
void Unbind()
Unbind a single audio stream from its audio device.
Definition SDL3pp_audio.h:2376
bool DevicePaused() const
Use this function to query if an audio device associated with a stream is paused.
Definition SDL3pp_audio.h:2131
AudioStreamBase(const AudioDeviceBase &devid, OptionalRef< const AudioSpec > spec=std::nullopt, AudioStreamCallback callback=nullptr, void *userdata=nullptr)
Convenience function for straightforward audio init for the common case.
Definition SDL3pp_audio.h:1392
void Bind(AudioDeviceBase &devid)
Bind a single audio stream to an audio device.
Definition SDL3pp_audio.h:2360
AudioSpec GetInputFormat() const
Query the current input format of an audio stream.
Definition SDL3pp_audio.h:1495
int GetAvailable() const
Get the number of converted/resampled bytes available.
Definition SDL3pp_audio.h:1995
void PauseDevice()
Use this function to pause audio playback on the audio device associated with an audio stream.
Definition SDL3pp_audio.h:2091
Locks a AudioStream.
Definition SDL3pp_audio.h:2511
AudioStreamLock(AudioStreamBase &stream)
Lock an audio stream for serialized access.
Definition SDL3pp_audio.h:2550
void reset()
Same as Unlock(), just for uniformity.
Definition SDL3pp_audio.h:2581
~AudioStreamLock()
Destructor.
Definition SDL3pp_audio.h:2559
constexpr AudioStreamLock(AudioStreamLock &&other)
Move ctor.
Definition SDL3pp_audio.h:2520
void Unlock()
Unlock an audio stream for serialized access.
Definition SDL3pp_audio.h:2576
constexpr AudioStreamLock()=default
Creates an empty lock.
Handle to a non owned audioStream.
Definition SDL3pp_audio.h:2407
constexpr AudioStreamRef(const AudioStreamRef &other)
Copy constructor.
Definition SDL3pp_audio.h:2413
void reset(SDL_AudioStream *newResource={})
Free an audio stream.
Definition SDL3pp_audio.h:2457
AudioStreamRef & operator=(AudioStreamRef other)
Assignment operator.
Definition SDL3pp_audio.h:2434
constexpr ~AudioStreamRef()=default
Default constructor.
constexpr AudioStreamRef(AudioStreamRef &&other)
Move constructor.
Definition SDL3pp_audio.h:2421
Handle to an owned audioStream.
Definition SDL3pp_audio.h:2474
constexpr AudioStream(AudioStream &&other)=default
Move constructor.
~AudioStream()
Frees up resource when object goes out of scope.
Definition SDL3pp_audio.h:2495
AudioStream & operator=(AudioStream other)
Assignment operator.
Definition SDL3pp_audio.h:2500
constexpr AudioStream(SDL_AudioStream *resource={})
Constructs from the underlying resource.
Definition SDL3pp_audio.h:2480
The read/write operation structure.
Definition SDL3pp_iostream.h:107
Store callbacks by key.
Definition SDL3pp_callbackWrapper.h:222
static ValueType release(KeyType key)
Return unwrapped value associated by key and remove association.
Definition SDL3pp_callbackWrapper.h:183
Handle to a non owned properties.
Definition SDL3pp_properties.h:693
Source byte stream.
Definition SDL3pp_strings.h:239
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:241
const void * data
The data copied from.
Definition SDL3pp_strings.h:240
Target byte stream.
Definition SDL3pp_strings.h:305
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:307
void * data
The address to have data copied to it.
Definition SDL3pp_strings.h:306