Beautiful Bleep

Patch scripts

A patch script builds a cell of modules and cables. It runs once, when you press Compile, and the result is ordinary patch content: you can open the cell, change it by hand, cable to it, and save it. Where a module script describes one module that runs in real time, a patch script describes a patch, so loops and arithmetic are free.

import { cell } from "bleep/patch";

export default cell("Stack", { description: "Three oscillators into a filter." }, (c) => {
  c.input("pitch", "Pitch", "pitch");
  c.stereoOut();
  const filter = c.add("filter", { cutoff: 2000 });
  for (let i = 0; i < 3; i++) {
    const osc = c.add("oscillator", { coarse: i * 7, wave: "Sine", wave2: "Triangle", morph: 0.6 });
    c.cable(c.cin, "pitch", osc, "pitch");
    c.cable(osc, "out", filter, "l", 0.5);
  }
  c.stereo(filter, c.cout);
});

A script is recognised as a patch script if it has no @module and calls cell(. It must end in export default cell(...). The only import allowed is "bleep/patch" (and it is optional: cell is always there).

cell(name, options, build)

Argument Meaning
name The cell's name on the board
options.description A sentence shown under the editor
options.settings [{ id: "VOICES", name: "Oscillators", min: 1, max: 200, init: 100 }]: whole numbers the user can change, like module settings. id in capitals
build(c, s) Your function. c is the builder, s the settings' values (s.VOICES)

options can be left out: cell("Name", (c) => { … }).

The builder c

Call What it does
c.input(id, name, type) A shared input jack on the cell. Type: control (default), audio, pitch, gate, trigger or notes
c.output(id, name, type) A shared output jack. Type defaults to audio
c.stereoIn(), c.stereoOut() The jacks l and r (In L, In R / Out L, Out R)
c.add(type, values, opts) Adds a module and returns a handle
c.cable(from, fromJack, to, toJack, amount) A cable from an output to an input jack or to a modulatable knob. Amount −1 to +1, default 1
c.stereo(from, to, jacks) Both channels: from.l to to.l and from.r to to.r. jacks: { from: ["l","r"], to: ["l","r"], amount }
c.place(handle, x, y) Puts a module at a pixel position inside the cell
c.cin, c.cout The cell's own input and output sides, to cable from and to

c.add(type, values, opts)

  • type is the module's type id, the one used in the Add menu's search and in saved patches: "oscillator", "filter", "vca", "adsr", "lfo".
  • values is an object of knob values in their own units: { cutoff: 4000, resonance: 0.2 }, { fine: -12 }. Choices take the choice's name ({ mode: "LP" }) or its index; toggles take true or false. A value outside the knob's range, of the wrong kind, or for a knob that does not exist is an error.
  • opts: name (the module's name), id, at: [column, row] (a grid of 300 by 220 pixels), x and y in pixels, data (a module's node data).
  • Without a position, modules are laid out in a grid between Cell In and Cell Out.

Cables

c.cable(osc, "out", filter, "l", 0.5) reads: from the oscillator's output out to the filter's input l, at half strength. A cable can also end at a knob that cables can drive: c.cable(lfo, "out", filter, "cutoff", 0.3). Several cables into one input add up, which is how a hundred oscillators mix into one filter.

Notes connect only to notes; everything else connects freely. A cable to a jack or knob that does not exist is an error that names the module and the jack.

What compiling does

  1. The script runs in a sandbox and returns a description.
  2. The description is checked against the board's module types.
  3. If it is valid, the cell is built next to the Code node in one undoable step. The cell and everything in it are marked as generated by that node.
  4. Pressing Compile again replaces what was generated before. The cell keeps its position and any name you gave it. Cables you drew from outside to the cell's own jacks are kept as long as those jacks are still declared; cables you drew to modules inside the cell are lost, because the modules are rebuilt.

If the script has errors, nothing changes: the cell stays as it was and the problems are listed with their lines.

A patch script's own Code node has no jacks or knobs: the jacks are on the cell it built.

Limits

Limit
Modules added 1000
Cables added 5000
Source size 200,000 characters
Memory 64 MB
Run time 3 seconds
Result size 4 MB

A script that goes over a limit is stopped and the problem says which.

TypeScript or JavaScript

Write either: type annotations, interfaces and as casts are removed before the script runs. Error lines refer to your source.

Example: a chain of delays

export default cell("Echo chain", { settings: [{ id: "TAPS", name: "Taps", min: 1, max: 8, init: 4 }] }, (c, s) => {
  c.stereoIn();
  c.stereoOut();
  let previous = c.cin;
  for (let i = 0; i < s.TAPS; i++) {
    const tap = c.add("delay", { time: 0.12 * (i + 1) }, { name: "Tap " + (i + 1) });
    c.stereo(previous, tap);
    previous = tap;
  }
  c.stereo(previous, c.cout);
});

Look up the exact module and knob ids in the module reference or by adding the module on the board; a mistake is reported with the knobs that do exist.