Runtime WAV Loading (AudioClip from disk)
Runtime WAV Loading (AudioClip from disk)
When a code-only 7DTD mod needs a sampled sound but can't rebuild its Unity
asset bundle (.unity3d), you can ship a loose .wav in the mod's Resources/
folder and decode it into an AudioClip at runtime — no bundle, no
UnityWebRequest.
How
- Ship the WAV in
resources/and copy it to<ModDest>/Resources/indeploy.ps1(same pattern as the goggles PNG in the FPV mod). - Resolve the deployed path via the mod instance path:
ModInfo/Mod.Path(FPV exposes it asFPVModInit.ModPath), thenPath.Combine(modPath, "Resources", "file.wav"). File.ReadAllBytes(path)→ parse the RIFF/WAVE header → decode PCM into afloat[]normalized to -1..1 →AudioClip.Create(name, samplesPerChannel, channels, sampleRate, false)thenclip.SetData(data, 0).
WAV parsing notes
- Walk the chunk list (
4-byte id+4-byte little-endian size+ payload); don't assumefmtis immediately followed bydata— extra chunks (LIST, etc.) can sit between them. Chunks are word-aligned: an odd size has a pad byte. fmtfields: audioFormat(2) must be 1 (uncompressed PCM), channels(2), sampleRate(4), then bitsPerSample at offset +14.- Decode per bit depth: 8-bit is unsigned (0..255, centre 128); 16/24/32-bit are signed little-endian. 24-bit needs manual sign extension.
AudioClip.Createtakes samples per channel (not total interleaved samples);SetDatatakes the full interleavedfloat[].- Reference implementation:
FPV/src/FPVWavLoader.cs.
MP3/other compressed sources
The loader is PCM-WAV-only by design — don't try to decode MP3 in managed code.
Convert once at authoring time and ship the WAV:
ffmpeg -i input.mp3 -ac 1 -ar 44100 -acodec pcm_s16le output.wav
(ffmpeg is installed via chocolatey on this machine). Keep the original MP3 in
resources/ as the source of truth; only the WAV is deployed. Example: the FPV
mod's Betaflight startup melody (betaflight_startup.mp3 →
betaflight-startup.wav).
Applies when
Any mod that wants recorded audio without an asset-bundle round-trip. Same
runtime-AudioClip path the FPV mod already uses for its synthesized rotor
sounds — this just fills the buffer from a file instead of a synth. For the
synth side (RPM→pitch model, anti-rasp spectral tuning), see
Synthesized Drone Rotor Audio.
sounds ([[Procedural Engine-Rotor Loop Synthesis]]) — this just fills the buffer
from a file instead of a synth.