src/audio/SDL_audiocvt.c


Log

Author Commit Date CI Message
Ryan C. Gordon 29694869 2022-04-28T15:56:52 audio: Revert one of the resampler optimizations. This is the one that splits the "left wing" into two for loops to bubble out the conditional that decides if it should read from the left padding or the input buffer. I still believe the optimization is good, but the basic logic of it was incorrect, and needs to be reexamined and fixed before going back into revision control.
Ryan C. Gordon 111c3add 2022-04-10T13:23:51 audio: Resampler optimizations. - Calculate `j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING` once per loop iteration since we use it multiple times. - Do the left-wing loop in two sections: while `srcframe < 0` and then the remaining calculations when `srcframe >= 0`. This bubbles a conditional out of every iteration of a tight loop, giving us a boost. We could _probably_ do this to the right-wing loop too, but it's less straightforward there. - The real win: Use floats instead of doubles. This almost doubles the speed of the entire function on Intel CPUs, and for embedded things without hardware-level support for doubles, the speedup is enormous. This in theory might reduce audio quality, though, and I had to put a check in place to avoid a division-by-zero that we avoided at higher precision, but this is likely to be worth keeping for at least the Sony PSP and other smaller platforms, if not everyone.
Ryan C. Gordon de019568 2022-04-09T23:43:57 audio: Prebake the resampler's kaiser table instead of doing it at runtime.
Ryan C. Gordon 727eef70 2022-04-09T10:12:49 audio: SDL_ConvertStereoToMono_SSE3 missed an unaligned load.
pionere 2c6a9c51 2022-03-16T09:36:43 minor optimization (SDL_audiocvt.c)
Ozkan Sezer 5905696e 2022-03-15T23:10:04 SDL_audiocvt.c: minor cleanup.
Wohlstand 7c421fec 2022-03-14T05:55:41 SDL_audiocvt.c: Don't byteswap 8-bit streams Otherwise, this results an assert on big endian machines when attenpting to use SDL_LoadWAV_RW function to load 8-bit WAV files.
pionere f91211eb 2022-01-19T14:51:42 cleanup WASAPI_PrepDevice - reorganize the loop which checks for the right wave-format - use the return value of UpdateAudioStream - ensure SetError is called in SDL_NewAudioStream
pionere ebdd5366 2022-01-17T16:26:02 use SDL_InvalidParamError or SDL_assert instead of custom SDL_SetError
Sam Lantinga 120c76c8 2022-01-03T09:40:00 Updated copyright for 2022
Sylvain 5be8a221 2021-11-17T20:37:51 Add audio conversion from/to 61
Sylvain b649314d 2021-11-15T13:43:40 Add comment to audio clipping (see bug #4104)
Sylvain 072e3fdf 2021-10-14T23:17:08 Fixed bug #4534: NEON implementation of Convert51ToStereo (Thanks Ryan!)
Joshua Root 1e921352 2021-08-13T07:58:48 Improve portability of SDL_Convert51ToStereo_AVX Don't rely on checking __clang_major__ since it is not comparable between different vendors. Don't use "#pragma clang attribute" since it is only available in relatively recent versions, there's no obvious way to check if it's supported, and just using __attribute__ directly (for gcc as well) results in simpler code anyway.
Ozkan Sezer 44a76710 2021-08-11T12:55:50 Don't disable clang avx instrinsics on win32 if __AVX__ is defined. C.f.: https://github.com/libsdl-org/SDL/issues/4533
Sam Lantinga bec78357 2021-08-06T14:20:55 Better fix for compiling using clang on Windows This fixes https://github.com/libsdl-org/SDL/issues/4533
Sam Lantinga cae7bd9b 2021-08-06T12:01:24 Don't use AVX with clang if the compiler isn't building with AVX instructions Fixes https://github.com/libsdl-org/SDL/issues/4533
Ryan C. Gordon 659e1f0a 2021-07-29T17:49:52 audiocvt: The to-5.1 converters now soften FL and FR channels more. This is experimental and might be tweaked further. Reference #4104. Also reference: https://github.com/Keriew/augustus/issues/194#issuecomment-847655049
Jessica Clarke 8f38ba4d 2021-07-29T18:02:47 Fix casts that should be using uintptr_t This is needed to support CHERI, and thus Arm's experimental Morello prototype, where pointers are implemented using unforgeable capabilities that include bounds and permissions metadata to provide fine-grained spatial and referential memory safety, as well as revocation by sweeping memory to provide heap temporal memory safety. On most systems (anything with a flat memory hierarchy rather than using segment-based addressing), size_t and uintptr_t are the same type. However, on CHERI, size_t is just an integer offset, whereas uintptr_t is still a capability as described above. Casting a pointer to size_t will strip the metadata and validity tag, and casting from size_t to a pointer will result in a null-derived capability whose validity tag is not set, and thus cannot be dereferenced without faulting. The audio and cursor casts were harmless as they intend to stuff an integer into a pointer, but using uintptr_t is the idiomatic way to do that and silences our compiler warnings (which our build tool makes fatal by default as they often indicate real problems). The iconv and egl casts were true positives as SDL_iconv_t and iconv_t are pointer types, as is NativeDisplayType on most OSes, so this would have trapped at run time when using the round-tripped pointers. The gles2 casts were also harmless; the OpenGL API defines this argument to be a pointer type (and uses the argument name "pointer"), but it in fact represents an integer offset, so like audio and cursor the additional idiomatic cast is needed to silence the warning.
Brick 53987e9b 2021-07-28T21:03:42 Optimized SDL_Convert51ToStereo_AVX
Ozkan Sezer cf85710c 2021-07-28T22:55:10 SDL_audiocvt.c: disable AVX for clang < 5 and gcc < 4.9 See: https://github.com/libsdl-org/SDL/issues/4533
Ryan C. Gordon 8d790b10 2021-07-27T12:23:46 audiocvt: stereo-to-mono SSE3 now uses unaligned accesses. On modern CPUs, there's no penalty for using the unaligned instruction on aligned memory, but now it can vectorize unaligned data too, which even if it's not optimal, is still going to be faster than the scalar fallback. Fixes #4532.
Joel Linn b2c8d3e9 2021-06-11T22:14:54 audiocvt: 5.1 to Stereo conversion utilizing AVX
Joel Linn 638befc1 2021-06-10T17:22:39 audiocvt: 5.1 to Stereo conversion utilizing SSE
Joel Linn db56526f 2021-06-10T13:07:27 audiocvt: Replace divisions Division is expensive and the compiler can not replace it themselves. Though we now we will be ok in terms of floating point edge behaviour.
Ozkan Sezer 58884e4c 2021-05-04T00:23:40 SDL_audiocvt.c: fixed MSVC double->float conversion warnings.
Sylvain f8695185 2021-04-27T11:07:51 Audio: normalize conversion Stereo to 5.1, Quad to 7.1, 5.1 to 7.1 (bug #4104)
Sylvain 21349901 2021-04-27T10:57:48 Audio: convert 5.1 to 7.1, use right-surround for r-front and r-back (see #4104)
Sam Lantinga 9130f7c3 2021-01-02T10:25:38 Updated copyright for 2021
Sam Lantinga cb361896 2020-12-09T07:16:22 Fixed bug 5235 - All internal sources should include SDL_assert.h Ryan C. Gordon We should really stick this in SDL_internal.h or something so it's always available.
Sam Lantinga a8780c6a 2020-01-16T20:49:25 Updated copyright date for 2020
Sam Lantinga 3ce56f62 2020-01-13T08:12:10 Fixed error formatting
Sam Lantinga e3cedf96 2020-01-11T04:38:13 Add the destination format to the error when conversion isn't possible
Ozkan Sezer 7a47c292 2019-07-31T01:22:02 Fix bug 4746 - introduce SDL_zeroa macro.
Sam Lantinga a21b5b30 2019-06-08T19:09:43 Fixed build
Sam Lantinga 31765242 2019-06-08T18:22:18 Fixed bug 4294 - Audio: perform more validation on conversion request janisozaur There are many cases which are not able to be handled by SDL's audio conversion routines, including too low (negative) rate, too high rate (impossible to allocate). This patch aims to report such issues early and handle others in a graceful manner. The "INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING" value is the conservative approach in terms of what can _technically_ be supported, but its value is 4'194'303, or just shy of 4.2MHz. I highly doubt any sane person would use such rates, especially in SDL2, so I would like to drive this limit further down, but would need some assistance to do that, as doing so would have to introduce an arbitrary value. Are you OK with such approach? What would a good value be? Wikipedia (https://en.wikipedia.org/wiki/High-resolution_audio) lists 96kHz as the highest sampling rate in use, even if I quadruple it for a good measure, to 384kHz it's still an order of magnitude lower than 4MHz.
Sam Lantinga 5e13087b 2019-01-04T22:01:14 Updated copyright for 2019
Ryan C. Gordon 4773690d 2018-06-25T12:55:23 Deal with possible malloc(0) calls, as pointed out by static analysis.
Sam Lantinga e3cc5b2c 2018-01-03T10:03:25 Updated copyright for 2018
Sam Lantinga e830ef34 2017-10-20T16:53:42 Fixed typo converting 4 channel audio to 2 channel
Sam Lantinga 9a291c1e 2017-10-20T14:51:22 Added a note about adjusting channel weights when converting to fewer channels
Ryan C. Gordon 72932906 2017-10-19T18:05:42 audio: Added SDL_AudioStreamFlush().
Ryan C. Gordon e98920f5 2017-10-18T23:49:46 Check correct variable for malloc() results.
Sam Lantinga 653ab5d9 2017-10-18T19:26:36 Added a staging buffer to the audio stream so that we can accumulate small amounts of data if needed when resampling
Sam Lantinga 80f8464d 2017-10-18T15:54:05 Added audio stream conversion functions: SDL_NewAudioStream SDL_AudioStreamPut SDL_AudioStreamGet SDL_AudioStreamAvailable SDL_AudioStreamClear SDL_FreeAudioStream
Sam Lantinga ba10d2b6 2017-10-12T13:55:35 Fixed compiler warning
Ryan C. Gordon 5e5f2290 2017-10-11T12:07:43 audio: Turns out the accumulation errors sound better. :/ Moving to double fixed the overflows, but using "time = i * incr" instead of "time += incr" causes clicks in the output.
Ryan C. Gordon 9bd2c6b4 2017-10-11T11:51:14 audio: Moved the resampler state up to double precision. Fixes more buffer overflows.
Ryan C. Gordon b2f5123b 2017-10-11T11:43:35 audio: calculate resampling time directly, don't increment (thanks, Eric!). Fixes buffer overruns as floating point errors accumulate. Partially fixes Bugzilla #3848.
Ryan C. Gordon 763c3871 2017-10-11T02:33:55 audio: clamp resampler interpolation values to prevent buffer overflow. Partially fixes Bugzilla #3848.
Ryan C. Gordon 0085f917 2017-10-11T02:31:58 audio: Moved unchanging variable out of loop.
Ryan C. Gordon cb8bf6bb 2017-10-11T02:03:05 audio: Make sure audio stream resampling doesn't overflow buffers.
Ryan C. Gordon 459e2b0b 2017-10-11T01:37:11 audio: Fixed check for minimum audio stream put size.
Ryan C. Gordon 903ff641 2017-10-10T22:31:02 audio: SDL_ResampleCVT() should use memmove instead of memcpy. This copy can overlap. Fixes Bugzilla #3849.
Ryan C. Gordon 42fff7ce 2017-10-10T22:18:46 audio: Don't stack-allocate resampler padding. (I thought padding size ranged from 5 frames to ~30 frames (based around RESAMPLER_ZERO_CROSSINGS, which is 5), but it's actually between 512 and several thousands (based on RESAMPLER_SAMPLES_PER_ZERO_CROSSING)). It gets big fast when downsampling.
Ryan C. Gordon 37d89aa1 2017-10-10T16:12:56 audio: reworked audio streams to have right-hand resampling padding available. Fixes Bugzilla #3851.
Ryan C. Gordon 099ae43e 2017-09-22T22:28:21 audio: Fixed compiler warning on Visual Studio.
Sam Lantinga d74c00e6 2017-09-22T08:51:45 Fixed memory leak when HAVE_ALLOCA isn't defined
Ryan C. Gordon 6d206a7b 2017-09-22T07:42:24 audio: Stream resampling now saves some samples from previous run for padding. Previously, the padding was silence, which was a problem when streaming since you would sample a little bit of this silence between each buffer. We still need a means to get padding data for the right hand side, but this patch makes the resampler output more correct.
Ryan C. Gordon 1a3b95a1 2017-09-21T02:51:14 audio: Replaced the resampler. Again. This time it's using real math from a real whitepaper instead of my previous amateur, fast-but-low-quality attempt. The new resampler does "bandlimited interpolation," as described here: https://ccrma.stanford.edu/~jos/resample/ The output appears to sound cleaner, especially at high frequencies, and of course works with non-power-of-two rate conversions. There are some obvious optimizations to be done to this still, and there is other fallout: this doesn't resample a buffer in-place, the 2-channels-Sint16 fast path is gone because this resampler does a _lot_ of floating point math. There is a nasty hack to make it work with SDL_AudioCVT. It's possible these issues are solvable, but they aren't solved as of yet. Still, I hope this effort is slouching in the right direction.
Sam Lantinga d619d885 2017-08-28T21:42:39 Fixed bug 3662 - Error message when using the audio conversion setup without an initialized audio subsystem is a bit vague Simon Hug This issue actually raises the question if this API change (requirement of initialized audio subsystem) is breaking backwards compatibility. I don't see the documentation saying it is needed in 2.0.5.
Ryan C. Gordon b128e880 2017-08-29T00:41:45 audio: A whole bunch of improvements to audio conversion (thanks, Solra!). "Major changes, roughly in order of appearance: - Use float math everywhere, instead of promoting to double and casting back all the time. - Conserve sound energy when downmixing any channel into two other channels. - Add a QuadToStereo filter. (The previous technique of reusing StereoToMono never worked, since it assumed an incorrect channel layout for 4.0.) - Add a 71to51 filter. This removes just under half of the cases the previous code would silently break in. - Add a QuadTo51 filter. More silent breakage fixed. - Add a 51to71 filter, removing another almost-half of the silently broken cases. - Add 8 to the list of values SDL_SupportedChannelCount will accept. - Change SDL_BuildAudioCVT's channel-related logic to handle every case, and to actually fail if it fails instead of silently corrupting sound data and/or crashing down the road." (Note that SDL doesn't otherwise support 7.1 audio yet, but hopefully it will soon and the 7.1 converters are an important piece of that. --ryan.) Fixes Bugzilla #3727.
Ryan C. Gordon e3e6b4fd 2017-08-18T16:52:19 audio: better docs on conversion APIs, error if not init'd (thanks, Simon!). Fixes Bugzilla #3662.
Ryan C. Gordon 1683a0c1 2017-07-05T12:04:37 audio: trying to pacify static analysis.
Ryan C. Gordon a509719f 2017-06-12T21:35:24 audio: Converter now checks a strict list of channels and formats we support.
Sam Lantinga 553b3286 2017-06-12T16:39:15 Fixed bug 3668 - Overflow of SDL_AudioCVT.filters with some downmixes Simon Hug There's a chance that an audio conversion from many channels to a few can use more than 9 audio filters. SDL_AudioCVT has 10 SDL_AudioFilter pointers of which one has to be the terminating NULL pointer. The SDL code has no checks for this limit. If it overflows there can be stack or heap corruption or a call to 0xa. Attached patch adds a function that checks for this limit and throws an error if it is reached. Also adds some documentation. Test parameters that trigger this issue: AUDIO_U16MSB with 224 channels at 46359 Hz V AUDIO_S16MSB with 6 channels at 27463 Hz The fuzzer program I uploaded in bug 3667 has more of them.
Ryan C. Gordon ca0bf151 2017-03-03T16:38:17 Fix some more compiler warnings on armcc.
Ryan C. Gordon d526b8a1 2017-03-02T13:33:04 Some patches to make SDL compile with armcc (ARM's C compiler).
Ryan C. Gordon ad9c702f 2017-02-13T16:56:41 audio: SDL_AudioStream's *_sample_frame_size should be in bytes, not bits. Fixes failures where SDL_AudioStreamGet() incorrectly thinks it got a partial sample frame request.
Ryan C. Gordon 47e2f4e9 2017-01-24T20:30:48 audio: libsamplerate can't resample in-place; make space for a copy if needed.
Ryan C. Gordon c7f9dcb6 2017-01-24T15:52:22 audio: Offer a hint for libsamplerate quality/speed tradeoff. This defaults to the internal SDL resampler, since that's the likely default without a system-wide install of libsamplerate, but those that need more can tweak this.
Ryan C. Gordon 1da3a337 2017-01-24T10:09:29 audio: Fix static analysis concerns about a dead assignment.
Ryan C. Gordon 8f627c1c 2017-01-24T00:51:33 audio: Make sure SDL_AudioStream's work buffer is 16-byte aligned, for SIMD. Note the giantic FIXME, though!
Ryan C. Gordon 17dcee20 2017-01-24T00:17:40 audio: Streams now resample in-place. Removed second allocated buffer.
Ryan C. Gordon b5eeab77 2017-01-24T00:08:24 audio: allow stereo Sint16 resampling fast path in SDL_AudioStream. This currently favors libsamplerate over the fast path (quality over speed), but I'm not sure that's the correct approach, as there may be surprising changes in performance metrics depending on what packages are available on a user's system. That being said, currently, the only thing with access to SDL_AudioStream is an SDL audio device's thread, and it might be mostly idle otherwise, so maybe this is generally good.
Ryan C. Gordon a80cb672 2017-01-24T00:03:36 audio: Fixed off-by-one error in upsampling.
Ryan C. Gordon dad07f96 2017-01-23T16:45:50 audio: Resampler now special-cases stereo and mono processing. Turns out that iterating from 0 to channels-1 was a serious performance hit! These cases now tend to match or beat the original audio resampler's speed!
Ryan C. Gordon 8ce6ddf1 2017-01-23T16:42:47 audio: Fixed incorrect pointer in SDL_ResampleCVT_si16_c2(). Forgot to update this when we changed this to process in-place. Whoops!
Ryan C. Gordon 3594bf8e 2017-01-23T01:05:44 audio: Wired up new SSE code to build system.
Ryan C. Gordon 64056e81 2017-01-23T00:57:19 audio: Added SSE3 implementation of SDL_ConvertStereoToMono().
Ryan C. Gordon 8855daac 2017-01-22T23:48:15 audio: Make the simple resampler operate in-place. This allows us to avoid an extra copy, allocate less memory and reduce cache pressure. On the downside: we have to do a lot of tapdancing to resample the buffer in reverse when the output is growing.
Ryan C. Gordon 202ab30c 2017-01-22T20:27:48 audio: Special case for resampling stereo AUDIO_S16SYS audio data. This is a fairly common case, so we avoid the conversion to/from float here.
Ryan C. Gordon a7f86f2f 2017-01-22T20:18:59 audio: don't cast to double in SDL_ConvertStereoToMono(). It's expensive and (hopefully) unnecessary. If this becomes an overflow problem, we could multiply both values by 0.5f before adding them, but let's see if we can get by without the extra multiplication first.
Ryan C. Gordon 83454c82 2017-01-20T16:26:24 audio: removed conditional from simple resampler's inner loop. We never seem to overflow the source buffer now; this might have been a leftover from a bug that was covered by Vitaly's fixes? Removing this conditional makes the resampler 10-20% faster. Left an assert in there for debug builds, in case this still happens.
Ryan C. Gordon 3e1679c8 2017-01-18T02:11:56 audio: Several fixes to "simple" resampler (thanks, Vitaly!). Fixes Bugzilla #3551.
Ryan C. Gordon 1e66d457 2017-01-15T05:01:59 audio: Some fixes to the audio data type converter code. Removed some needless things ("len / sizeof (Uint8)"), and made sure the int32 -> float code uses doubles to avoid working with large integer values in a 32-bit float.
Sam Lantinga bf11cd50 2017-01-09T20:37:52 Fixed bug 3552 - Building SDL in release mode fails under VS 2017 RC Lukasz Biel Tried to compile SDL2 using newest version of VS. Got: SDL_audiocvt.obj : error LNK2019: unresolved external symbol memcpy referenced in function SDL_ResampleCVT 1>E:\Users\dotPo\Lib\SDL\VisualC\x64\Release\SDL2.dll : fatal error LNK1120: 1 unresolved externals whole compilation process: http://pastebin.com/eWDAvBce Steps to reproduce: clone http://hg.libsdl.org/SDL using tortoise hg, open SDL\VisualC\SDL.sln, when promted if should retarget solution click ok, select release x64 build type, Build/Build Solution attempt 2, using Visual Studio cmake support: open folder SDL\ select release x64 build type, run CMake\Build CMakeLists.txt build fails When switched to debug build type, buils succeeds in both cases. VS 2017 is still beta.
Ryan C. Gordon 23020f92 2017-01-09T16:31:57 audio: Don't ever use libsamplerate in the SDL_AudioCVT codepath. It causes audio pops if you're converting in chunks (and needs to allocate/initialize/free on each convert). We'll either adjust this interface when we break ABI for 2.1 to make this usable, or publish the SDL_AudioStream API for those that want a streaming solution. In the meantime, the "simple" resampler produces "good enough" audio without pops and doesn't have to be initialized, so that'll do for now on the SDL_AudioCVT interface.
Ryan C. Gordon 063c9d40 2017-01-09T06:00:58 audio: Replaced older resamplers in SDL_AudioCVT with the new ones.
Ryan C. Gordon 38854e03 2017-01-08T16:18:49 audio: Improvements in channel conversion code.
Ryan C. Gordon d005dc21 2017-01-08T14:23:15 audio: Patched to compile with libsamplerate support.
Ryan C. Gordon 19e937fc 2017-01-08T14:18:03 audio: libsamplerate loading now happens once at init time.
Ryan C. Gordon 98cc9d10 2017-01-08T14:17:09 Fixed coding style on a function signature.
Sam Lantinga df25258a 2017-01-06T20:43:53 Added configure and cmake support for libsamplerate
Ryan C. Gordon c5825b69 2017-01-06T21:23:51 audio: Don't call a NULL function pointer when clearing audio streams. (Partially?) fixes Bugzilla #3547.
Sam Lantinga cbe44f7f 2017-01-06T02:16:26 Added support for using libsamplerate to do audio resampling
Sam Lantinga 3443dc19 2017-01-05T23:53:46 Don't do any audio conversion if none is necessary
Ryan C. Gordon 992124d4 2017-01-06T01:02:58 audio: Fixed SDL_AudioStreamGet() function parameters. There was a draft of this where it did audio conversion into the final buffer, if there was enough room available past what you asked for, but that interface got removed, so the parameters didn't make sense (and we were using the wrong one in any case, too!).
Ryan C. Gordon 3761b5f6 2017-01-05T20:11:19 Fixed a few compiler warnings.
Ryan C. Gordon 30178a9b 2017-01-05T19:29:38 audio: Added SDL_AudioStream. Non-power-of-two resampling now works!