Commit graph

802 commits

Author SHA1 Message Date
Sam Lantinga
dd27ac6305 Fixed bug 3876 - Resampling of certain sounds adds heavy distortion
Simon Hug

Patch that adds [-1, 1] clamping to the scalar audio type conversions.

This may come from the SDL_Convert_F32_to_X_Scalar functions. They don't clamp the float value to [-1, 1] and when they cast it to the target integer it may be too large or too small for the type and get truncated, causing horrible noise.

The attached patch throws clamping in, but I don't know if that's the preferred way to fix this. For x86 (without SSE) the compiler (I tested MSVC) seems to throw a horrible amount of x87 code in it. It's a bit better with SSE, but probably still quite the performance hit. And SSE2 uses a branchless approach with maxss and minss.
2017-10-18 19:30:47 -07:00
Sam Lantinga
8538f75119 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
f36e701b48 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
Ryan C. Gordon
e8c14464e7 coreaudio: changed device close procedure to prevent long hangs in some cases.
The audioqueue thread needs to keep running, and processing the CFRunLoop
until the AudioQueue is disposed of, otherwise CoreAudio will hang waiting for
final data to feed the device.

At least, I think this is how it all works. It definitely fixes the bug here!

Since AudioQueueDispose() calls AudioQueueStop() internally, there's no need
for our thread to handle this, either, which is good because the AudioQueue
would be disposed by this point. So now the AudioQueue is disposed first, and
then our thread is joined, and everything works out okay.

Just in case, we mark the device "paused" before setting everything in motion,
so any further callbacks from CoreAudio will write silence and not fire the
app's audio callback again.

Fixes Bugzilla #3868.

--HG--
extra : rebase_source : 3b8ba6f8c5af56b8193ea1bd96ef8b9111533f87
2017-10-13 01:15:29 -04:00
Sam Lantinga
27da65637f Fixed compiler warning 2017-10-12 13:55:35 -07:00
Ryan C. Gordon
31c80a081d 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
9faffcc24c audio: Moved the resampler state up to double precision.
Fixes more buffer overflows.
2017-10-11 11:51:14 -04:00
Ryan C. Gordon
a5e57de5e3 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
0b41f9e1e9 audio: clamp resampler interpolation values to prevent buffer overflow.
Partially fixes Bugzilla #3848.
2017-10-11 02:33:55 -04:00
Ryan C. Gordon
94b758a288 audio: Moved unchanging variable out of loop. 2017-10-11 02:31:58 -04:00
Ryan C. Gordon
e19182e933 audio: Make sure audio stream resampling doesn't overflow buffers. 2017-10-11 02:03:05 -04:00
Ryan C. Gordon
87e6384fe0 audio: Fixed check for minimum audio stream put size.
--HG--
extra : rebase_source : c46940d87a4f506c0866e8f086e2874a19d0265d
2017-10-11 01:37:11 -04:00
Ryan C. Gordon
324676172e audio: SDL_ResampleCVT() should use memmove instead of memcpy.
This copy can overlap.

Fixes Bugzilla #3849.

--HG--
extra : rebase_source : 0ef5350523b489b091d2d3354b2fe57664cf4725
2017-10-10 22:31:02 -04:00
Ryan C. Gordon
b233e909a4 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.

--HG--
extra : rebase_source : 17870d2f7b9e1cf9657ead1969cfe090c4a9e094
2017-10-10 22:18:46 -04:00
Ryan C. Gordon
63e57f5a48 audio: reworked audio streams to have right-hand resampling padding available.
Fixes Bugzilla #3851.
2017-10-10 16:12:56 -04:00
Ryan C. Gordon
25f80b473f audio: Fixed compiler warning on Visual Studio. 2017-09-22 22:28:21 -04:00
Sam Lantinga
f486f87816 Fixed Mac OS X build 2017-09-22 11:25:52 -07:00
Sam Lantinga
796d1f6645 Fixed audio being silent on older iOS devices
Tested on an iPod running iOS 6.1
2017-09-22 11:15:14 -07:00
Sam Lantinga
d58b502fad Fixed memory leak when HAVE_ALLOCA isn't defined 2017-09-22 08:51:45 -07:00
Ryan C. Gordon
d06b6e98c5 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.

--HG--
extra : amend_source : 0cc91e0b0d6c2314a435a252d88c123c8fa58bfc
2017-09-22 07:42:24 -04:00
Sam Lantinga
26f9eab102 Added some missing "extern" declarations 2017-09-21 00:55:29 -07:00
Ryan C. Gordon
2f484d4e3a audio: removed my perl experiment script.
--HG--
extra : histedit_source : be66a1e28b142b6499cee293f281a8c85bd426f0
2017-09-21 02:06:53 -04:00
Ryan C. Gordon
4509de166f 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.

--HG--
extra : rebase_source : 2178d7f422d118dfb76608964f63d290b18c7408
extra : histedit_source : 5461b7f3025ed6c09213910a563318c95d185bf3%2C4a6a6c4c38784b4bc5f241d963c0d752c73ea5b9
2017-09-21 02:51:14 -04:00
Sam Lantinga
1e197f83b5 Added a hint SDL_HINT_AUDIO_CATEGORY to control the audio category,
determining whether the phone mute switch affects the audio
2017-09-15 17:27:32 -07:00
Ryan C. Gordon
1e31971896 alsa: removed snd_pcm_wait() call before writing to playback device.
This would cause playback problems in certain situations, such as on the
Raspberry Pi. The device that the wait was added for seems to not benefit from
it in modern times, and standard desktop Linux seems to do the right thing
when a USB device is unplugged now, without this patch.

Fixes Bugzilla #3599.
2017-09-09 21:17:46 -04:00
Ryan C. Gordon
59bc23e03c wave: SDL_LoadWAV now supports 24-bit audio. 2017-09-07 10:56:08 -04:00
Ryan C. Gordon
b4c19f59ef sndio: Patched to compile if SIO_DEVANY isn't defined.
(It isn't in whatever Raspbian is currently shipping.)
2017-09-02 16:41:14 -04:00
Sam Lantinga
a86f7e738e 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.
2017-08-28 21:42:39 -07:00
Ryan C. Gordon
70aacb0db9 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.

--HG--
extra : rebase_source : c61476a6434b4cfe1655f5d30332280cb4cd7af9
2017-08-29 00:41:45 -04:00
Ryan C. Gordon
1641fd7c50 audio: Converting audio samples from int to float was using wrong equation.
Fixes Bugzilla #3775.

--HG--
extra : rebase_source : 960d1ec9d440bca3da46e50e8f14c3139eac756c
2017-08-29 00:02:04 -04:00
Sam Lantinga
73ca40b566 Fixed WinRT build after changing the header guard preprocessor symbol 2017-08-28 01:59:53 -07:00
Sam Lantinga
f1e9abdefb Renaming of guard header names to quiet -Wreserved-id-macro 2017-08-28 00:22:23 -07:00
Sam Lantinga
0ff15ac41b Fixed bug 3710 - SDL_OpenAudio(desired, obtained) doesn't update desired's size when obtained is NULL
David Ludwig

I've created a new set of patches.  I am happy to create more, if it would help.

One version only copies 'size'.

A second version copies both 'size' and 'silence'.  When looking over the documentation for SDL_OpenAudio in SDL_audio.h, it mentioned that both 'size' and 'silence' were things that SDL_OpenAudio would calculate.

Regarding *both* patches, I did notice that SDL 1.2 appears to have always modified desired's size and silence fields.  The SDL wiki, at https://wiki.libsdl.org/SDL_OpenAudio#Remarks , does note:
2017-08-27 19:10:30 -07:00
Sam Lantinga
d5c138b4f0 Added WASAPI audio target to autoconf build process 2017-08-18 17:29:44 -07:00
Ryan C. Gordon
b44189aee2 audio: better docs on conversion APIs, error if not init'd (thanks, Simon!).
Fixes Bugzilla #3662.
2017-08-18 16:52:19 -04:00
Sam Lantinga
87ea96c003 Fixed bug 2330 - Debian bug report: SDL2 X11 driver buffer overflow with large X11 file descriptor
manuel.montezelo

Original bug report (note that it was against 2.0.0, it might have been fixed in between):  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=733015

--------------------------------------------------------
Package: libsdl2-2.0-0
Version: 2.0.0+dfsg1-3
Severity: normal
Tags: patch

I have occasional crashes here caused by the X11 backend of SDL2. It seems to
be caused by the X11_Pending function trying to add a high number (> 1024)
file descriptor to a fd_set before doing a select on it to avoid busy waiting
on X11 events. This causes a buffer overflow because the file descriptor is
larger (or equal) than the limit FD_SETSIZE.

Attached is a possible workaround patch.

Please also keep in mind that fd_set are also used in following files which
may have similar problems.

src/audio/bsd/SDL_bsdaudio.c
src/audio/paudio/SDL_paudio.c
src/audio/qsa/SDL_qsa_audio.c
src/audio/sun/SDL_sunaudio.c
src/joystick/linux/SDL_sysjoystick.c


--------------------------------------------------------

On Tuesday 24 December 2013 00:43:13 Sven Eckelmann wrote:
> I have occasional crashes here caused by the X11 backend of SDL2. It seems
> to be caused by the X11_Pending function trying to add a high number (>
> 1024) file descriptor to a fd_set before doing a select on it to avoid busy
> waiting on X11 events. This causes a buffer overflow because the file
> descriptor is larger (or equal) than the limit FD_SETSIZE.


I personally experienced this problem while hacking on the python bindings
package for SDL2 [1] (while doing make runtest). But it easier to reproduce in
a smaller, synthetic testcase.
2017-08-14 20:22:19 -07:00
Sam Lantinga
dc82a25489 Fixed bug 3702 - Clear error messages of SDL_LoadObject for optional libraries
Simon Hug

Some code in SDL loads libraries with SDL_LoadObject to get more information or use newer APIs. SDL_LoadObject may fail, set an error message and SDL will continue with some fallback code. Since SDL will overwrite the error or exit the function with a return value that indicates success, the error form SDL_LoadObject for the optional stuff might as well be cleared right away.
2017-08-11 10:21:19 -07:00
Ryan C. Gordon
187fea6b1b sndio: Fix for some platforms (Linux, for example) that don't define INFTIM.
Fixes Bugzilla #3712.

--HG--
extra : histedit_source : af73344d6ccf91bdbe8413d017899fdd91c8e84f
2017-08-07 00:25:18 -04:00
Ryan C. Gordon
350d9a1b0a psp: Force audio channels to stereo if > 2 channels requested (thanks, Solra!).
Fixes Bugzilla #3726.
2017-08-04 16:18:34 -04:00
Philipp Wiesemann
78636e9f5e qnx: Fixed error message. 2017-07-29 23:00:45 +02:00
Philipp Wiesemann
06d0fac495 aix: Removed unused local variable.
Found by Cppcheck.
2017-07-29 23:00:34 +02:00
Sam Lantinga
3ed584e7f4 Fixed crash if the WASAPI audio device couldn't be recovered 2017-07-27 22:55:18 -07:00
Sam Lantinga
c26f57e9ef Fixed infinite recursion if the WASAPI audio device couldn't be recovered 2017-07-27 22:52:19 -07:00
Sam Lantinga
fb87969169 Fixed typo in WASAPI shutdown code 2017-07-27 02:41:58 -07:00
Ryan C. Gordon
1969a981b0 Fixed compiler warnings on QNX. 2017-07-23 19:25:16 -04:00
Ryan C. Gordon
8f2891d58c sndio: fixed poll() call (thanks, kdrakehp!).
Fixes Bugzilla #3705.

--HG--
extra : histedit_source : c2f9bec926056c41ec1c7245ddb52569179c447a
2017-07-20 20:40:17 -04:00
Ryan C. Gordon
086e2c8e29 sndio: More improvements to the OpenBSD audio target (thanks, kdrakehp!).
Fixes Bugzilla #3705.
2017-07-20 18:16:02 -04:00
Sam Lantinga
3243cdc26a Fixed bug 3705 - Add capture support to the sndio backend
kdrakehp

The attached patch adds capture support to the sndio backend.

The patch also allows the `OpenDevice' function to accept arbitrary device names.
2017-07-20 10:39:47 -07:00
Philipp Wiesemann
9abd5d5785 qnx: Fixed setting a field twice. 2017-07-07 23:00:10 +02:00
Ryan C. Gordon
a40e02b690 audio: trying to pacify static analysis. 2017-07-05 12:04:37 -04:00