4#include <SDL3/SDL_audio.h>
5#include "SDL3pp_iostream.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
168#define SDL_AUDIO_MASK_BITSIZE (0xFFu)
177#define SDL_AUDIO_MASK_FLOAT (1u << 8)
187#define SDL_AUDIO_MASK_BIG_ENDIAN (1u << 12)
196#define SDL_AUDIO_MASK_SIGNED (1u << 15)
216 SDL_AudioFormat m_audioFormat;
225 : m_audioFormat(audioFormat)
252 constexpr AudioFormat(
bool sign,
bool bigendian,
bool flt, Uint16 size)
254 SDL_AudioFormat(SDL_DEFINE_AUDIO_FORMAT(sign, bigendian, flt, size)))
276 constexpr operator SDL_AudioFormat()
const {
return m_audioFormat; }
283 constexpr explicit operator bool()
const {
return m_audioFormat != 0; }
296 constexpr Uint16
GetBitSize() {
return SDL_AUDIO_BITSIZE(m_audioFormat); }
309 constexpr Uint16
GetByteSize() {
return SDL_AUDIO_BYTESIZE(m_audioFormat); }
322 constexpr bool IsFloat() {
return SDL_AUDIO_ISFLOAT(m_audioFormat); }
335 constexpr bool IsBigEndian() {
return SDL_AUDIO_ISBIGENDIAN(m_audioFormat); }
350 return SDL_AUDIO_ISLITTLEENDIAN(m_audioFormat);
364 constexpr bool IsSigned() {
return SDL_AUDIO_ISSIGNED(m_audioFormat); }
377 constexpr bool IsInt() {
return SDL_AUDIO_ISINT(m_audioFormat); }
390 constexpr bool IsUnsigned() {
return SDL_AUDIO_ISUNSIGNED(m_audioFormat); }
402 const char*
GetName()
const {
return SDL_GetAudioFormatName(m_audioFormat); }
419 return SDL_GetSilenceValueForFormat(m_audioFormat);
532 std::function<void(
const AudioSpec& spec, std::span<float> buffer)>;
633 return get() <=> other.
get();
649 const char*
GetName()
const {
return SDL_GetAudioDeviceName(
get()); }
685 CheckError(SDL_GetAudioDeviceFormat(
get(), &spec, sample_frames));
710 auto data = SDL_GetAudioDeviceChannelMap(
get(), &count);
826 bool Paused()
const {
return SDL_AudioDevicePaused(
get()); }
848 float GetGain()
const {
return SDL_GetAudioDeviceGain(
get()); }
1039 CheckError(SDL_SetAudioPostmixCallback(
get(), callback, userdata));
1101 void reset(SDL_AudioDeviceID newResource = {})
1104 SDL_CloseAudioDevice(
release(newResource));
1128 constexpr AudioDevice(
const AudioDevice& other) =
delete;
1160 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
1172 SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
1189 return SDL_AUDIO_FRAMESIZE(x);
1274 void(
AudioStreamRef stream,
int additional_amount,
int total_amount)>;
1395 void* userdata =
nullptr)
1397 SDL_OpenAudioDeviceStream(devid.
get(), spec, callback, userdata)))
1538 CheckError(SDL_GetAudioStreamFormat(
get(), src_spec, dst_spec));
1641 CheckError(SDL_SetAudioStreamFormat(
get(), src_spec, dst_spec));
1659 return SDL_GetAudioStreamFrequencyRatio(
get());
1756 auto data = SDL_GetAudioStreamInputChannelMap(
get(), &count);
1757 if (!data)
return {};
1783 auto data = SDL_GetAudioStreamOutputChannelMap(
get(), &count);
1784 if (!data)
return {};
1847 SDL_SetAudioStreamInputChannelMap(
get(), chmap.data(), chmap.size()));
1907 SDL_SetAudioStreamOutputChannelMap(
get(), chmap.data(), chmap.size()));
2244 CheckError(SDL_SetAudioStreamGetCallback(
get(), callback, userdata));
2339 CheckError(SDL_SetAudioStreamPutCallback(
get(), callback, userdata));
2457 void reset(SDL_AudioStream* newResource = {})
2461 SDL_DestroyAudioStream(
release(newResource));
2485 constexpr AudioStream(
const AudioStream& other) =
delete;
2631 return SDL_GetAudioDriver(index);
2650 return SDL_GetCurrentAudioDriver();
2681 auto data =
CheckError(SDL_GetAudioPlaybackDevices(&count));
2714 auto data =
CheckError(SDL_GetAudioRecordingDevices(&count));
2724 reinterpret_cast<SDL_AudioStream* const*
>(streams.data()),
2754 SDL_UnbindAudioStreams(
2755 reinterpret_cast<SDL_AudioStream* const*
>(streams.data()), streams.size());
2833 if (!SDL_LoadWAV_IO(src.
get(),
false, spec, &buf, &len))
return {};
2865 if (!SDL_LoadWAV(path, spec, &buf, &len))
return {};
2908 dst,
static_cast<const Uint8*
>(src.
data), format, src.
size_bytes, volume));
2949 MixAudio(
static_cast<Uint8*
>(dst.
data), src, format, volume);
2982 CheckError(SDL_ConvertAudioSamples(&src_spec,
2983 static_cast<const Uint8*
>(src_data.
data),
2997 auto cb = Wrapper::Wrap(
get(), std::move(callback));
2998 if (!SDL_SetAudioPostmixCallback(
3000 [](
void* userdata,
const AudioSpec* spec,
float* buffer,
int buflen) {
3001 Wrapper::Call(userdata, *spec, std::span{buffer, size_t(buflen)});
3004 Wrapper::release(
get());
3012 if (!SDL_SetAudioStreamGetCallback(
3015 SDL_AudioStream* stream,
3016 int additional_amount,
3018 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3020 Wrapper::Wrap(
get(), std::move(callback)))) {
3021 Wrapper::release(
get());
3029 if (!SDL_SetAudioStreamPutCallback(
3032 SDL_AudioStream* stream,
3033 int additional_amount,
3035 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3037 Wrapper::Wrap(
get(), std::move(callback)))) {
3038 Wrapper::release(
get());
3043#pragma endregion impl
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