Skip to content

Commit

Permalink
Use IAudioClient3 shared mode in WASAPI.
Browse files Browse the repository at this point in the history
We're pull based, so using optimal values here is a good thing.
  • Loading branch information
Themaister committed Oct 29, 2023
1 parent 7d30fbb commit b178260
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions audio/audio_wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct WASAPIBackend final : Backend

IMMDeviceEnumerator *pEnumerator = nullptr;
IMMDevice *pDevice = nullptr;
IAudioClient *pAudioClient = nullptr;
IAudioClient3 *pAudioClient = nullptr;
IAudioRenderClient *pRenderClient = nullptr;
uint32_t buffer_latency_us = 0;
UINT32 buffer_frames = 0;
Expand Down Expand Up @@ -180,12 +180,21 @@ bool WASAPIBackend::init(float sample_rate, unsigned channels)
if (sample_rate > 0.0f)
format->nSamplesPerSec = DWORD(sample_rate);

const double target_latency = 0.030;
auto reference_time = seconds_to_reference_time(target_latency);
UINT default_period = 0, fundamental_period = 0, min_period = 0, max_period = 0;
if (FAILED(pAudioClient->GetSharedModeEnginePeriod(format, &default_period, &fundamental_period, &min_period, &max_period)))
{
LOGE("WASAPI: Failed to query shared mode engine period.\n");
return false;
}

// Sanity check, but you'd think default period should "just werk".
if (fundamental_period == 0 && default_period % fundamental_period != 0)
{
LOGE("WASAPI: Nonsensical default period.\n");
return false;
}

if (FAILED(pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
reference_time, 0, format, nullptr)))
if (FAILED(pAudioClient->InitializeSharedAudioStream(AUDCLNT_STREAMFLAGS_EVENTCALLBACK, default_period, format, nullptr)))
{
LOGE("WASAPI: Failed to initialize audio client.\n");
return false;
Expand All @@ -212,7 +221,7 @@ bool WASAPIBackend::init(float sample_rate, unsigned channels)

if (callback)
{
callback->set_latency_usec(buffer_latency_us);
callback->set_latency_usec((1000000 * buffer_frames) / format->nSamplesPerSec);
callback->set_backend_parameters(get_sample_rate(), get_num_channels(), MAX_NUM_FRAMES);
}
return true;
Expand Down

0 comments on commit b178260

Please sign in to comment.