From 23ae66151b35750be4b83cf2d3020b947c55e07a Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sun, 21 Jan 2024 10:32:43 -0800 Subject: [PATCH 1/4] Make compatible with rtaudio API 5 and 6. Recent rtaudio changed the API to not throw exceptions anymore and also have DeviceIDs not queried by index but IDs that are provided separately ( https://github.com/thestk/rtaudio/releases ). Adapt the code-base to be compatible with the old and the new API as we have to exepect that in a transition period both APIs are common on various build platforms. --- sink_modules/audio_sink/src/main.cpp | 36 ++++++++++++++++++++---- source_modules/audio_source/src/main.cpp | 33 +++++++++++++++++++--- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/sink_modules/audio_sink/src/main.cpp b/sink_modules/audio_sink/src/main.cpp index 15b7c260..147e7782 100644 --- a/sink_modules/audio_sink/src/main.cpp +++ b/sink_modules/audio_sink/src/main.cpp @@ -31,6 +31,10 @@ public: monoPacker.init(&s2m.out, 512); stereoPacker.init(_stream->sinkOut, 512); +#if RTAUDIO_VERSION_MAJOR >= 6 + audio.setErrorCallback(&reportErrorsAsException); +#endif + bool created = false; std::string device = ""; config.acquire(); @@ -42,12 +46,15 @@ public: device = config.conf[_streamName]["device"]; config.release(created); - int count = audio.getDeviceCount(); RtAudio::DeviceInfo info; +#if RTAUDIO_VERSION_MAJOR >= 6 + for (int i : audio.getDeviceIds()) { +#else + int count = audio.getDeviceCount(); for (int i = 0; i < count; i++) { +#endif try { info = audio.getDeviceInfo(i); - if (!info.probed) { continue; } if (info.outputChannels == 0) { continue; } if (info.isDefaultOutput) { defaultDevId = devList.size(); } devList.push_back(info); @@ -55,8 +62,8 @@ public: txtDevList += info.name; txtDevList += '\0'; } - catch (std::exception e) { - flog::error("AudioSinkModule Error getting audio device info: {0}", e.what()); + catch (const std::exception& e) { + flog::error("AudioSinkModule Error getting audio device info: id={0}: {1}", i, e.what()); } } selectByName(device); @@ -156,6 +163,23 @@ public: } } +#if RTAUDIO_VERSION_MAJOR >= 6 + static void reportErrorsAsException(RtAudioErrorType type, + const std::string& errorText) { + switch (type) { + case RtAudioErrorType::RTAUDIO_NO_ERROR: + return; + case RtAudioErrorType::RTAUDIO_WARNING: + case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND: + case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT: + flog::warn("AudioSink: {0} ({1})", errorText, (int)type); + break; + default: + throw std::runtime_error(errorText); + } + } +#endif + private: bool doStart() { RtAudio::StreamParameters parameters; @@ -172,8 +196,8 @@ private: audio.startStream(); stereoPacker.start(); } - catch (RtAudioError& e) { - flog::error("Could not open audio device"); + catch (const std::exception& e) { + flog::error("Could not open audio device {0}", e.what()); return false; } diff --git a/source_modules/audio_source/src/main.cpp b/source_modules/audio_source/src/main.cpp index 99efa379..b7abe253 100644 --- a/source_modules/audio_source/src/main.cpp +++ b/source_modules/audio_source/src/main.cpp @@ -35,6 +35,10 @@ public: AudioSourceModule(std::string name) { this->name = name; +#if RTAUDIO_VERSION_MAJOR >= 6 + audio.setErrorCallback(&reportErrorsAsException); +#endif + sampleRate = 48000.0; handler.ctx = this; @@ -83,21 +87,25 @@ public: void refresh() { devices.clear(); +#if RTAUDIO_VERSION_MAJOR >= 6 + for (int i : audio.getDeviceIds()) { +#else int count = audio.getDeviceCount(); for (int i = 0; i < count; i++) { +#endif try { // Get info auto info = audio.getDeviceInfo(i); // Check that it has a stereo input - if (info.probed && info.inputChannels < 2) { continue; } + if (info.inputChannels < 2) { continue; } // Save info DeviceInfo dinfo = { info, i }; devices.define(info.name, info.name, dinfo); } - catch (std::exception e) { - flog::error("Error getting audio device info: {0}", e.what()); + catch (const std::exception& e) { + flog::error("Error getting audio device info: id={0}: {1}", i, e.what()); } } } @@ -254,6 +262,23 @@ private: return 0; } +#if RTAUDIO_VERSION_MAJOR >= 6 + static void reportErrorsAsException(RtAudioErrorType type, + const std::string& errorText) { + switch (type) { + case RtAudioErrorType::RTAUDIO_NO_ERROR: + return; + case RtAudioErrorType::RTAUDIO_WARNING: + case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND: + case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT: + flog::warn("AudioSource: {0} ({1})", errorText, (int)type); + break; + default: + throw std::runtime_error(errorText); + } + } +#endif + std::string name; bool enabled = true; dsp::stream stream; @@ -290,4 +315,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) { MOD_EXPORT void _END_() { config.disableAutoSave(); config.save(); -} \ No newline at end of file +} From 8cefeadbd477ab4c2a9eb0c07cb2d4696a8e05c3 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Sun, 21 Jan 2024 23:48:04 +0100 Subject: [PATCH 2/4] Fix styling, modern computers can display more than 40 columns --- sink_modules/audio_sink/src/main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sink_modules/audio_sink/src/main.cpp b/sink_modules/audio_sink/src/main.cpp index 147e7782..c7098c89 100644 --- a/sink_modules/audio_sink/src/main.cpp +++ b/sink_modules/audio_sink/src/main.cpp @@ -164,8 +164,7 @@ public: } #if RTAUDIO_VERSION_MAJOR >= 6 - static void reportErrorsAsException(RtAudioErrorType type, - const std::string& errorText) { + static void reportErrorsAsException(RtAudioErrorType type, const std::string& errorText) { switch (type) { case RtAudioErrorType::RTAUDIO_NO_ERROR: return; From f197cf6bd9fa1caf5127f64602b8daad0c4c77fd Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Sun, 21 Jan 2024 23:49:23 +0100 Subject: [PATCH 3/4] Fix styling in the other file --- source_modules/audio_source/src/main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source_modules/audio_source/src/main.cpp b/source_modules/audio_source/src/main.cpp index b7abe253..cbaf08b8 100644 --- a/source_modules/audio_source/src/main.cpp +++ b/source_modules/audio_source/src/main.cpp @@ -263,8 +263,7 @@ private: } #if RTAUDIO_VERSION_MAJOR >= 6 - static void reportErrorsAsException(RtAudioErrorType type, - const std::string& errorText) { + static void reportErrorsAsException(RtAudioErrorType type, const std::string& errorText) { switch (type) { case RtAudioErrorType::RTAUDIO_NO_ERROR: return; From 5a003e99d2b4337a2a8d24a9587d4e8019e62659 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sun, 21 Jan 2024 15:23:45 -0800 Subject: [PATCH 4/4] Address review comments. --- sink_modules/audio_sink/src/main.cpp | 11 +++++++---- source_modules/audio_source/src/main.cpp | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sink_modules/audio_sink/src/main.cpp b/sink_modules/audio_sink/src/main.cpp index c7098c89..a4c82118 100644 --- a/sink_modules/audio_sink/src/main.cpp +++ b/sink_modules/audio_sink/src/main.cpp @@ -32,7 +32,7 @@ public: stereoPacker.init(_stream->sinkOut, 512); #if RTAUDIO_VERSION_MAJOR >= 6 - audio.setErrorCallback(&reportErrorsAsException); + audio.setErrorCallback(&errorCallback); #endif bool created = false; @@ -55,6 +55,9 @@ public: #endif try { info = audio.getDeviceInfo(i); +#if !defined(RTAUDIO_VERSION_MAJOR) || RTAUDIO_VERSION_MAJOR < 6 + if (!info.probed) { continue; } +#endif if (info.outputChannels == 0) { continue; } if (info.isDefaultOutput) { defaultDevId = devList.size(); } devList.push_back(info); @@ -63,7 +66,7 @@ public: txtDevList += '\0'; } catch (const std::exception& e) { - flog::error("AudioSinkModule Error getting audio device info: id={0}: {1}", i, e.what()); + flog::error("AudioSinkModule Error getting audio device ({}) info: {}", i, e.what()); } } selectByName(device); @@ -164,14 +167,14 @@ public: } #if RTAUDIO_VERSION_MAJOR >= 6 - static void reportErrorsAsException(RtAudioErrorType type, const std::string& errorText) { + static void errorCallback(RtAudioErrorType type, const std::string& errorText) { switch (type) { case RtAudioErrorType::RTAUDIO_NO_ERROR: return; case RtAudioErrorType::RTAUDIO_WARNING: case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND: case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT: - flog::warn("AudioSink: {0} ({1})", errorText, (int)type); + flog::warn("AudioSinkModule Warning: {} ({})", errorText, (int)type); break; default: throw std::runtime_error(errorText); diff --git a/source_modules/audio_source/src/main.cpp b/source_modules/audio_source/src/main.cpp index cbaf08b8..e573a818 100644 --- a/source_modules/audio_source/src/main.cpp +++ b/source_modules/audio_source/src/main.cpp @@ -36,7 +36,7 @@ public: this->name = name; #if RTAUDIO_VERSION_MAJOR >= 6 - audio.setErrorCallback(&reportErrorsAsException); + audio.setErrorCallback(&errorCallback); #endif sampleRate = 48000.0; @@ -97,6 +97,9 @@ public: // Get info auto info = audio.getDeviceInfo(i); +#if !defined(RTAUDIO_VERSION_MAJOR) || RTAUDIO_VERSION_MAJOR < 6 + if (!info.probed) { continue; } +#endif // Check that it has a stereo input if (info.inputChannels < 2) { continue; } @@ -105,7 +108,7 @@ public: devices.define(info.name, info.name, dinfo); } catch (const std::exception& e) { - flog::error("Error getting audio device info: id={0}: {1}", i, e.what()); + flog::error("Error getting audio device ({}) info: {}", i, e.what()); } } } @@ -263,14 +266,14 @@ private: } #if RTAUDIO_VERSION_MAJOR >= 6 - static void reportErrorsAsException(RtAudioErrorType type, const std::string& errorText) { + static void errorCallback(RtAudioErrorType type, const std::string& errorText) { switch (type) { case RtAudioErrorType::RTAUDIO_NO_ERROR: return; case RtAudioErrorType::RTAUDIO_WARNING: case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND: case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT: - flog::warn("AudioSource: {0} ({1})", errorText, (int)type); + flog::warn("AudioSourceModule Warning: {} ({})", errorText, (int)type); break; default: throw std::runtime_error(errorText);