|
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 (AudioDeviceRef 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 from the underlying resource.
|
|
constexpr | Resource (const ResourceHandle< Resource< T > > auto &resource) |
| Constructs from pointer like.
|
|
constexpr | Resource (std::nullptr_t) |
| Equivalent to default ctor.
|
|
constexpr | Resource (std::nullopt_t) |
| Equivalent to default ctor.
|
|
constexpr | Resource (SDL_AudioStream * resource={}) |
| Constructs from the underlying resource.
|
|
constexpr | Resource (const ResourceHandle< Resource< SDL_AudioStream * > > auto &resource) |
| Constructs from pointer like.
|
|
constexpr | Resource (std::nullptr_t) |
| Equivalent to default ctor.
|
|
constexpr | Resource (std::nullopt_t) |
| Equivalent to default ctor.
|
|
constexpr | operator bool () const |
| True if contains a valid resource.
|
|
constexpr | operator value_type () const |
| Converts back to underlying type.
|
|
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 const SDL_AudioStream * | operator-> () const |
| Access to fields.
|
|
constexpr SDL_AudioStream * | operator-> () |
| Access to fields.
|
|
AudioStream is an audio conversion interface.
- It can handle resampling data in chunks without generating artifacts, when it doesn't have the complete buffer available.
- It can handle incoming data in any variable size.
- It can handle input/output format changes on the fly.
- It can remap audio channels between inputs and outputs.
- You push data as you have it, and pull it when you need it
- It can also function as a basic audio data queue even if you just have sound that needs to pass from one place to another.
- You can hook callbacks up to them when more data is added or requested, to manage data on-the-fly.
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.
- Category:
- Resource
- See also
- AudioStream.Create
-
AudioStream
int SDL::AudioStreamRef::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 AudioStreamRef.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 AudioStreamRef.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 AudioStreamRef.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
- AudioStreamRef.PutData
-
AudioStreamRef.Clear
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 AudioStreamRef.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
-
callback | the new callback function to call when data is requested from the stream. |
userdata | an opaque pointer provided to the callback for its own personal use. |
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AudioStreamRef.SetPutCallback
void SDL::AudioStreamRef::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 AudioStreamRef.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
-
callback | the new callback function to call when data is requested from the stream. |
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AudioStreamRef.SetPutCallback
void SDL::AudioStreamRef::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 AudioStreamRef.PutData. Future calls to AudioStreamRef.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
-
chmap | the new channel map, nullptr to reset to default. |
- Exceptions
-
- 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
- AudioStreamRef.SetInputChannelMap
void SDL::AudioStreamRef::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 AudioStreamRef.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 AudioStreamRef.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
-
chmap | the new channel map, nullptr to reset to default. |
- Exceptions
-
- 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
- AudioStreamRef.SetInputChannelMap
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 AudioStreamRef.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 AudioStreamRef.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
-
callback | the new callback function to call when data is added to the stream. |
userdata | an opaque pointer provided to the callback for its own personal use. |
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AudioStreamRef.SetGetCallback
void SDL::AudioStreamRef::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 AudioStreamRef.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 AudioStreamRef.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
-
callback | the new callback function to call when data is added to the stream. |
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AudioStreamRef.SetGetCallback