Skip to content

Commit 86dcec7

Browse files
Merge pull request #1284 from hzeller/20240121-adapt-rtaudio
Make compatible with rtaudio API 5 and 6.
2 parents fa76b4e + 5a003e9 commit 86dcec7

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

sink_modules/audio_sink/src/main.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class AudioSink : SinkManager::Sink {
3131
monoPacker.init(&s2m.out, 512);
3232
stereoPacker.init(_stream->sinkOut, 512);
3333

34+
#if RTAUDIO_VERSION_MAJOR >= 6
35+
audio.setErrorCallback(&errorCallback);
36+
#endif
37+
3438
bool created = false;
3539
std::string device = "";
3640
config.acquire();
@@ -42,21 +46,27 @@ class AudioSink : SinkManager::Sink {
4246
device = config.conf[_streamName]["device"];
4347
config.release(created);
4448

45-
int count = audio.getDeviceCount();
4649
RtAudio::DeviceInfo info;
50+
#if RTAUDIO_VERSION_MAJOR >= 6
51+
for (int i : audio.getDeviceIds()) {
52+
#else
53+
int count = audio.getDeviceCount();
4754
for (int i = 0; i < count; i++) {
55+
#endif
4856
try {
4957
info = audio.getDeviceInfo(i);
58+
#if !defined(RTAUDIO_VERSION_MAJOR) || RTAUDIO_VERSION_MAJOR < 6
5059
if (!info.probed) { continue; }
60+
#endif
5161
if (info.outputChannels == 0) { continue; }
5262
if (info.isDefaultOutput) { defaultDevId = devList.size(); }
5363
devList.push_back(info);
5464
deviceIds.push_back(i);
5565
txtDevList += info.name;
5666
txtDevList += '\0';
5767
}
58-
catch (std::exception e) {
59-
flog::error("AudioSinkModule Error getting audio device info: {0}", e.what());
68+
catch (const std::exception& e) {
69+
flog::error("AudioSinkModule Error getting audio device ({}) info: {}", i, e.what());
6070
}
6171
}
6272
selectByName(device);
@@ -156,6 +166,22 @@ class AudioSink : SinkManager::Sink {
156166
}
157167
}
158168

169+
#if RTAUDIO_VERSION_MAJOR >= 6
170+
static void errorCallback(RtAudioErrorType type, const std::string& errorText) {
171+
switch (type) {
172+
case RtAudioErrorType::RTAUDIO_NO_ERROR:
173+
return;
174+
case RtAudioErrorType::RTAUDIO_WARNING:
175+
case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND:
176+
case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT:
177+
flog::warn("AudioSinkModule Warning: {} ({})", errorText, (int)type);
178+
break;
179+
default:
180+
throw std::runtime_error(errorText);
181+
}
182+
}
183+
#endif
184+
159185
private:
160186
bool doStart() {
161187
RtAudio::StreamParameters parameters;
@@ -172,8 +198,8 @@ class AudioSink : SinkManager::Sink {
172198
audio.startStream();
173199
stereoPacker.start();
174200
}
175-
catch (RtAudioError& e) {
176-
flog::error("Could not open audio device");
201+
catch (const std::exception& e) {
202+
flog::error("Could not open audio device {0}", e.what());
177203
return false;
178204
}
179205

source_modules/audio_source/src/main.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class AudioSourceModule : public ModuleManager::Instance {
3535
AudioSourceModule(std::string name) {
3636
this->name = name;
3737

38+
#if RTAUDIO_VERSION_MAJOR >= 6
39+
audio.setErrorCallback(&errorCallback);
40+
#endif
41+
3842
sampleRate = 48000.0;
3943

4044
handler.ctx = this;
@@ -83,21 +87,28 @@ class AudioSourceModule : public ModuleManager::Instance {
8387
void refresh() {
8488
devices.clear();
8589

90+
#if RTAUDIO_VERSION_MAJOR >= 6
91+
for (int i : audio.getDeviceIds()) {
92+
#else
8693
int count = audio.getDeviceCount();
8794
for (int i = 0; i < count; i++) {
95+
#endif
8896
try {
8997
// Get info
9098
auto info = audio.getDeviceInfo(i);
9199

100+
#if !defined(RTAUDIO_VERSION_MAJOR) || RTAUDIO_VERSION_MAJOR < 6
101+
if (!info.probed) { continue; }
102+
#endif
92103
// Check that it has a stereo input
93-
if (info.probed && info.inputChannels < 2) { continue; }
104+
if (info.inputChannels < 2) { continue; }
94105

95106
// Save info
96107
DeviceInfo dinfo = { info, i };
97108
devices.define(info.name, info.name, dinfo);
98109
}
99-
catch (std::exception e) {
100-
flog::error("Error getting audio device info: {0}", e.what());
110+
catch (const std::exception& e) {
111+
flog::error("Error getting audio device ({}) info: {}", i, e.what());
101112
}
102113
}
103114
}
@@ -254,6 +265,22 @@ class AudioSourceModule : public ModuleManager::Instance {
254265
return 0;
255266
}
256267

268+
#if RTAUDIO_VERSION_MAJOR >= 6
269+
static void errorCallback(RtAudioErrorType type, const std::string& errorText) {
270+
switch (type) {
271+
case RtAudioErrorType::RTAUDIO_NO_ERROR:
272+
return;
273+
case RtAudioErrorType::RTAUDIO_WARNING:
274+
case RtAudioErrorType::RTAUDIO_NO_DEVICES_FOUND:
275+
case RtAudioErrorType::RTAUDIO_DEVICE_DISCONNECT:
276+
flog::warn("AudioSourceModule Warning: {} ({})", errorText, (int)type);
277+
break;
278+
default:
279+
throw std::runtime_error(errorText);
280+
}
281+
}
282+
#endif
283+
257284
std::string name;
258285
bool enabled = true;
259286
dsp::stream<dsp::complex_t> stream;
@@ -290,4 +317,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
290317
MOD_EXPORT void _END_() {
291318
config.disableAutoSave();
292319
config.save();
293-
}
320+
}

0 commit comments

Comments
 (0)