Skip to content

Commit

Permalink
[orx-midi] Allow incomplete device name (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamoid authored May 10, 2023
1 parent 42853ce commit 1c75e21
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,21 @@ fun listMidiDevices() = MidiDeviceDescription.list()

/**
* Open a MIDI device by name
* @param name the name of the MIDI device to open
* @param name the name of the MIDI device to open. Either the
* exact name or the first characters of the name.
* @since 0.4.3
*/
fun Program.openMidiDevice(name: String) = MidiTransceiver.fromDeviceVendor(this, name)
fun Program.openMidiDevice(name: String): MidiTransceiver {
val devices = listMidiDevices()

val matchingDevice = devices.firstOrNull {
// Existing device name matches `name`
it.name == name
} ?: devices.firstOrNull {
// Existing device name starts with `name`
it.name.startsWith(name)
}
val actualName = matchingDevice?.name ?: name

return MidiTransceiver.fromDeviceVendor(this, actualName)
}

0 comments on commit 1c75e21

Please sign in to comment.