Add audio underrun and late-update counters, and a knob for the priority of the game's audio thread.

NX2NAVI_AUDIO_STATS=1 logs output underruns, late passes of the audio render thread, waits for the renderer lock,
late audio updates from the game and voices that ran out of queued sound, each with the game frame, to find where
a crackle comes from. NX2NAVI_AUDIO_GAME_PRIO raises the thread that sends RequestUpdate while its update is read
(1) or also keeps it above normal (2). Both are off by default, so the audio path behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tangerineturtle1
2026-11-09 06:09:18 -05:00
committed by chrg6
parent fe3cca739b
commit 762bd3f934
5 changed files with 215 additions and 2 deletions

View File

@@ -61,6 +61,7 @@ typedef struct Mixer {
int target, period; // output target and device period, frames (output_open)
uint64_t stall_ns;
_Atomic uint64_t pulled_ns; // kern_now_ns of the device's last request, 0 before the first
_Atomic uint64_t pull_gap_max; // NX2NAVI_AUDIO_STATS: longest gap between two requests (ns)
_Atomic uint64_t flush_until; _Atomic unsigned stop_epoch; unsigned callback_epoch;
EffectState *effect; int *effect_order; unsigned effect_reported;
uint64_t *sink_address; uint32_t *sink_size, *sink_offset, *sink_old, *sink_written;
@@ -203,7 +204,9 @@ static void SDLCALL sdl_pull(void *user, SDL_AudioStream *stream, int additional
Mixer *m = user;
float tmp[OUT_MAX * 1024];
const uint32_t frame_bytes = (uint32_t)m->channels * sizeof(float);
atomic_store_explicit(&m->pulled_ns, kern_now_ns(), memory_order_release);
uint64_t now = kern_now_ns(), prev = atomic_exchange_explicit(&m->pulled_ns, now, memory_order_acq_rel);
if (prev && now > prev && now - prev > atomic_load_explicit(&m->pull_gap_max, memory_order_relaxed))
atomic_store_explicit(&m->pull_gap_max, now - prev, memory_order_relaxed); // a lost race only loses a sample
while (additional > 0) {
uint32_t frames = (uint32_t)additional / frame_bytes;
if (!frames) break;
@@ -263,6 +266,18 @@ int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now) {
return ns < 500000 ? 500000 : ns; // until the device has taken it below the target
}
int audio_mixer_out_stats(AudioRenderer *r, AudioOutStats *o) {
Mixer *m = r->mixer;
if (!m || !m->device_ok) return 0;
o->underruns = atomic_load(&m->underruns);
o->missing_frames = atomic_load(&m->missing_frames);
o->skipped_frames = atomic_load(&m->skipped_frames);
o->overruns = atomic_load(&m->overruns);
o->queued_frames = atomic_load(&m->wr) - atomic_load_explicit(&m->rd, memory_order_acquire);
o->pull_gap_max_ns = atomic_exchange(&m->pull_gap_max, 0);
return 1;
}
// Render thread only.
static void output_push(Mixer *m, const float *out, int frames) {
const int oc = m->channels;