Created
January 10, 2014 06:01
-
-
Save pramodtech/8347621 to your computer and use it in GitHub Desktop.
Html5 web audio api - Offline rendering
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
window.onload = init; | |
var context; | |
var bufferLoader; | |
var offline; | |
function init() { | |
// Fix up prefixing | |
window.AudioContext = window.AudioContext || window.webkitAudioContext; | |
context = new AudioContext(); | |
bufferLoader = new BufferLoader( | |
context, [ | |
'/sound/sound1.mp3', | |
], | |
finishedLoading | |
); | |
bufferLoader.load(); | |
} | |
function finishedLoading(bufferList) { | |
offline = new webkitOfflineAudioContext(2, 10 * 44100, 44100); | |
var source1 = offline.createBufferSource(); | |
source1.buffer = bufferList[0]; | |
var lowpass = offline.createBiquadFilter(); | |
lowpass.type = 0; | |
lowpass.frequency.value = 0; | |
source1.connect(lowpass); | |
lowpass.connect(offline.destination); | |
source1.start(0); | |
recorder = new Recorder(lowpass); | |
recorder.record(); | |
offline.startRendering(); | |
offline.oncomplete = function(ev) { | |
console.log('oncomplete'); | |
var source = context.createBufferSource(); | |
source.buffer = ev.renderedBuffer; | |
source.connect(context.destination); | |
source.start(0); | |
recorder.stop(); | |
recorder.exportWAV(function(s) { | |
sendWaveToPost(s); | |
}); | |
} | |
} | |
function sendWaveToPost(blob) { | |
var data = new FormData(); | |
data.append("audio", blob, (new Date()).getTime() + ".wav"); | |
var oReq = new XMLHttpRequest(); | |
oReq.open("POST", "/audio/save_file"); | |
oReq.send(data); | |
oReq.onload = function(oEvent) { | |
if (oReq.status == 200) { | |
console.log("Uploaded"); | |
} else { | |
console.log("Error " + oReq.status + " occurred uploading your file."); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment