jscwlib - JavaScript Morse Code Library



Status

2020-July-6
Release v0.2.0. Added Japanese Wabun code (tnx JE1TRV). Allow changing frequency while playing. Implement command |W for extra word spacing. Some small bug fixes.
2020-May-15
Release v0.1.0.
2020-April-24
Website created. No release yet - very preliminary but working code in git.

Introduction

jscwlib is a JavaScript library that generates Morse code (sound and optional graphical output) in the browser. It can easily be embedded in websites. It is developed for LCWO, but since it may be very useful for other purposes, it's available as a separate library.

jscwlib works with current versions of Firefox, Chrome, Edge, Opera and Safari, using the Web Audio API. For browsers that do not support the Web Audio API (like the Internet Explorer), there's a fall-back to the Embed Audio element.

Example usage

Code
Live Result
<!DOCTYPE html>
<script src="https://fkurz.net/ham/jscwlib/src/jscwlib.js"></script>

<div id="player"></div>    

<script>
  var m = new jscw({"wpm": 25});
  m.setText("hello cześć привет <do>おはよう<sn>");
  m.renderPlayer('player', m);
</script>

Note that browsers typically only play sound if it was created by a keyboard/mouse/touch event, therefore the need to press a button to start it.

Live examples: Minimum example (above), Pileup, Oscilloscope, visualisation of CW timing .

Constructor

new jscw(params)
params is an optional object containing initial settings corresponding to the variables that you can later also set by the methods listed below.
Example: var m = new jscw({"wpm": 40, "freq": 700, "text": "Hello"});

Methods

init()
Initialize the player (optional - will be called internally if not done by the user).
setWpm(x)
Sets the character speed to x words per minute.
setEff(x)
Sets the effective speed to x words per minute. If it's equal, higher than the character speed or zero, this parameter is ignored. Otherwise the spaces between the characters are stretched to achieve the effective speed with a higher character speed.
setReal(bool)
When true, generate code with real character speed, not PARIS timing.
setEws(n)
Add extra word spacing of n × the normal word space.
setFreq(x)
Set the tone frequency to x Hertz.
setFilter(f)
Set the cutoff-frequency of the low-pass filter that shapes the generated tone to f Hz.
setQ(q)
Set low-pass filter Q to q.
setText(text)
Set text to be played. Does not start playing yet. Text may contain latin or cyrillic characters. Additional character sets can easily be implemented.
setVolume(v)
Sets the volume to v (0 .. 100).
play(text)
Plays text (if not set: the last text set by play or setText in Morse code, with the parameters currently set. The text may contain commands to change parameters on the fly: Furthermore, anything enclosed in angle brackets will be sent without letter spaces (like a prosign), for example <SK> will generate ...-.-
pause()
Pauses or resumes the current text.
stop()
Stops the current text.
setStartDelay(s)
Delays the actual text by s seconds, after play() is invoked.
setPrefix(p)
Sets the transmission prefix to p. Each transmission is prefixed with this string (e.g. VVV = ), but it does not influence the calculation of speed.
setSuffix(s)
Sets the transmission suffix to s. Each transmission is suffixed refixed with this string (e.g. +), but it does not influence the calculation of speed.
enablePS(b)
When set to true, enable the transmission of the prefix and suffix.
getLength()
Get the total play time of the current text in seconds.
getRemaining()
Get the remaining play time of the current text in seconds.
getTime()
Get the current time within the played text in seconds.
draw(c)
Draws the last generated Morse code as a timing diagram on a HTML5 canvas (c).
renderPlayer(div, obj)
Renders a player UI (with progress bar and buttons for Stop, Play/Pause and the possibility to change CW parameters) for player obj into a div.
oscilloscope(c)
Draws the currently generated waveform on canvas c.

Events

onPlay
A user defined function that will be invoked each time the player is started. E.g.:
m.onPlay = function () { alert("Player started"); }

Download / Embedding

The latest version of the library can be found in the git repository: https://git.fkurz.net/dj1yfk/jscwlib/. It is published under the very liberal MIT license.

You may directly embed the library into your own site by hot-linking to https://fkurz.net/ham/jscwlib/releases/jscwlib-0.2.0.js


Author

jscwlib was written by Fabian Kurz, DJ1YFK <fabian@fkurz.net>.



Last modified: Monday, 06-Jul-2020 16:41:36 CEST