audio

@audio/saturate-waveshaper1.2.0

Waveshaper

Waveshaper — normalized tanh or custom curve, oversampled

Status
calibrated
Updated
Family
saturate, 5 packages under it
Plugin
waveshaper, effect, streaming, 2 parameters
Category
effects
Licence
MIT
Dependencies
1 package, all permissive: MIT
Downloads
2,226 last month
Tests
15 in audiojs/saturate's CI, passed at 027ffbc,

Listen

// "Local Forecast" by Kevin MacLeod (incompetech.com), CC BY 4.0, incompetech.com/music/royalty-free/faq.html
audio('/samples/music/modern/local-forecast.mp3').waveshaper()

// "Local Forecast" by Kevin MacLeod (incompetech.com), CC BY 4.0, incompetech.com/music/royalty-free/faq.html

Parameters

  • effect
  • streaming
  • latency
  • tail
drive
2
0.5 to 10
mix
1
0 to 1
import waveshaper from '@audio/saturate-waveshaper'

Applies a memoryless transfer function to every sample — normalized tanh(drive·x) by default, or your own curve function. Oversampled (windowed-sinc up → shape → windowed-sinc down) so harmonics pushed above Nyquist by the nonlinearity fold back as inaudible energy instead of audible aliasing, not a naive per-sample waveshaper.

waveshaper(data, { drive: 4, fs: 44100 })
waveshaper(data, { curve: x => x - x ** 3 / 3, fs: 44100 })  // custom transfer
Param Default
drive 2 pre-gain into the tanh curve (ignored when curve is set)
curve x => tanh(drive·x)/tanh(drive) custom (x: number) => number transfer function, applied at the oversampled rate
fs 44100 sample rate
oversample 4 oversample factor; 1 disables oversampling (naive, aliased)
mix 1 0..1 — wet/dry blend

Mutates data in place and returns it (same array reference, not a copy). mix: 0 is an exact passthrough — verified bit-close (<1e-6) in the family test suite.

stream(block, { drive: [4], mix: [1] }, state(1024)) is the same transfer in calls of any size (up to the size given to state), oversampled 4×, its state carried from call to call; a curve in the same object takes the place of drive. The whole call reads 31 samples ahead of each output; a stream cannot, so its output is LATENCY (31) samples late, and moves RING (31) samples past that once its input stops (for a curve that takes 0 to 0; the oversampler is @audio/resample-sinc's, on the arithmetic the whole call resamples with). Shifted back by LATENCY it is the whole call's output bit for bit, except within 31 samples of the ends of a whole signal. Values arrive as a host gives them to a processor, each a 1-length array, read at each call. The ./audio processor runs it on each channel: latency 31 samples, tail 31 samples, drive and mix live: each declares smoothing (20 ms) and is glided sample by sample along the host's ramp, 32 samples late, so a moved value is one line however the host cut its calls and a held one is the value itself, bit for bit (lanes after the length: drive and mix from 0 and stride).

Use when: you need a specific custom transfer curve, or a clean tanh baseline without a particular harmonic-symmetry signature.
Not for: a specific circuit-modeled color — use tube (even harmonics), transistor (odd harmonics), or tape (HF-rolled tanh) for those.


Part of the @audio/saturate family.

MIT © audiojs