user2539621
user2539621

Reputation: 361

Get frequency data from audio file immediately using the Web Audio API?

I'm trying to get the frequency data of an audio file without having to play through the entire actual audio file. Right now I'm doing the following:

var audio = new Audio();
audio.src = 'testAudio.mp3';
audio.controls = true;
audio.autoplay = true;
document.body.appendChild(audio);

var context = new AudioContext();
var analyser = context.createAnalyser();

 var source = context.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(context.destination);
var frequencyData = new Uint8Array(analyser.frequencyBinCount);
function renderFrame() {
   requestAnimationFrame(renderFrame);

   analyser.getByteFrequencyData(frequencyData);
}
renderFrame();

This works fine, but I need to wait for the entire audio file to play. Is there a way I can get this data immediately?

Upvotes: 1

Views: 2402

Answers (1)

cwilso
cwilso

Reputation: 13928

Nope. You'll have to do your own FFT, via a library like DSP.js.

Upvotes: 3

Related Questions