Add write/read indices and commands handling to MidiMessage

Introduced `writeIndex` and `readIndex` for managing MIDI commands. Added `addMidiCommand`, `readMessage`, and `resetReadIndex` methods to enhance MIDI message manipulation. Ensured validation to limit commands to a maximum of 16.
This commit is contained in:
2025-05-15 16:15:31 +02:00
parent 09d8f46a46
commit f5986499d6

View File

@@ -50,6 +50,8 @@ class TimedMidiMessage() : MidiMessage(
) {
var timeToPlay by double("timeToPlay")
var midi by blob("midi")
var writeIndex: Int = 0
var readIndex: Int = 0
init {
this.type = MidiMessageTypes.MIDI_DATA.typeId
@@ -70,6 +72,31 @@ class TimedMidiMessage() : MidiMessage(
this.timeToPlay = timeToPlay
val ba: ByteArray = data
this.midi = SlicedByteArray(ba)
writeIndex = ba.size / 3
}
fun addMidiCommand(vararg data: Byte) {
check(writeIndex <= 45) {
"Can't add more than 16 commands to a midi message"
}
for (index in data.indices) {
midi[writeIndex++] = data[index]
}
}
fun readMessage(): ByteArray? {
check(writeIndex <= 45) {
"Can't read more than 16 commands from a midi message"
}
val result = ByteArray(3)
for (index in 0 ..< 3) {
result[index] = midi[readIndex++]
}
return result
}
fun resetReadIndex() {
readIndex = 0
}
}