Background MIDI sound module and opening .sng from other apps (v0.28.0)

- "Keep running as MIDI sound module" setting: a mediaPlayback foreground
  service (SoundModuleService) keeps Sizzle from being frozen in the
  background, so another app (Mikro Seq) can play its mixer over MIDI with
  the transport stopped. Notification shows open sources and has Stop.
- MIDI notes are routed by MidiSoundModule, owned by SizzleApp next to the
  engine, not by the ViewModel; the UI only mirrors/punches them in. Held
  notes are released per source on disconnect and on CC 120/123. MIDI clock
  follow is wired in SizzleApp. Engine and MIDI ports are ref-counted
  between the Activity and both services.
- MidiByteParser: running status, split packets, realtime, SysEx.
- VIEW/SEND intent filters for .sng (application/x-sizzle-song,
  application/octet-stream, text/plain, .sng path); confirm before replacing.
- SngFormat reader follows the desktop reader: spaced block names, comments,
  loop/group/scale/root, track slot fallback, on-roll blocks first, length
  cap. Mikro Seq 4/4 and 3/4 exports as test fixtures.
- CHANGELOG.md started; handover §5.5/§5.6 and user guide updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-12 22:11:18 +01:00
co-authored by Claude Opus 5.5
parent bcec71597d
commit fb9a219e27
26 changed files with 1590 additions and 141 deletions
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.input
import org.junit.Assert.assertEquals
import org.junit.Test
class MidiByteParserTest {
private val channel = mutableListOf<Triple<Int, Int, Int>>()
private val realtime = mutableListOf<Int>()
private val parser = MidiByteParser({ s, a, b -> channel += Triple(s, a, b) }, { realtime += it })
private fun feed(vararg b: Int) {
val bytes = ByteArray(b.size) { b[it].toByte() }
parser.feed(bytes, 0, bytes.size)
}
@Test fun oneMessagePerPacket() {
feed(0x99, 36, 100)
feed(0x89, 36, 0)
assertEquals(listOf(Triple(0x99, 36, 100), Triple(0x89, 36, 0)), channel)
}
@Test fun runningStatus() {
feed(0x90, 60, 100, 64, 90, 60, 0)
assertEquals(listOf(Triple(0x90, 60, 100), Triple(0x90, 64, 90), Triple(0x90, 60, 0)), channel)
}
@Test fun messageSplitAcrossPackets() {
feed(0x90, 60)
feed(100)
assertEquals(listOf(Triple(0x90, 60, 100)), channel)
}
@Test fun realtimeInsideAMessageDoesNotBreakIt() {
feed(0x90, 0xF8, 60, 0xFA, 100, 0xFC)
assertEquals(listOf(Triple(0x90, 60, 100)), channel)
assertEquals(listOf(0xF8, 0xFA, 0xFC), realtime)
}
@Test fun oneDataByteMessagesAndSysexSkipped() {
feed(0xC2, 5, 0xF0, 0x7E, 0x01, 0x02, 0xF7, 0xB0, 123, 0)
assertEquals(listOf(Triple(0xC2, 5, 0), Triple(0xB0, 123, 0)), channel)
}
@Test fun strayDataBytesWithoutStatusAreIgnored() {
feed(60, 100, 0x80, 60, 0)
assertEquals(listOf(Triple(0x80, 60, 0)), channel)
}
@Test fun offsetAndCountAreHonoured() {
val bytes = byteArrayOf(0x00, 0x90.toByte(), 60, 100, 0x7F)
parser.feed(bytes, 1, 3)
assertEquals(listOf(Triple(0x90, 60, 100)), channel)
}
}
@@ -0,0 +1,140 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.io
import cc.tiredsysadmin.android.sizzle.model.Arrangement
import cc.tiredsysadmin.android.sizzle.model.Cell
import cc.tiredsysadmin.android.sizzle.model.Pitch
import cc.tiredsysadmin.android.sizzle.model.Project
import cc.tiredsysadmin.android.sizzle.model.TimeSignature
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* Files exported by Mikro Seq (internal/seq/sizzle.go, its sample pattern) in 4/4
* and 3/4: `#` comments, `loop`, `group`, `scale`/`root`, block names with spaces
* ("C1 muted"), blocks off the roll, `OFF` notes and channel-10 drums.
*/
class SngMikroSeqImportTest {
private fun load(name: String): String =
javaClass.getResource("/mikroseq/$name")!!.readText()
@Test
fun fourFour() {
val text = load("sig44.sng")
assertTrue(SngFormat.looksLikeSng(text))
val p = SngFormat.importInto(Project(), text)
assertEquals(97f, p.tempoBpm, 0.001f)
assertEquals(TimeSignature.FOUR_FOUR, p.timeSignature)
assertTrue(p.arrangement.loopEnabled)
assertEquals(listOf("C1", "C1 muted", "C2", "C6"), p.patterns.take(4).map { it.name })
assertEquals(listOf(16, 16, 48, 64), p.patterns.take(4).map { it.length })
// C1: drums on channel 10 (file channel 9), T1 and T3 keep their slots.
val c1 = p.patterns[0]
assertEquals(Pitch.parse("C-2"), c1.cell(0, 0).note)
assertEquals(10, c1.cell(0, 0).channel)
assertEquals(0x6E, c1.cell(0, 0).velocity)
assertEquals(Cell.OFF, c1.cell(0, 1).note)
assertEquals(Pitch.parse("F#2"), c1.cell(2, 2).note)
// C2: the tied bass on T4, channel 1, with OFF tokens.
val c2 = p.patterns[2]
assertEquals(Pitch.parse("B-1"), c2.cell(3, 0).note)
assertEquals(1, c2.cell(3, 0).channel)
assertEquals(Pitch.parse("C#2"), c2.cell(3, 6).note)
assertEquals(Cell.OFF, c2.cell(3, 9).note)
// C6: lead on channel 3.
assertEquals(3, p.patterns[3].cell(0, 0).channel)
// The roll: 96 beats for C1, C2, C6; the muted block is not on it.
for (lane in listOf(0, 2, 3)) {
assertEquals(lane, p.arrangement.patternAt(lane, 0))
assertEquals(lane, p.arrangement.patternAt(lane, 95))
assertEquals(Arrangement.EMPTY, p.arrangement.patternAt(lane, 96))
}
assertEquals(Arrangement.EMPTY, p.arrangement.patternAt(1, 0))
assertTrue(p.arrangement.lengthBars >= 24)
}
@Test
fun threeFour() {
val p = SngFormat.importInto(Project(), load("sig34.sng"))
assertEquals(TimeSignature.THREE_FOUR, p.timeSignature)
assertEquals(listOf("C1", "C1 muted", "C2", "C6"), p.patterns.take(4).map { it.name })
assertEquals(listOf(12, 12, 36, 48), p.patterns.take(4).map { it.length })
val c1 = p.patterns[0]
for (step in listOf(0, 3, 6, 9)) assertEquals(Pitch.parse("C-2"), c1.cell(0, step).note)
for (step in listOf(1, 4, 7, 10)) assertEquals(Cell.OFF, c1.cell(0, step).note)
assertEquals(Pitch.parse("D-2"), p.patterns[1].cell(1, 3).note) // muted T2
val c2 = p.patterns[2]
assertEquals(Pitch.parse("C#2"), c2.cell(3, 5).note)
assertEquals(Cell.OFF, c2.cell(3, 7).note)
assertEquals(Cell.OFF, c2.cell(3, 16).note)
assertEquals(2, p.arrangement.patternAt(2, 95))
assertEquals(Arrangement.EMPTY, p.arrangement.patternAt(2, 96))
assertTrue(p.arrangement.lengthBars >= 96 / 3)
}
@Test
fun moreBlocksThanLanesKeepsTheOnesOnTheRoll() {
// 8 canvases, each with a live block on the roll and a muted one off it:
// 16 blocks for 8 lanes.
val text = buildString {
appendLine("version 1\nbpm 120\nsig 4 4")
for (c in 1..8) {
appendLine("block C$c 16 1\nroll ####\ntrack T1 0\n0 C-4 64 ..\nendblock")
appendLine("block C$c muted 16 1\ntrack T2 0\n0 D-4 64 ..\nendblock")
}
}
val p = SngFormat.importInto(Project(), text)
assertEquals((1..8).map { "C$it" }, p.patterns.map { it.name })
for (lane in 0 until 8) assertEquals(lane, p.arrangement.patternAt(lane, 0))
}
@Test
fun trackNumbersBeyondFourTakeFreeSlotsAndExtraTracksAreDropped() {
val text = """
version 1
block A 16 6
track T1 0
0 C-4 .. ..
track T7 1
0 D-4 .. ..
track T2 2
0 E-4 .. ..
track T9 3
0 F-4 .. ..
track T12 4
0 G-4 .. ..
endblock
""".trimIndent()
val b = SngFormat.importInto(Project(), text).patterns[0]
assertEquals(Pitch.parse("C-4"), b.cell(0, 0).note)
assertEquals(Pitch.parse("D-4"), b.cell(1, 0).note) // T7 -> first free (2nd) slot
assertEquals(Pitch.parse("E-4"), b.cell(2, 0).note) // T2 taken -> next free
assertEquals(Pitch.parse("F-4"), b.cell(3, 0).note)
// T12 has no slot left: its G-4 is dropped rather than overwriting a track.
assertFalse((0 until 4).any { b.cell(it, 0).note == Pitch.parse("G-4") })
}
@Test
fun absurdBlockLengthIsBounded() {
val p = SngFormat.importInto(Project(), "version 1\nblock A 999999999 1\nendblock\n")
assertEquals(SngFormat.MAX_BLOCK_LENGTH, p.patterns[0].length)
}
@Test
fun plainTextIsNotASong() {
assertFalse(SngFormat.looksLikeSng("shopping list\nmilk\neggs"))
}
}
@@ -0,0 +1,93 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.playback
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class MidiSoundModuleTest {
/** Records engine calls as "on ch note vel" / "off ch note". */
private val engine = mutableListOf<String>()
private val module = MidiSoundModule(object : MidiSoundModule.NoteSink {
override fun busNoteOn(channel: Int, note: Int, velocity: Int) { engine += "on $channel $note $velocity" }
override fun busNoteOff(channel: Int, note: Int) { engine += "off $channel $note" }
})
@Test fun noteOnOffRouteToTheMixerChannel() {
assertTrue(module.onChannelMessage(1, 0x99, 36, 110)) // channel 10: drums
assertTrue(module.onChannelMessage(1, 0x89, 36, 0))
assertTrue(module.onChannelMessage(1, 0x92, 60, 90)) // channel 3
assertTrue(module.onChannelMessage(1, 0x92, 60, 0)) // velocity 0 == note off
assertEquals(listOf("on 10 36 110", "off 10 36", "on 3 60 90", "off 3 60"), engine)
assertTrue(module.heldNotes(1).isEmpty())
}
@Test fun otherMessagesAreLeftToTheCaller() {
assertFalse(module.onChannelMessage(1, 0xB0, 7, 100)) // CC 7: bindings / learn
assertFalse(module.onChannelMessage(1, 0xC0, 3, 0)) // program change
assertTrue(engine.isEmpty())
}
@Test fun disconnectReleasesOnlyThatSourcesHeldNotes() {
module.sourceConnected(1); module.sourceConnected(2)
assertEquals(2, module.sources.value)
module.onChannelMessage(1, 0x90, 60, 100)
module.onChannelMessage(1, 0x99, 38, 100)
module.onChannelMessage(2, 0x91, 64, 100)
engine.clear()
module.sourceDisconnected(1)
assertEquals(setOf("off 1 60", "off 10 38"), engine.toSet())
assertEquals(1, module.sources.value)
assertEquals(setOf(2 to 64), module.heldNotes(2))
}
@Test fun allNotesOffReleasesTheChannelAcrossSources() {
module.onChannelMessage(1, 0x90, 60, 100)
module.onChannelMessage(2, 0x90, 62, 100)
module.onChannelMessage(1, 0x93, 70, 100)
engine.clear()
assertTrue(module.onChannelMessage(1, 0xB0, 123, 0))
assertEquals(setOf("off 1 60", "off 1 62"), engine.toSet())
assertEquals(setOf(4 to 70), module.heldNotes(1))
assertTrue(module.heldNotes(2).isEmpty())
}
@Test fun allSoundOffAlsoReleases() {
module.onChannelMessage(1, 0x90, 60, 100)
engine.clear()
assertTrue(module.onChannelMessage(1, 0xB0, 120, 0))
assertEquals(listOf("off 1 60"), engine)
}
@Test fun listenerMirrorsNotesSoTheUiNeedNotSoundThem() {
val seen = mutableListOf<String>()
module.noteListener = { on, ch, note, vel -> seen += "${if (on) "on" else "off"} $ch $note $vel" }
module.onChannelMessage(1, 0x90, 60, 100)
module.sourceDisconnected(1)
assertEquals(listOf("on 1 60 100", "off 1 60 0"), seen)
}
@Test fun realtimeIsForwarded() {
val rt = mutableListOf<Int>()
module.realtime = { rt += it }
module.onRealtime(0xFA); module.onRealtime(0xF8); module.onRealtime(0xFC)
assertEquals(listOf(0xFA, 0xF8, 0xFC), rt)
}
@Test fun releaseAllEmptiesEverySource() {
module.onChannelMessage(1, 0x90, 60, 100)
module.onChannelMessage(2, 0x95, 61, 100)
engine.clear()
module.releaseAll()
assertEquals(setOf("off 1 60", "off 6 61"), engine.toSet())
assertTrue(module.heldNotes(1).isEmpty() && module.heldNotes(2).isEmpty())
}
}
+108
View File
@@ -0,0 +1,108 @@
# sizzletracker song
# exported from Mikro Seq: one block per canvas, all looping in parallel
version 1
bpm 97
sig 3 4
loop 0 23
group 0 Mikro Seq
block C1 12 2
scale Mel Minor
root D
roll ################################################################################################
track T1 9
0 C-2 6E ..
1 OFF .. ..
3 C-2 6E ..
4 OFF .. ..
6 C-2 6E ..
7 OFF .. ..
9 C-2 6E ..
10 OFF .. ..
track T3 9
2 F#2 46 ..
3 OFF .. ..
endblock
block C1 muted 12 1
scale Mel Minor
root D
track T2 9
3 D-2 64 ..
4 OFF .. ..
endblock
block C2 36 1
scale Mel Minor
root D
roll ################################################################################################
track T4 0
0 B-1 5A ..
5 C#2 5A ..
7 OFF .. ..
8 C#2 5A ..
9 D-2 5A ..
14 B-1 5A ..
16 OFF .. ..
17 B-1 5A ..
18 C#2 5A ..
23 D-2 5A ..
25 OFF .. ..
26 D-2 5A ..
27 B-1 5A ..
32 C#2 5A ..
34 OFF .. ..
35 C#2 5A ..
endblock
block C6 48 1
scale Mel Minor
root D
roll ################################################################################################
track T1 2
0 B-3 50 ..
1 D-4 40 ..
2 OFF .. ..
3 D-4 40 ..
4 OFF .. ..
5 F-4 50 ..
6 OFF .. ..
7 D-4 50 ..
8 D-4 40 ..
9 F-4 50 ..
10 D-4 40 ..
11 OFF .. ..
12 D-4 40 ..
13 OFF .. ..
14 E-4 50 ..
15 OFF .. ..
16 B-3 50 ..
17 D-4 40 ..
18 D-4 50 ..
19 D-4 40 ..
20 OFF .. ..
21 D-4 40 ..
22 OFF .. ..
23 D-4 50 ..
24 OFF .. ..
25 F-4 50 ..
26 D-4 40 ..
27 C#4 50 ..
28 D-4 40 ..
29 OFF .. ..
30 D-4 40 ..
31 OFF .. ..
32 B-3 50 ..
33 OFF .. ..
34 D-4 50 ..
35 D-4 40 ..
36 F-4 50 ..
37 D-4 40 ..
38 OFF .. ..
39 D-4 40 ..
40 OFF .. ..
41 F-4 50 ..
42 OFF .. ..
43 C#4 50 ..
44 D-4 40 ..
45 E-4 50 ..
46 D-4 40 ..
47 OFF .. ..
endblock
+123
View File
@@ -0,0 +1,123 @@
# sizzletracker song
# exported from Mikro Seq: one block per canvas, all looping in parallel
version 1
bpm 97
sig 4 4
loop 0 23
group 0 Mikro Seq
block C1 16 2
scale Mel Minor
root D
roll ################################################################################################
track T1 9
0 C-2 6E ..
1 OFF .. ..
4 C-2 6E ..
5 OFF .. ..
8 C-2 6E ..
9 OFF .. ..
12 C-2 6E ..
13 OFF .. ..
track T3 9
2 F#2 46 ..
3 OFF .. ..
endblock
block C1 muted 16 1
scale Mel Minor
root D
track T2 9
4 D-2 64 ..
5 OFF .. ..
endblock
block C2 48 1
scale Mel Minor
root D
roll ################################################################################################
track T4 0
0 B-1 5A ..
6 C#2 5A ..
9 OFF .. ..
10 C#2 5A ..
12 D-2 5A ..
18 B-1 5A ..
21 OFF .. ..
22 B-1 5A ..
24 C#2 5A ..
30 D-2 5A ..
33 OFF .. ..
34 D-2 5A ..
36 B-1 5A ..
42 C#2 5A ..
45 OFF .. ..
46 C#2 5A ..
endblock
block C6 64 1
scale Mel Minor
root D
roll ################################################################################################
track T1 2
0 B-3 50 ..
1 D-4 40 ..
2 OFF .. ..
3 D-4 50 ..
4 D-4 40 ..
5 OFF .. ..
6 F-4 50 ..
7 D-4 40 ..
8 OFF .. ..
9 D-4 50 ..
10 D-4 40 ..
11 OFF .. ..
12 F-4 50 ..
13 D-4 40 ..
14 OFF .. ..
15 C#4 50 ..
16 D-4 40 ..
17 OFF .. ..
18 E-4 50 ..
19 D-4 40 ..
20 OFF .. ..
21 B-3 50 ..
22 D-4 40 ..
23 OFF .. ..
24 D-4 50 ..
25 D-4 40 ..
26 OFF .. ..
27 F-4 50 ..
28 D-4 40 ..
29 OFF .. ..
30 D-4 50 ..
31 D-4 40 ..
32 OFF .. ..
33 F-4 50 ..
34 D-4 40 ..
35 OFF .. ..
36 C#4 50 ..
37 D-4 40 ..
38 OFF .. ..
39 E-4 50 ..
40 D-4 40 ..
41 OFF .. ..
42 B-3 50 ..
43 D-4 40 ..
44 OFF .. ..
45 D-4 50 ..
46 D-4 40 ..
47 OFF .. ..
48 F-4 50 ..
49 D-4 40 ..
50 OFF .. ..
51 D-4 50 ..
52 D-4 40 ..
53 OFF .. ..
54 F-4 50 ..
55 D-4 40 ..
56 OFF .. ..
57 C#4 50 ..
58 D-4 40 ..
59 OFF .. ..
60 E-4 50 ..
61 D-4 40 ..
62 OFF .. ..
endblock