Updated
infinity-knobs
Precise, endless MIDI knobs in the browser.
Status: Version 0.1. Calibrated and tested on one controller, an Akai MPD218; other controllers should work, and their speeds can be tuned.
What it is
A MIDI knob sends a 7-bit value: 128 positions from end to end. That's far too coarse to land on an exact value. Spread over 20 Hz to 20 kHz, the steps are almost a semitone apart. And the knob runs out at either end, even on controllers whose knobs physically turn forever.
infinity-knobs reads a knob's movement instead of its position. Turned slowly, each value is a quarter of a cent; spun hard, fifty. So the same knob sweeps the whole range in half a second and still lands within a fraction of a cent. On controllers that take values sent back to them, it keeps each knob away from its ends, so it never runs out.
I wrote it for curve, where it tunes two oscillators into exact ratios with an MPD218's knobs, then pulled it out into a library anyone can use: npm install infinity-knobs.
The live example has an on-screen knob that behaves like an MPD218's, down to going silent at either end and taking the value it's sent back, so you can feel the acceleration and watch the resets without a controller. Drag it, scroll over it or use the arrow keys, or connect a MIDI controller and turn both.
How it works
- Every message updates a running rate of the knob's movement, in values a second, averaged so a burst of USB messages doesn't count as a spin. The step grows with the cube of that speed, from the fine step to the coarse one, so careful turns stay fine.
- The speeds are calibrated on a real MPD218: its smooth knobs send at most one message every 10 ms, a careful turn runs at about 60 values a second and a hard spin at about 500.
- Endless knobs come from something I measured on the MPD218. At 127 a knob goes silent rather than repeating, but if the computer sends the knob's value back, the knob carries on from there. So when a knob nears either end, the library sends it back to 64, without the controller being reprogrammed.
- A message the knob sent before the reset reached it is closer to the old position than to the middle, so it's read from there and the value never jumps. Controllers that ignore the reset keep working as ordinary knobs.
- The first message from a knob only records where it is, so touching it never jumps. Relative encoders are decoded too, and the values can scale linearly, exponentially in cents, or wrap round for angles.
- It's TypeScript with no dependencies. The core is pure functions, tested in Node against a simulated MPD218, and the Web MIDI layer picks up devices plugged in later. Merging a version bump publishes to npm from GitHub Actions with provenance, no token stored.
Built with
Started