From dd9633303c5d77316eedd8fedd3891fbbbe91e62 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 1 Jan 2021 05:18:07 -0500 Subject: [PATCH] Turn off printing warnings to stderr for RtAudio and RtMidi. Fix exception catching when instantiating RtMidi objects. --- src/rtaudio.cpp | 2 ++ src/rtmidi.cpp | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/rtaudio.cpp b/src/rtaudio.cpp index 391d780b..7e77f41d 100644 --- a/src/rtaudio.cpp +++ b/src/rtaudio.cpp @@ -30,6 +30,7 @@ struct RtAudioDevice : audio::Device { RtAudioDevice(RtAudio::Api api, int deviceId) { rtAudio = new RtAudio(api); + rtAudio->showWarnings(false); if (!rtAudio) { throw Exception(string::f("Failed to create RtAudio driver %d", api)); } @@ -202,6 +203,7 @@ struct RtAudioDriver : audio::Driver { RtAudioDriver(RtAudio::Api api) { rtAudio = new RtAudio(api); + rtAudio->showWarnings(false); } ~RtAudioDriver() { diff --git a/src/rtmidi.cpp b/src/rtmidi.cpp index bbcf6ae2..689eb6de 100644 --- a/src/rtmidi.cpp +++ b/src/rtmidi.cpp @@ -21,16 +21,23 @@ namespace rack { +static void rtMidiErrorCallback(RtMidiError::Type type, const std::string& errorText, void* userData) { + // Do nothing +} + + struct RtMidiInputDevice : midi::InputDevice { RtMidiIn* rtMidiIn; std::string name; RtMidiInputDevice(int driverId, int deviceId) { - rtMidiIn = new RtMidiIn((RtMidi::Api) driverId, "VCV Rack"); - if (!rtMidiIn) { - throw Exception(string::f("Failed to create RtMidi input driver %d", driverId)); + try { + rtMidiIn = new RtMidiIn((RtMidi::Api) driverId, "VCV Rack"); } - + catch (RtMidiError& e) { + throw Exception(string::f("Failed to create RtMidi input driver %d: %s", driverId, e.what())); + } + rtMidiIn->setErrorCallback(rtMidiErrorCallback); rtMidiIn->ignoreTypes(false, false, false); rtMidiIn->setCallback(midiInputCallback, this); @@ -50,6 +57,7 @@ struct RtMidiInputDevice : midi::InputDevice { } ~RtMidiInputDevice() { + // This does not throw for any driver API rtMidiIn->closePort(); delete rtMidiIn; } @@ -93,10 +101,13 @@ struct RtMidiOutputDevice : midi::OutputDevice { bool stopped = false; RtMidiOutputDevice(int driverId, int deviceId) : messageQueue(messageEarlier) { - rtMidiOut = new RtMidiOut((RtMidi::Api) driverId, "VCV Rack"); - if (!rtMidiOut) { - throw Exception(string::f("Failed to create RtMidi output driver %d", driverId)); + try { + rtMidiOut = new RtMidiOut((RtMidi::Api) driverId, "VCV Rack"); } + catch (RtMidiError& e) { + throw Exception(string::f("Failed to create RtMidi output driver %d: %s", driverId, e.what())); + } + rtMidiOut->setErrorCallback(rtMidiErrorCallback); try { name = rtMidiOut->getPortName(deviceId); @@ -117,6 +128,7 @@ struct RtMidiOutputDevice : midi::OutputDevice { ~RtMidiOutputDevice() { stopThread(); + // This does not throw for any driver API rtMidiOut->closePort(); delete rtMidiOut; } @@ -194,20 +206,28 @@ struct RtMidiDriver : midi::Driver { RtMidiDriver(int driverId) { this->driverId = driverId; - rtMidiIn = new RtMidiIn((RtMidi::Api) driverId); - if (!rtMidiIn) { - throw Exception(string::f("Failed to create RtMidi input driver %d", driverId)); + + try { + rtMidiIn = new RtMidiIn((RtMidi::Api) driverId); + } + catch (RtMidiError& e) { + throw Exception(string::f("Failed to create RtMidi input driver %d: %s", driverId, e.what())); } + rtMidiIn->setErrorCallback(rtMidiErrorCallback); - rtMidiOut = new RtMidiOut((RtMidi::Api) driverId); - if (!rtMidiOut) { - throw Exception(string::f("Failed to create RtMidi output driver %d", driverId)); + try { + rtMidiOut = new RtMidiOut((RtMidi::Api) driverId); + } + catch (RtMidiError& e) { + throw Exception(string::f("Failed to create RtMidi output driver %d: %s", driverId, e.what())); } + rtMidiOut->setErrorCallback(rtMidiErrorCallback); } ~RtMidiDriver() { assert(inputDevices.empty()); assert(outputDevices.empty()); + // This does not throw for any driver API delete rtMidiIn; delete rtMidiOut; }