diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt index cbdabe7..f38d731 100644 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt +++ b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt @@ -20,7 +20,7 @@ actual class MidiWriterKotlin actual constructor(private val fileRepository: Fil finalNote -= 12 } if (lastPitch.size > voiceNumber && lastPitch[voiceNumber] > 0) { - sequence.addNote(channel, lastPitch[voiceNumber], tick) + sequence.addSequence(channel, lastPitch[voiceNumber], tick) } var finalVelocity = velocity var midiNote = finalNote @@ -28,7 +28,7 @@ actual class MidiWriterKotlin actual constructor(private val fileRepository: Fil midiNote = 40 finalVelocity = 0 } - sequence.addNote(channel, midiNote, tick, 90, finalVelocity) + sequence.addSequence(channel, midiNote, tick, 90, finalVelocity) while(lastPitch.size <= voiceNumber) { lastPitch.add(0) @@ -44,9 +44,7 @@ actual class MidiWriterKotlin actual constructor(private val fileRepository: Fil } } actual fun addMetaMessage(type: Int, tick: Int, nbData: Int, metaByteString: String) { - /*val byteArray = metaByteString.toByteArray() - val metaMessage = MetaMessage(type, byteArray, nbData) - track.add(MidiEvent(metaMessage, tick.toLong()))*/ + sequence.addMeta( type, tick, nbData, metaByteString) } actual fun process(pitches: List) { val lastTick = 0 diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiSequence.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiSequence.kt index 8555fbc..fe8e8d7 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiSequence.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiSequence.kt @@ -5,16 +5,11 @@ class MidiSequence(val resolution: Int = 60) { // Une liste de pistes, où chaque piste est une liste d'octets (ByteArray) val tracks = mutableListOf>() val out: MutableList = mutableListOf() - private var lastTick = mutableListOf() + private var lastTick = 0L fun getDeltaAndSync(channel: Int, currentTick: Long): Long { - // Si le canal demandé dépasse la taille actuelle, on remplit de 0L - while (lastTick.size <= channel) { - lastTick.add(0L) - } - - val delta = currentTick - lastTick[channel] - lastTick[channel] = currentTick + val delta = currentTick - lastTick + lastTick = currentTick return delta } fun createTrack(): MutableList { @@ -44,15 +39,11 @@ class MidiSequence(val resolution: Int = 60) { } } - fun addNote(channel: Int, pitch: Int, currentTick: Long, type: Int = 80, finalVelocity: Int = 80) { - while (tracks.size <= channel) { - tracks.add(mutableListOf(0)) - } - - val myTrack = tracks[channel] + fun addSequence(channel: Int, pitch: Int, currentTick: Long, type: Int = 80, finalVelocity: Int = 100) { + val myTrack = tracks[0] myTrack.addNote(channel, pitch, currentTick, type, finalVelocity) } - fun MutableList.addNote(channel: Int, pitch: Int, currentTick: Long, type: Int = 80, finalVelocity: Int = 80) { + fun MutableList.addNote(channel: Int, pitch: Int, currentTick: Long, type: Int = 80, finalVelocity: Int = 100) { // Calcul du Delta-Time val delta = getDeltaAndSync(channel, currentTick) @@ -92,7 +83,7 @@ class MidiSequence(val resolution: Int = 60) { out.write16Bit(1) // 4. Nombre de pistes (16 bits) - out.write16Bit(trackCount) // trackCount = 4 is wrong, why? + out.write16Bit(trackCount) // 5. Division / Résolution (16 bits) // Ticks par noire (votre variable 'resolution') @@ -125,4 +116,19 @@ class MidiSequence(val resolution: Int = 60) { writeTrackChunk( track) } } + fun addMeta(type: Int, tick: Int, nbData: Int, metaByteString: String) { + val data = metaByteString.encodeToByteArray() + if (tracks.isEmpty()) { + tracks.add(mutableListOf(0)) + } + val outTrack = tracks[0] + // Un Meta Message commence toujours par FF, puis le type, puis la longueur + outTrack.add(0x00.toByte()) // Status byte + outTrack.add(0xFF.toByte()) // Status byte + outTrack.add(type.toByte()) // Meta-event type + outTrack.addAll(nbData.toLong().toVLQList()) + // Ajout des données + outTrack.addAll(data.toList().take(nbData)) + + } } diff --git a/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt b/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt index 74c7302..21e39a7 100644 --- a/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt +++ b/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt @@ -10,7 +10,6 @@ actual class MidiWriterKotlin actual constructor(private val fileRepository: Fil private val sequence = Sequence(Sequence.PPQ, 60) private val track = sequence.createTrack() private var tick: Long = 0 - //private var nextTick: MidiPitch = mutableListOf() private var nextTick: MutableList = mutableListOf() private val noteOn = ShortMessage() private val noteOff = ShortMessage()