Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for WDM KS WaveRT low-latency microphone capture quality #752

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/hostapi/wdmks/pa_win_wdmks.c
Original file line number Diff line number Diff line change
Expand Up @@ -6582,31 +6582,46 @@ static PaError PaPinRenderSubmitHandler_WaveCyclic(PaProcessThreadInfo* pInfo, u
static PaError PaPinCaptureEventHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, unsigned eventIndex)
{
unsigned long pos;
unsigned realInBuf;
unsigned frameCount;
unsigned frameCount = 0;
unsigned bytesToRead;
PaWinWdmIOInfo* pCapture = &pInfo->stream->capture;
const unsigned halfInputBuffer = pCapture->hostBufferSize >> 1;
PaWinWdmPin* pin = pCapture->pPin;
DATAPACKET* packet = 0;

/* Get hold of current ADC position */
pin->fnAudioPosition(pin, &pos);
/* Wrap it (robi: why not use hw latency compensation here ?? because pos then gets _way_ off from
where it should be, i.e. at beginning or half buffer position. Why? No idea.) */

pos %= pCapture->hostBufferSize;
/* Then realInBuf will point to "other" half of double buffer */
realInBuf = pos < halfInputBuffer ? 1U : 0U;

packet = pInfo->stream->capture.packets + realInBuf;
pos -= pos % pCapture->bytesPerFrame;

/* Call barrier (or dummy) */
pin->fnMemBarrier();

/* Put it in queue */
frameCount = PaUtil_WriteRingBuffer(&pInfo->stream->ringBuffer, packet->Header.Data, pCapture->framesPerBuffer);

pInfo->capturePackets[pInfo->captureHead & cPacketsArrayMask].packet = packet;
/* Read all the data available at ring host buffer and push to intermediate ring buffer "queue" */
bytesToRead = (pCapture->hostBufferSize + pos - pCapture->lastPosition) % pCapture->hostBufferSize;
if (bytesToRead > 0)
{
unsigned bytesToRead_Step1 = min(bytesToRead, pCapture->hostBufferSize - pCapture->lastPosition);
unsigned framesToRead_Step1 = bytesToRead_Step1 / pCapture->bytesPerFrame;
unsigned frameCount_Step1 = PaUtil_WriteRingBuffer(&pInfo->stream->ringBuffer,
pCapture->hostBuffer + pCapture->lastPosition,
framesToRead_Step1);
frameCount += frameCount_Step1;
pCapture->lastPosition = (pCapture->lastPosition + frameCount_Step1 * pCapture->bytesPerFrame) % pCapture->hostBufferSize;
if (bytesToRead_Step1 < bytesToRead && frameCount_Step1 == framesToRead_Step1)
{
/* If the first step above does not move bytesToRead_Step1, do not run the code below.
The code below assumes the first write is complete. If it is not complete
bytesToRead_Step2 is not computed correctly, and the code does not account
for hostBuffer overrun since lastPosition may not be zero.
*/
unsigned bytesToRead_Step2 = bytesToRead - bytesToRead_Step1;
unsigned frameCount_Step2 = PaUtil_WriteRingBuffer(&pInfo->stream->ringBuffer,
pCapture->hostBuffer + pCapture->lastPosition,
bytesToRead_Step2 / pCapture->bytesPerFrame);
pCapture->lastPosition = (pCapture->lastPosition + frameCount_Step2 * pCapture->bytesPerFrame) % pCapture->hostBufferSize;
frameCount += frameCount_Step2;
}
}

PA_HP_TRACE((pInfo->stream->hLog, "Capture event (WaveRT): idx=%u head=%u (pos = %4.1lf%%, frames=%u)", realInBuf, pInfo->captureHead, (pos * 100.0 / pCapture->hostBufferSize), frameCount));

Expand All @@ -6621,7 +6636,7 @@ static PaError PaPinCaptureEventHandler_WaveRTPolled(PaProcessThreadInfo* pInfo,
unsigned long pos;
unsigned bytesToRead;
PaWinWdmIOInfo* pCapture = &pInfo->stream->capture;
const unsigned halfInputBuffer = pCapture->hostBufferSize>>1;
const unsigned halfInputBuffer = pCapture->hostBufferSize >> 1;
PaWinWdmPin* pin = pInfo->stream->capture.pPin;

/* Get hold of current ADC position */
Expand Down