Commit Graph

953 Commits

Author SHA1 Message Date
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
Ozkan Sezer
1d8dfbb223 avoid type redefinition errors after PR/8181 2023-09-04 21:29:40 +03:00
Sam Lantinga
3a932141e4 Restore audio format binary compatibility with SDL 2.0 2023-09-04 10:16:53 -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
0e552761b7 Renamed AudioStreamSpeed to AudioStreamFrequencyRatio 2023-09-04 07:46:18 -07:00
Brick
47bcb078f5 Fixed some incorrect SDL_AUDIO_F32 uses 2023-09-04 07:46:18 -07:00
Brick
a59152688a Try and avoid overflow when handling very large audio streams 2023-09-01 14:38:45 -04:00
Brick
e55844274d Added SDL_(Get|Set)AudioStreamSpeed 2023-09-01 14:38:45 -04:00
Brick
43c3c5736d Track the formats of data in an SDL_AudioStream 2023-09-01 14:38:45 -04:00
Brick
337fed3df6 Tweaked ResampleFrame_SSE
Use _mm_unpack(lo|hi)_ps instead of _mm_shuffle_ps
2023-09-01 14:38:45 -04: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
b385dc3b68 n3dsaudio: Patched to compile. 2023-08-30 19:53:54 -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
Brick
f8286df167 Fixed ResampleFrame_SSE doing unnecessary work 2023-08-27 13:08:15 -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
041dbd6b5f Fixed GetResamplerAvailableOutputFrames
Non-euclidean division is a pain
2023-08-26 09:04:47 -04:00
Sam Lantinga
5755de07a6 Fixed build warnings 2023-08-25 08:45:33 -07:00
Brick
106abce69f Refactored GetAudioStreamDataInternal buffer handling
The final conversion step should now always go straight into the output buffer.
2023-08-25 08:43:56 -04:00
Brick
e44f54ec54 Avoid using hex-floats 2023-08-25 08:43:56 -04:00
Brick
5b696996cd Added ResampleFrame_SSE 2023-08-25 08:43:56 -04:00
Brick
ab83f75bb9 Make sure GetAudioStreamDataInternal is called with a valid length 2023-08-25 08:43:56 -04:00
Brick
6a73f74b6b Rebuild full ResamplerFilter (left wing + right wing) at runtime 2023-08-25 08:43:56 -04:00
Brick
0c15ce0060 Add a missing int cast 2023-08-25 08:43:56 -04:00
Brick
b74ee86b1d Optimized ResampleAudio, with special cases for 1 and 2 channels
This would also benefit from some SIMD, since it's just a bunch of multiply-adds
2023-08-25 08:43:56 -04:00
Brick
fba6e1e3d3 Removed ResamplerFilterDifference
It takes 1 extra multiply to calculate the correct interpolation,
but I think the improvement in cache locality (and binary size) outweighs that.
2023-08-25 08:43:56 -04:00
Brick
9f7a22fa45 Removed 64-bit handling from AudioConvertByteswap 2023-08-25 08:43:56 -04:00
Brick
1f5327a9fb Removed future_buffer, left_padding, and right_padding from SDL_AudioStream 2023-08-25 08:43:56 -04:00
Brick
71ad52d6db Lowered SDL_GetAudioStreamData to 32 KB
No particular reason for this number, but 1 MB was a bit silly
2023-08-25 08:43:56 -04:00
Ryan C. Gordon
fb79211732 emscriptenaudio: Fire the capture silence_callback at an interval.
Previously it was using setTimeout, not setInterval, so it would only fire
once, which was obviously a mistake.
2023-08-23 16:12:30 -04:00
Ryan C. Gordon
5191b20541 emscriptenaudio: Don't bother undefining things about to be unreachable.
Since the top-level table is getting undefined, all the things in it will
be unreachable and eligible for garbage collection without explicitly
nulling them out.
2023-08-23 16:11:08 -04:00
Ryan C. Gordon
fd75a4ca05 emscriptenaudio: Deal with blocked audio devices better.
Now, if the AudioContext starts in a "suspended" state, because the browser
blocked it from playing by default, we we run the audio "thread" in a timer
and throw away the generated audio. Once the AudioContext is allowed to
resume, we clear this timer.

The end result is that the app will continue to drain its audio queue
instead of consuming more memory over time (and, if it relies on an audio
callback to make progress, continue to run!), with the effect that the
page is merely silent but otherwise functioning as intended.

Once the user interacts with the page and the browser permits the the
AudioContext to run for real, audio should still be in sync, instead of
just starting to play audio that might now be at least several seconds behind.
2023-08-23 14:32:23 -04:00
Ryan C. Gordon
981b8a337a emscriptenaudio: Remove unnecessary functions. 2023-08-23 14:32:23 -04:00
Brick
e6c878824c Fixed ResampleAudio interpolation factor calculation 2023-08-22 08:34:22 -04:00
Brick
4983638630 Misc audio tweaks/cleanup 2023-08-21 16:02:54 -04:00
Brick
72d9d53def Invert the inner ResampleAudio loops to avoid doing unnecessary work 2023-08-21 16:02:54 -04:00
Brick
88123a5109 The history buffer should always have the maximum possible padding frames 2023-08-21 16:02:54 -04:00
Brick
96e47f1657 Clamp results of GetResampler(AvailableOutput|NeededInput)Frames 2023-08-21 16:02:54 -04:00
Brick
14e38b17d6 Removed assertions from inner ResampleAudio loop 2023-08-21 16:02:54 -04:00
Brick
9d413dfdc2 The history buffer doesn't need to be so large 2023-08-21 16:02:54 -04:00
Brick
2788e848f8 Allow resampling less than 1 frame of input 2023-08-21 16:02:54 -04:00
Brick
383084e0ad Pre-calculate resampling rate, and use it instead of .freq in most places 2023-08-21 16:02:54 -04:00