[orx-midi] Allow incomplete device name (#310)

This commit is contained in:
Abe Pazos
2023-05-10 15:32:38 +02:00
committed by GitHub
parent 42853ce997
commit 1c75e2182a

View File

@@ -277,7 +277,21 @@ fun listMidiDevices() = MidiDeviceDescription.list()
/** /**
* Open a MIDI device by name * 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 * @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)
}