diff --git a/Tone/component/analysis/FFT.test.ts b/Tone/component/analysis/FFT.test.ts index d3c116bf4..a487959c9 100644 --- a/Tone/component/analysis/FFT.test.ts +++ b/Tone/component/analysis/FFT.test.ts @@ -35,6 +35,13 @@ describe("FFT", () => { fft.dispose(); }); + it("can get the frequency values of each index of the return array", () => { + const fft = new FFT(32); + expect(fft.getFrequencyOfIndex(0)).to.be.closeTo(0, 1); + expect(fft.getFrequencyOfIndex(16)).to.be.closeTo(fft.context.sampleRate / 4, 1); + fft.dispose(); + }); + it("can run waveform analysis", (done) => { const noise = new Noise(); const fft = new FFT(256); diff --git a/Tone/component/analysis/FFT.ts b/Tone/component/analysis/FFT.ts index 46c8c5912..db204796a 100644 --- a/Tone/component/analysis/FFT.ts +++ b/Tone/component/analysis/FFT.ts @@ -1,8 +1,9 @@ import { ToneAudioNode } from "../../core/context/ToneAudioNode"; import { dbToGain } from "../../core/type/Conversions"; -import { NormalRange, PowerOfTwo } from "../../core/type/Units"; +import { Hertz, NormalRange, PowerOfTwo } from "../../core/type/Units"; import { optionsFromArguments } from "../../core/util/Defaults"; import { MeterBase, MeterBaseOptions } from "./MeterBase"; +import { assert } from "../../core/util/Debug"; export interface FFTOptions extends MeterBaseOptions { size: PowerOfTwo; @@ -77,4 +78,15 @@ export class FFT extends MeterBase { set smoothing(val) { this._analyser.smoothing = val; } + + /** + * Returns the frequency value in hertz of each of the indices of the FFT's [[getValue]] response. + * @example + * const fft = new Tone.FFT(32); + * console.log(fft.getIndexFrequency()); + */ + getFrequencyOfIndex(index: number): Hertz { + assert(0 <= index && index < this.size, `index must be greater than or equal to 0 and less than ${this.size}`); + return index * this.context.sampleRate / (this.size * 2); + } }