Armed recording follows external MIDI clock Start/Stop, with its tail (0.34.0)

MidiClock reports Start/Continue/Stop to an onTransport hook before the
receive gate; the engine's recorder now runs while the sequencer plays or an
external clock runs, so an armed take starts on clock Start (also in
sound-module mode) and ends a TAIL after clock Stop. Sizzle's own Stop ends
a clock-started take too. ClockRecordingTest covers start, tail, disarmed
and stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-13 10:26:10 +01:00
co-authored by Claude Opus 5.5
parent 3088912264
commit e38e2a2707
9 changed files with 141 additions and 7 deletions
+8
View File
@@ -4,6 +4,14 @@ Sizzletracker for Android (`cc.tiredsysadmin.android.sizzle`). One entry per
version in `versions.properties`; versions before 0.28.0 are described in the git
log (one commit per version).
## 0.34.0 (versionCode 65)
- **Recording follows MIDI clock:** while ● ARM is on, an external clock Start (or
Continue) starts the master recording and its Stop ends it after the TAIL, the
same as Sizzle's own Play/Stop. Works in sound-module mode, where the clock
doesn't start Sizzle's song, and whether or not "Receive MIDI clock" is on.
Sizzle's own Stop also ends a clock-started take.
## 0.33.0 (versionCode 64)
- **Mix tab:** long-press an instrument or FX slot to open that device's settings
@@ -193,6 +193,12 @@ class SizzleApp : Application() {
main.post { audioEngine.pause() }
}
}
// An armed master recording follows the external clock's Start/Stop in every
// mode (in sound-module mode the clock doesn't start Sizzle's own song).
midiClock.onTransport = { start ->
if (MidiTrace.on && audioEngine.recordArmed) MidiTrace.log("clock ${if (start) "Start: armed recording starts" else "Stop: recording ends after its tail"}")
main.post { if (start) audioEngine.externalClockStart() else audioEngine.externalClockStop() }
}
midiClock.onReceiveTempo = { bpm -> main.post { project.tempoBpm = bpm.coerceIn(20f, 300f) } }
val s = runBlocking { settingsStore.load() }
midiClock.sendEnabled = s.sendMidiClock
@@ -306,6 +306,10 @@ class AudioEngine(
// Audio-thread recording state machine (see [recordSample]). Initialized in
// [maybeStartRecording] before the volatile [recCapturing] flag publishes them.
@Volatile private var recCapturing = false
/** An external MIDI clock is running (between its Start and Stop): an armed
* recording captures while either it or the sequencer runs, and its tail starts
* when both have stopped. */
@Volatile private var extClockRunning = false
private var recWasPlaying = false
private var recTailRemaining = 0
@@ -334,7 +338,7 @@ class AudioEngine(
* regardless of [playing], so the tail contains real decaying reverb/delay audio.
*/
private fun recordSample(proj: Project?, left: Float, right: Float) {
val nowPlaying = playing
val nowPlaying = playing || extClockRunning
if (recWasPlaying && !nowPlaying) {
recTailRemaining = if (proj == null) 0
else (recTailBars * proj.timeSignature.linesPerBar * samplesPerLine(proj)).toInt()
@@ -351,6 +355,17 @@ class AudioEngine(
}
}
/** An external MIDI clock sent Start (or Continue): start an armed recording, even
* while Sizzle's own sequencer stays stopped (sound-module mode). */
fun externalClockStart() {
extClockRunning = true
maybeStartRecording()
}
/** The external clock sent Stop: an armed recording ends after its FX tail (once
* Sizzle's own sequencer isn't playing either). */
fun externalClockStop() { extClockRunning = false }
fun setProject(p: Project) {
project = p
rebuildArrangement()
@@ -558,7 +573,7 @@ class AudioEngine(
publish()
}
fun pause() { playing = false; allSeqVoicesOff(); publish() }
fun pause() { playing = false; extClockRunning = false; allSeqVoicesOff(); publish() }
/**
* MIDI panic: immediately hard-silence every voice — sequencer, live (keyboard/
@@ -583,6 +598,7 @@ class AudioEngine(
/** Stop playback. If already stopped, rewind to the very beginning. */
fun stop() {
extClockRunning = false // Sizzle's Stop also ends a take an external clock started
if (!playing && currentLine == 0 && playlistIndex == 0 && lineInBeat == 0) return
val wasStopped = !playing
playing = false
@@ -151,7 +151,17 @@ class MidiClock(context: Context) {
private var pulseAccum = 0.0
private var pulseCount = 0
/** An external transport edge (Start / Continue = true, Stop = false), reported
* whether or not [receiveEnabled]: an armed master recording follows another
* app's or device's Start/Stop even when Sizzle doesn't follow its clock. Called
* on the MIDI port's thread. */
var onTransport: ((Boolean) -> Unit)? = null
fun handleRealtime(status: Int) {
when (status) {
0xFA, 0xFB -> onTransport?.invoke(true)
0xFC -> onTransport?.invoke(false)
}
if (!receiveEnabled) return
when (status) {
0xFA, 0xFB -> { resetTempoEstimate(); onReceiveStart?.invoke() } // Start / Continue
@@ -148,7 +148,7 @@ private fun RecordToolbar(vm: AppViewModel) {
val status = when {
recording -> "recording…"
vm.recordDirUri == null -> "pick a folder to record"
vm.recordArmed -> "armed — starts on play"
vm.recordArmed -> "armed — starts on play or MIDI clock start"
vm.lastRecordingName != null -> "saved ${vm.lastRecordingName}"
else -> "master → WAV"
}
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.audio
import cc.tiredsysadmin.android.sizzle.model.Project
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File
import java.io.RandomAccessFile
import java.nio.file.Files
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/** An armed master recording follows an external MIDI clock's Start/Stop, with its tail. */
class ClockRecordingTest {
private val fs = 48_000
private val block = 192
private fun engine(dir: File, tailBars: Int, done: (File) -> Unit) = AudioEngine(fs).apply {
setProject(Project().apply { tempoBpm = 120f })
recTempDir = dir
onRecordingComplete = { f, _ ->
val copy = File(dir, "take-${System.nanoTime()}.wav"); f.copyTo(copy); done(copy)
}
setRecordingArm(true, tailBars)
}
/** Render [frames], paced (about 10x real time) so the recorder's writer thread
* keeps up with its one-second ring, as it does on a device. */
private fun AudioEngine.run(frames: Int) {
val out = FloatArray(block * 2)
repeat(frames / block) { i -> fillBlock(out, block); if (i % 25 == 24) Thread.sleep(10) }
}
/** Sample frames in a 16-bit stereo WAV written by [MasterRecorder] (header patched on close). */
private fun frames(f: File): Long = RandomAccessFile(f, "r").use { it.length() - 44 } / 4
@Test fun clockStartRecordsAndStopEndsAfterTheTail() {
val dir = Files.createTempDirectory("rec").toFile()
val latch = CountDownLatch(1)
var take: File? = null
val e = engine(dir, tailBars = 1) { take = it; latch.countDown() }
e.run(fs) // armed, no clock: nothing
assertNull(take)
e.externalClockStart()
e.run(fs * 2) // two seconds of "the other app playing"
e.externalClockStop()
e.run(fs * 3) // 1 bar of 4/4 at 120 BPM = 2 s of tail, then done
assertTrue("the take finished", latch.await(5, TimeUnit.SECONDS))
val n = frames(assertNotNull(take).let { take!! })
// 2 s of clock + 2 s of tail (to a block)
assertEquals((fs * 4).toDouble(), n.toDouble(), block * 2.0)
}
@Test fun notArmedRecordsNothing() {
val dir = Files.createTempDirectory("rec").toFile()
var take: File? = null
val e = engine(dir, tailBars = 0) { take = it }
e.setRecordingArm(false, 0)
e.externalClockStart(); e.run(fs); e.externalClockStop(); e.run(fs)
Thread.sleep(200)
assertNull(take)
}
@Test fun sizzlesStopEndsAClockTake() {
val dir = Files.createTempDirectory("rec").toFile()
val latch = CountDownLatch(1)
val e = engine(dir, tailBars = 0) { latch.countDown() }
e.externalClockStart(); e.run(fs)
e.stop() // the user stops in Sizzle
e.run(fs / 10)
assertTrue(latch.await(5, TimeUnit.SECONDS))
assertFalse(e.transport.value.isPlaying)
}
}
+8
View File
@@ -397,6 +397,14 @@ one is treated as dead too (events within 1 s of our own open/close are ignored)
USB and Bluetooth sources are not probed: Android reports their removal.
`MidiInput` logs connects/reconnects under the `SizzleMidi` tag.
**Recording on the clock (0.34.0).** `MidiClock.onTransport` reports every
Start/Continue/Stop byte, before the `receiveEnabled` gate, and `SizzleApp` forwards
it to `AudioEngine.externalClockStart/Stop`. The master recorder's state machine
treats "the sequencer plays OR an external clock runs" as running: Start begins an
armed take (`maybeStartRecording`), and the TAIL countdown starts on the edge where
both have stopped. `pause()` / `stop()` clear the external flag, so Sizzle's own
Stop ends a clock-started take. `ClockRecordingTest` covers start, tail and stop.
**Tracing (0.29.1).** `input/MidiTrace` logs, when switched on with
`adb shell setprop log.tag.SizzleMidiTrace VERBOSE` (re-read every 500 ms, free
when off), every received packet (`recv <source> [bytes] <decoded> late=…ms`), each
+7 -2
View File
@@ -193,8 +193,13 @@ editor as in the Toolbox); **CLOSE** or Back brings you straight back to the Mix
The strip along the top controls **recording** of the master output:
- **● ARM** — arm recording; capture begins when you press play.
- **TAIL** — how many bars of effect tail to keep after the sequencer stops.
- **● ARM** — arm recording; capture begins when you press play, or when a MIDI
clock Start arrives from another app or device (Mikro Seq, a DAW, a drum
machine), even while Sizzle plays only as a MIDI sound module. A clock Stop (or
Sizzle's own Stop) ends the take after its tail. Each Start/Stop while armed is a
new take.
- **TAIL** — how many bars of effect tail to keep after the sequencer or the
external clock stops.
- **DIR** — the destination folder for the rendered file.
- **master → WAV** — the master bus is captured to a stereo WAV.
+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.33.0
android.versionCode = 64
android.versionName = 0.34.0
android.versionCode = 65
# 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