From 639b34294ffc20721c66db46e59e07d9100ac4b8 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 10 Sep 2020 20:30:31 +0800 Subject: *init --- ThirdParty/SDL2/src/SDL.c | 476 ++ ThirdParty/SDL2/src/SDL_assert.c | 441 ++ ThirdParty/SDL2/src/SDL_assert_c.h | 24 + ThirdParty/SDL2/src/SDL_dataqueue.c | 339 + ThirdParty/SDL2/src/SDL_dataqueue.h | 55 + ThirdParty/SDL2/src/SDL_error.c | 319 + ThirdParty/SDL2/src/SDL_error_c.h | 65 + ThirdParty/SDL2/src/SDL_hints.c | 236 + ThirdParty/SDL2/src/SDL_internal.h | 52 + ThirdParty/SDL2/src/SDL_log.c | 450 ++ ThirdParty/SDL2/src/atomic/SDL_atomic.c | 303 + ThirdParty/SDL2/src/atomic/SDL_spinlock.c | 152 + ThirdParty/SDL2/src/audio/SDL_audio.c | 1635 +++++ ThirdParty/SDL2/src/audio/SDL_audio_c.h | 79 + ThirdParty/SDL2/src/audio/SDL_audiocvt.c | 1673 +++++ ThirdParty/SDL2/src/audio/SDL_audiodev.c | 124 + ThirdParty/SDL2/src/audio/SDL_audiodev_c.h | 38 + ThirdParty/SDL2/src/audio/SDL_audiotypecvt.c | 829 +++ ThirdParty/SDL2/src/audio/SDL_mixer.c | 369 + ThirdParty/SDL2/src/audio/SDL_sysaudio.h | 211 + ThirdParty/SDL2/src/audio/SDL_wave.c | 694 ++ ThirdParty/SDL2/src/audio/SDL_wave.h | 77 + ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.c | 1030 +++ ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.h | 48 + .../SDL2/src/audio/android/SDL_androidaudio.c | 226 + .../SDL2/src/audio/android/SDL_androidaudio.h | 42 + ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.c | 365 + ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.h | 53 + .../SDL2/src/audio/coreaudio/SDL_coreaudio.h | 68 + .../SDL2/src/audio/coreaudio/SDL_coreaudio.m | 891 +++ .../SDL2/src/audio/directsound/SDL_directsound.c | 604 ++ .../SDL2/src/audio/directsound/SDL_directsound.h | 47 + ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.c | 207 + ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.h | 41 + ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.c | 320 + ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.h | 43 + ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.c | 65 + ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.h | 41 + .../src/audio/emscripten/SDL_emscriptenaudio.c | 379 + .../src/audio/emscripten/SDL_emscriptenaudio.h | 38 + ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.c | 335 + ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.h | 51 + .../SDL2/src/audio/fusionsound/SDL_fsaudio.c | 328 + .../SDL2/src/audio/fusionsound/SDL_fsaudio.h | 50 + ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.cc | 248 + ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.h | 38 + ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.c | 429 ++ ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.h | 42 + ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.c | 165 + ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.h | 43 + ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.c | 463 ++ ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.h | 56 + ThirdParty/SDL2/src/audio/netbsd/SDL_netbsdaudio.c | 412 ++ ThirdParty/SDL2/src/audio/netbsd/SDL_netbsdaudio.h | 48 + ThirdParty/SDL2/src/audio/paudio/SDL_paudio.c | 516 ++ ThirdParty/SDL2/src/audio/paudio/SDL_paudio.h | 48 + ThirdParty/SDL2/src/audio/psp/SDL_pspaudio.c | 181 + ThirdParty/SDL2/src/audio/psp/SDL_pspaudio.h | 45 + .../SDL2/src/audio/pulseaudio/SDL_pulseaudio.c | 782 +++ .../SDL2/src/audio/pulseaudio/SDL_pulseaudio.h | 52 + ThirdParty/SDL2/src/audio/qsa/SDL_qsa_audio.c | 666 ++ ThirdParty/SDL2/src/audio/qsa/SDL_qsa_audio.h | 57 + ThirdParty/SDL2/src/audio/sndio/SDL_sndioaudio.c | 382 + ThirdParty/SDL2/src/audio/sndio/SDL_sndioaudio.h | 49 + ThirdParty/SDL2/src/audio/sun/SDL_sunaudio.c | 419 ++ ThirdParty/SDL2/src/audio/sun/SDL_sunaudio.h | 47 + ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi.c | 779 ++ ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi.h | 85 + .../SDL2/src/audio/wasapi/SDL_wasapi_win32.c | 417 ++ .../SDL2/src/audio/wasapi/SDL_wasapi_winrt.cpp | 276 + ThirdParty/SDL2/src/audio/winmm/SDL_winmm.c | 457 ++ ThirdParty/SDL2/src/audio/winmm/SDL_winmm.h | 45 + ThirdParty/SDL2/src/core/android/SDL_android.c | 2171 ++++++ ThirdParty/SDL2/src/core/android/SDL_android.h | 112 + ThirdParty/SDL2/src/core/android/keyinfotable.h | 175 + ThirdParty/SDL2/src/core/linux/SDL_dbus.c | 335 + ThirdParty/SDL2/src/core/linux/SDL_dbus.h | 97 + ThirdParty/SDL2/src/core/linux/SDL_evdev.c | 698 ++ ThirdParty/SDL2/src/core/linux/SDL_evdev.h | 39 + ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd.c | 677 ++ ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd.h | 29 + .../src/core/linux/SDL_evdev_kbd_default_accents.h | 284 + .../src/core/linux/SDL_evdev_kbd_default_keymap.h | 4763 +++++++++++++ ThirdParty/SDL2/src/core/linux/SDL_fcitx.c | 373 + ThirdParty/SDL2/src/core/linux/SDL_fcitx.h | 40 + ThirdParty/SDL2/src/core/linux/SDL_ibus.c | 584 ++ ThirdParty/SDL2/src/core/linux/SDL_ibus.h | 58 + ThirdParty/SDL2/src/core/linux/SDL_ime.c | 152 + ThirdParty/SDL2/src/core/linux/SDL_ime.h | 40 + ThirdParty/SDL2/src/core/linux/SDL_udev.c | 553 ++ ThirdParty/SDL2/src/core/linux/SDL_udev.h | 121 + ThirdParty/SDL2/src/core/unix/SDL_poll.c | 87 + ThirdParty/SDL2/src/core/unix/SDL_poll.h | 34 + ThirdParty/SDL2/src/core/windows/SDL_directx.h | 111 + ThirdParty/SDL2/src/core/windows/SDL_windows.c | 230 + ThirdParty/SDL2/src/core/windows/SDL_windows.h | 75 + ThirdParty/SDL2/src/core/windows/SDL_xinput.c | 139 + ThirdParty/SDL2/src/core/windows/SDL_xinput.h | 177 + .../SDL2/src/core/winrt/SDL_winrtapp_common.cpp | 66 + .../SDL2/src/core/winrt/SDL_winrtapp_common.h | 31 + .../SDL2/src/core/winrt/SDL_winrtapp_direct3d.cpp | 855 +++ .../SDL2/src/core/winrt/SDL_winrtapp_direct3d.h | 92 + .../SDL2/src/core/winrt/SDL_winrtapp_xaml.cpp | 160 + ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_xaml.h | 33 + ThirdParty/SDL2/src/cpuinfo/SDL_cpuinfo.c | 777 ++ ThirdParty/SDL2/src/dynapi/SDL_dynapi.c | 325 + ThirdParty/SDL2/src/dynapi/SDL_dynapi.h | 67 + ThirdParty/SDL2/src/dynapi/SDL_dynapi_overrides.h | 671 ++ ThirdParty/SDL2/src/dynapi/SDL_dynapi_procs.h | 709 ++ ThirdParty/SDL2/src/dynapi/gendynapi.pl | 141 + ThirdParty/SDL2/src/events/SDL_clipboardevents.c | 46 + ThirdParty/SDL2/src/events/SDL_clipboardevents_c.h | 30 + ThirdParty/SDL2/src/events/SDL_dropevents.c | 98 + ThirdParty/SDL2/src/events/SDL_dropevents_c.h | 32 + ThirdParty/SDL2/src/events/SDL_events.c | 955 +++ ThirdParty/SDL2/src/events/SDL_events_c.h | 49 + ThirdParty/SDL2/src/events/SDL_gesture.c | 700 ++ ThirdParty/SDL2/src/events/SDL_gesture_c.h | 35 + ThirdParty/SDL2/src/events/SDL_keyboard.c | 1033 +++ ThirdParty/SDL2/src/events/SDL_keyboard_c.h | 70 + ThirdParty/SDL2/src/events/SDL_mouse.c | 1027 +++ ThirdParty/SDL2/src/events/SDL_mouse_c.h | 141 + ThirdParty/SDL2/src/events/SDL_quit.c | 152 + ThirdParty/SDL2/src/events/SDL_sysevents.h | 36 + ThirdParty/SDL2/src/events/SDL_touch.c | 375 + ThirdParty/SDL2/src/events/SDL_touch_c.h | 61 + ThirdParty/SDL2/src/events/SDL_windowevents.c | 228 + ThirdParty/SDL2/src/events/SDL_windowevents_c.h | 31 + ThirdParty/SDL2/src/events/blank_cursor.h | 33 + ThirdParty/SDL2/src/events/default_cursor.h | 114 + ThirdParty/SDL2/src/events/scancodes_darwin.h | 159 + ThirdParty/SDL2/src/events/scancodes_linux.h | 263 + ThirdParty/SDL2/src/events/scancodes_windows.h | 55 + ThirdParty/SDL2/src/events/scancodes_xfree86.h | 506 ++ ThirdParty/SDL2/src/file/SDL_rwops.c | 867 +++ .../SDL2/src/file/cocoa/SDL_rwopsbundlesupport.h | 30 + .../SDL2/src/file/cocoa/SDL_rwopsbundlesupport.m | 62 + .../src/filesystem/android/SDL_sysfilesystem.c | 62 + .../SDL2/src/filesystem/cocoa/SDL_sysfilesystem.m | 117 + .../SDL2/src/filesystem/dummy/SDL_sysfilesystem.c | 47 + .../src/filesystem/emscripten/SDL_sysfilesystem.c | 81 + .../SDL2/src/filesystem/haiku/SDL_sysfilesystem.cc | 110 + .../SDL2/src/filesystem/nacl/SDL_sysfilesystem.c | 43 + .../SDL2/src/filesystem/unix/SDL_sysfilesystem.c | 250 + .../src/filesystem/windows/SDL_sysfilesystem.c | 192 + .../src/filesystem/winrt/SDL_sysfilesystem.cpp | 233 + ThirdParty/SDL2/src/haptic/SDL_haptic.c | 849 +++ ThirdParty/SDL2/src/haptic/SDL_haptic_c.h | 25 + ThirdParty/SDL2/src/haptic/SDL_syshaptic.h | 208 + ThirdParty/SDL2/src/haptic/android/SDL_syshaptic.c | 357 + .../SDL2/src/haptic/android/SDL_syshaptic_c.h | 12 + ThirdParty/SDL2/src/haptic/darwin/SDL_syshaptic.c | 1417 ++++ .../SDL2/src/haptic/darwin/SDL_syshaptic_c.h | 26 + ThirdParty/SDL2/src/haptic/dummy/SDL_syshaptic.c | 186 + ThirdParty/SDL2/src/haptic/linux/SDL_syshaptic.c | 1162 +++ .../SDL2/src/haptic/windows/SDL_dinputhaptic.c | 1305 ++++ .../SDL2/src/haptic/windows/SDL_dinputhaptic_c.h | 47 + .../SDL2/src/haptic/windows/SDL_windowshaptic.c | 449 ++ .../SDL2/src/haptic/windows/SDL_windowshaptic_c.h | 88 + .../SDL2/src/haptic/windows/SDL_xinputhaptic.c | 487 ++ .../SDL2/src/haptic/windows/SDL_xinputhaptic_c.h | 47 + ThirdParty/SDL2/src/joystick/SDL_gamecontroller.c | 1871 +++++ .../SDL2/src/joystick/SDL_gamecontrollerdb.h | 232 + ThirdParty/SDL2/src/joystick/SDL_joystick.c | 1276 ++++ ThirdParty/SDL2/src/joystick/SDL_joystick_c.h | 62 + ThirdParty/SDL2/src/joystick/SDL_sysjoystick.h | 136 + .../SDL2/src/joystick/android/SDL_sysjoystick.c | 687 ++ .../SDL2/src/joystick/android/SDL_sysjoystick_c.h | 61 + ThirdParty/SDL2/src/joystick/bsd/SDL_sysjoystick.c | 691 ++ .../SDL2/src/joystick/darwin/SDL_sysjoystick.c | 837 +++ .../SDL2/src/joystick/darwin/SDL_sysjoystick_c.h | 73 + .../SDL2/src/joystick/dummy/SDL_sysjoystick.c | 127 + .../SDL2/src/joystick/emscripten/SDL_sysjoystick.c | 403 ++ .../src/joystick/emscripten/SDL_sysjoystick_c.h | 52 + .../SDL2/src/joystick/haiku/SDL_haikujoystick.cc | 266 + .../SDL2/src/joystick/iphoneos/SDL_sysjoystick.m | 795 +++ .../SDL2/src/joystick/iphoneos/SDL_sysjoystick_c.h | 59 + .../SDL2/src/joystick/linux/SDL_sysjoystick.c | 1049 +++ .../SDL2/src/joystick/linux/SDL_sysjoystick_c.h | 60 + ThirdParty/SDL2/src/joystick/psp/SDL_sysjoystick.c | 270 + ThirdParty/SDL2/src/joystick/sort_controllers.py | 66 + .../SDL2/src/joystick/steam/SDL_steamcontroller.c | 52 + .../SDL2/src/joystick/steam/SDL_steamcontroller.h | 33 + .../SDL2/src/joystick/windows/SDL_dinputjoystick.c | 992 +++ .../src/joystick/windows/SDL_dinputjoystick_c.h | 30 + .../SDL2/src/joystick/windows/SDL_mmjoystick.c | 461 ++ .../src/joystick/windows/SDL_windowsjoystick.c | 548 ++ .../src/joystick/windows/SDL_windowsjoystick_c.h | 93 + .../SDL2/src/joystick/windows/SDL_xinputjoystick.c | 522 ++ .../src/joystick/windows/SDL_xinputjoystick_c.h | 33 + ThirdParty/SDL2/src/libm/e_atan2.c | 134 + ThirdParty/SDL2/src/libm/e_fmod.c | 144 + ThirdParty/SDL2/src/libm/e_log.c | 152 + ThirdParty/SDL2/src/libm/e_log10.c | 106 + ThirdParty/SDL2/src/libm/e_pow.c | 343 + ThirdParty/SDL2/src/libm/e_rem_pio2.c | 161 + ThirdParty/SDL2/src/libm/e_sqrt.c | 457 ++ ThirdParty/SDL2/src/libm/k_cos.c | 82 + ThirdParty/SDL2/src/libm/k_rem_pio2.c | 299 + ThirdParty/SDL2/src/libm/k_sin.c | 65 + ThirdParty/SDL2/src/libm/k_tan.c | 118 + ThirdParty/SDL2/src/libm/math_libm.h | 40 + ThirdParty/SDL2/src/libm/math_private.h | 226 + ThirdParty/SDL2/src/libm/s_atan.c | 114 + ThirdParty/SDL2/src/libm/s_copysign.c | 29 + ThirdParty/SDL2/src/libm/s_cos.c | 73 + ThirdParty/SDL2/src/libm/s_fabs.c | 29 + ThirdParty/SDL2/src/libm/s_floor.c | 71 + ThirdParty/SDL2/src/libm/s_scalbn.c | 69 + ThirdParty/SDL2/src/libm/s_sin.c | 73 + ThirdParty/SDL2/src/libm/s_tan.c | 67 + ThirdParty/SDL2/src/loadso/dlopen/SDL_sysloadso.c | 88 + ThirdParty/SDL2/src/loadso/dummy/SDL_sysloadso.c | 54 + ThirdParty/SDL2/src/loadso/windows/SDL_sysloadso.c | 80 + .../SDL2/src/main/android/SDL_android_main.c | 7 + ThirdParty/SDL2/src/main/dummy/SDL_dummy_main.c | 28 + ThirdParty/SDL2/src/main/haiku/SDL_BApp.h | 399 ++ ThirdParty/SDL2/src/main/haiku/SDL_BeApp.cc | 157 + ThirdParty/SDL2/src/main/haiku/SDL_BeApp.h | 38 + ThirdParty/SDL2/src/main/nacl/SDL_nacl_main.c | 93 + ThirdParty/SDL2/src/main/psp/SDL_psp_main.c | 70 + .../SDL2/src/main/windows/SDL_windows_main.c | 207 + ThirdParty/SDL2/src/main/windows/version.rc | 38 + .../main/winrt/SDL2-WinRTResource_BlankCursor.cur | Bin 0 -> 326 bytes .../SDL2/src/main/winrt/SDL2-WinRTResources.rc | 3 + .../SDL2/src/main/winrt/SDL_winrt_main_NonXAML.cpp | 54 + ThirdParty/SDL2/src/power/SDL_power.c | 116 + ThirdParty/SDL2/src/power/SDL_syspower.h | 47 + ThirdParty/SDL2/src/power/android/SDL_syspower.c | 64 + .../SDL2/src/power/emscripten/SDL_syspower.c | 62 + ThirdParty/SDL2/src/power/haiku/SDL_syspower.c | 128 + ThirdParty/SDL2/src/power/linux/SDL_syspower.c | 642 ++ ThirdParty/SDL2/src/power/macosx/SDL_syspower.c | 192 + ThirdParty/SDL2/src/power/psp/SDL_syspower.c | 68 + ThirdParty/SDL2/src/power/uikit/SDL_syspower.h | 32 + ThirdParty/SDL2/src/power/uikit/SDL_syspower.m | 113 + ThirdParty/SDL2/src/power/windows/SDL_syspower.c | 76 + ThirdParty/SDL2/src/power/winrt/SDL_syspower.cpp | 44 + ThirdParty/SDL2/src/render/SDL_d3dmath.c | 136 + ThirdParty/SDL2/src/render/SDL_d3dmath.h | 72 + ThirdParty/SDL2/src/render/SDL_render.c | 2234 ++++++ ThirdParty/SDL2/src/render/SDL_sysrender.h | 204 + ThirdParty/SDL2/src/render/SDL_yuv_sw.c | 414 ++ ThirdParty/SDL2/src/render/SDL_yuv_sw_c.h | 67 + .../SDL2/src/render/direct3d/SDL_render_d3d.c | 1825 +++++ .../SDL2/src/render/direct3d/SDL_shaders_d3d.c | 274 + .../SDL2/src/render/direct3d/SDL_shaders_d3d.h | 34 + .../SDL2/src/render/direct3d11/SDL_render_d3d11.c | 2570 +++++++ .../src/render/direct3d11/SDL_render_winrt.cpp | 116 + .../SDL2/src/render/direct3d11/SDL_render_winrt.h | 40 + .../SDL2/src/render/direct3d11/SDL_shaders_d3d11.c | 1957 ++++++ .../SDL2/src/render/direct3d11/SDL_shaders_d3d11.h | 43 + .../SDL2/src/render/metal/SDL_render_metal.m | 1429 ++++ .../SDL2/src/render/metal/SDL_shaders_metal.metal | 109 + .../SDL2/src/render/metal/SDL_shaders_metal_ios.h | 1899 +++++ .../SDL2/src/render/metal/SDL_shaders_metal_osx.h | 1903 +++++ .../SDL2/src/render/metal/build-metal-shaders.sh | 18 + ThirdParty/SDL2/src/render/opengl/SDL_glfuncs.h | 478 ++ ThirdParty/SDL2/src/render/opengl/SDL_render_gl.c | 1690 +++++ ThirdParty/SDL2/src/render/opengl/SDL_shaders_gl.c | 524 ++ ThirdParty/SDL2/src/render/opengl/SDL_shaders_gl.h | 47 + .../SDL2/src/render/opengles/SDL_glesfuncs.h | 65 + .../SDL2/src/render/opengles/SDL_render_gles.c | 1290 ++++ .../SDL2/src/render/opengles2/SDL_gles2funcs.h | 80 + .../SDL2/src/render/opengles2/SDL_render_gles2.c | 2258 ++++++ .../SDL2/src/render/opengles2/SDL_shaders_gles2.c | 569 ++ .../SDL2/src/render/opengles2/SDL_shaders_gles2.h | 70 + ThirdParty/SDL2/src/render/psp/SDL_render_psp.c | 1028 +++ .../SDL2/src/render/software/SDL_blendfillrect.c | 336 + .../SDL2/src/render/software/SDL_blendfillrect.h | 27 + .../SDL2/src/render/software/SDL_blendline.c | 777 ++ .../SDL2/src/render/software/SDL_blendline.h | 27 + .../SDL2/src/render/software/SDL_blendpoint.c | 341 + .../SDL2/src/render/software/SDL_blendpoint.h | 27 + ThirdParty/SDL2/src/render/software/SDL_draw.h | 576 ++ ThirdParty/SDL2/src/render/software/SDL_drawline.c | 209 + ThirdParty/SDL2/src/render/software/SDL_drawline.h | 27 + .../SDL2/src/render/software/SDL_drawpoint.c | 114 + .../SDL2/src/render/software/SDL_drawpoint.h | 27 + .../SDL2/src/render/software/SDL_render_sw.c | 894 +++ .../SDL2/src/render/software/SDL_render_sw_c.h | 24 + ThirdParty/SDL2/src/render/software/SDL_rotate.c | 530 ++ ThirdParty/SDL2/src/render/software/SDL_rotate.h | 28 + ThirdParty/SDL2/src/stdlib/SDL_getenv.c | 315 + ThirdParty/SDL2/src/stdlib/SDL_iconv.c | 934 +++ ThirdParty/SDL2/src/stdlib/SDL_malloc.c | 5376 ++++++++++++++ ThirdParty/SDL2/src/stdlib/SDL_qsort.c | 534 ++ ThirdParty/SDL2/src/stdlib/SDL_stdlib.c | 1131 +++ ThirdParty/SDL2/src/stdlib/SDL_string.c | 1715 +++++ ThirdParty/SDL2/src/test/SDL_test_assert.c | 152 + ThirdParty/SDL2/src/test/SDL_test_common.c | 1783 +++++ ThirdParty/SDL2/src/test/SDL_test_compare.c | 117 + ThirdParty/SDL2/src/test/SDL_test_crc32.c | 166 + ThirdParty/SDL2/src/test/SDL_test_font.c | 3250 +++++++++ ThirdParty/SDL2/src/test/SDL_test_fuzzer.c | 534 ++ ThirdParty/SDL2/src/test/SDL_test_harness.c | 680 ++ ThirdParty/SDL2/src/test/SDL_test_imageBlit.c | 1559 ++++ ThirdParty/SDL2/src/test/SDL_test_imageBlitBlend.c | 2845 ++++++++ ThirdParty/SDL2/src/test/SDL_test_imageFace.c | 247 + .../SDL2/src/test/SDL_test_imagePrimitives.c | 514 ++ .../SDL2/src/test/SDL_test_imagePrimitivesBlend.c | 696 ++ ThirdParty/SDL2/src/test/SDL_test_log.c | 118 + ThirdParty/SDL2/src/test/SDL_test_md5.c | 338 + ThirdParty/SDL2/src/test/SDL_test_memory.c | 274 + ThirdParty/SDL2/src/test/SDL_test_random.c | 96 + ThirdParty/SDL2/src/thread/SDL_systhread.h | 70 + ThirdParty/SDL2/src/thread/SDL_thread.c | 503 ++ ThirdParty/SDL2/src/thread/SDL_thread_c.h | 95 + ThirdParty/SDL2/src/thread/generic/SDL_syscond.c | 220 + ThirdParty/SDL2/src/thread/generic/SDL_sysmutex.c | 165 + .../SDL2/src/thread/generic/SDL_sysmutex_c.h | 22 + ThirdParty/SDL2/src/thread/generic/SDL_syssem.c | 217 + ThirdParty/SDL2/src/thread/generic/SDL_systhread.c | 71 + .../SDL2/src/thread/generic/SDL_systhread_c.h | 26 + ThirdParty/SDL2/src/thread/generic/SDL_systls.c | 38 + ThirdParty/SDL2/src/thread/psp/SDL_syscond.c | 224 + ThirdParty/SDL2/src/thread/psp/SDL_sysmutex.c | 136 + ThirdParty/SDL2/src/thread/psp/SDL_sysmutex_c.h | 22 + ThirdParty/SDL2/src/thread/psp/SDL_syssem.c | 161 + ThirdParty/SDL2/src/thread/psp/SDL_systhread.c | 112 + ThirdParty/SDL2/src/thread/psp/SDL_systhread_c.h | 24 + ThirdParty/SDL2/src/thread/pthread/SDL_syscond.c | 158 + ThirdParty/SDL2/src/thread/pthread/SDL_sysmutex.c | 192 + .../SDL2/src/thread/pthread/SDL_sysmutex_c.h | 32 + ThirdParty/SDL2/src/thread/pthread/SDL_syssem.c | 209 + ThirdParty/SDL2/src/thread/pthread/SDL_systhread.c | 246 + .../SDL2/src/thread/pthread/SDL_systhread_c.h | 27 + ThirdParty/SDL2/src/thread/pthread/SDL_systls.c | 70 + ThirdParty/SDL2/src/thread/stdcpp/SDL_syscond.cpp | 164 + ThirdParty/SDL2/src/thread/stdcpp/SDL_sysmutex.cpp | 111 + ThirdParty/SDL2/src/thread/stdcpp/SDL_sysmutex_c.h | 30 + .../SDL2/src/thread/stdcpp/SDL_systhread.cpp | 168 + .../SDL2/src/thread/stdcpp/SDL_systhread_c.h | 26 + ThirdParty/SDL2/src/thread/windows/SDL_sysmutex.c | 110 + ThirdParty/SDL2/src/thread/windows/SDL_syssem.c | 152 + ThirdParty/SDL2/src/thread/windows/SDL_systhread.c | 258 + .../SDL2/src/thread/windows/SDL_systhread_c.h | 32 + ThirdParty/SDL2/src/thread/windows/SDL_systls.c | 72 + ThirdParty/SDL2/src/timer/SDL_timer.c | 373 + ThirdParty/SDL2/src/timer/SDL_timer_c.h | 34 + ThirdParty/SDL2/src/timer/dummy/SDL_systimer.c | 75 + ThirdParty/SDL2/src/timer/haiku/SDL_systimer.c | 80 + ThirdParty/SDL2/src/timer/psp/SDL_systimer.c | 91 + ThirdParty/SDL2/src/timer/unix/SDL_systimer.c | 232 + ThirdParty/SDL2/src/timer/windows/SDL_systimer.c | 200 + ThirdParty/SDL2/src/video/SDL_RLEaccel.c | 1581 +++++ ThirdParty/SDL2/src/video/SDL_RLEaccel_c.h | 31 + ThirdParty/SDL2/src/video/SDL_blit.c | 296 + ThirdParty/SDL2/src/video/SDL_blit.h | 553 ++ ThirdParty/SDL2/src/video/SDL_blit_0.c | 483 ++ ThirdParty/SDL2/src/video/SDL_blit_1.c | 552 ++ ThirdParty/SDL2/src/video/SDL_blit_A.c | 1388 ++++ ThirdParty/SDL2/src/video/SDL_blit_N.c | 2647 +++++++ ThirdParty/SDL2/src/video/SDL_blit_auto.c | 7419 ++++++++++++++++++++ ThirdParty/SDL2/src/video/SDL_blit_auto.h | 30 + ThirdParty/SDL2/src/video/SDL_blit_copy.c | 162 + ThirdParty/SDL2/src/video/SDL_blit_copy.h | 24 + ThirdParty/SDL2/src/video/SDL_blit_slow.c | 164 + ThirdParty/SDL2/src/video/SDL_blit_slow.h | 25 + ThirdParty/SDL2/src/video/SDL_bmp.c | 702 ++ ThirdParty/SDL2/src/video/SDL_clipboard.c | 90 + ThirdParty/SDL2/src/video/SDL_egl.c | 838 +++ ThirdParty/SDL2/src/video/SDL_egl_c.h | 149 + ThirdParty/SDL2/src/video/SDL_fillrect.c | 343 + ThirdParty/SDL2/src/video/SDL_pixels.c | 1125 +++ ThirdParty/SDL2/src/video/SDL_pixels_c.h | 40 + ThirdParty/SDL2/src/video/SDL_rect.c | 531 ++ ThirdParty/SDL2/src/video/SDL_rect_c.h | 25 + ThirdParty/SDL2/src/video/SDL_shape.c | 309 + ThirdParty/SDL2/src/video/SDL_shape_internals.h | 69 + ThirdParty/SDL2/src/video/SDL_stretch.c | 353 + ThirdParty/SDL2/src/video/SDL_surface.c | 1232 ++++ ThirdParty/SDL2/src/video/SDL_sysvideo.h | 459 ++ ThirdParty/SDL2/src/video/SDL_video.c | 4077 +++++++++++ ThirdParty/SDL2/src/video/SDL_vulkan_internal.h | 91 + ThirdParty/SDL2/src/video/SDL_vulkan_utils.c | 172 + ThirdParty/SDL2/src/video/SDL_yuv.c | 1834 +++++ ThirdParty/SDL2/src/video/SDL_yuv_c.h | 30 + .../SDL2/src/video/android/SDL_androidclipboard.c | 48 + .../SDL2/src/video/android/SDL_androidclipboard.h | 32 + .../SDL2/src/video/android/SDL_androidevents.c | 123 + .../SDL2/src/video/android/SDL_androidevents.h | 27 + ThirdParty/SDL2/src/video/android/SDL_androidgl.c | 62 + ThirdParty/SDL2/src/video/android/SDL_androidgl.h | 34 + .../SDL2/src/video/android/SDL_androidkeyboard.c | 391 ++ .../SDL2/src/video/android/SDL_androidkeyboard.h | 36 + .../SDL2/src/video/android/SDL_androidmessagebox.c | 37 + .../SDL2/src/video/android/SDL_androidmessagebox.h | 29 + .../SDL2/src/video/android/SDL_androidmouse.c | 117 + .../SDL2/src/video/android/SDL_androidmouse.h | 32 + .../SDL2/src/video/android/SDL_androidtouch.c | 151 + .../SDL2/src/video/android/SDL_androidtouch.h | 29 + .../SDL2/src/video/android/SDL_androidvideo.c | 254 + .../SDL2/src/video/android/SDL_androidvideo.h | 49 + .../SDL2/src/video/android/SDL_androidvulkan.c | 175 + .../SDL2/src/video/android/SDL_androidvulkan.h | 52 + .../SDL2/src/video/android/SDL_androidwindow.c | 152 + .../SDL2/src/video/android/SDL_androidwindow.h | 45 + .../SDL2/src/video/cocoa/SDL_cocoaclipboard.h | 36 + .../SDL2/src/video/cocoa/SDL_cocoaclipboard.m | 103 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoaevents.h | 32 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoaevents.m | 483 ++ .../SDL2/src/video/cocoa/SDL_cocoakeyboard.h | 36 + .../SDL2/src/video/cocoa/SDL_cocoakeyboard.m | 720 ++ .../SDL2/src/video/cocoa/SDL_cocoamessagebox.h | 29 + .../SDL2/src/video/cocoa/SDL_cocoamessagebox.m | 145 + .../SDL2/src/video/cocoa/SDL_cocoametalview.h | 63 + .../SDL2/src/video/cocoa/SDL_cocoametalview.m | 135 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoamodes.h | 46 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoamodes.m | 490 ++ ThirdParty/SDL2/src/video/cocoa/SDL_cocoamouse.h | 52 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoamouse.m | 467 ++ .../SDL2/src/video/cocoa/SDL_cocoamousetap.h | 34 + .../SDL2/src/video/cocoa/SDL_cocoamousetap.m | 279 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengl.h | 68 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengl.m | 428 ++ .../SDL2/src/video/cocoa/SDL_cocoaopengles.h | 49 + .../SDL2/src/video/cocoa/SDL_cocoaopengles.m | 132 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoashape.h | 45 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoashape.m | 113 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoavideo.h | 118 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoavideo.m | 253 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoavulkan.h | 55 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoavulkan.m | 231 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoawindow.h | 154 + ThirdParty/SDL2/src/video/cocoa/SDL_cocoawindow.m | 1877 +++++ .../SDL2/src/video/directfb/SDL_DirectFB_WM.c | 413 ++ .../SDL2/src/video/directfb/SDL_DirectFB_WM.h | 56 + .../SDL2/src/video/directfb/SDL_DirectFB_dyn.c | 117 + .../SDL2/src/video/directfb/SDL_DirectFB_dyn.h | 41 + .../SDL2/src/video/directfb/SDL_DirectFB_events.c | 748 ++ .../SDL2/src/video/directfb/SDL_DirectFB_events.h | 34 + .../SDL2/src/video/directfb/SDL_DirectFB_modes.c | 414 ++ .../SDL2/src/video/directfb/SDL_DirectFB_modes.h | 59 + .../SDL2/src/video/directfb/SDL_DirectFB_mouse.c | 389 + .../SDL2/src/video/directfb/SDL_DirectFB_mouse.h | 44 + .../SDL2/src/video/directfb/SDL_DirectFB_opengl.c | 332 + .../SDL2/src/video/directfb/SDL_DirectFB_opengl.h | 64 + .../SDL2/src/video/directfb/SDL_DirectFB_render.c | 1335 ++++ .../SDL2/src/video/directfb/SDL_DirectFB_render.h | 25 + .../SDL2/src/video/directfb/SDL_DirectFB_shape.c | 131 + .../SDL2/src/video/directfb/SDL_DirectFB_shape.h | 38 + .../SDL2/src/video/directfb/SDL_DirectFB_video.c | 418 ++ .../SDL2/src/video/directfb/SDL_DirectFB_video.h | 170 + .../SDL2/src/video/directfb/SDL_DirectFB_window.c | 565 ++ .../SDL2/src/video/directfb/SDL_DirectFB_window.h | 82 + ThirdParty/SDL2/src/video/dummy/SDL_nullevents.c | 41 + ThirdParty/SDL2/src/video/dummy/SDL_nullevents_c.h | 27 + .../SDL2/src/video/dummy/SDL_nullframebuffer.c | 89 + .../SDL2/src/video/dummy/SDL_nullframebuffer_c.h | 27 + ThirdParty/SDL2/src/video/dummy/SDL_nullvideo.c | 144 + ThirdParty/SDL2/src/video/dummy/SDL_nullvideo.h | 30 + .../src/video/emscripten/SDL_emscriptenevents.c | 716 ++ .../src/video/emscripten/SDL_emscriptenevents.h | 40 + .../video/emscripten/SDL_emscriptenframebuffer.c | 178 + .../video/emscripten/SDL_emscriptenframebuffer.h | 32 + .../src/video/emscripten/SDL_emscriptenmouse.c | 274 + .../src/video/emscripten/SDL_emscriptenmouse.h | 42 + .../src/video/emscripten/SDL_emscriptenopengles.c | 119 + .../src/video/emscripten/SDL_emscriptenopengles.h | 49 + .../src/video/emscripten/SDL_emscriptenvideo.c | 354 + .../src/video/emscripten/SDL_emscriptenvideo.h | 58 + ThirdParty/SDL2/src/video/haiku/SDL_BWin.h | 679 ++ ThirdParty/SDL2/src/video/haiku/SDL_bclipboard.cc | 95 + ThirdParty/SDL2/src/video/haiku/SDL_bclipboard.h | 33 + ThirdParty/SDL2/src/video/haiku/SDL_bevents.cc | 41 + ThirdParty/SDL2/src/video/haiku/SDL_bevents.h | 39 + .../SDL2/src/video/haiku/SDL_bframebuffer.cc | 259 + ThirdParty/SDL2/src/video/haiku/SDL_bframebuffer.h | 47 + ThirdParty/SDL2/src/video/haiku/SDL_bkeyboard.cc | 190 + ThirdParty/SDL2/src/video/haiku/SDL_bkeyboard.h | 44 + ThirdParty/SDL2/src/video/haiku/SDL_bmodes.cc | 333 + ThirdParty/SDL2/src/video/haiku/SDL_bmodes.h | 48 + ThirdParty/SDL2/src/video/haiku/SDL_bopengl.cc | 176 + ThirdParty/SDL2/src/video/haiku/SDL_bopengl.h | 55 + ThirdParty/SDL2/src/video/haiku/SDL_bvideo.cc | 178 + ThirdParty/SDL2/src/video/haiku/SDL_bvideo.h | 44 + ThirdParty/SDL2/src/video/haiku/SDL_bwindow.cc | 233 + ThirdParty/SDL2/src/video/haiku/SDL_bwindow.h | 55 + ThirdParty/SDL2/src/video/khronos/EGL/egl.h | 303 + ThirdParty/SDL2/src/video/khronos/EGL/eglext.h | 1241 ++++ .../SDL2/src/video/khronos/EGL/eglplatform.h | 132 + ThirdParty/SDL2/src/video/khronos/GLES2/gl2.h | 675 ++ ThirdParty/SDL2/src/video/khronos/GLES2/gl2ext.h | 3505 +++++++++ .../SDL2/src/video/khronos/GLES2/gl2platform.h | 38 + .../SDL2/src/video/khronos/KHR/khrplatform.h | 284 + .../SDL2/src/video/khronos/vulkan/vk_platform.h | 120 + ThirdParty/SDL2/src/video/khronos/vulkan/vulkan.h | 6458 +++++++++++++++++ ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmdyn.c | 171 + ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmdyn.h | 53 + .../SDL2/src/video/kmsdrm/SDL_kmsdrmevents.c | 42 + .../SDL2/src/video/kmsdrm/SDL_kmsdrmevents.h | 31 + ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmmouse.c | 501 ++ ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmmouse.h | 45 + .../SDL2/src/video/kmsdrm/SDL_kmsdrmopengles.c | 189 + .../SDL2/src/video/kmsdrm/SDL_kmsdrmopengles.h | 48 + ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmsym.h | 99 + ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmvideo.c | 664 ++ ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmvideo.h | 124 + ThirdParty/SDL2/src/video/mir/SDL_mirdyn.c | 170 + ThirdParty/SDL2/src/video/mir/SDL_mirdyn.h | 53 + ThirdParty/SDL2/src/video/mir/SDL_mirevents.c | 321 + ThirdParty/SDL2/src/video/mir/SDL_mirevents.h | 37 + ThirdParty/SDL2/src/video/mir/SDL_mirframebuffer.c | 134 + ThirdParty/SDL2/src/video/mir/SDL_mirframebuffer.h | 47 + ThirdParty/SDL2/src/video/mir/SDL_mirmouse.c | 292 + ThirdParty/SDL2/src/video/mir/SDL_mirmouse.h | 37 + ThirdParty/SDL2/src/video/mir/SDL_miropengl.c | 78 + ThirdParty/SDL2/src/video/mir/SDL_miropengl.h | 53 + ThirdParty/SDL2/src/video/mir/SDL_mirsym.h | 143 + ThirdParty/SDL2/src/video/mir/SDL_mirvideo.c | 423 ++ ThirdParty/SDL2/src/video/mir/SDL_mirvideo.h | 49 + ThirdParty/SDL2/src/video/mir/SDL_mirvulkan.c | 176 + ThirdParty/SDL2/src/video/mir/SDL_mirvulkan.h | 52 + ThirdParty/SDL2/src/video/mir/SDL_mirwindow.c | 374 + ThirdParty/SDL2/src/video/mir/SDL_mirwindow.h | 93 + ThirdParty/SDL2/src/video/nacl/SDL_naclevents.c | 438 ++ ThirdParty/SDL2/src/video/nacl/SDL_naclevents_c.h | 30 + ThirdParty/SDL2/src/video/nacl/SDL_naclglue.c | 24 + ThirdParty/SDL2/src/video/nacl/SDL_naclopengles.c | 174 + ThirdParty/SDL2/src/video/nacl/SDL_naclopengles.h | 38 + ThirdParty/SDL2/src/video/nacl/SDL_naclvideo.c | 183 + ThirdParty/SDL2/src/video/nacl/SDL_naclvideo.h | 67 + ThirdParty/SDL2/src/video/nacl/SDL_naclwindow.c | 79 + ThirdParty/SDL2/src/video/nacl/SDL_naclwindow.h | 32 + ThirdParty/SDL2/src/video/pandora/SDL_pandora.c | 838 +++ ThirdParty/SDL2/src/video/pandora/SDL_pandora.h | 101 + .../SDL2/src/video/pandora/SDL_pandora_events.c | 38 + .../SDL2/src/video/pandora/SDL_pandora_events.h | 25 + ThirdParty/SDL2/src/video/psp/SDL_pspevents.c | 290 + ThirdParty/SDL2/src/video/psp/SDL_pspevents_c.h | 31 + ThirdParty/SDL2/src/video/psp/SDL_pspgl.c | 210 + ThirdParty/SDL2/src/video/psp/SDL_pspgl_c.h | 54 + ThirdParty/SDL2/src/video/psp/SDL_pspmouse.c | 41 + ThirdParty/SDL2/src/video/psp/SDL_pspmouse_c.h | 24 + ThirdParty/SDL2/src/video/psp/SDL_pspvideo.c | 333 + ThirdParty/SDL2/src/video/psp/SDL_pspvideo.h | 102 + ThirdParty/SDL2/src/video/qnx/gl.c | 285 + ThirdParty/SDL2/src/video/qnx/keyboard.c | 133 + ThirdParty/SDL2/src/video/qnx/sdl_qnx.h | 48 + ThirdParty/SDL2/src/video/qnx/video.c | 364 + .../SDL2/src/video/raspberry/SDL_rpievents.c | 45 + .../SDL2/src/video/raspberry/SDL_rpievents_c.h | 31 + ThirdParty/SDL2/src/video/raspberry/SDL_rpimouse.c | 386 + ThirdParty/SDL2/src/video/raspberry/SDL_rpimouse.h | 43 + .../SDL2/src/video/raspberry/SDL_rpiopengles.c | 71 + .../SDL2/src/video/raspberry/SDL_rpiopengles.h | 49 + ThirdParty/SDL2/src/video/raspberry/SDL_rpivideo.c | 442 ++ ThirdParty/SDL2/src/video/raspberry/SDL_rpivideo.h | 104 + ThirdParty/SDL2/src/video/sdlgenblit.pl | 535 ++ .../SDL2/src/video/uikit/SDL_uikitappdelegate.h | 47 + .../SDL2/src/video/uikit/SDL_uikitappdelegate.m | 539 ++ .../SDL2/src/video/uikit/SDL_uikitclipboard.h | 35 + .../SDL2/src/video/uikit/SDL_uikitclipboard.m | 111 + ThirdParty/SDL2/src/video/uikit/SDL_uikitevents.h | 30 + ThirdParty/SDL2/src/video/uikit/SDL_uikitevents.m | 73 + .../SDL2/src/video/uikit/SDL_uikitmessagebox.h | 31 + .../SDL2/src/video/uikit/SDL_uikitmessagebox.m | 206 + .../SDL2/src/video/uikit/SDL_uikitmetalview.h | 58 + .../SDL2/src/video/uikit/SDL_uikitmetalview.m | 124 + ThirdParty/SDL2/src/video/uikit/SDL_uikitmodes.h | 50 + ThirdParty/SDL2/src/video/uikit/SDL_uikitmodes.m | 324 + .../SDL2/src/video/uikit/SDL_uikitopengles.h | 40 + .../SDL2/src/video/uikit/SDL_uikitopengles.m | 250 + .../SDL2/src/video/uikit/SDL_uikitopenglview.h | 60 + .../SDL2/src/video/uikit/SDL_uikitopenglview.m | 384 + ThirdParty/SDL2/src/video/uikit/SDL_uikitvideo.h | 46 + ThirdParty/SDL2/src/video/uikit/SDL_uikitvideo.m | 238 + ThirdParty/SDL2/src/video/uikit/SDL_uikitview.h | 41 + ThirdParty/SDL2/src/video/uikit/SDL_uikitview.m | 328 + .../SDL2/src/video/uikit/SDL_uikitviewcontroller.h | 91 + .../SDL2/src/video/uikit/SDL_uikitviewcontroller.m | 532 ++ ThirdParty/SDL2/src/video/uikit/SDL_uikitvulkan.h | 54 + ThirdParty/SDL2/src/video/uikit/SDL_uikitvulkan.m | 222 + ThirdParty/SDL2/src/video/uikit/SDL_uikitwindow.h | 56 + ThirdParty/SDL2/src/video/uikit/SDL_uikitwindow.m | 465 ++ ThirdParty/SDL2/src/video/uikit/keyinfotable.h | 174 + .../SDL2/src/video/vivante/SDL_vivanteopengles.c | 47 + .../SDL2/src/video/vivante/SDL_vivanteopengles.h | 48 + .../SDL2/src/video/vivante/SDL_vivanteplatform.c | 54 + .../SDL2/src/video/vivante/SDL_vivanteplatform.h | 47 + .../SDL2/src/video/vivante/SDL_vivantevideo.c | 409 ++ .../SDL2/src/video/vivante/SDL_vivantevideo.h | 91 + .../SDL2/src/video/wayland/SDL_waylandclipboard.c | 123 + .../SDL2/src/video/wayland/SDL_waylandclipboard.h | 32 + .../src/video/wayland/SDL_waylanddatamanager.c | 468 ++ .../src/video/wayland/SDL_waylanddatamanager.h | 103 + ThirdParty/SDL2/src/video/wayland/SDL_waylanddyn.c | 178 + ThirdParty/SDL2/src/video/wayland/SDL_waylanddyn.h | 107 + .../SDL2/src/video/wayland/SDL_waylandevents.c | 1129 +++ .../SDL2/src/video/wayland/SDL_waylandevents_c.h | 51 + .../SDL2/src/video/wayland/SDL_waylandmouse.c | 396 ++ .../SDL2/src/video/wayland/SDL_waylandmouse.h | 31 + .../SDL2/src/video/wayland/SDL_waylandopengles.c | 93 + .../SDL2/src/video/wayland/SDL_waylandopengles.h | 49 + ThirdParty/SDL2/src/video/wayland/SDL_waylandsym.h | 127 + .../SDL2/src/video/wayland/SDL_waylandtouch.c | 265 + .../SDL2/src/video/wayland/SDL_waylandtouch.h | 352 + .../SDL2/src/video/wayland/SDL_waylandvideo.c | 499 ++ .../SDL2/src/video/wayland/SDL_waylandvideo.h | 75 + .../SDL2/src/video/wayland/SDL_waylandvulkan.c | 176 + .../SDL2/src/video/wayland/SDL_waylandvulkan.h | 52 + .../SDL2/src/video/wayland/SDL_waylandwindow.c | 552 ++ .../SDL2/src/video/wayland/SDL_waylandwindow.h | 78 + ThirdParty/SDL2/src/video/windows/SDL_msctf.h | 242 + ThirdParty/SDL2/src/video/windows/SDL_vkeys.h | 76 + .../SDL2/src/video/windows/SDL_windowsclipboard.c | 160 + .../SDL2/src/video/windows/SDL_windowsclipboard.h | 36 + .../SDL2/src/video/windows/SDL_windowsevents.c | 1204 ++++ .../SDL2/src/video/windows/SDL_windowsevents.h | 36 + .../src/video/windows/SDL_windowsframebuffer.c | 127 + .../src/video/windows/SDL_windowsframebuffer.h | 27 + .../SDL2/src/video/windows/SDL_windowskeyboard.c | 1579 +++++ .../SDL2/src/video/windows/SDL_windowskeyboard.h | 40 + .../SDL2/src/video/windows/SDL_windowsmessagebox.c | 496 ++ .../SDL2/src/video/windows/SDL_windowsmessagebox.h | 29 + .../SDL2/src/video/windows/SDL_windowsmodes.c | 407 ++ .../SDL2/src/video/windows/SDL_windowsmodes.h | 47 + .../SDL2/src/video/windows/SDL_windowsmouse.c | 322 + .../SDL2/src/video/windows/SDL_windowsmouse.h | 33 + .../SDL2/src/video/windows/SDL_windowsopengl.c | 895 +++ .../SDL2/src/video/windows/SDL_windowsopengl.h | 142 + .../SDL2/src/video/windows/SDL_windowsopengles.c | 131 + .../SDL2/src/video/windows/SDL_windowsopengles.h | 49 + .../SDL2/src/video/windows/SDL_windowsshape.c | 110 + .../SDL2/src/video/windows/SDL_windowsshape.h | 40 + .../SDL2/src/video/windows/SDL_windowsvideo.c | 441 ++ .../SDL2/src/video/windows/SDL_windowsvideo.h | 199 + .../SDL2/src/video/windows/SDL_windowsvulkan.c | 176 + .../SDL2/src/video/windows/SDL_windowsvulkan.h | 52 + .../SDL2/src/video/windows/SDL_windowswindow.c | 970 +++ .../SDL2/src/video/windows/SDL_windowswindow.h | 84 + ThirdParty/SDL2/src/video/windows/wmmsg.h | 1052 +++ .../SDL2/src/video/winrt/SDL_winrtevents.cpp | 154 + .../SDL2/src/video/winrt/SDL_winrtevents_c.h | 82 + .../SDL2/src/video/winrt/SDL_winrtgamebar.cpp | 196 + .../SDL2/src/video/winrt/SDL_winrtgamebar_cpp.h | 35 + .../SDL2/src/video/winrt/SDL_winrtkeyboard.cpp | 430 ++ .../SDL2/src/video/winrt/SDL_winrtmessagebox.cpp | 112 + .../SDL2/src/video/winrt/SDL_winrtmessagebox.h | 29 + ThirdParty/SDL2/src/video/winrt/SDL_winrtmouse.cpp | 224 + ThirdParty/SDL2/src/video/winrt/SDL_winrtmouse_c.h | 40 + .../SDL2/src/video/winrt/SDL_winrtopengles.cpp | 203 + .../SDL2/src/video/winrt/SDL_winrtopengles.h | 70 + .../SDL2/src/video/winrt/SDL_winrtpointerinput.cpp | 415 ++ ThirdParty/SDL2/src/video/winrt/SDL_winrtvideo.cpp | 842 +++ .../SDL2/src/video/winrt/SDL_winrtvideo_cpp.h | 106 + ThirdParty/SDL2/src/video/x11/SDL_x11clipboard.c | 199 + ThirdParty/SDL2/src/video/x11/SDL_x11clipboard.h | 33 + ThirdParty/SDL2/src/video/x11/SDL_x11dyn.c | 212 + ThirdParty/SDL2/src/video/x11/SDL_x11dyn.h | 111 + ThirdParty/SDL2/src/video/x11/SDL_x11events.c | 1500 ++++ ThirdParty/SDL2/src/video/x11/SDL_x11events.h | 31 + ThirdParty/SDL2/src/video/x11/SDL_x11framebuffer.c | 257 + ThirdParty/SDL2/src/video/x11/SDL_x11framebuffer.h | 31 + ThirdParty/SDL2/src/video/x11/SDL_x11keyboard.c | 549 ++ ThirdParty/SDL2/src/video/x11/SDL_x11keyboard.h | 36 + ThirdParty/SDL2/src/video/x11/SDL_x11messagebox.c | 853 +++ ThirdParty/SDL2/src/video/x11/SDL_x11messagebox.h | 28 + ThirdParty/SDL2/src/video/x11/SDL_x11modes.c | 1112 +++ ThirdParty/SDL2/src/video/x11/SDL_x11modes.h | 85 + ThirdParty/SDL2/src/video/x11/SDL_x11mouse.c | 448 ++ ThirdParty/SDL2/src/video/x11/SDL_x11mouse.h | 31 + ThirdParty/SDL2/src/video/x11/SDL_x11opengl.c | 946 +++ ThirdParty/SDL2/src/video/x11/SDL_x11opengl.h | 84 + ThirdParty/SDL2/src/video/x11/SDL_x11opengles.c | 109 + ThirdParty/SDL2/src/video/x11/SDL_x11opengles.h | 56 + ThirdParty/SDL2/src/video/x11/SDL_x11shape.c | 115 + ThirdParty/SDL2/src/video/x11/SDL_x11shape.h | 39 + ThirdParty/SDL2/src/video/x11/SDL_x11sym.h | 337 + ThirdParty/SDL2/src/video/x11/SDL_x11touch.c | 54 + ThirdParty/SDL2/src/video/x11/SDL_x11touch.h | 32 + ThirdParty/SDL2/src/video/x11/SDL_x11video.c | 496 ++ ThirdParty/SDL2/src/video/x11/SDL_x11video.h | 156 + ThirdParty/SDL2/src/video/x11/SDL_x11vulkan.c | 243 + ThirdParty/SDL2/src/video/x11/SDL_x11vulkan.h | 48 + ThirdParty/SDL2/src/video/x11/SDL_x11window.c | 1609 +++++ ThirdParty/SDL2/src/video/x11/SDL_x11window.h | 110 + ThirdParty/SDL2/src/video/x11/SDL_x11xinput2.c | 313 + ThirdParty/SDL2/src/video/x11/SDL_x11xinput2.h | 42 + ThirdParty/SDL2/src/video/x11/edid-parse.c | 754 ++ ThirdParty/SDL2/src/video/x11/edid.h | 167 + ThirdParty/SDL2/src/video/x11/imKStoUCS.c | 350 + ThirdParty/SDL2/src/video/x11/imKStoUCS.h | 32 + ThirdParty/SDL2/src/video/yuv2rgb/LICENSE | 27 + ThirdParty/SDL2/src/video/yuv2rgb/README.md | 63 + ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb.c | 687 ++ ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb.h | 381 + .../SDL2/src/video/yuv2rgb/yuv_rgb_sse_func.h | 498 ++ .../SDL2/src/video/yuv2rgb/yuv_rgb_std_func.h | 220 + 690 files changed, 231271 insertions(+) create mode 100644 ThirdParty/SDL2/src/SDL.c create mode 100644 ThirdParty/SDL2/src/SDL_assert.c create mode 100644 ThirdParty/SDL2/src/SDL_assert_c.h create mode 100644 ThirdParty/SDL2/src/SDL_dataqueue.c create mode 100644 ThirdParty/SDL2/src/SDL_dataqueue.h create mode 100644 ThirdParty/SDL2/src/SDL_error.c create mode 100644 ThirdParty/SDL2/src/SDL_error_c.h create mode 100644 ThirdParty/SDL2/src/SDL_hints.c create mode 100644 ThirdParty/SDL2/src/SDL_internal.h create mode 100644 ThirdParty/SDL2/src/SDL_log.c create mode 100644 ThirdParty/SDL2/src/atomic/SDL_atomic.c create mode 100644 ThirdParty/SDL2/src/atomic/SDL_spinlock.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_audio.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_audio_c.h create mode 100644 ThirdParty/SDL2/src/audio/SDL_audiocvt.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_audiodev.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_audiodev_c.h create mode 100644 ThirdParty/SDL2/src/audio/SDL_audiotypecvt.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_mixer.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_sysaudio.h create mode 100644 ThirdParty/SDL2/src/audio/SDL_wave.c create mode 100644 ThirdParty/SDL2/src/audio/SDL_wave.h create mode 100644 ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.c create mode 100644 ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.h create mode 100644 ThirdParty/SDL2/src/audio/android/SDL_androidaudio.c create mode 100644 ThirdParty/SDL2/src/audio/android/SDL_androidaudio.h create mode 100644 ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.c create mode 100644 ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.h create mode 100644 ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.h create mode 100644 ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.m create mode 100644 ThirdParty/SDL2/src/audio/directsound/SDL_directsound.c create mode 100644 ThirdParty/SDL2/src/audio/directsound/SDL_directsound.h create mode 100644 ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.c create mode 100644 ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.h create mode 100644 ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.c create mode 100644 ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.h create mode 100644 ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.c create mode 100644 ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.h create mode 100644 ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.c create mode 100644 ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.h create mode 100644 ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.c create mode 100644 ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.h create mode 100644 ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.c create mode 100644 ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.h create mode 100644 ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.cc create mode 100644 ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.h create mode 100644 ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.c create mode 100644 ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.h create mode 100644 ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.c create mode 100644 ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.h create mode 100644 ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.c create mode 100644 ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.h create mode 100644 ThirdParty/SDL2/src/audio/netbsd/SDL_netbsdaudio.c create mode 100644 ThirdParty/SDL2/src/audio/netbsd/SDL_netbsdaudio.h create mode 100644 ThirdParty/SDL2/src/audio/paudio/SDL_paudio.c create mode 100644 ThirdParty/SDL2/src/audio/paudio/SDL_paudio.h create mode 100644 ThirdParty/SDL2/src/audio/psp/SDL_pspaudio.c create mode 100644 ThirdParty/SDL2/src/audio/psp/SDL_pspaudio.h create mode 100644 ThirdParty/SDL2/src/audio/pulseaudio/SDL_pulseaudio.c create mode 100644 ThirdParty/SDL2/src/audio/pulseaudio/SDL_pulseaudio.h create mode 100644 ThirdParty/SDL2/src/audio/qsa/SDL_qsa_audio.c create mode 100644 ThirdParty/SDL2/src/audio/qsa/SDL_qsa_audio.h create mode 100644 ThirdParty/SDL2/src/audio/sndio/SDL_sndioaudio.c create mode 100644 ThirdParty/SDL2/src/audio/sndio/SDL_sndioaudio.h create mode 100644 ThirdParty/SDL2/src/audio/sun/SDL_sunaudio.c create mode 100644 ThirdParty/SDL2/src/audio/sun/SDL_sunaudio.h create mode 100644 ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi.c create mode 100644 ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi.h create mode 100644 ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi_win32.c create mode 100644 ThirdParty/SDL2/src/audio/wasapi/SDL_wasapi_winrt.cpp create mode 100644 ThirdParty/SDL2/src/audio/winmm/SDL_winmm.c create mode 100644 ThirdParty/SDL2/src/audio/winmm/SDL_winmm.h create mode 100644 ThirdParty/SDL2/src/core/android/SDL_android.c create mode 100644 ThirdParty/SDL2/src/core/android/SDL_android.h create mode 100644 ThirdParty/SDL2/src/core/android/keyinfotable.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_dbus.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_dbus.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd_default_accents.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_evdev_kbd_default_keymap.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_fcitx.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_fcitx.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_ibus.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_ibus.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_ime.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_ime.h create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_udev.c create mode 100644 ThirdParty/SDL2/src/core/linux/SDL_udev.h create mode 100644 ThirdParty/SDL2/src/core/unix/SDL_poll.c create mode 100644 ThirdParty/SDL2/src/core/unix/SDL_poll.h create mode 100644 ThirdParty/SDL2/src/core/windows/SDL_directx.h create mode 100644 ThirdParty/SDL2/src/core/windows/SDL_windows.c create mode 100644 ThirdParty/SDL2/src/core/windows/SDL_windows.h create mode 100644 ThirdParty/SDL2/src/core/windows/SDL_xinput.c create mode 100644 ThirdParty/SDL2/src/core/windows/SDL_xinput.h create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_common.cpp create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_common.h create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_direct3d.cpp create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_direct3d.h create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_xaml.cpp create mode 100644 ThirdParty/SDL2/src/core/winrt/SDL_winrtapp_xaml.h create mode 100644 ThirdParty/SDL2/src/cpuinfo/SDL_cpuinfo.c create mode 100644 ThirdParty/SDL2/src/dynapi/SDL_dynapi.c create mode 100644 ThirdParty/SDL2/src/dynapi/SDL_dynapi.h create mode 100644 ThirdParty/SDL2/src/dynapi/SDL_dynapi_overrides.h create mode 100644 ThirdParty/SDL2/src/dynapi/SDL_dynapi_procs.h create mode 100644 ThirdParty/SDL2/src/dynapi/gendynapi.pl create mode 100644 ThirdParty/SDL2/src/events/SDL_clipboardevents.c create mode 100644 ThirdParty/SDL2/src/events/SDL_clipboardevents_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_dropevents.c create mode 100644 ThirdParty/SDL2/src/events/SDL_dropevents_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_events.c create mode 100644 ThirdParty/SDL2/src/events/SDL_events_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_gesture.c create mode 100644 ThirdParty/SDL2/src/events/SDL_gesture_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_keyboard.c create mode 100644 ThirdParty/SDL2/src/events/SDL_keyboard_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_mouse.c create mode 100644 ThirdParty/SDL2/src/events/SDL_mouse_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_quit.c create mode 100644 ThirdParty/SDL2/src/events/SDL_sysevents.h create mode 100644 ThirdParty/SDL2/src/events/SDL_touch.c create mode 100644 ThirdParty/SDL2/src/events/SDL_touch_c.h create mode 100644 ThirdParty/SDL2/src/events/SDL_windowevents.c create mode 100644 ThirdParty/SDL2/src/events/SDL_windowevents_c.h create mode 100644 ThirdParty/SDL2/src/events/blank_cursor.h create mode 100644 ThirdParty/SDL2/src/events/default_cursor.h create mode 100644 ThirdParty/SDL2/src/events/scancodes_darwin.h create mode 100644 ThirdParty/SDL2/src/events/scancodes_linux.h create mode 100644 ThirdParty/SDL2/src/events/scancodes_windows.h create mode 100644 ThirdParty/SDL2/src/events/scancodes_xfree86.h create mode 100644 ThirdParty/SDL2/src/file/SDL_rwops.c create mode 100644 ThirdParty/SDL2/src/file/cocoa/SDL_rwopsbundlesupport.h create mode 100644 ThirdParty/SDL2/src/file/cocoa/SDL_rwopsbundlesupport.m create mode 100644 ThirdParty/SDL2/src/filesystem/android/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/cocoa/SDL_sysfilesystem.m create mode 100644 ThirdParty/SDL2/src/filesystem/dummy/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/emscripten/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/haiku/SDL_sysfilesystem.cc create mode 100644 ThirdParty/SDL2/src/filesystem/nacl/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/unix/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/windows/SDL_sysfilesystem.c create mode 100644 ThirdParty/SDL2/src/filesystem/winrt/SDL_sysfilesystem.cpp create mode 100644 ThirdParty/SDL2/src/haptic/SDL_haptic.c create mode 100644 ThirdParty/SDL2/src/haptic/SDL_haptic_c.h create mode 100644 ThirdParty/SDL2/src/haptic/SDL_syshaptic.h create mode 100644 ThirdParty/SDL2/src/haptic/android/SDL_syshaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/android/SDL_syshaptic_c.h create mode 100644 ThirdParty/SDL2/src/haptic/darwin/SDL_syshaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/darwin/SDL_syshaptic_c.h create mode 100644 ThirdParty/SDL2/src/haptic/dummy/SDL_syshaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/linux/SDL_syshaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_dinputhaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_dinputhaptic_c.h create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_windowshaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_windowshaptic_c.h create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_xinputhaptic.c create mode 100644 ThirdParty/SDL2/src/haptic/windows/SDL_xinputhaptic_c.h create mode 100644 ThirdParty/SDL2/src/joystick/SDL_gamecontroller.c create mode 100644 ThirdParty/SDL2/src/joystick/SDL_gamecontrollerdb.h create mode 100644 ThirdParty/SDL2/src/joystick/SDL_joystick.c create mode 100644 ThirdParty/SDL2/src/joystick/SDL_joystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/SDL_sysjoystick.h create mode 100644 ThirdParty/SDL2/src/joystick/android/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/android/SDL_sysjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/bsd/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/darwin/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/darwin/SDL_sysjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/dummy/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/emscripten/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/emscripten/SDL_sysjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/haiku/SDL_haikujoystick.cc create mode 100644 ThirdParty/SDL2/src/joystick/iphoneos/SDL_sysjoystick.m create mode 100644 ThirdParty/SDL2/src/joystick/iphoneos/SDL_sysjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/linux/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/linux/SDL_sysjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/psp/SDL_sysjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/sort_controllers.py create mode 100644 ThirdParty/SDL2/src/joystick/steam/SDL_steamcontroller.c create mode 100644 ThirdParty/SDL2/src/joystick/steam/SDL_steamcontroller.h create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_dinputjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_dinputjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_mmjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_windowsjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_windowsjoystick_c.h create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_xinputjoystick.c create mode 100644 ThirdParty/SDL2/src/joystick/windows/SDL_xinputjoystick_c.h create mode 100644 ThirdParty/SDL2/src/libm/e_atan2.c create mode 100644 ThirdParty/SDL2/src/libm/e_fmod.c create mode 100644 ThirdParty/SDL2/src/libm/e_log.c create mode 100644 ThirdParty/SDL2/src/libm/e_log10.c create mode 100644 ThirdParty/SDL2/src/libm/e_pow.c create mode 100644 ThirdParty/SDL2/src/libm/e_rem_pio2.c create mode 100644 ThirdParty/SDL2/src/libm/e_sqrt.c create mode 100644 ThirdParty/SDL2/src/libm/k_cos.c create mode 100644 ThirdParty/SDL2/src/libm/k_rem_pio2.c create mode 100644 ThirdParty/SDL2/src/libm/k_sin.c create mode 100644 ThirdParty/SDL2/src/libm/k_tan.c create mode 100644 ThirdParty/SDL2/src/libm/math_libm.h create mode 100644 ThirdParty/SDL2/src/libm/math_private.h create mode 100644 ThirdParty/SDL2/src/libm/s_atan.c create mode 100644 ThirdParty/SDL2/src/libm/s_copysign.c create mode 100644 ThirdParty/SDL2/src/libm/s_cos.c create mode 100644 ThirdParty/SDL2/src/libm/s_fabs.c create mode 100644 ThirdParty/SDL2/src/libm/s_floor.c create mode 100644 ThirdParty/SDL2/src/libm/s_scalbn.c create mode 100644 ThirdParty/SDL2/src/libm/s_sin.c create mode 100644 ThirdParty/SDL2/src/libm/s_tan.c create mode 100644 ThirdParty/SDL2/src/loadso/dlopen/SDL_sysloadso.c create mode 100644 ThirdParty/SDL2/src/loadso/dummy/SDL_sysloadso.c create mode 100644 ThirdParty/SDL2/src/loadso/windows/SDL_sysloadso.c create mode 100644 ThirdParty/SDL2/src/main/android/SDL_android_main.c create mode 100644 ThirdParty/SDL2/src/main/dummy/SDL_dummy_main.c create mode 100644 ThirdParty/SDL2/src/main/haiku/SDL_BApp.h create mode 100644 ThirdParty/SDL2/src/main/haiku/SDL_BeApp.cc create mode 100644 ThirdParty/SDL2/src/main/haiku/SDL_BeApp.h create mode 100644 ThirdParty/SDL2/src/main/nacl/SDL_nacl_main.c create mode 100644 ThirdParty/SDL2/src/main/psp/SDL_psp_main.c create mode 100644 ThirdParty/SDL2/src/main/windows/SDL_windows_main.c create mode 100644 ThirdParty/SDL2/src/main/windows/version.rc create mode 100644 ThirdParty/SDL2/src/main/winrt/SDL2-WinRTResource_BlankCursor.cur create mode 100644 ThirdParty/SDL2/src/main/winrt/SDL2-WinRTResources.rc create mode 100644 ThirdParty/SDL2/src/main/winrt/SDL_winrt_main_NonXAML.cpp create mode 100644 ThirdParty/SDL2/src/power/SDL_power.c create mode 100644 ThirdParty/SDL2/src/power/SDL_syspower.h create mode 100644 ThirdParty/SDL2/src/power/android/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/emscripten/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/haiku/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/linux/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/macosx/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/psp/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/uikit/SDL_syspower.h create mode 100644 ThirdParty/SDL2/src/power/uikit/SDL_syspower.m create mode 100644 ThirdParty/SDL2/src/power/windows/SDL_syspower.c create mode 100644 ThirdParty/SDL2/src/power/winrt/SDL_syspower.cpp create mode 100644 ThirdParty/SDL2/src/render/SDL_d3dmath.c create mode 100644 ThirdParty/SDL2/src/render/SDL_d3dmath.h create mode 100644 ThirdParty/SDL2/src/render/SDL_render.c create mode 100644 ThirdParty/SDL2/src/render/SDL_sysrender.h create mode 100644 ThirdParty/SDL2/src/render/SDL_yuv_sw.c create mode 100644 ThirdParty/SDL2/src/render/SDL_yuv_sw_c.h create mode 100644 ThirdParty/SDL2/src/render/direct3d/SDL_render_d3d.c create mode 100644 ThirdParty/SDL2/src/render/direct3d/SDL_shaders_d3d.c create mode 100644 ThirdParty/SDL2/src/render/direct3d/SDL_shaders_d3d.h create mode 100644 ThirdParty/SDL2/src/render/direct3d11/SDL_render_d3d11.c create mode 100644 ThirdParty/SDL2/src/render/direct3d11/SDL_render_winrt.cpp create mode 100644 ThirdParty/SDL2/src/render/direct3d11/SDL_render_winrt.h create mode 100644 ThirdParty/SDL2/src/render/direct3d11/SDL_shaders_d3d11.c create mode 100644 ThirdParty/SDL2/src/render/direct3d11/SDL_shaders_d3d11.h create mode 100644 ThirdParty/SDL2/src/render/metal/SDL_render_metal.m create mode 100644 ThirdParty/SDL2/src/render/metal/SDL_shaders_metal.metal create mode 100644 ThirdParty/SDL2/src/render/metal/SDL_shaders_metal_ios.h create mode 100644 ThirdParty/SDL2/src/render/metal/SDL_shaders_metal_osx.h create mode 100644 ThirdParty/SDL2/src/render/metal/build-metal-shaders.sh create mode 100644 ThirdParty/SDL2/src/render/opengl/SDL_glfuncs.h create mode 100644 ThirdParty/SDL2/src/render/opengl/SDL_render_gl.c create mode 100644 ThirdParty/SDL2/src/render/opengl/SDL_shaders_gl.c create mode 100644 ThirdParty/SDL2/src/render/opengl/SDL_shaders_gl.h create mode 100644 ThirdParty/SDL2/src/render/opengles/SDL_glesfuncs.h create mode 100644 ThirdParty/SDL2/src/render/opengles/SDL_render_gles.c create mode 100644 ThirdParty/SDL2/src/render/opengles2/SDL_gles2funcs.h create mode 100644 ThirdParty/SDL2/src/render/opengles2/SDL_render_gles2.c create mode 100644 ThirdParty/SDL2/src/render/opengles2/SDL_shaders_gles2.c create mode 100644 ThirdParty/SDL2/src/render/opengles2/SDL_shaders_gles2.h create mode 100644 ThirdParty/SDL2/src/render/psp/SDL_render_psp.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendfillrect.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendfillrect.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendline.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendline.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendpoint.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_blendpoint.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_draw.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_drawline.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_drawline.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_drawpoint.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_drawpoint.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_render_sw.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_render_sw_c.h create mode 100644 ThirdParty/SDL2/src/render/software/SDL_rotate.c create mode 100644 ThirdParty/SDL2/src/render/software/SDL_rotate.h create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_getenv.c create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_iconv.c create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_malloc.c create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_qsort.c create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_stdlib.c create mode 100644 ThirdParty/SDL2/src/stdlib/SDL_string.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_assert.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_common.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_compare.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_crc32.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_font.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_fuzzer.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_harness.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_imageBlit.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_imageBlitBlend.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_imageFace.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_imagePrimitives.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_imagePrimitivesBlend.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_log.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_md5.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_memory.c create mode 100644 ThirdParty/SDL2/src/test/SDL_test_random.c create mode 100644 ThirdParty/SDL2/src/thread/SDL_systhread.h create mode 100644 ThirdParty/SDL2/src/thread/SDL_thread.c create mode 100644 ThirdParty/SDL2/src/thread/SDL_thread_c.h create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_syscond.c create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_sysmutex.c create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_sysmutex_c.h create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_syssem.c create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_systhread.c create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_systhread_c.h create mode 100644 ThirdParty/SDL2/src/thread/generic/SDL_systls.c create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_syscond.c create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_sysmutex.c create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_sysmutex_c.h create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_syssem.c create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_systhread.c create mode 100644 ThirdParty/SDL2/src/thread/psp/SDL_systhread_c.h create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_syscond.c create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_sysmutex.c create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_sysmutex_c.h create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_syssem.c create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_systhread.c create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_systhread_c.h create mode 100644 ThirdParty/SDL2/src/thread/pthread/SDL_systls.c create mode 100644 ThirdParty/SDL2/src/thread/stdcpp/SDL_syscond.cpp create mode 100644 ThirdParty/SDL2/src/thread/stdcpp/SDL_sysmutex.cpp create mode 100644 ThirdParty/SDL2/src/thread/stdcpp/SDL_sysmutex_c.h create mode 100644 ThirdParty/SDL2/src/thread/stdcpp/SDL_systhread.cpp create mode 100644 ThirdParty/SDL2/src/thread/stdcpp/SDL_systhread_c.h create mode 100644 ThirdParty/SDL2/src/thread/windows/SDL_sysmutex.c create mode 100644 ThirdParty/SDL2/src/thread/windows/SDL_syssem.c create mode 100644 ThirdParty/SDL2/src/thread/windows/SDL_systhread.c create mode 100644 ThirdParty/SDL2/src/thread/windows/SDL_systhread_c.h create mode 100644 ThirdParty/SDL2/src/thread/windows/SDL_systls.c create mode 100644 ThirdParty/SDL2/src/timer/SDL_timer.c create mode 100644 ThirdParty/SDL2/src/timer/SDL_timer_c.h create mode 100644 ThirdParty/SDL2/src/timer/dummy/SDL_systimer.c create mode 100644 ThirdParty/SDL2/src/timer/haiku/SDL_systimer.c create mode 100644 ThirdParty/SDL2/src/timer/psp/SDL_systimer.c create mode 100644 ThirdParty/SDL2/src/timer/unix/SDL_systimer.c create mode 100644 ThirdParty/SDL2/src/timer/windows/SDL_systimer.c create mode 100644 ThirdParty/SDL2/src/video/SDL_RLEaccel.c create mode 100644 ThirdParty/SDL2/src/video/SDL_RLEaccel_c.h create mode 100644 ThirdParty/SDL2/src/video/SDL_blit.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit.h create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_0.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_1.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_A.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_N.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_auto.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_auto.h create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_copy.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_copy.h create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_slow.c create mode 100644 ThirdParty/SDL2/src/video/SDL_blit_slow.h create mode 100644 ThirdParty/SDL2/src/video/SDL_bmp.c create mode 100644 ThirdParty/SDL2/src/video/SDL_clipboard.c create mode 100644 ThirdParty/SDL2/src/video/SDL_egl.c create mode 100644 ThirdParty/SDL2/src/video/SDL_egl_c.h create mode 100644 ThirdParty/SDL2/src/video/SDL_fillrect.c create mode 100644 ThirdParty/SDL2/src/video/SDL_pixels.c create mode 100644 ThirdParty/SDL2/src/video/SDL_pixels_c.h create mode 100644 ThirdParty/SDL2/src/video/SDL_rect.c create mode 100644 ThirdParty/SDL2/src/video/SDL_rect_c.h create mode 100644 ThirdParty/SDL2/src/video/SDL_shape.c create mode 100644 ThirdParty/SDL2/src/video/SDL_shape_internals.h create mode 100644 ThirdParty/SDL2/src/video/SDL_stretch.c create mode 100644 ThirdParty/SDL2/src/video/SDL_surface.c create mode 100644 ThirdParty/SDL2/src/video/SDL_sysvideo.h create mode 100644 ThirdParty/SDL2/src/video/SDL_video.c create mode 100644 ThirdParty/SDL2/src/video/SDL_vulkan_internal.h create mode 100644 ThirdParty/SDL2/src/video/SDL_vulkan_utils.c create mode 100644 ThirdParty/SDL2/src/video/SDL_yuv.c create mode 100644 ThirdParty/SDL2/src/video/SDL_yuv_c.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidclipboard.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidclipboard.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidevents.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidevents.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidgl.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidgl.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidkeyboard.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidkeyboard.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidmessagebox.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidmessagebox.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidmouse.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidmouse.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidtouch.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidtouch.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidvideo.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidvideo.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidvulkan.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidvulkan.h create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidwindow.c create mode 100644 ThirdParty/SDL2/src/video/android/SDL_androidwindow.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaclipboard.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaclipboard.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaevents.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaevents.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoakeyboard.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoakeyboard.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamessagebox.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamessagebox.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoametalview.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoametalview.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamodes.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamodes.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamouse.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamouse.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamousetap.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoamousetap.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengl.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengl.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengles.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoaopengles.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoashape.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoashape.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoavideo.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoavideo.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoavulkan.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoavulkan.m create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoawindow.h create mode 100644 ThirdParty/SDL2/src/video/cocoa/SDL_cocoawindow.m create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_WM.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_WM.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_dyn.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_dyn.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_events.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_events.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_modes.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_modes.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_mouse.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_mouse.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_opengl.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_opengl.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_render.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_render.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_shape.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_shape.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_video.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_video.h create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_window.c create mode 100644 ThirdParty/SDL2/src/video/directfb/SDL_DirectFB_window.h create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullevents.c create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullevents_c.h create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullframebuffer.c create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullframebuffer_c.h create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullvideo.c create mode 100644 ThirdParty/SDL2/src/video/dummy/SDL_nullvideo.h create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenevents.c create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenevents.h create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenframebuffer.c create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenframebuffer.h create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenmouse.c create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenmouse.h create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenopengles.c create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenopengles.h create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenvideo.c create mode 100644 ThirdParty/SDL2/src/video/emscripten/SDL_emscriptenvideo.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_BWin.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bclipboard.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bclipboard.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bevents.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bevents.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bframebuffer.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bframebuffer.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bkeyboard.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bkeyboard.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bmodes.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bmodes.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bopengl.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bopengl.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bvideo.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bvideo.h create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bwindow.cc create mode 100644 ThirdParty/SDL2/src/video/haiku/SDL_bwindow.h create mode 100644 ThirdParty/SDL2/src/video/khronos/EGL/egl.h create mode 100644 ThirdParty/SDL2/src/video/khronos/EGL/eglext.h create mode 100644 ThirdParty/SDL2/src/video/khronos/EGL/eglplatform.h create mode 100644 ThirdParty/SDL2/src/video/khronos/GLES2/gl2.h create mode 100644 ThirdParty/SDL2/src/video/khronos/GLES2/gl2ext.h create mode 100644 ThirdParty/SDL2/src/video/khronos/GLES2/gl2platform.h create mode 100644 ThirdParty/SDL2/src/video/khronos/KHR/khrplatform.h create mode 100644 ThirdParty/SDL2/src/video/khronos/vulkan/vk_platform.h create mode 100644 ThirdParty/SDL2/src/video/khronos/vulkan/vulkan.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmdyn.c create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmdyn.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmevents.c create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmevents.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmmouse.c create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmmouse.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmopengles.c create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmopengles.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmsym.h create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmvideo.c create mode 100644 ThirdParty/SDL2/src/video/kmsdrm/SDL_kmsdrmvideo.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirdyn.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirdyn.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirevents.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirevents.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirframebuffer.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirframebuffer.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirmouse.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirmouse.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_miropengl.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_miropengl.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirsym.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirvideo.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirvideo.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirvulkan.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirvulkan.h create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirwindow.c create mode 100644 ThirdParty/SDL2/src/video/mir/SDL_mirwindow.h create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclevents.c create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclevents_c.h create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclglue.c create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclopengles.c create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclopengles.h create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclvideo.c create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclvideo.h create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclwindow.c create mode 100644 ThirdParty/SDL2/src/video/nacl/SDL_naclwindow.h create mode 100644 ThirdParty/SDL2/src/video/pandora/SDL_pandora.c create mode 100644 ThirdParty/SDL2/src/video/pandora/SDL_pandora.h create mode 100644 ThirdParty/SDL2/src/video/pandora/SDL_pandora_events.c create mode 100644 ThirdParty/SDL2/src/video/pandora/SDL_pandora_events.h create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspevents.c create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspevents_c.h create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspgl.c create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspgl_c.h create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspmouse.c create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspmouse_c.h create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspvideo.c create mode 100644 ThirdParty/SDL2/src/video/psp/SDL_pspvideo.h create mode 100644 ThirdParty/SDL2/src/video/qnx/gl.c create mode 100644 ThirdParty/SDL2/src/video/qnx/keyboard.c create mode 100644 ThirdParty/SDL2/src/video/qnx/sdl_qnx.h create mode 100644 ThirdParty/SDL2/src/video/qnx/video.c create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpievents.c create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpievents_c.h create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpimouse.c create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpimouse.h create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpiopengles.c create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpiopengles.h create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpivideo.c create mode 100644 ThirdParty/SDL2/src/video/raspberry/SDL_rpivideo.h create mode 100644 ThirdParty/SDL2/src/video/sdlgenblit.pl create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitappdelegate.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitappdelegate.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitclipboard.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitclipboard.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitevents.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitevents.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmessagebox.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmessagebox.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmetalview.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmetalview.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmodes.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitmodes.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitopengles.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitopengles.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitopenglview.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitopenglview.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitvideo.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitvideo.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitview.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitview.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitviewcontroller.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitviewcontroller.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitvulkan.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitvulkan.m create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitwindow.h create mode 100644 ThirdParty/SDL2/src/video/uikit/SDL_uikitwindow.m create mode 100644 ThirdParty/SDL2/src/video/uikit/keyinfotable.h create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivanteopengles.c create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivanteopengles.h create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivanteplatform.c create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivanteplatform.h create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivantevideo.c create mode 100644 ThirdParty/SDL2/src/video/vivante/SDL_vivantevideo.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandclipboard.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandclipboard.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylanddatamanager.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylanddatamanager.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylanddyn.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylanddyn.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandevents.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandevents_c.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandmouse.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandmouse.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandopengles.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandopengles.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandsym.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandtouch.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandtouch.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandvideo.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandvideo.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandvulkan.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandvulkan.h create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandwindow.c create mode 100644 ThirdParty/SDL2/src/video/wayland/SDL_waylandwindow.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_msctf.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_vkeys.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsclipboard.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsclipboard.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsevents.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsevents.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsframebuffer.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsframebuffer.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowskeyboard.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowskeyboard.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmessagebox.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmessagebox.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmodes.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmodes.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmouse.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsmouse.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsopengl.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsopengl.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsopengles.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsopengles.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsshape.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsshape.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsvideo.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsvideo.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsvulkan.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowsvulkan.h create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowswindow.c create mode 100644 ThirdParty/SDL2/src/video/windows/SDL_windowswindow.h create mode 100644 ThirdParty/SDL2/src/video/windows/wmmsg.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtevents.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtevents_c.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtgamebar.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtgamebar_cpp.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtkeyboard.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtmessagebox.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtmessagebox.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtmouse.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtmouse_c.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtopengles.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtopengles.h create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtpointerinput.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtvideo.cpp create mode 100644 ThirdParty/SDL2/src/video/winrt/SDL_winrtvideo_cpp.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11clipboard.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11clipboard.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11dyn.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11dyn.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11events.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11events.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11framebuffer.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11framebuffer.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11keyboard.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11keyboard.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11messagebox.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11messagebox.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11modes.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11modes.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11mouse.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11mouse.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11opengl.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11opengl.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11opengles.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11opengles.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11shape.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11shape.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11sym.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11touch.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11touch.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11video.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11video.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11vulkan.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11vulkan.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11window.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11window.h create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11xinput2.c create mode 100644 ThirdParty/SDL2/src/video/x11/SDL_x11xinput2.h create mode 100644 ThirdParty/SDL2/src/video/x11/edid-parse.c create mode 100644 ThirdParty/SDL2/src/video/x11/edid.h create mode 100644 ThirdParty/SDL2/src/video/x11/imKStoUCS.c create mode 100644 ThirdParty/SDL2/src/video/x11/imKStoUCS.h create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/LICENSE create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/README.md create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb.c create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb.h create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb_sse_func.h create mode 100644 ThirdParty/SDL2/src/video/yuv2rgb/yuv_rgb_std_func.h (limited to 'ThirdParty/SDL2/src') diff --git a/ThirdParty/SDL2/src/SDL.c b/ThirdParty/SDL2/src/SDL.c new file mode 100644 index 0000000..0e55279 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL.c @@ -0,0 +1,476 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +#if defined(__WIN32__) +#include "core/windows/SDL_windows.h" +#endif + +/* Initialization code for SDL */ + +#include "SDL.h" +#include "SDL_bits.h" +#include "SDL_revision.h" +#include "SDL_assert_c.h" +#include "events/SDL_events_c.h" +#include "haptic/SDL_haptic_c.h" +#include "joystick/SDL_joystick_c.h" + +/* Initialization/Cleanup routines */ +#if !SDL_TIMERS_DISABLED +# include "timer/SDL_timer_c.h" +#endif +#if SDL_VIDEO_DRIVER_WINDOWS +extern int SDL_HelperWindowCreate(void); +extern int SDL_HelperWindowDestroy(void); +#endif + + +/* The initialized subsystems */ +#ifdef SDL_MAIN_NEEDED +static SDL_bool SDL_MainIsReady = SDL_FALSE; +#else +static SDL_bool SDL_MainIsReady = SDL_TRUE; +#endif +static SDL_bool SDL_bInMainQuit = SDL_FALSE; +static Uint8 SDL_SubsystemRefCount[ 32 ]; + +/* Private helper to increment a subsystem's ref counter. */ +static void +SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem) +{ + int subsystem_index = SDL_MostSignificantBitIndex32(subsystem); + SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255); + ++SDL_SubsystemRefCount[subsystem_index]; +} + +/* Private helper to decrement a subsystem's ref counter. */ +static void +SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem) +{ + int subsystem_index = SDL_MostSignificantBitIndex32(subsystem); + if (SDL_SubsystemRefCount[subsystem_index] > 0) { + --SDL_SubsystemRefCount[subsystem_index]; + } +} + +/* Private helper to check if a system needs init. */ +static SDL_bool +SDL_PrivateShouldInitSubsystem(Uint32 subsystem) +{ + int subsystem_index = SDL_MostSignificantBitIndex32(subsystem); + SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255); + return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE; +} + +/* Private helper to check if a system needs to be quit. */ +static SDL_bool +SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) { + int subsystem_index = SDL_MostSignificantBitIndex32(subsystem); + if (SDL_SubsystemRefCount[subsystem_index] == 0) { + return SDL_FALSE; + } + + /* If we're in SDL_Quit, we shut down every subsystem, even if refcount + * isn't zero. + */ + return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE; +} + +void +SDL_SetMainReady(void) +{ + SDL_MainIsReady = SDL_TRUE; +} + +int +SDL_InitSubSystem(Uint32 flags) +{ + if (!SDL_MainIsReady) { + SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?"); + return -1; + } + + /* Clear the error message */ + SDL_ClearError(); + + if ((flags & SDL_INIT_GAMECONTROLLER)) { + /* game controller implies joystick */ + flags |= SDL_INIT_JOYSTICK; + } + + if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) { + /* video or joystick implies events */ + flags |= SDL_INIT_EVENTS; + } + +#if SDL_VIDEO_DRIVER_WINDOWS + if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) { + if (SDL_HelperWindowCreate() < 0) { + return -1; + } + } +#endif + +#if !SDL_TIMERS_DISABLED + SDL_TicksInit(); +#endif + + /* Initialize the event subsystem */ + if ((flags & SDL_INIT_EVENTS)) { +#if !SDL_EVENTS_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) { + if (SDL_StartEventLoop() < 0) { + return (-1); + } + SDL_QuitInit(); + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS); +#else + return SDL_SetError("SDL not built with events support"); +#endif + } + + /* Initialize the timer subsystem */ + if ((flags & SDL_INIT_TIMER)){ +#if !SDL_TIMERS_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) { + if (SDL_TimerInit() < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER); +#else + return SDL_SetError("SDL not built with timer support"); +#endif + } + + /* Initialize the video subsystem */ + if ((flags & SDL_INIT_VIDEO)){ +#if !SDL_VIDEO_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) { + if (SDL_VideoInit(NULL) < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO); +#else + return SDL_SetError("SDL not built with video support"); +#endif + } + + /* Initialize the audio subsystem */ + if ((flags & SDL_INIT_AUDIO)){ +#if !SDL_AUDIO_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) { + if (SDL_AudioInit(NULL) < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO); +#else + return SDL_SetError("SDL not built with audio support"); +#endif + } + + /* Initialize the joystick subsystem */ + if ((flags & SDL_INIT_JOYSTICK)){ +#if !SDL_JOYSTICK_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) { + if (SDL_JoystickInit() < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK); +#else + return SDL_SetError("SDL not built with joystick support"); +#endif + } + + if ((flags & SDL_INIT_GAMECONTROLLER)){ +#if !SDL_JOYSTICK_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) { + if (SDL_GameControllerInit() < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER); +#else + return SDL_SetError("SDL not built with joystick support"); +#endif + } + + /* Initialize the haptic subsystem */ + if ((flags & SDL_INIT_HAPTIC)){ +#if !SDL_HAPTIC_DISABLED + if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) { + if (SDL_HapticInit() < 0) { + return (-1); + } + } + SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC); +#else + return SDL_SetError("SDL not built with haptic (force feedback) support"); +#endif + } + + return (0); +} + +int +SDL_Init(Uint32 flags) +{ + return SDL_InitSubSystem(flags); +} + +void +SDL_QuitSubSystem(Uint32 flags) +{ + /* Shut down requested initialized subsystems */ +#if !SDL_JOYSTICK_DISABLED + if ((flags & SDL_INIT_GAMECONTROLLER)) { + /* game controller implies joystick */ + flags |= SDL_INIT_JOYSTICK; + + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) { + SDL_GameControllerQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER); + } + + if ((flags & SDL_INIT_JOYSTICK)) { + /* joystick implies events */ + flags |= SDL_INIT_EVENTS; + + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) { + SDL_JoystickQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK); + } +#endif + +#if !SDL_HAPTIC_DISABLED + if ((flags & SDL_INIT_HAPTIC)) { + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) { + SDL_HapticQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC); + } +#endif + +#if !SDL_AUDIO_DISABLED + if ((flags & SDL_INIT_AUDIO)) { + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) { + SDL_AudioQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO); + } +#endif + +#if !SDL_VIDEO_DISABLED + if ((flags & SDL_INIT_VIDEO)) { + /* video implies events */ + flags |= SDL_INIT_EVENTS; + + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) { + SDL_VideoQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO); + } +#endif + +#if !SDL_TIMERS_DISABLED + if ((flags & SDL_INIT_TIMER)) { + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) { + SDL_TimerQuit(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER); + } +#endif + +#if !SDL_EVENTS_DISABLED + if ((flags & SDL_INIT_EVENTS)) { + if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) { + SDL_QuitQuit(); + SDL_StopEventLoop(); + } + SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS); + } +#endif +} + +Uint32 +SDL_WasInit(Uint32 flags) +{ + int i; + int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount); + Uint32 initialized = 0; + + if (!flags) { + flags = SDL_INIT_EVERYTHING; + } + + num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1); + + /* Iterate over each bit in flags, and check the matching subsystem. */ + for (i = 0; i < num_subsystems; ++i) { + if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) { + initialized |= (1 << i); + } + + flags >>= 1; + } + + return initialized; +} + +void +SDL_Quit(void) +{ + SDL_bInMainQuit = SDL_TRUE; + + /* Quit all subsystems */ +#if SDL_VIDEO_DRIVER_WINDOWS + SDL_HelperWindowDestroy(); +#endif + SDL_QuitSubSystem(SDL_INIT_EVERYTHING); + +#if !SDL_TIMERS_DISABLED + SDL_TicksQuit(); +#endif + + SDL_ClearHints(); + SDL_AssertionsQuit(); + SDL_LogResetPriorities(); + + /* Now that every subsystem has been quit, we reset the subsystem refcount + * and the list of initialized subsystems. + */ + SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) ); + + SDL_bInMainQuit = SDL_FALSE; +} + +/* Get the library version number */ +void +SDL_GetVersion(SDL_version * ver) +{ + SDL_VERSION(ver); +} + +/* Get the library source revision */ +const char * +SDL_GetRevision(void) +{ + return SDL_REVISION; +} + +/* Get the library source revision number */ +int +SDL_GetRevisionNumber(void) +{ + return SDL_REVISION_NUMBER; +} + +/* Get the name of the platform */ +const char * +SDL_GetPlatform() +{ +#if __AIX__ + return "AIX"; +#elif __ANDROID__ + return "Android"; +#elif __BSDI__ + return "BSDI"; +#elif __DREAMCAST__ + return "Dreamcast"; +#elif __EMSCRIPTEN__ + return "Emscripten"; +#elif __FREEBSD__ + return "FreeBSD"; +#elif __HAIKU__ + return "Haiku"; +#elif __HPUX__ + return "HP-UX"; +#elif __IRIX__ + return "Irix"; +#elif __LINUX__ + return "Linux"; +#elif __MINT__ + return "Atari MiNT"; +#elif __MACOS__ + return "MacOS Classic"; +#elif __MACOSX__ + return "Mac OS X"; +#elif __NACL__ + return "NaCl"; +#elif __NETBSD__ + return "NetBSD"; +#elif __OPENBSD__ + return "OpenBSD"; +#elif __OS2__ + return "OS/2"; +#elif __OSF__ + return "OSF/1"; +#elif __QNXNTO__ + return "QNX Neutrino"; +#elif __RISCOS__ + return "RISC OS"; +#elif __SOLARIS__ + return "Solaris"; +#elif __WIN32__ + return "Windows"; +#elif __WINRT__ + return "WinRT"; +#elif __TVOS__ + return "tvOS"; +#elif __IPHONEOS__ + return "iOS"; +#elif __PSP__ + return "PlayStation Portable"; +#else + return "Unknown (see SDL_platform.h)"; +#endif +} + +#if defined(__WIN32__) + +#if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB) +/* Need to include DllMain() on Watcom C for some reason.. */ + +BOOL APIENTRY +_DllMainCRTStartup(HANDLE hModule, + DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} +#endif /* Building DLL */ + +#endif /* __WIN32__ */ + +/* vi: set sts=4 ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_assert.c b/ThirdParty/SDL2/src/SDL_assert.c new file mode 100644 index 0000000..76f5d60 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_assert.c @@ -0,0 +1,441 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +#if defined(__WIN32__) +#include "core/windows/SDL_windows.h" +#endif + +#include "SDL.h" +#include "SDL_atomic.h" +#include "SDL_messagebox.h" +#include "SDL_video.h" +#include "SDL_assert.h" +#include "SDL_assert_c.h" +#include "video/SDL_sysvideo.h" + +#ifdef __WIN32__ +#ifndef WS_OVERLAPPEDWINDOW +#define WS_OVERLAPPEDWINDOW 0 +#endif +#else /* fprintf, _exit(), etc. */ +#include +#include +#if ! defined(__WINRT__) +#include +#endif +#endif + +#if defined(__EMSCRIPTEN__) +#include +#endif + + +static SDL_assert_state SDLCALL +SDL_PromptAssertion(const SDL_assert_data *data, void *userdata); + +/* + * We keep all triggered assertions in a singly-linked list so we can + * generate a report later. + */ +static SDL_assert_data *triggered_assertions = NULL; + +#ifndef SDL_THREADS_DISABLED +static SDL_mutex *assertion_mutex = NULL; +#endif + +static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion; +static void *assertion_userdata = NULL; + +#ifdef __GNUC__ +static void +debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2))); +#endif + +static void +debug_print(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap); + va_end(ap); +} + + +static void SDL_AddAssertionToReport(SDL_assert_data *data) +{ + /* (data) is always a static struct defined with the assert macros, so + we don't have to worry about copying or allocating them. */ + data->trigger_count++; + if (data->trigger_count == 1) { /* not yet added? */ + data->next = triggered_assertions; + triggered_assertions = data; + } +} + + +static void SDL_GenerateAssertionReport(void) +{ + const SDL_assert_data *item = triggered_assertions; + + /* only do this if the app hasn't assigned an assertion handler. */ + if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) { + debug_print("\n\nSDL assertion report.\n"); + debug_print("All SDL assertions between last init/quit:\n\n"); + + while (item != NULL) { + debug_print( + "'%s'\n" + " * %s (%s:%d)\n" + " * triggered %u time%s.\n" + " * always ignore: %s.\n", + item->condition, item->function, item->filename, + item->linenum, item->trigger_count, + (item->trigger_count == 1) ? "" : "s", + item->always_ignore ? "yes" : "no"); + item = item->next; + } + debug_print("\n"); + + SDL_ResetAssertionReport(); + } +} + + +static SDL_NORETURN void SDL_ExitProcess(int exitcode) +{ +#ifdef __WIN32__ + ExitProcess(exitcode); +#elif defined(__EMSCRIPTEN__) + emscripten_cancel_main_loop(); /* this should "kill" the app. */ + emscripten_force_exit(exitcode); /* this should "kill" the app. */ + exit(exitcode); +#else + _exit(exitcode); +#endif +} + + +static SDL_NORETURN void SDL_AbortAssertion(void) +{ + SDL_Quit(); + SDL_ExitProcess(42); +} + + +static SDL_assert_state SDLCALL +SDL_PromptAssertion(const SDL_assert_data *data, void *userdata) +{ +#ifdef __WIN32__ + #define ENDLINE "\r\n" +#else + #define ENDLINE "\n" +#endif + + const char *envr; + SDL_assert_state state = SDL_ASSERTION_ABORT; + SDL_Window *window; + SDL_MessageBoxData messagebox; + SDL_MessageBoxButtonData buttons[] = { + { 0, SDL_ASSERTION_RETRY, "Retry" }, + { 0, SDL_ASSERTION_BREAK, "Break" }, + { 0, SDL_ASSERTION_ABORT, "Abort" }, + { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, + SDL_ASSERTION_IGNORE, "Ignore" }, + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, + SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" } + }; + char *message; + int selected; + + (void) userdata; /* unused in default handler. */ + + message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); + if (!message) { + /* Uh oh, we're in real trouble now... */ + return SDL_ASSERTION_ABORT; + } + SDL_snprintf(message, SDL_MAX_LOG_MESSAGE, + "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE + " '%s'", + data->function, data->filename, data->linenum, + data->trigger_count, (data->trigger_count == 1) ? "time" : "times", + data->condition); + + debug_print("\n\n%s\n\n", message); + + /* let env. variable override, so unit tests won't block in a GUI. */ + envr = SDL_getenv("SDL_ASSERT"); + if (envr != NULL) { + SDL_stack_free(message); + + if (SDL_strcmp(envr, "abort") == 0) { + return SDL_ASSERTION_ABORT; + } else if (SDL_strcmp(envr, "break") == 0) { + return SDL_ASSERTION_BREAK; + } else if (SDL_strcmp(envr, "retry") == 0) { + return SDL_ASSERTION_RETRY; + } else if (SDL_strcmp(envr, "ignore") == 0) { + return SDL_ASSERTION_IGNORE; + } else if (SDL_strcmp(envr, "always_ignore") == 0) { + return SDL_ASSERTION_ALWAYS_IGNORE; + } else { + return SDL_ASSERTION_ABORT; /* oh well. */ + } + } + + /* Leave fullscreen mode, if possible (scary!) */ + window = SDL_GetFocusWindow(); + if (window) { + if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) { + SDL_MinimizeWindow(window); + } else { + /* !!! FIXME: ungrab the input if we're not fullscreen? */ + /* No need to mess with the window */ + window = NULL; + } + } + + /* Show a messagebox if we can, otherwise fall back to stdio */ + SDL_zero(messagebox); + messagebox.flags = SDL_MESSAGEBOX_WARNING; + messagebox.window = window; + messagebox.title = "Assertion Failed"; + messagebox.message = message; + messagebox.numbuttons = SDL_arraysize(buttons); + messagebox.buttons = buttons; + + if (SDL_ShowMessageBox(&messagebox, &selected) == 0) { + if (selected == -1) { + state = SDL_ASSERTION_IGNORE; + } else { + state = (SDL_assert_state)selected; + } + } + + else + { +#if defined(__EMSCRIPTEN__) + /* This is nasty, but we can't block on a custom UI. */ + for ( ; ; ) { + SDL_bool okay = SDL_TRUE; + char *buf = (char *) EM_ASM_INT({ + var str = + Pointer_stringify($0) + '\n\n' + + 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :'; + var reply = window.prompt(str, "i"); + if (reply === null) { + reply = "i"; + } + return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL); + }, message); + + if (SDL_strcmp(buf, "a") == 0) { + state = SDL_ASSERTION_ABORT; + /* (currently) no break functionality on Emscripten + } else if (SDL_strcmp(buf, "b") == 0) { + state = SDL_ASSERTION_BREAK; */ + } else if (SDL_strcmp(buf, "r") == 0) { + state = SDL_ASSERTION_RETRY; + } else if (SDL_strcmp(buf, "i") == 0) { + state = SDL_ASSERTION_IGNORE; + } else if (SDL_strcmp(buf, "A") == 0) { + state = SDL_ASSERTION_ALWAYS_IGNORE; + } else { + okay = SDL_FALSE; + } + free(buf); + + if (okay) { + break; + } + } +#elif defined(HAVE_STDIO_H) + /* this is a little hacky. */ + for ( ; ; ) { + char buf[32]; + fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : "); + fflush(stderr); + if (fgets(buf, sizeof (buf), stdin) == NULL) { + break; + } + + if (SDL_strncmp(buf, "a", 1) == 0) { + state = SDL_ASSERTION_ABORT; + break; + } else if (SDL_strncmp(buf, "b", 1) == 0) { + state = SDL_ASSERTION_BREAK; + break; + } else if (SDL_strncmp(buf, "r", 1) == 0) { + state = SDL_ASSERTION_RETRY; + break; + } else if (SDL_strncmp(buf, "i", 1) == 0) { + state = SDL_ASSERTION_IGNORE; + break; + } else if (SDL_strncmp(buf, "A", 1) == 0) { + state = SDL_ASSERTION_ALWAYS_IGNORE; + break; + } + } +#endif /* HAVE_STDIO_H */ + } + + /* Re-enter fullscreen mode */ + if (window) { + SDL_RestoreWindow(window); + } + + SDL_stack_free(message); + + return state; +} + + +SDL_assert_state +SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file, + int line) +{ + SDL_assert_state state = SDL_ASSERTION_IGNORE; + static int assertion_running = 0; + +#ifndef SDL_THREADS_DISABLED + static SDL_SpinLock spinlock = 0; + SDL_AtomicLock(&spinlock); + if (assertion_mutex == NULL) { /* never called SDL_Init()? */ + assertion_mutex = SDL_CreateMutex(); + if (assertion_mutex == NULL) { + SDL_AtomicUnlock(&spinlock); + return SDL_ASSERTION_IGNORE; /* oh well, I guess. */ + } + } + SDL_AtomicUnlock(&spinlock); + + if (SDL_LockMutex(assertion_mutex) < 0) { + return SDL_ASSERTION_IGNORE; /* oh well, I guess. */ + } +#endif + + /* doing this because Visual C is upset over assigning in the macro. */ + if (data->trigger_count == 0) { + data->function = func; + data->filename = file; + data->linenum = line; + } + + SDL_AddAssertionToReport(data); + + assertion_running++; + if (assertion_running > 1) { /* assert during assert! Abort. */ + if (assertion_running == 2) { + SDL_AbortAssertion(); + } else if (assertion_running == 3) { /* Abort asserted! */ + SDL_ExitProcess(42); + } else { + while (1) { /* do nothing but spin; what else can you do?! */ } + } + } + + if (!data->always_ignore) { + state = assertion_handler(data, assertion_userdata); + } + + switch (state) + { + case SDL_ASSERTION_ABORT: + SDL_AbortAssertion(); + return SDL_ASSERTION_IGNORE; /* shouldn't return, but oh well. */ + + case SDL_ASSERTION_ALWAYS_IGNORE: + state = SDL_ASSERTION_IGNORE; + data->always_ignore = 1; + break; + + case SDL_ASSERTION_IGNORE: + case SDL_ASSERTION_RETRY: + case SDL_ASSERTION_BREAK: + break; /* macro handles these. */ + } + + assertion_running--; + +#ifndef SDL_THREADS_DISABLED + SDL_UnlockMutex(assertion_mutex); +#endif + + return state; +} + + +void SDL_AssertionsQuit(void) +{ + SDL_GenerateAssertionReport(); +#ifndef SDL_THREADS_DISABLED + if (assertion_mutex != NULL) { + SDL_DestroyMutex(assertion_mutex); + assertion_mutex = NULL; + } +#endif +} + +void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata) +{ + if (handler != NULL) { + assertion_handler = handler; + assertion_userdata = userdata; + } else { + assertion_handler = SDL_PromptAssertion; + assertion_userdata = NULL; + } +} + +const SDL_assert_data *SDL_GetAssertionReport(void) +{ + return triggered_assertions; +} + +void SDL_ResetAssertionReport(void) +{ + SDL_assert_data *next = NULL; + SDL_assert_data *item; + for (item = triggered_assertions; item != NULL; item = next) { + next = (SDL_assert_data *) item->next; + item->always_ignore = SDL_FALSE; + item->trigger_count = 0; + item->next = NULL; + } + + triggered_assertions = NULL; +} + +SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void) +{ + return SDL_PromptAssertion; +} + +SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata) +{ + if (userdata != NULL) { + *userdata = assertion_userdata; + } + return assertion_handler; +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_assert_c.h b/ThirdParty/SDL2/src/SDL_assert_c.h new file mode 100644 index 0000000..aa690a3 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_assert_c.h @@ -0,0 +1,24 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +extern void SDL_AssertionsQuit(void); + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_dataqueue.c b/ThirdParty/SDL2/src/SDL_dataqueue.c new file mode 100644 index 0000000..97916f4 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_dataqueue.c @@ -0,0 +1,339 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "./SDL_internal.h" +#include "SDL.h" +#include "./SDL_dataqueue.h" +#include "SDL_assert.h" + +typedef struct SDL_DataQueuePacket +{ + size_t datalen; /* bytes currently in use in this packet. */ + size_t startpos; /* bytes currently consumed in this packet. */ + struct SDL_DataQueuePacket *next; /* next item in linked list. */ + Uint8 data[SDL_VARIABLE_LENGTH_ARRAY]; /* packet data */ +} SDL_DataQueuePacket; + +struct SDL_DataQueue +{ + SDL_DataQueuePacket *head; /* device fed from here. */ + SDL_DataQueuePacket *tail; /* queue fills to here. */ + SDL_DataQueuePacket *pool; /* these are unused packets. */ + size_t packet_size; /* size of new packets */ + size_t queued_bytes; /* number of bytes of data in the queue. */ +}; + +static void +SDL_FreeDataQueueList(SDL_DataQueuePacket *packet) +{ + while (packet) { + SDL_DataQueuePacket *next = packet->next; + SDL_free(packet); + packet = next; + } +} + + +/* this all expects that you managed thread safety elsewhere. */ + +SDL_DataQueue * +SDL_NewDataQueue(const size_t _packetlen, const size_t initialslack) +{ + SDL_DataQueue *queue = (SDL_DataQueue *) SDL_malloc(sizeof (SDL_DataQueue)); + + if (!queue) { + SDL_OutOfMemory(); + return NULL; + } else { + const size_t packetlen = _packetlen ? _packetlen : 1024; + const size_t wantpackets = (initialslack + (packetlen - 1)) / packetlen; + size_t i; + + SDL_zerop(queue); + queue->packet_size = packetlen; + + for (i = 0; i < wantpackets; i++) { + SDL_DataQueuePacket *packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + packetlen); + if (packet) { /* don't care if this fails, we'll deal later. */ + packet->datalen = 0; + packet->startpos = 0; + packet->next = queue->pool; + queue->pool = packet; + } + } + } + + return queue; +} + +void +SDL_FreeDataQueue(SDL_DataQueue *queue) +{ + if (queue) { + SDL_FreeDataQueueList(queue->head); + SDL_FreeDataQueueList(queue->pool); + SDL_free(queue); + } +} + +void +SDL_ClearDataQueue(SDL_DataQueue *queue, const size_t slack) +{ + const size_t packet_size = queue ? queue->packet_size : 1; + const size_t slackpackets = (slack + (packet_size-1)) / packet_size; + SDL_DataQueuePacket *packet; + SDL_DataQueuePacket *prev = NULL; + size_t i; + + if (!queue) { + return; + } + + packet = queue->head; + + /* merge the available pool and the current queue into one list. */ + if (packet) { + queue->tail->next = queue->pool; + } else { + packet = queue->pool; + } + + /* Remove the queued packets from the device. */ + queue->tail = NULL; + queue->head = NULL; + queue->queued_bytes = 0; + queue->pool = packet; + + /* Optionally keep some slack in the pool to reduce malloc pressure. */ + for (i = 0; packet && (i < slackpackets); i++) { + prev = packet; + packet = packet->next; + } + + if (prev) { + prev->next = NULL; + } else { + queue->pool = NULL; + } + + SDL_FreeDataQueueList(packet); /* free extra packets */ +} + +static SDL_DataQueuePacket * +AllocateDataQueuePacket(SDL_DataQueue *queue) +{ + SDL_DataQueuePacket *packet; + + SDL_assert(queue != NULL); + + packet = queue->pool; + if (packet != NULL) { + /* we have one available in the pool. */ + queue->pool = packet->next; + } else { + /* Have to allocate a new one! */ + packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + queue->packet_size); + if (packet == NULL) { + return NULL; + } + } + + packet->datalen = 0; + packet->startpos = 0; + packet->next = NULL; + + SDL_assert((queue->head != NULL) == (queue->queued_bytes != 0)); + if (queue->tail == NULL) { + queue->head = packet; + } else { + queue->tail->next = packet; + } + queue->tail = packet; + return packet; +} + + +int +SDL_WriteToDataQueue(SDL_DataQueue *queue, const void *_data, const size_t _len) +{ + size_t len = _len; + const Uint8 *data = (const Uint8 *) _data; + const size_t packet_size = queue ? queue->packet_size : 0; + SDL_DataQueuePacket *orighead; + SDL_DataQueuePacket *origtail; + size_t origlen; + size_t datalen; + + if (!queue) { + return SDL_InvalidParamError("queue"); + } + + orighead = queue->head; + origtail = queue->tail; + origlen = origtail ? origtail->datalen : 0; + + while (len > 0) { + SDL_DataQueuePacket *packet = queue->tail; + SDL_assert(!packet || (packet->datalen <= packet_size)); + if (!packet || (packet->datalen >= packet_size)) { + /* tail packet missing or completely full; we need a new packet. */ + packet = AllocateDataQueuePacket(queue); + if (!packet) { + /* uhoh, reset so we've queued nothing new, free what we can. */ + if (!origtail) { + packet = queue->head; /* whole queue. */ + } else { + packet = origtail->next; /* what we added to existing queue. */ + origtail->next = NULL; + origtail->datalen = origlen; + } + queue->head = orighead; + queue->tail = origtail; + queue->pool = NULL; + + SDL_FreeDataQueueList(packet); /* give back what we can. */ + return SDL_OutOfMemory(); + } + } + + datalen = SDL_min(len, packet_size - packet->datalen); + SDL_memcpy(packet->data + packet->datalen, data, datalen); + data += datalen; + len -= datalen; + packet->datalen += datalen; + queue->queued_bytes += datalen; + } + + return 0; +} + +size_t +SDL_PeekIntoDataQueue(SDL_DataQueue *queue, void *_buf, const size_t _len) +{ + size_t len = _len; + Uint8 *buf = (Uint8 *) _buf; + Uint8 *ptr = buf; + SDL_DataQueuePacket *packet; + + if (!queue) { + return 0; + } + + for (packet = queue->head; len && packet; packet = packet->next) { + const size_t avail = packet->datalen - packet->startpos; + const size_t cpy = SDL_min(len, avail); + SDL_assert(queue->queued_bytes >= avail); + + SDL_memcpy(ptr, packet->data + packet->startpos, cpy); + ptr += cpy; + len -= cpy; + } + + return (size_t) (ptr - buf); +} + +size_t +SDL_ReadFromDataQueue(SDL_DataQueue *queue, void *_buf, const size_t _len) +{ + size_t len = _len; + Uint8 *buf = (Uint8 *) _buf; + Uint8 *ptr = buf; + SDL_DataQueuePacket *packet; + + if (!queue) { + return 0; + } + + while ((len > 0) && ((packet = queue->head) != NULL)) { + const size_t avail = packet->datalen - packet->startpos; + const size_t cpy = SDL_min(len, avail); + SDL_assert(queue->queued_bytes >= avail); + + SDL_memcpy(ptr, packet->data + packet->startpos, cpy); + packet->startpos += cpy; + ptr += cpy; + queue->queued_bytes -= cpy; + len -= cpy; + + if (packet->startpos == packet->datalen) { /* packet is done, put it in the pool. */ + queue->head = packet->next; + SDL_assert((packet->next != NULL) || (packet == queue->tail)); + packet->next = queue->pool; + queue->pool = packet; + } + } + + SDL_assert((queue->head != NULL) == (queue->queued_bytes != 0)); + + if (queue->head == NULL) { + queue->tail = NULL; /* in case we drained the queue entirely. */ + } + + return (size_t) (ptr - buf); +} + +size_t +SDL_CountDataQueue(SDL_DataQueue *queue) +{ + return queue ? queue->queued_bytes : 0; +} + +void * +SDL_ReserveSpaceInDataQueue(SDL_DataQueue *queue, const size_t len) +{ + SDL_DataQueuePacket *packet; + + if (!queue) { + SDL_InvalidParamError("queue"); + return NULL; + } else if (len == 0) { + SDL_InvalidParamError("len"); + return NULL; + } else if (len > queue->packet_size) { + SDL_SetError("len is larger than packet size"); + return NULL; + } + + packet = queue->head; + if (packet) { + const size_t avail = queue->packet_size - packet->datalen; + if (len <= avail) { /* we can use the space at end of this packet. */ + void *retval = packet->data + packet->datalen; + packet->datalen += len; + queue->queued_bytes += len; + return retval; + } + } + + /* Need a fresh packet. */ + packet = AllocateDataQueuePacket(queue); + if (!packet) { + SDL_OutOfMemory(); + return NULL; + } + + packet->datalen = len; + queue->queued_bytes += len; + return packet->data; +} + +/* vi: set ts=4 sw=4 expandtab: */ + diff --git a/ThirdParty/SDL2/src/SDL_dataqueue.h b/ThirdParty/SDL2/src/SDL_dataqueue.h new file mode 100644 index 0000000..d44f58d --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_dataqueue.h @@ -0,0 +1,55 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#ifndef SDL_dataqueue_h_ +#define SDL_dataqueue_h_ + +/* this is not (currently) a public API. But maybe it should be! */ + +struct SDL_DataQueue; +typedef struct SDL_DataQueue SDL_DataQueue; + +SDL_DataQueue *SDL_NewDataQueue(const size_t packetlen, const size_t initialslack); +void SDL_FreeDataQueue(SDL_DataQueue *queue); +void SDL_ClearDataQueue(SDL_DataQueue *queue, const size_t slack); +int SDL_WriteToDataQueue(SDL_DataQueue *queue, const void *data, const size_t len); +size_t SDL_ReadFromDataQueue(SDL_DataQueue *queue, void *buf, const size_t len); +size_t SDL_PeekIntoDataQueue(SDL_DataQueue *queue, void *buf, const size_t len); +size_t SDL_CountDataQueue(SDL_DataQueue *queue); + +/* this sets a section of the data queue aside (possibly allocating memory for it) + as if it's been written to, but returns a pointer to that space. You may write + to this space until a read would consume it. Writes (and other calls to this + function) will safely append their data after this reserved space and can + be in flight at the same time. There is no thread safety. + If there isn't an existing block of memory that can contain the reserved + space, one will be allocated for it. You can not (currently) allocate + a space larger than the packetlen requested in SDL_NewDataQueue. + Returned buffer is uninitialized. + This lets you avoid an extra copy in some cases, but it's safer to use + SDL_WriteToDataQueue() unless you know what you're doing. + Returns pointer to buffer of at least (len) bytes, NULL on error. +*/ +void *SDL_ReserveSpaceInDataQueue(SDL_DataQueue *queue, const size_t len); + +#endif /* SDL_dataqueue_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ + diff --git a/ThirdParty/SDL2/src/SDL_error.c b/ThirdParty/SDL2/src/SDL_error.c new file mode 100644 index 0000000..14761c5 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_error.c @@ -0,0 +1,319 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +/* Simple error handling in SDL */ + +#include "SDL_log.h" +#include "SDL_error.h" +#include "SDL_error_c.h" + + +/* Routine to get the thread-specific error variable */ +#if SDL_THREADS_DISABLED +/* The default (non-thread-safe) global error variable */ +static SDL_error SDL_global_error; +#define SDL_GetErrBuf() (&SDL_global_error) +#else +extern SDL_error *SDL_GetErrBuf(void); +#endif /* SDL_THREADS_DISABLED */ + +#define SDL_ERRBUFIZE 1024 + +/* Private functions */ + +static const char * +SDL_LookupString(const char *key) +{ + /* FIXME: Add code to lookup key in language string hash-table */ + return key; +} + +/* Public functions */ + +static char *SDL_GetErrorMsg(char *errstr, int maxlen); + +int +SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + SDL_error *error; + + /* Ignore call if invalid format pointer was passed */ + if (fmt == NULL) return -1; + + /* Copy in the key, mark error as valid */ + error = SDL_GetErrBuf(); + error->error = 1; + SDL_strlcpy((char *) error->key, fmt, sizeof(error->key)); + + va_start(ap, fmt); + error->argc = 0; + while (*fmt) { + if (*fmt++ == '%') { + while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) { + ++fmt; + } + switch (*fmt++) { + case 0: /* Malformed format string.. */ + --fmt; + break; + case 'l': + switch (*fmt++) { + case 0: /* Malformed format string.. */ + --fmt; + break; + case 'i': case 'd': case 'u': + error->args[error->argc++].value_l = va_arg(ap, long); + break; + } + break; + case 'c': + case 'i': + case 'd': + case 'u': + case 'o': + case 'x': + case 'X': + error->args[error->argc++].value_i = va_arg(ap, int); + break; + case 'f': + error->args[error->argc++].value_f = va_arg(ap, double); + break; + case 'p': + error->args[error->argc++].value_ptr = va_arg(ap, void *); + break; + case 's': + { + int i = error->argc; + const char *str = va_arg(ap, const char *); + if (str == NULL) + str = "(null)"; + SDL_strlcpy((char *) error->args[i].buf, str, + ERR_MAX_STRLEN); + error->argc++; + } + break; + default: + break; + } + if (error->argc >= ERR_MAX_ARGS) { + break; + } + } + } + va_end(ap); + + if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) { + /* If we are in debug mode, print out an error message + * Avoid stomping on the static buffer in GetError, just + * in case this is called while processing a ShowMessageBox to + * show an error already in that static buffer. + */ + char errmsg[SDL_ERRBUFIZE]; + SDL_GetErrorMsg(errmsg, sizeof(errmsg)); + SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg); + } + return -1; +} + +/* Available for backwards compatibility */ +const char * +SDL_GetError(void) +{ + static char errmsg[SDL_ERRBUFIZE]; + + return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE); +} + +void +SDL_ClearError(void) +{ + SDL_error *error; + + error = SDL_GetErrBuf(); + error->error = 0; +} + +/* Very common errors go here */ +int +SDL_Error(SDL_errorcode code) +{ + switch (code) { + case SDL_ENOMEM: + return SDL_SetError("Out of memory"); + case SDL_EFREAD: + return SDL_SetError("Error reading from datastream"); + case SDL_EFWRITE: + return SDL_SetError("Error writing to datastream"); + case SDL_EFSEEK: + return SDL_SetError("Error seeking in datastream"); + case SDL_UNSUPPORTED: + return SDL_SetError("That operation is not supported"); + default: + return SDL_SetError("Unknown SDL error"); + } +} + +#ifdef TEST_ERROR +int +main(int argc, char *argv[]) +{ + char buffer[BUFSIZ + 1]; + + SDL_SetError("Hi there!"); + printf("Error 1: %s\n", SDL_GetError()); + SDL_ClearError(); + SDL_memset(buffer, '1', BUFSIZ); + buffer[BUFSIZ] = 0; + SDL_SetError("This is the error: %s (%f)", buffer, 1.0); + printf("Error 2: %s\n", SDL_GetError()); + exit(0); +} +#endif + + +/* keep this at the end of the file so it works with GCC builds that don't + support "#pragma GCC diagnostic push" ... we'll just leave the warning + disabled after this. */ +/* this pragma arrived in GCC 4.2 and causes a warning on older GCCs! Sigh. */ +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 2)))) +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif + +/* This function has a bit more overhead than most error functions + so that it supports internationalization and thread-safe errors. +*/ +static char * +SDL_GetErrorMsg(char *errstr, int maxlen) +{ + SDL_error *error; + + /* Clear the error string */ + *errstr = '\0'; + --maxlen; + + /* Get the thread-safe error, and print it out */ + error = SDL_GetErrBuf(); + if (error->error) { + const char *fmt; + char *msg = errstr; + int len; + int argi; + + fmt = SDL_LookupString(error->key); + argi = 0; + while (*fmt && (maxlen > 0)) { + if (*fmt == '%') { + char tmp[32], *spot = tmp; + *spot++ = *fmt++; + while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) + && spot < (tmp + SDL_arraysize(tmp) - 2)) { + *spot++ = *fmt++; + } + if (*fmt == 'l') { + *spot++ = *fmt++; + *spot++ = *fmt++; + *spot++ = '\0'; + switch (spot[-2]) { + case 'i': case 'd': case 'u': + len = SDL_snprintf(msg, maxlen, tmp, + error->args[argi++].value_l); + if (len > 0) { + msg += len; + maxlen -= len; + } + break; + } + continue; + } + *spot++ = *fmt++; + *spot++ = '\0'; + switch (spot[-2]) { + case '%': + *msg++ = '%'; + maxlen -= 1; + break; + case 'c': + case 'i': + case 'd': + case 'u': + case 'o': + case 'x': + case 'X': + len = + SDL_snprintf(msg, maxlen, tmp, + error->args[argi++].value_i); + if (len > 0) { + msg += len; + maxlen -= len; + } + break; + + case 'f': + len = + SDL_snprintf(msg, maxlen, tmp, + error->args[argi++].value_f); + if (len > 0) { + msg += len; + maxlen -= len; + } + break; + + case 'p': + len = + SDL_snprintf(msg, maxlen, tmp, + error->args[argi++].value_ptr); + if (len > 0) { + msg += len; + maxlen -= len; + } + break; + + case 's': + len = + SDL_snprintf(msg, maxlen, tmp, + SDL_LookupString(error->args[argi++]. + buf)); + if (len > 0) { + msg += len; + maxlen -= len; + } + break; + + } + } else { + *msg++ = *fmt++; + maxlen -= 1; + } + } + + /* slide back if we've overshot the end of our buffer. */ + if (maxlen < 0) { + msg -= (-maxlen) + 1; + } + + *msg = 0; /* NULL terminate the string */ + } + return (errstr); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_error_c.h b/ThirdParty/SDL2/src/SDL_error_c.h new file mode 100644 index 0000000..6bb9caa --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_error_c.h @@ -0,0 +1,65 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +/* This file defines a structure that carries language-independent + error messages +*/ + +#ifndef SDL_error_c_h_ +#define SDL_error_c_h_ + +#define ERR_MAX_STRLEN 128 +#define ERR_MAX_ARGS 5 + +typedef struct SDL_error +{ + /* This is a numeric value corresponding to the current error */ + int error; + + /* This is a key used to index into a language hashtable containing + internationalized versions of the SDL error messages. If the key + is not in the hashtable, or no hashtable is available, the key is + used directly as an error message format string. + */ + char key[ERR_MAX_STRLEN]; + + /* These are the arguments for the error functions */ + int argc; + union + { + void *value_ptr; +#if 0 /* What is a character anyway? (UNICODE issues) */ + unsigned char value_c; +#endif + int value_i; + long value_l; + double value_f; + char buf[ERR_MAX_STRLEN]; + } args[ERR_MAX_ARGS]; +} SDL_error; + +/* Defined in SDL_thread.c */ +extern SDL_error *SDL_GetErrBuf(void); + +#endif /* SDL_error_c_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_hints.c b/ThirdParty/SDL2/src/SDL_hints.c new file mode 100644 index 0000000..09689aa --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_hints.c @@ -0,0 +1,236 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +#include "SDL_hints.h" +#include "SDL_error.h" + + +/* Assuming there aren't many hints set and they aren't being queried in + critical performance paths, we'll just use linked lists here. + */ +typedef struct SDL_HintWatch { + SDL_HintCallback callback; + void *userdata; + struct SDL_HintWatch *next; +} SDL_HintWatch; + +typedef struct SDL_Hint { + char *name; + char *value; + SDL_HintPriority priority; + SDL_HintWatch *callbacks; + struct SDL_Hint *next; +} SDL_Hint; + +static SDL_Hint *SDL_hints; + +SDL_bool +SDL_SetHintWithPriority(const char *name, const char *value, + SDL_HintPriority priority) +{ + const char *env; + SDL_Hint *hint; + SDL_HintWatch *entry; + + if (!name || !value) { + return SDL_FALSE; + } + + env = SDL_getenv(name); + if (env && priority < SDL_HINT_OVERRIDE) { + return SDL_FALSE; + } + + for (hint = SDL_hints; hint; hint = hint->next) { + if (SDL_strcmp(name, hint->name) == 0) { + if (priority < hint->priority) { + return SDL_FALSE; + } + if (!hint->value || !value || SDL_strcmp(hint->value, value) != 0) { + for (entry = hint->callbacks; entry; ) { + /* Save the next entry in case this one is deleted */ + SDL_HintWatch *next = entry->next; + entry->callback(entry->userdata, name, hint->value, value); + entry = next; + } + SDL_free(hint->value); + hint->value = value ? SDL_strdup(value) : NULL; + } + hint->priority = priority; + return SDL_TRUE; + } + } + + /* Couldn't find the hint, add a new one */ + hint = (SDL_Hint *)SDL_malloc(sizeof(*hint)); + if (!hint) { + return SDL_FALSE; + } + hint->name = SDL_strdup(name); + hint->value = value ? SDL_strdup(value) : NULL; + hint->priority = priority; + hint->callbacks = NULL; + hint->next = SDL_hints; + SDL_hints = hint; + return SDL_TRUE; +} + +SDL_bool +SDL_SetHint(const char *name, const char *value) +{ + return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL); +} + +const char * +SDL_GetHint(const char *name) +{ + const char *env; + SDL_Hint *hint; + + env = SDL_getenv(name); + for (hint = SDL_hints; hint; hint = hint->next) { + if (SDL_strcmp(name, hint->name) == 0) { + if (!env || hint->priority == SDL_HINT_OVERRIDE) { + return hint->value; + } + break; + } + } + return env; +} + +SDL_bool +SDL_GetHintBoolean(const char *name, SDL_bool default_value) +{ + const char *hint = SDL_GetHint(name); + if (!hint || !*hint) { + return default_value; + } + if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) { + return SDL_FALSE; + } + return SDL_TRUE; +} + +void +SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata) +{ + SDL_Hint *hint; + SDL_HintWatch *entry; + const char *value; + + if (!name || !*name) { + SDL_InvalidParamError("name"); + return; + } + if (!callback) { + SDL_InvalidParamError("callback"); + return; + } + + SDL_DelHintCallback(name, callback, userdata); + + entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry)); + if (!entry) { + SDL_OutOfMemory(); + return; + } + entry->callback = callback; + entry->userdata = userdata; + + for (hint = SDL_hints; hint; hint = hint->next) { + if (SDL_strcmp(name, hint->name) == 0) { + break; + } + } + if (!hint) { + /* Need to add a hint entry for this watcher */ + hint = (SDL_Hint *)SDL_malloc(sizeof(*hint)); + if (!hint) { + SDL_OutOfMemory(); + SDL_free(entry); + return; + } + hint->name = SDL_strdup(name); + hint->value = NULL; + hint->priority = SDL_HINT_DEFAULT; + hint->callbacks = NULL; + hint->next = SDL_hints; + SDL_hints = hint; + } + + /* Add it to the callbacks for this hint */ + entry->next = hint->callbacks; + hint->callbacks = entry; + + /* Now call it with the current value */ + value = SDL_GetHint(name); + callback(userdata, name, value, value); +} + +void +SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata) +{ + SDL_Hint *hint; + SDL_HintWatch *entry, *prev; + + for (hint = SDL_hints; hint; hint = hint->next) { + if (SDL_strcmp(name, hint->name) == 0) { + prev = NULL; + for (entry = hint->callbacks; entry; entry = entry->next) { + if (callback == entry->callback && userdata == entry->userdata) { + if (prev) { + prev->next = entry->next; + } else { + hint->callbacks = entry->next; + } + SDL_free(entry); + break; + } + prev = entry; + } + return; + } + } +} + +void SDL_ClearHints(void) +{ + SDL_Hint *hint; + SDL_HintWatch *entry; + + while (SDL_hints) { + hint = SDL_hints; + SDL_hints = hint->next; + + SDL_free(hint->name); + SDL_free(hint->value); + for (entry = hint->callbacks; entry; ) { + SDL_HintWatch *freeable = entry; + entry = entry->next; + SDL_free(freeable); + } + SDL_free(hint); + } +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_internal.h b/ThirdParty/SDL2/src/SDL_internal.h new file mode 100644 index 0000000..e0ba2a8 --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_internal.h @@ -0,0 +1,52 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#ifndef SDL_internal_h_ +#define SDL_internal_h_ + +/* Many of SDL's features require _GNU_SOURCE on various platforms */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +/* This is for a variable-length array at the end of a struct: + struct x { int y; char z[SDL_VARIABLE_LENGTH_ARRAY]; }; + Use this because GCC 2 needs different magic than other compilers. */ +#if (defined(__GNUC__) && (__GNUC__ <= 2)) || defined(__CC_ARM) || defined(__cplusplus) +#define SDL_VARIABLE_LENGTH_ARRAY 1 +#else +#define SDL_VARIABLE_LENGTH_ARRAY +#endif + +#include "dynapi/SDL_dynapi.h" + +#if SDL_DYNAMIC_API +#include "dynapi/SDL_dynapi_overrides.h" +/* force DECLSPEC and SDLCALL off...it's all internal symbols now. + These will have actual #defines during SDL_dynapi.c only */ +#define DECLSPEC +#define SDLCALL +#endif + +#include "SDL_config.h" + +#endif /* SDL_internal_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/SDL_log.c b/ThirdParty/SDL2/src/SDL_log.c new file mode 100644 index 0000000..b1bf27d --- /dev/null +++ b/ThirdParty/SDL2/src/SDL_log.c @@ -0,0 +1,450 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "./SDL_internal.h" + +#if defined(__WIN32__) || defined(__WINRT__) +#include "core/windows/SDL_windows.h" +#endif + +/* Simple log messages in SDL */ + +#include "SDL_error.h" +#include "SDL_log.h" + +#if HAVE_STDIO_H +#include +#endif + +#if defined(__ANDROID__) +#include +#endif + +#define DEFAULT_PRIORITY SDL_LOG_PRIORITY_CRITICAL +#define DEFAULT_ASSERT_PRIORITY SDL_LOG_PRIORITY_WARN +#define DEFAULT_APPLICATION_PRIORITY SDL_LOG_PRIORITY_INFO +#define DEFAULT_TEST_PRIORITY SDL_LOG_PRIORITY_VERBOSE + +typedef struct SDL_LogLevel +{ + int category; + SDL_LogPriority priority; + struct SDL_LogLevel *next; +} SDL_LogLevel; + +/* The default log output function */ +static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message); + +static SDL_LogLevel *SDL_loglevels; +static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY; +static SDL_LogPriority SDL_assert_priority = DEFAULT_ASSERT_PRIORITY; +static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; +static SDL_LogPriority SDL_test_priority = DEFAULT_TEST_PRIORITY; +static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput; +static void *SDL_log_userdata = NULL; + +static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = { + NULL, + "VERBOSE", + "DEBUG", + "INFO", + "WARN", + "ERROR", + "CRITICAL" +}; + +#ifdef __ANDROID__ +static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = { + "APP", + "ERROR", + "SYSTEM", + "AUDIO", + "VIDEO", + "RENDER", + "INPUT" +}; + +static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = { + ANDROID_LOG_UNKNOWN, + ANDROID_LOG_VERBOSE, + ANDROID_LOG_DEBUG, + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR, + ANDROID_LOG_FATAL +}; +#endif /* __ANDROID__ */ + + +void +SDL_LogSetAllPriority(SDL_LogPriority priority) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + entry->priority = priority; + } + SDL_default_priority = priority; + SDL_assert_priority = priority; + SDL_application_priority = priority; +} + +void +SDL_LogSetPriority(int category, SDL_LogPriority priority) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + if (entry->category == category) { + entry->priority = priority; + return; + } + } + + /* Create a new entry */ + entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry)); + if (entry) { + entry->category = category; + entry->priority = priority; + entry->next = SDL_loglevels; + SDL_loglevels = entry; + } +} + +SDL_LogPriority +SDL_LogGetPriority(int category) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + if (entry->category == category) { + return entry->priority; + } + } + + if (category == SDL_LOG_CATEGORY_TEST) { + return SDL_test_priority; + } else if (category == SDL_LOG_CATEGORY_APPLICATION) { + return SDL_application_priority; + } else if (category == SDL_LOG_CATEGORY_ASSERT) { + return SDL_assert_priority; + } else { + return SDL_default_priority; + } +} + +void +SDL_LogResetPriorities(void) +{ + SDL_LogLevel *entry; + + while (SDL_loglevels) { + entry = SDL_loglevels; + SDL_loglevels = entry->next; + SDL_free(entry); + } + + SDL_default_priority = DEFAULT_PRIORITY; + SDL_assert_priority = DEFAULT_ASSERT_PRIORITY; + SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; + SDL_test_priority = DEFAULT_TEST_PRIORITY; +} + +void +SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); + va_end(ap); +} + +void +SDL_LogVerbose(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap); + va_end(ap); +} + +void +SDL_LogDebug(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap); + va_end(ap); +} + +void +SDL_LogInfo(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap); + va_end(ap); +} + +void +SDL_LogWarn(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap); + va_end(ap); +} + +void +SDL_LogError(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap); + va_end(ap); +} + +void +SDL_LogCritical(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap); + va_end(ap); +} + +void +SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, priority, fmt, ap); + va_end(ap); +} + +#ifdef __ANDROID__ +static const char * +GetCategoryPrefix(int category) +{ + if (category < SDL_LOG_CATEGORY_RESERVED1) { + return SDL_category_prefixes[category]; + } + if (category < SDL_LOG_CATEGORY_CUSTOM) { + return "RESERVED"; + } + return "CUSTOM"; +} +#endif /* __ANDROID__ */ + +void +SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) +{ + char *message; + size_t len; + + /* Nothing to do if we don't have an output function */ + if (!SDL_log_function) { + return; + } + + /* Make sure we don't exceed array bounds */ + if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) { + return; + } + + /* See if we want to do anything with this message */ + if (priority < SDL_LogGetPriority(category)) { + return; + } + + message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); + if (!message) { + return; + } + + SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap); + + /* Chop off final endline. */ + len = SDL_strlen(message); + if ((len > 0) && (message[len-1] == '\n')) { + message[--len] = '\0'; + if ((len > 0) && (message[len-1] == '\r')) { /* catch "\r\n", too. */ + message[--len] = '\0'; + } + } + + SDL_log_function(SDL_log_userdata, category, priority, message); + SDL_stack_free(message); +} + +#if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__) +/* Flag tracking the attachment of the console: 0=unattached, 1=attached to a console, 2=attached to a file, -1=error */ +static int consoleAttached = 0; + +/* Handle to stderr output of console. */ +static HANDLE stderrHandle = NULL; +#endif + +static void SDLCALL +SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, + const char *message) +{ +#if defined(__WIN32__) || defined(__WINRT__) + /* Way too many allocations here, urgh */ + /* Note: One can't call SDL_SetError here, since that function itself logs. */ + { + char *output; + size_t length; + LPTSTR tstr; + +#if !defined(HAVE_STDIO_H) && !defined(__WINRT__) + BOOL attachResult; + DWORD attachError; + unsigned long charsWritten; + DWORD consoleMode; + + /* Maybe attach console and get stderr handle */ + if (consoleAttached == 0) { + attachResult = AttachConsole(ATTACH_PARENT_PROCESS); + if (!attachResult) { + attachError = GetLastError(); + if (attachError == ERROR_INVALID_HANDLE) { + /* This is expected when running from Visual Studio */ + /*OutputDebugString(TEXT("Parent process has no console\r\n"));*/ + consoleAttached = -1; + } else if (attachError == ERROR_GEN_FAILURE) { + OutputDebugString(TEXT("Could not attach to console of parent process\r\n")); + consoleAttached = -1; + } else if (attachError == ERROR_ACCESS_DENIED) { + /* Already attached */ + consoleAttached = 1; + } else { + OutputDebugString(TEXT("Error attaching console\r\n")); + consoleAttached = -1; + } + } else { + /* Newly attached */ + consoleAttached = 1; + } + + if (consoleAttached == 1) { + stderrHandle = GetStdHandle(STD_ERROR_HANDLE); + + if (GetConsoleMode(stderrHandle, &consoleMode) == 0) { + /* WriteConsole fails if the output is redirected to a file. Must use WriteFile instead. */ + consoleAttached = 2; + } + } + } +#endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */ + + length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1; + output = SDL_stack_alloc(char, length); + SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message); + tstr = WIN_UTF8ToString(output); + + /* Output to debugger */ + OutputDebugString(tstr); + +#if !defined(HAVE_STDIO_H) && !defined(__WINRT__) + /* Screen output to stderr, if console was attached. */ + if (consoleAttached == 1) { + if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) { + OutputDebugString(TEXT("Error calling WriteConsole\r\n")); + if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) { + OutputDebugString(TEXT("Insufficient heap memory to write message\r\n")); + } + } + + } else if (consoleAttached == 2) { + if (!WriteFile(stderrHandle, output, lstrlenA(output), &charsWritten, NULL)) { + OutputDebugString(TEXT("Error calling WriteFile\r\n")); + } + } +#endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */ + + SDL_free(tstr); + SDL_stack_free(output); + } +#elif defined(__ANDROID__) + { + char tag[32]; + + SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category)); + __android_log_write(SDL_android_priority[priority], tag, message); + } +#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) + /* Technically we don't need SDL_VIDEO_DRIVER_COCOA, but that's where this function is defined for now. + */ + extern void SDL_NSLog(const char *text); + { + char *text; + + text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); + if (text) { + SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message); + SDL_NSLog(text); + SDL_stack_free(text); + return; + } + } +#elif defined(__PSP__) + { + FILE* pFile; + pFile = fopen ("SDL_Log.txt", "a"); + fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message); + fclose (pFile); + } +#endif +#if HAVE_STDIO_H + fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); +#if __NACL__ + fflush(stderr); +#endif +#endif +} + +void +SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata) +{ + if (callback) { + *callback = SDL_log_function; + } + if (userdata) { + *userdata = SDL_log_userdata; + } +} + +void +SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata) +{ + SDL_log_function = callback; + SDL_log_userdata = userdata; +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/atomic/SDL_atomic.c b/ThirdParty/SDL2/src/atomic/SDL_atomic.c new file mode 100644 index 0000000..df49201 --- /dev/null +++ b/ThirdParty/SDL2/src/atomic/SDL_atomic.c @@ -0,0 +1,303 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +#include "SDL_atomic.h" + +#if defined(_MSC_VER) && (_MSC_VER >= 1500) +#include +#define HAVE_MSC_ATOMICS 1 +#endif + +#if defined(__MACOSX__) /* !!! FIXME: should we favor gcc atomics? */ +#include +#endif + +#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__) +#include +#endif + +/* The __atomic_load_n() intrinsic showed up in different times for different compilers. */ +#if defined(HAVE_GCC_ATOMICS) +# if defined(__clang__) +# if __has_builtin(__atomic_load_n) + /* !!! FIXME: this advertises as available in the NDK but uses an external symbol we don't have. + It might be in a later NDK or we might need an extra library? --ryan. */ +# if !defined(__ANDROID__) +# define HAVE_ATOMIC_LOAD_N 1 +# endif +# endif +# elif defined(__GNUC__) +# if (__GNUC__ >= 5) +# define HAVE_ATOMIC_LOAD_N 1 +# endif +# endif +#endif + +#if defined(__WATCOMC__) && defined(__386__) +#define HAVE_WATCOM_ATOMICS +extern _inline int _SDL_xchg_watcom(volatile int *a, int v); +#pragma aux _SDL_xchg_watcom = \ + "xchg [ecx], eax" \ + parm [ecx] [eax] \ + value [eax] \ + modify exact [eax]; + +extern _inline unsigned char _SDL_cmpxchg_watcom(volatile int *a, int newval, int oldval); +#pragma aux _SDL_cmpxchg_watcom = \ + "lock cmpxchg [edx], ecx" \ + "setz al" \ + parm [edx] [ecx] [eax] \ + value [al] \ + modify exact [eax]; + +extern _inline int _SDL_xadd_watcom(volatile int *a, int v); +#pragma aux _SDL_xadd_watcom = \ + "lock xadd [ecx], eax" \ + parm [ecx] [eax] \ + value [eax] \ + modify exact [eax]; +#endif /* __WATCOMC__ && __386__ */ + +/* + If any of the operations are not provided then we must emulate some + of them. That means we need a nice implementation of spin locks + that avoids the "one big lock" problem. We use a vector of spin + locks and pick which one to use based on the address of the operand + of the function. + + To generate the index of the lock we first shift by 3 bits to get + rid on the zero bits that result from 32 and 64 bit allignment of + data. We then mask off all but 5 bits and use those 5 bits as an + index into the table. + + Picking the lock this way insures that accesses to the same data at + the same time will go to the same lock. OTOH, accesses to different + data have only a 1/32 chance of hitting the same lock. That should + pretty much eliminate the chances of several atomic operations on + different data from waiting on the same "big lock". If it isn't + then the table of locks can be expanded to a new size so long as + the new size is a power of two. + + Contributed by Bob Pendleton, bob@pendleton.com +*/ + +#if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__) && !defined(__SOLARIS__) && !defined(HAVE_WATCOM_ATOMICS) +#define EMULATE_CAS 1 +#endif + +#if EMULATE_CAS +static SDL_SpinLock locks[32]; + +static SDL_INLINE void +enterLock(void *a) +{ + uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f); + + SDL_AtomicLock(&locks[index]); +} + +static SDL_INLINE void +leaveLock(void *a) +{ + uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f); + + SDL_AtomicUnlock(&locks[index]); +} +#endif + + +SDL_bool +SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval) +{ +#ifdef HAVE_MSC_ATOMICS + return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval); +#elif defined(HAVE_WATCOM_ATOMICS) + return (SDL_bool) _SDL_cmpxchg_watcom(&a->value, newval, oldval); +#elif defined(HAVE_GCC_ATOMICS) + return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval); +#elif defined(__MACOSX__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */ + return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value); +#elif defined(__SOLARIS__) && defined(_LP64) + return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval); +#elif defined(__SOLARIS__) && !defined(_LP64) + return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval); +#elif EMULATE_CAS + SDL_bool retval = SDL_FALSE; + + enterLock(a); + if (a->value == oldval) { + a->value = newval; + retval = SDL_TRUE; + } + leaveLock(a); + + return retval; +#else + #error Please define your platform. +#endif +} + +SDL_bool +SDL_AtomicCASPtr(void **a, void *oldval, void *newval) +{ +#if defined(HAVE_MSC_ATOMICS) && (_M_IX86) + return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval); +#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86) + return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval); +#elif defined(HAVE_WATCOM_ATOMICS) + return (SDL_bool) _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval); +#elif defined(HAVE_GCC_ATOMICS) + return __sync_bool_compare_and_swap(a, oldval, newval); +#elif defined(__MACOSX__) && defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */ + return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a); +#elif defined(__MACOSX__) && !defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */ + return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a); +#elif defined(__SOLARIS__) + return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval); +#elif EMULATE_CAS + SDL_bool retval = SDL_FALSE; + + enterLock(a); + if (*a == oldval) { + *a = newval; + retval = SDL_TRUE; + } + leaveLock(a); + + return retval; +#else + #error Please define your platform. +#endif +} + +int +SDL_AtomicSet(SDL_atomic_t *a, int v) +{ +#ifdef HAVE_MSC_ATOMICS + return _InterlockedExchange((long*)&a->value, v); +#elif defined(HAVE_WATCOM_ATOMICS) + return _SDL_xchg_watcom(&a->value, v); +#elif defined(HAVE_GCC_ATOMICS) + return __sync_lock_test_and_set(&a->value, v); +#elif defined(__SOLARIS__) && defined(_LP64) + return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v); +#elif defined(__SOLARIS__) && !defined(_LP64) + return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v); +#else + int value; + do { + value = a->value; + } while (!SDL_AtomicCAS(a, value, v)); + return value; +#endif +} + +void* +SDL_AtomicSetPtr(void **a, void *v) +{ +#if defined(HAVE_MSC_ATOMICS) && (_M_IX86) + return (void *) _InterlockedExchange((long *)a, (long) v); +#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86) + return _InterlockedExchangePointer(a, v); +#elif defined(HAVE_WATCOM_ATOMICS) + return (void *) _SDL_xchg_watcom((int *)a, (long)v); +#elif defined(HAVE_GCC_ATOMICS) + return __sync_lock_test_and_set(a, v); +#elif defined(__SOLARIS__) + return atomic_swap_ptr(a, v); +#else + void *value; + do { + value = *a; + } while (!SDL_AtomicCASPtr(a, value, v)); + return value; +#endif +} + +int +SDL_AtomicAdd(SDL_atomic_t *a, int v) +{ +#ifdef HAVE_MSC_ATOMICS + return _InterlockedExchangeAdd((long*)&a->value, v); +#elif defined(HAVE_WATCOM_ATOMICS) + return _SDL_xadd_watcom(&a->value, v); +#elif defined(HAVE_GCC_ATOMICS) + return __sync_fetch_and_add(&a->value, v); +#elif defined(__SOLARIS__) + int pv = a->value; + membar_consumer(); +#if defined(_LP64) + atomic_add_64((volatile uint64_t*)&a->value, v); +#elif !defined(_LP64) + atomic_add_32((volatile uint32_t*)&a->value, v); +#endif + return pv; +#else + int value; + do { + value = a->value; + } while (!SDL_AtomicCAS(a, value, (value + v))); + return value; +#endif +} + +int +SDL_AtomicGet(SDL_atomic_t *a) +{ +#ifdef HAVE_ATOMIC_LOAD_N + return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST); +#else + int value; + do { + value = a->value; + } while (!SDL_AtomicCAS(a, value, value)); + return value; +#endif +} + +void * +SDL_AtomicGetPtr(void **a) +{ +#ifdef HAVE_ATOMIC_LOAD_N + return __atomic_load_n(a, __ATOMIC_SEQ_CST); +#else + void *value; + do { + value = *a; + } while (!SDL_AtomicCASPtr(a, value, value)); + return value; +#endif +} + +void +SDL_MemoryBarrierReleaseFunction(void) +{ + SDL_MemoryBarrierRelease(); +} + +void +SDL_MemoryBarrierAcquireFunction(void) +{ + SDL_MemoryBarrierAcquire(); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/atomic/SDL_spinlock.c b/ThirdParty/SDL2/src/atomic/SDL_spinlock.c new file mode 100644 index 0000000..1ebc718 --- /dev/null +++ b/ThirdParty/SDL2/src/atomic/SDL_spinlock.c @@ -0,0 +1,152 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +#if defined(__WIN32__) || defined(__WINRT__) +#include "../core/windows/SDL_windows.h" +#endif + +#include "SDL_atomic.h" +#include "SDL_mutex.h" +#include "SDL_timer.h" + +#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__) +#include +#endif + +#if defined(__WATCOMC__) && defined(__386__) +SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock)); +extern _inline int _SDL_xchg_watcom(volatile int *a, int v); +#pragma aux _SDL_xchg_watcom = \ + "xchg [ecx], eax" \ + parm [ecx] [eax] \ + value [eax] \ + modify exact [eax]; +#endif /* __WATCOMC__ && __386__ */ + +/* This function is where all the magic happens... */ +SDL_bool +SDL_AtomicTryLock(SDL_SpinLock *lock) +{ +#if SDL_ATOMIC_DISABLED + /* Terrible terrible damage */ + static SDL_mutex *_spinlock_mutex; + + if (!_spinlock_mutex) { + /* Race condition on first lock... */ + _spinlock_mutex = SDL_CreateMutex(); + } + SDL_LockMutex(_spinlock_mutex); + if (*lock == 0) { + *lock = 1; + SDL_UnlockMutex(_spinlock_mutex); + return SDL_TRUE; + } else { + SDL_UnlockMutex(_spinlock_mutex); + return SDL_FALSE; + } + +#elif defined(_MSC_VER) + SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long)); + return (InterlockedExchange((long*)lock, 1) == 0); + +#elif defined(__WATCOMC__) && defined(__386__) + return _SDL_xchg_watcom(lock, 1) == 0; + +#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET + return (__sync_lock_test_and_set(lock, 1) == 0); + +#elif defined(__GNUC__) && defined(__arm__) && \ + (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \ + defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \ + defined(__ARM_ARCH_5TEJ__)) + int result; + __asm__ __volatile__ ( + "swp %0, %1, [%2]\n" + : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory"); + return (result == 0); + +#elif defined(__GNUC__) && defined(__arm__) + int result; + __asm__ __volatile__ ( + "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]" + : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory"); + return (result == 0); + +#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) + int result; + __asm__ __volatile__( + "lock ; xchgl %0, (%1)\n" + : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory"); + return (result == 0); + +#elif defined(__MACOSX__) || defined(__IPHONEOS__) + /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */ + return OSAtomicCompareAndSwap32Barrier(0, 1, lock); + +#elif defined(__SOLARIS__) && defined(_LP64) + /* Used for Solaris with non-gcc compilers. */ + return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0); + +#elif defined(__SOLARIS__) && !defined(_LP64) + /* Used for Solaris with non-gcc compilers. */ + return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0); + +#else +#error Please implement for your platform. + return SDL_FALSE; +#endif +} + +void +SDL_AtomicLock(SDL_SpinLock *lock) +{ + /* FIXME: Should we have an eventual timeout? */ + while (!SDL_AtomicTryLock(lock)) { + SDL_Delay(0); + } +} + +void +SDL_AtomicUnlock(SDL_SpinLock *lock) +{ +#if defined(_MSC_VER) + _ReadWriteBarrier(); + *lock = 0; + +#elif defined(__WATCOMC__) && defined(__386__) + SDL_CompilerBarrier (); + *lock = 0; + +#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET + __sync_lock_release(lock); + +#elif defined(__SOLARIS__) + /* Used for Solaris when not using gcc. */ + *lock = 0; + membar_producer(); + +#else + *lock = 0; +#endif +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_audio.c b/ThirdParty/SDL2/src/audio/SDL_audio.c new file mode 100644 index 0000000..dcaebea --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audio.c @@ -0,0 +1,1635 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* Allow access to a raw mixing buffer */ + +#include "SDL.h" +#include "SDL_audio.h" +#include "SDL_audio_c.h" +#include "SDL_sysaudio.h" +#include "../thread/SDL_systhread.h" + +#define _THIS SDL_AudioDevice *_this + +static SDL_AudioDriver current_audio; +static SDL_AudioDevice *open_devices[16]; + +/* Available audio drivers */ +static const AudioBootStrap *const bootstrap[] = { +#if SDL_AUDIO_DRIVER_PULSEAUDIO + &PULSEAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_ALSA + &ALSA_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_SNDIO + &SNDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_NETBSD + &NETBSDAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_OSS + &DSP_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_QSA + &QSAAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_SUNAUDIO + &SUNAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_ARTS + &ARTS_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_ESD + &ESD_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_NACL + &NACLAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_NAS + &NAS_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_WASAPI + &WASAPI_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_DSOUND + &DSOUND_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_WINMM + &WINMM_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_PAUDIO + &PAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_HAIKU + &HAIKUAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_COREAUDIO + &COREAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_FUSIONSOUND + &FUSIONSOUND_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_ANDROID + &ANDROIDAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_PSP + &PSPAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_EMSCRIPTEN + &EMSCRIPTENAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_JACK + &JACK_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_DISK + &DISKAUDIO_bootstrap, +#endif +#if SDL_AUDIO_DRIVER_DUMMY + &DUMMYAUDIO_bootstrap, +#endif + NULL +}; + + +#ifdef HAVE_LIBSAMPLERATE_H +#ifdef SDL_LIBSAMPLERATE_DYNAMIC +static void *SRC_lib = NULL; +#endif +SDL_bool SRC_available = SDL_FALSE; +int SRC_converter = 0; +SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error) = NULL; +int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data) = NULL; +int (*SRC_src_reset)(SRC_STATE *state) = NULL; +SRC_STATE* (*SRC_src_delete)(SRC_STATE *state) = NULL; +const char* (*SRC_src_strerror)(int error) = NULL; + +static SDL_bool +LoadLibSampleRate(void) +{ + const char *hint = SDL_GetHint(SDL_HINT_AUDIO_RESAMPLING_MODE); + + SRC_available = SDL_FALSE; + SRC_converter = 0; + + if (!hint || *hint == '0' || SDL_strcasecmp(hint, "default") == 0) { + return SDL_FALSE; /* don't load anything. */ + } else if (*hint == '1' || SDL_strcasecmp(hint, "fast") == 0) { + SRC_converter = SRC_SINC_FASTEST; + } else if (*hint == '2' || SDL_strcasecmp(hint, "medium") == 0) { + SRC_converter = SRC_SINC_MEDIUM_QUALITY; + } else if (*hint == '3' || SDL_strcasecmp(hint, "best") == 0) { + SRC_converter = SRC_SINC_BEST_QUALITY; + } else { + return SDL_FALSE; /* treat it like "default", don't load anything. */ + } + +#ifdef SDL_LIBSAMPLERATE_DYNAMIC + SDL_assert(SRC_lib == NULL); + SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC); + if (!SRC_lib) { + SDL_ClearError(); + return SDL_FALSE; + } + + SRC_src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(SRC_lib, "src_new"); + SRC_src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(SRC_lib, "src_process"); + SRC_src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(SRC_lib, "src_reset"); + SRC_src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(SRC_lib, "src_delete"); + SRC_src_strerror = (const char* (*)(int error))SDL_LoadFunction(SRC_lib, "src_strerror"); + + if (!SRC_src_new || !SRC_src_process || !SRC_src_reset || !SRC_src_delete || !SRC_src_strerror) { + SDL_UnloadObject(SRC_lib); + SRC_lib = NULL; + return SDL_FALSE; + } +#else + SRC_src_new = src_new; + SRC_src_process = src_process; + SRC_src_reset = src_reset; + SRC_src_delete = src_delete; + SRC_src_strerror = src_strerror; +#endif + + SRC_available = SDL_TRUE; + return SDL_TRUE; +} + +static void +UnloadLibSampleRate(void) +{ +#ifdef SDL_LIBSAMPLERATE_DYNAMIC + if (SRC_lib != NULL) { + SDL_UnloadObject(SRC_lib); + } + SRC_lib = NULL; +#endif + + SRC_available = SDL_FALSE; + SRC_src_new = NULL; + SRC_src_process = NULL; + SRC_src_reset = NULL; + SRC_src_delete = NULL; + SRC_src_strerror = NULL; +} +#endif + +static SDL_AudioDevice * +get_audio_device(SDL_AudioDeviceID id) +{ + id--; + if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) { + SDL_SetError("Invalid audio device ID"); + return NULL; + } + + return open_devices[id]; +} + + +/* stubs for audio drivers that don't need a specific entry point... */ +static void +SDL_AudioDetectDevices_Default(void) +{ + /* you have to write your own implementation if these assertions fail. */ + SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice); + SDL_assert(current_audio.impl.OnlyHasDefaultCaptureDevice || !current_audio.impl.HasCaptureSupport); + + SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) ((size_t) 0x1)); + if (current_audio.impl.HasCaptureSupport) { + SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) ((size_t) 0x2)); + } +} + +static void +SDL_AudioThreadInit_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioThreadDeinit_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioBeginLoopIteration_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioWaitDevice_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioPlayDevice_Default(_THIS) +{ /* no-op. */ +} + +static int +SDL_AudioGetPendingBytes_Default(_THIS) +{ + return 0; +} + +static Uint8 * +SDL_AudioGetDeviceBuf_Default(_THIS) +{ + return NULL; +} + +static int +SDL_AudioCaptureFromDevice_Default(_THIS, void *buffer, int buflen) +{ + return -1; /* just fail immediately. */ +} + +static void +SDL_AudioFlushCapture_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioPrepareToClose_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioCloseDevice_Default(_THIS) +{ /* no-op. */ +} + +static void +SDL_AudioDeinitialize_Default(void) +{ /* no-op. */ +} + +static void +SDL_AudioFreeDeviceHandle_Default(void *handle) +{ /* no-op. */ +} + + +static int +SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture) +{ + return SDL_Unsupported(); +} + +static SDL_INLINE SDL_bool +is_in_audio_device_thread(SDL_AudioDevice * device) +{ + /* The device thread locks the same mutex, but not through the public API. + This check is in case the application, in the audio callback, + tries to lock the thread that we've already locked from the + device thread...just in case we only have non-recursive mutexes. */ + if (device->thread && (SDL_ThreadID() == device->threadid)) { + return SDL_TRUE; + } + + return SDL_FALSE; +} + +static void +SDL_AudioLockDevice_Default(SDL_AudioDevice * device) +{ + if (!is_in_audio_device_thread(device)) { + SDL_LockMutex(device->mixer_lock); + } +} + +static void +SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device) +{ + if (!is_in_audio_device_thread(device)) { + SDL_UnlockMutex(device->mixer_lock); + } +} + +static void +SDL_AudioLockOrUnlockDeviceWithNoMixerLock(SDL_AudioDevice * device) +{ +} + +static void +finish_audio_entry_points_init(void) +{ + /* + * Fill in stub functions for unused driver entry points. This lets us + * blindly call them without having to check for validity first. + */ + + if (current_audio.impl.SkipMixerLock) { + if (current_audio.impl.LockDevice == NULL) { + current_audio.impl.LockDevice = SDL_AudioLockOrUnlockDeviceWithNoMixerLock; + } + if (current_audio.impl.UnlockDevice == NULL) { + current_audio.impl.UnlockDevice = SDL_AudioLockOrUnlockDeviceWithNoMixerLock; + } + } + +#define FILL_STUB(x) \ + if (current_audio.impl.x == NULL) { \ + current_audio.impl.x = SDL_Audio##x##_Default; \ + } + FILL_STUB(DetectDevices); + FILL_STUB(OpenDevice); + FILL_STUB(ThreadInit); + FILL_STUB(ThreadDeinit); + FILL_STUB(BeginLoopIteration); + FILL_STUB(WaitDevice); + FILL_STUB(PlayDevice); + FILL_STUB(GetPendingBytes); + FILL_STUB(GetDeviceBuf); + FILL_STUB(CaptureFromDevice); + FILL_STUB(FlushCapture); + FILL_STUB(PrepareToClose); + FILL_STUB(CloseDevice); + FILL_STUB(LockDevice); + FILL_STUB(UnlockDevice); + FILL_STUB(FreeDeviceHandle); + FILL_STUB(Deinitialize); +#undef FILL_STUB +} + + +/* device hotplug support... */ + +static int +add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount) +{ + int retval = -1; + const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1; + SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size); + if (item == NULL) { + return -1; + } + + SDL_assert(handle != NULL); /* we reserve NULL, audio backends can't use it. */ + + item->handle = handle; + SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem)); + + SDL_LockMutex(current_audio.detectionLock); + item->next = *devices; + *devices = item; + retval = (*devCount)++; + SDL_UnlockMutex(current_audio.detectionLock); + + return retval; +} + +static SDL_INLINE int +add_capture_device(const char *name, void *handle) +{ + SDL_assert(current_audio.impl.HasCaptureSupport); + return add_audio_device(name, handle, ¤t_audio.inputDevices, ¤t_audio.inputDeviceCount); +} + +static SDL_INLINE int +add_output_device(const char *name, void *handle) +{ + return add_audio_device(name, handle, ¤t_audio.outputDevices, ¤t_audio.outputDeviceCount); +} + +static void +free_device_list(SDL_AudioDeviceItem **devices, int *devCount) +{ + SDL_AudioDeviceItem *item, *next; + for (item = *devices; item != NULL; item = next) { + next = item->next; + if (item->handle != NULL) { + current_audio.impl.FreeDeviceHandle(item->handle); + } + SDL_free(item); + } + *devices = NULL; + *devCount = 0; +} + + +/* The audio backends call this when a new device is plugged in. */ +void +SDL_AddAudioDevice(const int iscapture, const char *name, void *handle) +{ + const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle); + if (device_index != -1) { + /* Post the event, if desired */ + if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) { + SDL_Event event; + SDL_zero(event); + event.adevice.type = SDL_AUDIODEVICEADDED; + event.adevice.which = device_index; + event.adevice.iscapture = iscapture; + SDL_PushEvent(&event); + } + } +} + +/* The audio backends call this when a currently-opened device is lost. */ +void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device) +{ + SDL_assert(get_audio_device(device->id) == device); + + if (!SDL_AtomicGet(&device->enabled)) { + return; + } + + /* Ends the audio callback and mark the device as STOPPED, but the + app still needs to close the device to free resources. */ + current_audio.impl.LockDevice(device); + SDL_AtomicSet(&device->enabled, 0); + current_audio.impl.UnlockDevice(device); + + /* Post the event, if desired */ + if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) { + SDL_Event event; + SDL_zero(event); + event.adevice.type = SDL_AUDIODEVICEREMOVED; + event.adevice.which = device->id; + event.adevice.iscapture = device->iscapture ? 1 : 0; + SDL_PushEvent(&event); + } +} + +static void +mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag) +{ + SDL_AudioDeviceItem *item; + SDL_assert(handle != NULL); + for (item = devices; item != NULL; item = item->next) { + if (item->handle == handle) { + item->handle = NULL; + *removedFlag = SDL_TRUE; + return; + } + } +} + +/* The audio backends call this when a device is removed from the system. */ +void +SDL_RemoveAudioDevice(const int iscapture, void *handle) +{ + int device_index; + SDL_AudioDevice *device = NULL; + + SDL_LockMutex(current_audio.detectionLock); + if (iscapture) { + mark_device_removed(handle, current_audio.inputDevices, ¤t_audio.captureDevicesRemoved); + } else { + mark_device_removed(handle, current_audio.outputDevices, ¤t_audio.outputDevicesRemoved); + } + for (device_index = 0; device_index < SDL_arraysize(open_devices); device_index++) + { + device = open_devices[device_index]; + if (device != NULL && device->handle == handle) + { + SDL_OpenedAudioDeviceDisconnected(device); + break; + } + } + SDL_UnlockMutex(current_audio.detectionLock); + + current_audio.impl.FreeDeviceHandle(handle); +} + + + +/* buffer queueing support... */ + +static void SDLCALL +SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len) +{ + /* this function always holds the mixer lock before being called. */ + SDL_AudioDevice *device = (SDL_AudioDevice *) userdata; + size_t dequeued; + + SDL_assert(device != NULL); /* this shouldn't ever happen, right?! */ + SDL_assert(!device->iscapture); /* this shouldn't ever happen, right?! */ + SDL_assert(len >= 0); /* this shouldn't ever happen, right?! */ + + dequeued = SDL_ReadFromDataQueue(device->buffer_queue, stream, len); + stream += dequeued; + len -= (int) dequeued; + + if (len > 0) { /* fill any remaining space in the stream with silence. */ + SDL_assert(SDL_CountDataQueue(device->buffer_queue) == 0); + SDL_memset(stream, device->spec.silence, len); + } +} + +static void SDLCALL +SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len) +{ + /* this function always holds the mixer lock before being called. */ + SDL_AudioDevice *device = (SDL_AudioDevice *) userdata; + + SDL_assert(device != NULL); /* this shouldn't ever happen, right?! */ + SDL_assert(device->iscapture); /* this shouldn't ever happen, right?! */ + SDL_assert(len >= 0); /* this shouldn't ever happen, right?! */ + + /* note that if this needs to allocate more space and run out of memory, + we have no choice but to quietly drop the data and hope it works out + later, but you probably have bigger problems in this case anyhow. */ + SDL_WriteToDataQueue(device->buffer_queue, stream, len); +} + +int +SDL_QueueAudio(SDL_AudioDeviceID devid, const void *data, Uint32 len) +{ + SDL_AudioDevice *device = get_audio_device(devid); + int rc = 0; + + if (!device) { + return -1; /* get_audio_device() will have set the error state */ + } else if (device->iscapture) { + return SDL_SetError("This is a capture device, queueing not allowed"); + } else if (device->callbackspec.callback != SDL_BufferQueueDrainCallback) { + return SDL_SetError("Audio device has a callback, queueing not allowed"); + } + + if (len > 0) { + current_audio.impl.LockDevice(device); + rc = SDL_WriteToDataQueue(device->buffer_queue, data, len); + current_audio.impl.UnlockDevice(device); + } + + return rc; +} + +Uint32 +SDL_DequeueAudio(SDL_AudioDeviceID devid, void *data, Uint32 len) +{ + SDL_AudioDevice *device = get_audio_device(devid); + Uint32 rc; + + if ( (len == 0) || /* nothing to do? */ + (!device) || /* called with bogus device id */ + (!device->iscapture) || /* playback devices can't dequeue */ + (device->callbackspec.callback != SDL_BufferQueueFillCallback) ) { /* not set for queueing */ + return 0; /* just report zero bytes dequeued. */ + } + + current_audio.impl.LockDevice(device); + rc = (Uint32) SDL_ReadFromDataQueue(device->buffer_queue, data, len); + current_audio.impl.UnlockDevice(device); + return rc; +} + +Uint32 +SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid) +{ + Uint32 retval = 0; + SDL_AudioDevice *device = get_audio_device(devid); + + if (!device) { + return 0; + } + + /* Nothing to do unless we're set up for queueing. */ + if (device->callbackspec.callback == SDL_BufferQueueDrainCallback) { + current_audio.impl.LockDevice(device); + retval = ((Uint32) SDL_CountDataQueue(device->buffer_queue)) + current_audio.impl.GetPendingBytes(device); + current_audio.impl.UnlockDevice(device); + } else if (device->callbackspec.callback == SDL_BufferQueueFillCallback) { + current_audio.impl.LockDevice(device); + retval = (Uint32) SDL_CountDataQueue(device->buffer_queue); + current_audio.impl.UnlockDevice(device); + } + + return retval; +} + +void +SDL_ClearQueuedAudio(SDL_AudioDeviceID devid) +{ + SDL_AudioDevice *device = get_audio_device(devid); + + if (!device) { + return; /* nothing to do. */ + } + + /* Blank out the device and release the mutex. Free it afterwards. */ + current_audio.impl.LockDevice(device); + + /* Keep up to two packets in the pool to reduce future malloc pressure. */ + SDL_ClearDataQueue(device->buffer_queue, SDL_AUDIOBUFFERQUEUE_PACKETLEN * 2); + + current_audio.impl.UnlockDevice(device); +} + + +/* The general mixing thread function */ +static int SDLCALL +SDL_RunAudio(void *devicep) +{ + SDL_AudioDevice *device = (SDL_AudioDevice *) devicep; + void *udata = device->callbackspec.userdata; + SDL_AudioCallback callback = device->callbackspec.callback; + int data_len = 0; + Uint8 *data; + + SDL_assert(!device->iscapture); + + /* The audio mixing is always a high priority thread */ + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); + + /* Perform any thread setup */ + device->threadid = SDL_ThreadID(); + current_audio.impl.ThreadInit(device); + + /* Loop, filling the audio buffers */ + while (!SDL_AtomicGet(&device->shutdown)) { + current_audio.impl.BeginLoopIteration(device); + data_len = device->callbackspec.size; + + /* Fill the current buffer with sound */ + if (!device->stream && SDL_AtomicGet(&device->enabled)) { + SDL_assert(data_len == device->spec.size); + data = current_audio.impl.GetDeviceBuf(device); + } else { + /* if the device isn't enabled, we still write to the + work_buffer, so the app's callback will fire with + a regular frequency, in case they depend on that + for timing or progress. They can use hotplug + now to know if the device failed. + Streaming playback uses work_buffer, too. */ + data = NULL; + } + + if (data == NULL) { + data = device->work_buffer; + } + + /* !!! FIXME: this should be LockDevice. */ + SDL_LockMutex(device->mixer_lock); + if (SDL_AtomicGet(&device->paused)) { + SDL_memset(data, device->spec.silence, data_len); + } else { + callback(udata, data, data_len); + } + SDL_UnlockMutex(device->mixer_lock); + + if (device->stream) { + /* Stream available audio to device, converting/resampling. */ + /* if this fails...oh well. We'll play silence here. */ + SDL_AudioStreamPut(device->stream, data, data_len); + + while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) { + int got; + data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL; + got = SDL_AudioStreamGet(device->stream, data ? data : device->work_buffer, device->spec.size); + SDL_assert((got < 0) || (got == device->spec.size)); + + if (data == NULL) { /* device is having issues... */ + const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq); + SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */ + } else { + if (got != device->spec.size) { + SDL_memset(data, device->spec.silence, device->spec.size); + } + current_audio.impl.PlayDevice(device); + current_audio.impl.WaitDevice(device); + } + } + } else if (data == device->work_buffer) { + /* nothing to do; pause like we queued a buffer to play. */ + const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq); + SDL_Delay(delay); + } else { /* writing directly to the device. */ + /* queue this buffer and wait for it to finish playing. */ + current_audio.impl.PlayDevice(device); + current_audio.impl.WaitDevice(device); + } + } + + current_audio.impl.PrepareToClose(device); + + /* Wait for the audio to drain. */ + SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2); + + current_audio.impl.ThreadDeinit(device); + + return 0; +} + +/* !!! FIXME: this needs to deal with device spec changes. */ +/* The general capture thread function */ +static int SDLCALL +SDL_CaptureAudio(void *devicep) +{ + SDL_AudioDevice *device = (SDL_AudioDevice *) devicep; + const int silence = (int) device->spec.silence; + const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq); + const int data_len = device->spec.size; + Uint8 *data; + void *udata = device->callbackspec.userdata; + SDL_AudioCallback callback = device->callbackspec.callback; + + SDL_assert(device->iscapture); + + /* The audio mixing is always a high priority thread */ + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); + + /* Perform any thread setup */ + device->threadid = SDL_ThreadID(); + current_audio.impl.ThreadInit(device); + + /* Loop, filling the audio buffers */ + while (!SDL_AtomicGet(&device->shutdown)) { + int still_need; + Uint8 *ptr; + + current_audio.impl.BeginLoopIteration(device); + + if (SDL_AtomicGet(&device->paused)) { + SDL_Delay(delay); /* just so we don't cook the CPU. */ + if (device->stream) { + SDL_AudioStreamClear(device->stream); + } + current_audio.impl.FlushCapture(device); /* dump anything pending. */ + continue; + } + + /* Fill the current buffer with sound */ + still_need = data_len; + + /* Use the work_buffer to hold data read from the device. */ + data = device->work_buffer; + SDL_assert(data != NULL); + + ptr = data; + + /* We still read from the device when "paused" to keep the state sane, + and block when there isn't data so this thread isn't eating CPU. + But we don't process it further or call the app's callback. */ + + if (!SDL_AtomicGet(&device->enabled)) { + SDL_Delay(delay); /* try to keep callback firing at normal pace. */ + } else { + while (still_need > 0) { + const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need); + SDL_assert(rc <= still_need); /* device should not overflow buffer. :) */ + if (rc > 0) { + still_need -= rc; + ptr += rc; + } else { /* uhoh, device failed for some reason! */ + SDL_OpenedAudioDeviceDisconnected(device); + break; + } + } + } + + if (still_need > 0) { + /* Keep any data we already read, silence the rest. */ + SDL_memset(ptr, silence, still_need); + } + + if (device->stream) { + /* if this fails...oh well. */ + SDL_AudioStreamPut(device->stream, data, data_len); + + while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->callbackspec.size)) { + const int got = SDL_AudioStreamGet(device->stream, device->work_buffer, device->callbackspec.size); + SDL_assert((got < 0) || (got == device->callbackspec.size)); + if (got != device->callbackspec.size) { + SDL_memset(device->work_buffer, device->spec.silence, device->callbackspec.size); + } + + /* !!! FIXME: this should be LockDevice. */ + SDL_LockMutex(device->mixer_lock); + if (!SDL_AtomicGet(&device->paused)) { + callback(udata, device->work_buffer, device->callbackspec.size); + } + SDL_UnlockMutex(device->mixer_lock); + } + } else { /* feeding user callback directly without streaming. */ + /* !!! FIXME: this should be LockDevice. */ + SDL_LockMutex(device->mixer_lock); + if (!SDL_AtomicGet(&device->paused)) { + callback(udata, data, device->callbackspec.size); + } + SDL_UnlockMutex(device->mixer_lock); + } + } + + current_audio.impl.FlushCapture(device); + + current_audio.impl.ThreadDeinit(device); + + return 0; +} + + +static SDL_AudioFormat +SDL_ParseAudioFormat(const char *string) +{ +#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x + CHECK_FMT_STRING(U8); + CHECK_FMT_STRING(S8); + CHECK_FMT_STRING(U16LSB); + CHECK_FMT_STRING(S16LSB); + CHECK_FMT_STRING(U16MSB); + CHECK_FMT_STRING(S16MSB); + CHECK_FMT_STRING(U16SYS); + CHECK_FMT_STRING(S16SYS); + CHECK_FMT_STRING(U16); + CHECK_FMT_STRING(S16); + CHECK_FMT_STRING(S32LSB); + CHECK_FMT_STRING(S32MSB); + CHECK_FMT_STRING(S32SYS); + CHECK_FMT_STRING(S32); + CHECK_FMT_STRING(F32LSB); + CHECK_FMT_STRING(F32MSB); + CHECK_FMT_STRING(F32SYS); + CHECK_FMT_STRING(F32); +#undef CHECK_FMT_STRING + return 0; +} + +int +SDL_GetNumAudioDrivers(void) +{ + return SDL_arraysize(bootstrap) - 1; +} + +const char * +SDL_GetAudioDriver(int index) +{ + if (index >= 0 && index < SDL_GetNumAudioDrivers()) { + return bootstrap[index]->name; + } + return NULL; +} + +int +SDL_AudioInit(const char *driver_name) +{ + int i = 0; + int initialized = 0; + int tried_to_init = 0; + + if (SDL_WasInit(SDL_INIT_AUDIO)) { + SDL_AudioQuit(); /* shutdown driver if already running. */ + } + + SDL_zero(current_audio); + SDL_zero(open_devices); + + /* Select the proper audio driver */ + if (driver_name == NULL) { + driver_name = SDL_getenv("SDL_AUDIODRIVER"); + } + + for (i = 0; (!initialized) && (bootstrap[i]); ++i) { + /* make sure we should even try this driver before doing so... */ + const AudioBootStrap *backend = bootstrap[i]; + if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) || + (!driver_name && backend->demand_only)) { + continue; + } + + tried_to_init = 1; + SDL_zero(current_audio); + current_audio.name = backend->name; + current_audio.desc = backend->desc; + initialized = backend->init(¤t_audio.impl); + } + + if (!initialized) { + /* specific drivers will set the error message if they fail... */ + if (!tried_to_init) { + if (driver_name) { + SDL_SetError("Audio target '%s' not available", driver_name); + } else { + SDL_SetError("No available audio device"); + } + } + + SDL_zero(current_audio); + return -1; /* No driver was available, so fail. */ + } + + current_audio.detectionLock = SDL_CreateMutex(); + + finish_audio_entry_points_init(); + + /* Make sure we have a list of devices available at startup. */ + current_audio.impl.DetectDevices(); + +#ifdef HAVE_LIBSAMPLERATE_H + LoadLibSampleRate(); +#endif + + return 0; +} + +/* + * Get the current audio driver name + */ +const char * +SDL_GetCurrentAudioDriver() +{ + return current_audio.name; +} + +/* Clean out devices that we've removed but had to keep around for stability. */ +static void +clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag) +{ + SDL_AudioDeviceItem *item = *devices; + SDL_AudioDeviceItem *prev = NULL; + int total = 0; + + while (item) { + SDL_AudioDeviceItem *next = item->next; + if (item->handle != NULL) { + total++; + prev = item; + } else { + if (prev) { + prev->next = next; + } else { + *devices = next; + } + SDL_free(item); + } + item = next; + } + + *devCount = total; + *removedFlag = SDL_FALSE; +} + + +int +SDL_GetNumAudioDevices(int iscapture) +{ + int retval = 0; + + if (!SDL_WasInit(SDL_INIT_AUDIO)) { + return -1; + } + + SDL_LockMutex(current_audio.detectionLock); + if (iscapture && current_audio.captureDevicesRemoved) { + clean_out_device_list(¤t_audio.inputDevices, ¤t_audio.inputDeviceCount, ¤t_audio.captureDevicesRemoved); + } + + if (!iscapture && current_audio.outputDevicesRemoved) { + clean_out_device_list(¤t_audio.outputDevices, ¤t_audio.outputDeviceCount, ¤t_audio.outputDevicesRemoved); + current_audio.outputDevicesRemoved = SDL_FALSE; + } + + retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount; + SDL_UnlockMutex(current_audio.detectionLock); + + return retval; +} + + +const char * +SDL_GetAudioDeviceName(int index, int iscapture) +{ + const char *retval = NULL; + + if (!SDL_WasInit(SDL_INIT_AUDIO)) { + SDL_SetError("Audio subsystem is not initialized"); + return NULL; + } + + if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) { + SDL_SetError("No capture support"); + return NULL; + } + + if (index >= 0) { + SDL_AudioDeviceItem *item; + int i; + + SDL_LockMutex(current_audio.detectionLock); + item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; + i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount; + if (index < i) { + for (i--; i > index; i--, item = item->next) { + SDL_assert(item != NULL); + } + SDL_assert(item != NULL); + retval = item->name; + } + SDL_UnlockMutex(current_audio.detectionLock); + } + + if (retval == NULL) { + SDL_SetError("No such device"); + } + + return retval; +} + + +static void +close_audio_device(SDL_AudioDevice * device) +{ + if (!device) { + return; + } + + if (device->id > 0) { + SDL_AudioDevice *opendev = open_devices[device->id - 1]; + SDL_assert((opendev == device) || (opendev == NULL)); + if (opendev == device) { + open_devices[device->id - 1] = NULL; + } + } + + SDL_AtomicSet(&device->shutdown, 1); + SDL_AtomicSet(&device->enabled, 0); + if (device->thread != NULL) { + SDL_WaitThread(device->thread, NULL); + } + if (device->mixer_lock != NULL) { + SDL_DestroyMutex(device->mixer_lock); + } + + SDL_free(device->work_buffer); + SDL_FreeAudioStream(device->stream); + + if (device->hidden != NULL) { + current_audio.impl.CloseDevice(device); + } + + SDL_FreeDataQueue(device->buffer_queue); + + SDL_free(device); +} + + +/* + * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig). + * Fills in a sanitized copy in (prepared). + * Returns non-zero if okay, zero on fatal parameters in (orig). + */ +static int +prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared) +{ + SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec)); + + if (orig->freq == 0) { + const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY"); + if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) { + prepared->freq = 22050; /* a reasonable default */ + } + } + + if (orig->format == 0) { + const char *env = SDL_getenv("SDL_AUDIO_FORMAT"); + if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) { + prepared->format = AUDIO_S16; /* a reasonable default */ + } + } + + switch (orig->channels) { + case 0:{ + const char *env = SDL_getenv("SDL_AUDIO_CHANNELS"); + if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) { + prepared->channels = 2; /* a reasonable default */ + } + break; + } + case 1: /* Mono */ + case 2: /* Stereo */ + case 4: /* surround */ + case 6: /* surround with center and lfe */ + break; + default: + SDL_SetError("Unsupported number of audio channels."); + return 0; + } + + if (orig->samples == 0) { + const char *env = SDL_getenv("SDL_AUDIO_SAMPLES"); + if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) { + /* Pick a default of ~46 ms at desired frequency */ + /* !!! FIXME: remove this when the non-Po2 resampling is in. */ + const int samples = (prepared->freq / 1000) * 46; + int power2 = 1; + while (power2 < samples) { + power2 *= 2; + } + prepared->samples = power2; + } + } + + /* Calculate the silence and size of the audio specification */ + SDL_CalculateAudioSpec(prepared); + + return 1; +} + +static SDL_AudioDeviceID +open_audio_device(const char *devname, int iscapture, + const SDL_AudioSpec * desired, SDL_AudioSpec * obtained, + int allowed_changes, int min_id) +{ + const SDL_bool is_internal_thread = (desired->callback == NULL); + SDL_AudioDeviceID id = 0; + SDL_AudioSpec _obtained; + SDL_AudioDevice *device; + SDL_bool build_stream; + void *handle = NULL; + int i = 0; + + if (!SDL_WasInit(SDL_INIT_AUDIO)) { + SDL_SetError("Audio subsystem is not initialized"); + return 0; + } + + if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) { + SDL_SetError("No capture support"); + return 0; + } + + /* !!! FIXME: there is a race condition here if two devices open from two threads at once. */ + /* Find an available device ID... */ + for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) { + if (open_devices[id] == NULL) { + break; + } + } + + if (id == SDL_arraysize(open_devices)) { + SDL_SetError("Too many open audio devices"); + return 0; + } + + if (!obtained) { + obtained = &_obtained; + } + if (!prepare_audiospec(desired, obtained)) { + return 0; + } + + /* If app doesn't care about a specific device, let the user override. */ + if (devname == NULL) { + devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME"); + } + + /* + * Catch device names at the high level for the simple case... + * This lets us have a basic "device enumeration" for systems that + * don't have multiple devices, but makes sure the device name is + * always NULL when it hits the low level. + * + * Also make sure that the simple case prevents multiple simultaneous + * opens of the default system device. + */ + + if ((iscapture) && (current_audio.impl.OnlyHasDefaultCaptureDevice)) { + if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) { + SDL_SetError("No such device"); + return 0; + } + devname = NULL; + + for (i = 0; i < SDL_arraysize(open_devices); i++) { + if ((open_devices[i]) && (open_devices[i]->iscapture)) { + SDL_SetError("Audio device already open"); + return 0; + } + } + } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) { + if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) { + SDL_SetError("No such device"); + return 0; + } + devname = NULL; + + for (i = 0; i < SDL_arraysize(open_devices); i++) { + if ((open_devices[i]) && (!open_devices[i]->iscapture)) { + SDL_SetError("Audio device already open"); + return 0; + } + } + } else if (devname != NULL) { + /* if the app specifies an exact string, we can pass the backend + an actual device handle thingey, which saves them the effort of + figuring out what device this was (such as, reenumerating + everything again to find the matching human-readable name). + It might still need to open a device based on the string for, + say, a network audio server, but this optimizes some cases. */ + SDL_AudioDeviceItem *item; + SDL_LockMutex(current_audio.detectionLock); + for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) { + if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) { + handle = item->handle; + break; + } + } + SDL_UnlockMutex(current_audio.detectionLock); + } + + if (!current_audio.impl.AllowsArbitraryDeviceNames) { + /* has to be in our device list, or the default device. */ + if ((handle == NULL) && (devname != NULL)) { + SDL_SetError("No such device."); + return 0; + } + } + + device = (SDL_AudioDevice *) SDL_calloc(1, sizeof (SDL_AudioDevice)); + if (device == NULL) { + SDL_OutOfMemory(); + return 0; + } + device->id = id + 1; + device->spec = *obtained; + device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE; + device->handle = handle; + + SDL_AtomicSet(&device->shutdown, 0); /* just in case. */ + SDL_AtomicSet(&device->paused, 1); + SDL_AtomicSet(&device->enabled, 1); + + /* Create a mutex for locking the sound buffers */ + if (!current_audio.impl.SkipMixerLock) { + device->mixer_lock = SDL_CreateMutex(); + if (device->mixer_lock == NULL) { + close_audio_device(device); + SDL_SetError("Couldn't create mixer lock"); + return 0; + } + } + + if (current_audio.impl.OpenDevice(device, handle, devname, iscapture) < 0) { + close_audio_device(device); + return 0; + } + + /* if your target really doesn't need it, set it to 0x1 or something. */ + /* otherwise, close_audio_device() won't call impl.CloseDevice(). */ + SDL_assert(device->hidden != NULL); + + /* See if we need to do any conversion */ + build_stream = SDL_FALSE; + if (obtained->freq != device->spec.freq) { + if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) { + obtained->freq = device->spec.freq; + } else { + build_stream = SDL_TRUE; + } + } + if (obtained->format != device->spec.format) { + if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) { + obtained->format = device->spec.format; + } else { + build_stream = SDL_TRUE; + } + } + if (obtained->channels != device->spec.channels) { + if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) { + obtained->channels = device->spec.channels; + } else { + build_stream = SDL_TRUE; + } + } + + /* !!! FIXME in 2.1: add SDL_AUDIO_ALLOW_SAMPLES_CHANGE flag? + As of 2.0.6, we will build a stream to buffer the difference between + what the app wants to feed and the device wants to eat, so everyone + gets their way. In prior releases, SDL would force the callback to + feed at the rate the device requested, adjusted for resampling. + */ + if (device->spec.samples != obtained->samples) { + build_stream = SDL_TRUE; + } + + SDL_CalculateAudioSpec(obtained); /* recalc after possible changes. */ + + device->callbackspec = *obtained; + + if (build_stream) { + if (iscapture) { + device->stream = SDL_NewAudioStream(device->spec.format, + device->spec.channels, device->spec.freq, + obtained->format, obtained->channels, obtained->freq); + } else { + device->stream = SDL_NewAudioStream(obtained->format, obtained->channels, + obtained->freq, device->spec.format, + device->spec.channels, device->spec.freq); + } + + if (!device->stream) { + close_audio_device(device); + return 0; + } + } + + if (device->spec.callback == NULL) { /* use buffer queueing? */ + /* pool a few packets to start. Enough for two callbacks. */ + device->buffer_queue = SDL_NewDataQueue(SDL_AUDIOBUFFERQUEUE_PACKETLEN, obtained->size * 2); + if (!device->buffer_queue) { + close_audio_device(device); + SDL_SetError("Couldn't create audio buffer queue"); + return 0; + } + device->callbackspec.callback = iscapture ? SDL_BufferQueueFillCallback : SDL_BufferQueueDrainCallback; + device->callbackspec.userdata = device; + } + + /* Allocate a scratch audio buffer */ + device->work_buffer_len = build_stream ? device->callbackspec.size : 0; + if (device->spec.size > device->work_buffer_len) { + device->work_buffer_len = device->spec.size; + } + SDL_assert(device->work_buffer_len > 0); + + device->work_buffer = (Uint8 *) SDL_malloc(device->work_buffer_len); + if (device->work_buffer == NULL) { + close_audio_device(device); + SDL_OutOfMemory(); + return 0; + } + + open_devices[id] = device; /* add it to our list of open devices. */ + + /* Start the audio thread if necessary */ + if (!current_audio.impl.ProvidesOwnCallbackThread) { + /* Start the audio thread */ + /* !!! FIXME: we don't force the audio thread stack size here if it calls into user code, but maybe we should? */ + /* buffer queueing callback only needs a few bytes, so make the stack tiny. */ + const size_t stacksize = is_internal_thread ? 64 * 1024 : 0; + char threadname[64]; + + SDL_snprintf(threadname, sizeof (threadname), "SDLAudio%c%d", (iscapture) ? 'C' : 'P', (int) device->id); + device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, threadname, stacksize, device); + + if (device->thread == NULL) { + close_audio_device(device); + SDL_SetError("Couldn't create audio thread"); + return 0; + } + } + + return device->id; +} + + +int +SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained) +{ + SDL_AudioDeviceID id = 0; + + /* Start up the audio driver, if necessary. This is legacy behaviour! */ + if (!SDL_WasInit(SDL_INIT_AUDIO)) { + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { + return -1; + } + } + + /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */ + if (open_devices[0] != NULL) { + SDL_SetError("Audio device is already opened"); + return -1; + } + + if (obtained) { + id = open_audio_device(NULL, 0, desired, obtained, + SDL_AUDIO_ALLOW_ANY_CHANGE, 1); + } else { + SDL_AudioSpec _obtained; + SDL_zero(_obtained); + id = open_audio_device(NULL, 0, desired, &_obtained, 0, 1); + /* On successful open, copy calculated values into 'desired'. */ + if (id > 0) { + desired->size = _obtained.size; + desired->silence = _obtained.silence; + } + } + + SDL_assert((id == 0) || (id == 1)); + return (id == 0) ? -1 : 0; +} + +SDL_AudioDeviceID +SDL_OpenAudioDevice(const char *device, int iscapture, + const SDL_AudioSpec * desired, SDL_AudioSpec * obtained, + int allowed_changes) +{ + return open_audio_device(device, iscapture, desired, obtained, + allowed_changes, 2); +} + +SDL_AudioStatus +SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid) +{ + SDL_AudioDevice *device = get_audio_device(devid); + SDL_AudioStatus status = SDL_AUDIO_STOPPED; + if (device && SDL_AtomicGet(&device->enabled)) { + if (SDL_AtomicGet(&device->paused)) { + status = SDL_AUDIO_PAUSED; + } else { + status = SDL_AUDIO_PLAYING; + } + } + return status; +} + + +SDL_AudioStatus +SDL_GetAudioStatus(void) +{ + return SDL_GetAudioDeviceStatus(1); +} + +void +SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on) +{ + SDL_AudioDevice *device = get_audio_device(devid); + if (device) { + current_audio.impl.LockDevice(device); + SDL_AtomicSet(&device->paused, pause_on ? 1 : 0); + current_audio.impl.UnlockDevice(device); + } +} + +void +SDL_PauseAudio(int pause_on) +{ + SDL_PauseAudioDevice(1, pause_on); +} + + +void +SDL_LockAudioDevice(SDL_AudioDeviceID devid) +{ + /* Obtain a lock on the mixing buffers */ + SDL_AudioDevice *device = get_audio_device(devid); + if (device) { + current_audio.impl.LockDevice(device); + } +} + +void +SDL_LockAudio(void) +{ + SDL_LockAudioDevice(1); +} + +void +SDL_UnlockAudioDevice(SDL_AudioDeviceID devid) +{ + /* Obtain a lock on the mixing buffers */ + SDL_AudioDevice *device = get_audio_device(devid); + if (device) { + current_audio.impl.UnlockDevice(device); + } +} + +void +SDL_UnlockAudio(void) +{ + SDL_UnlockAudioDevice(1); +} + +void +SDL_CloseAudioDevice(SDL_AudioDeviceID devid) +{ + close_audio_device(get_audio_device(devid)); +} + +void +SDL_CloseAudio(void) +{ + SDL_CloseAudioDevice(1); +} + +void +SDL_AudioQuit(void) +{ + SDL_AudioDeviceID i; + + if (!current_audio.name) { /* not initialized?! */ + return; + } + + for (i = 0; i < SDL_arraysize(open_devices); i++) { + close_audio_device(open_devices[i]); + } + + free_device_list(¤t_audio.outputDevices, ¤t_audio.outputDeviceCount); + free_device_list(¤t_audio.inputDevices, ¤t_audio.inputDeviceCount); + + /* Free the driver data */ + current_audio.impl.Deinitialize(); + + SDL_DestroyMutex(current_audio.detectionLock); + + SDL_zero(current_audio); + SDL_zero(open_devices); + +#ifdef HAVE_LIBSAMPLERATE_H + UnloadLibSampleRate(); +#endif + + SDL_FreeResampleFilter(); +} + +#define NUM_FORMATS 10 +static int format_idx; +static int format_idx_sub; +static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = { + {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, + AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB}, + {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, + AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB}, + {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB, + AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB, + AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB, + AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB, + AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB, + AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB, + AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB, + AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8}, + {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB, + AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8}, +}; + +SDL_AudioFormat +SDL_FirstAudioFormat(SDL_AudioFormat format) +{ + for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) { + if (format_list[format_idx][0] == format) { + break; + } + } + format_idx_sub = 0; + return SDL_NextAudioFormat(); +} + +SDL_AudioFormat +SDL_NextAudioFormat(void) +{ + if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) { + return 0; + } + return format_list[format_idx][format_idx_sub++]; +} + +void +SDL_CalculateAudioSpec(SDL_AudioSpec * spec) +{ + switch (spec->format) { + case AUDIO_U8: + spec->silence = 0x80; + break; + default: + spec->silence = 0x00; + break; + } + spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8; + spec->size *= spec->channels; + spec->size *= spec->samples; +} + + +/* + * Moved here from SDL_mixer.c, since it relies on internals of an opened + * audio device (and is deprecated, by the way!). + */ +void +SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume) +{ + /* Mix the user-level audio format */ + SDL_AudioDevice *device = get_audio_device(1); + if (device != NULL) { + SDL_MixAudioFormat(dst, src, device->callbackspec.format, len, volume); + } +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_audio_c.h b/ThirdParty/SDL2/src/audio/SDL_audio_c.h new file mode 100644 index 0000000..d47ebb1 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audio_c.h @@ -0,0 +1,79 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL_audio_c_h_ +#define SDL_audio_c_h_ + +#include "../SDL_internal.h" + +#ifndef DEBUG_CONVERT +#define DEBUG_CONVERT 0 +#endif + +#if DEBUG_CONVERT +#define LOG_DEBUG_CONVERT(from, to) fprintf(stderr, "Converting %s to %s.\n", from, to); +#else +#define LOG_DEBUG_CONVERT(from, to) +#endif + +/* Functions and variables exported from SDL_audio.c for SDL_sysaudio.c */ + +#ifdef HAVE_LIBSAMPLERATE_H +#include "samplerate.h" +extern SDL_bool SRC_available; +extern int SRC_converter; +extern SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error); +extern int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data); +extern int (*SRC_src_reset)(SRC_STATE *state); +extern SRC_STATE* (*SRC_src_delete)(SRC_STATE *state); +extern const char* (*SRC_src_strerror)(int error); +#endif + +/* Functions to get a list of "close" audio formats */ +extern SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format); +extern SDL_AudioFormat SDL_NextAudioFormat(void); + +/* Function to calculate the size and silence for a SDL_AudioSpec */ +extern void SDL_CalculateAudioSpec(SDL_AudioSpec * spec); + +/* Choose the audio filter functions below */ +extern void SDL_ChooseAudioConverters(void); + +/* These pointers get set during SDL_ChooseAudioConverters() to various SIMD implementations. */ +extern SDL_AudioFilter SDL_Convert_S8_to_F32; +extern SDL_AudioFilter SDL_Convert_U8_to_F32; +extern SDL_AudioFilter SDL_Convert_S16_to_F32; +extern SDL_AudioFilter SDL_Convert_U16_to_F32; +extern SDL_AudioFilter SDL_Convert_S32_to_F32; +extern SDL_AudioFilter SDL_Convert_F32_to_S8; +extern SDL_AudioFilter SDL_Convert_F32_to_U8; +extern SDL_AudioFilter SDL_Convert_F32_to_S16; +extern SDL_AudioFilter SDL_Convert_F32_to_U16; +extern SDL_AudioFilter SDL_Convert_F32_to_S32; + +/* You need to call SDL_PrepareResampleFilter() before using the internal resampler. + SDL_AudioQuit() calls SDL_FreeResamplerFilter(), you should never call it yourself. */ +extern int SDL_PrepareResampleFilter(void); +extern void SDL_FreeResampleFilter(void); + +#endif /* SDL_audio_c_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_audiocvt.c b/ThirdParty/SDL2/src/audio/SDL_audiocvt.c new file mode 100644 index 0000000..7fde2b9 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audiocvt.c @@ -0,0 +1,1673 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* Functions for audio drivers to perform runtime conversion of audio format */ + +/* FIXME: Channel weights when converting from more channels to fewer may need to be adjusted, see https://msdn.microsoft.com/en-us/library/windows/desktop/ff819070(v=vs.85).aspx +*/ + +#include "SDL.h" +#include "SDL_audio.h" +#include "SDL_audio_c.h" + +#include "SDL_loadso.h" +#include "SDL_assert.h" +#include "../SDL_dataqueue.h" +#include "SDL_cpuinfo.h" + +#define DEBUG_AUDIOSTREAM 0 + +#ifdef __SSE3__ +#define HAVE_SSE3_INTRINSICS 1 +#endif + +#if HAVE_SSE3_INTRINSICS +/* Convert from stereo to mono. Average left and right. */ +static void SDLCALL +SDL_ConvertStereoToMono_SSE3(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i = cvt->len_cvt / 8; + + LOG_DEBUG_CONVERT("stereo", "mono (using SSE3)"); + SDL_assert(format == AUDIO_F32SYS); + + /* We can only do this if dst is aligned to 16 bytes; since src is the + same pointer and it moves by 2, it can't be forcibly aligned. */ + if ((((size_t) dst) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 divby2 = _mm_set1_ps(0.5f); + while (i >= 4) { /* 4 * float32 */ + _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2)); + i -= 4; src += 8; dst += 4; + } + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (src[0] + src[1]) * 0.5f; + dst++; i--; src += 2; + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} +#endif + +/* Convert from stereo to mono. Average left and right. */ +static void SDLCALL +SDL_ConvertStereoToMono(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i; + + LOG_DEBUG_CONVERT("stereo", "mono"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / 8; i; --i, src += 2) { + *(dst++) = (src[0] + src[1]) * 0.5f; + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Convert from 5.1 to stereo. Average left and right, distribute center, discard LFE. */ +static void SDLCALL +SDL_Convert51ToStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i; + + LOG_DEBUG_CONVERT("5.1", "stereo"); + SDL_assert(format == AUDIO_F32SYS); + + /* SDL's 5.1 layout: FL+FR+FC+LFE+BL+BR */ + for (i = cvt->len_cvt / (sizeof (float) * 6); i; --i, src += 6, dst += 2) { + const float front_center_distributed = src[2] * 0.5f; + dst[0] = (src[0] + front_center_distributed + src[4]) / 2.5f; /* left */ + dst[1] = (src[1] + front_center_distributed + src[5]) / 2.5f; /* right */ + } + + cvt->len_cvt /= 3; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Convert from quad to stereo. Average left and right. */ +static void SDLCALL +SDL_ConvertQuadToStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i; + + LOG_DEBUG_CONVERT("quad", "stereo"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / (sizeof (float) * 4); i; --i, src += 4, dst += 2) { + dst[0] = (src[0] + src[2]) * 0.5f; /* left */ + dst[1] = (src[1] + src[3]) * 0.5f; /* right */ + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Convert from 7.1 to 5.1. Distribute sides across front and back. */ +static void SDLCALL +SDL_Convert71To51(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i; + + LOG_DEBUG_CONVERT("7.1", "5.1"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / (sizeof (float) * 8); i; --i, src += 8, dst += 6) { + const float surround_left_distributed = src[6] * 0.5f; + const float surround_right_distributed = src[7] * 0.5f; + dst[0] = (src[0] + surround_left_distributed) / 1.5f; /* FL */ + dst[1] = (src[1] + surround_right_distributed) / 1.5f; /* FR */ + dst[2] = src[2] / 1.5f; /* CC */ + dst[3] = src[3] / 1.5f; /* LFE */ + dst[4] = (src[4] + surround_left_distributed) / 1.5f; /* BL */ + dst[5] = (src[5] + surround_right_distributed) / 1.5f; /* BR */ + } + + cvt->len_cvt /= 8; + cvt->len_cvt *= 6; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Convert from 5.1 to quad. Distribute center across front, discard LFE. */ +static void SDLCALL +SDL_Convert51ToQuad(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float *dst = (float *) cvt->buf; + const float *src = dst; + int i; + + LOG_DEBUG_CONVERT("5.1", "quad"); + SDL_assert(format == AUDIO_F32SYS); + + /* SDL's 4.0 layout: FL+FR+BL+BR */ + /* SDL's 5.1 layout: FL+FR+FC+LFE+BL+BR */ + for (i = cvt->len_cvt / (sizeof (float) * 6); i; --i, src += 6, dst += 4) { + const float front_center_distributed = src[2] * 0.5f; + dst[0] = (src[0] + front_center_distributed) / 1.5f; /* FL */ + dst[1] = (src[1] + front_center_distributed) / 1.5f; /* FR */ + dst[2] = src[4] / 1.5f; /* BL */ + dst[3] = src[5] / 1.5f; /* BR */ + } + + cvt->len_cvt /= 6; + cvt->len_cvt *= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Upmix mono to stereo (by duplication) */ +static void SDLCALL +SDL_ConvertMonoToStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) (cvt->buf + cvt->len_cvt); + float *dst = (float *) (cvt->buf + cvt->len_cvt * 2); + int i; + + LOG_DEBUG_CONVERT("mono", "stereo"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / sizeof (float); i; --i) { + src--; + dst -= 2; + dst[0] = dst[1] = *src; + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Upmix stereo to a pseudo-5.1 stream */ +static void SDLCALL +SDL_ConvertStereoTo51(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + int i; + float lf, rf, ce; + const float *src = (const float *) (cvt->buf + cvt->len_cvt); + float *dst = (float *) (cvt->buf + cvt->len_cvt * 3); + + LOG_DEBUG_CONVERT("stereo", "5.1"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / (sizeof(float) * 2); i; --i) { + dst -= 6; + src -= 2; + lf = src[0]; + rf = src[1]; + ce = (lf + rf) * 0.5f; + /* !!! FIXME: FL and FR may clip */ + dst[0] = lf + (lf - ce); /* FL */ + dst[1] = rf + (rf - ce); /* FR */ + dst[2] = ce; /* FC */ + dst[3] = 0; /* LFE (only meant for special LFE effects) */ + dst[4] = lf; /* BL */ + dst[5] = rf; /* BR */ + } + + cvt->len_cvt *= 3; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Upmix quad to a pseudo-5.1 stream */ +static void SDLCALL +SDL_ConvertQuadTo51(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + int i; + float lf, rf, lb, rb, ce; + const float *src = (const float *) (cvt->buf + cvt->len_cvt); + float *dst = (float *) (cvt->buf + cvt->len_cvt * 3 / 2); + + LOG_DEBUG_CONVERT("quad", "5.1"); + SDL_assert(format == AUDIO_F32SYS); + SDL_assert(cvt->len_cvt % (sizeof(float) * 4) == 0); + + for (i = cvt->len_cvt / (sizeof(float) * 4); i; --i) { + dst -= 6; + src -= 4; + lf = src[0]; + rf = src[1]; + lb = src[2]; + rb = src[3]; + ce = (lf + rf) * 0.5f; + /* !!! FIXME: FL and FR may clip */ + dst[0] = lf + (lf - ce); /* FL */ + dst[1] = rf + (rf - ce); /* FR */ + dst[2] = ce; /* FC */ + dst[3] = 0; /* LFE (only meant for special LFE effects) */ + dst[4] = lb; /* BL */ + dst[5] = rb; /* BR */ + } + + cvt->len_cvt = cvt->len_cvt * 3 / 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Upmix stereo to a pseudo-4.0 stream (by duplication) */ +static void SDLCALL +SDL_ConvertStereoToQuad(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) (cvt->buf + cvt->len_cvt); + float *dst = (float *) (cvt->buf + cvt->len_cvt * 2); + float lf, rf; + int i; + + LOG_DEBUG_CONVERT("stereo", "quad"); + SDL_assert(format == AUDIO_F32SYS); + + for (i = cvt->len_cvt / (sizeof(float) * 2); i; --i) { + dst -= 4; + src -= 2; + lf = src[0]; + rf = src[1]; + dst[0] = lf; /* FL */ + dst[1] = rf; /* FR */ + dst[2] = lf; /* BL */ + dst[3] = rf; /* BR */ + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + + +/* Upmix 5.1 to 7.1 */ +static void SDLCALL +SDL_Convert51To71(SDL_AudioCVT * cvt, SDL_AudioFormat format) +{ + float lf, rf, lb, rb, ls, rs; + int i; + const float *src = (const float *) (cvt->buf + cvt->len_cvt); + float *dst = (float *) (cvt->buf + cvt->len_cvt * 4 / 3); + + LOG_DEBUG_CONVERT("5.1", "7.1"); + SDL_assert(format == AUDIO_F32SYS); + SDL_assert(cvt->len_cvt % (sizeof(float) * 6) == 0); + + for (i = cvt->len_cvt / (sizeof(float) * 6); i; --i) { + dst -= 8; + src -= 6; + lf = src[0]; + rf = src[1]; + lb = src[4]; + rb = src[5]; + ls = (lf + lb) * 0.5f; + rs = (rf + rb) * 0.5f; + /* !!! FIXME: these four may clip */ + lf += lf - ls; + rf += rf - ls; + lb += lb - ls; + rb += rb - ls; + dst[3] = src[3]; /* LFE */ + dst[2] = src[2]; /* FC */ + dst[7] = rs; /* SR */ + dst[6] = ls; /* SL */ + dst[5] = rb; /* BR */ + dst[4] = lb; /* BL */ + dst[1] = rf; /* FR */ + dst[0] = lf; /* FL */ + } + + cvt->len_cvt = cvt->len_cvt * 4 / 3; + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index] (cvt, format); + } +} + +/* SDL's resampler uses a "bandlimited interpolation" algorithm: + https://ccrma.stanford.edu/~jos/resample/ */ + +#define RESAMPLER_ZERO_CROSSINGS 5 +#define RESAMPLER_BITS_PER_SAMPLE 16 +#define RESAMPLER_SAMPLES_PER_ZERO_CROSSING (1 << ((RESAMPLER_BITS_PER_SAMPLE / 2) + 1)) +#define RESAMPLER_FILTER_SIZE ((RESAMPLER_SAMPLES_PER_ZERO_CROSSING * RESAMPLER_ZERO_CROSSINGS) + 1) + +/* This is a "modified" bessel function, so you can't use POSIX j0() */ +static double +bessel(const double x) +{ + const double xdiv2 = x / 2.0; + double i0 = 1.0f; + double f = 1.0f; + int i = 1; + + while (SDL_TRUE) { + const double diff = SDL_pow(xdiv2, i * 2) / SDL_pow(f, 2); + if (diff < 1.0e-21f) { + break; + } + i0 += diff; + i++; + f *= (double) i; + } + + return i0; +} + +/* build kaiser table with cardinal sine applied to it, and array of differences between elements. */ +static void +kaiser_and_sinc(float *table, float *diffs, const int tablelen, const double beta) +{ + const int lenm1 = tablelen - 1; + const int lenm1div2 = lenm1 / 2; + int i; + + table[0] = 1.0f; + for (i = 1; i < tablelen; i++) { + const double kaiser = bessel(beta * SDL_sqrt(1.0 - SDL_pow(((i - lenm1) / 2.0) / lenm1div2, 2.0))) / bessel(beta); + table[tablelen - i] = (float) kaiser; + } + + for (i = 1; i < tablelen; i++) { + const float x = (((float) i) / ((float) RESAMPLER_SAMPLES_PER_ZERO_CROSSING)) * ((float) M_PI); + table[i] *= SDL_sinf(x) / x; + diffs[i - 1] = table[i] - table[i - 1]; + } + diffs[lenm1] = 0.0f; +} + + +static SDL_SpinLock ResampleFilterSpinlock = 0; +static float *ResamplerFilter = NULL; +static float *ResamplerFilterDifference = NULL; + +int +SDL_PrepareResampleFilter(void) +{ + SDL_AtomicLock(&ResampleFilterSpinlock); + if (!ResamplerFilter) { + /* if dB > 50, beta=(0.1102 * (dB - 8.7)), according to Matlab. */ + const double dB = 80.0; + const double beta = 0.1102 * (dB - 8.7); + const size_t alloclen = RESAMPLER_FILTER_SIZE * sizeof (float); + + ResamplerFilter = (float *) SDL_malloc(alloclen); + if (!ResamplerFilter) { + SDL_AtomicUnlock(&ResampleFilterSpinlock); + return SDL_OutOfMemory(); + } + + ResamplerFilterDifference = (float *) SDL_malloc(alloclen); + if (!ResamplerFilterDifference) { + SDL_free(ResamplerFilter); + ResamplerFilter = NULL; + SDL_AtomicUnlock(&ResampleFilterSpinlock); + return SDL_OutOfMemory(); + } + kaiser_and_sinc(ResamplerFilter, ResamplerFilterDifference, RESAMPLER_FILTER_SIZE, beta); + } + SDL_AtomicUnlock(&ResampleFilterSpinlock); + return 0; +} + +void +SDL_FreeResampleFilter(void) +{ + SDL_free(ResamplerFilter); + SDL_free(ResamplerFilterDifference); + ResamplerFilter = NULL; + ResamplerFilterDifference = NULL; +} + +static int +ResamplerPadding(const int inrate, const int outrate) +{ + if (inrate == outrate) { + return 0; + } else if (inrate > outrate) { + return (int) SDL_ceil(((float) (RESAMPLER_SAMPLES_PER_ZERO_CROSSING * inrate) / ((float) outrate))); + } + return RESAMPLER_SAMPLES_PER_ZERO_CROSSING; +} + +/* lpadding and rpadding are expected to be buffers of (ResamplePadding(inrate, outrate) * chans * sizeof (float)) bytes. */ +static int +SDL_ResampleAudio(const int chans, const int inrate, const int outrate, + const float *lpadding, const float *rpadding, + const float *inbuf, const int inbuflen, + float *outbuf, const int outbuflen) +{ + const double finrate = (double) inrate; + const double outtimeincr = 1.0 / ((float) outrate); + const double ratio = ((float) outrate) / ((float) inrate); + const int paddinglen = ResamplerPadding(inrate, outrate); + const int framelen = chans * (int)sizeof (float); + const int inframes = inbuflen / framelen; + const int wantedoutframes = (int) ((inbuflen / framelen) * ratio); /* outbuflen isn't total to write, it's total available. */ + const int maxoutframes = outbuflen / framelen; + const int outframes = SDL_min(wantedoutframes, maxoutframes); + float *dst = outbuf; + double outtime = 0.0; + int i, j, chan; + + for (i = 0; i < outframes; i++) { + const int srcindex = (int) (outtime * inrate); + const double intime = ((double) srcindex) / finrate; + const double innexttime = ((double) (srcindex + 1)) / finrate; + const double interpolation1 = 1.0 - ((innexttime - outtime) / (innexttime - intime)); + const int filterindex1 = (int) (interpolation1 * RESAMPLER_SAMPLES_PER_ZERO_CROSSING); + const double interpolation2 = 1.0 - interpolation1; + const int filterindex2 = (int) (interpolation2 * RESAMPLER_SAMPLES_PER_ZERO_CROSSING); + + for (chan = 0; chan < chans; chan++) { + float outsample = 0.0f; + + /* do this twice to calculate the sample, once for the "left wing" and then same for the right. */ + /* !!! FIXME: do both wings in one loop */ + for (j = 0; (filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)) < RESAMPLER_FILTER_SIZE; j++) { + const int srcframe = srcindex - j; + /* !!! FIXME: we can bubble this conditional out of here by doing a pre loop. */ + const float insample = (srcframe < 0) ? lpadding[((paddinglen + srcframe) * chans) + chan] : inbuf[(srcframe * chans) + chan]; + outsample += (float)(insample * (ResamplerFilter[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)] + (interpolation1 * ResamplerFilterDifference[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)]))); + } + + for (j = 0; (filterindex2 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)) < RESAMPLER_FILTER_SIZE; j++) { + const int srcframe = srcindex + 1 + j; + /* !!! FIXME: we can bubble this conditional out of here by doing a post loop. */ + const float insample = (srcframe >= inframes) ? rpadding[((srcframe - inframes) * chans) + chan] : inbuf[(srcframe * chans) + chan]; + outsample += (float)(insample * (ResamplerFilter[filterindex2 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)] + (interpolation2 * ResamplerFilterDifference[filterindex2 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)]))); + } + *(dst++) = outsample; + } + + outtime += outtimeincr; + } + + return outframes * chans * sizeof (float); +} + +int +SDL_ConvertAudio(SDL_AudioCVT * cvt) +{ + /* !!! FIXME: (cvt) should be const; stack-copy it here. */ + /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */ + + /* Make sure there's data to convert */ + if (cvt->buf == NULL) { + return SDL_SetError("No buffer allocated for conversion"); + } + + /* Return okay if no conversion is necessary */ + cvt->len_cvt = cvt->len; + if (cvt->filters[0] == NULL) { + return 0; + } + + /* Set up the conversion and go! */ + cvt->filter_index = 0; + cvt->filters[0] (cvt, cvt->src_format); + return 0; +} + +static void SDLCALL +SDL_Convert_Byteswap(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ +#if DEBUG_CONVERT + printf("Converting byte order\n"); +#endif + + switch (SDL_AUDIO_BITSIZE(format)) { + #define CASESWAP(b) \ + case b: { \ + Uint##b *ptr = (Uint##b *) cvt->buf; \ + int i; \ + for (i = cvt->len_cvt / sizeof (*ptr); i; --i, ++ptr) { \ + *ptr = SDL_Swap##b(*ptr); \ + } \ + break; \ + } + + CASESWAP(16); + CASESWAP(32); + CASESWAP(64); + + #undef CASESWAP + + default: SDL_assert(!"unhandled byteswap datatype!"); break; + } + + if (cvt->filters[++cvt->filter_index]) { + /* flip endian flag for data. */ + if (format & SDL_AUDIO_MASK_ENDIAN) { + format &= ~SDL_AUDIO_MASK_ENDIAN; + } else { + format |= SDL_AUDIO_MASK_ENDIAN; + } + cvt->filters[cvt->filter_index](cvt, format); + } +} + +static int +SDL_AddAudioCVTFilter(SDL_AudioCVT *cvt, const SDL_AudioFilter filter) +{ + if (cvt->filter_index >= SDL_AUDIOCVT_MAX_FILTERS) { + return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS); + } + if (filter == NULL) { + return SDL_SetError("Audio filter pointer is NULL"); + } + cvt->filters[cvt->filter_index++] = filter; + cvt->filters[cvt->filter_index] = NULL; /* Moving terminator */ + return 0; +} + +static int +SDL_BuildAudioTypeCVTToFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat src_fmt) +{ + int retval = 0; /* 0 == no conversion necessary. */ + + if ((SDL_AUDIO_ISBIGENDIAN(src_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) { + return -1; + } + retval = 1; /* added a converter. */ + } + + if (!SDL_AUDIO_ISFLOAT(src_fmt)) { + const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt); + const Uint16 dst_bitsize = 32; + SDL_AudioFilter filter = NULL; + + switch (src_fmt & ~SDL_AUDIO_MASK_ENDIAN) { + case AUDIO_S8: filter = SDL_Convert_S8_to_F32; break; + case AUDIO_U8: filter = SDL_Convert_U8_to_F32; break; + case AUDIO_S16: filter = SDL_Convert_S16_to_F32; break; + case AUDIO_U16: filter = SDL_Convert_U16_to_F32; break; + case AUDIO_S32: filter = SDL_Convert_S32_to_F32; break; + default: SDL_assert(!"Unexpected audio format!"); break; + } + + if (!filter) { + return SDL_SetError("No conversion from source format to float available"); + } + + if (SDL_AddAudioCVTFilter(cvt, filter) < 0) { + return -1; + } + if (src_bitsize < dst_bitsize) { + const int mult = (dst_bitsize / src_bitsize); + cvt->len_mult *= mult; + cvt->len_ratio *= mult; + } else if (src_bitsize > dst_bitsize) { + cvt->len_ratio /= (src_bitsize / dst_bitsize); + } + + retval = 1; /* added a converter. */ + } + + return retval; +} + +static int +SDL_BuildAudioTypeCVTFromFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat dst_fmt) +{ + int retval = 0; /* 0 == no conversion necessary. */ + + if (!SDL_AUDIO_ISFLOAT(dst_fmt)) { + const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt); + const Uint16 src_bitsize = 32; + SDL_AudioFilter filter = NULL; + switch (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN) { + case AUDIO_S8: filter = SDL_Convert_F32_to_S8; break; + case AUDIO_U8: filter = SDL_Convert_F32_to_U8; break; + case AUDIO_S16: filter = SDL_Convert_F32_to_S16; break; + case AUDIO_U16: filter = SDL_Convert_F32_to_U16; break; + case AUDIO_S32: filter = SDL_Convert_F32_to_S32; break; + default: SDL_assert(!"Unexpected audio format!"); break; + } + + if (!filter) { + return SDL_SetError("No conversion from float to destination format available"); + } + + if (SDL_AddAudioCVTFilter(cvt, filter) < 0) { + return -1; + } + if (src_bitsize < dst_bitsize) { + const int mult = (dst_bitsize / src_bitsize); + cvt->len_mult *= mult; + cvt->len_ratio *= mult; + } else if (src_bitsize > dst_bitsize) { + cvt->len_ratio /= (src_bitsize / dst_bitsize); + } + retval = 1; /* added a converter. */ + } + + if ((SDL_AUDIO_ISBIGENDIAN(dst_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) { + return -1; + } + retval = 1; /* added a converter. */ + } + + return retval; +} + +static void +SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format) +{ + /* !!! FIXME in 2.1: there are ten slots in the filter list, and the theoretical maximum we use is six (seven with NULL terminator). + !!! FIXME in 2.1: We need to store data for this resampler, because the cvt structure doesn't store the original sample rates, + !!! FIXME in 2.1: so we steal the ninth and tenth slot. :( */ + const int inrate = (int) (size_t) cvt->filters[SDL_AUDIOCVT_MAX_FILTERS-1]; + const int outrate = (int) (size_t) cvt->filters[SDL_AUDIOCVT_MAX_FILTERS]; + const float *src = (const float *) cvt->buf; + const int srclen = cvt->len_cvt; + /*float *dst = (float *) cvt->buf; + const int dstlen = (cvt->len * cvt->len_mult);*/ + /* !!! FIXME: remove this if we can get the resampler to work in-place again. */ + float *dst = (float *) (cvt->buf + srclen); + const int dstlen = (cvt->len * cvt->len_mult) - srclen; + const int paddingsamples = (ResamplerPadding(inrate, outrate) * chans); + float *padding; + + SDL_assert(format == AUDIO_F32SYS); + + /* we keep no streaming state here, so pad with silence on both ends. */ + padding = (float *) SDL_calloc(paddingsamples, sizeof (float)); + if (!padding) { + SDL_OutOfMemory(); + return; + } + + cvt->len_cvt = SDL_ResampleAudio(chans, inrate, outrate, padding, padding, src, srclen, dst, dstlen); + + SDL_free(padding); + + SDL_memmove(cvt->buf, dst, cvt->len_cvt); /* !!! FIXME: remove this if we can get the resampler to work in-place again. */ + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, format); + } +} + +/* !!! FIXME: We only have this macro salsa because SDL_AudioCVT doesn't + !!! FIXME: store channel info, so we have to have function entry + !!! FIXME: points for each supported channel count and multiple + !!! FIXME: vs arbitrary. When we rev the ABI, clean this up. */ +#define RESAMPLER_FUNCS(chans) \ + static void SDLCALL \ + SDL_ResampleCVT_c##chans(SDL_AudioCVT *cvt, SDL_AudioFormat format) { \ + SDL_ResampleCVT(cvt, chans, format); \ + } +RESAMPLER_FUNCS(1) +RESAMPLER_FUNCS(2) +RESAMPLER_FUNCS(4) +RESAMPLER_FUNCS(6) +RESAMPLER_FUNCS(8) +#undef RESAMPLER_FUNCS + +static SDL_AudioFilter +ChooseCVTResampler(const int dst_channels) +{ + switch (dst_channels) { + case 1: return SDL_ResampleCVT_c1; + case 2: return SDL_ResampleCVT_c2; + case 4: return SDL_ResampleCVT_c4; + case 6: return SDL_ResampleCVT_c6; + case 8: return SDL_ResampleCVT_c8; + default: break; + } + + return NULL; +} + +static int +SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, const int dst_channels, + const int src_rate, const int dst_rate) +{ + SDL_AudioFilter filter; + + if (src_rate == dst_rate) { + return 0; /* no conversion necessary. */ + } + + filter = ChooseCVTResampler(dst_channels); + if (filter == NULL) { + return SDL_SetError("No conversion available for these rates"); + } + + if (SDL_PrepareResampleFilter() < 0) { + return -1; + } + + /* Update (cvt) with filter details... */ + if (SDL_AddAudioCVTFilter(cvt, filter) < 0) { + return -1; + } + + /* !!! FIXME in 2.1: there are ten slots in the filter list, and the theoretical maximum we use is six (seven with NULL terminator). + !!! FIXME in 2.1: We need to store data for this resampler, because the cvt structure doesn't store the original sample rates, + !!! FIXME in 2.1: so we steal the ninth and tenth slot. :( */ + if (cvt->filter_index >= (SDL_AUDIOCVT_MAX_FILTERS-2)) { + return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS-2); + } + cvt->filters[SDL_AUDIOCVT_MAX_FILTERS-1] = (SDL_AudioFilter) (size_t) src_rate; + cvt->filters[SDL_AUDIOCVT_MAX_FILTERS] = (SDL_AudioFilter) (size_t) dst_rate; + + if (src_rate < dst_rate) { + const double mult = ((double) dst_rate) / ((double) src_rate); + cvt->len_mult *= (int) SDL_ceil(mult); + cvt->len_ratio *= mult; + } else { + cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate); + } + + /* !!! FIXME: remove this if we can get the resampler to work in-place again. */ + /* the buffer is big enough to hold the destination now, but + we need it large enough to hold a separate scratch buffer. */ + cvt->len_mult *= 2; + + return 1; /* added a converter. */ +} + +static SDL_bool +SDL_SupportedAudioFormat(const SDL_AudioFormat fmt) +{ + switch (fmt) { + case AUDIO_U8: + case AUDIO_S8: + case AUDIO_U16LSB: + case AUDIO_S16LSB: + case AUDIO_U16MSB: + case AUDIO_S16MSB: + case AUDIO_S32LSB: + case AUDIO_S32MSB: + case AUDIO_F32LSB: + case AUDIO_F32MSB: + return SDL_TRUE; /* supported. */ + + default: + break; + } + + return SDL_FALSE; /* unsupported. */ +} + +static SDL_bool +SDL_SupportedChannelCount(const int channels) +{ + switch (channels) { + case 1: /* mono */ + case 2: /* stereo */ + case 4: /* quad */ + case 6: /* 5.1 */ + case 8: /* 7.1 */ + return SDL_TRUE; /* supported. */ + + default: + break; + } + + return SDL_FALSE; /* unsupported. */ +} + + +/* Creates a set of audio filters to convert from one format to another. + Returns 0 if no conversion is needed, 1 if the audio filter is set up, + or -1 if an error like invalid parameter, unsupported format, etc. occurred. +*/ + +int +SDL_BuildAudioCVT(SDL_AudioCVT * cvt, + SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate, + SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate) +{ + /* Sanity check target pointer */ + if (cvt == NULL) { + return SDL_InvalidParamError("cvt"); + } + + /* Make sure we zero out the audio conversion before error checking */ + SDL_zerop(cvt); + + if (!SDL_SupportedAudioFormat(src_fmt)) { + return SDL_SetError("Invalid source format"); + } else if (!SDL_SupportedAudioFormat(dst_fmt)) { + return SDL_SetError("Invalid destination format"); + } else if (!SDL_SupportedChannelCount(src_channels)) { + return SDL_SetError("Invalid source channels"); + } else if (!SDL_SupportedChannelCount(dst_channels)) { + return SDL_SetError("Invalid destination channels"); + } else if (src_rate == 0) { + return SDL_SetError("Source rate is zero"); + } else if (dst_rate == 0) { + return SDL_SetError("Destination rate is zero"); + } + +#if DEBUG_CONVERT + printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n", + src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate); +#endif + + /* Start off with no conversion necessary */ + cvt->src_format = src_fmt; + cvt->dst_format = dst_fmt; + cvt->needed = 0; + cvt->filter_index = 0; + SDL_zero(cvt->filters); + cvt->len_mult = 1; + cvt->len_ratio = 1.0; + cvt->rate_incr = ((double) dst_rate) / ((double) src_rate); + + /* Make sure we've chosen audio conversion functions (MMX, scalar, etc.) */ + SDL_ChooseAudioConverters(); + + /* Type conversion goes like this now: + - byteswap to CPU native format first if necessary. + - convert to native Float32 if necessary. + - resample and change channel count if necessary. + - convert back to native format. + - byteswap back to foreign format if necessary. + + The expectation is we can process data faster in float32 + (possibly with SIMD), and making several passes over the same + buffer is likely to be CPU cache-friendly, avoiding the + biggest performance hit in modern times. Previously we had + (script-generated) custom converters for every data type and + it was a bloat on SDL compile times and final library size. */ + + /* see if we can skip float conversion entirely. */ + if (src_rate == dst_rate && src_channels == dst_channels) { + if (src_fmt == dst_fmt) { + return 0; + } + + /* just a byteswap needed? */ + if ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) { + return -1; + } + cvt->needed = 1; + return 1; + } + } + + /* Convert data types, if necessary. Updates (cvt). */ + if (SDL_BuildAudioTypeCVTToFloat(cvt, src_fmt) < 0) { + return -1; /* shouldn't happen, but just in case... */ + } + + /* Channel conversion */ + if (src_channels < dst_channels) { + /* Upmixing */ + /* Mono -> Stereo [-> ...] */ + if ((src_channels == 1) && (dst_channels > 1)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertMonoToStereo) < 0) { + return -1; + } + cvt->len_mult *= 2; + src_channels = 2; + cvt->len_ratio *= 2; + } + /* [Mono ->] Stereo -> 5.1 [-> 7.1] */ + if ((src_channels == 2) && (dst_channels >= 6)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertStereoTo51) < 0) { + return -1; + } + src_channels = 6; + cvt->len_mult *= 3; + cvt->len_ratio *= 3; + } + /* Quad -> 5.1 [-> 7.1] */ + if ((src_channels == 4) && (dst_channels >= 6)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertQuadTo51) < 0) { + return -1; + } + src_channels = 6; + cvt->len_mult = (cvt->len_mult * 3 + 1) / 2; + cvt->len_ratio *= 1.5; + } + /* [[Mono ->] Stereo ->] 5.1 -> 7.1 */ + if ((src_channels == 6) && (dst_channels == 8)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert51To71) < 0) { + return -1; + } + src_channels = 8; + cvt->len_mult = (cvt->len_mult * 4 + 2) / 3; + /* Should be numerically exact with every valid input to this + function */ + cvt->len_ratio = cvt->len_ratio * 4 / 3; + } + /* [Mono ->] Stereo -> Quad */ + if ((src_channels == 2) && (dst_channels == 4)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertStereoToQuad) < 0) { + return -1; + } + src_channels = 4; + cvt->len_mult *= 2; + cvt->len_ratio *= 2; + } + } else if (src_channels > dst_channels) { + /* Downmixing */ + /* 7.1 -> 5.1 [-> Stereo [-> Mono]] */ + /* 7.1 -> 5.1 [-> Quad] */ + if ((src_channels == 8) && (dst_channels <= 6)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert71To51) < 0) { + return -1; + } + src_channels = 6; + cvt->len_ratio *= 0.75; + } + /* [7.1 ->] 5.1 -> Stereo [-> Mono] */ + if ((src_channels == 6) && (dst_channels <= 2)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert51ToStereo) < 0) { + return -1; + } + src_channels = 2; + cvt->len_ratio /= 3; + } + /* 5.1 -> Quad */ + if ((src_channels == 6) && (dst_channels == 4)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_Convert51ToQuad) < 0) { + return -1; + } + src_channels = 4; + cvt->len_ratio = cvt->len_ratio * 2 / 3; + } + /* Quad -> Stereo [-> Mono] */ + if ((src_channels == 4) && (dst_channels <= 2)) { + if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertQuadToStereo) < 0) { + return -1; + } + src_channels = 2; + cvt->len_ratio /= 2; + } + /* [... ->] Stereo -> Mono */ + if ((src_channels == 2) && (dst_channels == 1)) { + SDL_AudioFilter filter = NULL; + + #if HAVE_SSE3_INTRINSICS + if (SDL_HasSSE3()) { + filter = SDL_ConvertStereoToMono_SSE3; + } + #endif + + if (!filter) { + filter = SDL_ConvertStereoToMono; + } + + if (SDL_AddAudioCVTFilter(cvt, filter) < 0) { + return -1; + } + + src_channels = 1; + cvt->len_ratio /= 2; + } + } + + if (src_channels != dst_channels) { + /* All combinations of supported channel counts should have been + handled by now, but let's be defensive */ + return SDL_SetError("Invalid channel combination"); + } + + /* Do rate conversion, if necessary. Updates (cvt). */ + if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) < 0) { + return -1; /* shouldn't happen, but just in case... */ + } + + /* Move to final data type. */ + if (SDL_BuildAudioTypeCVTFromFloat(cvt, dst_fmt) < 0) { + return -1; /* shouldn't happen, but just in case... */ + } + + cvt->needed = (cvt->filter_index != 0); + return (cvt->needed); +} + +typedef int (*SDL_ResampleAudioStreamFunc)(SDL_AudioStream *stream, const void *inbuf, const int inbuflen, void *outbuf, const int outbuflen); +typedef void (*SDL_ResetAudioStreamResamplerFunc)(SDL_AudioStream *stream); +typedef void (*SDL_CleanupAudioStreamResamplerFunc)(SDL_AudioStream *stream); + +struct _SDL_AudioStream +{ + SDL_AudioCVT cvt_before_resampling; + SDL_AudioCVT cvt_after_resampling; + SDL_DataQueue *queue; + SDL_bool first_run; + Uint8 *staging_buffer; + int staging_buffer_size; + int staging_buffer_filled; + Uint8 *work_buffer_base; /* maybe unaligned pointer from SDL_realloc(). */ + int work_buffer_len; + int src_sample_frame_size; + SDL_AudioFormat src_format; + Uint8 src_channels; + int src_rate; + int dst_sample_frame_size; + SDL_AudioFormat dst_format; + Uint8 dst_channels; + int dst_rate; + double rate_incr; + Uint8 pre_resample_channels; + int packetlen; + int resampler_padding_samples; + float *resampler_padding; + void *resampler_state; + SDL_ResampleAudioStreamFunc resampler_func; + SDL_ResetAudioStreamResamplerFunc reset_resampler_func; + SDL_CleanupAudioStreamResamplerFunc cleanup_resampler_func; +}; + +static Uint8 * +EnsureStreamBufferSize(SDL_AudioStream *stream, const int newlen) +{ + Uint8 *ptr; + size_t offset; + + if (stream->work_buffer_len >= newlen) { + ptr = stream->work_buffer_base; + } else { + ptr = (Uint8 *) SDL_realloc(stream->work_buffer_base, newlen + 32); + if (!ptr) { + SDL_OutOfMemory(); + return NULL; + } + /* Make sure we're aligned to 16 bytes for SIMD code. */ + stream->work_buffer_base = ptr; + stream->work_buffer_len = newlen; + } + + offset = ((size_t) ptr) & 15; + return offset ? ptr + (16 - offset) : ptr; +} + +#ifdef HAVE_LIBSAMPLERATE_H +static int +SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const void *_inbuf, const int inbuflen, void *_outbuf, const int outbuflen) +{ + const float *inbuf = (const float *) _inbuf; + float *outbuf = (float *) _outbuf; + const int framelen = sizeof(float) * stream->pre_resample_channels; + SRC_STATE *state = (SRC_STATE *)stream->resampler_state; + SRC_DATA data; + int result; + + SDL_assert(inbuf != ((const float *) outbuf)); /* SDL_AudioStreamPut() shouldn't allow in-place resamples. */ + + data.data_in = (float *)inbuf; /* Older versions of libsamplerate had a non-const pointer, but didn't write to it */ + data.input_frames = inbuflen / framelen; + data.input_frames_used = 0; + + data.data_out = outbuf; + data.output_frames = outbuflen / framelen; + + data.end_of_input = 0; + data.src_ratio = stream->rate_incr; + + result = SRC_src_process(state, &data); + if (result != 0) { + SDL_SetError("src_process() failed: %s", SRC_src_strerror(result)); + return 0; + } + + /* If this fails, we need to store them off somewhere */ + SDL_assert(data.input_frames_used == data.input_frames); + + return data.output_frames_gen * (sizeof(float) * stream->pre_resample_channels); +} + +static void +SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *stream) +{ + SRC_src_reset((SRC_STATE *)stream->resampler_state); +} + +static void +SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream) +{ + SRC_STATE *state = (SRC_STATE *)stream->resampler_state; + if (state) { + SRC_src_delete(state); + } + + stream->resampler_state = NULL; + stream->resampler_func = NULL; + stream->reset_resampler_func = NULL; + stream->cleanup_resampler_func = NULL; +} + +static SDL_bool +SetupLibSampleRateResampling(SDL_AudioStream *stream) +{ + int result = 0; + SRC_STATE *state = NULL; + + if (SRC_available) { + state = SRC_src_new(SRC_converter, stream->pre_resample_channels, &result); + if (!state) { + SDL_SetError("src_new() failed: %s", SRC_src_strerror(result)); + } + } + + if (!state) { + SDL_CleanupAudioStreamResampler_SRC(stream); + return SDL_FALSE; + } + + stream->resampler_state = state; + stream->resampler_func = SDL_ResampleAudioStream_SRC; + stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC; + stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC; + + return SDL_TRUE; +} +#endif /* HAVE_LIBSAMPLERATE_H */ + + +static int +SDL_ResampleAudioStream(SDL_AudioStream *stream, const void *_inbuf, const int inbuflen, void *_outbuf, const int outbuflen) +{ + const Uint8 *inbufend = ((const Uint8 *) _inbuf) + inbuflen; + const float *inbuf = (const float *) _inbuf; + float *outbuf = (float *) _outbuf; + const int chans = (int) stream->pre_resample_channels; + const int inrate = stream->src_rate; + const int outrate = stream->dst_rate; + const int paddingsamples = stream->resampler_padding_samples; + const int paddingbytes = paddingsamples * sizeof (float); + float *lpadding = (float *) stream->resampler_state; + const float *rpadding = (const float *) inbufend; /* we set this up so there are valid padding samples at the end of the input buffer. */ + const int cpy = SDL_min(inbuflen, paddingbytes); + int retval; + + SDL_assert(inbuf != ((const float *) outbuf)); /* SDL_AudioStreamPut() shouldn't allow in-place resamples. */ + + retval = SDL_ResampleAudio(chans, inrate, outrate, lpadding, rpadding, inbuf, inbuflen, outbuf, outbuflen); + + /* update our left padding with end of current input, for next run. */ + SDL_memcpy((lpadding + paddingsamples) - (cpy / sizeof (float)), inbufend - cpy, cpy); + return retval; +} + +static void +SDL_ResetAudioStreamResampler(SDL_AudioStream *stream) +{ + /* set all the padding to silence. */ + const int len = stream->resampler_padding_samples; + SDL_memset(stream->resampler_state, '\0', len * sizeof (float)); +} + +static void +SDL_CleanupAudioStreamResampler(SDL_AudioStream *stream) +{ + SDL_free(stream->resampler_state); +} + +SDL_AudioStream * +SDL_NewAudioStream(const SDL_AudioFormat src_format, + const Uint8 src_channels, + const int src_rate, + const SDL_AudioFormat dst_format, + const Uint8 dst_channels, + const int dst_rate) +{ + const int packetlen = 4096; /* !!! FIXME: good enough for now. */ + Uint8 pre_resample_channels; + SDL_AudioStream *retval; + + retval = (SDL_AudioStream *) SDL_calloc(1, sizeof (SDL_AudioStream)); + if (!retval) { + return NULL; + } + + /* If increasing channels, do it after resampling, since we'd just + do more work to resample duplicate channels. If we're decreasing, do + it first so we resample the interpolated data instead of interpolating + the resampled data (!!! FIXME: decide if that works in practice, though!). */ + pre_resample_channels = SDL_min(src_channels, dst_channels); + + retval->first_run = SDL_TRUE; + retval->src_sample_frame_size = (SDL_AUDIO_BITSIZE(src_format) / 8) * src_channels; + retval->src_format = src_format; + retval->src_channels = src_channels; + retval->src_rate = src_rate; + retval->dst_sample_frame_size = (SDL_AUDIO_BITSIZE(dst_format) / 8) * dst_channels; + retval->dst_format = dst_format; + retval->dst_channels = dst_channels; + retval->dst_rate = dst_rate; + retval->pre_resample_channels = pre_resample_channels; + retval->packetlen = packetlen; + retval->rate_incr = ((double) dst_rate) / ((double) src_rate); + retval->resampler_padding_samples = ResamplerPadding(retval->src_rate, retval->dst_rate) * pre_resample_channels; + retval->resampler_padding = (float *) SDL_calloc(retval->resampler_padding_samples, sizeof (float)); + + if (retval->resampler_padding == NULL) { + SDL_FreeAudioStream(retval); + SDL_OutOfMemory(); + return NULL; + } + + retval->staging_buffer_size = ((retval->resampler_padding_samples / retval->pre_resample_channels) * retval->src_sample_frame_size); + if (retval->staging_buffer_size > 0) { + retval->staging_buffer = (Uint8 *) SDL_malloc(retval->staging_buffer_size); + if (retval->staging_buffer == NULL) { + SDL_FreeAudioStream(retval); + SDL_OutOfMemory(); + return NULL; + } + } + + /* Not resampling? It's an easy conversion (and maybe not even that!) */ + if (src_rate == dst_rate) { + retval->cvt_before_resampling.needed = SDL_FALSE; + if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, src_format, src_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) { + SDL_FreeAudioStream(retval); + return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */ + } + } else { + /* Don't resample at first. Just get us to Float32 format. */ + /* !!! FIXME: convert to int32 on devices without hardware float. */ + if (SDL_BuildAudioCVT(&retval->cvt_before_resampling, src_format, src_channels, src_rate, AUDIO_F32SYS, pre_resample_channels, src_rate) < 0) { + SDL_FreeAudioStream(retval); + return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */ + } + +#ifdef HAVE_LIBSAMPLERATE_H + SetupLibSampleRateResampling(retval); +#endif + + if (!retval->resampler_func) { + retval->resampler_state = SDL_calloc(retval->resampler_padding_samples, sizeof (float)); + if (!retval->resampler_state) { + SDL_FreeAudioStream(retval); + SDL_OutOfMemory(); + return NULL; + } + + if (SDL_PrepareResampleFilter() < 0) { + SDL_free(retval->resampler_state); + retval->resampler_state = NULL; + SDL_FreeAudioStream(retval); + return NULL; + } + + retval->resampler_func = SDL_ResampleAudioStream; + retval->reset_resampler_func = SDL_ResetAudioStreamResampler; + retval->cleanup_resampler_func = SDL_CleanupAudioStreamResampler; + } + + /* Convert us to the final format after resampling. */ + if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, AUDIO_F32SYS, pre_resample_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) { + SDL_FreeAudioStream(retval); + return NULL; /* SDL_BuildAudioCVT should have called SDL_SetError. */ + } + } + + retval->queue = SDL_NewDataQueue(packetlen, packetlen * 2); + if (!retval->queue) { + SDL_FreeAudioStream(retval); + return NULL; /* SDL_NewDataQueue should have called SDL_SetError. */ + } + + return retval; +} + +static int +SDL_AudioStreamPutInternal(SDL_AudioStream *stream, const void *buf, int len, int *maxputbytes) +{ + int buflen = len; + int workbuflen; + Uint8 *workbuf; + Uint8 *resamplebuf = NULL; + int resamplebuflen = 0; + int neededpaddingbytes; + int paddingbytes; + + /* !!! FIXME: several converters can take advantage of SIMD, but only + !!! FIXME: if the data is aligned to 16 bytes. EnsureStreamBufferSize() + !!! FIXME: guarantees the buffer will align, but the + !!! FIXME: converters will iterate over the data backwards if + !!! FIXME: the output grows, and this means we won't align if buflen + !!! FIXME: isn't a multiple of 16. In these cases, we should chop off + !!! FIXME: a few samples at the end and convert them separately. */ + + /* no padding prepended on first run. */ + neededpaddingbytes = stream->resampler_padding_samples * sizeof (float); + paddingbytes = stream->first_run ? 0 : neededpaddingbytes; + stream->first_run = SDL_FALSE; + + /* Make sure the work buffer can hold all the data we need at once... */ + workbuflen = buflen; + if (stream->cvt_before_resampling.needed) { + workbuflen *= stream->cvt_before_resampling.len_mult; + } + + if (stream->dst_rate != stream->src_rate) { + /* resamples can't happen in place, so make space for second buf. */ + const int framesize = stream->pre_resample_channels * sizeof (float); + const int frames = workbuflen / framesize; + resamplebuflen = ((int) SDL_ceil(frames * stream->rate_incr)) * framesize; + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: will resample %d bytes to %d (ratio=%.6f)\n", workbuflen, resamplebuflen, stream->rate_incr); + #endif + workbuflen += resamplebuflen; + } + + if (stream->cvt_after_resampling.needed) { + /* !!! FIXME: buffer might be big enough already? */ + workbuflen *= stream->cvt_after_resampling.len_mult; + } + + workbuflen += neededpaddingbytes; + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: Putting %d bytes of preconverted audio, need %d byte work buffer\n", buflen, workbuflen); + #endif + + workbuf = EnsureStreamBufferSize(stream, workbuflen); + if (!workbuf) { + return -1; /* probably out of memory. */ + } + + resamplebuf = workbuf; /* default if not resampling. */ + + SDL_memcpy(workbuf + paddingbytes, buf, buflen); + + if (stream->cvt_before_resampling.needed) { + stream->cvt_before_resampling.buf = workbuf + paddingbytes; + stream->cvt_before_resampling.len = buflen; + if (SDL_ConvertAudio(&stream->cvt_before_resampling) == -1) { + return -1; /* uhoh! */ + } + buflen = stream->cvt_before_resampling.len_cvt; + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: After initial conversion we have %d bytes\n", buflen); + #endif + } + + if (stream->dst_rate != stream->src_rate) { + /* save off some samples at the end; they are used for padding now so + the resampler is coherent and then used at the start of the next + put operation. Prepend last put operation's padding, too. */ + + /* prepend prior put's padding. :P */ + if (paddingbytes) { + SDL_memcpy(workbuf, stream->resampler_padding, paddingbytes); + buflen += paddingbytes; + } + + /* save off the data at the end for the next run. */ + SDL_memcpy(stream->resampler_padding, workbuf + (buflen - neededpaddingbytes), neededpaddingbytes); + + resamplebuf = workbuf + buflen; /* skip to second piece of workbuf. */ + SDL_assert(buflen >= neededpaddingbytes); + if (buflen > neededpaddingbytes) { + buflen = stream->resampler_func(stream, workbuf, buflen - neededpaddingbytes, resamplebuf, resamplebuflen); + } else { + buflen = 0; + } + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: After resampling we have %d bytes\n", buflen); + #endif + } + + if (stream->cvt_after_resampling.needed && (buflen > 0)) { + stream->cvt_after_resampling.buf = resamplebuf; + stream->cvt_after_resampling.len = buflen; + if (SDL_ConvertAudio(&stream->cvt_after_resampling) == -1) { + return -1; /* uhoh! */ + } + buflen = stream->cvt_after_resampling.len_cvt; + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: After final conversion we have %d bytes\n", buflen); + #endif + } + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: Final output is %d bytes\n", buflen); + #endif + + if (maxputbytes) { + const int maxbytes = *maxputbytes; + if (buflen > maxbytes) + buflen = maxbytes; + *maxputbytes -= buflen; + } + + /* resamplebuf holds the final output, even if we didn't resample. */ + return buflen ? SDL_WriteToDataQueue(stream->queue, resamplebuf, buflen) : 0; +} + +int +SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len) +{ + /* !!! FIXME: several converters can take advantage of SIMD, but only + !!! FIXME: if the data is aligned to 16 bytes. EnsureStreamBufferSize() + !!! FIXME: guarantees the buffer will align, but the + !!! FIXME: converters will iterate over the data backwards if + !!! FIXME: the output grows, and this means we won't align if buflen + !!! FIXME: isn't a multiple of 16. In these cases, we should chop off + !!! FIXME: a few samples at the end and convert them separately. */ + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: wants to put %d preconverted bytes\n", buflen); + #endif + + if (!stream) { + return SDL_InvalidParamError("stream"); + } else if (!buf) { + return SDL_InvalidParamError("buf"); + } else if (len == 0) { + return 0; /* nothing to do. */ + } else if ((len % stream->src_sample_frame_size) != 0) { + return SDL_SetError("Can't add partial sample frames"); + } + + if (!stream->cvt_before_resampling.needed && + (stream->dst_rate == stream->src_rate) && + !stream->cvt_after_resampling.needed) { + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: no conversion needed at all, queueing %d bytes.\n", len); + #endif + return SDL_WriteToDataQueue(stream->queue, buf, len); + } + + while (len > 0) { + int amount; + + /* If we don't have a staging buffer or we're given enough data that + we don't need to store it for later, skip the staging process. + */ + if (!stream->staging_buffer_filled && len >= stream->staging_buffer_size) { + return SDL_AudioStreamPutInternal(stream, buf, len, NULL); + } + + /* If there's not enough data to fill the staging buffer, just save it */ + if ((stream->staging_buffer_filled + len) < stream->staging_buffer_size) { + SDL_memcpy(stream->staging_buffer + stream->staging_buffer_filled, buf, len); + stream->staging_buffer_filled += len; + return 0; + } + + /* Fill the staging buffer, process it, and continue */ + amount = (stream->staging_buffer_size - stream->staging_buffer_filled); + SDL_assert(amount > 0); + SDL_memcpy(stream->staging_buffer + stream->staging_buffer_filled, buf, amount); + stream->staging_buffer_filled = 0; + if (SDL_AudioStreamPutInternal(stream, stream->staging_buffer, stream->staging_buffer_size, NULL) < 0) { + return -1; + } + buf = (void *)((Uint8 *)buf + amount); + len -= amount; + } + return 0; +} + +int SDL_AudioStreamFlush(SDL_AudioStream *stream) +{ + if (!stream) { + return SDL_InvalidParamError("stream"); + } + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: flushing! staging_buffer_filled=%d bytes\n", stream->staging_buffer_filled); + #endif + + /* shouldn't use a staging buffer if we're not resampling. */ + SDL_assert((stream->dst_rate != stream->src_rate) || (stream->staging_buffer_filled == 0)); + + if (stream->staging_buffer_filled > 0) { + /* push the staging buffer + silence. We need to flush out not just + the staging buffer, but the piece that the stream was saving off + for right-side resampler padding. */ + const SDL_bool first_run = stream->first_run; + const int filled = stream->staging_buffer_filled; + int actual_input_frames = filled / stream->src_sample_frame_size; + if (!first_run) + actual_input_frames += stream->resampler_padding_samples / stream->pre_resample_channels; + + if (actual_input_frames > 0) { /* don't bother if nothing to flush. */ + /* This is how many bytes we're expecting without silence appended. */ + int flush_remaining = ((int) SDL_ceil(actual_input_frames * stream->rate_incr)) * stream->dst_sample_frame_size; + + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: flushing with padding to get max %d bytes!\n", flush_remaining); + #endif + + SDL_memset(stream->staging_buffer + filled, '\0', stream->staging_buffer_size - filled); + if (SDL_AudioStreamPutInternal(stream, stream->staging_buffer, stream->staging_buffer_size, &flush_remaining) < 0) { + return -1; + } + + /* we have flushed out (or initially filled) the pending right-side + resampler padding, but we need to push more silence to guarantee + the staging buffer is fully flushed out, too. */ + SDL_memset(stream->staging_buffer, '\0', filled); + if (SDL_AudioStreamPutInternal(stream, stream->staging_buffer, stream->staging_buffer_size, &flush_remaining) < 0) { + return -1; + } + } + } + + stream->staging_buffer_filled = 0; + stream->first_run = SDL_TRUE; + + return 0; +} + +/* get converted/resampled data from the stream */ +int +SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len) +{ + #if DEBUG_AUDIOSTREAM + printf("AUDIOSTREAM: want to get %d converted bytes\n", len); + #endif + + if (!stream) { + return SDL_InvalidParamError("stream"); + } else if (!buf) { + return SDL_InvalidParamError("buf"); + } else if (len <= 0) { + return 0; /* nothing to do. */ + } else if ((len % stream->dst_sample_frame_size) != 0) { + return SDL_SetError("Can't request partial sample frames"); + } + + return (int) SDL_ReadFromDataQueue(stream->queue, buf, len); +} + +/* number of converted/resampled bytes available */ +int +SDL_AudioStreamAvailable(SDL_AudioStream *stream) +{ + return stream ? (int) SDL_CountDataQueue(stream->queue) : 0; +} + +void +SDL_AudioStreamClear(SDL_AudioStream *stream) +{ + if (!stream) { + SDL_InvalidParamError("stream"); + } else { + SDL_ClearDataQueue(stream->queue, stream->packetlen * 2); + if (stream->reset_resampler_func) { + stream->reset_resampler_func(stream); + } + stream->first_run = SDL_TRUE; + stream->staging_buffer_filled = 0; + } +} + +/* dispose of a stream */ +void +SDL_FreeAudioStream(SDL_AudioStream *stream) +{ + if (stream) { + if (stream->cleanup_resampler_func) { + stream->cleanup_resampler_func(stream); + } + SDL_FreeDataQueue(stream->queue); + SDL_free(stream->staging_buffer); + SDL_free(stream->work_buffer_base); + SDL_free(stream->resampler_padding); + SDL_free(stream); + } +} + +/* vi: set ts=4 sw=4 expandtab: */ + diff --git a/ThirdParty/SDL2/src/audio/SDL_audiodev.c b/ThirdParty/SDL2/src/audio/SDL_audiodev.c new file mode 100644 index 0000000..d0b94a0 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audiodev.c @@ -0,0 +1,124 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* Get the name of the audio device we use for output */ + +#if SDL_AUDIO_DRIVER_NETBSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO + +#include +#include +#include +#include /* For close() */ + +#include "SDL_stdinc.h" +#include "SDL_audiodev_c.h" + +#ifndef _PATH_DEV_DSP +#if defined(__NETBSD__) || defined(__OPENBSD__) +#define _PATH_DEV_DSP "/dev/audio" +#else +#define _PATH_DEV_DSP "/dev/dsp" +#endif +#endif +#ifndef _PATH_DEV_DSP24 +#define _PATH_DEV_DSP24 "/dev/sound/dsp" +#endif +#ifndef _PATH_DEV_AUDIO +#define _PATH_DEV_AUDIO "/dev/audio" +#endif + +static void +test_device(const int iscapture, const char *fname, int flags, int (*test) (int fd)) +{ + struct stat sb; + if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) { + const int audio_fd = open(fname, flags, 0); + if (audio_fd >= 0) { + const int okay = test(audio_fd); + close(audio_fd); + if (okay) { + static size_t dummyhandle = 0; + dummyhandle++; + SDL_assert(dummyhandle != 0); + SDL_AddAudioDevice(iscapture, fname, (void *) dummyhandle); + } + } + } +} + +static int +test_stub(int fd) +{ + return 1; +} + +static void +SDL_EnumUnixAudioDevices_Internal(const int iscapture, const int classic, int (*test)(int)) +{ + const int flags = iscapture ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT; + const char *audiodev; + char audiopath[1024]; + + if (test == NULL) + test = test_stub; + + /* Figure out what our audio device is */ + if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) && + ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) { + if (classic) { + audiodev = _PATH_DEV_AUDIO; + } else { + struct stat sb; + + /* Added support for /dev/sound/\* in Linux 2.4 */ + if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode)) + && ((stat(_PATH_DEV_DSP24, &sb) == 0) + && S_ISCHR(sb.st_mode))) { + audiodev = _PATH_DEV_DSP24; + } else { + audiodev = _PATH_DEV_DSP; + } + } + } + test_device(iscapture, audiodev, flags, test); + + if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) { + int instance = 0; + while (instance <= 64) { + SDL_snprintf(audiopath, SDL_arraysize(audiopath), + "%s%d", audiodev, instance); + instance++; + test_device(iscapture, audiopath, flags, test); + } + } +} + +void +SDL_EnumUnixAudioDevices(const int classic, int (*test)(int)) +{ + SDL_EnumUnixAudioDevices_Internal(SDL_TRUE, classic, test); + SDL_EnumUnixAudioDevices_Internal(SDL_FALSE, classic, test); +} + +#endif /* Audio driver selection */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_audiodev_c.h b/ThirdParty/SDL2/src/audio/SDL_audiodev_c.h new file mode 100644 index 0000000..15928d1 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audiodev_c.h @@ -0,0 +1,38 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL.h" +#include "../SDL_internal.h" +#include "SDL_sysaudio.h" + +/* Open the audio device for playback, and don't block if busy */ +/* #define USE_BLOCKING_WRITES */ + +#ifdef USE_BLOCKING_WRITES +#define OPEN_FLAGS_OUTPUT O_WRONLY +#define OPEN_FLAGS_INPUT O_RDONLY +#else +#define OPEN_FLAGS_OUTPUT (O_WRONLY|O_NONBLOCK) +#define OPEN_FLAGS_INPUT (O_RDONLY|O_NONBLOCK) +#endif + +extern void SDL_EnumUnixAudioDevices(const int classic, int (*test)(int)); + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_audiotypecvt.c b/ThirdParty/SDL2/src/audio/SDL_audiotypecvt.c new file mode 100644 index 0000000..2fbd916 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_audiotypecvt.c @@ -0,0 +1,829 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../SDL_internal.h" +#include "SDL_audio.h" +#include "SDL_audio_c.h" +#include "SDL_cpuinfo.h" +#include "SDL_assert.h" + +/* !!! FIXME: write NEON code. */ +#define HAVE_NEON_INTRINSICS 0 + +#ifdef __SSE2__ +#define HAVE_SSE2_INTRINSICS 1 +#endif + +#if defined(__x86_64__) && HAVE_SSE2_INTRINSICS +#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* x86_64 guarantees SSE2. */ +#elif __MACOSX__ && HAVE_SSE2_INTRINSICS +#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* Mac OS X/Intel guarantees SSE2. */ +#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 8) && HAVE_NEON_INTRINSICS +#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* ARMv8+ promise NEON. */ +#elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7) && HAVE_NEON_INTRINSICS +#define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* All Apple ARMv7 chips promise NEON support. */ +#endif + +/* Set to zero if platform is guaranteed to use a SIMD codepath here. */ +#ifndef NEED_SCALAR_CONVERTER_FALLBACKS +#define NEED_SCALAR_CONVERTER_FALLBACKS 1 +#endif + +/* Function pointers set to a CPU-specific implementation. */ +SDL_AudioFilter SDL_Convert_S8_to_F32 = NULL; +SDL_AudioFilter SDL_Convert_U8_to_F32 = NULL; +SDL_AudioFilter SDL_Convert_S16_to_F32 = NULL; +SDL_AudioFilter SDL_Convert_U16_to_F32 = NULL; +SDL_AudioFilter SDL_Convert_S32_to_F32 = NULL; +SDL_AudioFilter SDL_Convert_F32_to_S8 = NULL; +SDL_AudioFilter SDL_Convert_F32_to_U8 = NULL; +SDL_AudioFilter SDL_Convert_F32_to_S16 = NULL; +SDL_AudioFilter SDL_Convert_F32_to_U16 = NULL; +SDL_AudioFilter SDL_Convert_F32_to_S32 = NULL; + + +#define DIVBY128 0.0078125f +#define DIVBY32768 0.000030517578125f +#define DIVBY2147483648 0.00000000046566128730773926 + + +#if NEED_SCALAR_CONVERTER_FALLBACKS +static void SDLCALL +SDL_Convert_S8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32"); + + for (i = cvt->len_cvt; i; --i, --src, --dst) { + *dst = ((float) *src) * DIVBY128; + } + + cvt->len_cvt *= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_U8_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32"); + + for (i = cvt->len_cvt; i; --i, --src, --dst) { + *dst = (((float) *src) * DIVBY128) - 1.0f; + } + + cvt->len_cvt *= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_S16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32"); + + for (i = cvt->len_cvt / sizeof (Sint16); i; --i, --src, --dst) { + *dst = ((float) *src) * DIVBY32768; + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_U16_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32"); + + for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) { + *dst = (((float) *src) * DIVBY32768) - 1.0f; + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_S32_to_F32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint32 *src = (const Sint32 *) cvt->buf; + float *dst = (float *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32"); + + for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) { + *dst = (float) (((double) *src) * DIVBY2147483648); + } + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint8 *dst = (Sint8 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8"); + + for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { + const float sample = *src; + if (sample > 1.0f) { + *dst = 127; + } else if (sample < -1.0f) { + *dst = -127; + } else { + *dst = (Sint8)(sample * 127.0f); + } + } + + cvt->len_cvt /= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S8); + } +} + +static void SDLCALL +SDL_Convert_F32_to_U8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Uint8 *dst = (Uint8 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8"); + + for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { + const float sample = *src; + if (sample > 1.0f) { + *dst = 255; + } else if (sample < -1.0f) { + *dst = 0; + } else { + *dst = (Uint8)((sample + 1.0f) * 127.0f); + } + } + + cvt->len_cvt /= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_U8); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint16 *dst = (Sint16 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16"); + + for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { + const float sample = *src; + if (sample > 1.0f) { + *dst = 32767; + } else if (sample < -1.0f) { + *dst = -32767; + } else { + *dst = (Sint16)(sample * 32767.0f); + } + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_U16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Uint16 *dst = (Uint16 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16"); + + for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { + const float sample = *src; + if (sample > 1.0f) { + *dst = 65534; + } else if (sample < -1.0f) { + *dst = 0; + } else { + *dst = (Uint16)((sample + 1.0f) * 32767.0f); + } + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint32 *dst = (Sint32 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32"); + + for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { + const float sample = *src; + if (sample > 1.0f) { + *dst = 2147483647; + } else if (sample < -1.0f) { + *dst = -2147483647; + } else { + *dst = (Sint32)((double)sample * 2147483647.0); + } + } + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS); + } +} +#endif + + +#if HAVE_SSE2_INTRINSICS +static void SDLCALL +SDL_Convert_S8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32 (using SSE2)"); + + /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */ + for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) { + *dst = ((float) *src) * DIVBY128; + } + + src -= 15; dst -= 15; /* adjust to read SSE blocks from the start. */ + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128i *mmsrc = (const __m128i *) src; + const __m128i zero = _mm_setzero_si128(); + const __m128 divby128 = _mm_set1_ps(DIVBY128); + while (i >= 16) { /* 16 * 8-bit */ + const __m128i bytes = _mm_load_si128(mmsrc); /* get 16 sint8 into an XMM register. */ + /* treat as int16, shift left to clear every other sint16, then back right with sign-extend. Now sint16. */ + const __m128i shorts1 = _mm_srai_epi16(_mm_slli_epi16(bytes, 8), 8); + /* right-shift-sign-extend gets us sint16 with the other set of values. */ + const __m128i shorts2 = _mm_srai_epi16(bytes, 8); + /* unpack against zero to make these int32, shift to make them sign-extend, convert to float, multiply. Whew! */ + const __m128 floats1 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts1, zero), 16), 16)), divby128); + const __m128 floats2 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpacklo_epi16(shorts2, zero), 16), 16)), divby128); + const __m128 floats3 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts1, zero), 16), 16)), divby128); + const __m128 floats4 = _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_slli_epi32(_mm_unpackhi_epi16(shorts2, zero), 16), 16)), divby128); + /* Interleave back into correct order, store. */ + _mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2)); + _mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2)); + _mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4)); + _mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4)); + i -= 16; mmsrc--; dst -= 16; + } + + src = (const Sint8 *) mmsrc; + } + + src += 15; dst += 15; /* adjust for any scalar finishing. */ + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = ((float) *src) * DIVBY128; + i--; src--; dst--; + } + + cvt->len_cvt *= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_U8_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32 (using SSE2)"); + + /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */ + for (i = cvt->len_cvt; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) { + *dst = (((float) *src) * DIVBY128) - 1.0f; + } + + src -= 15; dst -= 15; /* adjust to read SSE blocks from the start. */ + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128i *mmsrc = (const __m128i *) src; + const __m128i zero = _mm_setzero_si128(); + const __m128 divby128 = _mm_set1_ps(DIVBY128); + const __m128 minus1 = _mm_set1_ps(-1.0f); + while (i >= 16) { /* 16 * 8-bit */ + const __m128i bytes = _mm_load_si128(mmsrc); /* get 16 uint8 into an XMM register. */ + /* treat as int16, shift left to clear every other sint16, then back right with zero-extend. Now uint16. */ + const __m128i shorts1 = _mm_srli_epi16(_mm_slli_epi16(bytes, 8), 8); + /* right-shift-zero-extend gets us uint16 with the other set of values. */ + const __m128i shorts2 = _mm_srli_epi16(bytes, 8); + /* unpack against zero to make these int32, convert to float, multiply, add. Whew! */ + /* Note that AVX2 can do floating point multiply+add in one instruction, fwiw. SSE2 cannot. */ + const __m128 floats1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts1, zero)), divby128), minus1); + const __m128 floats2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts2, zero)), divby128), minus1); + const __m128 floats3 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts1, zero)), divby128), minus1); + const __m128 floats4 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts2, zero)), divby128), minus1); + /* Interleave back into correct order, store. */ + _mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2)); + _mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2)); + _mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4)); + _mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4)); + i -= 16; mmsrc--; dst -= 16; + } + + src = (const Uint8 *) mmsrc; + } + + src += 15; dst += 15; /* adjust for any scalar finishing. */ + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (((float) *src) * DIVBY128) - 1.0f; + i--; src--; dst--; + } + + cvt->len_cvt *= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_S16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32 (using SSE2)"); + + /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */ + for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) { + *dst = ((float) *src) * DIVBY32768; + } + + src -= 7; dst -= 7; /* adjust to read SSE blocks from the start. */ + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 divby32768 = _mm_set1_ps(DIVBY32768); + while (i >= 8) { /* 8 * 16-bit */ + const __m128i ints = _mm_load_si128((__m128i const *) src); /* get 8 sint16 into an XMM register. */ + /* treat as int32, shift left to clear every other sint16, then back right with sign-extend. Now sint32. */ + const __m128i a = _mm_srai_epi32(_mm_slli_epi32(ints, 16), 16); + /* right-shift-sign-extend gets us sint32 with the other set of values. */ + const __m128i b = _mm_srai_epi32(ints, 16); + /* Interleave these back into the right order, convert to float, multiply, store. */ + _mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768)); + _mm_store_ps(dst+4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768)); + i -= 8; src -= 8; dst -= 8; + } + } + + src += 7; dst += 7; /* adjust for any scalar finishing. */ + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = ((float) *src) * DIVBY32768; + i--; src--; dst--; + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_U16_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1; + float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1; + int i; + + LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32 (using SSE2)"); + + /* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */ + for (i = cvt->len_cvt / sizeof (Sint16); i && (((size_t) (dst-7)) & 15); --i, --src, --dst) { + *dst = (((float) *src) * DIVBY32768) - 1.0f; + } + + src -= 7; dst -= 7; /* adjust to read SSE blocks from the start. */ + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 divby32768 = _mm_set1_ps(DIVBY32768); + const __m128 minus1 = _mm_set1_ps(1.0f); + while (i >= 8) { /* 8 * 16-bit */ + const __m128i ints = _mm_load_si128((__m128i const *) src); /* get 8 sint16 into an XMM register. */ + /* treat as int32, shift left to clear every other sint16, then back right with zero-extend. Now sint32. */ + const __m128i a = _mm_srli_epi32(_mm_slli_epi32(ints, 16), 16); + /* right-shift-sign-extend gets us sint32 with the other set of values. */ + const __m128i b = _mm_srli_epi32(ints, 16); + /* Interleave these back into the right order, convert to float, multiply, store. */ + _mm_store_ps(dst, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768), minus1)); + _mm_store_ps(dst+4, _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768), minus1)); + i -= 8; src -= 8; dst -= 8; + } + } + + src += 7; dst += 7; /* adjust for any scalar finishing. */ + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (((float) *src) * DIVBY32768) - 1.0f; + i--; src--; dst--; + } + + cvt->len_cvt *= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +#if defined(__GNUC__) && (__GNUC__ < 4) +/* these were added as of gcc-4.0: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19418 */ +static inline __m128 _mm_castsi128_ps(__m128i __A) { + return (__m128) __A; +} +static inline __m128i _mm_castps_si128(__m128 __A) { + return (__m128i) __A; +} +#endif + +static void SDLCALL +SDL_Convert_S32_to_F32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const Sint32 *src = (const Sint32 *) cvt->buf; + float *dst = (float *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (Sint32); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (float) (((double) *src) * DIVBY2147483648); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + SDL_assert(!i || ((((size_t) src) & 15) == 0)); + + { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128d divby2147483648 = _mm_set1_pd(DIVBY2147483648); + const __m128i *mmsrc = (const __m128i *) src; + while (i >= 4) { /* 4 * sint32 */ + const __m128i ints = _mm_load_si128(mmsrc); + /* bitshift the whole register over, so _mm_cvtepi32_pd can read the top ints in the bottom of the vector. */ + const __m128d doubles1 = _mm_mul_pd(_mm_cvtepi32_pd(_mm_srli_si128(ints, 8)), divby2147483648); + const __m128d doubles2 = _mm_mul_pd(_mm_cvtepi32_pd(ints), divby2147483648); + /* convert to float32, bitshift/or to get these into a vector to store. */ + _mm_store_ps(dst, _mm_castsi128_ps(_mm_or_si128(_mm_slli_si128(_mm_castps_si128(_mm_cvtpd_ps(doubles1)), 8), _mm_castps_si128(_mm_cvtpd_ps(doubles2))))); + i -= 4; mmsrc++; dst += 4; + } + src = (const Sint32 *) mmsrc; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (float) (((double) *src) * DIVBY2147483648); + i--; src++; dst++; + } + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint8 *dst = (Sint8 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (Sint8) (*src * 127.0f); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 mulby127 = _mm_set1_ps(127.0f); + __m128i *mmdst = (__m128i *) dst; + while (i >= 16) { /* 16 * float32 */ + const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+8), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+12), mulby127)); /* load 4 floats, convert to sint32 */ + _mm_store_si128(mmdst, _mm_packs_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4))); /* pack down, store out. */ + i -= 16; src += 16; mmdst++; + } + dst = (Sint8 *) mmdst; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (Sint8) (*src * 127.0f); + i--; src++; dst++; + } + + cvt->len_cvt /= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S8); + } +} + +static void SDLCALL +SDL_Convert_F32_to_U8_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Uint8 *dst = (Uint8 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (Uint8) ((*src + 1.0f) * 127.0f); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 add1 = _mm_set1_ps(1.0f); + const __m128 mulby127 = _mm_set1_ps(127.0f); + __m128i *mmdst = (__m128i *) dst; + while (i >= 16) { /* 16 * float32 */ + const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src), add1), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+4), add1), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints3 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+8), add1), mulby127)); /* load 4 floats, convert to sint32 */ + const __m128i ints4 = _mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(_mm_load_ps(src+12), add1), mulby127)); /* load 4 floats, convert to sint32 */ + _mm_store_si128(mmdst, _mm_packus_epi16(_mm_packs_epi32(ints1, ints2), _mm_packs_epi32(ints3, ints4))); /* pack down, store out. */ + i -= 16; src += 16; mmdst++; + } + dst = (Uint8 *) mmdst; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (Uint8) ((*src + 1.0f) * 127.0f); + i--; src++; dst++; + } + + cvt->len_cvt /= 4; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_U8); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint16 *dst = (Sint16 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (Sint16) (*src * 32767.0f); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128 mulby32767 = _mm_set1_ps(32767.0f); + __m128i *mmdst = (__m128i *) dst; + while (i >= 8) { /* 8 * float32 */ + const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby32767)); /* load 4 floats, convert to sint32 */ + const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby32767)); /* load 4 floats, convert to sint32 */ + _mm_store_si128(mmdst, _mm_packs_epi32(ints1, ints2)); /* pack to sint16, store out. */ + i -= 8; src += 8; mmdst++; + } + dst = (Sint16 *) mmdst; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (Sint16) (*src * 32767.0f); + i--; src++; dst++; + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_U16_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Uint16 *dst = (Uint16 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (Uint16) ((*src + 1.0f) * 32767.0f); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + + /* Make sure src is aligned too. */ + if ((((size_t) src) & 15) == 0) { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + /* This calculates differently than the scalar path because SSE2 can't + pack int32 data down to unsigned int16. _mm_packs_epi32 does signed + saturation, so that would corrupt our data. _mm_packus_epi32 exists, + but not before SSE 4.1. So we convert from float to sint16, packing + that down with legit signed saturation, and then xor the top bit + against 1. This results in the correct unsigned 16-bit value, even + though it looks like dark magic. */ + const __m128 mulby32767 = _mm_set1_ps(32767.0f); + const __m128i topbit = _mm_set1_epi16(-32768); + __m128i *mmdst = (__m128i *) dst; + while (i >= 8) { /* 8 * float32 */ + const __m128i ints1 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src), mulby32767)); /* load 4 floats, convert to sint32 */ + const __m128i ints2 = _mm_cvtps_epi32(_mm_mul_ps(_mm_load_ps(src+4), mulby32767)); /* load 4 floats, convert to sint32 */ + _mm_store_si128(mmdst, _mm_xor_si128(_mm_packs_epi32(ints1, ints2), topbit)); /* pack to sint16, xor top bit, store out. */ + i -= 8; src += 8; mmdst++; + } + dst = (Uint16 *) mmdst; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (Uint16) ((*src + 1.0f) * 32767.0f); + i--; src++; dst++; + } + + cvt->len_cvt /= 2; + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS); + } +} + +static void SDLCALL +SDL_Convert_F32_to_S32_SSE2(SDL_AudioCVT *cvt, SDL_AudioFormat format) +{ + const float *src = (const float *) cvt->buf; + Sint32 *dst = (Sint32 *) cvt->buf; + int i; + + LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32 (using SSE2)"); + + /* Get dst aligned to 16 bytes */ + for (i = cvt->len_cvt / sizeof (float); i && (((size_t) dst) & 15); --i, ++src, ++dst) { + *dst = (Sint32) (((double) *src) * 2147483647.0); + } + + SDL_assert(!i || ((((size_t) dst) & 15) == 0)); + SDL_assert(!i || ((((size_t) src) & 15) == 0)); + + { + /* Aligned! Do SSE blocks as long as we have 16 bytes available. */ + const __m128d mulby2147483647 = _mm_set1_pd(2147483647.0); + __m128i *mmdst = (__m128i *) dst; + while (i >= 4) { /* 4 * float32 */ + const __m128 floats = _mm_load_ps(src); + /* bitshift the whole register over, so _mm_cvtps_pd can read the top floats in the bottom of the vector. */ + const __m128d doubles1 = _mm_mul_pd(_mm_cvtps_pd(_mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(floats), 8))), mulby2147483647); + const __m128d doubles2 = _mm_mul_pd(_mm_cvtps_pd(floats), mulby2147483647); + _mm_store_si128(mmdst, _mm_or_si128(_mm_slli_si128(_mm_cvtpd_epi32(doubles1), 8), _mm_cvtpd_epi32(doubles2))); + i -= 4; src += 4; mmdst++; + } + dst = (Sint32 *) mmdst; + } + + /* Finish off any leftovers with scalar operations. */ + while (i) { + *dst = (Sint32) (((double) *src) * 2147483647.0); + i--; src++; dst++; + } + + if (cvt->filters[++cvt->filter_index]) { + cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS); + } +} +#endif + + +void SDL_ChooseAudioConverters(void) +{ + static SDL_bool converters_chosen = SDL_FALSE; + + if (converters_chosen) { + return; + } + +#define SET_CONVERTER_FUNCS(fntype) \ + SDL_Convert_S8_to_F32 = SDL_Convert_S8_to_F32_##fntype; \ + SDL_Convert_U8_to_F32 = SDL_Convert_U8_to_F32_##fntype; \ + SDL_Convert_S16_to_F32 = SDL_Convert_S16_to_F32_##fntype; \ + SDL_Convert_U16_to_F32 = SDL_Convert_U16_to_F32_##fntype; \ + SDL_Convert_S32_to_F32 = SDL_Convert_S32_to_F32_##fntype; \ + SDL_Convert_F32_to_S8 = SDL_Convert_F32_to_S8_##fntype; \ + SDL_Convert_F32_to_U8 = SDL_Convert_F32_to_U8_##fntype; \ + SDL_Convert_F32_to_S16 = SDL_Convert_F32_to_S16_##fntype; \ + SDL_Convert_F32_to_U16 = SDL_Convert_F32_to_U16_##fntype; \ + SDL_Convert_F32_to_S32 = SDL_Convert_F32_to_S32_##fntype; \ + converters_chosen = SDL_TRUE + +#if HAVE_SSE2_INTRINSICS + if (SDL_HasSSE2()) { + SET_CONVERTER_FUNCS(SSE2); + return; + } +#endif + +#if NEED_SCALAR_CONVERTER_FALLBACKS + SET_CONVERTER_FUNCS(Scalar); +#endif + +#undef SET_CONVERTER_FUNCS + + SDL_assert(converters_chosen == SDL_TRUE); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_mixer.c b/ThirdParty/SDL2/src/audio/SDL_mixer.c new file mode 100644 index 0000000..d416a94 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_mixer.c @@ -0,0 +1,369 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* This provides the default mixing callback for the SDL audio routines */ + +#include "SDL_cpuinfo.h" +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "SDL_sysaudio.h" + +/* This table is used to add two sound values together and pin + * the value to avoid overflow. (used with permission from ARDI) + * Changed to use 0xFE instead of 0xFF for better sound quality. + */ +static const Uint8 mix8[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, + 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, + 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, + 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, + 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, + 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, + 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, + 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, + 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, + 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, + 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, + 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, + 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, + 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, + 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, + 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE +}; + +/* The volume ranges from 0 - 128 */ +#define ADJUST_VOLUME(s, v) (s = (s*v)/SDL_MIX_MAXVOLUME) +#define ADJUST_VOLUME_U8(s, v) (s = (((s-128)*v)/SDL_MIX_MAXVOLUME)+128) + + +void +SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format, + Uint32 len, int volume) +{ + if (volume == 0) { + return; + } + + switch (format) { + + case AUDIO_U8: + { +#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES) + SDL_MixAudio_m68k_U8((char *) dst, (char *) src, + (unsigned long) len, (long) volume, + (char *) mix8); +#else + Uint8 src_sample; + + while (len--) { + src_sample = *src; + ADJUST_VOLUME_U8(src_sample, volume); + *dst = mix8[*dst + src_sample]; + ++dst; + ++src; + } +#endif + } + break; + + case AUDIO_S8: + { + Sint8 *dst8, *src8; + Sint8 src_sample; + int dst_sample; + const int max_audioval = ((1 << (8 - 1)) - 1); + const int min_audioval = -(1 << (8 - 1)); + + src8 = (Sint8 *) src; + dst8 = (Sint8 *) dst; + while (len--) { + src_sample = *src8; + ADJUST_VOLUME(src_sample, volume); + dst_sample = *dst8 + src_sample; + if (dst_sample > max_audioval) { + *dst8 = max_audioval; + } else if (dst_sample < min_audioval) { + *dst8 = min_audioval; + } else { + *dst8 = dst_sample; + } + ++dst8; + ++src8; + } + } + break; + + case AUDIO_S16LSB: + { + Sint16 src1, src2; + int dst_sample; + const int max_audioval = ((1 << (16 - 1)) - 1); + const int min_audioval = -(1 << (16 - 1)); + + len /= 2; + while (len--) { + src1 = ((src[1]) << 8 | src[0]); + ADJUST_VOLUME(src1, volume); + src2 = ((dst[1]) << 8 | dst[0]); + src += 2; + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + dst[0] = dst_sample & 0xFF; + dst_sample >>= 8; + dst[1] = dst_sample & 0xFF; + dst += 2; + } + } + break; + + case AUDIO_S16MSB: + { +#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES) + SDL_MixAudio_m68k_S16MSB((short *) dst, (short *) src, + (unsigned long) len, (long) volume); +#else + Sint16 src1, src2; + int dst_sample; + const int max_audioval = ((1 << (16 - 1)) - 1); + const int min_audioval = -(1 << (16 - 1)); + + len /= 2; + while (len--) { + src1 = ((src[0]) << 8 | src[1]); + ADJUST_VOLUME(src1, volume); + src2 = ((dst[0]) << 8 | dst[1]); + src += 2; + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + dst[1] = dst_sample & 0xFF; + dst_sample >>= 8; + dst[0] = dst_sample & 0xFF; + dst += 2; + } +#endif + } + break; + + case AUDIO_U16LSB: + { + Uint16 src1, src2; + int dst_sample; + const int max_audioval = 0xFFFF; + + len /= 2; + while (len--) { + src1 = ((src[1]) << 8 | src[0]); + ADJUST_VOLUME(src1, volume); + src2 = ((dst[1]) << 8 | dst[0]); + src += 2; + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } + dst[0] = dst_sample & 0xFF; + dst_sample >>= 8; + dst[1] = dst_sample & 0xFF; + dst += 2; + } + } + break; + + case AUDIO_U16MSB: + { + Uint16 src1, src2; + int dst_sample; + const int max_audioval = 0xFFFF; + + len /= 2; + while (len--) { + src1 = ((src[0]) << 8 | src[1]); + ADJUST_VOLUME(src1, volume); + src2 = ((dst[0]) << 8 | dst[1]); + src += 2; + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } + dst[1] = dst_sample & 0xFF; + dst_sample >>= 8; + dst[0] = dst_sample & 0xFF; + dst += 2; + } + } + break; + + case AUDIO_S32LSB: + { + const Uint32 *src32 = (Uint32 *) src; + Uint32 *dst32 = (Uint32 *) dst; + Sint64 src1, src2; + Sint64 dst_sample; + const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1); + const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1)); + + len /= 4; + while (len--) { + src1 = (Sint64) ((Sint32) SDL_SwapLE32(*src32)); + src32++; + ADJUST_VOLUME(src1, volume); + src2 = (Sint64) ((Sint32) SDL_SwapLE32(*dst32)); + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + *(dst32++) = SDL_SwapLE32((Uint32) ((Sint32) dst_sample)); + } + } + break; + + case AUDIO_S32MSB: + { + const Uint32 *src32 = (Uint32 *) src; + Uint32 *dst32 = (Uint32 *) dst; + Sint64 src1, src2; + Sint64 dst_sample; + const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1); + const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1)); + + len /= 4; + while (len--) { + src1 = (Sint64) ((Sint32) SDL_SwapBE32(*src32)); + src32++; + ADJUST_VOLUME(src1, volume); + src2 = (Sint64) ((Sint32) SDL_SwapBE32(*dst32)); + dst_sample = src1 + src2; + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + *(dst32++) = SDL_SwapBE32((Uint32) ((Sint32) dst_sample)); + } + } + break; + + case AUDIO_F32LSB: + { + const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME); + const float fvolume = (float) volume; + const float *src32 = (float *) src; + float *dst32 = (float *) dst; + float src1, src2; + double dst_sample; + /* !!! FIXME: are these right? */ + const double max_audioval = 3.402823466e+38F; + const double min_audioval = -3.402823466e+38F; + + len /= 4; + while (len--) { + src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume); + src2 = SDL_SwapFloatLE(*dst32); + src32++; + + dst_sample = ((double) src1) + ((double) src2); + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + *(dst32++) = SDL_SwapFloatLE((float) dst_sample); + } + } + break; + + case AUDIO_F32MSB: + { + const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME); + const float fvolume = (float) volume; + const float *src32 = (float *) src; + float *dst32 = (float *) dst; + float src1, src2; + double dst_sample; + /* !!! FIXME: are these right? */ + const double max_audioval = 3.402823466e+38F; + const double min_audioval = -3.402823466e+38F; + + len /= 4; + while (len--) { + src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume); + src2 = SDL_SwapFloatBE(*dst32); + src32++; + + dst_sample = ((double) src1) + ((double) src2); + if (dst_sample > max_audioval) { + dst_sample = max_audioval; + } else if (dst_sample < min_audioval) { + dst_sample = min_audioval; + } + *(dst32++) = SDL_SwapFloatBE((float) dst_sample); + } + } + break; + + default: /* If this happens... FIXME! */ + SDL_SetError("SDL_MixAudioFormat(): unknown audio format"); + return; + } +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_sysaudio.h b/ThirdParty/SDL2/src/audio/SDL_sysaudio.h new file mode 100644 index 0000000..f0e1f3d --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_sysaudio.h @@ -0,0 +1,211 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +#ifndef SDL_sysaudio_h_ +#define SDL_sysaudio_h_ + +#include "SDL_mutex.h" +#include "SDL_thread.h" +#include "../SDL_dataqueue.h" +#include "./SDL_audio_c.h" + +/* !!! FIXME: These are wordy and unlocalized... */ +#define DEFAULT_OUTPUT_DEVNAME "System audio output device" +#define DEFAULT_INPUT_DEVNAME "System audio capture device" + +/* The SDL audio driver */ +typedef struct SDL_AudioDevice SDL_AudioDevice; +#define _THIS SDL_AudioDevice *_this + +/* Audio targets should call this as devices are added to the system (such as + a USB headset being plugged in), and should also be called for + for every device found during DetectDevices(). */ +extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle); + +/* Audio targets should call this as devices are removed, so SDL can update + its list of available devices. */ +extern void SDL_RemoveAudioDevice(const int iscapture, void *handle); + +/* Audio targets should call this if an opened audio device is lost while + being used. This can happen due to i/o errors, or a device being unplugged, + etc. If the device is totally gone, please also call SDL_RemoveAudioDevice() + as appropriate so SDL's list of devices is accurate. */ +extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device); + +/* This is the size of a packet when using SDL_QueueAudio(). We allocate + these as necessary and pool them, under the assumption that we'll + eventually end up with a handful that keep recycling, meeting whatever + the app needs. We keep packing data tightly as more arrives to avoid + wasting space, and if we get a giant block of data, we'll split them + into multiple packets behind the scenes. My expectation is that most + apps will have 2-3 of these in the pool. 8k should cover most needs, but + if this is crippling for some embedded system, we can #ifdef this. + The system preallocates enough packets for 2 callbacks' worth of data. */ +#define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024) + +typedef struct SDL_AudioDriverImpl +{ + void (*DetectDevices) (void); + int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture); + void (*ThreadInit) (_THIS); /* Called by audio thread at start */ + void (*ThreadDeinit) (_THIS); /* Called by audio thread at end */ + void (*BeginLoopIteration)(_THIS); /* Called by audio thread at top of loop */ + void (*WaitDevice) (_THIS); + void (*PlayDevice) (_THIS); + int (*GetPendingBytes) (_THIS); + Uint8 *(*GetDeviceBuf) (_THIS); + int (*CaptureFromDevice) (_THIS, void *buffer, int buflen); + void (*FlushCapture) (_THIS); + void (*PrepareToClose) (_THIS); /**< Called between run and draining wait for playback devices */ + void (*CloseDevice) (_THIS); + void (*LockDevice) (_THIS); + void (*UnlockDevice) (_THIS); + void (*FreeDeviceHandle) (void *handle); /**< SDL is done with handle from SDL_AddAudioDevice() */ + void (*Deinitialize) (void); + + /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */ + + /* Some flags to push duplicate code into the core and reduce #ifdefs. */ + /* !!! FIXME: these should be SDL_bool */ + int ProvidesOwnCallbackThread; + int SkipMixerLock; + int HasCaptureSupport; + int OnlyHasDefaultOutputDevice; + int OnlyHasDefaultCaptureDevice; + int AllowsArbitraryDeviceNames; +} SDL_AudioDriverImpl; + + +typedef struct SDL_AudioDeviceItem +{ + void *handle; + struct SDL_AudioDeviceItem *next; + char name[SDL_VARIABLE_LENGTH_ARRAY]; +} SDL_AudioDeviceItem; + + +typedef struct SDL_AudioDriver +{ + /* * * */ + /* The name of this audio driver */ + const char *name; + + /* * * */ + /* The description of this audio driver */ + const char *desc; + + SDL_AudioDriverImpl impl; + + /* A mutex for device detection */ + SDL_mutex *detectionLock; + SDL_bool captureDevicesRemoved; + SDL_bool outputDevicesRemoved; + int outputDeviceCount; + int inputDeviceCount; + SDL_AudioDeviceItem *outputDevices; + SDL_AudioDeviceItem *inputDevices; +} SDL_AudioDriver; + + +/* Define the SDL audio driver structure */ +struct SDL_AudioDevice +{ + /* * * */ + /* Data common to all devices */ + SDL_AudioDeviceID id; + + /* The device's current audio specification */ + SDL_AudioSpec spec; + + /* The callback's expected audio specification (converted vs device's spec). */ + SDL_AudioSpec callbackspec; + + /* Stream that converts and resamples. NULL if not needed. */ + SDL_AudioStream *stream; + + /* Current state flags */ + SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */ + SDL_atomic_t enabled; /* true if device is functioning and connected. */ + SDL_atomic_t paused; + SDL_bool iscapture; + + /* Scratch buffer used in the bridge between SDL and the user callback. */ + Uint8 *work_buffer; + + /* Size, in bytes, of work_buffer. */ + Uint32 work_buffer_len; + + /* A mutex for locking the mixing buffers */ + SDL_mutex *mixer_lock; + + /* A thread to feed the audio device */ + SDL_Thread *thread; + SDL_threadID threadid; + + /* Queued buffers (if app not using callback). */ + SDL_DataQueue *buffer_queue; + + /* * * */ + /* Data private to this driver */ + struct SDL_PrivateAudioData *hidden; + + void *handle; +}; +#undef _THIS + +typedef struct AudioBootStrap +{ + const char *name; + const char *desc; + int (*init) (SDL_AudioDriverImpl * impl); + int demand_only; /* 1==request explicitly, or it won't be available. */ +} AudioBootStrap; + +/* Not all of these are available in a given build. Use #ifdefs, etc. */ +extern AudioBootStrap PULSEAUDIO_bootstrap; +extern AudioBootStrap ALSA_bootstrap; +extern AudioBootStrap JACK_bootstrap; +extern AudioBootStrap SNDIO_bootstrap; +extern AudioBootStrap NETBSDAUDIO_bootstrap; +extern AudioBootStrap DSP_bootstrap; +extern AudioBootStrap QSAAUDIO_bootstrap; +extern AudioBootStrap SUNAUDIO_bootstrap; +extern AudioBootStrap ARTS_bootstrap; +extern AudioBootStrap ESD_bootstrap; +extern AudioBootStrap NACLAUDIO_bootstrap; +extern AudioBootStrap NAS_bootstrap; +extern AudioBootStrap WASAPI_bootstrap; +extern AudioBootStrap DSOUND_bootstrap; +extern AudioBootStrap WINMM_bootstrap; +extern AudioBootStrap PAUDIO_bootstrap; +extern AudioBootStrap HAIKUAUDIO_bootstrap; +extern AudioBootStrap COREAUDIO_bootstrap; +extern AudioBootStrap DISKAUDIO_bootstrap; +extern AudioBootStrap DUMMYAUDIO_bootstrap; +extern AudioBootStrap FUSIONSOUND_bootstrap; +extern AudioBootStrap ANDROIDAUDIO_bootstrap; +extern AudioBootStrap PSPAUDIO_bootstrap; +extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap; + +#endif /* SDL_sysaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_wave.c b/ThirdParty/SDL2/src/audio/SDL_wave.c new file mode 100644 index 0000000..2c76a8c --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_wave.c @@ -0,0 +1,694 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* Microsoft WAVE file loading routines */ + +#include "SDL_audio.h" +#include "SDL_wave.h" + + +static int ReadChunk(SDL_RWops * src, Chunk * chunk); + +struct MS_ADPCM_decodestate +{ + Uint8 hPredictor; + Uint16 iDelta; + Sint16 iSamp1; + Sint16 iSamp2; +}; +static struct MS_ADPCM_decoder +{ + WaveFMT wavefmt; + Uint16 wSamplesPerBlock; + Uint16 wNumCoef; + Sint16 aCoeff[7][2]; + /* * * */ + struct MS_ADPCM_decodestate state[2]; +} MS_ADPCM_state; + +static int +InitMS_ADPCM(WaveFMT * format) +{ + Uint8 *rogue_feel; + int i; + + /* Set the rogue pointer to the MS_ADPCM specific data */ + MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); + MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); + MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); + MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); + MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); + MS_ADPCM_state.wavefmt.bitspersample = + SDL_SwapLE16(format->bitspersample); + rogue_feel = (Uint8 *) format + sizeof(*format); + if (sizeof(*format) == 16) { + /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */ + rogue_feel += sizeof(Uint16); + } + MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]); + rogue_feel += sizeof(Uint16); + MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]); + rogue_feel += sizeof(Uint16); + if (MS_ADPCM_state.wNumCoef != 7) { + SDL_SetError("Unknown set of MS_ADPCM coefficients"); + return (-1); + } + for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) { + MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]); + rogue_feel += sizeof(Uint16); + MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]); + rogue_feel += sizeof(Uint16); + } + return (0); +} + +static Sint32 +MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state, + Uint8 nybble, Sint16 * coeff) +{ + const Sint32 max_audioval = ((1 << (16 - 1)) - 1); + const Sint32 min_audioval = -(1 << (16 - 1)); + const Sint32 adaptive[] = { + 230, 230, 230, 230, 307, 409, 512, 614, + 768, 614, 512, 409, 307, 230, 230, 230 + }; + Sint32 new_sample, delta; + + new_sample = ((state->iSamp1 * coeff[0]) + + (state->iSamp2 * coeff[1])) / 256; + if (nybble & 0x08) { + new_sample += state->iDelta * (nybble - 0x10); + } else { + new_sample += state->iDelta * nybble; + } + if (new_sample < min_audioval) { + new_sample = min_audioval; + } else if (new_sample > max_audioval) { + new_sample = max_audioval; + } + delta = ((Sint32) state->iDelta * adaptive[nybble]) / 256; + if (delta < 16) { + delta = 16; + } + state->iDelta = (Uint16) delta; + state->iSamp2 = state->iSamp1; + state->iSamp1 = (Sint16) new_sample; + return (new_sample); +} + +static int +MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len) +{ + struct MS_ADPCM_decodestate *state[2]; + Uint8 *freeable, *encoded, *decoded; + Sint32 encoded_len, samplesleft; + Sint8 nybble; + Uint8 stereo; + Sint16 *coeff[2]; + Sint32 new_sample; + + /* Allocate the proper sized output buffer */ + encoded_len = *audio_len; + encoded = *audio_buf; + freeable = *audio_buf; + *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) * + MS_ADPCM_state.wSamplesPerBlock * + MS_ADPCM_state.wavefmt.channels * sizeof(Sint16); + *audio_buf = (Uint8 *) SDL_malloc(*audio_len); + if (*audio_buf == NULL) { + return SDL_OutOfMemory(); + } + decoded = *audio_buf; + + /* Get ready... Go! */ + stereo = (MS_ADPCM_state.wavefmt.channels == 2); + state[0] = &MS_ADPCM_state.state[0]; + state[1] = &MS_ADPCM_state.state[stereo]; + while (encoded_len >= MS_ADPCM_state.wavefmt.blockalign) { + /* Grab the initial information for this block */ + state[0]->hPredictor = *encoded++; + if (stereo) { + state[1]->hPredictor = *encoded++; + } + state[0]->iDelta = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + if (stereo) { + state[1]->iDelta = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + } + state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + if (stereo) { + state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + } + state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + if (stereo) { + state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]); + encoded += sizeof(Sint16); + } + coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor]; + coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor]; + + /* Store the two initial samples we start with */ + decoded[0] = state[0]->iSamp2 & 0xFF; + decoded[1] = state[0]->iSamp2 >> 8; + decoded += 2; + if (stereo) { + decoded[0] = state[1]->iSamp2 & 0xFF; + decoded[1] = state[1]->iSamp2 >> 8; + decoded += 2; + } + decoded[0] = state[0]->iSamp1 & 0xFF; + decoded[1] = state[0]->iSamp1 >> 8; + decoded += 2; + if (stereo) { + decoded[0] = state[1]->iSamp1 & 0xFF; + decoded[1] = state[1]->iSamp1 >> 8; + decoded += 2; + } + + /* Decode and store the other samples in this block */ + samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) * + MS_ADPCM_state.wavefmt.channels; + while (samplesleft > 0) { + nybble = (*encoded) >> 4; + new_sample = MS_ADPCM_nibble(state[0], nybble, coeff[0]); + decoded[0] = new_sample & 0xFF; + new_sample >>= 8; + decoded[1] = new_sample & 0xFF; + decoded += 2; + + nybble = (*encoded) & 0x0F; + new_sample = MS_ADPCM_nibble(state[1], nybble, coeff[1]); + decoded[0] = new_sample & 0xFF; + new_sample >>= 8; + decoded[1] = new_sample & 0xFF; + decoded += 2; + + ++encoded; + samplesleft -= 2; + } + encoded_len -= MS_ADPCM_state.wavefmt.blockalign; + } + SDL_free(freeable); + return (0); +} + +struct IMA_ADPCM_decodestate +{ + Sint32 sample; + Sint8 index; +}; +static struct IMA_ADPCM_decoder +{ + WaveFMT wavefmt; + Uint16 wSamplesPerBlock; + /* * * */ + struct IMA_ADPCM_decodestate state[2]; +} IMA_ADPCM_state; + +static int +InitIMA_ADPCM(WaveFMT * format) +{ + Uint8 *rogue_feel; + + /* Set the rogue pointer to the IMA_ADPCM specific data */ + IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); + IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); + IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); + IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); + IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); + IMA_ADPCM_state.wavefmt.bitspersample = + SDL_SwapLE16(format->bitspersample); + rogue_feel = (Uint8 *) format + sizeof(*format); + if (sizeof(*format) == 16) { + /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */ + rogue_feel += sizeof(Uint16); + } + IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]); + return (0); +} + +static Sint32 +IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble) +{ + const Sint32 max_audioval = ((1 << (16 - 1)) - 1); + const Sint32 min_audioval = -(1 << (16 - 1)); + const int index_table[16] = { + -1, -1, -1, -1, + 2, 4, 6, 8, + -1, -1, -1, -1, + 2, 4, 6, 8 + }; + const Sint32 step_table[89] = { + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, + 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, + 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, + 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, + 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, + 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, + 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, + 22385, 24623, 27086, 29794, 32767 + }; + Sint32 delta, step; + + /* Compute difference and new sample value */ + if (state->index > 88) { + state->index = 88; + } else if (state->index < 0) { + state->index = 0; + } + /* explicit cast to avoid gcc warning about using 'char' as array index */ + step = step_table[(int)state->index]; + delta = step >> 3; + if (nybble & 0x04) + delta += step; + if (nybble & 0x02) + delta += (step >> 1); + if (nybble & 0x01) + delta += (step >> 2); + if (nybble & 0x08) + delta = -delta; + state->sample += delta; + + /* Update index value */ + state->index += index_table[nybble]; + + /* Clamp output sample */ + if (state->sample > max_audioval) { + state->sample = max_audioval; + } else if (state->sample < min_audioval) { + state->sample = min_audioval; + } + return (state->sample); +} + +/* Fill the decode buffer with a channel block of data (8 samples) */ +static void +Fill_IMA_ADPCM_block(Uint8 * decoded, Uint8 * encoded, + int channel, int numchannels, + struct IMA_ADPCM_decodestate *state) +{ + int i; + Sint8 nybble; + Sint32 new_sample; + + decoded += (channel * 2); + for (i = 0; i < 4; ++i) { + nybble = (*encoded) & 0x0F; + new_sample = IMA_ADPCM_nibble(state, nybble); + decoded[0] = new_sample & 0xFF; + new_sample >>= 8; + decoded[1] = new_sample & 0xFF; + decoded += 2 * numchannels; + + nybble = (*encoded) >> 4; + new_sample = IMA_ADPCM_nibble(state, nybble); + decoded[0] = new_sample & 0xFF; + new_sample >>= 8; + decoded[1] = new_sample & 0xFF; + decoded += 2 * numchannels; + + ++encoded; + } +} + +static int +IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len) +{ + struct IMA_ADPCM_decodestate *state; + Uint8 *freeable, *encoded, *decoded; + Sint32 encoded_len, samplesleft; + unsigned int c, channels; + + /* Check to make sure we have enough variables in the state array */ + channels = IMA_ADPCM_state.wavefmt.channels; + if (channels > SDL_arraysize(IMA_ADPCM_state.state)) { + SDL_SetError("IMA ADPCM decoder can only handle %u channels", + (unsigned int)SDL_arraysize(IMA_ADPCM_state.state)); + return (-1); + } + state = IMA_ADPCM_state.state; + + /* Allocate the proper sized output buffer */ + encoded_len = *audio_len; + encoded = *audio_buf; + freeable = *audio_buf; + *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) * + IMA_ADPCM_state.wSamplesPerBlock * + IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16); + *audio_buf = (Uint8 *) SDL_malloc(*audio_len); + if (*audio_buf == NULL) { + return SDL_OutOfMemory(); + } + decoded = *audio_buf; + + /* Get ready... Go! */ + while (encoded_len >= IMA_ADPCM_state.wavefmt.blockalign) { + /* Grab the initial information for this block */ + for (c = 0; c < channels; ++c) { + /* Fill the state information for this block */ + state[c].sample = ((encoded[1] << 8) | encoded[0]); + encoded += 2; + if (state[c].sample & 0x8000) { + state[c].sample -= 0x10000; + } + state[c].index = *encoded++; + /* Reserved byte in buffer header, should be 0 */ + if (*encoded++ != 0) { + /* Uh oh, corrupt data? Buggy code? */ ; + } + + /* Store the initial sample we start with */ + decoded[0] = (Uint8) (state[c].sample & 0xFF); + decoded[1] = (Uint8) (state[c].sample >> 8); + decoded += 2; + } + + /* Decode and store the other samples in this block */ + samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels; + while (samplesleft > 0) { + for (c = 0; c < channels; ++c) { + Fill_IMA_ADPCM_block(decoded, encoded, + c, channels, &state[c]); + encoded += 4; + samplesleft -= 8; + } + decoded += (channels * 8 * 2); + } + encoded_len -= IMA_ADPCM_state.wavefmt.blockalign; + } + SDL_free(freeable); + return (0); +} + + +static int +ConvertSint24ToSint32(Uint8 ** audio_buf, Uint32 * audio_len) +{ + const double DIVBY8388608 = 0.00000011920928955078125; + const Uint32 original_len = *audio_len; + const Uint32 samples = original_len / 3; + const Uint32 expanded_len = samples * sizeof (Uint32); + Uint8 *ptr = (Uint8 *) SDL_realloc(*audio_buf, expanded_len); + const Uint8 *src; + Uint32 *dst; + Uint32 i; + + if (!ptr) { + return SDL_OutOfMemory(); + } + + *audio_buf = ptr; + *audio_len = expanded_len; + + /* work from end to start, since we're expanding in-place. */ + src = (ptr + original_len) - 3; + dst = ((Uint32 *) (ptr + expanded_len)) - 1; + for (i = 0; i < samples; i++) { + /* There's probably a faster way to do all this. */ + const Sint32 converted = ((Sint32) ( (((Uint32) src[2]) << 24) | + (((Uint32) src[1]) << 16) | + (((Uint32) src[0]) << 8) )) >> 8; + const double scaled = (((double) converted) * DIVBY8388608); + src -= 3; + *(dst--) = (Sint32) (scaled * 2147483647.0); + } + + return 0; +} + + +/* GUIDs that are used by WAVE_FORMAT_EXTENSIBLE */ +static const Uint8 extensible_pcm_guid[16] = { 1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 }; +static const Uint8 extensible_ieee_guid[16] = { 3, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 }; + +SDL_AudioSpec * +SDL_LoadWAV_RW(SDL_RWops * src, int freesrc, + SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len) +{ + int was_error; + Chunk chunk; + int lenread; + int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded; + int samplesize; + + /* WAV magic header */ + Uint32 RIFFchunk; + Uint32 wavelen = 0; + Uint32 WAVEmagic; + Uint32 headerDiff = 0; + + /* FMT chunk */ + WaveFMT *format = NULL; + WaveExtensibleFMT *ext = NULL; + + SDL_zero(chunk); + + /* Make sure we are passed a valid data source */ + was_error = 0; + if (src == NULL) { + was_error = 1; + goto done; + } + + /* Check the magic header */ + RIFFchunk = SDL_ReadLE32(src); + wavelen = SDL_ReadLE32(src); + if (wavelen == WAVE) { /* The RIFFchunk has already been read */ + WAVEmagic = wavelen; + wavelen = RIFFchunk; + RIFFchunk = RIFF; + } else { + WAVEmagic = SDL_ReadLE32(src); + } + if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) { + SDL_SetError("Unrecognized file type (not WAVE)"); + was_error = 1; + goto done; + } + headerDiff += sizeof(Uint32); /* for WAVE */ + + /* Read the audio data format chunk */ + chunk.data = NULL; + do { + SDL_free(chunk.data); + chunk.data = NULL; + lenread = ReadChunk(src, &chunk); + if (lenread < 0) { + was_error = 1; + goto done; + } + /* 2 Uint32's for chunk header+len, plus the lenread */ + headerDiff += lenread + 2 * sizeof(Uint32); + } while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT) || (chunk.magic == JUNK)); + + /* Decode the audio data format */ + format = (WaveFMT *) chunk.data; + if (chunk.magic != FMT) { + SDL_SetError("Complex WAVE files not supported"); + was_error = 1; + goto done; + } + IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; + switch (SDL_SwapLE16(format->encoding)) { + case PCM_CODE: + /* We can understand this */ + break; + case IEEE_FLOAT_CODE: + IEEE_float_encoded = 1; + /* We can understand this */ + break; + case MS_ADPCM_CODE: + /* Try to understand this */ + if (InitMS_ADPCM(format) < 0) { + was_error = 1; + goto done; + } + MS_ADPCM_encoded = 1; + break; + case IMA_ADPCM_CODE: + /* Try to understand this */ + if (InitIMA_ADPCM(format) < 0) { + was_error = 1; + goto done; + } + IMA_ADPCM_encoded = 1; + break; + case EXTENSIBLE_CODE: + /* note that this ignores channel masks, smaller valid bit counts + inside a larger container, and most subtypes. This is just enough + to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE + to be useful working when they use this format flag. */ + ext = (WaveExtensibleFMT *) format; + if (SDL_SwapLE16(ext->size) < 22) { + SDL_SetError("bogus extended .wav header"); + was_error = 1; + goto done; + } + if (SDL_memcmp(ext->subformat, extensible_pcm_guid, 16) == 0) { + break; /* cool. */ + } else if (SDL_memcmp(ext->subformat, extensible_ieee_guid, 16) == 0) { + IEEE_float_encoded = 1; + break; + } + break; + case MP3_CODE: + SDL_SetError("MPEG Layer 3 data not supported"); + was_error = 1; + goto done; + default: + SDL_SetError("Unknown WAVE data format: 0x%.4x", + SDL_SwapLE16(format->encoding)); + was_error = 1; + goto done; + } + SDL_zerop(spec); + spec->freq = SDL_SwapLE32(format->frequency); + + if (IEEE_float_encoded) { + if ((SDL_SwapLE16(format->bitspersample)) != 32) { + was_error = 1; + } else { + spec->format = AUDIO_F32; + } + } else { + switch (SDL_SwapLE16(format->bitspersample)) { + case 4: + if (MS_ADPCM_encoded || IMA_ADPCM_encoded) { + spec->format = AUDIO_S16; + } else { + was_error = 1; + } + break; + case 8: + spec->format = AUDIO_U8; + break; + case 16: + spec->format = AUDIO_S16; + break; + case 24: /* convert this. */ + spec->format = AUDIO_S32; + break; + case 32: + spec->format = AUDIO_S32; + break; + default: + was_error = 1; + break; + } + } + + if (was_error) { + SDL_SetError("Unknown %d-bit PCM data format", + SDL_SwapLE16(format->bitspersample)); + goto done; + } + spec->channels = (Uint8) SDL_SwapLE16(format->channels); + spec->samples = 4096; /* Good default buffer size */ + + /* Read the audio data chunk */ + *audio_buf = NULL; + do { + SDL_free(*audio_buf); + *audio_buf = NULL; + lenread = ReadChunk(src, &chunk); + if (lenread < 0) { + was_error = 1; + goto done; + } + *audio_len = lenread; + *audio_buf = chunk.data; + if (chunk.magic != DATA) + headerDiff += lenread + 2 * sizeof(Uint32); + } while (chunk.magic != DATA); + headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */ + + if (MS_ADPCM_encoded) { + if (MS_ADPCM_decode(audio_buf, audio_len) < 0) { + was_error = 1; + goto done; + } + } + if (IMA_ADPCM_encoded) { + if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) { + was_error = 1; + goto done; + } + } + + if (SDL_SwapLE16(format->bitspersample) == 24) { + if (ConvertSint24ToSint32(audio_buf, audio_len) < 0) { + was_error = 1; + goto done; + } + } + + /* Don't return a buffer that isn't a multiple of samplesize */ + samplesize = ((SDL_AUDIO_BITSIZE(spec->format)) / 8) * spec->channels; + *audio_len &= ~(samplesize - 1); + + done: + SDL_free(format); + if (src) { + if (freesrc) { + SDL_RWclose(src); + } else { + /* seek to the end of the file (given by the RIFF chunk) */ + SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR); + } + } + if (was_error) { + spec = NULL; + } + return (spec); +} + +/* Since the WAV memory is allocated in the shared library, it must also + be freed here. (Necessary under Win32, VC++) + */ +void +SDL_FreeWAV(Uint8 * audio_buf) +{ + SDL_free(audio_buf); +} + +static int +ReadChunk(SDL_RWops * src, Chunk * chunk) +{ + chunk->magic = SDL_ReadLE32(src); + chunk->length = SDL_ReadLE32(src); + chunk->data = (Uint8 *) SDL_malloc(chunk->length); + if (chunk->data == NULL) { + return SDL_OutOfMemory(); + } + if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) { + SDL_free(chunk->data); + chunk->data = NULL; + return SDL_Error(SDL_EFREAD); + } + return (chunk->length); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/SDL_wave.h b/ThirdParty/SDL2/src/audio/SDL_wave.h new file mode 100644 index 0000000..5c60f75 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/SDL_wave.h @@ -0,0 +1,77 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../SDL_internal.h" + +/* WAVE files are little-endian */ + +/*******************************************/ +/* Define values for Microsoft WAVE format */ +/*******************************************/ +#define RIFF 0x46464952 /* "RIFF" */ +#define WAVE 0x45564157 /* "WAVE" */ +#define FACT 0x74636166 /* "fact" */ +#define LIST 0x5453494c /* "LIST" */ +#define BEXT 0x74786562 /* "bext" */ +#define JUNK 0x4B4E554A /* "JUNK" */ +#define FMT 0x20746D66 /* "fmt " */ +#define DATA 0x61746164 /* "data" */ +#define PCM_CODE 0x0001 +#define MS_ADPCM_CODE 0x0002 +#define IEEE_FLOAT_CODE 0x0003 +#define IMA_ADPCM_CODE 0x0011 +#define MP3_CODE 0x0055 +#define EXTENSIBLE_CODE 0xFFFE +#define WAVE_MONO 1 +#define WAVE_STEREO 2 + +/* Normally, these three chunks come consecutively in a WAVE file */ +typedef struct WaveFMT +{ +/* Not saved in the chunk we read: + Uint32 FMTchunk; + Uint32 fmtlen; +*/ + Uint16 encoding; + Uint16 channels; /* 1 = mono, 2 = stereo */ + Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */ + Uint32 byterate; /* Average bytes per second */ + Uint16 blockalign; /* Bytes per sample block */ + Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */ +} WaveFMT; + +/* The general chunk found in the WAVE file */ +typedef struct Chunk +{ + Uint32 magic; + Uint32 length; + Uint8 *data; +} Chunk; + +typedef struct WaveExtensibleFMT +{ + WaveFMT format; + Uint16 size; + Uint16 validbits; + Uint32 channelmask; + Uint8 subformat[16]; /* a GUID. */ +} WaveExtensibleFMT; + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.c b/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.c new file mode 100644 index 0000000..2dba1ff --- /dev/null +++ b/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.c @@ -0,0 +1,1030 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_ALSA + +/* Allow access to a raw mixing buffer */ + +#include +#include /* For kill() */ +#include + +#include "SDL_assert.h" +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_alsa_audio.h" + +#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC +#include "SDL_loadso.h" +#endif + +static int (*ALSA_snd_pcm_open) + (snd_pcm_t **, const char *, snd_pcm_stream_t, int); +static int (*ALSA_snd_pcm_close) (snd_pcm_t * pcm); +static snd_pcm_sframes_t (*ALSA_snd_pcm_writei) + (snd_pcm_t *, const void *, snd_pcm_uframes_t); +static snd_pcm_sframes_t (*ALSA_snd_pcm_readi) + (snd_pcm_t *, void *, snd_pcm_uframes_t); +static int (*ALSA_snd_pcm_recover) (snd_pcm_t *, int, int); +static int (*ALSA_snd_pcm_prepare) (snd_pcm_t *); +static int (*ALSA_snd_pcm_drain) (snd_pcm_t *); +static const char *(*ALSA_snd_strerror) (int); +static size_t(*ALSA_snd_pcm_hw_params_sizeof) (void); +static size_t(*ALSA_snd_pcm_sw_params_sizeof) (void); +static void (*ALSA_snd_pcm_hw_params_copy) + (snd_pcm_hw_params_t *, const snd_pcm_hw_params_t *); +static int (*ALSA_snd_pcm_hw_params_any) (snd_pcm_t *, snd_pcm_hw_params_t *); +static int (*ALSA_snd_pcm_hw_params_set_access) + (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t); +static int (*ALSA_snd_pcm_hw_params_set_format) + (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t); +static int (*ALSA_snd_pcm_hw_params_set_channels) + (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int); +static int (*ALSA_snd_pcm_hw_params_get_channels) + (const snd_pcm_hw_params_t *, unsigned int *); +static int (*ALSA_snd_pcm_hw_params_set_rate_near) + (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *); +static int (*ALSA_snd_pcm_hw_params_set_period_size_near) + (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *); +static int (*ALSA_snd_pcm_hw_params_get_period_size) + (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *); +static int (*ALSA_snd_pcm_hw_params_set_periods_near) + (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *); +static int (*ALSA_snd_pcm_hw_params_get_periods) + (const snd_pcm_hw_params_t *, unsigned int *, int *); +static int (*ALSA_snd_pcm_hw_params_set_buffer_size_near) + (snd_pcm_t *pcm, snd_pcm_hw_params_t *, snd_pcm_uframes_t *); +static int (*ALSA_snd_pcm_hw_params_get_buffer_size) + (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *); +static int (*ALSA_snd_pcm_hw_params) (snd_pcm_t *, snd_pcm_hw_params_t *); +static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *, + snd_pcm_sw_params_t *); +static int (*ALSA_snd_pcm_sw_params_set_start_threshold) + (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t); +static int (*ALSA_snd_pcm_sw_params) (snd_pcm_t *, snd_pcm_sw_params_t *); +static int (*ALSA_snd_pcm_nonblock) (snd_pcm_t *, int); +static int (*ALSA_snd_pcm_wait)(snd_pcm_t *, int); +static int (*ALSA_snd_pcm_sw_params_set_avail_min) + (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t); +static int (*ALSA_snd_pcm_reset)(snd_pcm_t *); +static int (*ALSA_snd_device_name_hint) (int, const char *, void ***); +static char* (*ALSA_snd_device_name_get_hint) (const void *, const char *); +static int (*ALSA_snd_device_name_free_hint) (void **); +#ifdef SND_CHMAP_API_VERSION +static snd_pcm_chmap_t* (*ALSA_snd_pcm_get_chmap) (snd_pcm_t *); +static int (*ALSA_snd_pcm_chmap_print) (const snd_pcm_chmap_t *map, size_t maxlen, char *buf); +#endif + +#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC +#define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof +#define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof + +static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC; +static void *alsa_handle = NULL; + +static int +load_alsa_sym(const char *fn, void **addr) +{ + *addr = SDL_LoadFunction(alsa_handle, fn); + if (*addr == NULL) { + /* Don't call SDL_SetError(): SDL_LoadFunction already did. */ + return 0; + } + + return 1; +} + +/* cast funcs to char* first, to please GCC's strict aliasing rules. */ +#define SDL_ALSA_SYM(x) \ + if (!load_alsa_sym(#x, (void **) (char *) &ALSA_##x)) return -1 +#else +#define SDL_ALSA_SYM(x) ALSA_##x = x +#endif + +static int +load_alsa_syms(void) +{ + SDL_ALSA_SYM(snd_pcm_open); + SDL_ALSA_SYM(snd_pcm_close); + SDL_ALSA_SYM(snd_pcm_writei); + SDL_ALSA_SYM(snd_pcm_readi); + SDL_ALSA_SYM(snd_pcm_recover); + SDL_ALSA_SYM(snd_pcm_prepare); + SDL_ALSA_SYM(snd_pcm_drain); + SDL_ALSA_SYM(snd_strerror); + SDL_ALSA_SYM(snd_pcm_hw_params_sizeof); + SDL_ALSA_SYM(snd_pcm_sw_params_sizeof); + SDL_ALSA_SYM(snd_pcm_hw_params_copy); + SDL_ALSA_SYM(snd_pcm_hw_params_any); + SDL_ALSA_SYM(snd_pcm_hw_params_set_access); + SDL_ALSA_SYM(snd_pcm_hw_params_set_format); + SDL_ALSA_SYM(snd_pcm_hw_params_set_channels); + SDL_ALSA_SYM(snd_pcm_hw_params_get_channels); + SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near); + SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near); + SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size); + SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_near); + SDL_ALSA_SYM(snd_pcm_hw_params_get_periods); + SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near); + SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size); + SDL_ALSA_SYM(snd_pcm_hw_params); + SDL_ALSA_SYM(snd_pcm_sw_params_current); + SDL_ALSA_SYM(snd_pcm_sw_params_set_start_threshold); + SDL_ALSA_SYM(snd_pcm_sw_params); + SDL_ALSA_SYM(snd_pcm_nonblock); + SDL_ALSA_SYM(snd_pcm_wait); + SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min); + SDL_ALSA_SYM(snd_pcm_reset); + SDL_ALSA_SYM(snd_device_name_hint); + SDL_ALSA_SYM(snd_device_name_get_hint); + SDL_ALSA_SYM(snd_device_name_free_hint); +#ifdef SND_CHMAP_API_VERSION + SDL_ALSA_SYM(snd_pcm_get_chmap); + SDL_ALSA_SYM(snd_pcm_chmap_print); +#endif + + return 0; +} + +#undef SDL_ALSA_SYM + +#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC + +static void +UnloadALSALibrary(void) +{ + if (alsa_handle != NULL) { + SDL_UnloadObject(alsa_handle); + alsa_handle = NULL; + } +} + +static int +LoadALSALibrary(void) +{ + int retval = 0; + if (alsa_handle == NULL) { + alsa_handle = SDL_LoadObject(alsa_library); + if (alsa_handle == NULL) { + retval = -1; + /* Don't call SDL_SetError(): SDL_LoadObject already did. */ + } else { + retval = load_alsa_syms(); + if (retval < 0) { + UnloadALSALibrary(); + } + } + } + return retval; +} + +#else + +static void +UnloadALSALibrary(void) +{ +} + +static int +LoadALSALibrary(void) +{ + load_alsa_syms(); + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */ + +static const char * +get_audio_device(void *handle, const int channels) +{ + const char *device; + + if (handle != NULL) { + return (const char *) handle; + } + + /* !!! FIXME: we also check "SDL_AUDIO_DEVICE_NAME" at the higher level. */ + device = SDL_getenv("AUDIODEV"); /* Is there a standard variable name? */ + if (device != NULL) { + return device; + } + + if (channels == 6) { + return "plug:surround51"; + } else if (channels == 4) { + return "plug:surround40"; + } + + return "default"; +} + + +/* This function waits until it is possible to write a full sound buffer */ +static void +ALSA_WaitDevice(_THIS) +{ + /* We're in blocking mode, so there's nothing to do here */ +} + + +/* !!! FIXME: is there a channel swizzler in alsalib instead? */ +/* + * http://bugzilla.libsdl.org/show_bug.cgi?id=110 + * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE + * and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR" + */ +#define SWIZ6(T, buf, numframes) \ + T *ptr = (T *) buf; \ + Uint32 i; \ + for (i = 0; i < numframes; i++, ptr += 6) { \ + T tmp; \ + tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \ + tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \ + } + +static void +swizzle_alsa_channels_6_64bit(void *buffer, Uint32 bufferlen) +{ + SWIZ6(Uint64, buffer, bufferlen); +} + +static void +swizzle_alsa_channels_6_32bit(void *buffer, Uint32 bufferlen) +{ + SWIZ6(Uint32, buffer, bufferlen); +} + +static void +swizzle_alsa_channels_6_16bit(void *buffer, Uint32 bufferlen) +{ + SWIZ6(Uint16, buffer, bufferlen); +} + +static void +swizzle_alsa_channels_6_8bit(void *buffer, Uint32 bufferlen) +{ + SWIZ6(Uint8, buffer, bufferlen); +} + +#undef SWIZ6 + + +/* + * Called right before feeding this->hidden->mixbuf to the hardware. Swizzle + * channels from Windows/Mac order to the format alsalib will want. + */ +static void +swizzle_alsa_channels(_THIS, void *buffer, Uint32 bufferlen) +{ + if (this->spec.channels == 6) { + switch (SDL_AUDIO_BITSIZE(this->spec.format)) { + case 8: swizzle_alsa_channels_6_8bit(buffer, bufferlen); break; + case 16: swizzle_alsa_channels_6_16bit(buffer, bufferlen); break; + case 32: swizzle_alsa_channels_6_32bit(buffer, bufferlen); break; + case 64: swizzle_alsa_channels_6_64bit(buffer, bufferlen); break; + default: SDL_assert(!"unhandled bitsize"); break; + } + } + + /* !!! FIXME: update this for 7.1 if needed, later. */ +} + +#ifdef SND_CHMAP_API_VERSION +/* Some devices have the right channel map, no swizzling necessary */ +static void +no_swizzle(_THIS, void *buffer, Uint32 bufferlen) +{ + return; +} +#endif /* SND_CHMAP_API_VERSION */ + + +static void +ALSA_PlayDevice(_THIS) +{ + const Uint8 *sample_buf = (const Uint8 *) this->hidden->mixbuf; + const int frame_size = (((int) SDL_AUDIO_BITSIZE(this->spec.format)) / 8) * + this->spec.channels; + snd_pcm_uframes_t frames_left = ((snd_pcm_uframes_t) this->spec.samples); + + this->hidden->swizzle_func(this, this->hidden->mixbuf, frames_left); + + while ( frames_left > 0 && SDL_AtomicGet(&this->enabled) ) { + int status = ALSA_snd_pcm_writei(this->hidden->pcm_handle, + sample_buf, frames_left); + + if (status < 0) { + if (status == -EAGAIN) { + /* Apparently snd_pcm_recover() doesn't handle this case - + does it assume snd_pcm_wait() above? */ + SDL_Delay(1); + continue; + } + status = ALSA_snd_pcm_recover(this->hidden->pcm_handle, status, 0); + if (status < 0) { + /* Hmm, not much we can do - abort */ + fprintf(stderr, "ALSA write failed (unrecoverable): %s\n", + ALSA_snd_strerror(status)); + SDL_OpenedAudioDeviceDisconnected(this); + return; + } + continue; + } + else if (status == 0) { + /* No frames were written (no available space in pcm device). + Allow other threads to catch up. */ + Uint32 delay = (frames_left / 2 * 1000) / this->spec.freq; + SDL_Delay(delay); + } + + sample_buf += status * frame_size; + frames_left -= status; + } +} + +static Uint8 * +ALSA_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + +static int +ALSA_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + Uint8 *sample_buf = (Uint8 *) buffer; + const int frame_size = (((int) SDL_AUDIO_BITSIZE(this->spec.format)) / 8) * + this->spec.channels; + const int total_frames = buflen / frame_size; + snd_pcm_uframes_t frames_left = total_frames; + snd_pcm_uframes_t wait_time = frame_size / 2; + + SDL_assert((buflen % frame_size) == 0); + + while ( frames_left > 0 && SDL_AtomicGet(&this->enabled) ) { + int status; + + status = ALSA_snd_pcm_readi(this->hidden->pcm_handle, + sample_buf, frames_left); + + if (status == -EAGAIN) { + ALSA_snd_pcm_wait(this->hidden->pcm_handle, wait_time); + status = 0; + } + else if (status < 0) { + /*printf("ALSA: capture error %d\n", status);*/ + status = ALSA_snd_pcm_recover(this->hidden->pcm_handle, status, 0); + if (status < 0) { + /* Hmm, not much we can do - abort */ + fprintf(stderr, "ALSA read failed (unrecoverable): %s\n", + ALSA_snd_strerror(status)); + return -1; + } + continue; + } + + /*printf("ALSA: captured %d bytes\n", status * frame_size);*/ + sample_buf += status * frame_size; + frames_left -= status; + } + + this->hidden->swizzle_func(this, buffer, total_frames - frames_left); + + return (total_frames - frames_left) * frame_size; +} + +static void +ALSA_FlushCapture(_THIS) +{ + ALSA_snd_pcm_reset(this->hidden->pcm_handle); +} + +static void +ALSA_CloseDevice(_THIS) +{ + if (this->hidden->pcm_handle) { + /* Wait for the submitted audio to drain + ALSA_snd_pcm_drop() can hang, so don't use that. + */ + Uint32 delay = ((this->spec.samples * 1000) / this->spec.freq) * 2; + SDL_Delay(delay); + + ALSA_snd_pcm_close(this->hidden->pcm_handle); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + +static int +ALSA_finalize_hardware(_THIS, snd_pcm_hw_params_t *hwparams, int override) +{ + int status; + snd_pcm_uframes_t bufsize; + + /* "set" the hardware with the desired parameters */ + status = ALSA_snd_pcm_hw_params(this->hidden->pcm_handle, hwparams); + if ( status < 0 ) { + return(-1); + } + + /* Get samples for the actual buffer size */ + status = ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize); + if ( status < 0 ) { + return(-1); + } + if ( !override && bufsize != this->spec.samples * 2 ) { + return(-1); + } + + /* !!! FIXME: Is this safe to do? */ + this->spec.samples = bufsize / 2; + + /* This is useful for debugging */ + if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) { + snd_pcm_uframes_t persize = 0; + unsigned int periods = 0; + + ALSA_snd_pcm_hw_params_get_period_size(hwparams, &persize, NULL); + ALSA_snd_pcm_hw_params_get_periods(hwparams, &periods, NULL); + + fprintf(stderr, + "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", + persize, periods, bufsize); + } + + return(0); +} + +static int +ALSA_set_period_size(_THIS, snd_pcm_hw_params_t *params, int override) +{ + const char *env; + int status; + snd_pcm_hw_params_t *hwparams; + snd_pcm_uframes_t frames; + unsigned int periods; + + /* Copy the hardware parameters for this setup */ + snd_pcm_hw_params_alloca(&hwparams); + ALSA_snd_pcm_hw_params_copy(hwparams, params); + + if ( !override ) { + env = SDL_getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE"); + if ( env ) { + override = SDL_atoi(env); + if ( override == 0 ) { + return(-1); + } + } + } + + frames = this->spec.samples; + status = ALSA_snd_pcm_hw_params_set_period_size_near( + this->hidden->pcm_handle, hwparams, &frames, NULL); + if ( status < 0 ) { + return(-1); + } + + periods = 2; + status = ALSA_snd_pcm_hw_params_set_periods_near( + this->hidden->pcm_handle, hwparams, &periods, NULL); + if ( status < 0 ) { + return(-1); + } + + return ALSA_finalize_hardware(this, hwparams, override); +} + +static int +ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params, int override) +{ + const char *env; + int status; + snd_pcm_hw_params_t *hwparams; + snd_pcm_uframes_t frames; + + /* Copy the hardware parameters for this setup */ + snd_pcm_hw_params_alloca(&hwparams); + ALSA_snd_pcm_hw_params_copy(hwparams, params); + + if ( !override ) { + env = SDL_getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE"); + if ( env ) { + override = SDL_atoi(env); + if ( override == 0 ) { + return(-1); + } + } + } + + frames = this->spec.samples * 2; + status = ALSA_snd_pcm_hw_params_set_buffer_size_near( + this->hidden->pcm_handle, hwparams, &frames); + if ( status < 0 ) { + return(-1); + } + + return ALSA_finalize_hardware(this, hwparams, override); +} + +static int +ALSA_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + int status = 0; + snd_pcm_t *pcm_handle = NULL; + snd_pcm_hw_params_t *hwparams = NULL; + snd_pcm_sw_params_t *swparams = NULL; + snd_pcm_format_t format = 0; + SDL_AudioFormat test_format = 0; + unsigned int rate = 0; + unsigned int channels = 0; +#ifdef SND_CHMAP_API_VERSION + snd_pcm_chmap_t *chmap; + char chmap_str[64]; +#endif + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Open the audio device */ + /* Name of device should depend on # channels in spec */ + status = ALSA_snd_pcm_open(&pcm_handle, + get_audio_device(handle, this->spec.channels), + iscapture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, + SND_PCM_NONBLOCK); + + if (status < 0) { + return SDL_SetError("ALSA: Couldn't open audio device: %s", + ALSA_snd_strerror(status)); + } + + this->hidden->pcm_handle = pcm_handle; + + /* Figure out what the hardware is capable of */ + snd_pcm_hw_params_alloca(&hwparams); + status = ALSA_snd_pcm_hw_params_any(pcm_handle, hwparams); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't get hardware config: %s", + ALSA_snd_strerror(status)); + } + + /* SDL only uses interleaved sample output */ + status = ALSA_snd_pcm_hw_params_set_access(pcm_handle, hwparams, + SND_PCM_ACCESS_RW_INTERLEAVED); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't set interleaved access: %s", + ALSA_snd_strerror(status)); + } + + /* Try for a closest match on audio format */ + status = -1; + for (test_format = SDL_FirstAudioFormat(this->spec.format); + test_format && (status < 0);) { + status = 0; /* if we can't support a format, it'll become -1. */ + switch (test_format) { + case AUDIO_U8: + format = SND_PCM_FORMAT_U8; + break; + case AUDIO_S8: + format = SND_PCM_FORMAT_S8; + break; + case AUDIO_S16LSB: + format = SND_PCM_FORMAT_S16_LE; + break; + case AUDIO_S16MSB: + format = SND_PCM_FORMAT_S16_BE; + break; + case AUDIO_U16LSB: + format = SND_PCM_FORMAT_U16_LE; + break; + case AUDIO_U16MSB: + format = SND_PCM_FORMAT_U16_BE; + break; + case AUDIO_S32LSB: + format = SND_PCM_FORMAT_S32_LE; + break; + case AUDIO_S32MSB: + format = SND_PCM_FORMAT_S32_BE; + break; + case AUDIO_F32LSB: + format = SND_PCM_FORMAT_FLOAT_LE; + break; + case AUDIO_F32MSB: + format = SND_PCM_FORMAT_FLOAT_BE; + break; + default: + status = -1; + break; + } + if (status >= 0) { + status = ALSA_snd_pcm_hw_params_set_format(pcm_handle, + hwparams, format); + } + if (status < 0) { + test_format = SDL_NextAudioFormat(); + } + } + if (status < 0) { + return SDL_SetError("ALSA: Couldn't find any hardware audio formats"); + } + this->spec.format = test_format; + + /* Validate number of channels and determine if swizzling is necessary + * Assume original swizzling, until proven otherwise. + */ + this->hidden->swizzle_func = swizzle_alsa_channels; +#ifdef SND_CHMAP_API_VERSION + chmap = ALSA_snd_pcm_get_chmap(pcm_handle); + if (chmap) { + ALSA_snd_pcm_chmap_print(chmap, sizeof(chmap_str), chmap_str); + if (SDL_strcmp("FL FR FC LFE RL RR", chmap_str) == 0 || + SDL_strcmp("FL FR FC LFE SL SR", chmap_str) == 0) { + this->hidden->swizzle_func = no_swizzle; + } + free(chmap); + } +#endif /* SND_CHMAP_API_VERSION */ + + /* Set the number of channels */ + status = ALSA_snd_pcm_hw_params_set_channels(pcm_handle, hwparams, + this->spec.channels); + channels = this->spec.channels; + if (status < 0) { + status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't set audio channels"); + } + this->spec.channels = channels; + } + + /* Set the audio rate */ + rate = this->spec.freq; + status = ALSA_snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, + &rate, NULL); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't set audio frequency: %s", + ALSA_snd_strerror(status)); + } + this->spec.freq = rate; + + /* Set the buffer size, in samples */ + if ( ALSA_set_period_size(this, hwparams, 0) < 0 && + ALSA_set_buffer_size(this, hwparams, 0) < 0 ) { + /* Failed to set desired buffer size, do the best you can... */ + status = ALSA_set_period_size(this, hwparams, 1); + if (status < 0) { + return SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status)); + } + } + /* Set the software parameters */ + snd_pcm_sw_params_alloca(&swparams); + status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't get software config: %s", + ALSA_snd_strerror(status)); + } + status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, this->spec.samples); + if (status < 0) { + return SDL_SetError("Couldn't set minimum available samples: %s", + ALSA_snd_strerror(status)); + } + status = + ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1); + if (status < 0) { + return SDL_SetError("ALSA: Couldn't set start threshold: %s", + ALSA_snd_strerror(status)); + } + status = ALSA_snd_pcm_sw_params(pcm_handle, swparams); + if (status < 0) { + return SDL_SetError("Couldn't set software audio parameters: %s", + ALSA_snd_strerror(status)); + } + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + /* Allocate mixing buffer */ + if (!iscapture) { + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen); + } + + if (!iscapture) { + ALSA_snd_pcm_nonblock(pcm_handle, 0); + } + + /* We're ready to rock and roll. :-) */ + return 0; +} + +typedef struct ALSA_Device +{ + char *name; + SDL_bool iscapture; + struct ALSA_Device *next; +} ALSA_Device; + +static void +add_device(const int iscapture, const char *name, void *hint, ALSA_Device **pSeen) +{ + ALSA_Device *dev = SDL_malloc(sizeof (ALSA_Device)); + char *desc; + char *handle = NULL; + char *ptr; + + if (!dev) { + return; + } + + /* Not all alsa devices are enumerable via snd_device_name_get_hint + (i.e. bluetooth devices). Therefore if hint is passed in to this + function as NULL, assume name contains desc. + Make sure not to free the storage associated with desc in this case */ + if (hint) { + desc = ALSA_snd_device_name_get_hint(hint, "DESC"); + if (!desc) { + SDL_free(dev); + return; + } + } else { + desc = (char *) name; + } + + SDL_assert(name != NULL); + + /* some strings have newlines, like "HDA NVidia, HDMI 0\nHDMI Audio Output". + just chop the extra lines off, this seems to get a reasonable device + name without extra details. */ + if ((ptr = strchr(desc, '\n')) != NULL) { + *ptr = '\0'; + } + + /*printf("ALSA: adding %s device '%s' (%s)\n", iscapture ? "capture" : "output", name, desc);*/ + + handle = SDL_strdup(name); + if (!handle) { + if (hint) { + free(desc); + } + SDL_free(dev); + return; + } + + SDL_AddAudioDevice(iscapture, desc, handle); + if (hint) + free(desc); + dev->name = handle; + dev->iscapture = iscapture; + dev->next = *pSeen; + *pSeen = dev; +} + + +static SDL_atomic_t ALSA_hotplug_shutdown; +static SDL_Thread *ALSA_hotplug_thread; + +static int SDLCALL +ALSA_HotplugThread(void *arg) +{ + SDL_sem *first_run_semaphore = (SDL_sem *) arg; + ALSA_Device *devices = NULL; + ALSA_Device *next; + ALSA_Device *dev; + Uint32 ticks; + + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW); + + while (!SDL_AtomicGet(&ALSA_hotplug_shutdown)) { + void **hints = NULL; + ALSA_Device *unseen; + ALSA_Device *seen; + ALSA_Device *prev; + + if (ALSA_snd_device_name_hint(-1, "pcm", &hints) != -1) { + int i, j; + const char *match = NULL; + int bestmatch = 0xFFFF; + size_t match_len = 0; + int defaultdev = -1; + static const char * const prefixes[] = { + "hw:", "sysdefault:", "default:", NULL + }; + + unseen = devices; + seen = NULL; + /* Apparently there are several different ways that ALSA lists + actual hardware. It could be prefixed with "hw:" or "default:" + or "sysdefault:" and maybe others. Go through the list and see + if we can find a preferred prefix for the system. */ + for (i = 0; hints[i]; i++) { + char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME"); + if (!name) { + continue; + } + + /* full name, not a prefix */ + if ((defaultdev == -1) && (SDL_strcmp(name, "default") == 0)) { + defaultdev = i; + } + + for (j = 0; prefixes[j]; j++) { + const char *prefix = prefixes[j]; + const size_t prefixlen = SDL_strlen(prefix); + if (SDL_strncmp(name, prefix, prefixlen) == 0) { + if (j < bestmatch) { + bestmatch = j; + match = prefix; + match_len = prefixlen; + } + } + } + + free(name); + } + + /* look through the list of device names to find matches */ + for (i = 0; hints[i]; i++) { + char *name; + + /* if we didn't find a device name prefix we like at all... */ + if ((!match) && (defaultdev != i)) { + continue; /* ...skip anything that isn't the default device. */ + } + + name = ALSA_snd_device_name_get_hint(hints[i], "NAME"); + if (!name) { + continue; + } + + /* only want physical hardware interfaces */ + if (!match || (SDL_strncmp(name, match, match_len) == 0)) { + char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID"); + const SDL_bool isoutput = (ioid == NULL) || (SDL_strcmp(ioid, "Output") == 0); + const SDL_bool isinput = (ioid == NULL) || (SDL_strcmp(ioid, "Input") == 0); + SDL_bool have_output = SDL_FALSE; + SDL_bool have_input = SDL_FALSE; + + free(ioid); + + if (!isoutput && !isinput) { + free(name); + continue; + } + + prev = NULL; + for (dev = unseen; dev; dev = next) { + next = dev->next; + if ( (SDL_strcmp(dev->name, name) == 0) && (((isinput) && dev->iscapture) || ((isoutput) && !dev->iscapture)) ) { + if (prev) { + prev->next = next; + } else { + unseen = next; + } + dev->next = seen; + seen = dev; + if (isinput) have_input = SDL_TRUE; + if (isoutput) have_output = SDL_TRUE; + } else { + prev = dev; + } + } + + if (isinput && !have_input) { + add_device(SDL_TRUE, name, hints[i], &seen); + } + if (isoutput && !have_output) { + add_device(SDL_FALSE, name, hints[i], &seen); + } + } + + free(name); + } + + ALSA_snd_device_name_free_hint(hints); + + devices = seen; /* now we have a known-good list of attached devices. */ + + /* report anything still in unseen as removed. */ + for (dev = unseen; dev; dev = next) { + /*printf("ALSA: removing usb %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/ + next = dev->next; + SDL_RemoveAudioDevice(dev->iscapture, dev->name); + SDL_free(dev->name); + SDL_free(dev); + } + } + + /* On first run, tell ALSA_DetectDevices() that we have a complete device list so it can return. */ + if (first_run_semaphore) { + SDL_SemPost(first_run_semaphore); + first_run_semaphore = NULL; /* let other thread clean it up. */ + } + + /* Block awhile before checking again, unless we're told to stop. */ + ticks = SDL_GetTicks() + 5000; + while (!SDL_AtomicGet(&ALSA_hotplug_shutdown) && !SDL_TICKS_PASSED(SDL_GetTicks(), ticks)) { + SDL_Delay(100); + } + } + + /* Shutting down! Clean up any data we've gathered. */ + for (dev = devices; dev; dev = next) { + /*printf("ALSA: at shutdown, removing %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/ + next = dev->next; + SDL_free(dev->name); + SDL_free(dev); + } + + return 0; +} + +static void +ALSA_DetectDevices(void) +{ + /* Start the device detection thread here, wait for an initial iteration to complete. */ + SDL_sem *semaphore = SDL_CreateSemaphore(0); + if (!semaphore) { + return; /* oh well. */ + } + + SDL_AtomicSet(&ALSA_hotplug_shutdown, 0); + + ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", semaphore); + if (ALSA_hotplug_thread) { + SDL_SemWait(semaphore); /* wait for the first iteration to finish. */ + } + + SDL_DestroySemaphore(semaphore); +} + +static void +ALSA_Deinitialize(void) +{ + if (ALSA_hotplug_thread != NULL) { + SDL_AtomicSet(&ALSA_hotplug_shutdown, 1); + SDL_WaitThread(ALSA_hotplug_thread, NULL); + ALSA_hotplug_thread = NULL; + } + + UnloadALSALibrary(); +} + +static int +ALSA_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadALSALibrary() < 0) { + return 0; + } + + /* Set the function pointers */ + impl->DetectDevices = ALSA_DetectDevices; + impl->OpenDevice = ALSA_OpenDevice; + impl->WaitDevice = ALSA_WaitDevice; + impl->GetDeviceBuf = ALSA_GetDeviceBuf; + impl->PlayDevice = ALSA_PlayDevice; + impl->CloseDevice = ALSA_CloseDevice; + impl->Deinitialize = ALSA_Deinitialize; + impl->CaptureFromDevice = ALSA_CaptureFromDevice; + impl->FlushCapture = ALSA_FlushCapture; + + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + + +AudioBootStrap ALSA_bootstrap = { + "alsa", "ALSA PCM audio", ALSA_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_ALSA */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.h b/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.h new file mode 100644 index 0000000..f620500 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/alsa/SDL_alsa_audio.h @@ -0,0 +1,48 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_ALSA_audio_h_ +#define SDL_ALSA_audio_h_ + +#include + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The audio device handle */ + snd_pcm_t *pcm_handle; + + /* Raw mixing buffer */ + Uint8 *mixbuf; + int mixlen; + + /* swizzle function */ + void (*swizzle_func)(_THIS, void *buffer, Uint32 bufferlen); +}; + +#endif /* SDL_ALSA_audio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.c b/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.c new file mode 100644 index 0000000..7a25424 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.c @@ -0,0 +1,226 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_ANDROID + +/* Output audio to Android */ + +#include "SDL_assert.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_androidaudio.h" + +#include "../../core/android/SDL_android.h" + +#include + +static SDL_AudioDevice* audioDevice = NULL; +static SDL_AudioDevice* captureDevice = NULL; + +static int +ANDROIDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + SDL_AudioFormat test_format; + + SDL_assert((captureDevice == NULL) || !iscapture); + SDL_assert((audioDevice == NULL) || iscapture); + + if (iscapture) { + captureDevice = this; + } else { + audioDevice = this; + } + + this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + + test_format = SDL_FirstAudioFormat(this->spec.format); + while (test_format != 0) { /* no "UNKNOWN" constant */ + if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) { + this->spec.format = test_format; + break; + } + test_format = SDL_NextAudioFormat(); + } + + if (test_format == 0) { + /* Didn't find a compatible format :( */ + return SDL_SetError("No compatible audio format!"); + } + + if (this->spec.channels > 1) { + this->spec.channels = 2; + } else { + this->spec.channels = 1; + } + + if (this->spec.freq < 8000) { + this->spec.freq = 8000; + } + if (this->spec.freq > 48000) { + this->spec.freq = 48000; + } + + /* TODO: pass in/return a (Java) device ID */ + this->spec.samples = Android_JNI_OpenAudioDevice(iscapture, this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples); + + if (this->spec.samples == 0) { + /* Init failed? */ + return SDL_SetError("Java-side initialization failed!"); + } + + SDL_CalculateAudioSpec(&this->spec); + + return 0; +} + +static void +ANDROIDAUDIO_PlayDevice(_THIS) +{ + Android_JNI_WriteAudioBuffer(); +} + +static Uint8 * +ANDROIDAUDIO_GetDeviceBuf(_THIS) +{ + return Android_JNI_GetAudioBuffer(); +} + +static int +ANDROIDAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + return Android_JNI_CaptureAudioBuffer(buffer, buflen); +} + +static void +ANDROIDAUDIO_FlushCapture(_THIS) +{ + Android_JNI_FlushCapturedAudio(); +} + +static void +ANDROIDAUDIO_CloseDevice(_THIS) +{ + /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread + so it's safe to terminate the Java side buffer and AudioTrack + */ + Android_JNI_CloseAudioDevice(this->iscapture); + if (this->iscapture) { + SDL_assert(captureDevice == this); + captureDevice = NULL; + } else { + SDL_assert(audioDevice == this); + audioDevice = NULL; + } + SDL_free(this->hidden); +} + +static int +ANDROIDAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + /* Set the function pointers */ + impl->OpenDevice = ANDROIDAUDIO_OpenDevice; + impl->PlayDevice = ANDROIDAUDIO_PlayDevice; + impl->GetDeviceBuf = ANDROIDAUDIO_GetDeviceBuf; + impl->CloseDevice = ANDROIDAUDIO_CloseDevice; + impl->CaptureFromDevice = ANDROIDAUDIO_CaptureFromDevice; + impl->FlushCapture = ANDROIDAUDIO_FlushCapture; + + /* and the capabilities */ + impl->HasCaptureSupport = SDL_TRUE; + impl->OnlyHasDefaultOutputDevice = 1; + impl->OnlyHasDefaultCaptureDevice = 1; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap ANDROIDAUDIO_bootstrap = { + "android", "SDL Android audio driver", ANDROIDAUDIO_Init, 0 +}; + +/* Pause (block) all non already paused audio devices by taking their mixer lock */ +void ANDROIDAUDIO_PauseDevices(void) +{ + /* TODO: Handle multiple devices? */ + struct SDL_PrivateAudioData *private; + if(audioDevice != NULL && audioDevice->hidden != NULL) { + private = (struct SDL_PrivateAudioData *) audioDevice->hidden; + if (SDL_AtomicGet(&audioDevice->paused)) { + /* The device is already paused, leave it alone */ + private->resume = SDL_FALSE; + } + else { + SDL_LockMutex(audioDevice->mixer_lock); + SDL_AtomicSet(&audioDevice->paused, 1); + private->resume = SDL_TRUE; + } + } + + if(captureDevice != NULL && captureDevice->hidden != NULL) { + private = (struct SDL_PrivateAudioData *) captureDevice->hidden; + if (SDL_AtomicGet(&captureDevice->paused)) { + /* The device is already paused, leave it alone */ + private->resume = SDL_FALSE; + } + else { + SDL_LockMutex(captureDevice->mixer_lock); + SDL_AtomicSet(&captureDevice->paused, 1); + private->resume = SDL_TRUE; + } + } +} + +/* Resume (unblock) all non already paused audio devices by releasing their mixer lock */ +void ANDROIDAUDIO_ResumeDevices(void) +{ + /* TODO: Handle multiple devices? */ + struct SDL_PrivateAudioData *private; + if(audioDevice != NULL && audioDevice->hidden != NULL) { + private = (struct SDL_PrivateAudioData *) audioDevice->hidden; + if (private->resume) { + SDL_AtomicSet(&audioDevice->paused, 0); + private->resume = SDL_FALSE; + SDL_UnlockMutex(audioDevice->mixer_lock); + } + } + + if(captureDevice != NULL && captureDevice->hidden != NULL) { + private = (struct SDL_PrivateAudioData *) captureDevice->hidden; + if (private->resume) { + SDL_AtomicSet(&captureDevice->paused, 0); + private->resume = SDL_FALSE; + SDL_UnlockMutex(captureDevice->mixer_lock); + } + } +} + +#else + +void ANDROIDAUDIO_ResumeDevices(void) {} +void ANDROIDAUDIO_PauseDevices(void) {} + +#endif /* SDL_AUDIO_DRIVER_ANDROID */ + +/* vi: set ts=4 sw=4 expandtab: */ + diff --git a/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.h b/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.h new file mode 100644 index 0000000..c732ac6 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/android/SDL_androidaudio.h @@ -0,0 +1,42 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_androidaudio_h_ +#define SDL_androidaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* Resume device if it was paused automatically */ + int resume; +}; + +void ANDROIDAUDIO_ResumeDevices(void); +void ANDROIDAUDIO_PauseDevices(void); + +#endif /* SDL_androidaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.c b/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.c new file mode 100644 index 0000000..4e3ebf2 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.c @@ -0,0 +1,365 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_ARTS + +/* Allow access to a raw mixing buffer */ + +#ifdef HAVE_SIGNAL_H +#include +#endif +#include +#include + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_artsaudio.h" + +#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC +#include "SDL_name.h" +#include "SDL_loadso.h" +#else +#define SDL_NAME(X) X +#endif + +#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC + +static const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC; +static void *arts_handle = NULL; + +/* !!! FIXME: I hate this SDL_NAME clutter...it makes everything so messy! */ +static int (*SDL_NAME(arts_init)) (void); +static void (*SDL_NAME(arts_free)) (void); +static arts_stream_t(*SDL_NAME(arts_play_stream)) (int rate, int bits, + int channels, + const char *name); +static int (*SDL_NAME(arts_stream_set)) (arts_stream_t s, + arts_parameter_t param, int value); +static int (*SDL_NAME(arts_stream_get)) (arts_stream_t s, + arts_parameter_t param); +static int (*SDL_NAME(arts_write)) (arts_stream_t s, const void *buffer, + int count); +static void (*SDL_NAME(arts_close_stream)) (arts_stream_t s); +static int (*SDL_NAME(arts_suspend))(void); +static int (*SDL_NAME(arts_suspended)) (void); +static const char *(*SDL_NAME(arts_error_text)) (int errorcode); + +#define SDL_ARTS_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) } +static struct +{ + const char *name; + void **func; +} arts_functions[] = { +/* *INDENT-OFF* */ + SDL_ARTS_SYM(arts_init), + SDL_ARTS_SYM(arts_free), + SDL_ARTS_SYM(arts_play_stream), + SDL_ARTS_SYM(arts_stream_set), + SDL_ARTS_SYM(arts_stream_get), + SDL_ARTS_SYM(arts_write), + SDL_ARTS_SYM(arts_close_stream), + SDL_ARTS_SYM(arts_suspend), + SDL_ARTS_SYM(arts_suspended), + SDL_ARTS_SYM(arts_error_text), +/* *INDENT-ON* */ +}; + +#undef SDL_ARTS_SYM + +static void +UnloadARTSLibrary() +{ + if (arts_handle != NULL) { + SDL_UnloadObject(arts_handle); + arts_handle = NULL; + } +} + +static int +LoadARTSLibrary(void) +{ + int i, retval = -1; + + if (arts_handle == NULL) { + arts_handle = SDL_LoadObject(arts_library); + if (arts_handle != NULL) { + retval = 0; + for (i = 0; i < SDL_arraysize(arts_functions); ++i) { + *arts_functions[i].func = + SDL_LoadFunction(arts_handle, arts_functions[i].name); + if (!*arts_functions[i].func) { + retval = -1; + UnloadARTSLibrary(); + break; + } + } + } + } + + return retval; +} + +#else + +static void +UnloadARTSLibrary() +{ + return; +} + +static int +LoadARTSLibrary(void) +{ + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */ + +/* This function waits until it is possible to write a full sound buffer */ +static void +ARTS_WaitDevice(_THIS) +{ + Sint32 ticks; + + /* Check to see if the thread-parent process is still alive */ + { + static int cnt = 0; + /* Note that this only works with thread implementations + that use a different process id for each thread. + */ + /* Check every 10 loops */ + if (this->hidden->parent && (((++cnt) % 10) == 0)) { + if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) { + SDL_OpenedAudioDeviceDisconnected(this); + } + } + } + + /* Use timer for general audio synchronization */ + ticks = + ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS; + if (ticks > 0) { + SDL_Delay(ticks); + } +} + +static void +ARTS_PlayDevice(_THIS) +{ + /* Write the audio data */ + int written = SDL_NAME(arts_write) (this->hidden->stream, + this->hidden->mixbuf, + this->hidden->mixlen); + + /* If timer synchronization is enabled, set the next write frame */ + if (this->hidden->frame_ticks) { + this->hidden->next_frame += this->hidden->frame_ticks; + } + + /* If we couldn't write, assume fatal error for now */ + if (written < 0) { + SDL_OpenedAudioDeviceDisconnected(this); + } +#ifdef DEBUG_AUDIO + fprintf(stderr, "Wrote %d bytes of audio data\n", written); +#endif +} + +static Uint8 * +ARTS_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + + +static void +ARTS_CloseDevice(_THIS) +{ + if (this->hidden->stream) { + SDL_NAME(arts_close_stream) (this->hidden->stream); + } + SDL_NAME(arts_free) (); + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + +static int +ARTS_Suspend(void) +{ + const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */ + while ( (!SDL_NAME(arts_suspended)()) && !SDL_TICKS_PASSED(SDL_GetTicks(), abortms) ) { + if ( SDL_NAME(arts_suspend)() ) { + break; + } + } + return SDL_NAME(arts_suspended)(); +} + +static int +ARTS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + int rc = 0; + int bits = 0, frag_spec = 0; + SDL_AudioFormat test_format = 0, format = 0; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Try for a closest match on audio format */ + for (test_format = SDL_FirstAudioFormat(this->spec.format); + !format && test_format;) { +#ifdef DEBUG_AUDIO + fprintf(stderr, "Trying format 0x%4.4x\n", test_format); +#endif + switch (test_format) { + case AUDIO_U8: + bits = 8; + format = 1; + break; + case AUDIO_S16LSB: + bits = 16; + format = 1; + break; + default: + format = 0; + break; + } + if (!format) { + test_format = SDL_NextAudioFormat(); + } + } + if (format == 0) { + return SDL_SetError("Couldn't find any hardware audio formats"); + } + this->spec.format = test_format; + + if ((rc = SDL_NAME(arts_init) ()) != 0) { + return SDL_SetError("Unable to initialize ARTS: %s", + SDL_NAME(arts_error_text) (rc)); + } + + if (!ARTS_Suspend()) { + return SDL_SetError("ARTS can not open audio device"); + } + + this->hidden->stream = SDL_NAME(arts_play_stream) (this->spec.freq, + bits, + this->spec.channels, + "SDL"); + + /* Play nothing so we have at least one write (server bug workaround). */ + SDL_NAME(arts_write) (this->hidden->stream, "", 0); + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + /* Determine the power of two of the fragment size */ + for (frag_spec = 0; (0x01 << frag_spec) < this->spec.size; ++frag_spec); + if ((0x01 << frag_spec) != this->spec.size) { + return SDL_SetError("Fragment size must be a power of two"); + } + frag_spec |= 0x00020000; /* two fragments, for low latency */ + +#ifdef ARTS_P_PACKET_SETTINGS + SDL_NAME(arts_stream_set) (this->hidden->stream, + ARTS_P_PACKET_SETTINGS, frag_spec); +#else + SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_SIZE, + frag_spec & 0xffff); + SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_COUNT, + frag_spec >> 16); +#endif + this->spec.size = SDL_NAME(arts_stream_get) (this->hidden->stream, + ARTS_P_PACKET_SIZE); + + /* Allocate mixing buffer */ + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + + /* Get the parent process id (we're the parent of the audio thread) */ + this->hidden->parent = getpid(); + + /* We're ready to rock and roll. :-) */ + return 0; +} + + +static void +ARTS_Deinitialize(void) +{ + UnloadARTSLibrary(); +} + + +static int +ARTS_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadARTSLibrary() < 0) { + return 0; + } else { + if (SDL_NAME(arts_init) () != 0) { + UnloadARTSLibrary(); + SDL_SetError("ARTS: arts_init failed (no audio server?)"); + return 0; + } + + /* Play a stream so aRts doesn't crash */ + if (ARTS_Suspend()) { + arts_stream_t stream; + stream = SDL_NAME(arts_play_stream) (44100, 16, 2, "SDL"); + SDL_NAME(arts_write) (stream, "", 0); + SDL_NAME(arts_close_stream) (stream); + } + + SDL_NAME(arts_free) (); + } + + /* Set the function pointers */ + impl->OpenDevice = ARTS_OpenDevice; + impl->PlayDevice = ARTS_PlayDevice; + impl->WaitDevice = ARTS_WaitDevice; + impl->GetDeviceBuf = ARTS_GetDeviceBuf; + impl->CloseDevice = ARTS_CloseDevice; + impl->Deinitialize = ARTS_Deinitialize; + impl->OnlyHasDefaultOutputDevice = 1; + + return 1; /* this audio target is available. */ +} + + +AudioBootStrap ARTS_bootstrap = { + "arts", "Analog RealTime Synthesizer", ARTS_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_ARTS */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.h b/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.h new file mode 100644 index 0000000..7743654 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/arts/SDL_artsaudio.h @@ -0,0 +1,53 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_artsaudio_h_ +#define SDL_artsaudio_h_ + +#include + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The stream descriptor for the audio device */ + arts_stream_t stream; + + /* The parent process id, to detect when application quits */ + pid_t parent; + + /* Raw mixing buffer */ + Uint8 *mixbuf; + int mixlen; + + /* Support for audio timing using a timer, in addition to SDL_IOReady() */ + float frame_ticks; + float next_frame; +}; +#define FUDGE_TICKS 10 /* The scheduler overhead ticks per frame */ + +#endif /* SDL_artsaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.h b/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.h new file mode 100644 index 0000000..7ce8b8d --- /dev/null +++ b/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.h @@ -0,0 +1,68 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_coreaudio_h_ +#define SDL_coreaudio_h_ + +#include "../SDL_sysaudio.h" + +#if !defined(__IPHONEOS__) +#define MACOSX_COREAUDIO 1 +#endif + +#if MACOSX_COREAUDIO +#include +#include +#else +#import +#import +#endif + +#include +#include + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + SDL_Thread *thread; + AudioQueueRef audioQueue; + AudioQueueBufferRef *audioBuffer; + void *buffer; + UInt32 bufferOffset; + UInt32 bufferSize; + AudioStreamBasicDescription strdesc; + SDL_sem *ready_semaphore; + char *thread_error; + SDL_atomic_t shutdown; +#if MACOSX_COREAUDIO + AudioDeviceID deviceID; +#else + SDL_bool interrupted; + CFTypeRef interruption_listener; +#endif +}; + +#endif /* SDL_coreaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.m b/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.m new file mode 100644 index 0000000..92f5f12 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/coreaudio/SDL_coreaudio.m @@ -0,0 +1,891 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_COREAUDIO + +/* !!! FIXME: clean out some of the macro salsa in here. */ + +#include "SDL_audio.h" +#include "SDL_hints.h" +#include "../SDL_audio_c.h" +#include "../SDL_sysaudio.h" +#include "SDL_coreaudio.h" +#include "SDL_assert.h" +#include "../../thread/SDL_systhread.h" + +#define DEBUG_COREAUDIO 0 + +#define CHECK_RESULT(msg) \ + if (result != noErr) { \ + SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \ + return 0; \ + } + +#if MACOSX_COREAUDIO +static const AudioObjectPropertyAddress devlist_address = { + kAudioHardwarePropertyDevices, + kAudioObjectPropertyScopeGlobal, + kAudioObjectPropertyElementMaster +}; + +typedef void (*addDevFn)(const char *name, const int iscapture, AudioDeviceID devId, void *data); + +typedef struct AudioDeviceList +{ + AudioDeviceID devid; + SDL_bool alive; + struct AudioDeviceList *next; +} AudioDeviceList; + +static AudioDeviceList *output_devs = NULL; +static AudioDeviceList *capture_devs = NULL; + +static SDL_bool +add_to_internal_dev_list(const int iscapture, AudioDeviceID devId) +{ + AudioDeviceList *item = (AudioDeviceList *) SDL_malloc(sizeof (AudioDeviceList)); + if (item == NULL) { + return SDL_FALSE; + } + item->devid = devId; + item->alive = SDL_TRUE; + item->next = iscapture ? capture_devs : output_devs; + if (iscapture) { + capture_devs = item; + } else { + output_devs = item; + } + + return SDL_TRUE; +} + +static void +addToDevList(const char *name, const int iscapture, AudioDeviceID devId, void *data) +{ + if (add_to_internal_dev_list(iscapture, devId)) { + SDL_AddAudioDevice(iscapture, name, (void *) ((size_t) devId)); + } +} + +static void +build_device_list(int iscapture, addDevFn addfn, void *addfndata) +{ + OSStatus result = noErr; + UInt32 size = 0; + AudioDeviceID *devs = NULL; + UInt32 i = 0; + UInt32 max = 0; + + result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, + &devlist_address, 0, NULL, &size); + if (result != kAudioHardwareNoError) + return; + + devs = (AudioDeviceID *) alloca(size); + if (devs == NULL) + return; + + result = AudioObjectGetPropertyData(kAudioObjectSystemObject, + &devlist_address, 0, NULL, &size, devs); + if (result != kAudioHardwareNoError) + return; + + max = size / sizeof (AudioDeviceID); + for (i = 0; i < max; i++) { + CFStringRef cfstr = NULL; + char *ptr = NULL; + AudioDeviceID dev = devs[i]; + AudioBufferList *buflist = NULL; + int usable = 0; + CFIndex len = 0; + const AudioObjectPropertyAddress addr = { + kAudioDevicePropertyStreamConfiguration, + iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, + kAudioObjectPropertyElementMaster + }; + + const AudioObjectPropertyAddress nameaddr = { + kAudioObjectPropertyName, + iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, + kAudioObjectPropertyElementMaster + }; + + result = AudioObjectGetPropertyDataSize(dev, &addr, 0, NULL, &size); + if (result != noErr) + continue; + + buflist = (AudioBufferList *) SDL_malloc(size); + if (buflist == NULL) + continue; + + result = AudioObjectGetPropertyData(dev, &addr, 0, NULL, + &size, buflist); + + if (result == noErr) { + UInt32 j; + for (j = 0; j < buflist->mNumberBuffers; j++) { + if (buflist->mBuffers[j].mNumberChannels > 0) { + usable = 1; + break; + } + } + } + + SDL_free(buflist); + + if (!usable) + continue; + + + size = sizeof (CFStringRef); + result = AudioObjectGetPropertyData(dev, &nameaddr, 0, NULL, &size, &cfstr); + if (result != kAudioHardwareNoError) + continue; + + len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr), + kCFStringEncodingUTF8); + + ptr = (char *) SDL_malloc(len + 1); + usable = ((ptr != NULL) && + (CFStringGetCString + (cfstr, ptr, len + 1, kCFStringEncodingUTF8))); + + CFRelease(cfstr); + + if (usable) { + len = strlen(ptr); + /* Some devices have whitespace at the end...trim it. */ + while ((len > 0) && (ptr[len - 1] == ' ')) { + len--; + } + usable = (len > 0); + } + + if (usable) { + ptr[len] = '\0'; + +#if DEBUG_COREAUDIO + printf("COREAUDIO: Found %s device #%d: '%s' (devid %d)\n", + ((iscapture) ? "capture" : "output"), + (int) i, ptr, (int) dev); +#endif + addfn(ptr, iscapture, dev, addfndata); + } + SDL_free(ptr); /* addfn() would have copied the string. */ + } +} + +static void +free_audio_device_list(AudioDeviceList **list) +{ + AudioDeviceList *item = *list; + while (item) { + AudioDeviceList *next = item->next; + SDL_free(item); + item = next; + } + *list = NULL; +} + +static void +COREAUDIO_DetectDevices(void) +{ + build_device_list(SDL_TRUE, addToDevList, NULL); + build_device_list(SDL_FALSE, addToDevList, NULL); +} + +static void +build_device_change_list(const char *name, const int iscapture, AudioDeviceID devId, void *data) +{ + AudioDeviceList **list = (AudioDeviceList **) data; + AudioDeviceList *item; + for (item = *list; item != NULL; item = item->next) { + if (item->devid == devId) { + item->alive = SDL_TRUE; + return; + } + } + + add_to_internal_dev_list(iscapture, devId); /* new device, add it. */ + SDL_AddAudioDevice(iscapture, name, (void *) ((size_t) devId)); +} + +static void +reprocess_device_list(const int iscapture, AudioDeviceList **list) +{ + AudioDeviceList *item; + AudioDeviceList *prev = NULL; + for (item = *list; item != NULL; item = item->next) { + item->alive = SDL_FALSE; + } + + build_device_list(iscapture, build_device_change_list, list); + + /* free items in the list that aren't still alive. */ + item = *list; + while (item != NULL) { + AudioDeviceList *next = item->next; + if (item->alive) { + prev = item; + } else { + SDL_RemoveAudioDevice(iscapture, (void *) ((size_t) item->devid)); + if (prev) { + prev->next = item->next; + } else { + *list = item->next; + } + SDL_free(item); + } + item = next; + } +} + +/* this is called when the system's list of available audio devices changes. */ +static OSStatus +device_list_changed(AudioObjectID systemObj, UInt32 num_addr, const AudioObjectPropertyAddress *addrs, void *data) +{ + reprocess_device_list(SDL_TRUE, &capture_devs); + reprocess_device_list(SDL_FALSE, &output_devs); + return 0; +} +#endif + + +static int open_playback_devices = 0; +static int open_capture_devices = 0; + +#if !MACOSX_COREAUDIO + +static void interruption_begin(_THIS) +{ + if (this != NULL && this->hidden->audioQueue != NULL) { + this->hidden->interrupted = SDL_TRUE; + AudioQueuePause(this->hidden->audioQueue); + } +} + +static void interruption_end(_THIS) +{ + if (this != NULL && this->hidden != NULL && this->hidden->audioQueue != NULL + && this->hidden->interrupted + && AudioQueueStart(this->hidden->audioQueue, NULL) == AVAudioSessionErrorCodeNone) { + this->hidden->interrupted = SDL_FALSE; + } +} + +@interface SDLInterruptionListener : NSObject + +@property (nonatomic, assign) SDL_AudioDevice *device; + +@end + +@implementation SDLInterruptionListener + +- (void)audioSessionInterruption:(NSNotification *)note +{ + @synchronized (self) { + NSNumber *type = note.userInfo[AVAudioSessionInterruptionTypeKey]; + if (type.unsignedIntegerValue == AVAudioSessionInterruptionTypeBegan) { + interruption_begin(self.device); + } else { + interruption_end(self.device); + } + } +} + +- (void)applicationBecameActive:(NSNotification *)note +{ + @synchronized (self) { + interruption_end(self.device); + } +} + +@end + +static BOOL update_audio_session(_THIS, SDL_bool open) +{ + @autoreleasepool { + AVAudioSession *session = [AVAudioSession sharedInstance]; + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; + /* Set category to ambient by default so that other music continues playing. */ + NSString *category = AVAudioSessionCategoryAmbient; + NSError *err = nil; + + if (open_playback_devices && open_capture_devices) { + category = AVAudioSessionCategoryPlayAndRecord; + } else if (open_capture_devices) { + category = AVAudioSessionCategoryRecord; + } else { + const char *hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY); + if (hint) { + if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) { + category = AVAudioSessionCategoryAmbient; + } else if (SDL_strcasecmp(hint, "AVAudioSessionCategorySoloAmbient") == 0) { + category = AVAudioSessionCategorySoloAmbient; + } else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayback") == 0 || + SDL_strcasecmp(hint, "playback") == 0) { + category = AVAudioSessionCategoryPlayback; + } + } + } + + if (![session setCategory:category error:&err]) { + NSString *desc = err.description; + SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String); + return NO; + } + + if (open_playback_devices + open_capture_devices == 1) { + if (![session setActive:YES error:&err]) { + NSString *desc = err.description; + SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String); + return NO; + } + } else if (!open_playback_devices && !open_capture_devices) { + [session setActive:NO error:nil]; + } + + if (open) { + SDLInterruptionListener *listener = [SDLInterruptionListener new]; + listener.device = this; + + [center addObserver:listener + selector:@selector(audioSessionInterruption:) + name:AVAudioSessionInterruptionNotification + object:session]; + + /* An interruption end notification is not guaranteed to be sent if + we were previously interrupted... resuming if needed when the app + becomes active seems to be the way to go. */ + [center addObserver:listener + selector:@selector(applicationBecameActive:) + name:UIApplicationDidBecomeActiveNotification + object:session]; + + [center addObserver:listener + selector:@selector(applicationBecameActive:) + name:UIApplicationWillEnterForegroundNotification + object:session]; + + this->hidden->interruption_listener = CFBridgingRetain(listener); + } else { + if (this->hidden->interruption_listener != NULL) { + SDLInterruptionListener *listener = nil; + listener = (SDLInterruptionListener *) CFBridgingRelease(this->hidden->interruption_listener); + @synchronized (listener) { + listener.device = NULL; + } + [center removeObserver:listener]; + } + } + } + + return YES; +} +#endif + + +/* The AudioQueue callback */ +static void +outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData; + if (SDL_AtomicGet(&this->hidden->shutdown)) { + return; /* don't do anything. */ + } + + if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) { + /* Supply silence if audio is not enabled or paused */ + SDL_memset(inBuffer->mAudioData, this->spec.silence, inBuffer->mAudioDataBytesCapacity); + } else { + UInt32 remaining = inBuffer->mAudioDataBytesCapacity; + Uint8 *ptr = (Uint8 *) inBuffer->mAudioData; + + while (remaining > 0) { + UInt32 len; + if (this->hidden->bufferOffset >= this->hidden->bufferSize) { + /* Generate the data */ + SDL_LockMutex(this->mixer_lock); + (*this->callbackspec.callback)(this->callbackspec.userdata, + this->hidden->buffer, this->hidden->bufferSize); + SDL_UnlockMutex(this->mixer_lock); + this->hidden->bufferOffset = 0; + } + + len = this->hidden->bufferSize - this->hidden->bufferOffset; + if (len > remaining) { + len = remaining; + } + SDL_memcpy(ptr, (char *)this->hidden->buffer + + this->hidden->bufferOffset, len); + ptr = ptr + len; + remaining -= len; + this->hidden->bufferOffset += len; + } + } + + AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL); + + inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity; +} + +static void +inputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, + const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions, + const AudioStreamPacketDescription *inPacketDescs ) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData; + + if (SDL_AtomicGet(&this->shutdown)) { + return; /* don't do anything. */ + } + + /* ignore unless we're active. */ + if (!SDL_AtomicGet(&this->paused) && SDL_AtomicGet(&this->enabled) && !SDL_AtomicGet(&this->paused)) { + const Uint8 *ptr = (const Uint8 *) inBuffer->mAudioData; + UInt32 remaining = inBuffer->mAudioDataByteSize; + while (remaining > 0) { + UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset; + if (len > remaining) { + len = remaining; + } + + SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len); + ptr += len; + remaining -= len; + this->hidden->bufferOffset += len; + + if (this->hidden->bufferOffset >= this->hidden->bufferSize) { + SDL_LockMutex(this->mixer_lock); + (*this->callbackspec.callback)(this->callbackspec.userdata, this->hidden->buffer, this->hidden->bufferSize); + SDL_UnlockMutex(this->mixer_lock); + this->hidden->bufferOffset = 0; + } + } + } + + AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL); +} + + +#if MACOSX_COREAUDIO +static const AudioObjectPropertyAddress alive_address = +{ + kAudioDevicePropertyDeviceIsAlive, + kAudioObjectPropertyScopeGlobal, + kAudioObjectPropertyElementMaster +}; + +static OSStatus +device_unplugged(AudioObjectID devid, UInt32 num_addr, const AudioObjectPropertyAddress *addrs, void *data) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) data; + SDL_bool dead = SDL_FALSE; + UInt32 isAlive = 1; + UInt32 size = sizeof (isAlive); + OSStatus error; + + if (!SDL_AtomicGet(&this->enabled)) { + return 0; /* already known to be dead. */ + } + + error = AudioObjectGetPropertyData(this->hidden->deviceID, &alive_address, + 0, NULL, &size, &isAlive); + + if (error == kAudioHardwareBadDeviceError) { + dead = SDL_TRUE; /* device was unplugged. */ + } else if ((error == kAudioHardwareNoError) && (!isAlive)) { + dead = SDL_TRUE; /* device died in some other way. */ + } + + if (dead) { + SDL_OpenedAudioDeviceDisconnected(this); + } + + return 0; +} +#endif + +static void +COREAUDIO_CloseDevice(_THIS) +{ + const SDL_bool iscapture = this->iscapture; + +/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */ +/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */ +#if MACOSX_COREAUDIO + /* Fire a callback if the device stops being "alive" (disconnected, etc). */ + AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this); +#endif + +#if !MACOSX_COREAUDIO + update_audio_session(this, SDL_FALSE); +#endif + + /* if callback fires again, feed silence; don't call into the app. */ + SDL_AtomicSet(&this->paused, 1); + + if (this->hidden->audioQueue) { + AudioQueueDispose(this->hidden->audioQueue, 1); + } + + if (this->hidden->thread) { + SDL_AtomicSet(&this->hidden->shutdown, 1); + SDL_WaitThread(this->hidden->thread, NULL); + } + + if (this->hidden->ready_semaphore) { + SDL_DestroySemaphore(this->hidden->ready_semaphore); + } + + /* AudioQueueDispose() frees the actual buffer objects. */ + SDL_free(this->hidden->audioBuffer); + SDL_free(this->hidden->thread_error); + SDL_free(this->hidden->buffer); + SDL_free(this->hidden); + + if (iscapture) { + open_capture_devices--; + } else { + open_playback_devices--; + } +} + +#if MACOSX_COREAUDIO +static int +prepare_device(_THIS, void *handle, int iscapture) +{ + AudioDeviceID devid = (AudioDeviceID) ((size_t) handle); + OSStatus result = noErr; + UInt32 size = 0; + UInt32 alive = 0; + pid_t pid = 0; + + AudioObjectPropertyAddress addr = { + 0, + kAudioObjectPropertyScopeGlobal, + kAudioObjectPropertyElementMaster + }; + + if (handle == NULL) { + size = sizeof (AudioDeviceID); + addr.mSelector = + ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice : + kAudioHardwarePropertyDefaultOutputDevice); + result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, + 0, NULL, &size, &devid); + CHECK_RESULT("AudioHardwareGetProperty (default device)"); + } + + addr.mSelector = kAudioDevicePropertyDeviceIsAlive; + addr.mScope = iscapture ? kAudioDevicePropertyScopeInput : + kAudioDevicePropertyScopeOutput; + + size = sizeof (alive); + result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &alive); + CHECK_RESULT + ("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)"); + + if (!alive) { + SDL_SetError("CoreAudio: requested device exists, but isn't alive."); + return 0; + } + + addr.mSelector = kAudioDevicePropertyHogMode; + size = sizeof (pid); + result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &pid); + + /* some devices don't support this property, so errors are fine here. */ + if ((result == noErr) && (pid != -1)) { + SDL_SetError("CoreAudio: requested device is being hogged."); + return 0; + } + + this->hidden->deviceID = devid; + return 1; +} +#endif + +static int +prepare_audioqueue(_THIS) +{ + const AudioStreamBasicDescription *strdesc = &this->hidden->strdesc; + const int iscapture = this->iscapture; + OSStatus result; + int i; + + SDL_assert(CFRunLoopGetCurrent() != NULL); + + if (iscapture) { + result = AudioQueueNewInput(strdesc, inputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue); + CHECK_RESULT("AudioQueueNewInput"); + } else { + result = AudioQueueNewOutput(strdesc, outputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue); + CHECK_RESULT("AudioQueueNewOutput"); + } + +#if MACOSX_COREAUDIO +{ + const AudioObjectPropertyAddress prop = { + kAudioDevicePropertyDeviceUID, + iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, + kAudioObjectPropertyElementMaster + }; + CFStringRef devuid; + UInt32 devuidsize = sizeof (devuid); + result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid); + CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)"); + result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize); + CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)"); + + /* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */ + /* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */ + /* Fire a callback if the device stops being "alive" (disconnected, etc). */ + AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this); +} +#endif + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + /* Allocate a sample buffer */ + this->hidden->bufferSize = this->spec.size; + this->hidden->bufferOffset = iscapture ? 0 : this->hidden->bufferSize; + + this->hidden->buffer = SDL_malloc(this->hidden->bufferSize); + if (this->hidden->buffer == NULL) { + SDL_OutOfMemory(); + return 0; + } + + /* Make sure we can feed the device a minimum amount of time */ + double MINIMUM_AUDIO_BUFFER_TIME_MS = 15.0; +#if defined(__IPHONEOS__) + if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { + /* Older iOS hardware, use 40 ms as a minimum time */ + MINIMUM_AUDIO_BUFFER_TIME_MS = 40.0; + } +#endif + const double msecs = (this->spec.samples / ((double) this->spec.freq)) * 1000.0; + int numAudioBuffers = 2; + if (msecs < MINIMUM_AUDIO_BUFFER_TIME_MS) { /* use more buffers if we have a VERY small sample set. */ + numAudioBuffers = ((int)SDL_ceil(MINIMUM_AUDIO_BUFFER_TIME_MS / msecs) * 2); + } + + this->hidden->audioBuffer = SDL_calloc(1, sizeof (AudioQueueBufferRef) * numAudioBuffers); + if (this->hidden->audioBuffer == NULL) { + SDL_OutOfMemory(); + return 0; + } + +#if DEBUG_COREAUDIO + printf("COREAUDIO: numAudioBuffers == %d\n", numAudioBuffers); +#endif + + for (i = 0; i < numAudioBuffers; i++) { + result = AudioQueueAllocateBuffer(this->hidden->audioQueue, this->spec.size, &this->hidden->audioBuffer[i]); + CHECK_RESULT("AudioQueueAllocateBuffer"); + SDL_memset(this->hidden->audioBuffer[i]->mAudioData, this->spec.silence, this->hidden->audioBuffer[i]->mAudioDataBytesCapacity); + this->hidden->audioBuffer[i]->mAudioDataByteSize = this->hidden->audioBuffer[i]->mAudioDataBytesCapacity; + result = AudioQueueEnqueueBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i], 0, NULL); + CHECK_RESULT("AudioQueueEnqueueBuffer"); + } + + result = AudioQueueStart(this->hidden->audioQueue, NULL); + CHECK_RESULT("AudioQueueStart"); + + /* We're running! */ + return 1; +} + +static int +audioqueue_thread(void *arg) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) arg; + const int rc = prepare_audioqueue(this); + if (!rc) { + this->hidden->thread_error = SDL_strdup(SDL_GetError()); + SDL_SemPost(this->hidden->ready_semaphore); + return 0; + } + + /* init was successful, alert parent thread and start running... */ + SDL_SemPost(this->hidden->ready_semaphore); + while (!SDL_AtomicGet(&this->hidden->shutdown)) { + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.10, 1); + } + + if (!this->iscapture) { /* Drain off any pending playback. */ + const CFTimeInterval secs = (((this->spec.size / (SDL_AUDIO_BITSIZE(this->spec.format) / 8)) / this->spec.channels) / ((CFTimeInterval) this->spec.freq)) * 2.0; + CFRunLoopRunInMode(kCFRunLoopDefaultMode, secs, 0); + } + + return 0; +} + +static int +COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + AudioStreamBasicDescription *strdesc; + SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); + int valid_datatype = 0; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + strdesc = &this->hidden->strdesc; + + if (iscapture) { + open_capture_devices++; + } else { + open_playback_devices++; + } + +#if !MACOSX_COREAUDIO + if (!update_audio_session(this, SDL_TRUE)) { + return -1; + } + + /* Stop CoreAudio from doing expensive audio rate conversion */ + @autoreleasepool { + AVAudioSession* session = [AVAudioSession sharedInstance]; + [session setPreferredSampleRate:this->spec.freq error:nil]; + this->spec.freq = (int)session.sampleRate; + } +#endif + + /* Setup a AudioStreamBasicDescription with the requested format */ + SDL_zerop(strdesc); + strdesc->mFormatID = kAudioFormatLinearPCM; + strdesc->mFormatFlags = kLinearPCMFormatFlagIsPacked; + strdesc->mChannelsPerFrame = this->spec.channels; + strdesc->mSampleRate = this->spec.freq; + strdesc->mFramesPerPacket = 1; + + while ((!valid_datatype) && (test_format)) { + this->spec.format = test_format; + /* Just a list of valid SDL formats, so people don't pass junk here. */ + switch (test_format) { + case AUDIO_U8: + case AUDIO_S8: + case AUDIO_U16LSB: + case AUDIO_S16LSB: + case AUDIO_U16MSB: + case AUDIO_S16MSB: + case AUDIO_S32LSB: + case AUDIO_S32MSB: + case AUDIO_F32LSB: + case AUDIO_F32MSB: + valid_datatype = 1; + strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format); + if (SDL_AUDIO_ISBIGENDIAN(this->spec.format)) + strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; + + if (SDL_AUDIO_ISFLOAT(this->spec.format)) + strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat; + else if (SDL_AUDIO_ISSIGNED(this->spec.format)) + strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; + break; + } + } + + if (!valid_datatype) { /* shouldn't happen, but just in case... */ + return SDL_SetError("Unsupported audio format"); + } + + strdesc->mBytesPerFrame = strdesc->mBitsPerChannel * strdesc->mChannelsPerFrame / 8; + strdesc->mBytesPerPacket = strdesc->mBytesPerFrame * strdesc->mFramesPerPacket; + +#if MACOSX_COREAUDIO + if (!prepare_device(this, handle, iscapture)) { + return -1; + } +#endif + + /* This has to init in a new thread so it can get its own CFRunLoop. :/ */ + SDL_AtomicSet(&this->hidden->shutdown, 0); + this->hidden->ready_semaphore = SDL_CreateSemaphore(0); + if (!this->hidden->ready_semaphore) { + return -1; /* oh well. */ + } + + this->hidden->thread = SDL_CreateThreadInternal(audioqueue_thread, "AudioQueue thread", 512 * 1024, this); + if (!this->hidden->thread) { + return -1; + } + + SDL_SemWait(this->hidden->ready_semaphore); + SDL_DestroySemaphore(this->hidden->ready_semaphore); + this->hidden->ready_semaphore = NULL; + + if ((this->hidden->thread != NULL) && (this->hidden->thread_error != NULL)) { + SDL_SetError("%s", this->hidden->thread_error); + return -1; + } + + return (this->hidden->thread != NULL) ? 0 : -1; +} + +static void +COREAUDIO_Deinitialize(void) +{ +#if MACOSX_COREAUDIO + AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &devlist_address, device_list_changed, NULL); + free_audio_device_list(&capture_devs); + free_audio_device_list(&output_devs); +#endif +} + +static int +COREAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + /* Set the function pointers */ + impl->OpenDevice = COREAUDIO_OpenDevice; + impl->CloseDevice = COREAUDIO_CloseDevice; + impl->Deinitialize = COREAUDIO_Deinitialize; + +#if MACOSX_COREAUDIO + impl->DetectDevices = COREAUDIO_DetectDevices; + AudioObjectAddPropertyListener(kAudioObjectSystemObject, &devlist_address, device_list_changed, NULL); +#else + impl->OnlyHasDefaultOutputDevice = 1; + impl->OnlyHasDefaultCaptureDevice = 1; +#endif + + impl->ProvidesOwnCallbackThread = 1; + impl->HasCaptureSupport = 1; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap COREAUDIO_bootstrap = { + "coreaudio", "CoreAudio", COREAUDIO_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_COREAUDIO */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.c b/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.c new file mode 100644 index 0000000..09b83ae --- /dev/null +++ b/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.c @@ -0,0 +1,604 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_DSOUND + +/* Allow access to a raw mixing buffer */ + +#include "SDL_assert.h" +#include "SDL_timer.h" +#include "SDL_loadso.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_directsound.h" + +#ifndef WAVE_FORMAT_IEEE_FLOAT +#define WAVE_FORMAT_IEEE_FLOAT 0x0003 +#endif + +/* DirectX function pointers for audio */ +static void* DSoundDLL = NULL; +typedef HRESULT (WINAPI *fnDirectSoundCreate8)(LPGUID,LPDIRECTSOUND*,LPUNKNOWN); +typedef HRESULT (WINAPI *fnDirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID); +typedef HRESULT (WINAPI *fnDirectSoundCaptureCreate8)(LPCGUID,LPDIRECTSOUNDCAPTURE8 *,LPUNKNOWN); +typedef HRESULT (WINAPI *fnDirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW,LPVOID); +static fnDirectSoundCreate8 pDirectSoundCreate8 = NULL; +static fnDirectSoundEnumerateW pDirectSoundEnumerateW = NULL; +static fnDirectSoundCaptureCreate8 pDirectSoundCaptureCreate8 = NULL; +static fnDirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW = NULL; + +static void +DSOUND_Unload(void) +{ + pDirectSoundCreate8 = NULL; + pDirectSoundEnumerateW = NULL; + pDirectSoundCaptureCreate8 = NULL; + pDirectSoundCaptureEnumerateW = NULL; + + if (DSoundDLL != NULL) { + SDL_UnloadObject(DSoundDLL); + DSoundDLL = NULL; + } +} + + +static int +DSOUND_Load(void) +{ + int loaded = 0; + + DSOUND_Unload(); + + DSoundDLL = SDL_LoadObject("DSOUND.DLL"); + if (DSoundDLL == NULL) { + SDL_SetError("DirectSound: failed to load DSOUND.DLL"); + } else { + /* Now make sure we have DirectX 8 or better... */ + #define DSOUNDLOAD(f) { \ + p##f = (fn##f) SDL_LoadFunction(DSoundDLL, #f); \ + if (!p##f) loaded = 0; \ + } + loaded = 1; /* will reset if necessary. */ + DSOUNDLOAD(DirectSoundCreate8); + DSOUNDLOAD(DirectSoundEnumerateW); + DSOUNDLOAD(DirectSoundCaptureCreate8); + DSOUNDLOAD(DirectSoundCaptureEnumerateW); + #undef DSOUNDLOAD + + if (!loaded) { + SDL_SetError("DirectSound: System doesn't appear to have DX8."); + } + } + + if (!loaded) { + DSOUND_Unload(); + } + + return loaded; +} + +static int +SetDSerror(const char *function, int code) +{ + static const char *error; + static char errbuf[1024]; + + errbuf[0] = 0; + switch (code) { + case E_NOINTERFACE: + error = "Unsupported interface -- Is DirectX 8.0 or later installed?"; + break; + case DSERR_ALLOCATED: + error = "Audio device in use"; + break; + case DSERR_BADFORMAT: + error = "Unsupported audio format"; + break; + case DSERR_BUFFERLOST: + error = "Mixing buffer was lost"; + break; + case DSERR_CONTROLUNAVAIL: + error = "Control requested is not available"; + break; + case DSERR_INVALIDCALL: + error = "Invalid call for the current state"; + break; + case DSERR_INVALIDPARAM: + error = "Invalid parameter"; + break; + case DSERR_NODRIVER: + error = "No audio device found"; + break; + case DSERR_OUTOFMEMORY: + error = "Out of memory"; + break; + case DSERR_PRIOLEVELNEEDED: + error = "Caller doesn't have priority"; + break; + case DSERR_UNSUPPORTED: + error = "Function not supported"; + break; + default: + SDL_snprintf(errbuf, SDL_arraysize(errbuf), + "%s: Unknown DirectSound error: 0x%x", function, code); + break; + } + if (!errbuf[0]) { + SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, + error); + } + return SDL_SetError("%s", errbuf); +} + +static void +DSOUND_FreeDeviceHandle(void *handle) +{ + SDL_free(handle); +} + +static BOOL CALLBACK +FindAllDevs(LPGUID guid, LPCWSTR desc, LPCWSTR module, LPVOID data) +{ + const int iscapture = (int) ((size_t) data); + if (guid != NULL) { /* skip default device */ + char *str = WIN_LookupAudioDeviceName(desc, guid); + if (str != NULL) { + LPGUID cpyguid = (LPGUID) SDL_malloc(sizeof (GUID)); + SDL_memcpy(cpyguid, guid, sizeof (GUID)); + SDL_AddAudioDevice(iscapture, str, cpyguid); + SDL_free(str); /* addfn() makes a copy of this string. */ + } + } + return TRUE; /* keep enumerating. */ +} + +static void +DSOUND_DetectDevices(void) +{ + pDirectSoundCaptureEnumerateW(FindAllDevs, (void *) ((size_t) 1)); + pDirectSoundEnumerateW(FindAllDevs, (void *) ((size_t) 0)); +} + + +static void +DSOUND_WaitDevice(_THIS) +{ + DWORD status = 0; + DWORD cursor = 0; + DWORD junk = 0; + HRESULT result = DS_OK; + + /* Semi-busy wait, since we have no way of getting play notification + on a primary mixing buffer located in hardware (DirectX 5.0) + */ + result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf, + &junk, &cursor); + if (result != DS_OK) { + if (result == DSERR_BUFFERLOST) { + IDirectSoundBuffer_Restore(this->hidden->mixbuf); + } +#ifdef DEBUG_SOUND + SetDSerror("DirectSound GetCurrentPosition", result); +#endif + return; + } + + while ((cursor / this->spec.size) == this->hidden->lastchunk) { + /* FIXME: find out how much time is left and sleep that long */ + SDL_Delay(1); + + /* Try to restore a lost sound buffer */ + IDirectSoundBuffer_GetStatus(this->hidden->mixbuf, &status); + if ((status & DSBSTATUS_BUFFERLOST)) { + IDirectSoundBuffer_Restore(this->hidden->mixbuf); + IDirectSoundBuffer_GetStatus(this->hidden->mixbuf, &status); + if ((status & DSBSTATUS_BUFFERLOST)) { + break; + } + } + if (!(status & DSBSTATUS_PLAYING)) { + result = IDirectSoundBuffer_Play(this->hidden->mixbuf, 0, 0, + DSBPLAY_LOOPING); + if (result == DS_OK) { + continue; + } +#ifdef DEBUG_SOUND + SetDSerror("DirectSound Play", result); +#endif + return; + } + + /* Find out where we are playing */ + result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf, + &junk, &cursor); + if (result != DS_OK) { + SetDSerror("DirectSound GetCurrentPosition", result); + return; + } + } +} + +static void +DSOUND_PlayDevice(_THIS) +{ + /* Unlock the buffer, allowing it to play */ + if (this->hidden->locked_buf) { + IDirectSoundBuffer_Unlock(this->hidden->mixbuf, + this->hidden->locked_buf, + this->spec.size, NULL, 0); + } +} + +static Uint8 * +DSOUND_GetDeviceBuf(_THIS) +{ + DWORD cursor = 0; + DWORD junk = 0; + HRESULT result = DS_OK; + DWORD rawlen = 0; + + /* Figure out which blocks to fill next */ + this->hidden->locked_buf = NULL; + result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf, + &junk, &cursor); + if (result == DSERR_BUFFERLOST) { + IDirectSoundBuffer_Restore(this->hidden->mixbuf); + result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf, + &junk, &cursor); + } + if (result != DS_OK) { + SetDSerror("DirectSound GetCurrentPosition", result); + return (NULL); + } + cursor /= this->spec.size; +#ifdef DEBUG_SOUND + /* Detect audio dropouts */ + { + DWORD spot = cursor; + if (spot < this->hidden->lastchunk) { + spot += this->hidden->num_buffers; + } + if (spot > this->hidden->lastchunk + 1) { + fprintf(stderr, "Audio dropout, missed %d fragments\n", + (spot - (this->hidden->lastchunk + 1))); + } + } +#endif + this->hidden->lastchunk = cursor; + cursor = (cursor + 1) % this->hidden->num_buffers; + cursor *= this->spec.size; + + /* Lock the audio buffer */ + result = IDirectSoundBuffer_Lock(this->hidden->mixbuf, cursor, + this->spec.size, + (LPVOID *) & this->hidden->locked_buf, + &rawlen, NULL, &junk, 0); + if (result == DSERR_BUFFERLOST) { + IDirectSoundBuffer_Restore(this->hidden->mixbuf); + result = IDirectSoundBuffer_Lock(this->hidden->mixbuf, cursor, + this->spec.size, + (LPVOID *) & this-> + hidden->locked_buf, &rawlen, NULL, + &junk, 0); + } + if (result != DS_OK) { + SetDSerror("DirectSound Lock", result); + return (NULL); + } + return (this->hidden->locked_buf); +} + +static int +DSOUND_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + struct SDL_PrivateAudioData *h = this->hidden; + DWORD junk, cursor, ptr1len, ptr2len; + VOID *ptr1, *ptr2; + + SDL_assert(buflen == this->spec.size); + + while (SDL_TRUE) { + if (SDL_AtomicGet(&this->shutdown)) { /* in case the buffer froze... */ + SDL_memset(buffer, this->spec.silence, buflen); + return buflen; + } + + if (IDirectSoundCaptureBuffer_GetCurrentPosition(h->capturebuf, &junk, &cursor) != DS_OK) { + return -1; + } + if ((cursor / this->spec.size) == h->lastchunk) { + SDL_Delay(1); /* FIXME: find out how much time is left and sleep that long */ + } else { + break; + } + } + + if (IDirectSoundCaptureBuffer_Lock(h->capturebuf, h->lastchunk * this->spec.size, this->spec.size, &ptr1, &ptr1len, &ptr2, &ptr2len, 0) != DS_OK) { + return -1; + } + + SDL_assert(ptr1len == this->spec.size); + SDL_assert(ptr2 == NULL); + SDL_assert(ptr2len == 0); + + SDL_memcpy(buffer, ptr1, ptr1len); + + if (IDirectSoundCaptureBuffer_Unlock(h->capturebuf, ptr1, ptr1len, ptr2, ptr2len) != DS_OK) { + return -1; + } + + h->lastchunk = (h->lastchunk + 1) % h->num_buffers; + + return ptr1len; +} + +static void +DSOUND_FlushCapture(_THIS) +{ + struct SDL_PrivateAudioData *h = this->hidden; + DWORD junk, cursor; + if (IDirectSoundCaptureBuffer_GetCurrentPosition(h->capturebuf, &junk, &cursor) == DS_OK) { + h->lastchunk = cursor / this->spec.size; + } +} + +static void +DSOUND_CloseDevice(_THIS) +{ + if (this->hidden->mixbuf != NULL) { + IDirectSoundBuffer_Stop(this->hidden->mixbuf); + IDirectSoundBuffer_Release(this->hidden->mixbuf); + } + if (this->hidden->sound != NULL) { + IDirectSound_Release(this->hidden->sound); + } + if (this->hidden->capturebuf != NULL) { + IDirectSoundCaptureBuffer_Stop(this->hidden->capturebuf); + IDirectSoundCaptureBuffer_Release(this->hidden->capturebuf); + } + if (this->hidden->capture != NULL) { + IDirectSoundCapture_Release(this->hidden->capture); + } + SDL_free(this->hidden); +} + +/* This function tries to create a secondary audio buffer, and returns the + number of audio chunks available in the created buffer. This is for + playback devices, not capture. +*/ +static int +CreateSecondary(_THIS, const DWORD bufsize, WAVEFORMATEX *wfmt) +{ + LPDIRECTSOUND sndObj = this->hidden->sound; + LPDIRECTSOUNDBUFFER *sndbuf = &this->hidden->mixbuf; + HRESULT result = DS_OK; + DSBUFFERDESC format; + LPVOID pvAudioPtr1, pvAudioPtr2; + DWORD dwAudioBytes1, dwAudioBytes2; + + /* Try to create the secondary buffer */ + SDL_zero(format); + format.dwSize = sizeof(format); + format.dwFlags = DSBCAPS_GETCURRENTPOSITION2; + format.dwFlags |= DSBCAPS_GLOBALFOCUS; + format.dwBufferBytes = bufsize; + format.lpwfxFormat = wfmt; + result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL); + if (result != DS_OK) { + return SetDSerror("DirectSound CreateSoundBuffer", result); + } + IDirectSoundBuffer_SetFormat(*sndbuf, wfmt); + + /* Silence the initial audio buffer */ + result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes, + (LPVOID *) & pvAudioPtr1, &dwAudioBytes1, + (LPVOID *) & pvAudioPtr2, &dwAudioBytes2, + DSBLOCK_ENTIREBUFFER); + if (result == DS_OK) { + SDL_memset(pvAudioPtr1, this->spec.silence, dwAudioBytes1); + IDirectSoundBuffer_Unlock(*sndbuf, + (LPVOID) pvAudioPtr1, dwAudioBytes1, + (LPVOID) pvAudioPtr2, dwAudioBytes2); + } + + /* We're ready to go */ + return 0; +} + +/* This function tries to create a capture buffer, and returns the + number of audio chunks available in the created buffer. This is for + capture devices, not playback. +*/ +static int +CreateCaptureBuffer(_THIS, const DWORD bufsize, WAVEFORMATEX *wfmt) +{ + LPDIRECTSOUNDCAPTURE capture = this->hidden->capture; + LPDIRECTSOUNDCAPTUREBUFFER *capturebuf = &this->hidden->capturebuf; + DSCBUFFERDESC format; + HRESULT result; + + SDL_zero(format); + format.dwSize = sizeof (format); + format.dwFlags = DSCBCAPS_WAVEMAPPED; + format.dwBufferBytes = bufsize; + format.lpwfxFormat = wfmt; + + result = IDirectSoundCapture_CreateCaptureBuffer(capture, &format, capturebuf, NULL); + if (result != DS_OK) { + return SetDSerror("DirectSound CreateCaptureBuffer", result); + } + + result = IDirectSoundCaptureBuffer_Start(*capturebuf, DSCBSTART_LOOPING); + if (result != DS_OK) { + IDirectSoundCaptureBuffer_Release(*capturebuf); + return SetDSerror("DirectSound Start", result); + } + +#if 0 + /* presumably this starts at zero, but just in case... */ + result = IDirectSoundCaptureBuffer_GetCurrentPosition(*capturebuf, &junk, &cursor); + if (result != DS_OK) { + IDirectSoundCaptureBuffer_Stop(*capturebuf); + IDirectSoundCaptureBuffer_Release(*capturebuf); + return SetDSerror("DirectSound GetCurrentPosition", result); + } + + this->hidden->lastchunk = cursor / this->spec.size; +#endif + + return 0; +} + +static int +DSOUND_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + const DWORD numchunks = 8; + HRESULT result; + SDL_bool valid_format = SDL_FALSE; + SDL_bool tried_format = SDL_FALSE; + SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); + LPGUID guid = (LPGUID) handle; + DWORD bufsize; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Open the audio device */ + if (iscapture) { + result = pDirectSoundCaptureCreate8(guid, &this->hidden->capture, NULL); + if (result != DS_OK) { + return SetDSerror("DirectSoundCaptureCreate8", result); + } + } else { + result = pDirectSoundCreate8(guid, &this->hidden->sound, NULL); + if (result != DS_OK) { + return SetDSerror("DirectSoundCreate8", result); + } + result = IDirectSound_SetCooperativeLevel(this->hidden->sound, + GetDesktopWindow(), + DSSCL_NORMAL); + if (result != DS_OK) { + return SetDSerror("DirectSound SetCooperativeLevel", result); + } + } + + while ((!valid_format) && (test_format)) { + switch (test_format) { + case AUDIO_U8: + case AUDIO_S16: + case AUDIO_S32: + case AUDIO_F32: + tried_format = SDL_TRUE; + + this->spec.format = test_format; + + /* Update the fragment size as size in bytes */ + SDL_CalculateAudioSpec(&this->spec); + + bufsize = numchunks * this->spec.size; + if ((bufsize < DSBSIZE_MIN) || (bufsize > DSBSIZE_MAX)) { + SDL_SetError("Sound buffer size must be between %d and %d", + (int) ((DSBSIZE_MIN < numchunks) ? 1 : DSBSIZE_MIN / numchunks), + (int) (DSBSIZE_MAX / numchunks)); + } else { + int rc; + WAVEFORMATEX wfmt; + SDL_zero(wfmt); + if (SDL_AUDIO_ISFLOAT(this->spec.format)) { + wfmt.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; + } else { + wfmt.wFormatTag = WAVE_FORMAT_PCM; + } + + wfmt.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format); + wfmt.nChannels = this->spec.channels; + wfmt.nSamplesPerSec = this->spec.freq; + wfmt.nBlockAlign = wfmt.nChannels * (wfmt.wBitsPerSample / 8); + wfmt.nAvgBytesPerSec = wfmt.nSamplesPerSec * wfmt.nBlockAlign; + + rc = iscapture ? CreateCaptureBuffer(this, bufsize, &wfmt) : CreateSecondary(this, bufsize, &wfmt); + if (rc == 0) { + this->hidden->num_buffers = numchunks; + valid_format = SDL_TRUE; + } + } + break; + } + test_format = SDL_NextAudioFormat(); + } + + if (!valid_format) { + if (tried_format) { + return -1; /* CreateSecondary() should have called SDL_SetError(). */ + } + return SDL_SetError("DirectSound: Unsupported audio format"); + } + + /* Playback buffers will auto-start playing in DSOUND_WaitDevice() */ + + return 0; /* good to go. */ +} + + +static void +DSOUND_Deinitialize(void) +{ + DSOUND_Unload(); +} + + +static int +DSOUND_Init(SDL_AudioDriverImpl * impl) +{ + if (!DSOUND_Load()) { + return 0; + } + + /* Set the function pointers */ + impl->DetectDevices = DSOUND_DetectDevices; + impl->OpenDevice = DSOUND_OpenDevice; + impl->PlayDevice = DSOUND_PlayDevice; + impl->WaitDevice = DSOUND_WaitDevice; + impl->GetDeviceBuf = DSOUND_GetDeviceBuf; + impl->CaptureFromDevice = DSOUND_CaptureFromDevice; + impl->FlushCapture = DSOUND_FlushCapture; + impl->CloseDevice = DSOUND_CloseDevice; + impl->FreeDeviceHandle = DSOUND_FreeDeviceHandle; + impl->Deinitialize = DSOUND_Deinitialize; + + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap DSOUND_bootstrap = { + "directsound", "DirectSound", DSOUND_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_DSOUND */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.h b/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.h new file mode 100644 index 0000000..acb7b6a --- /dev/null +++ b/ThirdParty/SDL2/src/audio/directsound/SDL_directsound.h @@ -0,0 +1,47 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_directsound_h_ +#define SDL_directsound_h_ + +#include "../../core/windows/SDL_directx.h" + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +/* The DirectSound objects */ +struct SDL_PrivateAudioData +{ + LPDIRECTSOUND sound; + LPDIRECTSOUNDBUFFER mixbuf; + LPDIRECTSOUNDCAPTURE capture; + LPDIRECTSOUNDCAPTUREBUFFER capturebuf; + int num_buffers; + DWORD lastchunk; + Uint8 *locked_buf; +}; + +#endif /* SDL_directsound_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.c b/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.c new file mode 100644 index 0000000..2250375 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.c @@ -0,0 +1,207 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_DISK + +/* Output raw audio data to a file. */ + +#if HAVE_STDIO_H +#include +#endif + +#include "SDL_rwops.h" +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_diskaudio.h" +#include "SDL_log.h" + +/* !!! FIXME: these should be SDL hints, not environment variables. */ +/* environment variables and defaults. */ +#define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE" +#define DISKDEFAULT_OUTFILE "sdlaudio.raw" +#define DISKENVR_INFILE "SDL_DISKAUDIOFILEIN" +#define DISKDEFAULT_INFILE "sdlaudio-in.raw" +#define DISKENVR_IODELAY "SDL_DISKAUDIODELAY" + +/* This function waits until it is possible to write a full sound buffer */ +static void +DISKAUDIO_WaitDevice(_THIS) +{ + SDL_Delay(this->hidden->io_delay); +} + +static void +DISKAUDIO_PlayDevice(_THIS) +{ + const size_t written = SDL_RWwrite(this->hidden->io, + this->hidden->mixbuf, + 1, this->spec.size); + + /* If we couldn't write, assume fatal error for now */ + if (written != this->spec.size) { + SDL_OpenedAudioDeviceDisconnected(this); + } +#ifdef DEBUG_AUDIO + fprintf(stderr, "Wrote %d bytes of audio data\n", written); +#endif +} + +static Uint8 * +DISKAUDIO_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + +static int +DISKAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + struct SDL_PrivateAudioData *h = this->hidden; + const int origbuflen = buflen; + + SDL_Delay(h->io_delay); + + if (h->io) { + const size_t br = SDL_RWread(h->io, buffer, 1, buflen); + buflen -= (int) br; + buffer = ((Uint8 *) buffer) + br; + if (buflen > 0) { /* EOF (or error, but whatever). */ + SDL_RWclose(h->io); + h->io = NULL; + } + } + + /* if we ran out of file, just write silence. */ + SDL_memset(buffer, this->spec.silence, buflen); + + return origbuflen; +} + +static void +DISKAUDIO_FlushCapture(_THIS) +{ + /* no op...we don't advance the file pointer or anything. */ +} + + +static void +DISKAUDIO_CloseDevice(_THIS) +{ + if (this->hidden->io != NULL) { + SDL_RWclose(this->hidden->io); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + + +static const char * +get_filename(const int iscapture, const char *devname) +{ + if (devname == NULL) { + devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE); + if (devname == NULL) { + devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE; + } + } + return devname; +} + +static int +DISKAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + /* handle != NULL means "user specified the placeholder name on the fake detected device list" */ + const char *fname = get_filename(iscapture, handle ? NULL : devname); + const char *envr = SDL_getenv(DISKENVR_IODELAY); + + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc(sizeof(*this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + if (envr != NULL) { + this->hidden->io_delay = SDL_atoi(envr); + } else { + this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq); + } + + /* Open the audio device */ + this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb"); + if (this->hidden->io == NULL) { + return -1; + } + + /* Allocate mixing buffer */ + if (!iscapture) { + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + } + + SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, + "You are using the SDL disk i/o audio driver!\n"); + SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, + " %s file [%s].\n", iscapture ? "Reading from" : "Writing to", + fname); + + /* We're ready to rock and roll. :-) */ + return 0; +} + +static void +DISKAUDIO_DetectDevices(void) +{ + SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) 0x1); + SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) 0x2); +} + +static int +DISKAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + /* Set the function pointers */ + impl->OpenDevice = DISKAUDIO_OpenDevice; + impl->WaitDevice = DISKAUDIO_WaitDevice; + impl->PlayDevice = DISKAUDIO_PlayDevice; + impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf; + impl->CaptureFromDevice = DISKAUDIO_CaptureFromDevice; + impl->FlushCapture = DISKAUDIO_FlushCapture; + + impl->CloseDevice = DISKAUDIO_CloseDevice; + impl->DetectDevices = DISKAUDIO_DetectDevices; + + impl->AllowsArbitraryDeviceNames = 1; + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap DISKAUDIO_bootstrap = { + "disk", "direct-to-disk audio", DISKAUDIO_Init, 1 +}; + +#endif /* SDL_AUDIO_DRIVER_DISK */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.h b/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.h new file mode 100644 index 0000000..7e73ebe --- /dev/null +++ b/ThirdParty/SDL2/src/audio/disk/SDL_diskaudio.h @@ -0,0 +1,41 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_diskaudio_h_ +#define SDL_diskaudio_h_ + +#include "SDL_rwops.h" +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The file descriptor for the audio device */ + SDL_RWops *io; + Uint32 io_delay; + Uint8 *mixbuf; +}; + +#endif /* SDL_diskaudio_h_ */ +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.c b/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.c new file mode 100644 index 0000000..77653be --- /dev/null +++ b/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.c @@ -0,0 +1,320 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_OSS + +/* Allow access to a raw mixing buffer */ + +#include /* For perror() */ +#include /* For strerror() */ +#include +#include +#include +#include +#include +#include +#include + +#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H +/* This is installed on some systems */ +#include +#else +/* This is recommended by OSS */ +#include +#endif + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "../SDL_audiodev_c.h" +#include "SDL_dspaudio.h" + + +static void +DSP_DetectDevices(void) +{ + SDL_EnumUnixAudioDevices(0, NULL); +} + + +static void +DSP_CloseDevice(_THIS) +{ + if (this->hidden->audio_fd >= 0) { + close(this->hidden->audio_fd); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + + +static int +DSP_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT); + int format; + int value; + int frag_spec; + SDL_AudioFormat test_format; + + /* We don't care what the devname is...we'll try to open anything. */ + /* ...but default to first name in the list... */ + if (devname == NULL) { + devname = SDL_GetAudioDeviceName(0, iscapture); + if (devname == NULL) { + return SDL_SetError("No such audio device"); + } + } + + /* Make sure fragment size stays a power of 2, or OSS fails. */ + /* I don't know which of these are actually legal values, though... */ + if (this->spec.channels > 8) + this->spec.channels = 8; + else if (this->spec.channels > 4) + this->spec.channels = 4; + else if (this->spec.channels > 2) + this->spec.channels = 2; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Open the audio device */ + this->hidden->audio_fd = open(devname, flags, 0); + if (this->hidden->audio_fd < 0) { + return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno)); + } + + /* Make the file descriptor use blocking i/o with fcntl() */ + { + long ctlflags; + ctlflags = fcntl(this->hidden->audio_fd, F_GETFL); + ctlflags &= ~O_NONBLOCK; + if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) { + return SDL_SetError("Couldn't set audio blocking mode"); + } + } + + /* Get a list of supported hardware formats */ + if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) { + perror("SNDCTL_DSP_GETFMTS"); + return SDL_SetError("Couldn't get audio format list"); + } + + /* Try for a closest match on audio format */ + format = 0; + for (test_format = SDL_FirstAudioFormat(this->spec.format); + !format && test_format;) { +#ifdef DEBUG_AUDIO + fprintf(stderr, "Trying format 0x%4.4x\n", test_format); +#endif + switch (test_format) { + case AUDIO_U8: + if (value & AFMT_U8) { + format = AFMT_U8; + } + break; + case AUDIO_S16LSB: + if (value & AFMT_S16_LE) { + format = AFMT_S16_LE; + } + break; + case AUDIO_S16MSB: + if (value & AFMT_S16_BE) { + format = AFMT_S16_BE; + } + break; +#if 0 +/* + * These formats are not used by any real life systems so they are not + * needed here. + */ + case AUDIO_S8: + if (value & AFMT_S8) { + format = AFMT_S8; + } + break; + case AUDIO_U16LSB: + if (value & AFMT_U16_LE) { + format = AFMT_U16_LE; + } + break; + case AUDIO_U16MSB: + if (value & AFMT_U16_BE) { + format = AFMT_U16_BE; + } + break; +#endif + default: + format = 0; + break; + } + if (!format) { + test_format = SDL_NextAudioFormat(); + } + } + if (format == 0) { + return SDL_SetError("Couldn't find any hardware audio formats"); + } + this->spec.format = test_format; + + /* Set the audio format */ + value = format; + if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || + (value != format)) { + perror("SNDCTL_DSP_SETFMT"); + return SDL_SetError("Couldn't set audio format"); + } + + /* Set the number of channels of output */ + value = this->spec.channels; + if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) { + perror("SNDCTL_DSP_CHANNELS"); + return SDL_SetError("Cannot set the number of channels"); + } + this->spec.channels = value; + + /* Set the DSP frequency */ + value = this->spec.freq; + if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) { + perror("SNDCTL_DSP_SPEED"); + return SDL_SetError("Couldn't set audio frequency"); + } + this->spec.freq = value; + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + /* Determine the power of two of the fragment size */ + for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec); + if ((0x01U << frag_spec) != this->spec.size) { + return SDL_SetError("Fragment size must be a power of two"); + } + frag_spec |= 0x00020000; /* two fragments, for low latency */ + + /* Set the audio buffering parameters */ +#ifdef DEBUG_AUDIO + fprintf(stderr, "Requesting %d fragments of size %d\n", + (frag_spec >> 16), 1 << (frag_spec & 0xFFFF)); +#endif + if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) { + perror("SNDCTL_DSP_SETFRAGMENT"); + } +#ifdef DEBUG_AUDIO + { + audio_buf_info info; + ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info); + fprintf(stderr, "fragments = %d\n", info.fragments); + fprintf(stderr, "fragstotal = %d\n", info.fragstotal); + fprintf(stderr, "fragsize = %d\n", info.fragsize); + fprintf(stderr, "bytes = %d\n", info.bytes); + } +#endif + + /* Allocate mixing buffer */ + if (!iscapture) { + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + } + + /* We're ready to rock and roll. :-) */ + return 0; +} + + +static void +DSP_PlayDevice(_THIS) +{ + struct SDL_PrivateAudioData *h = this->hidden; + if (write(h->audio_fd, h->mixbuf, h->mixlen) == -1) { + perror("Audio write"); + SDL_OpenedAudioDeviceDisconnected(this); + } +#ifdef DEBUG_AUDIO + fprintf(stderr, "Wrote %d bytes of audio data\n", h->mixlen); +#endif +} + +static Uint8 * +DSP_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + +static int +DSP_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + return (int) read(this->hidden->audio_fd, buffer, buflen); +} + +static void +DSP_FlushCapture(_THIS) +{ + struct SDL_PrivateAudioData *h = this->hidden; + audio_buf_info info; + if (ioctl(h->audio_fd, SNDCTL_DSP_GETISPACE, &info) == 0) { + while (info.bytes > 0) { + char buf[512]; + const size_t len = SDL_min(sizeof (buf), info.bytes); + const ssize_t br = read(h->audio_fd, buf, len); + if (br <= 0) { + break; + } + info.bytes -= br; + } + } +} + +static int +DSP_Init(SDL_AudioDriverImpl * impl) +{ + /* Set the function pointers */ + impl->DetectDevices = DSP_DetectDevices; + impl->OpenDevice = DSP_OpenDevice; + impl->PlayDevice = DSP_PlayDevice; + impl->GetDeviceBuf = DSP_GetDeviceBuf; + impl->CloseDevice = DSP_CloseDevice; + impl->CaptureFromDevice = DSP_CaptureFromDevice; + impl->FlushCapture = DSP_FlushCapture; + + impl->AllowsArbitraryDeviceNames = 1; + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + + +AudioBootStrap DSP_bootstrap = { + "dsp", "OSS /dev/dsp standard audio", DSP_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_OSS */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.h b/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.h new file mode 100644 index 0000000..6bd86d7 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/dsp/SDL_dspaudio.h @@ -0,0 +1,43 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_dspaudio_h_ +#define SDL_dspaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The file descriptor for the audio device */ + int audio_fd; + + /* Raw mixing buffer */ + Uint8 *mixbuf; + int mixlen; +}; +#define FUDGE_TICKS 10 /* The scheduler overhead ticks per frame */ + +#endif /* SDL_dspaudio_h_ */ +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.c b/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.c new file mode 100644 index 0000000..f91dea3 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.c @@ -0,0 +1,65 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +/* Output audio to nowhere... */ + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_dummyaudio.h" + +static int +DUMMYAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + return 0; /* always succeeds. */ +} + +static int +DUMMYAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + /* Delay to make this sort of simulate real audio input. */ + SDL_Delay((this->spec.samples * 1000) / this->spec.freq); + + /* always return a full buffer of silence. */ + SDL_memset(buffer, this->spec.silence, buflen); + return buflen; +} + +static int +DUMMYAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + /* Set the function pointers */ + impl->OpenDevice = DUMMYAUDIO_OpenDevice; + impl->CaptureFromDevice = DUMMYAUDIO_CaptureFromDevice; + + impl->OnlyHasDefaultOutputDevice = 1; + impl->OnlyHasDefaultCaptureDevice = 1; + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap DUMMYAUDIO_bootstrap = { + "dummy", "SDL dummy audio driver", DUMMYAUDIO_Init, 1 +}; + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.h b/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.h new file mode 100644 index 0000000..18241ee --- /dev/null +++ b/ThirdParty/SDL2/src/audio/dummy/SDL_dummyaudio.h @@ -0,0 +1,41 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_dummyaudio_h_ +#define SDL_dummyaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The file descriptor for the audio device */ + Uint8 *mixbuf; + Uint32 mixlen; + Uint32 write_delay; + Uint32 initial_calls; +}; + +#endif /* SDL_dummyaudio_h_ */ +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.c b/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.c new file mode 100644 index 0000000..e519f08 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.c @@ -0,0 +1,379 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_EMSCRIPTEN + +#include "SDL_audio.h" +#include "SDL_log.h" +#include "../SDL_audio_c.h" +#include "SDL_emscriptenaudio.h" +#include "SDL_assert.h" + +#include + +static void +FeedAudioDevice(_THIS, const void *buf, const int buflen) +{ + const int framelen = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels; + EM_ASM_ARGS({ + var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels']; + for (var c = 0; c < numChannels; ++c) { + var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c); + if (channelData.length != $1) { + throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!'; + } + + for (var j = 0; j < $1; ++j) { + channelData[j] = HEAPF32[$0 + ((j*numChannels + c) << 2) >> 2]; /* !!! FIXME: why are these shifts here? */ + } + } + }, buf, buflen / framelen); +} + +static void +HandleAudioProcess(_THIS) +{ + SDL_AudioCallback callback = this->callbackspec.callback; + const int stream_len = this->callbackspec.size; + + /* Only do something if audio is enabled */ + if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) { + if (this->stream) { + SDL_AudioStreamClear(this->stream); + } + return; + } + + if (this->stream == NULL) { /* no conversion necessary. */ + SDL_assert(this->spec.size == stream_len); + callback(this->callbackspec.userdata, this->work_buffer, stream_len); + } else { /* streaming/converting */ + int got; + while (SDL_AudioStreamAvailable(this->stream) < ((int) this->spec.size)) { + callback(this->callbackspec.userdata, this->work_buffer, stream_len); + if (SDL_AudioStreamPut(this->stream, this->work_buffer, stream_len) == -1) { + SDL_AudioStreamClear(this->stream); + SDL_AtomicSet(&this->enabled, 0); + break; + } + } + + got = SDL_AudioStreamGet(this->stream, this->work_buffer, this->spec.size); + SDL_assert((got < 0) || (got == this->spec.size)); + if (got != this->spec.size) { + SDL_memset(this->work_buffer, this->spec.silence, this->spec.size); + } + } + + FeedAudioDevice(this, this->work_buffer, this->spec.size); +} + +static void +HandleCaptureProcess(_THIS) +{ + SDL_AudioCallback callback = this->callbackspec.callback; + const int stream_len = this->callbackspec.size; + + /* Only do something if audio is enabled */ + if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) { + SDL_AudioStreamClear(this->stream); + return; + } + + EM_ASM_ARGS({ + var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels; + for (var c = 0; c < numChannels; ++c) { + var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c); + if (channelData.length != $1) { + throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!'; + } + + if (numChannels == 1) { /* fastpath this a little for the common (mono) case. */ + for (var j = 0; j < $1; ++j) { + setValue($0 + (j * 4), channelData[j], 'float'); + } + } else { + for (var j = 0; j < $1; ++j) { + setValue($0 + (((j * numChannels) + c) * 4), channelData[j], 'float'); + } + } + } + }, this->work_buffer, (this->spec.size / sizeof (float)) / this->spec.channels); + + /* okay, we've got an interleaved float32 array in C now. */ + + if (this->stream == NULL) { /* no conversion necessary. */ + SDL_assert(this->spec.size == stream_len); + callback(this->callbackspec.userdata, this->work_buffer, stream_len); + } else { /* streaming/converting */ + if (SDL_AudioStreamPut(this->stream, this->work_buffer, this->spec.size) == -1) { + SDL_AtomicSet(&this->enabled, 0); + } + + while (SDL_AudioStreamAvailable(this->stream) >= stream_len) { + const int got = SDL_AudioStreamGet(this->stream, this->work_buffer, stream_len); + SDL_assert((got < 0) || (got == stream_len)); + if (got != stream_len) { + SDL_memset(this->work_buffer, this->callbackspec.silence, stream_len); + } + callback(this->callbackspec.userdata, this->work_buffer, stream_len); /* Send it to the app. */ + } + } +} + + +static void +EMSCRIPTENAUDIO_CloseDevice(_THIS) +{ + EM_ASM_({ + if ($0) { + if (SDL2.capture.silenceTimer !== undefined) { + clearTimeout(SDL2.capture.silenceTimer); + } + if (SDL2.capture.stream !== undefined) { + var tracks = SDL2.capture.stream.getAudioTracks(); + for (var i = 0; i < tracks.length; i++) { + SDL2.capture.stream.removeTrack(tracks[i]); + } + SDL2.capture.stream = undefined; + } + if (SDL2.capture.scriptProcessorNode !== undefined) { + SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {}; + SDL2.capture.scriptProcessorNode.disconnect(); + SDL2.capture.scriptProcessorNode = undefined; + } + if (SDL2.capture.mediaStreamNode !== undefined) { + SDL2.capture.mediaStreamNode.disconnect(); + SDL2.capture.mediaStreamNode = undefined; + } + if (SDL2.capture.silenceBuffer !== undefined) { + SDL2.capture.silenceBuffer = undefined + } + SDL2.capture = undefined; + } else { + if (SDL2.audio.scriptProcessorNode != undefined) { + SDL2.audio.scriptProcessorNode.disconnect(); + SDL2.audio.scriptProcessorNode = undefined; + } + SDL2.audio = undefined; + } + if ((SDL2.audioContext !== undefined) && (SDL2.audio === undefined) && (SDL2.capture === undefined)) { + SDL2.audioContext.close(); + SDL2.audioContext = undefined; + } + }, this->iscapture); + +#if 0 /* !!! FIXME: currently not used. Can we move some stuff off the SDL2 namespace? --ryan. */ + SDL_free(this->hidden); +#endif +} + +static int +EMSCRIPTENAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + SDL_bool valid_format = SDL_FALSE; + SDL_AudioFormat test_format; + int result; + + /* based on parts of library_sdl.js */ + + /* create context (TODO: this puts stuff in the global namespace...)*/ + result = EM_ASM_INT({ + if(typeof(SDL2) === 'undefined') { + SDL2 = {}; + } + if (!$0) { + SDL2.audio = {}; + } else { + SDL2.capture = {}; + } + + if (!SDL2.audioContext) { + if (typeof(AudioContext) !== 'undefined') { + SDL2.audioContext = new AudioContext(); + } else if (typeof(webkitAudioContext) !== 'undefined') { + SDL2.audioContext = new webkitAudioContext(); + } + } + return SDL2.audioContext === undefined ? -1 : 0; + }, iscapture); + if (result < 0) { + return SDL_SetError("Web Audio API is not available!"); + } + + test_format = SDL_FirstAudioFormat(this->spec.format); + while ((!valid_format) && (test_format)) { + switch (test_format) { + case AUDIO_F32: /* web audio only supports floats */ + this->spec.format = test_format; + + valid_format = SDL_TRUE; + break; + } + test_format = SDL_NextAudioFormat(); + } + + if (!valid_format) { + /* Didn't find a compatible format :( */ + return SDL_SetError("No compatible audio format!"); + } + + /* Initialize all variables that we clean on shutdown */ +#if 0 /* !!! FIXME: currently not used. Can we move some stuff off the SDL2 namespace? --ryan. */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); +#endif + + /* limit to native freq */ + this->spec.freq = EM_ASM_INT_V({ return SDL2.audioContext.sampleRate; }); + + SDL_CalculateAudioSpec(&this->spec); + + if (iscapture) { + /* The idea is to take the capture media stream, hook it up to an + audio graph where we can pass it through a ScriptProcessorNode + to access the raw PCM samples and push them to the SDL app's + callback. From there, we "process" the audio data into silence + and forget about it. */ + + /* This should, strictly speaking, use MediaRecorder for capture, but + this API is cleaner to use and better supported, and fires a + callback whenever there's enough data to fire down into the app. + The downside is that we are spending CPU time silencing a buffer + that the audiocontext uselessly mixes into any output. On the + upside, both of those things are not only run in native code in + the browser, they're probably SIMD code, too. MediaRecorder + feels like it's a pretty inefficient tapdance in similar ways, + to be honest. */ + + EM_ASM_({ + var have_microphone = function(stream) { + //console.log('SDL audio capture: we have a microphone! Replacing silence callback.'); + if (SDL2.capture.silenceTimer !== undefined) { + clearTimeout(SDL2.capture.silenceTimer); + SDL2.capture.silenceTimer = undefined; + } + SDL2.capture.mediaStreamNode = SDL2.audioContext.createMediaStreamSource(stream); + SDL2.capture.scriptProcessorNode = SDL2.audioContext.createScriptProcessor($1, $0, 1); + SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) { + if ((SDL2 === undefined) || (SDL2.capture === undefined)) { return; } + audioProcessingEvent.outputBuffer.getChannelData(0).fill(0.0); + SDL2.capture.currentCaptureBuffer = audioProcessingEvent.inputBuffer; + Runtime.dynCall('vi', $2, [$3]); + }; + SDL2.capture.mediaStreamNode.connect(SDL2.capture.scriptProcessorNode); + SDL2.capture.scriptProcessorNode.connect(SDL2.audioContext.destination); + SDL2.capture.stream = stream; + }; + + var no_microphone = function(error) { + //console.log('SDL audio capture: we DO NOT have a microphone! (' + error.name + ')...leaving silence callback running.'); + }; + + /* we write silence to the audio callback until the microphone is available (user approves use, etc). */ + SDL2.capture.silenceBuffer = SDL2.audioContext.createBuffer($0, $1, SDL2.audioContext.sampleRate); + SDL2.capture.silenceBuffer.getChannelData(0).fill(0.0); + var silence_callback = function() { + SDL2.capture.currentCaptureBuffer = SDL2.capture.silenceBuffer; + Runtime.dynCall('vi', $2, [$3]); + }; + + SDL2.capture.silenceTimer = setTimeout(silence_callback, ($1 / SDL2.audioContext.sampleRate) * 1000); + + if ((navigator.mediaDevices !== undefined) && (navigator.mediaDevices.getUserMedia !== undefined)) { + navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(have_microphone).catch(no_microphone); + } else if (navigator.webkitGetUserMedia !== undefined) { + navigator.webkitGetUserMedia({ audio: true, video: false }, have_microphone, no_microphone); + } + }, this->spec.channels, this->spec.samples, HandleCaptureProcess, this); + } else { + /* setup a ScriptProcessorNode */ + EM_ASM_ARGS({ + SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0); + SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) { + if ((SDL2 === undefined) || (SDL2.audio === undefined)) { return; } + SDL2.audio.currentOutputBuffer = e['outputBuffer']; + Runtime.dynCall('vi', $2, [$3]); + }; + SDL2.audio.scriptProcessorNode['connect'](SDL2.audioContext['destination']); + }, this->spec.channels, this->spec.samples, HandleAudioProcess, this); + } + + return 0; +} + +static int +EMSCRIPTENAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + int available; + int capture_available; + + /* Set the function pointers */ + impl->OpenDevice = EMSCRIPTENAUDIO_OpenDevice; + impl->CloseDevice = EMSCRIPTENAUDIO_CloseDevice; + + impl->OnlyHasDefaultOutputDevice = 1; + + /* no threads here */ + impl->SkipMixerLock = 1; + impl->ProvidesOwnCallbackThread = 1; + + /* check availability */ + available = EM_ASM_INT_V({ + if (typeof(AudioContext) !== 'undefined') { + return 1; + } else if (typeof(webkitAudioContext) !== 'undefined') { + return 1; + } + return 0; + }); + + if (!available) { + SDL_SetError("No audio context available"); + } + + capture_available = available && EM_ASM_INT_V({ + if ((typeof(navigator.mediaDevices) !== 'undefined') && (typeof(navigator.mediaDevices.getUserMedia) !== 'undefined')) { + return 1; + } else if (typeof(navigator.webkitGetUserMedia) !== 'undefined') { + return 1; + } + return 0; + }); + + impl->HasCaptureSupport = capture_available ? SDL_TRUE : SDL_FALSE; + impl->OnlyHasDefaultCaptureDevice = capture_available ? SDL_TRUE : SDL_FALSE; + + return available; +} + +AudioBootStrap EMSCRIPTENAUDIO_bootstrap = { + "emscripten", "SDL emscripten audio driver", EMSCRIPTENAUDIO_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_EMSCRIPTEN */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.h b/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.h new file mode 100644 index 0000000..3c95668 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/emscripten/SDL_emscriptenaudio.h @@ -0,0 +1,38 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_emscriptenaudio_h_ +#define SDL_emscriptenaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + int unused; +}; + +#endif /* SDL_emscriptenaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.c b/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.c new file mode 100644 index 0000000..802ea78 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.c @@ -0,0 +1,335 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_ESD + +/* Allow access to an ESD network stream mixing buffer */ + +#include +#include +#include +#include +#include + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_esdaudio.h" + +#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC +#include "SDL_name.h" +#include "SDL_loadso.h" +#else +#define SDL_NAME(X) X +#endif + +#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC + +static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC; +static void *esd_handle = NULL; + +static int (*SDL_NAME(esd_open_sound)) (const char *host); +static int (*SDL_NAME(esd_close)) (int esd); +static int (*SDL_NAME(esd_play_stream)) (esd_format_t format, int rate, + const char *host, const char *name); + +#define SDL_ESD_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) } +static struct +{ + const char *name; + void **func; +} const esd_functions[] = { + SDL_ESD_SYM(esd_open_sound), + SDL_ESD_SYM(esd_close), SDL_ESD_SYM(esd_play_stream), +}; + +#undef SDL_ESD_SYM + +static void +UnloadESDLibrary() +{ + if (esd_handle != NULL) { + SDL_UnloadObject(esd_handle); + esd_handle = NULL; + } +} + +static int +LoadESDLibrary(void) +{ + int i, retval = -1; + + if (esd_handle == NULL) { + esd_handle = SDL_LoadObject(esd_library); + if (esd_handle) { + retval = 0; + for (i = 0; i < SDL_arraysize(esd_functions); ++i) { + *esd_functions[i].func = + SDL_LoadFunction(esd_handle, esd_functions[i].name); + if (!*esd_functions[i].func) { + retval = -1; + UnloadESDLibrary(); + break; + } + } + } + } + return retval; +} + +#else + +static void +UnloadESDLibrary() +{ + return; +} + +static int +LoadESDLibrary(void) +{ + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */ + + +/* This function waits until it is possible to write a full sound buffer */ +static void +ESD_WaitDevice(_THIS) +{ + Sint32 ticks; + + /* Check to see if the thread-parent process is still alive */ + { + static int cnt = 0; + /* Note that this only works with thread implementations + that use a different process id for each thread. + */ + /* Check every 10 loops */ + if (this->hidden->parent && (((++cnt) % 10) == 0)) { + if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) { + SDL_OpenedAudioDeviceDisconnected(this); + } + } + } + + /* Use timer for general audio synchronization */ + ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS; + if (ticks > 0) { + SDL_Delay(ticks); + } +} + +static void +ESD_PlayDevice(_THIS) +{ + int written = 0; + + /* Write the audio data, checking for EAGAIN on broken audio drivers */ + do { + written = write(this->hidden->audio_fd, + this->hidden->mixbuf, this->hidden->mixlen); + if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) { + SDL_Delay(1); /* Let a little CPU time go by */ + } + } while ((written < 0) && + ((errno == 0) || (errno == EAGAIN) || (errno == EINTR))); + + /* Set the next write frame */ + this->hidden->next_frame += this->hidden->frame_ticks; + + /* If we couldn't write, assume fatal error for now */ + if (written < 0) { + SDL_OpenedAudioDeviceDisconnected(this); + } +} + +static Uint8 * +ESD_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + +static void +ESD_CloseDevice(_THIS) +{ + if (this->hidden->audio_fd >= 0) { + SDL_NAME(esd_close) (this->hidden->audio_fd); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + +/* Try to get the name of the program */ +static char * +get_progname(void) +{ + char *progname = NULL; +#ifdef __LINUX__ + FILE *fp; + static char temp[BUFSIZ]; + + SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid()); + fp = fopen(temp, "r"); + if (fp != NULL) { + if (fgets(temp, sizeof(temp) - 1, fp)) { + progname = SDL_strrchr(temp, '/'); + if (progname == NULL) { + progname = temp; + } else { + progname = progname + 1; + } + } + fclose(fp); + } +#endif + return (progname); +} + + +static int +ESD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + esd_format_t format = (ESD_STREAM | ESD_PLAY); + SDL_AudioFormat test_format = 0; + int found = 0; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + this->hidden->audio_fd = -1; + + /* Convert audio spec to the ESD audio format */ + /* Try for a closest match on audio format */ + for (test_format = SDL_FirstAudioFormat(this->spec.format); + !found && test_format; test_format = SDL_NextAudioFormat()) { +#ifdef DEBUG_AUDIO + fprintf(stderr, "Trying format 0x%4.4x\n", test_format); +#endif + found = 1; + switch (test_format) { + case AUDIO_U8: + format |= ESD_BITS8; + break; + case AUDIO_S16SYS: + format |= ESD_BITS16; + break; + default: + found = 0; + break; + } + } + + if (!found) { + return SDL_SetError("Couldn't find any hardware audio formats"); + } + + if (this->spec.channels == 1) { + format |= ESD_MONO; + } else { + format |= ESD_STEREO; + } +#if 0 + this->spec.samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */ +#endif + + /* Open a connection to the ESD audio server */ + this->hidden->audio_fd = + SDL_NAME(esd_play_stream) (format, this->spec.freq, NULL, + get_progname()); + + if (this->hidden->audio_fd < 0) { + return SDL_SetError("Couldn't open ESD connection"); + } + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + this->hidden->frame_ticks = + (float) (this->spec.samples * 1000) / this->spec.freq; + this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks; + + /* Allocate mixing buffer */ + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + + /* Get the parent process id (we're the parent of the audio thread) */ + this->hidden->parent = getpid(); + + /* We're ready to rock and roll. :-) */ + return 0; +} + +static void +ESD_Deinitialize(void) +{ + UnloadESDLibrary(); +} + +static int +ESD_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadESDLibrary() < 0) { + return 0; + } else { + int connection = 0; + + /* Don't start ESD if it's not running */ + SDL_setenv("ESD_NO_SPAWN", "1", 0); + + connection = SDL_NAME(esd_open_sound) (NULL); + if (connection < 0) { + UnloadESDLibrary(); + SDL_SetError("ESD: esd_open_sound failed (no audio server?)"); + return 0; + } + SDL_NAME(esd_close) (connection); + } + + /* Set the function pointers */ + impl->OpenDevice = ESD_OpenDevice; + impl->PlayDevice = ESD_PlayDevice; + impl->WaitDevice = ESD_WaitDevice; + impl->GetDeviceBuf = ESD_GetDeviceBuf; + impl->CloseDevice = ESD_CloseDevice; + impl->Deinitialize = ESD_Deinitialize; + impl->OnlyHasDefaultOutputDevice = 1; + + return 1; /* this audio target is available. */ +} + + +AudioBootStrap ESD_bootstrap = { + "esd", "Enlightened Sound Daemon", ESD_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_ESD */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.h b/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.h new file mode 100644 index 0000000..9b5c25a --- /dev/null +++ b/ThirdParty/SDL2/src/audio/esd/SDL_esdaudio.h @@ -0,0 +1,51 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_esdaudio_h_ +#define SDL_esdaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* The file descriptor for the audio device */ + int audio_fd; + + /* The parent process id, to detect when application quits */ + pid_t parent; + + /* Raw mixing buffer */ + Uint8 *mixbuf; + int mixlen; + + /* Support for audio timing using a timer */ + float frame_ticks; + float next_frame; +}; +#define FUDGE_TICKS 10 /* The scheduler overhead ticks per frame */ + +#endif /* SDL_esdaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.c b/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.c new file mode 100644 index 0000000..36fa5c5 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.c @@ -0,0 +1,328 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_FUSIONSOUND + +/* !!! FIXME: why is this is SDL_FS_* instead of FUSIONSOUND_*? */ + +/* Allow access to a raw mixing buffer */ + +#ifdef HAVE_SIGNAL_H +#include +#endif +#include + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_fsaudio.h" + +#include + +/* #define SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "libfusionsound.so" */ + +#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC +#include "SDL_name.h" +#include "SDL_loadso.h" +#else +#define SDL_NAME(X) X +#endif + +#if (FUSIONSOUND_MAJOR_VERSION == 1) && (FUSIONSOUND_MINOR_VERSION < 1) +typedef DFBResult DirectResult; +#endif + +/* Buffers to use - more than 2 gives a lot of latency */ +#define FUSION_BUFFERS (2) + +#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC + +static const char *fs_library = SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC; +static void *fs_handle = NULL; + +static DirectResult (*SDL_NAME(FusionSoundInit)) (int *argc, char *(*argv[])); +static DirectResult (*SDL_NAME(FusionSoundCreate)) (IFusionSound ** + ret_interface); + +#define SDL_FS_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) } +static struct +{ + const char *name; + void **func; +} fs_functions[] = { +/* *INDENT-OFF* */ + SDL_FS_SYM(FusionSoundInit), + SDL_FS_SYM(FusionSoundCreate), +/* *INDENT-ON* */ +}; + +#undef SDL_FS_SYM + +static void +UnloadFusionSoundLibrary() +{ + if (fs_handle != NULL) { + SDL_UnloadObject(fs_handle); + fs_handle = NULL; + } +} + +static int +LoadFusionSoundLibrary(void) +{ + int i, retval = -1; + + if (fs_handle == NULL) { + fs_handle = SDL_LoadObject(fs_library); + if (fs_handle != NULL) { + retval = 0; + for (i = 0; i < SDL_arraysize(fs_functions); ++i) { + *fs_functions[i].func = + SDL_LoadFunction(fs_handle, fs_functions[i].name); + if (!*fs_functions[i].func) { + retval = -1; + UnloadFusionSoundLibrary(); + break; + } + } + } + } + + return retval; +} + +#else + +static void +UnloadFusionSoundLibrary() +{ + return; +} + +static int +LoadFusionSoundLibrary(void) +{ + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC */ + +/* This function waits until it is possible to write a full sound buffer */ +static void +SDL_FS_WaitDevice(_THIS) +{ + this->hidden->stream->Wait(this->hidden->stream, + this->hidden->mixsamples); +} + +static void +SDL_FS_PlayDevice(_THIS) +{ + DirectResult ret; + + ret = this->hidden->stream->Write(this->hidden->stream, + this->hidden->mixbuf, + this->hidden->mixsamples); + /* If we couldn't write, assume fatal error for now */ + if (ret) { + SDL_OpenedAudioDeviceDisconnected(this); + } +#ifdef DEBUG_AUDIO + fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen); +#endif +} + + +static Uint8 * +SDL_FS_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + + +static void +SDL_FS_CloseDevice(_THIS) +{ + if (this->hidden->stream) { + this->hidden->stream->Release(this->hidden->stream); + } + if (this->hidden->fs) { + this->hidden->fs->Release(this->hidden->fs); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + + +static int +SDL_FS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + int bytes; + SDL_AudioFormat test_format = 0, format = 0; + FSSampleFormat fs_format; + FSStreamDescription desc; + DirectResult ret; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Try for a closest match on audio format */ + for (test_format = SDL_FirstAudioFormat(this->spec.format); + !format && test_format;) { +#ifdef DEBUG_AUDIO + fprintf(stderr, "Trying format 0x%4.4x\n", test_format); +#endif + switch (test_format) { + case AUDIO_U8: + fs_format = FSSF_U8; + bytes = 1; + format = 1; + break; + case AUDIO_S16SYS: + fs_format = FSSF_S16; + bytes = 2; + format = 1; + break; + case AUDIO_S32SYS: + fs_format = FSSF_S32; + bytes = 4; + format = 1; + break; + case AUDIO_F32SYS: + fs_format = FSSF_FLOAT; + bytes = 4; + format = 1; + break; + default: + format = 0; + break; + } + if (!format) { + test_format = SDL_NextAudioFormat(); + } + } + + if (format == 0) { + return SDL_SetError("Couldn't find any hardware audio formats"); + } + this->spec.format = test_format; + + /* Retrieve the main sound interface. */ + ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs); + if (ret) { + return SDL_SetError("Unable to initialize FusionSound: %d", ret); + } + + this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels; + + /* Fill stream description. */ + desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | + FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER; + desc.samplerate = this->spec.freq; + desc.buffersize = this->spec.size * FUSION_BUFFERS; + desc.channels = this->spec.channels; + desc.prebuffer = 10; + desc.sampleformat = fs_format; + + ret = + this->hidden->fs->CreateStream(this->hidden->fs, &desc, + &this->hidden->stream); + if (ret) { + return SDL_SetError("Unable to create FusionSoundStream: %d", ret); + } + + /* See what we got */ + desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | + FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT; + ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc); + + this->spec.freq = desc.samplerate; + this->spec.size = + desc.buffersize / FUSION_BUFFERS * bytes * desc.channels; + this->spec.channels = desc.channels; + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + /* Allocate mixing buffer */ + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + + /* We're ready to rock and roll. :-) */ + return 0; +} + + +static void +SDL_FS_Deinitialize(void) +{ + UnloadFusionSoundLibrary(); +} + + +static int +SDL_FS_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadFusionSoundLibrary() < 0) { + return 0; + } else { + DirectResult ret; + + ret = SDL_NAME(FusionSoundInit) (NULL, NULL); + if (ret) { + UnloadFusionSoundLibrary(); + SDL_SetError + ("FusionSound: SDL_FS_init failed (FusionSoundInit: %d)", + ret); + return 0; + } + } + + /* Set the function pointers */ + impl->OpenDevice = SDL_FS_OpenDevice; + impl->PlayDevice = SDL_FS_PlayDevice; + impl->WaitDevice = SDL_FS_WaitDevice; + impl->GetDeviceBuf = SDL_FS_GetDeviceBuf; + impl->CloseDevice = SDL_FS_CloseDevice; + impl->Deinitialize = SDL_FS_Deinitialize; + impl->OnlyHasDefaultOutputDevice = 1; + + return 1; /* this audio target is available. */ +} + + +AudioBootStrap FUSIONSOUND_bootstrap = { + "fusionsound", "FusionSound", SDL_FS_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.h b/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.h new file mode 100644 index 0000000..27e45ce --- /dev/null +++ b/ThirdParty/SDL2/src/audio/fusionsound/SDL_fsaudio.h @@ -0,0 +1,50 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_fsaudio_h_ +#define SDL_fsaudio_h_ + +#include + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + /* Interface */ + IFusionSound *fs; + + /* The stream interface for the audio device */ + IFusionSoundStream *stream; + + /* Raw mixing buffer */ + Uint8 *mixbuf; + int mixlen; + int mixsamples; + +}; + +#endif /* SDL_fsaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.cc b/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.cc new file mode 100644 index 0000000..52946a5 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.cc @@ -0,0 +1,248 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_HAIKU + +/* Allow access to the audio stream on Haiku */ + +#include +#include + +#include "../../main/haiku/SDL_BeApp.h" + +extern "C" +{ + +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "../SDL_sysaudio.h" +#include "SDL_haikuaudio.h" +#include "SDL_assert.h" + +} + + +/* !!! FIXME: have the callback call the higher level to avoid code dupe. */ +/* The Haiku callback for handling the audio buffer */ +static void +FillSound(void *device, void *stream, size_t len, + const media_raw_audio_format & format) +{ + SDL_AudioDevice *audio = (SDL_AudioDevice *) device; + SDL_AudioCallback callback = audio->callbackspec.callback; + + /* Only do something if audio is enabled */ + if (!SDL_AtomicGet(&audio->enabled) || SDL_AtomicGet(&audio->paused)) { + if (audio->stream) { + SDL_AudioStreamClear(audio->stream); + } + SDL_memset(stream, audio->spec.silence, len); + return; + } + + SDL_assert(audio->spec.size == len); + + if (audio->stream == NULL) { /* no conversion necessary. */ + SDL_LockMutex(audio->mixer_lock); + callback(audio->callbackspec.userdata, (Uint8 *) stream, len); + SDL_UnlockMutex(audio->mixer_lock); + } else { /* streaming/converting */ + const int stream_len = audio->callbackspec.size; + const int ilen = (int) len; + while (SDL_AudioStreamAvailable(audio->stream) < ilen) { + callback(audio->callbackspec.userdata, audio->work_buffer, stream_len); + if (SDL_AudioStreamPut(audio->stream, audio->work_buffer, stream_len) == -1) { + SDL_AudioStreamClear(audio->stream); + SDL_AtomicSet(&audio->enabled, 0); + break; + } + } + + const int got = SDL_AudioStreamGet(audio->stream, stream, ilen); + SDL_assert((got < 0) || (got == ilen)); + if (got != ilen) { + SDL_memset(stream, audio->spec.silence, len); + } + } +} + +static void +HAIKUAUDIO_CloseDevice(_THIS) +{ + if (_this->hidden->audio_obj) { + _this->hidden->audio_obj->Stop(); + delete _this->hidden->audio_obj; + } + delete _this->hidden; +} + + +static const int sig_list[] = { + SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGWINCH, 0 +}; + +static inline void +MaskSignals(sigset_t * omask) +{ + sigset_t mask; + int i; + + sigemptyset(&mask); + for (i = 0; sig_list[i]; ++i) { + sigaddset(&mask, sig_list[i]); + } + sigprocmask(SIG_BLOCK, &mask, omask); +} + +static inline void +UnmaskSignals(sigset_t * omask) +{ + sigprocmask(SIG_SETMASK, omask, NULL); +} + + +static int +HAIKUAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + int valid_datatype = 0; + media_raw_audio_format format; + SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format); + + /* Initialize all variables that we clean on shutdown */ + _this->hidden = new SDL_PrivateAudioData; + if (_this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(_this->hidden); + + /* Parse the audio format and fill the Be raw audio format */ + SDL_zero(format); + format.byte_order = B_MEDIA_LITTLE_ENDIAN; + format.frame_rate = (float) _this->spec.freq; + format.channel_count = _this->spec.channels; /* !!! FIXME: support > 2? */ + while ((!valid_datatype) && (test_format)) { + valid_datatype = 1; + _this->spec.format = test_format; + switch (test_format) { + case AUDIO_S8: + format.format = media_raw_audio_format::B_AUDIO_CHAR; + break; + + case AUDIO_U8: + format.format = media_raw_audio_format::B_AUDIO_UCHAR; + break; + + case AUDIO_S16LSB: + format.format = media_raw_audio_format::B_AUDIO_SHORT; + break; + + case AUDIO_S16MSB: + format.format = media_raw_audio_format::B_AUDIO_SHORT; + format.byte_order = B_MEDIA_BIG_ENDIAN; + break; + + case AUDIO_S32LSB: + format.format = media_raw_audio_format::B_AUDIO_INT; + break; + + case AUDIO_S32MSB: + format.format = media_raw_audio_format::B_AUDIO_INT; + format.byte_order = B_MEDIA_BIG_ENDIAN; + break; + + case AUDIO_F32LSB: + format.format = media_raw_audio_format::B_AUDIO_FLOAT; + break; + + case AUDIO_F32MSB: + format.format = media_raw_audio_format::B_AUDIO_FLOAT; + format.byte_order = B_MEDIA_BIG_ENDIAN; + break; + + default: + valid_datatype = 0; + test_format = SDL_NextAudioFormat(); + break; + } + } + + if (!valid_datatype) { /* shouldn't happen, but just in case... */ + return SDL_SetError("Unsupported audio format"); + } + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&_this->spec); + + format.buffer_size = _this->spec.size; + + /* Subscribe to the audio stream (creates a new thread) */ + sigset_t omask; + MaskSignals(&omask); + _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio", + FillSound, NULL, _this); + UnmaskSignals(&omask); + + if (_this->hidden->audio_obj->Start() == B_NO_ERROR) { + _this->hidden->audio_obj->SetHasData(true); + } else { + return SDL_SetError("Unable to start Be audio"); + } + + /* We're running! */ + return 0; +} + +static void +HAIKUAUDIO_Deinitialize(void) +{ + SDL_QuitBeApp(); +} + +static int +HAIKUAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + /* Initialize the Be Application, if it's not already started */ + if (SDL_InitBeApp() < 0) { + return 0; + } + + /* Set the function pointers */ + impl->OpenDevice = HAIKUAUDIO_OpenDevice; + impl->CloseDevice = HAIKUAUDIO_CloseDevice; + impl->Deinitialize = HAIKUAUDIO_Deinitialize; + impl->ProvidesOwnCallbackThread = 1; + impl->OnlyHasDefaultOutputDevice = 1; + + return 1; /* this audio target is available. */ +} + +extern "C" +{ + extern AudioBootStrap HAIKUAUDIO_bootstrap; +} +AudioBootStrap HAIKUAUDIO_bootstrap = { + "haiku", "Haiku BSoundPlayer", HAIKUAUDIO_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_HAIKU */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.h b/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.h new file mode 100644 index 0000000..f63ccdb --- /dev/null +++ b/ThirdParty/SDL2/src/audio/haiku/SDL_haikuaudio.h @@ -0,0 +1,38 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_haikuaudio_h_ +#define SDL_haikuaudio_h_ + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *_this + +struct SDL_PrivateAudioData +{ + BSoundPlayer *audio_obj; +}; + +#endif /* SDL_haikuaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.c b/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.c new file mode 100644 index 0000000..a252da7 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.c @@ -0,0 +1,429 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_JACK + +#include "SDL_assert.h" +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "../SDL_audio_c.h" +#include "SDL_jackaudio.h" +#include "SDL_loadso.h" +#include "../../thread/SDL_systhread.h" + + +static jack_client_t * (*JACK_jack_client_open) (const char *, jack_options_t, jack_status_t *, ...); +static int (*JACK_jack_client_close) (jack_client_t *); +static void (*JACK_jack_on_shutdown) (jack_client_t *, JackShutdownCallback, void *); +static int (*JACK_jack_activate) (jack_client_t *); +static int (*JACK_jack_deactivate) (jack_client_t *); +static void * (*JACK_jack_port_get_buffer) (jack_port_t *, jack_nframes_t); +static int (*JACK_jack_port_unregister) (jack_client_t *, jack_port_t *); +static void (*JACK_jack_free) (void *); +static const char ** (*JACK_jack_get_ports) (jack_client_t *, const char *, const char *, unsigned long); +static jack_nframes_t (*JACK_jack_get_sample_rate) (jack_client_t *); +static jack_nframes_t (*JACK_jack_get_buffer_size) (jack_client_t *); +static jack_port_t * (*JACK_jack_port_register) (jack_client_t *, const char *, const char *, unsigned long, unsigned long); +static const char * (*JACK_jack_port_name) (const jack_port_t *); +static int (*JACK_jack_connect) (jack_client_t *, const char *, const char *); +static int (*JACK_jack_set_process_callback) (jack_client_t *, JackProcessCallback, void *); + +static int load_jack_syms(void); + + +#ifdef SDL_AUDIO_DRIVER_JACK_DYNAMIC + +static const char *jack_library = SDL_AUDIO_DRIVER_JACK_DYNAMIC; +static void *jack_handle = NULL; + +/* !!! FIXME: this is copy/pasted in several places now */ +static int +load_jack_sym(const char *fn, void **addr) +{ + *addr = SDL_LoadFunction(jack_handle, fn); + if (*addr == NULL) { + /* Don't call SDL_SetError(): SDL_LoadFunction already did. */ + return 0; + } + + return 1; +} + +/* cast funcs to char* first, to please GCC's strict aliasing rules. */ +#define SDL_JACK_SYM(x) \ + if (!load_jack_sym(#x, (void **) (char *) &JACK_##x)) return -1 + +static void +UnloadJackLibrary(void) +{ + if (jack_handle != NULL) { + SDL_UnloadObject(jack_handle); + jack_handle = NULL; + } +} + +static int +LoadJackLibrary(void) +{ + int retval = 0; + if (jack_handle == NULL) { + jack_handle = SDL_LoadObject(jack_library); + if (jack_handle == NULL) { + retval = -1; + /* Don't call SDL_SetError(): SDL_LoadObject already did. */ + } else { + retval = load_jack_syms(); + if (retval < 0) { + UnloadJackLibrary(); + } + } + } + return retval; +} + +#else + +#define SDL_JACK_SYM(x) JACK_##x = x + +static void +UnloadJackLibrary(void) +{ +} + +static int +LoadJackLibrary(void) +{ + load_jack_syms(); + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_JACK_DYNAMIC */ + + +static int +load_jack_syms(void) +{ + SDL_JACK_SYM(jack_client_open); + SDL_JACK_SYM(jack_client_close); + SDL_JACK_SYM(jack_on_shutdown); + SDL_JACK_SYM(jack_activate); + SDL_JACK_SYM(jack_deactivate); + SDL_JACK_SYM(jack_port_get_buffer); + SDL_JACK_SYM(jack_port_unregister); + SDL_JACK_SYM(jack_free); + SDL_JACK_SYM(jack_get_ports); + SDL_JACK_SYM(jack_get_sample_rate); + SDL_JACK_SYM(jack_get_buffer_size); + SDL_JACK_SYM(jack_port_register); + SDL_JACK_SYM(jack_port_name); + SDL_JACK_SYM(jack_connect); + SDL_JACK_SYM(jack_set_process_callback); + return 0; +} + + +static void +jackShutdownCallback(void *arg) /* JACK went away; device is lost. */ +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) arg; + SDL_OpenedAudioDeviceDisconnected(this); + SDL_SemPost(this->hidden->iosem); /* unblock the SDL thread. */ +} + +// !!! FIXME: implement and register these! +//typedef int(* JackSampleRateCallback)(jack_nframes_t nframes, void *arg) +//typedef int(* JackBufferSizeCallback)(jack_nframes_t nframes, void *arg) + +static int +jackProcessPlaybackCallback(jack_nframes_t nframes, void *arg) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) arg; + jack_port_t **ports = this->hidden->sdlports; + const int total_channels = this->spec.channels; + const int total_frames = this->spec.samples; + int channelsi; + + if (!SDL_AtomicGet(&this->enabled)) { + /* silence the buffer to avoid repeats and corruption. */ + SDL_memset(this->hidden->iobuffer, '\0', this->spec.size); + } + + for (channelsi = 0; channelsi < total_channels; channelsi++) { + float *dst = (float *) JACK_jack_port_get_buffer(ports[channelsi], nframes); + if (dst) { + const float *src = ((float *) this->hidden->iobuffer) + channelsi; + int framesi; + for (framesi = 0; framesi < total_frames; framesi++) { + *(dst++) = *src; + src += total_channels; + } + } + } + + SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; refill the buffer. */ + return 0; /* success */ +} + + +/* This function waits until it is possible to write a full sound buffer */ +static void +JACK_WaitDevice(_THIS) +{ + if (SDL_AtomicGet(&this->enabled)) { + if (SDL_SemWait(this->hidden->iosem) == -1) { + SDL_OpenedAudioDeviceDisconnected(this); + } + } +} + +static Uint8 * +JACK_GetDeviceBuf(_THIS) +{ + return (Uint8 *) this->hidden->iobuffer; +} + + +static int +jackProcessCaptureCallback(jack_nframes_t nframes, void *arg) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) arg; + if (SDL_AtomicGet(&this->enabled)) { + jack_port_t **ports = this->hidden->sdlports; + const int total_channels = this->spec.channels; + const int total_frames = this->spec.samples; + int channelsi; + + for (channelsi = 0; channelsi < total_channels; channelsi++) { + const float *src = (const float *) JACK_jack_port_get_buffer(ports[channelsi], nframes); + if (src) { + float *dst = ((float *) this->hidden->iobuffer) + channelsi; + int framesi; + for (framesi = 0; framesi < total_frames; framesi++) { + *dst = *(src++); + dst += total_channels; + } + } + } + } + + SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; new buffer is ready! */ + return 0; /* success */ +} + +static int +JACK_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + SDL_assert(buflen == this->spec.size); /* we always fill a full buffer. */ + + /* Wait for JACK to fill the iobuffer */ + if (SDL_SemWait(this->hidden->iosem) == -1) { + return -1; + } + + SDL_memcpy(buffer, this->hidden->iobuffer, buflen); + return buflen; +} + +static void +JACK_FlushCapture(_THIS) +{ + SDL_SemWait(this->hidden->iosem); +} + + +static void +JACK_CloseDevice(_THIS) +{ + if (this->hidden->client) { + JACK_jack_deactivate(this->hidden->client); + + if (this->hidden->sdlports) { + const int channels = this->spec.channels; + int i; + for (i = 0; i < channels; i++) { + JACK_jack_port_unregister(this->hidden->client, this->hidden->sdlports[i]); + } + SDL_free(this->hidden->sdlports); + } + + JACK_jack_client_close(this->hidden->client); + } + + if (this->hidden->iosem) { + SDL_DestroySemaphore(this->hidden->iosem); + } + + if (this->hidden->devports) { + JACK_jack_free(this->hidden->devports); + } + + SDL_free(this->hidden->iobuffer); +} + +static int +JACK_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + /* Note that JACK uses "output" for capture devices (they output audio + data to us) and "input" for playback (we input audio data to them). + Likewise, SDL's playback port will be "output" (we write data out) + and capture will be "input" (we read data in). */ + const unsigned long sysportflags = iscapture ? JackPortIsOutput : JackPortIsInput; + const unsigned long sdlportflags = iscapture ? JackPortIsInput : JackPortIsOutput; + const JackProcessCallback callback = iscapture ? jackProcessCaptureCallback : jackProcessPlaybackCallback; + const char *sdlportstr = iscapture ? "input" : "output"; + const char **devports = NULL; + jack_client_t *client = NULL; + jack_status_t status; + int channels = 0; + int i; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof (*this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + + /* !!! FIXME: we _still_ need an API to specify an app name */ + client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL); + this->hidden->client = client; + if (client == NULL) { + return SDL_SetError("Can't open JACK client"); + } + + devports = JACK_jack_get_ports(client, NULL, NULL, JackPortIsPhysical | sysportflags); + this->hidden->devports = devports; + if (!devports || !devports[0]) { + return SDL_SetError("No physical JACK ports available"); + } + + while (devports[++channels]) { + /* spin to count devports */ + } + + /* !!! FIXME: docs say about buffer size: "This size may change, clients that depend on it must register a bufsize_callback so they will be notified if it does." */ + + /* Jack pretty much demands what it wants. */ + this->spec.format = AUDIO_F32SYS; + this->spec.freq = JACK_jack_get_sample_rate(client); + this->spec.channels = channels; + this->spec.samples = JACK_jack_get_buffer_size(client); + + SDL_CalculateAudioSpec(&this->spec); + + this->hidden->iosem = SDL_CreateSemaphore(0); + if (!this->hidden->iosem) { + return -1; /* error was set by SDL_CreateSemaphore */ + } + + this->hidden->iobuffer = (float *) SDL_calloc(1, this->spec.size); + if (!this->hidden->iobuffer) { + return SDL_OutOfMemory(); + } + + /* Build SDL's ports, which we will connect to the device ports. */ + this->hidden->sdlports = (jack_port_t **) SDL_calloc(channels, sizeof (jack_port_t *)); + if (this->hidden->sdlports == NULL) { + return SDL_OutOfMemory(); + } + + for (i = 0; i < channels; i++) { + char portname[32]; + SDL_snprintf(portname, sizeof (portname), "sdl_jack_%s_%d", sdlportstr, i); + this->hidden->sdlports[i] = JACK_jack_port_register(client, portname, JACK_DEFAULT_AUDIO_TYPE, sdlportflags, 0); + if (this->hidden->sdlports[i] == NULL) { + return SDL_SetError("jack_port_register failed"); + } + } + + if (JACK_jack_set_process_callback(client, callback, this) != 0) { + return SDL_SetError("JACK: Couldn't set process callback"); + } + + JACK_jack_on_shutdown(client, jackShutdownCallback, this); + + if (JACK_jack_activate(client) != 0) { + return SDL_SetError("Failed to activate JACK client"); + } + + /* once activated, we can connect all the ports. */ + for (i = 0; i < channels; i++) { + const char *sdlport = JACK_jack_port_name(this->hidden->sdlports[i]); + const char *srcport = iscapture ? devports[i] : sdlport; + const char *dstport = iscapture ? sdlport : devports[i]; + if (JACK_jack_connect(client, srcport, dstport) != 0) { + return SDL_SetError("Couldn't connect JACK ports: %s => %s", srcport, dstport); + } + } + + /* don't need these anymore. */ + this->hidden->devports = NULL; + JACK_jack_free(devports); + + /* We're ready to rock and roll. :-) */ + return 0; +} + +static void +JACK_Deinitialize(void) +{ + UnloadJackLibrary(); +} + +static int +JACK_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadJackLibrary() < 0) { + return 0; + } else { + /* Make sure a JACK server is running and available. */ + jack_status_t status; + jack_client_t *client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL); + if (client == NULL) { + UnloadJackLibrary(); + return 0; + } + JACK_jack_client_close(client); + } + + /* Set the function pointers */ + impl->OpenDevice = JACK_OpenDevice; + impl->WaitDevice = JACK_WaitDevice; + impl->GetDeviceBuf = JACK_GetDeviceBuf; + impl->CloseDevice = JACK_CloseDevice; + impl->Deinitialize = JACK_Deinitialize; + impl->CaptureFromDevice = JACK_CaptureFromDevice; + impl->FlushCapture = JACK_FlushCapture; + impl->OnlyHasDefaultOutputDevice = SDL_TRUE; + impl->OnlyHasDefaultCaptureDevice = SDL_TRUE; + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap JACK_bootstrap = { + "jack", "JACK Audio Connection Kit", JACK_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_JACK */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.h b/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.h new file mode 100644 index 0000000..aab199a --- /dev/null +++ b/ThirdParty/SDL2/src/audio/jack/SDL_jackaudio.h @@ -0,0 +1,42 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#ifndef SDL_jackaudio_h_ +#define SDL_jackaudio_h_ + +#include + +#include "../SDL_sysaudio.h" + +/* Hidden "this" pointer for the audio functions */ +#define _THIS SDL_AudioDevice *this + +struct SDL_PrivateAudioData +{ + jack_client_t *client; + SDL_sem *iosem; + float *iobuffer; + const char **devports; + jack_port_t **sdlports; +}; + +#endif /* SDL_jackaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.c b/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.c new file mode 100644 index 0000000..3e3afc0 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.c @@ -0,0 +1,165 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_NACL + +#include "SDL_naclaudio.h" + +#include "SDL_audio.h" +#include "SDL_mutex.h" +#include "../SDL_audio_c.h" +#include "../SDL_audiodev_c.h" + +#include "ppapi/c/pp_errors.h" +#include "ppapi/c/pp_instance.h" +#include "ppapi_simple/ps.h" +#include "ppapi_simple/ps_interface.h" +#include "ppapi_simple/ps_event.h" + +/* The tag name used by NACL audio */ +#define NACLAUDIO_DRIVER_NAME "nacl" + +#define SAMPLE_FRAME_COUNT 4096 + +/* Audio driver functions */ +static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data); + +/* FIXME: Make use of latency if needed */ +static void nacl_audio_callback(void* stream, uint32_t buffer_size, PP_TimeDelta latency, void* data) { + const int len = (int) buffer_size; + SDL_AudioDevice* _this = (SDL_AudioDevice*) data; + SDL_AudioCallback callback = _this->callbackspec.callback; + + SDL_LockMutex(private->mutex); /* !!! FIXME: is this mutex necessary? */ + + /* Only do something if audio is enabled */ + if (!SDL_AtomicGet(&_this->enabled) || SDL_AtomicGet(&_this->paused)) { + if (_this->stream) { + SDL_AudioStreamClear(_this->stream); + } + SDL_memset(stream, _this->spec.silence, len); + return; + } + + SDL_assert(_this->spec.size == len); + + if (_this->stream == NULL) { /* no conversion necessary. */ + SDL_LockMutex(_this->mixer_lock); + callback(_this->callbackspec.userdata, stream, len); + SDL_UnlockMutex(_this->mixer_lock); + } else { /* streaming/converting */ + const int stream_len = _this->callbackspec.size; + while (SDL_AudioStreamAvailable(_this->stream) < len) { + callback(_this->callbackspec.userdata, _this->work_buffer, stream_len); + if (SDL_AudioStreamPut(_this->stream, _this->work_buffer, stream_len) == -1) { + SDL_AudioStreamClear(_this->stream); + SDL_AtomicSet(&_this->enabled, 0); + break; + } + } + + const int got = SDL_AudioStreamGet(_this->stream, stream, len); + SDL_assert((got < 0) || (got == len)); + if (got != len) { + SDL_memset(stream, _this->spec.silence, len); + } + } + + SDL_UnlockMutex(private->mutex); +} + +static void NACLAUDIO_CloseDevice(SDL_AudioDevice *device) { + const PPB_Core *core = PSInterfaceCore(); + const PPB_Audio *ppb_audio = PSInterfaceAudio(); + SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden; + + ppb_audio->StopPlayback(hidden->audio); + SDL_DestroyMutex(hidden->mutex); + core->ReleaseResource(hidden->audio); +} + +static int +NACLAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { + PP_Instance instance = PSGetInstanceId(); + const PPB_Audio *ppb_audio = PSInterfaceAudio(); + const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig(); + + private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private)); + if (private == NULL) { + return SDL_OutOfMemory(); + } + + private->mutex = SDL_CreateMutex(); + _this->spec.freq = 44100; + _this->spec.format = AUDIO_S16LSB; + _this->spec.channels = 2; + _this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount( + instance, + PP_AUDIOSAMPLERATE_44100, + SAMPLE_FRAME_COUNT); + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&_this->spec); + + private->audio = ppb_audio->Create( + instance, + ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples), + nacl_audio_callback, + _this); + + /* Start audio playback while we are still on the main thread. */ + ppb_audio->StartPlayback(private->audio); + + return 0; +} + +static int +NACLAUDIO_Init(SDL_AudioDriverImpl * impl) +{ + if (PSGetInstanceId() == 0) { + return 0; + } + + /* Set the function pointers */ + impl->OpenDevice = NACLAUDIO_OpenDevice; + impl->CloseDevice = NACLAUDIO_CloseDevice; + impl->OnlyHasDefaultOutputDevice = 1; + impl->ProvidesOwnCallbackThread = 1; + /* + * impl->WaitDevice = NACLAUDIO_WaitDevice; + * impl->GetDeviceBuf = NACLAUDIO_GetDeviceBuf; + * impl->PlayDevice = NACLAUDIO_PlayDevice; + * impl->Deinitialize = NACLAUDIO_Deinitialize; + */ + + return 1; +} + +AudioBootStrap NACLAUDIO_bootstrap = { + NACLAUDIO_DRIVER_NAME, "SDL NaCl Audio Driver", + NACLAUDIO_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_NACL */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.h b/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.h new file mode 100644 index 0000000..5ec842b --- /dev/null +++ b/ThirdParty/SDL2/src/audio/nacl/SDL_naclaudio.h @@ -0,0 +1,43 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +#ifndef SDL_naclaudio_h_ +#define SDL_naclaudio_h_ + +#include "SDL_audio.h" +#include "../SDL_sysaudio.h" +#include "SDL_mutex.h" + +#include "ppapi/c/ppb_audio.h" + +#define _THIS SDL_AudioDevice *_this +#define private _this->hidden + +typedef struct SDL_PrivateAudioData { + SDL_mutex* mutex; + PP_Resource audio; +} SDL_PrivateAudioData; + +#endif /* SDL_naclaudio_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.c b/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.c new file mode 100644 index 0000000..5a02a3b --- /dev/null +++ b/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.c @@ -0,0 +1,463 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#if SDL_AUDIO_DRIVER_NAS + +/* Allow access to a raw mixing buffer */ + +#include +#include + +#include "SDL_timer.h" +#include "SDL_audio.h" +#include "SDL_loadso.h" +#include "../SDL_audio_c.h" +#include "SDL_nasaudio.h" + +static void (*NAS_AuCloseServer) (AuServer *); +static void (*NAS_AuNextEvent) (AuServer *, AuBool, AuEvent *); +static AuBool(*NAS_AuDispatchEvent) (AuServer *, AuEvent *); +static void (*NAS_AuHandleEvents) (AuServer *); +static AuFlowID(*NAS_AuCreateFlow) (AuServer *, AuStatus *); +static void (*NAS_AuStartFlow) (AuServer *, AuFlowID, AuStatus *); +static void (*NAS_AuSetElements) + (AuServer *, AuFlowID, AuBool, int, AuElement *, AuStatus *); +static void (*NAS_AuWriteElement) + (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuBool, AuStatus *); +static AuUint32 (*NAS_AuReadElement) + (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuStatus *); +static AuServer *(*NAS_AuOpenServer) + (_AuConst char *, int, _AuConst char *, int, _AuConst char *, char **); +static AuEventHandlerRec *(*NAS_AuRegisterEventHandler) + (AuServer *, AuMask, int, AuID, AuEventHandlerCallback, AuPointer); + + +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC + +static const char *nas_library = SDL_AUDIO_DRIVER_NAS_DYNAMIC; +static void *nas_handle = NULL; + +static int +load_nas_sym(const char *fn, void **addr) +{ + *addr = SDL_LoadFunction(nas_handle, fn); + if (*addr == NULL) { + return 0; + } + return 1; +} + +/* cast funcs to char* first, to please GCC's strict aliasing rules. */ +#define SDL_NAS_SYM(x) \ + if (!load_nas_sym(#x, (void **) (char *) &NAS_##x)) return -1 +#else +#define SDL_NAS_SYM(x) NAS_##x = x +#endif + +static int +load_nas_syms(void) +{ + SDL_NAS_SYM(AuCloseServer); + SDL_NAS_SYM(AuNextEvent); + SDL_NAS_SYM(AuDispatchEvent); + SDL_NAS_SYM(AuHandleEvents); + SDL_NAS_SYM(AuCreateFlow); + SDL_NAS_SYM(AuStartFlow); + SDL_NAS_SYM(AuSetElements); + SDL_NAS_SYM(AuWriteElement); + SDL_NAS_SYM(AuReadElement); + SDL_NAS_SYM(AuOpenServer); + SDL_NAS_SYM(AuRegisterEventHandler); + return 0; +} + +#undef SDL_NAS_SYM + +#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC + +static void +UnloadNASLibrary(void) +{ + if (nas_handle != NULL) { + SDL_UnloadObject(nas_handle); + nas_handle = NULL; + } +} + +static int +LoadNASLibrary(void) +{ + int retval = 0; + if (nas_handle == NULL) { + nas_handle = SDL_LoadObject(nas_library); + if (nas_handle == NULL) { + /* Copy error string so we can use it in a new SDL_SetError(). */ + const char *origerr = SDL_GetError(); + const size_t len = SDL_strlen(origerr) + 1; + char *err = (char *) alloca(len); + SDL_strlcpy(err, origerr, len); + retval = -1; + SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s", + nas_library, err); + } else { + retval = load_nas_syms(); + if (retval < 0) { + UnloadNASLibrary(); + } + } + } + return retval; +} + +#else + +static void +UnloadNASLibrary(void) +{ +} + +static int +LoadNASLibrary(void) +{ + load_nas_syms(); + return 0; +} + +#endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */ + +/* This function waits until it is possible to write a full sound buffer */ +static void +NAS_WaitDevice(_THIS) +{ + while (this->hidden->buf_free < this->hidden->mixlen) { + AuEvent ev; + NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev); + NAS_AuDispatchEvent(this->hidden->aud, &ev); + } +} + +static void +NAS_PlayDevice(_THIS) +{ + while (this->hidden->mixlen > this->hidden->buf_free) { + /* + * We think the buffer is full? Yikes! Ask the server for events, + * in the hope that some of them is LowWater events telling us more + * of the buffer is free now than what we think. + */ + AuEvent ev; + NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev); + NAS_AuDispatchEvent(this->hidden->aud, &ev); + } + this->hidden->buf_free -= this->hidden->mixlen; + + /* Write the audio data */ + NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0, + this->hidden->mixlen, this->hidden->mixbuf, AuFalse, + NULL); + + this->hidden->written += this->hidden->mixlen; + +#ifdef DEBUG_AUDIO + fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen); +#endif +} + +static Uint8 * +NAS_GetDeviceBuf(_THIS) +{ + return (this->hidden->mixbuf); +} + +static int +NAS_CaptureFromDevice(_THIS, void *buffer, int buflen) +{ + struct SDL_PrivateAudioData *h = this->hidden; + int retval; + + while (SDL_TRUE) { + /* just keep the event queue moving and the server chattering. */ + NAS_AuHandleEvents(h->aud); + + retval = (int) NAS_AuReadElement(h->aud, h->flow, 1, buflen, buffer, NULL); + /*printf("read %d capture bytes\n", (int) retval);*/ + if (retval == 0) { + SDL_Delay(10); /* don't burn the CPU if we're waiting for data. */ + } else { + break; + } + } + + return retval; +} + +static void +NAS_FlushCapture(_THIS) +{ + struct SDL_PrivateAudioData *h = this->hidden; + AuUint32 total = 0; + AuUint32 br; + Uint8 buf[512]; + + do { + /* just keep the event queue moving and the server chattering. */ + NAS_AuHandleEvents(h->aud); + br = NAS_AuReadElement(h->aud, h->flow, 1, sizeof (buf), buf, NULL); + /*printf("flushed %d capture bytes\n", (int) br);*/ + total += br; + } while ((br == sizeof (buf)) && (total < this->spec.size)); +} + +static void +NAS_CloseDevice(_THIS) +{ + if (this->hidden->aud) { + NAS_AuCloseServer(this->hidden->aud); + } + SDL_free(this->hidden->mixbuf); + SDL_free(this->hidden); +} + +static unsigned char +sdlformat_to_auformat(unsigned int fmt) +{ + switch (fmt) { + case AUDIO_U8: + return AuFormatLinearUnsigned8; + case AUDIO_S8: + return AuFormatLinearSigned8; + case AUDIO_U16LSB: + return AuFormatLinearUnsigned16LSB; + case AUDIO_U16MSB: + return AuFormatLinearUnsigned16MSB; + case AUDIO_S16LSB: + return AuFormatLinearSigned16LSB; + case AUDIO_S16MSB: + return AuFormatLinearSigned16MSB; + } + return AuNone; +} + +static AuBool +event_handler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd) +{ + SDL_AudioDevice *this = (SDL_AudioDevice *) hnd->data; + struct SDL_PrivateAudioData *h = this->hidden; + if (this->iscapture) { + return AuTrue; /* we don't (currently) care about any of this for capture devices */ + } + + switch (ev->type) { + case AuEventTypeElementNotify: + { + AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev; + + switch (event->kind) { + case AuElementNotifyKindLowWater: + if (h->buf_free >= 0) { + h->really += event->num_bytes; + gettimeofday(&h->last_tv, 0); + h->buf_free += event->num_bytes; + } else { + h->buf_free = event->num_bytes; + } + break; + case AuElementNotifyKindState: + switch (event->cur_state) { + case AuStatePause: + if (event->reason != AuReasonUser) { + if (h->buf_free >= 0) { + h->really += event->num_bytes; + gettimeofday(&h->last_tv, 0); + h->buf_free += event->num_bytes; + } else { + h->buf_free = event->num_bytes; + } + } + break; + } + } + } + } + return AuTrue; +} + +static AuDeviceID +find_device(_THIS) +{ + /* These "Au" things are all macros, not functions... */ + struct SDL_PrivateAudioData *h = this->hidden; + const unsigned int devicekind = this->iscapture ? AuComponentKindPhysicalInput : AuComponentKindPhysicalOutput; + const int numdevs = AuServerNumDevices(h->aud); + const int nch = this->spec.channels; + int i; + + /* Try to find exact match on channels first... */ + for (i = 0; i < numdevs; i++) { + const AuDeviceAttributes *dev = AuServerDevice(h->aud, i); + if ((AuDeviceKind(dev) == devicekind) && (AuDeviceNumTracks(dev) == nch)) { + return AuDeviceIdentifier(dev); + } + } + + /* Take anything, then... */ + for (i = 0; i < numdevs; i++) { + const AuDeviceAttributes *dev = AuServerDevice(h->aud, i); + if (AuDeviceKind(dev) == devicekind) { + this->spec.channels = AuDeviceNumTracks(dev); + return AuDeviceIdentifier(dev); + } + } + return AuNone; +} + +static int +NAS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) +{ + AuElement elms[3]; + int buffer_size; + SDL_AudioFormat test_format, format; + + /* Initialize all variables that we clean on shutdown */ + this->hidden = (struct SDL_PrivateAudioData *) + SDL_malloc((sizeof *this->hidden)); + if (this->hidden == NULL) { + return SDL_OutOfMemory(); + } + SDL_zerop(this->hidden); + + /* Try for a closest match on audio format */ + format = 0; + for (test_format = SDL_FirstAudioFormat(this->spec.format); + !format && test_format;) { + format = sdlformat_to_auformat(test_format); + if (format == AuNone) { + test_format = SDL_NextAudioFormat(); + } + } + if (format == 0) { + return SDL_SetError("NAS: Couldn't find any hardware audio formats"); + } + this->spec.format = test_format; + + this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL); + if (this->hidden->aud == 0) { + return SDL_SetError("NAS: Couldn't open connection to NAS server"); + } + + this->hidden->dev = find_device(this); + if ((this->hidden->dev == AuNone) + || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, 0)))) { + return SDL_SetError("NAS: Couldn't find a fitting device on NAS server"); + } + + buffer_size = this->spec.freq; + if (buffer_size < 4096) + buffer_size = 4096; + + if (buffer_size > 32768) + buffer_size = 32768; /* So that the buffer won't get unmanageably big. */ + + /* Calculate the final parameters for this audio specification */ + SDL_CalculateAudioSpec(&this->spec); + + if (iscapture) { + AuMakeElementImportDevice(elms, this->spec.freq, this->hidden->dev, + AuUnlimitedSamples, 0, NULL); + AuMakeElementExportClient(elms + 1, 0, this->spec.freq, format, + this->spec.channels, AuTrue, buffer_size, + buffer_size, 0, NULL); + } else { + AuMakeElementImportClient(elms, this->spec.freq, format, + this->spec.channels, AuTrue, buffer_size, + buffer_size / 4, 0, NULL); + AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq, + AuUnlimitedSamples, 0, NULL); + } + + NAS_AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, + 2, elms, NULL); + + NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, + this->hidden->flow, event_handler, + (AuPointer) this); + + NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL); + + /* Allocate mixing buffer */ + if (!iscapture) { + this->hidden->mixlen = this->spec.size; + this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); + if (this->hidden->mixbuf == NULL) { + return SDL_OutOfMemory(); + } + SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); + } + + /* We're ready to rock and roll. :-) */ + return 0; +} + +static void +NAS_Deinitialize(void) +{ + UnloadNASLibrary(); +} + +static int +NAS_Init(SDL_AudioDriverImpl * impl) +{ + if (LoadNASLibrary() < 0) { + return 0; + } else { + AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL); + if (aud == NULL) { + SDL_SetError("NAS: AuOpenServer() failed (no audio server?)"); + return 0; + } + NAS_AuCloseServer(aud); + } + + /* Set the function pointers */ + impl->OpenDevice = NAS_OpenDevice; + impl->PlayDevice = NAS_PlayDevice; + impl->WaitDevice = NAS_WaitDevice; + impl->GetDeviceBuf = NAS_GetDeviceBuf; + impl->CaptureFromDevice = NAS_CaptureFromDevice; + impl->FlushCapture = NAS_FlushCapture; + impl->CloseDevice = NAS_CloseDevice; + impl->Deinitialize = NAS_Deinitialize; + + impl->OnlyHasDefaultOutputDevice = 1; + impl->OnlyHasDefaultCaptureDevice = 1; + impl->HasCaptureSupport = SDL_TRUE; + + return 1; /* this audio target is available. */ +} + +AudioBootStrap NAS_bootstrap = { + "nas", "Network Audio System", NAS_Init, 0 +}; + +#endif /* SDL_AUDIO_DRIVER_NAS */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.h b/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.h new file mode 100644 index 0000000..b1a51d1 --- /dev/null +++ b/ThirdParty/SDL2/src/audio/nas/SDL_nasaudio.h @@ -0,0 +1,56 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "../../SDL_internal.h" + +#ifndef SDL_nasaudio_h_ +#define SDL_nasaudio_h_ + +#ifdef __sgi +#include +#else +#include