0

我正在尝试在 Typescript 中计算 wav 文件的快速傅里叶变换。

我正在考虑为此使用专用的 npm 包,例如 fft.js,但后来我注意到标准AnalyzerNode具有内置的 FFT 功能。但是,我只看到 AnalyzerNode 用于处理实时音频流。

您将如何发送从通用 wav 文件加载的数据,无论是从文件系统加载还是从任何其他来源(例如 wav-decoder)加载,而不是麦克风?

4

1 回答 1

1

首先,请注意,AnalyserNode它只给出了 FFT 的大小;不包括相位部分。但是,如果这对您有用,那么您可以使用OfflineAudioContextandsuspend(time)来获得 FFT(幅度)。就像是:

// Let f be the file that has the wav file.
// c is used only so we can run decodeAudioData.  There are other ways to 
// do this.
let c = new AudioContext();
let b = await fetch(f)
  .then(response => response.arrayBuffer())
  .then(buffer => c.decodeAudioData(buffer));

let oac = new OfflineAudioContext({
  numberOfChannels: b.numberOfChannels,
  length: b.length,
  sampleRate: b.sampleRate});

let a = new AnalyserNode(oac, {fftSize: 1024});

// Suspend the context every fftSize frames so we can get the FFT of 
// the previous fftSize frames.
for (let k = a.fftSize; k < b.length; k += a.fftSize) {
  oac.suspend(k / b.sampleRate)
    .then(() => {
      a.getFloatFrequencyData(fftData);
      // Copy fftData out or do something with it
      });
    .then(() => oac.resume());
}

// We can ignore the buffer that startRendering would return from the
// resolved promise.
await oac.startRendering();
于 2021-06-21T21:47:28.523 回答