Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# 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 web

Import in JavaScript:

import init, { FftAnalyzer, WindowType } from './pkg/fft_analyzer.js';
await init();
const analyzer = new FftAnalyzer(2048, 44100);

API Overview

Configuration

  • 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.

Analysis

  • 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): Float32Array
  • reset_accumulation(): void
  • get_averaged_spectrum(): Float32Array

Getters (last processed frame)

  • get_spectrum(): Float32Array – magnitude (scaled to amplitude).
  • get_spectrum_db(): Float32Array – dB.
  • get_phase(): Float32Array – unwrapped radians.
  • get_smoothed_spectrum(): Float32Array
  • get_peak_spectrum(): Float32Array
  • get_frequencies(): Float32Array
  • get_normalized_spectrum(): Float32Array – max = 1.
  • get_bands(): Float32Array
  • get_band_edges(): Float32Array

Metrics

  • get_dominant_frequency(): number – with quadratic interpolation.
  • get_spectral_centroid(): number
  • get_spectral_flatness(): number
  • get_rms(): number
  • get_peak(): number
  • get_crest_factor(): number
  • get_mel_spectrum(numMels, minFreq, maxFreq): Float32Array – triangular Mel filters.

WindowType enum

enum WindowType { Hann, Hamming, Blackman, Rectangular }

Examples

Real‑time microphone

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();

Offline file

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 spectrum

Streaming with buffer

function 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();

Performance Notes

  • 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_spectrum in every frame unless needed.

Building from Source

git clone CodeHorizon0/FFT_WASM
cd fft-analyzer
wasm-pack build --target web

About

WASM lib for fast fft audio analyze

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages