Skip to content

Instantly share code, notes, and snippets.

@halfbyte
Created January 14, 2013 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfbyte/4532252 to your computer and use it in GitHub Desktop.
Save halfbyte/4532252 to your computer and use it in GitHub Desktop.
A very simple audio demo.
<html>
<head>
<title>HTML5 BUG Audio Example</title>
</head>
<style type="text/css">
#play { font-size: 3em;}
</style>
<script type="text/javascript">
(function(){
var ctx;
var pos = 0;
var LOFREQ = 100.0;
var HIFREQ = 10000.0;
var BPM = 120.0;
var BEAT_LEN = 60/BPM;
var note2Freq = function(note) {
return 880.0 * ( Math.pow(2.0,((note - 69.0) / 12)));
};
var play = function(e) {
playNote(ctx.currentTime, note2Freq(52));
};
var playNote = function(time, freq) {
var osc = ctx.createOscillator();
osc.frequency.value = freq;
osc.type = osc.SAWTOOTH;
osc.connect(ctx.destination);
osc.noteOn(time);
osc.noteOff(time + 0.4);
};
var init = function() {
ctx = new webkitAudioContext();
document.getElementById('play').addEventListener('click', play);
};
window.addEventListener('load', init);
})();
</script>
<body>
<h1>Audio Example.</h1>
<button id="play">&gt;</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment