Repository navigation
Expand file tree
/
Copy pathAudioOutputMixer.cs
More file actions
99 lines (88 loc) · 3.13 KB
/
Copy pathAudioOutputMixer.cs
File metadata and controls
99 lines (88 loc) · 3.13 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
using System;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
namespace DigimonNOAccess
{
/// <summary>
/// Shared audio output that mixes all positional audio sources through a single WaveOutEvent.
/// Eliminates stuttering caused by multiple WaveOutEvent instances competing for audio resources.
/// All PositionalAudio instances register their provider chains here instead of owning WaveOutEvents.
/// </summary>
public static class AudioOutputMixer
{
private static WaveOutEvent _waveOut;
private static MixingSampleProvider _mixer;
private static bool _initialized;
public static bool IsInitialized => _initialized;
public static void Initialize()
{
if (_initialized) return;
try
{
// 44100Hz stereo matches Steam Audio HRTF output and all our source chains
_mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2))
{
ReadFully = true // Output silence when no inputs (keeps WaveOut alive)
};
_waveOut = new WaveOutEvent
{
DesiredLatency = 200
};
_waveOut.PlaybackStopped += (sender, args) =>
{
if (args.Exception != null)
DebugLogger.Error($"[AudioMixer] Playback error: {args.Exception.GetType().Name}: {args.Exception.Message}");
};
_waveOut.Init(_mixer);
_waveOut.Play();
_initialized = true;
DebugLogger.Log("[AudioMixer] Initialized - single output for all positional audio");
}
catch (Exception ex)
{
DebugLogger.Error($"[AudioMixer] Init failed: {ex.Message}");
}
}
public static void AddInput(ISampleProvider source)
{
if (!_initialized || _mixer == null || source == null) return;
try
{
_mixer.AddMixerInput(source);
}
catch (Exception ex)
{
DebugLogger.Error($"[AudioMixer] AddInput error: {ex.Message}");
}
}
public static void RemoveInput(ISampleProvider source)
{
if (!_initialized || _mixer == null || source == null) return;
try
{
_mixer.RemoveMixerInput(source);
}
catch (Exception ex)
{
DebugLogger.Error($"[AudioMixer] RemoveInput error: {ex.Message}");
}
}
public static void Shutdown()
{
if (!_initialized) return;
_initialized = false;
try
{
_waveOut?.Stop();
_waveOut?.Dispose();
_waveOut = null;
_mixer = null;
}
catch (Exception ex)
{
DebugLogger.Error($"[AudioMixer] Shutdown error: {ex.Message}");
}
DebugLogger.Log("[AudioMixer] Shut down");
}
}
}