Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SPU audio single-buffered (audio latency improvement) #2286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Sparronator9999
Copy link

@Sparronator9999 Sparronator9999 commented Mar 7, 2025

Why this PR exists

Since commit 05b94ef, audio produced by the emulated SPU has been double-buffered (2x2048 samples, 128 ms/~7.7 frames latency). Adding on top the output latency (another 1024 samples/~21ms/1.4 frames) gives a maximum output latency of just under 150(!) ms (>10 frames) of audio lag. This makes some games (in particular rhythm games) feel very unresponsive to input compared to real hardware.

What this PR does

This pull request does the following:

  • Reverts the SPU audio output to single-buffered, but keeping the thread safety (via use of the AudioLock mutex) and reduces the buffer size back to 1024 (what it was before commit 05b94ef). This improves total audio latency to a maximum ~53 ms (~3.2 frames), which greatly improves responsiveness of the emulated software.
  • Makes the audio output buffer size configurable via a single variable (named audioBufSize). This (along with audioFreq) should be made configurable in a future release (I might give it a go at some point).
  • Audio init now handles driver changes to the output buffer size (same as what is done with the sample rate). This prevents buffer sizes that the audio driver can't handle (too small/large) being used (and adding SDL overhead). This will become useful when the buffer size is made user-configurable (see below).

Notes

  • This is one of my first times ever writing anything in C++, scrutinize accordingly.
  • I did read the contribution guidelines, which said I should use PascalCase for my variable name, but I used camelCase instead (since that's what the previous person did, so I copied them without reading the contribution guidelines before commiting my work :/). Should I change this (along with variable type to e.g. s32)?

EDIT: fixed latency calculations (I accidentally used 48 KHz for SPU buffer latency calculations when it should've been ~32 KHz...)

Basically reverts audio buffer handling to what it was before commit 05b94ef, but with the mutexes kept for thread safety (which the referenced commit was trying to do).

The SPU audio buffer should still be thread-safe in theory... right?
This commit changes the audio output buffer to be configured by a variable, and fixes the case where the sound driver may change the buffer size after calling SDL_OpenAudioDevice (e.g. if the buffer size is set too low for the driver to handle).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant