Calculating The Average Amplitude of an Audio File Using FFT in Javascript

岁酱吖の 提交于 2019-12-13 07:18:44

问题


I am currently involved in a project in which I want to find the average amplitude for given audio data in any given AAC file. I am currently reading the file as an array buffer and passing into an Uint8Array.

var dataArray = new Uint8Array(buffer)

Then I set up two arrays, one real(containing the audio data) and one imaginary(containing all zeros), and pass them into an FFT. The audio data is then placed into a new array such that the numbers within the array are no longer treated as unsigned 8-bit integers.

var realArray = [audio data here]
var imagArray = [0,0,0,0,0,0,...]
transform(realArray, imagArray)

I then go through the arrays, looping from 0 to N/2, where N is the size of the initial buffer containing the raw audio data, and calculate the magnitude of each frequency bin. Finally, I divide the sum of these magnitudes by N/2.

The problem is that on some occasions, for audio played at a lower intensity, I get a high value compared to the value given by audio played at a higher intensity. Is my approach correct in relation to achieving my goal or is there a better way of going about it? Thanks.

Note: For those interested the FFT being used can be found here in several languages. FFT I am passing the middle 2^20 bytes of the audio file into the FFT then doing my calculations.


回答1:


You really don't need to use an FFT for this - Parseval's theorem essentially means that energy in the time domain is equal to energy in the frequency domain, so the FFT part is redundant - you can just calculate amplitude in the time domain. Typically this is done by calculating the RMS value of the signal over a chosen time window (the length of this time window depends on what you are trying to achieve).



来源:https://stackoverflow.com/questions/34937030/calculating-the-average-amplitude-of-an-audio-file-using-fft-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!