diff --git a/docs/building.md b/docs/building.md index b49ee7d2..ef8cb1bf 100644 --- a/docs/building.md +++ b/docs/building.md @@ -359,12 +359,17 @@ sites do: at 144 fps each of 64 water objects gets its ripple steps and the day gives what the game computes. No game files. A change to how the mixer applies volumes (`runtime/audio_mixer.c` `accumulate_ramp`, the voice and splitter paths -of `audio_mixer_render`) passes `python tests\audio_mixer_check.py`, which builds `audio_mixer.c` with the rest of -the runtime stubbed and mixes voices of one constant sample, so a mix buffer shows the gain sample by sample: a +of `audio_mixer_render`) or to what it owes the device after a stall (`ring_pull`, `audio_mixer_owed`) passes +`python tests\audio_mixer_check.py`, which builds `audio_mixer.c` with the rest of the runtime stubbed. It mixes +voices of one constant sample, so a mix buffer shows the gain sample by sample: a volume stepped from 0 to 1 rises in equal steps across the next 5 ms frame and holds after it, for a voice into a mix, a voice into a splitter destination and a submix into one; a voice that starts, was silent in the frame before or was moved to another mix, a destination that is new or was moved, and the first frame after the renderer starts take the -new volume at once; `NX2NAVI_AUDIO_RAMP=0` applies every volume at once. No game files and no audio device. +new volume at once; `NX2NAVI_AUDIO_RAMP=0` applies every volume at once. It also makes the device's requests by +hand: the silence of an underrun, up to the prefill's return, is owed in whole render frames, which are rendered and +not queued; the silence before the first prefill and across a stop is not owed, and more than 3 seconds of it is given +up. The render thread's own timing (`runtime/audren.c` `render_thread`) is not built by it. No game files and no audio +device. A change to the mouse camera (`runtime/mouse_direct.c`, the mouse path of `runtime/input.c`, the set `mouse_direct` of `recomp/patches-v111.txt` or `patches-v143.txt`) passes `python tests\mouse_direct_check.py --cc --deps diff --git a/nx2navi.ini.example b/nx2navi.ini.example index d39a68b3..27eb2e99 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -717,6 +717,11 @@ # as the console does (1, the default). 0 applies the new volume at once, which clicks when a volume moves quickly (a # fade, a sound passing by). Takes effect at the next start. # audio_ramp = 1 +# After the sound was held up (the PC was busy and a gap was heard), skip as much sound as the gap was long, so the +# sound keeps its place against the picture as on the console (1, the default). Gaps of more than 3 seconds are not +# skipped. 0 plays on from where the sound stopped, which leaves it later than the picture by every gap so far. +# Takes effect at the next start. +# audio_catchup = 1 # Volume in percent (0 to 100), on top of the game's own. Changes while the game runs take effect at once. # volume = 100 # Speakers: stereo, 5.1 or auto (5.1 when the Windows speaker setup has six or more channels, stereo otherwise). The diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index 5a0b9ebf..57d4e713 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -14,6 +14,9 @@ // The device's clock paces the render thread, as the audio hardware paces the Switch's DSP (audio_mixer_pace_ns): a // frame is rendered when the ring holds less than the output target, max(prefill, two device periods), so the queue and // the delay from the game to the speakers stay there and the host and device clocks cannot drift apart. +// When the render thread is held up the ring runs out and the device plays silence; the frames that silence stands for +// are then rendered and dropped (audio_mixer_owed, NX2NAVI_AUDIO_CATCHUP), so the sound keeps its place against the +// picture. // Without a device, while the renderer is // stopped and while the device takes nothing (a stall), the render thread keeps real time on its own timer; what piled // up meanwhile is skipped on the SDL thread once the device resumes (more than QUEUE_SLACK_MS above the target plus the @@ -69,6 +72,10 @@ typedef struct Mixer { float ring[RING_FRAMES * OUT_MAX]; _Atomic uint64_t wr, rd, underruns, overruns, missing_frames, callback_frames, callback_nonzero, skipped_frames; _Atomic int playing; int ready; // ready: the callback's own state + int gap; // the callback's own: an underrun's silence has not ended yet + _Atomic uint64_t silent_frames; // silence played in such gaps so far (audio_mixer_owed) + uint64_t made_up; unsigned owed_epoch; // render thread: how much of it is caught up or forgotten; the + // stop_epoch that count belongs to 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 @@ -282,10 +289,12 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { have = wr - rd; m->callback_epoch = epoch; m->ready = 0; + m->gap = 0; } if (!atomic_load_explicit(&m->playing, memory_order_acquire)) { memset(out, 0, (size_t)frames * oc * sizeof(float)); m->ready = 0; + m->gap = 0; atomic_store_explicit(&m->rd, wr, memory_order_release); return; } @@ -297,9 +306,13 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { if (!m->ready) { if (have < prefill) { memset(out, 0, (size_t)frames * oc * sizeof(float)); + // an underrun's silence goes on until the prefill is queued again (audio_mixer_owed); the wait for the + // first prefill after a start is not such a gap + if (m->gap) atomic_fetch_add_explicit(&m->silent_frames, frames, memory_order_release); return; } m->ready = 1; + m->gap = 0; } uint64_t keep = (prefill > (uint64_t)m->target ? prefill : (uint64_t)m->target) + frames; uint64_t slack = (uint64_t)m->rate * QUEUE_SLACK_MS / 1000; @@ -324,7 +337,9 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { memset(out + (size_t)oc * n, 0, (size_t)(frames - n) * oc * sizeof(float)); atomic_fetch_add(&m->underruns, 1); atomic_fetch_add(&m->missing_frames, frames - n); + atomic_fetch_add_explicit(&m->silent_frames, frames - n, memory_order_release); m->ready = 0; + m->gap = 1; } atomic_store_explicit(&m->rd, rd + n, memory_order_release); } @@ -426,6 +441,7 @@ static void channels_switch(Mixer *m, int channels) { atomic_store_explicit(&m->rd, atomic_load(&m->wr), memory_order_release); m->channels = channels; m->ready = 0; + m->gap = 0; m->reported &= ~(1u << 6); // the device sink's line again, with the new output atomic_store_explicit(&m->pulled_ns, 0, memory_order_release); rt_log("audio: the output is now %s", channels == 6 ? "5.1" : "stereo"); @@ -455,6 +471,7 @@ static void device_check(Mixer *m, uint64_t now) { m->device = 0; m->device_name[0] = 0; m->ready = 0; + m->gap = 0; atomic_store_explicit(&m->pulled_ns, 0, memory_order_release); char err[256]; if (stream_open(m, m->rate, SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, err, sizeof err)) { @@ -479,6 +496,36 @@ 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 } +// Catch-up after a stall (NX2NAVI_AUDIO_CATCHUP, audren.c render_thread), for the frames the device paces. While the +// render thread is held up the device goes on taking periods: the ring runs out and ring_pull plays silence, first for +// the rest of that request (the underrun) and then until the prefill is queued again. The device's clock went on by +// that silence and the game's sound did not, and audio_mixer_pace_ns only fills the ring to its target again, so +// everything after the gap would be heard that much later than the picture it belongs to, and every further gap would +// add to it. This says whether a whole render frame of such silence is still owed and takes it off the debt; the +// caller renders that frame with `drop` set (audio_mixer_render), which moves the game's sound on and queues nothing, +// so the ring is filled to the same target afterwards. The debt is counted in the device's own samples, not in host +// time: the two clocks cannot drift into it, and a gap of less than a frame waits for the next one. Whatever was owed +// when the renderer stopped is forgotten, and so is a debt of more than AUDIO_CATCHUP_MAX_MS, with -1 returned: after +// that long the sound goes on from now. +int audio_mixer_owed(AudioRenderer *r) { + Mixer *m = r->mixer; + if (!m || !m->device_ok) return 0; + unsigned epoch = atomic_load_explicit(&m->stop_epoch, memory_order_acquire); + uint64_t silent = atomic_load_explicit(&m->silent_frames, memory_order_acquire); + if (epoch != m->owed_epoch) { + m->owed_epoch = epoch; + m->made_up = silent; + } + uint64_t owed = silent - m->made_up; + if (owed < (uint64_t)m->frame) return 0; + if (owed > (uint64_t)m->rate * AUDIO_CATCHUP_MAX_MS / 1000) { + m->made_up = silent; + return -1; + } + m->made_up += (uint64_t)m->frame; + return 1; +} + int audio_mixer_out_stats(AudioRenderer *r, AudioOutStats *o) { Mixer *m = r->mixer; if (!m || !m->device_ok) return 0; @@ -1078,7 +1125,7 @@ static void mix_circular_sink(AudioRenderer *r,int id) { } uint32_t audio_mixer_sink_written(AudioRenderer *r,int id) {Mixer *m=r->mixer;return m?m->sink_written[id]:0;} -void audio_mixer_render(AudioRenderer *r, double seconds) { +void audio_mixer_render(AudioRenderer *r, double seconds, int drop) { (void)seconds; Mixer *m = r->mixer; if (!m) return; const int frames = m->frame, mc = r->param.submix_count + 1; @@ -1256,6 +1303,10 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { else if (r->sinks[i].type == SINK_CIRCULAR){mix_circular_sink(r,i);circular=1;} } else {m->sink_address[i]=0;m->sink_size[i]=m->sink_offset[i]=m->sink_old[i]=m->sink_written[i]=0;} if (m->channels != output_channels()) channels_switch(m, output_channels()); + // A catch-up frame (audren.c render_thread) ends here. Everything above ran, so the voices' filters and ramps, the + // effects and the sinks the game reads back are where they would be had the frame been heard; the device gets + // nothing of it. + if (drop) return; const int oc = m->channels; float *out=m->output_scratch; memset(out, 0, sizeof(float) * (size_t)oc * (size_t)frames); diff --git a/runtime/audren.c b/runtime/audren.c index ad1c7af9..104c058d 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -48,6 +48,9 @@ typedef struct AudrenStats { // voices (under the lock) uint64_t dry, late[3], late_max; // next buffer late by 1-2 passes, 3-8, 9-AS_LATE_PASSES uint64_t ramp_passes, ramps; // the mixer's volume ramp counts at the window's start (audio_mixer_ramp_stats) + // catch-up (render_thread, under the lock): frames rendered and dropped; gaps over AUDIO_CATCHUP_MAX_MS, which + // are left as they are + uint64_t caught_up, gave_up; int events; uint64_t voices_ns; // the game's update (under the lock): the last voice census line int voices_most; // the most voices in use a census found @@ -408,7 +411,8 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s } // ---- render tick: one frame each time the output device has taken the ring below its target (audio_mixer_pace_ns), -// or every sample_count / sample_rate seconds of the host clock without a device, while stopped or while it stalls +// or every sample_count / sample_rate seconds of the host clock without a device, while stopped or while it stalls; +// after a stall the frames that are owed are rendered and dropped (render_thread, "Catch-up") static void advance_voice(AudioRenderer *r, ServerVoice *v) { if (!v->in.in_use || v->state != SV_STARTED || !v->in.sample_rate) return; uint64_t samples = audio_voice_consumed(r, v, (int)r->param.sample_count); @@ -509,6 +513,7 @@ static void stats_report(AudioRenderer *r, uint64_t now, uint64_t lock_ns, uint6 st->upd_n = 0; // upd_last_ns and upd_pass_last stay: the next gap is measured from the last update st->upd_gap_max_ns = st->upd_gap_frame = st->upd_gaps_over = st->upd_passes_max = st->upd_hold_max_ns = 0; st->dry = st->late[0] = st->late[1] = st->late[2] = st->late_max = 0; + st->caught_up = st->gave_up = 0; } plat_mutex_unlock(&r->lock); if (*event) rt_log("%s", event); @@ -516,14 +521,15 @@ static void stats_report(AudioRenderer *r, uint64_t now, uint64_t lock_ns, uint6 const double rate = r->param.sample_rate; const uint64_t under = o.underruns - snap.out_underruns; const int bad = under || o.overruns != snap.out_overruns || o.skipped_frames != snap.out_skipped || - snap.gaps_over || snap.locks_over || snap.upd_gaps_over || snap.dry; + snap.gaps_over || snap.locks_over || snap.upd_gaps_over || snap.dry || snap.caught_up || + snap.gave_up; if (!bad && snap.windows % 6 != 5) return; rt_log("audio: stats %.0f s to game frame %llu: device underruns %llu (%.1f ms silent), skipped %.1f ms, dropped " "frames %llu, ring at least %.1f ms, longest device request gap %.1f ms; render passes %llu, gaps over %llu " "ms %llu (longest %.1f ms), lock waits over %llu ms %llu (longest %.2f ms); game updates %llu, gaps over " "%llu ms %llu (longest %.1f ms at game frame %llu, %llu passes), longest held the lock %.2f ms; voices ran " - "dry %llu, next buffer late by 1-2 passes %llu, 3-8 %llu, 9-%d %llu (most %llu); render passes that ramped a " - "volume %llu (%llu ramps)", + "dry %llu, next buffer late by 1-2 passes %llu, 3-8 %llu, 9-%d %llu (most %llu); render passes that ramped " + "a volume %llu (%llu ramps); catch-up frames rendered and dropped %llu, gaps over %d ms not caught up %llu", (now - snap.window_ns) / 1e9, (unsigned long long)game_frame(), (unsigned long long)under, (o.missing_frames - snap.out_missing) * 1000.0 / rate, (o.skipped_frames - snap.out_skipped) * 1000.0 / rate, (unsigned long long)(o.overruns - snap.out_overruns), @@ -535,7 +541,8 @@ static void stats_report(AudioRenderer *r, uint64_t now, uint64_t lock_ns, uint6 (unsigned long long)snap.upd_passes_max, snap.upd_hold_max_ns / 1e6, (unsigned long long)snap.dry, (unsigned long long)snap.late[0], (unsigned long long)snap.late[1], AS_LATE_PASSES, (unsigned long long)snap.late[2], (unsigned long long)snap.late_max, - (unsigned long long)(ramp_passes - snap.ramp_passes), (unsigned long long)(ramps - snap.ramps)); + (unsigned long long)(ramp_passes - snap.ramp_passes), (unsigned long long)(ramps - snap.ramps), + (unsigned long long)snap.caught_up, AUDIO_CATCHUP_MAX_MS, (unsigned long long)snap.gave_up); } static void render_thread(void *p) { @@ -555,33 +562,58 @@ static void render_thread(void *p) { #else const uint64_t gap_ns = 0; #endif - uint64_t next = kern_now_ns(), last = 0; + // Catch-up (NX2NAVI_AUDIO_CATCHUP, default 1). The console's audio hardware takes a frame every 5 ms whatever the + // rest of the system does, so the game's sound keeps its place against the picture. Here a frame that is not + // rendered when it is due is time the sound loses for good: the device plays silence meanwhile, rendering goes on + // where it stopped, and everything after the gap is heard that much later than the picture it belongs to. So after + // a stall the frames that are owed are rendered and dropped: mixed in full, which moves the voices, the effects + // and what the game reads back on as if they had been heard, and not queued for the device, whose queue is then + // filled as before. While the device paces, it says what is owed: the silence it played for want of frames + // (audio_mixer_owed), asked before anything is waited for or queued, so the dropped frames come right after the + // gap. On the timer a frame is owed when its deadline passed a whole frame ago. A gap of more than + // AUDIO_CATCHUP_MAX_MS (a debugger, the PC asleep) is not caught up: the sound goes on from now. With the setting + // off nothing is dropped, and the timer queues frames up to 100 ms late back to back and skips a longer gap. + const int catchup = cfg_bool("NX2NAVI_AUDIO_CATCHUP"); + const uint64_t late_max_ns = catchup ? AUDIO_CATCHUP_MAX_MS * 1000000ull : 100000000ull; + uint64_t next = kern_now_ns(), last = 0, gave_up = 0; while (plat_cas32(&r->running, 0, 0)) { uint64_t now = kern_now_ns(); int64_t pace = audio_mixer_pace_ns(r, now); - if (pace > 0) { + int drop = 0; + if (catchup && pace >= 0) { + int owed = audio_mixer_owed(r); + drop = owed > 0; + gave_up += owed < 0; + } + if (pace > 0 && !drop) { plat_wait_ns((uint64_t)pace); continue; } - if (pace == 0 && last && now - last < gap_ns) { + if (pace == 0 && !drop && last && now - last < gap_ns) { plat_wait_ns(gap_ns - (now - last)); continue; } - if (pace == 0) { + if (pace >= 0) { next = now + frame_ns; // where the timer goes on if the device stops pacing } else { if (next > now) plat_wait_ns(next - now); - else if (now - next > 100000000ull) next = now; // fell far behind (debugger, sleep): resynchronise + else if (now - next > late_max_ns) next = now, gave_up += catchup; // fell far behind: resynchronise + else if (catchup && now - next >= frame_ns) drop = 1; next += frame_ns; } const uint64_t prev = last; last = kern_now_ns(); plat_mutex_lock(&r->lock); const uint64_t lock_ns = kern_now_ns() - last; - if (r->stats && r->state == 0) stats_pass(r, last, lock_ns, pace == 0); + if (r->stats && r->state == 0) { + stats_pass(r, last, lock_ns, pace >= 0); + r->stats->caught_up += drop; + r->stats->gave_up += gave_up; + gave_up = 0; + } if (r->state == 0) { voices_prepare(r); - audio_mixer_render(r, frame); + audio_mixer_render(r, frame, drop); for (int i = 0; i < r->param.voice_count; i++) advance_voice(r, &r->voices[i]); r->elapsed_frames++; uint64_t given = r->stop_released + r->empty_played + r->flushed; @@ -695,12 +727,13 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs const char *gp = cfg_get("NX2NAVI_AUDIO_GAME_PRIO"); g_game_prio = gp && *gp ? atoi(gp) : 0; if (g_game_prio < 0 || g_game_prio > 2) g_game_prio = 0; - rt_log("audio: stats %s (NX2NAVI_AUDIO_STATS); the game's audio thread %s (NX2NAVI_AUDIO_GAME_PRIO=%d)", + rt_log("audio: stats %s (NX2NAVI_AUDIO_STATS); the game's audio thread %s (NX2NAVI_AUDIO_GAME_PRIO=%d); frames " + "owed after a stall are %s (NX2NAVI_AUDIO_CATCHUP)", r->stats ? "on" : "off", g_game_prio == 2 ? "runs above normal priority, at the highest while its update is read" : g_game_prio == 1 ? "runs at the highest priority while its update is read" : "keeps its own priority", - g_game_prio); + g_game_prio, cfg_bool("NX2NAVI_AUDIO_CATCHUP") ? "rendered and dropped" : "not caught up"); r->running = 1; audio_mixer_open(r); r->thread = plat_thread_new(render_thread, r, "audren"); diff --git a/runtime/audren.h b/runtime/audren.h index 56abf38c..44b811dd 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -126,6 +126,9 @@ _Static_assert(__builtin_offsetof(AudrenAuxParam, send_info) == 64, "auxiliary e #define AUDREN_WORK_BUFFER_BYTES 0x10000u // the host keeps its state in host memory #define AUDREN_BEHAVIOR_OUT 0xb0u #define AUDREN_RENDER_INFO_OUT 0x10u +// Catch-up after a stall (NX2NAVI_AUDIO_CATCHUP; audren.c render_thread, audio_mixer.c audio_mixer_owed): a gap longer +// than this is not caught up, the sound goes on from now. +#define AUDIO_CATCHUP_MAX_MS 3000 // Server-side voice: the DSP's view of one client voice. typedef struct { @@ -191,11 +194,19 @@ void audio_mixer_update_routing(AudioRenderer *r, const uint8_t *splitters, uint void audio_mixer_update_effects(AudioRenderer *r); void audio_mixer_effect_result(AudioRenderer *r, int id, void *output, unsigned size); uint32_t audio_mixer_sink_written(AudioRenderer *r, int id); -void audio_mixer_render(AudioRenderer *r, double seconds); // after audio_voices_prepare, before voices advance +// After audio_voices_prepare, before voices advance. `drop`: a catch-up frame (audren.c render_thread). It is mixed in +// full, so the voices' filters, the effects and the sinks the game reads back advance as for any frame, and nothing +// of it is queued for the device. +void audio_mixer_render(AudioRenderer *r, double seconds, int drop); // Device pacing for the render thread at kern_now_ns `now`: 0 renders the next frame now, a positive value waits that // many ns (the ring is at its target), -1 leaves the pace to the render thread's timer (no device, renderer stopped, // device stalled). int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now); +// Catch-up for the frames the device paces (render thread, lock not held). 1: the device has played at least one render +// frame of silence for want of rendered sound that is not made up for yet; that frame is taken off what is owed and +// the caller renders one frame with `drop` set. 0: nothing is owed. -1: more than AUDIO_CATCHUP_MAX_MS was owed and is +// forgotten. +int audio_mixer_owed(AudioRenderer *r); // NX2NAVI_AUDIO_STATS: the output's counters since it opened, and the longest gap between two device requests since // the last call (which starts that measure again). False without a device. typedef struct { uint64_t underruns, missing_frames, skipped_frames, overruns, queued_frames, pull_gap_max_ns; } AudioOutStats; diff --git a/runtime/cfg.c b/runtime/cfg.c index c88680aa..2e0d3774 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -35,7 +35,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) - "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", + "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CATCHUP", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", @@ -121,7 +121,7 @@ static const struct { const char *key, *section; } sections[] = { {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"}, {"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"}, {"AUDIO_DEVICE", "audio"}, {"MICROPHONE", "audio"}, {"MENU_SOUND_VOLUME", "audio"}, - {"AUDIO_RAMP", "audio"}, + {"AUDIO_RAMP", "audio"}, {"AUDIO_CATCHUP", "audio"}, {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, {"DS3", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, {"DEBUG_MENU", "input"}, @@ -233,6 +233,7 @@ static const CfgMeta metas[] = { {"AUDIO_DEVICE", "default", NULL, RESTART}, // default|<1-based index>| (audio_mixer.c resolve_audio_device) {"MICROPHONE", "1", "0|1", RESTART}, // ov_audio_input.c mic_on: 0 refuses the game's input streams {"AUDIO_RAMP", "1", "0|1", RESTART}, // audio_mixer.c accumulate_ramp: a changed volume is reached across the frame + {"AUDIO_CATCHUP", "1", "0|1", RESTART}, // audren.c render_thread: frames owed after a stall are rendered and dropped {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "nintendo", "auto|nintendo|xbox|playstation", LIVE}, {"DS3", "0", "0|1", LIVE}, // input_ds3_win.c ds3_read: read a DualShock 3 directly; off: no HID scan diff --git a/tests/audio_mixer_check.c b/tests/audio_mixer_check.c index 040f119e..1500cf10 100644 --- a/tests/audio_mixer_check.c +++ b/tests/audio_mixer_check.c @@ -1,5 +1,6 @@ -/* The mixer's volume ramps (runtime/audio_mixer.c accumulate_ramp, audio_mixer_render), with the rest of the runtime - stubbed. No game data and no audio device: NX2NAVI_AUDIO=none mixes without opening one. Each voice plays a wave +/* The mixer's volume ramps (runtime/audio_mixer.c accumulate_ramp, audio_mixer_render) and what it owes the device + after a stall (ring_pull, audio_mixer_owed), with the rest of the runtime stubbed. No game data and no audio device: + NX2NAVI_AUDIO=none mixes without opening one, and the device's requests are made by hand. Each voice plays a wave buffer of one constant sample, so a mix buffer holds the gain the voice went in with, sample by sample. */ #include "../runtime/audio_mixer.c" @@ -87,11 +88,13 @@ static void renderer_close(AudioRenderer *r) { free(r->voices), free(r->mixes), free(r->sinks), free(r->effects), free(r->channels), free(r); } -/* One render pass, counted as audren.c render_thread counts it. The voices are not advanced: every sample is the same. */ -static void pass(AudioRenderer *r) { - audio_mixer_render(r, (double)FRAME / 48000); +/* One render pass, counted as audren.c render_thread counts it; `drop`: a catch-up frame. The voices are not + advanced: every sample is the same. */ +static void render(AudioRenderer *r, int drop) { + audio_mixer_render(r, (double)FRAME / 48000, drop); r->elapsed_frames++; } +static void pass(AudioRenderer *r) { render(r, 0); } /* A voice as the game's update leaves a new one (audren.c update_voice with is_new: voice_reset, then started). Of what the slot's last voice was mixed with, voice_reset clears only ramp_pass, so the rest is kept here too. */ @@ -283,11 +286,82 @@ static void ramps_off(void) { ramp_setting = 1; } +/* What the device paces: the device's requests (ring_pull, the SDL callback's part) and the render thread's question + (audio_mixer_owed) by hand, on a mixer that takes itself to have a device. */ +static void catch_up(void) { + AudioRenderer *r = renderer_open(); + Mixer *m = r->mixer; + enum { PERIOD = 2 * FRAME }; + static float out[2 * (PERIOD + FRAME)]; + const int prefill = m->prefill; /* render frames the output waits for; even on every platform */ + m->device_ok = 1, m->period = PERIOD, m->target = prefill * FRAME; + r->channels[0].mix[0] = 1, r->channels[0].mix[1] = 1; + ServerVoice *v = voice_new(r, 0, 1, 0, SPLITTER_UNUSED); + + ring_pull(m, out, PERIOD); + ring_pull(m, out, PERIOD); + CHECK(out[0] == 0 && audio_mixer_owed(r) == 0, "the silence before the first prefill is not owed"); + for (int i = 0; i < prefill; i++) pass(r); + uint64_t queued = atomic_load(&m->wr) - atomic_load(&m->rd); + CHECK(queued == (uint64_t)prefill * FRAME, "the frames are queued for the device (%llu samples)", (unsigned long long)queued); + for (int i = 0; i < prefill; i += 2) { + ring_pull(m, out, PERIOD); + CHECK(out[0] == 0.5f && out[2 * PERIOD - 1] == 0.5f, "the device plays what was queued"); + } + CHECK(audio_mixer_owed(r) == 0, "nothing is owed while the ring keeps up"); + + /* the render thread is held up for three device periods: an underrun, then silence until the prefill is back */ + for (int i = 0; i < 3; i++) ring_pull(m, out, PERIOD); + CHECK(out[0] == 0 && atomic_load(&m->underruns) == 1, "the device plays silence"); + v->in.volume = 0.5f; /* the game's update that came meanwhile */ + for (int i = 0; i < 6; i++) { + uint64_t wr = atomic_load(&m->wr), frames = r->elapsed_frames; + CHECK(audio_mixer_owed(r) == 1, "three periods of silence: render frame %d of 6 is owed", i + 1); + render(r, 1); + CHECK(atomic_load(&m->wr) == wr && r->elapsed_frames == frames + 1, "a catch-up frame is rendered and not queued"); + if (i == 0) CHECK(ramped(buf(r, 0), 0.5f, 0.25f), "it is mixed in full: the volume the game set ramps in it"); + } + CHECK(audio_mixer_owed(r) == 0, "after six frames nothing more is owed"); + for (int i = 0; i < prefill; i++) pass(r); + queued = atomic_load(&m->wr) - atomic_load(&m->rd); + CHECK(queued == (uint64_t)prefill * FRAME, "the ring is filled to the same target afterwards"); + ring_pull(m, out, PERIOD); + CHECK(out[0] == 0.25f && out[2 * PERIOD - 1] == 0.25f, "the device goes on with the frames after the dropped ones"); + CHECK(audio_mixer_owed(r) == 0, "the device's requests while the ring keeps up add nothing"); + + /* less than a render frame of silence waits for the next gap */ + for (int i = 2; i < prefill; i += 2) ring_pull(m, out, PERIOD); /* the ring is empty again, */ + for (int i = 0; i < prefill; i++) pass(r); /* and filled */ + for (int i = 2; i < prefill; i += 2) ring_pull(m, out, PERIOD); /* down to one period */ + ring_pull(m, out, PERIOD + 100); /* a request for 100 samples more than that */ + CHECK(atomic_load(&m->underruns) == 2 && audio_mixer_owed(r) == 0, "100 samples of silence are not a frame yet"); + ring_pull(m, out, 200); /* still waiting for the prefill: 300 in all */ + CHECK(audio_mixer_owed(r) == 1 && audio_mixer_owed(r) == 0, "300 samples of silence: one frame is owed, 60 samples stay"); + + /* a stop forgets what was owed, and the wait for the prefill after the start is not owed */ + for (int i = 0; i < 3; i++) ring_pull(m, out, PERIOD); + audio_mixer_set_running(r, 0); + ring_pull(m, out, PERIOD); + audio_mixer_set_running(r, 1); + ring_pull(m, out, PERIOD); + ring_pull(m, out, PERIOD); + CHECK(audio_mixer_owed(r) == 0, "nothing is owed across a stop"); + + /* more than AUDIO_CATCHUP_MAX_MS of silence is not caught up */ + for (int i = 0; i < prefill; i++) pass(r); + for (int i = 0; i < prefill / 2 + 1 + 48000 / PERIOD * (AUDIO_CATCHUP_MAX_MS + 100) / 1000; i++) ring_pull(m, out, PERIOD); + CHECK(audio_mixer_owed(r) == -1, "over %d ms of silence is given up", AUDIO_CATCHUP_MAX_MS); + CHECK(audio_mixer_owed(r) == 0, "and nothing of it is owed afterwards"); + m->device_ok = 0; + renderer_close(r); +} + int main(void) { voice_into_mix(); voice_into_splitter(); mix_into_splitter(); ramps_off(); + catch_up(); printf("audio_mixer_check: %s\n", fails ? "FAIL" : "ok"); return fails ? 1 : 0; }