diff --git a/runtime/audio_cat.c b/runtime/audio_cat.c index e35c4688..bdf4ae07 100644 --- a/runtime/audio_cat.c +++ b/runtime/audio_cat.c @@ -1,4 +1,19 @@ +// 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" @@ -6,386 +21,269 @@ #include #include -#define AUDIO_RANGE_CAP 32768 -#define COEF_TABLE_CAP 65536 +#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; - uint64_t end; + uint64_t start, end; uint8_t cat; - uint8_t pad[7]; -} AudioRange; +} Range; -static AudioRange s_ranges[AUDIO_RANGE_CAP]; -static _Atomic uint32_t s_range_head = 0; -static PlatMutex s_range_lock = PLAT_MUTEX_INIT; +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; + _Atomic uint64_t hash; // 0 = free; a slot is claimed once and never freed _Atomic uint8_t cat; -} CoefEntry; +} Coef; -static CoefEntry s_coef_table[COEF_TABLE_CAP]; +static Coef coefs[COEF_CAP]; typedef struct { uint64_t guest; uint8_t cat; -} DecoderEntry; +} Decoder; -static DecoderEntry s_decoders[64]; -static PlatMutex s_dec_lock = PLAT_MUTEX_INIT; +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) { - if (!haystack || !needle) return 0; size_t nlen = strlen(needle); - for (; *haystack; haystack++) { + 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++) { +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 fnv1a_coef32(const void *data) { - const uint8_t *p = (const uint8_t *)data; +static uint64_t fnv1a32(const uint8_t *p) { uint64_t h = 14695981039346656037ULL; for (int i = 0; i < 32; i++) { - h ^= (uint64_t)p[i]; + h ^= p[i]; h *= 1099511628211ULL; } - return h ? h : 1ULL; + return h ? h : 1; } -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); - } +// 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 == 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; - } + 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) 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; + 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 = (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); - } + 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) 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) { + 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; - } - - 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); + 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; } } - - 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); + 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; - - 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; + 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; } } - - 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; + 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) return; - - if (fallback_cat != AUDIO_CAT_UNKNOWN) { - audio_cat_tag_range(dst, size, fallback_cat); - } - - if (size < 16) return; + 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; - if (*(const uint32_t *)base != 0x53524142u) { - return; - } - - uint32_t count = *(const uint32_t *)(base + 0x0C); + uint32_t count; + memcpy(&count, base + 0x0C, 4); if (count > 16384 || 0x10ull + (uint64_t)count * 12ull > size) return; - - const uint32_t *pairs = (const uint32_t *)(base + 0x10 + count * 4); + const uint8_t *pairs = base + 0x10 + (uint64_t)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]; + 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; - + 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 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; } } } - - uint64_t next_off = size; + 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 = 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); + 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); - 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; - } + 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) { - 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 (slot < 0) { + slot = (int)(decoder_next++ & 63); + decoders[slot].guest = decoder_guest; } - 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; + 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(&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); - + 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 index b630f2b2..e3aaf130 100644 --- a/runtime/audio_cat.h +++ b/runtime/audio_cat.h @@ -1,3 +1,4 @@ +// Sound categories for the music / effects / voice volume settings (audio_cat.c). #ifndef TOTK_AUDIO_CAT_H #define TOTK_AUDIO_CAT_H #include @@ -13,13 +14,15 @@ enum { AUDIO_CAT_VOICE = 3, }; -int audio_cat_from_path(const char *path); +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 } diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index 9969bd85..a71008bc 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -85,66 +85,63 @@ static float host_volume(void) { return v; } -static float host_volume_music(void) { - static float v = 1.0f; +// 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) 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 (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); } } - 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(); + 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; @@ -874,11 +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 = v->in.volume * voice_category_gain(v); + 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]; 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/ov_media.c b/runtime/ov_media.c index 5c702024..4c71f469 100644 --- a/runtime/ov_media.c +++ b/runtime/ov_media.c @@ -165,7 +165,7 @@ 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) + 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 4907d34e..fbfb33e8 100644 --- a/runtime/ov_zstd.c +++ b/runtime/ov_zstd.c @@ -235,13 +235,11 @@ 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); + // 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 31d1a252..65968259 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -70,7 +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 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 @@ -1397,12 +1397,13 @@ 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 && i != UINT64_MAX) { + 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); - } + 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++) @@ -1461,10 +1462,8 @@ uint32_t romfs_read(uint64_t *out, uint64_t h, int64_t offset, void *buffer, uin } *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]); - } + 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; diff --git a/totk.ini.example b/totk.ini.example index cf364ab6..86c87046 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -322,6 +322,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 +# 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