Compare commits

..
2 Commits
Author SHA1 Message Date
ReactorcoremeltdownandClaude Opus 5 68e9ed8b41 Edge-scroll the arrangement roll while dragging a selection (v0.26.0)
A long-press selection drag could only reach what was already on screen — about
four bars — so selecting a long span meant selecting, scrolling, and selecting
again. Dragging into either edge of the roll's viewport now scrolls it under the
finger, and the selection keeps extending while it moves.

Speed ramps with how deep into the 56.dp edge zone the finger is, so easing in
creeps for fine work and pushing to the rim covers roughly 15 bars a second.
Both edges, not just the right one: scrolling out is not much use without
scrolling back, and a drag that overshoots has to be able to return.

The subtlety is that a pointer's canvas-local x goes stale the moment the view
moves beneath it, and no new pointer event arrives at all while the finger is
held still at the rim — which is exactly the case that has to keep working. The
drag therefore tracks a VIEWPORT-relative x and recomputes the beat against the
live scroll.value each frame, rather than trusting the last reported position.

The per-frame loop only runs between onDragStart and onDragEnd/onDragCancel,
and it applies a selection update only when the beat under the finger actually
changes, so an edge scroll does not bump the revision sixty times a second.

Verified on an emulator with synthesised motionevents (adb input swipe cannot
hold still long enough to trigger a long press): a right-edge drag carried the
view from bar 1 to bar 34 with the selection following; a left-edge drag
carried it back to bar 12; the scroll stops dead on finger lift, with no drift
four seconds later; and an ordinary drag inside the viewport still selects a
single bar without scrolling at all.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-11-03 22:10:18 +01:00
ReactorcoremeltdownandClaude Opus 5 c2aedcc95c 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>
2026-11-03 21:53:04 +01:00
8 changed files with 186 additions and 27 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))
}
}
}
@@ -7,6 +7,7 @@ import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@@ -27,6 +28,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
@@ -41,6 +43,10 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import cc.tiredsysadmin.android.sizzle.model.Arrangement as ArrModel
import cc.tiredsysadmin.android.sizzle.model.LoopRegion
import cc.tiredsysadmin.android.sizzle.ui.AppViewModel
@@ -230,26 +236,96 @@ fun ArrangementRoll(vm: AppViewModel) {
}
}
// Long-press then drag sweeps out a rectangular selection.
.pointerInput(arr, sig) {
// Dragging into either edge of the viewport scrolls the
// roll under the finger, so a selection can run far past
// what fits on screen.
.pointerInput(arr, sig, scroll) {
val edgeZonePx = EDGE_SCROLL_ZONE.toPx()
val maxSpeedPx = EDGE_SCROLL_MAX_SPEED.toPx()
fun laneAt(off: Offset): Int {
val laneH = (size.height - rulerHeightPx) / ArrModel.LANE_COUNT
return ((off.y - rulerHeightPx) / laneH).toInt()
.coerceIn(0, ArrModel.LANE_COUNT - 1)
}
fun beatAt(off: Offset): Int =
(off.x / beatWidthPx).toInt().coerceIn(0, lengthBeats - 1)
// Beat under a VIEWPORT-relative x. The canvas is wider
// than the viewport and slides under the finger while
// edge-scrolling, so a pointer's canvas-local x goes
// stale the instant the view moves — and no new pointer
// event arrives while the finger is held still at the
// rim. scroll.value is the live half of the position.
fun beatAtViewportX(vx: Float): Int =
((scroll.value + vx) / beatWidthPx).toInt()
.coerceIn(0, lengthBeats - 1)
var dragLane0 = 0; var dragBeat0 = 0
var fingerVx = 0f // viewport-relative x of the finger
var fingerLane = 0
var lastLane = -1; var lastBeat = -1
fun applySelection() {
val beat = beatAtViewportX(fingerVx)
// An edge scroll ticks every frame but only crosses
// a beat boundary now and then; skipping the rest
// keeps it from bumping the revision 60x a second.
if (fingerLane == lastLane && beat == lastBeat) return
lastLane = fingerLane; lastBeat = beat
vm.setArrangementSelection(dragLane0, dragBeat0, fingerLane, beat)
}
coroutineScope {
var edgeJob: Job? = null
fun stopEdgeScroll() { edgeJob?.cancel(); edgeJob = null }
fun startEdgeScroll() {
if (edgeJob?.isActive == true) return
edgeJob = launch {
var last = 0L
while (isActive) {
val now = withFrameNanos { it }
val dt = if (last == 0L) 0f
else (now - last) / 1_000_000_000f
last = now
val w = viewportWidthPx
if (w <= 0 || dt <= 0f) continue
// Speed ramps with how deep into the
// edge zone the finger is: easing in
// creeps, pushing to the rim races.
val push = when {
fingerVx > w - edgeZonePx ->
(fingerVx - (w - edgeZonePx)) / edgeZonePx
fingerVx < edgeZonePx ->
-(edgeZonePx - fingerVx) / edgeZonePx
else -> 0f
}.coerceIn(-1f, 1f)
if (push == 0f) continue
val before = scroll.value
scroll.scrollBy(push * maxSpeedPx * dt)
if (scroll.value != before) applySelection()
}
}
}
detectDragGesturesAfterLongPress(
onDragStart = { off ->
dragLane0 = laneAt(off); dragBeat0 = beatAt(off)
vm.setArrangementSelection(dragLane0, dragBeat0, dragLane0, dragBeat0)
lastLane = -1; lastBeat = -1
fingerVx = off.x - scroll.value
fingerLane = laneAt(off)
dragLane0 = fingerLane
dragBeat0 = beatAtViewportX(fingerVx)
applySelection()
startEdgeScroll()
},
onDrag = { change, _ ->
vm.setArrangementSelection(
dragLane0, dragBeat0, laneAt(change.position), beatAt(change.position),
)
fingerVx = change.position.x - scroll.value
fingerLane = laneAt(change.position)
applySelection()
},
onDragEnd = { stopEdgeScroll() },
onDragCancel = { stopEdgeScroll() },
)
}
},
) {
// Subscribe the draw phase to edit revisions (live state read,
@@ -409,3 +485,17 @@ private fun RegionControl(name: String, region: LoopRegion, onTap: () -> Unit, v
RepeatButton("+") { vm.changeRegionRepeats(region, +1) }
}
}
/**
* How close to the viewport edge a selection drag has to get before the roll starts
* scrolling under it. A thumb's worth — small enough that an ordinary selection
* inside the visible range never trips it, wide enough to aim at.
*/
private val EDGE_SCROLL_ZONE = 56.dp
/**
* Edge-scroll speed at the very rim, per second. The roll is 20.dp per beat, so this
* is roughly 15 bars a second in 4/4 at full push — a 16-bar selection sweeps out in
* about a second, and the ramp below full push makes finer work practical.
*/
private val EDGE_SCROLL_MAX_SPEED = 1200.dp
@@ -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
View File
@@ -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.26.0
android.versionCode = 55
# 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