forked from flipswitchingmonkey/FlexASIO_GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
474 lines (404 loc) · 18.3 KB
/
Form1.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using Nett;
using NAudio.CoreAudioApi;
namespace FlexASIOGUI
{
public partial class Form1 : Form
{
private readonly bool _initDone;
private readonly string _tomlPath;
private FlexGuiConfig _flexGuiConfig;
private const string FlexasioGuiVersion = "EIMChanged";
private const string FlexasioVersion = "1.9";
private const string TomlName = "FlexASIO.toml";
private const string DocUrl = "https://github.com/EchoInMirror/FlexASIO_GUI";
public Form1()
{
InitializeComponent();
Text = $@"FlexASIO GUI {FlexasioGuiVersion}";
var customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// get the value of the "Language for non-Unicode programs" setting (1252 for English)
// note: in Win11 this could be UTF-8 already, since it's natively supported
Encoding.GetEncoding("GBK");
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
CultureInfo.DefaultThreadCurrentCulture = customCulture;
CultureInfo.DefaultThreadCurrentUICulture = customCulture;
_tomlPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\{TomlName}";
this.LoadFlexAsioConfig(_tomlPath);
_initDone = true;
SetStatusMessage($"FlexASIO GUI for FlexASIO {FlexasioVersion} started (NAudio 2.1.0)");
GenerateOutput();
}
public sealed override string Text
{
get => base.Text;
set => base.Text = value;
}
private void LoadFlexAsioConfig(string tomlPath)
{
_flexGuiConfig = new FlexGuiConfig();
if (File.Exists(tomlPath))
{
_flexGuiConfig = Toml.ReadFile<FlexGuiConfig>(tomlPath);
}
numericBufferSize.Maximum = 8192;
numericBufferSize.Increment = 16;
numericLatencyInput.Increment = 0.1m;
numericLatencyOutput.Increment = 0.1m;
comboBackend.Items.Add("Windows WASAPI");
// for (var i = 0; i < Configuration.HostApiCount; i++)
// {
// comboBackend.Items.Add(Configuration.GetHostApiInfo(i).name);
// }
comboBackend.SelectedIndex = comboBackend.Items.Contains(_flexGuiConfig.Backend) ? comboBackend.Items.IndexOf(_flexGuiConfig.Backend) : 0;
if (_flexGuiConfig.BufferSizeSamples != null)
numericBufferSize.Value = (Int64)_flexGuiConfig.BufferSizeSamples;
checkBoxSetBufferSize.Checked = numericBufferSize.Enabled = _flexGuiConfig.BufferSizeSamples != null;
treeDevicesInput.SelectedNode = treeDevicesInput.Nodes.Cast<TreeNode>().FirstOrDefault(x => x.Text == (_flexGuiConfig.Input.Device == "" ? "(None)" : _flexGuiConfig.Input.Device));
treeDevicesOutput.SelectedNode = treeDevicesOutput.Nodes.Cast<TreeNode>().FirstOrDefault(x => x.Text == (_flexGuiConfig.Output.Device == "" ? "(None)" : _flexGuiConfig.Output.Device));
checkBoxSetInputLatency.Checked = numericLatencyInput.Enabled = _flexGuiConfig.Input.SuggestedLatencySeconds != null;
checkBoxSetOutputLatency.Checked = numericLatencyOutput.Enabled = _flexGuiConfig.Output.SuggestedLatencySeconds != null;
if (_flexGuiConfig.Input.SuggestedLatencySeconds != null)
numericLatencyInput.Value = (decimal)(double)_flexGuiConfig.Input.SuggestedLatencySeconds;
if (_flexGuiConfig.Output.SuggestedLatencySeconds != null)
numericLatencyOutput.Value = (decimal)(double)_flexGuiConfig.Output.SuggestedLatencySeconds;
numericChannelsInput.Value = _flexGuiConfig.Input.Channels ?? 0;
numericChannelsOutput.Value = _flexGuiConfig.Output.Channels ?? 0;
checkBoxWasapiInputSet.Checked = _flexGuiConfig.Input.WasapiExclusiveMode != null || _flexGuiConfig.Input.WasapiAutoConvert != null;
checkBoxWasapiOutputSet.Checked = _flexGuiConfig.Output.WasapiExclusiveMode != null || _flexGuiConfig.Output.WasapiAutoConvert != null;
wasapiExclusiveInput.Enabled = _flexGuiConfig.Input.WasapiExclusiveMode != null;
wasapiExclusiveInput.Checked = _flexGuiConfig.Input.WasapiExclusiveMode ?? false;
wasapiExclusiveOutput.Enabled = _flexGuiConfig.Output.WasapiExclusiveMode != null;
wasapiExclusiveOutput.Checked = _flexGuiConfig.Output.WasapiExclusiveMode ?? false;
wasapiAutoConvertInput.Enabled = _flexGuiConfig.Input.WasapiAutoConvert != null;
wasapiAutoConvertInput.Checked = _flexGuiConfig.Input.WasapiAutoConvert ?? false;
wasapiAutoConvertOutput.Enabled = _flexGuiConfig.Output.WasapiAutoConvert != null;
wasapiAutoConvertOutput.Checked = _flexGuiConfig.Output.WasapiAutoConvert ?? false;
}
private static TreeNode[] GetDevices(bool input)
{
var treeNodes = new List<TreeNode> { new TreeNode("(None)") };
var devices = new MMDeviceEnumerator();
if (!input)
{
var endpoints = devices.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
treeNodes.AddRange(endpoints.Select(endpoint => new TreeNode(endpoint.FriendlyName)));
}
else
{
var endpoints = devices.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
treeNodes.AddRange(endpoints.Select(endpoint => new TreeNode(endpoint.FriendlyName)));
}
return treeNodes.ToArray();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBackend_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is not ComboBox o) return;
var selectedBackend = o.SelectedItem as string;
RefreshDevices(selectedBackend);
if (selectedBackend == "(None)") selectedBackend = "";
_flexGuiConfig.Backend = selectedBackend;
GenerateOutput();
}
private void RefreshDevices(string selectedBackend)
{
var tmpInput = treeDevicesInput.SelectedNode;
var tmpOutput = treeDevicesOutput.SelectedNode;
if (selectedBackend == null) return;
treeDevicesInput.Nodes.Clear();
treeDevicesInput.Nodes.AddRange(GetDevices(true));
for (var i = 0; i < treeDevicesInput!.Nodes.Count; i++)
{
if (treeDevicesInput?.Nodes[i].Text != tmpInput?.Text) continue;
treeDevicesInput!.SelectedNode = treeDevicesInput.Nodes[i];
break;
}
treeDevicesOutput.Nodes.Clear();
treeDevicesOutput.Nodes.AddRange(GetDevices(false));
for (var i = 0; i < treeDevicesOutput!.Nodes.Count; i++)
{
if (treeDevicesOutput?.Nodes[i].Text != tmpOutput?.Text) continue;
treeDevicesOutput!.SelectedNode = treeDevicesOutput.Nodes[i];
break;
}
}
private void GenerateOutput()
{
if (!_initDone) return;
if (!checkBoxSetBufferSize.Checked) _flexGuiConfig.BufferSizeSamples = null;
if (!checkBoxSetInputLatency.Checked) _flexGuiConfig.Input.SuggestedLatencySeconds = null;
if (!checkBoxSetOutputLatency.Checked) _flexGuiConfig.Output.SuggestedLatencySeconds = null;
if (!checkBoxWasapiInputSet.Checked)
{
_flexGuiConfig.Input.WasapiAutoConvert = null;
_flexGuiConfig.Input.WasapiExclusiveMode = null;
}
if (!checkBoxWasapiOutputSet.Checked)
{
_flexGuiConfig.Output.WasapiAutoConvert = null;
_flexGuiConfig.Output.WasapiExclusiveMode = null;
}
configOutput.Clear();
configOutput.Text = Toml.WriteString(_flexGuiConfig);
}
private void SetStatusMessage(string msg)
{
toolStripStatusLabel1.Text = $@"{DateTime.Now.ToShortDateString()} - {DateTime.Now.ToShortTimeString()}: {msg}";
}
private void btClipboard_Click(object sender, EventArgs e)
{
Clipboard.SetText(configOutput.Text);
SetStatusMessage("Configuration copied to Clipboard");
}
private void btSaveToProfile_Click(object sender, EventArgs e)
{
File.WriteAllText(_tomlPath, configOutput.Text);
SetStatusMessage($"Configuration written to {_tomlPath}");
}
private void btSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
saveFileDialog.FileName = TomlName;
var ret = saveFileDialog.ShowDialog();
if (ret == DialogResult.OK)
{
File.WriteAllText(saveFileDialog.FileName, configOutput.Text);
}
SetStatusMessage($"Configuration written to {saveFileDialog.FileName}");
}
private void treeDevicesInput_AfterSelect(object sender, TreeViewEventArgs e)
{
if (sender == null) return;
e.Node!.Checked = true;
OnTreeViewSelected(eventArgs: e, forInput: true);
}
private void treeDevicesOutput_AfterSelect(object sender, TreeViewEventArgs e)
{
if (sender == null) return;
e.Node!.Checked = true;
OnTreeViewSelected(eventArgs: e, forInput: false);
}
private void treeDevicesOutput_AfterCheck(object sender, TreeViewEventArgs e)
{
if (sender == null) return;
OnTreeViewSelected(eventArgs: e, forInput: false);
}
private void treeDevicesInput_AfterCheck(object sender, TreeViewEventArgs e)
{
if (sender == null) return;
OnTreeViewSelected(eventArgs: e, forInput: true);
}
private static void UnCheckAllOthers(TreeNode treeNode)
{
foreach (TreeNode node in treeNode.TreeView.Nodes)
{
if (node != treeNode)
{
node.Checked = false;
}
}
}
private void OnTreeViewSelected(TreeViewEventArgs eventArgs, bool forInput)
{
if (eventArgs.Node!.Checked != true) return;
if (forInput)
_flexGuiConfig.Input.Device = eventArgs.Node.Text == @"(None)" ? "" : eventArgs.Node.Text;
else
_flexGuiConfig.Output.Device = eventArgs.Node.Text == @"(None)" ? "" : eventArgs.Node.Text;
UnCheckAllOthers(eventArgs.Node);
GenerateOutput();
}
private void numericChannelsOutput_ValueChanged(object sender, EventArgs e)
{
var o = sender as NumericUpDown;
if (o == null) return;
_flexGuiConfig.Output.Channels = (o.Value > 0 ? (int?)o.Value : null);
GenerateOutput();
}
private void numericChannelsInput_ValueChanged(object sender, EventArgs e)
{
var o = sender as NumericUpDown;
if (o == null) return;
_flexGuiConfig.Input.Channels = (o.Value > 0 ? (int?)o.Value : null);
GenerateOutput();
}
private void numericLatencyOutput_ValueChanged(object sender, EventArgs e)
{
var o = sender as NumericUpDown;
if (o == null) return;
if (checkBoxSetOutputLatency.Enabled)
{
_flexGuiConfig.Output.SuggestedLatencySeconds = (o.Value > 0 ? (double)o.Value : 0);
GenerateOutput();
}
}
private void numericLatencyInput_ValueChanged(object sender, EventArgs e)
{
var o = sender as NumericUpDown;
if (o == null) return;
if (checkBoxSetInputLatency.Enabled)
{
_flexGuiConfig.Input.SuggestedLatencySeconds = (o.Value > 0 ? (double)o.Value : 0);
GenerateOutput();
}
}
private void wasapiAutoConvertOutput_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
_flexGuiConfig.Output.WasapiAutoConvert = o.Checked;
GenerateOutput();
}
private void wasapiExclusiveOutput_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
_flexGuiConfig.Output.WasapiExclusiveMode = o.Checked;
GenerateOutput();
}
private void wasapiAutoConvertInput_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
_flexGuiConfig.Input.WasapiAutoConvert = o.Checked;
GenerateOutput();
}
private void wasapiExclusiveInput_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
_flexGuiConfig.Input.WasapiExclusiveMode = o.Checked;
GenerateOutput();
}
private void numericBufferSize_ValueChanged(object sender, EventArgs e)
{
var o = sender as NumericUpDown;
if (o == null) return;
_flexGuiConfig.BufferSizeSamples = (o.Value > 0 ? (int)o.Value : 0);
GenerateOutput();
}
private void checkBoxSetInputLatency_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
numericLatencyInput.Enabled = o.Checked;
if (o.Checked == false)
{
_flexGuiConfig.Input.SuggestedLatencySeconds = null;
}
else
{
numericLatencyInput_ValueChanged(numericLatencyInput, null);
}
GenerateOutput();
}
private void checkBoxSetOutputLatency_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
numericLatencyOutput.Enabled = o.Checked;
if (o.Checked == false) {
_flexGuiConfig.Output.SuggestedLatencySeconds = null;
}
else
{
numericLatencyOutput_ValueChanged(numericLatencyOutput, null);
}
GenerateOutput();
}
private void checkBoxSetBufferSize_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
numericBufferSize.Enabled = o.Checked;
if (o.Checked == false) {
_flexGuiConfig.BufferSizeSamples = null;
}
else
{
numericBufferSize_ValueChanged(numericBufferSize, null);
}
GenerateOutput();
}
private void checkBoxWasapiInputSet_CheckedChanged(object sender, EventArgs e)
{
var o = sender as CheckBox;
if (o == null) return;
wasapiAutoConvertInput.Enabled = o.Checked;
wasapiExclusiveInput.Enabled = o.Checked;
if (o.Checked == false)
{
_flexGuiConfig.Input.WasapiAutoConvert = null;
_flexGuiConfig.Input.WasapiExclusiveMode = null;
}
else
{
_flexGuiConfig.Input.WasapiAutoConvert = wasapiAutoConvertInput.Checked;
_flexGuiConfig.Input.WasapiExclusiveMode = wasapiExclusiveInput.Checked;
}
GenerateOutput();
}
private void checkBoxWasapiOutputSet_CheckedChanged(object sender, EventArgs e)
{
if (sender is not CheckBox o) return;
wasapiAutoConvertOutput.Enabled = o.Checked;
wasapiExclusiveOutput.Enabled = o.Checked;
if (o.Checked == false)
{
_flexGuiConfig.Output.WasapiAutoConvert = null;
_flexGuiConfig.Output.WasapiExclusiveMode = null;
}
else
{
_flexGuiConfig.Output.WasapiAutoConvert = wasapiAutoConvertOutput.Checked;
_flexGuiConfig.Output.WasapiExclusiveMode = wasapiExclusiveOutput.Checked;
}
GenerateOutput();
}
private void btRefreshDevices_Click(object sender, EventArgs e)
{
var selectedBackend = comboBackend.SelectedItem as string;
RefreshDevices(selectedBackend);
}
private void linkLabelDocs_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo(DocUrl) { UseShellExecute = true });
}
private void btLoadFrom_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
openFileDialog.FileName = TomlName;
openFileDialog.Filter = @"FlexASIO Config (*.toml)|*.toml";
openFileDialog.CheckFileExists = true;
var ret = openFileDialog.ShowDialog();
if (ret == DialogResult.OK)
{
try
{
this.LoadFlexAsioConfig(openFileDialog.FileName);
}
catch (Exception)
{
SetStatusMessage($"Error loading from {openFileDialog.FileName}");
this.LoadFlexAsioConfig(_tomlPath);
return;
}
}
SetStatusMessage($"Configuration loaded from {openFileDialog.FileName}");
}
}
}