<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Playing Morse Code with Web Audio API</title> <!-- https://c7sky.com/playing-morse-code-with-web-audio-api.html --> <style type="text/css"> </style> </head> <body> <form id="demo"> <input type="text" pattern="[.\- ]+" name="code" value="-.-. --... ... -.- -.--" > <button>Play</button> </form> </body> <!-- <script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script> --> <script type="text/javascript"> var AudioContext = window.AudioContext || window.webkitAudioContext; var ctx = new AudioContext(); var dot = 1.2 / 15; document.getElementById("demo").onsubmit = function() { var t = ctx.currentTime; var oscillator = ctx.createOscillator(); oscillator.type = "sine"; oscillator.frequency.value = 600; var gainNode = ctx.createGain(); gainNode.gain.setValueAtTime(0, t); this.code.value.split("").forEach(function(letter) { switch(letter) { case ".": gainNode.gain.setValueAtTime(1, t); t += dot; gainNode.gain.setValueAtTime(0, t); t += dot; break; case "-": gainNode.gain.setValueAtTime(1, t); t += 3 * dot; gainNode.gain.setValueAtTime(0, t); t += dot; break; case " ": t += 7 * dot; break; } }); oscillator.connect(gainNode); gainNode.connect(ctx.destination); oscillator.start(); return false; } </script> </html> 提示:你可以先修改部分代码再运行。 转载请注明:有爱前端 » 播放摩斯密码 喜欢 (0)or分享 (0)