From 132d6deef6f0d1f846bc1e3b5ead52c1f17b855d Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Fri, 12 Jul 2024 09:54:30 +0200 Subject: [PATCH] Open using the audio device id --- host/audio-settings-widget.cc | 14 +++++++++----- host/engine.cc | 30 ++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/host/audio-settings-widget.cc b/host/audio-settings-widget.cc index 666973c..8baa333 100644 --- a/host/audio-settings-widget.cc +++ b/host/audio-settings-widget.cc @@ -113,7 +113,11 @@ void AudioSettingsWidget::updateSampleRateList() { bool didSelectSampleRate = false; auto deviceIds = _audio->getDeviceIds(); - auto info = _audio->getDeviceInfo(deviceIds[_deviceChooser->currentIndex()]); + const auto index = _deviceChooser->currentIndex(); + if (index != -1) + return; + + auto info = _audio->getDeviceInfo(deviceIds[index]); for (size_t i = 0; i < info.sampleRates.size(); ++i) { int sr = info.sampleRates[i]; _sampleRateChooser->addItem(QString::number(sr)); @@ -136,13 +140,13 @@ void AudioSettingsWidget::updateDeviceList() { auto deviceCount = _audio->getDeviceCount(); bool deviceFound = false; - auto deviceIds = _audio->getDeviceIds(); + const auto deviceIds = _audio->getDeviceIds(); // Populate the choices for (int i = 0; i < deviceCount; ++i) { - auto deviceId = deviceIds[i]; - auto deviceInfo = _audio->getDeviceInfo(deviceId); - QString name = QString::fromStdString(deviceInfo.name); + const auto deviceId = deviceIds[i]; + const auto deviceInfo = _audio->getDeviceInfo(deviceId); + const QString name = QString::fromStdString(deviceInfo.name); _deviceChooser->addItem(name); if (!deviceFound && _audioSettings.deviceReference()._name == name) { diff --git a/host/engine.cc b/host/engine.cc index 87d9612..ed3d56d 100644 --- a/host/engine.cc +++ b/host/engine.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include "application.hh" #include "engine.hh" @@ -94,8 +95,29 @@ void Engine::start() { _audio = std::make_unique(RtAudio::getCompiledApiByName(deviceRef._api.toStdString())); if (_audio) { + const auto deviceIds = _audio->getDeviceIds(); + if (deviceIds.empty()) { + qWarning() << "Can't activate audio engine: no audio devices"; + stop(); + return; + } + + std::optional deviceId; + for (auto id : deviceIds) { + const auto deviceInfo = _audio->getDeviceInfo(id); + if (deviceRef._name.toStdString() != deviceInfo.name) + continue; + deviceId = id; + break; + } + + if (!deviceId.has_value()) { + // At least we can try something... + deviceId = _audio->getDefaultOutputDevice(); + } + RtAudio::StreamParameters outParams; - outParams.deviceId = deviceRef._index; + outParams.deviceId = deviceId.value(); outParams.firstChannel = 0; outParams.nChannels = 2; @@ -172,7 +194,7 @@ int Engine::audioCallback(void *outputBuffer, for (int i = 0; i < 8; i++) { uint32_t data; - do data = thiz->keyboardNoteData[i]; + do data = thiz->keyboardNoteData[i]; #ifdef _WIN32 while (data != InterlockedCompareExchange((LONG volatile *) &thiz->keyboardNoteData[i], 0, data)); #else @@ -181,7 +203,7 @@ int Engine::audioCallback(void *outputBuffer, bool release = data & 0x8000; data &= ~0x8000; uint32_t note = 0; - + if (data == 'Z') note = 48; if (data == 'S') note = 49; if (data == 'X') note = 50; if (data == 'D') note = 51; if (data == 'C') note = 52; if (data == 'V') note = 53; if (data == 'G') note = 54; if (data == 'B') note = 55; if (data == 'H') note = 56; if (data == 'N') note = 57; if (data == 'J') note = 58; if (data == 'M') note = 59; @@ -192,7 +214,7 @@ int Engine::audioCallback(void *outputBuffer, if (data == 'U') note = 71; if (data == 'I') note = 72; if (data == '9') note = 73; if (data == 'O') note = 74; if (data == '0') note = 75; if (data == 'P') note = 76; if (data == '[') note = 77; if (data == '=') note = 78; if (data == ']') note = 79; - + if (!note) { } else if (release) { thiz->_pluginHost->processNoteOff(0, 0, note, 100);