Skip to content

Morse Code JavaScript Library

The engine behind this site, as one file you can use in your own project.

Everything on this site runs on one engine: encoding, decoding, the timing, the sound, the images. That engine is published here as a single file with no dependencies. Include it with a script tag and you have MorseEngine available.

Getting it

<script src="https://www.morse-code.info/lib/morse-engine.js"></script>
<script>
  MorseEngine.encode('SOS');            // "... --- ..."
  MorseEngine.decode('... --- ...');    // "SOS"
</script>

There is also a pinned version at /lib/morse-engine-1.0.0.js. Use that one in anything you will not be watching, so a future change here cannot alter your behavior. The unpinned file always follows the current version.

It works as a plain script, with CommonJS require and with AMD. There is no npm package, because there is nothing to install: it is one file.

What it does

Text and code

MorseEngine.encode('Hello')          // text to Morse
MorseEngine.decode('.... ..')        // Morse to text
MorseEngine.detect('... ---')        // "morse", "text" or "leer"
MorseEngine.translate('SOS')         // { art, text, morse } - picks the direction
MorseEngine.validate('A€B')          // { art, gueltig, unbekannt: ['€'] }

Timing

MorseEngine.timing.unit(20)                       // 0.06 seconds per unit
MorseEngine.timing.schedule('... ---', {wpm: 20}) // [{an, dauer}, ...]
MorseEngine.timing.figures(20, 10)                // Farnsworth: all durations in ms

The schedule is a list of on and off segments in seconds. Everything that makes sound or light works from it, which is why the audio and the flashing can never drift apart.

Sound

const audio = MorseEngine.audio.create();
audio.abspielen(MorseEngine.timing.schedule('... ---', {wpm: 18}), {hz: 600});
audio.stoppen();

// or a WAV file without Web Audio, which also works in Node
const {puffer} = MorseEngine.audio.wav(schedule, {hz: 600});

Images

MorseEngine.graphic.svg('... ---', {stil: 'balken'})  // { svg, breite, hoehe }
MorseEngine.graphic.beads('... --- ...')             // bead counts for jewellery
MorseEngine.graphic.styles                           // the four available styles

Your own alphabet

The international alphabet is built in, but the engine takes any table. This is how you would add American Morse, a fictional code for a game, or a stripped-down set for teaching:

const eigene = MorseEngine.create({
  letters: { A: '.-', B: '-...' },
  digits: {}, punctuation: {}, extended: {}, prosigns: {}
});
eigene.encode('AB');   // ".- -..."

The built-in table is available as MorseEngine.alphabet if you want to extend it rather than replace it.

Things worth knowing

  • Function names are German. The engine was written for this site and the internal names stayed as they were. The outer interface documented above is English; one layer down you will meet abspielen and zeitplan.
  • Two characters share a code. Å and À are both .--.-, Ö and Ø both ---.. Decoding returns the first, and MorseEngine.collisions tells you which.
  • Case is lost. Morse has none. The German ss is handled specially, because uppercasing it would turn it into two letters and destroy its own code.
  • No network, ever. The library makes no requests. What you pass in stays in the process.

Free to use, including commercially. A mention of where it came from is welcome and is not a condition. If you would rather embed a finished translator than build one, the widget does that in three lines.

Frequently asked questions

  • Is there an npm package?

    No. It is a single file with no dependencies, so there is nothing an installer would add. Download it or link to it directly.

  • Is there a REST API instead?

    No, deliberately. Translating Morse code needs no server: the whole thing is a lookup table and some arithmetic. An API would add latency, a rate limit and a dependency on this site staying up, in exchange for nothing.

  • How big is it?

    About 25 kB unminified, including the full character tables. Roughly a third of that is comments.

  • Does it work in Node?

    Yes, with require. Everything except the Web Audio playback works there - the WAV writer in particular is pure arithmetic and runs anywhere.

  • Can I host it myself?

    Yes, and for anything in production you probably should. Copy the file to your own server so your site does not depend on this one.