diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthPatch.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthPatch.kt index 1fc0be2..2c40d8e 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthPatch.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthPatch.kt @@ -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, ) } diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthVoice.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthVoice.kt index e4a4404..ec25160 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthVoice.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/audio/SynthVoice.kt @@ -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 diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/SynthDefs.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/SynthDefs.kt index 644e252..d786ddd 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/SynthDefs.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/SynthDefs.kt @@ -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. */ diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/Toolbox.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/Toolbox.kt index f9b6307..5f6ee93 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/Toolbox.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/model/Toolbox.kt @@ -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)) diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/toolbox/NesSynthEditor.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/toolbox/NesSynthEditor.kt index 0c16776..3ef13da 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/toolbox/NesSynthEditor.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/toolbox/NesSynthEditor.kt @@ -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)) } } } diff --git a/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/SynthAmpModTest.kt b/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/SynthAmpModTest.kt index 5c62855..0d02a18 100644 --- a/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/SynthAmpModTest.kt +++ b/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/SynthAmpModTest.kt @@ -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 { 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. */ diff --git a/versions.properties b/versions.properties index 6cc7d31..5bcd712 100644 --- a/versions.properties +++ b/versions.properties @@ -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