Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

c - IAudioClient initialization issues in exclusive mode

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?

question from:https://stackoverflow.com/questions/65938671/iaudioclient-initialization-issues-in-exclusive-mode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...