← Home
music physics audioworklet fourier rust wasm

Windwheel: The Winding Is the Sound

Prerequisites: Beats as a Wave, for the "audio as a function of position on a cycle" reading this post tests against a second instrument.

A magnet falls through a copper coil and induces a voltage. If the coil is wound in a pattern, some turns one way, some the other, tight in places and sparse in others, the voltage follows the pattern, and a speaker on the coil plays it. The winding is a recording you read by moving a magnet through it. windwheel simulates that instrument: draw a winding on a hoop, or type the notes you want and let it design one, spin the magnets, and hear the EMF they induce, computed sample by sample in the browser.

It is a linear tonewheel. A Hammond's tonewheel spins a toothed disc past a pickup; windwheel pulls a magnet along a wound path. The code is a Rust library and CLI with a WASM build for the page, MIT, at github.com/stackdump/windwheel-bitwrap-io.

A Magnet in a Coil

The physics fits in four lines, and the README has the derivation. A uniformly magnetised sphere is exactly a point dipole outside itself. The flux it pushes through one coaxial turn of radius a, at axial offset d, is

Φ(d) = μ0 m a² / (2 (a² + d²)^{3/2})

A winding is signed turns per section, so its linkage Λ(z) is the sum of Φ over every turn, a convolution of the turn density with Φ. The voltage is

EMF = −dΛ/dt = −v · dΛ/dz

so what you hear is the spatial derivative of the winding, read at the magnet's speed. A winding that alternates every 15 mm, read at 4.5 m/s, is a 297 Hz tone (v/λ; the CLI's forward example gives 0.88 V peak from 100-turn sections into a 137 Ω coil).

The kernel is also why the instrument has a character. Φ is smooth on the scale of the coil radius, so its spectrum falls off as roughly e^{−ka}: the coil is a spatial low-pass with a corner at v/(2πa), about 120 Hz at 4.5 m/s with a 6 mm coil. Notes above the corner cost turns exponentially. The page's brightness slider trades this off by choosing a tighter coil and the largest magnet that fits inside it, which raises the corner and lowers the level. In the screenshot it reads "coil 3.5 mm · corner 205 Hz at 4.5 m/s".

Current through the coil brakes the magnet (Lenz). The force is F = I · dΛ/dz, always opposing motion, and the test suite checks that the kinetic energy lost equals ∫ I·EMF dt to within 2%. Inductance is ignored, since ωL ≪ R for these coils at audio frequencies, and so is the hoop's curvature, which is valid while the circumference is much larger than the coil.

The Hoop Is a Phase

On a straight tube the magnet passes once. On a hoop of circumference C the path is periodic, so the magnet's position is z mod C. That is a phase in the literal sense: nothing about the state records how many laps the cord has run, and one lap of winding plays once a lap, forever. The worklet wraps it exactly that way (worklet.js):

this.z += this.v * dt;
if (this.z >= this.C) this.z -= this.C;
else if (this.z < 0) this.z += this.C;

Up to 16 magnets ride one cord, evenly spaced, so magnet k sits at z + kC/M and the EMF is a sum over them, −v · Σₖ dΛ/dz(z + kC/M). The Bowed rope preset uses this: three magnets a third of a lap apart on a loop wound with three sustained notes, so each magnet is always over a different note and the bow holds a D major chord.

Besides the hoop there are drums and drawn loops (also periodic), and open paths: a rack of tubes the magnet falls through, an incline, a rail you drag, a spring, and a pendulum arc. Each has its own motion law in motion.rs, and the worklet integrates the same laws per sample.

Winding a Note Sequence

Drawing turns by hand works, but the useful direction is backwards: type C E G B C B G E and get a winding that plays it. That is a deconvolution. The motion gives the time t(z) and speed v(z) at each point of the path. The target signal s(t) becomes a target slope dΛ/dz(z) = −s(t(z)) / v(z), and the turn density is that target deconvolved by the single-turn kernel. design.rs does it with a Wiener filter,

N = S · conj(K) / (|K|² + ε),      ε = reg · max|K|²

then sums the density into 1 mm sections, caps and rounds the turns, and iterates against the full multi-layer forward model. On the hoop the FFT's circular convolution is the physics rather than an approximation, because the path really is periodic. On a tube the grid is zero-padded so circular equals linear.

That design takes tens to hundreds of milliseconds, which is too slow to run while the wheel plays. livewind.rs is the player's shortcut. It keeps the kernel's spectrum cached, applies the Wiener response |K|²/(|K|² + ε) to the target directly, and skips the turn cap and quantisation. A new key or note sequence then costs one FFT pair, so every setting changes live and crossfades in over 30 ms. The turns it draws are a picture of the winding, not a build plan. What would it take? runs the real design and exports a plan (sections, turns, direction, layers) as CSV or JSON.

A sequence is laid out in equal-time slots for the path's motion law. On a hoop at constant speed that means equal arcs; on the incline, where the marble accelerates, the later slots are longer. patch::parse_sequence reads the names: sharps, flats and octaves are optional, and a bare name takes the octave nearest the note before it.

Presets

Every preset is a patch file, a JSON document whose CID (CIDv1, sha2-256 over the canonical JSON) is recorded in presets/index.json. The page shows it under the sequence (bagaaierafvq3bo747… for the Cmaj7 hoop). tests/presets.rs builds each one in Physical mode and checks that every note lands within 3% of pitch at correlation ≥ 0.85, and that it is audible through its own motion law with drag on.

Preset What it is
Cmaj7 hoop 1.8 m hoop wound with C E G B C B G E; spin it to loop the arpeggio
Hoop looper 1.8 m hoop, an 8-note riff in equal arcs
Bowed rope drawn loop, 3 magnets a third apart, 3 sustained notes: a held D major chord
Drop chimes rack of 5 tubes; a magnet falls through each and plucks a pentatonic note designed for the accelerating fall
Marble run 20° incline, a magnet marble every 0.3 s through a 6-note phrase
Pendulum 1 m rod, ±60° arc with an fm winding: pitch sweeps with the swing, plus a wound-in vibrato
Spring bass magnet on a 2.5 Hz spring through a short sine winding: a wobbly, decaying bass
Slide hand-dragged rail with a chirp winding (λ 16 → 6 mm): portamento
Music-box drum 2.4 m drum at a constant 4 m/s, an 8-note tune of pure plinks

Physical mode enforces real limits (constraints.rs): at most 12 layers, coil clearance round the magnet, N52 remanence, cord speed ≤ 25 m/s, and pitch, tempo and speed coupled. A buildable badge says the patch passes. Unbound mode drops the coupling for re-tune and tone-filter effects, and the export marks the patch not buildable. Nobody has wound a physical windwheel yet, so "buildable" means the simulation's limits pass, not that the instrument has been made.

Tempo Against the Decks

A wheel that plays its notes once a lap has a tempo: note onsets a minute,

wheel_bpm = notes × v / C × 60

with magnets counting as extra onsets per lap. Its relation to a DJ deck's BPM is live::relate. It takes the ratio, finds the nearest whole-number multiple or divisor, and calls the pair locked within 1% (TEMPO_TOLERANCE). sync_bpm picks the multiple of the deck nearest the wheel's tempo. Voice::motor_for_speed inverts the motor's settled speed against friction and mean Lenz drag, so a tempo you set is the tempo you hear. The ladder functions (Rung, ladder_place, rung_span, step_rung) give the rungs …, ⅓, ½, ×1, ×2, ×3, … that the motor's speed range can reach against a given deck, and step between them.

This is where windwheel meets the decks. live.rs is the toy's AudioWorklet ported to Rust as Voice, for hosts that render their own audio. The host is djandroid, our Android DJ app, which has a Wheel tab next to Deck A, Mix, Deck B, Vocoder, Effects and Inserts. A feeder thread renders Voice::render into a lock-free ring that feeds a mixer input strip labelled "Windwheel", so the wheel goes through the same master as the decks. The tab draws the path with its moving magnets. It shows the wheel's BPM as "onsets a lap (notes × magnets)" and the relation to each deck as ×n or 1/n with ◀ ▶ to step a rung, plus a Sync per deck, the presets, a note-sequence editor, key and sound controls. The Kotlin side reaches Rust through UniFFI (wheel_load, wheel_sequence, wheel_sync(deck), wheel_step(deck, up), …). djandroid's source is private, so there's nothing to link. The tempo arithmetic it calls is all in the public live.rs.

The Wheel as a Modulator

In the app the wheel is an input to a signal chain, and the chain is where it stops being a toy. On the Vocoder tab, "Shaped by" has three choices: Deck A, Deck B and the Wheel. Pick the Wheel and hold a chord. The vocoder's internal synth (three oscillators a voice) plays the chord as the carrier, and the wheel is the modulator: its envelope, band by band, decides which parts of the chord sound. On the Inserts tab, the master carries a filter → bitcrush → delay → reverb chain, with the delay synced to the lead deck. The wheel's motor speed, its note sequence and the coil's brightness now act like oscillator and envelope controls on a synth patch. Spinning the hoop faster raises the wheel's pitch and also changes the rhythm the chord is gated by, and a ×2 rung on the tempo ladder doubles that rhythm against the deck.

The routing is a few lines of code, and they are worth reading because the order matters. djandroid's set_vocoder("Wheel") looks up the wheel's input strip by its label and hands the engine ModulatorPick::Input(i), with CarrierPick::Synth as the carrier. In djrust, ModulatorPick can be an input strip, a deck, a clip, a capture or the synth, and CarrierPick can be almost any of the same. The phone only offers the synth as carrier, so in the app the wheel is always the modulator, never the carrier. The engine's render block then runs, per block of samples:

  1. Every input strip pulls its ring. For the wheel, that is the Voice output the feeder thread wrote.
  2. vocoder_pass reads the modulator from the strip before its fader and runs the channel vocoder (fx/vocoder.rs: band-pass banks of up to 32 bands, an envelope follower per band, formant shift, noise for unvoiced sounds, and a vowel filter).
  3. Each input strip runs its own insert chain, then its fader, into the master.
  4. The vocoder's output is mixed into the master.
  5. The master's insert chain runs, then the post chain and the master level.

The tap in step 2 comes before step 3, so the vocoder hears the wheel dry. Inserts on the Wheel target colour only the wheel's own path to the master, not what it does to the chord; to process the vocoded chord, put the inserts on the Master. "Only the vocoder" mutes the wheel's strip in the mix while the tap still feeds the vocoder, which leaves just the chord, gated and coloured by the wheel. The insert targets map onto the engine in djandroid's inserts.rs ("Wheel" becomes input:Windwheel, following djrust's INPUT_PREFIX). The app's Effects tab is something else: per-deck tricks quantised to the bar, which don't touch the wheel.

What makes this feel like tweaking a synthesizer and not mixing a track is that every knob in the chain is continuous and live. The winding re-designs in milliseconds, the voice crossfades a new table in over 30 ms, and the insert chains are smoothed on the engine side (a switch fades, a tail rings out), so a change you make lands in the sound as you make it. That is a description of how it plays, not a measurement. We have not timed the path from a slider to the speaker on a phone.

What Carried Over from Beats as a Wave

Beats as a Wave made five claims about the beats sequencer. Single-token rings are cyclic groups, so firing is phase. The joint marking is a point on a torus with period lcm(n_i). A ring's gate spectrum is the DFT of its place weights. The audio can be computed per sample in an AudioWorklet. And independent rings can be pre-rendered as one hyperperiod and cached. Windwheel and the Rust DJ engine, djrust, implement some of these and not others. Here is what we found when we read the code.

Per-sample audio was built, twice. Windwheel's whole sound path is a per-sample loop in process() (worklet.js). Each sample it reads dΛ/dz at every magnet from a table, sums the EMF, applies the shared current's drag to the motion, and steps the motion law. There is no message per note and no node per note; the page only sends a new table when the winding changes. djrust's wave engine does the same for the beats net. WaveRunner::process_with loops over frames and calls do_tick inside the sample loop when the tick counter rolls over, so the net fires in the audio callback (a cpal stream on desktop and Android, not an AudioWorklet). A wasm build of djrust for beats.bitwrap.io's AudioWorklet exists only on an unmerged branch. The Rust Voice in windwheel is a line-by-line port of the worklet. No test compares the two sample for sample, so treat "the same" as intended, not verified.

Firing-as-phase holds for windwheel, in a simpler form than the net version. Windwheel has no Petri net. Its phase is a position on a circle, z mod C, and its lap plays like a single-token ring's cycle, with the winding standing in for the place weights. On the net side, djrust has incidence_matrix and p_invariants, but only its tests call them. At runtime nothing treats a ring as Z_n or checks the all-ones invariant.

The torus and its lcm were not built. Nothing in djrust computes a joint period, and windwheel's magnets share one cord, so they move along a single diagonal phase rather than over a torus. The tempo ladder is the nearest relative. It keeps the wheel at a whole-number ratio to a deck so the two phases realign every few beats. But it is a speed control against one other clock, not an lcm of ring lengths.

The Fourier reading turned up in windwheel's physics, not in either sequencer. The ring-DFT claim, a gate spectrum as the DFT of place weights, is not in either codebase. djrust's FFTs build oscillator wavetables and measure spectra offline. Windwheel's Fourier content is elsewhere. Its spectrum is the coil kernel's, Φ̂(k) = μ0 m k a K1(ka), which is why the instrument is dark. Its design is a Wiener deconvolution that is exact on the hoop because the hoop is periodic. And its fm pattern winds a vibrato into the coil spatially, so the modulation is in the copper. The draft's gate × carrier and nested AM/FM did land in djrust: lane_sample computes env * c * level, and WaveMod { target, source, kind: "am" | "fm", depth } wires one lane's gate into another's level or pitch.

Hyperperiod pre-rendering and caching were not built. Neither engine renders a loop once and replays it, and nothing is keyed by (marking, seed). djrust's AUTO-NEXT pre-renders the next track a bar early, which is a handover, not a cache. Windwheel precomputes something narrower: the winding's dΛ/dz table, which the worklet interpolates per sample. The table is the expensive part (a kernel convolution over the whole path), and the audio is still computed live, because speed, drag and the hand change it every sample.

So the part of the draft that became code is the engineering: per-sample evaluation in the audio thread, and gates multiplying carriers. The algebra (cyclic groups, the torus, ring spectra, hyperperiod caching) is still a description of how the net behaves, not something either engine computes or relies on. Windwheel is the instrument where "the sound is a function of position on a cycle" is plainly true, because there the cycle is a piece of copper on a hoop.

Open question: a beats ring and a windwheel hoop are both read once a lap. If a ring's place weights were wound onto a hoop, would the gate's predicted c_k, shaped by the coil's low-pass, be the spectrum the magnet actually plays?

Related: Beats as a Wave · Petri Nets as a Music Sequencer · windwheel.bitwrap.io · source

×

Follow on Mastodon