Skip to content

Conversation

ITotalJustice
Copy link
Contributor

audout has always suffered from a bug where the playback would become messed up when resuming from sleep.

Looking into the issue, i noticed that the distorted playback sounds exactly like samples being dropped, as if we are working with a single audio buffer.

Looking at audoutWaitPlayFinish, the bug is very clear. Basically, when the switch goes to sleep, both (or all) audio buffers have finished playing, so the event is fired multiple times. In the next call to audoutWaitPlayFinish, the event is waited on, which returns immediately because the event was already fired. However, this clears the event, so the next call will actually block until that single buffer has finished, which results in dropped samples. Another way of looking at the bug is like this.

// push 2 buffers.
audoutAppendAudioOutBuffer(buf[0]) // push buf 0
audoutAppendAudioOutBuffer(buf[1]) // push buf 0

// sleep long enough for both buffers to finish (simulating sleep).
svcSleepThread(1e+9);

// audio dropouts here because it will only function on 1 buffer. 
for (;;) {
  AudioOutBuffer* released;
  audoutWaitPlayFinish(&released, -1);

  audoutAppendAudioOutBuffer(released);
}

The fix is to first change the event to not auto clear (so that we can clear it ourselves) and then poll audoutGetReleasedAudioOutBuffer before waiting on the event. In the above example, this would return immediately to the first 2 calls after the sleep, which fixes the issue. If audoutGetReleasedAudioOutBuffer fails, then we wait on the event, and the code functions the same as before.


Also, if this fix gets merged, could we merge back audout into SDL2 rather than using audren? As i understand, audren was added because of this bug, however audren has its own limitations such as session limits. I would open an issue / PR for that repo, but i am still for no reason banned.

@HookedBehemoth
Copy link
Contributor

I've tested this PR in the linked repository. Resolves our long standing issue, which was why we switched to audren in the past.

Audren never was optimal as there are only two sessions and some games (notably everything by activision) uses both already and the audio-sysmodule fatals on exhaustion.

@fincs fincs merged commit 8d3dfbf into switchbrew:master Jul 25, 2025
1 check passed
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.

4 participants