-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioMirror.cs
169 lines (157 loc) · 5.8 KB
/
AudioMirror.cs
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
using CSCore.CoreAudioAPI;
using CSCore.SoundIn;
using CSCore.SoundOut;
using CSCore.Streams;
namespace WASAPI_Audio_Mirror
{
internal class AudioMirror
{
private Settings settings;
private MMDeviceEnumerator enumerator;
private CancellationToken token;
private volatile bool started = false;
private volatile bool paused = false;
private volatile WasapiCapture capture = null;
private volatile SoundInSource source = null;
private HashSet<string> outputStrings = new HashSet<string>(1);
private HashSet<WasapiOut> outputs = new HashSet<WasapiOut>(1);
public AudioMirror(Settings settings, MMDeviceEnumerator deviceEnumerator, CancellationToken token)
{
this.settings = settings;
this.enumerator = deviceEnumerator;
this.token = token;
token.Register(stopMirror);
}
public void updateSettings(Settings settings)
{
this.settings = settings;
}
public void startMirror()
{
if (!started && settings.selectedInputDeviceId != null)
{
try
{
capture = new WasapiLoopbackCapture();
MMDevice device = enumerator.GetDevice(settings.selectedInputDeviceId);
if (device.DeviceState == DeviceState.Active)
{
System.Diagnostics.Debug.WriteLine("Starting audio mirror");
capture.Device = device;
capture.Initialize();
if (paused)
{
capture.Stop();
}
else
{
capture.Start();
}
source = new SoundInSource(capture) { FillWithZeros = true };
started = true;
}
}
catch (Exception) { }
}
}
public void stopMirror()
{
System.Diagnostics.Debug.WriteLine("Stopping audio mirror");
try
{
outputStrings.Clear();
outputs.RemoveWhere(output =>
{
output.Dispose();
return true;
});
if (source != null)
{
source.Dispose();
}
if (capture != null)
{
capture.Dispose();
}
}
catch (Exception) { }
started = false;
}
public void pauseMirror()
{
System.Diagnostics.Debug.WriteLine("Pausing audio mirror");
paused = true;
if (capture != null)
{
capture.Stop();
}
}
public void resumeMirror()
{
System.Diagnostics.Debug.WriteLine("Resuming audio mirror");
paused = false;
if (capture != null)
{
capture.Start();
}
}
public void updateOutputs()
{
if (started && capture != null && source != null && capture.Device.DeviceState == DeviceState.Active)
{
System.Diagnostics.Debug.WriteLine("Updating audio mirror");
try
{
outputs.RemoveWhere(output => {
if (output.Device.DeviceState != DeviceState.Active)
{
outputStrings.Remove(output.Device.DeviceID);
return true;
}
return false;
});
outputStrings.RemoveWhere(id =>
{
if (!settings.selectedOutputDeviceIds.Contains(id))
{
outputs.RemoveWhere(output =>
{
if (id.Equals(output.Device.DeviceID))
{
output.Dispose();
return true;
}
return false;
});
return true;
}
return false;
});
foreach (string id in settings.selectedOutputDeviceIds)
{
if (!outputStrings.Contains(id))
{
try
{
System.Diagnostics.Debug.WriteLine("Searching for: " + id);
MMDevice device = enumerator.GetDevice(id);
System.Diagnostics.Debug.WriteLine("Found Name: " + device.FriendlyName + " State: " + device.DeviceState.ToString());
if (device.DeviceState == DeviceState.Active)
{
WasapiOut output = new WasapiOut(true, AudioClientShareMode.Shared, 5);
output.Device = device;
output.Initialize(source);
output.Play();
outputs.Add(output);
outputStrings.Add(id);
}
}
catch (Exception) { }
}
}
}
catch (Exception) { }
}
}
}
}