Skip to content

Commit

Permalink
Fix CoreMIDI output sender (partly fix #81). The use of pointer was w…
Browse files Browse the repository at this point in the history
…eird.
  • Loading branch information
atsushieno committed Jul 4, 2024
1 parent bdeadd5 commit 48d4103
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal interface ListenerHolder {
}

// MIDIClientRef setup
@OptIn(ExperimentalStdlibApi::class, ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class)
internal class ClientHolder(access: CoreMidiAccess) : AutoCloseable {
val clientRef: MIDIClientRef = memScoped {
val clientName = "KTMidiClient"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.atsushieno.ktmidi

import kotlinx.cinterop.*
import platform.CoreFoundation.CFStringRefVar
import platform.CoreMIDI.*
import platform.posix.alloca

Expand Down Expand Up @@ -110,22 +111,21 @@ private open class TraditionalCoreMidiInput(holder: ClientHolder, customReadProc
private open class TraditionalCoreMidiOutput(holder: ClientHolder, private val coreMidiPortDetails: CoreMidiPortDetails)
: CoreMidiPort(holder, coreMidiPortDetails), MidiOutput
{
private val portRef by lazy {
memScoped {
val portName = "KTMidiOutputPort"
val port = alloc<MIDIPortRefVar>()
checkStatus { MIDIOutputPortCreate(clientRef, portName.toCFStringRef(), port.ptr) }
port.value
}
private val portRef = memScoped {
val portName = "KTMidiOutputPort"
val port = alloc<MIDIPortRefVar>()
checkStatus { MIDIOutputPortCreate(clientRef, portName.toCFStringRef(), port.ptr) }
port.value
}

override fun send(mevent: ByteArray, offset: Int, length: Int, timestampInNanoseconds: Long) {
mevent.usePinned { pinned ->
val packetListPtr = alloca(length.toULong()) ?: return
val packetListRef: CValuesRef<MIDIPacketList> = packetListPtr.reinterpret()
MIDIPacketListInit(packetListRef)
MIDIPacketListAdd(packetListRef, 1U, null, timestampInNanoseconds.toULong(), length.toULong(), pinned.addressOf(offset).reinterpret())
MIDISend(portRef, coreMidiPortDetails.endpoint, packetListRef)
memScoped {
val packetList = alloc<MIDIPacketList>()
val curPacket = MIDIPacketListInit(packetList.ptr)
MIDIPacketListAdd(packetList.ptr, length.toULong(), curPacket, timestampInNanoseconds.toULong(), length.toULong(), pinned.addressOf(offset).reinterpret())
MIDISend(portRef, coreMidiPortDetails.endpoint, packetList.ptr)
}
}
}
}

0 comments on commit 48d4103

Please sign in to comment.