FillerLists commited on
Commit
1bb4a67
·
verified ·
1 Parent(s): 55018c3

Add Morse code timing visualiser

Browse files
Files changed (2) hide show
  1. README.md +23 -5
  2. index.html +265 -17
README.md CHANGED
@@ -1,11 +1,29 @@
1
  ---
2
- title: Morse Code Timing
3
- emoji: 📊
4
- colorFrom: gray
5
- colorTo: blue
6
  sdk: static
7
  pinned: false
 
8
  short_description: See dits, dahs and gaps on a timeline at any WPM
 
 
 
 
 
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Morse Code Timing Visualiser
3
+ emoji: 📡
4
+ colorFrom: yellow
5
+ colorTo: gray
6
  sdk: static
7
  pinned: false
8
+ license: cc0-1.0
9
  short_description: See dits, dahs and gaps on a timeline at any WPM
10
+ tags:
11
+ - morse-code
12
+ - audio
13
+ - signal-processing
14
+ - education
15
  ---
16
 
17
+ # Morse Code Timing Visualiser
18
+
19
+ Type some text and see exactly how it is sent in Morse code: every dit, dah and gap on one timeline, measured in units and milliseconds.
20
+
21
+ - Change the character speed (WPM) and the Farnsworth effective speed and watch the gaps stretch.
22
+ - Play it as a tone, with a playhead that follows the timeline.
23
+ - Compare the same word at three speeds, which shows why an audio decoder can't use fixed thresholds and has to estimate the unit length from the signal.
24
+
25
+ Timing follows the PARIS standard (50 units per word, so one unit is 1200 / WPM milliseconds) and the ARRL formula for Farnsworth spacing.
26
+
27
+ Everything runs in the browser in a single HTML file. No libraries, no data leaves the page.
28
+
29
+ Made by [SimpleMorse](https://simplemorse.com/morse-code-audio-decoder), which has a free Morse audio decoder that reads from a file or the microphone. CC0: reuse it however you like.
index.html CHANGED
@@ -1,19 +1,267 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>Morse Code Timing Visualiser</title>
7
+ <meta name="description" content="See every dit, dah and gap of Morse code on a timeline, in units and milliseconds, at any WPM and Farnsworth speed." />
8
+ <style>
9
+ :root {
10
+ --bg: #fbfaf7; --card: #ffffff; --ink: #1d1d1b; --muted: #66645e; --line: #e4e1d9;
11
+ --on: #c2410c; --gap-el: #d8d4ca; --gap-ch: #8fb3d9; --gap-wd: #3d6fa8; --accent: #c2410c;
12
+ }
13
+ @media (prefers-color-scheme: dark) {
14
+ :root {
15
+ --bg: #161614; --card: #1f1f1c; --ink: #eceae4; --muted: #a29f96; --line: #34332e;
16
+ --on: #fb923c; --gap-el: #45433d; --gap-ch: #4f78a3; --gap-wd: #8db7e6; --accent: #fb923c;
17
+ }
18
+ }
19
+ * { box-sizing: border-box; }
20
+ body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
21
+ main { max-width: 900px; margin: 0 auto; padding: 24px 16px 48px; }
22
+ h1 { font-size: 1.7rem; margin: 0 0 4px; }
23
+ h2 { font-size: 1.15rem; margin: 0 0 8px; }
24
+ p { margin: 0 0 12px; }
25
+ .lead { color: var(--muted); margin-bottom: 20px; }
26
+ .card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
27
+ .controls { display: grid; grid-template-columns: 1fr; gap: 12px; }
28
+ @media (min-width: 640px) { .controls { grid-template-columns: 2fr 1fr 1fr; } }
29
+ label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
30
+ input[type=text] { width: 100%; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
31
+ input[type=range] { width: 100%; accent-color: var(--accent); }
32
+ .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
33
+ button { font: inherit; padding: 8px 16px; border-radius: 8px; border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer; }
34
+ button.ghost { background: transparent; color: var(--accent); }
35
+ .morse { font-family: ui-monospace, Menlo, monospace; font-size: 1.05rem; letter-spacing: .08em; word-break: break-all; }
36
+ .stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 8px; }
37
+ @media (min-width: 640px) { .stats { grid-template-columns: repeat(4, 1fr); } }
38
+ .stat { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
39
+ .stat b { display: block; font-size: 1.15rem; }
40
+ .stat span { font-size: .8rem; color: var(--muted); }
41
+ .tl-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
42
+ svg { display: block; }
43
+ .legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: .85rem; color: var(--muted); margin-top: 8px; }
44
+ .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 5px; }
45
+ table { width: 100%; border-collapse: collapse; font-size: .92rem; }
46
+ th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }
47
+ th { color: var(--muted); font-weight: 600; }
48
+ .note { font-size: .88rem; color: var(--muted); }
49
+ a { color: var(--accent); }
50
+ </style>
51
+ </head>
52
+ <body>
53
+ <main>
54
+ <h1>Morse Code Timing Visualiser</h1>
55
+ <p class="lead">Morse code is a timing code. Type a word and see every dit, dah and gap on one timeline, in units and milliseconds.</p>
56
+
57
+ <section class="card">
58
+ <div class="controls">
59
+ <div><label for="text">Text</label><input id="text" type="text" value="PARIS" maxlength="60" autocomplete="off" /></div>
60
+ <div><label for="wpm">Character speed: <b id="wpmv"></b> WPM</label><input id="wpm" type="range" min="5" max="40" value="20" /></div>
61
+ <div><label for="eff">Effective speed (Farnsworth): <b id="effv"></b> WPM</label><input id="eff" type="range" min="5" max="40" value="20" /></div>
62
+ </div>
63
+ <div class="row">
64
+ <button id="play" type="button">Play</button>
65
+ <span class="morse" id="morse"></span>
66
+ </div>
67
+ <div class="stats">
68
+ <div class="stat"><b id="sUnit"></b><span>one unit (dit)</span></div>
69
+ <div class="stat"><b id="sUnits"></b><span>units in total</span></div>
70
+ <div class="stat"><b id="sTime"></b><span>to send it</span></div>
71
+ <div class="stat"><b id="sOn"></b><span>of the time the tone is on</span></div>
72
+ </div>
73
+ </section>
74
+
75
+ <section class="card">
76
+ <h2>The timeline</h2>
77
+ <div class="tl-wrap"><svg id="tl" height="96" role="img" aria-label="Timeline of tone on and off periods"></svg></div>
78
+ <div class="legend">
79
+ <span><i class="sw" style="background:var(--on)"></i>Tone on (dit 1 unit, dah 3)</span>
80
+ <span><i class="sw" style="background:var(--gap-el)"></i>Gap inside a letter (1)</span>
81
+ <span><i class="sw" style="background:var(--gap-ch)"></i>Gap between letters (3)</span>
82
+ <span><i class="sw" style="background:var(--gap-wd)"></i>Gap between words (7)</span>
83
+ </div>
84
+ <p class="note" id="farnNote" style="margin-top:10px"></p>
85
+ </section>
86
+
87
+ <section class="card">
88
+ <h2>Same word, three speeds</h2>
89
+ <p>The pattern is identical; only the clock changes. A dah at 30 WPM (120 ms) is shorter than a dit at 5 WPM (240 ms).</p>
90
+ <div class="tl-wrap"><svg id="cmp" height="150" role="img" aria-label="The same word at 5, 15 and 30 WPM on one time scale"></svg></div>
91
+ <p class="note" style="margin-top:10px">This is why a Morse audio decoder can't use fixed thresholds like "anything over 100 ms is a dah". It has to estimate the unit length from the signal itself, for example by grouping the measured tone lengths into a short cluster (dits) and a long one (dahs), and keep adjusting as the sender speeds up or slows down. Hand-sent Morse also drifts from the ideal 1:3:7 ratios, so the decoder has to allow for that too.</p>
92
+ </section>
93
+
94
+ <section class="card">
95
+ <h2>The rules</h2>
96
+ <table>
97
+ <tr><th>Element</th><th>Length</th><th id="hMs">At 20 WPM</th></tr>
98
+ <tbody id="rules"></tbody>
99
+ </table>
100
+ <p class="note" style="margin-top:10px">Speed is defined by the word PARIS, which is exactly 50 units long including the gap after it. At <i>W</i> words per minute, one unit is 60 000 / (50 × <i>W</i>) = 1200 / <i>W</i> milliseconds. With Farnsworth spacing the characters keep their full speed and only the gaps between letters and words are stretched (ARRL formula), so beginners hear fast character shapes with more thinking time.</p>
101
+ </section>
102
+
103
+ <p class="note">Made by <a href="https://simplemorse.com/morse-code-audio-decoder" target="_blank" rel="noopener">SimpleMorse</a>, which has a free Morse audio decoder for files and the microphone. This page is public domain (CC0).</p>
104
+ </main>
105
+
106
+ <script>
107
+ const CODES = {
108
+ A: '.-', B: '-...', C: '-.-.', D: '-..', E: '.', F: '..-.', G: '--.', H: '....', I: '..', J: '.---',
109
+ K: '-.-', L: '.-..', M: '--', N: '-.', O: '---', P: '.--.', Q: '--.-', R: '.-.', S: '...', T: '-',
110
+ U: '..-', V: '...-', W: '.--', X: '-..-', Y: '-.--', Z: '--..',
111
+ 0: '-----', 1: '.----', 2: '..---', 3: '...--', 4: '....-', 5: '.....', 6: '-....', 7: '--...', 8: '---..', 9: '----.',
112
+ '.': '.-.-.-', ',': '--..--', '?': '..--..', '/': '-..-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '@': '.--.-.',
113
+ };
114
+ const $ = (id) => document.getElementById(id);
115
+ const css = (v) => getComputedStyle(document.documentElement).getPropertyValue(v).trim();
116
+
117
+ /** Words of letter codes, skipping characters with no Morse code. */
118
+ function encode(text) {
119
+ return text.toUpperCase().split(/\s+/).filter(Boolean)
120
+ .map((w) => [...w].map((c) => CODES[c]).filter(Boolean)).filter((w) => w.length);
121
+ }
122
+
123
+ /**
124
+ * Segments for the timeline: { kind: 'dit'|'dah'|'el'|'ch'|'wd', units, ms }.
125
+ * Farnsworth gaps use the ARRL formula; units for stretched gaps are in character-speed units.
126
+ */
127
+ function segments(words, wpm, eff) {
128
+ const u = 1200 / wpm;
129
+ let ch = 3 * u, wd = 7 * u;
130
+ if (eff < wpm) {
131
+ const ta = ((60 * wpm - 37.2 * eff) / (eff * wpm)) * 1000;
132
+ ch = (3 * ta) / 19; wd = (7 * ta) / 19;
133
+ }
134
+ const out = [];
135
+ words.forEach((word, wi) => {
136
+ if (wi) out.push({ kind: 'wd', ms: wd });
137
+ word.forEach((code, li) => {
138
+ if (li) out.push({ kind: 'ch', ms: ch });
139
+ [...code].forEach((s, si) => {
140
+ if (si) out.push({ kind: 'el', ms: u });
141
+ out.push({ kind: s === '.' ? 'dit' : 'dah', ms: s === '.' ? u : 3 * u });
142
+ });
143
+ });
144
+ });
145
+ out.forEach((s) => (s.units = s.ms / u));
146
+ return { out, u };
147
+ }
148
+
149
+ const fmtMs = (ms) => (ms >= 1000 ? (ms / 1000).toFixed(2) + ' s' : Math.round(ms) + ' ms');
150
+ const fill = { dit: '--on', dah: '--on', el: '--gap-el', ch: '--gap-ch', wd: '--gap-wd' };
151
+
152
+ function drawTimeline(svg, segs, { pxPerMs, rowY = 10, h = 34, labels = true, offset = 0 }) {
153
+ let x = offset, html = '';
154
+ for (const s of segs) {
155
+ const w = Math.max(1, s.ms * pxPerMs);
156
+ const on = s.kind === 'dit' || s.kind === 'dah';
157
+ const y = on ? rowY : rowY + h * 0.35, hh = on ? h : h * 0.3;
158
+ html += `<rect x="${x.toFixed(2)}" y="${y}" width="${w.toFixed(2)}" height="${hh}" rx="2" fill="${css(fill[s.kind])}"><title>${s.kind === 'dit' ? 'Dit' : s.kind === 'dah' ? 'Dah' : s.kind === 'el' ? 'Gap in letter' : s.kind === 'ch' ? 'Gap between letters' : 'Gap between words'}: ${Math.round(s.ms)} ms (${+s.units.toFixed(2)} units)</title></rect>`;
159
+ if (labels && w > 16) html += `<text x="${(x + w / 2).toFixed(2)}" y="${rowY + h + 16}" font-size="11" text-anchor="middle" fill="${css('--muted')}">${+s.units.toFixed(1)}</text>`;
160
+ x += w;
161
+ }
162
+ return { html, end: x };
163
+ }
164
+
165
+ let state = null;
166
+ function render() {
167
+ const wpm = +$('wpm').value;
168
+ if (+$('eff').value > wpm) $('eff').value = wpm;
169
+ const eff = +$('eff').value;
170
+ $('wpmv').textContent = wpm; $('effv').textContent = eff; $('hMs').textContent = `At ${wpm} WPM`;
171
+ const words = encode($('text').value);
172
+ $('morse').textContent = words.map((w) => w.join(' ')).join(' / ') || 'Type letters or digits';
173
+ const { out, u } = segments(words, wpm, eff);
174
+ const total = out.reduce((a, s) => a + s.ms, 0);
175
+ const onMs = out.filter((s) => s.kind === 'dit' || s.kind === 'dah').reduce((a, s) => a + s.ms, 0);
176
+ $('sUnit').textContent = Math.round(u) + ' ms';
177
+ $('sUnits').textContent = +(total / u).toFixed(1);
178
+ $('sTime').textContent = fmtMs(total);
179
+ $('sOn').textContent = total ? Math.round((100 * onMs) / total) + '%' : '0%';
180
+ $('farnNote').textContent = eff < wpm
181
+ ? `Farnsworth on: characters at ${wpm} WPM, but gaps between letters and words stretched so the overall speed is ${eff} WPM.`
182
+ : 'Numbers under the bars are lengths in units. Hover a bar for milliseconds.';
183
+
184
+ const tl = $('tl');
185
+ const width = Math.max(tl.parentElement.clientWidth - 2, 300);
186
+ const pxPerMs = total ? Math.max((width - 20) / total, 0.04) : 1;
187
+ const d = drawTimeline(tl, out, { pxPerMs, offset: 10 });
188
+ tl.setAttribute('width', Math.max(width, d.end + 10));
189
+ tl.innerHTML = d.html + `<line id="head" x1="10" x2="10" y1="2" y2="70" stroke="${css('--ink')}" stroke-width="2" opacity="0"/>`;
190
+ state = { out, total, pxPerMs };
191
+
192
+ $('rules').innerHTML = [
193
+ ['Dit', '1 unit', u], ['Dah', '3 units', 3 * u], ['Gap inside a letter', '1 unit', u],
194
+ ['Gap between letters', '3 units', 3 * u], ['Gap between words', '7 units', 7 * u],
195
+ ].map(([a, b, ms]) => `<tr><td>${a}</td><td>${b}</td><td>${Math.round(ms)} ms</td></tr>`).join('');
196
+
197
+ drawCompare(words);
198
+ }
199
+
200
+ function drawCompare(words) {
201
+ const svg = $('cmp');
202
+ const width = Math.max(svg.parentElement.clientWidth - 2, 300);
203
+ const speeds = [5, 15, 30];
204
+ const sets = speeds.map((s) => segments(words, s, s).out);
205
+ const longest = Math.max(1, ...sets.map((o) => o.reduce((a, x) => a + x.ms, 0)));
206
+ const pxPerMs = (width - 80) / longest;
207
+ let html = '';
208
+ sets.forEach((segs, i) => {
209
+ const y = 12 + i * 46;
210
+ html += `<text x="6" y="${y + 18}" font-size="12" fill="${css('--muted')}">${speeds[i]} WPM</text>`;
211
+ html += drawTimeline(svg, segs, { pxPerMs, rowY: y, h: 26, labels: false, offset: 70 }).html;
212
+ });
213
+ svg.setAttribute('width', width);
214
+ svg.innerHTML = html;
215
+ }
216
+
217
+ let ctx = null, playing = null;
218
+ function play() {
219
+ if (!state || !state.total) return;
220
+ if (playing) { stop(); return; }
221
+ ctx = ctx || new (window.AudioContext || window.webkitAudioContext)();
222
+ const osc = ctx.createOscillator(), gain = ctx.createGain();
223
+ osc.frequency.value = 600; osc.type = 'sine'; gain.gain.value = 0;
224
+ osc.connect(gain).connect(ctx.destination);
225
+ const t0 = ctx.currentTime + 0.05, ramp = 0.004;
226
+ let t = t0;
227
+ for (const s of state.out) {
228
+ const dur = s.ms / 1000;
229
+ if (s.kind === 'dit' || s.kind === 'dah') {
230
+ gain.gain.setValueAtTime(0, t);
231
+ gain.gain.linearRampToValueAtTime(0.4, t + ramp);
232
+ gain.gain.setValueAtTime(0.4, t + dur - ramp);
233
+ gain.gain.linearRampToValueAtTime(0, t + dur);
234
+ }
235
+ t += dur;
236
+ }
237
+ osc.start(t0); osc.stop(t + 0.05);
238
+ const head = document.getElementById('head');
239
+ head.setAttribute('opacity', '1');
240
+ playing = { osc, t0, end: t };
241
+ $('play').textContent = 'Stop';
242
+ const step = () => {
243
+ if (!playing) return;
244
+ const ms = (ctx.currentTime - playing.t0) * 1000;
245
+ const x = 10 + Math.max(0, Math.min(ms, state.total)) * state.pxPerMs;
246
+ head.setAttribute('x1', x); head.setAttribute('x2', x);
247
+ if (ctx.currentTime >= playing.end) stop(); else requestAnimationFrame(step);
248
+ };
249
+ requestAnimationFrame(step);
250
+ osc.onended = () => { if (playing && playing.osc === osc) stop(); };
251
+ }
252
+ function stop() {
253
+ if (playing) { try { playing.osc.stop(); } catch (e) {} }
254
+ playing = null;
255
+ $('play').textContent = 'Play';
256
+ const head = document.getElementById('head');
257
+ if (head) head.setAttribute('opacity', '0');
258
+ }
259
+
260
+ ['text', 'wpm', 'eff'].forEach((id) => $(id).addEventListener('input', () => { stop(); render(); }));
261
+ $('play').addEventListener('click', play);
262
+ window.addEventListener('resize', render);
263
+ matchMedia('(prefers-color-scheme: dark)').addEventListener('change', render);
264
+ render();
265
+ </script>
266
+ </body>
267
  </html>