From 1f224dc0f944f2ceb96ccda910a07722efec4ea5 Mon Sep 17 00:00:00 2001 From: 0xEmz Date: Thu, 12 Nov 2026 17:25:33 +0200 Subject: [PATCH] Add separate Music, SFX, and Voice volume sliders (#722) Add volume_music, volume_sfx, and volume_voice sliders under the Sound tab in the F1 settings menu and in totk.ini to control background music, sound effects, and character voices independently. Classifies audio streams by file path in RomFS, parses BARS and BWAV headers for ADPCM coefficient tables and stream formats, tags decompressed Opus streams from nn::codec, and applies per-category gain factors in the audio mixer during voice rendering. Localized across English, Russian, Brazilian Portuguese, and Simplified Chinese. Documented in totk.ini.example. --- runtime/audio_cat.c | 391 ++++++++++++++++++++++++++++++++++++++++++ runtime/audio_cat.h | 27 +++ runtime/audio_mixer.c | 67 +++++++- runtime/cfg.c | 6 +- runtime/ov_media.c | 4 + runtime/ov_zstd.c | 7 + runtime/romfs.c | 18 ++ runtime/ui/menu.cpp | 3 + runtime/ui_text.c | 15 ++ runtime/ui_text.h | 3 + totk.ini.example | 3 + 11 files changed, 539 insertions(+), 5 deletions(-) create mode 100644 runtime/audio_cat.c create mode 100644 runtime/audio_cat.h diff --git a/runtime/audio_cat.c b/runtime/audio_cat.c new file mode 100644 index 00000000..e35c4688 --- /dev/null +++ b/runtime/audio_cat.c @@ -0,0 +1,391 @@ +#include "audio_cat.h" +#include "host.h" +#include "platform/platform.h" + +#include +#include +#include + +#define AUDIO_RANGE_CAP 32768 +#define COEF_TABLE_CAP 65536 + +typedef struct { + uint64_t start; + uint64_t end; + uint8_t cat; + uint8_t pad[7]; +} AudioRange; + +static AudioRange s_ranges[AUDIO_RANGE_CAP]; +static _Atomic uint32_t s_range_head = 0; +static PlatMutex s_range_lock = PLAT_MUTEX_INIT; + +typedef struct { + _Atomic uint64_t hash; + _Atomic uint8_t cat; +} CoefEntry; + +static CoefEntry s_coef_table[COEF_TABLE_CAP]; + +typedef struct { + uint64_t guest; + uint8_t cat; +} DecoderEntry; + +static DecoderEntry s_decoders[64]; +static PlatMutex s_dec_lock = PLAT_MUTEX_INIT; + +static int ci_str_has(const char *haystack, const char *needle) { + if (!haystack || !needle) return 0; + size_t nlen = strlen(needle); + for (; *haystack; haystack++) { + if (strncasecmp(haystack, needle, nlen) == 0) return 1; + } + return 0; +} + +static int is_all_zero32(const void *data) { + const uint8_t *p = (const uint8_t *)data; + for (int i = 0; i < 32; i++) { + if (p[i]) return 0; + } + return 1; +} + +static uint64_t fnv1a_coef32(const void *data) { + const uint8_t *p = (const uint8_t *)data; + uint64_t h = 14695981039346656037ULL; + for (int i = 0; i < 32; i++) { + h ^= (uint64_t)p[i]; + h *= 1099511628211ULL; + } + return h ? h : 1ULL; +} + +static void register_coef32(const void *coef32, int cat) { + if (!coef32 || cat <= AUDIO_CAT_UNKNOWN || cat > AUDIO_CAT_VOICE) return; + if (is_all_zero32(coef32)) return; + + uint64_t h = fnv1a_coef32(coef32); + for (uint32_t step = 0; step < 64; step++) { + uint32_t idx = ((uint32_t)h + step) & (COEF_TABLE_CAP - 1); + uint64_t cur = atomic_load_explicit(&s_coef_table[idx].hash, memory_order_acquire); + if (cur == h) { + uint8_t existing = atomic_load_explicit(&s_coef_table[idx].cat, memory_order_relaxed); + if (cat == AUDIO_CAT_MUSIC || cat == AUDIO_CAT_VOICE || existing <= AUDIO_CAT_UNKNOWN || existing == AUDIO_CAT_SFX) { + atomic_store_explicit(&s_coef_table[idx].cat, (uint8_t)cat, memory_order_release); + } + return; + } + if (cur == 0) { + if (atomic_compare_exchange_strong_explicit(&s_coef_table[idx].hash, &cur, h, memory_order_acq_rel, memory_order_acquire) || cur == h) { + atomic_store_explicit(&s_coef_table[idx].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) return AUDIO_CAT_UNKNOWN; + if (!mem_guest_readable(extra_addr, 32)) return AUDIO_CAT_UNKNOWN; + const void *ptr = (const void *)(uintptr_t)extra_addr; + if (is_all_zero32(ptr)) return AUDIO_CAT_UNKNOWN; + uint64_t h = fnv1a_coef32(ptr); + for (uint32_t step = 0; step < 64; step++) { + uint32_t idx = ((uint32_t)h + step) & (COEF_TABLE_CAP - 1); + uint64_t cur = atomic_load_explicit(&s_coef_table[idx].hash, memory_order_acquire); + if (cur == h) { + return (int)atomic_load_explicit(&s_coef_table[idx].cat, memory_order_acquire); + } + if (cur == 0) return AUDIO_CAT_UNKNOWN; + } + return AUDIO_CAT_UNKNOWN; +} + +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 = (const uint8_t *)data; + if (*(const uint32_t *)b != 0x56415742u) return; + + uint16_t num_ch = *(const uint16_t *)(b + 0x0E); + if (num_ch == 0 || num_ch > 16) return; + + for (uint16_t ch = 0; ch < num_ch; ch++) { + uint64_t ch_off = 0x10ull + (uint64_t)ch * 0x4Cull; + if (ch_off + 0x30ull > size) break; + uint16_t codec = *(const uint16_t *)(b + ch_off); + if (codec == 1) { + register_coef32(b + ch_off + 0x10, cat); + } + } +} + +int audio_cat_from_path(const char *path) { + if (!path) return AUDIO_CAT_UNKNOWN; + + const char *slash = strrchr(path, '/'); + const char *bslash = strrchr(path, '\\'); + const char *fname = path; + if (slash && slash + 1 > fname) fname = slash + 1; + if (bslash && bslash + 1 > fname) fname = bslash + 1; + if (strncasecmp(fname, "nv_", 3) == 0 || strncasecmp(fname, "vo_", 3) == 0) { + return AUDIO_CAT_VOICE; + } + + if (!ci_str_has(path, "sound/") && + !ci_str_has(path, "sound\\") && + !ci_str_has(path, "voice/") && + !ci_str_has(path, "voice\\") && + !ci_str_has(path, ".bars") && + !ci_str_has(path, ".bwav") && + !ci_str_has(path, ".bfsar")) { + return AUDIO_CAT_UNKNOWN; + } + + if (ci_str_has(path, "bgm_") || + ci_str_has(path, "/bgm/") || + ci_str_has(path, "\\bgm\\") || + ci_str_has(path, "sound/bgm") || + ci_str_has(path, "sound\\bgm")) { + return AUDIO_CAT_MUSIC; + } + + if (ci_str_has(path, "voice/") || + ci_str_has(path, "voice\\") || + ci_str_has(path, "_voice") || + ci_str_has(path, "voice_") || + ci_str_has(path, "player_voice") || + ci_str_has(path, "_vo_") || + ci_str_has(path, "/vo_") || + ci_str_has(path, "\\vo_") || + ci_str_has(path, "/nv_") || + ci_str_has(path, "\\nv_") || + ci_str_has(path, "shoutvoice") || + ci_str_has(path, "eventflowmsg") || + ci_str_has(path, "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(&s_range_lock); + uint32_t head = atomic_load_explicit(&s_range_head, memory_order_relaxed); + uint32_t search_depth = head < 64 ? head : 64; + for (uint32_t k = 0; k < search_depth; k++) { + uint32_t idx = (head - 1 - k) & (AUDIO_RANGE_CAP - 1); + if (s_ranges[idx].start == addr && s_ranges[idx].end == addr + size) { + s_ranges[idx].cat = (uint8_t)cat; + plat_mutex_unlock(&s_range_lock); + return; + } + } + + uint32_t idx = head & (AUDIO_RANGE_CAP - 1); + s_ranges[idx].start = addr; + s_ranges[idx].end = addr + size; + s_ranges[idx].cat = (uint8_t)cat; + atomic_store_explicit(&s_range_head, head + 1, memory_order_release); + plat_mutex_unlock(&s_range_lock); +} + +int audio_cat_lookup(uint64_t addr) { + if (!addr) return AUDIO_CAT_UNKNOWN; + + uint32_t head = atomic_load_explicit(&s_range_head, memory_order_acquire); + uint32_t limit = head > AUDIO_RANGE_CAP ? head - AUDIO_RANGE_CAP : 0; + for (uint32_t i = head; i > limit; i--) { + uint32_t idx = (i - 1) & (AUDIO_RANGE_CAP - 1); + uint64_t start = s_ranges[idx].start; + uint64_t end = s_ranges[idx].end; + if (addr >= start && addr < end) { + return (int)s_ranges[idx].cat; + } + } + + if (addr >= 0x40 && addr < RT_ADDRESS_LIMIT) { + uint64_t min_check = addr >= 256 ? addr - 256 : 0; + min_check &= ~0xFull; + for (uint64_t b = addr & ~0xFull; b >= min_check; b -= 16) { + if (mem_guest_readable(b, 0x20)) { + const uint32_t *p = (const uint32_t *)(uintptr_t)b; + if (p[0] == 0x56415742u) { + const uint8_t *bp = (const uint8_t *)p; + uint16_t num_ch = *(const uint16_t *)(bp + 0x0E); + uint16_t codec = *(const uint16_t *)(bp + 0x10); + if (codec == 2) return AUDIO_CAT_VOICE; + if (codec == 1 && num_ch >= 2) return AUDIO_CAT_MUSIC; + return AUDIO_CAT_SFX; + } + } + if (b == 0) break; + } + } + + return AUDIO_CAT_UNKNOWN; +} + +void audio_cat_scan_bars(uint64_t dst, uint64_t size, int fallback_cat) { + if (!dst || !size) return; + + if (fallback_cat != AUDIO_CAT_UNKNOWN) { + audio_cat_tag_range(dst, size, fallback_cat); + } + + if (size < 16) return; + const uint8_t *base = (const uint8_t *)(uintptr_t)dst; + if (*(const uint32_t *)base != 0x53524142u) { + return; + } + + uint32_t count = *(const uint32_t *)(base + 0x0C); + if (count > 16384 || 0x10ull + (uint64_t)count * 12ull > size) return; + + const uint32_t *pairs = (const uint32_t *)(base + 0x10 + count * 4); + for (uint32_t k = 0; k < count; k++) { + uint32_t amta_off = pairs[k * 2]; + uint32_t bwav_off = pairs[k * 2 + 1]; + if (bwav_off >= size) continue; + + int cat = (fallback_cat != AUDIO_CAT_UNKNOWN) ? fallback_cat : AUDIO_CAT_SFX; + + if (amta_off < size) { + uint32_t amta_scan_limit = 512; + if (amta_off + amta_scan_limit > size) { + amta_scan_limit = (uint32_t)(size - amta_off); + } + const char *amta_bytes = (const char *)(base + amta_off); + for (uint32_t s = 0; s + 4 <= amta_scan_limit; s++) { + if (memcmp(amta_bytes + s, "BGM_", 4) == 0) { + cat = AUDIO_CAT_MUSIC; + break; + } + if (memcmp(amta_bytes + s, "_Vo_", 4) == 0 || memcmp(amta_bytes + s, "Vo_", 3) == 0 || + memcmp(amta_bytes + s, "NV_", 3) == 0) { + cat = AUDIO_CAT_VOICE; + break; + } + if (s + 5 <= amta_scan_limit && memcmp(amta_bytes + s, "Voice", 5) == 0) { + cat = AUDIO_CAT_VOICE; + break; + } + if (amta_bytes[s] == 'P' && amta_bytes[s + 1] == 'V' && + amta_bytes[s + 2] >= '0' && amta_bytes[s + 2] <= '9') { + cat = AUDIO_CAT_VOICE; + break; + } + } + } + + uint64_t next_off = size; + for (uint32_t j = 0; j < count; j++) { + uint32_t o = pairs[j * 2 + 1]; + if (o > bwav_off && o < next_off) { + next_off = o; + } + } + uint64_t bwav_span = next_off - bwav_off; + + if (bwav_off + 0x40 <= size && *(const uint32_t *)(base + bwav_off) == 0x56415742u) { + uint16_t codec = *(const uint16_t *)(base + bwav_off + 0x10); + if (codec == 2 && cat == AUDIO_CAT_SFX) { + cat = AUDIO_CAT_VOICE; + } + audio_cat_scan_bwav(base + bwav_off, size - bwav_off, cat); + if (codec == 2 || cat != fallback_cat) { + audio_cat_tag_range(dst + bwav_off, bwav_span, cat); + } + } else if (cat != fallback_cat) { + audio_cat_tag_range(dst + bwav_off, bwav_span, cat); + } + } +} + +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); + if (cat == AUDIO_CAT_UNKNOWN) { + if (in_addr >= 0x40 && in_addr < RT_ADDRESS_LIMIT) { + uint64_t min_check = in_addr >= 256 ? in_addr - 256 : 0; + min_check &= ~0xFull; + for (uint64_t b = in_addr & ~0xFull; b >= min_check; b -= 16) { + if (mem_guest_readable(b, 0x20)) { + const uint32_t *p = (const uint32_t *)(uintptr_t)b; + if (p[0] == 0x56415742u) { + uint16_t codec = *(const uint16_t *)((const uint8_t *)p + 0x10); + if (codec == 2) { cat = AUDIO_CAT_VOICE; break; } + } + } + if (b == 0) break; + } + } + } + + plat_mutex_lock(&s_dec_lock); + if (cat == AUDIO_CAT_UNKNOWN && decoder_guest) { + for (int i = 0; i < 64; i++) { + if (s_decoders[i].guest == decoder_guest && s_decoders[i].cat != AUDIO_CAT_UNKNOWN) { + cat = s_decoders[i].cat; + break; + } + } + } + if (cat == AUDIO_CAT_UNKNOWN) { + cat = AUDIO_CAT_VOICE; + } + if (decoder_guest) { + int found = 0; + for (int i = 0; i < 64; i++) { + if (s_decoders[i].guest == decoder_guest) { + if (cat != AUDIO_CAT_UNKNOWN) s_decoders[i].cat = (uint8_t)cat; + found = 1; + break; + } + } + if (!found) { + static int dec_head = 0; + int slot = dec_head++ & 63; + s_decoders[slot].guest = decoder_guest; + s_decoders[slot].cat = (uint8_t)cat; + } + } + plat_mutex_unlock(&s_dec_lock); + + plat_mutex_lock(&s_range_lock); + uint32_t head = atomic_load_explicit(&s_range_head, memory_order_relaxed); + uint32_t check_count = head < 32 ? head : 32; + for (uint32_t k = 0; k < check_count; k++) { + uint32_t idx = (head - 1 - k) & (AUDIO_RANGE_CAP - 1); + if (s_ranges[idx].cat == (uint8_t)cat) { + if (out_addr >= s_ranges[idx].start && out_addr + out_size <= s_ranges[idx].end) { + plat_mutex_unlock(&s_range_lock); + return; + } + if (out_addr == s_ranges[idx].end) { + s_ranges[idx].end = out_addr + out_size; + plat_mutex_unlock(&s_range_lock); + return; + } + if (out_addr + out_size == s_ranges[idx].start) { + s_ranges[idx].start = out_addr; + plat_mutex_unlock(&s_range_lock); + return; + } + if (out_addr >= (s_ranges[idx].start > 65536 ? s_ranges[idx].start - 65536 : 0) && + out_addr + out_size <= s_ranges[idx].end + 65536) { + if (out_addr < s_ranges[idx].start) s_ranges[idx].start = out_addr; + if (out_addr + out_size > s_ranges[idx].end) s_ranges[idx].end = out_addr + out_size; + plat_mutex_unlock(&s_range_lock); + return; + } + } + } + plat_mutex_unlock(&s_range_lock); + + audio_cat_tag_range(out_addr, out_size, cat); +} diff --git a/runtime/audio_cat.h b/runtime/audio_cat.h new file mode 100644 index 00000000..b630f2b2 --- /dev/null +++ b/runtime/audio_cat.h @@ -0,0 +1,27 @@ +#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_from_path(const char *path); +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); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index e0192db4..9969bd85 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,67 @@ static float host_volume(void) { v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); return v; } + +static float host_volume_music(void) { + static float v = 1.0f; + static unsigned seen = ~0u; + unsigned ver = cfg_version(); + if (ver == seen) return v; + seen = ver; + double pct = cfg_double("TOTK_VOLUME_MUSIC"); + v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); + return v; +} + +static float host_volume_sfx(void) { + static float v = 1.0f; + static unsigned seen = ~0u; + unsigned ver = cfg_version(); + if (ver == seen) return v; + seen = ver; + double pct = cfg_double("TOTK_VOLUME_SFX"); + v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); + return v; +} + +static float host_volume_voice(void) { + static float v = 1.0f; + static unsigned seen = ~0u; + unsigned ver = cfg_version(); + if (ver == seen) return v; + seen = ver; + double pct = cfg_double("TOTK_VOLUME_VOICE"); + v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100); + return v; +} + +static float voice_category_gain(ServerVoice *v) { + int cat = AUDIO_CAT_UNKNOWN; + if (v->in.sample_format == FMT_ADPCM && v->in.extra_address && v->in.extra_size >= 32) { + cat = audio_cat_lookup_coef(v->in.extra_address, v->in.extra_size); + } + if (cat == AUDIO_CAT_UNKNOWN) { + uint64_t addr = v->buffers[v->index].address; + cat = audio_cat_lookup(addr); + } + if (cat == AUDIO_CAT_UNKNOWN) { + for (int b = 0; b < 4; b++) { + if (v->valid[b] && v->buffers[b].address) { + cat = audio_cat_lookup(v->buffers[b].address); + if (cat != AUDIO_CAT_UNKNOWN) break; + } + } + } + if (cat == AUDIO_CAT_UNKNOWN && v->in.extra_address) { + cat = audio_cat_lookup(v->in.extra_address); + } + if (cat == AUDIO_CAT_UNKNOWN) { + cat = AUDIO_CAT_SFX; + } + if (cat == AUDIO_CAT_MUSIC) return host_volume_music(); + if (cat == AUDIO_CAT_VOICE) return host_volume_voice(); + return host_volume_sfx(); +} 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; @@ -816,6 +878,7 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { 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 = v->in.volume * voice_category_gain(v); 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 +904,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/cfg.c b/runtime/cfg.c index dbb73f7d..4af27809 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -60,7 +60,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", }; @@ -85,7 +85,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"}, {"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"}, @@ -148,7 +148,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..5c702024 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_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..4907d34e 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" @@ -234,6 +235,12 @@ static void decode(Cpu *c, uint64_t dict, uint64_t dict_size, uint64_t ddict, ui return; } c->x[0] = r; + if (r > 0) { + int cat = audio_cat_lookup(src); + if (cat != AUDIO_CAT_UNKNOWN || (r >= 16 && memcmp((const void *)(uintptr_t)dst, "BARS", 4) == 0)) { + audio_cat_scan_bars(dst, (uint64_t)r, cat); + } + } count(size, r, kern_now_ns() - t0); } diff --git a/runtime/romfs.c b/runtime/romfs.c index 3196f7ee..96b07d96 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]; 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 @@ -1249,12 +1251,20 @@ 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); } + int cat = audio_cat_from_path(p); + if (cat != AUDIO_CAT_UNKNOWN && !ov && image_map && i != UINT64_MAX) { + 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; @@ -1304,6 +1314,13 @@ 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 (j < NH && handle_audio_cat[j] != AUDIO_CAT_UNKNOWN && buffer && done > 0) { + if (offset == 0 && done >= 0x40) { + 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; } @@ -1326,6 +1343,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 2bed97ed..76cc93b3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -733,6 +733,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 690383b7..eaf3322f 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -232,6 +232,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.", @@ -712,6 +715,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 " "настроено шесть каналов или больше. Применяется после перезапуска.", @@ -1120,6 +1126,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.", @@ -1680,6 +1689,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 3bed8949..9dedb5ec 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -97,6 +97,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 c45a74b2..ba6465d3 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -304,6 +304,9 @@ # 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 +# 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