From 8a497dbb79d6f0cf1ac3cb2350d7c349715dccc8 Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Tue, 9 May 2023 13:01:19 +0200 Subject: [PATCH] [orx-midi] Allow incomplete device name --- .../src/main/kotlin/MidiTransceiver.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt b/orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt index 6e01d44ef..1d46a8d47 100644 --- a/orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt +++ b/orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt @@ -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) \ No newline at end of file +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) +} \ No newline at end of file