ogc: rework audio driver to avoid clicks and gaps #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When playing an audio stream the previous implementation was producing frequent clicks (also audible in the emulator). The reason is that SDL runs a thread that does these operations in a loop:
The clicks occurred because after a buffer had finished playing, our completion callback would set the condition variable, allowing the audio thread (which was blocking in point 4) to continue. However, since libaesnd does not offer an API to queue audio buffer, we were sending the next audio buffer only in PlayAudio(), that is after the points 1 and 2 were performed. And since point 2 involves copying buffers and possibly even converting them, this would often generate an audible delay.
The solution is to play the next buffer directly from the completion callback. The waiting logic has been rewritten to use a semaphore blocking on the availability of a buffer for writing (not for playback!).
The number of buffers has also been increase to protect against sudden spikes in CPU usage, although in the test applications 2 buffers are good enough too.