WASAPI IsFormatSupported succeeds but causes Initialize to fail

Tucker von Holten 21 Reputation points
2021-03-05T20:49:27.307+00:00

Is there a situation where IsFormatSupported will succeed but the input format will not actually be a valid format for an exclusive mode stream for audio?

I'm using WASAPI's Exclusive Mode stream to try to get raw audio data from the OS and would like to get it in IEEE float (for it's increased accuracy over s16 PCM). The IsFormatSupported passes without error using the format specified below, yet, when I call Initialize, I get back an error of 0x80070057. Has anyone experienced anything similar to this?

A snippet of my code is shown below (everything runs successfully except the last printf, which is never reached. The above error is thrown directly before that line is run):

    WAVEFORMATEXTENSIBLE pwfx_ex_back;
    pwfx = (WAVEFORMATEX *) &pwfx_ex_back;
    WAVEFORMATEXTENSIBLE *pwfx_ex = (WAVEFORMATEXTENSIBLE*) &pwfx_ex_back;
    pwfx->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
    pwfx->nChannels = 2;
    pwfx->nSamplesPerSec = 48000;
    pwfx->wBitsPerSample = 32;
    pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
    pwfx->nAvgBytesPerSec = pwfx->nChannels * pwfx->nSamplesPerSec * (pwfx->wBitsPerSample / 8);
    pwfx->cbSize = 22;
    pwfx_ex->Samples.wValidBitsPerSample = 32;
    pwfx_ex->dwChannelMask = 0x00000001 | 0x00000002;
    pwfx_ex->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;

    print_waveformat(pwfx);

    write_headers_for_wav_file(wavfile, pwfx);

    WAVEFORMATEX **ppClosestMatch = NULL;
    hr = pAudioClient->IsFormatSupported(
        AUDCLNT_SHAREMODE_EXCLUSIVE,
        pwfx,
        NULL
    );
    EXIT_ON_ERROR(hr)
    printf("Closest match pointer-to-a-pointer value: %llx\n", (uint64_t) ppClosestMatch);


    REFERENCE_TIME phnsDefaultDevicePeriod;
    REFERENCE_TIME phnsMinimumDevicePeriod;
    hr = pAudioClient->GetDevicePeriod(
        &phnsDefaultDevicePeriod,
        &phnsMinimumDevicePeriod);
    EXIT_ON_ERROR(hr)
    printf("Default Period: %lli, Minimum Period: %lli\n", phnsDefaultDevicePeriod, phnsMinimumDevicePeriod);

    hr = pAudioClient->Initialize(
        AUDCLNT_SHAREMODE_EXCLUSIVE,
        0,
        60000, // 10 is 1 us, 10000 is 1 ms, 10000000 is 1 second, 0 is defaultminimumperiod for exclusive mode streams
        30000,
        pwfx,
        NULL
    );
    EXIT_ON_ERROR(hr)

    printf("Audioclient Initialized successfully.\n");
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,516 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roman R 331 Reputation points
    2021-03-08T17:02:52.147+00:00

    pwfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE; // not WAVE_FORMAT_IEEE_FLOAT;

    0 comments No comments