Skip to content

Commit

Permalink
Open using the audio device id
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Jul 12, 2024
1 parent f084196 commit 132d6de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 9 additions & 5 deletions host/audio-settings-widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down
30 changes: 26 additions & 4 deletions host/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QFile>
#include <QThread>
#include <QtGlobal>
#include <QtLogging>

#include "application.hh"
#include "engine.hh"
Expand Down Expand Up @@ -94,8 +95,29 @@ void Engine::start() {
_audio =
std::make_unique<RtAudio>(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<int> 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;

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 132d6de

Please sign in to comment.