MIDI trace, spaced block names survive save/load, clearer clock-receive text (v0.29.1)
- input/MidiTrace: optional (setprop log.tag.SizzleMidiTrace VERBOSE) log of every received packet with lateness and every note handed to the engine with the mixer strips it reaches; flags Sizzle's own transport playing and clock Start/Stop acting on it. AudioEngine.describeBusTargets backs it. - ProjectIo percent-encodes spaces/%/] in [PATTERN name=...], so "C1 muted" no longer comes back as "C1"; old files load unchanged. - "Receive MIDI clock" explains that it starts Sizzle's own song, which then plays along with incoming notes (the cause of "mutes don't work / only C2" reports when used as a sound module with an imported Mikro Seq song). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
df64af8c9b
commit
3c248028c9
@@ -13,6 +13,7 @@ import cc.tiredsysadmin.android.sizzle.input.InputRouter
|
||||
import cc.tiredsysadmin.android.sizzle.input.KeyboardInput
|
||||
import cc.tiredsysadmin.android.sizzle.input.InputAction
|
||||
import cc.tiredsysadmin.android.sizzle.input.MidiInput
|
||||
import cc.tiredsysadmin.android.sizzle.input.MidiTrace
|
||||
import cc.tiredsysadmin.android.sizzle.io.PresetLibrary
|
||||
import cc.tiredsysadmin.android.sizzle.io.ProjectStore
|
||||
import cc.tiredsysadmin.android.sizzle.io.SettingsStore
|
||||
@@ -61,9 +62,18 @@ class SizzleApp : Application() {
|
||||
* engine, so it keeps playing incoming notes with no Activity or ViewModel. */
|
||||
val soundModule: MidiSoundModule by lazy {
|
||||
MidiSoundModule(object : MidiSoundModule.NoteSink {
|
||||
override fun busNoteOn(channel: Int, note: Int, velocity: Int) =
|
||||
override fun busNoteOn(channel: Int, note: Int, velocity: Int) {
|
||||
if (MidiTrace.on) MidiTrace.log(
|
||||
"engine ON ch=$channel ${MidiTrace.note(note)} v=$velocity -> " +
|
||||
audioEngine.describeBusTargets(channel, note) +
|
||||
if (audioEngine.transport.value.isPlaying) " [Sizzle transport PLAYING]" else "",
|
||||
)
|
||||
audioEngine.busNoteOn(channel, note, velocity)
|
||||
override fun busNoteOff(channel: Int, note: Int) = audioEngine.busNoteOff(channel, note)
|
||||
}
|
||||
override fun busNoteOff(channel: Int, note: Int) {
|
||||
if (MidiTrace.on) MidiTrace.log("engine OFF ch=$channel ${MidiTrace.note(note)}")
|
||||
audioEngine.busNoteOff(channel, note)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -158,8 +168,14 @@ class SizzleApp : Application() {
|
||||
midiClock.playingProvider = { audioEngine.transport.value.isPlaying }
|
||||
midiClock.bpmProvider = { project.tempoBpm }
|
||||
midiClock.noteSource = { audioEngine.pollMidiOut() } // forward sequenced notes out
|
||||
midiClock.onReceiveStart = { main.post { if (!audioEngine.transport.value.isPlaying) audioEngine.play() } }
|
||||
midiClock.onReceiveStop = { main.post { audioEngine.pause() } }
|
||||
midiClock.onReceiveStart = {
|
||||
if (MidiTrace.on) MidiTrace.log("clock Start -> Sizzle's own sequencer plays the loaded song")
|
||||
main.post { if (!audioEngine.transport.value.isPlaying) audioEngine.play() }
|
||||
}
|
||||
midiClock.onReceiveStop = {
|
||||
if (MidiTrace.on) MidiTrace.log("clock Stop -> Sizzle's own sequencer pauses")
|
||||
main.post { audioEngine.pause() }
|
||||
}
|
||||
midiClock.onReceiveTempo = { bpm -> main.post { project.tempoBpm = bpm.coerceIn(20f, 300f) } }
|
||||
val s = runBlocking { settingsStore.load() }
|
||||
midiClock.sendEnabled = s.sendMidiClock
|
||||
|
||||
@@ -1327,6 +1327,27 @@ class AudioEngine(
|
||||
return pool[base].also { it.kill() }
|
||||
}
|
||||
|
||||
/** For the MIDI trace only: what [busNoteOn] would do with [midi] on
|
||||
* [midiChannel], e.g. "strip2 SAMPLER pad2 | strip4 NES_SYNTH" or "no strip". */
|
||||
fun describeBusTargets(midiChannel: Int, midi: Int): String {
|
||||
val proj = project ?: return "no project"
|
||||
val parts = ArrayList<String>()
|
||||
for (ch in 0 until Pattern.TRACK_COUNT) {
|
||||
if (proj.mixer.channels[ch].midiChannel != midiChannel) continue
|
||||
val slot = proj.toolbox.getOrNull(proj.mixer.channels[ch].instrumentSlot)
|
||||
val type = slot?.type
|
||||
parts += "strip${ch + 1} " + when {
|
||||
type == null -> "no instrument (silent)"
|
||||
type == ToolboxType.SAMPLER -> {
|
||||
val pad = SamplerPads.padFor(midi)
|
||||
if (voiceSample(slot, midi) != null) "SAMPLER pad${pad + 1}" else "SAMPLER pad${pad + 1} empty (silent)"
|
||||
}
|
||||
else -> type.name + if (voiceSample(slot, midi) == null && type == ToolboxType.SOUNDFONT) " no sample" else ""
|
||||
}
|
||||
}
|
||||
return if (parts.isEmpty()) "no strip on channel $midiChannel" else parts.joinToString(" | ")
|
||||
}
|
||||
|
||||
/** Release [midi] on every mixer channel listening on [midiChannel]. */
|
||||
fun busNoteOff(midiChannel: Int, midi: Int) {
|
||||
val proj = project ?: return
|
||||
|
||||
@@ -240,8 +240,10 @@ class MidiInput(
|
||||
onRealtime = module::onRealtime,
|
||||
)
|
||||
port.connect(object : MidiReceiver() {
|
||||
override fun onSend(data: ByteArray, offset: Int, count: Int, timestamp: Long) =
|
||||
override fun onSend(data: ByteArray, offset: Int, count: Int, timestamp: Long) {
|
||||
if (MidiTrace.on) MidiTrace.packet(nameOf(info), data, offset, count, timestamp)
|
||||
parser.feed(data, offset, count)
|
||||
}
|
||||
})
|
||||
open[id] = device to port
|
||||
lastChange[id] = SystemClock.uptimeMillis()
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.input
|
||||
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Optional MIDI trace for checking what reaches Sizzle and what it plays. Off by
|
||||
* default and free when off; switch it on from a computer:
|
||||
*
|
||||
* adb shell setprop log.tag.SizzleMidiTrace VERBOSE # on (takes effect at once)
|
||||
* adb shell setprop log.tag.SizzleMidiTrace INFO # off
|
||||
* adb logcat -s SizzleMidiTrace
|
||||
*
|
||||
* Logs every received packet (source, bytes, how late it arrived against its
|
||||
* timestamp) and every note the sound module hands the engine with the mixer
|
||||
* strips it reaches. Real-time bytes (clock) are left out of packet lines unless
|
||||
* they come with other data, to keep the log readable.
|
||||
*/
|
||||
object MidiTrace {
|
||||
const val TAG = "SizzleMidiTrace"
|
||||
|
||||
private var checkedAt = 0L
|
||||
private var cached = false
|
||||
|
||||
/** Re-reads the property at most every 500 ms (setprop applies without a restart). */
|
||||
val on: Boolean
|
||||
get() {
|
||||
val now = System.nanoTime()
|
||||
if (now - checkedAt > 500_000_000L) {
|
||||
cached = Log.isLoggable(TAG, Log.VERBOSE)
|
||||
checkedAt = now
|
||||
}
|
||||
return cached
|
||||
}
|
||||
|
||||
fun log(msg: String) { Log.v(TAG, msg) }
|
||||
|
||||
fun packet(source: String, data: ByteArray, offset: Int, count: Int, timestamp: Long) {
|
||||
if (count == 1 && (data[offset].toInt() and 0xFF) == 0xF8) return // clock tick
|
||||
val hex = (offset until offset + count).joinToString(" ") { "%02X".format(data[it]) }
|
||||
val lateMs = if (timestamp > 0) (System.nanoTime() - timestamp) / 1_000_000.0 else Double.NaN
|
||||
log("recv $source [$hex] ${describe(data, offset, count)} late=${"%.1f".format(lateMs)}ms")
|
||||
}
|
||||
|
||||
private fun describe(data: ByteArray, offset: Int, count: Int): String {
|
||||
val s = data[offset].toInt() and 0xFF
|
||||
val d1 = if (count > 1) data[offset + 1].toInt() and 0x7F else 0
|
||||
val d2 = if (count > 2) data[offset + 2].toInt() and 0x7F else 0
|
||||
val ch = (s and 0x0F) + 1
|
||||
return when (s and 0xF0) {
|
||||
0x90 -> if (d2 > 0) "NoteOn ch=$ch ${note(d1)} v=$d2" else "NoteOn(v0=Off) ch=$ch ${note(d1)}"
|
||||
0x80 -> "NoteOff ch=$ch ${note(d1)}"
|
||||
0xB0 -> "CC ch=$ch #$d1=$d2"
|
||||
0xC0 -> "Program ch=$ch $d1"
|
||||
0xF0 -> when (s) { 0xFA -> "Start"; 0xFB -> "Continue"; 0xFC -> "Stop"; else -> "System %02X".format(s) }
|
||||
else -> "status %02X".format(s)
|
||||
}
|
||||
}
|
||||
|
||||
private val NAMES = arrayOf("C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-")
|
||||
|
||||
/** "36(C-2)", in this app's naming (60 = C-4). */
|
||||
fun note(n: Int): String = "$n(${NAMES[n % 12]}${n / 12 - 1})"
|
||||
}
|
||||
@@ -38,7 +38,7 @@ object ProjectIo {
|
||||
appendLine("ACTIVE=${project.activePatternId}")
|
||||
|
||||
project.patterns.forEach { p ->
|
||||
appendLine("[PATTERN id=${p.id} name=${p.name} len=${p.length}]")
|
||||
appendLine("[PATTERN id=${p.id} name=${encodeName(p.name)} len=${p.length}]")
|
||||
for (t in 0 until Pattern.TRACK_COUNT) {
|
||||
for (line in 0 until p.length) {
|
||||
val cell = p.cell(t, line)
|
||||
@@ -109,7 +109,7 @@ object ProjectIo {
|
||||
val attrs = parseAttrs(line)
|
||||
current = Pattern(
|
||||
id = attrs["id"]?.toIntOrNull() ?: project.patterns.size,
|
||||
name = attrs["name"] ?: "PTN",
|
||||
name = attrs["name"]?.let(::decodeName) ?: "PTN",
|
||||
length = attrs["len"]?.toIntOrNull() ?: 16,
|
||||
).also { project.patterns.add(it) }
|
||||
section = "PATTERN"
|
||||
@@ -232,4 +232,19 @@ object ProjectIo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Block names may contain spaces ("C1 muted" from a Mikro Seq import), but the
|
||||
* `[PATTERN …]` header is split on whitespace: escape space, `%` and `]`. */
|
||||
internal fun encodeName(name: String): String = buildString {
|
||||
for (c in name) when (c) {
|
||||
'%' -> append("%25"); ' ' -> append("%20"); ']' -> append("%5D")
|
||||
'\t', '\n', '\r' -> append("%20")
|
||||
else -> append(c)
|
||||
}
|
||||
}
|
||||
|
||||
/** Inverse of [encodeName]; anything that isn't one of its escapes (older files,
|
||||
* a literal "50%") is kept as written. */
|
||||
internal fun decodeName(text: String): String = text
|
||||
.replace("%20", " ").replace("%5D", "]").replace("%25", "%")
|
||||
}
|
||||
|
||||
@@ -518,7 +518,8 @@ private fun MidiSyncCard(vm: AppViewModel) {
|
||||
HorizontalDivider()
|
||||
ToggleRow(
|
||||
"Receive MIDI clock",
|
||||
"Follow an external clock's tempo, and start/stop with it.",
|
||||
"Follow an external clock's tempo, and start/stop Sizzle's own song with it. " +
|
||||
"Its song then plays along with any notes the other app sends.",
|
||||
vm.receiveMidiClock,
|
||||
) { vm.updateReceiveMidiClock(it) }
|
||||
HorizontalDivider()
|
||||
|
||||
Reference in New Issue
Block a user