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:
co-authored by
Claude Opus 5
parent
deb582727f
commit
c2aedcc95c
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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. */
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@
|
||||
# or polish. android.versionCode is monotonic and bumps by 1 on every release,
|
||||
# because F-Droid and the Android package manager order installs by it alone.
|
||||
|
||||
android.versionName = 0.24.0
|
||||
android.versionCode = 53
|
||||
android.versionName = 0.25.0
|
||||
android.versionCode = 54
|
||||
|
||||
# No desktop builds exist yet. These lines are the agreed slots so a port starts
|
||||
# with a version instead of inventing one; `-` means "not built on this
|
||||
|
||||
Reference in New Issue
Block a user