title: Calculating The Average Amplitude of an Audio File Using FFT in Javascript
body: <p>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. </p>
  
  <pre><code>var dataArray = new Uint8Array(buffer)
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>var realArray = [audio data here]
  var imagArray = [0,0,0,0,0,0,...]
  transform(realArray, imagArray)
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Note: For those interested the FFT being used can be found here in several languages. <a href="http://www.nayuki.io/page/free-small-fft-in-multiple-languages" rel="nofollow">FFT</a>
  I am passing the middle 2^20 bytes of the audio file into the FFT then doing my calculations. </p>