Created
August 30, 2011 09:22
-
-
Save joa/1180524 to your computer and use it in GitHub Desktop.
Web Audio API Playback
This file contains hidden or 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
// #1 Launch Chrome | |
// #1.1 If you are on the Beta channel you are fine | |
// #1.2 If you are not on the Beta channel enable Web Audio API in about:flags | |
// #2 Ctrl+Shift+C or Apple+Shift+J | |
// #3 Open Console | |
// #4 Paste | |
var bufferSize = 2048 | |
var ctx = new webkitAudioContext() | |
var js = ctx.createJavaScriptNode(bufferSize, 0, 1) | |
var phase = 0.0 | |
var phaseInc = 440.0/ctx.sampleRate | |
js.connect(ctx.destination, 0) | |
js.onaudioprocess = function(e) { | |
var l = e.outputBuffer.getChannelData(0) | |
var r = e.outputBuffer.getChannelData(1) | |
var n = e.outputBuffer.length | |
for(var i = 0; i < n; i++) { | |
phase += phaseInc | |
if(phase > 1.0) { --phase } | |
var amp = Math.sin(phase * Math.PI * 2.0) | |
l[i] = r[i] = amp | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment