8.2 KiB
Debugging Synthesizers and Generators
Inspect live RT-thread state — output samples, inner-node values, per-sample compute — without breaking real-time safety.
Overview
Two problems bring you here:
- A Synthesizer subclass produces wrong or no output. You want to see what a specific voice — often one instance out of a polyphonic pool — is actually emitting, and what its internal generators are doing.
- A new Generator's
_computeSamplesis off. You want to step the compute loop for one instance and watch its state and output evolve.
Both run on the RT render thread, where the usual rules apply: no allocation,
locks, print, or String construction (see doc:SignalFlow and the
SynthesisEngine contract). A breakpoint is safe — it halts the thread and you
inspect at leisure. Any code you add to a debug hook is not exempt from the RT
rules. This article covers both: setting instance-scoped breakpoints, and reading
state safely.
The tap's invocation is compiled out of release builds (the call site in
evaluate(graph:count:) is wrapped in #if DEBUG), and it is gated per-instance, so
nothing you add for debugging runs in shipping audio. The debugTap method and the
debugTapEnabled flag still exist in release — an override compiles unconditionally — but
the hook is simply never called there.
The debug tap — a per-instance breakpoint
SynthesizerBase/evaluate(graph:count:) is final, so you cannot hook it
directly. Instead override SynthesizerBase/debugTap(graph:count:outputs:) — an
open no-op the engine calls once per scheduling segment, after every output
generator has run, but only when SynthesizerBase/debugTapEnabled is set on that
instance.
final class FMTone: SynthesizerBase {
@LiveParam var carrierFreq: Double = 440.0
override func build() {
let mod: any Generator = OscSin.Audio(freq: 220.0) * 660.0
output(bus: 0, source: OscSin.Audio(freq: $carrierFreq + mod) * 0.18)
}
override func debugTap(
graph: SynthGraph,
count: Int,
outputs: [any Generator]
) {
// Breakpoint here. The RT thread halts with self, outputs, and graph
// in scope:
// po self.carrierFreq
// po outputs.first?._cachedResult // Out's samples this block
// po outputs.first?._result[0].prefix(8) // first 8 samples
let block: Int = count
_ = block
}
}
Enable it per instance — flip only the voice you want to inspect:
let voice: FMTone = FMTone()
#if DEBUG
voice.debugTapEnabled = true // main thread, before add — only this voice taps
#endif
try engine.add(voice)
In a polyphonic pool, gate the flag so a single note breaks:
for note: Int in chord {
let v: FMTone = FMTone()
v.debugTapEnabled = (note == 60) // break only middle C
try engine.add(v)
}
What is in scope when it halts
| In scope | Holds |
|---|---|
self |
Every let / @LiveParam on the voice. |
outputs |
This instance's registered output-generator tree (roots only — typically Out / ReplaceOut). Each generator's _cachedResult / _result = samples just computed for this block. |
graph.engine |
Sample rate, block size, bus pool — read a bus mid-render. |
count |
Samples in this scheduling segment (≤ 512). The engine splits an audio block at sample-accurate scheduled-event boundaries, so a block containing scheduled events fires the tap once per segment — count is the segment length, not necessarily the full block. |
Inspecting inner nodes
outputs gives you the roots only. Every generator stores its inputs
private (BinaryOpGenerator.lhs / rhs, oscillator freq inputs, …), so you
cannot walk the tree down to an interior node like the mod above from
outside. To inspect an inner node, keep a reference to it in build():
final class FMTone: SynthesizerBase {
@LiveParam var carrierFreq: Double = 440.0
#if DEBUG
// nonisolated(unsafe): written in build() on main before the add barrier,
// read on the RT thread in debugTap — same happens-before as outputGenerators.
private nonisolated(unsafe) var _dbgMod: (any Generator)?
#endif
override func build() {
let mod: any Generator = OscSin.Audio(freq: 220.0) * 660.0
output(bus: 0, source: OscSin.Audio(freq: $carrierFreq + mod) * 0.18)
#if DEBUG
_dbgMod = mod
(mod as? GeneratorBase)?.debugTag = "mod"
#endif
}
override func debugTap(
graph: SynthGraph,
count: Int,
outputs: [any Generator]
) {
#if DEBUG
let modOut: [Double]? = _dbgMod?._cachedResult.first
_ = modOut // po modOut?.prefix(8) — mod's samples this block
#endif
}
}
_cachedResult vs _result, and why the read is free
The tap fires after the generator loop, so mod has already run this block —
the fan-out cache (see doc:GeneratorProtocol) holds its output. Reading
_dbgMod?._cachedResult is a post-hoc read of that cache; it does not
recompute. Calling _dbgMod?.calculatedSamples(graph:count:) again would also just
return the cache (same _segmentSerial), so it will not re-enter the compute loop
either. For a mono generator _cachedResult[0] shares backing storage with
_result[0] — either is fine to read.
debugTag for identifying an instance
GeneratorBase/debugTag (DEBUG-only) is a human-readable label you set in
build() so a debugger can recognise one specific instance among many that share
the same class. It is never read by framework code on the RT thread — it exists
purely for LLDB conditions and hand-written taps, as shown next.
Stepping into a Generator's compute loop
The tap is post-hoc: it shows results, not the live per-sample computation. To step
the actual loop for one instance, breakpoint the generator's own
_computeSamples (see doc:WritingGenerators) and scope it to your instance —
otherwise you stop on every oscillator/multiply in the program.
Using debugTag:
(lldb) b BinaryOpGenerator._computeSamples
(lldb) breakpoint modify -c 'self.debugTag == "mod"'
Or, if you did not set a tag, capture identity from the tap and match on the pointer:
(lldb) p _dbgMod! // hit the tap first
(lldb) breakpoint modify -c 'ObjectIdentifier(self) == ObjectIdentifier(<ref>)'
Continue — you now step the multiply loop for that one mod, sample by sample. To
step into the modulator oscillator instead, hold and tag it too (let modOsc = OscSin.Audio(...); modOsc.debugTag = "modOsc") and breakpoint
OscSin.Audio._computeSamples with the same condition.
Note:
_computeSamplesmay run several times per audio block in sub-block mode (ControlGenerator) —countis the sub-block length, not the full block. See doc:WritingGenerators for when the sub-block loop runs.
RT rules while debugging
- A breakpoint anywhere on the RT thread is fine — it stops everything and you look. This is the primary technique.
- Code inside
SynthesizerBase/debugTap(graph:count:outputs:)(or any RT path) still obeys the RT rules: no allocation, locks,print, orStringconstruction. Reading a pre-computed buffer and comparing numbers is fine; formatting a log line is not. - Want continuous inspection without halting? Snapshot the values you care
about into a pre-allocated ring buffer inside the tap, and drain/format them
on the main thread. This is exactly how
NodeTreeMirrorpublishes RT state to the UI — see doc:DiagnosticsGuide.
When to reach for other tools
| Symptom | Tool |
|---|---|
| Need user-visible runtime messages, or a live node-tree view | doc:DiagnosticsGuide (LyrebirdMessage, NodeTreeMirror) |
| Want to inspect a whole signal offline, sample by sample | doc:NRTGuide — render to a buffer/file and analyse the samples |
| Verifying scheduling / timing | doc:Scheduling |
| A new Generator is structurally wrong (not just numerically) | doc:WritingGenerators testing checklist |