Skip to content

The session and your code

The session document describes the state of a musical session. It records tracks, device chains, routing, mixer values and arrangement bindings. Code remains an editor of that state. The mixer and routing streams use this same document model. Saving writes the document into the project. Opening restores a session from it, then runs the project’s entry file; see The entry file’s lifecycle and Save and open a session.

The document contains:

  • Track names, kinds, colours and order; outputs, sends, volume, pan, mute and solo.
  • Master volume, pan, output channels and effect chain.
  • Devices in chain order, identified by slot name, backend and device identity. CLAP IDs and VST3 class IDs identify plugins. Display names are labels.
  • Instrument identity, parameter target values, sampler kit and sample references.
  • Tempo, beats per cycle, time signature when representable, and transport position.
  • Named sections and timelines, their lengths, lane targets and layer order.

Positions and durations use integer ticks: 46,126,080 ticks per cycle. Saving and reading a third of a cycle therefore preserves its position exactly. The current language derives meter from beats per cycle, using the same spelling as MIDI export. Some cycle lengths cannot be represented as a time signature; those omit the signature and retain their exact beats-per-cycle value.

Meters, CPU readings, connected device names, numeric engine IDs and the play state are transient. They do not belong in the document. Opening a document must not start playback.

Patterns can contain generators and closures. A document stores their lane bindings and spans, not their contents. Evaluating the corresponding code reattaches the pattern through the same named track and arrangement binding. A document alone cannot reconstruct a generative pattern or a custom DSP body.

The session is the document. Code and the UI edit the same tracks, devices and arrangement. Creating a track in the Mixer does not require a matching declaration in the source. After saving, you can remove the code that created it. A chain statement explicitly replaces that chain; evaluating unrelated code leaves it alone.

The entry file is src/main.non, or the file entry names in resonon.toml. It has three parts, and each part runs at a different time:

PartIt runs onUse it for
fn setup()Open and Reset Project, and when your code calls setup();Tracks, with session.create_track(name), and settings that need no variable, such as set_bpm(120);
The top levelOpen, Reset Project and each Run File LiveImports, definitions, session.track(name), instruments, effects and patterns
fn teardown()Reset Project, RESET, opening another project, and closing or quittingWhat the engine cannot undo: hardware MIDI state, OSC, external files

setup() and teardown() are optional. Resonon calls them as it calls any other function, and gives them no other special treatment. main() has no special meaning: nothing calls it. An entry file that defines main() and does not call it gets the main_not_called warning.

Open and Reset Project do these steps in this order:

  1. Call teardown() of the current session, if it defines one. Resonon reports a failing teardown(). The failure does not stop the reset.
  2. Reset the session, as RESET does.
  3. Restore the saved session document from session/, if the project has one.
  4. Bind the function definitions and use imports of the entry file.
  5. Call setup(), if the entry file defines it.
  6. Run the rest of the top level, in order.

The transport stays stopped. Nothing plays until you press Play or Space. The undo history starts again.

The session owns every track: a track stays until it is deleted with session.delete_track(name), also when no variable holds it. For example:

use "std/instruments" { Sampler, Kit };
// Set up the piece. It runs when the project opens and on Reset Project.
// Create each track here, once: a name that exists is an error.
fn setup() {
set_bpm(120);
session.create_track("drums");
}
// The rest of the file runs after setup(), and again on each file run.
// session.track() looks up a track that setup() created.
let drums = session.track("drums");
drums.set_instrument(Sampler(Kit("TR-909")));
drums << [bd _ bd _, _ sd _ sd, ch*8];

session.create_track(name) makes a track, and a name that exists is an error: create each track once, in setup(). session.track(name) finds a track that exists, so the top level can run again on each file run. Explicit setters still write their values, so omit setters for settings you want to keep from the saved session.

ActionWhat it runsWhat it keepsUndo
Run Selection LiveThe selection, or the current lineThe running sessionNo undo step
Run File LiveThe whole entry file, without setup()The running session, the transport and the mixerNo undo step
Reset ProjectThe open sequence aboveOnly what the project’s files holdA new history

Run Selection Live is Command-Enter. Run File Live is Command-Shift-Enter. It saves the edited files first. A changed pattern starts on the next cycle. Correct a wrong run in code and run it again; session undo does not undo code execution.

Reset Project has no default key. Before it resets, it asks in two cases:

  • The session has changes since the last save, from UI edits or code. A reset loses those changes.
  • Tracks were renamed in the session after the code named them. Reset Project offers to update the old track names in the code.

Saving the session marks it as saved. A save is not an undo step. The keys and commands in each editor are in VSCode Extension, Neovim, Edit code in the window and Command Line.

A project that opens without its code is not the piece. Its patterns are silent, and the names its top level defines are undefined, so a selection that uses them fails. The open sequence runs the code, so the opened project plays as its files describe it, when you press Play.

A reset loses the changes since the last save. A key next to Command-Enter is too easy to press by mistake for that. So Command-Shift-Enter adds to the running session, and Reset Project is a menu command that asks first.

Code evaluations do not create session undo steps, including changes made before an evaluation fails. Correct the code and run it again. Every UI gesture is one step: a fader drag, a track deletion or a device move. Command-Z undoes the latest step; Command-Shift-Z redoes it, including while the code editor has focus. Use Control-Z and Control-Shift-Z for code text undo and redo. Single-line fields keep Command-Z for their own text. All connected clients share the session history.

Undo restores document state: tracks, devices, routing, mixer settings and arrangement. Retained tracks and devices bring their runtime bindings back. Existing tracks keep the patterns most recently assigned by code. Newer code writes take precedence over older UI edits. Undo does not rewind source text, variables, functions, callbacks, generator state, transport playback or position, window layout, saved files or external effects. A variable can therefore still refer to an object that undo removed; look up the current named track before making another edit. Callback activity is not an undo step. RESET, Open and Reset Project clear history; saving does not store it.

A track’s name identifies it across code, files and clients. An effect’s slot name identifies it within that track’s chain. The instrument has one address per track. A lane is identified by its named section or timeline, track and target; a layer adds its name to that address. A bound timeline held outside the top-level namespace is addressed by its track in track_timelines. None of these needs an unrelated generated ID. Recorded objects that have no code name can introduce their own stable IDs with their editing model.

Numeric audio IDs only connect a live streaming snapshot to meter readings. They are separate metadata and can change between processes without changing the saved session.

The format uses TOML. The project’s session/ directory contains session.toml for transport, master, arrangement and the ordered track index. Each track has its own file in tracks/. Editing one track changes its file without changing the index.

For a track named drums, the file is tracks/drums.toml. Names use reversible UTF-8 percent escaping for uppercase letters and punctuation. This avoids path separators and name collisions on case-insensitive filesystems. The original name remains the key in the track index.

Maps use deterministic key order. Chains, mixer order, layers and entries retain musical or authoring order. Text encoding adds no timestamps or process IDs. Serializing a document, reading it and serializing it again produces identical text files. Unknown fields, duplicate identities, missing references and invalid numeric values are errors rather than silently discarded state.

Opaque plugin state is raw binary in state/. A device’s state field references its sidecar. Explicit session capture gathers those bytes; ordinary streaming updates never call plugin save functions. Unsupported CLAP state extensions leave the state reference absent. A supported plugin whose state capture fails reports an error. Capture validates that every sidecar is referenced exactly once.

Sample and kit paths inside the project root are project-relative. External references retain absolute paths. Neither text encoding nor decoding copies media or executes code. Filesystem installation, restoration and missing-media handling belong to the save/open operation.

Opening rebuilds what the document can describe on its own: tracks, mixer, routing, tempo, built-in effects, samplers and plugins with their state. Built-in effects use their standard-library definition identity. Samplers restore their kit and sample references. What only code can define waits for it. A custom dsp device keeps its slot as a placeholder with its saved parameters, and a section or timeline stays in the document. When code places a device of the same identity in that slot, the new instance takes the saved values first. A plugin that is not installed keeps its slot and state the same way. Placeholders are presentation, not document state: the saved entry is written back unchanged.

Session restoration changes only the values, connections and objects that differ from the target document. Unchanged devices keep their live instances and keep playing. When a removed object is retained in memory, restoration reuses that instance and its pattern bindings before attempting to rebuild a device from saved state. Retained objects are released on the control thread.

Restoring live state preserves playback and the playhead position. Opening a project first resets the session, then restores its document and saved position, then runs the entry file. The project format does not store retained runtime objects.