← Home
petri-nets music beats-bitwrap tone-js

Petri Nets as a Music Sequencer

Go to beats.bitwrap.io. Pick a genre — techno, jazz, ambient, drum & bass, 19 in total. Hit Generate. You'll hear a full track: drums, bass, melody, arpeggios. Change the seed, get a different track. Replay the same seed, get the exact same one.

There's no piano roll behind this. No timeline grid, no DAW. The whole thing runs on a diagram from 1962 — circles connected by arrows, with little dots called tokens moving through them. It was invented to describe telephone networks and factory assembly lines. We're using it to make beats.

And not loosely. Every note you hear is literally a token moving one step through that diagram.

A Drum Machine Made of Tokens

Picture a ring of 8 circles connected by arrows. One dot — a token — sits at the first circle. Every tick, it hops to the next one. Some circles trigger a kick drum when the token lands. The rest are silent. The token goes around and around. You've got a beat.

The pattern of hits comes from the Bjorklund algorithm — it spreads K hits across N steps as evenly as possible. With 3 hits across 8 steps you get the tresillo, a rhythm that shows up everywhere from Afro-Cuban son to Rihanna.

Tresillo rhythm as a token ring

That ring of circles? It's called a Petri net. The circles are places, the arrows pass through transitions, and the dot is a token. That's the whole vocabulary. Change the numbers — 4 hits across 16 steps — and you've got four-on-the-floor techno. Same structure, different rhythm.

Each instrument gets its own ring. Kick, snare, hihat, clap — all running in parallel. Make the rings different lengths and they drift in and out of sync, giving you polyrhythm for free. A 6-step hihat over a 16-step kick sounds like the cross-rhythms in Afro-Cuban and West African music, because it is the same math.

Everything Is a Ring

Melodies work the same way. Instead of drum hits, each step carries a note — pitch, velocity, duration. The music theory picks notes that make sense over the chord (chord tones on strong beats, passing tones on weak beats, rests so it can breathe), and lays them out in a ring.

Bass lines? Ring. Arpeggios? Ring. Harmony pads? You guessed it.

Pick a genre — techno, jazz, ambient, dnb — and a preset fills in the details: scales, chords, drum patterns, how much swing, how much humanize. The generator assembles a bunch of rings, and the sequencer just... runs them. No AI, no samples — music theory and random numbers with a seed. Same seed, same track, every time.

From Loops to Songs

Loops are easy — tokens go around, patterns repeat. But a song needs intros, drops, breakdowns. The hihat should disappear for 8 bars, then come back. The bass should sneak in during the second verse.

So we added control nets — rings that don't make sound but fire commands like "mute the hihat" or "bring in the bass." A tiny stage director made of circles and arrows.

The first version was hilariously wasteful: a chain of 1,536 individual steps, one per tick, with a command every few hundred steps and nothing in between. It worked! But the project file was 22 MB for a 3-minute track.

The fix: countdown timers. Instead of 384 empty steps before a section change, we put 384 tokens in a single circle and drain one per tick. A special connection (called an inhibitor arc) holds back the control event until the countdown hits zero. Same result, way less diagram:

Before After
Places 37,148 800
Transitions 37,120 800
File size 22 MB 242 KB

One circle with many tokens does the job of many circles with one token each. Petri nets are flexible like that.

What You Get for Free

We didn't plan for these — they just fell out of using Petri nets:

Determinism. Same seed, same track. Always. On any machine, any browser. No hidden state, no drift.

You can watch it think. Every net is just circles and arrows — draw it on screen and you can literally see tokens hopping around in real time. When something sounds wrong, you can see why.

Mix and match. Each instrument is its own net. Song structure is a separate layer. They don't know about each other. Pull one out, add another — nothing breaks.

Math that means something. Petri nets have been studied since the '60s, and the analysis tools translate directly to music. We can prove a pattern loops, that every instrument eventually plays, and that a song actually reaches its end. Not bad for a diagram.

Try It

beats.bitwrap.io — 19 genres, all client-side, nothing to install.

A huge shoutout to Tone.js, which does all the heavy lifting on the audio side. Petri nets decide what plays and when — Tone.js makes it actually sound good. Polyphonic synths, reverb, compression, sub-millisecond scheduling — we got all of that essentially for free. It's one of those libraries where you keep expecting to hit a wall and never do.

The Petri net engine itself? About 100 lines of vanilla JS. The music theory — scales, chords, Euclidean rhythms, Markov chains — is another 3,700 lines. No npm, no bundler, no framework. Tone.js from CDN, a Go server that serves static files, and a 60-year-old idea about circles and arrows that turned out to be a pretty good sequencer.

Source: github.com/stackdump/beats-bitwrap-io

Curious about Petri nets beyond music? pflow.xyz is a browser-based editor where you can build and simulate your own.

Source: github.com/stackdump/beats-bitwrap-io

×

Follow on Mastodon