Commit Graph

109 Commits

Author SHA1 Message Date
Ryan C. Gordon 29694869b1
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.
2022-04-28 15:58:39 -04:00
Ryan C. Gordon 111c3add73 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.
2022-04-26 13:32:42 -04:00
Ryan C. Gordon de019568dc audio: Prebake the resampler's kaiser table instead of doing it at runtime. 2022-04-26 13:32:42 -04:00
Ryan C. Gordon 727eef7064
audio: SDL_ConvertStereoToMono_SSE3 missed an unaligned load. 2022-04-09 10:13:37 -04:00
pionere 2c6a9c5194 minor optimization (SDL_audiocvt.c) 2022-03-16 07:58:28 -04:00
Ozkan Sezer 5905696e66 SDL_audiocvt.c: minor cleanup. 2022-03-15 23:10:04 +03:00
Wohlstand 7c421fec16 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.
2022-03-14 08:41:07 -07:00
pionere f91211eb17 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
2022-01-28 20:40:19 -05:00
pionere ebdd536676 use SDL_InvalidParamError or SDL_assert instead of custom SDL_SetError 2022-01-28 20:40:19 -05:00
Sam Lantinga 120c76c84b Updated copyright for 2022 2022-01-03 09:40:21 -08:00
Sylvain 5be8a22113 Add audio conversion from/to 61 2021-11-17 16:04:54 -08:00
Sylvain b649314d62
Add comment to audio clipping (see bug #4104) 2021-11-15 13:43:40 +01:00
Sylvain 072e3fdfc4
Fixed bug #4534: NEON implementation of Convert51ToStereo (Thanks Ryan!) 2021-10-14 23:17:08 +02:00
Joshua Root 1e92135297 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.
2021-08-12 16:04:35 -07:00
Ozkan Sezer 44a76710d1 Don't disable clang avx instrinsics on win32 if __AVX__ is defined.
C.f.: https://github.com/libsdl-org/SDL/issues/4533
2021-08-11 08:47:52 -07:00
Sam Lantinga bec783571b Better fix for compiling using clang on Windows
This fixes https://github.com/libsdl-org/SDL/issues/4533
2021-08-06 14:20:55 -07:00
Sam Lantinga cae7bd9b65 Don't use AVX with clang if the compiler isn't building with AVX instructions
Fixes https://github.com/libsdl-org/SDL/issues/4533
2021-08-06 12:01:24 -07:00
Ryan C. Gordon 659e1f0a3f
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
2021-07-29 17:55:59 -04:00
Jessica Clarke 8f38ba4d68 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.
2021-07-29 14:42:15 -07:00
Brick 53987e9b4f Optimized SDL_Convert51ToStereo_AVX 2021-07-28 16:11:04 -07:00
Ozkan Sezer cf85710cf8 SDL_audiocvt.c: disable AVX for clang < 5 and gcc < 4.9
See: https://github.com/libsdl-org/SDL/issues/4533
2021-07-28 22:55:10 +03:00
Ryan C. Gordon 8d790b10f8
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.
2021-07-27 12:23:46 -04:00
Joel Linn b2c8d3e9e4 audiocvt: 5.1 to Stereo conversion utilizing AVX 2021-07-24 15:39:17 -04:00
Joel Linn 638befc1a9 audiocvt: 5.1 to Stereo conversion utilizing SSE 2021-07-24 15:39:17 -04:00
Joel Linn db56526f14 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.
2021-07-24 15:39:17 -04:00
Ozkan Sezer 58884e4c1b SDL_audiocvt.c: fixed MSVC double->float conversion warnings. 2021-05-04 00:23:40 +03:00
Sylvain f869518533
Audio: normalize conversion Stereo to 5.1, Quad to 7.1, 5.1 to 7.1 (bug #4104) 2021-04-27 11:07:51 +02:00
Sylvain 21349901ea
Audio: convert 5.1 to 7.1, use right-surround for r-front and r-back (see #4104) 2021-04-27 10:57:48 +02:00
Sam Lantinga 9130f7c377 Updated copyright for 2021 2021-01-02 10:25:38 -08:00
Sam Lantinga cb36189692 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.
2020-12-09 07:16:22 -08:00
Sam Lantinga a8780c6a28 Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
Sam Lantinga 3ce56f621c Fixed error formatting 2020-01-13 08:12:10 -08:00
Sam Lantinga e3cedf967d Add the destination format to the error when conversion isn't possible 2020-01-11 04:38:13 -08:00
Ozkan Sezer 7a47c292c0 Fix bug 4746 - introduce SDL_zeroa macro. 2019-07-31 01:22:02 +03:00
Sam Lantinga a21b5b3018 Fixed build 2019-06-08 19:09:43 -07:00
Sam Lantinga 31765242d6 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.
2019-06-08 18:22:18 -07:00
Sam Lantinga 5e13087b0f Updated copyright for 2019 2019-01-04 22:01:14 -08:00
Ryan C. Gordon 4773690d0f Deal with possible malloc(0) calls, as pointed out by static analysis. 2018-06-25 12:55:23 -04:00
Sam Lantinga e3cc5b2c6b Updated copyright for 2018 2018-01-03 10:03:25 -08:00
Sam Lantinga e830ef3458 Fixed typo converting 4 channel audio to 2 channel 2017-10-20 16:53:42 -07:00
Sam Lantinga 9a291c1e59 Added a note about adjusting channel weights when converting to fewer channels 2017-10-20 14:51:22 -07:00
Ryan C. Gordon 729329068b audio: Added SDL_AudioStreamFlush(). 2017-10-19 18:05:42 -04:00
Ryan C. Gordon e98920f5f3 Check correct variable for malloc() results. 2017-10-18 23:49:46 -04:00
Sam Lantinga 653ab5d9c4 Added a staging buffer to the audio stream so that we can accumulate small amounts of data if needed when resampling 2017-10-18 19:26:36 -07:00
Sam Lantinga 80f8464d97 Added audio stream conversion functions:
SDL_NewAudioStream
    SDL_AudioStreamPut
    SDL_AudioStreamGet
    SDL_AudioStreamAvailable
    SDL_AudioStreamClear
    SDL_FreeAudioStream
2017-10-18 15:54:05 -07:00
Sam Lantinga ba10d2b654 Fixed compiler warning 2017-10-12 13:55:35 -07:00
Ryan C. Gordon 5e5f2290f2 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.
2017-10-11 12:07:43 -04:00
Ryan C. Gordon 9bd2c6b491 audio: Moved the resampler state up to double precision.
Fixes more buffer overflows.
2017-10-11 11:51:14 -04:00
Ryan C. Gordon b2f5123b65 audio: calculate resampling time directly, don't increment (thanks, Eric!).
Fixes buffer overruns as floating point errors accumulate.

Partially fixes Bugzilla #3848.
2017-10-11 11:43:35 -04:00
Ryan C. Gordon 763c387149 audio: clamp resampler interpolation values to prevent buffer overflow.
Partially fixes Bugzilla #3848.
2017-10-11 02:33:55 -04:00