Ural
Ural

Reputation: 385

PhantomJS analyze FFT data of mp3 file and save it

I am learning/playing with Web Audio API and it is awesome. I have some code, that analyses FFT of audio stream and do some calculations in realtime. It runs in browser.

But now I need to do the same thing, but process the whole audiofile and get array of data, instead playing it and analyze in realtime, and best case do it in Phantomjs. If it is not possible, browser is ok also..

Sample code:

var audioElement = document.getElementById("player");
var audioContext = new AudioContext();
var source = audioContext.createMediaElementSource(audioElement);

analyserNode = audioContext.createAnalyser();
analyserNode.fftSize = 2048;

source.connect(analyserNode);
source.connect(audioContext.destination);

analyserNode.connect(audioContext.destination);

and looping function:

var freqByteData = new Uint8Array(analyserNode.frequencyBinCount);
analyserNode.getByteFrequencyData(freqByteData); 
//do processing FFT data

Now I need load an mp3 file, and process a looping function to it to get some array of result data. So not wait while it play, but process it immediately.

The problem is, I never tried phantomjs. I need exact results as browser does, also I am using analyserNode.smoothingTimeConstant in my calculations. The requirement is get data 30 times per second.

If it is possible, how to rewrite this code and run it like ./phantomapp file.mp3 which saves data to some txt file or stdout?

If it is not possible with phantomjs, how to adopt this code to run in browser?

Thanks

Upvotes: 2

Views: 864

Answers (2)

Raymond Toy
Raymond Toy

Reputation: 6048

Don't know anything about phantomjs, but if you can load the entire file into memory using decodeAudioData, you can use an OfflineAudioContext to process the file. This will run as fast as possible. Use OfflineAudioContext.suspend(time) to stop processing so you can call AnalyserNode.getByteFrequencyData(freqByteData) at the right times to get your frequency data. Then schedule a suspend at some appropriate time, and resume() from this suspension.

Upvotes: 1

Johan Karlsson
Johan Karlsson

Reputation: 494

There is a project on Github: https://github.com/sebpiq/node-web-audio-api

Which claims "Node.js implementation of Web audio API".

Warning: "And this is not even alpha. Use this library only if you're the adventurous kind."

Here is a related SO question: Why Web Audio API isn't supported in nodejs?

Upvotes: 2

Related Questions