Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1279 lines (1215 loc) · 45.7 KB
/
Copy pathscript.js
File metadata and controls
1279 lines (1215 loc) · 45.7 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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* =========================================================
MORSE CODE DEVICE — script.js
========================================================= */
"use strict";
/* ---------- Morse maps ---------- */
const MORSE = {
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:"--..",
"0":"-----", "1":".----", "2":"..---", "3":"...--", "4":"....-",
"5":".....", "6":"-....", "7":"--...", "8":"---..", "9":"----.",
".":".-.-.-", ",":"--..--", "?":"..--..", "!":"-.-.--", "/":"-..-."
};
const FROM_MORSE = Object.fromEntries(Object.entries(MORSE).map(([k,v]) => [v,k]));
const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
const NUMBERS = "0123456789".split("");
const PUNCT = [".",",","?","!","/"];
/* ---------- tree layout ----------
* 1 — standard binary tree (letters) + chip rows for numbers/punct (default)
* 2 — extended binary tree: numbers + punctuation also as LEDs (level 5/6)
* 3 — letters only
* 4 — compact "horizontal-spine" layout, letters only
*/
// Model 4 — compact mirrored layout on a column/row grid.
// ViewBox 1000 wide, antenna at x=500. dot spine extends LEFT, dash spine
// extends RIGHT (dot=left convention). Branches are drawn as right-angle
// (L-shaped) connectors. Fitted so the bottom row lands at the same height
// as the letters-only model, for seamless switching.
const M4_COL = 86; // horizontal spacing per column step
const M4_ROW = 52; // vertical spacing per row step
const M4_X = 500; // antenna x
const M4_TOP = 70; // antenna y (matches defaultBinaryTreePos TOP_Y for seamless height)
const m4 = (col, row) => ({ x: M4_X + col * M4_COL, y: M4_TOP + row * M4_ROW });
const MODEL4_POSITIONS = {
"": m4(0, 0), // antenna / root
// top spine — DOT side (left), dash side (right)
".": m4(-1, 0), // E
"..": m4(-2, 0), // I
"...": m4(-3, 0), // S
"....": m4(-4, 0), // H
"-": m4(1, 0), // T
"--": m4(2, 0), // M
"---": m4(3, 0), // O
// E sub-tree
".-": m4(-1, 3), // A
".-.": m4(-2, 3), // R
".-..": m4(-3, 3), // L
".--": m4(-1, 5), // W
".--.": m4(-2, 5), // P
".---": m4(-1, 6), // J
// I sub-tree
"..-": m4(-2, 1), // U
"..-.": m4(-2, 2), // F
// S sub-tree
"...-": m4(-3, 1), // V
// T sub-tree
"-.": m4(1, 3), // N
"-.-": m4(2, 3), // K
"-.--": m4(3, 3), // Y
"-.-.": m4(2, 4), // C
"-..": m4(1, 5), // D
"-..-": m4(2, 5), // X
"-...": m4(1, 6), // B
// M sub-tree
"--.": m4(2, 1), // G
"--..": m4(2, 2), // Z
"--.-": m4(3, 1), // Q
};
const MODELS = {
"1": {
label: "Original",
showLetterLEDs: true,
showNumPunctAsLEDs: false,
showNumPunctAsChips: true,
pos: defaultBinaryTreePos,
},
"2": {
label: "Extended",
showLetterLEDs: true,
showNumPunctAsLEDs: true,
showNumPunctAsChips: false,
pos: defaultBinaryTreePos,
},
"3": {
label: "Letters only",
showLetterLEDs: true,
showNumPunctAsLEDs: false,
showNumPunctAsChips: false,
pos: defaultBinaryTreePos,
},
"4": {
label: "Compact",
showLetterLEDs: true,
showNumPunctAsLEDs: false,
showNumPunctAsChips: false,
pos: (morse) => MODEL4_POSITIONS[morse] || null,
},
};
function defaultBinaryTreePos(morse, { extended } = {}) {
const W = extended ? 960 : 880;
const CX = 500;
const TOP_Y = 70;
const LEVEL_DY = extended ? 72 : 78;
let x = CX, dx = W / 4;
for (let i = 0; i < morse.length; i++) {
x += (morse[i] === "." ? -1 : 1) * dx;
dx /= 2;
}
return { x, y: TOP_Y + morse.length * LEVEL_DY };
}
// Build the nodes + branches for a given tree model.
function buildTree(modelId) {
const model = MODELS[modelId] || MODELS["1"];
// Collect every code path we need to render (all prefixes of every letter).
const codes = new Set([""]);
if (model.showLetterLEDs) {
LETTERS.forEach(L => {
const m = MORSE[L];
for (let i = 0; i <= m.length; i++) codes.add(m.slice(0, i));
});
}
if (model.showNumPunctAsLEDs) {
[...NUMBERS, ...PUNCT].forEach(ch => {
const m = MORSE[ch];
for (let i = 0; i <= m.length; i++) codes.add(m.slice(0, i));
});
}
// Compute positions; skip codes the model has no position for (Model 4).
const positionOpts = { extended: modelId === "2" };
const nodes = {};
codes.forEach(c => {
const p = model.pos(c, positionOpts);
if (!p) return;
nodes[c] = { x: p.x, y: p.y, code: c, label: FROM_MORSE[c] || null };
});
// Each non-root node has a parent one char shorter; that's the branch.
const branches = [];
Object.keys(nodes).forEach(c => {
if (c === "") return;
const parent = c.slice(0, -1);
if (nodes[parent]) branches.push({ from: parent, to: c, sym: c.slice(-1) });
});
return { nodes, branches, model, modelId };
}
let TREE = buildTree("1");
/* ---------- AUDIO ---------- */
const Beeper = (() => {
let ctx, masterGain;
let muted = false;
let scheduledNodes = [];
function ensure() {
if (!ctx) {
ctx = new (window.AudioContext || window.webkitAudioContext)();
masterGain = ctx.createGain();
masterGain.gain.value = .35;
masterGain.connect(ctx.destination);
}
if (ctx.state === "suspended") ctx.resume();
return ctx;
}
function trackNode(n) {
scheduledNodes.push(n);
n.onended = () => {
const i = scheduledNodes.indexOf(n);
if (i !== -1) scheduledNodes.splice(i, 1);
};
}
function beep(durMs, tone = "sine") {
if (muted) return;
ensure();
const t0 = ctx.currentTime;
const dur = durMs / 1000;
const env = ctx.createGain();
env.gain.setValueAtTime(0.0001, t0);
env.gain.exponentialRampToValueAtTime(1, t0 + 0.008);
env.gain.setValueAtTime(1, t0 + Math.max(dur - 0.02, 0.01));
env.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
env.connect(masterGain);
if (tone === "sine") {
const o = ctx.createOscillator();
o.type = "sine"; o.frequency.value = 700;
o.connect(env);
o.start(t0); o.stop(t0 + dur + 0.02);
trackNode(o);
} else if (tone === "telegraph") {
// Tight square-ish click + tone
const o1 = ctx.createOscillator(); o1.type = "square"; o1.frequency.value = 620;
const o2 = ctx.createOscillator(); o2.type = "triangle"; o2.frequency.value = 1240;
const mix = ctx.createGain(); mix.gain.value = 0.5;
o1.connect(mix); o2.connect(mix); mix.connect(env);
o1.start(t0); o2.start(t0); o1.stop(t0 + dur + 0.02); o2.stop(t0 + dur + 0.02);
trackNode(o1); trackNode(o2);
} else if (tone === "buzzer") {
// Sawtooth with slight detune for rasp
const o1 = ctx.createOscillator(); o1.type = "sawtooth"; o1.frequency.value = 480;
const o2 = ctx.createOscillator(); o2.type = "sawtooth"; o2.frequency.value = 484;
const lp = ctx.createBiquadFilter(); lp.type = "lowpass"; lp.frequency.value = 1600;
o1.connect(lp); o2.connect(lp); lp.connect(env);
o1.start(t0); o2.start(t0); o1.stop(t0 + dur + 0.02); o2.stop(t0 + dur + 0.02);
trackNode(o1); trackNode(o2);
}
}
function killAll() {
scheduledNodes.forEach(n => { try { n.stop(); } catch(_){} });
scheduledNodes = [];
}
function setMuted(v) { muted = !!v; if (muted) killAll(); }
return { beep, ensure, setMuted, killAll, get muted(){return muted;} };
})();
/* ---------- STATE ---------- */
const State = {
wpm: 15,
tone: "sine",
buffer: "", // current dot/dash buffer being typed
decoded: "", // accumulated decoded text
morseOut: "", // accumulated raw morse (with " " and "/" separators)
playing: false, // text-to-morse playback in progress
abortPlay: false,
};
const unit = () => 1200 / State.wpm;
// Decoder commit-pause: must exceed one dash plus a human inter-symbol gap.
const pauseMs = () => Math.max(700, unit() * 5);
/* ---------- TREE RENDERING ---------- */
const svg = document.getElementById("tree-svg");
const SVG_NS = "http://www.w3.org/2000/svg";
function svgEl(name, attrs = {}) {
const e = document.createElementNS(SVG_NS, name);
for (const k in attrs) e.setAttribute(k, attrs[k]);
return e;
}
function renderTree() {
svg.innerHTML = "";
const model = TREE.model;
const modelId = TREE.modelId;
// group for branches (drawn under nodes)
const gBranches = svgEl("g", { class: "branches" });
const gNodes = svgEl("g", { class: "nodes" });
const gExtras = svgEl("g", { class: "extras" });
svg.appendChild(gBranches);
svg.appendChild(gNodes);
svg.appendChild(gExtras);
// branches
const isModel4 = modelId === "4";
TREE.branches.forEach(b => {
const a = TREE.nodes[b.from], c = TREE.nodes[b.to];
let d;
if (isModel4 && Math.abs(a.x - c.x) > 0.5 && Math.abs(a.y - c.y) > 0.5) {
// right-angle (L-shaped) connector: drop vertically from parent, then
// run horizontally to the child — matches the compact PCB-style layout.
d = `M${a.x},${a.y} L${a.x},${c.y} L${c.x},${c.y}`;
} else {
d = `M${a.x},${a.y} L${c.x},${c.y}`;
}
const ln = svgEl("path", {
class: "branch", "data-to": b.to, d,
});
gBranches.appendChild(ln);
});
// root: small triangle/antenna stub
const root = TREE.nodes[""];
gNodes.appendChild(svgEl("line", {
class: "root-mast", x1: root.x, y1: root.y - 16, x2: root.x, y2: root.y + 4,
}));
const tri = svgEl("polygon", {
class: "root-core",
points: `${root.x-8},${root.y-2} ${root.x+8},${root.y-2} ${root.x},${root.y-16}`,
});
gNodes.appendChild(tri);
gNodes.appendChild(svgEl("circle", { class: "root-core", cx: root.x, cy: root.y + 6, r: 3 }));
// LED size per level — shrink for deep levels (used by Model 2)
function ledSizeFor(code) {
const scale = +getComputedStyle(document.documentElement)
.getPropertyValue("--led-scale") || 1;
const apply = (sz) => ({
r: sz.r * scale, s: sz.s * scale, rx: sz.rx
});
const len = code.length;
if (modelId === "2") {
if (len <= 4) return apply({ r: 21, s: 38, rx: 5 });
if (len === 5) return apply({ r: 15, s: 28, rx: 4 }); // numbers
return apply({ r: 11, s: 22, rx: 3 }); // punctuation
}
if (modelId === "4") return apply({ r: 21, s: 38, rx: 5 });
return apply({ r: 21, s: 38, rx: 5 });
}
// all nodes (letters and, for Model 2, numbers + punctuation)
Object.keys(TREE.nodes).forEach(code => {
if (code === "") return;
const n = TREE.nodes[code];
if (!n.label) return;
const isDot = code.slice(-1) === ".";
const sz = ledSizeFor(code);
const g = svgEl("g", { class: "led-g", "data-code": code, transform: `translate(${n.x},${n.y})` });
if (NUMBERS.includes(n.label)) g.classList.add("led-num");
if (PUNCT.includes(n.label)) g.classList.add("led-punct");
let led;
if (isDot) {
led = svgEl("circle", { class: "led", cx: 0, cy: 0, r: sz.r });
} else {
led = svgEl("rect", { class: "led", x: -sz.s/2, y: -sz.s/2, width: sz.s, height: sz.s, rx: sz.rx });
}
g.appendChild(led);
const txt = svgEl("text", { class: "led-glyph", x: 0, y: 1, "text-anchor": "middle" });
txt.textContent = n.label;
g.appendChild(txt);
gNodes.appendChild(g);
});
// optional chip rows below the tree
let bottomY = 0;
Object.values(TREE.nodes).forEach(n => { if (n.y > bottomY) bottomY = n.y; });
if (model.showNumPunctAsChips) {
const scale = +getComputedStyle(document.documentElement)
.getPropertyValue("--led-scale") || 1;
const numY = bottomY + 80;
const numSpan = 880;
NUMBERS.forEach((d, i) => {
const m = MORSE[d];
const x = 500 + ((i - (NUMBERS.length-1)/2) / (NUMBERS.length-1)) * numSpan;
const g = svgEl("g", { class: "chip-g", "data-chip": d, transform: `translate(${x},${numY})` });
const w = 64 * scale, h = 50 * scale;
g.appendChild(svgEl("rect", { class: "chip-box", x: -w/2, y: -h/2, width: w, height: h, rx: 9 }));
const t = svgEl("text", { class: "chip-letter", x: 0, y: -7, "text-anchor": "middle" });
t.textContent = d;
g.appendChild(t);
const mt = svgEl("text", { class: "chip-code", x: 0, y: 14, "text-anchor": "middle" });
mt.textContent = m.replace(/\./g, "·").replace(/-/g, "—");
g.appendChild(mt);
gExtras.appendChild(g);
});
const punY = numY + 78;
const punSpan = 840;
PUNCT.forEach((sym, i) => {
const m = MORSE[sym];
const x = 500 + ((i - (PUNCT.length-1)/2) / (PUNCT.length-1)) * punSpan;
const g = svgEl("g", { class: "chip-g", "data-chip": sym, transform: `translate(${x},${punY})` });
const w = 124 * scale, h = 48 * scale;
g.appendChild(svgEl("rect", { class: "chip-box", x: -w/2, y: -h/2, width: w, height: h, rx: 9 }));
const t = svgEl("text", { class: "chip-letter", x: -34, y: 0, "text-anchor": "middle" });
t.textContent = sym;
g.appendChild(t);
const mt = svgEl("text", { class: "chip-code", x: 14, y: 0, "text-anchor": "middle" });
mt.textContent = m.replace(/\./g, "·").replace(/-/g, "—");
g.appendChild(mt);
gExtras.appendChild(g);
});
bottomY = punY + 30;
} else {
bottomY += 40;
}
svg.setAttribute("viewBox", `0 0 1000 ${bottomY}`);
}
/* ---------- light up helpers ---------- */
function clearLights() {
svg.querySelectorAll(".branch.lit").forEach(n => { n.classList.remove("lit"); n.style.removeProperty("--step-pos"); });
svg.querySelectorAll(".led-g.lit, .led-g.final").forEach(n => { n.classList.remove("lit", "final"); n.style.removeProperty("--step-pos"); });
svg.querySelectorAll(".chip-g.lit, .chip-g.final").forEach(n => { n.classList.remove("lit", "final"); n.style.removeProperty("--step-pos"); });
}
function litLED(code, opts={}) {
const ledg = svg.querySelector(`.led-g[data-code="${cssEsc(code)}"]`);
if (ledg) {
ledg.classList.remove("lit", "final");
void ledg.getBoundingClientRect();
ledg.classList.add(opts.final ? "final" : "lit");
if (opts.stepPos != null) ledg.style.setProperty("--step-pos", opts.stepPos);
}
const br = svg.querySelector(`.branch[data-to="${cssEsc(code)}"]`);
if (br) {
br.classList.add("lit");
if (opts.stepPos != null) br.style.setProperty("--step-pos", opts.stepPos);
}
}
function litChip(sym, opts={}) {
const ch = svg.querySelector(`.chip-g[data-chip="${cssEsc(sym)}"]`);
if (!ch) return;
ch.classList.remove("lit", "final");
void ch.getBoundingClientRect();
ch.classList.add(opts.final ? "final" : "lit");
}
function unlitChips() {
svg.querySelectorAll(".chip-g").forEach(ch => ch.classList.remove("lit", "final"));
}
function cssEsc(s){ return s.replace(/[\\"]/g, "\\$&"); }
/* ---------- decoded letter spotlight ---------- */
const letterSpot = document.getElementById("letter-spot");
function showSpotlight(letter, code) {
letterSpot.querySelector(".ls-glyph").textContent = letter;
letterSpot.querySelector(".ls-morse").textContent = code.replace(/\./g, "·").replace(/-/g, "—");
letterSpot.classList.add("show");
clearTimeout(showSpotlight._t);
showSpotlight._t = setTimeout(() => letterSpot.classList.remove("show"), 700);
}
/* ---------- transmit animation for a full letter ---------- */
async function transmitLetter(letter) {
letter = letter.toUpperCase();
if (letter === " ") {
appendDecoded(" ");
State.morseOut += "/ ";
updateReadout();
await sleep(unit() * 4);
return;
}
const code = MORSE[letter];
if (!code) return;
clearLights();
unlitChips();
setTransmitting(true);
// light each step
for (let i = 1; i <= code.length; i++) {
if (State.abortPlay) break;
const prefix = code.slice(0, i);
const isFinal = i === code.length;
const isInTree = TREE.nodes[prefix] && TREE.nodes[prefix].label;
const sym = prefix.slice(-1);
const dur = sym === "." ? unit() : unit() * 3;
const stepPos = code.length === 1 ? 1 : i / code.length;
const isNumOrPunct = NUMBERS.includes(letter) || PUNCT.includes(letter);
if (isInTree) {
litLED(prefix, { final: isFinal, stepPos });
} else if (isFinal && isNumOrPunct) {
// final chip lit after the loop
} else if (i < code.length) {
const br = svg.querySelector(`.branch[data-to="${cssEsc(prefix)}"]`);
if (br) { br.classList.add("lit"); br.style.setProperty("--step-pos", stepPos); }
}
Beeper.beep(dur, State.tone);
await sleep(dur);
if (i < code.length) await sleep(unit());
}
if ((NUMBERS.includes(letter) || PUNCT.includes(letter)) && !(TREE.nodes[code] && TREE.nodes[code].label)) {
litChip(letter, { final: true });
}
if (!State.abortPlay) {
showSpotlight(letter, code);
appendDecoded(letter);
State.morseOut += code + " ";
updateReadout();
}
await sleep(unit() * 1.5);
// avoid READY→TX flicker if more letters are queued behind this one
if (txPending <= 1 && !State.playing) setTransmitting(false);
}
async function transmitText(text) {
State.playing = true;
State.abortPlay = false;
const playBtn = document.querySelector('[data-action="play-t2m"]');
if (playBtn) playBtn.disabled = true;
for (const ch of text) {
if (State.abortPlay) break;
if (ch === " ") {
appendDecoded(" "); State.morseOut += "/ "; updateReadout();
await sleep(unit() * 7);
continue;
}
const u = ch.toUpperCase();
if (!MORSE[u]) continue;
await transmitLetter(u);
await sleep(unit() * 3); // inter-letter gap
}
State.playing = false;
if (playBtn) playBtn.disabled = false;
setTimeout(() => { clearLights(); unlitChips(); }, 600);
}
function stopTransmit() {
State.abortPlay = true;
Beeper.killAll();
setTransmitting(false);
}
function setTransmitting(on) {
document.body.classList.toggle("transmitting", !!on);
document.getElementById("status-text").textContent = on ? "TX…" : "READY";
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
/* serialize standalone letter transmissions (live-type, ref clicks) */
let txQueue = Promise.resolve();
let txPending = 0;
function enqueueLetter(ch) {
// Don't interleave live-typed letters with a Text→Morse playback in progress
if (State.playing) return txQueue;
txPending += 1;
txQueue = txQueue
.then(() => transmitLetter(ch))
.then(() => sleep(unit() * 2))
.finally(() => {
txPending -= 1;
if (txPending === 0 && !State.playing) setTransmitting(false);
});
return txQueue;
}
/* ---------- READOUT ---------- */
const readoutMarks = document.getElementById("readout-marks");
const decodedText = document.getElementById("decoded-text");
const dcFill = document.getElementById("dc-fill");
function updateReadout() {
decodedText.textContent = State.decoded;
}
function appendDecoded(ch) {
State.decoded += ch;
// push to history (skip plain spaces — they're word separators)
if (ch && ch !== " ") pushHistory(ch);
else if (ch === " ") pushHistory(" ");
}
function setBufferDisplay() {
if (!State.buffer) {
readoutMarks.innerHTML = '<span class="rm-placeholder">— tap to send —</span>';
return;
}
const display = State.buffer.split("").map(s =>
s === "." ? "·" : "—"
).join(" ");
readoutMarks.textContent = display;
}
/* ---------- HISTORY TAPE ---------- */
const historyTape = document.getElementById("history-tape");
const HISTORY = []; // [{ ch, code }] — newest last
const HISTORY_MAX = 60;
function pushHistory(ch) {
if (ch === " ") {
// drop leading spaces; collapse consecutive ones
if (!HISTORY.length) return;
if (HISTORY[HISTORY.length-1].ch === " ") return;
HISTORY.push({ ch: " ", code: "" });
} else {
HISTORY.push({ ch, code: MORSE[ch] || "?" });
}
while (HISTORY.length > HISTORY_MAX) HISTORY.shift();
renderHistory();
}
function renderHistory() {
if (!HISTORY.length) {
historyTape.textContent = "";
historyTape.setAttribute("data-state", "empty");
return;
}
historyTape.removeAttribute("data-state");
historyTape.innerHTML = "";
HISTORY.forEach((item, i) => {
if (item.ch === " ") {
const sep = document.createElement("span");
sep.className = "ht-sep";
sep.title = "word break";
historyTape.appendChild(sep);
return;
}
const chip = document.createElement("button");
chip.className = "ht-chip";
chip.type = "button";
chip.title = `Replay ${item.ch}`;
chip.innerHTML =
`<span class="ht-ch">${item.ch}</span>` +
`<span class="ht-code">${item.code.replace(/\./g,"·").replace(/-/g,"—")}</span>`;
chip.addEventListener("click", () => {
if (State.playing) return;
enqueueLetter(item.ch);
});
if (i === HISTORY.length - 1) chip.classList.add("fresh");
historyTape.appendChild(chip);
});
// auto-scroll to the right edge
historyTape.scrollLeft = historyTape.scrollWidth;
}
function clearHistory() {
HISTORY.length = 0;
renderHistory();
}
/* ---------- DECODER STATE ---------- */
let decodeTimer = null;
let countdownRAF = null;
function pushSymbol(sym) {
Beeper.ensure();
State.buffer += sym;
setBufferDisplay();
// animate tree path so user sees progress
paintBufferOnTree();
// play beep
const dur = sym === "." ? unit() : unit() * 3;
Beeper.beep(dur, State.tone);
scheduleDecode();
}
function paintBufferOnTree() {
clearLights(); unlitChips();
if (!State.buffer) {
// buffer fully erased — clear TX state
if (!State.playing && txPending === 0) setTransmitting(false);
return;
}
const L = State.buffer.length;
for (let i = 1; i <= L; i++) {
const p = State.buffer.slice(0, i);
const stepPos = L === 1 ? 1 : i / L;
if (TREE.nodes[p] && TREE.nodes[p].label) litLED(p, { stepPos });
else {
const br = svg.querySelector(`.branch[data-to="${cssEsc(p)}"]`);
if (br) { br.classList.add("lit"); br.style.setProperty("--step-pos", stepPos); }
}
}
setTransmitting(true);
}
function scheduleDecode() {
if (decodeTimer) clearTimeout(decodeTimer);
if (countdownRAF) cancelAnimationFrame(countdownRAF);
const start = performance.now();
const dur = pauseMs();
const tick = () => {
const t = performance.now() - start;
const pct = Math.min(100, (t / dur) * 100);
dcFill.style.width = pct + "%";
if (t < dur) countdownRAF = requestAnimationFrame(tick);
};
tick();
decodeTimer = setTimeout(commitLetter, dur);
}
function commitLetter() {
dcFill.style.width = "0%";
if (!State.buffer) return;
const letter = FROM_MORSE[State.buffer];
if (letter) {
appendDecoded(letter);
State.morseOut += State.buffer + " ";
showSpotlight(letter, State.buffer);
// turn last LED green / chip
if (NUMBERS.includes(letter) || PUNCT.includes(letter)) {
litChip(letter, { final: true });
} else {
litLED(State.buffer, { final: true });
}
} else {
// unknown: flash decoded? show "?"
appendDecoded("?");
State.morseOut += "? ";
showSpotlight("?", State.buffer);
}
updateReadout();
State.buffer = "";
setBufferDisplay();
setTimeout(() => {
if (!State.buffer && !State.playing) { clearLights(); unlitChips(); setTransmitting(false); }
}, 600);
}
/* ---------- INPUTS ---------- */
/* Spacebar: tap=dot, hold=dash (threshold relative to WPM) */
let spaceDownT = 0, spaceHeld = false, spaceTimer = null;
window.addEventListener("keydown", e => {
if (e.target.matches("textarea, input")) return;
if (e.code === "Space") {
e.preventDefault();
Beeper.ensure();
if (!spaceHeld) {
spaceDownT = performance.now();
spaceHeld = true;
keypadEl.classList.add("pressed");
keypadSymbol.textContent = "·";
const threshold = Math.max(180, unit() * 1.6);
spaceTimer = setTimeout(() => { keypadSymbol.textContent = "—"; }, threshold);
}
} else if (/^Key[A-Z]$|^Digit[0-9]$/.test(e.code) && !e.metaKey && !e.ctrlKey && !e.altKey && !e.repeat) {
const ch = e.key.toUpperCase();
if (MORSE[ch] && !State.playing) {
// resume AudioContext inside the gesture so the first key plays sound
Beeper.ensure();
enqueueLetter(ch);
}
} else if (e.key === "Backspace" && !e.target.matches("textarea, input")) {
e.preventDefault();
State.decoded = State.decoded.slice(0, -1);
State.morseOut = State.morseOut.replace(/[^\s/]+\s$/, "");
updateReadout();
}
});
window.addEventListener("keyup", e => {
if (e.code === "Space") {
if (!spaceHeld) return;
const t = performance.now() - spaceDownT;
const threshold = Math.max(120, unit() * 1.6);
pushSymbol(t > threshold ? "-" : ".");
spaceHeld = false;
keypadEl.classList.remove("pressed");
keypadSymbol.textContent = "·";
clearTimeout(spaceTimer);
}
});
/* Keypad: tap = dot, hold = dash; mouse left/right too */
const keypadEl = document.getElementById("keypad");
const keypadSymbol = document.getElementById("keypad-symbol");
let kpDownT = 0, kpTimer = null;
function kpDown() {
kpDownT = performance.now();
keypadEl.classList.add("pressed");
keypadSymbol.textContent = "·";
// visual upgrade to dash after threshold
const threshold = Math.max(180, unit() * 1.6);
kpTimer = setTimeout(() => { keypadSymbol.textContent = "—"; }, threshold);
}
function kpUp(forceDash) {
if (!kpDownT) return;
const t = performance.now() - kpDownT;
const threshold = Math.max(180, unit() * 1.6);
pushSymbol(forceDash || t > threshold ? "-" : ".");
kpDownT = 0;
keypadEl.classList.remove("pressed");
keypadSymbol.textContent = "·";
clearTimeout(kpTimer);
}
function kpCancel() {
kpDownT = 0;
keypadEl.classList.remove("pressed");
keypadSymbol.textContent = "·";
clearTimeout(kpTimer);
}
keypadEl.addEventListener("pointerdown", e => {
e.preventDefault();
keypadEl.setPointerCapture(e.pointerId);
Beeper.ensure();
// right-click = dash directly on pointerup (no hold needed)
if (e.button === 2) {
pushSymbol("-"); return;
}
kpDown();
});
keypadEl.addEventListener("pointerup", e => {
if (e.button === 2) return;
kpUp();
});
keypadEl.addEventListener("pointercancel", kpCancel);
keypadEl.addEventListener("pointerleave", () => { if (kpDownT) kpUp(); });
keypadEl.addEventListener("contextmenu", e => e.preventDefault());
/* dot/dash/space/backspace buttons */
document.querySelector('[data-action="dot"]').addEventListener("click", () => pushSymbol("."));
document.querySelector('[data-action="dash"]').addEventListener("click", () => pushSymbol("-"));
document.querySelector('[data-action="space"]').addEventListener("click", () => {
if (decodeTimer) clearTimeout(decodeTimer);
if (State.buffer) commitLetter();
appendDecoded(" ");
State.morseOut += "/ ";
updateReadout();
});
document.querySelector('[data-action="backspace"]').addEventListener("click", () => {
if (State.buffer) {
State.buffer = State.buffer.slice(0, -1);
setBufferDisplay();
paintBufferOnTree();
scheduleDecode();
} else {
State.decoded = State.decoded.slice(0, -1);
State.morseOut = State.morseOut.replace(/[^\s/]+\s$/, "");
updateReadout();
}
});
/* ---------- TEXT → MORSE ---------- */
const t2mInput = document.getElementById("t2m-input");
const t2mPreview = document.getElementById("t2m-preview");
function refreshT2M() {
const text = t2mInput.value.trim();
if (!text) { t2mPreview.textContent = ""; return; }
t2mPreview.textContent = text.toUpperCase().split("").map(ch =>
ch === " " ? "/" : (MORSE[ch] || "?")
).join(" ").replace(/\./g, "·").replace(/-/g, "—");
}
t2mInput.addEventListener("input", refreshT2M);
document.querySelector('[data-action="play-t2m"]').addEventListener("click", () => {
if (State.playing) return;
Beeper.ensure();
State.decoded = "";
State.morseOut = "";
updateReadout();
const txt = t2mInput.value;
if (txt.trim()) transmitText(txt);
});
document.querySelector('[data-action="stop-t2m"]').addEventListener("click", stopTransmit);
document.querySelector('[data-action="share-t2m"]').addEventListener("click", () => {
const txt = t2mInput.value.trim();
if (!txt) return;
const morse = txt.toUpperCase().split("").map(ch =>
ch === " " ? "/" : (MORSE[ch] || "")
).join(" ");
const url = new URL(location.href);
url.searchParams.set("m", morse.replace(/ /g, "_"));
navigator.clipboard.writeText(url.toString()).catch(()=>{});
toast("Share link copied");
});
/* ---------- MORSE → TEXT ---------- */
const m2tInput = document.getElementById("m2t-input");
const m2tPreview = document.getElementById("m2t-preview");
function refreshM2T() {
const raw = m2tInput.value.replace(/[·•]/g, ".").replace(/[—–]/g, "-");
const words = raw.split(/\s*\/\s*|\s{3,}/);
m2tPreview.textContent = words.map(w =>
w.trim().split(/\s+/).map(s => FROM_MORSE[s] || (s ? "?" : "")).join("")
).join(" ");
}
m2tInput.addEventListener("input", refreshM2T);
function m2tInsert(s) { m2tInput.value += s; refreshM2T(); m2tInput.focus(); }
document.querySelector('[data-action="m2t-dot"]').addEventListener("click", () => m2tInsert("."));
document.querySelector('[data-action="m2t-dash"]').addEventListener("click", () => m2tInsert("-"));
document.querySelector('[data-action="m2t-slash"]').addEventListener("click", () => m2tInsert(" / "));
document.querySelector('[data-action="m2t-clear"]').addEventListener("click", () => { m2tInput.value = ""; refreshM2T(); });
document.querySelector('[data-action="play-m2t"]').addEventListener("click", () => {
if (State.playing) return;
Beeper.ensure();
// decode the morse to plain text, then route through the same playback pipeline
const raw = m2tInput.value.replace(/[·•]/g, ".").replace(/[—–]/g, "-");
const words = raw.split(/\s*\/\s*|\s{3,}/);
const text = words.map(w =>
w.trim().split(/\s+/).map(s => FROM_MORSE[s] || "").join("")
).join(" ").trim();
if (!text) return;
State.decoded = ""; State.morseOut = "";
updateReadout();
transmitText(text);
});
document.querySelector('[data-action="stop-m2t"]').addEventListener("click", stopTransmit);
/* ---------- COPY / CLEAR ---------- */
function copyBtn(action, getter) {
const btn = document.querySelector(`[data-action="${action}"]`);
btn.addEventListener("click", async () => {
const v = getter().trim();
if (!v) return;
try { await navigator.clipboard.writeText(v); } catch(_) {}
btn.classList.add("copied");
setTimeout(() => btn.classList.remove("copied"), 900);
});
}
copyBtn("copy-text", () => State.decoded);
copyBtn("copy-morse", () => State.morseOut);
document.querySelector('[data-action="clear"]').addEventListener("click", () => {
State.decoded = ""; State.morseOut = ""; State.buffer = "";
updateReadout(); setBufferDisplay();
clearLights(); unlitChips();
});
document.querySelector('[data-action="clear-history"]').addEventListener("click", clearHistory);
/* ---------- SOS ---------- */
document.querySelector('[data-action="sos"]').addEventListener("click", () => {
if (State.playing) return;
Beeper.ensure();
State.decoded = ""; State.morseOut = "";
updateReadout();
transmitText("SOS");
});
/* ---------- TABS ---------- */
document.querySelectorAll(".tab").forEach(t => {
t.addEventListener("click", () => {
document.querySelectorAll(".tab").forEach(x => {
const isActive = x === t;
x.classList.toggle("active", isActive);
x.setAttribute("aria-selected", isActive ? "true" : "false");
});
document.querySelectorAll(".tabpanel").forEach(p => p.hidden = p.dataset.panel !== t.dataset.tab);
if (t.dataset.tab === "drill") Drill.start();
});
});
/* ---------- DRILL (practice mode) ---------- */
const Drill = (() => {
const pools = {
letters: LETTERS.slice(),
alnum: LETTERS.concat(NUMBERS),
all: LETTERS.concat(NUMBERS).concat(PUNCT),
};
const state = { pool: "letters", current: null, correct: 0, total: 0, streak: 0, locked: false, mode: "blind" };
let advanceTimer = null;
const morseEl = document.getElementById("drill-morse");
const choicesEl = document.getElementById("drill-choices");
const feedbackEl = document.getElementById("drill-feedback");
const scoreEl = document.getElementById("drill-score");
const streakEl = document.getElementById("drill-streak");
function pickAnswer() {
const pool = pools[state.pool];
return pool[Math.floor(Math.random() * pool.length)];
}
function pickDistractors(answer) {
const pool = pools[state.pool].filter(c => c !== answer);
// shuffle and pick 3
for (let i = pool.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[pool[i], pool[j]] = [pool[j], pool[i]];
}
return pool.slice(0, 3);
}
function renderScore() {
scoreEl.textContent = `${state.correct} / ${state.total}`;
if (state.streak >= 3) {
streakEl.textContent = `${state.streak}× streak`;
streakEl.style.opacity = 1;
} else {
streakEl.textContent = "";
streakEl.style.opacity = 0;
}
}
async function newRound({ silent } = {}) {
state.locked = false;
feedbackEl.textContent = "";
feedbackEl.removeAttribute("data-result");
state.current = pickAnswer();
const opts = [state.current, ...pickDistractors(state.current)];
// shuffle
for (let i = opts.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[opts[i], opts[j]] = [opts[j], opts[i]];
}
// render choices
choicesEl.innerHTML = "";
opts.forEach(c => {
const b = document.createElement("button");
b.className = "drill-choice";
b.type = "button";
b.textContent = c;
b.dataset.val = c;
b.addEventListener("click", () => answer(c, b));
choicesEl.appendChild(b);
});
morseEl.textContent = MORSE[state.current].replace(/\./g, "·").replace(/-/g, "—");
if (state.mode === "visible") {
morseEl.classList.remove("hidden");
} else {
// blind mode: show placeholder until answered
morseEl.textContent = "— — —";
morseEl.classList.add("hidden");
}
if (!silent) await playCurrent();
}
async function playCurrent() {
if (!state.current) return;
// play through tree + audio; don't touch decoded text or history
Beeper.ensure();
const code = MORSE[state.current];
clearLights(); unlitChips();
setTransmitting(true);
for (let i = 1; i <= code.length; i++) {
const prefix = code.slice(0, i);
const sym = prefix.slice(-1);
const dur = sym === "." ? unit() : unit() * 3;
const inTree = TREE.nodes[prefix] && TREE.nodes[prefix].label;
if (inTree) {
litLED(prefix, { final: false });
} else {
const br = svg.querySelector(`.branch[data-to="${cssEsc(prefix)}"]`);
if (br) br.classList.add("lit");
}
Beeper.beep(dur, State.tone);
await sleep(dur);
if (i < code.length) await sleep(unit());
}
if (NUMBERS.includes(state.current) || PUNCT.includes(state.current)) {
litChip(state.current);
}
await sleep(unit() * 1.5);
setTransmitting(false);
}
function answer(choice, btnEl) {
if (state.locked) return;