Demonstrations and samples here
import morseplayer.js (in head or body)
<script src="morseplayer.js"></script>
Instantiate the player :
var player = new CWPlayer({wpm:25});
// can't call play() for the first time directly from onload or arbitrary code : this must be done on a user gesture (https://developer.chrome.com/blog/autoplay/#web_audio)
//player.play('Hello world !');
⚠ : as stated above, the first call to a playing method (such as play or setting AutoPlay attribute to true) must be initiated from a user gesture on the page (explanation). Subsequent calls can be done without condition
Then try it :
<a href="#" onclick="player.play('Hello world !')">click me!</a>
player.Text = "StrÀngé ïnpùt tèxt";
console.log(player.Text);
// 'STRANGE INPUT TEXT'
Prosigns can be entered between { and } and will be played as prosigns :
player.Text = "VE3YYY de F8XXX {AS}";
Text changesThe CWPlayer constructor accepts options as an Object in which keys can be :
Note: When WPM is lower than Effective WPM, the latest value is used for both
play method start the morse playing of the text. The text can be passed as parameter otherwise Text property will be used. Text is cleaned. The method returns a promise wich is fulfilled at the first pause or at the end of the playing.
await player.play('test');
await player.play('this text is read after the first has finished');
renderToFile method render the morse representation of the text and save it to a .wav file. The text can be passed as parameter otherwise Text property will be used. Text is cleaned. The method returns a promise wich is fulfilled at the end of the conversion to .wav file.
await player.renderToFile();
stops the playing
stops the playing and reset the current playing time
play a ‘boop’ sound (used for wrong entry in simple mode)
Register an event listener
player.addEventListener('play', () => {
console.log('CW Time !');
});
player.addEventListener('parameterchanged', (param) => {
console.log(`"${param}" changed to ${player[param]}`);
});
parameterchanged : one of the properties of the player has changed. The parameter’s name is passed as argument with the event.indexchanged : the current index in the input text changed. Warning the index reference the Text property, which is cleaned (and thus can be different from original provided text)play : playing has started or been resumed from pausepause : playing has been temporarily stoppedstop : playing has been definitely stoppedrecording : recording to wave has started. stop is triggered once finishedRegister an event listener, triggered only once
player.on('play', () => {
// this message will appear only once even if play is called multiple times
console.log('CW Time !');
});
Remove the event listener. If no func is provided, remove all the listeners. If only func is provided, remove this listener from all events.
Translate to Morse the specified text :
let translated = CWPlayer.translate("Bonjour");
console.log(translated);
// '-... --- -. .--- --- ..- .-.'
// Prosigns
translated = CWPlayer.translate('{AS}');
console.log(translated);
// '.-...'
Used to clean the provided text.
let cleaned = CWPlayer.cleanText("StrÀngé ïnpùt tèxt");
console.log(cleaned);
// 'STRANGE INPUT TEXT'