Skip to content

Instantly share code, notes, and snippets.

@kevincennis
Created October 21, 2012 21:03
Show Gist options
  • Save kevincennis/3928503 to your computer and use it in GitHub Desktop.
Save kevincennis/3928503 to your computer and use it in GitHub Desktop.
Instant karaoke track with the Web Audio API
var url = 'http://static1.kevincennis.com/sounds/callmemaybe.mp3'
, audio = new Audio(url)
, context = new webkitAudioContext()
// 512 samples per frame, stereo input, mono output
, processor = context.createJavaScriptNode(512, 2, 1)
, sourceNode
audio.addEventListener('canplaythrough', function(){
sourceNode = context.createMediaElementSource(audio)
sourceNode.connect(processor)
processor.connect(context.destination)
audio.play()
}, false)
processor.onaudioprocess = function(evt){
var inputL = evt.inputBuffer.getChannelData(0)
, inputR = evt.inputBuffer.getChannelData(1)
, output = evt.outputBuffer.getChannelData(0)
, len = inputL.length
, i = 0
for ( ; i < len; i++ )
// flip phase of right channel
// http://www.soundonsound.com/sos/sep04/articles/qa0904-7.htm
output[i] = inputL[i] - inputR[i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment