Search code examples
javascriptaudiobrowserhtml5-audio

Getting mp3 file frequency data with js


Is there a way to get mp3 data (its frequencies) before playing it? I don't have to get it all, mind you - it would be enough if I could get it to give me just the frame data for 5 seconds "into the future" (a frame that hasn't been played yet). I also need this to be client/browser-side, and not server-side. Is there a way to do this?


Solution

  • You don't have to stream the audio data from the WebAudioAPI to the output source. This is the node network they usually use in tutorials:

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

    if you disconnect the context.destination, you should get the data, but muted. For a code like this:

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

    then you can run this code on two same audio files, each with a different timestamp, and save the data from the silent one.