Commit graph

238 commits

Author SHA1 Message Date
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
Ryan C. Gordon
1dffb72c1d
pipewire: Hooked up default device change notifications. 2023-07-30 11:56:02 -04:00
Ryan C. Gordon
cfc8a0d17d
pipewire: First shot at moving to the new SDL3 audio interfaces.
This needs a little work still, but it mostly works.
2023-07-30 11:56:01 -04:00
Ryan C. Gordon
13202642a3
aaudio: Fixed capitialization, plus some minor cleanups. 2023-07-30 11:56:01 -04:00
Ryan C. Gordon
dac25fe9eb
audio: Seperate audio capture into Wait/Read operations.
Before it would just block in read operations, but separating this out
matches what output devices already do, and also lets us separate out the
unlocked waiting part from the fast part that holds the device lock.
2023-07-30 11:55:59 -04:00
Ryan C. Gordon
3e10c0005d
audio: Capture devices should respect logical device pausing. 2023-07-30 11:55:59 -04:00
Ryan C. Gordon
7e700531c5
audio: Allow SDL_OpenAudioDevice to accept a NULL spec.
This means "I don't care what format I get at all" and will just use
the device's current (and/or default) format.

This can be useful, since audio streams cover the differences anyhow.
2023-07-30 11:55:59 -04:00
Ryan C. Gordon
883aee32c5
audio: Let default formats differ for output and capture devices. 2023-07-30 11:55:58 -04:00
Ryan C. Gordon
2be5f726d4
audio: Removed debug logging. 2023-07-30 11:55:57 -04:00
Ryan C. Gordon
fe1daf6fb5
audio: Mark disconnected default devices as "zombies".
Zombie devices just sit there doing nothing until a new default device
is chosen, and then they migrate all their logical devices before being
destroyed.

This lets the system deal with the likely outcome of a USB headset being
the default audio device, and when its cable is yanked out, the backend
will likely announce this _before_ it chooses a new default (or, perhaps,
the only device in the system got yanked out and there _isn't_ a new
default to be had until the user plugs the cable back in).

This lets the audio device hold on without disturbing the app until it can
seamlessly migrate audio, and it also means the backend does not have to
be careful in how it announces device events, since SDL will manage the
time between a device loss and its replacement.

Note that this _only_ applies to things opened as the default device
(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, etc). If those USB headphones are the
default, and one SDL_OpenAudioDevice() call asked for them specifically and
the other just said "give me the system default," the explicitly requested
open will get a device-lost notification immediately. The other open will
live on as a zombie until it can migrate to the new default.

This drops the complexity of the PulseAudio hotplug thread dramatically,
back to what it was previously, since it no longer needs to fight against
Pulse's asychronous nature, but just report device disconnects and new
default choices as they arrive.

loopwave has been updated to not check for device removals anymore; since
it opens the default device, this is now managed for it; it no longer
needs to close and reopen a device, and as far as it knows, the device
is never lost in the first place.
2023-07-30 11:55:56 -04:00