-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
389 lines (337 loc) · 12.9 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
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 MorseCode;
namespace MorseCodeCoverter
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region Button-related
private void Button_Convert_Click(object sender, RoutedEventArgs e)
{
TextBox_Right.Text = TextBox_Left.Text.ToMorse();
}
private void Button_Play_Click(object sender, RoutedEventArgs e)
{
Button_Convert_Click(null, null);
Morse.PlayMorse(TextBox_Left.Text);
}
private void Button_CloseAbout_Click(object sender, RoutedEventArgs e)
{
Border_About.Visibility = Visibility.Hidden;
}
private void Button_About_Click(object sender, RoutedEventArgs e)
{
Border_About.Visibility = Visibility.Visible;
}
#endregion
#region Setting-related
private void TextBox_Conversion_Dash_TextChanged(object sender, TextChangedEventArgs e)
{
Morse.dash = TextBox_Conversion_Dash.Text;
}
private void TextBox_Conversion_Dot_TextChanged(object sender, TextChangedEventArgs e)
{
Morse.dot = TextBox_Conversion_Dot.Text;
}
private void TextBox_Spacing_Morse_TextChanged(object sender, TextChangedEventArgs e)
{
Morse.morseSpace = TextBox_Spacing_Morse.Text;
}
private void TextBox_Spacing_Chars_TextChanged(object sender, TextChangedEventArgs e)
{
Morse.charSpace = TextBox_Spacing_Chars.Text;
}
private void TextBox_Spacing_Words_TextChanged(object sender, TextChangedEventArgs e)
{
Morse.wordSpace = TextBox_Spacing_Words.Text;
}
#endregion
private void Button_Changelog_Click(object sender, RoutedEventArgs e)
{
Border_Changelog.Visibility = Visibility.Visible;
}
private void Button_CloseChangelog_Click(object sender, RoutedEventArgs e)
{
Border_Changelog.Visibility = Visibility.Hidden;
}
}
}
namespace MorseCode
{
/// <summary>
/// The class "Morse" contains two public functions:
///
/// ToMorse(this string) which return the string in morse code according to the settings:
/// dot, dash, morseSpace, charSpace, wordSpace. An unsupported character will not be converted
/// and will be treated as a regular character in terms of spacing. When the converter detects
/// an unsupported character, the return string will end with a warning message.
///
/// The other public function is PlayMorse(this string) which will play audio of the morse code with beeps and boops.
/// </summary>
public static class Morse
{
// International morse code: https://en.wikipedia.org/wiki/Morse_code
#region Conversion
// Dictionairy for conversion.
// False == Dot == "•" && True == Dash == "−"
private static readonly Dictionary<char, bool[]> ConversionDict = new Dictionary<char, bool[]>()
{
{'a', new bool[] {false, true } },
{'b', new bool[] {true , false, false, false} },
{'c', new bool[] {true , false, true , false} },
{'d', new bool[] {true , false, false} },
{'e', new bool[] {false} },
{'f', new bool[] {false, false, true , false} },
{'g', new bool[] {true , true , false } },
{'h', new bool[] {false, false, false, false, } },
{'i', new bool[] {false, false} },
{'j', new bool[] {false, true , true , true } },
{'k', new bool[] {true , false, true } },
{'l', new bool[] {false, true , false, false} },
{'m', new bool[] {true , true } },
{'n', new bool[] {true , false} },
{'o', new bool[] {true , false} },
{'p', new bool[] {false, true , true , false} },
{'q', new bool[] {true , true , false, true } },
{'r', new bool[] {false, true , false} },
{'s', new bool[] {false, false, false} },
{'t', new bool[] {true } },
{'u', new bool[] {false, false, true } },
{'v', new bool[] {false, false, false, true } },
{'w', new bool[] {false, true , true } },
{'x', new bool[] {true , false, false, true } },
{'y', new bool[] {true , false, true , true } },
{'z', new bool[] {true , true , false, false} },
{'1', new bool[] {false, true , true , true , true } },
{'2', new bool[] {false, false, true , true , true } },
{'3', new bool[] {false, false, false, true , true } },
{'4', new bool[] {false, false, false, false, true } },
{'5', new bool[] {false, false, false, false, false} },
{'6', new bool[] {true , false, false, false, false} },
{'7', new bool[] {true , true , false, false, false} },
{'8', new bool[] {true , true , true , false, false} },
{'9', new bool[] {true , true , true , true , false} },
{'0', new bool[] {true , true , true , true , true } }
};
// These are public since they are settings that can be changed
public static string dot = "•", dash = "−";
public static string morseSpace = "", charSpace = " ", wordSpace = " ";
// Convert string to morse string according to settings
public static string ToMorse(this string str)
{
/// This method returns the string converted into international morse code.
/// It supports the 26 English letters and 10 numerals.
/// Unknown characters become "*".
/// Dots => "•", Dash => "−".
/// See also: static class Morse.
///
// Preventing errors ...
if(str == "")
{
return "";
}
//
bool someNotConverted = false;
//
List<string> lines = new List<string>();
// For each line
foreach (string line in str.ToLower().Trim().Split('\n'))
{
List<string> words = new List<string>();
// For each word in line
foreach (string word in str.ToLower().Trim().Split(' '))
{
List<string> chars = new List<string>();
// For each char in word
foreach (char c in word)
{
// KeyNotFound
if (!Morse.ConversionDict.ContainsKey(c))
{
chars.Add(c.ToString());
someNotConverted = true;
}
else
{
chars.Add(c.ToMorse());
}
}
// Add word
words.Add(chars.Aggregate((i, j) => i + charSpace + j));
}
// Join words
lines.Add(words.Aggregate((i, j) => i + wordSpace + j));
}
string rStr = lines.Aggregate((i, j) => i + "\n" + j);
if (someNotConverted)
{
rStr += "\n\nWarning: Some caracters were not converted.";
}
return rStr;
}
// Convert chars to morse string
private static string ToMorse(this char c)
{
List<string> r = new List<string>();
foreach (bool isDash in ConversionDict[c])
{
if (isDash)
{
r.Add(dash);
}
else
{
r.Add(dot);
}
}
return r.Aggregate((i, j) => i + morseSpace + j);
}
#endregion
#region Audio
// Media player
private static readonly SoundPlayer SP_Beep = new SoundPlayer(MorseCodeConverter.Properties.Resources.Beep);
private static readonly SoundPlayer SP_Boop = new SoundPlayer(MorseCodeConverter.Properties.Resources.Boop);
// Playlist
private static List<byte> PlayList;
private static int PlayListIndex;
// string to morse code audio
public static void PlayMorse(this string str)
{
/// The length of a dot is one unit.
/// A dash is three units.
/// The space between parts of the same letter is one unit.
/// The space between letters is three units.
/// The space between words is seven units.
///
/// Playlist is a list of bytes
///
/// 0 == Dot (Beep) duration is 145ms
/// 1 == Dash (Boop) duration is 435ms (3 x 145)
/// 2 == 145ms pauze (between dots and dashes)
/// 3 == 435ms pauze (between letters)
/// 4 == 1015ms pauze (between words)
// Reset playlist
PlayList = new List<byte>();
string[] words = str.ToLower().Trim().Split(' ');
// For each word in str
for (int i=0; i<words.Length; i++)
{
char[] chars = words[i].ToCharArray();
// For each char in word
for (int j=0; j<chars.Length; j++)
{
if (ConversionDict.ContainsKey(chars[j]))
{
bool[] b = ConversionDict[chars[j]];
// For each morse char in c
for (int k = 0; k < b.Length; k++)
{
// Add morse char to playlist
if (b[k])
{
PlayList.Add(1);
}
else
{
PlayList.Add(0);
}
// Add space
if (k != b.Length - 1)
{
PlayList.Add(2);
}
}
// Add space
if (j != chars.Length - 1)
{
PlayList.Add(3);
}
}
else if (chars[j] == '\n') // KeyNotFound
{
PlayList.Add(5); // line break
}
else
{
PlayList.Add(2);
}
}
// Add space
if (i != words.Length - 1)
{
PlayList.Add(4);
}
}
// Play first sound
PlayListIndex = 0;
PlayNextInPlaylist();
}
// Function that calls itself
static void PlayNextInPlaylist()
{
/// Playlist is a list of bytes
///
/// 0 == Dot (Beep) duration is 145ms
/// 1 == Dash (Boop) duration is 435ms (3 x 145)
/// 2 == 145ms pauze (between dots and dashes)
/// 3 == 435ms pauze (between letters)
/// 4 == 1015ms pauze (between words)
// Playlist ended exception
if (PlayListIndex >= PlayList.Count)
{
return;
}
int sleepTime;
// Play sound/pauze
switch (PlayList[PlayListIndex])
{
case 0:
SP_Beep.Play();
sleepTime = 145;
break;
case 1:
SP_Boop.Play();
sleepTime = 145;
break;
case 2:
sleepTime = 145;
break;
case 3:
sleepTime = 435;
break;
case 4:
sleepTime = 1015;
break;
default: // Really case 5
sleepTime = 1000;
break;
}
// Increment playlist index
PlayListIndex++;
// Queue up next sound/pauze
Task.Factory.StartNew(() =>
{
System.Threading.Thread.Sleep(sleepTime);
PlayNextInPlaylist();
});
}
#endregion
}
}