117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
using RustToDust.Core.Audio;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Owns all immutable <see cref="AudioStreamWav"/> assets built once at client startup:
|
|
/// 25 curated Haven Kendrick WAVs from <c>assets/audio/haven/</c> plus all deterministic
|
|
/// <see cref="PcmSynth"/> procedural clips (with separable <see cref="WetMixMode"/>).
|
|
/// Zero <c>.import</c> sidecar dependency.
|
|
/// </summary>
|
|
public sealed class ProceduralAudioBank
|
|
{
|
|
public static readonly string[] ExpectedHavenClips =
|
|
{
|
|
"mg_shot_01", "mg_shot_02", "mg_tail",
|
|
"ac_light_01", "ac_light_02", "ac_med_01", "ac_rotary",
|
|
"flak_blast_01", "flak_blast_02", "cannon_heavy_01", "cannon_heavy_02",
|
|
"pd_bark_01", "pd_bark_02", "pd_laser",
|
|
"rocket_salvo_01", "rocket_salvo_02",
|
|
"plasma_ppc_01", "plasma_ppc_02", "lance_beam_01", "laser_heavy_01", "gauss_rail_01",
|
|
"mech_reload_bolt", "mech_quick_bolt", "mech_eject", "mech_turret_servo"
|
|
};
|
|
|
|
private readonly Dictionary<string, AudioStreamWav> _streams = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public ProceduralAudioBank(bool wetMixMode = true, string? havenDirOverride = null)
|
|
{
|
|
WetMixMode = wetMixMode;
|
|
LoadHavenClips(havenDirOverride ?? LocateHavenAudioDirectory());
|
|
BuildProceduralClips(wetMixMode);
|
|
}
|
|
|
|
public bool WetMixMode { get; private set; }
|
|
public int HavenClipCount { get; private set; }
|
|
public int ProceduralClipCount { get; private set; }
|
|
public int TotalClipCount => _streams.Count;
|
|
|
|
public bool TryGetStream(string key, out AudioStreamWav stream) =>
|
|
_streams.TryGetValue(key, out stream!);
|
|
|
|
public AudioStreamWav GetStream(string key) =>
|
|
_streams.TryGetValue(key, out var stream)
|
|
? stream
|
|
: throw new KeyNotFoundException($"Missing audio clip '{key}' in ProceduralAudioBank.");
|
|
|
|
public void RebuildProcedural(bool wetMixMode)
|
|
{
|
|
WetMixMode = wetMixMode;
|
|
BuildProceduralClips(wetMixMode);
|
|
}
|
|
|
|
public static string LocateHavenAudioDirectory()
|
|
{
|
|
string assetsDir = ClientAssets.LocateAssetsDirectory();
|
|
string havenDir = Path.Combine(assetsDir, "audio", "haven");
|
|
if (!Directory.Exists(havenDir))
|
|
{
|
|
throw new DirectoryNotFoundException($"Curated Haven audio directory not found: {havenDir}");
|
|
}
|
|
return havenDir;
|
|
}
|
|
|
|
private void LoadHavenClips(string havenDir)
|
|
{
|
|
int loaded = 0;
|
|
for (int i = 0; i < ExpectedHavenClips.Length; i++)
|
|
{
|
|
string key = ExpectedHavenClips[i];
|
|
string path = Path.Combine(havenDir, key + ".wav");
|
|
if (!File.Exists(path))
|
|
{
|
|
throw new FileNotFoundException($"Missing curated Haven WAV '{path}'.", path);
|
|
}
|
|
|
|
WavPcmInfo wav = WavFileReader.ReadMonoOrStereo16(path);
|
|
var stream = new AudioStreamWav
|
|
{
|
|
Format = AudioStreamWav.FormatEnum.Format16Bits,
|
|
MixRate = wav.SampleRate,
|
|
Stereo = wav.Channels == 2,
|
|
LoopMode = AudioStreamWav.LoopModeEnum.Disabled,
|
|
Data = wav.PcmBytes
|
|
};
|
|
_streams[key] = stream;
|
|
loaded++;
|
|
}
|
|
HavenClipCount = loaded;
|
|
}
|
|
|
|
private void BuildProceduralClips(bool wetMixMode)
|
|
{
|
|
Dictionary<string, PcmClipData> clips = PcmSynth.SynthesizeAll(wetMixMode);
|
|
int count = 0;
|
|
foreach (var kv in clips)
|
|
{
|
|
PcmClipData clip = kv.Value;
|
|
var stream = new AudioStreamWav
|
|
{
|
|
Format = AudioStreamWav.FormatEnum.Format16Bits,
|
|
MixRate = clip.SampleRate,
|
|
Stereo = false,
|
|
LoopMode = clip.IsLoop ? AudioStreamWav.LoopModeEnum.Forward : AudioStreamWav.LoopModeEnum.Disabled,
|
|
LoopBegin = 0,
|
|
LoopEnd = clip.IsLoop ? clip.Samples.Length : 0,
|
|
Data = clip.Pcm16Bytes
|
|
};
|
|
_streams[clip.Key] = stream;
|
|
count++;
|
|
}
|
|
ProceduralClipCount = count;
|
|
}
|
|
}
|