Midi: improve error handling

This commit is contained in:
Megamouse 2023-11-08 09:03:05 +01:00
parent e8a3ac592d
commit 648bf25557
3 changed files with 77 additions and 18 deletions

View File

@ -35,29 +35,44 @@ usb_device_rb3_midi_guitar::usb_device_rb3_midi_guitar(const std::array<u8, 7>&
midi_in = rtmidi_in_create_default(); midi_in = rtmidi_in_create_default();
ensure(midi_in); 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) if (!midi_in->ok)
{ {
rb3_midi_guitar_log.error("Could not get MIDI in ptr: %s", midi_in->msg); rb3_midi_guitar_log.error("Could not get MIDI in ptr: %s", midi_in->msg);
return; 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); 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); rb3_midi_guitar_log.error("Could not get MIDI port count: %s", midi_in->msg);
return; 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]{}; char buf[128]{};
s32 size = sizeof(buf); 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); rb3_midi_guitar_log.error("Error getting port name for port %d: %s", port_number, midi_in->msg);
return; return;
@ -68,6 +83,13 @@ usb_device_rb3_midi_guitar::usb_device_rb3_midi_guitar(const std::array<u8, 7>&
if (device_name == buf) if (device_name == buf)
{ {
rtmidi_open_port(midi_in, port_number, "RPCS3 MIDI Guitar Input"); 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); rb3_midi_guitar_log.success("Connected to device: %s", device_name);
return; return;
} }

View File

@ -23,27 +23,42 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::array<u8,
midi_in = rtmidi_in_create_default(); midi_in = rtmidi_in_create_default();
ensure(midi_in); ensure(midi_in);
rb3_midi_keyboard_log.notice("Using %s API", rtmidi_api_name(rtmidi_in_get_current_api(midi_in)));
if (!midi_in->ok) if (!midi_in->ok)
{ {
rb3_midi_keyboard_log.error("Could not get MIDI in ptr: %s", midi_in->msg); rb3_midi_keyboard_log.error("Could not get MIDI in ptr: %s", midi_in->msg);
return; return;
} }
const s32 port_count = rtmidi_get_port_count(midi_in); const RtMidiApi api = rtmidi_in_get_current_api(midi_in);
if (port_count == -1) if (!midi_in->ok)
{
rb3_midi_keyboard_log.error("Could not get MIDI api: %s", midi_in->msg);
return;
}
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); rb3_midi_keyboard_log.error("Could not get MIDI port count: %s", midi_in->msg);
return; 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]{}; char buf[128]{};
s32 size = sizeof(buf); 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); rb3_midi_keyboard_log.error("Error getting port name for port %d: %s", port_number, midi_in->msg);
return; return;
@ -54,6 +69,13 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::array<u8,
if (device_name == buf) if (device_name == buf)
{ {
rtmidi_open_port(midi_in, port_number, "RPCS3 MIDI Keyboard Input"); rtmidi_open_port(midi_in, port_number, "RPCS3 MIDI Keyboard Input");
if (!midi_in->ok)
{
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); rb3_midi_keyboard_log.success("Connected to device: %s", device_name);
return; return;
} }

View File

@ -27,27 +27,42 @@ void midi_creator::refresh_list()
std::unique_ptr<RtMidiWrapper, decltype(deleter)> midi_in(rtmidi_in_create_default()); std::unique_ptr<RtMidiWrapper, decltype(deleter)> midi_in(rtmidi_in_create_default());
ensure(midi_in); 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) if (!midi_in->ok)
{ {
cfg_log.error("Could not get MIDI in ptr: %s", midi_in->msg); cfg_log.error("Could not get MIDI in ptr: %s", midi_in->msg);
return; return;
} }
const s32 port_count = rtmidi_get_port_count(midi_in.get()); const RtMidiApi api = rtmidi_in_get_current_api(midi_in.get());
if (port_count == -1) 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 (!midi_in->ok || port_count == umax)
{ {
cfg_log.error("Could not get MIDI port count: %s", midi_in->msg); cfg_log.error("Could not get MIDI port count: %s", midi_in->msg);
return; 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]{}; char buf[128]{};
s32 size = sizeof(buf); 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); cfg_log.error("Error getting MIDI port name for port %d: %s", port_number, midi_in->msg);
continue; continue;