| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | // Output audio to nowhere... |
| 24 | |
| 25 | #include "../SDL_sysaudio.h" |
| 26 | #include "SDL_dummyaudio.h" |
| 27 | |
| 28 | #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__) |
| 29 | #include <emscripten/emscripten.h> |
| 30 | #endif |
| 31 | |
| 32 | static bool DUMMYAUDIO_WaitDevice(SDL_AudioDevice *device) |
| 33 | { |
| 34 | SDL_Delay(device->hidden->io_delay); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | static bool DUMMYAUDIO_OpenDevice(SDL_AudioDevice *device) |
| 39 | { |
| 40 | device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden)); |
| 41 | if (!device->hidden) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if (!device->recording) { |
| 46 | device->hidden->mixbuf = (Uint8 *) SDL_malloc(device->buffer_size); |
| 47 | if (!device->hidden->mixbuf) { |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | device->hidden->io_delay = ((device->sample_frames * 1000) / device->spec.freq); |
| 53 | |
| 54 | const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DUMMY_TIMESCALE); |
| 55 | if (hint) { |
| 56 | double scale = SDL_atof(hint); |
| 57 | if (scale >= 0.0) { |
| 58 | device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // on Emscripten without threads, we just fire a repeating timer to consume audio. |
| 63 | #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__) |
| 64 | MAIN_THREAD_EM_ASM({ |
| 65 | var a = Module['SDL3'].dummy_audio; |
| 66 | if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); } |
| 67 | a.timers[$0] = setInterval(function() { dynCall('vi', $3, [$4]); }, ($1 / $2) * 1000); |
| 68 | }, device->recording ? 1 : 0, device->sample_frames, device->spec.freq, device->recording ? SDL_RecordingAudioThreadIterate : SDL_PlaybackAudioThreadIterate, device); |
| 69 | #endif |
| 70 | |
| 71 | return true; // we're good; don't change reported device format. |
| 72 | } |
| 73 | |
| 74 | static void DUMMYAUDIO_CloseDevice(SDL_AudioDevice *device) |
| 75 | { |
| 76 | if (device->hidden) { |
| 77 | // on Emscripten without threads, we just fire a repeating timer to consume audio. |
| 78 | #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__) |
| 79 | MAIN_THREAD_EM_ASM({ |
| 80 | var a = Module['SDL3'].dummy_audio; |
| 81 | if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); } |
| 82 | a.timers[$0] = undefined; |
| 83 | }, device->recording ? 1 : 0); |
| 84 | #endif |
| 85 | SDL_free(device->hidden->mixbuf); |
| 86 | SDL_free(device->hidden); |
| 87 | device->hidden = NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static Uint8 *DUMMYAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size) |
| 92 | { |
| 93 | return device->hidden->mixbuf; |
| 94 | } |
| 95 | |
| 96 | static int DUMMYAUDIO_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen) |
| 97 | { |
| 98 | // always return a full buffer of silence. |
| 99 | SDL_memset(buffer, device->silence_value, buflen); |
| 100 | return buflen; |
| 101 | } |
| 102 | |
| 103 | static bool DUMMYAUDIO_Init(SDL_AudioDriverImpl *impl) |
| 104 | { |
| 105 | impl->OpenDevice = DUMMYAUDIO_OpenDevice; |
| 106 | impl->CloseDevice = DUMMYAUDIO_CloseDevice; |
| 107 | impl->WaitDevice = DUMMYAUDIO_WaitDevice; |
| 108 | impl->GetDeviceBuf = DUMMYAUDIO_GetDeviceBuf; |
| 109 | impl->WaitRecordingDevice = DUMMYAUDIO_WaitDevice; |
| 110 | impl->RecordDevice = DUMMYAUDIO_RecordDevice; |
| 111 | |
| 112 | impl->OnlyHasDefaultPlaybackDevice = true; |
| 113 | impl->OnlyHasDefaultRecordingDevice = true; |
| 114 | impl->HasRecordingSupport = true; |
| 115 | |
| 116 | // on Emscripten without threads, we just fire a repeating timer to consume audio. |
| 117 | #if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__) |
| 118 | MAIN_THREAD_EM_ASM({ |
| 119 | if (typeof(Module['SDL3']) === 'undefined') { |
| 120 | Module['SDL3'] = {}; |
| 121 | } |
| 122 | Module['SDL3'].dummy_audio = {}; |
| 123 | Module['SDL3'].dummy_audio.timers = []; |
| 124 | Module['SDL3'].dummy_audio.timers[0] = undefined; |
| 125 | Module['SDL3'].dummy_audio.timers[1] = undefined; |
| 126 | }); |
| 127 | impl->ProvidesOwnCallbackThread = true; |
| 128 | #endif |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | AudioBootStrap DUMMYAUDIO_bootstrap = { |
| 134 | "dummy" , "SDL dummy audio driver" , DUMMYAUDIO_Init, true, false |
| 135 | }; |
| 136 | |