mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 08:39:57 -04:00
Refuse or leave out the audio values the game sends that the renderer cannot use, instead of ending the game on them.
The audio renderer checked the game's RequestUpdate with about 80 rt_fatal and need() sites in audren.c, audio_mixer.c, audio_effects.c and audio_reverb.c, so a section size, a splitter or mix record or an effect setting out of range (a reverb3d whose delays add up past 0.4 s, a limiter attack outside 0 to 1, a mix buffer index past its mix, a filter coefficient that is not finite) ended the process. The tested route never reached one; dungeons and bosses were never run. Now an update whose sections do not match the renderer is refused whole with 0x5299 before anything of it is applied: the SDK's RequestUpdateAudioRenderer hands that result back to the game, which ignores it and sends its next update, where any other result makes the SDK abort. A record with an id the renderer does not have is left out of an update that is otherwise applied, a splitter destination id it does not have is stored as -1, which both render loops skip, and an effect or sink whose values cannot be used is passed through or skipped in the render pass, with every index checked before its first write. audio_bad logs the first of each kind per renderer with the game frame and counts the rest. rt_fatal stays for host allocations, OpenAudioRenderer's parameters and the checks the new ones turn into invariants. Two inputs that did not end the process before change too: a mix in use with a negative buffer count is refused (it laid the mixes after it out before buffer 0), and a limiter attack or release that is not a number passes the sound through instead of turning the mix into NaN. NX2NAVI_AUDIO_STRICT=1 (audio_strict, default 0) makes audio_bad end the process as before, for bug reports. Not reproduced before the change in the game; crafted updates reproduced the fatals. The new tests/audio_params_check sends 27 refused updates, 10 records and 38 effects and sinks the renderer cannot use through renderer_handler and runs the strict switch; 15 deliberate breakages of the change each made it fail. audio_mixer_check, cfg_rows_check, check_content, a compile of every runtime file with no new -Wall -Wextra warnings in the changed ones and the stability pass's test matrix (the two baseline environment failures only) passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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_max<p->channels || 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_max<p->channels)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_max<p->channels)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_max<p->channels)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_max<p->channels || 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_max<p->channels || !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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;i<r->param.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<p.channels;c++)
|
||||
if(p.input[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<p.channels;c++) {
|
||||
if(p.input[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<p.channels;c++)
|
||||
if(p.input[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<p.channels;c++) {
|
||||
if(p.input[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;k<m->frame;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-cpu<previous?cpu-1:next;
|
||||
}
|
||||
static void mix_capture(AudioRenderer *r,int mix,const AudrenEffectIn *e) {
|
||||
static void mix_capture(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.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<p.channels;c++)
|
||||
if(p.input[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<p.channels;c++){
|
||||
if(p.input[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;k<m->frame;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<p.count;c++)
|
||||
if(p.input[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<p.count;c++){
|
||||
if(p.input[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;k<m->frame;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;c<channels;c++){
|
||||
unsigned idx=c;
|
||||
if(channels==6 && ((e->type==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;c<channels;c++)if(in[c]!=out[c])memmove(out[c],in[c],(size_t)m->frame*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;c<channels;c++)if(in[c]!=out[c])memmove(out[c],in[c],(size_t)m->frame*sizeof(float));
|
||||
return;
|
||||
}
|
||||
if(e->type==6){
|
||||
for(unsigned c=0;c<channels;c++)for(int k=0;k<m->frame;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;n<count;n++) {
|
||||
int id=m->effect_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<<e->type))) { m->effect_reported|=1u<<e->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;c<p->channels;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;c<p->channels && !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;c<p->channels;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;c<p->channels;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<p.channels;c++)
|
||||
if(p.input[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<p.channels;c++){
|
||||
if(p.input[c]>=m->nbuf)rt_fatal("circular audio sink input index");
|
||||
float *in=mixbuf(m,p.input[c]);
|
||||
for(int k=0;k<m->frame;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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
307
runtime/audren.c
307
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 <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user