core.js — DSP Utilities

Pure functions with no DOM or module dependencies.

Constants

Export Value
SAMPLE_RATE 48000

Math Helpers

Function Description
randInRange(min, max) Random float in [min, max)
randInt(min, max) Random integer in [min, max] inclusive
log10(x) Base-10 logarithm
logRand(min, max) Logarithmically-distributed random in 10^[log10(min), log10(max))
freqToNote(freq) Convert Hz to nearest note name (e.g. 440"A4")
hexToRgba(hex, alpha) "#ff0000""rgba(255,0,0,0.5)"

Audio Encoding

encodeWAV(samples, sr)ArrayBuffer

Encodes a Float32Array of PCM samples as a 24-bit mono WAV file with a 44-byte RIFF header. Samples are clamped to ±1.0 before 24-bit signed integer conversion.

Format: - PCM (format 1) - 1 channel (mono) - 24 bits per sample - Sample rate as provided

adsrAmplitude(t, a, d, s, r, dur)number

Evaluates the ADSR envelope at normalised time t (0 = start, 1 = end).

0 ≤ t < a:            ramp 0 → 1                (attack)
a ≤ t < a+d:          ramp 1 → s                (decay)
a+d ≤ t < dur-r:      hold at s                 (sustain)
dur-r ≤ t < dur:      ramp s → 0                (release)
t ≥ dur:              0

normalize(samples, target)

Scales samples (Float32Array) in-place so the absolute peak reaches target (default 0.944, ≈ –0.5 dBFS). No-op if the peak is below 0.001.