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
116using AudioFormatRaw = SDL_AudioFormat;
117
118// Forward decl
119struct AudioFormat;
120
121// Forward decl
122struct AudioDevice;
123
125using AudioDeviceID = SDL_AudioDeviceID;
126
127// Forward decl
128struct AudioDeviceRef;
129
132{
134
137 : value(value)
138 {
139 }
140
142 constexpr AudioDeviceParam(std::nullptr_t = nullptr)
143 : value(0)
144 {
145 }
146
148 constexpr explicit operator bool() const { return !!value; }
149
151 constexpr auto operator<=>(const AudioDeviceParam& other) const = default;
152
154 constexpr operator AudioDeviceID() const { return value; }
155};
156
157// Forward decl
158struct AudioStream;
159
161using AudioStreamRaw = SDL_AudioStream*;
162
163// Forward decl
164struct AudioStreamRef;
165
168{
170
173 : value(value)
174 {
175 }
176
178 constexpr AudioStreamParam(std::nullptr_t = nullptr)
179 : value(nullptr)
180 {
181 }
182
184 constexpr explicit operator bool() const { return !!value; }
185
187 constexpr auto operator<=>(const AudioStreamParam& other) const = default;
188
190 constexpr operator AudioStreamRaw() const { return value; }
191};
192
193// Forward decl
194struct AudioStreamLock;
195
204constexpr Uint32 AUDIO_MASK_BITSIZE = SDL_AUDIO_MASK_BITSIZE;
205
214constexpr Uint32 AUDIO_MASK_FLOAT = SDL_AUDIO_MASK_FLOAT;
215
224constexpr Uint32 AUDIO_MASK_BIG_ENDIAN = SDL_AUDIO_MASK_BIG_ENDIAN;
225
234constexpr Uint32 AUDIO_MASK_SIGNED = SDL_AUDIO_MASK_SIGNED;
235
243using AudioSpec = SDL_AudioSpec;
244
260{
261 AudioFormatRaw m_audioFormat;
262
263public:
269 constexpr AudioFormat(AudioFormatRaw audioFormat = {}) noexcept
270 : m_audioFormat(audioFormat)
271 {
272 }
273
297 constexpr AudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
298 : m_audioFormat(
299 AudioFormatRaw(SDL_DEFINE_AUDIO_FORMAT(sign, bigendian, flt, size)))
300 {
301 }
302
308 constexpr operator AudioFormatRaw() const noexcept { return m_audioFormat; }
309
321 constexpr Uint16 GetBitSize() const;
322
334 constexpr Uint16 GetByteSize() const;
335
347 constexpr bool IsFloat() const;
348
360 constexpr bool IsBigEndian() const;
361
373 constexpr bool IsLittleEndian() const;
374
386 constexpr bool IsSigned() const;
387
399 constexpr bool IsInt() const;
400
412 constexpr bool IsUnsigned() const;
413
424 const char* GetName() const;
425
438 int GetSilenceValue() const;
439};
440
441// Unfortunate name clash with SDL_oldnames.h
442#undef AUDIO_U8
443#undef AUDIO_S8
444#undef AUDIO_S16
445#undef AUDIO_S32
446#undef AUDIO_F32
447
449 SDL_AUDIO_UNKNOWN;
450
451constexpr AudioFormat AUDIO_U8 = SDL_AUDIO_U8;
452
453constexpr AudioFormat AUDIO_S8 = SDL_AUDIO_S8;
454
455constexpr AudioFormat AUDIO_S16LE = SDL_AUDIO_S16LE;
456
458 SDL_AUDIO_S16BE;
459
460constexpr AudioFormat AUDIO_S32LE = SDL_AUDIO_S32LE;
461
463 SDL_AUDIO_S32BE;
464
466 SDL_AUDIO_F32LE;
467
469 SDL_AUDIO_F32BE;
470
471constexpr AudioFormat AUDIO_S16 = SDL_AUDIO_S16;
472
473constexpr AudioFormat AUDIO_S32 = SDL_AUDIO_S32;
474
475constexpr AudioFormat AUDIO_F32 = SDL_AUDIO_F32;
476
500constexpr AudioFormat DefineAudioFormat(bool sign,
501 bool bigendian,
502 bool flt,
503 Uint16 size)
504{
505 return AudioFormat(sign, bigendian, flt, size);
506}
507
520constexpr Uint16 AudioBitSize(AudioFormatRaw x) { return SDL_AUDIO_BITSIZE(x); }
521
523{
524 return SDL::AudioBitSize(m_audioFormat);
525}
526
540{
541 return SDL_AUDIO_BYTESIZE(x);
542}
543
545{
546 return SDL::AudioByteSize(m_audioFormat);
547}
548
561constexpr bool IsAudioFloat(AudioFormatRaw x) { return SDL_AUDIO_ISFLOAT(x); }
562
563constexpr bool AudioFormat::IsFloat() const
564{
565 return SDL::IsAudioFloat(m_audioFormat);
566}
567
581{
582 return SDL_AUDIO_ISBIGENDIAN(x);
583}
584
585constexpr bool AudioFormat::IsBigEndian() const
586{
587 return SDL::IsAudioBigENDIAN(m_audioFormat);
588}
589
603{
604 return SDL_AUDIO_ISLITTLEENDIAN(x);
605}
606
607constexpr bool AudioFormat::IsLittleEndian() const
608{
609 return SDL::IsAudioLittleEndian(m_audioFormat);
610}
611
624constexpr bool IsAudioSigned(AudioFormatRaw x) { return SDL_AUDIO_ISSIGNED(x); }
625
626constexpr bool AudioFormat::IsSigned() const
627{
628 return SDL::IsAudioSigned(m_audioFormat);
629}
630
643constexpr bool IsAudioInt(AudioFormatRaw x) { return SDL_AUDIO_ISINT(x); }
644
645constexpr bool AudioFormat::IsInt() const
646{
647 return SDL::IsAudioInt(m_audioFormat);
648}
649
663{
664 return SDL_AUDIO_ISUNSIGNED(x);
665}
666
667constexpr bool AudioFormat::IsUnsigned() const
668{
669 return SDL::IsAudioUnsigned(m_audioFormat);
670}
671
707using AudioPostmixCallback = void(SDLCALL*)(void* userdata,
708 const AudioSpec* spec,
709 float* buffer,
710 int buflen);
711
747 MakeFrontCallback<void(const AudioSpec* spec, float* buffer, int buflen)>;
748
788using AudioStreamCallback = void(SDLCALL*)(void* userdata,
789 AudioStreamRaw stream,
790 int additional_amount,
791 int total_amount);
792
833 void(AudioStreamRaw stream, int additional_amount, int total_amount)>;
834
845{
846 AudioDeviceID m_resource = 0;
847
848public:
850 constexpr AudioDevice(std::nullptr_t = nullptr) noexcept
851 : m_resource(0)
852 {
853 }
854
862 constexpr explicit AudioDevice(const AudioDeviceID resource) noexcept
863 : m_resource(resource)
864 {
865 }
866
867protected:
869 constexpr AudioDevice(const AudioDevice& other) noexcept = default;
870
871public:
873 constexpr AudioDevice(AudioDevice&& other) noexcept
874 : AudioDevice(other.release())
875 {
876 }
877
878 constexpr AudioDevice(const AudioDeviceRef& other) = delete;
879
880 constexpr AudioDevice(AudioDeviceRef&& other) = delete;
881
954 : m_resource(CheckError(SDL_OpenAudioDevice(devid, spec)))
955 {
956 }
957
959 ~AudioDevice() { SDL_CloseAudioDevice(m_resource); }
960
962 constexpr AudioDevice& operator=(AudioDevice&& other) noexcept
963 {
964 std::swap(m_resource, other.m_resource);
965 return *this;
966 }
967
968protected:
970 constexpr AudioDevice& operator=(const AudioDevice& other) noexcept = default;
971
972public:
974 constexpr AudioDeviceID get() const noexcept { return m_resource; }
975
977 constexpr AudioDeviceID release() noexcept
978 {
979 auto r = m_resource;
980 m_resource = 0;
981 return r;
982 }
983
985 constexpr auto operator<=>(const AudioDevice& other) const noexcept = default;
986
988 constexpr explicit operator bool() const noexcept { return !!m_resource; }
989
991 constexpr operator AudioDeviceParam() const noexcept { return {m_resource}; }
992
1009 void Close();
1010
1033 const char* GetName() const;
1034
1066 AudioSpec GetFormat(int* sample_frames = nullptr) const;
1067
1087
1110 bool IsPhysical() const;
1111
1123 bool IsPlayback() const;
1124
1152 void Pause();
1153
1177 void Resume();
1178
1198 bool Paused() const;
1199
1220 float GetGain() const;
1221
1253 void SetGain(float gain);
1254
1290 void BindAudioStreams(std::span<AudioStreamRef> streams);
1291
1309 void BindAudioStream(AudioStreamParam stream);
1310
1361 void SetPostmixCallback(AudioPostmixCallback callback, void* userdata);
1362
1412 void SetPostmixCallback(AudioPostmixCB callback);
1413
1470 AudioStreamCallback callback,
1471 void* userdata);
1472
1525 AudioStreamCB callback);
1526};
1527
1530{
1532
1541 : AudioDevice(resource.value)
1542 {
1543 }
1544
1553 : AudioDevice(resource)
1554 {
1555 }
1556
1558 constexpr AudioDeviceRef(const AudioDeviceRef& other) noexcept = default;
1559
1562};
1563
1574 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
1575
1586 SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
1587
1601constexpr int AudioFrameSize(const AudioSpec& x)
1602{
1603 return SDL_AUDIO_FRAMESIZE(x);
1604}
1605
1606#if SDL_VERSION_ATLEAST(3, 4, 0)
1607
1634using AudioStreamDataCompleteCallback = void(SDLCALL*)(void* userdata,
1635 const void* buf,
1636 int buflen);
1637
1665 std::function<void(const void* buf, int buflen)>;
1666
1667#endif // SDL_VERSION_ATLEAST(3, 4, 0)
1668
1696{
1697 AudioStreamRaw m_resource = nullptr;
1698
1699public:
1701 constexpr AudioStream(std::nullptr_t = nullptr) noexcept
1702 : m_resource(0)
1703 {
1704 }
1705
1713 constexpr explicit AudioStream(const AudioStreamRaw resource) noexcept
1714 : m_resource(resource)
1715 {
1716 }
1717
1718protected:
1720 constexpr AudioStream(const AudioStream& other) noexcept = default;
1721
1722public:
1724 constexpr AudioStream(AudioStream&& other) noexcept
1725 : AudioStream(other.release())
1726 {
1727 }
1728
1729 constexpr AudioStream(const AudioStreamRef& other) = delete;
1730
1731 constexpr AudioStream(AudioStreamRef&& other) = delete;
1732
1755 : m_resource(CheckError(SDL_CreateAudioStream(src_spec, dst_spec)))
1756 {
1757 }
1758
1817 OptionalRef<const AudioSpec> spec = std::nullopt,
1818 AudioStreamCallback callback = nullptr,
1819 void* userdata = nullptr)
1820 : m_resource(
1821 CheckError(SDL_OpenAudioDeviceStream(devid, spec, callback, userdata)))
1822 {
1823 }
1824
1880 AudioStreamCB callback);
1881
1883 ~AudioStream() { SDL_DestroyAudioStream(m_resource); }
1884
1886 constexpr AudioStream& operator=(AudioStream&& other) noexcept
1887 {
1888 std::swap(m_resource, other.m_resource);
1889 return *this;
1890 }
1891
1892protected:
1894 constexpr AudioStream& operator=(const AudioStream& other) noexcept = default;
1895
1896public:
1898 constexpr AudioStreamRaw get() const noexcept { return m_resource; }
1899
1901 constexpr AudioStreamRaw release() noexcept
1902 {
1903 auto r = m_resource;
1904 m_resource = nullptr;
1905 return r;
1906 }
1907
1909 constexpr auto operator<=>(const AudioStream& other) const noexcept = default;
1910
1912 constexpr explicit operator bool() const noexcept { return !!m_resource; }
1913
1915 constexpr operator AudioStreamParam() const noexcept { return {m_resource}; }
1916
1934 void Destroy();
1935
1958
1973 {
1974 AudioSpec spec;
1975 GetFormat(&spec, nullptr);
1976 return spec;
1977 }
1978
1993 {
1994 AudioSpec spec;
1995 GetFormat(nullptr, &spec);
1996 return spec;
1997 }
1998
2013 void GetFormat(AudioSpec* src_spec, AudioSpec* dst_spec) const;
2014
2045 void SetInputFormat(const AudioSpec& spec) { SetFormat(spec, std::nullopt); }
2046
2077 void SetOutputFormat(const AudioSpec& spec) { SetFormat(std::nullopt, spec); }
2078
2114
2128 float GetFrequencyRatio() const;
2129
2154 void SetFrequencyRatio(float ratio);
2155
2174 float GetGain() const;
2175
2197 void SetGain(float gain);
2198
2219
2240
2296 void SetInputChannelMap(std::span<int> chmap);
2297
2351 void SetOutputChannelMap(std::span<int> chmap);
2352
2378 void PutData(SourceBytes buf);
2379
2380#if SDL_VERSION_ATLEAST(3, 4, 0)
2381
2424 void PutDataNoCopy(SourceBytes buf,
2426 void* userdata);
2427
2469
2470#endif // SDL_VERSION_ATLEAST(3, 4, 0)
2471
2498 int GetData(TargetBytes buf);
2499
2523 int GetAvailable() const;
2524
2560 int GetQueued() const;
2561
2578 void Flush();
2579
2597 void Clear();
2598
2619 void PauseDevice();
2620
2640 void ResumeDevice();
2641
2658 bool DevicePaused() const;
2659
2685
2700 void Unlock(AudioStreamLock&& lock);
2701
2744 void SetGetCallback(AudioStreamCallback callback, void* userdata);
2745
2786 void SetGetCallback(AudioStreamCB callback);
2787
2833 void SetPutCallback(AudioStreamCallback callback, void* userdata);
2834
2878 void SetPutCallback(AudioStreamCB callback);
2879
2892 void Unbind();
2893
2912 AudioDeviceRef GetDevice() const;
2913
2914#if SDL_VERSION_ATLEAST(3, 4, 0)
2915
2964 void PutPlanarData(const void* const* channel_buffers,
2965 int num_channels,
2966 int num_samples);
2967
2968#endif // SDL_VERSION_ATLEAST(3, 4, 0)
2969};
2970
2973{
2975
2984 : AudioStream(resource.value)
2985 {
2986 }
2987
2996 : AudioStream(resource)
2997 {
2998 }
2999
3001 constexpr AudioStreamRef(const AudioStreamRef& other) noexcept = default;
3002
3005};
3006
3026{
3027 AudioStreamRef m_lock;
3028
3029public:
3057
3059 AudioStreamLock(const AudioStreamLock& other) = delete;
3060
3062 constexpr AudioStreamLock(AudioStreamLock&& other) noexcept
3063 : m_lock(other.m_lock)
3064 {
3065 other.m_lock = {};
3066 }
3067
3081
3082 AudioStreamLock& operator=(const AudioStreamLock& other) = delete;
3083
3086 {
3087 std::swap(m_lock, other.m_lock);
3088 return *this;
3089 }
3090
3092 constexpr operator bool() const { return bool(m_lock); }
3093
3108 void reset();
3109
3111 AudioStreamRef get() { return m_lock; }
3112
3114 void release() { m_lock.release(); }
3115};
3116
3138inline int GetNumAudioDrivers() { return SDL_GetNumAudioDrivers(); }
3139
3162inline const char* GetAudioDriver(int index)
3163{
3164 return SDL_GetAudioDriver(index);
3165}
3166
3181inline const char* GetCurrentAudioDriver()
3182{
3183 return SDL_GetCurrentAudioDriver();
3184}
3185
3211{
3212 int count;
3213 auto data = CheckError(SDL_GetAudioPlaybackDevices(&count));
3214 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
3215 size_t(count)};
3216}
3217
3243{
3244 int count;
3245 auto data = CheckError(SDL_GetAudioRecordingDevices(&count));
3246 return OwnArray<AudioDeviceRef>{reinterpret_cast<AudioDeviceRef*>(data),
3247 size_t(count)};
3248}
3249
3273inline const char* GetAudioDeviceName(AudioDeviceParam devid)
3274{
3275 return CheckError(SDL_GetAudioDeviceName(devid));
3276}
3277
3278inline const char* AudioDevice::GetName() const
3279{
3280 return SDL::GetAudioDeviceName(m_resource);
3281}
3282
3314 int* sample_frames = nullptr)
3315{
3316 AudioSpec spec;
3317 CheckError(SDL_GetAudioDeviceFormat(devid, &spec, sample_frames));
3318 return spec;
3319}
3320
3321inline AudioSpec AudioDevice::GetFormat(int* sample_frames) const
3322{
3323 return SDL::GetAudioDeviceFormat(m_resource, sample_frames);
3324}
3325
3346{
3347 int count;
3348 auto data = SDL_GetAudioDeviceChannelMap(devid, &count);
3349 return OwnArray<int>{data, size_t(count)};
3350}
3351
3353{
3354 return SDL::GetAudioDeviceChannelMap(m_resource);
3355}
3356
3429{
3430 return AudioDevice(devid, spec);
3431}
3432
3457{
3458 return SDL_IsAudioDevicePhysical(devid);
3459}
3460
3461inline bool AudioDevice::IsPhysical() const
3462{
3463 return SDL::IsAudioDevicePhysical(m_resource);
3464}
3465
3479{
3480 return SDL_IsAudioDevicePlayback(devid);
3481}
3482
3483inline bool AudioDevice::IsPlayback() const
3484{
3485 return SDL::IsAudioDevicePlayback(m_resource);
3486}
3487
3517{
3518 CheckError(SDL_PauseAudioDevice(devid));
3519}
3520
3521inline void AudioDevice::Pause() { SDL::PauseAudioDevice(m_resource); }
3522
3548{
3549 CheckError(SDL_ResumeAudioDevice(devid));
3550}
3551
3552inline void AudioDevice::Resume() { SDL::ResumeAudioDevice(m_resource); }
3553
3575{
3576 return SDL_AudioDevicePaused(devid);
3577}
3578
3579inline bool AudioDevice::Paused() const
3580{
3581 return SDL::AudioDevicePaused(m_resource);
3582}
3583
3606{
3607 return SDL_GetAudioDeviceGain(devid);
3608}
3609
3610inline float AudioDevice::GetGain() const
3611{
3612 return SDL::GetAudioDeviceGain(m_resource);
3613}
3614
3647inline void SetAudioDeviceGain(AudioDeviceParam devid, float gain)
3648{
3649 CheckError(SDL_SetAudioDeviceGain(devid, gain));
3650}
3651
3652inline void AudioDevice::SetGain(float gain)
3653{
3654 SDL::SetAudioDeviceGain(m_resource, gain);
3655}
3656
3677{
3678 SDL_CloseAudioDevice(devid);
3679}
3680
3682
3720 std::span<AudioStreamRef> streams)
3721{
3722 CheckError(SDL_BindAudioStreams(
3723 devid,
3724 reinterpret_cast<SDL_AudioStream* const*>(streams.data()),
3725 streams.size()));
3726}
3727
3728inline void AudioDevice::BindAudioStreams(std::span<AudioStreamRef> streams)
3729{
3730 SDL::BindAudioStreams(m_resource, streams);
3731}
3732
3752{
3753 CheckError(SDL_BindAudioStream(devid, stream));
3754}
3755
3757{
3758 SDL::BindAudioStream(m_resource, stream);
3759}
3760
3779inline void UnbindAudioStreams(std::span<AudioStreamRef> streams)
3780{
3781 SDL_UnbindAudioStreams(
3782 reinterpret_cast<SDL_AudioStream* const*>(streams.data()), streams.size());
3783}
3784
3800{
3801 SDL_UnbindAudioStream(stream);
3802}
3803
3804inline void AudioStream::Unbind() { SDL::UnbindAudioStream(m_resource); }
3805
3825{
3826 return {SDL_GetAudioStreamDevice(stream)};
3827}
3828
3830{
3831 return SDL::GetAudioStreamDevice(m_resource);
3832}
3833
3856{
3857 return AudioStream(src_spec, dst_spec);
3858}
3859
3883{
3884 return {CheckError(SDL_GetAudioStreamProperties(stream))};
3885}
3886
3888{
3889 return SDL::GetAudioStreamProperties(m_resource);
3890}
3891
3892#if SDL_VERSION_ATLEAST(3, 4, 0)
3893
3894namespace prop::AudioStream {
3895
3896constexpr auto _AUTO_CLEANUP_BOOLEAN =
3897 SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN;
3898
3899} // namespace prop::AudioStream
3900
3901#endif // SDL_VERSION_ATLEAST(3, 4, 0)
3902
3919 AudioSpec* src_spec,
3920 AudioSpec* dst_spec)
3921{
3922 CheckError(SDL_GetAudioStreamFormat(stream, src_spec, dst_spec));
3923}
3924
3925inline void AudioStream::GetFormat(AudioSpec* src_spec,
3926 AudioSpec* dst_spec) const
3927{
3928 SDL::GetAudioStreamFormat(m_resource, src_spec, dst_spec);
3929}
3930
3968{
3969 CheckError(SDL_SetAudioStreamFormat(stream, src_spec, dst_spec));
3970}
3971
3974{
3975 SDL::SetAudioStreamFormat(m_resource, src_spec, dst_spec);
3976}
3977
3993{
3994 return SDL_GetAudioStreamFrequencyRatio(stream);
3995}
3996
3998{
3999 return SDL::GetAudioStreamFrequencyRatio(m_resource);
4000}
4001
4027inline void SetAudioStreamFrequencyRatio(AudioStreamParam stream, float ratio)
4028{
4029 CheckError(SDL_SetAudioStreamFrequencyRatio(stream, ratio));
4030}
4031
4032inline void AudioStream::SetFrequencyRatio(float ratio)
4033{
4034 SDL::SetAudioStreamFrequencyRatio(m_resource, ratio);
4035}
4036
4057{
4058 return SDL_GetAudioStreamGain(stream);
4059}
4060
4061inline float AudioStream::GetGain() const
4062{
4063 return SDL::GetAudioStreamGain(m_resource);
4064}
4065
4088inline void SetAudioStreamGain(AudioStreamParam stream, float gain)
4089{
4090 CheckError(SDL_SetAudioStreamGain(stream, gain));
4091}
4092
4093inline void AudioStream::SetGain(float gain)
4094{
4095 SDL::SetAudioStreamGain(m_resource, gain);
4096}
4097
4119{
4120 int count;
4121 auto data = SDL_GetAudioStreamInputChannelMap(stream, &count);
4122 if (!data) return {};
4123 return OwnArray<int>{data, size_t(count)};
4124}
4125
4127{
4128 return SDL::GetAudioStreamInputChannelMap(m_resource);
4129}
4130
4152{
4153 int count;
4154 auto data = SDL_GetAudioStreamOutputChannelMap(stream, &count);
4155 if (!data) return {};
4156 return OwnArray<int>{data, size_t(count)};
4157}
4158
4160{
4161 return SDL::GetAudioStreamOutputChannelMap(m_resource);
4162}
4163
4221 std::span<int> chmap)
4222{
4223 CheckError(
4224 SDL_SetAudioStreamInputChannelMap(stream, chmap.data(), chmap.size()));
4225}
4226
4227inline void AudioStream::SetInputChannelMap(std::span<int> chmap)
4228{
4229 SDL::SetAudioStreamInputChannelMap(m_resource, chmap);
4230}
4231
4287 std::span<int> chmap)
4288{
4289 CheckError(
4290 SDL_SetAudioStreamOutputChannelMap(stream, chmap.data(), chmap.size()));
4291}
4292
4293inline void AudioStream::SetOutputChannelMap(std::span<int> chmap)
4294{
4295 SDL::SetAudioStreamOutputChannelMap(m_resource, chmap);
4296}
4297
4325{
4326 CheckError(SDL_PutAudioStreamData(stream, buf.data(), buf.size_bytes()));
4327}
4328
4330{
4331 SDL::PutAudioStreamData(m_resource, std::move(buf));
4332}
4333
4334#if SDL_VERSION_ATLEAST(3, 4, 0)
4335
4380 SourceBytes buf,
4382 void* userdata)
4383{
4384 CheckError(SDL_PutAudioStreamDataNoCopy(
4385 stream, buf.data(), buf.size_bytes(), callback, userdata));
4386}
4387
4430 SourceBytes buf,
4432{
4435 std::move(buf),
4436 &Wrapper::CallOnce,
4437 Wrapper::Wrap(std::move(callback)));
4438}
4439
4442 void* userdata)
4443{
4444 SDL::PutAudioStreamDataNoCopy(m_resource, std::move(buf), callback, userdata);
4445}
4446
4449{
4450 SDL::PutAudioStreamDataNoCopy(m_resource, std::move(buf), callback);
4451}
4452
4502 const void* const* channel_buffers,
4503 int num_channels,
4504 int num_samples)
4505{
4506 CheckError(SDL_PutAudioStreamPlanarData(
4507 stream, channel_buffers, num_channels, num_samples));
4508}
4509
4510inline void AudioStream::PutPlanarData(const void* const* channel_buffers,
4511 int num_channels,
4512 int num_samples)
4513{
4515 m_resource, channel_buffers, num_channels, num_samples);
4516}
4517
4518#endif // SDL_VERSION_ATLEAST(3, 4, 0)
4519
4548{
4549 return SDL_GetAudioStreamData(stream, buf.data(), buf.size_bytes());
4550}
4551
4553{
4554 return SDL::GetAudioStreamData(m_resource, std::move(buf));
4555}
4556
4581{
4582 return SDL_GetAudioStreamAvailable(stream);
4583}
4584
4586{
4587 return SDL::GetAudioStreamAvailable(m_resource);
4588}
4589
4626{
4627 return SDL_GetAudioStreamQueued(stream);
4628}
4629
4630inline int AudioStream::GetQueued() const
4631{
4632 return SDL::GetAudioStreamQueued(m_resource);
4633}
4634
4653{
4654 CheckError(SDL_FlushAudioStream(stream));
4655}
4656
4657inline void AudioStream::Flush() { SDL::FlushAudioStream(m_resource); }
4658
4678{
4679 CheckError(SDL_ClearAudioStream(stream));
4680}
4681
4682inline void AudioStream::Clear() { SDL::ClearAudioStream(m_resource); }
4683
4706{
4707 CheckError(SDL_PauseAudioStreamDevice(stream));
4708}
4709
4711{
4712 SDL::PauseAudioStreamDevice(m_resource);
4713}
4714
4736{
4737 CheckError(SDL_ResumeAudioStreamDevice(stream));
4738}
4739
4741{
4742 SDL::ResumeAudioStreamDevice(m_resource);
4743}
4744
4763{
4764 return SDL_AudioStreamDevicePaused(stream);
4765}
4766
4767inline bool AudioStream::DevicePaused() const
4768{
4769 return SDL::AudioStreamDevicePaused(m_resource);
4770}
4771
4798{
4799 CheckError(SDL_LockAudioStream(stream));
4800}
4801
4803
4805 : m_lock(std::move(resource))
4806{
4807 LockAudioStream(m_lock);
4808}
4809
4826{
4827 CheckError(SDL_UnlockAudioStream(stream));
4828}
4829
4831{
4832 SDL_assert_paranoid(lock.get() == *this);
4833 lock.reset();
4834}
4835
4837{
4838 if (!m_lock) return;
4839 UnlockAudioStream(m_lock);
4840 m_lock = {};
4841}
4842
4885 AudioStreamCallback callback,
4886 void* userdata)
4887{
4888 CheckError(SDL_SetAudioStreamGetCallback(stream, callback, userdata));
4889}
4890
4931 AudioStreamCB callback)
4932{
4933 SetAudioStreamGetCallback(stream, callback.wrapper, callback.data);
4934}
4935
4937 void* userdata)
4938{
4939 SDL::SetAudioStreamGetCallback(m_resource, callback, userdata);
4940}
4941
4943{
4944 SDL::SetAudioStreamGetCallback(m_resource, callback);
4945}
4946
4993 AudioStreamCallback callback,
4994 void* userdata)
4995{
4996 CheckError(SDL_SetAudioStreamPutCallback(stream, callback, userdata));
4997}
4998
5043 AudioStreamCB callback)
5044{
5045 SetAudioStreamPutCallback(stream, callback.wrapper, callback.data);
5046}
5047
5049 void* userdata)
5050{
5051 SDL::SetAudioStreamPutCallback(m_resource, callback, userdata);
5052}
5053
5055{
5056 SDL::SetAudioStreamPutCallback(m_resource, callback);
5057}
5058
5078{
5079 SDL_DestroyAudioStream(stream);
5080}
5081
5083
5143 AudioStreamCallback callback = nullptr,
5144 void* userdata = nullptr)
5145{
5146 return AudioStream(devid, spec, callback, userdata);
5147}
5148
5204 AudioStreamCB callback)
5205{
5206 return AudioStream(devid, spec, callback);
5207}
5208
5210 AudioStreamCallback callback,
5211 void* userdata)
5212{
5213 return AudioStream(m_resource, spec, callback, userdata);
5214}
5215
5217 AudioStreamCB callback)
5218{
5219 return AudioStream(m_resource, spec, callback);
5220}
5221
5224 AudioStreamCB callback)
5225 : AudioStream(devid, spec)
5226{
5227 if (IsAudioDevicePlayback(devid)) {
5228 SetGetCallback(std::move(callback));
5229 } else {
5230 SetPutCallback(std::move(callback));
5231 }
5232}
5233
5286 AudioPostmixCallback callback,
5287 void* userdata)
5288{
5289 CheckError(SDL_SetAudioPostmixCallback(devid, callback, userdata));
5290}
5291
5343 AudioPostmixCB callback)
5344{
5345 SetAudioPostmixCallback(devid, callback.wrapper, callback.data);
5346}
5347
5349 void* userdata)
5350{
5351 SDL::SetAudioPostmixCallback(m_resource, callback, userdata);
5352}
5353
5355{
5356 SDL::SetAudioPostmixCallback(m_resource, callback);
5357}
5358
5426 AudioSpec* spec,
5427 bool closeio = false)
5428{
5429 Uint8* buf;
5430 Uint32 len;
5431 if (!SDL_LoadWAV_IO(src, closeio, spec, &buf, &len)) return {};
5432 return OwnArray<Uint8>{buf, size_t(len)};
5433}
5434
5459{
5460 Uint8* buf;
5461 Uint32 len;
5462 if (!SDL_LoadWAV(path, spec, &buf, &len)) return {};
5463 return OwnArray<Uint8>{buf, size_t(len)};
5464}
5465
5497inline void MixAudio(Uint8* dst,
5498 SourceBytes src,
5499 AudioFormat format,
5500 float volume)
5501{
5502 CheckError(
5503 SDL_MixAudio(dst, src.data_as<Uint8>(), format, src.size_bytes(), volume));
5504}
5505
5537inline void MixAudio(TargetBytes dst,
5538 SourceBytes src,
5539 AudioFormat format,
5540 float volume)
5541{
5542 if (dst.size_bytes() < src.size_bytes()) {
5543 MixAudio(dst.data_as<Uint8>(),
5544 SourceBytes{src.data(), dst.size_bytes()},
5545 format,
5546 volume);
5547 } else
5548 MixAudio(dst.data_as<Uint8>(), src, format, volume);
5549}
5550
5574 SourceBytes src_data,
5575 const AudioSpec& dst_spec)
5576{
5577 Uint8* buf;
5578 int len;
5579 CheckError(SDL_ConvertAudioSamples(&src_spec,
5580 src_data.data_as<Uint8>(),
5581 src_data.size_bytes(),
5582 &dst_spec,
5583 &buf,
5584 &len));
5585 return OwnArray<Uint8>{buf, size_t(len)};
5586}
5587
5599inline const char* GetAudioFormatName(AudioFormatRaw format)
5600{
5601 return SDL_GetAudioFormatName(format);
5602}
5603
5604inline const char* AudioFormat::GetName() const
5605{
5606 return SDL::GetAudioFormatName(m_audioFormat);
5607}
5608
5623{
5624 return SDL_GetSilenceValueForFormat(format);
5625}
5626
5628{
5629 return SDL::GetSilenceValueForFormat(m_audioFormat);
5630}
5631
5633
5634} // namespace SDL
5635
5636#endif /* SDL3PP_AUDIO_H_ */
SDL Audio Device instance IDs.
Definition: SDL3pp_audio.h:845
constexpr AudioDeviceID get() const noexcept
Retrieves underlying AudioDeviceID.
Definition: SDL3pp_audio.h:974
constexpr AudioDevice & operator=(const AudioDevice &other) noexcept=default
Assignment operator.
constexpr AudioDeviceID release() noexcept
Retrieves underlying AudioDeviceID and clear this.
Definition: SDL3pp_audio.h:977
~AudioDevice()
Destructor.
Definition: SDL3pp_audio.h:959
AudioDevice(AudioDeviceParam devid, OptionalRef< const AudioSpec > spec)
Open a specific audio device.
Definition: SDL3pp_audio.h:953
constexpr AudioDevice(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_audio.h:850
constexpr AudioDevice(const AudioDeviceID resource) noexcept
Constructs from AudioDeviceParam.
Definition: SDL3pp_audio.h:862
constexpr auto operator<=>(const AudioDevice &other) const noexcept=default
Comparison.
constexpr AudioDevice(const AudioDevice &other) noexcept=default
Copy constructor.
constexpr AudioDevice & operator=(AudioDevice &&other) noexcept
Assignment operator.
Definition: SDL3pp_audio.h:962
constexpr AudioDevice(AudioDevice &&other) noexcept
Move constructor.
Definition: SDL3pp_audio.h:873
Audio format.
Definition: SDL3pp_audio.h:260
constexpr AudioFormat(AudioFormatRaw audioFormat={}) noexcept
Wraps AudioFormat.
Definition: SDL3pp_audio.h:269
constexpr AudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
Define an AudioFormat value.
Definition: SDL3pp_audio.h:297
Lock an audio stream for serialized access.
Definition: SDL3pp_audio.h:3026
AudioStreamRef get()
Get the reference to locked resource.
Definition: SDL3pp_audio.h:3111
AudioStreamLock & operator=(AudioStreamLock &&other) noexcept
Assignment operator.
Definition: SDL3pp_audio.h:3085
void release()
Releases the lock without unlocking.
Definition: SDL3pp_audio.h:3114
AudioStreamLock(const AudioStreamLock &other)=delete
Copy constructor.
~AudioStreamLock()
Unlock an audio stream for serialized access.
Definition: SDL3pp_audio.h:3080
constexpr AudioStreamLock(AudioStreamLock &&other) noexcept
Move constructor.
Definition: SDL3pp_audio.h:3062
The opaque handle that represents an audio stream.
Definition: SDL3pp_audio.h:1696
constexpr auto operator<=>(const AudioStream &other) const noexcept=default
Comparison.
~AudioStream()
Destructor.
Definition: SDL3pp_audio.h:1883
AudioSpec GetOutputFormat() const
Query the current output format of an audio stream.
Definition: SDL3pp_audio.h:1992
void SetOutputFormat(const AudioSpec &spec)
Change the output format of an audio stream.
Definition: SDL3pp_audio.h:2077
constexpr AudioStream(AudioStream &&other) noexcept
Move constructor.
Definition: SDL3pp_audio.h:1724
void SetInputFormat(const AudioSpec &spec)
Change the input format of an audio stream.
Definition: SDL3pp_audio.h:2045
constexpr AudioStream(const AudioStreamRaw resource) noexcept
Constructs from AudioStreamParam.
Definition: SDL3pp_audio.h:1713
AudioStream(OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Create a new audio stream.
Definition: SDL3pp_audio.h:1753
constexpr AudioStream(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_audio.h:1701
constexpr AudioStreamRaw get() const noexcept
Retrieves underlying AudioStreamRaw.
Definition: SDL3pp_audio.h:1898
constexpr AudioStreamRaw release() noexcept
Retrieves underlying AudioStreamRaw and clear this.
Definition: SDL3pp_audio.h:1901
constexpr AudioStream(const AudioStream &other) noexcept=default
Copy constructor.
AudioSpec GetInputFormat() const
Query the current input format of an audio stream.
Definition: SDL3pp_audio.h:1972
AudioStream(AudioDeviceParam 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:1816
constexpr AudioStream & operator=(const AudioStream &other) noexcept=default
Assignment operator.
constexpr AudioStream & operator=(AudioStream &&other) noexcept
Assignment operator.
Definition: SDL3pp_audio.h:1886
Optional-like shim for references.
Definition: SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
Source byte stream.
Definition: SDL3pp_strings.h:240
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:304
constexpr const char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:307
constexpr const T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:314
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
Target byte stream.
Definition: SDL3pp_strings.h:326
constexpr char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:412
constexpr T * data_as() const
Retrieves contained data.
Definition: SDL3pp_strings.h:416
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:409
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition: SDL3pp_assert.h:383
constexpr bool IsAudioFloat(AudioFormatRaw x)
Determine if an AudioFormat represents floating point data.
Definition: SDL3pp_audio.h:561
void Pause()
Use this function to pause audio playback on a specified device.
Definition: SDL3pp_audio.h:3521
constexpr AudioFormat AUDIO_UNKNOWN
Unspecified audio format.
Definition: SDL3pp_audio.h:448
bool IsAudioDevicePlayback(AudioDeviceParam devid)
Determine if an audio device is a playback device (instead of recording).
Definition: SDL3pp_audio.h:3478
AudioStream OpenStream(OptionalRef< const AudioSpec > spec, AudioStreamCallback callback, void *userdata)
Convenience function for straightforward audio init for the common case.
Definition: SDL3pp_audio.h:5209
void Resume()
Use this function to unpause audio playback on a specified device.
Definition: SDL3pp_audio.h:3552
constexpr bool IsLittleEndian() const
Determine if an AudioFormat represents littleendian data.
Definition: SDL3pp_audio.h:607
void SetAudioPostmixCallback(AudioDeviceParam devid, AudioPostmixCallback callback, void *userdata)
Set a callback that fires when data is about to be fed to an audio device.
Definition: SDL3pp_audio.h:5285
const char * GetAudioDeviceName(AudioDeviceParam devid)
Get the human-readable name of a specific audio device.
Definition: SDL3pp_audio.h:3273
void GetAudioStreamFormat(AudioStreamParam stream, AudioSpec *src_spec, AudioSpec *dst_spec)
Query the current format of an audio stream.
Definition: SDL3pp_audio.h:3918
constexpr bool IsBigEndian() const
Determine if an AudioFormat represents bigendian data.
Definition: SDL3pp_audio.h:585
void BindAudioStreams(std::span< AudioStreamRef > streams)
Bind a list of audio streams to an audio device.
Definition: SDL3pp_audio.h:3728
constexpr bool IsAudioInt(AudioFormatRaw x)
Determine if an AudioFormat represents integer data.
Definition: SDL3pp_audio.h:643
void LockAudioStream(AudioStreamParam stream)
Lock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4797
constexpr Uint32 AUDIO_MASK_SIGNED
Mask of bits in an AudioFormat that contain the signed data flag.
Definition: SDL3pp_audio.h:234
constexpr Uint32 AUDIO_MASK_BITSIZE
Mask of bits in an AudioFormat that contains the format bit size.
Definition: SDL3pp_audio.h:204
bool AudioDevicePaused(AudioDeviceParam devid)
Use this function to query if an audio device is paused.
Definition: SDL3pp_audio.h:3574
void PauseDevice()
Use this function to pause audio playback on the audio device associated with an audio stream.
Definition: SDL3pp_audio.h:4710
void GetFormat(AudioSpec *src_spec, AudioSpec *dst_spec) const
Query the current format of an audio stream.
Definition: SDL3pp_audio.h:3925
constexpr AudioFormat AUDIO_S8
Signed 8-bit samples.
Definition: SDL3pp_audio.h:453
AudioDeviceRef GetAudioStreamDevice(AudioStreamParam stream)
Query an audio stream for its currently-bound device.
Definition: SDL3pp_audio.h:3824
constexpr Uint32 AUDIO_MASK_FLOAT
Mask of bits in an AudioFormat that contain the floating point flag.
Definition: SDL3pp_audio.h:214
SDL_AudioFormat AudioFormatRaw
Alias to raw representation for AudioFormat.
Definition: SDL3pp_audio.h:116
OwnArray< int > GetInputChannelMap() const
Get the current input channel map of an audio stream.
Definition: SDL3pp_audio.h:4126
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:5573
void SetFrequencyRatio(float ratio)
Change the frequency ratio of an audio stream.
Definition: SDL3pp_audio.h:4032
AudioStreamLock(AudioStreamRef resource)
Lock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4804
SDL_AudioSpec AudioSpec
Format specifier for audio data.
Definition: SDL3pp_audio.h:243
OwnArray< int > GetAudioStreamOutputChannelMap(AudioStreamParam stream)
Get the current output channel map of an audio stream.
Definition: SDL3pp_audio.h:4151
constexpr bool IsAudioUnsigned(AudioFormatRaw x)
Determine if an AudioFormat represents unsigned data.
Definition: SDL3pp_audio.h:662
int GetSilenceValueForFormat(AudioFormatRaw format)
Get the appropriate memset value for silencing an audio format.
Definition: SDL3pp_audio.h:5622
int GetData(TargetBytes buf)
Get converted/resampled data from the stream.
Definition: SDL3pp_audio.h:4552
OwnArray< Uint8 > LoadWAV(IOStreamParam src, AudioSpec *spec, bool closeio=false)
Load the audio data of a WAVE file into memory.
Definition: SDL3pp_audio.h:5425
int GetAudioStreamQueued(AudioStreamParam stream)
Get the number of bytes currently queued.
Definition: SDL3pp_audio.h:4625
constexpr AudioFormat AUDIO_S16
AUDIO_S16.
Definition: SDL3pp_audio.h:471
constexpr AudioFormat AUDIO_F32LE
32-bit floating point samples
Definition: SDL3pp_audio.h:465
void SetPutCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is added to an audio stream.
Definition: SDL3pp_audio.h:5048
OwnArray< int > GetOutputChannelMap() const
Get the current output channel map of an audio stream.
Definition: SDL3pp_audio.h:4159
float GetAudioDeviceGain(AudioDeviceParam devid)
Get the gain of an audio device.
Definition: SDL3pp_audio.h:3605
constexpr Uint16 GetByteSize() const
Retrieve the size, in bytes, from an AudioFormat.
Definition: SDL3pp_audio.h:544
void(SDLCALL *)(void *userdata, const AudioSpec *spec, float *buffer, int buflen) AudioPostmixCallback
A callback that fires when data is about to be fed to an audio device.
Definition: SDL3pp_audio.h:710
void ClearAudioStream(AudioStreamParam stream)
Clear any pending data in the stream.
Definition: SDL3pp_audio.h:4677
void SetAudioStreamGain(AudioStreamParam stream, float gain)
Change the gain of an audio stream.
Definition: SDL3pp_audio.h:4088
const char * GetCurrentAudioDriver()
Get the name of the current audio driver.
Definition: SDL3pp_audio.h:3181
void(SDLCALL *)(void *userdata, const void *buf, int buflen) AudioStreamDataCompleteCallback
A callback that fires for completed AudioStream.PutDataNoCopy() data.
Definition: SDL3pp_audio.h:1636
OwnArray< int > GetChannelMap() const
Get the current channel map of an audio device.
Definition: SDL3pp_audio.h:3352
int GetAvailable() const
Get the number of converted/resampled bytes available.
Definition: SDL3pp_audio.h:4585
float GetAudioStreamGain(AudioStreamParam stream)
Get the gain of an audio stream.
Definition: SDL3pp_audio.h:4056
void PutAudioStreamData(AudioStreamParam stream, SourceBytes buf)
Add data to the stream.
Definition: SDL3pp_audio.h:4324
float GetFrequencyRatio() const
Get the frequency ratio of an audio stream.
Definition: SDL3pp_audio.h:3997
void SetAudioStreamGetCallback(AudioStreamParam stream, AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is requested from an audio stream.
Definition: SDL3pp_audio.h:4884
constexpr bool IsAudioSigned(AudioFormatRaw x)
Determine if an AudioFormat represents signed data.
Definition: SDL3pp_audio.h:624
void ResumeDevice()
Use this function to unpause audio playback on the audio device associated with an audio stream.
Definition: SDL3pp_audio.h:4740
void SetAudioStreamOutputChannelMap(AudioStreamParam stream, std::span< int > chmap)
Set the current output channel map of an audio stream.
Definition: SDL3pp_audio.h:4286
const char * GetAudioFormatName(AudioFormatRaw format)
Get the human readable name of an audio format.
Definition: SDL3pp_audio.h:5599
AudioDevice OpenAudioDevice(AudioDeviceParam devid, OptionalRef< const AudioSpec > spec)
Open a specific audio device.
Definition: SDL3pp_audio.h:3427
AudioStreamLock Lock()
Lock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4802
bool IsAudioDevicePhysical(AudioDeviceParam devid)
Determine if an audio device is physical (instead of logical).
Definition: SDL3pp_audio.h:3456
bool Paused() const
Use this function to query if an audio device is paused.
Definition: SDL3pp_audio.h:3579
OwnArray< int > GetAudioStreamInputChannelMap(AudioStreamParam stream)
Get the current input channel map of an audio stream.
Definition: SDL3pp_audio.h:4118
void BindAudioStream(AudioStreamParam stream)
Bind a single audio stream to an audio device.
Definition: SDL3pp_audio.h:3756
void PutPlanarData(const void *const *channel_buffers, int num_channels, int num_samples)
Add data to the stream with each channel in a separate array.
Definition: SDL3pp_audio.h:4510
void SetAudioDeviceGain(AudioDeviceParam devid, float gain)
Change the gain of an audio device.
Definition: SDL3pp_audio.h:3647
constexpr AudioFormat AUDIO_U8
Unsigned 8-bit samples.
Definition: SDL3pp_audio.h:451
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:3972
bool DevicePaused() const
Use this function to query if an audio device associated with a stream is paused.
Definition: SDL3pp_audio.h:4767
int GetAudioStreamAvailable(AudioStreamParam stream)
Get the number of converted/resampled bytes available.
Definition: SDL3pp_audio.h:4580
SDL_AudioDeviceID AudioDeviceID
Alias to raw representation for AudioDevice.
Definition: SDL3pp_audio.h:125
constexpr Uint16 AudioBitSize(AudioFormatRaw x)
Retrieve the size, in bits, from an AudioFormat.
Definition: SDL3pp_audio.h:520
constexpr AudioFormat AUDIO_S32
AUDIO_S32.
Definition: SDL3pp_audio.h:473
void SetAudioStreamPutCallback(AudioStreamParam stream, AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is added to an audio stream.
Definition: SDL3pp_audio.h:4992
int GetSilenceValue() const
Get the appropriate memset value for silencing an audio format.
Definition: SDL3pp_audio.h:5627
constexpr bool IsAudioLittleEndian(AudioFormatRaw x)
Determine if an AudioFormat represents littleendian data.
Definition: SDL3pp_audio.h:602
constexpr AudioFormat AUDIO_S16LE
Signed 16-bit samples.
Definition: SDL3pp_audio.h:455
constexpr Uint32 AUDIO_MASK_BIG_ENDIAN
Mask of bits in an AudioFormat that contain the bigendian flag.
Definition: SDL3pp_audio.h:224
AudioSpec GetFormat(int *sample_frames=nullptr) const
Get the current audio format of a specific audio device.
Definition: SDL3pp_audio.h:3321
PropertiesRef GetProperties() const
Get the properties associated with an audio stream.
Definition: SDL3pp_audio.h:3887
float GetGain() const
Get the gain of an audio stream.
Definition: SDL3pp_audio.h:4061
void SetAudioStreamFrequencyRatio(AudioStreamParam stream, float ratio)
Change the frequency ratio of an audio stream.
Definition: SDL3pp_audio.h:4027
OwnArray< AudioDeviceRef > GetAudioRecordingDevices()
Get a list of currently-connected audio recording devices.
Definition: SDL3pp_audio.h:3242
void SetOutputChannelMap(std::span< int > chmap)
Set the current output channel map of an audio stream.
Definition: SDL3pp_audio.h:4293
void PutAudioStreamDataNoCopy(AudioStreamParam stream, SourceBytes buf, AudioStreamDataCompleteCallback callback, void *userdata)
Add external data to an audio stream without copying it.
Definition: SDL3pp_audio.h:4379
void reset()
Unlock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4836
void SetGetCallback(AudioStreamCallback callback, void *userdata)
Set a callback that runs when data is requested from an audio stream.
Definition: SDL3pp_audio.h:4936
AudioDeviceRef GetDevice() const
Query an audio stream for its currently-bound device.
Definition: SDL3pp_audio.h:3829
bool AudioStreamDevicePaused(AudioStreamParam stream)
Use this function to query if an audio device associated with a stream is paused.
Definition: SDL3pp_audio.h:4762
constexpr AudioFormat AUDIO_S32BE
As above, but big-endian byte order.
Definition: SDL3pp_audio.h:462
const char * GetAudioDriver(int index)
Use this function to get the name of a built in audio driver.
Definition: SDL3pp_audio.h:3162
bool IsPlayback() const
Determine if an audio device is a playback device (instead of recording).
Definition: SDL3pp_audio.h:3483
constexpr AudioDeviceID AUDIO_DEVICE_DEFAULT_RECORDING
A value used to request a default recording audio device.
Definition: SDL3pp_audio.h:1585
AudioStream OpenAudioDeviceStream(AudioDeviceParam devid, OptionalRef< const AudioSpec > spec, AudioStreamCallback callback=nullptr, void *userdata=nullptr)
Convenience function for straightforward audio init for the common case.
Definition: SDL3pp_audio.h:5141
constexpr AudioFormat DefineAudioFormat(bool sign, bool bigendian, bool flt, Uint16 size)
Define an AudioFormat value.
Definition: SDL3pp_audio.h:500
constexpr AudioDeviceID AUDIO_DEVICE_DEFAULT_PLAYBACK
A value used to request a default playback audio device.
Definition: SDL3pp_audio.h:1573
constexpr AudioFormat AUDIO_S32LE
32-bit integer samples
Definition: SDL3pp_audio.h:460
void SetAudioStreamFormat(AudioStreamParam stream, OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Change the input and output formats of an audio stream.
Definition: SDL3pp_audio.h:3965
void Close()
Close a previously-opened audio device.
Definition: SDL3pp_audio.h:3681
constexpr bool IsInt() const
Determine if an AudioFormat represents integer data.
Definition: SDL3pp_audio.h:645
void BindAudioStream(AudioDeviceParam devid, AudioStreamParam stream)
Bind a single audio stream to an audio device.
Definition: SDL3pp_audio.h:3751
OwnArray< int > GetAudioDeviceChannelMap(AudioDeviceParam devid)
Get the current channel map of an audio device.
Definition: SDL3pp_audio.h:3345
void MixAudio(Uint8 *dst, SourceBytes src, AudioFormat format, float volume)
Mix audio data in a specified format.
Definition: SDL3pp_audio.h:5497
AudioStream CreateAudioStream(OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
Create a new audio stream.
Definition: SDL3pp_audio.h:3854
void SetInputChannelMap(std::span< int > chmap)
Set the current input channel map of an audio stream.
Definition: SDL3pp_audio.h:4227
void Unbind()
Unbind a single audio stream from its audio device.
Definition: SDL3pp_audio.h:3804
SDL_AudioStream * AudioStreamRaw
Alias to raw representation for AudioStream.
Definition: SDL3pp_audio.h:161
const char * GetName() const
Get the human-readable name of a specific audio device.
Definition: SDL3pp_audio.h:3278
constexpr bool IsFloat() const
Determine if an AudioFormat represents floating point data.
Definition: SDL3pp_audio.h:563
constexpr AudioFormat AUDIO_F32BE
As above, but big-endian byte order.
Definition: SDL3pp_audio.h:468
constexpr AudioFormat AUDIO_F32
AUDIO_F32.
Definition: SDL3pp_audio.h:475
int GetNumAudioDrivers()
Use this function to get the number of built-in audio drivers.
Definition: SDL3pp_audio.h:3138
void BindAudioStreams(AudioDeviceParam devid, std::span< AudioStreamRef > streams)
Bind a list of audio streams to an audio device.
Definition: SDL3pp_audio.h:3719
void UnbindAudioStreams(std::span< AudioStreamRef > streams)
Unbind a list of audio streams from their audio devices.
Definition: SDL3pp_audio.h:3779
void SetAudioStreamInputChannelMap(AudioStreamParam stream, std::span< int > chmap)
Set the current input channel map of an audio stream.
Definition: SDL3pp_audio.h:4220
int GetQueued() const
Get the number of bytes currently queued.
Definition: SDL3pp_audio.h:4630
int GetAudioStreamData(AudioStreamParam stream, TargetBytes buf)
Get converted/resampled data from the stream.
Definition: SDL3pp_audio.h:4547
void UnbindAudioStream(AudioStreamParam stream)
Unbind a single audio stream from its audio device.
Definition: SDL3pp_audio.h:3799
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:5348
bool IsPhysical() const
Determine if an audio device is physical (instead of logical).
Definition: SDL3pp_audio.h:3461
void DestroyAudioStream(AudioStreamRaw stream)
Free an audio stream.
Definition: SDL3pp_audio.h:5077
constexpr bool IsSigned() const
Determine if an AudioFormat represents signed data.
Definition: SDL3pp_audio.h:626
constexpr bool IsAudioBigENDIAN(AudioFormatRaw x)
Determine if an AudioFormat represents bigendian data.
Definition: SDL3pp_audio.h:580
void SetGain(float gain)
Change the gain of an audio device.
Definition: SDL3pp_audio.h:3652
void UnlockAudioStream(AudioStreamParam stream)
Unlock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4825
constexpr AudioFormat AUDIO_S16BE
As above, but big-endian byte order.
Definition: SDL3pp_audio.h:457
void PutData(SourceBytes buf)
Add data to the stream.
Definition: SDL3pp_audio.h:4329
void PauseAudioStreamDevice(AudioStreamParam stream)
Use this function to pause audio playback on the audio device associated with an audio stream.
Definition: SDL3pp_audio.h:4705
OwnArray< AudioDeviceRef > GetAudioPlaybackDevices()
Get a list of currently-connected audio playback devices.
Definition: SDL3pp_audio.h:3210
float GetGain() const
Get the gain of an audio device.
Definition: SDL3pp_audio.h:3610
AudioSpec GetAudioDeviceFormat(AudioDeviceParam devid, int *sample_frames=nullptr)
Get the current audio format of a specific audio device.
Definition: SDL3pp_audio.h:3313
PropertiesRef GetAudioStreamProperties(AudioStreamParam stream)
Get the properties associated with an audio stream.
Definition: SDL3pp_audio.h:3882
void FlushAudioStream(AudioStreamParam stream)
Tell the stream that you're done sending data, and anything being buffered should be converted/resamp...
Definition: SDL3pp_audio.h:4652
void(SDLCALL *)(void *userdata, AudioStreamRaw stream, int additional_amount, int total_amount) AudioStreamCallback
A callback that fires when data passes through an AudioStream.
Definition: SDL3pp_audio.h:791
constexpr Uint16 AudioByteSize(AudioFormatRaw x)
Retrieve the size, in bytes, from an AudioFormat.
Definition: SDL3pp_audio.h:539
void PutDataNoCopy(SourceBytes buf, AudioStreamDataCompleteCallback callback, void *userdata)
Add external data to an audio stream without copying it.
Definition: SDL3pp_audio.h:4440
void Clear()
Clear any pending data in the stream.
Definition: SDL3pp_audio.h:4682
float GetAudioStreamFrequencyRatio(AudioStreamParam stream)
Get the frequency ratio of an audio stream.
Definition: SDL3pp_audio.h:3992
void Flush()
Tell the stream that you're done sending data, and anything being buffered should be converted/resamp...
Definition: SDL3pp_audio.h:4657
void PauseAudioDevice(AudioDeviceParam devid)
Use this function to pause audio playback on a specified device.
Definition: SDL3pp_audio.h:3516
void ResumeAudioDevice(AudioDeviceParam devid)
Use this function to unpause audio playback on a specified device.
Definition: SDL3pp_audio.h:3547
void SetGain(float gain)
Change the gain of an audio stream.
Definition: SDL3pp_audio.h:4093
void Unlock(AudioStreamLock &&lock)
Unlock an audio stream for serialized access.
Definition: SDL3pp_audio.h:4830
const char * GetName() const
Get the human readable name of an audio format.
Definition: SDL3pp_audio.h:5604
void PutAudioStreamPlanarData(AudioStreamParam stream, const void *const *channel_buffers, int num_channels, int num_samples)
Add data to the stream with each channel in a separate array.
Definition: SDL3pp_audio.h:4501
void ResumeAudioStreamDevice(AudioStreamParam stream)
Use this function to unpause audio playback on the audio device associated with an audio stream.
Definition: SDL3pp_audio.h:4735
std::function< void(const void *buf, int buflen)> AudioStreamDataCompleteCB
A callback that fires for completed AudioStream.PutDataNoCopy() data.
Definition: SDL3pp_audio.h:1665
void CloseAudioDevice(AudioDeviceID devid)
Close a previously-opened audio device.
Definition: SDL3pp_audio.h:3676
constexpr Uint16 GetBitSize() const
Retrieve the size, in bits, from an AudioFormat.
Definition: SDL3pp_audio.h:522
constexpr int AudioFrameSize(const AudioSpec &x)
Calculate the size of each audio frame (in bytes) from an AudioSpec.
Definition: SDL3pp_audio.h:1601
void Destroy()
Free an audio stream.
Definition: SDL3pp_audio.h:5082
constexpr bool IsUnsigned() const
Determine if an AudioFormat represents unsigned data.
Definition: SDL3pp_audio.h:667
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
::Uint8 Uint8
An unsigned 8-bit integer type.
Definition: SDL3pp_stdinc.h:289
::Uint16 Uint16
An unsigned 16-bit integer type.
Definition: SDL3pp_stdinc.h:315
Main include header for the SDL3pp library.
Safely wrap AudioDevice for non owning parameters.
Definition: SDL3pp_audio.h:132
AudioDeviceID value
parameter's AudioDeviceID
Definition: SDL3pp_audio.h:133
constexpr AudioDeviceParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_audio.h:142
constexpr auto operator<=>(const AudioDeviceParam &other) const =default
Comparison.
constexpr AudioDeviceParam(AudioDeviceID value)
Constructs from AudioDeviceID.
Definition: SDL3pp_audio.h:136
Semi-safe reference for AudioDevice.
Definition: SDL3pp_audio.h:1530
constexpr AudioDeviceRef(const AudioDeviceRef &other) noexcept=default
Copy constructor.
AudioDeviceRef(AudioDeviceID resource) noexcept
Constructs from AudioDeviceParam.
Definition: SDL3pp_audio.h:1552
AudioDeviceRef(AudioDeviceParam resource) noexcept
Constructs from AudioDeviceParam.
Definition: SDL3pp_audio.h:1540
~AudioDeviceRef()
Destructor.
Definition: SDL3pp_audio.h:1561
Safely wrap AudioStream for non owning parameters.
Definition: SDL3pp_audio.h:168
AudioStreamRaw value
parameter's AudioStreamRaw
Definition: SDL3pp_audio.h:169
constexpr auto operator<=>(const AudioStreamParam &other) const =default
Comparison.
constexpr AudioStreamParam(AudioStreamRaw value)
Constructs from AudioStreamRaw.
Definition: SDL3pp_audio.h:172
constexpr AudioStreamParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_audio.h:178
Semi-safe reference for AudioStream.
Definition: SDL3pp_audio.h:2973
constexpr AudioStreamRef(const AudioStreamRef &other) noexcept=default
Copy constructor.
~AudioStreamRef()
Destructor.
Definition: SDL3pp_audio.h:3004
AudioStreamRef(AudioStreamParam resource) noexcept
Constructs from AudioStreamParam.
Definition: SDL3pp_audio.h:2983
AudioStreamRef(AudioStreamRaw resource) noexcept
Constructs from AudioStreamParam.
Definition: SDL3pp_audio.h:2995
Definition: SDL3pp_callbackWrapper.h:20
Safely wrap IOStream for non owning parameters.
Definition: SDL3pp_iostream.h:34
Definition: SDL3pp_callbackWrapper.h:169
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716