-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
339 lines (295 loc) · 11.6 KB
/
index.html
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
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Morse code generator">
<meta name="keywords" content="HTML, CSS, JavaScript, Morse, Code">
<meta name="author" content="Jee Vang, Ph.D.">
<meta name="organization" content="One-Off Coder">
<title>Morse Code</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
* {
font-family: monospace;
}
body {
margin: 10px;
}
.letter {
font-size: large;
cursor: pointer;
}
.dihdah {
font-size: xx-large;
font-weight: bolder;
cursor: pointer;
}
</style>
</head>
<body>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Morse Code</h1>
<p class="lead">
<a href="https://en.wikipedia.org/wiki/Morse_code" target="_blank">Morse code</a> audio generator
</p>
<hr class="my-4">
<p>
Dih-dah sounds generated using
<a href="https://github.com/delimitry/morse-code-wav" target="_blank">
morse-code-wav
</a>
</p>
<p>
Awesome <a href="https://www.youtube.com/watch?v=xsDk5_bktFo" target="_blank">video on Morse code</a>
</p>
<p>
<a href="https://github.com/oneoffcoder/lightning-projects/tree/master/html/morse-code" target="_blank">
Source Code
</a>
</p>
</div>
</div>
<div class="form-group">
<label for="text">Type your text below</label>
<textarea class="form-control" id="text"
rows="3">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0</textarea>
</div>
<button type="button" class="btn btn-primary" onclick="generate()">Generate</button>
<br />
<br />
<div id="letterOutputHint" style="display: none;">Hover over words to play sound</div>
<div id="letterOutput" class="alert alert-primary" role="alert" style="display: none;"> </div>
<div id="dihDahOutputHint" style="display: none;">Hover over letters to play sound</div>
<div id="dihDahOutput" class="alert alert-secondary" role="alert" style="display: none;"> </div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.0/howler.min.js" crossorigin="anonymous"></script>
<script>
const l2d = getL2D();
const d2l = getD2L();
let isPlaying = false;
function generate() {
if (isPlaying === true) {
return;
}
const text = getTextInput();
if (!text) {
return;
}
clearOutputs();
const morseCode = getMorseCode(text, modifyOutputs);
isPlaying = true;
showOutputs();
morseCode.play();
}
function showOutputs() {
document.getElementById('letterOutput').style.display = 'block';
document.getElementById('dihDahOutput').style.display = 'block';
document.getElementById('letterOutputHint').style.display = 'block';
document.getElementById('dihDahOutputHint').style.display = 'block';
}
function clearOutputs() {
document.getElementById('letterOutput').innerHTML = '';
document.getElementById('dihDahOutput').innerHTML = '';
}
function modifyOutputs(index, size, letter, dihDah) {
function modifyLetter() {
let normLetter = letter === '_BT_WORD_SPACE' ? ' ' : letter;
document.getElementById('letterOutput').innerHTML += normLetter;
}
function postProcessWords() {
if (index === size - 1) {
const words = document.getElementById('letterOutput').innerHTML
.split(' ')
.map(word => `<span class="letter" onmouseover="playWord('${word}')">${word}</span>`)
.join(' ');
document.getElementById('letterOutput').innerHTML = words;
}
}
function modifyDihDah() {
const lt = letter === '_BT_WORD_SPACE' ? ' ' : `<span class="letter" onmouseover="playLetter('${letter}')">${letter}</span>`;
const dd = dihDah === '_BT_WORD_SPACE' ? ' ' : `<span class="dihdah" onmouseover="playDihDah('${dihDah}')">${dihDah}</span>`;
const odd = document.getElementById('dihDahOutput').innerHTML;
if (lt === ' ') {
document.getElementById('dihDahOutput').innerHTML = `<br/> ${odd}`;
} else {
document.getElementById('dihDahOutput').innerHTML = `${lt} : ${dd} <br/> ${odd}`;
}
}
modifyLetter();
modifyDihDah();
postProcessWords();
}
function playWord(word) {
if (isPlaying) {
return;
}
isPlaying = true;
const morseCode = getMorseCode(word, undefined);
morseCode.play();
}
function playLetter(letter) {
if (isPlaying) {
return;
}
const howl = new Howl({
src: [`audios/${letter}.wav`],
onend: () => isPlaying = false
});
isPlaying = true;
howl.play();
}
function playDihDah(dihDah) {
playLetter(d2l.get(dihDah));
}
function getMorseCode(text, letterHandler) {
function getCode(c) {
function getSrc(dd) {
const audioPattern = [...dd].join(' ');
const audios = [...audioPattern]
.map(c => c === '.' ? 'audios/dih.wav' : c === '-' ? 'audios/dah.wav' : 'audios/space.wav');
return audios;
}
const dd = l2d.get(c);
const src = getSrc(dd);
return {
letter: c,
dd: dd,
src: src
}
}
const normText = text
.split(' ')
.map(t => t.toUpperCase().trim())
.filter(t => t.length > 0)
.map(t => [...t].filter(l => l2d.has(l)).join(''))
.join(' ');
const words = [...normText];
const tokens = Array();
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (word === ' ') {
tokens.push({
letter: '_BT_WORD_SPACE',
dd: '_BT_WORD_SPACE',
src: [...' '].map(c => 'audios/space.wav')
});
} else {
const letters = [...word];
for (let j = 0; j < letters.length; j++) {
tokens.push(getCode(letters[j]));
if (j < letters.length - 1) {
tokens.push({
letter: '_BT_LETTER_SPACE',
dd: '_BT_LETTER_SPACE',
src: [...' '].map(c => 'audios/space.wav')
})
}
}
}
}
function getLetterPlayable(letter, callback) {
return {
index: 0,
letter: letter.letter,
dd: letter.dd,
audios: letter.src,
callback: callback,
play: function () {
const audio = this.audios[this.index];
const howl = new Howl({
src: [audio],
onend: () => {
this.index += 1;
if (this.index <= this.audios.length - 1) {
this.play();
} else {
callback.play();
}
}
});
howl.play();
}
};
}
return {
letterIndex: 0,
letters: tokens,
play: function () {
if (this.letterIndex >= this.letters.length) {
isPlaying = false;
return;
}
const letter = this.letters[this.letterIndex];
if (letterHandler) {
letterHandler(this.letterIndex, this.letters.length, letter.letter, letter.dd);
}
this.letterIndex += 1;
const playable = getLetterPlayable(letter, this);
playable.play();
}
}
}
function getTextInput() {
const textArea = document.getElementById('text');
return textArea.value;
}
function getD2L() {
const l2d = getL2D();
const d2l = new Map();
for (const [l, d] of l2d) {
d2l.set(d, l);
}
return d2l;
}
function getL2D() {
const l2d = new Map();
l2d.set('A', '.-');
l2d.set('B', '-...');
l2d.set('C', '-.-.');
l2d.set('D', '-..');
l2d.set('E', '.');
l2d.set('F', '..-.');
l2d.set('G', '--.');
l2d.set('H', '....');
l2d.set('I', '..');
l2d.set('J', '.---');
l2d.set('K', '-.-');
l2d.set('L', '.-..');
l2d.set('M', '--');
l2d.set('N', '-.');
l2d.set('O', '---');
l2d.set('P', '.--.');
l2d.set('Q', '--.-');
l2d.set('R', '.-.');
l2d.set('S', '...');
l2d.set('T', '-');
l2d.set('U', '..-');
l2d.set('V', '...-');
l2d.set('W', '.--');
l2d.set('X', '-..-');
l2d.set('Y', '-.--');
l2d.set('Z', '--..');
l2d.set('1', '.----');
l2d.set('2', '..---');
l2d.set('3', '...--');
l2d.set('4', '....-');
l2d.set('5', '.....');
l2d.set('6', '-....');
l2d.set('7', '--...');
l2d.set('8', '---..');
l2d.set('9', '----.');
l2d.set('0', '-----');
return l2d;
}
</script>
</body>
</html>