Morse Code Sound Formula and Web Audio API Frequency Guide

PublishedToolMorse Code Translator with Audio

Contents
  1. 1. Timing Formulas for Morse Code Audio Tones
  2. 2. JavaScript Web Audio API Code Structure
  3. 3. Preventing Click Noise for Clean Audio Synthesis
  4. Wrapping Up
  5. Frequently Asked Questions

Morse code was the communication method of the wireless era — a classic way to express letters and numbers using nothing but dots (.) and dashes (-).

Going beyond converting Morse code to a string of characters, playing it back as actual beep sounds in the browser means building audio frequency oscillation and the playback timeline yourself, with no JavaScript audio library. This post covers the standard timing formulas for synthesizing Morse code audio and how to control them with the Web Audio API.

1. Timing Formulas for Morse Code Audio Tones

Morse code audio timing is built on one base unit: the length of a single dot, defined as 1 Unit. Dashes and every gap are calculated as multiples of that base time.

  • Dot sound length: 1 Unit
  • Dash sound length: 3 Units (exactly 3x the dot sound)
  • Silent gap between dots and dashes within a character: 1 Unit
  • Silent gap between characters: 3 Units
  • Space between words: 7 Units (silence held for 7x the dot length)

Adjust the playback speed in WPM (words per minute), and the real length of 1 Unit in ms shrinks or grows in inverse proportion. The faster the WPM, the shorter the unit time. The slower the WPM, the longer it gets.

How the Web Audio API Generates Digital Frequencies

To make a beep in the browser without loading any audio file, you generate a pure sine wave with an Oscillator node. During the "on" portion of the sound, it vibrates at the frequency you specify. During the "off" portion, the volume drops to 0. Frequencies between roughly 400Hz and 1000Hz sound relatively clear to the human ear, and whichever value you pick in that range, the rhythm of dots and dashes itself doesn't change.

2. JavaScript Web Audio API Code Structure

Here's the core node connection flow of the Web Audio API and the beep playback logic.

// Start the audio context
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();

function playBeep(durationMs, frequency = 600) {
  // Create oscillator and gain (volume control) nodes
  const osc = audioCtx.createOscillator();
  const gainNode = audioCtx.createGain();

  osc.connect(gainNode);
  gainNode.connect(audioCtx.destination); // Connect output to the speakers

  // Set the frequency waveform (sine)
  osc.type = "sine";
  osc.frequency.setValueAtTime(frequency, audioCtx.currentTime);

  // Volume envelope (fade-in/out) to prevent clicking noise
  const now = audioCtx.currentTime;
  gainNode.gain.setValueAtTime(0, now);
  gainNode.gain.linearRampToValueAtTime(0.5, now + 0.005); // Fast ramp on
  gainNode.gain.setValueAtTime(0.5, now + (durationMs / 1000) - 0.005);
  gainNode.gain.linearRampToValueAtTime(0, now + (durationMs / 1000)); // Ramp off

  osc.start(now);
  osc.stop(now + (durationMs / 1000));
}

Hear it without writing any code. Type in some text and you can instantly check the sound produced by the formulas and code above.

Morse Code Translator with Audio

Type the text or English copy you want, translate it into dots and dashes automatically, and listen to the converted sound as standard-frequency beeps in real time.

3. Preventing Click Noise for Clean Audio Synthesis

  1. Gain Smoothing: If you start an audio signal instantly and cut it to 0ms, the speaker output adds a sharp "pop" — a click noise. A gain smoothing technique, like the code above, raises the volume linearly over a very short time (about 5ms) and then lowers it smoothly to stop this.
  2. Audio Context State Management: Under current browser policies, autoplay audio (AudioContext) is blocked (Suspended) until the user takes a direct action, such as clicking on the screen. You need to activate the context in the playback button's click event before the sound plays properly.

Wrapping Up

Morse code audio comes down to five time values: dot (1 Unit), dash (3 Units), gap within a character (1 Unit), gap between characters (3 Units), and gap between words (7 Units). Add a sine wave from an oscillator and a volume envelope (gain smoothing) to prevent click noise, and you can synthesize beeps with the same structure as real wireless signals, using nothing but the browser.

Morse Code Translator with Audio

Type the text or English copy you want, translate it into dots and dashes automatically, and listen to the converted sound as standard-frequency beeps in real time.

Frequently Asked Questions

I pressed play, but there's no sound

By browser policy, audio playback is blocked until the user clicks a button directly. Check that the sound plays when you press the play button rather than right as the page opens, and also check the mute status and volume on your device and browser.

Does changing the playback speed (WPM) change the order of the symbols?

No. WPM only changes the length (speed) of 1 Unit. The arrangement and order of dots and dashes stay the same. For the same text, the resulting Morse code itself is identical no matter what WPM you use.

Does changing the frequency (Hz) change the symbols?

No. Frequency only changes the pitch (tone) of the beep. Whichever frequency you choose, the length ratio and spacing of dots and dashes follow the timing formulas above exactly.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. Morse Code Decoding: Alphabet Chart and How to TranslateSee the one-to-one mapping of letters, numbers and symbols to dots and dashes, plus the rules for separating letters and words when you encode or decode.
  2. Initials from English Names: Rules, Order, and CapitalizationHow to pull initials from a full English name: which word is the family name, how to set capitals, and when to use periods. Includes middle name cases.
  3. Binary, Octal, Decimal, Hex Conversion: Formulas and CodeLearn how binary, octal, decimal, and hexadecimal convert into each other, plus JavaScript toString and parseInt examples and why floats drift.
  4. Open Graph Meta Tags: Setup & SNS Link Preview GuideLearn the Open Graph and Twitter Card tags you need so links to your site show a clean thumbnail, title, and description in chat apps, Facebook, and X.
  5. NATO Phonetic Alphabet Chart: A=Alpha Through NumbersFull NATO phonetic alphabet mapping from A=Alpha to Z=Zulu, plus how to read numbers 0-9. Use the chart or convert any spelling in one step.
  6. TCP/UDP port numbers: common ports and security checksWhat the well-known and registered port ranges are, and what common TCP/UDP ports like 80, 443, 22, 3306, and 8080 do and how to secure them.

Related tools