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