Build DAW Playback: Transport, Clips, Automation
RealtimeEngine is libsonare's transport and playback engine: sample-accurate commands for parameters and transport, a per-track lane mixer (lanes, buses, sends, channel strips), MIDI clip scheduling, group routing and sidechains, surround group buses, capture, offline bounce, freeze, and telemetry. Use it when clips, MIDI, transport, and mixed audio are the output — a DAW-like timeline or an instrument host.
Mixing on the engine (lanes, buses, channel strips, PFL/AFL taps, group routing, sidechains, surround buses) is on Mixing on the Realtime Engine. For the streaming analyzer (StreamAnalyzer), the AudioWorklet bridge, and paged clip audio streaming, see Realtime and Streaming.
What You Will Learn
The sections below are largely independent — read the first one, then jump to whichever matches your host: transport and telemetry, offline render and capture, parameter automation, warped audio clips and page underruns, MIDI clip scheduling, and external MIDI gear.
Check the engine ABI before constructing
capabilities().abi.engine reports the engine ABI of the loaded WASM binary — the application binary interface, the exact memory layout and call signatures the binary and the JS package agree on. The realtime engine is the most version-sensitive API in the library; constructing it against a mismatched binary is undefined. Keep the WASM binary and the JS package from the same release: update your @libraz/libsonare package so both come together.
Transport and output
RealtimeEngine exposes sample-accurate commands for parameters and transport, plus offline render helpers for non-realtime export.
Start with transport and output before adding the advanced pieces: construct the engine with the device sample rate and block size, set tempo/loop state, call play(), process blocks, and only then add meters, lane mixing, MIDI clips, or capture. That order keeps debugging clear because you can confirm "the engine plays" before asking it to route or record anything.
import { init, RealtimeEngine, capabilities } from '@libraz/libsonare';
await init();
const engineAbi = capabilities().abi.engine; // the engine ABI of the loaded WASM binary
// { sampleRate, maxBlockSize?, commandCapacity?, telemetryCapacity?, maxChannels? }
const engine = new RealtimeEngine({ sampleRate: 48000, maxBlockSize: 128 });
engine.setTempo(128);
engine.setTimeSignature(4, 4);
engine.setLoop({ startPpq: 0, endPpq: 16, enabled: true });
engine.play();
const output = engine.process([leftBlock, rightBlock]);
const transport = engine.transportState();
const telemetry = engine.drainTelemetry();
engine.stop();
engine.destroy();import libsonare as sonare
engine = sonare.RealtimeEngine(sample_rate=48000, max_block_size=128)
engine.set_tempo(128)
engine.set_time_signature(4, 4)
engine.set_loop(0, 16, enabled=True)
engine.play()
output = engine.process([left_block, right_block]) # float32 NumPy arrays
transport = engine.transport_state()
telemetry = engine.drain_telemetry()
engine.stop()
engine.close()transportState() includes a musical playhead as well as the raw sample and PPQ (musical position in quarter-note units) positions. barIndex is zero-based, while beat is one-based within that bar and beatFraction is in [0, 1), so a UI can show a conventional bar:beat display (${barIndex + 1}:${beat}, for example "3:2") without deriving it from PPQ. Use beatFraction for a smooth indicator.
Three clocks
The engine keeps three positions, and the API says which one each argument means:
| Clock | Counts | Moves |
|---|---|---|
Render frame (renderFrame, the worklet's sampleTime) | Frames processed since prepare | Always forward, by every block, even while stopped; never jumps |
Timeline sample (timelineSample) | The playhead's position in samples | Only while playing; jumps on a seek and folds back at a loop end |
PPQ (ppq) | The playhead's musical position in quarter notes | Follows the timeline through the tempo map |
- render frame
- timeline sample
- PPQ (quarter notes)
Commands are scheduled in render frames: renderFrame says in which upcoming frame a command takes effect, and omitting it means the block head, the first frame of the next block the engine processes. Clips, markers, and automation are placed in timeline samples or PPQ, which is why they follow seeks and loops. sampleAtPpq(ppq) converts between the last two. One field name breaks the pattern: a MIDI clip event's renderFrame holds an engine-timeline sample.
Sizing prepare for the real channel count
The constructor and prepare(...) take the same config object, whose optional maxChannels field sizes this. Prepare reserves the capture, instrument, PDC (plugin delay compensation), and monitor planes for that count rather than always reserving 64, so a stereo host is not paying for 64 planes of scratch it will never touch.
// A stereo host reserves 2 planes, not 64 (the other fields left default).
const engine = new RealtimeEngine({ sampleRate: 48000, maxChannels: 2 });
engine.prepare({ sampleRate: 48000, maxChannels: 8 }); // or re-prepare for a 7.1 targetSet it to the largest channel count you will actually render (1 to 64). Leaving it unset reserves 64.
Control-only hosts
A host that never calls process() (a headless controller, or an offline path that queues commands and reads state) can apply the queued commands explicitly with engine.flushControlCommands(), which renders nothing.
Seeking and the metronome
seekSample(timelineSample, renderFrame?), seekPpq(ppq, renderFrame?), and seekMarker(markerId, renderFrame?) queue a jump of the playhead (after setMarkers([...]) for the marker form); like every queued command they apply at renderFrame, or at the next block head when it is omitted. A seek releases sounding notes and resets controllers, as a loop wrap or stop does; when playback next rolls, the engine resends the controller, program, channel pressure, pitch bend and MIDI 2.0 registered/assignable controller values that the clips set before the new position, so a note started there sounds with the right state. Data entry, RPN/NRPN selection, portamento control (CC 84) and channel-mode messages are not resent. A negative seek position is refused (RangeError; Python SonareValueError). setMetronome({ enabled, beatGain?, accentGain?, clickSamples?, clickSec? }) configures the click, metronome() reads it back, and the click length is limited to one second. On the worklet SonareEngine, setMetronome returns nothing and changes neither engine when the command ring is full.
Install a compiled Project timeline
Project.compile({ withTimeline: true }) returns the diagnostics plus a timeline handle. Check the diagnostics before applying it; an error result carries no timeline. RealtimeEngine.applyProjectTimeline(timeline) is a control-thread operation for a stopped, prepared engine, and the timeline's project sample rate must equal the prepared rate. It replaces the tempo and time-signature segments, markers, track lanes, track automation, audio and MIDI clips, and the per-track strips from the project's mixer scene. Instruments, buses, the master strip, metronome, loop, and capture remain as configured on the engine, so bind instruments separately.
The apply is all-or-nothing. A disposed timeline, an invalid scene or timeline, or a scene strip routed from several tracks is rejected without publishing a replacement. In WASM, the timeline must be a ProjectTimeline created by the same initialized module; a handle from another module instance or a released handle cannot be applied. After a successful apply the engine owns its reference, so release the timeline immediately with destroy(). Once a timeline is installed, preparing the engine at another sample rate is refused until both timeline clip domains have been cleared with setClips([]) and setMidiClips([]).
const compiled = project.compile({ withTimeline: true }); // project: a Project with tempo, clips and events
if (compiled.timeline === undefined) {
throw new Error(JSON.stringify(compiled.diagnostics));
}
const engine = new RealtimeEngine({ sampleRate: 48000 });
try {
engine.applyProjectTimeline(compiled.timeline);
compiled.timeline.destroy(); // the engine retains its own reference
} finally {
engine.destroy();
}Meter and scope telemetry
Two telemetry families matter when wiring a UI:
- Meters:
drainMeterTelemetry(maxRecords?)returns one record per target, with one value per plane (numChannelsof them) in the array fields. - Scopes. Call
configureScopeTelemetry(intervalFrames, bandCount)to enable per-target spectrum and vectorscope capture, then read snapshots withdrainScopeTelemetry(). The frame interval is an integer in [0, 2147483647], with 0 disabling capture. The band count is an integer in [1, 64]. An out-of-domain value raisesRangeErrornaming the field and preserves the current settings. The call returns the applied band count.
Each meter record carries these fields:
| Field | Value | Notes |
|---|---|---|
| Sample peak | peakDb[] | One value per plane (numChannels of them); an element is undefined until measured and -Infinity for a silent plane |
| RMS | rmsDb[] | Same per-plane availability and silence behavior as sample peak |
| True peak | truePeakDbtp[], maxTruePeakDbtp? | Streaming measurement, a few samples behind the block; per-plane values may be undefined, and the summary is omitted until measured |
| Pre-trim input peak | inputPeakDb[] | One value per valid plane; an element is undefined until measured |
| Loudness | momentaryLufs?, shortTermLufs?, integratedLufs? | Omitted until the corresponding window or gate has a reading; silence can read -Infinity |
| Gain reduction | gainReductionDb | Reduction from the target strip or bus, as well as the master path |
| Phase and width | correlation?, monoCompatWidth? | Omitted when not measurable; width can be +Infinity when the mid signal is silent but the side is not |
droppedRecords | cumulative count | See below |
Per-plane arrays contain numChannels entries, but an entry can be undefined until measured. Optional scalar fields can be absent, and measured silence can produce -Infinity; check values before formatting or serializing them. Missing measurements are not zero.
Each record returned by drainMeterTelemetry() and drainScopeTelemetry() carries a droppedRecords count for its producing tap. The count is cumulative from that tap's prepare or reset; draining does not clear it. Compare it with the value from the previous poll: an increase means records were lost between polls, while an unchanged value means no new drop. A non-zero value can refer to an earlier interval.
For the individual insert values behind gainReductionDb, call meterTargetInsertGainReduction(targetId). It returns the last rendered block's reduction in dB (each value is <= 0) in combined pre-fader-then-post-fader insert order; bypassed and non-dynamics inserts return 0, and the meter record's gainReductionDb is the minimum. Target 0 is the master, 1..32 are lanes, 33..40 are buses, and the input monitor target returns an empty array. In an AudioWorklet, a structured postMessage meter snapshot can include insertGainReductionDb only on the newest record for each target in one drain, and it describes the latest committed host block. The fixed SharedArrayBuffer meter ring has no room for this optional field: call SonareEngine.pollInsertGainReduction(targetId) (or the lower-level SonareRealtimeEngineNode method) when the ring path is in use.
The AudioWorklet raw meter ring uses a 49-float record whose header slot 3 carries protocol version 3 (SONARE_METER_RING_PROTOCOL_VERSION), and every slot has a state word, so a reader never returns a record that mixes two writes. A main thread and a worklet from different package versions must not share a ring. Code that reads its SharedArrayBuffer directly must size and stride records with the exported SONARE_METER_RING_RECORD_FLOATS constant and read the record count and header instead of hard-coding the record layout.
Each drained scope snapshot is addressed by targetId (master, a lane, or a bus) and carries two arrays: bands holds the linear-band FFT magnitudes in dB (length = the applied band count), and points holds up to 32 stereo goniometer samples as { left, right } records for a vectorscope display. (The worklet scope ring buffer carries the same cloud in its own interleaved [l0, r0, l1, r1, …] Float32Array form.) The spectrum uses a Hann-windowed, coherently normalized FFT and reports the mean power of the bins in each linear band in dBFS. Linear bands are equally wide in Hz: with 64 bands at 48 kHz each is 375 Hz, so everything below 375 Hz (kick, bass) lands in one band. Regroup the bands into logarithmic ones if you want a musical spectrum display. Readings depend on the render block size, FFT frequency resolution, and bandCount; compare snapshots only when those settings match.
To restart the master meter's integrated loudness, call resetMasterLoudnessMeter(renderFrame?) (reset_master_loudness_meter(render_frame=None) in Python; the worklet node takes resetMasterLoudnessMeter(sampleTime?)). The command clears only the integrated-loudness accumulator; momentary, short-term, and true-peak history are retained. renderFrame is the render frame at which the reset applies; omit it to apply at the next block head. A negative value is refused (RangeError in WASM and Node), and the same holds for every renderFrame parameter on transport, parameter, solo/mute, and MIDI calls.
Offline render, bounce, and freeze
Scheduled clips and sequenced MIDI only sound while the transport is rolling; on a stopped engine they stay silent rather than leaking audio. The offline helpers roll the transport for the render duration and restore the prior state afterwards, so offline clip and MIDI rendering needs no manual play().
| Call | Renders |
|---|---|
renderOffline | Into channel buffers you supply. The chunkable, lower-level renderer. |
bounceOffline | The timeline into planar { channels, sampleRate, integratedLufs }, optionally resampled. |
freezeOffline | The source layer into a single clip. |
bounceOffline renders at the prepared engine rate. An omitted sourceSampleRate selects that rate; a given source rate must match it or the call is refused (an explicit 0 is refused too). totalFrames is counted at the source rate. An omitted targetSampleRate keeps the source rate, while a different target rate resamples the returned buffer and is reported in result.sampleRate. The render runs the reported latency ahead and drops it, so frame 0 of the result is the playhead's sample and the result needs no manual latency trimming; freezeOffline does the same, so a frozen clip does not carry the latency.
bounceOffline and freezeOffline perform a non-rendering offline prime before the audible render: they drain queued commands, reset every mixer and effect processor to its prepared state (instruments are not reset), adopt published tempo, graph, lane, and automation snapshots, apply start-position automation, and settle parameter ramps. The prime keeps transport stopped, so it does not advance the playhead, and no user play() or priming process() is needed. Because processors start from their prepared state, insert tails and delay lines still ringing from live playback are cut, and a second bounce of the same engine renders the same audio as the first.
renderOffline preserves the engine's current lifecycle and ramps, rolls a stopped transport only for the render, then restores it. A stopped process() quantum still advances processor state and is not an equivalent prime; settleParameters() only snaps known targets and does not adopt pending lane, graph, or tempo snapshots. Use it only off the live audio thread.
A block size larger than the block size the engine was prepared with is refused by renderOffline, bounceOffline, freezeOffline, and primeOfflineParameters (RangeError on Node and WASM, SonareValueError on Python) rather than rendering at the prepared size. blockSize defaults to 128 and may be smaller than the prepared size.
Chunked renders and priming by hand
A host that renders block by block can run the pieces of that prime itself:
| Method | Does |
|---|---|
primeOfflineParameters(numChannels, blockSize?) | The whole offline prime without rendering: queued commands applied, mixer and effect processors reset, snapshots adopted, automation and lane gates resolved at the transport position, smoothers snapped. Call it once before the first chunk so a repeat render starts from the same state. |
resetProcessorState(renderFrame?) | Queues the processor reset alone, applied at renderFrame (omitted: the next block head). Called during playback it cuts running insert tails and delay lines mid-sound, like a seek. |
settleParameters() / settleInsertParameters() | Snap in-flight parameter ramps, or only the insert automation slots after a structural change. |
finishOfflineRender() | Ends a chunked render started with renderOffline({ ..., finalize: false }): releases held notes and flushes the delay lines. Skipping it leaves notes at the last chunk sounding. |
tailSamples / graphLatencySamples | The longest audible tail as a conservative upper bound in samples (2147483647 means unbounded), and the processing latency in samples (fractional), both read-only properties. A drum-kit synth reports the tail of the kit pieces struck so far (about 0.91 s after one kick), and the whole-kit maximum only before the first note. |
engine.primeOfflineParameters(2, 128);
// Each chunk is a whole number of blocks, all with the same blockSize.
engine.renderOffline({ channels: chunkA, blockSize: 128, finalize: false });
engine.renderOffline({ channels: chunkB, blockSize: 128, finalize: false });
engine.finishOfflineRender();Python spells these prime_offline_parameters, reset_processor_state, settle_insert_parameters, finish_offline_render, tail_samples and graph_latency_samples. The worklet SonareEngine also offers bounceOffline, freezeOffline, finishOfflineRender, and renderOffline({ totalFrames, blockSize?, finalize? }); they run on the facade's main-thread offline engine and block the calling thread for the whole render. See Offline render, bounce, and freeze.
Capture and live MIDI
For recording, the capture API adds a few controls:
configureCapture({ capacityFrames, numChannels?, source?, recordOffsetSamples?, punch? })sets the capture options in one call. Omitted fields take their defaults.armCapture({ armed })starts and stops recording.source('output'or'input') records the engine's rendered output bus or the raw input you pass toprocess(...).recordOffsetSamplesshifts the captured audio to compensate for monitoring round-trip latency. In a loop every pass stays whole: the input that arrives within the offset after a wrap is captured into the end of the previous pass.setInputMonitor({ enabled, gain? })mixes the live input into the output so the performer can hear themselves.countInEndSample(startSample, bars)(Csonare_engine_count_in_end_sample) resolves the end of a count-in on the tempo map; forbars <= 0it returnsstartSample.
captureStatus() reports the active capture source ('output' or 'input'), the current recordOffsetSamples, droppedFrames as the authoritative number of discarded frames. See Recording and Takes for the full flow.
The engine also accepts live MIDI into its instruments: pushMidiNoteOn, pushMidiNoteOff, pushMidiCc, pushMidiProgram, pushMidiPitchBend, pushMidiChannelPressure, pushMidiPolyPressure, pushMidiUmp, and pushMidiSysex. A MIDI value outside its range (a channel above 15, say) throws a RangeError naming the argument and its range. MIDI Input covers the Web MIDI bridge (port management, CC binding, NativeSynth and SF2 destinations), and Recording and Takes covers loop-recording takes, comp lanes, and the browser microphone helper bindMicrophoneInput(...).
Mixing, routing, and surround buses
Lane mixing, the PFL/AFL monitor taps, group routing, sidechains, live strip controls, and surround group buses are on Mixing on the Realtime Engine.
Parameter automation
RealtimeEngine carries an engine-level parameter registry separate from the strip-insert params of setStripInsertParam. Register a parameter once with addParameter(info), then change it live with setParameter(id, value, renderFrame?) (add { smoothed: true } as the fourth argument for a ramp), or schedule it along the timeline with setAutomationLane(id, points).
Inspect metadata with parameterInfo(id). It resolves host-registered ids and reserved ids for hosted-instrument parameters, mixer fader/pan/width targets, and channel-strip insert parameters when the engine retains their strip specification. Reserved metadata comes from compiled defaults and the insert catalog, not the current audio state: an instrument's reported default describes its unloaded patch, and an externally bound strip has no retained specification for insert metadata. parameters() enumerates host-registered parameters only; reserved ids are available through parameterInfo(id) but never appear in that enumeration. Python uses parameter_info and parameters; the C API uses sonare_engine_parameter_info, sonare_engine_parameter_count, and sonare_engine_parameter_info_by_index.
// EngineParameterInfo: id, name, unit, min/max/default, rtSafe, defaultCurve ('linear')
engine.addParameter({
id: 1, name: 'volume', unit: 'lin',
minValue: 0, maxValue: 1, defaultValue: 1,
rtSafe: true, defaultCurve: 'linear',
});
// Automation points are positioned in PPQ (quarter-note units), with an
// optional curveToNext ('linear' | 'exponential' | 'hold' | 'sCurve').
engine.setAutomationLane(1, [
{ ppq: 0, value: 1, curveToNext: 'linear' },
{ ppq: 4, value: 0 },
]);
// Or set it imperatively from the control thread (omit renderFrame to apply at the next block head):
engine.setParameter(1, 0.5);A track, bus, or master fader, a track or master pan, and the master width can be automated without registering a parameter. On the raw RealtimeEngine, declare the lane and bus first (setTrackLanes, setTrackBuses), then resolve the id:
// Throws RangeError if the lane or bus was not declared first.
const faderId = engine.resolveStripAutomationId(1, 'faderDb'); // or 'pan'
const busId = engine.resolveBusAutomationId(1, 'faderDb');
engine.setAutomationLane(faderId, [{ ppq: 0, value: 0 }, { ppq: 8, value: -12 }]);Both throw RangeError when the track has no lane or the bus is not configured (Python raises SonareValueError: resolve_strip_automation_id, resolve_bus_automation_id; C: sonare_engine_resolve_track_lane_automation_id, sonare_engine_resolve_bus_automation_id). The id names the track or bus, not its position, so it keeps driving that strip across setTrackLanes and setTrackBuses reorders and the removal of other strips; once its own track or bus is removed it applies nothing. The master strip always exists, so resolveStripAutomationId('master', kind) takes 'faderDb', 'pan' or 'width' and its id never goes stale (any other kind is a RangeError on WASM and Node and a SonareValueError on Python; C: sonare_engine_resolve_master_automation_id). The reserved mixer id namespace (0x4D58xxxx) accepts only master ids: setParameter and setAutomationLane refuse any other id in it (RangeError; Python SonareValueError), so take track and bus ids only from the resolvers, never by computing them. On the SonareEngine worklet API, resolveStripAutomationId(target, 'faderDb' | 'pan' | 'width') ('width' for the master only) and resolveBusAutomationId(busId, 'faderDb') return the same kind of id, declaring the mixer lane or bus on first use.
Insert parameters use the same automation-lane mechanism, but first need a reserved id. Call resolveStripInsertAutomationId(ref, insertIndex, paramName) (ref is a track id or 'master') or resolveBusInsertAutomationId(busId, insertIndex, paramName), then pass the returned id to setAutomationLane or setParameter. insertIndex addresses the strip's combined pre-then-post insert sequence, and paramName is the key reported by masteringInsertParamInfo. An unknown strip, insert, or key, or a parameter that is not realtime-safe, throws RangeError on WASM and Node and SonareValueError on Python.
Track, bus, and master insert ids share one lifetime rule: an id names its strip by identity and the kind of processor in its slot (for effects.gsEfx, its EFX type too). It survives setTrackLanes and setTrackBuses reorders and the removal of other strips. It goes inert when its strip is removed or the slot comes to hold another kind of processor: it then applies nothing, its queued edits are dropped, and it is never reissued, so resolve again for the new processor. An engine holds 8192 insert-id entries; a strip, bus, or lane change that would exceed them is refused with InvalidState. insertParameterConstructedValue(paramId) returns the immutable value used when that insert parameter was constructed, rather than its current live or automated value.
The strip-insert processors — eq.*, dynamics.*, saturation.*, spectral.*, stereo.*, maximizer.*, and multiband.* — all resolve through these methods and can be driven at audio-block precision. repair.decrackle (median), repair.dehum, repair.denoiseClassical, and repair.dereverbClassical also run as realtime inserts. They match the offline function, delayed by the latency the insert reports, but their parameters resolve no automation id. Stages that need the whole signal (loudness, the match stages, and the remaining repair.*) have no insert form.
const thresholdId = engine.resolveBusInsertAutomationId(1, 0, 'thresholdDb');
engine.setAutomationLane(thresholdId, [
{ ppq: 0, value: -18 },
{ ppq: 8, value: -24, curveToNext: 'sCurve' },
]);Automating a hosted instrument
A synth bound to a MIDI destination has automatable parameters too, and they resolve the same way an insert parameter does. resolveInstrumentAutomationId(destinationId, paramName) turns a hosted instrument's continuous parameter — addressed by its key name, such as 'cutoffHz' — into a reserved id you pass straight to setAutomationLane or setParameter. A host can then drive a synth's cutoff or vibrato depth from an automation lane at audio-block precision instead of stepping it from the control thread, and because the resolved lane is smoothed on the audio thread, live and offline rendering agree.
Bind the instrument with setSynthInstrument or setSf2Instrument first, then resolve. Resolution is control-thread only and touches no audio state.
engine.setSynthInstrument(0, patch);
// Throws RangeError if cutoffHz is not automatable on this instrument.
const cutoffId = engine.resolveInstrumentAutomationId(0, 'cutoffHz');
engine.setAutomationLane(cutoffId, [
{ ppq: 0, value: 400 },
{ ppq: 8, value: 6000, curveToNext: 'exponential' },
]);from libsonare import AutomationPoint
engine.set_synth_instrument(patch, destination_id=0)
cutoff_id = engine.resolve_instrument_automation_id(0, "cutoffHz")
engine.set_automation_lane(cutoff_id, [
AutomationPoint(ppq=0, value=400, curve_to_next="exponential"),
AutomationPoint(ppq=8, value=6000),
])WASM and Node take resolveInstrumentAutomationId(destinationId, paramName); Python takes resolve_instrument_automation_id(destination_id, param_name), the same order, and destination_id has no default.
The id encodes the destination slot as well as the parameter, so it survives an unbind and rebind of the same destination_id and simply applies nothing while that destination has no instrument bound. Structural fields — preset, engine mode, waveform, filter model, unison, polyphony — are not automatable: they resize voice pools or swap DSP topology, which is not audio-thread safe. Rebind the instrument with a new patch instead.
The engine holds 32 instrument-automation slots; once they are claimed, further resolutions fail rather than retargeting an existing lane. Resolution throws RangeError on WASM and Node (SonareValueError on Python) — or returns SONARE_ERROR_INVALID_PARAMETER at the C entry point sonare_engine_resolve_instrument_automation_id — when the key is unknown, no instrument is bound to that destination, the instrument exposes no automatable parameters, or the slot table is full. Resolution never returns a sentinel id, so the returned id can go straight to a lane. In a build without the arrangement subsystem the C entry point returns SONARE_ERROR_NOT_SUPPORTED, and that build reports instrumentParamAutomation: false in its capability JSON — the way a host detects up front that the resolver will not answer. See capabilities().
setParamSmoothingMs(ms) sets the time constant of the ramps started by setParameter(..., { smoothed: true }), whatever id they target. On the SonareEngine facade that includes setStripFaderDb, setStripPan, and setBusGainDb, which send smoothed fader, pan, and bus-gain changes. The default is 20 ms; 0 makes those changes immediate. It does not affect automation-lane playback, insert and instrument automation (fixed 5 ms smoothers), MIDI-CC mappings (applied directly), or the lane fader, pan, and gate smoothers. Set it once from the control thread before playback.
How a lane plays back
A lane is a breakpoint curve, not a list of scheduled jumps. Each point is { ppq, value, curveToNext? }, and a point's curveToNext shapes the segment that leads to the next point:
curveToNext | Segment to the next point |
|---|---|
'linear' | a straight line between the two values |
'exponential' | interpolates the magnitude in the log domain, so a frequency or linear-gain sweep sounds even; two values of opposite sign fall back to linear, and 0 counts as positive (see the warning below). A faderDb lane is already in dB, where linear is the even fade (see exponential on a dB lane) |
'hold' | keeps this point's value until the next point, then steps |
'sCurve' | eases out of this point and into the next (smoothstep) |
Do not fade linear gain to 0 with exponential. The log domain needs a non-zero value, so a segment that ends at 0 spends almost all of its length near 0: about 3e-5 (-90 dB) halfway, effectively an instant drop. Fade linear gain to a small value such as 0.001 (-60 dB), or automate faderDb linearly.
Outside the curve the lane is flat: before the first point it returns the first value, after the last point the last value, and nothing extrapolates. Points are sorted by ppq on receipt, and two points at the same ppq collapse to the first one you supplied — an instantaneous jump is a hold segment (or a second point placed a hair later), not two coincident points. A non-finite ppq or value is a RangeError, and so is a curve that is not one of those four names (a number is a TypeError), rather than being clamped.
The audio thread samples the curve at every breakpoint — the block is split there, so a breakpoint lands on its exact frame — and every 64 frames in between, widened only for blocks so large that the fixed boundary list would overflow. Each sampled value goes to the target the way a live setParameter would: mixer fader, pan and bus gain, insert parameters and instrument parameters run through their own fixed smoothers (5 ms for lane fader and pan, insert, and instrument parameters), so a step in the curve is a short glide; a parameter registered with addParameter is set directly and steps. A block holding more breakpoints than the boundary list can carry is reported as 'boundaryOverflow', and the surplus breakpoints apply at the next 64-frame boundary instead of their own frame.
Writing a lane while audio is running does not glitch. setAutomationLane(id, points) replaces the lane for that one id and leaves the others in place. The control thread builds the new lane set and publishes it; the audio thread adopts the newest set once, at the start of its next block — never mid-block, never with a lock or an allocation. What can be audible is the value you wrote: if the new curve differs from the old one at the current playhead, the target moves to the new value at the next sub-block — a glide on a smoothed target, a step on a directly-set one. A lane aimed at an id nothing is bound to is skipped and reported on drainTelemetry() as 'unknownTarget'; a parameter registered with rtSafe: false is refused up front (a RangeError on WASM and Node, SONARE_ERROR_INVALID_PARAMETER at the C entry point).
To clear a lane, pass an empty point list: setAutomationLane(id, []). After the audio thread adopts the clear, the target restores the last value sent through setParameter. If no manual value was ever sent for that id, the current value remains unchanged. The result is the same regardless of whether the manual write or the lane clear reaches the audio thread first. Python passes []; the C API uses point_count == 0.
Audio clips: warp mode and page underruns
setClips(clips) replaces the engine's whole audio clip schedule in one call, on the control thread. A clip is either direct (channels: one Float32Array per channel) or paged (pageProvider: a provider the host feeds page by page — see Paged clip audio streaming); startPpq places it, lengthSamples, clipOffsetSamples, loop, gain and the fade lengths shape it, and warpMode plus warpAnchors decide how it follows the tempo map. Python spells the same call set_clips with warp_mode.
engine.setClips([{
id: 1, trackId: 1, channels: [left, right],
startPpq: 0, lengthSamples: barLength,
warpMode: 'timeStretch', // 'off' | 'repitch' | 'tempoSync' | 'timeStretch'
warpAnchors: [ // warpSample: from the clip start; sourceSample: into the source
{ warpSample: 0, sourceSample: 0 },
{ warpSample: barLength, sourceSample: sourceBarLength },
],
}]);setClips replaces the whole schedule. To add, replace, or remove a single clip without resending the others, use upsertClip(clip) (replaces the clip with the same id, or adds it) and removeClip(clipId) (RangeError when no clip has that id; Python SonareValueError). The other clips are not copied again, and a refused clip leaves the published set unchanged. Python spells them upsert_clip and remove_clip.
- stretched
- compressed
- warp anchor
Anchors must be finite, non-negative and strictly increasing on both axes, or the call is rejected. 'repitch' and 'timeStretch' read the same anchor map on the audio thread; with fewer than two anchors both play the clip at its native rate, under either mode the map alone decides the source position (clipOffsetSamples is not added on top), and the loop-seam crossfade is not applied. 'tempoSync' is different in kind on the raw engine: the stretched audio is baked when setClips runs, so a tempoSync clip cannot be paged, cannot loop, and must keep clipOffsetSamples inside the source — each is refused. What the modes mean musically, and when to prefer one, is on Warp and Tempo Sync.
The 'timeStretch' voice budget
A 'timeStretch' clip borrows one preallocated stretcher voice for each block it renders. The pool capacity is 8 by default, and each voice handles up to two channels. Set it from the control thread with setWarpVoiceCapacity(voices) and read it with the warpVoiceCapacity property; Python uses set_warp_voice_capacity() and warp_voice_capacity(), while C uses sonare_engine_set_warp_voice_capacity() and sonare_engine_warp_voice_capacity(). The accepted range is 0..64. A value above 64 is rejected and leaves the previous capacity unchanged. Capacity 0 disables time-stretch for these clips: they use the 'repitch' path, and those fallbacks do not increment warpStretchOverflowCount (warp_stretch_overflow_count in Python, sonare_engine_warp_stretch_overflow_count() in C). When the engine is already prepared, changing the capacity rebuilds the voice pool immediately, so any clip using a voice restarts its WSOLA state.
A clip keeps the voice it used last block; a new clip takes a free voice, or the voice that has been idle longest — a voice still producing output is never stolen mid-note. If no voice is available, the clip uses the 'repitch' path for that block and the overflow counter increments. A source with more than two channels also uses 'repitch' because it cannot fit the stretcher state. The counter is monotonic within a prepared session and resets on prepare. A seek, a loop wrap, or a voice reassignment restarts the stretcher stream at the new position, so the join is a clean start rather than a smear of the previous one.
What a page underrun looks like
A paged clip reads its samples from the provider on the audio thread. When the page holding a sample is not resident, the read is a page miss: that clip contributes silence for that sample, every other clip and instrument renders as usual, and the transport keeps rolling — the engine never stalls waiting for storage. The miss is queued as a page request for the host to serve, and it is reported once per block on drainTelemetry() as the error 'clipPageUnderrun' with value set to the clip id. That record is the dropout signal.
To keep the record from ever appearing, the player also asks for the pages it is about to read: by default the half second of timeline ahead of each block, through the same request queue. The window, its setter and how it interacts with the JS-side streaming window are on Lookahead. A request does not say whether it was a miss or a lookahead — only the telemetry record marks a miss — so serve every request promptly. If the bounded request queue fills, clipPageRequestOverflowCount rises; the dropped pages are asked for again on the following block, but every block that arrives before they are served is a block of silence.
Recognising a paging dropout
A gap in one clip that lines up with a 'clipPageUnderrun' record is the host supplying a page after the playhead reached it. Check clipPageRequestOverflowCount first — a rising value means requests are being dropped — then widen the lookahead or the streamer's read-ahead window. A dropout with no such record is not a paging problem; look at droppedRecords, command-queue overflow, or the block budget instead.
MIDI clip scheduling and sampleAtPpq
Audio clips have the clip schedule and page providers; MIDI clips have their own realtime schedule. setMidiClips(clips) replaces the engine's whole MIDI clip schedule in one call, and each clip routes its events to a MIDI destination id — the instrument bound with setBuiltinInstrument, setSynthInstrument, or setSf2Instrument (see MIDI Input for the destination model).
The schedule is compiled: timing is in absolute samples on the engine timeline, not PPQ. Use sampleAtPpq(ppq) to convert musical positions through the engine's tempo map — it integrates every setTempo / setTempoSegments change, so the result stays correct across tempo ramps.
setTempoSegments([{ startPpq, bpm, endBpm? }, ...]) and setTimeSignatureSegments([{ startPpq, numerator, denominator }, ...]) install a piecewise map on the control thread. A non-zero endBpm ramps from that segment's bpm; pass an empty array to clear the map and return to the most recent single value set with setTempo or setTimeSignature.
// UMP MIDI 1.0 channel-voice words (note-on = status 0x9, note-off = 0x8).
const noteOn = (ch: number, note: number, vel: number) => (0x2 << 28) | (0x9 << 20) | (ch << 16) | (note << 8) | vel;
const noteOff = (ch: number, note: number) => (0x2 << 28) | (0x8 << 20) | (ch << 16) | (note << 8);
const start = engine.sampleAtPpq(8); // tempo-map-aware
const length = engine.sampleAtPpq(16) - start;
engine.setMidiClips([{
id: 1, trackId: 1,
destinationId: 0, // the instrument destination that renders these events
startSample: start, startPpq: 8,
lengthSamples: length, loop: true, loopLengthSamples: length,
events: [
// renderFrame is an absolute engine-timeline sample; an omitted wordCount is
// inferred from the last nonzero word.
{ renderFrame: start, word0: noteOn(0, 60, 100) },
{ renderFrame: start + Math.floor(length / 2), word0: noteOff(0, 60) },
],
}]);Looping clips require a positive loopLengthSamples and repeat their events at that interval. Omit loopLengthSamples on a one-shot clip. To clear the schedule, call setMidiClips([]). If you work at the project level instead (notes in PPQ, takes, comping), build the arrangement with Project Editing and bounce it — this realtime schedule is the lower-level API a DAW front end compiles into.
A clip carries gain (linear, default 1), fadeInSamples and fadeOutSamples (default 0, over the clip's full length rather than per loop repeat), applied to the destination's rendered instrument output rather than to the events themselves — note timing and velocity are unaffected. Per destination, the envelope follows the most recently started active clip. When that clip ends, an older clip that is still active takes over; only when none is active does the destination hold the most recently ended clip's end value (0 after a fade-out, its gain otherwise). If clips start together, the larger clip id wins. Before any clip on a destination has started, the destination plays at unity. Several tracks routed to the same destination share this envelope — the selected clip governs the whole destination, not one clip per track. fadeOutSamples above 0 is rejected when lengthSamples is omitted (open-ended): a clip with no end has nothing to fade toward. An explicit lengthSamples of 0 is refused. A zero-initialized SonareEngineMidiClipSchedule on the C ABI is silent (gain reads 0); the JS and Python bindings default an omitted gain to 1 instead.
Replacing an instrument during playback
Rebinding or clearing a destination's instrument takes effect while the transport rolls, on the raw engine and the SonareEngine facade alike. The calls are setBuiltinInstrument, setSynthInstrument, setSf2Instrument, loadSoundFont, setMidiFx, and clearMidiFx.
| Part | Behavior |
|---|---|
| Outgoing instrument | Fades out over 10 ms instead of being cut at a block boundary. You may destroy it as soon as the call returns. |
| Incoming instrument | Starts from its default channel state. The engine does not replay program, bank, or controller state, so resend what the new instrument needs after the change. |
| Tail length | tailSamples is a conservative upper bound on the longest audible tail. Size a bounce or freeze from it, not from the 10 ms fade. |
Every released, destroyed or closed engine, project, renderer or other native object raises a coded InvalidState on Node, WASM and Python; releasing twice is a no-op. A failure caused by earlier configuration (a full insert-automation, strip, MIDI CC, external-destination, controller-binding or sidechain table, a master-strip setter before a master strip exists, a bus or lane change that earlier routing, pan or retained delay forbids, a refused sidechain plan, or binding an instrument to a full rack) is also InvalidState, while a refused argument is a RangeError (Python SonareValueError).
Running the engine in an AudioWorklet
The regular WASM package exposes this RealtimeEngine class directly. To run it on the realtime audio thread, the worklet bridge hosts the same embind-backed engine inside AudioWorkletGlobalScope, and the higher-level SonareEngine facade mirrors nearly the whole engine surface to the worklet through control messages. See Realtime and Streaming — AudioWorklet notes for the bridge setup, the SonareEngine facade table, and worklet-side scope snapshots.
Related
- Mixing on the Realtime Engine — lanes, buses, strips, PFL/AFL taps, group routing, sidechains, and surround buses
- Realtime and Streaming —
StreamAnalyzer, the AudioWorklet bridge, and paged clip streaming - Mixing Engine — the standalone strip/bus/send mixer this engine's lane mixer shares its strip model with
- MIDI Input · Recording and Takes — live MIDI into the engine, and capturing what it plays