Skip to content

Instantly share code, notes, and snippets.

@siwells
Created February 15, 2018 19:10
Show Gist options
  • Save siwells/d0e8c60317cdd81286a1e80438cefc12 to your computer and use it in GitHub Desktop.
Save siwells/d0e8c60317cdd81286a1e80438cefc12 to your computer and use it in GitHub Desktop.
Morse Code Audio Example using Web Audio API
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
</head>
<body>
<button onclick="morseSounds();">Sound</button>
<script>
var context = null;
function playSound(start, stop){
osc = context.createOscillator();
osc.connect(context.destination);
console.log("starting");
osc.start(start);
console.log("stopping");
osc.stop(stop);
}
function morseSounds(){
var text = '-.-.';
var dot = 0.1;
var dash = 0.3;
var sep = 0.5;
var AudioContext = window.AudioContext || window.webkitAudioContext;
if (context == null){
context = new AudioContext();
}
var osc;
now = context.currentTime;
for(var i=0; i<text.length; i++){
now = now+sep;
if (text[i] === '-'){
console.log(text[i]);
playSound(now, now + dash);
}
else{
console.log(text[i]);
playSound(now, now + dot);
}
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment