forked from flipswitchingmonkey/FlexASIO_GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
335 lines (279 loc) · 11.6 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Commons.Media.PortAudio;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using Nett;
namespace FlexASIOGUI
{
public partial class Form1 : Form
{
private bool InitDone = false;
private string TOMLPath;
private FlexGUIConfig flexGUIConfig;
private System.Text.Encoding enc1252;
public Form1()
{
InitializeComponent();
this.Text = "FlexASIO GUI v0.2";
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
enc1252 = Encoding.GetEncoding(1252);
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
CultureInfo.DefaultThreadCurrentCulture = customCulture;
CultureInfo.DefaultThreadCurrentUICulture = customCulture;
TOMLPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\FlexASIO.toml";
flexGUIConfig = new FlexGUIConfig();
if (File.Exists(TOMLPath))
{
flexGUIConfig = Toml.ReadFile<FlexGUIConfig>(TOMLPath);
}
numericBufferSize.Maximum = 8192;
numericBufferSize.Value = 256;
numericBufferSize.Increment = 16;
numericLatencyInput.Increment = 0.1m;
numericLatencyOutput.Increment = 0.1m;
for (var i=0; i<Configuration.HostApiCount; i++)
{
comboBackend.Items.Add(Configuration.GetHostApiInfo(i).name);
}
if (comboBackend.Items.Contains(flexGUIConfig.backend))
{
comboBackend.SelectedIndex = comboBackend.Items.IndexOf(flexGUIConfig.backend);
}
else
{
comboBackend.SelectedIndex = 0;
}
numericBufferSize.Value = (Int64)flexGUIConfig.bufferSizeSamples;
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));
numericLatencyInput.Value = (decimal)(double)flexGUIConfig.input.suggestedLatencySeconds;
numericLatencyOutput.Value = (decimal)(double)flexGUIConfig.output.suggestedLatencySeconds;
numericChannelsInput.Value = (decimal)(flexGUIConfig.input.channels ?? 0);
numericChannelsOutput.Value = (decimal)(flexGUIConfig.output.channels ?? 0);
wasapiExclusiveInput.Checked = flexGUIConfig.input.wasapiExclusiveMode;
wasapiExclusiveOutput.Checked = flexGUIConfig.output.wasapiExclusiveMode;
wasapiAutoConvertInput.Checked = flexGUIConfig.input.wasapiAutoConvert;
wasapiAutoConvertOutput.Checked = flexGUIConfig.output.wasapiAutoConvert;
InitDone = true;
SetStatusMessage($"FlexASIO GUI for FlexASIO 0.15 started ({Configuration.VersionString})");
GenerateOutput();
}
private string W1252fromUTF(string s)
{
byte[] bytes = Encoding.Default.GetBytes(s);
byte[] output = Encoding.Convert(Encoding.UTF8, enc1252, bytes);
return Encoding.UTF8.GetString(output);
}
private TreeNode[] GetDevicesForBackend(string Backend, bool Input)
{
List<TreeNode> treeNodes = new List<TreeNode>();
treeNodes.Add(new TreeNode("(None)"));
for (var i = 0; i < Configuration.DeviceCount; i++)
{
var deviceInfo = Configuration.GetDeviceInfo(i);
var apiInfo = Configuration.GetHostApiInfo(deviceInfo.hostApi);
if (apiInfo.name != Backend)
continue;
if (Input == true)
{
if (deviceInfo.maxInputChannels > 0)
{
treeNodes.Add(new TreeNode(W1252fromUTF(deviceInfo.name)));
}
}
else
{
if (deviceInfo.maxOutputChannels > 0)
{
treeNodes.Add(new TreeNode(W1252fromUTF(deviceInfo.name)));
}
}
}
return treeNodes.ToArray();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBackend_SelectedIndexChanged(object sender, EventArgs e)
{
var o = sender as ComboBox;
if (o != null)
{
var selectedBackend = o.SelectedItem as string;
if (selectedBackend != null)
{
treeDevicesInput.Nodes.Clear();
treeDevicesInput.Nodes.AddRange(GetDevicesForBackend(selectedBackend, true));
treeDevicesOutput.Nodes.Clear();
treeDevicesOutput.Nodes.AddRange(GetDevicesForBackend(selectedBackend, false));
}
flexGUIConfig.backend = selectedBackend;
GenerateOutput();
}
}
private void GenerateOutput()
{
if (!InitDone) return;
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 = "FlexASIO.toml";
var ret = saveFileDialog.ShowDialog();
if (ret == DialogResult.OK)
{
File.WriteAllText(saveFileDialog.FileName, configOutput.Text);
}
SetStatusMessage($"Configuration written to {saveFileDialog.FileName}");
}
private void treeDevicesInput_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
var o = sender as TreeView;
if (o.SelectedNode != null)
{
o.SelectedNode.Checked = false;
}
}
private void treeDevicesOutput_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
var o = sender as TreeView;
if (o.SelectedNode != null)
{
o.SelectedNode.Checked = false;
}
}
private void treeDevicesInput_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.IsSelected == false)
{
e.Cancel = true;
}
}
private void treeDevicesOutput_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.IsSelected == false)
{
e.Cancel = true;
}
}
private void treeDevicesInput_AfterSelect(object sender, TreeViewEventArgs e)
{
var o = sender as TreeView;
if (o == null) return;
if (o.SelectedNode != null)
{
o.SelectedNode.Checked = true;
flexGUIConfig.input.device = o.SelectedNode.Text;
GenerateOutput();
}
}
private void treeDevicesOutput_AfterSelect(object sender, TreeViewEventArgs e)
{
var o = sender as TreeView;
if (o == null) return;
if (o.SelectedNode != null)
{
o.SelectedNode.Checked = true;
flexGUIConfig.output.device = o.SelectedNode.Text;
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;
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;
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 treeDevicesOutput_AfterCheck(object sender, TreeViewEventArgs e)
{
}
private void treeDevicesInput_AfterCheck(object sender, TreeViewEventArgs e)
{
}
}
}