Pregunta

I am having trouble with WASAPI. It do not output any sound and I have been checked the data that writing to the buffer.

Because of it does not output any sound, I haven't any idea to find out the problem.

It may have some problems in following code.

SoundStream::SoundStream() : writtenCursor(0), writeCursor(0), distroy(false)
{

    IMMDeviceEnumerator * pEnumerator = nullptr;
    HResult(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pEnumerator)));

    IMMDevice * pDevice = nullptr;
    HResult(pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice));
    SafeRelease(&pEnumerator);

    HResult(pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&pAudioClient));
    SafeRelease(&pDevice);

    WAVEFORMATEXTENSIBLE * pwfx = nullptr;

    hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC * 2;

    HResult(pAudioClient->GetMixFormat((WAVEFORMATEX**)&pwfx));

    HResult(pAudioClient->Initialize(
        AUDCLNT_SHAREMODE_SHARED,
        AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
        hnsRequestedDuration,
        0,
        (WAVEFORMATEX*)pwfx,
        NULL));

    pAudioClient->SetEventHandle(hEvent);

    channel = (size_t)pwfx->Format.nChannels;
    bits = (size_t)pwfx->Format.wBitsPerSample;
    validBits = (size_t)pwfx->Samples.wValidBitsPerSample;
    frequency = (size_t)pwfx->Format.nSamplesPerSec;
    buffer.reshape({ 0, channel, bits >> 3 });

    CoTaskMemFree(pwfx);

    HResult(pAudioClient->GetBufferSize(&bufferFrameCount));
    HResult(pAudioClient->Start());

    if (pAudioClient)
    {
        thread = std::thread([&]()
        {
            this->Sync();
        });
    }

}
¿Fue útil?

Solución

You could look at my WASAPI.cpp code at http://jdmcox.com (which works fine).

You should also check if the expected wave format is float:

//SubFormat 00000003-0000-0010-8000-00aa00389b71 defines KSDATAFORMAT_SUBTYPE_IEEE_FLOAT 
//SubFormat 00000001-0000-0010-8000-00aa00389b71 defines KSDATAFORMAT_SUBTYPE_PCM 
GUID G; 
WORD V;
WAVEFORMATEX *pwfx = NULL;
bool itsfloat;

pAudioClient->GetMixFormat(&pwfx);
// Do we received a WAVEFORMATEXTENSIBLE?
if(pwfx.cbSize >= 22) {
   G = ((WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat;
   V = ((WAVEFORMATEXTENSIBLE*)pwfx)->Samples.wValidBitsPerSample; 
   if (G.Data1 == 3) itsfloat = true; 
   else if (G.Data1 == 1) itsfloat = false;
}

You know you received a WAVEFORMATEXTENSIBLE and not a simple WAVEFORMATEX because the "pwfx.cbSize >= 22".

See more at:
IAudioClient::GetMixFormat
https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-getmixformat

WAVEFORMATEXTENSIBLE
https://learn.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible

Otros consejos

You could look at my WASAPI.cpp code at http://jdmcox.com AGAIN. Now it works in shared mode as well as exclusive mode. I should note that no conversion of wave format or wave is necessary in shared mode -- Windows takes care of both converting to and from their format used to mix waves.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top