Hello,
Welcome to Microsoft Q&A!
Sorry to keep you waiting. Since I didn't have a rendering device with me, I could only do capture tests.
When I did my tests, I found that the buffer could read incomplete data by CopyMemory, but for C++ memory safety, I recommend using WaitForSingleObject(AudioSamplesReadyEvent, INFINITE) in the thread to prevent memory problems.
//InitializeAudioEngine
hr = _AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST, 100* 10000, 0, _MixFormat, NULL);
if (FAILED(hr))
{
printf("Unable to initialize audio client: %x.\n", hr);
return false;
}
//
// Retrieve the buffer size for the audio client.
//
hr = _AudioClient->GetBufferSize(&_BufferSize);
//printf(" get audio client buffer: %d \n", _BufferSize);
if (FAILED(hr))
{
printf("Unable to get audio client buffer: %x. \n", hr);
return false;
}
hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);
if (FAILED(hr))
{
printf("Unable to set ready event: %x.\n", hr);
return false;
}
hr = _AudioClient->GetService(IID_PPV_ARGS(&_CaptureClient));
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return false;
}
//start capture
hr = _AudioClient->Start();
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return false;
}
bool stillPlaying = true;
while (stillPlaying)
{
DWORD waitResult = WaitForSingleObject(_AudioSamplesReadyEvent, INFINITE);
BYTE* pData, * pBuffer;
INT nBufferLenght;
UINT32 framesAvailable;
DWORD flags;
pBuffer = new BYTE[MAX_AUDIO_FRAME_SIZE];
hr = _CaptureClient->GetBuffer(&pData, &framesAvailable, &flags, NULL, NULL);
if (SUCCEEDED(hr))
{
if (framesAvailable != 0)
{
if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
{
//
// Fill 0s from the capture buffer to the output buffer.
//
}
else
{
//
// Copy data from the audio engine buffer to the output buffer.
//
CopyMemory(pBuffer, pData, framesAvailable * _FrameSize);
printf("get capture frames: %d!\n", framesAvailable);
}
}
delete[] pBuffer;
hr = _CaptureClient->ReleaseBuffer(framesAvailable);
if (FAILED(hr))
{
printf("Unable to release capture buffer: %x!\n", hr);
}
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.