* @brief Atomically restores Audio settings, resolving sub-settings by value. */
| 1524 | * @brief Atomically restores Audio settings, resolving sub-settings by value. |
| 1525 | */ |
| 1526 | void IO::Drivers::Audio::applyConnectionSettings(const QJsonObject& settings) |
| 1527 | { |
| 1528 | if (settings.isEmpty() || isOpen()) |
| 1529 | return; |
| 1530 | |
| 1531 | const auto deviceId = settings.value(QStringLiteral("deviceId")).toObject(); |
| 1532 | const auto savedDevName = deviceId.value(QStringLiteral("inputDeviceName")).toString(); |
| 1533 | const int savedDevIndex = settings.value(QStringLiteral("inputDevice")).toInt(-1); |
| 1534 | |
| 1535 | int deviceIndex = -1; |
| 1536 | if (!savedDevName.isEmpty()) { |
| 1537 | for (int i = 0; i < m_inputDevices.size(); ++i) { |
| 1538 | if (QString::fromUtf8(m_inputDevices[i].name) == savedDevName) { |
| 1539 | deviceIndex = i; |
| 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | if (deviceIndex < 0 && savedDevIndex >= 0 && savedDevIndex < m_inputDevices.size()) |
| 1546 | deviceIndex = savedDevIndex; |
| 1547 | |
| 1548 | if (deviceIndex < 0 || deviceIndex >= m_inputCapabilities.size()) |
| 1549 | return; |
| 1550 | |
| 1551 | m_selectedInputDevice = deviceIndex; |
| 1552 | const auto& caps = m_inputCapabilities[deviceIndex]; |
| 1553 | |
| 1554 | const int savedRateHz = deviceId.value(QStringLiteral("sampleRateValue")).toInt(0); |
| 1555 | const int savedRateIndex = settings.value(QStringLiteral("sampleRate")).toInt(-1); |
| 1556 | |
| 1557 | int rateIndex = -1; |
| 1558 | if (savedRateHz > 0) |
| 1559 | rateIndex = caps.supportedSampleRates.indexOf(savedRateHz); |
| 1560 | |
| 1561 | if (rateIndex < 0 && savedRateIndex >= 0 && savedRateIndex < caps.supportedSampleRates.size()) |
| 1562 | rateIndex = savedRateIndex; |
| 1563 | |
| 1564 | if (rateIndex < 0) { |
| 1565 | #ifdef Q_OS_WIN |
| 1566 | rateIndex = caps.supportedSampleRates.indexOf(22050); |
| 1567 | #else |
| 1568 | rateIndex = caps.supportedSampleRates.indexOf(44100); |
| 1569 | #endif |
| 1570 | } |
| 1571 | |
| 1572 | if (rateIndex < 0) |
| 1573 | rateIndex = 0; |
| 1574 | |
| 1575 | const auto savedFmtName = deviceId.value(QStringLiteral("formatName")).toString(); |
| 1576 | const int savedFmtIndex = settings.value(QStringLiteral("inputFormat")).toInt(-1); |
| 1577 | const auto fmtNames = inputSampleFormats(); |
| 1578 | |
| 1579 | int fmtIndex = -1; |
| 1580 | if (!savedFmtName.isEmpty()) |
| 1581 | fmtIndex = fmtNames.indexOf(savedFmtName); |
| 1582 | |
| 1583 | if (fmtIndex < 0 && savedFmtIndex >= 0 && savedFmtIndex < caps.supportedFormats.size()) |
no test coverage detected