1

I'm trying to access a specific USB audio device with WASAPI in exclusive mode in a UWP app. First, I'm creating a WAVEFORMATEXTENSIBLE and check if my device supports this format :

WAVEFORMATEXTENSIBLE wf;

wf.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
wf.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wf.Format.nChannels = 2;
wf.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;

// PCM 16
wf.Format.wBitsPerSample = 16;
wf.Samples.wValidBitsPerSample = wf.Format.wBitsPerSample;
wf.Format.nBlockAlign = wf.Format.nChannels * (wf.Format.wBitsPerSample / 8);
wf.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;

wf.Format.nSamplesPerSec = 48000;
wf.Format.nAvgBytesPerSec = wf.Format.nSamplesPerSec * wf.Format.nBlockAlign;
hr = IAudioClient_IsFormatSupported(
    pAudioClient,
    AUDCLNT_SHAREMODE_EXCLUSIVE,
    (WAVEFORMATEX*)&wf,
    NULL);

IsFormatSupported returns S_OK. Then, I'm initializing the AudioClient like this :

hr = IAudioClient_Initialize(
    pAudioClient,
    AUDCLNT_SHAREMODE_EXCLUSIVE,
    AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
    bufferDuration,
    bufferDuration,
    (WAVEFORMATEX*) &wf,
    NULL);

The problem is here: Initialize returns E_INVALIDARG. According to Microsoft documentation, this can be caused by a few issues, but in my case I suspect it's the "Parameter pFormat points to an invalid format description" because I don't use any of the mentioned flags and I don't call SetClientProperty.

Am I correctly initializing the WAVEFORMAT?

Drake Wu
  • 6,927
  • 1
  • 7
  • 30
loics2
  • 616
  • 1
  • 10
  • 24
  • Have you tried to initialize wf to 0? If you don't change all members in the struct, there might be some garbage. Either use memset or 'WAVEFORMATEXTENSIBLE wv = {0};' – Devolus Jan 28 '21 at 14:05
  • I just tried `WAVEFORMATEXTENSIBLE wv = {0};`, it doesn't change anything – loics2 Jan 28 '21 at 14:16
  • Have you tried this? https://learn.microsoft.com/en-us/windows/win32/coreaudio/exclusive-mode-streams. Maybe the buffer duration value is wrong? – Devolus Jan 28 '21 at 14:26
  • Yep, the buffer duration comes from `GetDevicePeriod`. I think there are specific buffer duration errors (AUDCLNT_E_INVALID_DEVICE_PERIOD and AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED) so it shouldn't be E_INVALIDARG – loics2 Jan 28 '21 at 14:44
  • WASAPI under some circumstances does not return alignment-specific errors. See also https://stackoverflow.com/questions/37754642/wasapi-play-sine-wave-sound-in-minimum-latency-without-glitches-exclusive-even/37757764#37757764. Also do you have a minimal repro we can compile and run? – Sjoerd van Kreel Jan 28 '21 at 17:32
  • Oh, that's interesting, thanks for the link. Not yet, I was trying to integrate that in an existing codebase. I'm currently writing a little program to test that in isolation, I'll put a link when it's done. – loics2 Jan 29 '21 at 08:01
  • Using and modifying [this classic sample](https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/Win7Samples/multimedia/audio/RenderExclusiveEventDriven/WASAPIRenderer.cpp#L42) as yours here cannot reproduce your problem. – Drake Wu Jan 29 '21 at 08:24

0 Answers0