Skip to content

Commit

Permalink
Merge pull request #1284 from hzeller/20240121-adapt-rtaudio
Browse files Browse the repository at this point in the history
Make compatible with rtaudio API 5 and 6.
  • Loading branch information
AlexandreRouma committed Jan 22, 2024
2 parents fa76b4e + 5a003e9 commit 86dcec7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
36 changes: 31 additions & 5 deletions sink_modules/audio_sink/src/main.cpp
Expand Up @@ -31,6 +31,10 @@ class AudioSink : SinkManager::Sink {
monoPacker.init(&s2m.out, 512);
stereoPacker.init(_stream->sinkOut, 512);

#if RTAUDIO_VERSION_MAJOR >= 6
audio.setErrorCallback(&errorCallback);
#endif

bool created = false;
std::string device = "";
config.acquire();
Expand All @@ -42,21 +46,27 @@ class AudioSink : SinkManager::Sink {
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 !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);
deviceIds.push_back(i);
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: {}", i, e.what());
}
}
selectByName(device);
Expand Down Expand Up @@ -156,6 +166,22 @@ class AudioSink : SinkManager::Sink {
}
}

#if RTAUDIO_VERSION_MAJOR >= 6
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("AudioSinkModule Warning: {} ({})", errorText, (int)type);
break;
default:
throw std::runtime_error(errorText);
}
}
#endif

private:
bool doStart() {
RtAudio::StreamParameters parameters;
Expand All @@ -172,8 +198,8 @@ class AudioSink : SinkManager::Sink {
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;
}

Expand Down
35 changes: 31 additions & 4 deletions source_modules/audio_source/src/main.cpp
Expand Up @@ -35,6 +35,10 @@ class AudioSourceModule : public ModuleManager::Instance {
AudioSourceModule(std::string name) {
this->name = name;

#if RTAUDIO_VERSION_MAJOR >= 6
audio.setErrorCallback(&errorCallback);
#endif

sampleRate = 48000.0;

handler.ctx = this;
Expand Down Expand Up @@ -83,21 +87,28 @@ class AudioSourceModule : public ModuleManager::Instance {
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);

#if !defined(RTAUDIO_VERSION_MAJOR) || RTAUDIO_VERSION_MAJOR < 6
if (!info.probed) { continue; }
#endif
// 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: {}", i, e.what());
}
}
}
Expand Down Expand Up @@ -254,6 +265,22 @@ class AudioSourceModule : public ModuleManager::Instance {
return 0;
}

#if RTAUDIO_VERSION_MAJOR >= 6
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("AudioSourceModule Warning: {} ({})", errorText, (int)type);
break;
default:
throw std::runtime_error(errorText);
}
}
#endif

std::string name;
bool enabled = true;
dsp::stream<dsp::complex_t> stream;
Expand Down Expand Up @@ -290,4 +317,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
MOD_EXPORT void _END_() {
config.disableAutoSave();
config.save();
}
}

0 comments on commit 86dcec7

Please sign in to comment.