Morse Code

Some History about Morse Code

Downton Abbey fans will undoubtedly remember the various telegraphs that are exchanged during the show (click here for the photo source)

A History of Web Audio

Web Audio Through History (click here for the source)

Introducing the Web Audio API

(click here for the photo source)

Morse Sound

this.audioContext = new AudioContext();
this.oscillator = this.audioContext.createOscillator();
this.gain = this.audioContext.createGain();
this.gain.gain.value = 0;
this.oscillator.frequency.value = 750;
this.oscillator.connect(this.gain);
this.gain.connect(this.audioContext.destination);
this.dot = 1.2 / this.rate;
this.oscillator.start(0);
generateMorse(time: any, phrase: string) {
phrase = phrase.toUpperCase();
this.morseDisplay = [];
for (const p of phrase) {
if (p === ' ') {
time += 3 * this.dot;
} else if (this.MORSE[p] !== undefined) {
time = this.createSound(time, this.MORSE[p]);
time += 2 * this.dot;
}

const morseOuput = new MorseOutput();
morseOuput.morseText = p;
morseOuput.morseValue = this.MORSE[p];
this.morseDisplay.push(morseOuput);
}

return time;
}
createSound(time: any, char: string) {
for (const c of char) {
switch (c) {
case '.':
this.gain.gain.setValueAtTime(1.0, time);
time += this.dot;
this.gain.gain.setValueAtTime(0.0, time);
break;
case '-':
this.gain.gain.setValueAtTime(1.0, time);
time += 3 * this.dot;
this.gain.gain.setValueAtTime(0.0, time);
break;
}
time += this.dot;
}

return time;
}

Morse Light

async transmit() {
// time = 1200 / words per minute
// 20 words per minute
// follows a 3 to 1 ratio
// 60 milliseconds for one dot
// 180 milliseconds for a dash
// multiplied by factor of 4 to slow it down here
const dot = 60 * 4;
const dash = 180 * 4;

this.showMorse = '';
const messageUpper = this.message.toUpperCase();
for (const char of messageUpper) {
this.showMorse = this.showMorse + '(' + char + ') ';
const morseValue = this.morseTranslation[char];
for (const morse of morseValue) {
this.showMorse = this.showMorse + ' ' + morse;
if (morse === '.') {
// dot
await this.flashlight('yellow', dot);
// show white light to show when flash is finished
await this.flashlight('white', 60);
} else {
// dash at 3 X 60 or 180
await this.flashlight('yellow', dash);
// show white light to show when flash is finished
await this.flashlight('white', 60);
}
}
}
}
flashlight(color: String, time: any): Promise<any> {
return new Promise(resolve => {
setTimeout(function() {
// this.drawLight(color);
const c: any = document.getElementById('flashlight');
const ctx = c.getContext('2d');
ctx.beginPath();
ctx.arc(c.width / 2, c.height - 50, 50, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.stroke();
resolve(true);
}, time);
});
}

Wrapping Up

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store