From 89cb56ae95fec15126851d1ff2239818bf88c113 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 27 Mar 2024 15:04:54 -0400 Subject: [PATCH] If Microphone permission is blocked on Mac, show error dialog with instructions to give permission. --- Makefile | 2 +- adapters/standalone.cpp | 7 +++++++ include/rtaudio.hpp | 5 +++++ src/rtaudio.mm | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/rtaudio.mm diff --git a/Makefile b/Makefile index dd65371e..9d26f615 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ ifdef ARCH_MAC SOURCES += $(wildcard src/*.mm src/*/*.mm) SOURCES += dep/osdialog/osdialog_mac.m LDFLAGS += -lpthread -ldl - LDFLAGS += -framework SystemConfiguration -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo -framework CoreAudio -framework CoreMIDI + LDFLAGS += -framework SystemConfiguration -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo -framework CoreAudio -framework CoreMIDI -framework AVFoundation LDFLAGS += -Wl,-all_load LDFLAGS += dep/lib/libGLEW.a dep/lib/libglfw3.a dep/lib/libjansson.a dep/lib/libcurl.a dep/lib/libssl.a dep/lib/libcrypto.a -Wl,-load_hidden,dep/lib/libarchive.a -Wl,-load_hidden,dep/lib/libzstd.a dep/lib/libspeexdsp.a dep/lib/libsamplerate.a -Wl,-load_hidden,dep/lib/librtmidi.a -Wl,-load_hidden,dep/lib/librtaudio.a endif diff --git a/adapters/standalone.cpp b/adapters/standalone.cpp index 3afb242a..4c6b98a8 100644 --- a/adapters/standalone.cpp +++ b/adapters/standalone.cpp @@ -204,6 +204,13 @@ int main(int argc, char* argv[]) { INFO("Initializing audio"); audio::init(); rtaudioInit(); +#if defined ARCH_MAC + if (rtaudioIsMicrophoneBlocked()) { + std::string msg = "Rack cannot access audio input because Microphone permission is blocked."; + msg += "\n\nGive permission to Rack by opening Apple's System Settings and enabling Privacy & Security > Microphone > " + APP_NAME + " " + APP_VERSION_MAJOR + " " + APP_EDITION_NAME + "."; + osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, msg.c_str()); + } +#endif INFO("Initializing MIDI"); midi::init(); rtmidiInit(); diff --git a/include/rtaudio.hpp b/include/rtaudio.hpp index 899f507d..e15f377c 100644 --- a/include/rtaudio.hpp +++ b/include/rtaudio.hpp @@ -7,5 +7,10 @@ namespace rack { PRIVATE void rtaudioInit(); +#if defined ARCH_MAC +/** Checks if Microphone permission is blocked on Mac. */ +bool rtaudioIsMicrophoneBlocked(); +#endif + } // namespace rack diff --git a/src/rtaudio.mm b/src/rtaudio.mm new file mode 100644 index 00000000..176cbaf6 --- /dev/null +++ b/src/rtaudio.mm @@ -0,0 +1,18 @@ +#include +#import + +namespace rack { + + +bool rtaudioIsMicrophoneBlocked() { + // authorizationStatusForMediaType is only available on Mac 10.14+. + if (@available(macOS 10.14, *)) { + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; + return status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied; + } + // On earlier versions, assume microphone is not blocked. + return true; +} + + +} // namespace rack