Repository navigation
Expand file tree
/
Copy pathPositionalAudio.cs
More file actions
658 lines (576 loc) · 22.5 KB
/
Copy pathPositionalAudio.cs
File metadata and controls
658 lines (576 loc) · 22.5 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
namespace DigimonNOAccess
{
/// <summary>
/// Provides true positional audio using NAudio, bypassing the game's audio system.
/// Uses Steam Audio HRTF for 3D binaural spatialization when available,
/// falls back to stereo panning when phonon.dll is not present.
/// Supports loading custom WAV files for different object types.
/// Audio output is routed through the shared AudioOutputMixer (single WaveOutEvent).
/// </summary>
public class PositionalAudio : IDisposable
{
// Audio chain (no WaveOutEvent - uses shared AudioOutputMixer)
private PanningSampleProvider _panner;
private HrtfSampleProvider _hrtfProvider;
private VolumeSampleProvider _volumeProvider;
private SignalGenerator _signalGenerator;
// Whether this instance is using HRTF (true) or panning fallback (false)
private bool _useHrtf;
// Whether our _volumeProvider is currently registered with the mixer
private bool _addedToMixer;
// Position tracking (full 3D camera orientation for HRTF)
private float _targetX, _targetY, _targetZ;
private float _playerX, _playerY, _playerZ;
private float _camFwdX, _camFwdY, _camFwdZ;
private float _camUpX, _camUpY, _camUpZ;
private readonly object _positionLock = new object();
// Configuration
private float _maxDistance = 50f;
private float _minVolume = 0.05f;
private float _maxVolume = 0.8f;
private bool _isPlaying = false;
private bool _disposed = false;
private bool _useWavFile = false;
// Update thread
private Thread _updateThread;
private bool _shouldUpdate = false;
// Sound files directory
private static string _soundsPath;
// Different sound types for different objects
public enum SoundType
{
Item, // Items (item.wav)
NPC, // NPCs (potential npc.wav)
Enemy, // Enemy Digimon (potential enemie digimon.wav)
Transition, // Area transitions (transission.wav)
Facility // Facilities, fishing, toilets (facility.wav)
}
private SoundType _currentSoundType = SoundType.Item;
// Static audio cache: read each WAV file once, reuse across all instances
private static readonly Dictionary<string, (float[] samples, WaveFormat format)> _audioCache
= new Dictionary<string, (float[], WaveFormat)>();
private static readonly object _cacheLock = new object();
private static readonly Random _cacheRandom = new Random();
public PositionalAudio()
{
try
{
// Find sounds folder relative to mod DLL location (only resolves once)
if (_soundsPath == null)
{
string modPath = Path.GetDirectoryName(typeof(PositionalAudio).Assembly.Location);
_soundsPath = Path.Combine(Path.GetDirectoryName(modPath), "sounds");
if (!Directory.Exists(_soundsPath))
_soundsPath = Path.Combine(modPath, "sounds");
DebugLogger.Log($"[PositionalAudio] Sounds path: {_soundsPath}");
}
InitializeAudio(SoundType.Item);
}
catch (Exception ex)
{
DebugLogger.Log($"[PositionalAudio] Init error: {ex.Message}\n{ex.StackTrace}");
}
}
/// <summary>
/// Get the WAV file path for a sound type
/// </summary>
private string GetSoundFilePath(SoundType soundType)
{
string filename = soundType switch
{
SoundType.Item => "item.wav",
SoundType.NPC => "potential npc.wav",
SoundType.Enemy => "potential enemie digimon.wav",
SoundType.Transition => "transission.wav",
SoundType.Facility => "facility.wav",
_ => "item.wav"
};
return Path.Combine(_soundsPath, filename);
}
private void InitializeAudio(SoundType soundType)
{
// Clean up existing (removes from mixer if needed)
StopInternal();
_currentSoundType = soundType;
_useWavFile = false;
_useHrtf = false;
// Try to load WAV file first
string wavPath = GetSoundFilePath(soundType);
if (File.Exists(wavPath))
{
try
{
InitializeFromWavFile(wavPath);
_useWavFile = true;
return;
}
catch (Exception ex)
{
DebugLogger.Log($"[PositionalAudio] Failed to load WAV: {ex.Message}, falling back to generated tone");
}
}
else
{
// Facility type: no WAV = no audio (no tone fallback)
if (soundType == SoundType.Facility)
{
DebugLogger.Log($"[PositionalAudio] WAV file not found: {wavPath}, facility audio disabled");
return;
}
DebugLogger.Log($"[PositionalAudio] WAV file not found: {wavPath}, using generated tone");
}
// Fallback to generated tone (not used for Facility type)
InitializeFromGenerator(soundType);
}
private void InitializeFromWavFile(string wavPath)
{
// Use cached audio data - only reads from disk the first time per file
var (samples, format) = LoadOrGetCachedAudio(wavPath);
// Random start offset prevents comb filtering when many instances play the same WAV
int startOffset;
lock (_cacheLock) { startOffset = _cacheRandom.Next(samples.Length); }
var monoSource = new CachedLoopingSampleProvider(samples, format, startOffset);
BuildAudioChain(monoSource);
}
internal static (float[] samples, WaveFormat format) LoadOrGetCachedAudio(string wavPath)
{
lock (_cacheLock)
{
if (_audioCache.TryGetValue(wavPath, out var cached))
return cached;
}
// First load: read WAV file, decode to mono float samples
using (var reader = new AudioFileReader(wavPath))
{
ISampleProvider source;
if (reader.WaveFormat.Channels == 2)
source = new StereoToMonoSampleProvider(reader);
else
source = reader;
var allSamples = new List<float>();
float[] buf = new float[4096];
int read;
while ((read = source.Read(buf, 0, buf.Length)) > 0)
{
for (int i = 0; i < read; i++)
allSamples.Add(buf[i]);
}
var samples = allSamples.ToArray();
var format = WaveFormat.CreateIeeeFloatWaveFormat(reader.WaveFormat.SampleRate, 1);
lock (_cacheLock)
{
_audioCache[wavPath] = (samples, format);
}
DebugLogger.Log($"[PositionalAudio] Cached {wavPath}: {samples.Length} samples ({samples.Length / (float)format.SampleRate:F1}s)");
return (samples, format);
}
}
private void InitializeFromGenerator(SoundType soundType)
{
// Create signal generator based on sound type
float frequency;
SignalGeneratorType signalType;
switch (soundType)
{
case SoundType.NPC:
frequency = 600f;
signalType = SignalGeneratorType.Sin;
break;
case SoundType.Enemy:
frequency = 880f;
signalType = SignalGeneratorType.Square;
break;
case SoundType.Transition:
frequency = 523f;
signalType = SignalGeneratorType.Triangle;
break;
case SoundType.Item:
default:
frequency = 440f;
signalType = SignalGeneratorType.Sin;
break;
}
// Create mono signal generator
_signalGenerator = new SignalGenerator(44100, 1)
{
Frequency = frequency,
Type = signalType,
Gain = 0.3
};
BuildAudioChain(_signalGenerator);
DebugLogger.Log($"[PositionalAudio] Audio initialized with generator: {soundType}, freq={frequency}Hz");
}
/// <summary>
/// Build the audio processing chain from a mono source.
/// Uses HRTF when available, falls back to stereo panning.
/// Chain ends at VolumeSampleProvider which is registered with AudioOutputMixer.
/// </summary>
private void BuildAudioChain(ISampleProvider monoSource)
{
ISampleProvider stereoSource;
if (SteamAudioManager.IsAvailable)
{
// HRTF: spatialize mono → stereo
_hrtfProvider = new HrtfSampleProvider(monoSource);
stereoSource = _hrtfProvider;
_useHrtf = true;
_panner = null;
}
else
{
// Fallback: stereo panning (no front/back distinction)
_panner = new PanningSampleProvider(monoSource)
{
Pan = 0f
};
stereoSource = _panner;
_hrtfProvider = null;
_useHrtf = false;
}
_volumeProvider = new VolumeSampleProvider(stereoSource)
{
Volume = 0f // Start silent, UpdateAudioParameters will set real volume
};
}
/// <summary>
/// Start playing positional audio for a target
/// </summary>
public void StartTracking(SoundType soundType, float maxDistance = 50f)
{
try
{
if (_currentSoundType != soundType || _volumeProvider == null)
{
InitializeAudio(soundType);
}
_maxDistance = maxDistance;
_isPlaying = true;
_shouldUpdate = true;
// Register with shared mixer if not already
if (!_addedToMixer && _volumeProvider != null)
{
AudioOutputMixer.AddInput(_volumeProvider);
_addedToMixer = true;
}
// Start position update thread
if (_updateThread == null || !_updateThread.IsAlive)
{
_updateThread = new Thread(UpdateLoop)
{
IsBackground = true,
Name = "PositionalAudio_Update"
};
_updateThread.Start();
}
// Reduced logging - only significant events
}
catch (Exception ex)
{
DebugLogger.Log($"[PositionalAudio] StartTracking error: {ex.Message}");
}
}
/// <summary>
/// Stop playing
/// </summary>
public void Stop()
{
_shouldUpdate = false;
_isPlaying = false;
StopInternal();
DebugLogger.Log("[PositionalAudio] Stopped");
}
/// <summary>
/// Change the sound type while playing (for continuous tracking mode)
/// </summary>
public void ChangeSoundType(SoundType soundType, float maxDistance = 50f)
{
if (_currentSoundType == soundType)
{
// Same sound type, just update max distance
_maxDistance = maxDistance;
return;
}
// Different sound type - reinitialize audio
DebugLogger.Log($"[PositionalAudio] Changing sound from {_currentSoundType} to {soundType}");
bool wasPlaying = _isPlaying;
_maxDistance = maxDistance;
// Reinitialize with new sound type (StopInternal removes from mixer)
InitializeAudio(soundType);
// Resume if we were playing
if (wasPlaying)
{
_isPlaying = true;
_shouldUpdate = true;
// Re-register with mixer
if (!_addedToMixer && _volumeProvider != null)
{
AudioOutputMixer.AddInput(_volumeProvider);
_addedToMixer = true;
}
// Restart update thread if needed
if (_updateThread == null || !_updateThread.IsAlive)
{
_updateThread = new Thread(UpdateLoop)
{
IsBackground = true,
Name = "PositionalAudio_Update"
};
_updateThread.Start();
}
}
}
private void StopInternal()
{
// Remove from mixer first (before disposing the chain)
if (_addedToMixer && _volumeProvider != null)
{
AudioOutputMixer.RemoveInput(_volumeProvider);
_addedToMixer = false;
}
try
{
_hrtfProvider?.Dispose();
_hrtfProvider = null;
}
catch { }
_signalGenerator = null;
_panner = null;
_volumeProvider = null;
}
/// <summary>
/// Update the target position (call this from game thread)
/// </summary>
public void UpdateTargetPosition(float x, float y, float z)
{
lock (_positionLock)
{
_targetX = x;
_targetY = y;
_targetZ = z;
}
}
/// <summary>
/// Update the player position and camera orientation (call this from game thread).
/// Uses full 3D camera vectors for proper HRTF spatialization matching the game's AudioListener.
/// </summary>
public void UpdatePlayerPosition(float x, float y, float z,
float camFwdX, float camFwdY, float camFwdZ,
float camUpX, float camUpY, float camUpZ)
{
lock (_positionLock)
{
_playerX = x;
_playerY = y;
_playerZ = z;
_camFwdX = camFwdX;
_camFwdY = camFwdY;
_camFwdZ = camFwdZ;
_camUpX = camUpX;
_camUpY = camUpY;
_camUpZ = camUpZ;
}
}
/// <summary>
/// Background thread that continuously updates audio parameters
/// </summary>
private void UpdateLoop()
{
while (_shouldUpdate && !_disposed)
{
try
{
UpdateAudioParameters();
Thread.Sleep(16); // ~60fps update rate
}
catch (Exception ex)
{
DebugLogger.Log($"[PositionalAudio] Update error: {ex.Message}");
Thread.Sleep(100);
}
}
}
private void UpdateAudioParameters()
{
if (_volumeProvider == null)
return;
float targetX, targetY, targetZ;
float playerX, playerY, playerZ;
float cfX, cfY, cfZ, cuX, cuY, cuZ;
lock (_positionLock)
{
targetX = _targetX;
targetY = _targetY;
targetZ = _targetZ;
playerX = _playerX;
playerY = _playerY;
playerZ = _playerZ;
cfX = _camFwdX; cfY = _camFwdY; cfZ = _camFwdZ;
cuX = _camUpX; cuY = _camUpY; cuZ = _camUpZ;
}
// Calculate 3D direction and distance to target
float dx = targetX - playerX;
float dy = targetY - playerY;
float dz = targetZ - playerZ;
float distance = (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
// Normalize direction
if (distance > 0.01f)
{
dx /= distance;
dy /= distance;
dz /= distance;
}
if (_useHrtf && _hrtfProvider != null)
{
// Full 3D HRTF: transform world direction into camera's local space.
// This matches how the game's own AudioListener on the camera works.
// Camera right = cross(camUp, camForward) in Unity's left-handed system
float crX = cuY * cfZ - cuZ * cfY;
float crY = cuZ * cfX - cuX * cfZ;
float crZ = cuX * cfY - cuY * cfX;
// Project direction onto camera axes
float localRight = dx * crX + dy * crY + dz * crZ;
float localUp = dx * cuX + dy * cuY + dz * cuZ;
float localForward = dx * cfX + dy * cfY + dz * cfZ;
// Steam Audio: right-handed, -Z = forward
_hrtfProvider.SetDirection(localRight, localUp, -localForward);
}
else if (_panner != null)
{
// Panning fallback: project onto camera right axis for L/R
float crX = cuY * cfZ - cuZ * cfY;
float crY = cuZ * cfX - cuX * cfZ;
float crZ = cuX * cfY - cuY * cfX;
float pan = dx * crX + dy * crY + dz * crZ;
_panner.Pan = Math.Max(-1f, Math.Min(1f, pan));
}
// Volume based on distance - squared falloff for realistic attenuation
// At 25% range: ~56% vol, at 50%: ~25% vol, at 75%: ~6% vol
float normalizedDist = Math.Min(1f, distance / _maxDistance);
if (distance < 3f)
{
_volumeProvider.Volume = _maxVolume;
}
else
{
float falloff = (1f - normalizedDist) * (1f - normalizedDist); // squared
float volume = _minVolume + falloff * (_maxVolume - _minVolume);
_volumeProvider.Volume = volume;
}
// Pitch modulation for generated tones (distance cue)
if (_signalGenerator != null && !_useWavFile)
{
float basePitch = _currentSoundType switch
{
SoundType.NPC => 600f,
SoundType.Enemy => 880f,
SoundType.Transition => 523f,
_ => 440f
};
float pitchMod = 1f + (1f - normalizedDist) * 0.2f;
_signalGenerator.Frequency = basePitch * pitchMod;
}
}
/// <summary>
/// Check if currently playing
/// </summary>
public bool IsPlaying => _isPlaying;
/// <summary>
/// Set the max volume for this source. Used to make non-nearest sources quieter.
/// </summary>
public void SetMaxVolume(float maxVol)
{
_maxVolume = maxVol;
}
/// <summary>
/// Get current distance to target
/// </summary>
public float GetCurrentDistance()
{
lock (_positionLock)
{
float dx = _targetX - _playerX;
float dy = _targetY - _playerY;
float dz = _targetZ - _playerZ;
return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_shouldUpdate = false;
_isPlaying = false;
try
{
_updateThread?.Join(500);
}
catch { }
StopInternal();
}
}
/// <summary>
/// Wraps a WaveStream to loop continuously
/// </summary>
public class LoopingWaveProvider : IWaveProvider
{
private readonly WaveStream _sourceStream;
public LoopingWaveProvider(WaveStream sourceStream)
{
_sourceStream = sourceStream;
}
public WaveFormat WaveFormat => _sourceStream.WaveFormat;
public int Read(byte[] buffer, int offset, int count)
{
int totalBytesRead = 0;
while (totalBytesRead < count)
{
int bytesRead = _sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
if (bytesRead == 0)
{
// End of stream, loop back to beginning
_sourceStream.Position = 0;
bytesRead = _sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
if (bytesRead == 0)
break; // Empty stream
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
}
/// <summary>
/// Loops over a shared float[] sample buffer without owning or copying it.
/// Each instance has its own read position, enabling many simultaneous players
/// of the same sound with zero disk I/O after the first load.
/// </summary>
public class CachedLoopingSampleProvider : ISampleProvider
{
private readonly float[] _samples;
private int _position;
public WaveFormat WaveFormat { get; }
public CachedLoopingSampleProvider(float[] samples, WaveFormat format, int startOffset = 0)
{
_samples = samples;
WaveFormat = format;
_position = samples.Length > 0 ? startOffset % samples.Length : 0;
}
public int Read(float[] buffer, int offset, int count)
{
if (_samples.Length == 0) return 0;
for (int i = 0; i < count; i++)
{
buffer[offset + i] = _samples[_position];
_position++;
if (_position >= _samples.Length)
_position = 0;
}
return count;
}
}
}