Add an LFO phase dropdown to the synth (v0.25.0)

The synth's LFO always started at phase 0 on every note, so a Square LFO could
only ever gate on-beat-first and a Saw could only ever ramp up. The new
lfophase parameter offsets it in eight 45-degree steps — enough to put the gate
on the off-beat (180) or land a ramp anywhere useful, without a dropdown nobody
wants to scroll.

The offset is applied where the LFO is READ, not seeded into lfoPhase at
note-on. Two reasons: setPatch swaps the patch under a sounding voice, so
turning the knob moves a held note's LFO immediately rather than waiting for
the next trigger; and it matches the toolbox LFO device, which offsets its
phase the same way. Sample & Hold is deliberately unaffected — it clocks on the
raw wrap, and a held random value has no phase to shift.

Presets saved before this existed have no lfophase key, read the default label
and land on 0, which is exactly what they played.

The control reuses the editor's existing EnumParam dropdown and sits beside
WAVE and RATE in the LFO card. Checked on an emulator that three dropdowns
still fit that row at the widest label (PHASE:180°) without crowding, and that
the selection sticks.

Tests: 180 degrees must inverse a square LFO's loud/quiet halves, 90 must leave
both halves audible (a shift, not just an inversion), and the labels must map
onto the cycle with an unknown or missing one falling back to no offset.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-03 21:53:04 +01:00
co-authored by Claude Opus 5
parent deb582727f
commit c2aedcc95c
7 changed files with 82 additions and 13 deletions
@@ -36,6 +36,7 @@ class SynthPatch(
// ---- LFO ----
val lfoWave: Int, // index into LFO_WAVES
val lfoHz: Float, // tempo-synced rate, precomputed from the division + tempo
val lfoPhase: Float, // start-phase offset as a fraction of a cycle, 0..1
// ---- modulation matrix (index 0..MOD_SLOTS-1) ----
val modSrc: IntArray, // index into MOD_SRC (0 == Off)
val modDst: IntArray, // index into MOD_DST
@@ -84,6 +85,11 @@ class SynthPatch(
aS = (slot?.float("sustain", 0.7f) ?: 0.7f), aR = (slot?.float("release", 0.2f) ?: 0.2f),
lfoWave = lfoWaveIndex(slot?.string("lfowave", "Tri") ?: "Tri"),
lfoHz = lfoHz,
// Absent in presets saved before the phase control existed; those
// read the default label and land on 0, which is what they played.
lfoPhase = SynthDefs.lfoPhaseFraction(
slot?.string("lfophase", SynthDefs.LFO_PHASES[0]) ?: SynthDefs.LFO_PHASES[0],
),
modSrc = src, modDst = dst, modAmt = amt,
)
}
@@ -242,7 +242,14 @@ class SynthVoice(private val sampleRate: Int) {
}
private fun lfoValue(): Float {
val p = lfoPhase
// The offset is applied at read time rather than seeded into lfoPhase at
// note-on, for two reasons: setPatch swaps the patch under a sounding voice,
// so turning the knob moves a held note's LFO immediately; and it matches the
// toolbox LFO device, which offsets its phase the same way. Sample & Hold is
// unaffected by design — it clocks on the raw wrap, and a held random value
// has no phase to shift.
var p = lfoPhase + patch.lfoPhase
if (p >= 1.0) p -= 1.0
return when (patch.lfoWave) {
1 -> (2.0 * p - 1.0).toFloat() // Saw
2 -> if (p < 0.5) 1f else -1f // Square
@@ -14,6 +14,20 @@ object SynthDefs {
val WAVES = listOf("Tri", "Saw", "Square", "Pulse25", "Pulse12")
/** LFO waveforms. */
val LFO_WAVES = listOf("Tri", "Saw", "Square", "Sine", "S&H")
/**
* LFO start-phase offsets. Eight 45-degree steps: enough to put a Square LFO's
* gate on the off-beat (180) or a Saw's ramp anywhere useful, without a dropdown
* nobody wants to scroll.
*/
val LFO_PHASES = listOf("0°", "45°", "90°", "135°",
"180°", "225°", "270°", "315°")
/** A [LFO_PHASES] label as a fraction of one LFO cycle (0..1). */
fun lfoPhaseFraction(label: String): Float {
val i = LFO_PHASES.indexOf(label)
return if (i < 0) 0f else i / LFO_PHASES.size.toFloat()
}
/** Modulation-matrix sources; index 0 ("Off") disables the slot. */
val MOD_SRC = listOf("Off", "LFO", "FiltEnv", "AmpEnv", "Vel", "KeyTrk")
/** Modulation-matrix destinations. */
@@ -77,6 +77,7 @@ enum class ToolboxType(
// Tempo-synced LFO (rate is an index into [TimeDivision]).
add(ParamSpec("lfowave", "LFO Wave", choices = SynthDefs.LFO_WAVES))
add(ParamSpec("lfodiv", "LFO Rate", 3f, 0f, 6f, isInt = true))
add(ParamSpec("lfophase", "LFO Phase", choices = SynthDefs.LFO_PHASES))
// Modulation matrix.
for (n in 0 until SynthDefs.MOD_SLOTS) {
add(ParamSpec("m${n}src", "Mod${n + 1} Src", choices = SynthDefs.MOD_SRC))
@@ -139,6 +139,8 @@ private fun AmpLfoCard(vm: AppViewModel, slot: ToolboxSlot, rev: Int) {
verticalAlignment = Alignment.CenterVertically) {
EnumParam(vm, slot, rev, "lfowave", "WAVE", SynthDefs.LFO_WAVES, "Tri", Modifier.weight(1f))
LfoRateDropdown(vm, slot, rev, Modifier.weight(1f))
EnumParam(vm, slot, rev, "lfophase", "PHASE", SynthDefs.LFO_PHASES,
SynthDefs.LFO_PHASES[0], Modifier.weight(1f))
}
}
}