dsound: Use smaller buffers for wavein capture.

This commit is contained in:
Maarten Lankhorst 2007-09-29 01:54:03 +02:00 committed by Alexandre Julliard
parent 290869b03d
commit 3bafbb22b8
3 changed files with 110 additions and 145 deletions

View File

@ -384,6 +384,31 @@ DirectSoundCaptureEnumerateW(
return DS_OK; return DS_OK;
} }
static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
{
unsigned i;
for (i = 0; i < This->nrofnotifies; ++i) {
LPDSBPOSITIONNOTIFY event = This->notifies + i;
DWORD offset = event->dwOffset;
TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
if (offset == DSBPN_OFFSETSTOP) {
if (!from && !len) {
SetEvent(event->hEventNotify);
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
return;
}
else return;
}
if (offset >= from && offset < (from + len))
{
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
SetEvent(event->hEventNotify);
}
}
}
static void CALLBACK static void CALLBACK
DSOUND_capture_callback( DSOUND_capture_callback(
HWAVEIN hwi, HWAVEIN hwi,
@ -393,27 +418,25 @@ DSOUND_capture_callback(
DWORD dw2 ) DWORD dw2 )
{ {
DirectSoundCaptureDevice * This = (DirectSoundCaptureDevice*)dwUser; DirectSoundCaptureDevice * This = (DirectSoundCaptureDevice*)dwUser;
IDirectSoundCaptureBufferImpl * Moi = (IDirectSoundCaptureBufferImpl*) This->capture_buffer;
TRACE("(%p,%08x(%s),%08x,%08x,%08x) entering at %d\n",hwi,msg, TRACE("(%p,%08x(%s),%08x,%08x,%08x) entering at %d\n",hwi,msg,
msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" : msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount()); msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
if (msg == MM_WIM_DATA) { if (msg == MM_WIM_DATA) {
LPWAVEHDR pHdr = (LPWAVEHDR)dw1;
EnterCriticalSection( &(This->lock) ); EnterCriticalSection( &(This->lock) );
TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n", TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
captureStateString[This->state],This->index); captureStateString[This->state],This->index);
if (This->state != STATE_STOPPED) { if (This->state != STATE_STOPPED) {
int index = This->index; int index = This->index;
if (This->state == STATE_STARTING) { if (This->state == STATE_STARTING)
This->read_position = pHdr->dwBytesRecorded;
This->state = STATE_CAPTURING; This->state = STATE_CAPTURING;
} capture_CheckNotify(Moi, (DWORD_PTR)This->pwave[index].lpData - (DWORD_PTR)This->buffer, This->pwave[index].dwBufferLength);
if (This->capture_buffer->nrofnotifies) This->index = (++This->index) % This->nrofpwaves;
SetEvent(This->capture_buffer->notifies[This->index].hEventNotify);
This->index = (This->index + 1) % This->nrofpwaves;
if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) { if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
TRACE("end of buffer\n"); TRACE("end of buffer\n");
This->state = STATE_STOPPED; This->state = STATE_STOPPED;
capture_CheckNotify(Moi, 0, 0);
} else { } else {
if (This->state == STATE_CAPTURING) { if (This->state == STATE_CAPTURING) {
waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR)); waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
@ -574,7 +597,6 @@ HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
WARN("already initialized\n"); WARN("already initialized\n");
return DSERR_ALREADYINITIALIZED; return DSERR_ALREADYINITIALIZED;
} }
return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID); return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
} }
@ -895,28 +917,18 @@ IDirectSoundCaptureBufferImpl_GetCurrentPosition(
if (hres != DS_OK) if (hres != DS_OK)
WARN("IDsCaptureDriverBuffer_GetPosition failed\n"); WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
} else if (This->device->hwi) { } else if (This->device->hwi) {
TRACE("old This->device->state=%s\n",captureStateString[This->device->state]); DWORD pos;
if (lpdwCapturePosition) {
MMTIME mtime; EnterCriticalSection(&This->device->lock);
mtime.wType = TIME_BYTES; pos = (DWORD_PTR)This->device->pwave[This->device->index].lpData - (DWORD_PTR)This->device->buffer;
waveInGetPosition(This->device->hwi, &mtime, sizeof(mtime)); TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
TRACE("mtime.u.cb=%d,This->device->buflen=%d\n", mtime.u.cb, if (lpdwCapturePosition)
This->device->buflen); *lpdwCapturePosition = pos;
mtime.u.cb = mtime.u.cb % This->device->buflen;
*lpdwCapturePosition = mtime.u.cb; if (lpdwReadPosition)
} *lpdwReadPosition = (This->device->pwave[This->device->index].dwBufferLength + pos) % This->device->buflen;
LeaveCriticalSection(&This->device->lock);
EnterCriticalSection(&(This->device->lock));
if (lpdwReadPosition) {
if (This->device->state == STATE_STARTING) {
if (lpdwCapturePosition)
This->device->read_position = *lpdwCapturePosition;
This->device->state = STATE_CAPTURING;
}
*lpdwReadPosition = This->device->read_position;
}
TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
LeaveCriticalSection(&(This->device->lock));
if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%d\n",*lpdwCapturePosition); if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%d\n",*lpdwCapturePosition);
if (lpdwReadPosition) TRACE("*lpdwReadPosition=%d\n",*lpdwReadPosition); if (lpdwReadPosition) TRACE("*lpdwReadPosition=%d\n",*lpdwReadPosition);
} else { } else {
@ -1122,99 +1134,47 @@ IDirectSoundCaptureBufferImpl_Start(
DirectSoundCaptureDevice *device = This->device; DirectSoundCaptureDevice *device = This->device;
if (device->buffer) { if (device->buffer) {
if (This->nrofnotifies) { int c;
int c; DWORD blocksize = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
device->nrofpwaves = device->buflen / blocksize + !!(device->buflen % blocksize);
TRACE("nrofpwaves=%d\n", device->nrofpwaves);
device->nrofpwaves = This->nrofnotifies; /* prepare headers */
TRACE("nrofnotifies=%d\n", This->nrofnotifies); if (device->pwave)
device->pwave = HeapReAlloc(GetProcessHeap(), 0,device->pwave, device->nrofpwaves*sizeof(WAVEHDR));
else
device->pwave = HeapAlloc(GetProcessHeap(), 0, device->nrofpwaves*sizeof(WAVEHDR));
/* prepare headers */ for (c = 0; c < device->nrofpwaves; ++c) {
if (device->pwave) device->pwave[c].lpData = (char *)device->buffer + c * blocksize;
device->pwave = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->pwave[c].dwBufferLength = blocksize;
device->nrofpwaves*sizeof(WAVEHDR)); device->pwave[c].dwBytesRecorded = 0;
else device->pwave[c].dwUser = (DWORD)device;
device->pwave = HeapAlloc(GetProcessHeap(),0, device->pwave[c].dwFlags = 0;
device->nrofpwaves*sizeof(WAVEHDR)); device->pwave[c].dwLoops = 0;
hres = mmErr(waveInPrepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR)));
for (c = 0; c < device->nrofpwaves; c++) { if (hres != DS_OK) {
if (This->notifies[c].dwOffset == DSBPN_OFFSETSTOP) { WARN("waveInPrepareHeader failed\n");
TRACE("got DSBPN_OFFSETSTOP\n"); while (c--)
device->nrofpwaves = c; waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
break; break;
}
if (c == 0) {
device->pwave[0].lpData = (LPSTR)device->buffer;
device->pwave[0].dwBufferLength =
This->notifies[0].dwOffset + 1;
} else {
device->pwave[c].lpData = (LPSTR)device->buffer +
This->notifies[c-1].dwOffset + 1;
device->pwave[c].dwBufferLength =
This->notifies[c].dwOffset -
This->notifies[c-1].dwOffset;
}
device->pwave[c].dwBytesRecorded = 0;
device->pwave[c].dwUser = (DWORD)device;
device->pwave[c].dwFlags = 0;
device->pwave[c].dwLoops = 0;
hres = mmErr(waveInPrepareHeader(device->hwi,
&(device->pwave[c]),sizeof(WAVEHDR)));
if (hres != DS_OK) {
WARN("waveInPrepareHeader failed\n");
while (c--)
waveInUnprepareHeader(device->hwi,
&(device->pwave[c]),sizeof(WAVEHDR));
break;
}
hres = mmErr(waveInAddBuffer(device->hwi,
&(device->pwave[c]), sizeof(WAVEHDR)));
if (hres != DS_OK) {
WARN("waveInAddBuffer failed\n");
while (c--)
waveInUnprepareHeader(device->hwi,
&(device->pwave[c]),sizeof(WAVEHDR));
break;
}
} }
FillMemory(device->buffer, device->buflen, hres = mmErr(waveInAddBuffer(device->hwi, &(device->pwave[c]), sizeof(WAVEHDR)));
(device->pwfx->wBitsPerSample == 8) ? 128 : 0);
} else {
TRACE("no notifiers specified\n");
/* no notifiers specified so just create a single default header */
device->nrofpwaves = 1;
if (device->pwave)
device->pwave = HeapReAlloc(GetProcessHeap(),0,device->pwave,sizeof(WAVEHDR));
else
device->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
device->pwave[0].lpData = (LPSTR)device->buffer;
device->pwave[0].dwBufferLength = device->buflen;
device->pwave[0].dwBytesRecorded = 0;
device->pwave[0].dwUser = (DWORD)device;
device->pwave[0].dwFlags = 0;
device->pwave[0].dwLoops = 0;
hres = mmErr(waveInPrepareHeader(device->hwi,
&(device->pwave[0]),sizeof(WAVEHDR)));
if (hres != DS_OK) { if (hres != DS_OK) {
WARN("waveInPrepareHeader failed\n"); WARN("waveInAddBuffer failed\n");
waveInUnprepareHeader(device->hwi, while (c--)
&(device->pwave[0]),sizeof(WAVEHDR)); waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
} break;
hres = mmErr(waveInAddBuffer(device->hwi, }
&(device->pwave[0]), sizeof(WAVEHDR))); }
if (hres != DS_OK) { if (device->buflen % blocksize)
WARN("waveInAddBuffer failed\n"); device->pwave[device->nrofpwaves - 1].dwBufferLength = device->buflen % blocksize;
waveInUnprepareHeader(device->hwi,
&(device->pwave[0]),sizeof(WAVEHDR)); FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
}
}
} }
device->index = 0; device->index = 0;
device->read_position = 0;
if (hres == DS_OK) { if (hres == DS_OK) {
/* start filling the first buffer */ /* start filling the first buffer */
@ -1300,10 +1260,7 @@ IDirectSoundCaptureBufferImpl_Unlock(
dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2); dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
if (hres != DS_OK) if (hres != DS_OK)
WARN("IDsCaptureDriverBuffer_Unlock failed\n"); WARN("IDsCaptureDriverBuffer_Unlock failed\n");
} else if (This->device->hwi) { } else if (!This->device->hwi) {
This->device->read_position = (This->device->read_position +
(dwAudioBytes1 + dwAudioBytes2)) % This->device->buflen;
} else {
WARN("invalid call\n"); WARN("invalid call\n");
hres = DSERR_INVALIDCALL; hres = DSERR_INVALIDCALL;
} }

View File

@ -253,7 +253,6 @@ struct DirectSoundCaptureDevice
/* more stuff */ /* more stuff */
LPBYTE buffer; LPBYTE buffer;
DWORD buflen; DWORD buflen;
DWORD read_position;
PWAVEFORMATEX pwfx; PWAVEFORMATEX pwfx;
@ -420,6 +419,7 @@ HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS);
/* primary.c */ /* primary.c */
DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign);
HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device); HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device); HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device); HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);

View File

@ -36,36 +36,44 @@
WINE_DEFAULT_DEBUG_CHANNEL(dsound); WINE_DEFAULT_DEBUG_CHANNEL(dsound);
/** Calculate how long a fragment length of about 10 ms should be in frames
*
* nSamplesPerSec: Frequency rate in samples per second
* nBlockAlign: Size of a single blockalign
*
* Returns:
* Size in bytes of a single fragment
*/
DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
{
DWORD fraglen = 512 * nBlockAlign;
/* Compensate for only being roughly accurate */
if (nSamplesPerSec <= 26000)
fraglen /= 2;
if (nSamplesPerSec <= 12000)
fraglen /= 2;
if (nSamplesPerSec >= 80000)
fraglen *= 2;
return fraglen;
}
static void DSOUND_RecalcPrimary(DirectSoundDevice *device) static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
{ {
DWORD nBlockAlign; TRACE("(%p)\n", device);
DWORD fraglen;
TRACE("(%p)\n", device);
nBlockAlign = device->pwfx->nBlockAlign; device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
/* Alsa doesn't have continuous buffers, instead it has buffers with power of 2, device->helfrags = device->buflen / device->fraglen;
* If DS_TIME_DEL is about 10 ms, 512 * nBlockAlign is roughly correct */ TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
fraglen = 512 * nBlockAlign;
/* Compensate for only being roughly accurate */ if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
if (device->pwfx->nSamplesPerSec <= 26000) device->writelead = 0;
fraglen /= 2; else
/* calculate the 10ms write lead */
if (device->pwfx->nSamplesPerSec <= 12000) device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
fraglen /= 2;
if (device->pwfx->nSamplesPerSec >= 80000)
fraglen *= 2;
device->fraglen = fraglen;
device->helfrags = device->buflen / fraglen;
TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
device->writelead = 0;
else
/* calculate the 10ms write lead */
device->writelead = (device->pwfx->nSamplesPerSec / 100) * nBlockAlign;
} }
HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)