-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
460 lines (408 loc) · 14.6 KB
/
Program.cs
File metadata and controls
460 lines (408 loc) · 14.6 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
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
using KCNScopeClock.Helpers;
using KCNScopeClock.Models;
using KCNScopeClock.Services;
using NAudio.Wave;
namespace KCNScopeClock;
internal class Program
{
private static AppConfig _config = new();
[STAThread]
private static void Main(string[] args)
{
Console.Title = "KCN-Server Scope Clock 示波器时钟 V1.0.0";
_config = ConfigManager.Load();
if (args.Length > 0)
{
ParseCommandLineArgs(args);
StartClock();
}
else
{
RunInteractiveMenu();
}
}
/// <summary>
/// 主菜单循环
/// </summary>
private static void RunInteractiveMenu()
{
while (true)
{
Console.Clear();
Console.WriteLine("=========================================");
Console.WriteLine(" KCN-Server Scope Clock 示波器时钟 ");
Console.WriteLine("=========================================");
PrintStatusPanel();
Console.WriteLine("1. 启动时钟 (Start)");
Console.WriteLine("2. 音频设置 (Audio Settings)");
Console.WriteLine("3. 时钟设置 (Clock Settings)");
Console.WriteLine("4. 退出 (Exit)");
Console.WriteLine("-----------------------------------------");
Console.Write("> ");
var input = Console.ReadLine();
switch (input)
{
case "1":
StartClock();
break;
case "2":
ShowAudioSettingsMenu();
break;
case "3":
ShowClockSettingsMenu();
break;
case "4":
return;
}
}
}
/// <summary>
/// 当前配置状态
/// </summary>
private static void PrintStatusPanel()
{
int rate = _config.Audio.GetEffectiveSampleRate();
Console.WriteLine("当前状态:");
Console.WriteLine($" [Audio] {_config.Audio.DriverType} | {rate} Hz | {_config.Audio.BitDepth}");
Console.WriteLine($" Device: {_config.Audio.DeviceName ?? "未设置"}");
Console.WriteLine($" [Clock] Style: {_config.Clock.Style} | FPS: {_config.Clock.RefreshRate} | Channel Flip: {_config.Clock.ChannelFlip}");
Console.WriteLine("-----------------------------------------");
}
/// <summary>
/// 启动时钟模式
/// </summary>
private static void StartClock()
{
EnsureAsioConfigured();
int sampleRate = _config.Audio.GetEffectiveSampleRate();
var clockGen = new KCNScopeClockGenerator(sampleRate);
clockGen.RefreshRate = _config.Clock.RefreshRate;
clockGen.DrawDensity = _config.Clock.DrawDensity;
clockGen.CHFilp = _config.Clock.ChannelFlip;
clockGen.CurrentStyle = _config.Clock.Style;
RunAudioHost(clockGen, "Scope Clock Mode");
}
/// <summary>
/// 启动圆形测试模式
/// </summary>
private static void StartTestPattern()
{
EnsureAsioConfigured();
int sampleRate = _config.Audio.GetEffectiveSampleRate();
var circleGen = new ScopeCircleGenerator(sampleRate);
RunAudioHost(circleGen, "Calibration Circle Mode");
}
/// <summary>
/// 通用音频宿主引擎
/// </summary>
private static void RunAudioHost(ISampleProvider sourceGenerator, string modeName)
{
IWavePlayer? outputDevice = null;
try
{
Console.Clear();
Console.WriteLine($"[INFO] 正在初始化音频引擎 ({modeName})...");
// 适配位深
IWaveProvider finalProvider;
if (_config.Audio.BitDepth == BitDepthEnum.BitFloat32)
{
finalProvider = sourceGenerator.ToWaveProvider();
}
else
{
int bits = _config.Audio.BitDepth == BitDepthEnum.Bit16 ? 16 : 24;
finalProvider = new BitDepthAdapter(sourceGenerator, bits);
}
// 初始化驱动
switch (_config.Audio.DriverType)
{
case DriverTypeEnum.ASIO:
if (string.IsNullOrEmpty(_config.Audio.DeviceName))
throw new Exception("ASIO 设备未设置");
var asio = new AsioOut(_config.Audio.DeviceName);
outputDevice = asio;
break;
case DriverTypeEnum.WASAPI:
outputDevice = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Exclusive, 100);
break;
case DriverTypeEnum.WAVEOUT:
default:
outputDevice = new WaveOutEvent();
break;
}
outputDevice.Init(finalProvider);
outputDevice.Play();
Console.WriteLine($"\n[RUNNING] {modeName} 正在运行...");
Console.WriteLine($" Driver: {_config.Audio.DriverType}");
Console.WriteLine($" Device: {_config.Audio.DeviceName ?? "Default"}");
Console.WriteLine($" Format: {finalProvider.WaveFormat}");
Console.WriteLine("\n按 [Enter] 停止运行并返回菜单...");
Console.ReadLine();
outputDevice.Stop();
}
catch (Exception ex)
{
Console.WriteLine($"\n[FATAL ERROR] 启动失败: {ex.Message}");
Console.WriteLine("按任意键返回...");
Console.ReadKey();
}
finally
{
outputDevice?.Dispose();
if (sourceGenerator is IDisposable disposableGen)
disposableGen.Dispose();
}
}
/// <summary>
/// 检查 ASIO 配置
/// </summary>
private static void EnsureAsioConfigured()
{
if (_config.Audio.DriverType == DriverTypeEnum.ASIO && string.IsNullOrEmpty(_config.Audio.DeviceName))
{
Console.WriteLine("\n[INFO] ASIO 模式需要选择输出设备:");
SelectAsioDevice();
ConfigManager.Save(_config);
}
}
/// <summary>
/// 音频设置
/// </summary>
private static void ShowAudioSettingsMenu()
{
while (true)
{
Console.Clear();
Console.WriteLine("--- [音频设置] ---");
Console.WriteLine($" 1. 驱动类型 [{_config.Audio.DriverType}]");
Console.WriteLine($" 2. ASIO设备 [{_config.Audio.DeviceName ?? "未设置"}]");
Console.WriteLine($" 3. 采样率 [{_config.Audio.GetEffectiveSampleRate()} Hz]");
Console.WriteLine($" 4. 位深 [{_config.Audio.BitDepth}]");
Console.WriteLine($" 5. 打印测试圆");
Console.WriteLine($" 0. 保存并返回");
Console.Write("> ");
var key = Console.ReadLine();
switch (key)
{
case "1":
SelectDriverType();
break;
case "2":
if (_config.Audio.DriverType == DriverTypeEnum.ASIO) SelectAsioDevice();
else
{
Console.WriteLine("提示: 仅在 ASIO 模式下需要选择设备。按任意键继续...");
Console.ReadKey();
}
break;
case "3":
SelectSampleRate();
break;
case "4":
SelectBitDepth();
break;
case "5":
StartTestPattern();
break;
case "0":
ConfigManager.Save(_config);
return;
}
}
}
/// <summary>
/// 选择驱动类型
/// </summary>
private static void SelectDriverType()
{
Console.WriteLine("\n--- [选择驱动类型] ---");
Console.WriteLine(" 1. ASIO (推荐,需要声卡硬件及驱动)");
Console.WriteLine(" 2. WASAPI Exclusive");
Console.WriteLine(" 3. WaveOut");
Console.Write("> ");
var input = Console.ReadLine();
_config.Audio.DriverType = input switch
{
"1" => DriverTypeEnum.ASIO,
"2" => DriverTypeEnum.WASAPI,
"3" => DriverTypeEnum.WAVEOUT,
_ => _config.Audio.DriverType
};
}
/// <summary>
/// 选择ASIO设备
/// </summary>
private static void SelectAsioDevice()
{
try
{
var drivers = AsioOut.GetDriverNames();
if (drivers.Length == 0)
{
Console.WriteLine("\n[ERROR] 未检测到 ASIO 驱动!");
Console.ReadKey();
return;
}
Console.WriteLine("\n--- [可用 ASIO 设备] ---");
for (int i = 0; i < drivers.Length; i++)
{
Console.WriteLine($" {i}. {drivers[i]}");
}
Console.Write("> ");
if (int.TryParse(Console.ReadLine(), out int idx) && idx >= 0 && idx < drivers.Length)
{
_config.Audio.DeviceName = drivers[idx];
}
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] 获取驱动列表失败: {ex.Message}");
Console.ReadKey();
}
}
private static void SelectSampleRate()
{
Console.WriteLine("\n--- [选择采样率] ---");
var rates = Enum.GetValues<SampleRateEnum>()
.Cast<SampleRateEnum>()
.Where(e => e != SampleRateEnum.RateCustom)
.OrderByDescending(e => (int)e)
.ToList();
int index = 1;
foreach (var rate in rates)
{
Console.WriteLine($" {index++}. {(int)rate} Hz");
}
Console.WriteLine($" {index}. 自定义");
Console.Write("> ");
if (int.TryParse(Console.ReadLine(), out int selection) && selection > 0 && selection <= index)
{
if (selection == index)
{
Console.WriteLine(" 请输入自定义采样率 (Hz):");
Console.Write($">");
if (int.TryParse(Console.ReadLine(), out int customRate))
MapIntToAudioConfig(customRate);
}
else
{
_config.Audio.SampleRate = rates[selection - 1];
}
}
}
/// <summary>
/// 选择位深
/// </summary>
private static void SelectBitDepth()
{
Console.WriteLine("\n--- [选择位深] ---");
Console.WriteLine(" 1. 16 Bit");
Console.WriteLine(" 2. 24 Bit");
Console.WriteLine(" 3. 32 Bit Float (推荐)");
Console.Write("> ");
var input = Console.ReadLine();
_config.Audio.BitDepth = input switch
{
"1" => BitDepthEnum.Bit16,
"2" => BitDepthEnum.Bit24,
"3" => BitDepthEnum.BitFloat32,
_ => _config.Audio.BitDepth
};
}
/// <summary>
/// 时钟设置
/// </summary>
private static void ShowClockSettingsMenu()
{
while (true)
{
Console.Clear();
Console.WriteLine("--- [时钟设置] ---");
Console.WriteLine($" 1. 刷新率 (FPS) [{_config.Clock.RefreshRate}]");
Console.WriteLine($" 2. 绘图密度 [{_config.Clock.DrawDensity}]");
Console.WriteLine($" 3. 时钟风格 [{_config.Clock.Style}]");
Console.WriteLine($" 4. 通道翻转 [{_config.Clock.ChannelFlip}]");
Console.WriteLine($" 0. 保存并返回");
Console.Write("> ");
var key = Console.ReadLine();
switch (key)
{
case "1":
Console.Write("输入 FPS (建议 40 - 60) > ");
if (double.TryParse(Console.ReadLine(), out double fps)) _config.Clock.RefreshRate = fps;
break;
case "2":
Console.Write("输入密度 (1.0 - 3.0) > ");
if (float.TryParse(Console.ReadLine(), out float d)) _config.Clock.DrawDensity = d;
break;
case "3":
SelectClockStyle();
break;
case "4":
_config.Clock.ChannelFlip = !_config.Clock.ChannelFlip;
break;
case "0":
ConfigManager.Save(_config);
return;
}
}
}
/// <summary>
/// 选择风格
/// </summary>
private static void SelectClockStyle()
{
Console.WriteLine("\n--- [选择风格] ---");
var styles = Enum.GetValues<ClockStyle>();
for (int i = 0; i < styles.Length; i++)
{
Console.WriteLine($" {i}. {styles[i]}");
}
Console.Write("> ");
if (int.TryParse(Console.ReadLine(), out int idx) && Enum.IsDefined(typeof(ClockStyle), idx))
{
_config.Clock.Style = (ClockStyle)idx;
}
}
/// <summary>
/// 映射整数采样率至 AudioConfig
/// </summary>
private static void MapIntToAudioConfig(int rate)
{
if (Enum.IsDefined(typeof(SampleRateEnum), rate) && rate != 0)
{
_config.Audio.SampleRate = (SampleRateEnum)rate;
}
else
{
_config.Audio.SampleRate = SampleRateEnum.RateCustom;
_config.Audio.SampleRateCustom = rate;
}
}
/// <summary>
/// 解析命令行参数
/// </summary>
private static void ParseCommandLineArgs(string[] args)
{
try
{
if (Enum.TryParse(args[0], true, out DriverTypeEnum dt)) _config.Audio.DriverType = dt;
if (args.Length > 1) _config.Audio.DeviceName = args[1];
if (args.Length > 2 && int.TryParse(args[2], out int rate)) MapIntToAudioConfig(rate);
if (args.Length > 3 && int.TryParse(args[3], out int bits))
{
_config.Audio.BitDepth = bits switch
{
16 => BitDepthEnum.Bit16,
24 => BitDepthEnum.Bit24,
_ => BitDepthEnum.BitFloat32
};
}
Console.WriteLine("[INFO] 命令行参数已加载");
}
catch (Exception ex)
{
Console.WriteLine($"[WARN] 命令行参数解析部分失败: {ex.Message}");
}
}
}