diff --git a/runtime/audio_cat.c b/runtime/audio_cat.c new file mode 100644 index 00000000..bdf4ae07 --- /dev/null +++ b/runtime/audio_cat.c @@ -0,0 +1,289 @@ +// Sound categories (music, effects, voices) for the volume_music / volume_sfx / volume_voice settings. +// +// The audio renderer only sees sample memory, so the category is learned where the game loads its sounds: +// - RomFS opens and reads of Sound/Resource/ files (romfs.c romfs_open / romfs_read): the file name gives the +// category (BGM_* music, NV_* and *_Vo_* / *Voice* voices, the rest effects); +// - zstd decodes of BARS banks (ov_zstd.c decode): each bank entry's AMTA name refines the bank's category; +// - Opus decodes (ov_media.c ov_decode): the decoded PCM inherits the category of the packet it came from. +// Two tables answer the mixer (audio_mixer.c voice_category): +// - DSP-ADPCM coefficient tables, by a hash of their 32 bytes, read from each BWAV channel header. The game copies +// these coefficients next to the voice, so a voice's own table names its sound wherever its samples live; +// - guest address ranges of loaded or decoded audio, newest first, for PCM voices (Opus output). +// Everything here runs only while one of the three settings is below 100 (audio_cat_active): at the defaults no table +// is written or read. A setting moved during play classifies what loads from then on; sounds loaded before (an area's +// effect banks, a stream opened earlier) are found when their next stream read lands, otherwise they count as effects. +#include "audio_cat.h" +#include "cfg.h" +#include "host.h" +#include "platform/platform.h" + +#include +#include +#include + +#define RANGE_CAP 4096 // address ranges, a ring: the oldest go first. Lookups scan it, so it stays small +#define COEF_CAP 65536 // coefficient tables, open addressing; a full probe run drops the new table (it counts as SFX) +#define COEF_PROBES 64 + +typedef struct { + uint64_t start, end; + uint8_t cat; +} Range; + +static Range ranges[RANGE_CAP]; +static uint32_t range_head; // ranges written so far; under range_lock +static PlatMutex range_lock = PLAT_MUTEX_INIT; + +typedef struct { + _Atomic uint64_t hash; // 0 = free; a slot is claimed once and never freed + _Atomic uint8_t cat; +} Coef; + +static Coef coefs[COEF_CAP]; + +typedef struct { + uint64_t guest; + uint8_t cat; +} Decoder; + +static Decoder decoders[64]; // the category each Opus decoder produced last; under decoder_lock +static unsigned decoder_next; +static PlatMutex decoder_lock = PLAT_MUTEX_INIT; + +static _Atomic uint64_t coef_count[4], range_count, bars_count; + +int audio_cat_active(void) { + static _Atomic unsigned seen = ~0u; + static _Atomic int on; + unsigned ver = cfg_version(); + if (atomic_load_explicit(&seen, memory_order_acquire) != ver) { + int a = cfg_double("TOTK_VOLUME_MUSIC") < 100 || cfg_double("TOTK_VOLUME_SFX") < 100 || + cfg_double("TOTK_VOLUME_VOICE") < 100; + atomic_store_explicit(&on, a, memory_order_relaxed); + atomic_store_explicit(&seen, ver, memory_order_release); + } + return atomic_load_explicit(&on, memory_order_relaxed); +} + +static int ci_str_has(const char *haystack, const char *needle) { + size_t nlen = strlen(needle); + for (; *haystack; haystack++) + if (strncasecmp(haystack, needle, nlen) == 0) return 1; + return 0; +} + +static int all_zero32(const uint8_t *p) { + for (int i = 0; i < 32; i++) + if (p[i]) return 0; + return 1; +} + +static uint64_t fnv1a32(const uint8_t *p) { + uint64_t h = 14695981039346656037ULL; + for (int i = 0; i < 32; i++) { + h ^= p[i]; + h *= 1099511628211ULL; + } + return h ? h : 1; +} + +// A table seen under several categories keeps music or voice over effects: one sample shared by a voice line and an +// effect bank follows the voice setting. +static void register_coef32(const uint8_t *coef, int cat) { + if (cat <= AUDIO_CAT_UNKNOWN || cat > AUDIO_CAT_VOICE || all_zero32(coef)) return; + uint64_t h = fnv1a32(coef); + for (uint32_t step = 0; step < COEF_PROBES; step++) { + Coef *e = &coefs[((uint32_t)h + step) & (COEF_CAP - 1)]; + uint64_t cur = atomic_load_explicit(&e->hash, memory_order_acquire); + if (cur == 0 && atomic_compare_exchange_strong_explicit(&e->hash, &cur, h, memory_order_acq_rel, + memory_order_acquire)) { + atomic_store_explicit(&e->cat, (uint8_t)cat, memory_order_release); + atomic_fetch_add_explicit(&coef_count[cat], 1, memory_order_relaxed); + return; + } + if (cur == h) { + if (cat != AUDIO_CAT_SFX) atomic_store_explicit(&e->cat, (uint8_t)cat, memory_order_release); + return; + } + } +} + +int audio_cat_lookup_coef(uint64_t extra_addr, uint64_t extra_size) { + if (!extra_addr || extra_size < 32 || extra_addr >= RT_ADDRESS_LIMIT || !mem_guest_readable(extra_addr, 32)) + return AUDIO_CAT_UNKNOWN; + const uint8_t *p = (const uint8_t *)(uintptr_t)extra_addr; + if (all_zero32(p)) return AUDIO_CAT_UNKNOWN; + uint64_t h = fnv1a32(p); + for (uint32_t step = 0; step < COEF_PROBES; step++) { + Coef *e = &coefs[((uint32_t)h + step) & (COEF_CAP - 1)]; + uint64_t cur = atomic_load_explicit(&e->hash, memory_order_acquire); + if (cur == h) return atomic_load_explicit(&e->cat, memory_order_acquire); // 0 while the writer is midway + if (cur == 0) break; + } + return AUDIO_CAT_UNKNOWN; +} + +// BWAV: "BWAV", channel count at 0x0E, then 0x4C-byte channel infos from 0x10: codec u16 (1 = DSP-ADPCM) and the 16 +// ADPCM coefficients at +0x10. +void audio_cat_scan_bwav(const void *data, uint64_t size, int cat) { + if (!data || size < 0x40 || cat <= AUDIO_CAT_UNKNOWN) return; + const uint8_t *b = data; + uint32_t magic; + uint16_t nch; + memcpy(&magic, b, 4); + memcpy(&nch, b + 0x0E, 2); + if (magic != 0x56415742u || nch == 0 || nch > 16) return; + for (uint16_t ch = 0; ch < nch; ch++) { + uint64_t at = 0x10ull + ch * 0x4Cull; + if (at + 0x30 > size) break; + uint16_t codec; + memcpy(&codec, b + at, 2); + if (codec == 1) register_coef32(b + at + 0x10, cat); + } +} + +// Every sound bank and stream of the game is under Sound/Resource/ (Sound/Resource/*.bars.zs, Sound/Resource/Stream/ +// *.bwav); everything else is not audio. Creature and player vocal banks (Bokoblin_Voice0, Player_Voice, *_Vo_*) +// count as voices, as do the NV_* character voice lines. +int audio_cat_from_path(const char *path) { + if (!path || strncmp(path, "Sound/Resource/", 15)) return AUDIO_CAT_UNKNOWN; + const char *name = strrchr(path, '/') + 1; + if (!strncasecmp(name, "nv_", 3) || !strncasecmp(name, "vo_", 3)) return AUDIO_CAT_VOICE; + if (ci_str_has(name, "bgm_")) return AUDIO_CAT_MUSIC; + if (ci_str_has(name, "_voice") || ci_str_has(name, "voice_") || ci_str_has(name, "_vo_") || + ci_str_has(name, "shoutvoice") || ci_str_has(name, "shoutmsg")) + return AUDIO_CAT_VOICE; + return AUDIO_CAT_SFX; +} + +void audio_cat_tag_range(uint64_t addr, uint64_t size, int cat) { + if (!addr || !size || cat <= AUDIO_CAT_UNKNOWN || cat > AUDIO_CAT_VOICE) return; + plat_mutex_lock(&range_lock); + // A stream refills the same buffers: the same range read again only changes its category. + uint32_t depth = range_head < 64 ? range_head : 64; + for (uint32_t k = 0; k < depth; k++) { + Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)]; + if (r->start == addr && r->end == addr + size) { + r->cat = (uint8_t)cat; + plat_mutex_unlock(&range_lock); + return; + } + } + Range *r = &ranges[range_head & (RANGE_CAP - 1)]; + r->start = addr; + r->end = addr + size; + r->cat = (uint8_t)cat; + range_head++; + plat_mutex_unlock(&range_lock); + atomic_fetch_add_explicit(&range_count, 1, memory_order_relaxed); +} + +// The newest range holding addr; newer loads into reused memory win over older ones. +int audio_cat_lookup(uint64_t addr) { + if (!addr) return AUDIO_CAT_UNKNOWN; + int cat = AUDIO_CAT_UNKNOWN; + plat_mutex_lock_shared(&range_lock); + uint32_t n = range_head < RANGE_CAP ? range_head : RANGE_CAP; + for (uint32_t k = 0; k < n; k++) { + const Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)]; + if (addr >= r->start && addr < r->end) { + cat = r->cat; + break; + } + } + plat_mutex_unlock_shared(&range_lock); + return cat; +} + +// BARS: "BARS", entry count at 0x0C, then count CRC32 name hashes and count (AMTA offset, BWAV offset) pairs. The AMTA +// block holds the entry's name; music and voice names inside an effects bank (a boss bank with its BGM) get their own +// category. ADPCM entries register their coefficients; other entries tag their bytes. +void audio_cat_scan_bars(uint64_t dst, uint64_t size, int fallback_cat) { + if (!dst || size < 16) return; + atomic_fetch_add_explicit(&bars_count, 1, memory_order_relaxed); + if (fallback_cat != AUDIO_CAT_UNKNOWN) audio_cat_tag_range(dst, size, fallback_cat); + const uint8_t *base = (const uint8_t *)(uintptr_t)dst; + uint32_t count; + memcpy(&count, base + 0x0C, 4); + if (count > 16384 || 0x10ull + (uint64_t)count * 12ull > size) return; + const uint8_t *pairs = base + 0x10 + (uint64_t)count * 4; + for (uint32_t k = 0; k < count; k++) { + uint32_t amta_off, bwav_off; + memcpy(&amta_off, pairs + k * 8ull, 4); + memcpy(&bwav_off, pairs + k * 8ull + 4, 4); + if (bwav_off >= size) continue; + int cat = fallback_cat != AUDIO_CAT_UNKNOWN ? fallback_cat : AUDIO_CAT_SFX; + if (amta_off < size) { + uint64_t limit = size - amta_off < 512 ? size - amta_off : 512; + const char *a = (const char *)(base + amta_off); + for (uint64_t s = 0; s + 4 <= limit; s++) { + if (!memcmp(a + s, "BGM_", 4)) { cat = AUDIO_CAT_MUSIC; break; } + if (!memcmp(a + s, "Vo_", 3) || !memcmp(a + s, "NV_", 3)) { cat = AUDIO_CAT_VOICE; break; } + if (s + 5 <= limit && !memcmp(a + s, "Voice", 5)) { cat = AUDIO_CAT_VOICE; break; } + } + } + const uint8_t *w = base + bwav_off; + uint32_t magic = 0; + uint16_t codec = 0; + if (bwav_off + 0x40 <= size) { + memcpy(&magic, w, 4); + memcpy(&codec, w + 0x10, 2); + } + if (magic == 0x56415742u) audio_cat_scan_bwav(w, size - bwav_off, cat); + if (cat == fallback_cat || (magic == 0x56415742u && codec == 1)) continue; + // The entry ends where the next BWAV starts: one pass over the offsets, only for the few entries tagged here. + uint64_t next = size; + for (uint32_t j = 0; j < count; j++) { + uint32_t o; + memcpy(&o, pairs + j * 8ull + 4, 4); + if (o > bwav_off && o < next) next = o; + } + audio_cat_tag_range(dst + bwav_off, next - bwav_off, cat); + } +} + +// The PCM out of one Opus packet takes the packet's category, else the decoder's last one, else effects. Consecutive +// packets of one decoder fill one buffer, so the output grows the newest matching range instead of adding one each. +void audio_cat_on_opus_decode(uint64_t decoder_guest, uint64_t in_addr, uint64_t out_addr, uint64_t out_size) { + if (!out_addr || !out_size) return; + int cat = audio_cat_lookup(in_addr); + plat_mutex_lock(&decoder_lock); + int slot = -1; + for (int i = 0; i < 64 && decoder_guest; i++) + if (decoders[i].guest == decoder_guest) slot = i; + if (cat == AUDIO_CAT_UNKNOWN && slot >= 0) cat = decoders[slot].cat; + if (cat == AUDIO_CAT_UNKNOWN) cat = AUDIO_CAT_SFX; + if (decoder_guest) { + if (slot < 0) { + slot = (int)(decoder_next++ & 63); + decoders[slot].guest = decoder_guest; + } + decoders[slot].cat = (uint8_t)cat; + } + plat_mutex_unlock(&decoder_lock); + + plat_mutex_lock(&range_lock); + uint32_t depth = range_head < 32 ? range_head : 32; + for (uint32_t k = 0; k < depth; k++) { + Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)]; + if (r->cat != (uint8_t)cat) continue; + uint64_t lo = r->start > 65536 ? r->start - 65536 : 0, hi = r->end + 65536; + if (out_addr >= lo && out_addr + out_size <= hi) { + if (out_addr < r->start) r->start = out_addr; + if (out_addr + out_size > r->end) r->end = out_addr + out_size; + plat_mutex_unlock(&range_lock); + return; + } + } + plat_mutex_unlock(&range_lock); + audio_cat_tag_range(out_addr, out_size, cat); +} + +void audio_cat_log_tables(void) { + rt_log("audio: categories known: coefficient tables music %llu, sfx %llu, voice %llu; address ranges %llu " + "(ring %d); BARS banks scanned %llu", + (unsigned long long)atomic_load(&coef_count[AUDIO_CAT_MUSIC]), + (unsigned long long)atomic_load(&coef_count[AUDIO_CAT_SFX]), + (unsigned long long)atomic_load(&coef_count[AUDIO_CAT_VOICE]), (unsigned long long)atomic_load(&range_count), + RANGE_CAP, (unsigned long long)atomic_load(&bars_count)); +} diff --git a/runtime/audio_cat.h b/runtime/audio_cat.h new file mode 100644 index 00000000..e3aaf130 --- /dev/null +++ b/runtime/audio_cat.h @@ -0,0 +1,30 @@ +// Sound categories for the music / effects / voice volume settings (audio_cat.c). +#ifndef TOTK_AUDIO_CAT_H +#define TOTK_AUDIO_CAT_H +#include +#include +#ifdef __cplusplus +extern "C" { +#endif + +enum { + AUDIO_CAT_UNKNOWN = 0, + AUDIO_CAT_MUSIC = 1, + AUDIO_CAT_SFX = 2, + AUDIO_CAT_VOICE = 3, +}; + +int audio_cat_active(void); // one of volume_music / volume_sfx / volume_voice is below 100 +int audio_cat_from_path(const char *path); // a RomFS-relative path; UNKNOWN outside Sound/Resource/ +void audio_cat_tag_range(uint64_t addr, uint64_t size, int cat); +void audio_cat_scan_bars(uint64_t dst, uint64_t size, int fallback_cat); +void audio_cat_scan_bwav(const void *data, uint64_t size, int cat); +void audio_cat_on_opus_decode(uint64_t decoder_guest, uint64_t in_addr, uint64_t out_addr, uint64_t out_size); +int audio_cat_lookup(uint64_t addr); +int audio_cat_lookup_coef(uint64_t extra_addr, uint64_t extra_size); +void audio_cat_log_tables(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index e0192db4..a71008bc 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -28,6 +28,7 @@ #include "cfg.h" #include "audio_effects.h" #include "audio_reverb.h" +#include "audio_cat.h" #include "audren.h" #include "host.h" #include "ipc.h" @@ -83,6 +84,64 @@ static float host_volume(void) { v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); return v; } + +// TOTK_VOLUME_MUSIC / _SFX / _VOICE: per-category volumes in percent on top of TOTK_VOLUME, live like it. Render thread +// only. +static float category_volume(int cat) { + static const char *const names[4] = {NULL, "TOTK_VOLUME_MUSIC", "TOTK_VOLUME_SFX", "TOTK_VOLUME_VOICE"}; + static float v[4] = {1, 1, 1, 1}; + static unsigned seen = ~0u; + unsigned ver = cfg_version(); + if (ver != seen) { + seen = ver; + for (int k = 1; k < 4; k++) { + double pct = cfg_double(names[k]); + v[k] = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); + } + } + return v[cat >= AUDIO_CAT_MUSIC && cat <= AUDIO_CAT_VOICE ? cat : AUDIO_CAT_SFX]; +} + +// What the voice plays (audio_cat.c): an ADPCM voice by its coefficient table, any voice by the newest loaded or decoded +// range holding its current wave buffer, else effects. Kept per voice until it moves to another buffer or table, so a +// table lookup runs once per wave buffer, not once per frame. The counts feed category_report. +static uint64_t cat_counts[4], cat_by_coef, cat_by_range, cat_ns; +static int voice_category(ServerVoice *v) { + uint64_t addr = v->buffers[v->index].address, extra = v->in.extra_address; + if (v->cat && v->cat_address == addr && v->cat_extra == extra) return v->cat; + uint64_t t0 = kern_now_ns(); + int cat = AUDIO_CAT_UNKNOWN; + if (v->in.sample_format == FMT_ADPCM) cat = audio_cat_lookup_coef(extra, v->in.extra_size); + if (cat != AUDIO_CAT_UNKNOWN) cat_by_coef++; + else if ((cat = audio_cat_lookup(addr)) != AUDIO_CAT_UNKNOWN) cat_by_range++; + else cat = AUDIO_CAT_SFX; + cat_counts[cat]++; + v->cat = (uint8_t)cat; + v->cat_address = addr; + v->cat_extra = extra; + cat_ns += kern_now_ns() - t0; + return cat; +} + +// While a category volume is set, once a minute: how the wave buffers played were classified and what it cost. +static void category_report(void) { + static uint64_t next; + uint64_t now = kern_now_ns(); + if (!next) next = now + 60000000000ull; + if (now < next) return; + next = now + 60000000000ull; + uint64_t n = cat_counts[AUDIO_CAT_MUSIC] + cat_counts[AUDIO_CAT_SFX] + cat_counts[AUDIO_CAT_VOICE]; + rt_log("audio: categories over the last minute: %llu wave buffers, music %llu, sfx %llu, voice %llu; by coefficients " + "%llu, by address %llu, unmatched (played as sfx) %llu; %.1f us classifying", + (unsigned long long)n, (unsigned long long)cat_counts[AUDIO_CAT_MUSIC], + (unsigned long long)cat_counts[AUDIO_CAT_SFX], (unsigned long long)cat_counts[AUDIO_CAT_VOICE], + (unsigned long long)cat_by_coef, (unsigned long long)cat_by_range, + (unsigned long long)(n - cat_by_coef - cat_by_range), cat_ns / 1000.0); + audio_cat_log_tables(); + memset(cat_counts, 0, sizeof cat_counts); + cat_by_coef = cat_by_range = cat_ns = 0; +} + static unsigned audio_revision(const AudioRenderer *r) { unsigned byte = r->revision >> 24; return byte >= '0' ? byte - '0' : 0; } static void report_once(Mixer *m, unsigned bit, const char *msg, int value) { if (m->reported & (1u << bit)) return; @@ -812,10 +871,13 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { 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; + const int by_category = audio_cat_active(); // the defaults (all 100) skip the category lookups + if (by_category) category_report(); for (int i = 0; i < r->param.voice_count; i++) { ServerVoice *v = &r->voices[i]; if (!v->in.in_use || v->state != SV_STARTED || !v->in.sample_rate || !v->valid[v->index]) continue; int cc = (int)v->in.channel_count; if (cc < 1 || cc > 6) { report_once(m, 2, "voice channel count unsupported", cc); continue; } + const float vvol = by_category ? v->in.volume * category_volume(voice_category(v)) : v->in.volume; voice_render(r, v, vs, frames); for (int f = 0; f < 2; f++) if (v->in.biquads[f].enable) { const AudrenBiquad *b = &v->in.biquads[f]; @@ -841,13 +903,13 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { 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); - for (int b = 0; b < r->mixes[d->dest].buffer_count && b < 24; b++) accumulate(m, m->offset[d->dest] + b, source, d->vol[b] * v->in.volume, frames); + for (int b = 0; b < r->mixes[d->dest].buffer_count && b < 24; b++) accumulate(m, m->offset[d->dest] + b, source, d->vol[b] * vvol, frames); } } else if (mix_valid(r, v->in.mix_id)) { for (int c = 0; c < cc; c++) { int32_t id = v->in.channel_ids[c]; if (id < 0 || id >= r->param.voice_count * 6) continue; const AudrenChannelIn *ch = &r->channels[id]; - for (int b = 0; b < r->mixes[v->in.mix_id].buffer_count && b < 24; b++) accumulate(m, m->offset[v->in.mix_id] + b, vs[c], ch->mix[b] * v->in.volume, frames); + for (int b = 0; b < r->mixes[v->in.mix_id].buffer_count && b < 24; b++) accumulate(m, m->offset[v->in.mix_id] + b, vs[c], ch->mix[b] * vvol, frames); } } } diff --git a/runtime/audren.c b/runtime/audren.c index 5ede6072..b230626b 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -86,6 +86,7 @@ static void voice_reset(ServerVoice *v) { v->loops_done = 0; memset(&v->adpcm, 0, sizeof v->adpcm); memset(v->biquad_state, 0, sizeof v->biquad_state); + v->cat = 0; } static void update_voice(AudioRenderer *r, const AudrenVoiceIn *in) { diff --git a/runtime/audren.h b/runtime/audren.h index b2fccba0..159f9af4 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -145,6 +145,8 @@ typedef struct { uint32_t consumed, drops; struct { int16_t history[2]; int32_t buffer; uint64_t at; } adpcm; // DSP-ADPCM decode cursor (buffer slot, next sample) and history float biquad_state[2][6][2]; // filter, channel: transposed direct-form II delays + uint64_t cat_address, cat_extra; // the wave buffer and coefficients voice_category (audio_mixer.c) last classified + uint8_t cat; // their AUDIO_CAT_*, 0 until classified; voice_reset clears it } ServerVoice; enum { SV_STOPPED, SV_STARTED, SV_PAUSED, SV_STOPPING }; diff --git a/runtime/cfg.c b/runtime/cfg.c index 7a7b6520..9488381b 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -65,7 +65,7 @@ static const char *const known[] = { "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SYNC_AHEAD", "SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", - "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", + "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", }; @@ -90,7 +90,7 @@ static const struct { const char *key, *section; } sections[] = { {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"}, {"TEX_HEAP_EXTRA_MB", "mods"}, {"MENU_LANG", "language"}, {"MENU_FONT_FONTCONFIG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, - {"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"}, + {"VOLUME", "audio"}, {"VOLUME_MUSIC", "audio"}, {"VOLUME_SFX", "audio"}, {"VOLUME_VOICE", "audio"}, {"AUDIO_CHANNELS", "audio"}, {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, @@ -161,7 +161,7 @@ static const CfgMeta metas[] = { {"OPERATION_MODE", "docked", "docked|handheld", RESTART}, {"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c) // Sound, Controls, Menu - {"VOLUME", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART}, + {"VOLUME", "100", "0..100", LIVE}, {"VOLUME_MUSIC", "100", "0..100", LIVE}, {"VOLUME_SFX", "100", "0..100", LIVE}, {"VOLUME_VOICE", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART}, {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox", LIVE}, {"PROMPTS", "nintendo", "nintendo|xbox|pc", RESTART}, diff --git a/runtime/ov_media.c b/runtime/ov_media.c index 6e0bd03e..4c71f469 100644 --- a/runtime/ov_media.c +++ b/runtime/ov_media.c @@ -9,6 +9,7 @@ #include #include "host.h" +#include "audio_cat.h" #include "platform/platform.h" #define CODEC_R_INVALID 0x1fcd1u // MacNativeGames' "invalid argument" result for nn::codec @@ -153,6 +154,7 @@ static void ov_decode(Cpu *c) { int got = opus_decode(d->dec, in + 8, (opus_int32)n, out, (int)capacity, 0); opus_uint32 final = 0; if (got >= 0) opus_decoder_ctl(d->dec, OPUS_GET_FINAL_RANGE(&final)); + int channels = d->channels; release(d); if (got < 0) { rt_log("nn::codec: opus_decode: %s", opus_strerror(got)); @@ -163,6 +165,8 @@ static void ov_decode(Cpu *c) { rt_log("nn::codec: Opus final range %#x != stream %#x (logged once)", (unsigned)final, (unsigned)range); *consumed = 8 + n; *samples = got; + if (got > 0 && audio_cat_active()) // the PCM's sound category for the music / effects / voice volumes + audio_cat_on_opus_decode(guest, (uint64_t)(uintptr_t)in, (uint64_t)(uintptr_t)out, (uint64_t)got * (uint64_t)channels * sizeof(int16_t)); ret_x(c, 0); } diff --git a/runtime/ov_zstd.c b/runtime/ov_zstd.c index 0446f307..fbfb33e8 100644 --- a/runtime/ov_zstd.c +++ b/runtime/ov_zstd.c @@ -20,6 +20,7 @@ #include "cfg.h" #include "host.h" +#include "audio_cat.h" #if __has_include("patches.h") #include "patches.h" @@ -235,6 +236,10 @@ static void decode(Cpu *c, uint64_t dict, uint64_t dict_size, uint64_t ddict, ui } c->x[0] = r; count(size, r, kern_now_ns() - t0); + // A sound bank (BARS) for the music / effects / voice volumes (audio_cat.c); its compressed bytes were tagged + // by the RomFS read, which gives the bank's category. + if (r >= 16 && !memcmp((const void *)(uintptr_t)dst, "BARS", 4) && audio_cat_active()) + audio_cat_scan_bars(dst, (uint64_t)r, audio_cat_lookup(src)); } static void host_multiframe(Cpu *c) { decode(c, c->x[5], c->x[6], c->x[7], GUEST_MULTIFRAME, 8); } diff --git a/runtime/romfs.c b/runtime/romfs.c index d6669611..65968259 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -38,6 +38,7 @@ #include "mods.h" #include "romfs_config.h" #include "romfs_path.h" +#include "audio_cat.h" #include "platform/platform.h" #include "restbl.h" #include "sha256.h" @@ -69,6 +70,7 @@ static PlatMutex lock = PLAT_MUTEX_INIT; static uint64_t handles[NH]; // file index + 1; 0 is free static uint32_t handle_ovl[NH]; // overlay slot + 1 of an open handle, 0 = original static PlatFile *handle_file[NH]; // the handle's open overlay file, when handle_ovl is set +static uint8_t handle_audio_cat[NH]; // the open file's sound category (audio_cat.c), AUDIO_CAT_UNKNOWN when not audio static Overlay *overlays; // sorted by RomFS index after overlay_init static size_t noverlay, overlay_cap; static char **added; // RomFS paths of the added files, sorted; added[k] is index file_count + k @@ -1395,12 +1397,21 @@ uint32_t romfs_open(uint64_t *out, const char *path, int mode) { if (!f || memcmp(&id, &o->id, sizeof id)) rt_fatal("RomFS overlay %s is missing or changed since the start check (restart the game)", o->path); } + // The handle keeps its sound category (audio_cat.c) even while the volume settings are at 100, so a stream opened + // before a setting moves is classified by its later reads. A stream may start reading past its header (the + // header came with the BARS bank), so the coefficients are taken from the image here. + int cat = audio_cat_from_path(p); + if (cat != AUDIO_CAT_UNKNOWN && !ov && image_map && audio_cat_active()) { + Entry e = entry(i); + if (e.size >= 0x40) audio_cat_scan_bwav(image_map + e.offset, e.size < 4096 ? e.size : 4096, cat); + } plat_mutex_lock(&lock); for (unsigned j = 1; j < NH; j++) if (!handles[j]) { handles[j] = i + 1; handle_ovl[j] = ov; handle_file[j] = f; + handle_audio_cat[j] = (uint8_t)cat; *out = ROMFS_HANDLE_BASE + j; plat_mutex_unlock(&lock); return 0; @@ -1450,6 +1461,11 @@ uint32_t romfs_read(uint64_t *out, uint64_t h, int64_t offset, void *buffer, uin done += (uint64_t)r; } *out = done; + uint64_t j = h - ROMFS_HANDLE_BASE; + if (handle_audio_cat[j] != AUDIO_CAT_UNKNOWN && done && audio_cat_active()) { + if (offset == 0) audio_cat_scan_bwav(buffer, done, handle_audio_cat[j]); + audio_cat_tag_range((uint64_t)(uintptr_t)buffer, done, handle_audio_cat[j]); + } return 0; } @@ -1472,6 +1488,7 @@ void romfs_close(uint64_t h) { handles[j] = 0; handle_ovl[j] = 0; handle_file[j] = NULL; + handle_audio_cat[j] = 0; plat_mutex_unlock(&lock); if (of) plat_file_close(of); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 486f04f1..44031fcd 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -771,6 +771,9 @@ static void tab_game(float scale) { static void tab_sound(float scale) { opt_begin("##sound", scale); opt_slider("TOTK_VOLUME", UI_MENU_VOLUME, 0, 100, UI_MENU_VOLUME_DESC, 5, "%.0f %%"); + opt_slider("TOTK_VOLUME_MUSIC", UI_MENU_VOLUME_MUSIC, 0, 100, UI_MENU_VOLUME_MUSIC_DESC, 5, "%.0f %%"); + opt_slider("TOTK_VOLUME_SFX", UI_MENU_VOLUME_SFX, 0, 100, UI_MENU_VOLUME_SFX_DESC, 5, "%.0f %%"); + opt_slider("TOTK_VOLUME_VOICE", UI_MENU_VOLUME_VOICE, 0, 100, UI_MENU_VOLUME_VOICE_DESC, 5, "%.0f %%"); static const UiTextId channels[] = {UI_MENU_CHANNELS_AUTO, UI_MENU_CHANNELS_STEREO, UI_MENU_CHANNELS_51}; opt_choice("TOTK_AUDIO_CHANNELS", UI_MENU_AUDIO_CHANNELS, channels, 3, UI_MENU_AUDIO_CHANNELS_DESC); opt_end(); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 84d1ec82..54a7b690 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -242,6 +242,9 @@ static const char *const text_en[UI_TEXT_COUNT] = { "at 60 FPS.", [UI_MENU_VOLUME] = "Volume", [UI_MENU_VOLUME_DESC] = "Master volume of the game.", + [UI_MENU_VOLUME_MUSIC] = "Music volume", [UI_MENU_VOLUME_MUSIC_DESC] = "Volume of background music.", + [UI_MENU_VOLUME_SFX] = "SFX volume", [UI_MENU_VOLUME_SFX_DESC] = "Volume of sound effects.", + [UI_MENU_VOLUME_VOICE] = "Voice volume", [UI_MENU_VOLUME_VOICE_DESC] = "Volume of character voices.", [UI_MENU_AUDIO_CHANNELS] = "Speakers", [UI_MENU_AUDIO_CHANNELS_DESC] = "Stereo or 5.1 surround output. Automatic picks 5.1 when the Windows speaker setup " "has six or more channels. Applies after a restart.", @@ -770,6 +773,9 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_MODS_QUICKMENU_DESC] = "Исправление порта: чёрный фон меню оружия, щитов и материалов при 60 FPS.", [UI_MENU_VOLUME] = "Громкость", [UI_MENU_VOLUME_DESC] = "Общая громкость игры.", + [UI_MENU_VOLUME_MUSIC] = "Громкость музыки", [UI_MENU_VOLUME_MUSIC_DESC] = "Громкость фоновой музыки.", + [UI_MENU_VOLUME_SFX] = "Громкость эффектов", [UI_MENU_VOLUME_SFX_DESC] = "Громкость звуковых эффектов.", + [UI_MENU_VOLUME_VOICE] = "Громкость голосов", [UI_MENU_VOLUME_VOICE_DESC] = "Громкость голосов персонажей.", [UI_MENU_AUDIO_CHANNELS] = "Динамики", [UI_MENU_AUDIO_CHANNELS_DESC] = "Стерео или объёмный звук 5.1. Автоматически выбирается 5.1, если в Windows " "настроено шесть каналов или больше. Применяется после перезапуска.", @@ -1217,6 +1223,9 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_MODE_DOCKED] = "Modo TV", [UI_MENU_MODE_HANDHELD] = "Modo portátil", [UI_MENU_VOLUME] = "Volume", [UI_MENU_VOLUME_DESC] = "Volume geral do jogo.", + [UI_MENU_VOLUME_MUSIC] = "Volume da música", [UI_MENU_VOLUME_MUSIC_DESC] = "Volume da música de fundo.", + [UI_MENU_VOLUME_SFX] = "Volume dos efeitos", [UI_MENU_VOLUME_SFX_DESC] = "Volume dos efeitos sonoros.", + [UI_MENU_VOLUME_VOICE] = "Volume das vozes", [UI_MENU_VOLUME_VOICE_DESC] = "Volume das vozes dos personagens.", [UI_MENU_AUDIO_CHANNELS] = "Alto-falantes", [UI_MENU_AUDIO_CHANNELS_DESC] = "Saída estéreo ou surround 5.1. Automático escolhe 5.1 quando a configuração de " "alto-falantes do Windows tem seis canais ou mais. Vale após reiniciar.", @@ -1825,6 +1834,12 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { "这样被打断的读取可能会永久卡住。", [UI_MENU_VOLUME] = "音量", [UI_MENU_VOLUME_DESC] = "游戏的主音量。", + [UI_MENU_VOLUME_MUSIC] = "音乐音量", + [UI_MENU_VOLUME_MUSIC_DESC] = "背景音乐的音量。", + [UI_MENU_VOLUME_SFX] = "音效音量", + [UI_MENU_VOLUME_SFX_DESC] = "声音特效的音量。", + [UI_MENU_VOLUME_VOICE] = "语音音量", + [UI_MENU_VOLUME_VOICE_DESC] = "角色语音的音量。", [UI_MENU_AUDIO_CHANNELS] = "扬声器", [UI_MENU_AUDIO_CHANNELS_DESC] = "立体声或 5.1 环绕声输出。选择自动时,若 Windows 扬声器设置有六个或更多声道则使用 5.1。重启后生效。", [UI_MENU_CHANNELS_AUTO] = "自动", [UI_MENU_CHANNELS_STEREO] = "立体声", [UI_MENU_CHANNELS_51] = "5.1 环绕声", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 0448d6a1..0c9ee4e6 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -99,6 +99,9 @@ typedef enum { // Sound UI_MENU_VOLUME, UI_MENU_VOLUME_DESC, + UI_MENU_VOLUME_MUSIC, UI_MENU_VOLUME_MUSIC_DESC, + UI_MENU_VOLUME_SFX, UI_MENU_VOLUME_SFX_DESC, + UI_MENU_VOLUME_VOICE, UI_MENU_VOLUME_VOICE_DESC, UI_MENU_AUDIO_CHANNELS, UI_MENU_AUDIO_CHANNELS_DESC, UI_MENU_CHANNELS_AUTO, UI_MENU_CHANNELS_STEREO, UI_MENU_CHANNELS_51, // Controls diff --git a/totk.ini.example b/totk.ini.example index e30d744b..86c87046 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -322,6 +322,12 @@ # audio_prefill_ms = 20 # Volume in percent (0 to 100), on top of the game's own. Changes while the game runs take effect at once. # volume = 100 +# Music, sound effects and voices (characters, creatures, Link) in percent, on top of volume. Live like volume. Below +# 100 the port tells the three apart by the sound files the game loads; a value moved during play reaches sounds +# loaded after the change at once and earlier ones by their next stream read, the rest count as effects. +# volume_music = 100 +# volume_sfx = 100 +# volume_voice = 100 # Speakers: stereo, 5.1 or auto (5.1 when the Windows speaker setup has six or more channels, stereo otherwise). The # game is told the same channel count and mixes for it. Takes effect at the next start. # audio_channels = stereo