SDL3pp
A slim C++ wrapper for SDL3
|
Handle to a non owned audioDevice. More...
Public Member Functions | |
constexpr | AudioDeviceRef (const AudioDeviceRef &other) |
Copy constructor. | |
constexpr | AudioDeviceRef (AudioDeviceRef &&other) |
Move constructor. | |
constexpr | ~AudioDeviceRef ()=default |
Default constructor. | |
AudioDeviceRef & | operator= (AudioDeviceRef other) |
Assignment operator. | |
void | reset (SDL_AudioDeviceID newResource={}) |
Close a previously-opened audio device. | |
AudioDeviceBase (const AudioDeviceBase &devid, OptionalRef< const SDL_AudioSpec > spec) | |
Open a specific audio device. | |
![]() | |
AudioDeviceBase (const AudioDeviceBase &devid, OptionalRef< const SDL_AudioSpec > spec) | |
Open a specific audio device. | |
constexpr auto | operator<=> (const AudioDeviceBase &other) const |
Comparison. | |
const char * | GetName () const |
Get the human-readable name of a specific audio device. | |
AudioSpec | GetFormat (int *sample_frames=nullptr) const |
Get the current audio format of a specific audio device. | |
OwnArray< int > | GetChannelMap () const |
Get the current channel map of an audio device. | |
bool | IsPhysical () const |
Determine if an audio device is physical (instead of logical). | |
bool | IsPlayback () const |
Determine if an audio device is a playback device (instead of recording). | |
void | Pause () |
Use this function to pause audio playback on a specified device. | |
void | Resume () |
Use this function to unpause audio playback on a specified device. | |
bool | Paused () const |
Use this function to query if an audio device is paused. | |
float | GetGain () const |
Get the gain of an audio device. | |
void | SetGain (float gain) |
Change the gain of an audio device. | |
void | BindAudioStreams (std::span< AudioStreamRef > streams) |
Bind a list of audio streams to an audio device. | |
void | BindAudioStream (AudioStreamBase &stream) |
Bind a single audio stream to an audio device. | |
void | SetPostmixCallback (AudioPostmixCB callback) |
Set a callback that fires when data is about to be fed to an audio device. | |
void | SetPostmixCallback (AudioPostmixCallback callback, void *userdata) |
Set a callback that fires when data is about to be fed to an audio device. | |
constexpr | Resource (T resource={}) |
Constructs the underlying resource. | |
constexpr | Resource (std::nullptr_t) |
Equivalent to default ctor. | |
constexpr | Resource (std::nullopt_t) |
Equivalent to default ctor. | |
Resource (const Resource &other)=delete | |
Resource (Resource &&other)=delete | |
![]() | |
constexpr | Resource (SDL_AudioDeviceID resource={}) |
Constructs the underlying resource. | |
constexpr | Resource (std::nullptr_t) |
Equivalent to default ctor. | |
constexpr | Resource (std::nullopt_t) |
Equivalent to default ctor. | |
Resource (const Resource &other)=delete | |
Resource (Resource &&other)=delete | |
Resource & | operator= (const Resource &other)=delete |
Resource & | operator= (Resource &&other)=delete |
constexpr | operator bool () const |
True if contains a valid resource. | |
constexpr bool | operator== (const Resource &other) const=default |
Comparison. | |
constexpr bool | operator== (std::nullopt_t) const |
Comparison. | |
constexpr bool | operator== (std::nullptr_t) const |
Comparison. | |
constexpr SDL_AudioDeviceID | get () const |
Return contained resource;. | |
constexpr SDL_AudioDeviceID | release (SDL_AudioDeviceID newResource={}) |
Return contained resource and empties or replace value. | |
constexpr const SDL_AudioDeviceID | operator-> () const |
Access to fields. | |
constexpr SDL_AudioDeviceID | operator-> () |
Access to fields. | |
|
inline |
You can open both playback and recording devices through this function. Playback devices will take data from bound audio streams, mix it, and send it to the hardware. Recording devices will feed any bound audio streams with a copy of any incoming data.
An opened audio device starts out with no audio streams bound. To start audio playing, bind a stream and supply audio data to it. Unlike SDL2, there is no audio callback; you only bind audio streams and make sure they have data flowing into them (however, you can simulate SDL2's semantics fairly closely by using AudioStreamBase.AudioStreamBase instead of this function).
If you don't care about opening a specific device, pass a devid
of either AUDIO_DEVICE_DEFAULT_PLAYBACK
or AUDIO_DEVICE_DEFAULT_RECORDING
. In this case, SDL will try to pick the most reasonable default, and may also switch between physical devices seamlessly later, if the most reasonable default changes during the lifetime of this opened device (user changed the default in the OS's system preferences, the default got unplugged so the system jumped to a new default, the user plugged in headphones on a mobile device, etc). Unless you have a good reason to choose a specific device, this is probably what you want.
You may request a specific format for the audio device, but there is no promise the device will honor that request for several reasons. As such, it's only meant to be a hint as to what data your app will provide. Audio streams will accept data in whatever format you specify and manage conversion for you as appropriate. AudioDeviceBase.GetFormat can tell you the preferred format for the device before opening and the actual format the device is using after opening.
It's legal to open the same device ID more than once; each successful open will generate a new logical AudioDeviceBase that is managed separately from others on the same physical device. This allows libraries to open a device separately from the main app and bind its own streams without conflicting.
It is also legal to open a device ID returned by a previous call to this function; doing so just creates another logical device on the same physical device. This may be useful for making logical groupings of audio streams.
This function returns the opened device ID on success. This is a new, unique AudioDeviceBase that represents a logical device.
Some backends might offer arbitrary devices (for example, a networked audio protocol that can connect to an arbitrary server). For these, as a change from SDL2, you should open a default device ID and use an SDL hint to specify the target if you care, or otherwise let the backend figure out a reasonable default. Most backends don't offer anything like this, and often this would be an end user setting an environment variable for their custom need, and not something an application should specifically manage.
When done with an audio device, possibly at the end of the app's life, one should call AudioDeviceRef.reset() on the returned device id.
devid | the device instance id to open, or AUDIO_DEVICE_DEFAULT_PLAYBACK or AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable default device. |
spec | the requested device configuration. Can be nullptr to use reasonable defaults. |
Error | on failure. |
|
inline |
The application should close open audio devices once they are no longer needed.
This function may block briefly while pending audio data is played by the hardware, so that applications don't drop the last buffer of data they supplied if terminating immediately afterwards.