Skip to main content

Using the Engine Directly

The @waveform-playlist/engine package is framework-agnostic — no React, Vue, or Svelte dependencies. It provides:

  • A stateful EditorEngine class with event-driven state management
  • An arrangement (createArrangement()): the owner of the tracks, the mix and the undo history, which an engine plays
  • A pluggable PlayoutAdapter interface for any audio backend
  • Pure functions for clip/timeline operations

Use the engine directly when building with Svelte, Vue, vanilla JS, or when you need headless timeline editing (e.g., server-side or testing).

Installation​

npm install @waveform-playlist/engine@next @waveform-playlist/core@next

The engine's only peer dependency is @waveform-playlist/core (data model types).

Quick Start​

import { EditorEngine } from '@waveform-playlist/engine';
import { createTrack, createClipFromSeconds } from '@waveform-playlist/core';

// 1. Create an engine (use AudioContext.sampleRate for the hardware rate)
const engine = new EditorEngine({
sampleRate: 48000, // Match your AudioContext.sampleRate
samplesPerPixel: 1024, // must be one of the zoom levels
});

// 2. Subscribe to state changes
engine.on('statechange', (state) => {
console.log('Tracks:', state.tracks.length);
console.log('Duration:', state.duration, 'seconds');
console.log('Playing:', state.isPlaying);
});

// 3. Add tracks
const track = createTrack({
name: 'Vocals',
clips: [
createClipFromSeconds({
audioBuffer: myAudioBuffer,
start: 0,
name: 'Intro',
}),
],
});

engine.setTracks([track]);

// 4. Control playback (requires a PlayoutAdapter — see below)
engine.play();
engine.pause();
engine.stop();

// 5. Clean up
engine.dispose();

:::tip Headless editing The adapter option is optional. Without an adapter, the engine handles all state management (tracks, selection, zoom, etc.) but playback methods are no-ops. This is useful for headless editing, testing, or building a custom audio layer. :::

EditorEngine​

Constructor Options​

const engine = new EditorEngine(options?: EditorEngineOptions);
OptionTypeDefaultDescription
adapterPlayoutAdapternullAudio playback backend
arrangementArrangementa new, empty oneThe arrangement to play, from createArrangement(). See The Arrangement
sampleRatenumber48000Audio sample rate (use AudioContext.sampleRate for hardware rate). Throws for a value that is not a positive finite number; undefined or null takes the default
samplesPerPixelnumber1024Initial zoom level. Must be one of zoomLevels, or the constructor throws
zoomLevelsnumber[][256, 512, 1024, 2048, 4096, 8192]Available zoom steps (samples per pixel)
undoLimitnumber100Maximum number of undo steps. Only when the engine makes its own arrangement; with arrangement it is ignored, with a console warning
attachDecodeContextbooleantrueWith an adapter, the engine gives its arrangement adapter.audioContext to decode audio with and takes it away in dispose(). false: the host attaches a context itself. See Loading Audio
bpmnumber120Initial tempo. Throws for a value that is not a positive finite number; undefined or null takes the default
ppqnnumber960Pulses per quarter note without an adapter, or when the adapter's is invalid. A valid adapter.ppqn is used before it. Either one that is given but is not a whole number of 1 or more is ignored with a console warning: the next valid one is used, else 960. undefined or null is passed over without a warning

State Snapshot​

Call getState() to get the engine state as one object:

const state = engine.getState();

The returned EngineState contains:

Tracks

FieldTypeDescription
tracksreadonly ClipTrack[]The tracks and their clips, each track with its current volume, mute, solo, pan and pan law. Not a copy: read it, never write to it
tracksVersionnumberMonotonic counter — increments on structural track changes only
mixerVersionnumberMonotonic counter — increments on every change of the current mix: a per-track mixer edit (volume/mute/solo/pan/pan law), a track's first decoded audio deciding its pan law, an undo, a redo or an aborted transaction that brings back other values, and the start or end of a take's mute on a track that is in the current tracks
durationnumberTotal timeline duration in seconds

Playback

FieldTypeDescription
currentTimenumberCurrent playhead position in seconds
isPlayingbooleanWhether playback is active

Zoom

FieldTypeDescription
samplesPerPixelnumberCurrent zoom level
sampleRatenumberAudio sample rate
zoomIndexnumberIndex into the zoomLevels array
canZoomInbooleanWhether further zoom in is possible
canZoomOutbooleanWhether further zoom out is possible

Selection & Loop

FieldTypeDescription
selectedTrackIdstring | nullCurrently selected track
selectionStartnumberStart of selection range (guaranteed <= selectionEnd)
selectionEndnumberEnd of selection range
loopStartnumberStart of loop region (guaranteed <= loopEnd)
loopEndnumberEnd of loop region
isLoopEnabledbooleanWhether loop playback is active

Volume

FieldTypeDescription
masterVolumenumberMaster output volume: linear gain, 0 or more (default 1)

Tempo & History

FieldTypeDescription
bpmnumberCurrent base tempo
ppqnnumberPulses per quarter note
canUndobooleanWhether there is a step to undo
canRedobooleanWhether there is a step to redo

:::warning tracks is shared, not copied getState() returns a new object on every call, but state.tracks is the engine's own array, shared with the undo history. It is the same array until the tracks or the mix change. Never write to the array, to a track or to a clip in it: make a copy and hand the copy to updateTrack(). Outside production builds the array, each track, its clips array and each clip are frozen, so a write throws. See Shared, Read-Only Tracks.

setTracks(), addTrack() and updateTrack() copy the tracks they are given, so the objects you pass in stay yours. :::

Track Management​

// Replace all tracks (clears the undo history and ends an open transaction)
engine.setTracks(tracks: readonly ClipTrack[]): void;

// Add a track: at the end, or at `index` in the order (cut to a whole number,
// held between 0 and the number of tracks; an index that is not a finite
// number throws a TypeError and changes nothing). Not an undo step unless
// `undoable: true` (see "What you add is not an undo step" below)
engine.addTrack(track: ClipTrack, options?: AddTrackOptions): void;

// Remove by ID (no-op if not found)
engine.removeTrack(trackId: string): void;

// With a track: replace the track that has this ID (no-op if no track has it).
// Without one: send the current track to the adapter again.
engine.updateTrack(trackId: string, track?: ClipTrack): void;

// Move a track to another place in the order (not audible; the adapter is not called).
// toIndex is an integer: one outside the order is the first or the last place;
// anything else (NaN, an infinity, a fraction, a string) throws a TypeError.
engine.reorderTrack(trackId: string, toIndex: number): void;

// Select a track (no-op if already selected)
engine.selectTrack(trackId: string | null): void;

setTracks(), addTrack() and updateTrack(id, track) check the tracks before they change anything, and throw an Error for:

  • a track whose clips do not match its kind;
  • a track ID that another track has;
  • two clips with the same ID on one track (the same clip ID on two different tracks is accepted);
  • clips on one track that overlap (clips that touch are accepted; findClipOverlap(clips) names the first two that overlap).

They also throw for a number that cannot be played, a TypeError for a value that is not a finite number and a RangeError for one out of range: a track's volume (0 or more) and pan (-1 to 1), and each loaded clip's start, offset, sourceDuration, gain and startTick (0 or more) and duration (above 0); a fadeIn or fadeOut must be an object (else a TypeError) with a duration of 0 or more. A clip also needs a length on the clip-time grid (its end at least one unit, about 3.5 ns, past its start) and a start, an offset, an end (start + duration) and an end in the source (offset + duration) under 2^22 seconds (4,194,304 s, about 48 days; core's CLIP_TIME_LIMIT_SECONDS): outside either is a RangeError. A move or a trim stops a grid unit before that limit, as it stops at 0 and at a neighbour, and a tempo change that would move a clip there throws a RangeError before anything changes, so every edit of a valid clip, a split and a recording's carve included, gives valid clips. The one gap is a tempo at a tick with an adapter that has ticksToSeconds and no previewTempo (the PlayoutAdapter method that gives the conversion after a change): it is not checked. An optional field (volume, pan, gain, sourceDuration, startTick, fadeIn, fadeOut) that is undefined or null was not given: the default applies, and a null fade is no fade. A missing or null start, offset or duration throws a TypeError, which is also what a clip in the older shape gets (sample counts and a sampleRate, no start): its old fields are not read. A clip, loaded or loading, that gives a value for startSample, durationSamples, offsetSamples or sourceDurationSamples is refused with a TypeError too, also when it has its times in seconds beside them (… startSample is no longer a clip field; give start, offset and duration in seconds). A clip that names a src and has neither an audioBuffer nor midiNotes is a loading clip (isLoadingClip), also when it carries waveformData: it is loaded from its src, and its start, offset and duration are checked when the audio arrives. A loaded clip without gain or sourceDuration gets a gain of 1 and its source's length (the buffer's duration, else the waveform data's), else offset + duration. A loading clip's numbers are checked when its audio arrives (see Loading Audio).

A track's volume, muted, soloed, pan and panLaw fields are the mix it enters with (a track without a panLaw takes the law of the first decoded audio it holds, and keeps it). They are read when its ID has no mixer entry yet. setTracks() keeps the mix of an ID that is in the tracks it replaces; updateTrack(id, track) keeps the track's mix, also when the new track has another ID. In those cases the fields are not read (a volume or pan that cannot be played still throws). addTrack() reads them, also for the ID of a track removed earlier. Use the volume methods to change the mix.

The mix holds an entry for each current track; each undo and redo step keeps the mix it had, so a removed track's entry is in the step from before the removal and comes back with it.

updateTrack(id, track) calls the adapter only when the track plays differently from before. A track whose loaded clips are the same objects in the same order and whose own fields are unchanged, apart from the view fields (TRACK_VIEW_FIELDS: name, colour, height, render mode, spectrogram config and colour map), is not sent; the undo step and the statechange are the same either way. Clips are compared by identity: a clip object this track already holds (one taken from this track in getState().tracks or arrangement.getTracks()) is kept as it is, and any other clip object, also one held by another track, is copied and counts as changed.

updateTrack(id, track) may give the track an ID no other track has. The track keeps its mix, and the adapter receives every track through setTracks.

What you add is not an undo step​

addTrack() makes no undo step unless you pass { undoable: true }. A track your app loads is part of its content, like a project a DAW opens: no undo, redo or aborted transaction takes it out. The track is written into every undo and redo state, and into the base of an open transaction, and the redo steps are kept. In an older state it goes after the nearest track before it (in the current order) that the state holds, or first when the state holds none of them, with its own mix. An older state that holds another track of the same ID (one the user removed) loses that track first, and a step that then changes nothing is dropped. That includes a track your app renamed away from the ID (updateTrack with a new id): an undo back past the rename then has neither the renamed track nor the old one (the new track stays), so do not reuse an ID you renamed away from.

Pass { undoable: true } when the add is the user's own edit, such as a dropped file or a "new track" button: it is then one undo step. For several tracks of one gesture (one drop of three files), make them one step with a transaction:

engine.beginTransaction();
for (const track of droppedTracks) engine.addTrack(track, { undoable: true });
engine.commitTransaction();

removeTrack() is an undo step either way.

selectedTrackId becomes null when its track goes away: by removeTrack(), by an undo, a redo or an aborted transaction that takes the track away, or when updateTrack() gives the track a new ID. setTracks() leaves it alone, also when the new tracks do not include it: a host may empty the tracks and add them again.

Clip Editing​

All clip operations take seconds, the unit a clip's times are in:

// delta and at must be finite numbers: any other value throws a
// TypeError and changes nothing.

// Move a clip by delta seconds (constrained to prevent overlaps).
// Returns the delta actually applied (0 when the clip cannot move).
engine.moveClip(trackId: string, clipId: string, delta: number, skipAdapter?: boolean): number;

// Trim a clip boundary by delta seconds
engine.trimClip(
trackId: string,
clipId: string,
boundary: 'left' | 'right',
delta: number,
skipAdapter?: boolean
): void;

// Split a clip at a time on the timeline, in seconds
engine.splitClip(trackId: string, clipId: string, at: number): void;

A split keeps the clip's id on the left half and gives the right half a new one, so whatever names the clip (a selection, an element, a cache keyed by id) follows its left half. An undo brings the whole clip back under the same id.

A MIDI clip splits the same way. Both halves keep the clip's whole midiNotes array, its midiChannel and its midiProgram; as with audio, each half is a window on that source (offset and duration). The piano roll draws only the notes inside a clip's window, and the Tone adapter (@waveform-playlist/playout, the adapter that plays MIDI) plays only those. A note that crosses the split is played by both halves: the left half releases it at the split and the right half strikes it again there. A SoundFont sample that does not loop (a piano, a drum) rings for its whole length, also past the split.

All three methods:

  • Validate track and clip IDs (warn to console if not found)
  • Apply collision constraints automatically (no overlaps)
  • Enforce minimum clip duration (0.1 seconds)
  • Increment tracksVersion and emit statechange
  • Are undo steps

With skipAdapter set to true, moveClip() and trimClip() change the state and emit statechange without calling the adapter. It is for a custom drag that writes each pointer move: send the track once on drop with engine.updateTrack(trackId). The library's own drags (<daw-editor>, the React editor) draw a preview and call moveClip() / trimClip() once on release.

Playback​

// Start playback (optionally from a time position)
engine.play(startTime?: number, endTime?: number): void;

// Pause at current position
engine.pause(): void;

// Stop and return to the position playback started from
engine.stop(): void;

// Seek to a time position (any non-negative value — plays silence beyond audio)
engine.seek(time: number): void;

:::note Adapter required Playback methods delegate to the PlayoutAdapter. Without an adapter, play() still updates isPlaying state and emits events, but no audio plays. :::

Selection & Loop​

// Set selection range (automatically normalizes start <= end)
engine.setSelection(start: number, end: number): void;

// Set loop region (automatically normalizes start <= end)
engine.setLoopRegion(start: number, end: number): void;

// Toggle loop mode
engine.setLoopEnabled(enabled: boolean): void;

Zoom​

// Step zoom in (lower samplesPerPixel = more detail)
engine.zoomIn(): void;

// Step zoom out
engine.zoomOut(): void;

// Jump to a specific zoom level (snaps to closest available level)
engine.setZoomLevel(samplesPerPixel: number): void;

Volume​

// Master output volume: linear gain, finite and 0 or more (above 1 boosts).
// Any other value throws (a TypeError, or a RangeError below 0) and changes nothing.
engine.setMasterVolume(volume: number): void;

// Per-track mix: each call is an undo step (see Undo & Redo).
// volume: finite, 0 or more. pan: finite, -1 to 1. A non-number or a non-finite value
// throws a TypeError, one out of range a RangeError, before anything changes.
engine.setTrackVolume(trackId: string, volume: number, options?: MixerChangeOptions): void;
engine.setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
engine.setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
engine.setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
// The pan law: 'mono' or 'stereo'. Any other value, 'auto' included, throws a TypeError
engine.setTrackPanLaw(trackId: string, panLaw: 'mono' | 'stereo', options?: MixerChangeOptions): void;

interface MixerChangeOptions {
/** Changes with the same id, one after another, are one undo step. */
gesture?: string;
/** Not an undo step: the value is written into every saved state as well. */
undoable?: false;
}

// The take's mute: the track a take records onto plays muted; not an undo step
engine.setTrackRecording(trackId: string, recording: boolean): void;
engine.recordingTrackIds: ReadonlySet<string>; // read-only: the arrangement's set

Each per-track method stores the value in the arrangement's mix, calls the adapter, increments mixerVersion and emits statechange; the next state.tracks carries the value in that track's field. Each call is an undo step, unless its options say otherwise (Mixer moves). They act on a track that is in the current tracks, and on a value that changes. For any other track ID (one the engine has never seen, or a removed track) and for the value the track already has, no state changes, no undo step is made and no statechange is emitted, but the adapter method is still called.

setTrackRecording() marks the track a take is recording onto, and clears it when the take ends. That track plays muted for the take: state.tracks and the adapter have it muted, while its mute in arrangement.getMixer() stays as it is and comes back when the take ends (arrangement.recordingTrackIds holds the IDs, and engine.recordingTrackIds is the same set: use it to disable that track's mute control). A mute set during the take is stored and heard after it. It is not an undo step, and undo, redo, an aborted transaction, setTracks() and clearHistory() do not change which tracks are recording. An ID that is not a current track can be set and mutes nothing until a track of that ID enters. It is not a record-arm: <daw-editor> sets it for the length of a take.

Undo & Redo​

engine.undo(): void;
engine.redo(): void;
engine.canUndo; // boolean, also in EngineState
engine.canRedo;
engine.clearHistory(): void;

// Group several changes into one undo step (a drag)
engine.beginTransaction(): void;
engine.commitTransaction(): void;
engine.abortTransaction(): void; // put the tracks and the mix back as they were at beginTransaction()
  • Undo steps are made by addTrack(track, { undoable: true }), removeTrack(), updateTrack(id, track), reorderTrack(), moveClip(), trimClip(), splitClip(), setTrackVolume(), setTrackMute(), setTrackSolo(), setTrackPan() and setTrackPanLaw(). setTracks() clears the history. addTrack() without { undoable: true }, setTrackRecording(), setTempo(), the master volume, selection, loop and zoom are not undo steps (What you add is not an undo step).
  • Undo, redo and an aborted transaction restore the tracks, the clips and the mix of the state they return to. Undoing a removeTrack() brings the track back with the mix it had; undoing an updateTrack() that gave a track a new ID brings back the old ID with the mix it had then. An undoable mixer change made inside a transaction is part of it, and an abort takes it back; one made with { undoable: false } is written into the transaction's base as well, so an abort keeps it. An undo that changes only the mix increments mixerVersion, not tracksVersion.
  • undo() and redo() called while a transaction is open abort the transaction first.
  • commitTransaction() adds one undo step and emits statechange when the transaction changed something. When the tracks and the mix are back where the transaction began (the same track objects in the same order and the same mixer values; for example a track added during the transaction and then taken out with the arrangement's removeTrack(id, { undoable: false }). A clip moved away and back is not that: the clip is a new object), it adds no step and emits nothing. clearHistory() emits statechange when there was history. So canUndo and canRedo in the state are current.
  • After an undo, a redo or an aborted transaction the adapter receives only the tracks that differ when the same track IDs are there before and after: its updateTrack for each, or, when the adapter has no updateTrack, one setTracks per differing track. When the set of IDs differs it receives one setTracks. An undo of a reorder calls no adapter method. A mixer value that differs reaches the adapter through its setter (setTrackVolume and so on), because a track call does not carry the mix to every adapter; an undo that changes only the mix calls the setters and no track method.

Mixer moves​

By default each mixer call is an undo step of its own. A fader drag makes many calls, so pass the same gesture ID for all of them: calls with one ID, one after another, are one step. Another undo step, an undo, a redo, clearHistory(), setTracks(), a transaction, or a removeTrack() or removeClip() with { undoable: false } that takes something out of the tracks or of a saved state, in between ends that step, and the next call with the ID makes a new one; a take's mute (setTrackRecording()), a call that changes nothing and an { undoable: false } call do not (unless the undoable: false call makes the gesture's step equal to the current state, which drops the step; one that makes the step below equal to the gesture's step leaves the gesture going). When a gesture ends where its step began, the step is dropped and the history it found comes back: the redo states its first call cleared, and a step the undo limit evicted for it. { undoable: false } makes no step: the value is written into every undo and redo state as well, so no undo or redo brings back an older one; it is for values that are not the user's edit, such as automation, and it wins over gesture.

createMixerGesture(), from @waveform-playlist/core, makes the IDs for one control. Use one per slider:

import { createMixerGesture } from '@waveform-playlist/core';

const gesture = createMixerGesture();
const detach = gesture.attach(slider); // ends the gesture on the slider's own events
slider.addEventListener('input', () => {
engine.setTrackVolume(trackId, slider.valueAsNumber, { gesture: gesture.id });
});

gesture.end(); // ends the gesture by hand: the next call starts a new step
detach(); // removes the listeners attach added

attach(input) ends the gesture on the input's change event (the end of a pointer drag), except while an arrow, Page Up, Page Down, Home or End key is held down: then on that key's keyup, so a held key that repeats is one step. A blur ends it too. Mute and solo buttons need no helper: each click is one step. The IDs are unique per helper, also across two installed copies of the package.

Events​

engine.on(event, listener): void;
engine.off(event, listener): void;
EventListener SignatureWhen
statechange(state: EngineState) => voidAny state change
play() => voidPlayback starts
pause() => voidPlayback pauses
stop() => voidPlayback stops

The statechange event fires on every change (tracks, mix, undo history, selection, zoom, volume, playback state), after the engine has made its adapter call for that change. A change made from inside a statechange listener is delivered after the one being handled has reached every listener, so all listeners see the changes in the order they were made. One track edit made from inside a listener is still the current state when it is delivered, and the adapter gets the call for that edit (updateTrack, addTrack or removeTrack). Two or more track edits made in one listener run are different: the first is delivered after the tracks have changed again, so the engine sends the adapter every track once (setTracks, a full rebuild that can interrupt playback), and the edits still waiting make no adapter call. A reorder and a move made with skipAdapter count as track edits here, although neither needs an adapter call of its own. When a rebuild matters, make several track edits from an event handler, not from inside a statechange or subscribe listener (a transaction does not change this: it groups undo steps, not adapter calls). After an adapter call for a track change has thrown, the next track change also sends every track. If the adapter throws while a track change is applied, that change emits no statechange and the error reaches the caller of the engine method; for a change made from inside a listener, that is the caller of the outer method, the one whose statechange the listener was handling. Use the tracksVersion field to distinguish structural track changes, and mixerVersion to distinguish per-track mixer edits (volume/mute/solo/pan), from other state updates.

Cleanup​

engine.dispose(options?: { keepAdapter?: boolean }): void;

Disposes the adapter, clears all event listeners, stops following the arrangement and takes back the audio context it gave the arrangement to decode with (loads in flight are aborted and stay pending). Safe to call multiple times. An arrangement you passed in is not cleared: see The Arrangement.

With { keepAdapter: true } the adapter is not disposed: playback is stopped first (as stop() does, with its stop event), and the engine lets go of the adapter's onPlaybackEnded. The adapter is emptied (setTracks([])), so the tracks of this engine stop playing through it even when the next arrangement has none (an empty arrangement sends nothing). A new engine can then be built with the same adapter on another arrangement; its constructor sends that arrangement's tracks to the adapter. <daw-editor> does this when editor.arrangement is set.

The Arrangement​

An arrangement is the owner of three things: the tracks with their clips, the mix (volume, mute, solo, pan and pan law by track ID), and the undo history with its transactions. Everything else reads it and changes it by calling its methods. An EditorEngine plays an arrangement: it follows it by subscription, and for each change it makes the adapter call and emits statechange. The engine owns the rest: transport, selection, loop, zoom, the selected track and the master volume.

An engine built without the arrangement option makes an arrangement of its own, and every example above works that way. Make the arrangement yourself when it has to exist before the engine does, or outlive it:

import { createArrangement, EditorEngine } from '@waveform-playlist/engine';

const arrangement = createArrangement({
tracks: [track], // the first tracks; not an undo step
undoLimit: 50, // default 100; read back as arrangement.undoLimit
});

const engine = new EditorEngine({ adapter, arrangement });

Reading​

// The tracks and their clips. The same array until they change.
arrangement.getTracks(); // readonly ArrangementTrack[]: clips are AudioClip | LoadingClip

// The current mix by track ID. The same Map until a value changes, an entry is added or entries are dropped.
arrangement.getMixer(); // ReadonlyMap<string, TrackMixerValues>
arrangement.getMixer().get(trackId); // { volume, muted, soloed, pan } | undefined

// Told once per change (tracks, mix or history), in the order the changes were made.
const unsubscribe = arrangement.subscribe(() => {
render(arrangement.getTracks(), arrangement.getMixer());
});

arrangement.tracksVersion; // counts changes to the tracks
arrangement.mixerVersion; // counts the changes of the current mix (the setters, an undo of the mix, a take's mute)
arrangement.canUndo;
arrangement.canRedo;

getTracks() holds each track as it entered: its volume, muted, soloed, pan and panLaw fields are not updated by the mixer setters. The current values are in getMixer(). (engine.getState().tracks is the same tracks with the current mix written into those fields and without the clips that are still loading.)

A subscriber is called with { type, removedClips? }: the kind of change it was (ArrangementChangeType in the type reference) and, when the change took clips out by itself because nothing of their audio was left after their offset, those clips ({ trackId, clipId }). It reads what changed from the arrangement itself. A listener that takes no argument works as well. It is called after the engine has made its adapter call for that change. Two changes reach subscribers without the adapter being in step: one made with skipAdapter, and the startTick assignment when an engine attaches, which subscribers hear before that engine sends its first tracks to its adapter. getMixer() is also replaced, without a change of mixerVersion, when a track with a new ID enters or entries are dropped, and an undo, a redo or an abort replaces it with the mix of the state it returns to. A subscriber that throws is reported with console.warn and does not fail the change. getTracks() and getMixer() return the same object until it changes, so a subscriber can tell what changed by comparing with what it read last time.

Make track changes from an event handler (or a listener of arrangement.sources), not from inside a subscribe() or statechange listener: two or more track edits made inside one listener run make an engine send its adapter every track at once (setTracks), which can interrupt playback (see Events). The same rule holds under the React provider: see Loading Audio.

Changing​

The arrangement has the engine's track, clip, mixer, recording and history methods: setTracks, addTrack, removeTrack, updateTrack, reorderTrack, moveClip, trimClip, splitClip, getClipBounds, constrainTrimDelta, setTrackVolume, setTrackMute, setTrackSolo, setTrackPan, setTrackPanLaw (each with the same options), setTrackRecording, undo, redo, clearHistory, beginTransaction, commitTransaction and abortTransaction, and it tells you whether a transaction is open (inTransaction: code that groups its own changes into one step joins an open transaction rather than opening one, which would replace it). The engine's methods call them, so a change made through either reaches the adapter and emits statechange.

Besides loading audio, four things are on the arrangement only:

// Add a clip at the end of a track's clips: not an undo step unless { undoable: true }
arrangement.addClip(trackId, clip);
arrangement.addClip(trackId, clip, { undoable: true });

// Remove a track for good: no undo step, and no undo or redo brings it back
arrangement.removeTrack(trackId, { undoable: false });

// Remove a clip: an undo step; with { undoable: false }, for good, as above
arrangement.removeClip(trackId, clipId);
arrangement.removeClip(trackId, clipId, { undoable: false });

// Replace a track with a function of the current one (not called for an unknown ID)
arrangement.updateTrack(trackId, (track) => ({ ...track, name: 'Bass' }));

addClip() checks the track with the clip added as updateTrack() checks a track: a clip of the other kind, a clip ID the track already holds, or a clip that overlaps one of the track's throws an Error and changes nothing, and a loaded clip whose numbers cannot be played throws a TypeError or RangeError. For an ID no track has it changes nothing; a loaded clip whose numbers cannot be played throws there too. Without { undoable: true } it is added like a track that is not an undo step: the clip is written into every undo and redo state that holds the track, except a state where it would overlap one of that track's clips there (an undo of the edit that made room for it takes it out, and a redo brings it back), and an older clip of the same ID in that track is taken out of the saved states first. With { undoable: true } it is one undo step. The engine has no addClip().

updateTrack(), given a track or a function, keeps each clip object the track already holds and copies every other clip. The clips you did not replace are therefore the same objects afterwards.

removeTrack(trackId, { undoable: false }) is for a track that should never have been there, such as a dropped file that could not be decoded. The track is taken out of the current tracks and out of every undo and redo state, and an undo step that no longer changes anything is dropped. Its mixer values go with it.

removeClip(trackId, clipId, { undoable: false }) does the same for one clip, in every state that holds its track. A track that, without the clip, is the track as an earlier state holds it becomes that object, so the step that only added the clip is dropped too and no undo or redo is left that changes nothing. <daw-editor> uses it for a <daw-clip> the page removes: what the page removes is gone for good. Without the option, removeClip is an undo step; nothing happens when the current tracks do not hold the clip.

The arrangement's moveClip() and trimClip() have no skipAdapter argument; that one is the engine's.

Loading Audio​

A clip of an arrangement may name its audio instead of carrying it. The arrangement fetches and decodes each source once, reports where each one is, and replaces the clip with a loaded one when the audio arrives.

An arrangement's tracks are ArrangementTrack (from @waveform-playlist/core): each clip is an AudioClip (loaded) or a LoadingClip (a src and times in seconds; the audio is not loaded yet). isLoadingClip(clip) tells them apart. createArrangement({ tracks }), getTracks(), setTracks(), addTrack() and updateTrack() of the arrangement use that type. The engine's own setTracks(), addTrack() and updateTrack() still take ClipTrack, whose clips are all loaded.

audioTrack() from core builds a track with one clip. With src and no audioBuffer that clip is a LoadingClip:

import { createArrangement, EditorEngine } from '@waveform-playlist/engine';
import { audioTrack, isLoadingClip } from '@waveform-playlist/core';

const arrangement = createArrangement({
tracks: [
audioTrack({ id: 'guitar', src: '/audio/guitar.opus' }),
audioTrack({ id: 'vocals', src: '/audio/vocals.opus', start: 4 }), // seconds
],
});

// Nothing is fetched yet: there is no audio context to decode with.
arrangement.sources.getSnapshot().pendingCount; // 2

// An engine with an adapter attaches adapter.audioContext, and the loads start.
const engine = new EditorEngine({ adapter, arrangement });

// Where each source is
const stop = arrangement.sources.subscribe(({ sources, isLoading }) => {
showSpinner(isLoading);
for (const [src, state] of sources) {
if (state.status === 'failed') showRetryButton(src, state.error); // state.error is an Error
}
});

// The tracks: a loading clip's start, duration and offset may be undefined; narrow with isLoadingClip
arrangement.subscribe(() => {
for (const track of arrangement.getTracks()) {
for (const clip of track.clips) {
if (isLoadingClip(clip)) {
drawPlaceholder(clip.start ?? 0, clip.duration); // seconds; duration may be undefined
} else {
drawClip(clip.start, clip.duration); // seconds
}
}
}
});

// A source whose load failed is started again only by this
arrangement.retry('/audio/guitar.opus');

A src is a URL string, a Blob or a File. A string is one source however many clips name it; a Blob or File is a source by identity.

What a loading clip is, and is not​

  • It is copied on its way in, stays the same object through every operation on its track, keeps its place among the track's clips and is part of the undo steps. It has no startTick.
  • It cannot be edited. moveClip(), trimClip() and splitClip() on it log a console warning (… is still loading and cannot be edited until its audio has loaded) and change nothing; getClipBounds() returns null and constrainTrimDelta() returns 0. A move or a trim of a loaded clip stops at its span: at its start or its end when its length is known (a duration, or waveformData); when it is not, the clip holds its start (one unit of the clip-time grid, a few nanoseconds), which nothing may cover.
  • It is an audio clip, whatever else it carries. createArrangement({ tracks }), setTracks(), addTrack() and updateTrack() throw for a kind: 'midi' track that holds one, with the same error as for a MIDI track that holds a loaded audio clip, and change nothing.
  • An engine neither plays nor reports it. The tracks an adapter is sent and getState().tracks are each track without its loading clips, and getState().duration counts loaded clips only. Read arrangement.getTracks() to draw the clips that are still loading.
  • A change that leaves a track's loaded clips (the same objects in the same order) and its own fields other than the view fields as they were makes no adapter call for that track: adding, replacing or removing a loading clip, a change of a view field, and an undo or a redo of those. The statechange is still emitted. engine.updateTrack(id) with no track always sends.

Which sources are loaded​

Every source a loading clip names in the current tracks, in an undo or redo state, in the base of an open transaction, or in a state that an open mixer gesture's first change let go (the redo steps it cleared, or the oldest undo step at the undo limit): a gesture that returns to its start value brings them back, and they are let go when the gesture ends (an undo, a redo, a transaction, another undoable change, clearHistory(), setTracks(); a mixer change with undoable: false or a load arriving can leave it open). An undo therefore never fetches a source again. When no state names a source any more (the track was removed and the history cleared, an edit cleared the redo steps that named it, or setTracks() replaced everything), its load is aborted and its audio is let go; a clip that names it later starts a new fetch.

A loaded clip that carries both src and audioBuffer (what audioTrack({ src, audioBuffer }) builds) counts as well: its buffer is used for that source, nothing is fetched, and a loading clip of the same src is loaded from it. A loaded clip with src and no audioBuffer is not fetched and does not appear in sources: it never gets audio from the arrangement.

When the audio arrives​

Every loading clip that names the source becomes an AudioClip:

  • It keeps its start, offset and duration in seconds. Without a duration the clip runs from offset to the end of the audio, and its sourceDuration is the buffer's duration.
  • The clip keeps its id, and its name, color, gain, fadeIn, fadeOut, waveformData and src.
  • A duration that runs past the end of the audio is cut to it, with one console warning when it is more than one sample of the buffer past the end.
  • It gets a startTick when an engine is attached; without one, when the next engine attaches.

This happens in the current tracks and in every undo and redo state, so an undo brings back a loaded clip. It is not an undo step. When the current tracks changed, subscribers are told once and tracksVersion goes up by one; an engine sends each track that gained a clip to its adapter (updateTrack, or setTracks for an adapter without it) and emits one statechange. When only undo or redo states changed, nobody is told except the subscribers of sources.

A clip added later that names a source that is already loaded is a loaded clip when the call that added it returns. A clip that comes in carrying src and audioBuffer for a source other loading clips are waiting for loads those clips inside the same call: one notification and one statechange, and the adapter is also sent each other track that gained a clip.

If the adapter throws while an engine applies audio that arrived, there is no caller to fail. The engine logs [waveform-playlist] The playout adapter failed while loaded audio was applied: with the error, still emits the statechange, and sends the adapter every track at the next track change. For every other change an adapter that throws fails the call that made the change, and no statechange is emitted for it.

arrangement.sources and retry()​

sources is a ValueSource: getSnapshot() returns the current value and subscribe(listener) calls the listener with each new one (the shape React's useSyncExternalStore takes). The value:

interface ArrangementSourcesValue {
sources: ReadonlyMap<ClipSource, ArrangementSourceState>;
pendingCount: number;
failedCount: number;
isLoading: boolean; // true while a source is pending
}

interface ArrangementSourceState {
status: 'pending' | 'loaded' | 'failed';
error?: Error; // when failed
}

It is the same object until a source is added or dropped, a status changes, or a failed source gets another error. A new value is published after the subscribers of arrangement.subscribe() have been told about the change it belongs to, so a listener of sources that sees loaded finds the loaded clip in getTracks(). The other way round, a listener of arrangement.subscribe() that finds the loaded clip still reads its source as pending in sources. The order of the entries of sources means nothing.

A source is failed in two cases:

CaseWhat the tracks showerrorHow it ends
The load failed: the fetch, the decode, or getting the audio contextNo track changes; the clips stay loading clipsWhat the loader rejected witharrangement.retry(src) starts the load again. Nothing else does
A clip in the current tracks cannot be made from the audio: its start, offset, duration or gain is not a usable number, or a fadeIn or fadeOut is not an object with a usable duration (a null gain or fade is not given)That clip stays a loading clip; other clips of the same source are loadedAn Error with the message clip "<id>": <reason>When that clip is no longer in the current tracks. retry() does nothing
Nothing of a clip's audio is left after its offset (the offset is at or past the end of the audio)That clip is removed from every state that holds it, with a console warning; not an undo step, and a step that only added it is droppedNone: the source is not failed (loaded while another clip names it; otherwise it leaves sources)Nothing to do

For the second case, replace or remove the clip with updateTrack(): the failure is cleared at once. Only clips in the current tracks count, so an undo step that still holds the clip does not keep the failure, and an undo that brings the clip back brings the failure back.

retry(src) does nothing for a source that is pending or loaded, and for one no clip names.

How a source is loaded​

createArrangement({ loadSource }) replaces how a source becomes an AudioBuffer. The default, exported as defaultLoadSource, fetches a string (or reads a Blob or File with arrayBuffer()) and decodes it with audioContext.decodeAudioData(). It checks the abort signal before it starts and after each step, so a cancelled load neither decodes nor resolves. A decoded source is held whole: about 1.38 GB for an hour of stereo audio at 48 kHz, and two to three times that while it decodes, more for an uncompressed file in Chrome and Safari (the React loading guide, Memory Use of Decoded Audio).

import { createArrangement, defaultLoadSource } from '@waveform-playlist/engine';
import type { LoadSource } from '@waveform-playlist/engine';

const loadSource: LoadSource = async (src, context) => {
// Pass the whole context on: it carries `stallTimeout`, which defaultLoadSource
// applies to a URL (a Blob is not timed). The fetch below is not under that limit.
if (typeof src !== 'string') return defaultLoadSource(src, context);
const { signal, audioContext } = context;
const response = await fetch(src, { signal, credentials: 'include' });
if (!response.ok) throw new Error(`Failed to fetch audio: ${response.status}`);
return audioContext.decodeAudioData(await response.arrayBuffer());
};

const arrangement = createArrangement({ loadSource });

A loadSource of your own should pass signal to what it awaits: the arrangement aborts a load when its source is no longer named and when the audio context is taken away.

The error messages defaultLoadSource writes itself:

MessageWhen
Failed to fetch <source>: <status> <statusText>The response is not ok
Failed to fetch <source>fetch threw a TypeError (a network failure, a refused cross-origin request). The TypeError is in the error's cause when the source is named unchanged (see below)
No data arrived from <source> for <n> sNo response had started, or no new bytes of the body had arrived, for the stall limit (an Error named TimeoutError, see below)
Decoded audio is empty: <source>The decoded buffer has no samples
Failed to decode audio: <source>The decoder rejected with something that is not an Error

In these, <source> is the URL without its query string, its fragment and a user name or password before the host (each can hold a secret). A URL in which the browser's URL parser finds a user name or password (also in forms such as one slash after the scheme, backslashes, or white space in front) is named in its parsed, absolute form without them. A malformed URL that the parser refuses loses only its query string and fragment. The browser's TypeError is kept in cause only when the name in the message is the source unchanged. When anything was left out of the name (a user name or password, a query string, a fragment, a data: or blob: URL) the error has no cause, because the browser's own message can quote the whole URL (Chromium's does for a URL with a user name or password). A data: or blob: URL is never echoed (data: URL, blob: URL), a File is called by its name and any other Blob is Blob. An Error the browser's decoder rejects with is passed on unchanged: its message is the browser's and does not name the source. An abort is passed on as the AbortError it is.

A load that takes too long​

Two limits fail a load that takes too long. Both are options of createArrangement, in milliseconds. A load that runs into one fails its source like any other failed load: the source is failed, its error is an Error whose name is 'TimeoutError', and retry(src) starts it again with its whole time.

OptionDefaultWhat it limits
sourceStallTimeout30000How long a load may go without receiving data: no response yet, or no new bytes of the body. defaultLoadSource applies it to a fetch, so a download that keeps arriving is never cut off, however long it takes. Reading a Blob or File and decoding are not timed
sourceTimeoutno limitThe total time of one load, from the call of loadSource to its result, decoding included. The arrangement enforces it around any loadSource, also one that ignores its signal
const arrangement = createArrangement({
sourceStallTimeout: 60_000, // a slow server: wait a minute for data
sourceTimeout: 5 * 60_000, // and never longer than five minutes for one source
});

Infinity switches a limit off, and so does a number above 2147483647 (2^31 - 1 ms, about 24.8 days: no timer holds more). undefined and null take the default; any other value that is not a number above 0 throws when the arrangement is made (a TypeError for a value that is not a number, a RangeError for 0 or less). The wait for an audio context to decode with is not counted: the time starts when the load does, and starts again when the load is started again (a retry, or a context attached again).

A loadSource of your own is not timed by sourceStallTimeout. It receives the value as stallTimeout in its second argument, and defaultLoadSource applies it to a URL when you pass that argument on. A fetch your loader makes itself is under no limit unless you time it (with context.stallTimeout, say) or set sourceTimeout. A source can still load for ever where the stall limit does not look: a loadSource of your own, a download that keeps arriving and never ends (a live stream), a file read or a decode that never finishes, a limit set to Infinity. sourceTimeout ends each of these. The one wait no limit covers is the wait for an audio context: a source stays pending until one is attached.

The messages are No data arrived from <source> for 30 s and Loading <source> took longer than 300 s (the number is the limit in force, in seconds), with <source> named as described above. Where a response has no readable body stream, only the wait for the response is timed.

The audio context to decode with​

Nothing is fetched until the arrangement has an audio context to decode with. Until then its sources are pending.

  • new EditorEngine({ adapter, arrangement }) attaches adapter.audioContext in its constructor and takes it away in dispose().
  • An engine without an adapter attaches nothing.
  • With attachDecodeContext: false the engine leaves it to you.

To attach one yourself:

import { attachDecodeContext } from '@waveform-playlist/engine';

// getContext is called for each load, so it may create or resume the context when the first one is needed
const detach = attachDecodeContext(arrangement, async () => audioContext);

detach();

detach() aborts the loads in flight. Their sources stay pending, loaded sources stay loaded, and the next attach starts the aborted ones again. That is what happens between one engine's dispose() and the next engine. One context at a time: a second attach while one is attached logs a console warning and replaces the first, and the first one's detach then does nothing. A getContext that rejects fails the source with that error; retry() asks again.

Without an Engine​

An arrangement needs no engine. Every method above works on one that has none, with one difference:

  • Clips are given no startTick, because there is no tempo to convert with. A clip that came with one keeps it until it is moved, trimmed on the left or split; after that it has none. When an engine attaches, every clip without a startTick gets one from its start, in the current tracks and in every undo and redo state.

Swapping the Engine​

An arrangement outlives its engine. Dispose the engine, build another on the same arrangement, and the tracks, the mix and the undo history are all still there:

const arrangement = createArrangement();
let engine = new EditorEngine({ adapter: adapterA, arrangement });
engine.setTracks(tracks);
engine.setTrackVolume(trackId, 0.5);
engine.moveClip(trackId, clipId, 1); // one second

engine.dispose();

engine = new EditorEngine({ adapter: adapterB, arrangement });
engine.getState().tracks.length; // as before
engine.canUndo; // true

A clip's startTick is counted in an engine's ppqn. When an engine attaches, every clip without a startTick gets one from its start, and when the engine has another ppqn than the one before (the Tone.js adapter at 192 after the native transport at 960, or the reverse) every stored tick is converted to the new unit, rounded to the nearest tick. Both happen in the current tracks, in every undo and redo state and in the base of an open transaction. start is not touched and it is not an undo step. When the current tracks changed, subscribers are told once (tracksVersion goes up by one); with the same ppqn and a tick on every clip, nothing happens. It is all or nothing: if the engine's tempo map throws while the ticks are worked out, the constructor throws, nothing in the arrangement has changed and it is not attached.

A setTempo places every clip that has a tick on the tick grid of the engine it is called on, swap or no swap. Going to a coarser ppqn rounds: 7 ticks at 960 become 1 at 192, and 5 when an engine at 960 takes over again. After such a swap the next tempo change can therefore place a clip up to half a tick of the coarser engine away from where it was: 0.5 × 60 × sampleRate / (ppqn × bpm) samples, about 62 samples (1.3 ms) at 192 ppqn, 120 bpm and 48 kHz.

A different tempo is not converted. A tick is the musical position, so an engine built at another tempo than the one the ticks were made under has clips whose start and startTick disagree until the next setTempo, which moves start to follow the tick. Clips added under the new engine get their ticks at its tempo and keep their place at that setTempo, while the older ones move.

The new engine sends the tracks it finds to its adapter once, in its constructor, with their mix. If that call throws, the constructor throws and the engine is no longer attached to the arrangement. After dispose(), the engine's track, clip, mixer, recording and history methods still change the arrangement, but call no adapter and emit no statechange, and setTempo() does nothing. Selection, loop, zoom, the playback position and the master volume belong to the engine and start from their defaults in the new one.

An arrangement plays through one engine at a time. Building a second engine on an arrangement whose first engine has not been disposed logs a console warning. Two engines alive on one arrangement is unsupported, in particular with different ppqn: the second engine's time base replaces the first's, so ticks the first engine gives later are computed with the second engine's. The arrangement option accepts only an object made by createArrangement(); anything else throws. isArrangement(value) (exported) tells whether a value is one, the way the engine checks it. The engine recognises an arrangement by a versioned Symbol.for key, not by instanceof, so one made by another installed copy of @waveform-playlist/engine (an app and a library that pin different versions) is accepted when both copies use the same key version. The keys are @waveform-playlist/engine/arrangement/time-base/v4 and @waveform-playlist/engine/arrangement/attach-decode/v4 (v1 until the mixer became undoable, v2 until addTrack took an index, v3 until addTrack became no undo step by default and addClip was added): an engine and an arrangement from copies on either side of such a change refuse each other.

Implementing a PlayoutAdapter​

The PlayoutAdapter interface connects the engine to any audio backend. The engine calls these methods; your adapter translates them into audio API calls.

import type { PlayoutAdapter } from '@waveform-playlist/engine';
import type { PlayoutTrack } from '@waveform-playlist/core';

class MyAudioAdapter implements PlayoutAdapter {
readonly audioContext = new AudioContext();
readonly ppqn = 960;

async init(): Promise<void> {
// Resume the audio context, load worklets, etc.
}

setTracks(tracks: readonly PlayoutTrack[]): void {
// Rebuild audio graph from tracks.
// Read the tracks, never write to them. They carry no view fields (name,
// color, renderMode…): a change of those alone never causes a call.
}

play(startTime: number, endTime?: number): void {
// Start audio playback from startTime
}

pause(): void {
// Pause audio playback
}

stop(): void {
// Stop audio and reset
}

seek(time: number): void {
// Move playhead to time position
}

getCurrentTime(): number {
// Return current playback position in seconds
return 0;
}

isPlaying(): boolean {
// Return whether audio is currently playing
return false;
}

setMasterVolume(volume: number): void {
// Set master output level
}

setTrackVolume(trackId: string, volume: number): void { /* ... */ }
setTrackMute(trackId: string, muted: boolean): void { /* ... */ }
setTrackSolo(trackId: string, soloed: boolean): void { /* ... */ }
setTrackPan(trackId: string, pan: number): void { /* ... */ }

setLoop(enabled: boolean, start: number, end: number): void {
// Configure loop region
}

dispose(): void {
// Clean up audio resources
}
}

// Use it:
const engine = new EditorEngine({
adapter: new MyAudioAdapter(),
});

addTrack(track), removeTrack(trackId) and updateTrack(trackId, track) are optional. When the adapter has them, the engine calls them for a change to one track; otherwise it calls setTracks with every track. The tracks an adapter receives carry the current mix in their volume, muted, soloed and pan fields.

:::tip Built-in adapter The @waveform-playlist/playout package provides createToneAdapter() — a production PlayoutAdapter built on Tone.js. The React provider uses this internally. :::

Pure Operations​

The engine also exports pure functions used internally. These are useful for custom UIs or headless processing:

Clip Operations​

import {
constrainClipDrag,
constrainBoundaryTrim,
splitClip,
canSplitAt,
calculateSplitPoint,
trackClipSpans,
findClipOverlap,
} from '@waveform-playlist/engine';

// What a move or a trim of `clip` collides with: the track's loaded clips and
// the spans of its clips still loading, in time order
const sortedClips = trackClipSpans(track.clips);
const clipIndex = sortedClips.indexOf(clip);

// Every delta, position and minimum below is in seconds.

// Constrain a drag delta to prevent overlaps (a delta of 0 never moves the clip)
const safeDelta = constrainClipDrag(clip, delta, sortedClips, clipIndex);

// Constrain a boundary trim (MIN_CLIP_DURATION_SECONDS is from @waveform-playlist/core)
const safeTrim = constrainBoundaryTrim(
clip,
delta,
'left',
sortedClips,
clipIndex,
MIN_CLIP_DURATION_SECONDS
);

// Check if a split is valid, then split
if (canSplitAt(clip, at, MIN_CLIP_DURATION_SECONDS)) {
const { left, right } = splitClip(clip, at); // left.id === clip.id
}

// Clips on a track cannot overlap: the arrangement refuses a track whose clips
// do. To check a list first:
const overlap = findClipOverlap(track.clips); // { first, second } or null

// Snap a split position, in samples of the view, down to a pixel boundary; divide the
// result by the view's sample rate before passing it to canSplitAt / splitClip
const snapped = calculateSplitPoint(samplePosition, samplesPerPixel);

Timeline Operations​

import {
calculateDuration,
findClosestZoomIndex,
calculateZoomScrollPosition,
clampSeekPosition,
} from '@waveform-playlist/engine';

// Total duration across all tracks
const duration = calculateDuration(tracks);

// Find nearest zoom level
const index = findClosestZoomIndex(targetSpp, zoomLevels);

// Keep viewport centered during zoom
const newScrollLeft = calculateZoomScrollPosition(
oldSpp, newSpp, scrollLeft, containerWidth, sampleRate
);

// Clamp a seek position to the range 0..duration
const time = clampSeekPosition(requestedTime, duration);

Viewport Operations​

import {
calculateViewportBounds,
getVisibleChunkIndices,
shouldUpdateViewport,
} from '@waveform-playlist/engine';

// Calculate visible region with overscan buffer
const { visibleStart, visibleEnd } = calculateViewportBounds(scrollLeft, containerWidth);

// Which canvas chunks are visible?
const chunks = getVisibleChunkIndices(totalWidth, chunkWidth, visibleStart, visibleEnd);

// Should we recalculate? (debounce small scroll movements)
if (shouldUpdateViewport(oldScroll, newScroll, threshold)) {
// re-render
}

Key Concepts​

Clip Times Are Seconds, Compared on One Grid​

A clip holds seconds and no sample rate, so the same clip means the same thing on a 44.1 kHz and on a 48 kHz machine:

clip.start; // 1 where the clip starts on the timeline
clip.duration; // 2 how long it plays
clip.offset; // 0 where in its audio it starts playing
clip.sourceDuration; // 10 how long its audio is

Doubles in seconds cannot be compared exactly (0.1 + 0.2 !== 0.3), so every comparison of clip times (do two clips touch, does one overlap another, is a split inside the clip) is made in whole units of one grid, 282,240,000 units per second, which every common sample rate divides. A clip ends at the unit of start + duration, and an edit computes in units and stores the result, so clips that touch keep touching through any number of moves, trims and splits, at every sample rate.

Where a sample count is needed (a buffer position for playback, a pixel for drawing), it is derived at the rate of the thing being indexed, with clipTimelineSamples(clip, rate) and clipSourceSamples(clip, rate) from @waveform-playlist/core:

import { clipTimelineSamples, clipSourceSamples } from '@waveform-playlist/core';

// On a timeline counted at the view's or the engine's rate
const { startSample, durationSamples } = clipTimelineSamples(clip, 48000);

// In the clip's own audio, at the buffer's rate
const { offsetSamples } = clipSourceSamples(clip, clip.audioBuffer.sampleRate);

No-Op Emission Guards​

The engine avoids unnecessary listener calls. Mutating methods bail early when:

  • A constrained drag/trim delta resolves to zero
  • A track/clip ID is not found
  • A zoom level is already at the requested value
  • Selection, loop region or master volume values haven't changed
  • updateTrack(id, track) names a track the engine does not have (the track object is still checked, and an invalid one throws)
  • moveClip, trimClip or splitClip names a clip that is still loading
  • A transaction that changed nothing is committed or aborted

This prevents wasted re-renders in your UI framework.

Shared, Read-Only Tracks​

The tracks are immutable. A change replaces the tracks array and only the track and clip objects that changed; every other track is the same object as before, so comparing by identity (prev.tracks[i] !== next.tracks[i]) finds what changed. The exception is setTracks(): it copies every track it is given, so after it no track is the object it was. An undo step is a reference to an earlier array.

  • setTracks(), addTrack() and updateTrack() copy each track and clip they are given — your objects are not retained
  • updateTrack() keeps a clip object the track already holds, so a clip you did not replace is the same object afterwards
  • getState().tracks and arrangement.getTracks() are not copies. They are typed readonly ClipTrack[] (readonly ArrangementTrack[] for getTracks(), which also holds the clips that are still loading). The tracks an adapter receives are copies without the view fields (core's TRACK_VIEW_FIELDS, typed PlayoutTrack) that share these clips, frozen the same way.
  • Outside production builds (process.env.NODE_ENV is not 'production'; a page that loads the module with no bundler, and so has no process, counts as production) the tracks array, each track, its clips array and each clip are frozen, so a write throws in a module. What a clip refers to (its AudioBuffer, peaks, fades, notes) is not frozen

To change a track, build a new one from it:

const track = engine.getState().tracks.find((t) => t.id === trackId);
if (track) {
engine.updateTrack(trackId, { ...track, name: 'Bass' });
}

Invariant Normalization​

The engine normalizes ordering automatically:

  • setSelection(10, 5) → selectionStart = 5, selectionEnd = 10
  • setLoopRegion(20, 10) → loopStart = 10, loopEnd = 20

Consumers can trust that EngineState.selectionStart <= selectionEnd and loopStart <= loopEnd.

tracksVersion Counter​

The tracksVersion field in EngineState is a monotonic counter that increments only on structural track changes: setTracks, addTrack, removeTrack, reorderTrack, updateTrack(id, track), moveClip, trimClip, splitClip, undo, redo, an aborted transaction that had changed something, a setTempo that the adapter did not refuse (setTempo with the tempo the engine already has and no atTick returns before that), and an engine being built on an arrangement that has clips without a startTick, or whose ticks are in another ppqn (they are converted), and the audio of a loading clip in the current tracks arriving (see Loading Audio). It does not increment on selection, zoom, master volume, or loop changes, on a change of the undo history alone (commitTransaction, clearHistory), nor on per-track mixer edits. Use it to skip expensive operations (like audio graph rebuilds or peak regeneration) when only non-structural state changed.

Per-track mixer edits (setTrackVolume, setTrackMute, setTrackSolo, setTrackPan, setTrackPanLaw) instead bump a separate mixerVersion counter (and emit statechange). This lets a consumer refresh what it shows of the mix without re-running the structural work gated on tracksVersion. The split matters because mixer setters fire continuously during a volume/pan slider drag.

Example: Vanilla JavaScript​

A plain DOM UI that reacts to engine state — no framework required:

<div id="app">
<p id="status">Stopped</p>
<p id="info">Duration: 0s | Tracks: 0</p>
<button id="play">Play</button>
<button id="pause">Pause</button>
<button id="stop">Stop</button>
<button id="zoom-in">Zoom In</button>
<button id="zoom-out">Zoom Out</button>
</div>

<script type="module">
import { EditorEngine } from '@waveform-playlist/engine';
import { createTrack, createClipFromSeconds } from '@waveform-playlist/core';

const engine = new EditorEngine({ sampleRate: 48000 });

// Update DOM on every state change
engine.on('statechange', (state) => {
document.getElementById('status').textContent =
state.isPlaying ? 'Playing' : 'Stopped';
document.getElementById('info').textContent =
`Duration: ${state.duration.toFixed(2)}s | Tracks: ${state.tracks.length}`;
document.getElementById('zoom-in').disabled = !state.canZoomIn;
document.getElementById('zoom-out').disabled = !state.canZoomOut;
});

// Wire up buttons
document.getElementById('play').onclick = () => engine.play();
document.getElementById('pause').onclick = () => engine.pause();
document.getElementById('stop').onclick = () => engine.stop();
document.getElementById('zoom-in').onclick = () => engine.zoomIn();
document.getElementById('zoom-out').onclick = () => engine.zoomOut();
</script>

Example: Svelte Integration​

A minimal Svelte store that subscribes to engine state:

<script>
import { onMount, onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { EditorEngine } from '@waveform-playlist/engine';

const engine = new EditorEngine({ sampleRate: 48000 });

const state = writable(engine.getState());

function handleStateChange(newState) {
state.set(newState);
}

onMount(() => {
engine.on('statechange', handleStateChange);
});

onDestroy(() => {
engine.dispose();
});
</script>

<div>
<p>Duration: {$state.duration.toFixed(2)}s</p>
<p>Playing: {$state.isPlaying}</p>
<p>Tracks: {$state.tracks.length}</p>

<button on:click={() => engine.play()}>Play</button>
<button on:click={() => engine.pause()}>Pause</button>
<button on:click={() => engine.stop()}>Stop</button>

<button on:click={() => engine.zoomIn()} disabled={!$state.canZoomIn}>Zoom In</button>
<button on:click={() => engine.zoomOut()} disabled={!$state.canZoomOut}>Zoom Out</button>
</div>

TypeScript​

All types are exported from @waveform-playlist/engine:

import type {
EngineState,
EngineEvents,
EditorEngineOptions,
PlayoutAdapter,
Arrangement,
CreateArrangementOptions,
AddTrackOptions,
AddClipOptions,
RemoveTrackOptions,
RemoveClipOptions,
EngineDisposeOptions,
MixerChangeOptions,
ClipBounds,
TrackMixerValues,
ArrangementSourcesValue,
ArrangementSourceState,
LoadSource,
} from '@waveform-playlist/engine';

Data model types come from @waveform-playlist/core:

import type {
AudioClip,
ClipTrack,
ArrangementTrack,
LoadingClip,
ClipSource,
ValueSource,
MixerGesture, // what createMixerGesture() returns
} from '@waveform-playlist/core';

EngineState​

interface EngineState {
tracks: readonly ClipTrack[]; // shared, not a copy; frozen outside production builds; loaded clips only
tracksVersion: number;
mixerVersion: number;
duration: number;
currentTime: number;
isPlaying: boolean;
samplesPerPixel: number;
sampleRate: number;
selectedTrackId: string | null;
zoomIndex: number;
canZoomIn: boolean;
canZoomOut: boolean;
selectionStart: number;
selectionEnd: number;
masterVolume: number;
loopStart: number;
loopEnd: number;
isLoopEnabled: boolean;
bpm: number;
ppqn: number;
canUndo: boolean;
canRedo: boolean;
}

EditorEngineOptions​

interface EditorEngineOptions {
adapter?: PlayoutAdapter;
arrangement?: Arrangement;
sampleRate?: number;
samplesPerPixel?: number;
zoomLevels?: number[];
undoLimit?: number;
attachDecodeContext?: boolean; // default true
bpm?: number;
ppqn?: number;
}

Arrangement​

function createArrangement(options?: CreateArrangementOptions): Arrangement;

interface CreateArrangementOptions {
tracks?: readonly ArrangementTrack[]; // the first tracks; not an undo step
undoLimit?: number; // default 100
loadSource?: LoadSource; // default: defaultLoadSource
sourceTimeout?: number; // ms; the total time of one load. Default: no limit (Infinity)
sourceStallTimeout?: number; // ms; how long a load may receive no data. Default 30000
}

type LoadSource = (
src: ClipSource,
context: {
signal: AbortSignal;
audioContext: BaseAudioContext;
stallTimeout?: number; // the arrangement's sourceStallTimeout (ms); defaultLoadSource applies it
}
) => Promise<AudioBuffer>;

const defaultLoadSource: LoadSource;

// Returns the function that detaches
function attachDecodeContext(
arrangement: Arrangement,
getContext: () => Promise<BaseAudioContext>
): () => void;

interface ArrangementSourceState {
status: 'pending' | 'loaded' | 'failed';
error?: Error;
}

interface ArrangementSourcesValue {
sources: ReadonlyMap<ClipSource, ArrangementSourceState>;
pendingCount: number;
failedCount: number;
isLoading: boolean;
}

interface AddTrackOptions {
index?: number; // where the track enters (cut to a whole number, held between 0 and the number of tracks); left out: the end
undoable?: boolean; // true: one undo step. Left out: no step; the track is written into every saved state
}

interface AddClipOptions {
undoable?: boolean; // true: one undo step. Left out: no step; the clip is written into every saved state where it fits
}

interface RemoveTrackOptions {
undoable?: boolean; // false: no undo step, and the track leaves every undo and redo state
}

interface MixerChangeOptions {
gesture?: string; // changes with the same id, one after another, are one undo step
undoable?: false; // not an undo step: the value is written into every saved state as well
}

// From @waveform-playlist/core
function createMixerGesture(): MixerGesture;

interface MixerGesture {
readonly id: string; // the id to pass while a gesture is going on
end(): void; // the next change starts a new step
attach(input: HTMLInputElement): () => void; // ends the gesture on the input's own events; returns the detach function
}

type ArrangementChangeType =
| 'set' // setTracks replaced the whole set
| 'add' // one track was added
| 'remove' // one track was removed
| 'update' // one track changed: updateTrack, or a clip added, removed, moved, trimmed or split
| 'reorder' // one track was moved
| 'restore' // an undo, a redo or an abort changed the tracks (and maybe the mix)
| 'restoreMix' // an undo, a redo or an abort changed the mix only
| 'load' // audio arrived: loading clips became loaded clips (or were removed: removedClips)
| 'mixer' // one of the mixer setters changed a volume, mute, solo, pan or pan law
| 'recording' // a take started or stopped recording onto a track
| 'recompute' // every track was gone through again: clip positions after a tempo change, or clip ticks when an engine attached
| 'history'; // only what can be undone or redone changed

interface ArrangementChangeInfo {
readonly type: ArrangementChangeType;
/**
* The clips this change took out of the current tracks by itself, because
* nothing of their audio was left after their `offset`: a clip still
* loading whose audio arrived (on a 'load'), or one added when its audio
* was loaded already (on that 'add', 'update' or 'set'). Absent when there
* are none; a clip your code removes is never listed.
*/
readonly removedClips?: readonly ArrangementRemovedClip[];
}

interface ArrangementRemovedClip {
readonly trackId: string;
readonly clipId: string;
}

interface TrackMixerValues {
volume: number;
muted: boolean;
soloed: boolean;
pan: number;
/** 'auto' until the track's first decoded audio decides it; then 'mono' or 'stereo'. */
panLaw: 'auto' | 'mono' | 'stereo';
}

/** A clip's times, in seconds. */
interface ClipBounds {
offset: number;
duration: number;
start: number;
sourceDuration: number;
}

interface Arrangement {
// read
getTracks(): readonly ArrangementTrack[];
getMixer(): ReadonlyMap<string, TrackMixerValues>;
subscribe(listener: (change: ArrangementChangeInfo) => void): () => void;
readonly tracksVersion: number;
readonly mixerVersion: number;

// whole set
setTracks(tracks: readonly ArrangementTrack[]): void;

// tracks
addTrack(track: ArrangementTrack, options?: AddTrackOptions): void;
addClip(trackId: string, clip: AudioClip | LoadingClip, options?: AddClipOptions): void;
removeTrack(trackId: string, options?: RemoveTrackOptions): void;
removeClip(trackId: string, clipId: string, options?: RemoveClipOptions): void;
updateTrack(
trackId: string,
next: ArrangementTrack | ((track: ArrangementTrack) => ArrangementTrack)
): void;
reorderTrack(trackId: string, toIndex: number): void;

// clips (a clip that is still loading cannot be edited)
// (deltas and positions are seconds)
moveClip(trackId: string, clipId: string, delta: number): number;
trimClip(trackId: string, clipId: string, boundary: 'left' | 'right', delta: number): void;
splitClip(trackId: string, clipId: string, at: number): void;
getClipBounds(trackId: string, clipId: string): ClipBounds | null;
constrainTrimDelta(
trackId: string,
clipId: string,
boundary: 'left' | 'right',
delta: number
): number;

// mixer (each call is an undo step; see MixerChangeOptions)
setTrackVolume(trackId: string, volume: number, options?: MixerChangeOptions): void;
setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
setTrackPanLaw(trackId: string, panLaw: 'mono' | 'stereo', options?: MixerChangeOptions): void;

// the take's mute: the tracks a take is recording onto (outside the history)
readonly recordingTrackIds: ReadonlySet<string>;
setTrackRecording(trackId: string, recording: boolean): void;

// history
readonly undoLimit: number; // the undoLimit it was made with (default 100)
readonly canUndo: boolean;
readonly canRedo: boolean;
undo(): void;
redo(): void;
clearHistory(): void;
beginTransaction(): void;
commitTransaction(): void;
abortTransaction(): void;
readonly inTransaction: boolean; // a transaction is open: join it rather than open one

// loading
readonly sources: ValueSource<ArrangementSourcesValue>;
retry(src: ClipSource): void;
}

PlayoutAdapter​

interface PlayoutAdapter {
readonly audioContext: AudioContext;
readonly ppqn: number;
init(): Promise<void>;
setTracks(tracks: readonly PlayoutTrack[]): void; // PlayoutTrack: a ClipTrack without core's TRACK_VIEW_FIELDS
addTrack?(track: PlayoutTrack): void;
removeTrack?(trackId: string): void;
updateTrack?(trackId: string, track: PlayoutTrack): void;
play(startTime: number, endTime?: number): void;
pause(): void;
stop(): void;
seek(time: number): void;
getCurrentTime(): number;
isPlaying(): boolean;
setMasterVolume(volume: number): void;
setTrackVolume(trackId: string, volume: number): void;
setTrackMute(trackId: string, muted: boolean): void;
setTrackSolo(trackId: string, soloed: boolean): void;
setTrackPan(trackId: string, pan: number): void;
/** Optional. A track's pan law changed; 'auto' means "from the track's clips" (`trackPanLaw` from core).
* Without it the engine sends the track again (`updateTrack`, else `setTracks`). */
setTrackPanLaw?(trackId: string, panLaw: 'auto' | 'mono' | 'stereo'): void;
setLoop(enabled: boolean, start: number, end: number): void;
dispose(): void;
}

The interface has further optional members (tempo and tick conversion, onPlaybackEnded, worklet helpers, masterOutputNode, lookAhead); see packages/engine/src/types.ts.

EngineEvents​

interface EngineEvents {
statechange: (state: EngineState) => void;
play: () => void;
pause: () => void;
stop: () => void;
}

See Also​