forked from Anc813/MicMute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
368 lines (312 loc) · 12.1 KB
/
MainForm.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
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
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
using Microsoft.Win32;
using Shortcut;
using System;
using System.Drawing;
using System.IO;
using System.Media;
using System.Reactive;
using System.Windows.Forms;
namespace MicMute
{
public partial class MainForm : Form
{
const string DEFAULT_RECORDING_DEVICE = "Default recording device";
public CoreAudioController AudioController = new CoreAudioController();
private readonly HotkeyBinder hotkeyBinder = new HotkeyBinder();
private readonly RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\MicMute");
// toggle
private readonly string registryKeyName = "Hotkey";
private Hotkey hotkey;
// mute
private readonly string registryKeyMute = "HotkeyMute";
private Hotkey muteHotkey;
// unmute
private readonly string registryKeyUnmute = "HotkeyUnmute";
private Hotkey unMuteHotkey;
private readonly string registryDeviceId = "DeviceId";
private readonly string registryDeviceName = "DeviceName";
private string selectedDeviceId;
private string selectedDeviceName;
private MicSelectorForm micSelectorForm;
enum MicStatus
{
Initial, On, Off, Error
}
private MicStatus currentStatus;
private bool myVisible;
public bool MyVisible
{
get { return myVisible; }
set { myVisible = value; Visible = value; }
}
public MainForm()
{
InitializeComponent();
}
private void OnNextDevice(DeviceChangedArgs next)
{
UpdateSelectedDevice();
}
private void MyHide()
{
ShowInTaskbar = false;
Location = new Point(-10000, -10000);
MyVisible = false;
}
private void MyShow()
{
MyVisible = true;
ShowInTaskbar = true;
CenterToScreen();
}
private void MainForm_Load(object sender, EventArgs e)
{
MyHide();
selectedDeviceId = (string)registryKey.GetValue(registryDeviceId) ?? "";
selectedDeviceName = (string)registryKey.GetValue(registryDeviceName) ?? DEFAULT_RECORDING_DEVICE;
UpdateSelectedDevice();
AudioController.AudioDeviceChanged.Subscribe(OnNextDevice);
// toggle
var hotkeyValue = registryKey.GetValue(registryKeyName);
if (hotkeyValue != null)
{
var converter = new Shortcut.Forms.HotkeyConverter();
hotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
}
// mute
hotkeyValue = registryKey.GetValue(registryKeyMute);
if (hotkeyValue != null)
{
var converter = new Shortcut.Forms.HotkeyConverter();
muteHotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Bind(muteHotkey).To(MuteMicStatus);
}
// unmute
hotkeyValue = registryKey.GetValue(registryKeyUnmute);
if (hotkeyValue != null)
{
var converter = new Shortcut.Forms.HotkeyConverter();
unMuteHotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey)) hotkeyBinder.Bind(unMuteHotkey).To(UnMuteMicStatus);
}
//AudioController.AudioDeviceChanged.Subscribe(x =>
//{
// Debug.WriteLine("{0} - {1}", x.Device.Name, x.ChangedType.ToString());
//});
}
private void OnMuteChanged(DeviceMuteChangedArgs next)
{
UpdateStatus(next.Device);
}
IDisposable muteChangedSubscription;
public void UpdateDevice(IDevice device)
{
muteChangedSubscription?.Dispose();
muteChangedSubscription = device?.MuteChanged.Subscribe(OnMuteChanged);
UpdateStatus(device);
}
public IDevice getSelectedDevice()
{
return selectedDeviceId == "" ? AudioController.DefaultCaptureDevice : AudioController.GetDevice(new Guid(selectedDeviceId), DeviceState.Active);
}
public void UpdateSelectedDevice()
{
UpdateDevice(getSelectedDevice());
}
Icon iconOff = Properties.Resources.off;
Icon iconOn = Properties.Resources.on;
Icon iconError = Properties.Resources.error;
public void PlaySound(string relativePath)
{
string path = Path.Combine(Application.StartupPath, relativePath);
if (File.Exists(path))
{
SoundPlayer simpleSound = new SoundPlayer(path);
simpleSound.Play();
}
}
public void UpdateStatus(IDevice device)
{
MicStatus newStatus = (device != null) ? (device.IsMuted ? MicStatus.Off : MicStatus.On) : MicStatus.Error;
bool playSound = currentStatus != MicStatus.Initial && currentStatus != newStatus;
currentStatus = newStatus;
switch (currentStatus)
{
case MicStatus.On:
UpdateIcon(iconOn, device.FullName);
if (playSound) PlaySound("on.wav");
break;
case MicStatus.Off:
UpdateIcon(iconOff, device.FullName);
if (playSound) PlaySound("off.wav");
break;
case MicStatus.Error:
UpdateIcon(iconError, "< No device >");
if (playSound) PlaySound("error.wav");
break;
}
}
private void UpdateIcon(Icon icon, string tooltipText)
{
this.icon.Icon = icon;
this.icon.Text = tooltipText;
}
public async void ToggleMicStatus()
{
await getSelectedDevice()?.ToggleMuteAsync();
}
public async void MuteMicStatus()
{
await getSelectedDevice()?.SetMuteAsync(true);
}
public async void UnMuteMicStatus()
{
await getSelectedDevice()?.SetMuteAsync(false);
}
private void Icon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ToggleMicStatus();
}
}
private void HotkeyToolStripMenuItem_Click(object sender, EventArgs e)
{
// toggle
if (hotkey != null)
{
hotkeyTextBox.Hotkey = hotkey;
if (hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Unbind(hotkey);
}
// mute
if (muteHotkey != null)
{
muteTextBox.Hotkey = muteHotkey;
if (hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Unbind(muteHotkey);
}
// unmute
if (unMuteHotkey != null)
{
unmuteTextBox.Hotkey = unMuteHotkey;
if (hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey)) hotkeyBinder.Unbind(unMuteHotkey);
}
MyShow();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MyVisible)
{
MyHide();
e.Cancel = true;
hotkey = hotkeyTextBox.Hotkey;
if (hotkey == null)
{
registryKey.DeleteValue(registryKeyName, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey))
{
registryKey.SetValue(registryKeyName, hotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
}
}
muteHotkey = muteTextBox.Hotkey;
if (muteHotkey == null)
{
registryKey.DeleteValue(registryKeyMute, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey))
{
registryKey.SetValue(registryKeyMute, muteHotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Bind(muteHotkey).To(MuteMicStatus);
}
}
unMuteHotkey = unmuteTextBox.Hotkey;
if (unMuteHotkey == null)
{
registryKey.DeleteValue(registryKeyUnmute, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey))
{
registryKey.SetValue(registryKeyUnmute, unMuteHotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey)) hotkeyBinder.Bind(unMuteHotkey).To(UnMuteMicStatus);
}
}
}
}
private void ButtonReset_Click(object sender, EventArgs e)
{
hotkeyTextBox.Hotkey = null;
hotkeyTextBox.Text = "None";
}
private void muteReset_Click(object sender, EventArgs e)
{
muteTextBox.Hotkey = null;
muteTextBox.Text = "None";
}
private void unmuteReset_Click(object sender, EventArgs e)
{
unmuteTextBox.Hotkey = null;
unmuteTextBox.Text = "None";
}
private void ExitMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
micSelectorForm = new MicSelectorForm();
ComboBox comboBox = micSelectorForm.cbMics;
comboBox.Items.Clear();
bool registryExists = false;
ComboboxItem defaultItem = new ComboboxItem();
defaultItem.Text = DEFAULT_RECORDING_DEVICE;
defaultItem.deviceId = "";
comboBox.Items.Add(defaultItem);
if (selectedDeviceId == "")
{
registryExists = true;
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
foreach (CoreAudioDevice device in AudioController.GetCaptureDevices())
{
if (device.State == DeviceState.Active)
{
ComboboxItem item = new ComboboxItem();
item.Text = device.FullName;
item.deviceId = device.Id.ToString();
comboBox.Items.Add(item);
if (item.deviceId == selectedDeviceId)
{
registryExists = true;
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
}
}
if (!registryExists) {
ComboboxItem item = new ComboboxItem();
item.Text = "(unavailable) " + registryDeviceName.ToString();
item.deviceId = selectedDeviceId.ToString();
comboBox.Items.Add(item);
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
DialogResult result = micSelectorForm.ShowDialog();
Console.WriteLine(result);
ComboboxItem selectedItem = (ComboboxItem)comboBox.SelectedItem;
registryKey.SetValue(registryDeviceId, selectedItem.deviceId);
registryKey.SetValue(registryDeviceName, selectedItem.Text);
selectedDeviceName = selectedItem.Text;
selectedDeviceId = selectedItem.deviceId;
micSelectorForm.Dispose();
UpdateSelectedDevice();
}
}
}