SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SDL::AudioStreamBase Struct Reference

The opaque handle that represents an audio stream. More...

Inheritance diagram for SDL::AudioStreamBase:
Inheritance graph
[legend]

Public Member Functions

 AudioStreamBase (OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
 Create a new audio stream.
 
 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.
 
 AudioStreamBase (const AudioDeviceBase &devid, OptionalRef< const AudioSpec > spec, AudioStreamCB callback)
 Convenience function for straightforward audio init for the common case.
 
PropertiesRef GetProperties () const
 Get the properties associated with an audio stream.
 
AudioSpec GetInputFormat () const
 Query the current input format of an audio stream.
 
AudioSpec GetOutputFormat () const
 Query the current output format of an audio stream.
 
void GetFormat (AudioSpec *src_spec, AudioSpec *dst_spec) const
 Query the current format of an audio stream.
 
void SetInputFormat (const AudioSpec &spec)
 Change the input format of an audio stream.
 
void SetOutputFormat (const AudioSpec &spec)
 Change the output format of an audio stream.
 
void SetFormat (OptionalRef< const AudioSpec > src_spec, OptionalRef< const AudioSpec > dst_spec)
 Change the input and output formats of an audio stream.
 
float GetFrequencyRatio () const
 Get the frequency ratio of an audio stream.
 
void SetFrequencyRatio (float ratio)
 Change the frequency ratio of an audio stream.
 
float GetGain () const
 Get the gain of an audio stream.
 
void SetGain (float gain)
 Change the gain of an audio stream.
 
OwnArray< int > GetInputChannelMap () const
 Get the current input channel map of an audio stream.
 
OwnArray< int > GetOutputChannelMap () const
 Get the current output channel map of an audio stream.
 
void SetInputChannelMap (std::span< int > chmap)
 Set the current input channel map of an audio stream.
 
void SetOutputChannelMap (std::span< int > chmap)
 Set the current output channel map of an audio stream.
 
void PutData (SourceBytes buf)
 Add data to the stream.
 
int GetData (TargetBytes buf)
 Get converted/resampled data from the stream.
 
int GetAvailable () const
 Get the number of converted/resampled bytes available.
 
int GetQueued () const
 Get the number of bytes currently queued.
 
void Flush ()
 Tell the stream that you're done sending data, and anything being buffered should be converted/resampled and made available immediately.
 
void Clear ()
 Clear any pending data in the stream.
 
void PauseDevice ()
 Use this function to pause audio playback on the audio device associated with an audio stream.
 
void ResumeDevice ()
 Use this function to unpause audio playback on the audio device associated with an audio stream.
 
bool DevicePaused () const
 Use this function to query if an audio device associated with a stream is paused.
 
AudioStreamLock Lock ()
 Lock an audio stream for serialized access.
 
void SetGetCallback (AudioStreamCB callback)
 Set a callback that runs when data is requested from an audio stream.
 
void SetGetCallback (AudioStreamCallback callback, void *userdata)
 Set a callback that runs when data is requested from an audio stream.
 
void SetPutCallback (AudioStreamCB callback)
 Set a callback that runs when data is added to an audio stream.
 
void SetPutCallback (AudioStreamCallback callback, void *userdata)
 Set a callback that runs when data is added to an audio stream.
 
void Bind (AudioDeviceBase &devid)
 Bind a single audio stream to an audio device.
 
void Unbind ()
 Unbind a single audio stream from its audio device.
 
AudioDeviceRef GetDevice () const
 Query an audio stream for its currently-bound 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
 
- Public Member Functions inherited from SDL::Resource< SDL_AudioStream * >
constexpr Resource (SDL_AudioStream * 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
 
Resourceoperator= (const Resource &other)=delete
 
Resourceoperator= (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_AudioStream * get () const
 Return contained resource;.
 
constexpr SDL_AudioStream * release (SDL_AudioStream * newResource={})
 Return contained resource and empties or replace value.
 
constexpr const SDL_AudioStream * operator-> () const
 Access to fields.
 
constexpr SDL_AudioStream * operator-> ()
 Access to fields.
 

Detailed Description

AudioStreamBase is an audio conversion interface.

Audio streams are the core of the SDL3 audio interface. You create one or more of them, bind them to an opened audio device, and feed data to them (or for recording, consume data from them).

Since
This struct is available since SDL 3.2.0.
See also
AudioStreamBase.AudioStreamBase
Category:
Resource
See also
AudioStream
AudioStreamRef

Constructor & Destructor Documentation

◆ AudioStreamBase() [1/3]

SDL::AudioStreamBase::AudioStreamBase ( OptionalRef< const AudioSpec src_spec,
OptionalRef< const AudioSpec dst_spec 
)
inline
Parameters
src_specthe format details of the input audio.
dst_specthe format details of the output audio.
Postcondition
a new audio stream on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.PutData
AudioStreamBase.GetData
AudioStreamBase.GetAvailable
AudioStreamBase.Flush
AudioStreamBase.Clear
AudioStreamBase.SetFormat

◆ AudioStreamBase() [2/3]

SDL::AudioStreamBase::AudioStreamBase ( const AudioDeviceBase devid,
OptionalRef< const AudioSpec spec = std::nullopt,
AudioStreamCallback  callback = nullptr,
void *  userdata = nullptr 
)
inline

If all your app intends to do is provide a single source of PCM audio, this function allows you to do all your audio setup in a single call.

This is also intended to be a clean means to migrate apps from SDL2.

This function will open an audio device, create a stream and bind it. Unlike other methods of setup, the audio device will be closed when this stream is destroyed, so the app can treat the returned AudioStreamBase as the only object needed to manage audio playback.

Also unlike other functions, the audio device begins paused. This is to map more closely to SDL2-style behavior, since there is no extra step here to bind a stream to begin audio flowing. The audio device should be resumed with AudioStreamBase.ResumeDevice(stream);

This function works with both playback and recording devices.

The spec parameter represents the app's side of the audio stream. That is, for recording audio, this will be the output format, and for playing audio, this will be the input format. If spec is nullptr, the system will choose the format, and the app can use AudioStreamBase.GetFormat() to obtain this information later.

If you don't care about opening a specific audio device, you can (and probably should), use AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and AUDIO_DEVICE_DEFAULT_RECORDING for recording.

One can optionally provide a callback function; if nullptr, the app is expected to queue audio data for playback (or unqueue audio data if capturing). Otherwise, the callback will begin to fire once the device is unpaused.

Destroying the returned stream with AudioStreamRef.reset will also close the audio device associated with this stream.

Parameters
devidan audio device to open, or AUDIO_DEVICE_DEFAULT_PLAYBACK or AUDIO_DEVICE_DEFAULT_RECORDING.
specthe audio stream's data format. Can be std::nullopt.
callbacka callback where the app will provide new data for playback, or receive new data for recording. Can be nullptr, in which case the app will need to call AudioStreamBase.PutData or AudioStreamBase.GetData as necessary.
userdataapp-controlled pointer passed to callback. Can be nullptr. Ignored if callback is nullptr.
Postcondition
an audio stream on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetDevice
AudioStreamBase.ResumeDevice

◆ AudioStreamBase() [3/3]

SDL::AudioStreamBase::AudioStreamBase ( const AudioDeviceBase devid,
OptionalRef< const AudioSpec spec,
AudioStreamCB  callback 
)
inline

If all your app intends to do is provide a single source of PCM audio, this function allows you to do all your audio setup in a single call.

This is also intended to be a clean means to migrate apps from SDL2.

This function will open an audio device, create a stream and bind it. Unlike other methods of setup, the audio device will be closed when this stream is destroyed, so the app can treat the returned AudioStreamBase as the only object needed to manage audio playback.

Also unlike other functions, the audio device begins paused. This is to map more closely to SDL2-style behavior, since there is no extra step here to bind a stream to begin audio flowing. The audio device should be resumed with AudioStreamBase.ResumeDevice(stream);

This function works with both playback and recording devices.

The spec parameter represents the app's side of the audio stream. That is, for recording audio, this will be the output format, and for playing audio, this will be the input format. If spec is nullptr, the system will choose the format, and the app can use AudioStreamBase.GetFormat() to obtain this information later.

If you don't care about opening a specific audio device, you can (and probably should), use AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and AUDIO_DEVICE_DEFAULT_RECORDING for recording.

One can optionally provide a callback function; if nullptr, the app is expected to queue audio data for playback (or unqueue audio data if capturing). Otherwise, the callback will begin to fire once the device is unpaused.

Destroying the returned stream with AudioStreamRef.reset will also close the audio device associated with this stream.

Parameters
devidan audio device to open, or AUDIO_DEVICE_DEFAULT_PLAYBACK or AUDIO_DEVICE_DEFAULT_RECORDING.
specthe audio stream's data format. Can be std::nullopt.
callbacka callback where the app will provide new data for playback, or receive new data for recording. Can not be nullptr.
Postcondition
an audio stream on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetDevice
AudioStreamBase.ResumeDevice

Member Function Documentation

◆ Bind()

void SDL::AudioStreamBase::Bind ( AudioDeviceBase devid)
inline

This is a convenience function, equivalent to calling AudioDeviceBase.BindAudioStreams(devid, &stream, 1).

Parameters
devidthe device to bind to.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioDeviceBase.BindAudioStreams
AudioDeviceBase.UnbindAudioStream
AudioStreamBase.GetDevice
AudioDeviceBase.BindAudioStream

◆ Clear()

void SDL::AudioStreamBase::Clear ( )
inline

This drops any queued data, so there will be nothing to read from the stream until more is added.

Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetAvailable
AudioStreamBase.GetData
AudioStreamBase.GetQueued
AudioStreamBase.PutData

◆ DevicePaused()

bool SDL::AudioStreamBase::DevicePaused ( ) const
inline

Unlike in SDL2, audio devices start in an unpaused state, since an app has to bind a stream before any audio will flow.

Returns
true if device is valid and paused, false otherwise.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.PauseDevice
AudioStreamBase.ResumeDevice

◆ Flush()

void SDL::AudioStreamBase::Flush ( )
inline

It is legal to add more data to a stream after flushing, but there may be audio gaps in the output. Generally this is intended to signal the end of input, so the complete output becomes available.

Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.PutData

◆ GetAvailable()

int SDL::AudioStreamBase::GetAvailable ( ) const
inline

The stream may be buffering data behind the scenes until it has enough to resample correctly, so this number might be lower than what you expect, or even be zero. Add more data or flush the stream if you need the data now.

If the stream has so much data that it would overflow an int, the return value is clamped to a maximum value, but no queued data is lost; if there are gigabytes of data queued, the app might need to read some of it with AudioStreamBase.GetData before this function's return value is no longer clamped.

Returns
the number of converted/resampled bytes available or -1 on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetData
AudioStreamBase.PutData

◆ GetData()

int SDL::AudioStreamBase::GetData ( TargetBytes  buf)
inline

The input/output data format/channels/samplerate is specified when creating the stream, and can be changed after creation by calling AudioStreamBase.SetFormat.

Note that any conversion and resampling necessary is done during this call, and AudioStreamBase.PutData simply queues unconverted data for later. This is different than SDL2, where that work was done while inputting new data to the stream and requesting the output just copied the converted data.

Parameters
bufa buffer to fill with audio data.
Returns
the number of bytes read from the stream or -1 on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.Clear
AudioStreamBase.GetAvailable
AudioStreamBase.PutData

◆ GetDevice()

AudioDeviceRef SDL::AudioStreamBase::GetDevice ( ) const
inline

This reports the audio device that an audio stream is currently bound to.

If not bound, or invalid, this returns zero, which is not a valid device ID.

Returns
the bound audio device, or 0 if not bound or invalid.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioDeviceBase.BindAudioStream
AudioDeviceBase.BindAudioStreams

◆ GetFormat()

void SDL::AudioStreamBase::GetFormat ( AudioSpec src_spec,
AudioSpec dst_spec 
) const
inline
Parameters
src_specwhere to store the input audio format; ignored if nullptr.
dst_specwhere to store the output audio format; ignored if nullptr.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetFormat

◆ GetFrequencyRatio()

float SDL::AudioStreamBase::GetFrequencyRatio ( ) const
inline
Returns
the frequency ratio of the stream or 0.0 on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetFrequencyRatio

◆ GetGain()

float SDL::AudioStreamBase::GetGain ( ) const
inline

The gain of a stream is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio streams default to a gain of 1.0f (no change in output).

Returns
the gain of the stream or -1.0f on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetGain

◆ GetInputChannelMap()

OwnArray< int > SDL::AudioStreamBase::GetInputChannelMap ( ) const
inline

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

Audio streams default to no remapping applied. This is represented by returning nullptr, and does not signify an error.

Returns
an array of the current channel mapping, with as many elements as the current output spec's channels, or nullptr if default.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetInputChannelMap

◆ GetInputFormat()

AudioSpec SDL::AudioStreamBase::GetInputFormat ( ) const
inline
Returns
the input audio format.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetFormat

◆ GetOutputChannelMap()

OwnArray< int > SDL::AudioStreamBase::GetOutputChannelMap ( ) const
inline

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

Audio streams default to no remapping applied. This is represented by returning nullptr, and does not signify an error.

Returns
an array of the current channel mapping, with as many elements as the current output spec's channels, or nullptr if default.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetInputChannelMap

◆ GetOutputFormat()

AudioSpec SDL::AudioStreamBase::GetOutputFormat ( ) const
inline
Returns
the output audio format.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetFormat

◆ GetProperties()

PropertiesRef SDL::AudioStreamBase::GetProperties ( ) const
inline
Returns
a valid property ID on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.

◆ GetQueued()

int SDL::AudioStreamBase::GetQueued ( ) const
inline

This is the number of bytes put into a stream as input, not the number that can be retrieved as output. Because of several details, it's not possible to calculate one number directly from the other. If you need to know how much usable data can be retrieved right now, you should use AudioStreamBase.GetAvailable() and not this function.

Note that audio streams can change their input format at any time, even if there is still data queued in a different format, so the returned byte count will not necessarily match the number of sample frames available. Users of this API should be aware of format changes they make when feeding a stream and plan accordingly.

Queued data is not converted until it is consumed by AudioStreamBase.GetData, so this value should be representative of the exact data that was put into the stream.

If the stream has so much data that it would overflow an int, the return value is clamped to a maximum value, but no queued data is lost; if there are gigabytes of data queued, the app might need to read some of it with AudioStreamBase.GetData before this function's return value is no longer clamped.

Returns
the number of bytes queued or -1 on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.PutData
AudioStreamBase.Clear

◆ PauseDevice()

void SDL::AudioStreamBase::PauseDevice ( )
inline

This function pauses audio processing for a given device. Any bound audio streams will not progress, and no audio will be generated. Pausing one device does not prevent other unpaused devices from running.

Pausing a device can be useful to halt all audio without unbinding all the audio streams. This might be useful while a game is paused, or a level is loading, etc.

Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.ResumeDevice

◆ PutData()

void SDL::AudioStreamBase::PutData ( SourceBytes  buf)
inline

This data must match the format/channels/samplerate specified in the latest call to AudioStreamBase.SetFormat, or the format specified when creating the stream if it hasn't been changed.

Note that this call simply copies the unconverted data for later. This is different than SDL2, where data was converted during the Put call and the Get call would just dequeue the previously-converted data.

Parameters
bufa span with the audio data to add.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.Clear
AudioStreamBase.Flush
AudioStreamBase.GetData
AudioStreamBase.GetQueued

◆ ResumeDevice()

void SDL::AudioStreamBase::ResumeDevice ( )
inline

This function unpauses audio processing for a given device that has previously been paused. Once unpaused, any bound audio streams will begin to progress again, and audio can be generated.

Remember, AudioStreamBase.AudioStreamBase opens device in a paused state, so this function call is required for audio playback to begin on such device.

Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.PauseDevice

◆ SetFormat()

void SDL::AudioStreamBase::SetFormat ( OptionalRef< const AudioSpec src_spec,
OptionalRef< const AudioSpec dst_spec 
)
inline

Future calls to and AudioStreamBase.GetAvailable and AudioStreamBase.GetData will reflect the new format, and future calls to AudioStreamBase.PutData must provide data in the new input formats.

Data that was previously queued in the stream will still be operated on in the format that was current when it was added, which is to say you can put the end of a sound file in one format to a stream, change formats for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

If a stream is bound to a device, then the format of the side of the stream bound to a device cannot be changed (src_spec for recording devices, dst_spec for playback devices). Attempts to make a change to this side will be ignored, but this will not report an error. The other side's format can be changed.

Parameters
src_specthe new format of the audio input; if std::nullopt, it is not changed.
dst_specthe new format of the audio output; if std::nullopt, it is not changed.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetFormat
AudioStreamBase.SetFrequencyRatio

◆ SetFrequencyRatio()

void SDL::AudioStreamBase::SetFrequencyRatio ( float  ratio)
inline

The frequency ratio is used to adjust the rate at which input data is consumed. Changing this effectively modifies the speed and pitch of the audio. A value greater than 1.0 will play the audio faster, and at a higher pitch. A value less than 1.0 will play the audio slower, and at a lower pitch.

This is applied during AudioStreamBase.GetData, and can be continuously changed to create various effects.

Parameters
ratiothe frequency ratio. 1.0 is normal speed. Must be between 0.01 and 100.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetFrequencyRatio
AudioStreamBase.SetFormat

◆ SetGain()

void SDL::AudioStreamBase::SetGain ( float  gain)
inline

The gain of a stream is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio streams default to a gain of 1.0f (no change in output).

This is applied during AudioStreamBase.GetData, and can be continuously changed to create various effects.

Parameters
gainthe gain. 1.0f is no change, 0.0f is silence.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetGain

◆ SetGetCallback() [1/2]

void SDL::AudioStreamBase::SetGetCallback ( AudioStreamCallback  callback,
void *  userdata 
)
inline

This callback is called before data is obtained from the stream, giving the callback the chance to add more on-demand.

The callback can (optionally) call AudioStreamBase.PutData() to add more audio to the stream during this call; if needed, the request that triggered this callback will obtain the new data immediately.

The callback's additional_amount argument is roughly how many bytes of unconverted data (in the stream's input format) is needed by the caller, although this may overestimate a little for safety. This takes into account how much is already in the stream and only asks for any extra necessary to resolve the request, which means the callback may be asked for zero bytes, and a different amount on each call.

The callback is not required to supply exact amounts; it is allowed to supply too much or too little or none at all. The caller will get what's available, up to the amount they requested, regardless of this callback's outcome.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a nullptr function turns off the callback.

Parameters
callbackthe new callback function to call when data is requested from the stream.
userdataan opaque pointer provided to the callback for its own personal use.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetPutCallback

◆ SetGetCallback() [2/2]

void SDL::AudioStreamBase::SetGetCallback ( AudioStreamCB  callback)
inline

This callback is called before data is obtained from the stream, giving the callback the chance to add more on-demand.

The callback can (optionally) call AudioStreamBase.PutData() to add more audio to the stream during this call; if needed, the request that triggered this callback will obtain the new data immediately.

The callback's additional_amount argument is roughly how many bytes of unconverted data (in the stream's input format) is needed by the caller, although this may overestimate a little for safety. This takes into account how much is already in the stream and only asks for any extra necessary to resolve the request, which means the callback may be asked for zero bytes, and a different amount on each call.

The callback is not required to supply exact amounts; it is allowed to supply too much or too little or none at all. The caller will get what's available, up to the amount they requested, regardless of this callback's outcome.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a nullptr function turns off the callback.

Parameters
callbackthe new callback function to call when data is requested from the stream.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetPutCallback

◆ SetInputChannelMap()

void SDL::AudioStreamBase::SetInputChannelMap ( std::span< int >  chmap)
inline

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

The input channel map reorders data that is added to a stream via AudioStreamBase.PutData. Future calls to AudioStreamBase.PutData must provide data in the new channel order.

Each item in the array represents an input channel, and its value is the channel that it should be remapped to. To reverse a stereo signal's left and right values, you'd have an array of { 1, 0 }. It is legal to remap multiple channels to the same thing, so { 1, 1 } would duplicate the right channel to both channels of a stereo signal. An element in the channel map set to -1 instead of a valid channel will mute that channel, setting it to a silence value.

You cannot change the number of channels through a channel map, just reorder/mute them.

Data that was previously queued in the stream will still be operated on in the order that was current when it was added, which is to say you can put the end of a sound file in one order to a stream, change orders for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

Audio streams default to no remapping applied. Passing a nullptr channel map is legal, and turns off remapping.

SDL will copy the channel map; the caller does not have to save this array after this call.

If count is not equal to the current number of channels in the audio stream's format, this will fail. This is a safety measure to make sure a race condition hasn't changed the format while this call is setting the channel map.

Unlike attempting to change the stream's format, the input channel map on a stream bound to a recording device is permitted to change at any time; any data added to the stream from the device after this call will have the new mapping, but previously-added data will still have the prior mapping.

Parameters
chmapthe new channel map, nullptr to reset to default.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running. Don't change the stream's format to have a different number of channels from a a different thread at the same time, though!
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetInputChannelMap

◆ SetInputFormat()

void SDL::AudioStreamBase::SetInputFormat ( const AudioSpec spec)
inline

Future calls to and AudioStreamBase.GetAvailable and AudioStreamBase.GetData will reflect the new format, and future calls to AudioStreamBase.PutData must provide data in the new input formats.

Data that was previously queued in the stream will still be operated on in the format that was current when it was added, which is to say you can put the end of a sound file in one format to a stream, change formats for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

If a stream is bound to a device, then the format of the side of the stream bound to a device cannot be changed (src_spec for recording devices, dst_spec for playback devices). Attempts to make a change to this side will be ignored, but this will not report an error. The other side's format can be changed.

Parameters
specthe new format of the audio input.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetFormat
AudioStreamBase.SetFrequencyRatio

◆ SetOutputChannelMap()

void SDL::AudioStreamBase::SetOutputChannelMap ( std::span< int >  chmap)
inline

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

The output channel map reorders data that leaving a stream via AudioStreamBase.GetData.

Each item in the array represents an input channel, and its value is the channel that it should be remapped to. To reverse a stereo signal's left and right values, you'd have an array of { 1, 0 }. It is legal to remap multiple channels to the same thing, so { 1, 1 } would duplicate the right channel to both channels of a stereo signal. An element in the channel map set to -1 instead of a valid channel will mute that channel, setting it to a silence value.

You cannot change the number of channels through a channel map, just reorder/mute them.

The output channel map can be changed at any time, as output remapping is applied during AudioStreamBase.GetData.

Audio streams default to no remapping applied. Passing a nullptr channel map is legal, and turns off remapping.

SDL will copy the channel map; the caller does not have to save this array after this call.

If count is not equal to the current number of channels in the audio stream's format, this will fail. This is a safety measure to make sure a race condition hasn't changed the format while this call is setting the channel map.

Unlike attempting to change the stream's format, the output channel map on a stream bound to a recording device is permitted to change at any time; any data added to the stream after this call will have the new mapping, but previously-added data will still have the prior mapping. When the channel map doesn't match the hardware's channel layout, SDL will convert the data before feeding it to the device for playback.

Parameters
chmapthe new channel map, nullptr to reset to default.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running. Don't change the stream's format to have a different number of channels from a a different thread at the same time, though!
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetInputChannelMap

◆ SetOutputFormat()

void SDL::AudioStreamBase::SetOutputFormat ( const AudioSpec spec)
inline

Future calls to and AudioStreamBase.GetAvailable and AudioStreamBase.GetData will reflect the new format, and future calls to AudioStreamBase.PutData must provide data in the new input formats.

Data that was previously queued in the stream will still be operated on in the format that was current when it was added, which is to say you can put the end of a sound file in one format to a stream, change formats for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

If a stream is bound to a device, then the format of the side of the stream bound to a device cannot be changed (src_spec for recording devices, dst_spec for playback devices). Attempts to make a change to this side will be ignored, but this will not report an error. The other side's format can be changed.

Parameters
specthe new format of the audio output.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread, as it holds a stream-specific mutex while running.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.GetFormat
AudioStreamBase.SetFrequencyRatio

◆ SetPutCallback() [1/2]

void SDL::AudioStreamBase::SetPutCallback ( AudioStreamCallback  callback,
void *  userdata 
)
inline

This callback is called after the data is added to the stream, giving the callback the chance to obtain it immediately.

The callback can (optionally) call AudioStreamBase.GetData() to obtain audio from the stream during this call.

The callback's additional_amount argument is how many bytes of converted data (in the stream's output format) was provided by the caller, although this may underestimate a little for safety. This value might be less than what is currently available in the stream, if data was already there, and might be less than the caller provided if the stream needs to keep a buffer to aid in resampling. Which means the callback may be provided with zero bytes, and a different amount on each call.

The callback may call AudioStreamBase.GetAvailable to see the total amount currently available to read from the stream, instead of the total provided by the current call.

The callback is not required to obtain all data. It is allowed to read less or none at all. Anything not read now simply remains in the stream for later access.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a nullptr function turns off the callback.

Parameters
callbackthe new callback function to call when data is added to the stream.
userdataan opaque pointer provided to the callback for its own personal use.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetGetCallback

◆ SetPutCallback() [2/2]

void SDL::AudioStreamBase::SetPutCallback ( AudioStreamCB  callback)
inline

This callback is called after the data is added to the stream, giving the callback the chance to obtain it immediately.

The callback can (optionally) call AudioStreamBase.GetData() to obtain audio from the stream during this call.

The callback's additional_amount argument is how many bytes of converted data (in the stream's output format) was provided by the caller, although this may underestimate a little for safety. This value might be less than what is currently available in the stream, if data was already there, and might be less than the caller provided if the stream needs to keep a buffer to aid in resampling. Which means the callback may be provided with zero bytes, and a different amount on each call.

The callback may call AudioStreamBase.GetAvailable to see the total amount currently available to read from the stream, instead of the total provided by the current call.

The callback is not required to obtain all data. It is allowed to read less or none at all. Anything not read now simply remains in the stream for later access.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a nullptr function turns off the callback.

Parameters
callbackthe new callback function to call when data is added to the stream.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioStreamBase.SetGetCallback

◆ Unbind()

void SDL::AudioStreamBase::Unbind ( )
inline

This is a convenience function, equivalent to calling UnbindAudioStreams(&stream, 1).

Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AudioDeviceBase.BindAudioStream
UnbindAudioStreams

The documentation for this struct was generated from the following file: