Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created January 16, 2017 16:23
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 benfoxall/9c4f8a8d1f861f35b1d90b51f814465c to your computer and use it in GitHub Desktop.
Save benfoxall/9c4f8a8d1f861f35b1d90b51f814465c to your computer and use it in GitHub Desktop.
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