K

How devices work

The mental model, and why a device is shaped the way it is.

A device is a small program Msh loads and runs like any built-in device. It's sandboxed: it can make sound and draw its face, but it can't touch your files or the rest of your system. That's why loading someone else's device is safe, and why a misbehaving one can be muted instead of taking the app down.

A few files

A device is a folder of parts, each compiled on its own:

  • index.ts is the sound.
  • ui.ts is the look.
  • canvases/.../paint.ts is your own artwork, drawn on the CPU.
  • canvases/.../shader.ts is the same canvas drawn on the GPU, a fragment shader instead of paint.ts, for visuals that need the graphics card.

They're separate on purpose. The sound and the look have nothing to do with each other, so editing one never disturbs the other. That's why you can redesign a device's face while it keeps playing.

A device can also push live data from its sound to its visuals: process fills buckets the canvas or shader reads each frame, so a meter or scope tracks the audio in real time. And the tile's face can open into a full-window editor for a bigger view of the same controls.

Two worlds: audio time and build time

This is the one idea worth holding onto. A device lives in two worlds with opposite rules.

  • Audio time is process. It runs constantly, thousands of times a second, on a strict clock. Fast and predictable.
  • Build time is everything else: describe, ui.ts, paint.ts. These run when the device loads or its look changes. They can take their time.

Almost every rule comes from this split. Heavy setup happens once at build time (in prepare); the fast loop just uses what was prepared. There's a whole page on why process has to stay fast.

Hot reload

Save a file and Msh recompiles that one part and swaps it into the running graph, in about a second, without stopping the audio. Knob positions, drawn curves, and saved state all carry across. That tiny gap between a change and hearing it is what makes building a device feel like sound design instead of programming.

Hot reload is for a device you're editing. A device you publish and share is frozen instead: see Distributing devices.

Why AssemblyScript

It compiles to WebAssembly, which is what lets a device run at native speed on the audio thread while staying sandboxed. And it's a cousin of TypeScript, so the code reads close to English and help is everywhere.