Spectrum Analyzer API
Real-time audio frequency analysis with windowing and dB conversion.
createSpectrumAnalyzer()is currently a CPU-only utility. It does not use the GPU FFT engine internally.
createSpectrumAnalyzer()
Creates a spectrum analyzer instance.
Signature
ts
function createSpectrumAnalyzer(
config: SpectrumAnalyzerConfig
): SpectrumAnalyzer;1
2
3
2
3
Example
ts
import { createSpectrumAnalyzer, WindowType } from 'webgpu-fft';
const analyzer = createSpectrumAnalyzer({
fftSize: 2048,
windowType: 'hann',
sampleRate: 44100,
});1
2
3
4
5
6
7
2
3
4
5
6
7
SpectrumAnalyzer Class
Methods
analyze()
Analyzes an audio buffer and returns dB values.
ts
analyze(buffer: Float32Array): Promise<Float32Array>;1
Parameters:
buffer— Audio samples (real-valued)
Returns: Float32Array — Magnitude in dB per frequency bin.
ts
const audioBuffer = new Float32Array(2048);
// ... fill with audio samples from Web Audio API ...
const spectrum = await analyzer.analyze(audioBuffer);
// spectrum[i] is the dB value for bin i1
2
3
4
5
2
3
4
5
getFrequency()
Returns the center frequency for one FFT bin.
ts
getFrequency(binIndex: number): number;1
ts
console.log(analyzer.getFrequency(10));1
getFrequencies()
Returns the frequency values for each bin.
ts
getFrequencies(): Float32Array;1
ts
const frequencies = analyzer.getFrequencies();
console.log(`Bin 10 corresponds to ${frequencies[10]} Hz`);1
2
2
dispose()
Releases resources.
ts
dispose(): void;1
SpectrumAnalyzerConfig Type
ts
interface SpectrumAnalyzerConfig {
/** FFT size (power of 2, default 2048) */
fftSize: number;
/** Window function type */
windowType: WindowType;
/** Audio sample rate in Hz (default 44100) */
sampleRate: number;
}1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
WindowType Type
ts
type WindowType = 'hann' | 'hamming' | 'blackman' | 'flattop' | 'rectangular';1
Related
- Spectrum Analysis Tutorial — Usage guide
- Window Functions — Window function details