Skip to content

Commit

Permalink
canCreateVirtualPort is now a function that takes PortCreateContext a…
Browse files Browse the repository at this point in the history
…s argument.

It is required because when we use MergedMidiAccess each entry might support
virtual port or not depending on its context and implementation (e.g. when
the context expects UMP ports and the MidiAccess only supports MIDI1).

To make it effective we now deprecate canCreateVirtualPort property.
  • Loading branch information
atsushieno committed Aug 26, 2024
1 parent ac849c4 commit 987a036
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class AlsaMidiAccess : MidiAccess() {

override val name: String
get() = "ALSA"
@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
override val canCreateVirtualPort: Boolean
get() = true
override fun canCreateVirtualPort(context: PortCreatorContext): Boolean = true
override val supportsUmpTransport: Boolean
get() = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ class RtMidiAccess : MidiAccess() {

// Input/Output

@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
override val canCreateVirtualPort: Boolean
get() = when(library.rtmidi_out_get_current_api(library.rtmidi_out_create_default())) {
library.RTMIDI_API_LINUX_ALSA,
library.RTMIDI_API_MACOSX_CORE -> true
else -> false
}

override fun canCreateVirtualPort(context: PortCreatorContext): Boolean =
context.midiProtocol == MidiTransportProtocol.MIDI1 && canCreateVirtualPort

override suspend fun createVirtualInputSender(context: PortCreatorContext): MidiOutput =
RtMidiVirtualOutput(context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ class RtMidiNativeAccess() : MidiAccess() {

// Input/Output

@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
override val canCreateVirtualPort: Boolean
get() = when(rtmidi_out_get_current_api(rtmidi_out_create_default())) {
RtMidiApi.RTMIDI_API_LINUX_ALSA,
RtMidiApi.RTMIDI_API_MACOSX_CORE -> true
else -> false
}

override fun canCreateVirtualPort(context: PortCreatorContext): Boolean =
context.midiProtocol == MidiTransportProtocol.MIDI1 && canCreateVirtualPort

override suspend fun createVirtualInputSender(context: PortCreatorContext): MidiOutput =
RtMidiVirtualOutput(context)

Expand Down
6 changes: 6 additions & 0 deletions ktmidi/api/android/ktmidi.api
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,14 @@ public final class dev/atsushieno/ktmidi/GeneralMidi2$Percussions {

public final class dev/atsushieno/ktmidi/MergedMidiAccess : dev/atsushieno/ktmidi/MidiAccess {
public fun <init> (Ljava/lang/String;Ljava/util/List;)V
public fun canCreateVirtualPort (Ldev/atsushieno/ktmidi/PortCreatorContext;)Z
public fun createVirtualInputSender (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun createVirtualOutputReceiver (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getCanCreateVirtualPort ()Z
public fun getInputs ()Ljava/lang/Iterable;
public fun getName ()Ljava/lang/String;
public fun getOutputs ()Ljava/lang/Iterable;
public fun getSupportsUmpTransport ()Z
public fun openInput (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun openOutput (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Expand Down Expand Up @@ -1018,6 +1023,7 @@ public final class dev/atsushieno/ktmidi/Midi2TrackSplitter$Companion {

public abstract class dev/atsushieno/ktmidi/MidiAccess {
public fun <init> ()V
public fun canCreateVirtualPort (Ldev/atsushieno/ktmidi/PortCreatorContext;)Z
public fun createVirtualInputSender (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun createVirtualOutputReceiver (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getCanCreateVirtualPort ()Z
Expand Down
6 changes: 6 additions & 0 deletions ktmidi/api/jvm/ktmidi.api
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,14 @@ public final class dev/atsushieno/ktmidi/JvmMidiAccess : dev/atsushieno/ktmidi/M

public final class dev/atsushieno/ktmidi/MergedMidiAccess : dev/atsushieno/ktmidi/MidiAccess {
public fun <init> (Ljava/lang/String;Ljava/util/List;)V
public fun canCreateVirtualPort (Ldev/atsushieno/ktmidi/PortCreatorContext;)Z
public fun createVirtualInputSender (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun createVirtualOutputReceiver (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getCanCreateVirtualPort ()Z
public fun getInputs ()Ljava/lang/Iterable;
public fun getName ()Ljava/lang/String;
public fun getOutputs ()Ljava/lang/Iterable;
public fun getSupportsUmpTransport ()Z
public fun openInput (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun openOutput (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Expand Down Expand Up @@ -1013,6 +1018,7 @@ public final class dev/atsushieno/ktmidi/Midi2TrackSplitter$Companion {

public abstract class dev/atsushieno/ktmidi/MidiAccess {
public fun <init> ()V
public fun canCreateVirtualPort (Ldev/atsushieno/ktmidi/PortCreatorContext;)Z
public fun createVirtualInputSender (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun createVirtualOutputReceiver (Ldev/atsushieno/ktmidi/PortCreatorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getCanCreateVirtualPort ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ abstract class CoreMidiAccess : MidiAccess() {
TraditionalCoreMidiAccess()
}

@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
override val canCreateVirtualPort: Boolean = true

override fun canCreateVirtualPort(context: PortCreatorContext): Boolean = canCreateVirtualPort

override val inputs: Iterable<MidiPortDetails>
get() = (0UL until MIDIGetNumberOfSources())
.map { MIDIGetSource(it) }.filter { it != 0u }.map { CoreMidiPortDetails(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,23 @@ class MergedMidiAccess(override val name: String, private val list: List<MidiAcc
val details = outputs.first { it.id == portId } as MidiPortDetailsWrapper
return MidiOutputWrapper(details, details.midiAccess.openOutput(details.source.id))
}

@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
override val canCreateVirtualPort: Boolean
get() = list.any { it.canCreateVirtualPort }

override val supportsUmpTransport: Boolean
get() = list.any { it.supportsUmpTransport }

override fun canCreateVirtualPort(context: PortCreatorContext) = list.any { it.canCreateVirtualPort(context) }

override suspend fun createVirtualInputSender(context: PortCreatorContext): MidiOutput =
list.firstOrNull { it.canCreateVirtualPort(context) }
?.createVirtualInputSender(context)
?: throw UnsupportedOperationException()

override suspend fun createVirtualOutputReceiver(context: PortCreatorContext): MidiInput =
list.firstOrNull { it.canCreateVirtualPort(context) }
?.createVirtualOutputReceiver(context)
?: throw UnsupportedOperationException()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ abstract class MidiAccess {
open val canDetectStateChanges = false
var stateChanged : (StateChange, MidiPortDetails) -> Unit = { _, _ -> }

@Deprecated("Use canCreateVirtualPort(PortCreatorContext) instead")
open val canCreateVirtualPort = false

open fun canCreateVirtualPort(context: PortCreatorContext): Boolean =
canCreateVirtualPort && if (context.midiProtocol == MidiTransportProtocol.UMP) supportsUmpTransport else true

open suspend fun createVirtualInputSender(context: PortCreatorContext): MidiOutput {
throw UnsupportedOperationException()
}
Expand Down

0 comments on commit 987a036

Please sign in to comment.