Commit Graph

592 Commits (e9908d840b7fb71b903876161c5ec111872f46eb)

Author SHA1 Message Date
Sam Lantinga 03cf24162f OpenSL ES audio cleanup and added a note with low latency audio discussion 2019-06-08 10:21:38 -07:00
Sam Lantinga 166d15fd75 Fixed surround sound channel setup for Android OpenSL ES audio driver 2019-06-07 15:09:15 -07:00
Sam Lantinga 723d014336 Fixed bug 4171 - SDL_GetQueuedAudioSize is broken with WASAPI
Cameron Gutman

I was trying to use SDL_GetQueuedAudioSize() to ensure my audio latency didn't get too high while streaming data in from the network. If I get more than N frames of audio queued, I know that the network is giving me more data than I can play and I need to drop some to keep latency low.

This doesn't work well on WASAPI out of the box, due to the addition of GetPendingBytes() to the amount of queued data. As a terrible hack, I loop 100 times calling SDL_Delay(10) and SDL_GetQueuedAudioSize() before I ever call SDL_QueueAudio() to get a "baseline" amount that I then subtract from SDL_GetQueuedAudioSize() later. However, because this value isn't actually a constant, this hack can cause SDL_GetQueuedAudioSize() - baselineSize to be < 0. This means I have no accurate way of determining how much data is actually queued in SDL's audio buffer queue.

The SDL_GetQueuedAudioSize() documentation says: "This is the number of bytes that have been queued for playback with SDL_QueueAudio(), but have not yet been sent to the hardware." Yet, SDL_GetQueuedAudioSize() returns > 0 value when SDL_QueueAudio() has never been called.

Based on that documentation, I believe the current behavior contradicts the documented behavior of this function and should be changed in line with Boris's patch.

I understand that exposing the IAudioClient::GetCurrentPadding() value is useful, but a solution there needs to take into account what of that data is silence inserted by SDL and what is actual data queued by the user with SDL_QueueAudio(). Until that happens, I think the best approach is to remove the GetPendingBytes() call until SDL is able to keep track of queued data to make sense of it. This would make SDL_GetQueuedAudioSize() possible to use accurately with WASAPI.
2019-06-04 17:32:15 -07:00
Sam Lantinga f3e76ea1d0 Use the OpenSL ES audio driver by default on Android, as it has the lowest latency. 2019-05-23 13:47:30 -07:00
Sam Lantinga 02f9667a08 Fixed static and buzzing when trying to use floating point audio on the OpenSL ES audio driver. 2019-05-23 13:47:27 -07:00
Sam Lantinga abcfe80480 [SDL] iOS fix bug with audio interrupted by a phone call not restoring. 2019-05-14 14:20:54 -07:00
Ryan C. Gordon 2fbfe8b912 coreaudio: Set audio callback thread priority.
Fixes Bugzilla #4155.
2019-03-25 12:59:30 -04:00
Ryan C. Gordon 6a3356ab3f Backed out changeset cec31de4e126
This was meant to migrate CoreAudio onto the same SDL_RunAudio() path that
most other audio drivers are on, but it introduced a bug because it doesn't
deal with dropped audio buffers...and fixing that properly just introduces
latency.

I might revisit this later, perhaps by reworking SDL_RunAudio to allow for
this sort of API better, or redesigning the whole subsystem or something, I
don't know. I'm not super-thrilled that this has to exist outside of the usual
codepaths, though.

Fixes Bugzilla #4481.
2019-03-25 12:24:38 -04:00
Sam Lantinga 35255342cd Fixed bug 4525 - Fix crash in ALSA_HotplugThread caused by bad return value check
Anthony Pesch

Fix snd_device_name_hint return value check

According to the ALSA documentation, snd_device_name_hint returns 0 on
success, otherwise a negative error code. The code previously only
considered -1 to be an error, which let other error codes through
resulting in a segfault when hints (which was NULL) was dereferenced
2019-03-16 18:48:21 -07:00
Sylvain Becker 03cbac4040 Android/openslES: fix warnings, comment out un-used interface 2019-02-05 15:14:15 +01:00
Sylvain Becker 614c8aea20 Android/openslES: set number of buffers of DATALOCATOR to internal NUM_BUFFER
If we increase NUM_BUFFER, Enqueue won't fail with SL_RESULT_BUFFER_INSUFFICIENT
2019-02-05 15:09:41 +01:00
Sylvain Becker bf823bf2dc Android/openslES: prevent to run out of buffers if Enqueue() fails. 2019-02-05 15:05:32 +01:00
Alon Zakai 3b4e369365 Emscripten: No need for Runtime. for dynCalls 2019-01-29 12:21:22 +00:00
Alon Zakai 53ead95e1d Emscripten: Avoid SDL2 in JS global scope
After this fix, closure works with the LLVM wasm backend on SDL2.
2019-01-29 12:19:36 +00:00
Sylvain Becker 1b24b2eca5 Android/openslES: fix Pause/ResumeDevices when openslES is not used 2019-01-14 22:56:57 +01:00
Sylvain Becker 647b1f6a6d Android/openslES: check for non NULL variable, some intialization.
use the previous naming
2019-01-14 14:36:13 +01:00
Sylvain Becker 7b1cc441dd Android/openslES: start playing, after creating ressources 2019-01-14 14:31:06 +01:00
Sylvain Becker 955d87894b Android/openslES: set audio in paused/resumed state for Android event loop
And also in "stopped" state before closing the device.
2019-01-14 12:33:29 +01:00
Sylvain Becker 59c8c7b684 Android/openslES: move a few static variables to SDL_PrivateAudioData structure 2019-01-14 10:58:57 +01:00
Sylvain Becker 5aeeaaab70 Android/openslES: register and use CloseDevice function. 2019-01-14 10:16:26 +01:00
Sylvain Becker 365fd9c602 Android/openslES: some space and indentation to match SDL conventions 2019-01-14 10:04:54 +01:00
Sam Lantinga 7dc92a7669 Initial Android OpenSL ES implementation, contributed by ANTA 2019-01-12 12:18:44 -08:00
Sylvain Becker d23c2f07e3 Fixed bug 3930 - Android, set thread priorities and names
SDLActivity thread priority is unchanged, by default -10 (THREAD_PRIORITY_VIDEO).

SDLAudio thread priority was -4 (SDL_SetThreadPriority was ignored) and is now -16 (THREAD_PRIORITY_AUDIO).

SDLThread thread priority was 0 (THREAD_PRIORITY_DEFAULT) and is -4 (THREAD_PRIORITY_DISPLAY).
2019-01-10 18:05:56 +01:00
Sam Lantinga 5e13087b0f Updated copyright for 2019 2019-01-04 22:01:14 -08:00
Sylvain Becker aea7e56a24 android: use __ARM_NEON instead of __ARM_NEON__ to include <arm_neon.h>
Only __ARM_NEON is defined with Android NDK and arm64-v8a
Tested on ndk-r18, ndk-r13 and also Xcode.
(Visual Studio needs a different fix).

Fixes Bugzilla #4409.
2018-12-04 12:34:45 +01:00
Sylvain Beucler 1f6bd95110 Emscripten: make CloseAudio actually close audio
cf. https://bugzilla.libsdl.org/show_bug.cgi?id=4176
2018-11-15 18:22:30 +00:00
Micha? Janiszewski 91820998fc Add and update include guards
Include guards in most changed files were missing, I added them keeping
the same style as other SDL files. In some cases I moved the include
guards around to be the first thing the header has to take advantage of
any possible improvements compiler may have for inclusion guards.
2018-10-28 21:36:48 +01:00
Ryan C. Gordon 4a50a04213 wasapi/win32: Sort initial device lists by device GUID.
This makes an unchanged set of hardware always report devices in the same
order on each run.
2018-10-21 22:40:17 -04:00
Ryan C. Gordon 04cbf13261 audio: All device names reported by SDL must be unique.
This means that if you have two devices named "Soundblaster Pro" in your
machine, one will be reported as "Soundblaster Pro" and the other as
"Soundblaster Pro (2)".

This makes it so you can't into a position where one of your devices can't
be opened because another is sitting on the same name.
2018-10-10 15:20:56 -04:00
Ryan C. Gordon 0378529e1e audio: clean_out_device_list() already sets this flag to false for us. 2018-10-10 14:55:24 -04:00
Sam Lantinga f5a21ebf0c Added support for surround sound and float audio on Android 2018-10-09 20:12:43 -07:00
Sam Lantinga b251876126 commit c6b28f46b8116552ec2b38d1d3c8535df28ba7a1
Author: Anthony Pesch <inolen@gmail.com>
Date:   Fri May 4 20:21:21 2018 -0400

    Added SDL_AUDIO_ALLOW_SAMPLES_CHANGE flag enabling users of SDL_OpenAudioDevice to get
    the sample size of the actual hardware buffer vs having a stream created to handle the
    delta
2018-10-01 09:47:10 -07:00
Ryan C. Gordon 56ec349d2a audio: disable NEON converters for now.
To be revisited after 2.0.9 ships!

(doesn't fix Bugzilla #4186, but stops the regression for the time being.)
2018-09-29 16:48:15 -04:00
Ethan Lee 7f9854b9b2 WinRT: Wait until audio device activation is complete and PrepDevice during OpenAudio 2018-09-25 01:45:12 -04:00
Sam Lantinga 5febdfcece Fixed whitespace 2018-09-24 11:49:25 -07:00
Ryan C. Gordon 623a6defd3 alsa: optionally run entire pipeline non-blocking. 2018-08-07 16:49:18 -04:00
Ryan C. Gordon 56f44cfa0f audio: Deal with device shutdown more carefully.
This would cause problems in various ways, but specifically triggers an
assert when you close a WASAPI capture device in an app running over RDP.

Related to (but not the actual bug) in Bugzilla #3924.
2018-08-07 13:04:15 -04:00
Wohlstand ff8c62f227 Fixed bug 4210 - SSE2-based converter makes junk result of S32 -> Float
At the HG state abdd17144682, 64-bit assemblies are using SSE2-based resampler, produces junk sound when converting the S32 -> Float32 -> S16 chain. The `NEED_SCALAR_CONVERTER_FALLBACKS` thing works perfectly.

If I will find a reason that caused this mistake, I'll send a patch by myself.
2018-07-02 03:53:57 +03: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
Anthony Pesch c591429542 alsa: avoid hardware parameters with an excessive number of periods.
The previous code attempted to use set_buffer_size / set_period_size
discretely, favoring the parameters which generated a buffer size that was
exactly 2x the requested buffer size. This solution ultimately prioritizes
only the buffer size, which comes at a large performance cost on some machines
where this results in an excessive number of periods. In my case, for a 4096
sample buffer, this configured the device to use 37 periods with a period size
of 221 samples and a buffer size of 8192 samples. With 37 periods, the SDL
Audio thread was consuming 25% of the CPU.

This code has been refactored to use set_period_size and set_buffer_size
together. set_period_size is called first to attempt to set the period to
exactly match the requested buffer size, and set_buffer_size is called second
to further refine the parameters to attempt to use only 2 periods. The
fundamental change here is that the period size / count won't go to extreme
values if the buffer size can't be exactly matched, the buffer size should
instead just increase to the next closest multiple of the target period size
that is supported. After changing this, for a 4096 sample buffer, the device
is configured to use 3 periods with a period size of 4096 samples and a buffer
size of 12288 samples. With only 3 periods, the SDL Audio thread doesn't even
show up when profiling.

Fixes Bugzilla #4156.
2018-05-04 21:21:32 -04:00
Sam Lantinga 1d25135b71 Fixed bug 4184 - jack audio driver fails in presence of midi ports
Martin ?irokov

Launching an SDL application with SDL_AUDIODRIVER=jack, and then calling SDL_OpenAudioDevice() with whatever parameters fails with an error like this one:

SDL_OpenAudioDevice: Couldn't connect JACK ports: SDL:sdl_jack_output_0 => system:midi_playback_1

This happens because JACK_OpenDevice in src/audio/jack/SDL_jackaudio.c blindly tries to connect to all input ports without checking whether they are for audio or midi.

The fix is to check port types and ignore all non audio ports. Also I removed devports field from struct SDL_PrivateAudioData, because it's never really used and removing unused ports from it would be PITA.
2018-06-01 19:43:53 -07:00
Sam Lantinga 8325df25aa Fixed bug 4169 - Crash due to audio session observer race condition
Jona

The following explains why this bug was happening:
This crash was caused because the audio session was being set as active [session setActive:YES error:&err] when the audio device was actually being CLOSED. Certain cases the audio session being set to active would fail and the method would return right away. Because of the way the error was handled we never removed the SDLInterruptionListener thus leaking it. Later when an interruption was received the THIS_ object would contain a pointer to an already released device causing the crash.

The fix:
When only one device remained open and it was being closed we needed to set the audio session as NOT active and completely ignore the returned error to successfully release the SDLInterruptionListener. I think the user assumed that the open_playback_devices and open_capture_devices would equal 0 when all of them where closed but the truth is that at the end of the closing process that the open devices count is decremented.
2018-05-24 07:30:24 -07:00
Ryan C. Gordon 101544d6f0 audio: Needed to fix two more instances for Visual Studio. 2018-05-21 12:05:17 -04:00
Ryan C. Gordon 49881861b1 audio: Patched to compile on Visual Studio.
(It gets upset at the -2147483648, thinking this should be an unsigned value
because 2147483648 is too large for an int32, so the negative sign upsets the
compiler.)
2018-05-21 11:54:09 -04:00
Ryan C. Gordon b7e88aaae0 audio: Added ARM NEON versions of audio converters.
These are _much_ faster than the scalar equivalents on the Raspberry Pi that
I tested on. Often 3x to 4x as fast!
2018-05-16 02:03:06 -04:00
Ryan C. Gordon cb0e614fb1 audio: SSE2 float-to-int converters should clamp input.
The scalar versions already do this.
2018-05-15 02:29:35 -04:00
Ryan C. Gordon a07e5815a5 audio: Fix range on float-to-int data clamping.
I can't tell if there was a good reason for this or it was just me getting
numbers wrong due to exhaustion.
2018-05-15 01:40:05 -04:00
Ryan C. Gordon 7832cb652e audio: float to int converters should clamp inclusively.
If we have to test if a sample is > 1.0f anyhow, we might as well use this
to avoid the unnecessary multiplication when it's == 1.0f, too. (etc).
2018-05-15 01:35:53 -04:00
Ryan C. Gordon e2ec1eb12e audio: converting int32 to/from float shouldn't use doubles.
The concern is that a massive int sample, like 0x7FFFFFFF, won't fit in a
float32, which doesn't have enough bits to hold a whole number this large,
just to divide it to get a value between 0 and 1.
Previously we would convert to double, to get more bits, do the division, and
cast back to a float, but this is expensive.

Casting to double is more accurate, but it's 2x to 3x slower. Shifting out
the least significant byte of an int32, so it'll definitely fit in a float,
and dividing by 0x7FFFFF is still accurate to about 5 decimal places, and the
difference doesn't appear to be perceptable.
2018-05-15 01:04:11 -04:00
Sam Lantinga f521b22eb5 Added SDL_THREAD_PRIORITY_TIME_CRITICAL 2018-04-23 22:07:56 -07:00
Ryan C. Gordon dc8b55e50b coreaudio: Use the standard SDL audio thread instead of spinning a new one.
Fixes corner cases, like the audio callback not firing if the device is
disconnected, etc.
2018-04-16 02:11:09 -04:00
Sam Lantinga 99a0c0f0e2 Fixed MinGW-w64 build 2018-02-24 08:23:44 -08:00
Ryan C. Gordon c7e4366530 wasapi: let Windows do the resampling for us if possible. 2018-02-21 21:34:06 -05:00
Ryan C. Gordon 7e1fa0ce53 wasapi: fixed typo in an assert message. 2018-02-21 21:34:35 -05:00
Ryan C. Gordon 97494f5374 pulseaudio: Just read/dump captured data in FlushCapture.
Apparently pa_stream_flush() doesn't work as expected:

https://lists.freedesktop.org/archives/pulseaudio-discuss/2012-April/013328.html

Fixes Bugzilla #4087.
2018-02-17 18:30:21 -05:00
sezero ba0ecc6712 fix building SDL_audiotypecvt.c with gcc < 4.0 2018-02-12 10:47:00 +03:00
sezero 40b27fd51b revert the recent typecast assignment changes (see bug #4079)
also change the void* typedefs for the two vulkan function
pointers added in vulkan_internal.h  into generic function
pointer typedefs.
2018-02-12 17:00:00 +03:00
Sam Lantinga 90e72bf4e2 Fixed ISO C99 compatibility
SDL now builds with gcc 7.2 with the following command line options:
-Wall -pedantic-errors -Wno-deprecated-declarations -Wno-overlength-strings --std=c99
2018-01-30 18:08:34 -08:00
Ryan C. Gordon 488824017a wasapi: Fixed some compiler warnings. 2018-01-22 09:36:40 -05:00
Sam Lantinga e3cc5b2c6b Updated copyright for 2018 2018-01-03 10:03:25 -08:00
Ryan C. Gordon 77bb49b7a7 wasapi: Patched to compile on non-UWP WinRT builds. 2017-12-31 03:34:16 -05:00
Ryan C. Gordon ab4695f48f wasapi: switched to event-driven interface.
This reduces latency and improves battery life.
2017-12-13 14:35:55 -05:00
Ryan C. Gordon 351d6d4784 audio: Port WASAPI to WinRT, remove XAudio2 backend.
XAudio2 doesn't have capture support, so WASAPI was to replace it; the holdout
was WinRT, which still needed it as its primary audio target until the WASAPI
code code be made to work.

The support matrix now looks like:

WinXP: directsound by default, winmm as a fallback for buggy drivers.
Vista+: WASAPI (directsound and winmm as fallbacks for debugging).
WinRT: WASAPI
2017-12-06 12:24:32 -05: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 afefcbfeba 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 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
Ryan C. Gordon fa15674134 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.
2017-10-13 01:15:29 -04: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
Ryan C. Gordon 0085f917e0 audio: Moved unchanging variable out of loop. 2017-10-11 02:31:58 -04:00
Ryan C. Gordon cb8bf6bbaf audio: Make sure audio stream resampling doesn't overflow buffers. 2017-10-11 02:03:05 -04:00
Ryan C. Gordon 459e2b0bbe audio: Fixed check for minimum audio stream put size. 2017-10-11 01:37:11 -04:00
Ryan C. Gordon 903ff6414e audio: SDL_ResampleCVT() should use memmove instead of memcpy.
This copy can overlap.

Fixes Bugzilla #3849.
2017-10-10 22:31:02 -04:00
Ryan C. Gordon 42fff7ce2b 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.
2017-10-10 22:18:46 -04:00
Ryan C. Gordon 37d89aa10f 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 099ae43e81 audio: Fixed compiler warning on Visual Studio. 2017-09-22 22:28:21 -04:00
Sam Lantinga fe6b8f1c31 Fixed Mac OS X build 2017-09-22 11:25:52 -07:00
Sam Lantinga 407e1693ae 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 d74c00e67d Fixed memory leak when HAVE_ALLOCA isn't defined 2017-09-22 08:51:45 -07:00
Ryan C. Gordon 6d206a7b28 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.
2017-09-22 07:42:24 -04:00
Sam Lantinga 8b660c5046 Added some missing "extern" declarations 2017-09-21 00:55:29 -07:00
Ryan C. Gordon f40bd5ee24 audio: removed my perl experiment script. 2017-09-21 02:06:53 -04:00
Ryan C. Gordon 1a3b95a11e 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.
2017-09-21 02:51:14 -04:00
Sam Lantinga c08a7a74a5 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 93583d461c 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 ca15c7d67f wave: SDL_LoadWAV now supports 24-bit audio. 2017-09-07 10:56:08 -04:00
Ryan C. Gordon 3267398d15 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 d619d88560 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 b128e8802d 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.
2017-08-29 00:41:45 -04:00
Ryan C. Gordon a0cd7d6bce audio: Converting audio samples from int to float was using wrong equation.
Fixes Bugzilla #3775.
2017-08-29 00:02:04 -04:00
Sam Lantinga 6dd3f55d55 Fixed WinRT build after changing the header guard preprocessor symbol 2017-08-28 01:59:53 -07:00
Sam Lantinga 0d011ec66d Renaming of guard header names to quiet -Wreserved-id-macro 2017-08-28 00:22:23 -07:00
Sam Lantinga 8e7998e19d 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 bcf0e07107 Added WASAPI audio target to autoconf build process 2017-08-18 17:29:44 -07:00
Ryan C. Gordon e3e6b4fd35 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 fb835f9e3b 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 96305832bc 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 9dde37eadb sndio: Fix for some platforms (Linux, for example) that don't define INFTIM.
Fixes Bugzilla #3712.
2017-08-07 00:25:18 -04:00
Ryan C. Gordon a09efc73d2 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 68ca9d9ed1 qnx: Fixed error message. 2017-07-29 23:00:45 +02:00
Philipp Wiesemann cea33bf5b8 aix: Removed unused local variable.
Found by Cppcheck.
2017-07-29 23:00:34 +02:00
Sam Lantinga 77ca0f273c Fixed crash if the WASAPI audio device couldn't be recovered 2017-07-27 22:55:18 -07:00
Sam Lantinga 4a734209a3 Fixed infinite recursion if the WASAPI audio device couldn't be recovered 2017-07-27 22:52:19 -07:00
Sam Lantinga f033ce61e1 Fixed typo in WASAPI shutdown code 2017-07-27 02:41:58 -07:00
Ryan C. Gordon 03eaddcad4 Fixed compiler warnings on QNX. 2017-07-23 19:25:16 -04:00
Ryan C. Gordon 8ac17a2ae6 sndio: fixed poll() call (thanks, kdrakehp!).
Fixes Bugzilla #3705.
2017-07-20 20:40:17 -04:00
Ryan C. Gordon ee9cc32493 sndio: More improvements to the OpenBSD audio target (thanks, kdrakehp!).
Fixes Bugzilla #3705.
2017-07-20 18:16:02 -04:00
Sam Lantinga 2cc6806472 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 fb9c2939c2 qnx: Fixed setting a field twice. 2017-07-07 23:00:10 +02:00
Ryan C. Gordon 1683a0c106 audio: trying to pacify static analysis. 2017-07-05 12:04:37 -04:00
Philipp Wiesemann 9f99b3d7ee aix: Fixed audio debug output.
DEBUG_AUDIO is checked with #ifdef not #if.
2017-07-02 22:46:49 +02:00
Philipp Wiesemann 4366721b46 qnx: Removed unused bootstrap declaration.
QNX_bootstrap is the VideoBootStrap. QSAAUDIO_bootstrap is still there.
2017-07-02 22:46:00 +02:00
Ryan C. Gordon 22241ed0b0 Support for QNX 7.0 (thanks, Elad!).
Fixes Bugzilla #3686.
2017-07-01 17:50:47 -04:00
Philipp Wiesemann 380e0693b1 aix: Fixed compile error. 2017-07-01 23:01:49 +02:00
Philipp Wiesemann 4c190ce584 netbsd: Fixed comment. 2017-07-01 23:00:07 +02:00
Philipp Wiesemann 4c48260ca2 netbsd: Removed unused field. 2017-06-29 23:00:18 +02:00
Philipp Wiesemann 7bb6b402c2 netbsd: Fixed compile error. 2017-06-29 23:00:09 +02:00
Ryan C. Gordon a509719fc3 audio: Converter now checks a strict list of channels and formats we support. 2017-06-12 21:35:24 -04:00
Sam Lantinga 553b328664 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.
2017-06-12 16:39:15 -07:00
Ryan C. Gordon 325330efdb jack: removed accidental copy/paste. 2017-06-09 17:37:43 -04:00
Ryan C. Gordon 58f08af46c jack: added capture support. 2017-06-09 00:47:47 -04:00
Ryan C. Gordon c39fd5777d jack: Move jack_client_t into the audio device instead a global variable. 2017-06-09 00:14:50 -04:00
Ryan C. Gordon b65e0777ce jack: Remove BROKEN_MULTI_DEVICE code. 2017-06-08 22:20:49 -04:00
Ryan C. Gordon d9039f2396 jack: Initial shot at a JACK audio target.
http://jackaudio.org/

Fixes Bugzilla #2163.
(with several more commits following to improve this code.)
2017-06-08 13:27:58 -04:00
Philipp Wiesemann 63b3e06f75 Corrected names of header file guards. 2017-06-03 23:00:15 +02:00
Philipp Wiesemann fc510bd798 nacl: Fixed crash if allocating memory for audio device failed. 2017-05-28 21:50:47 +02:00
Philipp Wiesemann 7c5078d8bd qnx: Removed unnecessary check for available audio devices. 2017-05-28 21:50:37 +02:00
Philipp Wiesemann 1e60ea76db qnx: Removed unnecessary call to SDL_zerop() after SDL_calloc(). 2017-05-28 21:50:27 +02:00
Ryan C. Gordon e5918acf46 wasapi: properly report init failure if on pre-Vista version of Windows.
We really should change the Init interface to return 0 on success and -1 on
error, like everything else, to avoid this sort of confusion.
2017-05-28 00:41:55 -04:00
Philipp Wiesemann 3639895eac Removed unused errno includes. 2017-05-27 23:30:07 +02:00
Philipp Wiesemann 759319729c emscripten: Fixed compiling on C89 compilers. 2017-05-26 22:45:40 +02:00
Ryan C. Gordon a7fc2822d4 audio: rename bsd target to netbsd.
Apparently this is no longer a generic BSD audio target, and hasn't been for
years, so rename it for NetBSD.
2017-05-24 19:56:59 -04:00
Ryan C. Gordon 6844d92c23 coreaudio: we don't need to track number of allocated audio buffers anymore.
CoreAudio takes care of iterating through the buffers and freeing them now,
so we don't have to manage this ourselves.
2017-05-24 13:28:13 -04:00
Ryan C. Gordon fc4402e5ff coreaudio: Better handling of audio buffer queue management.
We don't fill buffers just to throw them away during shutdown now, we let the
AudioQueue free its own buffers during disposal (which fixes possible warnings
getting printed to stderr by CoreAudio), and we stop the queue after running
any queued audio during shutdown, which prevents dropping the end of the
audio playback if you opened the device with an enormous sample buffer.

Fixes Bugzilla #3555.
2017-05-24 13:25:31 -04:00
Ryan C. Gordon 3fd35f6bb0 coreaudio: looks like we need more like a 10ms buffer minimum, not 50ms. 2017-05-24 01:28:03 -04:00
Ryan C. Gordon 793c788b1c coreaudio: dynamically allocate AudioQueueBuffers.
We need more than two buffers to flip between if they are small, or CoreAudio
won't make any sound; apparently it needs X milliseconds of audio queued when
it needs to play more or it drops any queued buffers. We are currently
guessing 50 milliseconds as a minimum, but there's probably a more proper
way to get the minimum time period from the system.

Fixes Bugzilla #3656.
2017-05-24 00:12:22 -04:00
Ryan C. Gordon 91e6054b03 wasapi: don't mark capture devices as failed for AUDCLNT_S_BUFFER_EMPTY.
Fixes Bugzilla #3633.
2017-05-19 12:40:55 -04:00
Ryan C. Gordon 81ab6c98fd Patched to compile on Windows. 2017-05-18 16:27:36 -04:00
Ryan C. Gordon 13b6d9959a wasapi: Replace tabs with strings in source code. 2017-05-18 15:46:06 -04:00
Ryan C. Gordon adabc38439 wasapi: Deal with AUDCLNT_S_BUFFER_EMPTY when flushing audio device. 2017-05-18 15:43:51 -04:00
Ryan C. Gordon 4073a6694f audio: One more callbackspec fix (thanks, Simon!). 2017-05-18 15:33:17 -04:00
Ryan C. Gordon c878b59bbe audio: fixed more "spec" references that should have been "callbackspec".
This should catch all the ones for audio targets that have provided their
own audio threads.
2017-05-10 16:18:43 -04:00
Alex Szpakowski 75fb07a6d2 iOS: Only mark interrupted audio devices as non-interrupted if AudioQueueStart is successful. 2017-05-03 18:05:29 -03:00
Ryan C. Gordon 226541cb5b audio: another wrong struct that causes NULL pointer crash (thanks, Simon!).
Fixes Bugzilla #3632.
2017-04-26 01:43:40 -04:00
Juha Kuikka 7382cebb41 audio: Fix audio queue functions to use new spec structure.
Using the old spec structure causes the audio queueing functions to fail
due to bad callback pointers being checked.
2017-04-20 21:25:29 -04:00
Sam Lantinga d20d426c3a Fix crash in SDL audio thread, by Juha Kuikka
Wrong audio spec structure was populated with the internal callback, causing the audio thread to call a NULL pointer.
2017-04-18 22:17:40 -07:00
Ryan C. Gordon 028716e79f wasapi: deal with default device changes, and more robust failure recovery. 2017-03-30 16:33:47 -04:00
Ryan C. Gordon c85c57a05d wasapi: Handle lost audio device endpoints.
This gracefully recovers when a device format is changed, and will switch
to the new default device if the current one is unplugged, etc.

This does not handle when a new default device is added; it only notices
if the current default goes away. That will be fixed by implementing the
stubbed-out MMNotificationClient_OnDefaultDeviceChanged() function.
2017-03-29 14:23:39 -04:00
Philipp Wiesemann 266816b4aa Removed newlines from error messages. 2017-03-26 21:00:19 +02:00
Sam Lantinga 6814f5dbc0 ALSA driver improvements:
* alsa hotplug thread is low priority
* give a chance for other threads to catch up when audio playback is not progressing
* use nonblocking for alsa audio capture
  There is a bug with SDL hanging when an audio capture USB device is removed, because poll never returns
2017-03-14 07:20:14 -07:00
Sam Lantinga c4d54504fa differentiate between capture / playback audio thread names 2017-03-14 07:16:56 -07:00
Ryan C. Gordon ca0bf151d5 Fix some more compiler warnings on armcc. 2017-03-03 16:38:17 -05:00
Ryan C. Gordon d526b8a1e9 Some patches to make SDL compile with armcc (ARM's C compiler). 2017-03-02 13:33:04 -05:00
Ryan C. Gordon a4249b48ee Patched to compile on C89 compilers. 2017-02-26 00:56:13 -05:00
Ryan C. Gordon 3b9e4d0a6c audio: Try to keep callbacks firing at normal pace when device is lost. 2017-02-26 00:39:22 -05:00
Ryan C. Gordon a366c35f37 audio: run the audio callback even if device was lost.
We will throw away the data anyhow, but some apps depend on the callback
firing to make progress; testmultiaudio.c, if nothing else, is an example
of this.

Capture also will now fire the callback in these conditions, offering nothing
but silence.

Apps can check SDL_GetAudioDeviceStatus() or listen for the
SDL_AUDIODEVICEREMOVED event if they want to gracefully deal with
an opened audio device that has been unexpectedly lost.
2017-02-26 00:12:33 -05:00
Ryan C. Gordon 5728cb2025 audio: Make sure the disk and dummy targets are the last ones we try to init. 2017-02-26 00:10:02 -05:00
Sam Lantinga 71a4e8ed13 Stop CoreAudio from doing expensive audio rate conversion 2017-02-23 12:10:02 -08:00
Philipp Wiesemann cfcec57f42 Fixed comment. 2017-02-19 21:05:09 +01:00
Ryan C. Gordon e8677a1bd2 audio: Added basic WAVE_FORMAT_EXTENSIBLE support to .wav loader.
This is just enough to get you through a file that just used the extended
header for float or int data. It doesn't handle all the other things that
you expect from this header, like 24-bit samples inside a 32-bit container
or speaker masks.
2017-02-17 02:25:37 -05:00
Ryan C. Gordon 1ed41d6d0d Patched to compile on Windows. 2017-02-14 03:12:09 -05:00
Ryan C. Gordon 6046fd4cb0 wasapi: Initial WASAPI support, for Windows Vista and later.
This should remain binary compatible with Windows XP, as we dynamically
load anything we need and fall back to DirectSound/WinMM/XAudio2 if not
available.
2017-02-14 03:03:27 -05:00
Ryan C. Gordon e5fc93baca audio: Don't wrap bootstrap declarations in preprocessor macros.
They are harmless and ignored if we don't actually link against them. The
preprocessor checks elsewhere if they're actually used.
2017-02-13 16:59:02 -05:00
Ryan C. Gordon ad9c702f6a 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.
2017-02-13 16:56:41 -05:00
Ryan C. Gordon 175f1e8f4a audio: Added a ThreadDeinit() method to match ThreadInit.
Not used by any targets at the moment, but will be shortly!
2017-02-13 16:55:00 -05:00
Sam Lantinga 886736a2c8 Fixed bug 3584 - Small stack size for audio callback thread
Walter van Niftrik

We have found that since SDL 2.0.5 the audio callback thread is created with a very small stack size. In our application this is leading to stack overflows.

We believe there is a bug at http://hg.libsdl.org/SDL/file/391fd532f79e/src/audio/SDL_audio.c#l1132, where the is_internal_thread flag appears to be inverted.
2017-02-11 16:38:16 -08:00
Sam Lantinga 107c19daad Log the error returned by XAudio2Create() 2017-02-09 06:01:14 -08:00
Sam Lantinga ede5c73484 Generalized the audio resampling hint for other resampling methods in the future 2017-01-24 19:38:01 -08:00
Ryan C. Gordon 47e2f4e950 audio: libsamplerate can't resample in-place; make space for a copy if needed. 2017-01-24 20:30:48 -05:00
Ryan C. Gordon c7f9dcb6fc 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.
2017-01-24 15:52:22 -05:00
Ryan C. Gordon 1da3a33773 audio: Fix static analysis concerns about a dead assignment. 2017-01-24 10:09:29 -05:00
Ryan C. Gordon 8f627c1cd8 audio: Make sure SDL_AudioStream's work buffer is 16-byte aligned, for SIMD.
Note the giantic FIXME, though!
2017-01-24 00:51:33 -05:00
Ryan C. Gordon 17dcee20c1 audio: Streams now resample in-place. Removed second allocated buffer. 2017-01-24 00:17:40 -05:00
Ryan C. Gordon b5eeab779f 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.
2017-01-24 00:08:24 -05:00
Ryan C. Gordon a80cb672e3 audio: Fixed off-by-one error in upsampling. 2017-01-24 00:03:36 -05:00
Ryan C. Gordon dad07f960b 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!
2017-01-23 16:45:50 -05:00
Ryan C. Gordon 8ce6ddf125 audio: Fixed incorrect pointer in SDL_ResampleCVT_si16_c2().
Forgot to update this when we changed this to process in-place. Whoops!
2017-01-23 16:42:47 -05:00
Ryan C. Gordon ecdc6c1207 audio: Fixed copy/paste bug in float32->sint16/SSE2 scalar leftover code. 2017-01-23 12:14:28 -05:00
Ryan C. Gordon 4b8f354668 audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128. 2017-01-23 12:06:10 -05:00
Ryan C. Gordon fab4501811 audio: use _mm_srli_si128 instead of _mm_bsrli_si128.
They're the same thing (one is generally a #define of the other), but some
toolchains don't offer the 'b' version.
2017-01-23 12:02:02 -05:00
Ryan C. Gordon 3594bf8eeb audio: Wired up new SSE code to build system. 2017-01-23 01:05:44 -05:00
Ryan C. Gordon 202ab30c16 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.
2017-01-22 20:27:48 -05:00
Ryan C. Gordon 8855daac66 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.
2017-01-22 23:48:15 -05:00
Ryan C. Gordon 64056e81cd audio: Added SSE3 implementation of SDL_ConvertStereoToMono(). 2017-01-23 00:57:19 -05:00
Ryan C. Gordon a7f86f2fd2 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.
2017-01-22 20:18:59 -05:00
Ryan C. Gordon 83454c821f 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.
2017-01-20 16:26:24 -05:00
Sam Lantinga 9b99265a5e Fixed mingw64 32-bit build, which does have the correct structure definitions 2017-01-19 20:19:37 -08:00
Sam Lantinga 5cb1ca551f Fixed building with mingw32 2017-01-18 11:57:27 -08:00
Ryan C. Gordon 3e1679c885 audio: Several fixes to "simple" resampler (thanks, Vitaly!).
Fixes Bugzilla #3551.
2017-01-18 02:11:56 -05:00
Ryan C. Gordon 5718293092 audio: Implemented SIMD support for audio data type converters.
This currently adds an SSE2 implementation (but it's #ifdef'd out for now,
until it's hooked up to the configure script and such).
2017-01-16 00:58:28 -05:00
Ryan C. Gordon 1e66d457d7 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.
2017-01-15 05:01:59 -05:00
Sam Lantinga bf11cd5084 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.
2017-01-09 20:37:52 -08:00
Ryan C. Gordon 23020f92fa 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.
2017-01-09 16:31:57 -05:00
Ryan C. Gordon 063c9d40d7 audio: Replaced older resamplers in SDL_AudioCVT with the new ones. 2017-01-09 06:00:58 -05:00
Ryan C. Gordon a41103b170 audio: Patched to compile if linking directly to libsamplerate. 2017-01-09 05:59:30 -05:00
Ryan C. Gordon 38854e0333 audio: Improvements in channel conversion code. 2017-01-08 16:18:49 -05:00
Ryan C. Gordon 35166609d5 audio: Patched to compile with libsamplerate support (again). 2017-01-08 14:28:44 -05:00
Ryan C. Gordon d005dc21d3 audio: Patched to compile with libsamplerate support. 2017-01-08 14:23:15 -05:00
Ryan C. Gordon 19e937fc2e audio: libsamplerate loading now happens once at init time. 2017-01-08 14:18:03 -05:00
Ryan C. Gordon 98cc9d10d3 Fixed coding style on a function signature. 2017-01-08 14:17:09 -05:00
Ryan C. Gordon 61a3ba303c Replaced a few single-line "//" comments. 2017-01-07 17:09:14 -05:00
Sam Lantinga df25258a1e Added configure and cmake support for libsamplerate 2017-01-06 20:43:53 -08:00
Ryan C. Gordon c5825b698d audio: Don't call a NULL function pointer when clearing audio streams.
(Partially?) fixes Bugzilla #3547.
2017-01-06 21:23:51 -05:00
Sam Lantinga cbe44f7ff1 Added support for using libsamplerate to do audio resampling 2017-01-06 02:16:26 -08:00
Sam Lantinga 37f404fb87 Fixed confusion between Ryan's new audio stream and the audio buffer we were calling stream in the callback 2017-01-06 00:47:42 -08:00
Ryan C. Gordon 748f46054f audio: Add an assert to make sure non-streaming audio uses good buffer sizes. 2017-01-06 03:38:14 -05:00
Ryan C. Gordon 345c5989f1 haiku: Patched to compile. 2017-01-06 03:15:27 -05:00
Sam Lantinga 3443dc19f9 Don't do any audio conversion if none is necessary 2017-01-05 23:53:46 -08:00
Ryan C. Gordon b3e8db802e audio: rename fake_stream to work_buffer.
It's more than an alternative for when the OS can't provide a DMA buffer, now.
2017-01-06 01:07:34 -05:00
Ryan C. Gordon 992124d4de 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!).
2017-01-06 01:02:58 -05:00
Ryan C. Gordon 99fc1ef994 naclaudio: Untested attempt to migrate to SDL_AudioStream. 2017-01-06 00:56:29 -05:00
Ryan C. Gordon 115d0ce71c haikuaudio: Untested attempt to get this working with SDL_AudioStream. 2017-01-06 00:50:01 -05:00
Ryan C. Gordon 1a90c72dfc emscriptenaudio: don't get stuck in infinite loop if SDL_AudioStreamPut fails. 2017-01-06 00:49:35 -05:00
Ryan C. Gordon f07a1a5ad5 emscriptenaudio: Reworked to use SDL_AudioStream. 2017-01-05 21:31:02 -05:00
Ryan C. Gordon 3761b5f60b Fixed a few compiler warnings. 2017-01-05 20:11:19 -05:00
Ryan C. Gordon 4aa9e36983 Patched to compile on some compilers. 2017-01-05 19:45:57 -05:00
Ryan C. Gordon 30178a9b24 audio: Added SDL_AudioStream. Non-power-of-two resampling now works! 2017-01-05 19:29:38 -05:00
Ryan C. Gordon f12ab8f2b3 audio: More effort to improve and simplify audio resamplers. 2017-01-05 19:12:20 -05:00
Ryan C. Gordon 52130bde40 diskaudio: Use SDL_Log, not fprintf. 2017-01-05 19:30:45 -05:00
Sam Lantinga 45b774e3f7 Updated copyright for 2017 2017-01-01 18:33:28 -08:00
Sam Lantinga 2ba66d0525 Fixed bug 3535 - Misplaced comment #if/#endif closure comment
Coriiander

This notice is about a misplaced comment.

Often times when we use an #if #endif sequence, the #endif is followed by a comment to indicate what #if statement it belonged to. The SDL_xaudio2.c file contains a misplaced comment, as follows (I stripped the other comments):

#ifdef __GNUC__
#  define SDL_XAUDIO2_HAS_SDK 1
#elif defined(__WINRT__)
#  define SDL_XAUDIO2_HAS_SDK
#include "SDL_xaudio2.h"
#else
#if 0
#include <dxsdkver.h>
#if (!defined(_DXSDK_BUILD_MAJOR) || (_DXSDK_BUILD_MAJOR < 1284))
#  pragma message("Your DirectX SDK is too old. Disabling XAudio2 support.")
#else
#  define SDL_XAUDIO2_HAS_SDK 1
#endif
#endif
#endif /* 0 */



That final /* 0 */ should be moved one line up. Like this (I tabbed it out for you to make it more clear):
2016-12-31 16:21:55 -08:00
Sam Lantinga 9492492d5f Fixed bug 3516 - fix build on illumos
Sylvain

trivial patch to fix the build on illumos

 -Werror=declaration-after-statement

https://gist.github.com/wiedi/15b71456667f7aa2a7f8815663723bb3
2016-12-26 01:56:52 -08:00
Sam Lantinga b2f6c4c1bd Fixed bus error when converting 16-bit to float for non-integral-multiple sample rates 2016-12-19 11:15:53 -08:00
Ryan C. Gordon 366c77a9f0 audio: fixed one more incorrectly-hardcoded value in the resamplers. 2016-12-18 20:17:33 -05:00
Ryan C. Gordon f956df23bb audio: fixed arbitrary upsampling (thanks, Sylvain!).
This was a leftover of simplifying the resamplers down from autogenerated
code; I forgot to make something that the generator hardcoded into something
variable.

Fixes Bugzilla #3507.
2016-12-17 16:15:24 -05:00
Ryan C. Gordon c023187548 audio: Fixed compiler warnings. 2016-12-06 12:23:17 -05:00
Ryan C. Gordon a0e003eebb Refactored the audio queueing code to a generic SDL_DataQueue interface.
This is not a public API (at the moment), but we will be needing this for
other internal things soon.
2016-12-06 02:23:54 -05:00
Ryan C. Gordon 8b960d4e0f Added SDL_VARIABLE_LENGTH_ARRAY so this #ifdef is localized to one place. 2016-12-06 02:20:58 -05:00
Sam Lantinga 26f05ecb4d Fixed missing prototypes on Android, patch from Sylvain 2016-12-02 02:25:12 -08:00
Ryan C. Gordon 52827361ae directsound: fixed compiler warnings. 2016-11-23 10:51:44 -05:00
Sam Lantinga 3615633571 Renaming of guard header names to quiet -Wreserved-id-macro
Patch contributed by Sylvain
2016-11-20 21:34:54 -08:00
Sam Lantinga 57d01d7d67 Patch from Sylvain to fix clang warnings 2016-11-13 22:57:41 -08:00
Sam Lantinga c13a077d15 Fixed bug 3488 - Random crashes (because Memory overlap in audio converters detected by Valgrind)
Vitaly Novichkov

Okay, when I researched code and algorithm, I tried to replace condition "while(dst >= target)" with "while(dst > target)" and crashes are gone.
Seems on some moments it tries to write into the place before memory block begin, therefore phantom crashes appearing after some moments.
2016-11-13 00:09:02 -08:00
Sam Lantinga 302a6e62aa Fixed bug 3484 - DSP driver does not detect /dev/dsp0
Tobias Kortkamp

using SDL 2.0.5 (and a repository checkout) on FreeBSD 11.0 I get this output
from testaudioinfo with SDL_AUDIODRIVER=dsp:

INFO: Found 8 output devices:
INFO:   0: /dev/dsp
INFO:   1: /dev/dsp1
INFO:   2: /dev/dsp2
INFO:   3: /dev/dsp3
INFO:   4: /dev/dsp4
INFO:   5: /dev/dsp5
INFO:   6: /dev/dsp6
INFO:   7: /dev/dsp7
INFO:
INFO: Found 3 capture devices:
INFO:   0: /dev/dsp
INFO:   1: /dev/dsp4
INFO:   2: /dev/dsp5
INFO:

This is /dev/sndstat:

Installed devices:
pcm0: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm1: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm2: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm3: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm4: <Realtek ALC887 (Rear Analog 7.1/2.0)> (play/rec)
pcm5: <Realtek ALC887 (Front Analog)> (play/rec) default
pcm6: <Realtek ALC887 (Rear Digital)> (play)
pcm7: <Realtek ALC887 (Onboard Digital)> (play)
No devices installed from userspace.

I'd expect to find /dev/dsp0 in the output device list.  It's not detected
because of a a small logic error in SDL_audiodev.c (see attached patch).

With the patch applied I get this which is what I'd expect:

INFO: Found 9 output devices:
INFO:   0: /dev/dsp
INFO:   1: /dev/dsp0
INFO:   2: /dev/dsp1
INFO:   3: /dev/dsp2
INFO:   4: /dev/dsp3
INFO:   5: /dev/dsp4
INFO:   6: /dev/dsp5
INFO:   7: /dev/dsp6
INFO:   8: /dev/dsp7
2016-11-11 12:41:06 -08:00
Philipp Wiesemann 6380d5c24e Fixed audio conversion for unsigned 16 bit data. 2016-11-07 21:10:01 +01:00
Philipp Wiesemann 7ad3a46d76 ALSA: Fixed compile warning about unused function.
Found by buildbot.
2016-11-05 21:23:17 +01:00
Sam Lantinga 6ed8213049 Fixed Windows build 2016-11-05 01:52:28 -07:00
Ryan C. Gordon a17abf10b7 Also patched to compile on C89 compilers. 2016-11-05 03:56:55 -04:00
Ryan C. Gordon 067f0c8482 Patched to compile on C89 compilers. 2016-11-05 03:53:59 -04:00
Ryan C. Gordon f3456e9a93 Reworked audio converter code.
This no longer uses a script to generate code for every possible type
conversion or resampler. This caused a bloat in binary size and and compile
times. Now we use a handful of more generic functions and assume staying in
the CPU cache is the most important thing anyhow.

This shrinks the size of the final build (in this case: macOS X amd64, -Os to
optimize for size) by 15%. When compiling on a single core, build times drop
by about 15% too (although the previous cost was largely hidden by multicore
builds).
2016-11-05 02:34:38 -04:00
Sam Lantinga 88f2d16e45 Fixed compiling on older versions of ALSA 2016-10-28 17:00:37 -07:00
Sam Lantinga fdcac1c24f Fixed audio data swizzling when the device channel map already matches what SDL expects 2016-10-28 16:47:06 -07:00
Sam Lantinga 62310c6bfd Work-around for a hang when USB devices are unplugged, contributed by James Zipperer 2016-10-12 22:25:19 -07:00
Sam Lantinga fed9b60492 Use SDL C runtime strlen() 2016-10-10 23:26:26 -07:00
Ryan C. Gordon ca42373fb5 alsa: more tapdancing to enumerate physical hardware devices.
Apparently some systems see "hw:", some see "default:" and some see
"sysdefault:" (and maybe others!). My workstation sees both "hw:" and
"sysdefault:" ...

Try to find a prefix we like and prioritize the prefixes we (think) we want
most. If everything else fails, if there's a "default" (not a prefix) device
name, list that by itself so the user gets _something_ here.

If we can't find a prefix we like _and_ there's no "default" device, report
no hardware found at all.
2016-10-10 15:29:18 -04:00
Sam Lantinga 52ae92eaf7 ALSA_snd_pcm_drop() can hang on some systems (Steam Link) so don't use that when shutting down the ALSA audio driver. 2016-10-07 19:08:22 -07:00
Sam Lantinga 93ff12ce83 Fixed bug 2952 - SDL_MixAudioFormat does not support audio format AUDIO_U16LSB/AUDIO_U16MSB
Simon Sandstr?m

As stated in Summary. The switch statement will execute the default case and set a SDL error message: "SDL_MixAudio(): unknown audio format".

There are atleast two more problems here:

1. SDL_MixAudioFormat does not notify the user that an error has occured and that a SDL error message was set. It took me awhile to understand why I couldn't mix down the volume on my AUDIO_U16LSB formatted audio stream.. until I started digging in the SDL source code.

2. The error message is incorrect, it should read: "SDL_MixAudioFormat(): unknown audio format".
2016-10-07 17:23:20 -07:00
Ryan C. Gordon 7d2108ce81 audio: Backed out the audio-thread detaching changes.
It added a ton of complexity. A simpler solution might arise at some
point though.
2016-10-07 19:39:43 -04:00
Ryan C. Gordon f6a280ab7f audio: Don't trust audio drivers to drain pending audio.
This tends to be a frequent spot where drivers hang, and the waits were
often unreliable in any case.

Instead, our audio thread now alerts the driver that we're done streaming audio
(which currently XAudio2 uses to alert the system not to warn about the
impending underflow) and then SDL_Delay()'s for a duration that's reasonable
to drain the DMA buffers before closing the device.
2016-10-07 15:13:46 -04:00
Ryan C. Gordon 551cdc8dec audio: better way to calculate buffer drain wait times. 2016-10-07 14:42:24 -04:00
Ryan C. Gordon 76f48acf63 audio: threading and device hang improvements.
This tries to make SDL robust against device drivers that have hung up,
apps don't freeze in catastrophic (but not necessarily uncommon) conditions.

Now we detach the audio thread and let it clean up and don't care if it
never actually runs to completion.
2016-10-07 14:35:25 -04:00
Sam Lantinga 3b0c79363d Some systems include both "default:" and "hw:" for the same usb device 2016-10-07 11:18:55 -07:00
Sam Lantinga 8e1994614c fix for finding ALSA hotplug devices on Steam Link
James Zipperer

The device names show up as "default:", not "hw:"
2016-10-06 06:08:16 -07:00
Sam Lantinga 9257b72d53 Backed out a very unsafe change that was trying to prevent audio hang at quit.
Ryan and I have ideas on a better way to handle this.
2016-10-05 00:12:16 -07:00
Sam Lantinga bac61096d8 ensure SDL_AUDIODEVICEREMOVED gets sent when hotplug removes a device
James Zipperer

The problem I was seeing was that the the ALSA hotplug thread would call SDL_RemoveAudioDevice, but my application code was not seeing an SDL_AUDIODEVICEREMOVED event to go along with it.   To fix it, I added some code into SDL_RemoveAudioDevice to call SDL_OpenedAudioDeviceDisconnected on the corresponding open audio device.  There didn't appear to be a way to cross reference the handle that SDL_RemoveAudioDevice gets and the SDL_AudioDevice pointer that SDL_OpenedAudioDeviceDisconnected needs, so I ended up adding a void *handle field to struct SDL_AudioDevice so that I could do the cross reference.

Is there some other way beside adding a void *handle field to the struct to get the proper information for SDL_OpenedAudioDeviceDisconnected?
2016-10-04 06:48:07 -07:00
Sam Lantinga 69cf170356 fix deadlock on close device
James Zipperer

snd_pcm_drain doesn't always drain when you unplug a usb device.  Use snd_pcm_drop instead
2016-10-04 06:46:46 -07:00
Sam Lantinga 2558c9c836 fix audio deadlock
James Zipperer

Close the audio device before waiting for the audio thread to complete, which fixes a situation where the audio thread never completes

Add an additional check in the audio thread to see if the device is enabled and bail out if the device is no longer enabled
2016-10-04 06:45:28 -07:00
Philipp Wiesemann 8e88f08150 Mac: Fixed whitespace around function return type. 2016-09-21 23:06:49 +02:00
Alex Szpakowski f0fca2880f Handle audio interruptions on iOS/tvOS. Fixes bugs 2569 and 2960. 2016-09-18 19:22:09 -03:00
Ryan C. Gordon 06700a905b emscripten: get even more aggressive about audio device closing.
I still get exceptions thrown sometimes on shutdown without this.
2016-09-18 18:07:47 -04:00
Alex Szpakowski 4209a1fd4c CoreAudio iOS/tvOS: Use AVFoundation instead of AudioSession. Fixes audio on tvOS.
Note that linking with AVFoundation is now required if you don't disable SDL_audio compilation on iOS and tvOS.
2016-09-15 19:59:57 -03:00
Alex Szpakowski f050576665 Initial Apple TV / tvOS support.
The Apple TV remote is currently exposed as a joystick with its touch surface treated as two axes. Key presses are also generated when its buttons and touch surface are used.

A new hint has been added to help deal with deciding whether to background the app when the remote's menu button is pressed: SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS.
2016-09-13 22:18:06 -03:00
Alon Zakai 63200ea395 optimize a getValue 2016-09-13 00:03:41 -07:00
Ryan C. Gordon 0265d3af9b coreaudio: Move from AudioUnits to AudioQueues.
AudioQueues are available in Mac OS X 10.5 and later (and iOS 2.0 and later).
Their API is much more clear (and if you don't mind the threading tapdance
to get its own CFRunLoop) much easier to use in general for our purposes.

As an added benefit: they seemlessly deal with format conversion in ways
AudioUnits don't: for example, my MacBook Pro's built-in microphone won't
capture at 8000Hz and the AudioUnit version wouldn't resample to hide this
fact; the AudioQueue version, however, can handle this.
2016-09-04 01:23:55 -04:00
Ryan C. Gordon 3b53304a94 coreaudio: capture devices should let the system allocate the render buffer. 2016-09-03 00:13:41 -04:00
Ryan C. Gordon fda7a3d158 coreaudio: Replaced an int with an SDL_bool. 2016-09-02 13:12:21 -04:00
Ryan C. Gordon f9d9708f6b coreaudio: Move some variable declarations to the top of the scope. 2016-09-02 13:11:28 -04:00
Ryan C. Gordon 2098bfb3ca emscripten: Be more aggressive when closing audio capture devices.
Fixes exceptions being thrown on shutdown.
2016-08-31 16:10:04 -04:00
Ryan C. Gordon c9bfcbde6e nacl: Patched to compile. 2016-08-28 18:52:25 -04:00
Ryan C. Gordon 714aa21136 Patched to compile on Haiku. 2016-08-28 18:24:44 -04:00
Ryan C. Gordon 2da1ec8354 Merge audio capture work back into the mainline. 2016-08-28 13:36:13 -04:00
Ryan C. Gordon 06dcdc7d48 Patched to compile. 2016-08-28 11:56:11 -04:00
Ryan C. Gordon 850da32f30 alsa: Implemented hotplug support, cleaned up device names. 2016-08-28 08:50:26 -07:00
Ryan C. Gordon cfa95fe68c alsa: don't enumerate virtual devices, just physical hardware. 2016-08-15 10:09:41 -04:00
Ryan C. Gordon 3b88f5c690 emscripten audio: check for an "undefined" object, remove some console.log(). 2016-08-12 00:03:58 -04:00
Ryan C. Gordon e435659c63 audio: Cleaned up "extern AudioBootStrap" list. 2016-08-11 22:26:58 -04:00
Ryan C. Gordon 6f4bcd2498 audio: Renamed some internal driver symbols in various targets. 2016-08-11 22:22:09 -04:00
Ryan C. Gordon 8f0af77354 android: implement audio capture support. 2016-08-11 22:04:49 -04:00
Ryan C. Gordon b78ec97496 directsound: Implemented audio capture support. 2016-08-10 16:00:16 -04:00
Ryan C. Gordon 21c7fe0060 windows: directsound should also map audio device GUIDs to proper names.
Moved this code from winmm into core so both can use it.

DirectSound (at least on Win10) also returns truncated device names, even
though it's handed in as a string pointer and not a static-sized buffer.  :/
2016-08-10 15:34:24 -04:00
Ryan C. Gordon b879595a2a audio: Patched to compile on C89 compilers. 2016-08-10 14:14:14 -04:00
Ryan C. Gordon 244d2dbcd5 emscripten audio: fix timer on capture's silence callback. 2016-08-10 14:13:48 -04:00
Ryan C. Gordon 7a8e4cb019 directsound: recalculate audiospec size before creating secondary buffer.
I think this was a bug before? Maybe I'm misunderstanding this, but it looks
like it was working because we allocate room for 8 chunks...
2016-08-09 19:35:46 -04:00
Ryan C. Gordon 358a168c9d emscripten audio: Added audio capture support. 2016-08-09 16:58:32 -04:00
Ryan C. Gordon 5de11a5fc5 Added a FIXME. 2016-08-09 16:58:06 -04:00
Ryan C. Gordon a05bde2170 audio: Only allocate fake_stream if we're using the standard audio threads. 2016-08-09 00:44:59 -04:00
Ryan C. Gordon be8d7a46fb audio: simplifed check for internal callback.
Easier to check when it's NULL instead of a list of known internal functions.
2016-08-09 00:44:05 -04:00
Ryan C. Gordon df4985e207 dsp: Implemented audio capture support. 2016-08-07 02:43:20 -04:00
Ryan C. Gordon a0ff2554c1 winmm: Try to get full device names from the Windows Registry. 2016-08-07 01:48:38 -04:00
Ryan C. Gordon ff7df7e687 winmm: Added a FIXME for truncated device names. 2016-08-06 23:05:02 -04:00
Ryan C. Gordon 51d1523380 winmm: Implemented audio capture support. 2016-08-06 19:34:32 -04:00
Ryan C. Gordon 4499e5bcc6 disk audio: Make default i/o delay match what device is meant to be running at. 2016-08-06 03:45:45 -04:00
Ryan C. Gordon 978df1ad74 disk audio: Implemented "capture" support, cleaned up some things. 2016-08-06 03:39:15 -04:00