Is there a way to use the Web Audio API to sample audio faster than real-time?
I'm playing around with the Web Audio API & trying to find a way to import an mp3 (so therefore this is only in Chrome), and generate a waveform of it on a canvas. I can do this in real-time, but my goal is to do this faster than real-time.
All the examples I've been able to find involve reading the frequency data from an analyser object, in a function attached to the onaudioprocess event:
processor = context.createJavascriptNode(2048,1,1);
processor.onaudioprocess = processAudio;
...
function processAudio{
var freqByteData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(freqByteData);
//calculate magnitude & render to canvas
}
It appears though, that analyser.frequencyBinCount
is only populated when the sound is playing (something about the buffer being filled).
What I want is to be able to manually/programmatically step through the file as fast as possible, to generate the canvas image.
What I've got so far is this:
$("#files").on('change',function(e){
var FileList = e.target.files,
Reader = new FileReader();
var File = FileList[0];
Reader.onload = (function(theFile){
return function(e){
context.decodeAudioData(e.target.result,function(buffer){
source.buffer = buffer;
source.connect(analyser);
analyser.connect(jsNode);
var freqData = new Uint8Array(buffer.getChannelData(0));
console.dir(analyser);
console.dir(jsNode);
jsNode.connect(context.destination);
//source.noteOn(0);
});
};
})(File);
Reader.readAsArrayBuffer(File);
});
But getChannelData() always returns an empty typed array.
Any insight is appreciated - even if it turns out it can't be done. I think I'm the only one the Internet not wanting to do stuff in real-time.
Thanks.
There is a really amazing 'offline' mode of the Web Audio API that allows you to pre-process an entire file through an audio context and then do something with the result:
var context = new webkitOfflineAudioContext();
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.noteOn(0);
context.oncomplete = function(e) {
var audioBuffer = e.renderedBuffer;
};
context.startRendering();
So the setup looks exactly the same as the real-time processing mode, except you set up the oncomplete
callback and the call to startRendering()
. What you get back in e.redneredBuffer
is an AudioBuffer
.
I got this to work using OfflineAudioContext using the following code. The complete example here shows how to use it to compute the FFT magnitudes for a linear chirp. Once you have the concept of hooking the nodes together, you can do just about anything with it offline.
function fsin(freq, phase, t) {
return Math.sin(2 * Math.PI * freq * t + phase)
}
function linearChirp(startFreq, endFreq, duration, sampleRate) {
if (duration === undefined) {
duration = 1; // seconds
}
if (sampleRate === undefined) {
sampleRate = 44100; // per second
}
var numSamples = Math.floor(duration * sampleRate);
var chirp = new Array(numSamples);
var df = (endFreq - startFreq) / numSamples;
for (var i = 0; i < numSamples; i++) {
chirp[i] = fsin(startFreq + df * i, 0, i / sampleRate);
}
return chirp;
}
function AnalyzeWithFFT() {
var numChannels = 1; // mono
var duration = 1; // seconds
var sampleRate = 44100; // Any value in [22050, 96000] is allowed
var chirp = linearChirp(10000, 20000, duration, sampleRate);
var numSamples = chirp.length;
// Now we create the offline context to render this with.
var ctx = new OfflineAudioContext(numChannels, numSamples, sampleRate);
// Our example wires up an analyzer node in between source and destination.
// You may or may not want to do that, but if you can follow how things are
// connected, it will at least give you an idea of what is possible.
//
// This is what computes the spectrum (FFT) information for us.
var analyser = ctx.createAnalyser();
// There are abundant examples of how to get audio from a URL or the
// microphone. This one shows you how to create it programmatically (we'll
// use the chirp array above).
var source = ctx.createBufferSource();
var chirpBuffer = ctx.createBuffer(numChannels, numSamples, sampleRate);
var data = chirpBuffer.getChannelData(0); // first and only channel
for (var i = 0; i < numSamples; i++) {
data[i] = 128 + Math.floor(chirp[i] * 127); // quantize to [0,256)
}
source.buffer = chirpBuffer;
// Now we wire things up: source (data) -> analyser -> offline destination.
source.connect(analyser);
analyser.connect(ctx.destination);
// When the audio buffer has been processed, this will be called.
ctx.oncomplete = function(event) {
console.log("audio processed");
// To get the spectrum data (e.g., if you want to plot it), you use this.
var frequencyBins = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(frequencyBins);
console.log(frequencyBins);
// You can also get the result of any filtering or any other stage here:
console.log(event.renderedBuffer);
};
// Everything is now wired up - start the source so that it produces a
// signal, and tell the context to start rendering.
//
// oncomplete above will be called when it is done.
source.start();
ctx.startRendering();
}