diff --git a/docs/architecture.md b/docs/architecture.md index 336fcfc9..3adc67f5 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -34,6 +34,10 @@ from other projects, with its license, is listed in [THIRD_PARTY_NOTICES.md](../ - The SDK boundary is at the `nn::*` function level: threads and synchronisation, IPC, memory, fs/RomFS, time, account, applet, hid, audio renderer and effects run as host code. Only `audren:u` and `sm:` are real IPC services. + What the game sends the audio renderer never ends the process (`audren.c` `audio_bad`): an update whose sections do + not match the renderer is refused whole with 0x5299, which the SDK hands back to the game, and a record, effect or + sink whose values the renderer cannot use is left out, passed through or skipped, with one log line per kind. + `NX2NAVI_AUDIO_STRICT=1` ends the process there instead, for bug reports. - Window, keyboard and mouse: Win32 (`gfx_window_win.c`, `input_win.c`). Gamepads and audio output: SDL3 (every connected gamepad is open; the game reads the one pressed last, `input.c`). Voice: libopus. Movies: a host VP9 decoder over libvpx replaces the SDK's `movie::Decoder` (`runtime/ov_movie.c`). diff --git a/docs/building.md b/docs/building.md index e85d7c3e..3c50db2c 100644 --- a/docs/building.md +++ b/docs/building.md @@ -517,6 +517,23 @@ not queued; the silence before the first prefill and across a stop is not owed, 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 what the audio renderer does with values the game sends (`runtime/audren.c` `update_check`, +`request_update` and `audio_bad`; `runtime/audio_mixer.c` `splitters_walk`, `audio_mixer_routing_check`, +`audio_mixer_mix_layout_check`, `audio_mixer_update_effects` and the render-pass effects; `runtime/audio_effects.c` +`audio_effect_invalid`; `runtime/audio_reverb.c` `audio_reverb_invalid`) passes `python tests\audio_params_check.py`. +It builds `audren.c` and `audio_mixer.c` with the rest of the runtime stubbed and the real effect DSPs linked, and +sends hand-built RequestUpdates through `renderer_handler`. An update whose sections do not match the renderer (sizes, +magic words, overruns, the output buffer, a mix layout with a negative count or more buffers than the renderer has, a +splitter id run whose length wraps in 32 bits) answers 0x5299, the one result the SDK hands back to the game instead +of aborting, and writes no output, changes no renderer state and places no C buffers. A channel, voice or splitter +record with an id the renderer does not have, a wave buffer index of 4, a filter that is not a finite number and a +changed mix with a foreign id are left out of an update that is otherwise applied; a splitter destination id the +renderer does not have is never read (a guard entry before the destination table would make it heard). Each effect and +sink whose values the renderer cannot use is passed through or skipped in every render pass, with nothing written to +the game's rings or sink buffer, and runs again once its values are usable. Each kind is logged once and counted every +time; valid effects of every type over 20 passes leave nothing out. With `NX2NAVI_AUDIO_STRICT=1` such a value ends +the process, in two runs of their own. No game files and no audio device. + A change to the sprint button (`runtime/sprint.c`, the set `sprint` of `recomp/patches-v111.txt`, its call in `runtime/ov_input.c` `npad_states`) passes `python tests\sprint_check.py`, which builds `sprint.c` with the rest of the runtime stubbed and plays frames against a stand-in Link: toggle and hold on each kind of button, the states that keep diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 4f50d304..c20a0c81 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -869,6 +869,10 @@ # 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 +# When the game sends an audio value this version cannot use (an effect setting out of range, an id it does not have), +# that effect or sound is left out and the log gets one line per kind (0, the default). 1 ends the game there instead, +# with the details in the log, as versions before 0.10.2 did: for bug reports. Takes effect at the next start. +# audio_strict = 0 # 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_effects.c b/runtime/audio_effects.c index 53780a1c..37b6e77f 100644 --- a/runtime/audio_effects.c +++ b/runtime/audio_effects.c @@ -62,20 +62,55 @@ static const float early_gain[5][10]={ {0} }; static const float pre_time[5]={12.5f,40,50,50,0}; -static void validate_delay(const AudrenDelayParam *p) { +/* What makes a delay, reverb or compressor block unusable for the DSP below, or NULL. The game sends these blocks, so + audio_mixer.c audio_mixer_update_effects asks audio_effect_invalid before it creates or updates a DSP and passes + the effect's sound through instead; the validate_* checks in create and update are then invariants. */ +static const char *delay_invalid(const AudrenDelayParam *p) { unsigned rate=(p->sample_rate>>14)/1000; - if(!channel_ok(p->channels) || p->channel_maxchannels || rate<1 || rate>192 || p->delay_max>20000 || p->delay>p->delay_max) - rt_fatal("audio delay parameters"); + if(!channel_ok(p->channels))return "channel count not 1, 2, 4 or 6"; + if(p->channel_maxchannels)return "more channels than its channel_max"; + if(rate<1 || rate>192)return "sample rate outside 1 to 192 kHz"; + if(p->delay_max>20000)return "longest delay above 20000"; + if(p->delay>p->delay_max)return "delay above its longest delay"; + return NULL; +} +static const char *reverb_invalid(const AudrenReverbParam *p) { + if(!channel_ok(p->channels))return "channel count not 1, 2, 4 or 6"; + if(p->channel_maxchannels)return "more channels than its channel_max"; + if(p->sample_rate<16384 || p->sample_rate>192*16384)return "sample rate outside 1 to 192 kHz"; + if(p->early_mode<0 || p->early_mode>4)return "early mode outside 0 to 4"; + if(p->late_mode<0 || p->late_mode>3)return "late mode outside 0 to 3"; + if(p->decay<=0 || p->hf_decay<=0)return "decay or high-frequency decay not above 0"; + if(p->pre_delay<0)return "pre-delay below 0"; + return NULL; +} +static const char *compressor_invalid(const AudrenCompressorParam *p) { + if(!channel_ok(p->channels))return "channel count not 1, 2, 4 or 6"; + if(p->channel_maxchannels)return "more channels than its channel_max"; + if(!isfinite(p->threshold))return "threshold not finite"; + if(!(p->ratio>0) || !isfinite(p->ratio))return "ratio not above 0 or not finite"; + if(!(p->input_gain>=0 && p->input_gain<=1) || !(p->attack>=0 && p->attack<=1) || !(p->release>=0 && p->release<=1)) + return "input gain, attack or release outside 0 to 1"; + if(!isfinite(p->output_gain))return "output gain not finite"; + return NULL; +} +const char *audio_effect_invalid(unsigned type,const void *param) { + if(type==3){AudrenDelayParam p;memcpy(&p,param,sizeof p);return delay_invalid(&p);} + if(type==4){AudrenReverbParam p;memcpy(&p,param,sizeof p);return reverb_invalid(&p);} + if(type==9){AudrenCompressorParam p;memcpy(&p,param,sizeof p);return compressor_invalid(&p);} + return NULL; +} +static void validate_delay(const AudrenDelayParam *p) { + const char *why=delay_invalid(p); + if(why)rt_fatal("audio delay parameters: %s",why); } static void validate_reverb(const AudrenReverbParam *p) { - if(!channel_ok(p->channels) || p->channel_maxchannels || p->sample_rate<16384 || p->sample_rate>192*16384 || - p->early_mode<0 || p->early_mode>4 || p->late_mode<0 || p->late_mode>3 || p->decay<=0 || p->hf_decay<=0 || p->pre_delay<0) - rt_fatal("audio standard reverb parameters"); + const char *why=reverb_invalid(p); + if(why)rt_fatal("audio standard reverb parameters: %s",why); } static void validate_compressor(const AudrenCompressorParam *p) { - if(!channel_ok(p->channels) || p->channel_maxchannels || !isfinite(p->threshold) || !(p->ratio>0) || !isfinite(p->ratio) || - !(p->input_gain>=0 && p->input_gain<=1) || !(p->attack>=0 && p->attack<=1) || !(p->release>=0 && p->release<=1) || !isfinite(p->output_gain)) - rt_fatal("audio compressor parameters"); + const char *why=compressor_invalid(p); + if(why)rt_fatal("audio compressor parameters: %s",why); } AudioEffectDsp *audio_effect_create(unsigned type,const void *param,unsigned revision) { AudioEffectDsp *s=calloc(1,sizeof *s);if(!s)rt_fatal("audio effect allocation"); @@ -96,6 +131,9 @@ AudioEffectDsp *audio_effect_create(unsigned type,const void *param,unsigned rev return s; } void audio_effect_free(AudioEffectDsp *s) { if(s){for(int i=0;i<10;i++)free(s->line[i].data);free(s);} } +/* The "requires reset" checks are invariants too: audio_mixer_update_effects frees the DSP whenever the channels, the + rate or the longest delay differ from the block of the update before, which is the one this DSP was made or last + updated from. */ void audio_effect_update(AudioEffectDsp *s,const void *param) { if(s->type==3){ AudrenDelayParam p;memcpy(&p,param,sizeof p);validate_delay(&p); diff --git a/runtime/audio_effects.h b/runtime/audio_effects.h index 5ffc61fa..1efeee35 100644 --- a/runtime/audio_effects.h +++ b/runtime/audio_effects.h @@ -29,6 +29,10 @@ _Static_assert(offsetof(AudrenCompressorParam,status)==52,"compressor wire layou _Static_assert(sizeof(AudrenBufferMixParam)==148,"buffer-mix wire layout"); _Static_assert(sizeof(AudrenBiquadEffectV1)==24 && sizeof(AudrenBiquadEffectV2)==40,"biquad wire layouts"); typedef struct AudioEffectDsp AudioEffectDsp; +/* Why the game's parameter block `param` of a delay (3), reverb (4) or compressor (9) cannot be used, as a constant + string, or NULL when it can (and for any other type). create and update end the process on such a block, so the + caller asks first. */ +const char *audio_effect_invalid(unsigned type, const void *param); AudioEffectDsp *audio_effect_create(unsigned type, const void *param, unsigned revision); void audio_effect_free(AudioEffectDsp *s); void audio_effect_update(AudioEffectDsp *s, const void *param); diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index a5e604ed..cef0ce97 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -59,6 +59,7 @@ typedef struct { float *delay, detector[6], compression[6], peak[6], minimum[6]; int position[6], delay_max, channels; AudioEffectDsp *dsp; unsigned type; uint8_t wire[160]; float biquad_z[6][2], numerator[3], denominator[2]; uint8_t biquad_channels; + uint8_t bad; // the game's parameters for it cannot be used: passed through (audio_mixer_update_effects) } EffectState; typedef struct Mixer { float *buf; int nbuf, frame; @@ -118,6 +119,11 @@ static void report_once(Mixer *m, unsigned bit, const char *msg, int value) { m->reported |= 1u << bit; rt_log("audio: %s (%d)", msg, value); } +// A guest range ipc.c ipc_guest takes (it ends the process on any other): not 0, not wrapping, below the guest address +// limit. It does not prove the memory is mapped. +static int guest_range_ok(uint64_t addr, uint64_t size) { + return addr && addr + size >= addr && addr + size <= RT_ADDRESS_LIMIT; +} // ---- output: SDL3 audio stream fed from the ring // How much audio the output waits for before it starts, and again after an underrun: NX2NAVI_AUDIO_PREFILL_MS rounded up @@ -701,73 +707,236 @@ void audio_mixer_close(AudioRenderer *r) { } /* ---------- routing input: splitters and mixes ---------- */ -static void splitters_update(AudioRenderer *r, const uint8_t *p, uint32_t n) { +#define SNDH 0x48444e53u +#define SNDI 0x49444e53u +#define SNDD 0x44444e53u +// The splitter section of an update: the SNDH header with the record counts, one SNDI record per splitter the update +// changes with its destination ids and 12 reserved bytes (measured, REV11), then one SNDD record per destination it +// changes (0x70, 0x90 or 0xa8 bytes by revision); the section is padded to 16 bytes, and the game leaves the padding as +// it is (measured). One walk serves both passes of an update, so both read the same bytes. With `apply` 0 +// (audio_mixer_routing_check: before anything of the update is applied, without the lock) it checks the framing only +// and returns AUDREN_R_INVALID_UPDATE on a fault, which refuses the update. With `apply` 1 (audio_mixer_update_routing, +// lock held) it stores the records: a record that names a splitter or destination the renderer does not have is left +// out, a destination id in a splitter's list that the renderer does not have is stored as -1, which both splitter loops +// of audio_mixer_render skip, and a destination filter with a coefficient that is not a finite number is off (audio_bad +// each). A framing fault while applying (the game's input changed between the two walks) stops the walk there. +// Offsets are 64-bit, so a huge id count cannot wrap a bounds test. +static uint32_t splitters_walk(AudioRenderer *r, uint32_t revision, const uint8_t *p, uint32_t n, int apply) { Mixer *m = r->mixer; - if (!n) return; + const uint32_t nsplit = (uint32_t)r->param.splitter_count, ndest = (uint32_t)r->param.splitter_destination_count; + if (!n) return 0; + if (n < 0x20) + return AUDIO_REFUSE(r, AB_SPL_HEADER, "the splitter section is %u bytes, under its 32-byte header", n); uint32_t hdr[3]; - if (n < 0x20) rt_fatal("audio splitter section header"); memcpy(hdr, p, 12); - if (hdr[0] != 0x48444e53u) rt_fatal("audio splitter header magic (SNDH)"); - uint32_t ni = hdr[1], nd = hdr[2], at = 0x20; + if (hdr[0] != SNDH) return AUDIO_REFUSE(r, AB_SPL_MAGIC, "the splitter section starts with %08x, not SNDH", hdr[0]); + const uint32_t ni = hdr[1], nd = hdr[2]; + const unsigned rev = (revision >> 24) - '0', stride = rev >= 15 ? 0xa8 : rev >= 12 ? 0x90 : 0x70; + uint64_t at = 0x20; for (uint32_t i = 0; i < ni; i++) { - uint32_t h[4]; if (at + 16 > n) rt_fatal("audio splitter info overruns section"); - memcpy(h, p + at, 16); at += 16; - if (h[0] != 0x49444e53u || h[1] >= (uint32_t)m->nsplit || h[3] > (uint32_t)m->ndest || at + 4u * h[3] > n) { - rt_log("audio: splitter info %u at %#x: %08x %08x %08x %08x (section %u bytes, infos %u, destinations %u)", i, at - 16, h[0], h[1], h[2], h[3], n, ni, nd); - rt_fatal("audio splitter info (SNDI)"); + uint32_t h[4]; + if (at + 16 > n) + return AUDIO_REFUSE(r, AB_SPL_INFO_OVERRUN, + "splitter info %u of %u at %#llx overruns the %u-byte splitter section", i, ni, + (unsigned long long)at, n); + memcpy(h, p + at, 16); + at += 16; + if (h[0] != SNDI || at + 4ull * h[3] > n) + return AUDIO_REFUSE(r, AB_SPL_INFO, + "splitter info %u at %#llx: %08x %08x %08x %08x (section %u bytes, infos %u, " + "destinations %u)", + i, (unsigned long long)(at - 16), h[0], h[1], h[2], h[3], n, ni, nd); + if (at + 4ull * h[3] + 12 > n) + return AUDIO_REFUSE(r, AB_SPL_PADDING, + "the reserved bytes after splitter info %u's %u ids overrun the %u-byte splitter " + "section", i, h[3], n); + if (apply && (h[1] >= nsplit || h[3] > ndest)) { + audio_bad(r, AB_SPL_ID, + "splitter info %u names splitter %u of %u with %u destinations of %u: that record is left out", + i, h[1], nsplit, h[3], ndest); + } else if (apply) { + int32_t *ids = calloc(h[3] ? h[3] : 1, sizeof(int32_t)); + if (!ids) rt_fatal("audio splitter destinations"); + memcpy(ids, p + at, 4u * h[3]); + for (uint32_t k = 0; k < h[3]; k++) + if (ids[k] < 0 || (uint32_t)ids[k] >= ndest) { + audio_bad(r, AB_SPL_DEST_REF, "splitter %u's destination %u is %d of %u: that destination is " + "left out", h[1], k, ids[k], ndest); + ids[k] = -1; + } + SplitInfo *s = &m->split[h[1]]; + free(s->ids); + s->ndest = (int32_t)h[3]; + s->ids = ids; } - SplitInfo *s = &m->split[h[1]]; free(s->ids); - s->ndest = (int32_t)h[3]; s->ids = calloc(h[3] ? h[3] : 1, sizeof(int32_t)); - if (!s->ids) rt_fatal("audio splitter destinations"); - memcpy(s->ids, p + at, 4u * h[3]); at += 4u * h[3] + 12; /* 12 reserved bytes follow the ids (measured, NX2NAVI REV11) */ - if (at > n) rt_fatal("audio splitter info padding overruns section"); - for (uint32_t k = 0; k < h[3]; k++) if (s->ids[k] < 0 || s->ids[k] >= m->ndest) rt_fatal("audio splitter destination id"); + at += 4ull * h[3] + 12; } for (uint32_t i = 0; i < nd; i++) { - unsigned rev=(r->revision>>24)-'0',stride=rev>=15?0xa8:rev>=12?0x90:0x70; - uint8_t e[0xa8]={0}; if (at>n||stride>n-at) rt_fatal("audio splitter destination overruns section"); - memcpy(e, p + at, stride); at += stride; - uint32_t magic, id; int32_t dst; memcpy(&magic, e, 4); memcpy(&id, e + 4, 4); memcpy(&dst, e + 0x68, 4); - if (magic != 0x44444e53u || id >= (uint32_t)m->ndest) rt_fatal("audio splitter destination (SNDD)"); - SplitDest *d = &m->dest[id]; memcpy(d->vol, e + 8, sizeof d->vol); d->dest = dst; - d->used=e[rev>=15?0x9c:rev>=12?0x84:0x6c]; - for(unsigned f=0;f<2;f++){ - AudrenBiquadFloat b={0}; - if(rev>=15)memcpy(&b,e+0x6c+f*sizeof b,sizeof b); - else if(rev>=12){AudrenBiquad q;memcpy(&q,e+0x6c+f*sizeof q,sizeof q);b.enable=q.enable;for(unsigned c=0;c<3;c++)b.numerator[c]=q.numerator[c]/16384.0f;for(unsigned c=0;c<2;c++)b.denominator[c]=q.denominator[c]/16384.0f;} - if(b.enable){for(unsigned c=0;c<3;c++)if(!isfinite(b.numerator[c]))rt_fatal("audio splitter nonfinite numerator");for(unsigned c=0;c<2;c++)if(!isfinite(b.denominator[c]))rt_fatal("audio splitter nonfinite denominator");} - if(!b.enable||!d->biquads[f].enable||!d->used)memset(d->state[f],0,sizeof d->state[f]); - d->biquads[f]=b; + uint8_t e[0xa8] = {0}; + if (at > n || stride > n - at) + return AUDIO_REFUSE(r, AB_DEST_OVERRUN, + "splitter destination %u of %u at %#llx overruns the %u-byte splitter section " + "(records of %#x bytes)", i, nd, (unsigned long long)at, n, stride); + memcpy(e, p + at, stride); + at += stride; + uint32_t magic, id; + int32_t dst; + memcpy(&magic, e, 4); + memcpy(&id, e + 4, 4); + memcpy(&dst, e + 0x68, 4); + if (magic != SNDD) + return AUDIO_REFUSE(r, AB_DEST_MAGIC, "splitter destination %u at %#llx starts with %08x, not SNDD", i, + (unsigned long long)(at - stride), magic); + if (!apply) continue; + if (id >= ndest) { + audio_bad(r, AB_DEST_ID, "splitter destination record %u names destination %u of %u: that record is left " + "out", i, id, ndest); + continue; + } + SplitDest *d = &m->dest[id]; + memcpy(d->vol, e + 8, sizeof d->vol); + d->dest = dst; + d->used = e[rev >= 15 ? 0x9c : rev >= 12 ? 0x84 : 0x6c]; + for (unsigned f = 0; f < 2; f++) { + AudrenBiquadFloat b = {0}; + if (rev >= 15) { + memcpy(&b, e + 0x6c + f * sizeof b, sizeof b); + } else if (rev >= 12) { // Q14 filters + AudrenBiquad q; + memcpy(&q, e + 0x6c + f * sizeof q, sizeof q); + b.enable = q.enable; + for (unsigned c = 0; c < 3; c++) b.numerator[c] = q.numerator[c] / 16384.0f; + for (unsigned c = 0; c < 2; c++) b.denominator[c] = q.denominator[c] / 16384.0f; + } + if (b.enable && !(isfinite(b.numerator[0]) && isfinite(b.numerator[1]) && isfinite(b.numerator[2]) && + isfinite(b.denominator[0]) && isfinite(b.denominator[1]))) { + audio_bad(r, AB_DEST_FILTER, "splitter destination %u's filter %u has a coefficient that is not a " + "finite number (%g %g %g / %g %g): that filter is off", id, f, b.numerator[0], + b.numerator[1], b.numerator[2], b.denominator[0], b.denominator[1]); + b.enable = 0; // its state is cleared below, as for a filter the game turned off + } + if (!b.enable || !d->biquads[f].enable || !d->used) memset(d->state[f], 0, sizeof d->state[f]); + d->biquads[f] = b; } } - if (at <= n && n - at < 16) at = n; /* the section is padded to 16 bytes; the padding is not cleared by the game (measured) */ - if (at != n) { rt_log("audio: splitter section %u bytes, parsed %u (infos %u, destinations %u)", n, at, ni, nd); rt_fatal("audio splitter section size"); } + if (at <= n && n - at < 16) at = n; // the padding to 16 bytes, which the game does not clear (measured) + if (at != n) + return AUDIO_REFUSE(r, AB_SPL_SIZE, + "the splitter section is %u bytes and its records end at %#llx (infos %u, destinations %u)", + n, (unsigned long long)at, ni, nd); + return 0; } -void audio_mixer_update_routing(AudioRenderer *r, const uint8_t *splitters, uint32_t splitter_bytes, const uint8_t *mixes, uint32_t mix_bytes, int mix_count) { - splitters_update(r, splitters, splitter_bytes); - if (mix_bytes == (uint32_t)mix_count * sizeof(AudrenMixIn)) { memcpy(r->mixes, mixes, mix_bytes); return; } +uint32_t audio_mixer_routing_check(AudioRenderer *r, uint32_t revision, const uint8_t *splitters, + uint32_t splitter_bytes, const uint8_t *mixes, uint32_t mix_bytes, int mix_count) { + uint32_t refused = splitters_walk(r, revision, splitters, splitter_bytes, 0); + if (refused) return refused; + if (mix_bytes == (uint32_t)mix_count * sizeof(AudrenMixIn)) return 0; /* MixInParameterDirtyOnly: 0x20-byte header {u32 magic 0xCAFEBABE, u32 count, pad} followed by the dirty mixes */ if (mix_bytes >= 0x20 && (mix_bytes - 0x20) % sizeof(AudrenMixIn) == 0) { - uint32_t magic, n; memcpy(&magic, mixes, 4); memcpy(&n, mixes + 4, 4); - if (magic != 0xcafebabeu || n * sizeof(AudrenMixIn) != mix_bytes - 0x20) rt_fatal("audio dirty mix header"); - for (uint32_t i = 0; i < n; i++) { - AudrenMixIn m; memcpy(&m, mixes + 0x20 + i * sizeof m, sizeof m); - if (m.mix_id < 0 || m.mix_id >= mix_count) rt_fatal("audio mix id"); - r->mixes[m.mix_id] = m; - } + uint32_t magic, count; + memcpy(&magic, mixes, 4); + memcpy(&count, mixes + 4, 4); + if (magic != 0xcafebabeu || count * sizeof(AudrenMixIn) != mix_bytes - 0x20) + return AUDIO_REFUSE(r, AB_MIX_DIRTY, "the changed-mix header reads %08x and %u mixes for %u bytes of mixes", + magic, count, mix_bytes - 0x20); + return 0; + } + return AUDIO_REFUSE(r, AB_MIX_SIZE, "the mix section is %u bytes for %d mixes of %#x bytes", mix_bytes, mix_count, + (unsigned)sizeof(AudrenMixIn)); +} +// The mixes' buffers as audio_mixer_render lays them out: one after another, for the mixes in use, from buffer 0. A +// negative count of a mix in use would start the mixes after it before buffer 0, and more buffers than the renderer +// has would run past them, so such an update is refused before anything of it is applied; the render's layout check is +// then an invariant. The update's records are applied as audio_mixer_update_routing applies them (a changed-mix record +// with an id the renderer does not have is left out). +uint32_t audio_mixer_mix_layout_check(AudioRenderer *r, const uint8_t *mixes, uint32_t mix_bytes, int mix_count) { + int32_t buffers[256]; + uint8_t used[256]; + if (mix_count > 256) mix_count = 256; // audren.c renderer_new keeps submix_count below 256 + for (int i = 0; i < mix_count; i++) buffers[i] = r->mixes[i].buffer_count, used[i] = r->mixes[i].in_use; + const int full = mix_bytes == (uint32_t)mix_count * sizeof(AudrenMixIn); + const uint32_t records = + full ? (uint32_t)mix_count : mix_bytes >= 0x20 ? (mix_bytes - 0x20) / sizeof(AudrenMixIn) : 0; + const uint8_t *x = full ? mixes : mixes + 0x20; + for (uint32_t i = 0; i < records; i++, x += sizeof(AudrenMixIn)) { + int32_t id = (int32_t)i; + if (!full) memcpy(&id, x + offsetof(AudrenMixIn, mix_id), 4); + if (id < 0 || id >= mix_count) continue; + memcpy(&buffers[id], x + offsetof(AudrenMixIn, buffer_count), 4); + used[id] = x[offsetof(AudrenMixIn, in_use)]; + } + int64_t need = 0; + for (int i = 0; i < mix_count; i++) { + if (!used[i]) continue; + if (buffers[i] < 0) + return AUDIO_REFUSE(r, AB_MIX_LAYOUT, "mix %d is in use with %d mix buffers", i, buffers[i]); + need += buffers[i]; + } + if (need > r->param.mix_buffer_count) + return AUDIO_REFUSE(r, AB_MIX_LAYOUT, "the mixes in use need %lld mix buffers, the renderer has %d", + (long long)need, r->param.mix_buffer_count); + return 0; +} +void audio_mixer_update_routing(AudioRenderer *r, const uint8_t *splitters, uint32_t splitter_bytes, + const uint8_t *mixes, uint32_t mix_bytes, int mix_count) { + splitters_walk(r, r->revision, splitters, splitter_bytes, 1); + if (mix_bytes == (uint32_t)mix_count * sizeof(AudrenMixIn)) { + memcpy(r->mixes, mixes, mix_bytes); return; } - rt_log("audio: mix section %u bytes for %d mixes", mix_bytes, mix_count); - rt_fatal("audio mix section size"); + if (mix_bytes < 0x20) return; // refused by audio_mixer_routing_check + /* the changed mixes only: audio_mixer_routing_check has checked the header against the section's size */ + const uint32_t n = (mix_bytes - 0x20) / sizeof(AudrenMixIn); + for (uint32_t i = 0; i < n; i++) { + AudrenMixIn x; + memcpy(&x, mixes + 0x20 + i * sizeof x, sizeof x); + if (x.mix_id < 0 || x.mix_id >= mix_count) { + audio_bad(r, AB_MIX_ID, "changed-mix record %u names mix %d of %d: that record is left out", i, x.mix_id, + mix_count); + continue; + } + r->mixes[x.mix_id] = x; + } } -/* Effect parameters arrive once per IPC update, while the DSP may process multiple frames. */ +// A delay, reverb or compressor block audio_effect_invalid refused, with its values: one line per kind. +static void effect_bad(AudioRenderer *r, int id, const AudrenEffectIn *e, const char *why) { + if (e->type == 3) { + AudrenDelayParam p; + memcpy(&p, e->specific, sizeof p); + audio_bad(r, AB_DELAY_PARAMS, + "delay effect %d on mix %d: %s (channels %u of %u, sample rate %#x, delay %u of %u): its sound " + "passes through unchanged", + id, e->mix_id, why, p.channels, p.channel_max, p.sample_rate, p.delay, p.delay_max); + } else if (e->type == 4) { + AudrenReverbParam p; + memcpy(&p, e->specific, sizeof p); + audio_bad(r, AB_REVERB_PARAMS, + "reverb effect %d on mix %d: %s (channels %u of %u, sample rate %#x, early mode %d, late mode %d, " + "decay %d, high-frequency decay %d, pre-delay %d): its sound passes through unchanged", + id, e->mix_id, why, p.channels, p.channel_max, (unsigned)p.sample_rate, p.early_mode, p.late_mode, + p.decay, p.hf_decay, p.pre_delay); + } else { + AudrenCompressorParam p; + memcpy(&p, e->specific, sizeof p); + audio_bad(r, AB_COMPRESSOR_PARAMS, + "compressor effect %d on mix %d: %s (channels %u of %u, threshold %g, ratio %g, input gain %g, " + "attack %g, release %g, output gain %g): its sound passes through unchanged", + id, e->mix_id, why, p.channels, p.channel_max, p.threshold, p.ratio, p.input_gain, p.attack, + p.release, p.output_gain); + } +} + +/* Effect parameters arrive once per IPC update, while the DSP may process multiple frames. An enabled effect whose + parameters cannot be used is marked bad (audio_bad logs it): audio_mixer_render passes its sound through, with no + DSP, delay line or reverb state made from those values, until an update brings usable ones. */ void audio_mixer_update_effects(AudioRenderer *r) { Mixer *m = r->mixer; for (int i=0;iparam.effect_count;i++) { const AudrenEffectIn *e=&r->effects[i]; EffectState *s=&m->effect[i]; unsigned revision=audio_revision(r); int reset=e->is_new || s->type!=e->type; + s->bad=0; if(reset){audio_effect_free(s->dsp);s->dsp=NULL;memset(s->biquad_z,0,sizeof s->biquad_z);} if(e->type==3 || e->type==4 || e->type==9) { unsigned status,channels;int dimensions_changed=0; @@ -775,14 +944,19 @@ void audio_mixer_update_effects(AudioRenderer *r) { else if(e->type==4){AudrenReverbParam p,old;memcpy(&p,e->specific,sizeof p);memcpy(&old,s->wire,sizeof old);status=p.status;channels=p.channels;dimensions_changed=p.channels!=old.channels || p.sample_rate!=old.sample_rate;} else {AudrenCompressorParam p,old;memcpy(&p,e->specific,sizeof p);memcpy(&old,s->wire,sizeof old);status=p.status;channels=p.channels;dimensions_changed=p.channels!=old.channels;} if(status==0 || dimensions_changed){audio_effect_free(s->dsp);s->dsp=NULL;} - if(e->enabled && channels){if(!s->dsp)s->dsp=audio_effect_create(e->type,e->specific,revision);else audio_effect_update(s->dsp,e->specific);} + if(e->enabled && channels){ + const char *why=audio_effect_invalid(e->type,e->specific); + if(why){audio_effect_free(s->dsp);s->dsp=NULL;s->bad=1;effect_bad(r,i,e,why);} + else if(!s->dsp)s->dsp=audio_effect_create(e->type,e->specific,revision); + else audio_effect_update(s->dsp,e->specific); + } } else if(s->dsp){audio_effect_free(s->dsp);s->dsp=NULL;} if(e->type==6){ unsigned status; if(revision>=15){AudrenBiquadEffectV2 p;memcpy(&p,e->specific,sizeof p);s->biquad_channels=p.channels;status=p.status;memcpy(s->numerator,p.numerator,sizeof p.numerator);memcpy(s->denominator,p.denominator,sizeof p.denominator);} else {AudrenBiquadEffectV1 p;memcpy(&p,e->specific,sizeof p);s->biquad_channels=p.channels;status=p.status;for(int k=0;k<3;k++)s->numerator[k]=p.numerator[k]/16384.f;for(int k=0;k<2;k++)s->denominator[k]=p.denominator[k]/16384.f;} if(status==0 || (status==1 && revision<6))memset(s->biquad_z,0,sizeof s->biquad_z); - if(e->enabled && (s->biquad_channels<1 || s->biquad_channels>6))rt_fatal("biquad effect channel count"); + // an enabled one with a channel count outside 1-6 is skipped by mix_simple_effect, which logs it } s->type=e->type;memcpy(s->wire,e->specific,sizeof s->wire); if(e->type!=5 || e->is_new) { audio_reverb_free(s->reverb); s->reverb=NULL; s->pending=1; } @@ -793,9 +967,17 @@ void audio_mixer_update_effects(AudioRenderer *r) { memset(s->detector,0,sizeof s->detector);memset(s->position,0,sizeof s->position); for(int c=0;c<6;c++)s->compression[c]=s->minimum[c]=1; memset(s->peak,0,sizeof s->peak); - if(e->enabled) { - if(p.channels<1 || p.channels>6 || p.delay_max<1 || p.delay_max>192000)rt_fatal("limiter delay dimensions"); - s->delay=calloc((size_t)p.channels*p.delay_max,sizeof(float));if(!s->delay)rt_fatal("limiter delay allocation"); + // channels outside 1-6: no delay line, and mix_limiter skips the effect and logs it + if(e->enabled && p.channels>=1 && p.channels<=6) { + if(p.delay_max<1 || p.delay_max>192000) { + s->bad=1; // checked again at the next update: without a delay line this block runs each time + audio_bad(r,AB_LIMITER_SIZE,"limiter effect %d on mix %d has a longest delay of %d " + "samples, not 1 to 192000: its sound passes through unchanged",i,e->mix_id, + p.delay_max); + } else { + s->delay=calloc((size_t)p.channels*p.delay_max,sizeof(float)); + if(!s->delay)rt_fatal("limiter delay allocation"); + } } } if(p.statistics_reset) { memset(s->peak,0,sizeof s->peak); for(int c=0;c<6;c++)s->minimum[c]=1; } @@ -805,6 +987,18 @@ void audio_mixer_update_effects(AudioRenderer *r) { if(s->reverb && p.sample_rate!=s->param.sample_rate) { audio_reverb_free(s->reverb); s->reverb=NULL; } if(memcmp(&p,&s->param,72) || p.status<2) s->pending=1; s->param=p; + // the render creates or updates the reverb from these values (mix_effects); one with another channel count is + // skipped there + if(e->enabled && (p.channels==1 || p.channels==2 || p.channels==4 || p.channels==6)) { + const char *why=audio_reverb_invalid(&p); + if(why) { + s->bad=1; + audio_bad(r,AB_R3D_PARAMS,"reverb3d effect %d on mix %d: %s (sample rate %u, decay time %g, " + "high-frequency decay ratio %g, density %g, reflection delay %g s, reverb delay %g s): its " + "sound passes through unchanged",i,e->mix_id,why,p.sample_rate,p.decay_time, + p.hf_decay_ratio,p.density,p.reflection_delay,p.reverb_delay); + } + } } } @@ -946,16 +1140,35 @@ static int mix_depth(AudioRenderer *r, int id) { /* Guest custom effects exchange int32 samples in two rings. Each ring has CPU and DSP 0x40-byte headers followed by samples; only the DSP header's owned cursor is advanced. */ -static void mix_aux(AudioRenderer *r, int mix, const AudrenEffectIn *e) { +// The render-pass effects below skip an effect whose dimensions, guest ring or mix buffer indices the game set out of +// range (nothing of it is read or written in that pass) and log it once per kind through audio_bad; every index is +// checked before the first write. +static void mix_aux(AudioRenderer *r, int mix, int id, const AudrenEffectIn *e) { Mixer *m=r->mixer; AudrenAuxParam p; memcpy(&p,e->specific,sizeof p); if(!p.send_info || !p.return_info || !p.capacity || !p.channels) return; - if(p.channels>24 || p.capacity>16*1024*1024)rt_fatal("auxiliary effect dimensions"); - uint8_t *send=ipc_guest(p.send_info,128+(uint64_t)p.capacity*4), *ret=ipc_guest(p.return_info,128+(uint64_t)p.capacity*4); + const uint64_t ring_bytes=128+(uint64_t)p.capacity*4; + if(p.channels>24 || p.capacity>16*1024*1024) { + audio_bad(r,AB_AUX_SIZE,"auxiliary effect %d on mix %d has %u channels and rings of %u samples (24 and 16 " + "Mi at most): it is skipped",id,mix,p.channels,p.capacity); + return; + } + if(!guest_range_ok(p.send_info,ring_bytes) || !guest_range_ok(p.return_info,ring_bytes)) { + audio_bad(r,AB_AUX_ADDRESS,"auxiliary effect %d on mix %d has its rings at %#llx and %#llx (%#llx bytes), " + "outside guest memory: it is skipped",id,mix,(unsigned long long)p.send_info, + (unsigned long long)p.return_info,(unsigned long long)ring_bytes); + return; + } + for(unsigned c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count) { + audio_bad(r,AB_AUX_INDEX,"auxiliary effect %d on mix %d sends buffer %u and returns to %u of the mix's " + "%d: it is skipped",id,mix,p.input[c],p.output[c],r->mixes[mix].buffer_count); + return; + } + uint8_t *send=ipc_guest(p.send_info,ring_bytes), *ret=ipc_guest(p.return_info,ring_bytes); uint32_t *sh=(uint32_t *)(send+64),*rh=(uint32_t *)(ret+64); int32_t *sd=(int32_t *)(send+128),*rd=(int32_t *)(ret+128); uint32_t wr=sh[1],read=rh[0],total=p.channels*(uint32_t)m->frame; if(e->enabled) report_once(m,6,"auxiliary effect in use, channels",(int)p.channels); for(unsigned c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count)rt_fatal("auxiliary effect mix buffer"); float *input=mixbuf(m,m->offset[mix]+p.input[c]),*output=mixbuf(m,m->offset[mix]+p.output[c]); if(!e->enabled) { if(input!=output)memmove(output,input,m->frame*sizeof(float)); continue; } uint64_t w=(uint64_t)wr+c*m->frame,at=(uint64_t)read+c*m->frame; @@ -978,12 +1191,30 @@ void audio_mixer_effect_result(AudioRenderer *r, int id, void *output, unsigned static void mix_limiter(AudioRenderer *r, int mix, int id) { Mixer *m=r->mixer; EffectState *s=&m->effect[id]; const AudrenEffectIn *e=&r->effects[id]; AudrenLimiterParam p;memcpy(&p,e->specific,sizeof p); - if(p.channels<1 || p.channels>6) { if(e->enabled)rt_fatal("limiter channel count");return; } - if(e->enabled && (!s->delay || p.delay_min<1 || p.delay_min>p.delay_max || p.attack<0 || p.attack>1 || p.release<0 || p.release>1))rt_fatal("limiter parameters"); + if(p.channels<1 || p.channels>6) { + if(e->enabled)audio_bad(r,AB_LIMITER_CHANNELS,"limiter effect %d on mix %d has %u channels, not 1 to 6: it " + "is skipped",id,mix,p.channels); + return; + } + for(int c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count) { + audio_bad(r,AB_LIMITER_INDEX,"limiter effect %d on mix %d reads buffer %u and writes %u of the mix's %d: " + "it is skipped",id,mix,p.input[c],p.output[c],r->mixes[mix].buffer_count); + return; + } + // NaN-safe: an attack or release that is not a number would turn the mix, and every mix it feeds, into NaN + int pass=!e->enabled; + if(e->enabled && (!s->delay || !(p.delay_min>=1 && p.delay_min<=p.delay_max) || !(p.attack>=0 && p.attack<=1) || + !(p.release>=0 && p.release<=1))) { + if(!s->bad) // bad: audio_mixer_update_effects logged the longest delay that left it without a delay line + audio_bad(r,AB_LIMITER_PARAMS,"limiter effect %d on mix %d: delay %d of %d samples, attack %g, release " + "%g (a delay of 1 to the longest, times of 0 to 1): its sound passes through unchanged",id,mix, + p.delay_min,p.delay_max,p.attack,p.release); + pass=1; + } for(int c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count)rt_fatal("limiter mix buffer"); float *input=mixbuf(m,m->offset[mix]+p.input[c]),*output=mixbuf(m,m->offset[mix]+p.output[c]); - if(!e->enabled) { if(input!=output)memmove(output,input,m->frame*sizeof(float));continue; } + if(pass) { if(input!=output)memmove(output,input,m->frame*sizeof(float));continue; } for(int k=0;kframe;k++) { float x=(input[k]*(32768.f/32767.f))*p.input_gain,level=fabsf(x); float coeff=level>s->detector[c]?p.attack:p.release;s->detector[c]+=coeff*(level-s->detector[c]); @@ -1010,16 +1241,32 @@ static void effect_counter_add(uint32_t cpu,uint32_t *dsp,uint32_t count) { uint32_t previous=*dsp-cpu,next=*dsp+count; *dsp=next-cpumixer;AudrenAuxParam p;memcpy(&p,e->specific,sizeof p); if(!p.send_info || !p.capacity || !p.channels)return; - if(p.channels>24 || p.capacity>16*1024*1024)rt_fatal("capture effect dimensions"); - uint8_t *base=ipc_guest(p.send_info,128+(uint64_t)p.capacity*4);uint32_t *cpu=(uint32_t *)base,*dsp=(uint32_t *)(base+64);int32_t *data=(int32_t *)(base+128); + const uint64_t ring_bytes=128+(uint64_t)p.capacity*4; + if(p.channels>24 || p.capacity>16*1024*1024) { + audio_bad(r,AB_CAPTURE_SIZE,"capture effect %d on mix %d has %u channels and a ring of %u samples (24 and 16 " + "Mi at most): it is skipped",id,mix,p.channels,p.capacity); + return; + } + if(!guest_range_ok(p.send_info,ring_bytes)) { + audio_bad(r,AB_CAPTURE_ADDRESS,"capture effect %d on mix %d has its ring at %#llx (%#llx bytes), outside guest " + "memory: it is skipped",id,mix,(unsigned long long)p.send_info,(unsigned long long)ring_bytes); + return; + } + uint8_t *base=ipc_guest(p.send_info,ring_bytes); + uint32_t *cpu=(uint32_t *)base,*dsp=(uint32_t *)(base+64);int32_t *data=(int32_t *)(base+128); dm_guest_write_prepare(base+64,64); if(!e->enabled){dsp[0]=dsp[1]=dsp[3]=0;return;} + for(unsigned c=0;c=r->mixes[mix].buffer_count) { + audio_bad(r,AB_CAPTURE_INDEX,"capture effect %d on mix %d reads buffer %u of the mix's %d: it is skipped", + id,mix,p.input[c],r->mixes[mix].buffer_count); + return; + } uint32_t wr=dsp[1],total=p.channels*(uint32_t)m->frame; for(unsigned c=0;c=r->mixes[mix].buffer_count)rt_fatal("capture effect input buffer"); float *in=mixbuf(m,m->offset[mix]+p.input[c]);uint64_t at=(uint64_t)wr+c*m->frame; if(at<=p.capacity)prepare_ring_write((uint8_t *)data,p.capacity*4,(uint32_t)(at%p.capacity)*4,(uint64_t)m->frame*4); for(int k=0;kframe;k++){int32_t value=effect_integer(in[k]);in[k]=value/32768.f;if(at<=p.capacity)data[(at+(unsigned)k)%p.capacity]=value;} @@ -1032,31 +1279,60 @@ static void mix_capture(AudioRenderer *r,int mix,const AudrenEffectIn *e) { } static void mix_simple_effect(AudioRenderer *r,int mix,int id) { Mixer *m=r->mixer;const AudrenEffectIn *e=&r->effects[id];EffectState *s=&m->effect[id]; + static const char *const names[10]={[1]="buffer-mix",[3]="delay",[4]="reverb",[6]="biquad",[9]="compressor"}; if(e->type==1){ AudrenBufferMixParam p;memcpy(&p,e->specific,sizeof p);if(!e->enabled)return; - if(p.count>24)rt_fatal("buffer-mix effect count"); + if(p.count>24) { + audio_bad(r,AB_BUFMIX_COUNT,"buffer-mix effect %d on mix %d mixes %u pairs of buffers (24 at most): it is " + "skipped",id,mix,p.count); + return; + } + for(unsigned c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count) { + audio_bad(r,AB_BUFMIX_INDEX,"buffer-mix effect %d on mix %d mixes buffer %u into %u of the mix's %d: " + "it is skipped",id,mix,p.input[c],p.output[c],r->mixes[mix].buffer_count); + return; + } for(unsigned c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count)rt_fatal("buffer-mix effect index"); float *in=mixbuf(m,m->offset[mix]+p.input[c]),*out=mixbuf(m,m->offset[mix]+p.output[c]); for(int k=0;kframe;k++)out[k]+=ceilf(in[k]*32768*p.volume[c])/32768; }return; } unsigned channels;if(e->type==6)channels=s->biquad_channels;else {uint16_t n;memcpy(&n,e->specific+14,2);channels=n;} - if(channels<1 || channels>6){if(e->enabled)rt_fatal("audio DSP effect channels");return;} + if(channels<1 || channels>6){ + // bad: a delay, reverb or compressor with such a count was logged at its update, by its parameters' kind + if(e->enabled && !s->bad) + audio_bad(r,AB_DSP_CHANNELS,"%s effect %d on mix %d has %u channels, not 1 to 6: it is skipped", + names[e->type],id,mix,channels); + return; + } float *in[6]={0},*out[6]={0};unsigned revision=audio_revision(r); for(unsigned c=0;ctype==3 && revision<11)||(e->type==4 && revision>=11))){static const unsigned map[6]={0,1,4,5,2,3};idx=map[c];} - if(e->specific[idx]>=r->mixes[mix].buffer_count || e->specific[6+idx]>=r->mixes[mix].buffer_count)rt_fatal("audio DSP effect buffer index"); + if(e->specific[idx]>=r->mixes[mix].buffer_count || e->specific[6+idx]>=r->mixes[mix].buffer_count) { + audio_bad(r,AB_DSP_INDEX,"%s effect %d on mix %d reads buffer %u and writes %u of the mix's %d: it is " + "skipped",names[e->type],id,mix,e->specific[idx],e->specific[6+idx],r->mixes[mix].buffer_count); + return; // nothing written yet: the loop only takes pointers + } in[c]=mixbuf(m,m->offset[mix]+e->specific[idx]);out[c]=mixbuf(m,m->offset[mix]+e->specific[6+idx]); } - if(!e->enabled){for(unsigned c=0;cframe*sizeof(float));return;} + // bad: parameters audio_mixer_update_effects could not use; it logged them and made no DSP + if(!e->enabled || s->bad){ + for(unsigned c=0;cframe*sizeof(float)); + return; + } if(e->type==6){ for(unsigned c=0;cframe;k++){ float x=in[c][k],y=s->numerator[0]*x+s->biquad_z[c][0]; s->biquad_z[c][0]=s->numerator[1]*x+s->denominator[0]*y+s->biquad_z[c][1];s->biquad_z[c][1]=s->numerator[2]*x+s->denominator[1]*y;out[c][k]=y; } - } else {if(!s->dsp)rt_fatal("audio DSP effect state missing");audio_effect_process(s->dsp,in,out,m->frame);} + } else { + // an invariant: audio_mixer_update_effects makes the DSP of every enabled effect with channels whose + // parameters pass, and marks the others bad + if(!s->dsp)rt_fatal("audio DSP effect state missing"); + audio_effect_process(s->dsp,in,out,m->frame); + } } static void mix_effects(AudioRenderer *r, int mix) { Mixer *m=r->mixer; int count=0; @@ -1066,22 +1342,38 @@ static void mix_effects(AudioRenderer *r, int mix) { for(int n=0;neffect_order[n]; const AudrenEffectIn *e=&r->effects[id]; EffectState *s=&m->effect[id]; if(e->type==1 || e->type==3 || e->type==4 || e->type==6 || e->type==9){mix_simple_effect(r,mix,id);continue;} - if(e->type==2) { mix_aux(r,mix,e); continue; } - if(e->type==8) { mix_capture(r,mix,e);continue; } + if(e->type==2) { mix_aux(r,mix,id,e); continue; } + if(e->type==8) { mix_capture(r,mix,id,e);continue; } if(e->type==7) { mix_limiter(r,mix,id); continue; } if(e->type!=5) { if(e->enabled && e->type<32 && !(m->effect_reported&(1u<type))) { m->effect_reported|=1u<type; rt_log("audio: effect type %u not implemented",e->type); } continue; } Reverb3dParam *p=&s->param; float *input[6]={0},*output[6]={0}; - if(p->channels!=1 && p->channels!=2 && p->channels!=4 && p->channels!=6) { if(e->enabled)rt_fatal("audio reverb channel count"); continue; } - for(int c=0;cchannels;c++) { + if(p->channels!=1 && p->channels!=2 && p->channels!=4 && p->channels!=6) { + if(e->enabled)audio_bad(r,AB_R3D_CHANNELS,"reverb3d effect %d on mix %d has %u channels, not 1, 2, 4 or " + "6: it is skipped",id,mix,p->channels); + continue; + } + int skip=0; + for(int c=0;cchannels && !skip;c++) { int idx=c; /* REV11+ channel resources use L,R,C,LFE,BL,BR; the reference DSP uses legacy order. */ if(p->channels==6 && (r->revision>>24)>='0'+11) { static const int map[6]={0,1,4,5,2,3}; idx=map[c]; } - if(p->input[idx]>=r->mixes[mix].buffer_count || p->output[idx]>=r->mixes[mix].buffer_count)rt_fatal("audio reverb buffer index"); + if(p->input[idx]>=r->mixes[mix].buffer_count || p->output[idx]>=r->mixes[mix].buffer_count) { + audio_bad(r,AB_R3D_INDEX,"reverb3d effect %d on mix %d reads buffer %u and writes %u of the mix's %d: " + "it is skipped",id,mix,p->input[idx],p->output[idx],r->mixes[mix].buffer_count); + skip=1; // nothing written yet: the loop only takes pointers + continue; + } input[c]=mixbuf(m,m->offset[mix]+p->input[idx]); output[c]=mixbuf(m,m->offset[mix]+p->output[idx]); } - if(!e->enabled) { for(int c=0;cchannels;c++)if(input[c]!=output[c])memmove(output[c],input[c],m->frame*sizeof(float)); continue; } + if(skip) continue; + // bad: values audio_reverb_invalid refused (audio_mixer_update_effects logged them); the reverb is neither made + // nor updated from them + if(!e->enabled || s->bad) { + for(int c=0;cchannels;c++)if(input[c]!=output[c])memmove(output[c],input[c],m->frame*sizeof(float)); + continue; + } if(!s->reverb)s->reverb=audio_reverb_create(p); else if(s->pending)audio_reverb_update(s->reverb,p); s->pending=0; audio_reverb_process(s->reverb,input,output,m->frame); } @@ -1108,13 +1400,28 @@ _Static_assert(__builtin_offsetof(CircularSinkParam,input)==28,"circular sink wi static void mix_circular_sink(AudioRenderer *r,int id) { Mixer *m=r->mixer;CircularSinkParam p;memcpy(&p,r->sinks[id].name,sizeof p); if(!p.address || !p.size)return; - if(p.channels<1 || p.channels>6 || p.format!=FMT_PCM16 || p.size>256*1024*1024 || p.size%2)rt_fatal("circular audio sink parameters"); + // a sink the game set out of range is skipped in this pass: nothing of it is written, its position stays + if(p.channels<1 || p.channels>6 || p.format!=FMT_PCM16 || p.size>256*1024*1024 || p.size%2) { + audio_bad(r,AB_SINK_PARAMS,"circular sink %d has %u channels, format %u and %u bytes (1 to 6, 16-bit, an even " + "size up to 256 MiB): it is skipped",id,p.channels,p.format,p.size); + return; + } + if(!guest_range_ok(p.address,p.size)) { + audio_bad(r,AB_SINK_ADDRESS,"circular sink %d has its buffer at %#llx (%#x bytes), outside guest memory: it is " + "skipped",id,(unsigned long long)p.address,p.size); + return; + } + for(unsigned c=0;c=m->nbuf) { + audio_bad(r,AB_SINK_INDEX,"circular sink %d reads mix buffer %u of %d: it is skipped",id,p.input[c], + m->nbuf); + return; + } if(m->sink_address[id]!=p.address || m->sink_size[id]!=p.size){m->sink_address[id]=p.address;m->sink_size[id]=p.size;m->sink_offset[id]=m->sink_old[id]=m->sink_written[id]=0;} uint8_t *data=ipc_guest(p.address,p.size);uint32_t at=m->sink_offset[id]; prepare_ring_write(data,p.size,at,(uint64_t)p.channels*m->frame*2); float gain=r->mixes[0].in_use?r->mixes[0].volume:1; for(unsigned c=0;c=m->nbuf)rt_fatal("circular audio sink input index"); float *in=mixbuf(m,p.input[c]); for(int k=0;kframe;k++){ float scaled=in[k]*gain*32768;int16_t value=!isfinite(scaled)?0:scaled>=32767?32767:scaled<=-32768?-32768:(int16_t)scaled; @@ -1132,6 +1439,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds, int drop) { memset(m->buf, 0, (size_t)m->nbuf * (size_t)frames * sizeof(float)); int at = 0; for (int i = 0; i < mc; i++) { m->offset[i] = at; if (r->mixes[i].in_use) at += r->mixes[i].buffer_count; } + // an invariant: audio_mixer_mix_layout_check refuses an update that would leave the mixes so if (at > m->nbuf) { rt_log("audio: mixes use %d buffers, renderer has %d", at, m->nbuf); rt_fatal("audio mix buffer layout"); } float (*vs)[MAX_FRAME]=m->voice_scratch,*filtered=m->filter_scratch; @@ -1192,6 +1500,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds, int drop) { if (v->in.splitter_id != SPLITTER_UNUSED && v->in.splitter_id < (uint32_t)m->nsplit) { const SplitInfo *s = &m->split[v->in.splitter_id]; for (int c = 0; c < cc; c++) for (int k = c; k < s->ndest; k += cc) { + if (s->ids[k] < 0) continue; // an id the renderer does not have (splitters_walk) SplitDest *d = &m->dest[s->ids[k]]; if (!d->used || !mix_valid(r, d->dest)) continue; const float *source=splitter_filter(d,vs[c],filtered,frames); @@ -1267,6 +1576,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds, int drop) { const MixPrev *xp = &m->mix_prev[order[a]]; const int xramp = ramp && xp->ok && xp->splitter == x->dest_splitter_id && xp->buffers == x->buffer_count; for (int c = 0; c < x->buffer_count && c < 24; c++) for (int k = c; k < s->ndest; k += x->buffer_count) { + if (s->ids[k] < 0) continue; // an id the renderer does not have (splitters_walk) SplitDest *d = &m->dest[s->ids[k]]; if (!d->used || !mix_valid(r, d->dest)) continue; const float *source=splitter_filter(d,mixbuf(m,src+c),filtered,frames); diff --git a/runtime/audio_reverb.c b/runtime/audio_reverb.c index 719da2d3..8cec6253 100644 --- a/runtime/audio_reverb.c +++ b/runtime/audio_reverb.c @@ -31,10 +31,21 @@ static float delay_tap(const Delay *d, unsigned at) { int i = (int)d->write - (i static float allpass(Delay *d, float x, float gain) { float v = x - gain * d->data[d->read]; return delay_update(d, v) + gain * v; } static float gain10(float x) { return x >= 0 ? 1 : x <= -5.3f ? 0 : powf(10, x); } static float radians(float x) { return x * 3.14159265358979323846f / 180; } +/* audio_mixer.c asks this before the game's block reaches create or update (and passes the sound through instead), so + validate's fatal is an invariant. */ +const char *audio_reverb_invalid(const Reverb3dParam *p) { + if (p->channels != 1 && p->channels != 2 && p->channels != 4 && p->channels != 6) + return "channel count not 1, 2, 4 or 6"; + if (p->sample_rate < 1000 || p->sample_rate > 192000) return "sample rate outside 1 to 192 kHz"; + if (!(p->decay_time > 0) || !(p->hf_decay_ratio > 0)) return "decay time or high-frequency decay ratio not above 0"; + if (!(p->density >= 0 && p->density <= 100)) return "density outside 0 to 100"; + if (!(p->reflection_delay >= 0 && p->reverb_delay >= 0 && p->reflection_delay + p->reverb_delay <= .4f)) + return "reflection and reverb delay outside 0 to 0.4 s together"; + return NULL; +} static void validate(const Reverb3dParam *p) { - if ((p->channels != 1 && p->channels != 2 && p->channels != 4 && p->channels != 6) || p->sample_rate < 1000 || p->sample_rate > 192000 || - !(p->decay_time > 0) || !(p->hf_decay_ratio > 0) || !(p->density >= 0 && p->density <= 100) || - !(p->reflection_delay >= 0 && p->reverb_delay >= 0 && p->reflection_delay + p->reverb_delay <= .4f)) rt_fatal("invalid reverb3d parameters"); + const char *why = audio_reverb_invalid(p); + if (why) rt_fatal("invalid reverb3d parameters: %s", why); } Reverb3d *audio_reverb_create(const Reverb3dParam *p) { validate(p); Reverb3d *s = calloc(1, sizeof *s); if (!s) rt_fatal("reverb allocation"); unsigned khz = p->sample_rate / 1000; diff --git a/runtime/audio_reverb.h b/runtime/audio_reverb.h index 51b23d42..beea81a4 100644 --- a/runtime/audio_reverb.h +++ b/runtime/audio_reverb.h @@ -12,6 +12,9 @@ typedef struct { } Reverb3dParam; _Static_assert(__builtin_offsetof(Reverb3dParam, status) == 72, "reverb3d wire layout"); typedef struct Reverb3d Reverb3d; +/* Why the game's block `p` cannot be used, as a constant string, or NULL. create and update end the process on such + a block, so the caller asks first. */ +const char *audio_reverb_invalid(const Reverb3dParam *p); Reverb3d *audio_reverb_create(const Reverb3dParam *p); void audio_reverb_free(Reverb3d *s); void audio_reverb_update(Reverb3d *s, const Reverb3dParam *p); diff --git a/runtime/audren.c b/runtime/audren.c index 013c0b9f..6f64db77 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -4,7 +4,11 @@ // service validates every section against the header, keeps server-side voice state, advances it in real time (one // frame of sample_count / sample_rate seconds per render tick, signalling the renderer's event) and writes the output // status the client expects. The mixer behind it is audio_mixer.c. +// What the game sends is never a reason to end the process (audio_bad): an update whose sections do not match the +// renderer is refused whole with AUDREN_R_INVALID_UPDATE, and a record, effect or sink whose values the renderer +// cannot use is left out of an update that is otherwise applied. NX2NAVI_AUDIO_STRICT=1 ends the process there instead. #include +#include #include #include #include @@ -69,6 +73,24 @@ long audren_renderers_live(void) { return plat_cas32(&g_live, 0, 0); } static int revision_number(uint32_t rev) { return (int)(rev >> 24) - '0'; } +void audio_bad(AudioRenderer *r, int kind, const char *fmt, ...) { + uint64_t n = __atomic_add_fetch(&r->bad_count, 1, __ATOMIC_RELAXED); + if (!r->strict && __atomic_exchange_n(&r->bad_seen[kind], 1, __ATOMIC_RELAXED)) return; // formats only once + char what[384]; + va_list ap; + va_start(ap, fmt); + vsnprintf(what, sizeof what, fmt, ap); + va_end(ap); + if (r->strict) { + ipc_backtrace(); // the game's frame chain when the game's update thread is here; nothing on the render thread + rt_fatal("audio: %s (NX2NAVI_AUDIO_STRICT=1)", what); + } + rt_log("audio: %s; game frame %llu, %llu such values so far; logged once per kind", what, + (unsigned long long)game_frame(), (unsigned long long)n); +} + +// OpenAudioRenderer's and GetWorkBufferSize's parameters, and host allocations: once per renderer, the SDK's fixed +// values, not the game's updates. static void need(int ok, const char *what) { if (ok) return; ipc_backtrace(); @@ -112,11 +134,9 @@ static void renderer_free(AudioRenderer *r) { // ---- RequestUpdate typedef struct { const uint8_t *p, *end; } Reader; -static const uint8_t *take(Reader *in, uint32_t n, const char *what) { - if ((size_t)(in->end - in->p) < n) { - rt_log("audren: input section %s (%u bytes) overruns the request", what, n); - need(0, "update input section size"); - } +// The next `n` bytes, or NULL when they run past the header's total (update_check refuses the update). +static const uint8_t *take(Reader *in, uint32_t n) { + if ((size_t)(in->end - in->p) < n) return NULL; const uint8_t *p = in->p; in->p += n; return p; @@ -139,14 +159,13 @@ static void voice_reset(ServerVoice *v) { v->ramp_pass = 0; // a new voice starts at its volumes: no ramp from what the slot's last voice was mixed with } +// request_update has checked the id against the voice count and, for a voice in use, its wave buffer index. static void update_voice(AudioRenderer *r, const AudrenVoiceIn *in) { - need(in->id < (uint32_t)r->param.voice_count, "voice id out of range"); ServerVoice *v = &r->voices[in->id]; if (in->is_new || !in->in_use) voice_reset(v); v->in = *in; v->use_float_biquads = 0; if (!in->in_use) return; - need(in->wave_buffer_index < 4, "wave buffer index"); // The play state as the DSP keeps it: Stop on a voice that is not stopped // becomes Stopping, which the next render frame turns into Stopped after giving back every queued buffer. if (in->play_state == PLAY_STARTED) v->state = SV_STARTED; @@ -247,22 +266,124 @@ static void voices_prepare(AudioRenderer *r) { } } -static uint32_t effect_out_bytes(const AudioRenderer *r) { return revision_number(r->revision) >= 9 ? 0x90u : 0x10u; } +static uint32_t effect_out_bytes(uint32_t revision) { return revision_number(revision) >= 9 ? 0x90u : 0x10u; } -static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_size, uint8_t *output, - uint64_t out_size, uint64_t perf_size) { - need(in_size >= sizeof(AudrenHeader), "update input too small"); - AudrenHeader h; - memcpy(&h, input, sizeof h); - need(h.total <= in_size, "update total size"); - Reader in = {input + sizeof h, input + h.total}; - plat_mutex_lock(&r->lock); - const uint64_t locked_ns = r->stats ? kern_now_ns() : 0; - r->revision = h.revision; - const int vc = r->param.voice_count, ec = r->param.effect_count, sc = r->param.sink_count, - mc = r->param.submix_count + 1; +// The output header of an update in the layout of `revision`, section by section: the SDK reads every section back and +// aborts when a size differs from the one it expects. +static AudrenHeader output_header(const AudioRenderer *r, uint32_t revision) { + const uint32_t npools = (uint32_t)r->param.effect_count + 4u * (uint32_t)r->param.voice_count; + AudrenHeader o = {0}; + o.revision = AUDREN_REV(revision_number(revision) >= 1 ? revision_number(revision) : 1); + o.memory_pools = npools * sizeof(AudrenPoolOut); + o.voices = (uint32_t)r->param.voice_count * sizeof(AudrenVoiceOut); + o.effects = (uint32_t)r->param.effect_count * effect_out_bytes(revision); + o.sinks = (uint32_t)r->param.sink_count * sizeof(AudrenSinkOut); + o.performance = 0x10; + o.behavior = AUDREN_BEHAVIOR_OUT; + o.render_info = AUDREN_RENDER_INFO_OUT; + o.total = (uint32_t)sizeof o + o.memory_pools + o.voices + o.effects + o.sinks + o.performance + o.behavior + + o.render_info; + return o; +} + +// An update's sections as update_check found them, and the output header it checked against the output buffer. +typedef struct { + AudrenHeader h, out; + const uint8_t *behavior, *voices, *splitters, *mixes; + const AudrenPoolIn *pools; + const AudrenChannelIn *chans; + const AudrenEffectIn *effects; + const AudrenSinkIn *sinks; + size_t voice_stride; + int float_voices; +} UpdateIn; + +// Every check of an update's framing, before anything of it is applied, in the order the sections come; without the +// renderer lock (it reads the renderer's fixed counts and the game's input). The SDK builds each section from the +// renderer's own counts, so a mismatch means this host reads another layout than the one the game wrote (a revision), +// not a value the game chose. 0, or AUDREN_R_INVALID_UPDATE after one audio_bad line. +static uint32_t update_check(AudioRenderer *r, const uint8_t *input, uint64_t in_size, uint64_t out_size, UpdateIn *u) { + if (in_size < sizeof(AudrenHeader)) + return AUDIO_REFUSE(r, AB_UPD_SMALL, "the input is %#llx bytes, shorter than its %#x-byte header", + (unsigned long long)in_size, (unsigned)sizeof(AudrenHeader)); + AudrenHeader *h = &u->h; + memcpy(h, input, sizeof *h); + if (h->total > in_size || h->total < sizeof *h) + return AUDIO_REFUSE(r, AB_UPD_TOTAL, "the header's total is %#x bytes, the input %#llx and the header %#x", + h->total, (unsigned long long)in_size, (unsigned)sizeof *h); + const int vc = r->param.voice_count, ec = r->param.effect_count, sc = r->param.sink_count; const uint32_t npools = (uint32_t)ec + 4u * (uint32_t)vc; - if (!r->logged) { + Reader in = {input + sizeof *h, input + h->total}; + const uint8_t *skip; + // the next section, or the update refused when it runs past the header's total +#define TAKE(dst, type, bytes, name) \ + if (!((dst) = (type)take(&in, (bytes)))) \ + return AUDIO_REFUSE(r, AB_UPD_OVERRUN, \ + "the %s section (%#x bytes at offset %#llx) runs past the header's total %#x", name, \ + (unsigned)(bytes), (unsigned long long)(in.p - input), h->total) + if (h->behavior != 0x10) + return AUDIO_REFUSE(r, AB_UPD_BEHAVIOR, "the behavior section is %#x bytes, the renderer reads %#x", + h->behavior, 0x10u); + TAKE(u->behavior, const uint8_t *, h->behavior, "behavior"); + if (h->memory_pools != npools * sizeof(AudrenPoolIn)) + return AUDIO_REFUSE(r, AB_UPD_POOLS, "the memory pool section is %#x bytes, the renderer reads %#x (%u pools)", + h->memory_pools, (unsigned)(npools * sizeof(AudrenPoolIn)), npools); + TAKE(u->pools, const AudrenPoolIn *, h->memory_pools, "memory pool"); + if (h->voice_resources % sizeof(AudrenChannelIn) || h->voice_resources / sizeof(AudrenChannelIn) > (uint32_t)vc * 6) + return AUDIO_REFUSE(r, AB_UPD_CHANNELS, + "the voice channel section is %#x bytes, the renderer reads records of %#x, %u at most", + h->voice_resources, (unsigned)sizeof(AudrenChannelIn), (unsigned)vc * 6); + TAKE(u->chans, const AudrenChannelIn *, h->voice_resources, "voice channel"); + u->float_voices = revision_number(h->revision) >= 15; + u->voice_stride = u->float_voices ? sizeof(AudrenVoiceInV2) : sizeof(AudrenVoiceIn); + if (h->voices != (uint32_t)vc * u->voice_stride) + return AUDIO_REFUSE(r, AB_UPD_VOICES, + "the voice section is %#x bytes, the renderer reads %#x (%d voices of %#x, REV%d)", + h->voices, (unsigned)(vc * u->voice_stride), vc, (unsigned)u->voice_stride, + revision_number(h->revision)); + TAKE(u->voices, const uint8_t *, h->voices, "voice"); + if (h->effects != (uint32_t)ec * sizeof(AudrenEffectIn)) + return AUDIO_REFUSE(r, AB_UPD_EFFECTS, "the effect section is %#x bytes, the renderer reads %#x (%d effects)", + h->effects, (unsigned)(ec * sizeof(AudrenEffectIn)), ec); + TAKE(u->effects, const AudrenEffectIn *, h->effects, "effect"); + TAKE(u->splitters, const uint8_t *, h->splitters, "splitter"); + TAKE(u->mixes, const uint8_t *, h->mixes, "mix"); + if (h->sinks != (uint32_t)sc * sizeof(AudrenSinkIn)) + return AUDIO_REFUSE(r, AB_UPD_SINKS, "the sink section is %#x bytes, the renderer reads %#x (%d sinks)", + h->sinks, (unsigned)(sc * sizeof(AudrenSinkIn)), sc); + TAKE(u->sinks, const AudrenSinkIn *, h->sinks, "sink"); + TAKE(skip, const uint8_t *, h->performance, "performance"); + TAKE(skip, const uint8_t *, h->render_info, "render info"); +#undef TAKE + (void)skip; + if (in.p != in.end) + return AUDIO_REFUSE(r, AB_UPD_UNCONSUMED, "%#llx bytes of the header's total %#x follow its last section", + (unsigned long long)(in.end - in.p), h->total); + u->out = output_header(r, h->revision); + if (u->out.total > out_size) + return AUDIO_REFUSE(r, AB_UPD_OUTPUT, + "the output buffer is %#llx bytes, the output %#x (effect results of %#x)", + (unsigned long long)out_size, u->out.total, effect_out_bytes(h->revision)); + return audio_mixer_routing_check(r, h->revision, u->splitters, h->splitters, u->mixes, h->mixes, + r->param.submix_count + 1); +} + +// A refused update, on the thread that sends RequestUpdate only (the SDK sends from one; a second sender would only +// race these diagnostic counts). The game ignores the result and goes on; while refusals go on, its sounds stop +// following it. +static void note_refused(AudioRenderer *r) { + r->refused++; + if (++r->refused_run == 1000) + rt_log("audio: 1000 RequestUpdates in a row were refused (about 5 s of the game's updates): the game's sounds " + "no longer follow it"); +} + +// 0, or AUDREN_R_INVALID_UPDATE with nothing of the update applied and nothing written to `output`. +static uint32_t request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_size, uint8_t *output, + uint64_t out_size, uint64_t perf_size) { + if (!r->logged && in_size >= sizeof(AudrenHeader)) { // the first update's sizes, also when it is refused + AudrenHeader h; + memcpy(&h, input, sizeof h); r->logged = 1; rt_log("audren: update REV%d sizes behavior=%u pools=%u voices=%u channels=%u effects=%u mixes=%u sinks=%u " "perf=%u splitters=%u render=%u total=%u out=%llu perf_out=%llu", @@ -270,80 +391,90 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s h.mixes, h.sinks, h.performance, h.splitters, h.render_info, h.total, (unsigned long long)out_size, (unsigned long long)perf_size); } - need(h.behavior == 0x10, "behavior section size"); - const uint8_t *behavior = take(&in, h.behavior, "behavior"); - memcpy(&r->behavior_flags, behavior + 8, 8); - need(h.memory_pools == npools * sizeof(AudrenPoolIn), "memory pool section size"); - const AudrenPoolIn *pools = (const AudrenPoolIn *)take(&in, h.memory_pools, "memory pools"); - need(h.voice_resources % sizeof(AudrenChannelIn) == 0 && - h.voice_resources / sizeof(AudrenChannelIn) <= (uint32_t)vc * 6, - "voice channel section size"); - const AudrenChannelIn *chans = (const AudrenChannelIn *)take(&in, h.voice_resources, "voice channels"); - const int float_voices = revision_number(h.revision) >= 15; - const size_t voice_stride = float_voices ? sizeof(AudrenVoiceInV2) : sizeof(AudrenVoiceIn); - need(h.voices == (uint32_t)vc * voice_stride, "voice section size"); - const uint8_t *voices = take(&in, h.voices, "voices"); - need(h.effects == (uint32_t)ec * sizeof(AudrenEffectIn), "effect section size"); - const AudrenEffectIn *effects = (const AudrenEffectIn *)take(&in, h.effects, "effects"); - const uint8_t *splitters = take(&in, h.splitters, "splitters"); - const uint8_t *mixes = take(&in, h.mixes, "mixes"); - need(h.sinks == (uint32_t)sc * sizeof(AudrenSinkIn), "sink section size"); - const AudrenSinkIn *sinks = (const AudrenSinkIn *)take(&in, h.sinks, "sinks"); - take(&in, h.performance, "performance"); - take(&in, h.render_info, "render info"); - need(in.p == in.end, "unconsumed update input"); + UpdateIn u; + uint32_t refused = update_check(r, input, in_size, out_size, &u); + if (refused) { + note_refused(r); + return refused; + } + const AudrenHeader h = u.h; + const int vc = r->param.voice_count, ec = r->param.effect_count, sc = r->param.sink_count, + mc = r->param.submix_count + 1; + const uint32_t npools = (uint32_t)ec + 4u * (uint32_t)vc; + plat_mutex_lock(&r->lock); + const uint64_t locked_ns = r->stats ? kern_now_ns() : 0; + // the one check that needs the mixes as they are: under the lock, before the first write + refused = audio_mixer_mix_layout_check(r, u.mixes, h.mixes, mc); + if (refused) { + plat_mutex_unlock(&r->lock); + note_refused(r); + return refused; + } + r->refused_run = 0; + r->revision = h.revision; + memcpy(&r->behavior_flags, u.behavior + 8, 8); + const AudrenPoolIn *pools = u.pools; + const AudrenChannelIn *chans = u.chans; + const AudrenEffectIn *effects = u.effects; + const AudrenSinkIn *sinks = u.sinks; + // From here on a value the renderer cannot use leaves its one record out (audio_bad); the rest is applied. for (uint32_t i = 0; i < h.voice_resources / sizeof(AudrenChannelIn); i++) { - need(chans[i].id < (uint32_t)vc * 6, "channel resource id"); - r->channels[chans[i].id] = chans[i]; + const uint32_t id = chans[i].id; + if (id >= (uint32_t)vc * 6) { + audio_bad(r, AB_CHANNEL_ID, "voice channel resource %u names id %u of %d: that record is left out", i, id, + vc * 6); + continue; + } + r->channels[id] = chans[i]; } for (int i = 0; i < vc; i++) { AudrenVoiceIn v; AudrenVoiceInV2 v2; - if (float_voices) { - memcpy(&v2, voices + i * voice_stride, sizeof v2); + if (u.float_voices) { + memcpy(&v2, u.voices + i * u.voice_stride, sizeof v2); memset(&v, 0, sizeof v); memcpy(&v, v2.prefix, sizeof v2.prefix); memcpy((uint8_t *)&v + 0x3c, v2.tail, sizeof v2.tail); for (unsigned f = 0; f < 2; f++) { - v.biquads[f].enable = v2.biquads[f].enable; - if (!v.biquads[f].enable) continue; - for (unsigned c = 0; c < 3; c++) need(isfinite(v2.biquads[f].numerator[c]), "voice biquad numerator"); - for (unsigned c = 0; c < 2; c++) - need(isfinite(v2.biquads[f].denominator[c]), "voice biquad denominator"); + const AudrenBiquadFloat *b = &v2.biquads[f]; + v.biquads[f].enable = b->enable; + if (!b->enable) continue; + if (isfinite(b->numerator[0]) && isfinite(b->numerator[1]) && isfinite(b->numerator[2]) && + isfinite(b->denominator[0]) && isfinite(b->denominator[1])) + continue; + audio_bad(r, AB_VOICE_FILTER, + "voice slot %d's filter %u has a coefficient that is not a finite number (%g %g %g / %g %g): " + "that filter is off", i, f, b->numerator[0], b->numerator[1], b->numerator[2], + b->denominator[0], b->denominator[1]); + v.biquads[f].enable = v2.biquads[f].enable = 0; } } else - memcpy(&v, voices + i * voice_stride, sizeof v); + memcpy(&v, u.voices + i * u.voice_stride, sizeof v); // An unused slot can have a zero id. Release its own voice and leave voice zero playing. if (!v.in_use) v.id = (uint32_t)i; + if (v.id >= (uint32_t)vc) { + audio_bad(r, AB_VOICE_ID, "voice slot %d names voice %u of %d: that record is left out", i, v.id, vc); + continue; + } + if (v.in_use && v.wave_buffer_index >= 4) { + audio_bad(r, AB_WAVE_INDEX, "voice %u's current wave buffer is %u of 4: that record is left out", v.id, + v.wave_buffer_index); + continue; + } update_voice(r, &v); ServerVoice *server = &r->voices[v.id]; - server->use_float_biquads = (uint8_t)float_voices; - if (float_voices) memcpy(server->float_biquads, v2.biquads, sizeof v2.biquads); + server->use_float_biquads = (uint8_t)u.float_voices; + if (u.float_voices) memcpy(server->float_biquads, v2.biquads, sizeof v2.biquads); } for (int i = 0; i < ec; i++) r->effects[i] = effects[i]; audio_mixer_update_effects(r); - audio_mixer_update_routing(r, splitters, h.splitters, mixes, h.mixes, mc); + audio_mixer_update_routing(r, u.splitters, h.splitters, u.mixes, h.mixes, mc); for (int i = 0; i < sc; i++) r->sinks[i] = sinks[i]; - // ---- output - const uint32_t eo = effect_out_bytes(r); - AudrenHeader o = {0}; - o.revision = AUDREN_REV(revision_number(h.revision) >= 1 ? revision_number(h.revision) : 1); - o.memory_pools = npools * sizeof(AudrenPoolOut); - o.voices = (uint32_t)vc * sizeof(AudrenVoiceOut); - o.effects = (uint32_t)ec * eo; - o.sinks = (uint32_t)sc * sizeof(AudrenSinkOut); - o.performance = 0x10; - o.behavior = AUDREN_BEHAVIOR_OUT; - o.render_info = AUDREN_RENDER_INFO_OUT; - o.total = (uint32_t)sizeof o + o.memory_pools + o.voices + o.effects + o.sinks + o.performance + o.behavior + - o.render_info; - if (o.total > out_size) { - rt_log("audren: output needs %u bytes, the client gave %llu (effect out %u)", o.total, - (unsigned long long)out_size, eo); - need(0, "update output size"); - } + // ---- output: update_check has checked its size against the output buffer + const uint32_t eo = effect_out_bytes(h.revision); + const AudrenHeader o = u.out; memset(output, 0, out_size); uint8_t *w = output; memcpy(w, &o, sizeof o); @@ -408,6 +539,7 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s if (late_gap) rt_log("audio: the game's audio update came %.1f ms after its last one (%llu render passes of 5 ms), game frame " "%llu", late_gap / 1e6, (unsigned long long)late_passes, (unsigned long long)game_frame()); + return 0; } // ---- render tick: one frame each time the output device has taken the ring below its target (audio_mixer_pace_ns), @@ -653,13 +785,19 @@ static void renderer_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *r rq->cmd, (unsigned long long)in.addr, (unsigned long long)in.size, (unsigned long long)out.addr, (unsigned long long)out.size, (unsigned long long)perf.addr, (unsigned long long)perf.size, rq->na, rq->nb, rq->nx, rq->nc); - need(in.size && out.size, "RequestUpdate buffers"); + if (!in.size || !out.size) { + rs->result = AUDIO_REFUSE(r, AB_UPD_BUFFERS, "the input buffer is %#llx bytes and the output buffer %#llx", + (unsigned long long)in.size, (unsigned long long)out.size); + note_refused(r); + return; + } int prio = 0; if (g_game_prio) prio = plat_thread_priority_get(), plat_thread_priority(PLAT_PRIO_HIGHEST); // AUDIO_GAME_PRIO - request_update(r, ipc_guest(in.addr, in.size), in.size, ipc_guest(out.addr, out.size), out.size, perf.size); + rs->result = + request_update(r, ipc_guest(in.addr, in.size), in.size, ipc_guest(out.addr, out.size), out.size, perf.size); if (g_game_prio) plat_thread_priority(g_game_prio >= 2 && prio < PLAT_PRIO_ABOVE_NORMAL ? PLAT_PRIO_ABOVE_NORMAL : prio); - for (unsigned i = 0; i < rq->nc && rq->cmd == 10; i++) + for (unsigned i = 0; i < rq->nc && rq->cmd == 10 && !rs->result; i++) // the SDK reads no output of a refusal if (rq->c[i].size && (i >= rq->nb || !rq->b[i].size)) rs->xout[rs->nxout++] = (IpcBuffer){rq->c[i].addr, rq->c[i].size, i}; return; @@ -706,7 +844,13 @@ static void renderer_close(void *p) { plat_dec32(&g_live); audio_mixer_close(r); kern_handle_close(r->event); - rt_log("audren: renderer closed after %llu frames", (unsigned long long)r->elapsed_frames); + uint64_t bad = __atomic_load_n(&r->bad_count, __ATOMIC_RELAXED); + if (bad || r->refused) + rt_log("audren: renderer closed after %llu frames, %llu game audio values left out or refused (%llu updates " + "refused)", (unsigned long long)r->elapsed_frames, (unsigned long long)bad, + (unsigned long long)r->refused); + else + rt_log("audren: renderer closed after %llu frames", (unsigned long long)r->elapsed_frames); renderer_free(r); } @@ -722,6 +866,7 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs int have_tm = kern_handle_copy(rq->copy[0], KERN_TMEM, &tm, sizeof tm); need(have_tm && tm.size >= work, "OpenAudioRenderer work buffer is not a transfer memory of the stated size"); AudioRenderer *r = renderer_new(&p); + r->strict = (uint8_t)cfg_bool("NX2NAVI_AUDIO_STRICT"); if (rt_knob("AUDIO_STATS", "0")) { r->stats = calloc(1, sizeof *r->stats); if (r->stats) r->stats->queue_min = UINT64_MAX; @@ -730,12 +875,14 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs 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); frames " - "owed after a stall are %s (NX2NAVI_AUDIO_CATCHUP)", + "owed after a stall are %s (NX2NAVI_AUDIO_CATCHUP); game audio values out of range are %s " + "(NX2NAVI_AUDIO_STRICT)", 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, cfg_bool("NX2NAVI_AUDIO_CATCHUP") ? "rendered and dropped" : "not caught up"); + g_game_prio, cfg_bool("NX2NAVI_AUDIO_CATCHUP") ? "rendered and dropped" : "not caught up", + r->strict ? "fatal" : "left out and logged once per kind"); 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 0c22bf60..c447dca6 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -162,6 +162,30 @@ typedef struct { } ServerVoice; enum { SV_STOPPED, SV_STARTED, SV_PAUSED, SV_STOPPING }; +// A RequestUpdate this host refuses (audren.c update_check, audio_mixer.c audio_mixer_routing_check and +// audio_mixer_mix_layout_check): audio module 153, description 41, the update's sections do not match the renderer. +// The SDK's RequestUpdateAudioRenderer hands it back to its caller (as it does descriptions 42 and 43) and aborts on +// any other result; the game ignores it and sends its next update. Nothing of a refused update is applied. +#define AUDREN_R_INVALID_UPDATE 0x5299u + +// Kinds of game value this host cannot use (audren.c audio_bad), one per site; each is logged once per renderer. +enum { + // the RequestUpdate is refused + AB_UPD_BUFFERS, AB_UPD_SMALL, AB_UPD_TOTAL, AB_UPD_BEHAVIOR, AB_UPD_POOLS, AB_UPD_CHANNELS, AB_UPD_VOICES, + AB_UPD_EFFECTS, AB_UPD_SINKS, AB_UPD_OVERRUN, AB_UPD_UNCONSUMED, AB_UPD_OUTPUT, + AB_SPL_HEADER, AB_SPL_MAGIC, AB_SPL_INFO_OVERRUN, AB_SPL_INFO, AB_SPL_PADDING, AB_DEST_OVERRUN, AB_DEST_MAGIC, + AB_SPL_SIZE, AB_MIX_DIRTY, AB_MIX_SIZE, AB_MIX_LAYOUT, + // one record of the update is left out, the rest is applied + AB_CHANNEL_ID, AB_VOICE_ID, AB_WAVE_INDEX, AB_VOICE_FILTER, AB_SPL_ID, AB_SPL_DEST_REF, AB_DEST_ID, + AB_DEST_FILTER, AB_MIX_ID, + // one effect or sink is passed through or skipped + AB_LIMITER_SIZE, AB_LIMITER_CHANNELS, AB_LIMITER_PARAMS, AB_LIMITER_INDEX, AB_DELAY_PARAMS, AB_REVERB_PARAMS, + AB_COMPRESSOR_PARAMS, AB_DSP_CHANNELS, AB_DSP_INDEX, AB_R3D_PARAMS, AB_R3D_CHANNELS, AB_R3D_INDEX, + AB_AUX_SIZE, AB_AUX_ADDRESS, AB_AUX_INDEX, AB_CAPTURE_SIZE, AB_CAPTURE_ADDRESS, AB_CAPTURE_INDEX, + AB_BUFMIX_COUNT, AB_BUFMIX_INDEX, AB_SINK_PARAMS, AB_SINK_ADDRESS, AB_SINK_INDEX, + AB_KINDS +}; + typedef struct AudioRenderer { AudrenParameter param; PlatMutex lock; @@ -179,22 +203,48 @@ typedef struct AudioRenderer { AudrenEffectIn *effects; AudrenChannelIn *channels; void *mixer; + uint8_t strict; // NX2NAVI_AUDIO_STRICT (OpenAudioRenderer): audio_bad ends the process + uint8_t bad_seen[AB_KINDS]; // audio_bad: the kinds logged so far (atomic exchange; any thread) + uint64_t bad_count; // audio_bad: values left out and updates refused (atomic) + uint64_t refused, refused_run; // updates refused, and refused in a row (the thread that sends RequestUpdate) } AudioRenderer; +// A value the game sent that this host cannot use: the caller refuses the update or leaves the one record, effect or +// sink out. Logs the first of each kind per renderer, with the game frame, and counts every one; with +// NX2NAVI_AUDIO_STRICT=1 it ends the process instead, as these values did before 0.10.2. Any thread; takes no lock but +// the log's. +void audio_bad(AudioRenderer *r, int kind, const char *fmt, ...) __attribute__((format(printf, 3, 4))); +// audio_bad for a refused RequestUpdate, as an expression that gives AUDREN_R_INVALID_UPDATE. +#define AUDIO_REFUSE(r, kind, fmt, ...) \ + (audio_bad((r), (kind), \ + "RequestUpdate refused with %#x: " fmt "; the renderer goes on with the last update it took", \ + AUDREN_R_INVALID_UPDATE, ##__VA_ARGS__), \ + AUDREN_R_INVALID_UPDATE) + // IAudioRendererManager, served under "audren:u" (ipc.c's service table). void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); long audren_renderers_live(void); // renderers whose render tick runs -// Mixer interface (audio_mixer.c). Every call runs with r->lock held except open, close and set_running, and the -// render thread's audio_mixer_pace_ns, audio_mixer_owed and audio_mixer_out_stats (audren.c render_thread and -// stats_report call them without it). Those three touch only atomics and state that only the render thread changes: -// device_check and channels_switch, which close and open the output stream, run on that thread too, and -// audio_mixer_close runs after renderer_close has joined it. +// Mixer interface (audio_mixer.c). Every call runs with r->lock held except open, close and set_running, the render +// thread's audio_mixer_pace_ns, audio_mixer_owed and audio_mixer_out_stats (audren.c render_thread and stats_report +// call them without it), and audio_mixer_routing_check. The render thread's three touch only atomics and state that +// only the render thread changes: device_check and channels_switch, which close and open the output stream, run on +// that thread too, and audio_mixer_close runs after renderer_close has joined it. audio_mixer_routing_check runs on the +// game's update thread before it takes the lock and reads only the renderer's fixed counts and the game's input. void audio_mixer_open(AudioRenderer *r); void audio_mixer_set_running(AudioRenderer *r, int running); void audio_mixer_close(AudioRenderer *r); +// Before an update is applied, without the renderer lock: the framing of the splitter and mix sections in the layout +// of `revision`. 0, or AUDREN_R_INVALID_UPDATE (logged through audio_bad). +uint32_t audio_mixer_routing_check(AudioRenderer *r, uint32_t revision, const uint8_t *splitters, + uint32_t splitter_bytes, const uint8_t *mixes, uint32_t mix_bytes, int mix_count); +// Renderer lock held, before the update is applied: the mixes in use, as the update leaves them, fit the renderer's +// mix buffers (audio_mixer_render lays them out one after another). 0, or AUDREN_R_INVALID_UPDATE. +uint32_t audio_mixer_mix_layout_check(AudioRenderer *r, const uint8_t *mixes, uint32_t mix_bytes, int mix_count); +// After both checks passed. A record whose ids the renderer does not have is left out (audio_bad). void audio_mixer_update_routing(AudioRenderer *r, const uint8_t *splitters, uint32_t splitter_bytes, const uint8_t *mixes, uint32_t mix_bytes, int mix_count); +// An enabled effect whose parameters the DSP cannot use is passed through by audio_mixer_render (audio_bad). 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); diff --git a/runtime/cfg.c b/runtime/cfg.c index 80c2f487..2b5474a6 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -36,6 +36,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o 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", "PHYSICS_STEPS_FULL", "PICKUP_COUNT", "WATER_STEPS", "SOUND_STEPS", "FLOOR_PROBE", "FLOOR_SCAN", "FLOOR_WATCH", "FLOOR_CARRY", "MOVIE_PTS", "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_CATCHUP", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", + "AUDIO_STRICT", "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", "CAPTURE_AT", "CAPTURE_DIR", "CAPTURE_REPLAY", "CAPTURE_REPLAY_CAMERA", // bug captures and their replays (bugcap.c) "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", @@ -141,6 +142,7 @@ static const struct { const char *key, *section; } sections[] = { {"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"TEXT_SPEED", "language"}, {"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"}, {"AUDIO_DEVICE", "audio"}, {"MICROPHONE", "audio"}, {"MENU_SOUND_VOLUME", "audio"}, {"AUDIO_RAMP", "audio"}, {"AUDIO_CATCHUP", "audio"}, + {"AUDIO_STRICT", "audio"}, {"MOTION", "input"}, {"SPRINT_BUTTON", "input"}, {"SPRINT_MODE", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, {"RUMBLE_HD", "input"}, {"DS3", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, {"DEBUG_MENU", "input"}, @@ -293,6 +295,8 @@ static const CfgMeta metas[] = { {"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 + // audren.c audio_bad: 1 ends the process at the first game audio value out of range, as before 0.10.2 + {"AUDIO_STRICT", "0", "0|1", RESTART}, // sprint.c: the sprint button (b the game's B, l3 the left stick's click) and toggle or hold; the Controls tab's // Sprint rows (1.1.1) {"SPRINT_BUTTON", "off", "off|b|l3", LIVE}, {"SPRINT_MODE", "toggle", "toggle|hold", LIVE}, diff --git a/tests/audio_mixer_check.c b/tests/audio_mixer_check.c index 33c7d7fb..35dbddbf 100644 --- a/tests/audio_mixer_check.c +++ b/tests/audio_mixer_check.c @@ -62,7 +62,11 @@ int cfg_bool(const char *name) { double cfg_double(const char *name) { (void)name; return 100; } unsigned cfg_version(void) { return 1; } int audio_buffer_empty(const AudrenWaveBuffer *b) { return !b->address || !b->size || b->end <= b->start; } -/* no effects in these renderers */ +/* no effects in these renderers, and no value the mixer leaves out (audren.c audio_bad: counted, none expected) */ +static int bad_calls; +void audio_bad(AudioRenderer *r, int kind, const char *fmt, ...) { (void)r, (void)kind, (void)fmt; bad_calls++; } +const char *audio_effect_invalid(unsigned type, const void *param) { (void)type, (void)param; return NULL; } +const char *audio_reverb_invalid(const Reverb3dParam *p) { (void)p; return NULL; } AudioEffectDsp *audio_effect_create(unsigned type, const void *param, unsigned revision) { (void)type, (void)param, (void)revision; return NULL; } void audio_effect_free(AudioEffectDsp *s) { (void)s; } void audio_effect_update(AudioEffectDsp *s, const void *param) { (void)s, (void)param; } @@ -431,6 +435,7 @@ int main(void) { ramps_off(); catch_up(); change_events_once(); + CHECK(!bad_calls, "the mixer left %d values of these renderers out", bad_calls); printf("audio_mixer_check: %s\n", fails ? "FAIL" : "ok"); return fails ? 1 : 0; } diff --git a/tests/audio_params_check.c b/tests/audio_params_check.c new file mode 100644 index 00000000..c40a3348 --- /dev/null +++ b/tests/audio_params_check.c @@ -0,0 +1,928 @@ +/* What the audio renderer does with values the game sends that it cannot use (runtime/audren.c update_check, + request_update, audio_bad; runtime/audio_mixer.c splitters_walk, audio_mixer_routing_check, + audio_mixer_mix_layout_check, audio_mixer_update_effects and the render-pass effects), with the rest of the runtime + stubbed and the real effect DSPs (runtime/audio_effects.c, runtime/audio_reverb.c) linked. No game data and no audio + device: NX2NAVI_AUDIO=none mixes without one. + + Every RequestUpdate goes through renderer_handler with a hand-built input. A refused update must answer 0x5299 (the + SDK hands that result to the game, which ignores it; any other result makes the SDK abort), write no output and + change no renderer state. A record, effect or sink the renderer cannot use is left out of an update that is + otherwise applied, or passed through or skipped in the render pass. Each kind is logged once per renderer. rt_fatal + prints "FATAL:" and exits with 2, so a value that still ends the process fails the run. + + audio_params_check [-v] every check below (-v prints the log lines) + audio_params_check strict-effect | strict-update NX2NAVI_AUDIO_STRICT=1: must end with "FATAL: audio:" */ +#include "../runtime/audren.c" +#include "../runtime/audio_mixer.c" + +#include + +static int fails; +#define CHECK(ok, ...) \ + do { \ + if (!(ok)) { \ + fails++; \ + printf("FAIL %s:%d: ", __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } \ + } while (0) + +/* ---- the runtime around the renderer ---- */ +GfxStats gfx_counters; +unsigned rt_trace; +static int strict_setting, verbose; +static int once_lines; /* log lines that end with "logged once per kind" */ +static char last_once[512]; +void rt_log(const char *fmt, ...) { + char line[1024]; + va_list ap; + va_start(ap, fmt); + vsnprintf(line, sizeof line, fmt, ap); + va_end(ap); + if (strstr(line, "logged once per kind")) once_lines++, snprintf(last_once, sizeof last_once, "%s", line); + if (verbose) printf(" log: %s\n", line); +} +void rt_logf(const char *fmt, ...) { (void)fmt; } +void rt_fatal(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + printf("FATAL: "); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + fflush(stdout); + exit(2); +} +void ipc_backtrace(void) {} +int rt_knob(const char *name, const char *def) { (void)name, (void)def; return 0; } +uint64_t kern_now_ns(void) { return 1; } +uint32_t kern_event_new(void) { return 1; } +uint32_t kern_event_readable(uint32_t writable) { return writable; } +void kern_event_signal(uint32_t writable) { (void)writable; } +void kern_handle_close(uint32_t h) { (void)h; } +int kern_handle_copy(uint32_t h, int kind, void *out, size_t n) { (void)h, (void)kind, (void)out, (void)n; return 0; } +int kern_core_away(void) { return 0; } +void kern_core_back(int *away) { (void)away; } +/* the guest address check of ipc.c ipc_guest, ending the process as it does */ +void *ipc_guest(uint64_t addr, uint64_t size) { + if ((!addr && size) || addr + size < addr || addr + size > RT_ADDRESS_LIMIT) + rt_fatal("ipc: client buffer %#llx+%#llx", (unsigned long long)addr, (unsigned long long)size); + return (void *)(uintptr_t)addr; +} +uint32_t ipc_session_new(const char *name, IpcHandler handler, void *state, void (*close)(void *state)) { + (void)name, (void)handler, (void)state, (void)close; + return 1; +} +void ipc_unimplemented(const IpcSession *s, const IpcRequest *rq) { (void)s, (void)rq; exit(3); } +void ipc_out(IpcResponse *rs, const void *p, uint32_t n) { (void)rs, (void)p, (void)n; } +const void *ipc_arg(const IpcRequest *rq, uint32_t off, uint32_t n) { (void)rq, (void)off, (void)n; exit(3); } +void menu_sound_mix(float *out, uint32_t frames, int channels, int rate) { + (void)out, (void)frames, (void)channels, (void)rate; +} +const char *cfg_get(const char *name) { return !strcmp(name, "NX2NAVI_AUDIO") ? "none" : NULL; } +const char *cfg_value(const char *name) { return !strcmp(name, "NX2NAVI_AUDIO_CHANNELS") ? "stereo" : "default"; } +int cfg_bool(const char *name) { + if (!strcmp(name, "NX2NAVI_AUDIO_RAMP") || !strcmp(name, "NX2NAVI_AUDIO_CATCHUP")) return 1; + if (!strcmp(name, "NX2NAVI_AUDIO_STRICT")) return strict_setting; + printf("unexpected setting %s\n", name); + exit(3); +} +double cfg_double(const char *name) { (void)name; return 100; } +unsigned cfg_version(void) { return 1; } +/* platform: one thread and no render thread (the checks render by hand) */ +void plat_mutex_init(PlatMutex *m) { (void)m; } +void plat_mutex_lock(PlatMutex *m) { (void)m; } +void plat_mutex_unlock(PlatMutex *m) { (void)m; } +int plat_thread_priority_get(void) { return 0; } +void plat_thread_priority(int p) { (void)p; } +PlatThread *plat_thread_new(PlatThreadFn fn, void *arg, const char *name) { + (void)fn, (void)arg, (void)name; + return NULL; +} +void plat_thread_join(PlatThread *t) { (void)t; } +void plat_wait_ns(uint64_t ns) { (void)ns; } + +#ifdef _WIN32 +__declspec(dllimport) void *__stdcall VirtualAlloc(void *address, size_t size, unsigned long type, + unsigned long protect); +#else +#include +#endif +/* `n` bytes of zeroed memory below RT_ADDRESS_LIMIT */ +static void *low_alloc(size_t n) { + for (uint64_t at = 1ull << 32; at + n <= RT_ADDRESS_LIMIT; at += 1ull << 30) { +#ifdef _WIN32 + void *p = VirtualAlloc((void *)(uintptr_t)at, n, 0x3000 /* MEM_COMMIT | MEM_RESERVE */, + 0x04 /* PAGE_READWRITE */); +#else + void *p = mmap((void *)(uintptr_t)at, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) p = NULL; + else if ((uintptr_t)p + n > RT_ADDRESS_LIMIT) munmap(p, n), p = NULL; +#endif + if (p) return p; + } + printf("no memory below the guest address limit\n"); + exit(3); +} + +/* ---- a renderer: 2 voices, 2 effect slots, 1 sink, the final mix (2 buffers, the mixer's 0-1) and 1 submix (2 + buffers, 2-3), 1 splitter with 2 destinations, 8 mix buffers. Voice 0 plays a wave buffer of one constant sample + (0.5) into final mix buffer 0, so an effect from buffer 0 to buffer 1 shows in buffer 1: 0.5 when it passes the sound + through, 0 when it is skipped. ---- */ +#define FRAME 240 +#define VOICES 2 +#define EFFECTS 2 +static int16_t pcm[4 * FRAME]; + +static AudioRenderer *open_renderer(void) { + AudrenParameter p = {.sample_rate = 48000, .sample_count = FRAME, .mix_buffer_count = 8, .submix_count = 1, + .voice_count = VOICES, .sink_count = 1, .effect_count = EFFECTS, .splitter_count = 1, + .splitter_destination_count = 2, .revision = AUDREN_REV(11)}; + for (size_t i = 0; i < sizeof pcm / sizeof *pcm; i++) pcm[i] = 16384; + AudioRenderer *r = renderer_new(&p); + r->strict = (uint8_t)cfg_bool("NX2NAVI_AUDIO_STRICT"); + audio_mixer_open(r); + r->state = 0; + audio_mixer_set_running(r, 1); + return r; +} +static void close_renderer(AudioRenderer *r) { + audio_mixer_close(r); + renderer_free(r); +} +/* One render pass as audren.c render_thread makes it; the voices are not advanced (every sample is the same). */ +static void pass(AudioRenderer *r) { + voices_prepare(r); + audio_mixer_render(r, (double)FRAME / 48000, 0); + r->elapsed_frames++; +} +static const float *buf(AudioRenderer *r, int b) { return mixbuf(r->mixer, b); } +static int flat(const float *d, float level) { + for (int k = 0; k < FRAME; k++) + if (fabsf(d[k] - level) > 1e-6f) return 0; + return 1; +} +static int finite_buffers(AudioRenderer *r) { + Mixer *m = r->mixer; + for (int i = 0; i < m->nbuf * m->frame; i++) + if (!isfinite(m->buf[i])) return 0; + return 1; +} +/* how many kinds audio_bad has logged for this renderer */ +static int kinds_seen(const AudioRenderer *r) { + int n = 0; + for (int k = 0; k < AB_KINDS; k++) n += r->bad_seen[k] != 0; + return n; +} + +/* ---- an update ---- */ +typedef struct { + int rev; /* 11 or 15 */ + AudrenChannelIn chan[2]; + uint32_t nchan; + AudrenVoiceIn voice[VOICES]; /* REV11 layout; at REV15 vfilter holds the voices' filters */ + AudrenBiquadFloat vfilter[VOICES][2]; + AudrenEffectIn effect[EFFECTS]; + uint8_t split[1024]; + uint32_t split_bytes; + AudrenMixIn mix[2]; + uint8_t mix_raw[0x20 + 3 * sizeof(AudrenMixIn)]; /* when mix_raw_bytes: the mix section, not the two mixes */ + uint32_t mix_raw_bytes; + AudrenSinkIn sink; +} Update; + +static void update_default(Update *u, int rev) { + memset(u, 0, sizeof *u); + u->rev = rev; + u->chan[0] = (AudrenChannelIn){.id = 0, .in_use = 1}; + u->chan[0].mix[0] = 1; + u->nchan = 1; + AudrenVoiceIn *v = &u->voice[0]; + v->id = 0, v->in_use = 1, v->play_state = PLAY_STARTED, v->sample_format = FMT_PCM16, v->sample_rate = 48000; + v->channel_count = 1, v->pitch = 1, v->volume = 1, v->mix_id = 0, v->splitter_id = SPLITTER_UNUSED; + v->channel_ids[0] = 0; + v->wave_buffers[0] = + (AudrenWaveBuffer){.address = (uintptr_t)pcm, .size = sizeof pcm, .start = 0, .end = 4 * FRAME}; + u->voice[1] = (AudrenVoiceIn){.id = 1, .splitter_id = SPLITTER_UNUSED}; + u->mix[0] = (AudrenMixIn){.volume = 1, .sample_rate = 48000, .buffer_count = 2, .in_use = 1, .mix_id = 0, + .dest_mix_id = -1, .dest_splitter_id = SPLITTER_UNUSED}; + u->mix[1] = (AudrenMixIn){.volume = 1, .sample_rate = 48000, .buffer_count = 2, .in_use = 1, .mix_id = 1, + .dest_mix_id = 0, .dest_splitter_id = SPLITTER_UNUSED}; + u->mix[1].mix[0][0] = u->mix[1].mix[1][1] = 1; + u->sink = (AudrenSinkIn){.type = SINK_DEVICE, .in_use = 1, .input_count = 2, .inputs = {0, 1}}; +} +/* voice 1 like voice 0, into `mix` through channel resource 6 (when splitter is SPLITTER_UNUSED) or into `splitter` */ +static void voice1_on(Update *u, int32_t mix, uint32_t splitter) { + u->voice[1] = u->voice[0]; + u->voice[1].id = 1, u->voice[1].mix_id = mix, u->voice[1].splitter_id = splitter, u->voice[1].channel_ids[0] = 6; + u->chan[1] = (AudrenChannelIn){.id = 6, .in_use = 1}; + u->chan[1].mix[0] = u->chan[1].mix[1] = 1; + u->nchan = 2; +} + +/* Where build put each section, for the checks that change the input afterwards. */ +static struct { uint32_t total, chans, voices, effects, splitters, mixes, sinks; } L; +/* The game's buffers: the input, the output, the auxiliary and capture rings and the circular sink's buffer. Guest + memory lies below RT_ADDRESS_LIMIT, and ipc_guest and the mixer's ring checks hold the game's addresses to it; this + process's own data lies above it, so main takes these from memory below the limit (low_alloc). */ +#define IN_BYTES 0x8000u +#define OUT_BYTES 0x2000u +#define SINK_SAMPLES 4096u +static uint8_t *in, *out; +static int32_t *ring_send, *ring_ret; /* 0x80-byte headers, then 4096 samples */ +static int16_t *sink_data; + +static uint32_t build(const Update *u) { + memset(in, 0, IN_BYTES); + const size_t stride = u->rev >= 15 ? sizeof(AudrenVoiceInV2) : sizeof(AudrenVoiceIn); + const uint32_t npools = EFFECTS + 4 * VOICES; + AudrenHeader h = {.revision = AUDREN_REV(u->rev), .behavior = 0x10, .memory_pools = npools * sizeof(AudrenPoolIn), + .voices = (uint32_t)(VOICES * stride), .voice_resources = u->nchan * sizeof(AudrenChannelIn), + .effects = EFFECTS * sizeof(AudrenEffectIn), + .mixes = u->mix_raw_bytes ? u->mix_raw_bytes : 2 * sizeof(AudrenMixIn), + .sinks = sizeof(AudrenSinkIn), .performance = 0x10, .splitters = u->split_bytes, + .render_info = 0x10}; + uint32_t at = sizeof h + h.behavior + h.memory_pools; + L.chans = at; + memcpy(in + at, u->chan, h.voice_resources); + at += h.voice_resources; + L.voices = at; + for (int i = 0; i < VOICES; i++, at += (uint32_t)stride) { + if (u->rev < 15) { + memcpy(in + at, &u->voice[i], sizeof u->voice[i]); + continue; + } + AudrenVoiceInV2 v2; + memcpy(v2.prefix, &u->voice[i], sizeof v2.prefix); + memcpy(v2.biquads, u->vfilter[i], sizeof v2.biquads); + memcpy(v2.tail, (const uint8_t *)&u->voice[i] + 0x3c, sizeof v2.tail); + memcpy(in + at, &v2, sizeof v2); + } + L.effects = at; + memcpy(in + at, u->effect, h.effects); + at += h.effects; + L.splitters = at; + memcpy(in + at, u->split, u->split_bytes); + at += u->split_bytes; + L.mixes = at; + memcpy(in + at, u->mix_raw_bytes ? (const void *)u->mix_raw : (const void *)u->mix, h.mixes); + at += h.mixes; + L.sinks = at; + memcpy(in + at, &u->sink, sizeof u->sink); + at += h.sinks + h.performance + h.render_info; + h.total = at; + memcpy(in, &h, sizeof h); + return L.total = at; +} +static AudrenHeader *header(void) { return (AudrenHeader *)in; } + +/* A RequestUpdate (cmd 4; 10 takes the output in C buffers) of `size` input bytes into an output of `out_size`. */ +static IpcResponse request(AudioRenderer *r, unsigned cmd, uint64_t size, uint64_t out_size) { + IpcSession s = {.state = r}; + IpcRequest rq = {.cmd = cmd}; + IpcBuffer i = {(uintptr_t)in, size, 0}, o = {(uintptr_t)out, out_size, 0}, p = {(uintptr_t)out + 0x1800, 0x10, 1}; + if (cmd == 4) rq.na = 1, rq.a[0] = i, rq.nb = 2, rq.b[0] = o, rq.b[1] = p; + else rq.nx = 1, rq.x[0] = i, rq.nc = 2, rq.c[0] = o, rq.c[1] = p; + IpcResponse rs = {0}; + renderer_handler(&s, &rq, &rs); + return rs; +} +static uint32_t apply(AudioRenderer *r, const Update *u) { return request(r, 4, build(u), OUT_BYTES).result; } + +/* ---- the splitter section ---- */ +typedef struct { uint32_t id; int32_t mix; int buffer; float volume; uint8_t used; AudrenBiquadFloat filter[2]; } Dest; +static const int32_t both_ids[2] = {0, 1}; +static const Dest both_dests[2] = {{.id = 0, .mix = 0, .buffer = 0, .volume = 1, .used = 1}, + {.id = 1, .mix = 0, .buffer = 1, .volume = 1, .used = 1}}; +/* splitter `splitter` with destination ids `ids` (`nids` of them; -1: no info record), then `ndest` destination + records, padded to 16 bytes */ +static uint32_t splitter_section(uint8_t *p, int rev, uint32_t splitter, int nids, const int32_t *ids, int ndest, + const Dest *d) { + const uint32_t stride = rev >= 15 ? 0xa8 : rev >= 12 ? 0x90 : 0x70; + memset(p, 0, 1024); + uint32_t head[3] = {0x48444e53u, nids >= 0 ? 1u : 0u, (uint32_t)ndest}, at = 0x20; + memcpy(p, head, sizeof head); + if (nids >= 0) { + uint32_t info[4] = {0x49444e53u, splitter, 48000, (uint32_t)nids}; + memcpy(p + at, info, sizeof info); + if (nids) memcpy(p + at + 16, ids, 4u * (uint32_t)nids); + at += 16 + 4u * (uint32_t)nids + 12; + } + for (int i = 0; i < ndest; i++, at += stride) { + uint32_t magic = 0x44444e53u; + float vol[24] = {0}; + vol[d[i].buffer] = d[i].volume; + memcpy(p + at, &magic, 4); + memcpy(p + at + 4, &d[i].id, 4); + memcpy(p + at + 8, vol, sizeof vol); + memcpy(p + at + 0x68, &d[i].mix, 4); + if (rev >= 15) memcpy(p + at + 0x6c, d[i].filter, sizeof d[i].filter); + p[at + (rev >= 15 ? 0x9c : rev >= 12 ? 0x84 : 0x6c)] = d[i].used; + } + return (at + 15) & ~15u; +} +static int split_is(AudioRenderer *r, int32_t a, int32_t b) { + const SplitInfo *s = &((Mixer *)r->mixer)->split[0]; + return s->ndest == 2 && s->ids && s->ids[0] == a && s->ids[1] == b; +} + +/* ---- the state a refused update must leave as it was ---- */ +typedef struct { + ServerVoice voices[VOICES]; + AudrenEffectIn effects[EFFECTS]; + AudrenMixIn mixes[2]; + AudrenSinkIn sinks[1]; + AudrenChannelIn channels[VOICES * 6]; + AudrenPoolIn pools[EFFECTS + 4 * VOICES]; + uint32_t revision; + uint64_t behavior; + int32_t split_n, split_ids[8]; + SplitDest dest[2]; +} Snap; +static void snap(AudioRenderer *r, Snap *s) { + Mixer *m = r->mixer; + memset(s, 0, sizeof *s); + memcpy(s->voices, r->voices, sizeof s->voices); + memcpy(s->effects, r->effects, sizeof s->effects); + memcpy(s->mixes, r->mixes, sizeof s->mixes); + memcpy(s->sinks, r->sinks, sizeof s->sinks); + memcpy(s->channels, r->channels, sizeof s->channels); + memcpy(s->pools, r->pools, sizeof s->pools); + s->revision = r->revision, s->behavior = r->behavior_flags; + s->split_n = m->split[0].ndest; + for (int k = 0; k < s->split_n && k < 8; k++) s->split_ids[k] = m->split[0].ids[k]; + memcpy(s->dest, m->dest, sizeof s->dest); +} +static int out_untouched(void) { + for (size_t i = 0; i < OUT_BYTES; i++) + if (out[i] != 0xa5) return 0; + return 1; +} + +/* 1. Valid updates answer 0, and the output's total is the sum of its sections (the SDK aborts on a mismatch). */ +static void valid_update(void) { + for (int rev = 11; rev <= 15; rev += 4) { + AudioRenderer *r = open_renderer(); + Update u; + update_default(&u, rev); + u.split_bytes = splitter_section(u.split, rev, 0, 2, both_ids, 2, both_dests); + memset(out, 0xa5, OUT_BYTES); + IpcResponse rs = request(r, 4, build(&u), OUT_BYTES); + const AudrenHeader *o = (const AudrenHeader *)out; + CHECK(rs.result == 0, "REV%d: a valid update answers %#x", rev, rs.result); + CHECK(o->total == sizeof *o + o->memory_pools + o->voices + o->effects + o->sinks + o->performance + + o->behavior + o->render_info && + o->total == output_header(r, AUDREN_REV(rev)).total, + "REV%d: the output's total %u is the sum of its sections", rev, o->total); + CHECK(r->voices[0].in.in_use && split_is(r, 0, 1), "REV%d: the voice and the splitter are applied", rev); + pass(r); + CHECK(flat(buf(r, 0), 0.5f), "REV%d: the voice plays into buffer 0 (%g)", rev, buf(r, 0)[0]); + rs = request(r, 10, L.total, OUT_BYTES); + CHECK(rs.result == 0 && rs.nxout == 2, "REV%d: RequestUpdateAuto answers 0 and places its two C buffers (%u)", + rev, rs.nxout); + CHECK(once_lines == 0 && r->bad_count == 0 && r->refused == 0, "REV%d: nothing logged or counted (%s)", rev, + last_once); + close_renderer(r); + } +} + +/* 2. Updates whose framing does not match the renderer: refused whole. */ +enum { + R_BUFFERS, R_SMALL, R_TOTAL_BIG, R_TOTAL_SMALL, R_BEHAVIOR, R_POOLS, R_CHANNELS, R_VOICES, R_VOICES_AUTO, R_EFFECTS, + R_SINKS, R_OVERRUN, R_UNCONSUMED, R_OUTPUT, R_SPL_HEADER, R_SPL_MAGIC, R_SPL_INFO_OVERRUN, R_SPL_INFO, R_SPL_WRAP, + R_SPL_PADDING, R_DEST_OVERRUN, R_DEST_MAGIC, R_SPL_SIZE, R_MIX_DIRTY, R_MIX_SIZE, R_LAYOUT_NEGATIVE, + R_LAYOUT_TOO_MANY, R_ROWS +}; +static const struct { const char *name; int kind; } refuse_rows[R_ROWS] = { + [R_BUFFERS] = {"an empty input buffer", AB_UPD_BUFFERS}, + [R_SMALL] = {"an input shorter than the header", AB_UPD_SMALL}, + [R_TOTAL_BIG] = {"a total past the input", AB_UPD_TOTAL}, + [R_TOTAL_SMALL] = {"a total under the header", AB_UPD_TOTAL}, + [R_BEHAVIOR] = {"a behavior section of 0x20 bytes", AB_UPD_BEHAVIOR}, + [R_POOLS] = {"a memory pool section one pool short", AB_UPD_POOLS}, + [R_CHANNELS] = {"a voice channel section of part of a record", AB_UPD_CHANNELS}, + [R_VOICES] = {"a voice section 0x10 bytes short", AB_UPD_VOICES}, + [R_VOICES_AUTO] = {"the same through RequestUpdateAuto", AB_UPD_VOICES}, + [R_EFFECTS] = {"an effect section one effect long", AB_UPD_EFFECTS}, + [R_SINKS] = {"an empty sink section", AB_UPD_SINKS}, + [R_OVERRUN] = {"a render info section past the total", AB_UPD_OVERRUN}, + [R_UNCONSUMED] = {"0x10 bytes after the last section", AB_UPD_UNCONSUMED}, + [R_OUTPUT] = {"an output buffer of 0x100 bytes", AB_UPD_OUTPUT}, + [R_SPL_HEADER] = {"a splitter section of 0x10 bytes", AB_SPL_HEADER}, + [R_SPL_MAGIC] = {"a splitter header that is not SNDH", AB_SPL_MAGIC}, + [R_SPL_INFO_OVERRUN] = {"a splitter info past the section", AB_SPL_INFO_OVERRUN}, + [R_SPL_INFO] = {"a splitter info that is not SNDI", AB_SPL_INFO}, + [R_SPL_WRAP] = {"a splitter info of 0x40000000 ids (4 * count wraps in 32 bits)", AB_SPL_INFO}, + [R_SPL_PADDING] = {"a splitter info's reserved bytes past the section", AB_SPL_PADDING}, + [R_DEST_OVERRUN] = {"a third destination record past the section", AB_DEST_OVERRUN}, + [R_DEST_MAGIC] = {"a destination record that is not SNDD", AB_DEST_MAGIC}, + [R_SPL_SIZE] = {"32 bytes after the splitter records", AB_SPL_SIZE}, + [R_MIX_DIRTY] = {"a changed-mix header of 2 mixes with 1 record", AB_MIX_DIRTY}, + [R_MIX_SIZE] = {"a mix section neither full nor changed-only", AB_MIX_SIZE}, + [R_LAYOUT_NEGATIVE] = {"a submix in use with -6 buffers", AB_MIX_LAYOUT}, + [R_LAYOUT_TOO_MANY] = {"mixes in use with 6 + 6 buffers of 8", AB_MIX_LAYOUT}, +}; +static IpcResponse refused_request(AudioRenderer *r, int row) { + Update u; + update_default(&u, 11); + u.split_bytes = splitter_section(u.split, 11, 0, 2, both_ids, 2, both_dests); /* 0x130 bytes, records to 0x124 */ + unsigned cmd = row == R_VOICES_AUTO ? 10 : 4; + uint64_t out_size = OUT_BYTES; + uint32_t word; + switch (row) { + case R_SPL_HEADER: u.split_bytes = 0x10; break; + case R_SPL_MAGIC: u.split[0] = 'X'; break; + case R_SPL_INFO_OVERRUN: /* one info record announced, 8 bytes of it in the section */ + splitter_section(u.split, 11, 0, 0, NULL, 0, NULL); + u.split_bytes = 0x28; + break; + case R_SPL_INFO: u.split[0x20] = 'X'; break; + case R_SPL_WRAP: word = 0x40000000u, memcpy(u.split + 0x2c, &word, 4); break; + case R_SPL_PADDING: /* the id fits, 4 of its 12 reserved bytes do */ + splitter_section(u.split, 11, 0, 1, both_ids, 0, NULL); + u.split_bytes = 0x38; + break; + case R_DEST_OVERRUN: u.split[8] = 3; break; + case R_DEST_MAGIC: u.split[0x44] = 'X'; break; + case R_SPL_SIZE: u.split_bytes += 32; break; + case R_MIX_DIRTY: + word = 0xcafebabeu, memcpy(u.mix_raw, &word, 4), word = 2, memcpy(u.mix_raw + 4, &word, 4); + memcpy(u.mix_raw + 0x20, &u.mix[1], sizeof u.mix[1]); + u.mix_raw_bytes = 0x20 + sizeof(AudrenMixIn); + break; + case R_MIX_SIZE: + memcpy(u.mix_raw, u.mix, sizeof u.mix); + u.mix_raw_bytes = sizeof u.mix + 0x10; + break; + case R_LAYOUT_NEGATIVE: u.mix[1].buffer_count = -6; break; + case R_LAYOUT_TOO_MANY: u.mix[0].buffer_count = u.mix[1].buffer_count = 6; break; + } + uint64_t size = build(&u); + AudrenHeader *h = header(); + switch (row) { + case R_BUFFERS: size = 0; break; + case R_SMALL: size = 0x20; break; + case R_TOTAL_BIG: h->total += 0x10; break; + case R_TOTAL_SMALL: h->total = 0x20; break; + case R_BEHAVIOR: h->behavior = 0x20; break; + case R_POOLS: h->memory_pools -= sizeof(AudrenPoolIn); break; + case R_CHANNELS: h->voice_resources += 8; break; + case R_VOICES: case R_VOICES_AUTO: h->voices -= 0x10, h->total -= 0x10, size -= 0x10; break; + case R_EFFECTS: h->effects = sizeof(AudrenEffectIn); break; + case R_SINKS: h->sinks = 0; break; + case R_OVERRUN: h->render_info = 0x100; break; + case R_UNCONSUMED: h->total += 0x10, size += 0x10; break; + case R_OUTPUT: out_size = 0x100; break; + } + return request(r, cmd, size, out_size); +} +static void refused_updates(void) { + for (int row = 0; row < R_ROWS; row++) { + const char *name = refuse_rows[row].name; + AudioRenderer *r = open_renderer(); + Update u; + update_default(&u, 11); + u.split_bytes = splitter_section(u.split, 11, 0, 2, both_ids, 2, both_dests); + CHECK(apply(r, &u) == 0, "%s: the update before it is taken", name); + pass(r); + const int lines = once_lines; + for (int n = 1; n <= 2; n++) { + Snap before, after; + snap(r, &before); + memset(out, 0xa5, OUT_BYTES); + IpcResponse rs = refused_request(r, row); + snap(r, &after); + CHECK(rs.result == 0x5299, "%s: answered %#x, not 0x5299", name, rs.result); + CHECK(out_untouched(), "%s: the output buffer was written", name); + CHECK(!memcmp(&before, &after, sizeof before), "%s: the renderer's state changed", name); + CHECK(rs.nxout == 0, "%s: %u C buffers placed for a refused update", name, rs.nxout); + CHECK(r->bad_seen[refuse_rows[row].kind] && kinds_seen(r) == 1, "%s: logged as kind %d (%d kinds)", name, + refuse_rows[row].kind, kinds_seen(r)); + CHECK(once_lines - lines == 1, "%s: %d log lines after send %d (one, on the first)", name, + once_lines - lines, n); + CHECK(r->refused == (uint64_t)n && r->refused_run == (uint64_t)n && r->bad_count == (uint64_t)n, + "%s: refused %llu, in a row %llu, counted %llu after send %d", name, (unsigned long long)r->refused, + (unsigned long long)r->refused_run, (unsigned long long)r->bad_count, n); + } + pass(r); + CHECK(flat(buf(r, 0), 0.5f) && flat(buf(r, 1), 0), "%s: the renderer goes on with the update before", name); + update_default(&u, 11); + CHECK(apply(r, &u) == 0 && r->refused_run == 0, "%s: the next valid update is taken (in a row %llu)", name, + (unsigned long long)r->refused_run); + close_renderer(r); + } +} + +/* 3. One record the renderer cannot use: left out, the rest of the update applied. */ +static int left_out(AudioRenderer *r, int lines, int kind) { + return once_lines - lines == 1 && r->bad_seen[kind] && kinds_seen(r) == 1 && r->refused == 0; +} +static void left_out_records(void) { + Update u; + int lines; + + AudioRenderer *r = open_renderer(); /* channel resource id 12 of 12 next to a valid record */ + update_default(&u, 11); + apply(r, &u); + lines = once_lines; + u.chan[1] = u.chan[0], u.chan[1].mix[0] = 0.25f, u.chan[0].id = 12, u.chan[1].id = 1, u.nchan = 2; + CHECK(apply(r, &u) == 0 && r->channels[1].mix[0] == 0.25f, "a valid channel resource next to id 12 is applied"); + CHECK(left_out(r, lines, AB_CHANNEL_ID), "channel resource id 12 is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* an in-use voice with id 7 of 2 next to a valid voice */ + update_default(&u, 11); + apply(r, &u); + pass(r); + lines = once_lines; + ServerVoice was = r->voices[1]; + u.voice[0].volume = 0.75f; + u.voice[1] = u.voice[0], u.voice[1].id = 7; + memset(out, 0xa5, OUT_BYTES); + CHECK(apply(r, &u) == 0 && r->voices[0].in.volume == 0.75f, "the valid voice next to voice id 7 is applied"); + CHECK(!memcmp(&was, &r->voices[1], sizeof was), "slot 1's server voice is untouched"); + const uint8_t *vo = out + sizeof(AudrenHeader) + (EFFECTS + 4 * VOICES) * sizeof(AudrenPoolOut); + CHECK(vo[0] != 0xa5 && vo[sizeof(AudrenVoiceOut)] != 0xa5, "the voice status is written for both slots"); + CHECK(left_out(r, lines, AB_VOICE_ID), "voice id 7 is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* a new voice whose current wave buffer is 4 of 4 */ + update_default(&u, 11); + apply(r, &u); + pass(r); + lines = once_lines; + was = r->voices[0]; + u.voice[0].is_new = 1, u.voice[0].wave_buffer_index = 4, u.voice[0].volume = 0.25f; + CHECK(apply(r, &u) == 0 && !memcmp(&was, &r->voices[0], sizeof was), "the voice is left as it was (not reset)"); + CHECK(left_out(r, lines, AB_WAVE_INDEX), "wave buffer index 4 is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* REV15: a voice filter with a NaN coefficient, next to a valid one */ + update_default(&u, 15); + apply(r, &u); + lines = once_lines; + u.vfilter[0][0] = (AudrenBiquadFloat){.enable = 1, .numerator = {NAN, 0, 0}}; + u.vfilter[0][1] = (AudrenBiquadFloat){.enable = 1, .numerator = {1, 0, 0}}; + CHECK(apply(r, &u) == 0, "an update with a NaN voice filter is taken"); + const ServerVoice *v = &r->voices[0]; + CHECK(!v->in.biquads[0].enable && !v->float_biquads[0].enable, "the NaN filter is off"); + CHECK(v->in.biquads[1].enable && v->float_biquads[1].enable && v->use_float_biquads, "the other filter stays on"); + for (int i = 0; i < 10; i++) pass(r); + CHECK(finite_buffers(r) && flat(buf(r, 0), 0.5f), "10 passes leave every mix buffer finite (%g)", buf(r, 0)[0]); + CHECK(left_out(r, lines, AB_VOICE_FILTER), "the NaN filter is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* splitter info naming splitter 3 of 1 */ + update_default(&u, 11); + u.split_bytes = splitter_section(u.split, 11, 0, 2, both_ids, 2, both_dests); + apply(r, &u); + lines = once_lines; + u.split_bytes = splitter_section(u.split, 11, 3, 2, both_ids, 0, NULL); + CHECK(apply(r, &u) == 0 && split_is(r, 0, 1), "splitter info 3 is left out, splitter 0 keeps its destinations"); + CHECK(left_out(r, lines, AB_SPL_ID), "splitter 3 is logged once (%s)", last_once); + close_renderer(r); + + /* a splitter listing destination 5 of 2: stored as -1, which both render loops skip. Destination 0 would put the + sound into the submix's first buffer (the mixer's 2), destination 1 goes to final mix buffer 1. The destination + table gets a guard entry in front of it, used and into the final mix at full volume, so a loop that reads id -1 + puts the sound into buffers 0 and 1 a second time. */ + static SplitDest guarded[1 + 2]; + for (int via_mix = 0; via_mix < 2; via_mix++) { + r = open_renderer(); + Mixer *m = r->mixer; + SplitDest *table = m->dest; + memset(guarded, 0, sizeof guarded); + guarded[0].used = 1, guarded[0].dest = 0; + for (int b = 0; b < 24; b++) guarded[0].vol[b] = 1; + m->dest = guarded + 1; + update_default(&u, 11); + apply(r, &u); + lines = once_lines; + const int32_t ids[2] = {5, 1}; + Dest d[2] = {both_dests[0], both_dests[1]}; + if (via_mix) { /* voice 1 into the submix (buffers 2-3), the submix through the splitter */ + voice1_on(&u, 1, SPLITTER_UNUSED); + u.mix[1].dest_mix_id = -1, u.mix[1].dest_splitter_id = 0; + } else { /* voice 1 through the splitter */ + voice1_on(&u, 0, 0); + d[0].mix = 1; + } + u.split_bytes = splitter_section(u.split, 11, 0, 2, ids, 2, d); + CHECK(apply(r, &u) == 0 && split_is(r, -1, 1), "%s: destination 5 is stored as -1, destination 1 kept", + via_mix ? "a submix" : "a voice"); + for (int i = 0; i < 10; i++) pass(r); + CHECK(flat(buf(r, 0), 0.5f) && flat(buf(r, 1), 0.5f), "%s: through the splitter into buffer 1 only (%g %g)", + via_mix ? "a submix" : "a voice", buf(r, 0)[0], buf(r, 1)[0]); + if (!via_mix) CHECK(flat(buf(r, 2), 0), "a voice: nothing through the destination left out (%g)", buf(r, 2)[0]); + CHECK(left_out(r, lines, AB_SPL_DEST_REF), "destination 5 is logged once (%s)", last_once); + m->dest = table; /* the mixer frees its own table */ + close_renderer(r); + } + + r = open_renderer(); /* a destination record naming destination 9 of 2, next to a valid one */ + update_default(&u, 11); + u.split_bytes = splitter_section(u.split, 11, 0, 2, both_ids, 2, both_dests); + apply(r, &u); + lines = once_lines; + SplitDest dest0 = ((Mixer *)r->mixer)->dest[0]; + Dest d9[2] = {both_dests[0], both_dests[1]}; + d9[0].id = 9, d9[0].volume = 0.25f, d9[1].volume = 0.5f; + u.split_bytes = splitter_section(u.split, 11, 0, -1, NULL, 2, d9); + CHECK(apply(r, &u) == 0 && ((Mixer *)r->mixer)->dest[1].vol[1] == 0.5f, "the valid destination record is applied"); + CHECK(!memcmp(&dest0, &((Mixer *)r->mixer)->dest[0], sizeof dest0), "destination 0 is untouched"); + CHECK(left_out(r, lines, AB_DEST_ID), "destination 9 is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* REV15: a destination filter with an infinite denominator */ + update_default(&u, 15); + apply(r, &u); + lines = once_lines; + Dest df[2] = {both_dests[0], both_dests[1]}; + df[0].filter[0] = (AudrenBiquadFloat){.enable = 1, .numerator = {1, 0, 0}, .denominator = {INFINITY, 0}}; + df[0].filter[1] = (AudrenBiquadFloat){.enable = 1, .numerator = {1, 0, 0}}; + u.split_bytes = splitter_section(u.split, 15, 0, 2, both_ids, 2, df); + voice1_on(&u, 0, 0); + CHECK(apply(r, &u) == 0, "an update with an infinite destination filter is taken"); + const SplitDest *sd = &((Mixer *)r->mixer)->dest[0]; + CHECK(!sd->biquads[0].enable && sd->biquads[1].enable, "that filter is off, the other stays on"); + for (int i = 0; i < 10; i++) pass(r); + CHECK(finite_buffers(r), "10 passes leave every mix buffer finite"); + CHECK(left_out(r, lines, AB_DEST_FILTER), "the infinite filter is logged once (%s)", last_once); + close_renderer(r); + + r = open_renderer(); /* a changed-mix record for mix 5 of 2 next to a valid one */ + update_default(&u, 11); + apply(r, &u); + lines = once_lines; + uint32_t word = 0xcafebabeu; + memcpy(u.mix_raw, &word, 4); + word = 2; + memcpy(u.mix_raw + 4, &word, 4); + AudrenMixIn bad = u.mix[1], good = u.mix[1]; + bad.mix_id = 5, good.volume = 0.5f; + memcpy(u.mix_raw + 0x20, &bad, sizeof bad); + memcpy(u.mix_raw + 0x20 + sizeof bad, &good, sizeof good); + u.mix_raw_bytes = 0x20 + 2 * sizeof(AudrenMixIn); + CHECK(apply(r, &u) == 0 && r->mixes[1].volume == 0.5f, "the valid changed mix next to mix 5 is applied"); + CHECK(left_out(r, lines, AB_MIX_ID), "mix 5 is logged once (%s)", last_once); + close_renderer(r); +} + +/* 4. Effects and sinks the renderer cannot use: passed through or skipped in each render pass. */ +static void rings_fill(void) { + memset(ring_send, 0, 128), memset(ring_ret, 0, 128); + for (int i = 32; i < 32 + 4096; i++) ring_send[i] = ring_ret[i] = 0x1234; + for (unsigned i = 0; i < SINK_SAMPLES; i++) sink_data[i] = 0x5a5a; +} +static int rings_untouched(void) { + int32_t head[32] = {0}; + if (memcmp(ring_send, head, 128) || memcmp(ring_ret, head, 128)) return 0; + for (int i = 32; i < 32 + 4096; i++) + if (ring_send[i] != 0x1234 || ring_ret[i] != 0x1234) return 0; + for (unsigned i = 0; i < SINK_SAMPLES; i++) + if (sink_data[i] != 0x5a5a) return 0; + return 1; +} + +enum { PASS, SKIP }; /* what the render does with it: buffer 1 holds buffer 0's 0.5, or stays 0 */ +enum { AT_UPDATE, EVERY_PASS }; /* where it is found: once per update, or in each render pass */ +enum { E_NONE, E_BAD, E_NO_DSP, E_NO_DELAY, E_NO_REVERB }; +typedef struct { const char *name; uint8_t type; int kind, render, found, state; } EffectRow; +static const EffectRow effect_rows[] = { + {"limiter attack 2", 7, AB_LIMITER_PARAMS, PASS, EVERY_PASS, E_NONE}, + {"limiter release NaN", 7, AB_LIMITER_PARAMS, PASS, EVERY_PASS, E_NONE}, + {"limiter delay 0", 7, AB_LIMITER_PARAMS, PASS, EVERY_PASS, E_NONE}, + {"limiter delay above the longest", 7, AB_LIMITER_PARAMS, PASS, EVERY_PASS, E_NONE}, + {"limiter longest delay 0", 7, AB_LIMITER_SIZE, PASS, AT_UPDATE, E_NO_DELAY}, + {"limiter longest delay 200000", 7, AB_LIMITER_SIZE, PASS, AT_UPDATE, E_NO_DELAY}, + {"limiter 7 channels", 7, AB_LIMITER_CHANNELS, SKIP, EVERY_PASS, E_NO_DELAY}, + {"limiter input 9", 7, AB_LIMITER_INDEX, SKIP, EVERY_PASS, E_NONE}, + {"delay rate 0", 3, AB_DELAY_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"delay above the longest", 3, AB_DELAY_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"delay 3 channels", 3, AB_DELAY_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"reverb early mode 5", 4, AB_REVERB_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"reverb decay 0", 4, AB_REVERB_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"compressor ratio 0", 9, AB_COMPRESSOR_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"compressor attack 2", 9, AB_COMPRESSOR_PARAMS, PASS, AT_UPDATE, E_NO_DSP}, + {"reverb3d delays of 0.5 s", 5, AB_R3D_PARAMS, PASS, AT_UPDATE, E_NO_REVERB}, + {"reverb3d decay time 0", 5, AB_R3D_PARAMS, PASS, AT_UPDATE, E_NO_REVERB}, + {"reverb3d density 101", 5, AB_R3D_PARAMS, PASS, AT_UPDATE, E_NO_REVERB}, + {"reverb3d 3 channels", 5, AB_R3D_CHANNELS, SKIP, EVERY_PASS, E_NO_REVERB}, + {"reverb3d input 9", 5, AB_R3D_INDEX, SKIP, EVERY_PASS, E_NO_REVERB}, + {"biquad 0 channels", 6, AB_DSP_CHANNELS, SKIP, EVERY_PASS, E_NONE}, + {"biquad 7 channels", 6, AB_DSP_CHANNELS, SKIP, EVERY_PASS, E_NONE}, + {"delay input 9", 3, AB_DSP_INDEX, SKIP, EVERY_PASS, E_NONE}, + {"buffer-mix of 25 pairs", 1, AB_BUFMIX_COUNT, SKIP, EVERY_PASS, E_NONE}, + {"buffer-mix input 9", 1, AB_BUFMIX_INDEX, SKIP, EVERY_PASS, E_NONE}, + {"aux 25 channels", 2, AB_AUX_SIZE, SKIP, EVERY_PASS, E_NONE}, + {"aux ring of 16 Mi + 1", 2, AB_AUX_SIZE, SKIP, EVERY_PASS, E_NONE}, + {"aux ring at 1 << 40", 2, AB_AUX_ADDRESS, SKIP, EVERY_PASS, E_NONE}, + {"aux input 9", 2, AB_AUX_INDEX, SKIP, EVERY_PASS, E_NONE}, + {"capture 25 channels", 8, AB_CAPTURE_SIZE, SKIP, EVERY_PASS, E_NONE}, + {"capture ring of 16 Mi + 1", 8, AB_CAPTURE_SIZE, SKIP, EVERY_PASS, E_NONE}, + {"capture ring at 1 << 40", 8, AB_CAPTURE_ADDRESS, SKIP, EVERY_PASS, E_NONE}, + {"capture input 9", 8, AB_CAPTURE_INDEX, SKIP, EVERY_PASS, E_NONE}, + {"circular sink PCM8", 0, AB_SINK_PARAMS, SKIP, EVERY_PASS, E_NONE}, + {"circular sink of an odd size", 0, AB_SINK_PARAMS, SKIP, EVERY_PASS, E_NONE}, + {"circular sink of 0 channels", 0, AB_SINK_PARAMS, SKIP, EVERY_PASS, E_NONE}, + {"circular sink at 1 << 40", 0, AB_SINK_ADDRESS, SKIP, EVERY_PASS, E_NONE}, + {"circular sink input 200", 0, AB_SINK_INDEX, SKIP, EVERY_PASS, E_NONE}, +}; +#define EFFECT_ROWS ((int)(sizeof effect_rows / sizeof *effect_rows)) + +/* Effect slot 0 on the final mix from buffer 0 to buffer 1 (type 0: the sink instead), valid or as row `row` has it. */ +static void effect_set(Update *u, int row, int bad) { + const char *n = effect_rows[row].name; + const uint8_t type = effect_rows[row].type; +#define IS(s) (bad && !strcmp(n, s)) + AudrenEffectIn *e = &u->effect[0]; + *e = (AudrenEffectIn){.type = type, .is_new = 1, .enabled = 1, .mix_id = 0, .order = 0}; + if (type == 7) { + AudrenLimiterParam p = {.input = {0}, .output = {1}, .channel_max = 1, .channels = 1, .sample_rate = 48000, + .attack = 0.5f, .release = 0.5f, .threshold = 1, .input_gain = 1, .output_gain = 1, + .delay_min = 1, .delay_max = 48}; + if (IS("limiter attack 2")) p.attack = 2; + if (IS("limiter release NaN")) p.release = NAN; + if (IS("limiter delay 0")) p.delay_min = 0; + if (IS("limiter delay above the longest")) p.delay_min = 49; + if (IS("limiter longest delay 0")) p.delay_max = 0; + if (IS("limiter longest delay 200000")) p.delay_max = 200000; + if (IS("limiter 7 channels")) p.channels = p.channel_max = 7; + if (IS("limiter input 9")) p.input[0] = 9; + memcpy(e->specific, &p, sizeof p); + } else if (type == 3) { + AudrenDelayParam p = {.input = {0, 0, 0}, .output = {1, 1, 1}, .channel_max = 1, .channels = 1, + .delay_max = 100, .delay = 50, .sample_rate = 48000u << 14, .in_gain = 1 << 14, + .feedback = 1 << 13, .out_gain = 1 << 14, .dry_gain = 1 << 14, .lowpass = 1 << 13}; + if (IS("delay rate 0")) p.sample_rate = 0; + if (IS("delay above the longest")) p.delay = 101; + if (IS("delay 3 channels")) p.channels = p.channel_max = 3; + if (IS("delay input 9")) p.input[0] = 9; + memcpy(e->specific, &p, sizeof p); + } else if (type == 4) { + AudrenReverbParam p = {.input = {0}, .output = {1}, .channel_max = 1, .channels = 1, .sample_rate = 48 << 14, + .early_gain = 1 << 14, .late_gain = 1 << 14, .decay = 1 << 14, .hf_decay = 1 << 14, + .reverb_gain = 1 << 14, .out_gain = 1 << 14, .dry_gain = 1 << 14}; + if (IS("reverb early mode 5")) p.early_mode = 5; + if (IS("reverb decay 0")) p.decay = 0; + memcpy(e->specific, &p, sizeof p); + } else if (type == 9) { + AudrenCompressorParam p = {.input = {0}, .output = {1}, .channel_max = 1, .channels = 1, .sample_rate = 48000, + .threshold = -10, .ratio = 2, .input_gain = 0.5f, .attack = 0.5f, .release = 0.5f}; + if (IS("compressor ratio 0")) p.ratio = 0; + if (IS("compressor attack 2")) p.attack = 2; + memcpy(e->specific, &p, sizeof p); + } else if (type == 5) { + Reverb3dParam p = {.input = {0}, .output = {1}, .channel_max = 1, .channels = 1, .sample_rate = 48000, + .hf_reference = 5000, .decay_time = 1, .hf_decay_ratio = 0.5f, .diffusion = 50, + .reflection_delay = 0.05f, .reverb_delay = 0.05f, .density = 50, .dry_gain = 1}; + if (IS("reverb3d delays of 0.5 s")) p.reflection_delay = 0.3f, p.reverb_delay = 0.2f; + if (IS("reverb3d decay time 0")) p.decay_time = 0; + if (IS("reverb3d density 101")) p.density = 101; + if (IS("reverb3d 3 channels")) p.channels = p.channel_max = 3; + if (IS("reverb3d input 9")) p.input[0] = 9; + memcpy(e->specific, &p, sizeof p); + } else if (type == 6) { + AudrenBiquadEffectV1 p = {.input = {0}, .output = {1}, .numerator = {1 << 14}, .channels = 1, .status = 1}; + if (IS("biquad 0 channels")) p.channels = 0; + if (IS("biquad 7 channels")) p.channels = 7; + memcpy(e->specific, &p, sizeof p); + } else if (type == 1) { + AudrenBufferMixParam p = {.input = {0}, .output = {1}, .volume = {1}, .count = 1}; + if (IS("buffer-mix of 25 pairs")) p.count = 25; + if (IS("buffer-mix input 9")) p.input[0] = 9; + memcpy(e->specific, &p, sizeof p); + } else if (type == 2 || type == 8) { + AudrenAuxParam p = {.input = {0}, .output = {1}, .channels = 1, .sample_rate = 48000, .capacity = 4096, + .channel_max = 1, .send_info = (uintptr_t)ring_send, .return_info = (uintptr_t)ring_ret}; + if (IS("aux 25 channels") || IS("capture 25 channels")) p.channels = 25; + if (IS("aux ring of 16 Mi + 1") || IS("capture ring of 16 Mi + 1")) p.capacity = 16 * 1024 * 1024 + 1; + if (IS("aux ring at 1 << 40") || IS("capture ring at 1 << 40")) p.send_info = p.return_info = 1ull << 40; + if (IS("aux input 9") || IS("capture input 9")) p.input[0] = 9; + memcpy(e->specific, &p, sizeof p); + } else { /* the circular sink in sink slot 0, instead of the device sink */ + *e = (AudrenEffectIn){0}; + CircularSinkParam p = {.address = (uintptr_t)sink_data, .size = 2 * SINK_SAMPLES, .channels = 1, + .format = FMT_PCM16, .input = {0}}; + if (IS("circular sink PCM8")) p.format = FMT_PCM8; + if (IS("circular sink of an odd size")) p.size = 2 * SINK_SAMPLES - 1; + if (IS("circular sink of 0 channels")) p.channels = 0; + if (IS("circular sink at 1 << 40")) p.address = 1ull << 40; + if (IS("circular sink input 200")) p.input[0] = 200; + u->sink = (AudrenSinkIn){.type = SINK_CIRCULAR, .in_use = 1}; + memcpy(u->sink.name, &p, sizeof p); + } +#undef IS +} + +/* What a valid effect of row `row`'s type leaves after a pass: its DSP, delay line or reverb, or its output. */ +static int effect_runs(AudioRenderer *r, int row) { + const EffectState *s = &((Mixer *)r->mixer)->effect[0]; + switch (effect_rows[row].type) { + case 7: return s->delay != NULL; + case 3: case 4: case 9: return s->dsp != NULL; + case 5: return s->reverb != NULL; + case 6: case 1: return flat(buf(r, 1), 0.5f); + case 2: case 8: return ring_send[32] != 0x1234; + default: return sink_data[0] != 0x5a5a; + } +} + +static void effects_left_out(void) { + for (int row = 0; row < EFFECT_ROWS; row++) { + const EffectRow *x = &effect_rows[row]; + AudioRenderer *r = open_renderer(); + Update u; + update_default(&u, 11); + apply(r, &u); + rings_fill(); + const int lines = once_lines; + effect_set(&u, row, 1); + CHECK(apply(r, &u) == 0, "%s: the update is taken", x->name); + for (int i = 0; i < 10; i++) pass(r); + const EffectState *s = &((Mixer *)r->mixer)->effect[0]; + if (x->render == PASS) + CHECK(flat(buf(r, 1), 0.5f) && flat(buf(r, 0), 0.5f), "%s: its sound passes through (%g)", x->name, + buf(r, 1)[0]); + else + CHECK(flat(buf(r, 1), 0) && rings_untouched(), "%s: skipped, nothing written (%g)", x->name, buf(r, 1)[0]); + CHECK(x->state != E_NO_DSP || (s->bad && !s->dsp), "%s: marked bad, no DSP", x->name); + CHECK(x->state != E_NO_DELAY || !s->delay, "%s: no delay line", x->name); + CHECK(x->state != E_NO_REVERB || !s->reverb, "%s: no reverb", x->name); + /* the values found at the update mark the effect bad; those found in a pass leave it as it is */ + CHECK(s->bad == (x->found == AT_UPDATE), "%s: bad is %d", x->name, s->bad); + CHECK(once_lines - lines == 1 && r->bad_seen[x->kind] && kinds_seen(r) == 1, + "%s: %d lines over the update and 10 passes, kind %d seen %d, %d kinds (%s)", x->name, once_lines - lines, + x->kind, r->bad_seen[x->kind], kinds_seen(r), last_once); + CHECK(r->bad_count == (x->found == AT_UPDATE ? 1u : 10u), "%s: %llu values counted", x->name, + (unsigned long long)r->bad_count); + CHECK(finite_buffers(r), "%s: every mix buffer finite", x->name); + /* the same slot with usable values: the effect runs again */ + const uint64_t counted = r->bad_count; + rings_fill(); + effect_set(&u, row, 0); + u.effect[0].is_new = 0; + CHECK(apply(r, &u) == 0, "%s: the valid update is taken", x->name); + pass(r); + CHECK(!s->bad && effect_runs(r, row) && r->bad_count == counted, "%s: with usable values it runs again", + x->name); + close_renderer(r); + } +} + +/* 5. Valid effects and sinks over 20 passes: nothing left out, every buffer finite. */ +static void in_range_quiet(void) { + uint8_t done[16] = {0}; /* by type: one valid block of each */ + for (int row = 0; row < EFFECT_ROWS; row++) { + if (done[effect_rows[row].type]) continue; + done[effect_rows[row].type] = 1; + AudioRenderer *r = open_renderer(); + Update u; + update_default(&u, 11); + rings_fill(); + effect_set(&u, row, 0); + CHECK(apply(r, &u) == 0, "type %u: taken", effect_rows[row].type); + for (int i = 0; i < 20; i++) pass(r); + CHECK(r->bad_count == 0 && finite_buffers(r) && effect_runs(r, row), "type %u: %llu values left out", + effect_rows[row].type, (unsigned long long)r->bad_count); + close_renderer(r); + } +} + +/* 6. NX2NAVI_AUDIO_STRICT=1: such a value ends the process, as before 0.10.2 (tests/audio_params_check.py runs these + in their own processes and expects exit 2). */ +static int strict(const char *what) { + strict_setting = 1; + AudioRenderer *r = open_renderer(); + Update u; + update_default(&u, 11); + if (!strcmp(what, "strict-effect")) { + effect_set(&u, 0, 1); /* limiter attack 2 */ + apply(r, &u); + pass(r); + } else { + build(&u); + header()->voices -= 0x10, header()->total -= 0x10; + request(r, 4, L.total - 0x10, OUT_BYTES); + } + printf("audio_params_check %s: the process went on\n", what); + return 1; +} + +int main(int argc, char **argv) { + uint8_t *low = low_alloc(IN_BYTES + OUT_BYTES + 2 * (32 + 4096) * 4 + 2 * SINK_SAMPLES); + in = low, out = in + IN_BYTES; + ring_send = (int32_t *)(out + OUT_BYTES), ring_ret = ring_send + 32 + 4096; + sink_data = (int16_t *)(ring_ret + 32 + 4096); + if (argc > 1 && !strcmp(argv[1], "-v")) verbose = 1; + else if (argc > 1) return strict(argv[1]); + valid_update(); + refused_updates(); + left_out_records(); + effects_left_out(); + in_range_quiet(); + printf("audio_params_check: %s (%d refused updates, %d left-out records, %d effects and sinks)\n", + fails ? "FAIL" : "ok", R_ROWS, 10, EFFECT_ROWS); + return fails ? 1 : 0; +} diff --git a/tests/audio_params_check.py b/tests/audio_params_check.py new file mode 100644 index 00000000..cd00f8cb --- /dev/null +++ b/tests/audio_params_check.py @@ -0,0 +1,59 @@ +"""Build and run tests/audio_params_check.c: runtime/audren.c and runtime/audio_mixer.c with the rest of the runtime +stubbed and the real effect DSPs (runtime/audio_effects.c, runtime/audio_reverb.c) linked. It sends RequestUpdates the +renderer cannot use: refused ones must answer 0x5299 and change nothing, records, effects and sinks are left out of +updates that are otherwise applied, each kind is logged once, and with NX2NAVI_AUDIO_STRICT=1 such a value ends the +process (two runs of their own, which must exit 2 with "FATAL: audio:"). No game files and no audio device. + +Windows: python tests\\audio_params_check.py [--toolchain C:/msys64/ucrt64] [-v] +Linux: python tests/audio_params_check.py (clang and SDL3 development files via pkg-config) +""" +import argparse +import os +import shlex +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS = Path(r'C:\msys64\ucrt64') + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument('--toolchain', type=Path, default=MSYS if os.name == 'nt' and MSYS.exists() else None) + ap.add_argument('-v', action='store_true', help='print the log lines of the checks') + a = ap.parse_args() + work = ROOT / 'build' / 'test' / 'audio_params' + work.mkdir(parents=True, exist_ok=True) + env = dict(os.environ) + if a.toolchain: + env['PATH'] = str(a.toolchain / 'bin') + os.pathsep + env.get('PATH', '') + cc = os.environ.get('CC') or str(a.toolchain / 'bin' / 'clang.exe') + sdl = ['-L', str(a.toolchain / 'lib'), '-lSDL3'] + else: + cc = os.environ.get('CC') or shutil.which('clang') or 'cc' + sdl = shlex.split(subprocess.check_output(['pkg-config', '--cflags', '--libs', 'sdl3'], text=True)) + exe = work / ('audio_params_check.exe' if os.name == 'nt' else 'audio_params_check') + # three translation units: both effect files have a static gain10; the default build's target (x86-64-v3) + subprocess.check_call([cc, '-std=gnu11', '-O2', '-fno-strict-aliasing', '-march=x86-64-v3', '-Wall', + '-Wno-unused-function', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'audio_params_check.c'), str(ROOT / 'runtime' / 'audio_effects.c'), + str(ROOT / 'runtime' / 'audio_reverb.c'), '-o', str(exe), *sdl, '-lm'], env=env) + fails = 0 + p = subprocess.run([str(exe)] + (['-v'] if a.v else []), env=env, timeout=120, capture_output=True, text=True) + sys.stdout.write(p.stdout + p.stderr) + if p.returncode != 0: + fails += 1 + for case in ('strict-effect', 'strict-update'): + p = subprocess.run([str(exe), case], env=env, timeout=60, capture_output=True, text=True) + text = p.stdout + p.stderr + ok = p.returncode == 2 and 'FATAL: audio:' in text and 'NX2NAVI_AUDIO_STRICT=1' in text + print(f'audio_params_check {case}: {"ok" if ok else "FAIL"} (exit {p.returncode}: {text.strip()[-200:]})') + fails += not ok + print('audio_params_check.py:', 'FAIL' if fails else 'ok') + return 1 if fails else 0 + + +if __name__ == '__main__': + sys.exit(main())