Commit Graph

347 Commits

Author SHA1 Message Date
Ryan C. Gordon
ac88ffb7ea audio: don't allocate buffer in SDL_SetAudioPostmixCallback for NULL callback. 2023-09-20 10:03:02 -04:00
Ryan C. Gordon
2a950f6ae6 audio: Replace some SDL_memcpy calls with SDL_copyp. 2023-09-20 10:01:52 -04:00
Ryan C. Gordon
0dc0434a3e audio: Fixed race condition in subsystem shutdown.
This makes sure new devices can't be created when we're in the process of
shutting down.
2023-09-20 10:00:44 -04:00
Ryan C. Gordon
23f60203a3 audio: precalculate if we can use simple copies instead of the full mixer.
This just saves a bunch of conditionals (and an atomic get!) per iteration
of the audio thread.
2023-09-20 09:58:27 -04:00
Ryan C. Gordon
36b0f11414 audio: Optimize setting device formats during audio thread iteration.
The API entry point does a ton of unnecessary validation just to eventually
do a memcpy().
2023-09-20 09:09:15 -04:00
Sam Lantinga
fde8499f69 Use around 20ms for the audio buffer size
This value is well tested in production software. Larger values introduce audio latency and smaller values cause underflows on many setups.
2023-09-18 09:25:56 -07:00
Mathieu Eyraud
39c2f97373 Fix NULL dereference in SDL_OpenAudio 2023-09-18 09:44:59 -04:00
Ryan C. Gordon
2f43f7bc53 audio: Allow querying of device buffer size. 2023-09-13 11:03:17 -04:00
Ryan C. Gordon
cf95721130 audio: Added a hint to let apps force device buffer size.
This is a hint because apps might (probably mistakenly) believe they need this
to migrate from SDL2, but most things don't need to specify this.
2023-09-13 10:42:08 -04:00
Ryan C. Gordon
47d8c77c67 audio: Choose better default sample frame counts.
This might still need tweaking, but this is probably better than it was.
2023-09-13 10:27:11 -04:00
Sam Lantinga
cd633b9a88 Renamed SDL_IsAudioDevicePaused() to SDL_AudioDevicePaused()
This aligns with the SDL3 convention of removing "Is" from self-explanatory function names

Also improved some documentation in SDL_audio.h
2023-09-12 12:11:09 -07:00
Ryan C. Gordon
3a992af446 audio: Added a postmix callback to logical devices.
You can see it in action in testaudio by mousing over a logical device; it
will show a visualizer for the current PCM (whatever is currently being
recorded on a capture device, or whatever is being mixed for output on
playback devices).

Fixes #8122.
2023-09-09 16:26:37 -04:00
Ryan C. Gordon
fafbea1ced audio: Move internal float32 mixing to a simplified function 2023-09-07 10:48:04 -04:00
Ryan C. Gordon
38c8fc05c5 audio: Remove ChooseMixStrategy.
This is adds complexity and fragility for small optimization wins.

The biggest win is the extremely common case of a single stream providing
the only output, so we'll check for that and skip silencing/mixing/converting.

Otherwise, just use a single mixer path.
2023-09-07 10:44:30 -04:00
Brick
f2ca9a615b Added SDL_AUDIO_FRAMESIZE 2023-09-05 17:56:58 -07:00
Brick
53122593f8 Added SDL_AUDIO_BYTESIZE 2023-09-05 17:56:58 -07:00
Sam Lantinga
233789b0d1 Audio types have the same naming convention as other SDL endian types, e.g. [S|U][BITS][LE|BE]
Native endian types have no LE/BE suffix
2023-09-04 09:48:44 -07:00
Brick
47bcb078f5 Fixed some incorrect SDL_AUDIO_F32 uses 2023-09-04 07:46:18 -07:00
Ryan C. Gordon
fd7cd91dc9 audio: Mix multiple streams in float32 to prevent clipping.
This only does this work if actually mixing; if the physical device only
has a single stream bound to it, it'll just write the data to the hardware
without the extra drama.

Fixes #8123.
2023-08-31 23:46:33 -04:00
Ryan C. Gordon
9097573e37 audio: Choose a mixing strategy on each iteration.
Currently it's SILENCE (just zero out the mix buffer), COPYONE (one stream
writes directly into the hardware's buffer), or MIX (everything gets mixed
together before sending to the hardware).

Devices that aren't doing anything result in SILENCE. Devices playing
one thing result in COPYONE.

This lets the two most common states take what are likely significantly
faster approaches.

There will likely be some other strategies later (like when we offer a
postmix callback, etc).
2023-08-31 14:47:06 -04:00
Ryan C. Gordon
4e0c7c91fc audio: PlayDevice() should return an error code.
Higher level code treats errors as fatal and disconnects the device.
2023-08-30 19:17:19 -04:00
Ryan C. Gordon
3699b12ed0 audio: Fixed some "is_*" variables to be cleaner and/or more specific.
Requested at https://github.com/libsdl-org/SDL/pull/8165#discussion_r1306700881_
2023-08-29 10:46:14 -04:00
Ryan C. Gordon
2471d8cc2a audio: Fixed logic error in SDL_OpenAudioDeviceStream. 2023-08-27 19:30:47 -04:00
Sam Lantinga
82db2b58f9 Renamed audio stream callback and moved the userdata parameter first
In general SDL API callbacks are called with the userdata paramter first, to mimic C++ method call convention
2023-08-27 14:43:21 -07:00
Ryan C. Gordon
58c859f64d audio: Rename SDL_GetAudioStreamBinding to SDL_GetAudioStreamDevice. 2023-08-27 16:54:30 -04:00
Ryan C. Gordon
1e775e0eef audio: Replace SDL_CreateAndBindAudioStream with SDL_OpenAudioDeviceStream.
This is meant to offer a simplified API for people that are either migrating
directly from SDL2 with minimal effort or just want to make noise without
any of the fancy new API features.

Users of this API can just deal with a single SDL_AudioStream as their only
object/handle into the audio subsystem.

They are still allowed to open multiple devices (or open the same device
multiple times), but cannot change stream bindings on logical devices opened
through this function.

Destroying the single audio stream will also close the logical device behind
the scenes.
2023-08-27 16:54:30 -04:00
Sam Lantinga
bd088c2f99 Revert "Clarify whether an audio function expects a physical or logical device ID"
This reverts commit 506a133d84.

Physical and logical audio devices are intended to be interchangeable at the API level.
2023-08-27 13:42:20 -07:00
Sam Lantinga
506a133d84 Clarify whether an audio function expects a physical or logical device ID 2023-08-26 17:03:01 -07:00
Brick
5b696996cd Added ResampleFrame_SSE 2023-08-25 08:43:56 -04:00
Ryan C. Gordon
e7d56dd0b2 audio: Renamed new API SDL_UnpauseAudioDevice to SDL_ResumeAudioDevice. 2023-08-05 19:20:14 -04:00
Mathieu Eyraud
778e8185cd Fix size of memcpy in SDL_AudioDeviceFormatChangedAlreadyLocked
And add diagnostic that allows to find this kind of issue in clang-tidy
2023-08-05 14:14:45 -04:00
Ryan C. Gordon
5ff87c6d4a android: Reworked audio backends for SDL3 audio API.
This involved moving an `#ifdef` out of SDL_audio.c for thread priority,
so the default ThreadInit now does the usual stuff for non-Android platforms,
the Android platforms provide an implementatin of ThreadInit with their
side of the `#ifdef` and other platforms that implement ThreadInit
incorporated the appropriate code...which is why WASAPI is touched in here.

The Android bits compile, but have not been tested, and there was some
reworkings in the Java bits, so this might need some further fixes still.
2023-07-30 11:56:41 -04:00
Ryan C. Gordon
455eef4cd9 audio: Use AtomicAdd for device counts, don't treat as a refcount. 2023-07-30 11:56:39 -04:00
Ryan C. Gordon
c58d95c343 wasapi: Reworked for new SDL3 audio API, other win32 fixes.
The WinRT code has _also_ be updated, but it has not been
tested or compiled, yet.
2023-07-30 11:56:35 -04:00
Ryan C. Gordon
be0dc630b7 audio: Fixed incorrect assertion 2023-07-30 11:56:35 -04:00
Ryan C. Gordon
4399b71715 audio: Generalize how backends can lookup an SDL_AudioDevice. 2023-07-30 11:56:34 -04:00
Ryan C. Gordon
2fb122fe46 audio: backends now "find" instead of "obtain" devices by handle.
Every single case of this didn't want the device locked, so just looking
it up without having to immediately unlock it afterwards is better here.

Often these devices are passed on to other functions that want to lock them
themselves anyhow (disconnects, default changes, etc).
2023-07-30 11:56:34 -04:00
Ryan C. Gordon
121a2dce15 audio: Make sure device->hidden is NULL after CloseDevice 2023-07-30 11:56:09 -04:00
Ryan C. Gordon
3f4f004794 audio: Remove an assertion that no longer makes sense.
One may happen to call SDL_AudioThreadFinalize when thread_alive is not set.
2023-07-30 11:56:07 -04:00
Ryan C. Gordon
65d296ef1a audio: Use SDL_powerof2 instead of reinventing it. 2023-07-30 11:56:06 -04:00
Ryan C. Gordon
0999a090a7 audio: More tweaking of device->thread_alive 2023-07-30 11:56:05 -04:00
Ryan C. Gordon
f94ffd6092 audio: Fixed logic error 2023-07-30 11:56:05 -04:00
Ryan C. Gordon
8473e522e0 audio: unify device thread naming. 2023-07-30 11:56:04 -04:00
Ryan C. Gordon
258bc9efed audio: PlayDevice now passes the buffer, too, for convenience. 2023-07-30 11:56:04 -04:00
Ryan C. Gordon
e518149d14 audio: Fixed locking in SDL_AudioDeviceDisconnected 2023-07-30 11:56:03 -04:00
Ryan C. Gordon
22afa5735f audio: FreeDeviceHandle should pass the whole device, for convenience. 2023-07-30 11:56:03 -04:00
Ryan C. Gordon
e969160de0 audio: unset a freed variable to NULL 2023-07-30 11:56:03 -04:00
Ryan C. Gordon
1fc01b0300 audio: Try to definitely have a default device set up. 2023-07-30 11:56:03 -04:00
Ryan C. Gordon
b60a56d368 audio: take first reported device if no default was specified. 2023-07-30 11:56:02 -04:00
Ryan C. Gordon
a8323ebe68 audio: Better handling of ProvidesOwnCallbackThread backends. 2023-07-30 11:56:02 -04:00