XboxDev / nxdk-sdl

SDL2 (adapted for original Xbox / nxdk toolchain)
Other
20 stars 8 forks source link

Rewrite Xbox audio to use the default SDL audio thread #27

Closed JayFoxRox closed 4 years ago

JayFoxRox commented 4 years ago

This is a rewrite of the crashy audio driver that was recently introduced.

This new code is run on a different thread, which lifts many restrictions from the SDL callback (although we potentially waste more CPU cycles on context switches now).

Initialization:

To summarize: We start playing silence at the second buffer (and following), until playback wraps around and reaches the first buffer with the actual application data (which was hopefully queued by the mainloop by then). This means we force a couple of milliseconds of silence on application startup, and we do force a higher latency, but we get more headroom for buffer underruns and more consistent timing at startup.

During the mainloop:

When a buffer finishes playback:


Some remaining issues which should be looked at during review:

I'll create issues after merge, if we don't deal with them during review.

JayFoxRox commented 4 years ago

I have documented some of the issues in https://discord.com/channels/428359196719972353/428360618102226946/711290999091101766 on XboxDev Discord::

Broken wave the highlighted part is broken. the high-part [32 bytes] is the end of an AC97 XAudioProvideSamples call [offset by 32 bytes to the right]. the flatline near 7.310 is the line between 2 separate SDL callbacks / buffer fills I also tried increasing the number of buffers and the problem persisted

There are still some issues with this, but it's "better than master". While the audio is still bad, it no longer appears to crash.

I feel like we should merge this after a quick review, and then someone will have to debug it. I currently lack the tools to debug it properly and I have also stared at this problem for too long.

A good way to check if this might be a problem with math functions, would be to dump the generated audio to disk or network (I haven't done either of that yet). A full log of events (interrupts, SDL callbacks, AC97 submissions) would also be helpful.

JayFoxRox commented 4 years ago

@thrimbor I'd like to delay this a bit.

I'd like to wait for https://github.com/XboxDev/nxdk/pull/364. That should resolve the issues with SDL audio. But this will need testing, and might require changes to this SDL audio PR.

With the current XAudio design, it would have been impossible to implement SDL audio correctly. I've explained some of the issues in the PR (originally on XboxDev Discord in https://discord.com/channels/428359196719972353/428360618102226946/719720606756634675, too).

While the current code will work with my planned XAudio changes, we might still have to increase the buffer size to support some commonly used formats, due to slow CPU conversion and stupid logic in SDL (it doesn't convert while waiting for buffer playback to finish, so we might need a longer buffer to give the CPU more time).

thrimbor commented 4 years ago

Since the XAudio changes have landed quite a while ago, I gave this another round of testing, and I'm going ahead with merging.