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 +}