Skip to content

Commit 608f706

Browse files
committed
audio: Added SDL_SetAudioIterationCallbacks().
1 parent 18a86ea commit 608f706

File tree

6 files changed

+127
-2
lines changed

6 files changed

+127
-2
lines changed

include/SDL3/SDL_audio.h

+78
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,84 @@ extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream)
19311931
*/
19321932
extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
19331933

1934+
/**
1935+
* A callback that fires around an audio device's processing work.
1936+
*
1937+
* This callback fires when a logical audio device is about to start
1938+
* accessing its bound audio streams, and fires again when it has
1939+
* finished accessing them. It covers the range of one "iteration" of
1940+
* the audio device.
1941+
*
1942+
* It can be useful to use this callback to update state that must
1943+
* apply to all bound audio streams atomically: to make sure state
1944+
* changes don't happen while half of the streams are already processed
1945+
* for the latest audio buffer.
1946+
*
1947+
* This callback should run as quickly as possible and not block for any
1948+
* significant time, as this callback delays submission of data to the audio
1949+
* device, which can cause audio playback problems. This callback delays all
1950+
* audio processing across a single physical audio device: all its logical
1951+
* devices and all bound audio streams. Use it carefully.
1952+
*
1953+
* \param userdata a pointer provided by the app through
1954+
* SDL_SetAudioPostmixCallback, for its own use.
1955+
* \param devid the audio device this callback is running for.
1956+
* \param start true if this is the start of the iteration, false if the end.
1957+
*
1958+
* \threadsafety This will run from a background thread owned by SDL. The
1959+
* application is responsible for locking resources the callback
1960+
* touches that need to be protected.
1961+
*
1962+
* \since This datatype is available since SDL 3.4.0.
1963+
*
1964+
* \sa SDL_SetAudioIterationCallbacks
1965+
*/
1966+
typedef void (SDLCALL *SDL_AudioIterationCallback)(void *userdata, SDL_AudioDeviceID devid, bool start);
1967+
1968+
/**
1969+
* Set callbacks that fire around a new iteration of audio device processing.
1970+
*
1971+
* Two callbacks are provided here: one that runs when a device is about to
1972+
* process its bound audio streams, and another that runs when the device has
1973+
* finished processing them.
1974+
*
1975+
* These callbacks can run at any time, and from any thread; if you need to
1976+
* serialize access to your app's data, you should provide and use a mutex or
1977+
* other synchronization device.
1978+
*
1979+
* Generally these callbacks are used to apply state that applies to multiple
1980+
* bound audio streams, with a guarantee that the audio device's thread isn't
1981+
* halfway through processing them. Generally a finer-grained lock through
1982+
* SDL_LockAudioStream() is more appropriate.
1983+
*
1984+
* The callbacks are extremely time-sensitive; the callback should do the
1985+
* least amount of work possible and return as quickly as it can. The longer
1986+
* the callback runs, the higher the risk of audio dropouts or other problems.
1987+
*
1988+
* This function will block until the audio device is in between iterations,
1989+
* so any existing callback that might be running will finish before this
1990+
* function sets the new callback and returns.
1991+
*
1992+
* Physical devices do not accept these callbacks, only logical devices
1993+
* created through SDL_OpenAudioDevice() can be.
1994+
*
1995+
* Setting a NULL callback function disables any previously-set callback.
1996+
* Either callback may be NULL, and the same callback is permitted to be used
1997+
* for both.
1998+
*
1999+
* \param devid the ID of an opened audio device.
2000+
* \param start a callback function to be called at the start of an iteration. Can be NULL.
2001+
* \param end a callback function to be called at the end of an iteration. Can be NULL.
2002+
* \param userdata app-controlled pointer passed to callback. Can be NULL.
2003+
* \returns true on success or false on failure; call SDL_GetError() for more
2004+
* information.
2005+
*
2006+
* \threadsafety It is safe to call this function from any thread.
2007+
*
2008+
* \since This function is available since SDL 3.4.0.
2009+
*/
2010+
extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioIterationCallbacks(SDL_AudioDeviceID devid, SDL_AudioIterationCallback start, SDL_AudioIterationCallback end, void *userdata);
2011+
19342012
/**
19352013
* A callback that fires when data is about to be fed to an audio device.
19362014
*

src/audio/SDL_audio.c

+39-2
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,20 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
11471147
// We should have updated this elsewhere if the format changed!
11481148
SDL_assert(SDL_AudioSpecsEqual(&stream->dst_spec, &device->spec, NULL, NULL));
11491149

1150-
const int br = SDL_GetAtomicInt(&logdev->paused) ? 0 : SDL_GetAudioStreamDataAdjustGain(stream, device_buffer, buffer_size, logdev->gain);
1150+
int br = 0;
1151+
1152+
if (!SDL_GetAtomicInt(&logdev->paused)) {
1153+
if (logdev->iteration_start) {
1154+
logdev->iteration_start(logdev->iteration_userdata, logdev->instance_id, true);
1155+
}
1156+
1157+
br = SDL_GetAudioStreamDataAdjustGain(stream, device_buffer, buffer_size, logdev->gain);
1158+
1159+
if (logdev->iteration_end) {
1160+
logdev->iteration_end(logdev->iteration_userdata, logdev->instance_id, false);
1161+
}
1162+
}
1163+
11511164
if (br < 0) { // Probably OOM. Kill the audio device; the whole thing is likely dying soon anyhow.
11521165
failed = true;
11531166
SDL_memset(device_buffer, device->silence_value, buffer_size); // just supply silence to the device before we die.
@@ -1185,6 +1198,10 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
11851198
SDL_memset(mix_buffer, '\0', work_buffer_size); // start with silence.
11861199
}
11871200

1201+
if (logdev->iteration_start) {
1202+
logdev->iteration_start(logdev->iteration_userdata, logdev->instance_id, true);
1203+
}
1204+
11881205
for (SDL_AudioStream *stream = logdev->bound_streams; stream; stream = stream->next_binding) {
11891206
// We should have updated this elsewhere if the format changed!
11901207
SDL_assert(SDL_AudioSpecsEqual(&stream->dst_spec, &outspec, NULL, NULL));
@@ -1207,6 +1224,10 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
12071224
}
12081225
}
12091226

1227+
if (logdev->iteration_end) {
1228+
logdev->iteration_end(logdev->iteration_userdata, logdev->instance_id, false);
1229+
}
1230+
12101231
if (postmix) {
12111232
SDL_assert(mix_buffer == device->postmix_buffer);
12121233
postmix(logdev->postmix_userdata, &outspec, mix_buffer, work_buffer_size);
@@ -1902,8 +1923,9 @@ bool SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallba
19021923
{
19031924
SDL_AudioDevice *device = NULL;
19041925
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid, &device);
1905-
bool result = true;
1926+
bool result = false;
19061927
if (logdev) {
1928+
result = true;
19071929
if (callback && !device->postmix_buffer) {
19081930
device->postmix_buffer = (float *)SDL_aligned_alloc(SDL_GetSIMDAlignment(), device->work_buffer_size);
19091931
if (!device->postmix_buffer) {
@@ -1922,6 +1944,21 @@ bool SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallba
19221944
return result;
19231945
}
19241946

1947+
bool SDL_SetAudioIterationCallbacks(SDL_AudioDeviceID devid, SDL_AudioIterationCallback iter_start, SDL_AudioIterationCallback iter_end, void *userdata)
1948+
{
1949+
SDL_AudioDevice *device = NULL;
1950+
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid, &device);
1951+
bool result = false;
1952+
if (logdev) {
1953+
logdev->iteration_start = iter_start;
1954+
logdev->iteration_end = iter_end;
1955+
logdev->iteration_userdata = userdata;
1956+
result = true;
1957+
}
1958+
ReleaseAudioDevice(device);
1959+
return result;
1960+
}
1961+
19251962
bool SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams)
19261963
{
19271964
const bool islogical = !(devid & (1<<1));

src/audio/SDL_sysaudio.h

+7
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ struct SDL_LogicalAudioDevice
264264
// true if device was opened with SDL_OpenAudioDeviceStream (so it forbids binding changes, etc).
265265
bool simplified;
266266

267+
// If non-NULL, callback into the app that alerts it to start/end of device iteration.
268+
SDL_AudioIterationCallback iteration_start;
269+
SDL_AudioIterationCallback iteration_end;
270+
271+
// App-supplied pointer for iteration callbacks.
272+
void *iteration_userdata;
273+
267274
// If non-NULL, callback into the app that lets them access the final postmix buffer.
268275
SDL_AudioPostmixCallback postmix;
269276

src/dynapi/SDL_dynapi.sym

+1
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ SDL3_0.0.0 {
12511251
SDL_GetGPUDeviceProperties;
12521252
SDL_CreateGPURenderer;
12531253
SDL_PutAudioStreamPlanarData;
1254+
SDL_SetAudioIterationCallbacks;
12541255
# extra symbols go here (don't modify this line)
12551256
local: *;
12561257
};

src/dynapi/SDL_dynapi_overrides.h

+1
Original file line numberDiff line numberDiff line change
@@ -1276,3 +1276,4 @@
12761276
#define SDL_GetGPUDeviceProperties SDL_GetGPUDeviceProperties_REAL
12771277
#define SDL_CreateGPURenderer SDL_CreateGPURenderer_REAL
12781278
#define SDL_PutAudioStreamPlanarData SDL_PutAudioStreamPlanarData_REAL
1279+
#define SDL_SetAudioIterationCallbacks SDL_SetAudioIterationCallbacks_REAL

src/dynapi/SDL_dynapi_procs.h

+1
Original file line numberDiff line numberDiff line change
@@ -1284,3 +1284,4 @@ SDL_DYNAPI_PROC(bool,SDL_GetRenderTextureAddressMode,(SDL_Renderer *a,SDL_Textur
12841284
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetGPUDeviceProperties,(SDL_GPUDevice *a),(a),return)
12851285
SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateGPURenderer,(SDL_Window *a,SDL_GPUShaderFormat b,SDL_GPUDevice **c),(a,b,c),return)
12861286
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamPlanarData,(SDL_AudioStream *a,const void * const*b,int c),(a,b,c),return)
1287+
SDL_DYNAPI_PROC(bool,SDL_SetAudioIterationCallbacks,(SDL_AudioDeviceID a,SDL_AudioIterationCallback b,SDL_AudioIterationCallback c,void *d),(a,b,c,d),return)

0 commit comments

Comments
 (0)