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

Midi: improve error handling #14802

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 28 additions & 6 deletions rpcs3/Emu/Io/RB3MidiGuitar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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;
Expand All @@ -68,6 +83,13 @@ usb_device_rb3_midi_guitar::usb_device_rb3_midi_guitar(const std::array<u8, 7>&
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;
}
Expand Down
34 changes: 28 additions & 6 deletions rpcs3/Emu/Io/RB3MidiKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,42 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::array<u8,
midi_in = rtmidi_in_create_default();
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)
{
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;
Expand All @@ -54,6 +69,13 @@ usb_device_rb3_midi_keyboard::usb_device_rb3_midi_keyboard(const std::array<u8,
if (device_name == buf)
{
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);
return;
}
Expand Down
27 changes: 21 additions & 6 deletions rpcs3/rpcs3qt/midi_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,42 @@ void midi_creator::refresh_list()
std::unique_ptr<RtMidiWrapper, decltype(deleter)> 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;
Expand Down