https://github.com/libsdl-org/SDL/commit/4ba9c2eaded5f75c78642e603676c9955312e80c
From 4ba9c2eaded5f75c78642e603676c9955312e80c Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 6 Jul 2023 17:18:37 -0400
Subject: [PATCH] dummyaudio: Configurable delay, other SDL3 API fixes.
---
src/audio/dummy/SDL_dummyaudio.c | 41 +++++++++++++++++++++++---------
src/audio/dummy/SDL_dummyaudio.h | 6 +----
2 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/src/audio/dummy/SDL_dummyaudio.c b/src/audio/dummy/SDL_dummyaudio.c
index c8d10ef8f558..b9b84c773a15 100644
--- a/src/audio/dummy/SDL_dummyaudio.c
+++ b/src/audio/dummy/SDL_dummyaudio.c
@@ -25,33 +25,51 @@
#include "../SDL_audio_c.h"
#include "SDL_dummyaudio.h"
-/* !!! FIXME: add a dummy WaitDevice to simulate real audio better? */
+/* !!! FIXME: this should be an SDL hint, not an environment variable. */
+#define DUMMYENVR_IODELAY "SDL_DUMMYAUDIODELAY"
+
+static void DUMMYAUDIO_WaitDevice(SDL_AudioDevice *device)
+{
+ SDL_Delay(device->hidden->io_delay);
+}
static int DUMMYAUDIO_OpenDevice(SDL_AudioDevice *device)
{
- device->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(device->buffer_size);
+ const char *envr = SDL_getenv(DUMMYENVR_IODELAY);
+
+ device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden));
if (!device->hidden) {
return SDL_OutOfMemory();
}
- return 0; /* don't change reported device format. */
+
+ if (!device->iscapture) {
+ device->hidden->mixbuf = (Uint8 *) SDL_malloc(device->buffer_size);
+ if (!device->hidden->mixbuf) {
+ return SDL_OutOfMemory();
+ }
+ }
+
+ device->hidden->io_delay = envr ? SDL_atoi(envr) : ((device->sample_frames * 1000) / device->spec.freq);
+
+ return 0; /* we're good; don't change reported device format. */
}
static void DUMMYAUDIO_CloseDevice(SDL_AudioDevice *device)
{
- SDL_free(device->hidden);
- device->hidden = NULL;
+ if (device->hidden) {
+ SDL_free(device->hidden->mixbuf);
+ SDL_free(device->hidden);
+ device->hidden = NULL;
+ }
}
static Uint8 *DUMMYAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
{
- return (Uint8 *) device->hidden;
+ return device->hidden->mixbuf;
}
static int DUMMYAUDIO_CaptureFromDevice(SDL_AudioDevice *device, void *buffer, int buflen)
{
- /* Delay to make this sort of simulate real audio input. */
- SDL_Delay((device->sample_frames * 1000) / device->spec.freq);
-
/* always return a full buffer of silence. */
SDL_memset(buffer, device->silence_value, buflen);
return buflen;
@@ -59,17 +77,18 @@ static int DUMMYAUDIO_CaptureFromDevice(SDL_AudioDevice *device, void *buffer, i
static SDL_bool DUMMYAUDIO_Init(SDL_AudioDriverImpl *impl)
{
- /* Set the function pointers */
impl->OpenDevice = DUMMYAUDIO_OpenDevice;
impl->CloseDevice = DUMMYAUDIO_CloseDevice;
+ impl->WaitDevice = DUMMYAUDIO_WaitDevice;
impl->GetDeviceBuf = DUMMYAUDIO_GetDeviceBuf;
+ impl->WaitCaptureDevice = DUMMYAUDIO_WaitDevice;
impl->CaptureFromDevice = DUMMYAUDIO_CaptureFromDevice;
impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
impl->HasCaptureSupport = SDL_TRUE;
- return SDL_TRUE; /* this audio target is available. */
+ return SDL_TRUE;
}
AudioBootStrap DUMMYAUDIO_bootstrap = {
diff --git a/src/audio/dummy/SDL_dummyaudio.h b/src/audio/dummy/SDL_dummyaudio.h
index 78709ac00277..44323e2c32b4 100644
--- a/src/audio/dummy/SDL_dummyaudio.h
+++ b/src/audio/dummy/SDL_dummyaudio.h
@@ -25,15 +25,11 @@
#include "../SDL_sysaudio.h"
-/* !!! FIXME: none of this is actually used. Dump this whole file. */
-
struct SDL_PrivateAudioData
{
/* The file descriptor for the audio device */
Uint8 *mixbuf;
- Uint32 mixlen;
- Uint32 write_delay;
- Uint32 initial_calls;
+ Uint32 io_delay;
};
#endif /* SDL_dummyaudio_h_ */