-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
548 lines (440 loc) · 19.5 KB
/
MainWindow.xaml.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using IPrompt;
using upSidetone.Sound;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using upSidetone.InputDevices;
using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics;
using NAudio.Gui;
using System.Runtime.Versioning;
namespace upSidetone {
public partial class MainWindow : Window, IDisposable {
static public MainWindow Me { get; private set; } // Singleton (Lazy hack mostly for debugging)
int Latency = 50;
AudioOut AudioOut;
ToneMaker SoundGen;
Levers Levers;
Keying Keyer;
MidiInput MidiInput;
KeyboardInputWPF KeyboardInput;
MorseMouses MorseMouses;
SerialPortInfo PortInfo;
SerialPortReader Port;
SerialPortReader VPort;
bool enableTcpSerialServer = false;
//DosboxENetSerial? ENetSerial; // experimental/broken
DosboxTcpSerial? TcpSerial; // test serial port connection server for dosbox
private bool disposedValue;
public MainWindow() {
Me = this;
Debug.WriteLine("hello.");
Closed += MainWindow_Closed;
InitializeComponent();
Levers = new Levers();
Keyer = new Keying(KeyerMode.Ultimatic);
Keyer.ListenToLevers(Levers);
MidiInput = new MidiInput();
MidiInput.Levers = Levers;
//MidiInput.Enabled = true;
KeyboardInput = new KeyboardInputWPF(); //todo: Enable()
KeyboardInput.Levers = Levers;
MorseMouses = new MorseMouses();
MorseMouses.Levers = Levers;
MorseMouses.StartPolling();
//GetOrCreateSounder(); // create default?
foreach (var device in AudioOut.Devices()) {
AudioOutputSelect.Items.Add(device);
}
foreach (var device in MidiInput.DeviceNames()) {
MidiSelect.Items.Add(device);
}
AudioOutputSelect.SelectedIndex = 0;
MidiSelect.SelectedIndex = 0;
KeyerModeSelect.Items.Add("Oscillator");
KeyerModeSelect.Items.Add("Straight key");
KeyerModeSelect.Items.Add("Iambic A");
KeyerModeSelect.Items.Add("Iambic B");
KeyerModeSelect.Items.Add("Hybrid"); // previously called Bugalike or Hopper or Cyborg (half human, half machine); isopod: like a bug but actually not
KeyerModeSelect.Items.Add("Ultimatic");
KeyerModeSelect.Items.Add("Autorepeat off");
//KeyerModeSelect.Items.Add("Locking");
//KeyerModeSelect.Items.Add("Locking Hybrid");
//KeyerModeSelect.Items.Add("Swamp");
KeyerModeSelect.SelectedIndex = 5; // Ultimatic
Volume.Text = "50";
Frequency.Text = "440";
WPM.Text = "18";
Flipped.IsChecked = false;
SoundGen?.SetGain(0.5);
PortInfo = new SerialPortInfo();
try {
PortInfo.ScanForCaptions();
} catch (Exception e) {
Debug.WriteLine("Error scanning for port captions: " + e);
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
foreach (var device in PortInfo.GetPortNamesWithCaptions()) {
SerialPorts.Items.Add(device);
}
SerialPorts.SelectedIndex = 0;
foreach (var device in PortInfo.GetPortNamesWithCaptions()) {
VSerialPorts.Items.Add(device);
}
VSerialPorts.SelectedIndex = 0;
////experimental (port 5672; never working)
//ENetSerial = new();
//ENetSerial.Levers = Levers;
//ENetSerial.StartAsThread();
if (enableTcpSerialServer) {
// experimental (creates server on port 5673)
TcpSerial = new();
TcpSerial.Levers = Levers;
TcpSerial.StartAsThread();
}
Debug.WriteLine("...hello.");
}
private ToneMaker GetOrCreateSounder() {
if (SoundGen == null) {
string? selected = AudioOutputSelect.SelectedValue as string; // ok if null
AudioOut?.Dispose();
AudioOut = new AudioOut();
AudioOut.Enable(selected, Latency);
SoundGen?.StopListeningToKeyer(Keyer); // remove old
SoundGen?.Dispose();
SoundGen = new ToneMaker(AudioOut);
// restore settings (bit kludgy)
Volume_TextChanged(null, null);
Frequency_TextChanged(null, null);
WPM_TextChanged(null, null);
SoundGen.Enable();
SoundGen.ListenToKeyer(Keyer);
//MidiIntput?.SetLevers(Sounder);
//KeyboardInput?.SetSounder(Sounder);
//MorseMouses?.SetSounder(Sounder);
DeviceInfoText.Text = AudioOut?.GetReport() ?? "";
////(commented out): Offer help? This is too much to squeeze in here
//if (AudioOut == null || AudioOut.OutDevice == null) {
// DeviceInfoText.Text = "WDM (aka WASAPI) offers low latency. MME has the strongest compatibility. Some audio devices have very low latency ASIO drivers.";
//}
if (AudioOut?.IsAsio() ?? false) {
AsioOutputOptionsButton.IsEnabled = true;
} else {
AsioOutputOptionsButton.IsEnabled = false;
}
//AudioOut.DeviceInfoDebug();
}
return SoundGen;
}
private void MidiSelect_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var selected = MidiSelect.SelectedValue as string;
DebugOut(selected);
var success = MidiInput.SelectDevice(selected);
DebugOut("success: " + success);
}
void ReloadAudioDevice() {
var selected = AudioOutputSelect.SelectedValue as string;
DebugOut("changing audio to: " + selected);
AudioOut?.Dispose();
SoundGen?.Dispose();
AudioOut = null;
SoundGen = null;
GetOrCreateSounder(); // create a new sounder immediately so Midi can talk to it
//TODO: make a sounder wrapper/indirection, for midi etc to keep pressing buttons on even when audio device changed?
//...Or just let AudioOut change on sounder
}
private void AudioOutputSelect_SelectionChanged(object sender, SelectionChangedEventArgs e) {
ReloadAudioDevice();
}
private void Button_Click(object sender, RoutedEventArgs e) {
//todo: delete me
//Sounder.StraightKeyDown();
}
private void Button_MouseDown(object sender, MouseButtonEventArgs e) {
/*
var sounder = GetOrCreateSounder();
if (e.LeftButton == MouseButtonState.Pressed) {
//Sounder?.StraightKeyDown(1);
Levers?.PushLeverDown(VirtualLever.Left);
} else {
Levers?.PushLeverDown(VirtualLever.Right);
}
*/
}
private void Button_MouseUp(object sender, MouseButtonEventArgs e) {
// commented out:
// already using MorseMouses.ListenToAll
// MouseButtonState is ambiguous when multiple buttons are in use (would need to track them)
/*
if (e.LeftButton == MouseButtonState.Released) {
Levers?.ReleaseLever(VirtualLever.Left);
}
if (e.RightButton == MouseButtonState.Released) {
Levers?.ReleaseLever(VirtualLever.Right);
}
*/
}
private void Button_MouseLeave(object sender, MouseEventArgs e) {
// stop it getting stuck
//Sounder?.StraightKeyUp();
if (MorseMouses != null) MorseMouses.ListenToAll = false;
}
private void Button_MouseEnter(object sender, MouseEventArgs e) {
if (MorseMouses != null) MorseMouses.ListenToAll = true;
}
public void Log(string text) {
// make sure we're on our own thread
//this.Dispatcher.Invoke(() => {
// if (piano != null) piano.Text = text;
// DebugText.Text = DebugText.Text + "\n" + text;
//});
System.Diagnostics.Debug.WriteLine(text);
}
public static void WriteLine(string text) {
// writes a line for mouse debugging
if (Me != null && Me.MousePianoText != null) {
//Me.Log("mouse: " + text, Me.MousePianoText);
Me.Dispatcher.Invoke(() => {
if (Me != null && Me.MousePianoText != null) { // double check
Me.MousePianoText.Text = text;
}
});
}
}
public static void DebugOut(string text) {
Me?.Log(text);
}
private void MainWindow_Closed(object? sender, EventArgs e) {
if (MorseMouses != null) MorseMouses.ListenToAll = false;
Dispose();
}
protected virtual void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
// dispose managed state (managed objects)
SoundGen?.Dispose();
MidiInput?.Dispose();
MorseMouses?.Dispose();
//Levers?.Dispose(); // TODO
TcpSerial?.Dispose();
//ENetSerial?.Dispose();
}
// free unmanaged resources (unmanaged objects) and override finalizer
AudioOut?.Dispose(); // really needs to be disposed (in case might hold onto ASIO)
// set large fields to null
SoundGen = null;
AudioOut = null;
MidiInput = null;
Me = null;
Levers = null;
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~MainWindow()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
private void AudioOutOptions_Button_Click(object sender, RoutedEventArgs e) {
// latency dialog
// https://github.com/ramer/IPrompt
//TODO: use my own dialog, and add preferred bitrate
// https://www.c-sharpcorner.com/article/dialogs-in-wpf-mvvm/
string result = IInputBox.Show("Desired Latency (ms):\n-1 for default.\nIgnored for ASIO drivers.", defaultResponse: Latency.ToString());
if (result != null && int.TryParse(result, out int val)) {
if (val >= -1 && val < 10_000) { // todo: max value? 10s probably too much
Latency = val;
ReloadAudioDevice();
} else if (val <= -1) {
Latency = -1;
ReloadAudioDevice();
}
}
}
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e) {
KeyboardInput?.KeyEvent(e);
if (e.Key == Key.F12 && !e.Handled && KeyerModeSelect.Items.Count < 8) {
string tag = " (experimental)";
KeyerModeSelect.Items.Add("Locking" + tag);
KeyerModeSelect.Items.Add("Locking Hybrid" + tag);
KeyerModeSelect.Items.Add("Swamp" + tag);
e.Handled = true;
}
}
private void Grid_PreviewKeyUp(object sender, KeyEventArgs e) {
KeyboardInput?.KeyEvent(e);
}
private void Grid_LostFocus(object sender, RoutedEventArgs e) {
// loses focus when interacting with drop downs, so doesn't seem useful
//TODO: only release for the key button
//if (MorseMouses != null) MorseMouses.ListenToAll = false;
//Levers?.ReleaseLever(VirtualLever.Left);
//Levers?.ReleaseLever(VirtualLever.Right);
}
private void AsioOutputOptionsButton_Click(object sender, RoutedEventArgs e) {
AudioOut?.LaunchAsioControlPanel();
}
public void RefreshPianoDisplay() {
// run on main thread (if called from midi's thread)
this.Dispatcher.Invoke(() => {
MidiPianoText.Text = MidiInput?.GetDownNotes() ?? "";
});
}
public void UpdateKeyerPianoText(string text) {
this.Dispatcher.Invoke(() => {
KeyerPianoText.Text = text ?? "";
});
}
internal void SetMouseNames(IEnumerable<string> mice, int selectedIndex = 0) {
this.Dispatcher.Invoke(() => {
foreach (var mouse in mice) {
MouseSelect.Items.Add(mouse);
}
MouseSelect.SelectedIndex = selectedIndex;
});
}
private void MouseSelect_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if (MorseMouses != null) {
MorseMouses.ChosenMouse = MouseSelect.SelectedIndex;
}
}
private void KeyerModeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e) {
Keyer.ReleaseAll();
if (KeyerModeSelect.SelectedIndex == 0) {
Keyer.Mode = KeyerMode.Oscillator;
} else if (KeyerModeSelect.SelectedIndex == 1) {
Keyer.Mode = KeyerMode.StraightKey;
} else if (KeyerModeSelect.SelectedIndex == 2) {
Keyer.Mode = KeyerMode.IambicA;
} else if (KeyerModeSelect.SelectedIndex == 3) {
Keyer.Mode = KeyerMode.IambicB;
} else if (KeyerModeSelect.SelectedIndex == 4) {
Keyer.Mode = KeyerMode.Hybrid;
} else if (KeyerModeSelect.SelectedIndex == 5) {
Keyer.Mode = KeyerMode.Ultimatic;
} else if (KeyerModeSelect.SelectedIndex == 6) {
Keyer.Mode = KeyerMode.NoRepeats;
} else if (KeyerModeSelect.SelectedIndex == 7) {
Keyer.Mode = KeyerMode.Locking;
} else if (KeyerModeSelect.SelectedIndex == 8) {
Keyer.Mode = KeyerMode.HybridLocking;
} else if (KeyerModeSelect.SelectedIndex == 9) {
Keyer.Mode = KeyerMode.Swamp;
}
}
private void Volume_TextChanged(object sender, TextChangedEventArgs e) {
if (Volume != null && double.TryParse(Volume.Text, out double result)) {
double vol = result / 100.0;
if (vol >= 0 && vol <= 1) {
// "Setting volume not supported on DirectSoundOut, adjust the volume on your WaveProvider instead"
//if (AudioOut?.OutDevice != null) AudioOut.OutDevice.Volume = vol;
var success = SoundGen?.SetGain(vol) ?? false;
}
}
}
private void Frequency_TextChanged(object sender, TextChangedEventArgs e) {
if (Frequency != null && double.TryParse(Frequency.Text, out double result)) {
var success = SoundGen?.SetFreq(result) ?? false;
}
}
private void WPM_TextChanged(object sender, TextChangedEventArgs e) {
if (WPM != null && double.TryParse(WPM.Text, out double result)) {
var success = SoundGen?.SetWPM(result) ?? false;
}
}
private void Flipped_Checked(object sender, RoutedEventArgs e) {
bool? flipped = Flipped.IsChecked;
if (Levers == null) return;
if (flipped.HasValue && flipped.Value) {
Levers.SwapLeftRight = true;
} else {
Levers.SwapLeftRight = false;
}
}
private void SerialPorts_SelectionChanged(object sender, SelectionChangedEventArgs arg) {
Port?.Dispose();
var selected = SerialPorts.SelectedValue as string;
selected = PortInfo.GetPortNameFromCaptioned(selected);
if (selected == null || selected == "(none)") {
Port?.Dispose();
PortPinsPianoUpdate("");
return;
}
try {
Debug.WriteLine("Port connection attempt...");
Port = new SerialPortReader(selected, Levers);
Port.PlayAsInputOn = true;
Port.PassThruOn = false;
Port.ListenOn = false;
Port.Enable();
Debug.WriteLine("Port connected: " + selected);
arg.Handled = true;
} catch (Exception e) {
Debug.WriteLine("Port connect failed: " + selected + " " + e.GetType() + ": " + e.Message);
}
}
private void VSerialPorts_SelectionChanged(object sender, SelectionChangedEventArgs arg) {
VPort?.Dispose();
var selected = VSerialPorts.SelectedValue as string;
selected = PortInfo.GetPortNameFromCaptioned(selected);
if (selected == null || selected == "(none)") {
VPort?.Dispose();
VPortPinsPianoUpdate("");
return;
}
try {
Debug.WriteLine("VPort connection attempt...");
VPort = new SerialPortReader(selected, Levers);
VPort.PlayAsInputOn = false;
VPort.ListenOn = false;
VPort.PassThruOn = true;
VPort.IsSecondPort = true;
VPort.RtsNormallyHigh = RTSHighCheckbox.IsChecked ?? VPort.RtsNormallyHigh;
VPort.Enable();
Debug.WriteLine("VPort connected: " + selected);
arg.Handled = true;
} catch (Exception e) {
Debug.WriteLine("VPort connect failed: " + selected + " " + e.GetType() + ": " + e.Message);
}
}
public void PortPinsPianoUpdate(string text) {
if (SerialPortPianoText == null) return;
//if (InvokeRequired)
this.Dispatcher.Invoke(() => {
if (SerialPortPianoText != null) SerialPortPianoText.Text = text;
});
}
public void VPortPinsPianoUpdate(string text) {
if (VSerialPortPianoText == null) return;
//if (InvokeRequired)
this.Dispatcher.Invoke(() => {
if (VSerialPortPianoText != null) VSerialPortPianoText.Text = text;
});
}
private void RTSHighCheckbox_Checked(object sender, RoutedEventArgs e) {
if (VPort != null) {
VPort.RtsNormallyHigh = RTSHighCheckbox.IsChecked ?? VPort.RtsNormallyHigh;
VPort.ResetPins();
}
}
}
}