From 033793faed6a6895877890a176753465bbee6b2b Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 25 May 2024 23:33:39 -0400 Subject: [PATCH] audio: SDL_OpenAudioDeviceStream() now allows a NULL spec. --- include/SDL3/SDL_audio.h | 9 +++++++-- src/audio/SDL_audio.c | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/SDL3/SDL_audio.h b/include/SDL3/SDL_audio.h index 64dbff0bf..e0ee2e4ca 100644 --- a/include/SDL3/SDL_audio.h +++ b/include/SDL3/SDL_audio.h @@ -1324,7 +1324,9 @@ extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream) * * 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. + * audio, this will be the input format. If spec is NULL, the system will + * choose the format, and the app can use SDL_GetAudioStreamFormat() to + * obtain this information later. * * If you don't care about opening a specific audio device, you can (and * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_OUTPUT for playback and @@ -1335,9 +1337,12 @@ extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream) * capturing). Otherwise, the callback will begin to fire once the device is * unpaused. * + * Destroying the returned stream with SDL_DestroyAudioStream will also close + * the audio device associated with this stream. + * * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE. - * \param spec the audio stream's data format. Required. + * \param spec the audio stream's data format. Can be NULL. * \param callback A callback where the app will provide new data for * playback, or receive new data for capture. Can be NULL, in * which case the app will need to call SDL_PutAudioStreamData diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index c6645943e..b3b1bd80c 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -1976,6 +1976,13 @@ SDL_AudioStream *SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_Au SDL_assert(device != NULL); const SDL_bool iscapture = device->iscapture; + // if the app didn't request a format _at all_, just make a stream that does no conversion; they can query for it later. + SDL_AudioSpec tmpspec; + if (!spec) { + SDL_copyp(&tmpspec, &device->spec); + spec = &tmpspec; + } + if (iscapture) { stream = SDL_CreateAudioStream(&device->spec, spec); } else {