Tuesday 11 April 2017

NodeJS 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 do it in Node.JS

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 node.js, and don't know even if it is possible to adopt this code to it. I found fft module, but it seems not be the same as browser version. 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 ./nodeapp file.mp3 which saves data to some txt file or stdout?

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

Thanks



via Ural

No comments:

Post a Comment