* @brief Opens the audio I/O device with the current configuration. */
| 499 | * @brief Opens the audio I/O device with the current configuration. |
| 500 | */ |
| 501 | bool IO::Drivers::Audio::open(const QIODevice::OpenMode mode) |
| 502 | { |
| 503 | if (!m_init || m_isOpen || !configurationOk()) |
| 504 | return false; |
| 505 | |
| 506 | m_config = ma_device_config_init(ma_device_type_duplex); |
| 507 | |
| 508 | // clang-format off |
| 509 | m_config.pUserData = this; |
| 510 | m_config.dataCallback = &Audio::callback; |
| 511 | m_config.sampleRate = m_inputCapabilities[m_selectedInputDevice].supportedSampleRates[m_selectedSampleRate]; |
| 512 | // clang-format on |
| 513 | |
| 514 | m_config.noClip = MA_FALSE; |
| 515 | m_config.noDisableDenormals = MA_FALSE; |
| 516 | m_config.noFixedSizedCallback = MA_TRUE; |
| 517 | m_config.noPreSilencedOutputBuffer = MA_FALSE; |
| 518 | |
| 519 | applyPlatformAudioConfig(); |
| 520 | configureCaptureFormat(mode); |
| 521 | if (!configurePlaybackFormat(mode)) |
| 522 | return false; |
| 523 | |
| 524 | m_rtCaptureFormat.store(m_config.capture.format, std::memory_order_relaxed); |
| 525 | m_rtCaptureChannels.store(m_config.capture.channels, std::memory_order_relaxed); |
| 526 | m_rtPlaybackFormat.store(m_config.playback.format, std::memory_order_relaxed); |
| 527 | m_rtPlaybackChannels.store(m_config.playback.channels, std::memory_order_release); |
| 528 | |
| 529 | std::memset(&m_device, 0, sizeof(m_device)); |
| 530 | if (ma_device_init(&m_context, &m_config, &m_device) != MA_SUCCESS) { |
| 531 | qWarning() << "Failed to initialize miniaudio device."; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | if (ma_device_start(&m_device) != MA_SUCCESS) { |
| 536 | qWarning() << "Failed to start miniaudio device."; |
| 537 | ma_device_uninit(&m_device); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | startInputWorker(); |
| 542 | |
| 543 | m_isOpen = true; |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * @brief Applies platform-specific miniaudio backend tweaks to m_config. |