An audio button for a Morse letter looks simple: make a short tone for a dot and a longer one for a dash. The silence between those tones matters just as much.
A useful way to build the control is to calculate its timing first, then pass that schedule to the audio layer. This gives you something small and deterministic to test before you listen to the result.
Start with one character
In International Morse, a dot takes one time unit, a dash takes three, and the gap between marks within a character takes one. The ITU's International Morse code recommendation also distinguishes the longer gaps between characters and words.
For a single letter, keep the problem small. This helper accepts a dot-and-dash pattern and returns a list of tone intervals:
function planCharacter(pattern, unitMs = 120) {
if (!/^[.-]+$/.test(pattern)) {
throw new RangeError("Use dots and dashes for one character");
}
if (!Number.isFinite(unitMs) || unitMs <= 0) {
throw new RangeError("The time unit must be positive");
}
const marks = [];
let cursorMs = 0;
for (const [index, symbol] of [...pattern].entries()) {
const durationMs = unitMs * (symbol === "." ? 1 : 3);
marks.push({ startMs: cursorMs, durationMs });
cursorMs += durationMs;
if (index < pattern.length - 1) cursorMs += unitMs;
}
return { marks, durationMs: cursorMs };
}
For A, which is .-, planCharacter(".-") returns:
{
marks: [
{ startMs: 0, durationMs: 120 },
{ startMs: 240, durationMs: 360 }
],
durationMs: 600
}
The second mark starts at 240 ms because the dot is followed by 120 ms of silence. There is no extra trailing gap in this result. A caller that plays a sequence of letters should add the appropriate character or word spacing separately; this function only plans one character.
Check the schedule before the sound
Some useful fixtures are easy to reason about:
| Pattern | Tone durations | Total, including internal gaps |
|---|---|---|
. |
120 ms | 120 ms |
- |
360 ms | 360 ms |
.- |
120, 360 ms | 600 ms |
... |
120, 120, 120 ms | 600 ms |
--- |
360, 360, 360 ms | 1320 ms |
Also check that empty patterns, spaces inside a pattern, and non-positive time units fail visibly. Silently dropping an unexpected symbol can make a wrong pattern sound plausible.
This helper validates the pattern's syntax. It does not check whether that pattern is assigned to a letter. Keep the alphabet mapping as a separate, reviewable table.
Keep playback predictable
When connecting the schedule to Web Audio, convert milliseconds to seconds once and schedule against the audio context's clock. Start playback from a user action, and decide what a second click should do: stop the current letter, queue the next one, or wait until playback finishes. That decision belongs in the control's behavior and its tests.
For the interface, use a native button with a clear name such as “Play A.” Keep the letter and its written pattern visible, so the sound supplements a reference that works without audio. A print view should retain those two pieces of information and remove controls that cannot be used on paper.
I maintain Txtopia, which has a free A–Z Morse alphabet reference with individual letter audio and a printable chart. The helper above is a standalone teaching example, rather than a claim about the application's internal implementation. It illustrates a boundary that is useful in small browser tools: calculate the behavior as data, then connect it to the browser API.
The timing fixtures make a good first check. Listening, keyboard use, repeated clicks, and the printed page still need separate checks.
Top comments (0)