commit a0eeddb88a27f9df922b090cc11239f71ab8aad5
Author: mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 18:07:15 2026 +0100
Commit: mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 18:07:15 2026 +0100
xex host: lower audio latency (fill several times a frame)
Sound was made once a frame after the vblank wait and queued ~25-43 ms
ahead, on top of the system's own ~21 ms. The host now makes core audio
itself before the wait, after the flip and in 4 ms slices of timer waits,
so a 5-frame (~27 ms) queue covers the gaps.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
platform/xex/source/XexHost.cpp | 58 +++++++++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 19 deletions(-)
diff --git a/platform/xex/source/XexHost.cpp b/platform/xex/source/XexHost.cpp
index c37929f..5622bf6 100644
--- a/platform/xex/source/XexHost.cpp
+++ b/platform/xex/source/XexHost.cpp
@@ -32,6 +32,7 @@
#include "../../../source/nibblehelpers.h"
#include "../../../source/logger.h"
#include "../../../source/filehelpers.h"
+#include "../../../source/Audio.h"
#include "xex_runtime.h"
#include "xex_video.h"
@@ -196,7 +197,11 @@ static inline uint64_t now() { return xex_timebase(); }
static const int CoreRate = 22050;
static const int OutRate = XEX_AUDIO_RATE;
-static const unsigned TargetQueuedFrames = 8; // 8 x 5.33 ms = ~43 ms ahead
+// The queue is topped up several times a frame (before the vblank wait,
+// after the flip, during timer waits), so it only has to cover the longest
+// gap between top-ups: 5 x 5.33 ms = ~27 ms ahead. Xbox 360 games have
+// ~21 ms in XAudio2 itself; RetroArch defaults to 64 ms.
+static const unsigned TargetQueuedFrames = 5;
static const int MaxCoreFrames = 2048;
static uint32_t coreBuf[MaxCoreFrames];
@@ -206,6 +211,7 @@ static int pendingCount = 0; // samples of the frame being built
static double resamplePos = 0.0; // fractional position carried between chunks
static int16_t resamplePrev = 0; // last core sample of the previous chunk
static bool audioOk = false;
+static Audio *coreAudio = nullptr;
static unsigned audioDropped = 0; // frames lost to a full ring
static void audioSetup() {
@@ -224,6 +230,24 @@ static void pushOutSample(float v) {
xex_audio_queue();
}
+static void audioPlayChunk();
+
+// Makes core audio up to the queue target, straight away (the core's own
+// once-a-frame fill after the flip is turned off: shouldFillAudioBuff).
+static void audioTopUp() {
+ if (!audioOk || !coreAudio) return;
+ unsigned queued = xex_audio_queued();
+ if (queued >= TargetQueuedFrames) return;
+ int missingOut = (int)(TargetQueuedFrames - queued) * XEX_AUDIO_FRAME_SAMPLES - pendingCount;
+ if (missingOut <= 0) return;
+ size_t frames = (size_t)missingOut * CoreRate / OutRate + 1;
+ if (frames > (size_t)MaxCoreFrames) frames = MaxCoreFrames;
+ coreAudio->FillAudioBuffer(coreBuf, 0, frames);
+ coreFrames = frames;
+ audioPlayChunk();
+ coreFrames = 0;
+}
+
static void audioPlayChunk() {
if (!audioOk || coreFrames == 0) return;
@@ -433,6 +457,7 @@ void Host::oneTimeSetup(Audio *audio) {
mouseOffsetX = destX;
mouseOffsetY = destY;
+ coreAudio = audio;
audioSetup();
lastFrameTb = now();
}
@@ -568,8 +593,15 @@ void Host::waitForTargetFps() {
uint64_t elapsed = t - lastFrameTb;
if (elapsed < targetFrameTb) {
uint64_t remaining = targetFrameTb - elapsed;
- unsigned ms = (unsigned)(remaining * 1000 / tbFreq);
- if (ms > 1) xex_sleep_ms(ms - 1); // kernel sleep, then spin
+ // sleep in slices of up to 4 ms, keeping the audio queue topped up
+ for (;;) {
+ audioTopUp();
+ uint64_t left = targetFrameTb - (now() - lastFrameTb);
+ if ((int64_t)left <= 0) break;
+ unsigned ms = (unsigned)(left * 1000 / tbFreq);
+ if (ms <= 1) break;
+ xex_sleep_ms(ms - 1 > 4 ? 4 : ms - 1);
+ }
while (now() - lastFrameTb < targetFrameTb) {}
perfSleepTb += remaining;
lastFrameTb += targetFrameTb;
@@ -602,6 +634,7 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
// 2. the back buffer, once the previous flip is on screen (at 60 fps
// this wait is the frame pacing)
+ audioTopUp(); // before the wait for the previous flip
uint64_t tbWaitStart = now();
xex_frame f = xex_video_begin();
uint32_t *pixels = f.pixels;
@@ -658,6 +691,7 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
// 4. flip at the next vertical blank
xex_video_present();
+ audioTopUp();
uint64_t tbEnd = now();
perfFrameDone(emuTb + (tbWaitStart - tbStart), tbBlitStart - tbWaitStart, tbEnd - tbBlitStart, tbBlitStart);
@@ -665,22 +699,8 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
}
bool Host::shouldFillAudioBuff() {
- if (!audioOk) return false;
-
- unsigned queued = xex_audio_queued();
- if (queued >= TargetQueuedFrames) {
- coreFrames = 0;
- return false;
- }
- int missingOut = (int)(TargetQueuedFrames - queued) * XEX_AUDIO_FRAME_SAMPLES - pendingCount;
- if (missingOut <= 0) {
- coreFrames = 0;
- return false;
- }
- size_t frames = (size_t)missingOut * CoreRate / OutRate + 1;
- if (frames > (size_t)MaxCoreFrames) frames = MaxCoreFrames;
- coreFrames = frames;
- return true;
+ // the host fills audio itself (audioTopUp), several times a frame
+ return false;
}
void *Host::getAudioBufferPointer() {