diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt deleted file mode 100644 index f38d731..0000000 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt +++ /dev/null @@ -1,62 +0,0 @@ -package mg.dot.feufaro.midi - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import mg.dot.feufaro.FileRepository - -actual class MidiWriterKotlin actual constructor(private val fileRepository: FileRepository) { - private val sequence = MidiSequence(60) - private val track = sequence.createTrack() - private var tick: Long = 0 - private var nextTick: MutableList = mutableListOf() - private val lastPitch : MutableList = mutableListOf() - private val useChord : Boolean = true - actual fun addNote( voiceNumber: Int, note: Int, velocity: Int, tick: Long) { - val channel = (voiceNumber -1).coerceIn(0, 3) - var finalNote = note - - if (voiceNumber == 3 || voiceNumber == 4) { - finalNote -= 12 - } - if (lastPitch.size > voiceNumber && lastPitch[voiceNumber] > 0) { - sequence.addSequence(channel, lastPitch[voiceNumber], tick) - } - var finalVelocity = velocity - var midiNote = finalNote - if (finalNote <= 0) { - midiNote = 40 - finalVelocity = 0 - } - sequence.addSequence(channel, midiNote, tick, 90, finalVelocity) - - while(lastPitch.size <= voiceNumber) { - lastPitch.add(0) - } - lastPitch[voiceNumber] = midiNote - } - actual fun save(filePath: String) { - val parseScope = CoroutineScope(Dispatchers.Default) - parseScope.launch { - sequence.write() - fileRepository.saveFile(filePath, sequence.out.toByteArray()) - //val fout = fileRepository.getOutputStream(filePath) - } - } - actual fun addMetaMessage(type: Int, tick: Int, nbData: Int, metaByteString: String) { - sequence.addMeta( type, tick, nbData, metaByteString) - } - actual fun process(pitches: List) { - val lastTick = 0 - nextTick.clear() - // addMetaMessage(0x59, 4, 2, 2,0) - tick = 0 - pitches.forEach { - if (it.metaType > 0) { - addMetaMessage(it.metaType, it.tick, it.metaByteSize, it.metaBytes) - } else if (it.pitch != "") { - addNote(it.voiceNumber, it.pitch.toInt(), 100, it.tick.toLong()) - } - } - } -} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt index cc32aad..e62d02b 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt @@ -1,10 +1,62 @@ package mg.dot.feufaro.midi +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import mg.dot.feufaro.FileRepository -expect class MidiWriterKotlin(fileRepository: FileRepository) { - fun addNote( voiceNumber: Int, note: Int, velocity: Int, tick: Long) - fun save(filePath: String) - fun addMetaMessage(type: Int, tick: Int, nbData: Int, metaByteString: String) - fun process(pitches: List) +class MidiWriterKotlin constructor(private val fileRepository: FileRepository) { + private val sequence = MidiSequence(60) + private val track = sequence.createTrack() + private var tick: Long = 0 + private var nextTick: MutableList = mutableListOf() + private val lastPitch : MutableList = mutableListOf() + private val useChord : Boolean = true + fun addNote( voiceNumber: Int, note: Int, velocity: Int, tick: Long) { + val channel = (voiceNumber -1).coerceIn(0, 3) + var finalNote = note + + if (voiceNumber == 3 || voiceNumber == 4) { + finalNote -= 12 + } + if (lastPitch.size > voiceNumber && lastPitch[voiceNumber] > 0) { + sequence.addSequence(channel, lastPitch[voiceNumber], tick) + } + var finalVelocity = velocity + var midiNote = finalNote + if (finalNote <= 0) { + midiNote = 40 + finalVelocity = 0 + } + sequence.addSequence(channel, midiNote, tick, 90, finalVelocity) + + while(lastPitch.size <= voiceNumber) { + lastPitch.add(0) + } + lastPitch[voiceNumber] = midiNote + } + fun save(filePath: String) { + val parseScope = CoroutineScope(Dispatchers.Default) + parseScope.launch { + sequence.write() + fileRepository.saveFile(filePath, sequence.out.toByteArray()) + //val fout = fileRepository.getOutputStream(filePath) + } + } + fun addMetaMessage(type: Int, tick: Int, nbData: Int, metaByteString: String) { + sequence.addMeta( type, tick, nbData, metaByteString) + } + fun process(pitches: List) { + val lastTick = 0 + nextTick.clear() + // addMetaMessage(0x59, 4, 2, 2,0) + tick = 0 + pitches.forEach { + if (it.metaType > 0) { + addMetaMessage(it.metaType, it.tick, it.metaByteSize, it.metaBytes) + } else if (it.pitch != "") { + addNote(it.voiceNumber, it.pitch.toInt(), 100, it.tick.toLong()) + } + } + } } \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt b/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt deleted file mode 100644 index 21e39a7..0000000 --- a/composeApp/src/desktopMain/kotlin/mg/dot/feufaro/midi/MidiWriterKotlin.kt +++ /dev/null @@ -1,73 +0,0 @@ -package mg.dot.feufaro.midi - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import mg.dot.feufaro.FileRepository -import javax.sound.midi.* - -actual class MidiWriterKotlin actual constructor(private val fileRepository: FileRepository) { - private val sequence = Sequence(Sequence.PPQ, 60) - private val track = sequence.createTrack() - private var tick: Long = 0 - private var nextTick: MutableList = mutableListOf() - private val noteOn = ShortMessage() - private val noteOff = ShortMessage() - private val lastPitch : MutableList = mutableListOf() - private val useChord : Boolean = true - actual fun addNote( voiceNumber: Int, note: Int, velocity: Int, tick: Long) { - val channel = (voiceNumber -1).coerceIn(0, 3) - var finalNote = note - - if (voiceNumber == 3 || voiceNumber == 4) { - finalNote -= 12 - } - if (lastPitch.size > voiceNumber && lastPitch[voiceNumber] > 0) { - val offMsg = ShortMessage() - offMsg.setMessage(ShortMessage.NOTE_OFF, channel, lastPitch[voiceNumber], 0) - track.add(MidiEvent(offMsg, tick)) - } - var finalVelocity = velocity - var midiNote = finalNote - if (finalNote <= 0) { - midiNote = 40 - finalVelocity = 0 - } - val onMsg = ShortMessage() - onMsg.setMessage(ShortMessage.NOTE_ON, channel, midiNote, finalVelocity) - track.add(MidiEvent(onMsg, tick)) - - while(lastPitch.size <= voiceNumber) { - lastPitch.add(0) - } - lastPitch[voiceNumber] = midiNote - } - actual fun save(filePath: String) { - val parseScope = CoroutineScope(Dispatchers.Default) - parseScope.launch { - val midiFileName = fileRepository.getFileName(filePath) - val out = fileRepository.getOutputStream(midiFileName) - println("write to : $midiFileName ") - MidiSystem.write(sequence, 1, out) - out.close() - } - } - 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())) - } - actual fun process(pitches: List) { - val lastTick = 0 - nextTick.clear() - // addMetaMessage(0x59, 4, 2, 2,0) - tick = 0 - pitches.forEach { - if (it.metaType > 0) { - addMetaMessage(it.metaType, it.tick, it.metaByteSize, it.metaBytes) - } else if (it.pitch != "") { - addNote(it.voiceNumber, it.pitch.toInt(), 100, it.tick.toLong()) - } - } - } -} \ No newline at end of file