Building a browser-based Morse audio engine looks trivial until you actually listen to a raw OscillatorNode toggled between gain = 0 and gain = 1. Instant amplitude steps introduce broadband spectral splatter—heard as harsh key clicks on every dot and dash—and naive fixed delays drift out of spec at higher words-per-minute (WPM) speeds.
Here is how to implement spec-accurate timing and clickless keying using pure Web Audio API primitives, plus a lightweight Goertzel tone detector for decoding incoming audio in the browser.
1. ITU-R M.1677-1 Unit Timing and Farnsworth Spacing
The international standard ITU-R M.1677-1 defines Morse timing around a single base unit (one dot, or dit) derived from the standard word PARIS (50 dot units):
-
Dot (dit):
1 unit -
Dash (dah):
3 units -
Intra-character gap (between elements of the same letter):
1 unit -
Inter-character gap (between letters):
3 units -
Inter-word gap:
7 units
At a given character speed WPM, one base unit in milliseconds is:
const unitMs = 1200 / wpm; // e.g., 60 ms at 20 WPM
For learners practicing with Farnsworth timing, characters are still sent at a crisp target speed (say c = 20 WPM) so the ear learns rhythmic patterns rather than counting dots, while the gaps between characters and words are stretched to match a slower overall speed s < c. The extra delay budget t_a per 50-unit PARIS cycle is:
function computeFarnsworthTimings(charWpm, overallWpm) {
const u = 1.2 / charWpm; // base unit in seconds
if (overallWpm >= charWpm) {
return { dot: u, dash: 3 * u, intra: u, charGap: 3 * u, wordGap: 7 * u };
}
const ta = (60 * charWpm - 37.2 * overallWpm) / (charWpm * overallWpm);
return {
dot: u,
dash: 3 * u,
intra: u,
charGap: (3 * ta) / 19,
wordGap: (7 * ta) / 19,
};
}
2. Eliminating Key Clicks with a 5 ms Raised-Cosine Envelope
Switching gain.setValueAtTime(1, t) creates a step discontinuity. Instead, shape both the attack and release edges over 5 ms using a smooth raised-cosine curve via setValueCurveAtTime (or a short exponentialRampToValueAtTime fallback):
function makeRaisedCosine(samples, rising = true) {
const curve = new Float32Array(samples);
for (let i = 0; i < samples; i++) {
const phase = i / (samples - 1);
const v = 0.5 * (1 - Math.cos(Math.PI * phase));
curve[i] = rising ? v : 1 - v;
}
return curve;
}
const RISE_CURVE = makeRaisedCosine(64, true);
const FALL_CURVE = makeRaisedCosine(64, false);
function scheduleTone(gainParam, startSec, durationSec, rampSec = 0.005) {
const safeRamp = Math.min(rampSec, durationSec / 3);
gainParam.setValueCurveAtTime(RISE_CURVE, startSec, safeRamp);
gainParam.setValueCurveAtTime(
FALL_CURVE,
startSec + durationSec - safeRamp,
safeRamp
);
}
Scheduling every envelope directly on AudioContext.currentTime avoids setTimeout jitter (which regularly drifts by 4–15 ms in background tabs) and keeps 25+ WPM keying rock-solid. You can test how this sounds in practice on morse-code.org, which runs the entire synthesis, WAV export, and timing pipeline locally in the browser.
3. Detecting Narrowband Audio Beeps with the Goertzel Algorithm
When decoding Morse audio from a microphone or uploaded WAV file, running a full FFT every 10 ms wastes CPU because you only care about the energy around a single carrier frequency (commonly 550–800 Hz). A single-bin Goertzel filter computes the magnitude at target frequency f0 in O(N) time with two multiplications per sample:
function goertzelPower(samples, sampleRate, targetHz) {
const k = Math.round((samples.length * targetHz) / sampleRate);
const omega = (2 * Math.PI * k) / samples.length;
const coeff = 2 * Math.cos(omega);
let s1 = 0, s2 = 0;
for (let i = 0; i < samples.length; i++) {
const s0 = samples[i] + coeff * s1 - s2;
s2 = s1;
s1 = s0;
}
return s1 * s1 + s2 * s2 - coeff * s1 * s2;
}
To turn raw frame power into dots and dashes, apply a dual-threshold hysteresis gate (to reject room noise) and track an exponential moving average of short pulse durations so the decoder adapts automatically if the sender speeds up or slows down. A live browser implementation of this client-side decoding flow is available at https://morse-code.org/morse-code-audio-decoder.
Summary Checklist
- Schedule all tone transitions on
AudioContext.currentTime, neversetInterval. - Keep rise/fall ramps between
4 msand6 msto suppress key clicks without blurring high-WPM dots. - Use a single-bin Goertzel filter plus adaptive unit-length tracking for low-latency client-side decoding.
Top comments (0)