Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[orx-midi] provide an alternative for when a device is missing, better logging #312

Merged
merged 4 commits into from
May 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion orx-jvm/orx-midi/src/main/kotlin/MidiTransceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fun listMidiDevices() = MidiDeviceDescription.list()
* Open a MIDI device by name
* @param name the name of the MIDI device to open. Either the
* exact name or the first characters of the name.
* Throws an exception if the device name is not found.
* @since 0.4.3
*/
fun Program.openMidiDevice(name: String): MidiTransceiver {
Expand All @@ -294,4 +295,39 @@ fun Program.openMidiDevice(name: String): MidiTransceiver {
val actualName = matchingDevice?.name ?: name

return MidiTransceiver.fromDeviceVendor(this, actualName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can openMidiDevice be implemented using return openMidiDevilceOrNull(name) ?: error("no MIDI device found for query '$name'") . This has the benefit of clearer error messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does 6d2a4c5 look like?

Should all throws be error? Or leave them as they were?

}
}

/**
* Open a MIDI device by name
*
* @param name the name of the MIDI device to open. Either the
* exact name or the first characters of the name.
* Returns null if the device name is not found.
* @since 0.4.3
*/
fun Program.openMidiDeviceOrNull(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)
}

return if(matchingDevice != null)
MidiTransceiver.fromDeviceVendor(this, matchingDevice.name)
else
null
}

/**
* Open a dummy MIDI device
*
* Enables running programs that depend on a specific MIDI device
* when that device is not available.
* Usage: `val dev = openMidiDeviceOrNull("Twister") ?: dummyMidiDevice()`
* @since 0.4.3
*/
fun Program.dummyMidiDevice() = MidiTransceiver(this, null, null)