From dad7ec7940a81771fdb11c4107e8600d83a81e85 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Wed, 8 Nov 2023 09:03:05 +0100 Subject: [PATCH] Midi: improve error handling --- rpcs3/Emu/Io/RB3MidiGuitar.cpp | 34 ++++++++++++++++++++++++++------ rpcs3/Emu/Io/RB3MidiKeyboard.cpp | 34 ++++++++++++++++++++++++++------ rpcs3/rpcs3qt/midi_creator.cpp | 27 +++++++++++++++++++------ 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/rpcs3/Emu/Io/RB3MidiGuitar.cpp b/rpcs3/Emu/Io/RB3MidiGuitar.cpp index 77633ebf6da4..98957226383c 100644 --- a/rpcs3/Emu/Io/RB3MidiGuitar.cpp +++ b/rpcs3/Emu/Io/RB3MidiGuitar.cpp @@ -35,29 +35,44 @@ usb_device_rb3_midi_guitar::usb_device_rb3_midi_guitar(const std::array& midi_in = rtmidi_in_create_default(); ensure(midi_in); - rb3_midi_guitar_log.notice("Using %s API", rtmidi_api_name(rtmidi_in_get_current_api(midi_in))); - if (!midi_in->ok) { rb3_midi_guitar_log.error("Could not get MIDI in ptr: %s", midi_in->msg); return; } + const RtMidiApi api = rtmidi_in_get_current_api(midi_in); + + if (!midi_in->ok) + { + rb3_midi_guitar_log.error("Could not get MIDI api: %s", midi_in->msg); + return; + } + + if (const char* api_name = rtmidi_api_name(api)) + { + rb3_midi_guitar_log.notice("Using %s api", api_name); + } + else + { + rb3_midi_guitar_log.warning("Could not get MIDI api name"); + } + rtmidi_in_ignore_types(midi_in, false, true, true); - const s32 port_count = rtmidi_get_port_count(midi_in); + const u32 port_count = rtmidi_get_port_count(midi_in); - if (port_count == -1) + if (!midi_in->ok || port_count == umax) { rb3_midi_guitar_log.error("Could not get MIDI port count: %s", midi_in->msg); return; } - for (s32 port_number = 0; port_number < port_count; port_number++) + for (u32 port_number = 0; port_number < port_count; port_number++) { char buf[128]{}; s32 size = sizeof(buf); - if (rtmidi_get_port_name(midi_in, port_number, buf, &size) == -1) + if (rtmidi_get_port_name(midi_in, port_number, buf, &size) == -1 || !midi_in->ok) { rb3_midi_guitar_log.error("Error getting port name for port %d: %s", port_number, midi_in->msg); return; @@ -68,6 +83,13 @@ usb_device_rb3_midi_guitar::usb_device_rb3_midi_guitar(const std::array& if (device_name == buf) { rtmidi_open_port(midi_in, port_number, "RPCS3 MIDI Guitar Input"); + + if (!midi_in->ok) + { + rb3_midi_guitar_log.error("Could not open port %d for device '%s': %s", port_number, device_name, midi_in->msg); + return; + } + rb3_midi_guitar_log.success("Connected to device: %s", device_name); return; } diff --git a/rpcs3/Emu/Io/RB3MidiKeyboard.cpp b/rpcs3/Emu/Io/RB3MidiKeyboard.cpp index dd53eef0ec9e..ccfc2dbdc871 100644 --- a/rpcs3/Emu/Io/RB3MidiKeyboard.cpp +++ b/rpcs3/Emu/Io/RB3MidiKeyboard.cpp @@ -23,27 +23,42 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::arrayok) { rb3_midi_keyboard_log.error("Could not get MIDI in ptr: %s", midi_in->msg); return; } - const s32 port_count = rtmidi_get_port_count(midi_in); + const RtMidiApi api = rtmidi_in_get_current_api(midi_in); + + if (!midi_in->ok) + { + rb3_midi_keyboard_log.error("Could not get MIDI api: %s", midi_in->msg); + return; + } - if (port_count == -1) + if (const char* api_name = rtmidi_api_name(api)) + { + rb3_midi_keyboard_log.notice("Using %s api", api_name); + } + else + { + rb3_midi_keyboard_log.warning("Could not get MIDI api name"); + } + + const u32 port_count = rtmidi_get_port_count(midi_in); + + if (!midi_in->ok || port_count == umax) { rb3_midi_keyboard_log.error("Could not get MIDI port count: %s", midi_in->msg); return; } - for (s32 port_number = 0; port_number < port_count; port_number++) + for (u32 port_number = 0; port_number < port_count; port_number++) { char buf[128]{}; s32 size = sizeof(buf); - if (rtmidi_get_port_name(midi_in, port_number, buf, &size) == -1) + if (rtmidi_get_port_name(midi_in, port_number, buf, &size) == -1 || !midi_in->ok) { rb3_midi_keyboard_log.error("Error getting port name for port %d: %s", port_number, midi_in->msg); return; @@ -54,6 +69,13 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::arrayok) + { + rb3_midi_keyboard_log.error("Could not open port %d for device '%s': %s", port_number, device_name, midi_in->msg); + return; + } + rb3_midi_keyboard_log.success("Connected to device: %s", device_name); return; } diff --git a/rpcs3/rpcs3qt/midi_creator.cpp b/rpcs3/rpcs3qt/midi_creator.cpp index ff83461fec76..434a3fc5951c 100644 --- a/rpcs3/rpcs3qt/midi_creator.cpp +++ b/rpcs3/rpcs3qt/midi_creator.cpp @@ -27,27 +27,42 @@ void midi_creator::refresh_list() std::unique_ptr midi_in(rtmidi_in_create_default()); ensure(midi_in); - cfg_log.notice("MIDI: Using %s api", rtmidi_api_name(rtmidi_in_get_current_api(midi_in.get()))); - if (!midi_in->ok) { cfg_log.error("Could not get MIDI in ptr: %s", midi_in->msg); return; } - const s32 port_count = rtmidi_get_port_count(midi_in.get()); + const RtMidiApi api = rtmidi_in_get_current_api(midi_in.get()); + + if (!midi_in->ok) + { + cfg_log.error("Could not get MIDI api: %s", midi_in->msg); + return; + } + + if (const char* api_name = rtmidi_api_name(api)) + { + cfg_log.notice("MIDI: Using %s api", api_name); + } + else + { + cfg_log.warning("Could not get MIDI api name"); + } + + const u32 port_count = rtmidi_get_port_count(midi_in.get()); - if (port_count == -1) + if (!midi_in->ok || port_count == umax) { cfg_log.error("Could not get MIDI port count: %s", midi_in->msg); return; } - for (s32 port_number = 0; port_number < port_count; port_number++) + for (u32 port_number = 0; port_number < port_count; port_number++) { char buf[128]{}; s32 size = sizeof(buf); - if (rtmidi_get_port_name(midi_in.get(), port_number, buf, &size) == -1) + if (rtmidi_get_port_name(midi_in.get(), port_number, buf, &size) == -1 || !midi_in->ok) { cfg_log.error("Error getting MIDI port name for port %d: %s", port_number, midi_in->msg); continue;