399bc709b build-scripts.pl: Added add-source-to-projects.pl ac6827187 Visual-WinRT: dos2unix the project files to match other Visual Studio targets. 34719cba9 Fixed crash in hid_init() if the HIDDeviceManager isn't available 2e92e94eb Make sure we update device->sample_frames in SDL_AudioDeviceFormatChangedAlreadyLocked() 9964e5c5b wayland: Don't retrieve the drag offer strings with every pointer motion event bac7eeaaa Added missing include a541e2ac1 audio: Change a few SDL_memcpy calls to SDL_copyp. 54125c140 audio: Only update bound audiostreams' formats when necessary. e0b0f9a36 testaudio: Fix mouseover testing. 2f3deec24 wayland: Don't process drag & drop events from surfaces not owned by SDL 42bdced05 events: Log file drop position events and print the pointer coordinates c10d93d3a wayland: Replace magic constant with define 500852153 emscripten: Restore compatibility with existing emsdk releases. 953b55dd6 Use EM_ASM_PTR when the return value is a pointer a4541a255 audio: SDL_GetAudioStreamQueued now returns bytes, not frames. 703aefbce Sync SDL3 wiki -> header 99421b64d linux: Add portal drag and drop 952c5059b Remove stray  eebd5d18a linux: Handle upower's UP_DEVICE_STATE_PENDING_CHARGE, PENDING_DISCHARGE f8fdb20d8 audio: Destroy all existing SDL_AudioStreams on shutdown. 62d445997 audio: Removed declarations of functions that don't exist anymore. b656720bc loopwave: Use SDL_GetAudioStreamQueued() for more accurate results. 34b931f7e audio: Added SDL_GetAudioStreamQueued 23206b9e3 audio: Added SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED c7e6d7a1f audio: Changed debug logging output. 87ec6acf2 audio: Added a FIXME ac88ffb7e audio: don't allocate buffer in SDL_SetAudioPostmixCallback for NULL callback. 2a950f6ae audio: Replace some SDL_memcpy calls with SDL_copyp. 0dc0434a3 audio: Fixed race condition in subsystem shutdown. 23f60203a audio: precalculate if we can use simple copies instead of the full mixer. 36b0f1141 audio: Optimize setting device formats during audio thread iteration. 4c3e84897 testspriteminimal: make standalone by embedding icon.bmp 2a01f9dcb tests: plug leaks when running with --trackmem f42bbeca2 SDL_test: track stack frames of allocations on Windows 12c0be028 SDL_test: clear text cache on exit event b4bfb1831 SDL_test: free state before logging allocations 248b1edd3 SDL_test: destroy windows in SDL_CommonQuit 98da2dd30 SDL_test: don't warn about expected allocations when running with --trackmem 6a381567b Support audio rate conversion up to 384KHz b2b548a1f Don't hang if IAudioRenderClient_GetBuffer() fails indefinitely a3a5e1728 Fixed build warning '=': conversion from 'Uint32' to 'Uint16', possible loss of data 6d3e21c27 Fixed android build warnings fca2f5318 Fixed warning: this function declaration is not a prototype a72dfa6a5 Fixed sensor timestamp units for third-party PS5 controllers f6756047a Fixed error: array subscript 2 is above array bounds of ‘const Uint8[2]’ 7059a55cc Fixed sensor timestamp calculation for third-party PS5 controllers c0443e5d1 Fixed crash in SDL_IMMDevice_FindByDevID() fde8499f6 Use around 20ms for the audio buffer size e5739d7d1 video: Remove SDL_GetFocusWindow() 39c2f9737 Fix NULL dereference in SDL_OpenAudio 9a23d0e3f Added new audio files to the Xcode project a62e62f97 Refactored SDL_audiocvt.c 31229fd47 include: Added a note about SDL's iOS app delegate functions. 65aaf3a9a x11: Always update clipboard owner f622f21e6 Fixed build 5774c9638 Prefer hidraw over libusb when libusb whitelisting is not enabled 9301f7ace hidapi/libusb: only enumerate each interface once 859dc14ad Replaced SDL_GetGamepadBindForAxis() and SDL_GetGamepadBindForButton() with SDL_GetGamepadBindings() 9e50048ab Revert "Removed SDL_GamepadBinding from the API" 9f17d1a9d Don't reference the same function in "see also" 86505ea63 fix SDL_AudioStreamCallback documentation d885d5c31 Sync SDL3 wiki -> header 2f43f7bc5 audio: Allow querying of device buffer size. cf9572113 audio: Added a hint to let apps force device buffer size. 47d8c77c6 audio: Choose better default sample frame counts. 8b26e95f9 audio: Change SDL_AudioStreamCallback 9da34e8fb docs: Updated README-emscripten.md. fd1c54a00 detect fanatec steering wheels cb4414608 docs: Whoops, this got added by the wiki bridge by accident! cd633b9a8 Renamed SDL_IsAudioDevicePaused() to SDL_AudioDevicePaused() c6cad07fa Sync SDL3 wiki -> header a6e52f9e4 Sync SDL3 wiki -> header 2de2e9d03 Fix flickering of window when using desktop-fullscreen and borderless window on multiple monitors on Linux. Closes #8186. 723835d16 Windows: fix for client rect resizing larger each time we came from exclusive fullscreen -> windowed on a monitor with HiDPI set. The problem was we were using the monitor DPI rather than the window DPI so AdjustWindowRectExForDpi was giving us an incorrect size which would be too large for the client rect. Closes #8237. ce27363df wikiheaders: Sort undocumented functions. e22282b09 Added README about transparent windows in Win32 1d1c6e630 Turn off COREAUDIO debug logging by default 52efefca0 wayland: Fix drag offer leak 3a992af44 audio: Added a postmix callback to logical devices. 7207bdce5 render: Enable clipping for zero-sized rectangles 22d81fb3e cmake: use MSVC_RUNTIME_LIBRARY to force MT a2e17852d cmake: make sure SDL_GetPrefPath is run before testfilesystem 2fb266e0a ci: run tests in parallel ad1313e75 testaudio: Patched to compile. 5747ddc01 testaudio: Clean up some messy memory management. fafbea1ce audio: Move internal float32 mixing to a simplified function 116b0ec97 include: minor tweak to audio API documentation fb1377035 include: Replaced old Bugzilla URL. 38c8fc05c audio: Remove ChooseMixStrategy. b00cbd76a wikiheaders.pl: create Unsupported.md file with list of functions undocumented in either the headers or the wiki 37e1fc3b5 wayland: Ensure that the toplevel window is recreated when switching decoration modes f2ca9a615 Added SDL_AUDIO_FRAMESIZE 53122593f Added SDL_AUDIO_BYTESIZE 544351c98 Sync SDL3 wiki -> header 2e7d2b94e Clarify that SDL_BlitSurface() ignores the width and height in dstrect a2c1984d3 Detect Simagic wheel bases as wheels (#8198) 1d8dfbb22 avoid type redefinition errors after PR/8181 266b91d2f Detect Logitech G923 Playstation as wheel G923 have two different versions - Xbox version is already present in the wheel list, but not the PS version. cde67ea49 Detect Logitech PRO Racing Wheel for Xbox (PC mode) as wheel Logitech PRO Racing Wheel have two different versions - for Playstation and Xbox. Vendor + Product ID for Playstation version already present in SDL sources, but not an Xbox version 3a932141e Restore audio format binary compatibility with SDL 2.0 e85206ffd wikiheaders.pl: add --rev= option to pass revision string 233789b0d Audio types have the same naming convention as other SDL endian types, e.g. [S|U][BITS][LE|BE] 36b5f3e35 Sync SDL3 wiki -> header 0e552761b Renamed AudioStreamSpeed to AudioStreamFrequencyRatio 47bcb078f Fixed some incorrect SDL_AUDIO_F32 uses 2833f2e7b Fixed OOB access in audio_convertAccuracy test 8387fae69 Sync SDL3 wiki -> header 832181345 docs: Add note about Wayland application icons 825d34475 Make sure that the same timestamp is used for all PS5 events from the same packet 9c1430324 Removed SDL_dataqueue 28b28bd8f Added audio_formatChange test a59152688 Try and avoid overflow when handling very large audio streams 5394a805f Improved testaudiostreamdynamicresample e55844274 Added SDL_(Get|Set)AudioStreamSpeed 43c3c5736 Track the formats of data in an SDL_AudioStream 337fed3df Tweaked ResampleFrame_SSE Use _mm_unpack(lo|hi)_ps instead of _mm_shuffle_ps fd7cd91dc audio: Mix multiple streams in float32 to prevent clipping. 9097573e3 audio: Choose a mixing strategy on each iteration. bbe2e012a Don't provide the SDL3 header path c17a35f09 Fixed typo 4f72255eb Fixed README.md link e0ab59754 Simplified SDL_main.h migration notes d44bde61e Added SDL migration information to the top level README.md 6ff31e10c metal: Add hint to select low power device instead of the default one (#8182) 8a8aed477 Make sure that we process touch events that position the mouse f84c87f20 Sync SDL3 wiki -> header a7eea9997 macOS: Don't raise the parent top-level window when raising a child window, only raise the child window to the top of the parent a5e721479 Add SDL_WINDOW_NOT_FOCUSABLE flag to set that the window should not be able to gain key focus b385dc3b6 n3dsaudio: Patched to compile. 4e0c7c91f audio: PlayDevice() should return an error code. a94d724f1 wayland: add SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL da5d93d3d wayland: don't define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_* macro's f002f7d12 ci: build emscripten with Debug buid type 3699b12ed audio: Fixed some "is_*" variables to be cleaner and/or more specific. 2471d8cc2 audio: Fixed logic error in SDL_OpenAudioDeviceStream. 1b03a2430 testsurround: fix order of arguments of callback 82db2b58f Renamed audio stream callback and moved the userdata parameter first 5bdad5210 Sync SDL3 wiki -> header 58c859f64 audio: Rename SDL_GetAudioStreamBinding to SDL_GetAudioStreamDevice. efd2023a7 audio: Fixed documentation. 1e775e0ee audio: Replace SDL_CreateAndBindAudioStream with SDL_OpenAudioDeviceStream. bd088c2f9 Revert "Clarify whether an audio function expects a physical or logical device ID" 82e481b52 Added --randmem test parameter ea68bb802 Add some additional checks to audio_convertAudio f8286df16 Fixed ResampleFrame_SSE doing unnecessary work b1d63be53 Fixed audio_resampleLoss test c191d6c30 Better Win32 transparent window support 923d612ca hidapi: sync macOS code with mainstream. 363f4fa9c avoid type redefinition errors after commit ee806597b9. 615824a80 Updated documentation now that SDL_GetAudioDevices() has been split into separate functions for output and capture devices 506a133d8 Clarify whether an audio function expects a physical or logical device ID 3b1d1e4e3 hidapi: sync the hidraw changes with mainstream f617918e0 cmake: check linkage to libusb too, instead of libusb.h presence only. 041dbd6b5 Fixed GetResamplerAvailableOutputFrames Non-euclidean division is a pain b49d0a607 x11: Avoid including full Vulkan headers. 4d2f9f3a3 yuv_rgb: Comment out unused code. 3c3486e2a wayland: Don't include full Vulkan headers when not necessary. f066bbe98 x11: Don't include system headers twice. d86d02bbb updated dynapi after SDL_GDKGetDefaultUser addition 4355f9cec Fixed warning C4389: '!=': signed/unsigned mismatch 5755de07a Fixed build warnings 0f80d47bb Fixed thread-safety warning ee806597b Removed SDL_vulkan_internal.h from SDL_sysvideo.h 34860b932 Fixed testautomation --filter pixels_allocFreeFormat 6f8a6a31c gdk: GetBasePath should be a UTF8 version of Win32 GetBasePath e30e5c77e Sync SDL3 wiki -> header c0cd8c814 gdk: Add SDL_GDKGetDefaultUser, SDL_GetPrefPath implementation 106abce69 Refactored GetAudioStreamDataInternal buffer handling The final conversion step should now always go straight into the output buffer. e44f54ec5 Avoid using hex-floats 5b696996c Added ResampleFrame_SSE 958b3cfae Tweaked and enabled audio_convertAudio test 7dbb9b65b audio_convertAccuracy: Shuffle the data in case of a bad SIMD implementation f6a4080ff audio_resampleLoss: Add support for multiple channels 4f894e748 audio_resampleLoss: SDL_GetAudioStreamData now returns the correct length ab83f75bb Make sure GetAudioStreamDataInternal is called with a valid length 6a73f74b6 Rebuild full ResamplerFilter (left wing + right wing) at runtime 0c15ce006 Add a missing int cast b74ee86b1 Optimized ResampleAudio, with special cases for 1 and 2 channels This would also benefit from some SIMD, since it's just a bunch of multiply-adds fba6e1e3d Removed ResamplerFilterDifference It takes 1 extra multiply to calculate the correct interpolation, but I think the improvement in cache locality (and binary size) outweighs that. 9f7a22fa4 Removed 64-bit handling from AudioConvertByteswap 1f5327a9f Removed future_buffer, left_padding, and right_padding from SDL_AudioStream 71ad52d6d Lowered SDL_GetAudioStreamData to 32 KB No particular reason for this number, but 1 MB was a bit silly 69aec8c91 Fixed the report format for the Razer Wolverine V2 Pro 7c2669c9d Accept key events from any source 1e9d31448 Updated to Android minSdkVersion 19 and targetSdkVersion 34 8924d0d92 Added missing function prototype for SDL_WriteS64BE() 845f3c745 Fixed mismatch between stdlib calloc() and SDL free() fb7921173 emscriptenaudio: Fire the capture silence_callback at an interval. 5191b2054 emscriptenaudio: Don't bother undefining things about to be unreachable. fd75a4ca0 emscriptenaudio: Deal with blocked audio devices better. 981b8a337 emscriptenaudio: Remove unnecessary functions. c7588e426 Transparent window for Win32 + OpenGL (#8143) f9581178d cmake: fixed a typo. e6c878824 Fixed ResampleAudio interpolation factor calculation 498363863 Misc audio tweaks/cleanup 72d9d53de Invert the inner ResampleAudio loops to avoid doing unnecessary work 88123a510 The history buffer should always have the maximum possible padding frames 96e47f165 Clamp results of GetResampler(AvailableOutput|NeededInput)Frames d2b9c8b80 Fixed maths in testaudiostreamdynamicresample (and just show the actual scale) 14e38b17d Removed assertions from inner ResampleAudio loop 9d413dfdc The history buffer doesn't need to be so large 2788e848f Allow resampling less than 1 frame of input 383084e0a Pre-calculate resampling rate, and use it instead of .freq in most places 40a6a445c Update resample_offset inside ResampleAudio 47fea7f06 Used fixed-point arithmetic in ResampleAudio 7bb4e806e Clear resample_offset in SDL_ClearAudioStream, not SetAudioStreamFormat Not entirely sure if ClearAudioStream is the right place, but SetAudioStreamFormat was the wrong place b9541b9ea Improved ResampleAudio * filterindex2 was off-by-one * Generate ResamplerFilter using doubles * Transpose ResamplerFilter to improve access patterns cdaa19869 Track offset within the current sample when resampling d60ebb06d mouse: Ensure that the dummy default cursor is removed from the cursor list e58c2731f mouse: Free the default cursor when destroyed 789ce17e1 audio: Don't resample in chunks for now. cbab33482 audio: Don't call SDL_AudioStream callbacks for empty data sets. 3e1ae0c86 Clearified the libusb whitelist default logic f4520821e Removed some unnecessary integer casts 0989b7e86 Avoid using designated initializers c6c1e673c Optimized SDL_Convert_*_to_*_Scalar f97b920b3 Optimized SDL_Convert_*_to_*_SSE2 Some of the SDL_Convert_F32_to_*_SSE2 do not explicitly clamp the input, but instead rely on saturating casts. Inputs very far outside the valid [-1.0, 1.0] range may produce an incorrect result, but I believe that is an acceptable trade-off. 300d1ec3e Added audio_convertAccuracy test 32cecc2ea Fixed assertion in audio_convertAudio 33f11e21e Removed assertions in AudioConvert(To|From)Float c2f388fd8 cmake: add SDL_HIDAPI_LIBUSB_SHARED option + test on ci 371cc2d17 wayland: Remove unnecessary flag and state settings fe85e6e75 cocoa: Send a maximized event instead of restored if a deminiaturized window is zoomed ddddcb78c cocoa: Use the close method to hide a miniaturized window be8c42cfd Clarify that a window being 'hidden' means that it is unmapped/ordered out a44338cbc Fix typo in SDL_audiocvt.c f464eb2c5 SDL_hidapi.c: change 'use_libusb_whitelist_default' into a macro. 6607a3cfa Disable cache in python http server 181d5d285 hidapi: Enable libusb support by default. f0f15e365 hidapi: Use a whitelist for libusb when other backends are available c3f7a7dc4 Convert audio using SDL_AUDIO_F32SYS format instead of SDL_AUDIO_F32 796713b9d xxd.py: always write \n line endings 723bcd0a8 SDL_TriggerBreakppoint for riscv arch (both 32/64) version. git-subtree-dir: external/sdl/SDL git-subtree-split: 399bc709b7485bab57880f8261f826f29dc0d7b2
1234 lines
42 KiB
C
1234 lines
42 KiB
C
/*
|
|
Simple DirectMedia Layer
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
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_c.h"
|
|
|
|
#include "SDL_audioqueue.h"
|
|
#include "SDL_audioresample.h"
|
|
|
|
#ifndef SDL_INT_MAX
|
|
#define SDL_INT_MAX ((int)(~0u>>1))
|
|
#endif
|
|
|
|
/*
|
|
* CHANNEL LAYOUTS AS SDL EXPECTS THEM:
|
|
*
|
|
* (Even if the platform expects something else later, that
|
|
* SDL will swizzle between the app and the platform).
|
|
*
|
|
* Abbreviations:
|
|
* - FRONT=single mono speaker
|
|
* - FL=front left speaker
|
|
* - FR=front right speaker
|
|
* - FC=front center speaker
|
|
* - BL=back left speaker
|
|
* - BR=back right speaker
|
|
* - SR=surround right speaker
|
|
* - SL=surround left speaker
|
|
* - BC=back center speaker
|
|
* - LFE=low-frequency speaker
|
|
*
|
|
* These are listed in the order they are laid out in
|
|
* memory, so "FL+FR" means "the front left speaker is
|
|
* layed out in memory first, then the front right, then
|
|
* it repeats for the next audio frame".
|
|
*
|
|
* 1 channel (mono) layout: FRONT
|
|
* 2 channels (stereo) layout: FL+FR
|
|
* 3 channels (2.1) layout: FL+FR+LFE
|
|
* 4 channels (quad) layout: FL+FR+BL+BR
|
|
* 5 channels (4.1) layout: FL+FR+LFE+BL+BR
|
|
* 6 channels (5.1) layout: FL+FR+FC+LFE+BL+BR
|
|
* 7 channels (6.1) layout: FL+FR+FC+LFE+BC+SL+SR
|
|
* 8 channels (7.1) layout: FL+FR+FC+LFE+BL+BR+SL+SR
|
|
*/
|
|
|
|
#ifdef SDL_SSE3_INTRINSICS
|
|
// Convert from stereo to mono. Average left and right.
|
|
static void SDL_TARGETING("sse3") SDL_ConvertStereoToMono_SSE3(float *dst, const float *src, int num_frames)
|
|
{
|
|
LOG_DEBUG_AUDIO_CONVERT("stereo", "mono (using SSE3)");
|
|
|
|
const __m128 divby2 = _mm_set1_ps(0.5f);
|
|
int i = num_frames;
|
|
|
|
/* Do SSE blocks as long as we have 16 bytes available.
|
|
Just use unaligned load/stores, if the memory at runtime is
|
|
aligned it'll be just as fast on modern processors */
|
|
while (i >= 4) { // 4 * float32
|
|
_mm_storeu_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_loadu_ps(src), _mm_loadu_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;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef SDL_SSE_INTRINSICS
|
|
// Convert from mono to stereo. Duplicate to stereo left and right.
|
|
static void SDL_TARGETING("sse") SDL_ConvertMonoToStereo_SSE(float *dst, const float *src, int num_frames)
|
|
{
|
|
LOG_DEBUG_AUDIO_CONVERT("mono", "stereo (using SSE)");
|
|
|
|
// convert backwards, since output is growing in-place.
|
|
src += (num_frames-4) * 1;
|
|
dst += (num_frames-4) * 2;
|
|
|
|
/* Do SSE blocks as long as we have 16 bytes available.
|
|
Just use unaligned load/stores, if the memory at runtime is
|
|
aligned it'll be just as fast on modern processors */
|
|
// convert backwards, since output is growing in-place.
|
|
int i = num_frames;
|
|
while (i >= 4) { // 4 * float32
|
|
const __m128 input = _mm_loadu_ps(src); // A B C D
|
|
_mm_storeu_ps(dst, _mm_unpacklo_ps(input, input)); // A A B B
|
|
_mm_storeu_ps(dst + 4, _mm_unpackhi_ps(input, input)); // C C D D
|
|
i -= 4;
|
|
src -= 4;
|
|
dst -= 8;
|
|
}
|
|
|
|
// Finish off any leftovers with scalar operations.
|
|
src += 3;
|
|
dst += 6; // adjust for smaller buffers.
|
|
while (i) { // convert backwards, since output is growing in-place.
|
|
const float srcFC = src[0];
|
|
dst[1] /* FR */ = srcFC;
|
|
dst[0] /* FL */ = srcFC;
|
|
i--;
|
|
src--;
|
|
dst -= 2;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Include the autogenerated channel converters...
|
|
#include "SDL_audio_channel_converters.h"
|
|
|
|
|
|
static void AudioConvertByteswap(void *dst, const void *src, int num_samples, int bitsize)
|
|
{
|
|
#if DEBUG_AUDIO_CONVERT
|
|
SDL_Log("SDL_AUDIO_CONVERT: Converting %d-bit byte order", bitsize);
|
|
#endif
|
|
|
|
switch (bitsize) {
|
|
#define CASESWAP(b) \
|
|
case b: { \
|
|
const Uint##b *tsrc = (const Uint##b *)src; \
|
|
Uint##b *tdst = (Uint##b *)dst; \
|
|
for (int i = 0; i < num_samples; i++) { \
|
|
tdst[i] = SDL_Swap##b(tsrc[i]); \
|
|
} \
|
|
break; \
|
|
}
|
|
|
|
CASESWAP(16);
|
|
CASESWAP(32);
|
|
|
|
#undef CASESWAP
|
|
|
|
default:
|
|
SDL_assert(!"unhandled byteswap datatype!");
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void AudioConvertToFloat(float *dst, const void *src, int num_samples, SDL_AudioFormat src_fmt)
|
|
{
|
|
// Endian conversion is handled separately
|
|
switch (src_fmt & ~SDL_AUDIO_MASK_BIG_ENDIAN) {
|
|
case SDL_AUDIO_S8: SDL_Convert_S8_to_F32(dst, (const Sint8 *) src, num_samples); break;
|
|
case SDL_AUDIO_U8: SDL_Convert_U8_to_F32(dst, (const Uint8 *) src, num_samples); break;
|
|
case SDL_AUDIO_S16LE: SDL_Convert_S16_to_F32(dst, (const Sint16 *) src, num_samples); break;
|
|
case SDL_AUDIO_S32LE: SDL_Convert_S32_to_F32(dst, (const Sint32 *) src, num_samples); break;
|
|
default: SDL_assert(!"Unexpected audio format!"); break;
|
|
}
|
|
}
|
|
|
|
static void AudioConvertFromFloat(void *dst, const float *src, int num_samples, SDL_AudioFormat dst_fmt)
|
|
{
|
|
// Endian conversion is handled separately
|
|
switch (dst_fmt & ~SDL_AUDIO_MASK_BIG_ENDIAN) {
|
|
case SDL_AUDIO_S8: SDL_Convert_F32_to_S8((Sint8 *) dst, src, num_samples); break;
|
|
case SDL_AUDIO_U8: SDL_Convert_F32_to_U8((Uint8 *) dst, src, num_samples); break;
|
|
case SDL_AUDIO_S16LE: SDL_Convert_F32_to_S16((Sint16 *) dst, src, num_samples); break;
|
|
case SDL_AUDIO_S32LE: SDL_Convert_F32_to_S32((Sint32 *) dst, src, num_samples); break;
|
|
default: SDL_assert(!"Unexpected audio format!"); break;
|
|
}
|
|
}
|
|
|
|
static SDL_bool SDL_IsSupportedAudioFormat(const SDL_AudioFormat fmt)
|
|
{
|
|
switch (fmt) {
|
|
case SDL_AUDIO_U8:
|
|
case SDL_AUDIO_S8:
|
|
case SDL_AUDIO_S16LE:
|
|
case SDL_AUDIO_S16BE:
|
|
case SDL_AUDIO_S32LE:
|
|
case SDL_AUDIO_S32BE:
|
|
case SDL_AUDIO_F32LE:
|
|
case SDL_AUDIO_F32BE:
|
|
return SDL_TRUE; // supported.
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return SDL_FALSE; // unsupported.
|
|
}
|
|
|
|
static SDL_bool SDL_IsSupportedChannelCount(const int channels)
|
|
{
|
|
return ((channels >= 1) && (channels <= 8)) ? SDL_TRUE : SDL_FALSE;
|
|
}
|
|
|
|
|
|
// This does type and channel conversions _but not resampling_ (resampling happens in SDL_AudioStream).
|
|
// This does not check parameter validity, (beyond asserts), it expects you did that already!
|
|
// All of this has to function as if src==dst==scratch (conversion in-place), but as a convenience
|
|
// if you're just going to copy the final output elsewhere, you can specify a different output pointer.
|
|
//
|
|
// The scratch buffer must be able to store `num_frames * CalculateMaxSampleFrameSize(src_format, src_channels, dst_format, dst_channels)` bytes.
|
|
// If the scratch buffer is NULL, this restriction applies to the output buffer instead.
|
|
void ConvertAudio(int num_frames, const void *src, SDL_AudioFormat src_format, int src_channels,
|
|
void *dst, SDL_AudioFormat dst_format, int dst_channels, void* scratch)
|
|
{
|
|
SDL_assert(src != NULL);
|
|
SDL_assert(dst != NULL);
|
|
SDL_assert(SDL_IsSupportedAudioFormat(src_format));
|
|
SDL_assert(SDL_IsSupportedAudioFormat(dst_format));
|
|
SDL_assert(SDL_IsSupportedChannelCount(src_channels));
|
|
SDL_assert(SDL_IsSupportedChannelCount(dst_channels));
|
|
|
|
if (!num_frames) {
|
|
return; // no data to convert, quit.
|
|
}
|
|
|
|
#if DEBUG_AUDIO_CONVERT
|
|
SDL_Log("SDL_AUDIO_CONVERT: Convert format %04x->%04x, channels %u->%u", src_format, dst_format, src_channels, dst_channels);
|
|
#endif
|
|
|
|
const int src_bitsize = (int) SDL_AUDIO_BITSIZE(src_format);
|
|
const int dst_bitsize = (int) SDL_AUDIO_BITSIZE(dst_format);
|
|
|
|
const int dst_sample_frame_size = (dst_bitsize / 8) * dst_channels;
|
|
|
|
/* Type conversion goes like this now:
|
|
- byteswap to CPU native format first if necessary.
|
|
- convert to native Float32 if necessary.
|
|
- change channel count if necessary.
|
|
- convert to final data 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_channels == dst_channels) {
|
|
if (src_format == dst_format) {
|
|
// nothing to do, we're already in the right format, just copy it over if necessary.
|
|
if (src != dst) {
|
|
SDL_memcpy(dst, src, num_frames * dst_sample_frame_size);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// just a byteswap needed?
|
|
if ((src_format & ~SDL_AUDIO_MASK_BIG_ENDIAN) == (dst_format & ~SDL_AUDIO_MASK_BIG_ENDIAN)) {
|
|
if (src_bitsize == 8) {
|
|
if (src != dst) {
|
|
SDL_memcpy(dst, src, num_frames * dst_sample_frame_size);
|
|
}
|
|
return; // nothing to do, it's a 1-byte format.
|
|
}
|
|
AudioConvertByteswap(dst, src, num_frames * src_channels, src_bitsize);
|
|
return; // all done.
|
|
}
|
|
}
|
|
|
|
if (scratch == NULL) {
|
|
scratch = dst;
|
|
}
|
|
|
|
const SDL_bool srcbyteswap = (SDL_AUDIO_ISBIGENDIAN(src_format) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN) && (src_bitsize > 8);
|
|
const SDL_bool srcconvert = !SDL_AUDIO_ISFLOAT(src_format);
|
|
const SDL_bool channelconvert = src_channels != dst_channels;
|
|
const SDL_bool dstconvert = !SDL_AUDIO_ISFLOAT(dst_format);
|
|
const SDL_bool dstbyteswap = (SDL_AUDIO_ISBIGENDIAN(dst_format) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN) && (dst_bitsize > 8);
|
|
|
|
// make sure we're in native byte order.
|
|
if (srcbyteswap) {
|
|
// No point writing straight to dst. If we only need a byteswap, we wouldn't be bere.
|
|
AudioConvertByteswap(scratch, src, num_frames * src_channels, src_bitsize);
|
|
src = scratch;
|
|
}
|
|
|
|
// get us to float format.
|
|
if (srcconvert) {
|
|
void* buf = (channelconvert || dstconvert || dstbyteswap) ? scratch : dst;
|
|
AudioConvertToFloat((float *) buf, src, num_frames * src_channels, src_format);
|
|
src = buf;
|
|
}
|
|
|
|
// Channel conversion
|
|
|
|
if (channelconvert) {
|
|
SDL_AudioChannelConverter channel_converter;
|
|
SDL_AudioChannelConverter override = NULL;
|
|
|
|
// SDL_IsSupportedChannelCount should have caught these asserts, or we added a new format and forgot to update the table.
|
|
SDL_assert(src_channels <= SDL_arraysize(channel_converters));
|
|
SDL_assert(dst_channels <= SDL_arraysize(channel_converters[0]));
|
|
|
|
channel_converter = channel_converters[src_channels - 1][dst_channels - 1];
|
|
SDL_assert(channel_converter != NULL);
|
|
|
|
// swap in some SIMD versions for a few of these.
|
|
if (channel_converter == SDL_ConvertStereoToMono) {
|
|
#ifdef SDL_SSE3_INTRINSICS
|
|
if (!override && SDL_HasSSE3()) { override = SDL_ConvertStereoToMono_SSE3; }
|
|
#endif
|
|
} else if (channel_converter == SDL_ConvertMonoToStereo) {
|
|
#ifdef SDL_SSE_INTRINSICS
|
|
if (!override && SDL_HasSSE()) { override = SDL_ConvertMonoToStereo_SSE; }
|
|
#endif
|
|
}
|
|
|
|
if (override) {
|
|
channel_converter = override;
|
|
}
|
|
|
|
void* buf = (dstconvert || dstbyteswap) ? scratch : dst;
|
|
channel_converter((float *) buf, (const float *) src, num_frames);
|
|
src = buf;
|
|
}
|
|
|
|
// Resampling is not done in here. SDL_AudioStream handles that.
|
|
|
|
// Move to final data type.
|
|
if (dstconvert) {
|
|
AudioConvertFromFloat(dst, (const float *) src, num_frames * dst_channels, dst_format);
|
|
src = dst;
|
|
}
|
|
|
|
// make sure we're in final byte order.
|
|
if (dstbyteswap) {
|
|
AudioConvertByteswap(dst, src, num_frames * dst_channels, dst_bitsize);
|
|
src = dst; // we've written to dst, future work will convert in-place.
|
|
}
|
|
|
|
SDL_assert(src == dst); // if we got here, we _had_ to have done _something_. Otherwise, we should have memcpy'd!
|
|
}
|
|
|
|
// Calculate the largest frame size needed to convert between the two formats.
|
|
static int CalculateMaxFrameSize(SDL_AudioFormat src_format, int src_channels, SDL_AudioFormat dst_format, int dst_channels)
|
|
{
|
|
const int src_format_size = SDL_AUDIO_BYTESIZE(src_format);
|
|
const int dst_format_size = SDL_AUDIO_BYTESIZE(dst_format);
|
|
const int max_app_format_size = SDL_max(src_format_size, dst_format_size);
|
|
const int max_format_size = SDL_max(max_app_format_size, sizeof (float)); // ConvertAudio and ResampleAudio use floats.
|
|
const int max_channels = SDL_max(src_channels, dst_channels);
|
|
return max_format_size * max_channels;
|
|
}
|
|
|
|
static Sint64 GetAudioStreamResampleRate(SDL_AudioStream* stream, int src_freq, Sint64 resample_offset)
|
|
{
|
|
src_freq = (int)((float)src_freq * stream->freq_ratio);
|
|
|
|
Sint64 resample_rate = SDL_GetResampleRate(src_freq, stream->dst_spec.freq);
|
|
|
|
// If src_freq == dst_freq, and we aren't between frames, don't resample
|
|
if ((resample_rate == 0x100000000) && (resample_offset == 0)) {
|
|
resample_rate = 0;
|
|
}
|
|
|
|
return resample_rate;
|
|
}
|
|
|
|
static int UpdateAudioStreamInputSpec(SDL_AudioStream *stream, const SDL_AudioSpec *spec)
|
|
{
|
|
if (AUDIO_SPECS_EQUAL(stream->input_spec, *spec)) {
|
|
return 0;
|
|
}
|
|
|
|
const size_t history_buffer_allocation = SDL_GetResamplerHistoryFrames() * SDL_AUDIO_FRAMESIZE(*spec);
|
|
Uint8 *history_buffer = stream->history_buffer;
|
|
|
|
if (stream->history_buffer_allocation < history_buffer_allocation) {
|
|
history_buffer = (Uint8 *) SDL_aligned_alloc(SDL_SIMDGetAlignment(), history_buffer_allocation);
|
|
if (!history_buffer) {
|
|
return SDL_OutOfMemory();
|
|
}
|
|
SDL_aligned_free(stream->history_buffer);
|
|
stream->history_buffer = history_buffer;
|
|
stream->history_buffer_allocation = history_buffer_allocation;
|
|
}
|
|
|
|
SDL_memset(history_buffer, SDL_GetSilenceValueForFormat(spec->format), history_buffer_allocation);
|
|
SDL_copyp(&stream->input_spec, spec);
|
|
|
|
return 0;
|
|
}
|
|
|
|
SDL_AudioStream *SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
|
|
{
|
|
if (!SDL_WasInit(SDL_INIT_AUDIO)) {
|
|
SDL_SetError("Audio subsystem is not initialized");
|
|
return NULL;
|
|
}
|
|
|
|
SDL_AudioStream *retval = (SDL_AudioStream *)SDL_calloc(1, sizeof(SDL_AudioStream));
|
|
if (retval == NULL) {
|
|
SDL_OutOfMemory();
|
|
return NULL;
|
|
}
|
|
|
|
retval->freq_ratio = 1.0f;
|
|
retval->queue = SDL_CreateAudioQueue(4096);
|
|
|
|
if (retval->queue == NULL) {
|
|
SDL_free(retval);
|
|
return NULL;
|
|
}
|
|
|
|
retval->lock = SDL_CreateMutex();
|
|
if (retval->lock == NULL) {
|
|
SDL_free(retval->queue);
|
|
SDL_free(retval);
|
|
return NULL;
|
|
}
|
|
|
|
OnAudioStreamCreated(retval);
|
|
|
|
if (SDL_SetAudioStreamFormat(retval, src_spec, dst_spec) == -1) {
|
|
SDL_DestroyAudioStream(retval);
|
|
return NULL;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
int SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
SDL_LockMutex(stream->lock);
|
|
stream->get_callback = callback;
|
|
stream->get_callback_userdata = userdata;
|
|
SDL_UnlockMutex(stream->lock);
|
|
return 0;
|
|
}
|
|
|
|
int SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
SDL_LockMutex(stream->lock);
|
|
stream->put_callback = callback;
|
|
stream->put_callback_userdata = userdata;
|
|
SDL_UnlockMutex(stream->lock);
|
|
return 0;
|
|
}
|
|
|
|
int SDL_LockAudioStream(SDL_AudioStream *stream)
|
|
{
|
|
return stream ? SDL_LockMutex(stream->lock) : SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
int SDL_UnlockAudioStream(SDL_AudioStream *stream)
|
|
{
|
|
return stream ? SDL_UnlockMutex(stream->lock) : SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
int SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
if (src_spec) {
|
|
SDL_copyp(src_spec, &stream->src_spec);
|
|
}
|
|
if (dst_spec) {
|
|
SDL_copyp(dst_spec, &stream->dst_spec);
|
|
}
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
if (src_spec && src_spec->format == 0) {
|
|
return SDL_SetError("Stream has no source format");
|
|
} else if (dst_spec && dst_spec->format == 0) {
|
|
return SDL_SetError("Stream has no destination format");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
// Picked mostly arbitrarily.
|
|
static const int min_freq = 4000;
|
|
static const int max_freq = 384000;
|
|
|
|
if (src_spec) {
|
|
if (!SDL_IsSupportedAudioFormat(src_spec->format)) {
|
|
return SDL_InvalidParamError("src_spec->format");
|
|
} else if (!SDL_IsSupportedChannelCount(src_spec->channels)) {
|
|
return SDL_InvalidParamError("src_spec->channels");
|
|
} else if (src_spec->freq <= 0) {
|
|
return SDL_InvalidParamError("src_spec->freq");
|
|
} else if (src_spec->freq < min_freq) {
|
|
return SDL_SetError("Source rate is too low");
|
|
} else if (src_spec->freq > max_freq) {
|
|
return SDL_SetError("Source rate is too high");
|
|
}
|
|
}
|
|
|
|
if (dst_spec) {
|
|
if (!SDL_IsSupportedAudioFormat(dst_spec->format)) {
|
|
return SDL_InvalidParamError("dst_spec->format");
|
|
} else if (!SDL_IsSupportedChannelCount(dst_spec->channels)) {
|
|
return SDL_InvalidParamError("dst_spec->channels");
|
|
} else if (dst_spec->freq <= 0) {
|
|
return SDL_InvalidParamError("dst_spec->freq");
|
|
} else if (dst_spec->freq < min_freq) {
|
|
return SDL_SetError("Destination rate is too low");
|
|
} else if (dst_spec->freq > max_freq) {
|
|
return SDL_SetError("Destination rate is too high");
|
|
}
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
|
|
// quietly refuse to change the format of the end currently bound to a device.
|
|
if (stream->bound_device) {
|
|
if (stream->bound_device->physical_device->iscapture) {
|
|
dst_spec = NULL;
|
|
} else {
|
|
src_spec = NULL;
|
|
}
|
|
}
|
|
|
|
if (src_spec) {
|
|
SDL_copyp(&stream->src_spec, src_spec);
|
|
}
|
|
|
|
if (dst_spec) {
|
|
SDL_copyp(&stream->dst_spec, dst_spec);
|
|
}
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
float SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream)
|
|
{
|
|
if (!stream) {
|
|
SDL_InvalidParamError("stream");
|
|
return 0.0f;
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
float freq_ratio = stream->freq_ratio;
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
return freq_ratio;
|
|
}
|
|
|
|
int SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float freq_ratio)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
// Picked mostly arbitrarily.
|
|
static const float min_freq_ratio = 0.01f;
|
|
static const float max_freq_ratio = 100.0f;
|
|
|
|
if (freq_ratio < min_freq_ratio) {
|
|
return SDL_SetError("Frequency ratio is too low");
|
|
} else if (freq_ratio > max_freq_ratio) {
|
|
return SDL_SetError("Frequency ratio is too high");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
stream->freq_ratio = freq_ratio;
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int CheckAudioStreamIsFullySetup(SDL_AudioStream *stream)
|
|
{
|
|
if (stream->src_spec.format == 0) {
|
|
return SDL_SetError("Stream has no source format");
|
|
} else if (stream->dst_spec.format == 0) {
|
|
return SDL_SetError("Stream has no destination format");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
|
|
{
|
|
#if DEBUG_AUDIOSTREAM
|
|
SDL_Log("AUDIOSTREAM: wants to put %d bytes", len);
|
|
#endif
|
|
|
|
if (stream == NULL) {
|
|
return SDL_InvalidParamError("stream");
|
|
} else if (buf == NULL) {
|
|
return SDL_InvalidParamError("buf");
|
|
} else if (len < 0) {
|
|
return SDL_InvalidParamError("len");
|
|
} else if (len == 0) {
|
|
return 0; // nothing to do.
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
|
|
if (CheckAudioStreamIsFullySetup(stream) != 0) {
|
|
SDL_UnlockMutex(stream->lock);
|
|
return -1;
|
|
}
|
|
|
|
if ((len % SDL_AUDIO_FRAMESIZE(stream->src_spec)) != 0) {
|
|
SDL_UnlockMutex(stream->lock);
|
|
return SDL_SetError("Can't add partial sample frames");
|
|
}
|
|
|
|
SDL_AudioTrack* track = NULL;
|
|
|
|
// When copying in large amounts of data, try and do as much work as possible
|
|
// outside of the stream lock, otherwise the output device is likely to be starved.
|
|
const int large_input_thresh = 1024 * 1024;
|
|
|
|
if (len >= large_input_thresh) {
|
|
SDL_AudioSpec src_spec;
|
|
SDL_copyp(&src_spec, &stream->src_spec);
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
size_t chunk_size = SDL_GetAudioQueueChunkSize(stream->queue);
|
|
track = SDL_CreateChunkedAudioTrack(&src_spec, buf, len, chunk_size);
|
|
|
|
if (track == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
}
|
|
|
|
const int prev_available = stream->put_callback ? SDL_GetAudioStreamAvailable(stream) : 0;
|
|
|
|
int retval = 0;
|
|
|
|
if (track != NULL) {
|
|
SDL_AddTrackToAudioQueue(stream->queue, track);
|
|
} else {
|
|
retval = SDL_WriteToAudioQueue(stream->queue, &stream->src_spec, buf, len);
|
|
}
|
|
|
|
if (retval == 0) {
|
|
stream->total_bytes_queued += len;
|
|
if (stream->put_callback) {
|
|
const int newavail = SDL_GetAudioStreamAvailable(stream) - prev_available;
|
|
stream->put_callback(stream->put_callback_userdata, stream, newavail, newavail);
|
|
}
|
|
}
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
return retval;
|
|
}
|
|
|
|
int SDL_FlushAudioStream(SDL_AudioStream *stream)
|
|
{
|
|
if (stream == NULL) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
SDL_FlushAudioQueue(stream->queue);
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* this does not save the previous contents of stream->work_buffer. It's a work buffer!!
|
|
The returned buffer is aligned/padded for use with SIMD instructions. */
|
|
static Uint8 *EnsureAudioStreamWorkBufferSize(SDL_AudioStream *stream, size_t newlen)
|
|
{
|
|
if (stream->work_buffer_allocation >= newlen) {
|
|
return stream->work_buffer;
|
|
}
|
|
|
|
Uint8 *ptr = (Uint8 *) SDL_aligned_alloc(SDL_SIMDGetAlignment(), newlen);
|
|
if (ptr == NULL) {
|
|
SDL_OutOfMemory();
|
|
return NULL; // previous work buffer is still valid!
|
|
}
|
|
|
|
SDL_aligned_free(stream->work_buffer);
|
|
stream->work_buffer = ptr;
|
|
stream->work_buffer_allocation = newlen;
|
|
return ptr;
|
|
}
|
|
|
|
static void UpdateAudioStreamHistoryBuffer(SDL_AudioStream* stream,
|
|
Uint8* input_buffer, int input_bytes, Uint8* left_padding, int padding_bytes)
|
|
{
|
|
const int history_buffer_frames = SDL_GetResamplerHistoryFrames();
|
|
|
|
// Even if we aren't currently resampling, we always need to update the history buffer
|
|
Uint8 *history_buffer = stream->history_buffer;
|
|
int history_bytes = history_buffer_frames * SDL_AUDIO_FRAMESIZE(stream->input_spec);
|
|
|
|
if (left_padding != NULL) {
|
|
// Fill in the left padding using the history buffer
|
|
SDL_assert(padding_bytes <= history_bytes);
|
|
SDL_memcpy(left_padding, history_buffer + history_bytes - padding_bytes, padding_bytes);
|
|
}
|
|
|
|
// Update the history buffer using the new input data
|
|
if (input_bytes >= history_bytes) {
|
|
SDL_memcpy(history_buffer, input_buffer + (input_bytes - history_bytes), history_bytes);
|
|
} else {
|
|
int preserve_bytes = history_bytes - input_bytes;
|
|
SDL_memmove(history_buffer, history_buffer + input_bytes, preserve_bytes);
|
|
SDL_memcpy(history_buffer + preserve_bytes, input_buffer, input_bytes);
|
|
}
|
|
}
|
|
|
|
static Sint64 NextAudioStreamIter(SDL_AudioStream* stream, void** inout_iter,
|
|
Sint64* inout_resample_offset, SDL_AudioSpec* out_spec, SDL_bool* out_flushed)
|
|
{
|
|
SDL_AudioSpec spec;
|
|
SDL_bool flushed;
|
|
size_t queued_bytes = SDL_NextAudioQueueIter(stream->queue, inout_iter, &spec, &flushed);
|
|
|
|
if (out_spec) {
|
|
SDL_copyp(out_spec, &spec);
|
|
}
|
|
|
|
// There is infinite audio available, whether or not we are resampling
|
|
if (queued_bytes == SDL_SIZE_MAX) {
|
|
*inout_resample_offset = 0;
|
|
|
|
if (out_flushed) {
|
|
*out_flushed = SDL_FALSE;
|
|
}
|
|
|
|
return SDL_MAX_SINT32;
|
|
}
|
|
|
|
Sint64 resample_offset = *inout_resample_offset;
|
|
Sint64 resample_rate = GetAudioStreamResampleRate(stream, spec.freq, resample_offset);
|
|
Sint64 output_frames = (Sint64)(queued_bytes / SDL_AUDIO_FRAMESIZE(spec));
|
|
|
|
if (resample_rate) {
|
|
// Resampling requires padding frames to the left and right of the current position.
|
|
// Past the end of the track, the right padding is filled with silence.
|
|
// But we only want to do that if the track is actually finished (flushed).
|
|
if (!flushed) {
|
|
output_frames -= SDL_GetResamplerPaddingFrames(resample_rate);
|
|
}
|
|
|
|
output_frames = SDL_GetResamplerOutputFrames(output_frames, resample_rate, &resample_offset);
|
|
}
|
|
|
|
if (flushed) {
|
|
resample_offset = 0;
|
|
}
|
|
|
|
*inout_resample_offset = resample_offset;
|
|
|
|
if (out_flushed) {
|
|
*out_flushed = flushed;
|
|
}
|
|
|
|
return output_frames;
|
|
}
|
|
|
|
static Sint64 GetAudioStreamAvailableFrames(SDL_AudioStream* stream, Sint64* out_resample_offset)
|
|
{
|
|
void* iter = SDL_BeginAudioQueueIter(stream->queue);
|
|
|
|
Sint64 resample_offset = stream->resample_offset;
|
|
Sint64 output_frames = 0;
|
|
|
|
while (iter) {
|
|
output_frames += NextAudioStreamIter(stream, &iter, &resample_offset, NULL, NULL);
|
|
|
|
// Already got loads of frames. Just clamp it to something reasonable
|
|
if (output_frames >= SDL_MAX_SINT32) {
|
|
output_frames = SDL_MAX_SINT32;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (out_resample_offset) {
|
|
*out_resample_offset = resample_offset;
|
|
}
|
|
|
|
return output_frames;
|
|
}
|
|
|
|
static Sint64 GetAudioStreamHead(SDL_AudioStream* stream, SDL_AudioSpec* out_spec, SDL_bool* out_flushed)
|
|
{
|
|
void* iter = SDL_BeginAudioQueueIter(stream->queue);
|
|
|
|
if (iter == NULL) {
|
|
SDL_zerop(out_spec);
|
|
*out_flushed = SDL_FALSE;
|
|
return 0;
|
|
}
|
|
|
|
Sint64 resample_offset = stream->resample_offset;
|
|
return NextAudioStreamIter(stream, &iter, &resample_offset, out_spec, out_flushed);
|
|
}
|
|
|
|
// You must hold stream->lock and validate your parameters before calling this!
|
|
// Enough input data MUST be available!
|
|
static int GetAudioStreamDataInternal(SDL_AudioStream *stream, void *buf, int output_frames)
|
|
{
|
|
const SDL_AudioSpec* src_spec = &stream->input_spec;
|
|
const SDL_AudioSpec* dst_spec = &stream->dst_spec;
|
|
|
|
const SDL_AudioFormat src_format = src_spec->format;
|
|
const int src_channels = src_spec->channels;
|
|
const int src_frame_size = SDL_AUDIO_FRAMESIZE(*src_spec);
|
|
|
|
const SDL_AudioFormat dst_format = dst_spec->format;
|
|
const int dst_channels = dst_spec->channels;
|
|
|
|
const int max_frame_size = CalculateMaxFrameSize(src_format, src_channels, dst_format, dst_channels);
|
|
const Sint64 resample_rate = GetAudioStreamResampleRate(stream, src_spec->freq, stream->resample_offset);
|
|
|
|
#if DEBUG_AUDIOSTREAM
|
|
SDL_Log("AUDIOSTREAM: asking for %d frames.", output_frames);
|
|
#endif
|
|
|
|
SDL_assert(output_frames > 0);
|
|
|
|
// Not resampling? It's an easy conversion (and maybe not even that!)
|
|
if (resample_rate == 0) {
|
|
Uint8* input_buffer = NULL;
|
|
|
|
// If no conversion is happening, read straight into the output buffer.
|
|
// Note, this is just to avoid extra copies.
|
|
// Some other formats may fit directly into the output buffer, but i'd rather process data in a SIMD-aligned buffer.
|
|
if ((src_format == dst_format) && (src_channels == dst_channels)) {
|
|
input_buffer = buf;
|
|
} else {
|
|
input_buffer = EnsureAudioStreamWorkBufferSize(stream, output_frames * max_frame_size);
|
|
|
|
if (!input_buffer) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
const int input_bytes = output_frames * src_frame_size;
|
|
if (SDL_ReadFromAudioQueue(stream->queue, input_buffer, input_bytes) != 0) {
|
|
SDL_assert(!"Not enough data in queue (read)");
|
|
}
|
|
|
|
stream->total_bytes_queued -= input_bytes;
|
|
|
|
// Even if we aren't currently resampling, we always need to update the history buffer
|
|
UpdateAudioStreamHistoryBuffer(stream, input_buffer, input_bytes, NULL, 0);
|
|
|
|
// Convert the data, if necessary
|
|
if (buf != input_buffer) {
|
|
ConvertAudio(output_frames, input_buffer, src_format, src_channels, buf, dst_format, dst_channels, input_buffer);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Time to do some resampling!
|
|
// Calculate the number of input frames necessary for this request.
|
|
// Because resampling happens "between" frames, The same number of output_frames
|
|
// can require a different number of input_frames, depending on the resample_offset.
|
|
// Infact, input_frames can sometimes even be zero when upsampling.
|
|
const int input_frames = (int) SDL_GetResamplerInputFrames(output_frames, resample_rate, stream->resample_offset);
|
|
const int input_bytes = input_frames * src_frame_size;
|
|
|
|
const int resampler_padding_frames = SDL_GetResamplerPaddingFrames(resample_rate);
|
|
|
|
// 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.
|
|
const int resample_channels = SDL_min(src_channels, dst_channels);
|
|
|
|
// The size of the frame used when resampling
|
|
const int resample_frame_size = resample_channels * sizeof(float);
|
|
|
|
// The main portion of the work_buffer can be used to store 3 things:
|
|
// src_sample_frame_size * (left_padding+input_buffer+right_padding)
|
|
// resample_frame_size * (left_padding+input_buffer+right_padding)
|
|
// dst_sample_frame_size * output_frames
|
|
//
|
|
// ResampleAudio also requires an additional buffer if it can't write straight to the output:
|
|
// resample_frame_size * output_frames
|
|
//
|
|
// Note, ConvertAudio requires (num_frames * max_sample_frame_size) of scratch space
|
|
const int work_buffer_frames = input_frames + (resampler_padding_frames * 2);
|
|
int work_buffer_capacity = work_buffer_frames * max_frame_size;
|
|
int resample_buffer_offset = -1;
|
|
|
|
// Check if we can resample directly into the output buffer.
|
|
// Note, this is just to avoid extra copies.
|
|
// Some other formats may fit directly into the output buffer, but i'd rather process data in a SIMD-aligned buffer.
|
|
if ((dst_format != SDL_AUDIO_F32) || (dst_channels != resample_channels)) {
|
|
// Allocate space for converting the resampled output to the destination format
|
|
int resample_convert_bytes = output_frames * max_frame_size;
|
|
work_buffer_capacity = SDL_max(work_buffer_capacity, resample_convert_bytes);
|
|
|
|
// SIMD-align the buffer
|
|
int simd_alignment = (int) SDL_SIMDGetAlignment();
|
|
work_buffer_capacity += simd_alignment - 1;
|
|
work_buffer_capacity -= work_buffer_capacity % simd_alignment;
|
|
|
|
// Allocate space for the resampled output
|
|
int resample_bytes = output_frames * resample_frame_size;
|
|
resample_buffer_offset = work_buffer_capacity;
|
|
work_buffer_capacity += resample_bytes;
|
|
}
|
|
|
|
Uint8* work_buffer = EnsureAudioStreamWorkBufferSize(stream, work_buffer_capacity);
|
|
|
|
if (!work_buffer) {
|
|
return -1;
|
|
}
|
|
|
|
const int padding_bytes = resampler_padding_frames * src_frame_size;
|
|
|
|
Uint8* work_buffer_tail = work_buffer;
|
|
|
|
// Split the work_buffer into [left_padding][input_buffer][right_padding]
|
|
Uint8* left_padding = work_buffer_tail;
|
|
work_buffer_tail += padding_bytes;
|
|
|
|
Uint8* input_buffer = work_buffer_tail;
|
|
work_buffer_tail += input_bytes;
|
|
|
|
Uint8* right_padding = work_buffer_tail;
|
|
work_buffer_tail += padding_bytes;
|
|
|
|
SDL_assert((work_buffer_tail - work_buffer) <= work_buffer_capacity);
|
|
|
|
// Now read unconverted data from the queue into the work buffer to fulfill the request.
|
|
if (SDL_ReadFromAudioQueue(stream->queue, input_buffer, input_bytes) != 0) {
|
|
SDL_assert(!"Not enough data in queue (resample read)");
|
|
}
|
|
stream->total_bytes_queued -= input_bytes;
|
|
|
|
// Update the history buffer and fill in the left padding
|
|
UpdateAudioStreamHistoryBuffer(stream, input_buffer, input_bytes, left_padding, padding_bytes);
|
|
|
|
// Fill in the right padding by peeking into the input queue (missing data is filled with silence)
|
|
if (SDL_PeekIntoAudioQueue(stream->queue, right_padding, padding_bytes) != 0) {
|
|
SDL_assert(!"Not enough data in queue (resample peek)");
|
|
}
|
|
|
|
SDL_assert(work_buffer_frames == input_frames + (resampler_padding_frames * 2));
|
|
|
|
// Resampling! get the work buffer to float32 format, etc, in-place.
|
|
ConvertAudio(work_buffer_frames, work_buffer, src_format, src_channels, work_buffer, SDL_AUDIO_F32, resample_channels, NULL);
|
|
|
|
// Update the work_buffer pointers based on the new frame size
|
|
input_buffer = work_buffer + ((input_buffer - work_buffer) / src_frame_size * resample_frame_size);
|
|
work_buffer_tail = work_buffer + ((work_buffer_tail - work_buffer) / src_frame_size * resample_frame_size);
|
|
SDL_assert((work_buffer_tail - work_buffer) <= work_buffer_capacity);
|
|
|
|
// Decide where the resampled output goes
|
|
void* resample_buffer = (resample_buffer_offset != -1) ? (work_buffer + resample_buffer_offset) : buf;
|
|
|
|
SDL_ResampleAudio(resample_channels,
|
|
(const float *) input_buffer, input_frames,
|
|
(float*) resample_buffer, output_frames,
|
|
resample_rate, &stream->resample_offset);
|
|
|
|
// Convert to the final format, if necessary
|
|
if (buf != resample_buffer) {
|
|
ConvertAudio(output_frames, resample_buffer, SDL_AUDIO_F32, resample_channels, buf, dst_format, dst_channels, work_buffer);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// get converted/resampled data from the stream
|
|
int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *voidbuf, int len)
|
|
{
|
|
Uint8 *buf = (Uint8 *) voidbuf;
|
|
|
|
#if DEBUG_AUDIOSTREAM
|
|
SDL_Log("AUDIOSTREAM: want to get %d converted bytes", len);
|
|
#endif
|
|
|
|
if (stream == NULL) {
|
|
return SDL_InvalidParamError("stream");
|
|
} else if (buf == NULL) {
|
|
return SDL_InvalidParamError("buf");
|
|
} else if (len < 0) {
|
|
return SDL_InvalidParamError("len");
|
|
} else if (len == 0) {
|
|
return 0; // nothing to do.
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
|
|
if (CheckAudioStreamIsFullySetup(stream) != 0) {
|
|
SDL_UnlockMutex(stream->lock);
|
|
return -1;
|
|
}
|
|
|
|
const int dst_frame_size = SDL_AUDIO_FRAMESIZE(stream->dst_spec);
|
|
|
|
len -= len % dst_frame_size; // chop off any fractional sample frame.
|
|
|
|
// give the callback a chance to fill in more stream data if it wants.
|
|
if (stream->get_callback) {
|
|
Sint64 total_request = len / dst_frame_size; // start with sample frames desired
|
|
Sint64 additional_request = total_request;
|
|
|
|
Sint64 resample_offset = 0;
|
|
Sint64 available_frames = GetAudioStreamAvailableFrames(stream, &resample_offset);
|
|
|
|
additional_request -= SDL_min(additional_request, available_frames);
|
|
|
|
Sint64 resample_rate = GetAudioStreamResampleRate(stream, stream->src_spec.freq, resample_offset);
|
|
|
|
if (resample_rate) {
|
|
total_request = SDL_GetResamplerInputFrames(total_request, resample_rate, resample_offset);
|
|
additional_request = SDL_GetResamplerInputFrames(additional_request, resample_rate, resample_offset);
|
|
}
|
|
|
|
total_request *= SDL_AUDIO_FRAMESIZE(stream->src_spec); // convert sample frames to bytes.
|
|
additional_request *= SDL_AUDIO_FRAMESIZE(stream->src_spec); // convert sample frames to bytes.
|
|
stream->get_callback(stream->get_callback_userdata, stream, (int) SDL_min(additional_request, SDL_INT_MAX), (int) SDL_min(total_request, SDL_INT_MAX));
|
|
}
|
|
|
|
// Process the data in chunks to avoid allocating too much memory (and potential integer overflows)
|
|
const int chunk_size = 4096;
|
|
|
|
int total = 0;
|
|
|
|
while (total < len) {
|
|
// Audio is processed a track at a time.
|
|
SDL_AudioSpec input_spec;
|
|
SDL_bool flushed;
|
|
const Sint64 available_frames = GetAudioStreamHead(stream, &input_spec, &flushed);
|
|
|
|
if (available_frames == 0) {
|
|
if (flushed) {
|
|
SDL_PopAudioQueueHead(stream->queue);
|
|
SDL_zero(stream->input_spec);
|
|
stream->resample_offset = 0;
|
|
continue;
|
|
}
|
|
// There are no frames available, but the track hasn't been flushed, so more might be added later.
|
|
break;
|
|
}
|
|
|
|
if (UpdateAudioStreamInputSpec(stream, &input_spec) != 0) {
|
|
total = total ? total : -1;
|
|
break;
|
|
}
|
|
|
|
// Clamp the output length to the maximum currently available.
|
|
// GetAudioStreamDataInternal requires enough input data is available.
|
|
int output_frames = (len - total) / dst_frame_size;
|
|
output_frames = SDL_min(output_frames, chunk_size);
|
|
output_frames = (int) SDL_min(output_frames, available_frames);
|
|
|
|
if (GetAudioStreamDataInternal(stream, &buf[total], output_frames) != 0) {
|
|
total = total ? total : -1;
|
|
break;
|
|
}
|
|
|
|
total += output_frames * dst_frame_size;
|
|
}
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
#if DEBUG_AUDIOSTREAM
|
|
SDL_Log("AUDIOSTREAM: Final result was %d", total);
|
|
#endif
|
|
|
|
return total;
|
|
}
|
|
|
|
// number of converted/resampled bytes available for output
|
|
int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
|
|
if (CheckAudioStreamIsFullySetup(stream) != 0) {
|
|
SDL_UnlockMutex(stream->lock);
|
|
return 0;
|
|
}
|
|
|
|
Sint64 count = GetAudioStreamAvailableFrames(stream, NULL);
|
|
|
|
// convert from sample frames to bytes in destination format.
|
|
count *= SDL_AUDIO_FRAMESIZE(stream->dst_spec);
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
// if this overflows an int, just clamp it to a maximum.
|
|
return (int) SDL_min(count, SDL_INT_MAX);
|
|
}
|
|
|
|
// number of sample frames that are currently queued as input.
|
|
int SDL_GetAudioStreamQueued(SDL_AudioStream *stream)
|
|
{
|
|
if (!stream) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
const Uint64 total = stream->total_bytes_queued;
|
|
SDL_UnlockMutex(stream->lock);
|
|
|
|
// if this overflows an int, just clamp it to a maximum.
|
|
return (int) SDL_min(total, SDL_INT_MAX);
|
|
}
|
|
|
|
int SDL_ClearAudioStream(SDL_AudioStream *stream)
|
|
{
|
|
if (stream == NULL) {
|
|
return SDL_InvalidParamError("stream");
|
|
}
|
|
|
|
SDL_LockMutex(stream->lock);
|
|
|
|
SDL_ClearAudioQueue(stream->queue);
|
|
SDL_zero(stream->input_spec);
|
|
stream->resample_offset = 0;
|
|
stream->total_bytes_queued = 0;
|
|
|
|
SDL_UnlockMutex(stream->lock);
|
|
return 0;
|
|
}
|
|
|
|
void SDL_DestroyAudioStream(SDL_AudioStream *stream)
|
|
{
|
|
if (stream == NULL) {
|
|
return;
|
|
}
|
|
|
|
OnAudioStreamDestroy(stream);
|
|
|
|
const SDL_bool simplified = stream->simplified;
|
|
if (simplified) {
|
|
SDL_assert(stream->bound_device->simplified);
|
|
SDL_CloseAudioDevice(stream->bound_device->instance_id); // this will unbind the stream.
|
|
} else {
|
|
SDL_UnbindAudioStream(stream);
|
|
}
|
|
|
|
SDL_aligned_free(stream->history_buffer);
|
|
SDL_aligned_free(stream->work_buffer);
|
|
SDL_DestroyAudioQueue(stream->queue);
|
|
SDL_DestroyMutex(stream->lock);
|
|
|
|
SDL_free(stream);
|
|
}
|
|
|
|
int SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len,
|
|
const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len)
|
|
{
|
|
if (dst_data) {
|
|
*dst_data = NULL;
|
|
}
|
|
|
|
if (dst_len) {
|
|
*dst_len = 0;
|
|
}
|
|
|
|
if (src_data == NULL) {
|
|
return SDL_InvalidParamError("src_data");
|
|
} else if (src_len < 0) {
|
|
return SDL_InvalidParamError("src_len");
|
|
} else if (dst_data == NULL) {
|
|
return SDL_InvalidParamError("dst_data");
|
|
} else if (dst_len == NULL) {
|
|
return SDL_InvalidParamError("dst_len");
|
|
}
|
|
|
|
int retval = -1;
|
|
Uint8 *dst = NULL;
|
|
int dstlen = 0;
|
|
|
|
SDL_AudioStream *stream = SDL_CreateAudioStream(src_spec, dst_spec);
|
|
if (stream != NULL) {
|
|
if ((SDL_PutAudioStreamData(stream, src_data, src_len) == 0) && (SDL_FlushAudioStream(stream) == 0)) {
|
|
dstlen = SDL_GetAudioStreamAvailable(stream);
|
|
if (dstlen >= 0) {
|
|
dst = (Uint8 *)SDL_malloc(dstlen);
|
|
if (!dst) {
|
|
SDL_OutOfMemory();
|
|
} else {
|
|
retval = (SDL_GetAudioStreamData(stream, dst, dstlen) >= 0) ? 0 : -1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (retval == -1) {
|
|
SDL_free(dst);
|
|
} else {
|
|
*dst_data = dst;
|
|
*dst_len = dstlen;
|
|
}
|
|
|
|
SDL_DestroyAudioStream(stream);
|
|
return retval;
|
|
}
|