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 AudioDeviceRef;
131
132// Forward decl
133struct AudioDevice;
134
144
154
155// Forward decl
156struct AudioStreamRef;
157
158// Forward decl
159struct AudioStream;
160
170
180
181// Forward decl
182struct AudioStreamLock;
183
184#ifdef SDL3PP_DOC
185
194#define SDL_AUDIO_MASK_BITSIZE (0xFFu)
195
203#define SDL_AUDIO_MASK_FLOAT (1u << 8)
204
213#define SDL_AUDIO_MASK_BIG_ENDIAN (1u << 12)
214
222#define SDL_AUDIO_MASK_SIGNED (1u << 15)
223
224#endif // SDL3PP_DOC
225
241{
242 SDL_AudioFormat m_audioFormat;
243
244public:
250 constexpr AudioFormat(SDL_AudioFormat audioFormat = {})
251 : m_audioFormat(audioFormat)
252 {
253 }
254
278 constexpr AudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
279 : m_audioFormat(
280 SDL_AudioFormat(SDL_DEFINE_AUDIO_FORMAT(sign, bigendian, flt, size)))
281 {
282 }
283
287 constexpr bool operator==(const AudioFormat& other) const = default;
288
292 constexpr bool operator==(SDL_AudioFormat audioFormat) const
293 {
294 return operator==(AudioFormat(audioFormat));
295 }
296
302 constexpr operator SDL_AudioFormat() const { return m_audioFormat; }
303
309 constexpr explicit operator bool() const { return m_audioFormat != 0; }
310
322 constexpr Uint16 GetBitSize() { return SDL_AUDIO_BITSIZE(m_audioFormat); }
323
335 constexpr Uint16 GetByteSize() { return SDL_AUDIO_BYTESIZE(m_audioFormat); }
336
348 constexpr bool IsFloat() { return SDL_AUDIO_ISFLOAT(m_audioFormat); }
349
361 constexpr bool IsBigEndian() { return SDL_AUDIO_ISBIGENDIAN(m_audioFormat); }
362
374 constexpr bool IsLittleEndian()
375 {
376 return SDL_AUDIO_ISLITTLEENDIAN(m_audioFormat);
377 }
378
390 constexpr bool IsSigned() { return SDL_AUDIO_ISSIGNED(m_audioFormat); }
391
403 constexpr bool IsInt() { return SDL_AUDIO_ISINT(m_audioFormat); }
404
416 constexpr bool IsUnsigned() { return SDL_AUDIO_ISUNSIGNED(m_audioFormat); }
417
428 const char* GetName() const { return SDL_GetAudioFormatName(m_audioFormat); }
429
443 int GetSilenceValue() const
444 {
445 return SDL_GetSilenceValueForFormat(m_audioFormat);
446 }
447};
448
449// Unfortunate name clash with SDL_oldnames.h
450#undef AUDIO_U8
451#undef AUDIO_S8
452#undef AUDIO_S16
453#undef AUDIO_S32
454#undef AUDIO_F32
455
456constexpr SDL_AudioFormat AUDIO_UNKNOWN =
457 SDL_AUDIO_UNKNOWN;
458
459constexpr SDL_AudioFormat AUDIO_U8 = SDL_AUDIO_U8;
460
461constexpr SDL_AudioFormat AUDIO_S8 = SDL_AUDIO_S8;
462
463constexpr SDL_AudioFormat AUDIO_S16LE =
464 SDL_AUDIO_S16LE;
465
466constexpr SDL_AudioFormat AUDIO_S16BE =
467 SDL_AUDIO_S16BE;
468
469constexpr SDL_AudioFormat AUDIO_S32LE =
470 SDL_AUDIO_S32LE;
471
472constexpr SDL_AudioFormat AUDIO_S32BE =
473 SDL_AUDIO_S32BE;
474
475constexpr SDL_AudioFormat AUDIO_F32LE =
476 SDL_AUDIO_F32LE;
477
478constexpr SDL_AudioFormat AUDIO_F32BE =
479 SDL_AUDIO_F32BE;
480
481constexpr SDL_AudioFormat AUDIO_S16 = SDL_AUDIO_S16;
482
483constexpr SDL_AudioFormat AUDIO_S32 = SDL_AUDIO_S32;
484
485constexpr SDL_AudioFormat AUDIO_F32 = SDL_AUDIO_F32;
486
522using AudioPostmixCallback = SDL_AudioPostmixCallback;
523
558 std::function<void(const AudioSpec& spec, std::span<float> buffer)>;
559
571struct AudioDeviceRef : Resource<SDL_AudioDeviceID>
572{
573 using Resource::Resource;
574
576 constexpr auto operator<=>(AudioDeviceRef other) const
577 {
578 return get() <=> other.get();
579 }
580
594 const char* GetName() const { return SDL_GetAudioDeviceName(get()); }
595
627 AudioSpec GetFormat(int* sample_frames = nullptr) const
628 {
629 AudioSpec spec;
630 CheckError(SDL_GetAudioDeviceFormat(get(), &spec, sample_frames));
631 return spec;
632 }
633
653 {
654 int count;
655 auto data = SDL_GetAudioDeviceChannelMap(get(), &count);
656 return OwnArray<int>{data, size_t(count)};
657 }
658
682 bool IsPhysical() const { return SDL_IsAudioDevicePhysical(get()); }
683
695 bool IsPlayback() const { return SDL_IsAudioDevicePlayback(get()); }
696
724 void Pause() { CheckError(SDL_PauseAudioDevice(get())); }
725
750 void Resume() { CheckError(SDL_ResumeAudioDevice(get())); }
751
771 bool Paused() const { return SDL_AudioDevicePaused(get()); }
772
793 float GetGain() const { return SDL_GetAudioDeviceGain(get()); }
794
826 void SetGain(float gain) { CheckError(SDL_SetAudioDeviceGain(get(), gain)); }
827
860 void BindAudioStreams(std::span<AudioStreamRef> streams);
861
879 void BindAudioStream(AudioStreamRef stream);
880
930 void SetPostmixCallback(AudioPostmixCB callback);
931
982 void SetPostmixCallback(AudioPostmixCallback callback, void* userdata)
983 {
984 CheckError(SDL_SetAudioPostmixCallback(get(), callback, userdata));
985 }
986
1006 static void reset(SDL_AudioDeviceID resource)
1007 {
1009 SDL_CloseAudioDevice(resource);
1010 }
1011};
1012
1020struct AudioDevice : ResourceUnique<AudioDeviceRef>
1021{
1023
1099 {
1100 return AudioDevice(CheckError(SDL_OpenAudioDevice(devid, spec)));
1101 }
1102
1119 void Close() { reset(); }
1124};
1125
1127{
1128 return AudioDeviceShared(std::move(*this));
1129}
1130
1140struct AudioDeviceUnsafe : ResourceUnsafe<AudioDeviceRef>
1141{
1143
1147 constexpr explicit AudioDeviceUnsafe(AudioDevice&& other)
1148 : AudioDeviceUnsafe(other.release())
1149 {
1150 }
1151};
1152
1163 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
1164
1175 SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
1176
1190constexpr int AudioFrameSize(const AudioSpec& x)
1191{
1192 return SDL_AUDIO_FRAMESIZE(x);
1193}
1194
1235using AudioStreamCallback = SDL_AudioStreamCallback;
1236
1276using AudioStreamCB = std::function<
1277 void(AudioStreamRef stream, int additional_amount, int total_amount)>;
1278
1306struct AudioStreamRef : Resource<SDL_AudioStream*>
1307{
1308 using Resource::Resource;
1309
1321 {
1322 return CheckError(SDL_GetAudioStreamProperties(get()));
1323 }
1324
1339 {
1340 AudioSpec spec;
1341 GetFormat(&spec, nullptr);
1342 return spec;
1343 }
1344
1359 {
1360 AudioSpec spec;
1361 GetFormat(nullptr, &spec);
1362 return spec;
1363 }
1364
1379 void GetFormat(AudioSpec* src_spec, AudioSpec* dst_spec) const
1380 {
1381 CheckError(SDL_GetAudioStreamFormat(get(), src_spec, dst_spec));
1382 }
1383
1414 void SetInputFormat(const AudioSpec& spec) { SetFormat(spec, std::nullopt); }
1415
1446 void SetOutputFormat(const AudioSpec& spec) { SetFormat(std::nullopt, spec); }
1447
1483 {
1484 CheckError(SDL_SetAudioStreamFormat(get(), src_spec, dst_spec));
1485 }
1486
1500 float GetFrequencyRatio() const
1501 {
1502 return SDL_GetAudioStreamFrequencyRatio(get());
1503 }
1504
1529 void SetFrequencyRatio(float ratio)
1530 {
1531 CheckError(SDL_SetAudioStreamFrequencyRatio(get(), ratio));
1532 }
1533
1552 float GetGain() const { return SDL_GetAudioStreamGain(get()); }
1553
1575 void SetGain(float gain) { CheckError(SDL_SetAudioStreamGain(get(), gain)); }
1576
1597 {
1598 int count;
1599 auto data = SDL_GetAudioStreamInputChannelMap(get(), &count);
1600 if (!data) return {};
1601 return OwnArray<int>{data, size_t(count)};
1602 }
1603
1624 {
1625 int count;
1626 auto data = SDL_GetAudioStreamOutputChannelMap(get(), &count);
1627 if (!data) return {};
1628 return OwnArray<int>{data, size_t(count)};
1629 }
1630
1686 void SetInputChannelMap(std::span<int> chmap)
1687 {
1688 SDL_assert_paranoid(chmap.size() < MAX_SINT32);
1689 CheckError(
1690 SDL_SetAudioStreamInputChannelMap(get(), chmap.data(), chmap.size()));
1691 }
1692
1746 void SetOutputChannelMap(std::span<int> chmap)
1747 {
1748 SDL_assert_paranoid(chmap.size() < MAX_SINT32);
1749 CheckError(
1750 SDL_SetAudioStreamOutputChannelMap(get(), chmap.data(), chmap.size()));
1751 }
1752
1779 {
1780 SDL_assert_paranoid(buf.size_bytes < MAX_SINT32);
1781 CheckError(SDL_PutAudioStreamData(get(), buf.data, buf.size_bytes));
1782 }
1783
1811 {
1812 return SDL_GetAudioStreamData(get(), buf.data, buf.size_bytes);
1813 }
1814
1838 int GetAvailable() const { return SDL_GetAudioStreamAvailable(get()); }
1839
1875 int GetQueued() const { return SDL_GetAudioStreamQueued(get()); }
1876
1893 void Flush() { CheckError(SDL_FlushAudioStream(get())); }
1894
1912 void Clear() { CheckError(SDL_ClearAudioStream(get())); }
1913
1934 void PauseDevice() { CheckError(SDL_PauseAudioStreamDevice(get())); }
1935
1956 void ResumeDevice() { CheckError(SDL_ResumeAudioStreamDevice(get())); }
1957
1974 bool DevicePaused() const { return SDL_AudioStreamDevicePaused(get()); }
1975
2001
2042 void SetGetCallback(AudioStreamCB callback);
2043
2086 void SetGetCallback(AudioStreamCallback callback, void* userdata)
2087 {
2088 CheckError(SDL_SetAudioStreamGetCallback(get(), callback, userdata));
2089 }
2090
2134 void SetPutCallback(AudioStreamCB callback);
2135
2181 void SetPutCallback(AudioStreamCallback callback, void* userdata)
2182 {
2183 CheckError(SDL_SetAudioStreamPutCallback(get(), callback, userdata));
2184 }
2185
2204 void Bind(AudioDeviceRef devid) { devid.BindAudioStream(*this); }
2205
2219 void Unbind() { SDL_UnbindAudioStream(get()); }
2220
2239 AudioDeviceRef GetDevice() const { return SDL_GetAudioStreamDevice(get()); }
2240
2260 static void reset(SDL_AudioStream* resource)
2261 {
2264 SDL_DestroyAudioStream(resource);
2265 }
2266};
2267
2275struct AudioStream : ResourceUnique<AudioStreamRef>
2276{
2278
2301 {
2302 return AudioStream(CheckError(SDL_CreateAudioStream(src_spec, dst_spec)));
2303 }
2304
2363 AudioStreamCB callback)
2364 {
2365 AudioStream stream = OpenAudioDeviceStream(devid, std::move(spec));
2366 if (devid.IsPlayback()) {
2367 stream->SetGetCallback(std::move(callback));
2368 } else {
2369 stream->SetPutCallback(std::move(callback));
2370 }
2371 return stream;
2372 }
2373
2433 AudioDeviceRef devid,
2434 OptionalRef<const AudioSpec> spec = std::nullopt,
2435 AudioStreamCallback callback = nullptr,
2436 void* userdata = nullptr)
2437 {
2438 return AudioStream(
2439 CheckError(SDL_OpenAudioDeviceStream(devid, spec, callback, userdata)));
2440 }
2441
2460 void Destroy() { reset(); }
2465};
2466
2468{
2469 return AudioStreamShared(std::move(*this));
2470}
2471
2481struct AudioStreamUnsafe : ResourceUnsafe<AudioStreamRef>
2482{
2484
2488 constexpr explicit AudioStreamUnsafe(AudioStream&& other)
2489 : AudioStreamUnsafe(other.release())
2490 {
2491 }
2492};
2493
2497struct AudioStreamLock : LockBase<AudioStreamRef>
2498{
2502 constexpr AudioStreamLock() = default;
2503
2508 : LockBase(other.release())
2509 {
2510 }
2511
2538 : LockBase(stream.get())
2539 {
2540 CheckError(SDL_LockAudioStream(stream));
2541 }
2542
2547
2562 void Unlock() { CheckError(SDL_UnlockAudioStream(release().get())); }
2563
2567 void reset() { Unlock(); }
2568};
2569
2591inline int GetNumAudioDrivers() { return SDL_GetNumAudioDrivers(); }
2592
2615inline const char* GetAudioDriver(int index)
2616{
2617 return SDL_GetAudioDriver(index);
2618}
2619
2634inline const char* GetCurrentAudioDriver()
2635{
2636 return SDL_GetCurrentAudioDriver();
2637}
2638
2665{
2666 int count;
2667 auto data = CheckError(SDL_GetAudioPlaybackDevices(&count));
2668 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
2669 size_t(count)};
2670}
2671
2698{
2699 int count;
2700 auto data = CheckError(SDL_GetAudioRecordingDevices(&count));
2701 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
2702 size_t(count)};
2703}
2704
2705inline void AudioDeviceRef::BindAudioStreams(std::span<AudioStreamRef> streams)
2706{
2707 SDL_assert_paranoid(streams.size() < SDL_MAX_SINT32);
2708 CheckError(SDL_BindAudioStreams(
2709 get(),
2710 reinterpret_cast<SDL_AudioStream* const*>(streams.data()),
2711 streams.size()));
2712}
2713
2715{
2716 CheckError(SDL_BindAudioStream(get(), stream));
2717}
2718
2737inline void UnbindAudioStreams(std::span<AudioStreamRef> streams)
2738{
2739 SDL_assert_paranoid(streams.size() < SDL_MAX_SINT32);
2740 SDL_UnbindAudioStreams(
2741 reinterpret_cast<SDL_AudioStream* const*>(streams.data()), streams.size());
2742}
2743
2745
2810{
2811 Uint8* buf;
2812 Uint32 len;
2813 if (!SDL_LoadWAV_IO(src, false, spec, &buf, &len)) return {};
2814 return OwnArray<Uint8>{buf, size_t(len)};
2815}
2816
2841{
2842 Uint8* buf;
2843 Uint32 len;
2844 if (!SDL_LoadWAV(path, spec, &buf, &len)) return {};
2845 return OwnArray<Uint8>{buf, size_t(len)};
2846}
2847
2880inline void MixAudio(Uint8* dst,
2881 SourceBytes src,
2882 AudioFormat format,
2883 float volume)
2884{
2885 SDL_assert_paranoid(src.size_bytes < SDL_MAX_SINT32);
2886 CheckError(SDL_MixAudio(
2887 dst, static_cast<const Uint8*>(src.data), format, src.size_bytes, volume));
2888}
2889
2922inline void MixAudio(TargetBytes dst,
2923 SourceBytes src,
2924 AudioFormat format,
2925 float volume)
2926{
2927 if (dst.size_bytes < src.size_bytes) src.size_bytes = dst.size_bytes;
2928 MixAudio(static_cast<Uint8*>(dst.data), src, format, volume);
2929}
2930
2955 SourceBytes src_data,
2956 const AudioSpec& dst_spec)
2957{
2958 SDL_assert_paranoid(src_data.size_bytes < SDL_MAX_SINT32);
2959 Uint8* buf;
2960 int len;
2961 CheckError(SDL_ConvertAudioSamples(&src_spec,
2962 static_cast<const Uint8*>(src_data.data),
2963 src_data.size_bytes,
2964 &dst_spec,
2965 &buf,
2966 &len));
2967 return OwnArray<Uint8>{buf, size_t(len)};
2968}
2969
2970#pragma region impl
2972
2974{
2976
2977 auto cb = Wrapper::Wrap(get(), std::move(callback));
2978 if (!SDL_SetAudioPostmixCallback(
2979 get(),
2980 [](void* userdata, const AudioSpec* spec, float* buffer, int buflen) {
2981 Wrapper::Call(userdata, *spec, std::span{buffer, size_t(buflen)});
2982 },
2983 cb)) {
2984 Wrapper::release(get());
2985 throw Error{};
2986 }
2987}
2988
2990{
2992 if (!SDL_SetAudioStreamGetCallback(
2993 get(),
2994 [](void* userdata,
2995 SDL_AudioStream* stream,
2996 int additional_amount,
2997 int total_amount) {
2998 Wrapper::Call(userdata, stream, additional_amount, total_amount);
2999 },
3000 Wrapper::Wrap(get(), std::move(callback)))) {
3001 Wrapper::release(get());
3002 throw Error{};
3003 }
3004}
3005
3007{
3009 if (!SDL_SetAudioStreamPutCallback(
3010 get(),
3011 [](void* userdata,
3012 SDL_AudioStream* stream,
3013 int additional_amount,
3014 int total_amount) {
3015 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3016 },
3017 Wrapper::Wrap(get(), std::move(callback)))) {
3018 Wrapper::release(get());
3019 throw Error{};
3020 }
3021}
3022
3023#pragma endregion impl
3024} // namespace SDL
3025
3026#endif /* SDL3PP_AUDIO_H_ */
Audio format.
Definition SDL3pp_audio.h:241
constexpr bool IsUnsigned()
Determine if an AudioFormat represents unsigned data.
Definition SDL3pp_audio.h:416
constexpr Uint16 GetByteSize()
Retrieve the size, in bytes, from an AudioFormat.
Definition SDL3pp_audio.h:335
constexpr bool IsInt()
Determine if an AudioFormat represents integer data.
Definition SDL3pp_audio.h:403
constexpr bool IsBigEndian()
Determine if an AudioFormat represents bigendian data.
Definition SDL3pp_audio.h:361
constexpr bool IsSigned()
Determine if an AudioFormat represents signed data.
Definition SDL3pp_audio.h:390
constexpr bool IsFloat()
Determine if an AudioFormat represents floating point data.
Definition SDL3pp_audio.h:348
constexpr bool IsLittleEndian()
Determine if an AudioFormat represents littleendian data.
Definition SDL3pp_audio.h:374
int GetSilenceValue() const
Get the appropriate memset value for silencing an audio format.
Definition SDL3pp_audio.h:443
constexpr Uint16 GetBitSize()
Retrieve the size, in bits, from an AudioFormat.
Definition SDL3pp_audio.h:322
constexpr AudioFormat(SDL_AudioFormat audioFormat={})
Wraps AudioFormat.
Definition SDL3pp_audio.h:250
constexpr bool operator==(SDL_AudioFormat audioFormat) const
Compares with the underlying type.
Definition SDL3pp_audio.h:292
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:278
const char * GetName() const
Get the human readable name of an audio format.
Definition SDL3pp_audio.h:428
An exception that returns GetError()
Definition SDL3pp_error.h:167
Base class for locks.
Definition SDL3pp_resource.h:408
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
reference & get()
Get reference.
Definition SDL3pp_resource.h:120
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_AudioDeviceID get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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:364
void BindAudioStream(AudioStreamRef stream)
Bind a single audio stream to an audio device.
Definition SDL3pp_audio.h:2714
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:2954
constexpr SDL_AudioFormat AUDIO_S16BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:466
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:522
constexpr AudioDeviceRef AUDIO_DEVICE_DEFAULT_PLAYBACK
A value used to request a default playback audio device.
Definition SDL3pp_audio.h:1162
const char * GetCurrentAudioDriver()
Get the name of the current audio driver.
Definition SDL3pp_audio.h:2634
constexpr SDL_AudioFormat AUDIO_S32LE
32-bit integer samples
Definition SDL3pp_audio.h:469
constexpr SDL_AudioFormat AUDIO_S32BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:472
ResourceShared< AudioDevice > AudioDeviceShared
Handle to a shared audioDevice.
Definition SDL3pp_audio.h:143
constexpr AudioDeviceRef AUDIO_DEVICE_DEFAULT_RECORDING
A value used to request a default recording audio device.
Definition SDL3pp_audio.h:1174
constexpr SDL_AudioFormat AUDIO_S16LE
Signed 16-bit samples.
Definition SDL3pp_audio.h:463
std::function< void(AudioStreamRef stream, int additional_amount, int total_amount)> AudioStreamCB
A callback that fires when data passes through an AudioStream.
Definition SDL3pp_audio.h:1277
OwnArray< AudioDeviceRef > GetAudioRecordingDevices()
Get a list of currently-connected audio recording devices.
Definition SDL3pp_audio.h:2697
AudioStreamShared share()
Move this audioStream into a AudioStreamShared.
Definition SDL3pp_audio.h:2467
const char * GetAudioDriver(int index)
Use this function to get the name of a built in audio driver.
Definition SDL3pp_audio.h:2615
constexpr SDL_AudioFormat AUDIO_S16
AUDIO_S16.
Definition SDL3pp_audio.h:481
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:558
void MixAudio(Uint8 *dst, SourceBytes src, AudioFormat format, float volume)
Mix audio data in a specified format.
Definition SDL3pp_audio.h:2880
constexpr SDL_AudioFormat AUDIO_F32
AUDIO_F32.
Definition SDL3pp_audio.h:485
constexpr SDL_AudioFormat AUDIO_F32LE
32-bit floating point samples
Definition SDL3pp_audio.h:475
int GetNumAudioDrivers()
Use this function to get the number of built-in audio drivers.
Definition SDL3pp_audio.h:2591
void UnbindAudioStreams(std::span< AudioStreamRef > streams)
Unbind a list of audio streams from their audio devices.
Definition SDL3pp_audio.h:2737
constexpr SDL_AudioFormat AUDIO_U8
Unsigned 8-bit samples.
Definition SDL3pp_audio.h:459
constexpr SDL_AudioFormat AUDIO_S32
AUDIO_S32.
Definition SDL3pp_audio.h:483
AudioStreamLock Lock()
Lock an audio stream for serialized access.
Definition SDL3pp_audio.h:2744
AudioDeviceShared share()
Move this audioDevice into a AudioDeviceShared.
Definition SDL3pp_audio.h:1126
void BindAudioStreams(std::span< AudioStreamRef > streams)
Bind a list of audio streams to an audio device.
Definition SDL3pp_audio.h:2705
OwnArray< Uint8 > LoadWAV(IOStreamRef src, AudioSpec *spec)
Load the audio data of a WAVE file into memory.
Definition SDL3pp_audio.h:2809
OwnArray< AudioDeviceRef > GetAudioPlaybackDevices()
Get a list of currently-connected audio playback devices.
Definition SDL3pp_audio.h:2664
constexpr SDL_AudioFormat AUDIO_S8
Signed 8-bit samples.
Definition SDL3pp_audio.h:461
SDL_AudioStreamCallback AudioStreamCallback
A callback that fires when data passes through an AudioStream.
Definition SDL3pp_audio.h:1235
constexpr SDL_AudioFormat AUDIO_F32BE
As above, but big-endian byte order.
Definition SDL3pp_audio.h:478
ResourceShared< AudioStream > AudioStreamShared
Handle to a shared audioStream.
Definition SDL3pp_audio.h:169
constexpr int AudioFrameSize(const AudioSpec &x)
Calculate the size of each audio frame (in bytes) from an AudioSpec.
Definition SDL3pp_audio.h:1190
constexpr SDL_AudioFormat AUDIO_UNKNOWN
Unspecified audio format.
Definition SDL3pp_audio.h:456
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:572
void SetPostmixCallback(AudioPostmixCB callback)
Set a callback that fires when data is about to be fed to an audio device.
Definition SDL3pp_audio.h:2973
void SetGain(float gain)
Change the gain of an audio device.
Definition SDL3pp_audio.h:826
bool IsPlayback() const
Determine if an audio device is a playback device (instead of recording).
Definition SDL3pp_audio.h:695
void Resume()
Use this function to unpause audio playback on a specified device.
Definition SDL3pp_audio.h:750
void Pause()
Use this function to pause audio playback on a specified device.
Definition SDL3pp_audio.h:724
static void reset(SDL_AudioDeviceID resource)
Close a previously-opened audio device.
Definition SDL3pp_audio.h:1006
AudioSpec GetFormat(int *sample_frames=nullptr) const
Get the current audio format of a specific audio device.
Definition SDL3pp_audio.h:627
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:982
OwnArray< int > GetChannelMap() const
Get the current channel map of an audio device.
Definition SDL3pp_audio.h:652
constexpr auto operator<=>(AudioDeviceRef other) const
Comparison.
Definition SDL3pp_audio.h:576
float GetGain() const
Get the gain of an audio device.
Definition SDL3pp_audio.h:793
bool IsPhysical() const
Determine if an audio device is physical (instead of logical).
Definition SDL3pp_audio.h:682
const char * GetName() const
Get the human-readable name of a specific audio device.
Definition SDL3pp_audio.h:594
bool Paused() const
Use this function to query if an audio device is paused.
Definition SDL3pp_audio.h:771
Unsafe Handle to audioDevice.
Definition SDL3pp_audio.h:1141
constexpr AudioDeviceUnsafe(AudioDevice &&other)
Constructs AudioDeviceUnsafe from AudioDevice.
Definition SDL3pp_audio.h:1147
Handle to an owned audioDevice.
Definition SDL3pp_audio.h:1021
static AudioDevice Open(AudioDeviceRef devid, OptionalRef< const SDL_AudioSpec > spec)
Open a specific audio device.
Definition SDL3pp_audio.h:1097
void Close()
Close a previously-opened audio device.
Definition SDL3pp_audio.h:1119
Locks a AudioStream.
Definition SDL3pp_audio.h:2498
void reset()
Same as Unlock(), just for uniformity.
Definition SDL3pp_audio.h:2567
~AudioStreamLock()
Destructor.
Definition SDL3pp_audio.h:2546
AudioStreamLock(AudioStreamRef stream)
Lock an audio stream for serialized access.
Definition SDL3pp_audio.h:2537
constexpr AudioStreamLock(AudioStreamLock &&other)
Move ctor.
Definition SDL3pp_audio.h:2507
void Unlock()
Unlock an audio stream for serialized access.
Definition SDL3pp_audio.h:2562
constexpr AudioStreamLock()=default
Creates an empty lock.
The opaque handle that represents an audio stream.
Definition SDL3pp_audio.h:1307
void PauseDevice()
Use this function to pause audio playback on the audio device associated with an audio stream.
Definition SDL3pp_audio.h:1934
AudioDeviceRef GetDevice() const
Query an audio stream for its currently-bound device.
Definition SDL3pp_audio.h:2239
OwnArray< int > GetInputChannelMap() const
Get the current input channel map of an audio stream.
Definition SDL3pp_audio.h:1596
void ResumeDevice()
Use this function to unpause audio playback on the audio device associated with an audio stream.
Definition SDL3pp_audio.h:1956
void SetPutCallback(AudioStreamCB callback)
Set a callback that runs when data is added to an audio stream.
Definition SDL3pp_audio.h:3006
AudioSpec GetOutputFormat() const
Query the current output format of an audio stream.
Definition SDL3pp_audio.h:1358
void Clear()
Clear any pending data in the stream.
Definition SDL3pp_audio.h:1912
int GetData(TargetBytes buf)
Get converted/resampled data from the stream.
Definition SDL3pp_audio.h:1810
float GetGain() const
Get the gain of an audio stream.
Definition SDL3pp_audio.h:1552
PropertiesRef GetProperties() const
Get the properties associated with an audio stream.
Definition SDL3pp_audio.h:1320
OwnArray< int > GetOutputChannelMap() const
Get the current output channel map of an audio stream.
Definition SDL3pp_audio.h:1623
float GetFrequencyRatio() const
Get the frequency ratio of an audio stream.
Definition SDL3pp_audio.h:1500
void SetGetCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is requested from an audio stream.
Definition SDL3pp_audio.h:2086
void SetFrequencyRatio(float ratio)
Change the frequency ratio of an audio stream.
Definition SDL3pp_audio.h:1529
void SetOutputChannelMap(std::span< int > chmap)
Set the current output channel map of an audio stream.
Definition SDL3pp_audio.h:1746
void SetGetCallback(AudioStreamCB callback)
Set a callback that runs when data is requested from an audio stream.
Definition SDL3pp_audio.h:2989
void PutData(SourceBytes buf)
Add data to the stream.
Definition SDL3pp_audio.h:1778
void SetGain(float gain)
Change the gain of an audio stream.
Definition SDL3pp_audio.h:1575
void SetPutCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is added to an audio stream.
Definition SDL3pp_audio.h:2181
void SetOutputFormat(const AudioSpec &spec)
Change the output format of an audio stream.
Definition SDL3pp_audio.h:1446
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:1481
void Unbind()
Unbind a single audio stream from its audio device.
Definition SDL3pp_audio.h:2219
int GetAvailable() const
Get the number of converted/resampled bytes available.
Definition SDL3pp_audio.h:1838
void Bind(AudioDeviceRef devid)
Bind a single audio stream to an audio device.
Definition SDL3pp_audio.h:2204
void SetInputFormat(const AudioSpec &spec)
Change the input format of an audio stream.
Definition SDL3pp_audio.h:1414
void Flush()
Tell the stream that you're done sending data, and anything being buffered should be converted/resamp...
Definition SDL3pp_audio.h:1893
int GetQueued() const
Get the number of bytes currently queued.
Definition SDL3pp_audio.h:1875
void GetFormat(AudioSpec *src_spec, AudioSpec *dst_spec) const
Query the current format of an audio stream.
Definition SDL3pp_audio.h:1379
bool DevicePaused() const
Use this function to query if an audio device associated with a stream is paused.
Definition SDL3pp_audio.h:1974
static void reset(SDL_AudioStream *resource)
Free an audio stream.
Definition SDL3pp_audio.h:2260
AudioSpec GetInputFormat() const
Query the current input format of an audio stream.
Definition SDL3pp_audio.h:1338
void SetInputChannelMap(std::span< int > chmap)
Set the current input channel map of an audio stream.
Definition SDL3pp_audio.h:1686
Unsafe Handle to audioStream.
Definition SDL3pp_audio.h:2482
constexpr AudioStreamUnsafe(AudioStream &&other)
Constructs AudioStreamUnsafe from AudioStream.
Definition SDL3pp_audio.h:2488
Handle to an owned audioStream.
Definition SDL3pp_audio.h:2276
static AudioStream OpenAudioDeviceStream(AudioDeviceRef devid, OptionalRef< const AudioSpec > spec, AudioStreamCB callback)
Convenience function for straightforward audio init for the common case.
Definition SDL3pp_audio.h:2361
static AudioStream Create(OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Create a new audio stream.
Definition SDL3pp_audio.h:2299
static AudioStream OpenAudioDeviceStream(AudioDeviceRef 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:2432
void Destroy()
Free an audio stream.
Definition SDL3pp_audio.h:2460
The read/write operation structure.
Definition SDL3pp_iostream.h:123
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
SDL properties ID.
Definition SDL3pp_properties.h:209
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