在Javascript中使用FFT计算音频文件的平均幅度 [英] Calculating The Average Amplitude of an Audio File Using FFT in Javascript

查看:816
本文介绍了在Javascript中使用FFT计算音频文件的平均幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在参与一个项目,我想在该项目中找到任何给定AAC文件中给定音频数据的平均幅度.我目前正在将文件读取为数组缓冲区,并传递给Uint8Array.

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)

然后,我建立了两个数组,一个是实数(包含音频数据),另一个是虚数(包含全零),然后将它们传递给FFT.然后,将音频数据放入新的数组中,以便不再将数组中的数字视为无符号的8位整数.

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)

然后,我遍历数组,从0循环到N/2,其中N是包含原始音频数据的初始缓冲区的大小,并计算每个频点的大小.最后,我将这些大小的总和除以N/2.

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.

注意:对于感兴趣的人,可以在此处使用几种语言找到正在使用的FFT. FFT 我将音频文件的中间2 ^ 20字节传递给FFT,然后进行计算.

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.

推荐答案

您真的不需要为此使用FFT- Parseval定理本质上意味着时域中的能量等于频域中的能量,因此FFT部分是多余的-您可以在时域中计算幅度.通常,这是通过在选定的时间范围内(信号的长度)计算信号的 RMS值来完成的.这段时间取决于您要达到的目标.

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).

这篇关于在Javascript中使用FFT计算音频文件的平均幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆