Spectrum Analyzer API
Real-time audio frequency analysis with windowing and dB conversion.
createSpectrumAnalyzer()
Creates a spectrum analyzer instance.
Signature
ts
function createSpectrumAnalyzer(
config?: Partial<SpectrumAnalyzerConfig>
): SpectrumAnalyzer;1
2
3
2
3
Example
ts
import { createSpectrumAnalyzer, WindowType } from 'webgpu-fft';
const analyzer = createSpectrumAnalyzer({
fftSize: 2048,
windowType: WindowType.Hann,
sampleRate: 44100,
});1
2
3
4
5
6
7
2
3
4
5
6
7
SpectrumAnalyzer Class
Properties
| Property | Type | Description |
|---|---|---|
fftSize | number | FFT size (power of 2) |
sampleRate | number | Audio sample rate in Hz |
windowType | WindowType | Current window function |
Methods
analyze()
Analyzes an audio buffer and returns dB values.
ts
analyze(buffer: Float32Array): 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 = analyzer.analyze(audioBuffer);
// spectrum[i] is the dB value for bin i1
2
3
4
5
2
3
4
5
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 Enum
ts
enum WindowType {
Rectangular = 'rectangular',
Hann = 'hann',
Hamming = 'hamming',
Blackman = 'blackman',
FlatTop = 'flattop',
}1
2
3
4
5
6
7
2
3
4
5
6
7
Related
- Spectrum Analysis Tutorial — Usage guide
- Window Functions — Window function details