Last active
June 30, 2024 09:21
-
-
Save revolunet/e620e2c532b7144c62768a36b8b96da2 to your computer and use it in GitHub Desktop.
Web Audio streaming with fetch 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
// | |
// loads remote file using fetch() streams and "pipe" it to webaudio API | |
// remote file must have CORS enabled if on another domain | |
// | |
// mostly from http://stackoverflow.com/questions/20475982/choppy-inaudible-playback-with-chunked-audio-through-web-audio-api | |
// | |
function play(url) { | |
var context = new (window.AudioContext || window.webkitAudioContext)(); | |
var audioStack = []; | |
var nextTime = 0; | |
fetch(url).then(function(response) { | |
var reader = response.body.getReader(); | |
function read() { | |
return reader.read().then(({ value, done })=> { | |
context.decodeAudioData(value.buffer, function(buffer) { | |
audioStack.push(buffer); | |
if (audioStack.length) { | |
scheduleBuffers(); | |
} | |
}, function(err) { | |
console.log("err(decodeAudioData): "+err); | |
}); | |
if (done) { | |
console.log('done'); | |
return; | |
} | |
read() | |
}); | |
} | |
read(); | |
}) | |
function scheduleBuffers() { | |
while ( audioStack.length) { | |
var buffer = audioStack.shift(); | |
var source = context.createBufferSource(); | |
source.buffer = buffer; | |
source.connect(context.destination); | |
if (nextTime == 0) | |
nextTime = context.currentTime + 0.01; /// add 50ms latency to work well across systems - tune this if you like | |
source.start(nextTime); | |
nextTime += source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played | |
}; | |
} | |
} | |
var url = 'http://www.metadecks.org/software/sweep/audio/demos/beats1.wav?adazdaz' | |
play(url); |
read()
can be improved:
function read() {
return reader.read().then(({ value, done })=> {
if (done) {
console.log('done');
return;
}else{
console.log(value,done);
context.decodeAudioData(value.buffer, function(buffer) {
audioStack.push(buffer);
if (audioStack.length) {
scheduleBuffers();
}
}, function(err) {
console.log("err(decodeAudioData): "+err);
});
}
read()
});
}
Otherwise I get a undefined error
It works on Chrome but not Firefox. The error on firefox: TypeError: response.body is undefined.
Also, how do we pause this?
I'm getting
Uncaught (in promise) DOMException: Unable to decode audio data
using your code in Chrome console on http://www.metadecks.org/
were you able to solve this?
any update i am also getting the same issue in firefox ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried your example,I also found that "decodeAudioData" cannot be called without audio header,So your example is problematic.