Skip to content

Instantly share code, notes, and snippets.

@eholk
Last active November 8, 2020 20:46
Show Gist options
  • Save eholk/0115691987090973cefe to your computer and use it in GitHub Desktop.
Save eholk/0115691987090973cefe to your computer and use it in GitHub Desktop.
A Morse Code generator in Java Script
// Copyright 2014–2017, Eric Holk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function MorseNode(ac, rate) {
// ac is an audio context.
this._oscillator = ac.createOscillator();
this._gain = ac.createGain();
this._gain.gain.value = 0;
this._oscillator.frequency.value = 750;
this._oscillator.connect(this._gain);
if(rate == undefined)
rate = 20;
this._dot = 1.2 / rate; // formula from Wikipedia.
this._oscillator.start(0);
}
MorseNode.prototype.connect = function(target) {
return this._gain.connect(target);
}
MorseNode.prototype.MORSE = {
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----"
};
MorseNode.prototype.playChar = function(t, c) {
for(var i = 0; i < c.length; i++) {
switch(c[i]) {
case '.':
this._gain.gain.setValueAtTime(1.0, t);
t += this._dot;
this._gain.gain.setValueAtTime(0.0, t);
break;
case '-':
this._gain.gain.setValueAtTime(1.0, t);
t += 3 * this._dot;
this._gain.gain.setValueAtTime(0.0, t);
break;
}
t += this._dot;
}
return t;
}
MorseNode.prototype.playString = function(t, w) {
w = w.toUpperCase();
for(var i = 0; i < w.length; i++) {
if(w[i] == ' ') {
t += 3 * this._dot; // 3 dots from before, three here, and
// 1 from the ending letter before.
}
else if(this.MORSE[w[i]] != undefined) {
t = this.playChar(t, this.MORSE[w[i]]);
t += 2 * this._dot;
}
}
return t;
}
@masari
Copy link

masari commented Nov 4, 2019

You're missing a "V": "...-" :).

Then I guess we can't play Beethoven's Fifth :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment