///////////////////////////////////////////////////////
| 582 | |
| 583 | //////////////////////////////////////////////////////////// |
| 584 | bool AudioDevice::initialize() |
| 585 | { |
| 586 | // Create the context |
| 587 | m_context.emplace(); |
| 588 | |
| 589 | auto contextConfig = ma_context_config_init(); |
| 590 | contextConfig.pLog = &*m_log; |
| 591 | std::uint32_t deviceCount = 0; |
| 592 | const auto nullBackend = ma_backend_null; |
| 593 | std::vector<const ma_backend*> backendLists; |
| 594 | |
| 595 | if (!getCurrentDeviceSelection().useNull) |
| 596 | backendLists.push_back(nullptr); |
| 597 | |
| 598 | backendLists.push_back(&nullBackend); |
| 599 | |
| 600 | for (const auto* backendList : backendLists) |
| 601 | { |
| 602 | // We can set backendCount to 1 since it is ignored when backends is set to nullptr |
| 603 | if (const auto result = ma_context_init(backendList, 1, &contextConfig, &*m_context); result != MA_SUCCESS) |
| 604 | { |
| 605 | m_context.reset(); |
| 606 | err() << "Failed to initialize the audio playback context: " << ma_result_description(result) << std::endl; |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | // Count the playback devices |
| 611 | if (const auto result = ma_context_get_devices(&*m_context, nullptr, &deviceCount, nullptr, nullptr); |
| 612 | result != MA_SUCCESS) |
| 613 | { |
| 614 | err() << "Failed to get audio playback devices: " << ma_result_description(result) << std::endl; |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | // Check if there are audio playback devices available on the system |
| 619 | if (deviceCount > 0) |
| 620 | break; |
| 621 | |
| 622 | // Warn if no devices were found using the default backend list |
| 623 | if (backendList == nullptr) |
| 624 | err() << "No audio playback devices available on the system" << std::endl; |
| 625 | |
| 626 | // Clean up the context if we didn't find any devices |
| 627 | ma_context_uninit(&*m_context); |
| 628 | } |
| 629 | |
| 630 | // If the NULL audio backend also doesn't provide a device we give up |
| 631 | if (deviceCount == 0) |
| 632 | { |
| 633 | m_context.reset(); |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | if (!getCurrentDeviceSelection().useNull && (m_context->backend == ma_backend_null)) |
| 638 | err() << "Using NULL audio playback device, even though it wasn't requested" << std::endl; |
| 639 | |
| 640 | const auto deviceId = getSelectedDeviceId(); |
| 641 |