Created
October 21, 2012 21:03
-
-
Save kevincennis/3928503 to your computer and use it in GitHub Desktop.
Instant karaoke track with the Web Audio API
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
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