acdream/src/AcDream.App/Audio/UiSoundController.cs
Erik 6eaa490bb3 feat(audio): Campaign A slice A4 — the interface sound bus
Retail's UI sound bank was absent, so three families of cue were silent:
the portal enter/exit stingers, the AdminEnvirons dungeon atmosphere
(chanting, drums, whispers, thunder — what players remember as dungeon
'music'), and every other interface slot.

The bank's DID is not a literal anywhere in retail: GetUISoundTable
@0x00563FB0 asks GetByEnum for enum slot 7, and DBCache::GetDIDFromEnum
@0x00413940 resolves it through two EnumIDMap hops off the portal dat
header's master map. UiSoundTableResolver walks that chain the way
RetailCursorResolver already walks it for cursors. Against the shipped
dats it resolves to 0x2000004B, and that table holds exactly the 32 UI_*
slots (UI_EnterPortal 0x6A .. UI_Thunder6 0x8A) — content that confirms
the walk independently of the decode. UiSoundTableResolutionTests pins
the walk, the DID, and the content, and skips when dats are absent.

Two corrections to the research along the way. The lane-5 note recorded
GetByEnum's arguments transposed: the 0x22 it called a fileType is the
CACHE type (CLOCache(cache, CSoundTable::Allocator, 0x22)) and the real
second-hop key is 0x10000003; walking it the other way finds nothing. And
its claim that the interface volume pref applies is wrong — GetAttenuation
with ambient=0 multiplies by the EFFECT knob, so retail's
interface_sound_volume stays the dead knob lane 1 byte-decoded it to be.

EnvironSoundCueMap is an explicit 21-case table read straight out of
Handle_Admin__Environs @0x0055DE20, not arithmetic: codes 0x65..0x72 sit
0x11 below their SoundType, but 0x73/0x74 have no case, so 0x75 lands on
UI_Squeal (0x84) where an offset gives 0x86, and the switch ends at 0x7B
with no 0x7C case. Verified case-by-case against the decomp rather than
from the lane note, whose tail table was ambiguous.

Cues are attached where retail plays them: the teleport-animation
boundary for the portal pair, and the AdminEnvirons handler for the
stingers. PlaySoundFromCenter's pan-0 / distance-0 shape is what
PlayUiWave already implements after A2.

Retires TS-54. Narrows AP-115 to its notice-presentation residual.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-08 22:22:09 +02:00

95 lines
3.2 KiB
C#

using System;
using AcDream.Core.Audio;
using DatReaderWriter.DBObjs;
using DRWSound = DatReaderWriter.Enums.Sound;
namespace AcDream.App.Audio;
/// <summary>
/// Retail's interface sound bus — <c>SoundManager::PlaySoundFromCenter</c> @
/// <c>0x00550950</c> over <c>ClientUISystem::GetUISoundTable</c> @
/// <c>0x00563FB0</c>.
///
/// <para>
/// "From centre" means pan 0 and <c>GetAttenuation(0.0f, vol, &amp;out, 0)</c> —
/// distance zero, so the flat branch of the curve, and the <b>effect</b> volume
/// knob rather than the ambient one. Retail's separate
/// <c>interface_sound_volume</c> preference is registered and never read, so
/// there is deliberately no interface volume here either (AP-174).
/// </para>
///
/// <para>
/// The bank's DID is resolved from the dats by <see cref="UiSoundTableResolver"/>
/// rather than hard-coded, and the table itself is fetched lazily on first use
/// exactly as retail's <c>GetUISoundTable</c> caches it behind a null check.
/// </para>
/// </summary>
public sealed class UiSoundController
{
private readonly OpenAlAudioEngine _engine;
private readonly DatSoundCache _cache;
private readonly ISoundRandom _rng;
private readonly uint _tableDid;
private SoundTable? _table;
private bool _tableMissing;
public UiSoundController(
OpenAlAudioEngine engine,
DatSoundCache cache,
uint tableDid,
ISoundRandom? rng = null)
{
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_tableDid = tableDid;
_rng = rng ?? new SoundRandom();
}
/// <summary>The resolved bank DID, or 0 when the dats carry no chain.</summary>
public uint TableDid => _tableDid;
/// <summary>
/// Play one interface slot. Returns false when the bank is absent, the slot
/// is unauthored, or the entry's probability gate says silence.
/// </summary>
public bool Play(SoundId sound)
{
if (!_engine.IsAvailable || _tableDid == 0 || _tableMissing)
return false;
if (_table is null)
{
_table = _cache.GetSoundTable(_tableDid);
if (_table is null)
{
_tableMissing = true;
return false;
}
}
var entry = SoundCookbook.Select(_table, (DRWSound)sound, _rng);
if (entry is null)
return false;
uint waveId = (uint)entry.Id;
if (waveId == 0)
return false;
WaveData? wave = _cache.GetWave(waveId);
if (wave is null)
return false;
// PlaySoundFromCenter takes no volume argument, so the authored entry
// volume is the one that reaches the mixer.
return _engine.PlayUiWave(waveId, wave, entry.Volume);
}
/// <summary>
/// Play the interface stinger for an <c>AdminEnvirons</c> change type — the
/// server-driven dungeon atmosphere (chanting, drums, whispers, thunder).
/// Codes retail has no case for, including <c>0x73</c>/<c>0x74</c> inside the
/// run, play nothing.
/// </summary>
public bool PlayEnvironCue(uint changeType) =>
EnvironSoundCueMap.TryGetSound(changeType, out SoundId sound) && Play(sound);
}