SDL: dummyaudio: single-threaded Emscripten support.

From d118af53a121f25207e126d4bbe2744624adb010 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sun, 25 Aug 2024 15:01:33 -0400
Subject: [PATCH] dummyaudio: single-threaded Emscripten support.

On Emscripten without pthreads, this would fail because SDL can't spin the
audio device thread, and the dummy backend didn't manage a thread itself.

With this patch, we use setInterval to fire the usual audio thread iterators
between iterations of the Emscripten mainloop on the main thread.

Fixes #10573.
---
 src/audio/dummy/SDL_dummyaudio.c | 36 ++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/audio/dummy/SDL_dummyaudio.c b/src/audio/dummy/SDL_dummyaudio.c
index 807004d3d1b6d..9984d78fe38ab 100644
--- a/src/audio/dummy/SDL_dummyaudio.c
+++ b/src/audio/dummy/SDL_dummyaudio.c
@@ -25,6 +25,10 @@
 #include "../SDL_sysaudio.h"
 #include "SDL_dummyaudio.h"
 
+#if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
+#include <emscripten/emscripten.h>
+#endif
+
 static int DUMMYAUDIO_WaitDevice(SDL_AudioDevice *device)
 {
     SDL_Delay(device->hidden->io_delay);
@@ -54,12 +58,30 @@ static int DUMMYAUDIO_OpenDevice(SDL_AudioDevice *device)
             device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale);
         }
     }
+
+    // on Emscripten without threads, we just fire a repeating timer to consume audio.
+    #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
+    MAIN_THREAD_EM_ASM({
+        var a = Module['SDL3'].dummy_audio;
+        if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); }
+        a.timers[$0] = setInterval(function() { dynCall('vi', $3, [$4]); }, ($1 / $2) * 1000);
+    }, device->recording ? 1 : 0, device->sample_frames, device->spec.freq, device->recording ? SDL_RecordingAudioThreadIterate : SDL_PlaybackAudioThreadIterate, device);
+    #endif
+
     return 0; // we're good; don't change reported device format.
 }
 
 static void DUMMYAUDIO_CloseDevice(SDL_AudioDevice *device)
 {
     if (device->hidden) {
+        // on Emscripten without threads, we just fire a repeating timer to consume audio.
+        #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
+        MAIN_THREAD_EM_ASM({
+            var a = Module['SDL3'].dummy_audio;
+            if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); }
+            a.timers[$0] = undefined;
+        }, device->recording ? 1 : 0);
+        #endif
         SDL_free(device->hidden->mixbuf);
         SDL_free(device->hidden);
         device->hidden = NULL;
@@ -91,6 +113,20 @@ static bool DUMMYAUDIO_Init(SDL_AudioDriverImpl *impl)
     impl->OnlyHasDefaultRecordingDevice = true;
     impl->HasRecordingSupport = true;
 
+    // on Emscripten without threads, we just fire a repeating timer to consume audio.
+    #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
+    MAIN_THREAD_EM_ASM({
+        if (typeof(Module['SDL3']) === 'undefined') {
+            Module['SDL3'] = {};
+        }
+        Module['SDL3'].dummy_audio = {};
+        Module['SDL3'].dummy_audio.timers = [];
+        Module['SDL3'].dummy_audio.timers[0] = undefined;
+        Module['SDL3'].dummy_audio.timers[1] = undefined;
+    });
+    impl->ProvidesOwnCallbackThread = true;
+    #endif
+
     return true;
 }