Created
January 16, 2017 16:23
-
-
Save benfoxall/9c4f8a8d1f861f35b1d90b51f814465c to your computer and use it in GitHub Desktop.
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 chunkToWAV = (function(chunkSize){ | |
var head = atob( | |
'UklGRiQAAABXQVZFZm10IBAAAAABAAEAgD4AAAB9AAACABAAZGF0YQAAAAA=' | |
) | |
var buffer = new ArrayBuffer(head.length + chunkSize) | |
var view = new DataView(buffer) | |
for (var i = 0; i < head.length; i++) { | |
view.setUint8(i, head.charCodeAt(i)) | |
} | |
var dataArray = new Int16Array(buffer) | |
return function toWAV(chunk) { | |
dataArray.set(new Int16Array(chunk), 22) | |
return buffer | |
} | |
})(640) | |
/// usage | |
var AudioContext = window.AudioContext || window.webkitAudioContext; | |
var context = new AudioContext(); | |
var time = 0 | |
ws.onmessage = function(e){ | |
var wav = chunkToWAV(e.data) | |
context.decodeAudioData(wav, function(buffer) { | |
var source = context.createBufferSource() | |
source.buffer = buffer | |
source.start(time += buffer.duration) | |
source.connect(context.destination) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment