Warm tip: This article is reproduced from serverfault.com, please click

AudioBuffer

发布于 2020-11-30 16:15:22

I'm working on a DAW in JavaScript and I need to be able to load many (up to 30) long audio files (up to 15 minutes each) and I don't want them to flood the memory. I just want to be able to cut little pieces, process them and mix in ScriptProcessorNode, but I encountered a couple of problems:

  • AudioBuffer will load the entire audio file into memory uncompressed
  • AudioBuffer.getChannelData() will extract the entire buffer - not possible just to extract one little chunk (for example, from sample 1024 to sample 2048)
  • MediaElementAudioSourceNode, which is made to handle large files - it won't let me extract any data from it, neither it will let me load the file dynamically from URL - it only allows me to connect to other nodes in AudioContext, but I don't want to connect it anywhere

Is there a way to load big audio files, for example, in MP3 format, keep them compressed in MP3 format in memory, but extract little chunks of audio data to the AudioBuffer?

Like, for example:

var request = new XMLHttpRequest();
request.open('GET', 'my_15min_audioFile.ogg', true);
request.responseType = 'arraybuffer';
request.onload = function() {
    var longCompressed = new LongAudioNode(request.response);// something similar to fs.open
    var channel=1, fromSample=1024, length=2048;
    var block/**Float32Array*/ = longCompressed.extractBlock(channel, fromSample, length);/// similar to fs.read
    /// do something with this block and then request another from any random index I want
}
request.send();
Questioner
shal
Viewed
0
chrisguttandin 2020-12-01 00:52:05

There is no API in the browser which does exactly what you want. But there might be one in the future. Web Codecs will probably support reading decoded data as a stream.

But there is a workaround which works under certain conditions. You can scan the ArrayBuffer with the encoded data of an MP3 for frame headers and then cut it accordingly. The pieces should still be valid MP3s which can then be decoded with decodeAudioData(). However there are some gotchas. The decoded pieces might not fit together without any post-processing. The phonograph library is built using this technique.