4#include <SDL3/SDL_audio.h>
5#include "SDL3pp_iostream.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
194#define SDL_AUDIO_MASK_BITSIZE (0xFFu)
203#define SDL_AUDIO_MASK_FLOAT (1u << 8)
213#define SDL_AUDIO_MASK_BIG_ENDIAN (1u << 12)
222#define SDL_AUDIO_MASK_SIGNED (1u << 15)
242 SDL_AudioFormat m_audioFormat;
251 : m_audioFormat(audioFormat)
278 constexpr AudioFormat(
bool sign,
bool bigendian,
bool flt, Uint16 size)
280 SDL_AudioFormat(SDL_DEFINE_AUDIO_FORMAT(sign, bigendian, flt, size)))
302 constexpr operator SDL_AudioFormat()
const {
return m_audioFormat; }
309 constexpr explicit operator bool()
const {
return m_audioFormat != 0; }
322 constexpr Uint16
GetBitSize() {
return SDL_AUDIO_BITSIZE(m_audioFormat); }
335 constexpr Uint16
GetByteSize() {
return SDL_AUDIO_BYTESIZE(m_audioFormat); }
348 constexpr bool IsFloat() {
return SDL_AUDIO_ISFLOAT(m_audioFormat); }
361 constexpr bool IsBigEndian() {
return SDL_AUDIO_ISBIGENDIAN(m_audioFormat); }
376 return SDL_AUDIO_ISLITTLEENDIAN(m_audioFormat);
390 constexpr bool IsSigned() {
return SDL_AUDIO_ISSIGNED(m_audioFormat); }
403 constexpr bool IsInt() {
return SDL_AUDIO_ISINT(m_audioFormat); }
416 constexpr bool IsUnsigned() {
return SDL_AUDIO_ISUNSIGNED(m_audioFormat); }
428 const char*
GetName()
const {
return SDL_GetAudioFormatName(m_audioFormat); }
445 return SDL_GetSilenceValueForFormat(m_audioFormat);
558 std::function<void(
const AudioSpec& spec, std::span<float> buffer)>;
578 return get() <=> other.
get();
594 const char*
GetName()
const {
return SDL_GetAudioDeviceName(
get()); }
630 CheckError(SDL_GetAudioDeviceFormat(
get(), &spec, sample_frames));
655 auto data = SDL_GetAudioDeviceChannelMap(
get(), &count);
771 bool Paused()
const {
return SDL_AudioDevicePaused(
get()); }
793 float GetGain()
const {
return SDL_GetAudioDeviceGain(
get()); }
984 CheckError(SDL_SetAudioPostmixCallback(
get(), callback, userdata));
1006 static void reset(SDL_AudioDeviceID resource)
1009 SDL_CloseAudioDevice(resource);
1163 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
1175 SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
1192 return SDL_AUDIO_FRAMESIZE(x);
1277 void(
AudioStreamRef stream,
int additional_amount,
int total_amount)>;
1381 CheckError(SDL_GetAudioStreamFormat(
get(), src_spec, dst_spec));
1484 CheckError(SDL_SetAudioStreamFormat(
get(), src_spec, dst_spec));
1502 return SDL_GetAudioStreamFrequencyRatio(
get());
1599 auto data = SDL_GetAudioStreamInputChannelMap(
get(), &count);
1600 if (!data)
return {};
1626 auto data = SDL_GetAudioStreamOutputChannelMap(
get(), &count);
1627 if (!data)
return {};
1690 SDL_SetAudioStreamInputChannelMap(
get(), chmap.data(), chmap.size()));
1750 SDL_SetAudioStreamOutputChannelMap(
get(), chmap.data(), chmap.size()));
2088 CheckError(SDL_SetAudioStreamGetCallback(
get(), callback, userdata));
2183 CheckError(SDL_SetAudioStreamPutCallback(
get(), callback, userdata));
2260 static void reset(SDL_AudioStream* resource)
2264 SDL_DestroyAudioStream(resource);
2367 stream->SetGetCallback(std::move(callback));
2369 stream->SetPutCallback(std::move(callback));
2436 void* userdata =
nullptr)
2439 CheckError(SDL_OpenAudioDeviceStream(devid, spec, callback, userdata)));
2617 return SDL_GetAudioDriver(index);
2636 return SDL_GetCurrentAudioDriver();
2667 auto data =
CheckError(SDL_GetAudioPlaybackDevices(&count));
2700 auto data =
CheckError(SDL_GetAudioRecordingDevices(&count));
2710 reinterpret_cast<SDL_AudioStream* const*
>(streams.data()),
2740 SDL_UnbindAudioStreams(
2741 reinterpret_cast<SDL_AudioStream* const*
>(streams.data()), streams.size());
2813 if (!SDL_LoadWAV_IO(src,
false, spec, &buf, &len))
return {};
2844 if (!SDL_LoadWAV(path, spec, &buf, &len))
return {};
2887 dst,
static_cast<const Uint8*
>(src.
data), format, src.
size_bytes, volume));
2928 MixAudio(
static_cast<Uint8*
>(dst.
data), src, format, volume);
2961 CheckError(SDL_ConvertAudioSamples(&src_spec,
2962 static_cast<const Uint8*
>(src_data.
data),
2977 auto cb = Wrapper::Wrap(
get(), std::move(callback));
2978 if (!SDL_SetAudioPostmixCallback(
2980 [](
void* userdata,
const AudioSpec* spec,
float* buffer,
int buflen) {
2981 Wrapper::Call(userdata, *spec, std::span{buffer, size_t(buflen)});
2984 Wrapper::release(
get());
2992 if (!SDL_SetAudioStreamGetCallback(
2995 SDL_AudioStream* stream,
2996 int additional_amount,
2998 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3000 Wrapper::Wrap(
get(), std::move(callback)))) {
3001 Wrapper::release(
get());
3009 if (!SDL_SetAudioStreamPutCallback(
3012 SDL_AudioStream* stream,
3013 int additional_amount,
3015 Wrapper::Call(userdata, stream, additional_amount, total_amount);
3017 Wrapper::Wrap(
get(), std::move(callback)))) {
3018 Wrapper::release(
get());
3023#pragma endregion impl
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