# FFT Analyzer – WASM Module
WebAssembly module for real‑time and offline audio spectral analysis, written in Rust. Provides FFT magnitude, phase, dB, band energies, Mel spectrum, smoothing, peak hold, RMS, peak, crest factor.
## Installation
Build with `wasm-pack`:
```bash
wasm-pack build --target webImport in JavaScript:
import init, { FftAnalyzer, WindowType } from './pkg/fft_analyzer.js';
await init();
const analyzer = new FftAnalyzer(2048, 44100);set_window_type(type: WindowType)– Hann, Hamming, Blackman, Rectangular.set_hop_size(hop: number)– overlap in samples (default fftSize/2).set_bands(numBands, minFreq, maxFreq)– logarithmic band edges.set_smoothing(factor: number)– 0..1 exponential smoothing.set_peak_decay(decay: number)– 0..1 peak hold decay per frame.set_dc_removal(enabled: boolean)– subtract mean from each block.
analyze(samples: Float32Array): Float32Array– process one block, return magnitude spectrum.analyze_stereo(left, right): Float32Array– average channels.process_stream(samples): Float32Array– streaming with internal buffer; returns spectrum when full frame available, else empty.flush_stream(): Float32Array– process remaining buffered samples (zero‑padded).process_file(samples): Float32Array– overlap‑process entire buffer, return average spectrum.process_file_stereo(left, right): Float32Arrayreset_accumulation(): voidget_averaged_spectrum(): Float32Array
get_spectrum(): Float32Array– magnitude (scaled to amplitude).get_spectrum_db(): Float32Array– dB.get_phase(): Float32Array– unwrapped radians.get_smoothed_spectrum(): Float32Arrayget_peak_spectrum(): Float32Arrayget_frequencies(): Float32Arrayget_normalized_spectrum(): Float32Array– max = 1.get_bands(): Float32Arrayget_band_edges(): Float32Array
get_dominant_frequency(): number– with quadratic interpolation.get_spectral_centroid(): numberget_spectral_flatness(): numberget_rms(): numberget_peak(): numberget_crest_factor(): numberget_mel_spectrum(numMels, minFreq, maxFreq): Float32Array– triangular Mel filters.
enum WindowType { Hann, Hamming, Blackman, Rectangular }const audioCtx = new AudioContext();
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const source = audioCtx.createMediaStreamSource(stream);
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 4096;
source.connect(analyser);
const data = new Float32Array(analyser.fftSize);
function loop() {
analyser.getFloatTimeDomainData(data);
analyzer.analyze(data);
const spectrum = analyzer.get_spectrum();
const db = analyzer.get_spectrum_db();
const bands = analyzer.get_bands();
// update visualisation
requestAnimationFrame(loop);
}
loop();const file = await fetch('audio.wav').then(r => r.arrayBuffer());
const audioBuffer = await audioCtx.decodeAudioData(file);
const pcm = audioBuffer.getChannelData(0);
analyzer.reset_accumulation();
const avg = analyzer.process_file(pcm);
// avg is average magnitude spectrumfunction onAudioChunk(chunk) {
const res = analyzer.process_stream(chunk);
if (res.length > 0) {
// full frame ready
const spec = analyzer.get_spectrum();
}
}
// at end:
const last = analyzer.flush_stream();- Reuses internal buffers;
get_*methods allocate new Float32Arrays – copy if called in hot loop. - Smoothing and peak hold are O(N) per frame.
- Mel spectrum is computed on demand; may be expensive for large FFT and many bands.
- For low latency, keep FFT size ≤ 4096 and avoid
get_mel_spectrumin every frame unless needed.
git clone CodeHorizon0/FFT_WASM
cd fft-analyzer
wasm-pack build --target web