openprocessing_p5js · SoundWaveform
Additive Synthesis Waveform Visualisation
Visualisation of additive synthesis: y(t) = A * sum_{i=0..N-1} a_i * sin(2π f_i t + phi_i). Each sine is rendered as a 1-px polyline coloured by its hue; the composite waveform is drawn on top in white. Five-to-N layered sines are the canonical entry point of the Processing Sound library example set.
Original: https://processing.org/tutorials/sound
▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.
Math essence
y(t) = sum a_i sin(2π f_i (t + x/W) + phi_i); amplitudes normalised so sum |a_i| = 1; detune in [-0.5, 0.5] per oscillator. Fundamental scaled by (i+1) for a harmonic stack; phases randomised at reset.
Parameters
| Name | Type | Default | Range | Description |
|---|---|---|---|---|
dim | int | 512 | 200–1200 | Square canvas side length. |
num_sines | int | 7 | 1–32 | Number of oscillators in the stack. |
fundamental | float | 1.5 | 0.1–8 | Base frequency in cycles across the canvas. |
detune | float | 0.05 | 0–0.5 | Per-oscillator random detune factor. |
speed | float | 0.06 | 0–0.4 | Time advance per tick. |
bg_color | tuple | 8,10,22 | — | Background canvas color. |
composite_color | tuple | 240,240,240 | — | Composite waveform color. |
seed | int | null | 0–999999 |
Reference
First 60 lines of reference.py. Full source:
library_external/openprocessing_p5js/SoundWaveform/reference.py
from __future__ import annotations
from typing import ClassVar
import numpy as np
class SoundWaveform:
"""Additive-synthesis waveform visualisation: sum of N sine waves at
different frequencies / amplitudes / phases.
This is the visual half of the canonical Processing Sound library
Example 01
(<https://processing.org/tutorials/sound> — "Five sine waves layered to
construct a cluster of frequencies") and its p5.js port. The audio
identity is
y(t) = A * sum_{i=0..N-1} a_i * sin(2π f_i t + phi_i)
with amplitudes normalised so the peak-to-peak stays bounded
(``sum |a_i| = 1``). step(n) advances time; render() draws an envelope-
coloured stack of the individual sines plus the composite waveform.
Each sine is rendered as a 1-px polyline coloured by its hue, and the
composite waveform is drawn on top in white. The result is the iconic
"wave packet" / additive-synthesis display that has been the entry
point for every coding musician learning Processing or p5.js.
"""
family: ClassVar[str] = "openprocessing_p5js"
sketch: ClassVar[str] = "soundWaveform"
def __init__(
self,
dim: int = 512,
num_sines: int = 7,
fundamental: float = 1.5,
detune: float = 0.05,
speed: float = 0.06,
bg_color: tuple[int, int, int] = (8, 10, 22),
composite_color: tuple[int, int, int] = (240, 240, 240),
seed: int | None = None,
) -> None:
self.dim = dim
self.num_sines = num_sines
self.fundamental = fundamental
self.detune = detune
self.speed = speed
self.bg_color = np.array(bg_color, dtype=np.float32)
self.composite_color = np.array(composite_color, dtype=np.float32)
self._seed = seed
self.reset()
def reset(self, seed: int | None = None) -> None:
if seed is not None:
self._seed = seed
self.rng = np.random.default_rng(self._seed)
self.canvas = np.tile(self.bg_color, (self.dim, self.dim, 1))
self._tick = 0
self._t = 0.0 Try running it locally
The reference implementation is self-contained Python with numpy. Install the package and run:
python library_external/openprocessing_p5js/SoundWaveform/reference.py Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.
More from openprocessing · p5.js
Inverse-distance Attractor Network
openprocessing_p5js · AttractorNetwork
A population of point-agents, each carrying a position and a velocity, mutually attracted by an inverse-distance force F = G / max(d, d_min)^alpha along the displacement vector.
Kaleidoscope Mirror Mandala
openprocessing_p5js · KaleidoscopeMirror
Draws a single wandering line and reflects it across N rotational mirrors around the canvas centre.
Mathematical Rose (Rhodonea) Patterns
openprocessing_p5js · PolarCurves
Overlay of multiple rose (rhodonea) curves in the canonical polar form r = a * cos((n/d) * theta + phase), parametrised by the rational multiplier k = n/d.
Poincaré Disk Hyperbolic Tessellation
openprocessing_p5js · HyperbolicTiling
Escher-style regular hyperbolic tessellation in the Poincaré disk.
Tron Lightcycle Trails
openprocessing_p5js · TronLightcycle
Tron-style lightcycle trails.