// SPDX-FileCopyrightText: Copyright 2026 Gloria Goertz, Mireille Meyer // SPDX-License-Identifier: BSD-2-Clause #include #include "../../StringTools.hh" namespace atom::Audio::Implementation { // Manager Result> WASAPManager::CreateRenderer(const Format& format, const u32 desiredCapacity) { IMMDevice* device = nullptr; HRESULT result = m_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device); switch (result) { case S_OK: { break; } case E_NOTFOUND: { return Status::Unavailable; } case E_OUTOFMEMORY: { return Status::OutOfMemory; } default: { ATOM_PANIC("IMMDeviceEnumerator::GetDefaultAudioEndpoint failed with {}", result); } } return CreateRendererImpl(device, format, desiredCapacity); } Result> WASAPManager::CreateRenderer(const DeviceInfo& info, const Format& format, const u32 desiredCapacity) { IMMDevice* device = nullptr; HRESULT result = m_enumerator->GetDevice(reinterpret_cast(info.m_id.get()), &device); switch (result) { case S_OK: { break; } case E_NOTFOUND: { return Status::Unavailable; } case E_OUTOFMEMORY: { return Status::OutOfMemory; } default: { ATOM_PANIC("IMMDeviceEnumerator::GetDevice failed with {}", result); } } return CreateRendererImpl(nullptr, format, desiredCapacity); } Result GetFormatDescription(const Format& format) { GUID formatID = { }; WORD bytesPerSample = 0; DWORD channelMask = 0; switch (format.type) { case Format::Type::F32_PCM: { formatID = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; bytesPerSample = 4; break; } case Format::Type::S32_PCM: { formatID = KSDATAFORMAT_SUBTYPE_PCM; bytesPerSample = 4; break; } case Format::Type::S24_PCM: { formatID = KSDATAFORMAT_SUBTYPE_PCM; bytesPerSample = 3; break; } case Format::Type::S16_PCM: { formatID = KSDATAFORMAT_SUBTYPE_PCM; bytesPerSample = 2; break; } } switch (format.channels) { case 1: { channelMask = KSAUDIO_SPEAKER_MONO; break; } case 2: { channelMask = KSAUDIO_SPEAKER_STEREO; break; } case 4: { channelMask = KSAUDIO_SPEAKER_SURROUND; break; } case 5: { channelMask = KSAUDIO_SPEAKER_5POINT0; break; } case 6: { channelMask = KSAUDIO_SPEAKER_5POINT1; break; } case 7: { channelMask = KSAUDIO_SPEAKER_7POINT0; break; } case 8: { channelMask = KSAUDIO_SPEAKER_7POINT1; break; } default: { return Status::Unsupported; } } const WORD bits = bytesPerSample * 8; WAVEFORMATEXTENSIBLE output = { .Format = { .wFormatTag = WAVE_FORMAT_EXTENSIBLE, .nChannels = format.channels, .nSamplesPerSec = format.rate, .nAvgBytesPerSec = 0, // calculated below .nBlockAlign = static_cast(format.channels * bytesPerSample), .wBitsPerSample = bits, .cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) }, .Samples.wValidBitsPerSample = bits, .dwChannelMask = channelMask, .SubFormat = formatID, }; output.Format.nAvgBytesPerSec = output.Format.nSamplesPerSec * output.Format.nBlockAlign; return output; } Result> WASAPManager::CreateRendererImpl(IMMDevice* device, const Format& format, const u32 desiredCapacity) { auto descriptionResult = GetFormatDescription(format); if (!descriptionResult.Ok()) { return descriptionResult.Code(); } const auto description = descriptionResult.Unwrap(); IAudioClient3* client = nullptr; HRESULT result = device->Activate(__uuidof(IAudioClient3), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast(&client)); switch (result) { case S_OK: { break; } case E_NOTFOUND: case E_NOINTERFACE: { device->Release(); return Status::Unavailable; } case AUDCLNT_E_DEVICE_INVALIDATED: { device->Release(); return Status::Expired; } case E_OUTOFMEMORY: { device->Release(); return Status::OutOfMemory; } default: { ATOM_PANIC("IMMDevice::Activate failed with {}", result); } } const u32 refTime = (u32(format.rate) / format.channels) * 1e+7; // refTime is units of 100 ns result = client->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, refTime, 0, reinterpret_cast(&description), nullptr); switch (result) { case S_OK: { break; } case E_INVALIDARG: { client->Release(); device->Release(); return Status::InvalidParameters; } case E_NOTFOUND: case AUDCLNT_E_SERVICE_NOT_RUNNING: { client->Release(); device->Release(); return Status::Unavailable; } case AUDCLNT_E_UNSUPPORTED_FORMAT: { client->Release(); device->Release(); return Status::Unsupported; } case AUDCLNT_E_DEVICE_INVALIDATED: case AUDCLNT_E_ENDPOINT_CREATE_FAILED: { client->Release(); device->Release(); return Status::Expired; } case AUDCLNT_E_DEVICE_IN_USE: case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED: { client->Release(); device->Release(); return Status::Locked; } case E_OUTOFMEMORY: { client->Release(); device->Release(); return Status::OutOfMemory; } default: { ATOM_PANIC("IAudioClient3::Initialize failed with {}", result); } } IAudioSessionControl* sessionControl = nullptr; result = client->GetService(__uuidof(IAudioSessionControl), reinterpret_cast(&sessionControl)); switch (result) { case S_OK: { break; } case E_NOINTERFACE: case AUDCLNT_E_SERVICE_NOT_RUNNING: { client->Release(); device->Release(); return Status::Unavailable; } case AUDCLNT_E_DEVICE_INVALIDATED: { client->Release(); device->Release(); return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::GetService(IAudioSessionControl2) failed with {}", result); } } auto name = StdStringToWCHAR(m_name); result = sessionControl->SetDisplayName(name.get(), nullptr); switch (result) { case S_OK: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { sessionControl->Release(); client->Release(); device->Release(); return Status::Expired; } default: { ATOM_PANIC("IAudioSessionControl::SetDisplayName failed with {}", result); } } IAudioRenderClient* renderClient = nullptr; result = client->GetService(__uuidof(IAudioRenderClient), reinterpret_cast(&renderClient)); switch (result) { case S_OK: { break; } case E_NOINTERFACE: case AUDCLNT_E_SERVICE_NOT_RUNNING: { sessionControl->Release(); client->Release(); device->Release(); return Status::Unavailable; } case AUDCLNT_E_DEVICE_INVALIDATED: { sessionControl->Release(); client->Release(); device->Release(); return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::GetService(IAudioRenderClient) failed with {}", result); } } UINT32 maxFrameCount = 0; result = client->GetBufferSize(&maxFrameCount); switch (result) { case S_OK: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { sessionControl->Release(); renderClient->Release(); client->Release(); device->Release(); return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::GetBufferSize failed with {}", result); } } return std::unique_ptr(new WASAPIRenderer(device, client, sessionControl, renderClient, desiredCapacity, description.Format.nBlockAlign, maxFrameCount)); } WASAPIRenderer::~WASAPIRenderer() { m_renderClient->Release(); m_sessionControl->Release(); m_client->Release(); m_device->Release(); } Status WASAPIRenderer::Start() { const HRESULT result = m_client->Start(); switch (result) { case S_OK: case AUDCLNT_E_NOT_STOPPED: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::Start failed with {}", result); } } return Status::Success; } Status WASAPIRenderer::Stop() { const HRESULT result = m_client->Stop(); switch (result) { case S_OK: case AUDCLNT_E_NOT_STOPPED: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::Stop failed with {}", result); } } return Status::Success; } Status WASAPIRenderer::Submit(std::span frames) { const usize size = frames.size_bytes(); if (size % m_blockSize != 0) { return Status::InvalidParameters; } const usize blockSize = size / m_blockSize; u8* frameBuffer = nullptr; HRESULT result = m_renderClient->GetBuffer(blockSize, &frameBuffer); switch (result) { case S_OK: { break; } case AUDCLNT_E_BUFFER_SIZE_ERROR: case AUDCLNT_E_BUFFER_TOO_LARGE: { return Status::BufferTooLarge; } case AUDCLNT_E_BUFFER_OPERATION_PENDING: { return Status::Locked; } case AUDCLNT_E_DEVICE_INVALIDATED: { return Status::Expired; } default: { ATOM_PANIC("IAudioRenderClient::GetBuffer failed with {}", result); } } std::memcpy(frameBuffer, frames.data(), size); result = m_renderClient->ReleaseBuffer(blockSize, 0); switch (result) { case S_OK: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { return Status::Expired; } default: { ATOM_PANIC("IAudioRenderClient::ReleaseBuffer failed with {}", result); } } REFERENCE_TIME time = 0; result = m_client->GetStreamLatency(&time); ATOM_ASSERT(SUCCEEDED(result), "Farts"); return Status::Success; } Result WASAPIRenderer::GetQueuedFrameCount() { UINT32 offset = 0; const HRESULT result = m_client->GetCurrentPadding(&offset); switch (result) { case S_OK: { break; } case AUDCLNT_E_DEVICE_INVALIDATED: { return Status::Expired; } default: { ATOM_PANIC("IAudioClient3::GetCurrentPadding failed with {}", result); } } return offset; } }