-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundModManager.cs
More file actions
394 lines (300 loc) · 13.6 KB
/
SoundModManager.cs
File metadata and controls
394 lines (300 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using BepInEx.Logging;
using UnityEngine;
namespace techtech.TechTechsSoundMod;
public static class SoundMap
{
// Key = filename stem in TechTechsSounds folder
// Value = AudioClip.name(s) used by the game
public static readonly Dictionary<string, string[]> Entries = new(StringComparer.OrdinalIgnoreCase)
{
///////// Vanilla /////////
/// General ///
{"player_disconnect", new[] {"playerdisconnect"}},
// Impostor
{"kill(killer)", new[] {"impostor_kill"}}, // the sound the killer hears when killing someone
{"sabotage_alarm", new[] {"Alarm_sabotage"}},
{"kill_music", new[] {"impostor_killMusic"}},
{"kill(victim)_knife", new[] {"kill_knife"}},
{"kill(victim)_tongue", new[] {"Kill_Alien"}},
{"kill(victim)_shooting", new[] {"kill_gun"}},
{"kill(victim)_neck_twist", new[] {"kill_neck"}},
// Meetings
{"meeting", new[] {"alarm_emergencymeeting"}}, // the whooop sound
{"emergency_button_click", new[] {"panel_emergencyButton"}},
{"notification", new[] {"notification"}},
{"vote_timer", new[] {"vote_timer"}},
{"select_player_to_vote", new[] {"votescreen_avote"}},
{"lock_vote", new[] {"votescreen_lockin"}},
{"eject_text_typing", new[] {"eject_text"}},
{"eject_sheld", new[] {"eject_skeld"}},
// UI
{"ui_hover", new[] {"UI_Hover"}},
{"button_click", new[] {"UI_Select"}},
// Ambient
{"ambient_weaponsroom", new[] {"AMB_Weapons"}},
{"ambient_oxygenroom", new[] {"AMB_O2Room"}},
{"ambient_shieldroom", new[] {"AMB_ShieldRoom"}},
{"ambient_reactorroom", new[] {"AMB_ReactorRoom"}},
{"ambient_engineroom", new[] {"AMB_EngineRoom"}},
{"ambient_commsroom", new[] {"AMB_CommsRoom"}},
{"ambient_securityroom", new[] {"AMB_SecurityRoom"}},
{"ambient_medbayroom", new[] {"AMB_MedbayRoom"}},
///////// Town of Us: Mira /////////
/// Guessing ///
{"guess_kill", new[] {"votescreen_playerdead"}},
/// Role Specific ///
// Mayor
{"mayor_reveal", new[] {"MayorRevealSound"}},
}; /// {"filename", new[] {"clipname"}},
public static readonly string README = @"TechTech's Sound Mod:
------------------------
To replace a sound, place a wav audio file in this folder named as shown below:
///////// Vanilla /////////
--- General ---
player_disconnect.wav -> Plays when a player disconnects
--- Impostor ---
kill(killer).wav -> The sound the killer hears when killing someone
sabotage_alarm.wav -> The alarm you hear when a sabotage happens
kill_music.wav
kill(victim)_knife.wav -> Plays with the stabbing kill animation on the victim
kill(victim)_tongue.wav -> Plays with the tongue kill animation on the victim
kill(victim)_shooting.wav -> Plays with the shoot kill animation on the victim
kill(victim)_neck_twist.wav -> Plays with the neck twist kill animation on the victim
--- Ambient ---
ambient_weaponsroom.wav -> Ambient Sound of the Weapons room
ambient_oxygenroom.wav -> Ambient Sound of the Oxygen room
ambient_shieldroom.wav -> Ambient Sound of the Shield room
ambient_reactorroom.wav -> Ambient Sound of the Reactor room
ambient_engineroom.wav -> Ambient Sound of the Engine room
ambient_commsroom.wav -> Ambient Sound of the Communications room
ambient_securityroom.wav -> Ambient Sound of the Security room
ambient_medbayroom.wav -> Ambient Sound of the Medbay room
--- Meetings ---
meeting.wav -> The whooop meeting sound
notification.wav -> The notification you hear when you get a new message in chat
emergency_button_click.wav -> The click of the emergency button
vote_timer.wav -> Plays with the final countdown at the end of a meeting
select_player_to_vote.wav -> Plays when selecting a player to vote.
lock_vote.wav -> Plays when pressing the tick button after selecting a player (locking your vote)
eject_text_typing.wav -> The typing of letters after a meeting
eject_skeld.wav -> Eject sound on the Skeld map
--- UI ---
ui_hover.wav -> The sound you hear when hovering over a button
button_click.wav -> Button click sound
///////// Town Of Us: Mira /////////
--- Guessing ---
guess_kill.wav -> Plays to everyone when someone dies during a meeting
--- Role Specific ----
- Mayor
mayor_reveal.wav -> Plays when a politician becomes a mayor / mayor reveals role
";
public static readonly string[] Extensions = { ".wav" };
}
public static class SoundModManager
{
public static string SoundsFolder { get; private set; } = null!;
private static ManualLogSource Log => TechTechsSoundMod.Instance.Log;
public static readonly Dictionary<string, float[]> PendingInjections = new(StringComparer.OrdinalIgnoreCase);
public static readonly HashSet<string> Injected = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, string> ClipInfo = new(StringComparer.OrdinalIgnoreCase);
private static bool DumpWritten = false;
private static readonly HashSet<string> SeenClips = new(StringComparer.OrdinalIgnoreCase);
private static void DebugLogClip(AudioClip clip, string clipName)
{
if (!TechTechsSoundModLocalSettings.Instance.DebugLogging.Value)
return;
if (SeenClips.Contains(clipName))
return;
SeenClips.Add(clipName);
Log.LogInfo(
$"[SoundMod][DEBUG] AudioClip detected: {clipName} | " +
$"{clip.frequency}Hz | {clip.channels}ch | {clip.samples} samples | {clip.length:F3}s"
);
}
private static void WriteDump()
{
if (!TechTechsSoundModLocalSettings.Instance.DumpClipInfoToggle.Value)
return;
if (DumpWritten) return;
try
{
var bepInExRoot = Path.GetFullPath(Path.Combine(Application.dataPath, "..", "BepInEx"));
var path = Path.Combine(bepInExRoot, "TechTechsSoundInfo.txt");
using var sw = new StreamWriter(path);
sw.WriteLine("TechTech's Sound Mod - Audio Info Dump");
sw.WriteLine("--------------------------------------");
sw.WriteLine();
foreach (var (stem, aliases) in SoundMap.Entries)
{
sw.WriteLine($"File name: {stem}.wav");
foreach (var alias in aliases)
{
sw.WriteLine($" ClipName: {alias}");
if (ClipInfo.TryGetValue(alias, out var info))
{
sw.WriteLine(info.Replace("\n", "\n "));
}
else
{
sw.WriteLine(" (not played yet)");
}
}
sw.WriteLine();
}
Log.LogInfo($"[SoundMod] Wrote clip info dump: {path}");
DumpWritten = true;
}
catch (Exception ex)
{
Log.LogWarning($"[SoundMod] Failed to write clip info dump: {ex}");
}
}
private static void RecordClipInfo(AudioClip clip, string clipName)
{
if (!TechTechsSoundModLocalSettings.Instance.DumpClipInfoToggle.Value)
return;
if (!SoundMap.Entries.ContainsValue(new[] { clipName }) && !PendingInjections.ContainsKey(clipName))
return;
if (ClipInfo.ContainsKey(clipName))
return;
var info =
$"Clip: {clipName}\n" +
$"SampleRate: {clip.frequency}\n" +
$"Channels: {clip.channels}\n" +
$"Samples: {clip.samples}\n" +
$"LengthSeconds: {clip.length:F3}\n";
ClipInfo[clipName] = info;
}
public static void Initialize()
{
var gameRoot = Path.GetDirectoryName(Application.dataPath)!;
SoundsFolder = Path.Combine(gameRoot, "TechTechsSounds");
if (!Directory.Exists(SoundsFolder))
{
Directory.CreateDirectory(SoundsFolder);
Log.LogInfo($"[SoundMod] Created sounds folder: {SoundsFolder}");
}
else
{
Log.LogInfo($"[SoundMod] Sounds folder found: {SoundsFolder}");
}
WriteReadme();
LoadAllWavs();
}
private static void LoadAllWavs()
{
PendingInjections.Clear();
Injected.Clear();
int loaded = 0;
foreach (var (stem, aliases) in SoundMap.Entries)
{
string? file = null;
foreach (var ext in SoundMap.Extensions)
{
var p = Path.Combine(SoundsFolder, stem + ext);
if (File.Exists(p)) { file = p; break; }
}
if (file == null) continue;
try
{
var samples = ParseWavToFloat(file);
foreach (var alias in aliases)
PendingInjections[alias] = samples;
Log.LogInfo($"[SoundMod] Loaded '{Path.GetFileName(file)}' → [{string.Join(", ", aliases)}] ({samples.Length} samples)");
loaded++;
}
catch (Exception ex)
{
Log.LogWarning($"[SoundMod] Failed to load '{file}': {ex.Message}");
}
}
Log.LogInfo($"[SoundMod] {loaded} WAV file(s) ready to inject.");
}
public static void TryInject(AudioClip clip, string clipName)
{
if (!TechTechsSoundModLocalSettings.Instance.ModToggle.Value) return;
if (!TechTechsSoundModLocalSettings.Instance.ModToggle.Value) return;
if (Injected.Contains(clipName)) return;
if (!PendingInjections.TryGetValue(clipName, out var samples)) return;
try {
var targetLength = clip.samples * clip.channels;
if (samples.Length != targetLength)
{
var resized = new float[targetLength];
Array.Copy(samples, resized, Math.Min(samples.Length, targetLength));
samples = resized;
}
clip.LoadAudioData();
clip.SetData(samples, 0);
Injected.Add(clipName);
Log.LogInfo($"[SoundMod] Injected '{clipName}' ({samples.Length} samples)");
}
catch (Exception ex) {
Log.LogWarning($"[SoundMod] Failed to inject '{clipName}': {ex.Message}");
}
DebugLogClip(clip, clipName);
RecordClipInfo(clip, clipName);
if (TechTechsSoundModLocalSettings.Instance.DumpClipInfoToggle.Value){ WriteDump(); }
}
private static float[] ParseWavToFloat(string path)
{
using var fs = File.OpenRead(path);
using var br = new BinaryReader(fs);
var riff = new string(br.ReadChars(4));
if (riff != "RIFF") throw new InvalidDataException($"Not a RIFF file: '{riff}'");
br.ReadInt32();
var wave = new string(br.ReadChars(4));
if (wave != "WAVE") throw new InvalidDataException($"Not a WAVE file: '{wave}'");
int channels = 0, sampleRate = 0, bitsPerSamp = 0;
byte[] pcmData = Array.Empty<byte>();
while (fs.Position < fs.Length - 8)
{
var id = new string(br.ReadChars(4));
var size = br.ReadInt32();
var next = fs.Position + size;
if (id == "fmt ")
{
var fmt = br.ReadInt16();
if (fmt != 1) throw new InvalidDataException($"Only PCM WAV supported (format={fmt}).");
channels = br.ReadInt16();
sampleRate = br.ReadInt32();
br.ReadInt32();
br.ReadInt16();
bitsPerSamp = br.ReadInt16();
}
else if (id == "data")
{
pcmData = br.ReadBytes(size);
}
fs.Position = next;
}
if (sampleRate == 0) throw new InvalidDataException("Missing fmt chunk.");
if (pcmData.Length == 0) throw new InvalidDataException("Missing data chunk.");
int bytesPerSample = bitsPerSamp / 8;
int sampleCount = pcmData.Length / bytesPerSample;
var samples = new float[sampleCount];
for (int i = 0; i < sampleCount; i++)
{
int o = i * bytesPerSample;
samples[i] = bitsPerSamp switch
{
8 => (pcmData[o] - 128) / 128f,
16 => BitConverter.ToInt16(pcmData, o) / 32768f,
24 => ((pcmData[o] | (pcmData[o+1] << 8) | (pcmData[o+2] << 16)) << 8) / 2147483648f,
32 => BitConverter.ToInt32(pcmData, o) / 2147483648f,
_ => throw new InvalidDataException($"Unsupported bit depth: {bitsPerSamp}")
};
}
return samples;
}
private static void WriteReadme()
{
var readme = Path.Combine(SoundsFolder, "README.txt");
if (File.Exists(readme)) return;
File.WriteAllText(readme, SoundMap.README);
}
}