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:
co-authored by
Claude Opus 5.5
parent
3088912264
commit
e38e2a2707
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user