Created
January 14, 2013 18:40
-
-
Save halfbyte/4532252 to your computer and use it in GitHub Desktop.
A very simple audio demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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">></button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment