Skip to content

Commit

Permalink
ALSA: fix virtual port creation, there was inappropriately reused cli…
Browse files Browse the repository at this point in the history
…ents.
  • Loading branch information
atsushieno committed Jul 10, 2024
1 parent 78fde6f commit d97116b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class MidiDeviceManager {
applicationName = "KtMidi-CI-Tool V-In",
portName = "KtMidi-CI-Tool Virtual Out Port",
version = "1.0",
midiProtocol = MidiTransportProtocol.MIDI1, // if applicable
midiProtocol = MidiTransportProtocol.MIDI1,
)
val pcIn = PortCreatorContext(
manufacturer = "KtMidi project",
applicationName = "KtMidi-CI-Tool V-Out",
portName = "KtMidi-CI-Tool Virtual In Port",
version = "1.0",
midiProtocol = MidiTransportProtocol.MIDI1, // if applicable
midiProtocol = MidiTransportProtocol.MIDI1,
)

virtualMidiInput = midiAccessValue.createVirtualOutputReceiver(pcOut)
Expand All @@ -46,16 +46,14 @@ class MidiDeviceManager {
applicationName = "KtMidi-CI-Tool UMP V-In",
portName = "KtMidi-CI-Tool UMP Virtual Out Port",
version = "1.0",
midiProtocol = MidiTransportProtocol.UMP, // if applicable
umpGroup = 1
midiProtocol = MidiTransportProtocol.UMP,
)
val pcIn2 = PortCreatorContext(
manufacturer = "KtMidi project",
applicationName = "KtMidi-CI-Tool UMP V-Out",
portName = "KtMidi-CI-Tool UMP Virtual In Port",
version = "1.0",
midiProtocol = MidiTransportProtocol.UMP, // if applicable
umpGroup = 2
midiProtocol = MidiTransportProtocol.UMP,
)

virtualMidiInput2 = midiAccessValue.createVirtualOutputReceiver(pcOut2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ class AlsaSequencer(
private fun sendMidi1(port: Int, data: ByteArray, index: Int, count: Int) {
if (midiEventParserOutput == null) {
val ptr = snd_midi_event_t()
Alsa.snd_midi_event_new(midiEventBufferSize, ptr)
val ret = Alsa.snd_midi_event_new(midiEventBufferSize, ptr)
if (ret < 0)
throw AlsaException(ret.toInt())
midiEventParserOutput = ptr
}

Expand All @@ -247,7 +249,9 @@ class AlsaSequencer(
eventBufferOutput.put(seq_evt_off_dest_port, AddressUnknown.toByte())
eventBufferOutput.put(seq_evt_off_queue, QueueDirect.toByte())
// FIXME: should we provide error handler and catch it?
Alsa.snd_seq_event_output_direct(seq, ev)
val ret = Alsa.snd_seq_event_output_direct(seq, ev)
if (ret < 0)
throw AlsaException(ret)
}
i += ret.toInt()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.atsushieno.ktmidi

import dev.atsushieno.alsakt.*
import kotlinx.coroutines.delay

internal fun Byte.toUnsigned() : Int = if (this < 0) this + 0x100 else this.toInt()

Expand Down Expand Up @@ -107,9 +106,12 @@ class AlsaMidiAccess : MidiAccess() {
return AlsaMidiOutput (seq, AlsaMidiPortDetails (appPort), destPort)
}

private val seq: AlsaSequencer by lazy { AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking) }
private val seqIn1: AlsaSequencer by lazy { AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking) }
private val seqIn2: AlsaSequencer by lazy { AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking) }

override suspend fun createVirtualInputSender ( context: PortCreatorContext) : MidiOutput {
val seq = if (context.midiProtocol == MidiTransportProtocol.UMP) seqIn2 else seqIn1

val isUmp = context.midiProtocol == MidiTransportProtocol.UMP
val portCap = virtual_input_sender_connected_cap or
if (isUmp) AlsaPortCapabilities.UmpEndpoint else 0
Expand All @@ -133,7 +135,12 @@ class AlsaMidiAccess : MidiAccess() {
return AlsaVirtualMidiOutput (seq, details, { seq.deleteSimplePort(portNumber) }, send)
}

// should we have this as a property? It seems to cause internal crash.
private val seqOut1: AlsaSequencer by lazy { AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking) }
private val seqOut2: AlsaSequencer by lazy { AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking) }

override suspend fun createVirtualOutputReceiver ( context:PortCreatorContext): MidiInput {
//val seq = if (context.midiProtocol == MidiTransportProtocol.UMP) seqOut2 else seqOut1
val seq = AlsaSequencer (AlsaIOType.Duplex, AlsaIOMode.NonBlocking)

val portCap = virtual_output_receiver_connected_cap or
Expand Down

0 comments on commit d97116b

Please sign in to comment.