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
@@ -4,6 +4,7 @@
package cc.tiredsysadmin.android.sizzle.audio
import cc.tiredsysadmin.android.sizzle.model.SynthDefs
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import kotlin.math.abs
@@ -41,7 +42,7 @@ class SynthAmpModTest {
}
/** A triangle-oscillator patch with one mod slot wired LFO -> Amp at full depth. */
private fun ampModPatch(lfoWave: String, amount: Float = 1f): SynthPatch {
private fun ampModPatch(lfoWave: String, amount: Float = 1f, phase: Float = 0f): SynthPatch {
val n = SynthDefs.OSC_COUNT
return SynthPatch(
oscWave = IntArray(n) { 0 }, // Tri on every oscillator
@@ -53,6 +54,7 @@ class SynthAmpModTest {
aA = 0.001f, aD = 0f, aS = 1f, aR = 0.1f, // flat sustain: no envelope motion
lfoWave = SynthDefs.LFO_WAVES.indexOf(lfoWave),
lfoHz = 8f, // 1/8 note at 120 BPM
lfoPhase = phase,
modSrc = IntArray(SynthPatch.MOD_SLOTS) { if (it == 0) 1 else 0 }, // LFO
modDst = IntArray(SynthPatch.MOD_SLOTS) { if (it == 0) 5 else 0 }, // Amp
modAmt = FloatArray(SynthPatch.MOD_SLOTS) { if (it == 0) amount else 0f },
@@ -92,34 +94,71 @@ class SynthAmpModTest {
/** The smoothing must not flatten the tremolo: a square LFO still has to swing
* the level most of the way between its two states within a half-cycle. */
@Test fun squareLfoStillModulatesTheLevel() {
/**
* Peaks of the two halves of one LFO cycle, in order. The LFO retriggers at
* phase 0 on note-on, so skipping a whole period lands back on an edge with the
* attack long settled; windows must be aligned to the half cycles or they
* straddle both states and read the same peak.
*/
private fun halfCyclePeaks(patch: SynthPatch): Pair<Float, Float> {
val v = SynthVoice(sr)
v.noteOn(48, 127, ampModPatch("Square"))
v.noteOn(48, 127, patch)
val period = sr / 8 // 8 Hz
// The LFO retriggers at phase 0 on note-on, so a whole period lands back on
// an edge with the attack long settled. Windows must be aligned to the half
// cycles or they straddle both states and read the same peak.
repeat(period) { v.render() }
// Peak over the back three-quarters of a half cycle, skipping the ramp in.
fun halfCyclePeak(): Float {
val half = period / 2
var peak = 0f
repeat(half) { i ->
val y = v.render()
if (i > half / 4) peak = maxOf(peak, abs(y))
if (i > half / 4) peak = maxOf(peak, abs(y)) // skip the ramp in
}
return peak
}
return halfCyclePeak() to halfCyclePeak()
}
val loud = halfCyclePeak() // LFO == +1 -> gain 2
val quiet = halfCyclePeak() // LFO == -1 -> gain 0
@Test fun squareLfoStillModulatesTheLevel() {
val (loud, quiet) = halfCyclePeaks(ampModPatch("Square"))
assertTrue(
"the LFO should still chop the level (loud=$loud quiet=$quiet)",
quiet < loud * 0.25f,
)
}
/** 180 degrees inverts a square LFO: the cycle that ran loud-then-quiet must
* now run quiet-then-loud. This is the whole point of the phase control. */
@Test fun halfCyclePhaseOffsetInvertsASquareLfo() {
val (loud, quiet) = halfCyclePeaks(ampModPatch("Square", phase = 0.5f))
assertTrue(
"180 degrees should start the cycle on the LFO's low half " +
"(first=$loud second=$quiet)",
loud < quiet * 0.25f,
)
}
/** A quarter turn lands mid-chop: the first half is split, so neither half is
* fully silent — proof the offset is a real shift and not just an inversion. */
@Test fun quarterPhaseOffsetShiftsRatherThanInverts() {
val (first, second) = halfCyclePeaks(ampModPatch("Square", phase = 0.25f))
assertTrue("a 90 degree offset should leave both halves audible " +
"(first=$first second=$second)", first > 0.01f && second > 0.01f)
}
/** The dropdown's labels must map onto the cycle, and an unknown or missing one
* must fall back to 0 so presets saved before the control existed play as they
* always did. */
@Test fun phaseLabelsMapOntoTheCycle() {
assertEquals(0f, SynthDefs.lfoPhaseFraction("0\u00b0"), 1e-6f)
assertEquals(0.25f, SynthDefs.lfoPhaseFraction("90\u00b0"), 1e-6f)
assertEquals(0.5f, SynthDefs.lfoPhaseFraction("180\u00b0"), 1e-6f)
assertEquals(0.875f, SynthDefs.lfoPhaseFraction("315\u00b0"), 1e-6f)
assertEquals("an unknown label falls back to no offset",
0f, SynthDefs.lfoPhaseFraction("nonsense"), 1e-6f)
assertEquals("a slot with no lfophase key plays unshifted",
0f, SynthPatch.from(null, 120f).lfoPhase, 1e-6f)
}
private companion object {
/** Generous: still ~30x the unmodulated tone's own per-sample motion, but
* far below the ~1.0 full-scale jump an unsmoothed square edge produces. */