Skip to content

Instantly share code, notes, and snippets.

@joa
Created August 30, 2011 09:22
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 joa/1180524 to your computer and use it in GitHub Desktop.
Save joa/1180524 to your computer and use it in GitHub Desktop.
Web Audio API Playback
// #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