jeudi 4 mars 2021

Detecting frequencies of mp3 file in JS in non real time

I want to get the frequencies of an audio file with JS in non-real-time, e.g before the file is played, and store it in an array. I used this code:

    var context = new AudioContext();
    src = context.createMediaElementSource(source);
    analyser = context.createAnalyser();
    var listen = context.createGain();

    src.connect(listen);
    listen.connect(analyser);
    analyser.connect(context.destination);
    analyser.fftSize = 2 ** 12;
    var frequencyBins = analyser.fftSize / 2;

    var bufferLength = analyser.frequencyBinCount;
    console.log(bufferLength);
    dataArray = new Float32Array(bufferLength);
    // dataArray = new Uint8Array(bufferLength);
    var scale = bufferLength/WIDTH;

And then in an animation frame,

dataArraya = new Float32Array(2048);
analyser.getFloatFrequencyData(dataArraya)

This works great in real-time, but I'd like to get the audio data before, how can I do that?




Aucun commentaire:

Enregistrer un commentaire