Periodic waveform functions. Phase t is normalized to [0, 1] — one full turn.
npm install periodic-function
import { sine, square, wavetable } from 'periodic-function'
sine(0.25) // 1 (peak)
square(0.75) // -1 (low)
wavetable(null, [0, 1, 0, 0.5]) // Float32Array wavetable from Fourier coefficientsAll functions take phase t ∈ [0, 1] as first argument. Values outside [0, 1] wrap correctly.
// Cosine as a phase-shifted sine
sine(0, 0.25) // 1 (same as cosine(0))
// Square wave with 10% duty cycle
square(0.05, 0.1) // 1
square(0.15, 0.1) // -1
// Triangle with peak at 0.25 (asymmetric)
triangle(0.25, 0.25) // -1 (valley, since peak is at t=0)
// Trapezoid as a square with soft edges
trapezoid(t, 0.05, 0.5, 0.55)
// Fourier series: pure sine
fourier(0.25, null, [0, 1]) // 1
// Wavetable for Web Audio API PeriodicWave
const real = new Float32Array(64)
const imag = new Float32Array(64)
for (let k = 1; k < 64; k += 2) imag[k] = 4 / (Math.PI * k) // square wave
const table = wavetable(real, imag) // Float32Array[8192], normalized to ±1- web-audio-api — uses
wavetable()forPeriodicWave - MDN: createPeriodicWave
- List of periodic functions
MIT © Dmitry Iv
