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
@@ -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"
}