2002-06-13 21:15:06 +02:00
|
|
|
/* DirectSound
|
|
|
|
*
|
|
|
|
* Copyright 1998 Marcus Meissner
|
|
|
|
* Copyright 1998 Rob Riggs
|
|
|
|
* Copyright 2000-2002 TransGaming Technologies, Inc.
|
2007-06-13 17:05:49 +02:00
|
|
|
* Copyright 2007 Peter Dons Tychsen
|
2007-07-29 23:47:01 +02:00
|
|
|
* Copyright 2007 Maarten Lankhorst
|
2011-01-20 16:41:04 +01:00
|
|
|
* Copyright 2011 Owen Rudge for CodeWeavers
|
2002-06-13 21:15:06 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-06-13 21:15:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2002-06-13 21:15:06 +02:00
|
|
|
#include <math.h> /* Insomnia - pow() function */
|
|
|
|
|
2005-01-24 14:31:27 +01:00
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
#define NONAMELESSUNION
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "mmsystem.h"
|
2011-01-20 16:41:04 +01:00
|
|
|
#include "wingdi.h"
|
|
|
|
#include "mmreg.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "dsound.h"
|
2011-01-20 16:41:04 +01:00
|
|
|
#include "ks.h"
|
|
|
|
#include "ksmedia.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "dsdriver.h"
|
|
|
|
#include "dsound_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dsound);
|
|
|
|
|
|
|
|
void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
{
|
|
|
|
double temp;
|
2003-06-28 00:22:15 +02:00
|
|
|
TRACE("(%p)\n",volpan);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2002-06-13 21:15:06 +02:00
|
|
|
/* the AmpFactors are expressed in 16.16 fixed point */
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
/* FIXME: dwPan{Left|Right}AmpFactor */
|
|
|
|
|
|
|
|
/* FIXME: use calculated vol and pan ampfactors */
|
|
|
|
temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("left = %x, right = %x\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2004-07-23 21:06:31 +02:00
|
|
|
void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
{
|
|
|
|
double left,right;
|
|
|
|
TRACE("(%p)\n",volpan);
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("left=%x, right=%x\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
|
2004-07-23 21:06:31 +02:00
|
|
|
if (volpan->dwTotalLeftAmpFactor==0)
|
|
|
|
left=-10000;
|
|
|
|
else
|
|
|
|
left=600 * log(((double)volpan->dwTotalLeftAmpFactor) / 0xffff) / log(2);
|
|
|
|
if (volpan->dwTotalRightAmpFactor==0)
|
|
|
|
right=-10000;
|
|
|
|
else
|
|
|
|
right=600 * log(((double)volpan->dwTotalRightAmpFactor) / 0xffff) / log(2);
|
|
|
|
if (left<right)
|
|
|
|
{
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=right;
|
2004-07-23 21:06:31 +02:00
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalRightAmpFactor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=left;
|
2004-07-23 21:06:31 +02:00
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalLeftAmpFactor;
|
|
|
|
}
|
|
|
|
if (volpan->lVolume < -10000)
|
|
|
|
volpan->lVolume=-10000;
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lPan=right-left;
|
2004-07-23 21:06:31 +02:00
|
|
|
if (volpan->lPan < -10000)
|
|
|
|
volpan->lPan=-10000;
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2004-07-23 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
2007-11-07 20:32:22 +01:00
|
|
|
/** Convert a primary buffer position to a pointer position for device->mix_buffer
|
|
|
|
* device: DirectSoundDevice for which to calculate
|
|
|
|
* pos: Primary buffer position to converts
|
|
|
|
* Returns: Offset for mix_buffer
|
|
|
|
*/
|
|
|
|
DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos)
|
|
|
|
{
|
|
|
|
DWORD ret = pos * 32 / device->pwfx->wBitsPerSample;
|
|
|
|
if (device->pwfx->wBitsPerSample == 32)
|
|
|
|
ret *= 2;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/* NOTE: Not all secpos have to always be mapped to a bufpos, other way around is always the case
|
|
|
|
* DWORD64 is used here because a single DWORD wouldn't be big enough to fit the freqAcc for big buffers
|
|
|
|
*/
|
|
|
|
/** This function converts a 'native' sample pointer to a resampled pointer that fits for primary
|
|
|
|
* secmixpos is used to decide which freqAcc is needed
|
|
|
|
* overshot tells what the 'actual' secpos is now (optional)
|
|
|
|
*/
|
|
|
|
DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot)
|
|
|
|
{
|
|
|
|
DWORD64 framelen = secpos / dsb->pwfx->nBlockAlign;
|
|
|
|
DWORD64 freqAdjust = dsb->freqAdjust;
|
|
|
|
DWORD64 acc, freqAcc;
|
|
|
|
|
|
|
|
if (secpos < secmixpos)
|
|
|
|
freqAcc = dsb->freqAccNext;
|
|
|
|
else freqAcc = dsb->freqAcc;
|
|
|
|
acc = (framelen << DSOUND_FREQSHIFT) + (freqAdjust - 1 - freqAcc);
|
|
|
|
acc /= freqAdjust;
|
|
|
|
if (overshot)
|
|
|
|
{
|
|
|
|
DWORD64 oshot = acc * freqAdjust + freqAcc;
|
|
|
|
assert(oshot >= framelen << DSOUND_FREQSHIFT);
|
|
|
|
oshot -= framelen << DSOUND_FREQSHIFT;
|
|
|
|
*overshot = (DWORD)oshot;
|
|
|
|
assert(*overshot < dsb->freqAdjust);
|
|
|
|
}
|
|
|
|
return (DWORD)acc * dsb->device->pwfx->nBlockAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert a resampled pointer that fits for primary to a 'native' sample pointer
|
2007-08-09 10:42:59 +02:00
|
|
|
* freqAccNext is used here rather than freqAcc: In case the app wants to fill up to
|
2007-07-29 23:47:01 +02:00
|
|
|
* the play position it won't overwrite it
|
|
|
|
*/
|
|
|
|
static DWORD DSOUND_bufpos_to_secpos(const IDirectSoundBufferImpl *dsb, DWORD bufpos)
|
|
|
|
{
|
|
|
|
DWORD oAdv = dsb->device->pwfx->nBlockAlign, iAdv = dsb->pwfx->nBlockAlign, pos;
|
|
|
|
DWORD64 framelen;
|
|
|
|
DWORD64 acc;
|
|
|
|
|
|
|
|
framelen = bufpos/oAdv;
|
|
|
|
acc = framelen * (DWORD64)dsb->freqAdjust + (DWORD64)dsb->freqAccNext;
|
|
|
|
acc = acc >> DSOUND_FREQSHIFT;
|
|
|
|
pos = (DWORD)acc * iAdv;
|
|
|
|
if (pos >= dsb->buflen)
|
|
|
|
/* Because of differences between freqAcc and freqAccNext, this might happen */
|
|
|
|
pos = dsb->buflen - iAdv;
|
|
|
|
TRACE("Converted %d/%d to %d/%d\n", bufpos, dsb->tmp_buffer_len, pos, dsb->buflen);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move freqAccNext to freqAcc, and find new values for buffer length and freqAccNext
|
|
|
|
*/
|
|
|
|
static void DSOUND_RecalcFreqAcc(IDirectSoundBufferImpl *dsb)
|
|
|
|
{
|
|
|
|
if (!dsb->freqneeded) return;
|
|
|
|
dsb->freqAcc = dsb->freqAccNext;
|
|
|
|
dsb->tmp_buffer_len = DSOUND_secpos_to_bufpos(dsb, dsb->buflen, 0, &dsb->freqAccNext);
|
|
|
|
TRACE("New freqadjust: %04x, new buflen: %d\n", dsb->freqAccNext, dsb->tmp_buffer_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recalculate the size for temporary buffer, and new writelead
|
|
|
|
* Should be called when one of the following things occur:
|
|
|
|
* - Primary buffer format is changed
|
|
|
|
* - This buffer format (frequency) is changed
|
|
|
|
*
|
|
|
|
* After this, DSOUND_MixToTemporary(dsb, 0, dsb->buflen) should
|
|
|
|
* be called to refill the temporary buffer with data.
|
|
|
|
*/
|
2002-06-13 21:15:06 +02:00
|
|
|
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
|
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
BOOL needremix = TRUE, needresample = (dsb->freq != dsb->device->pwfx->nSamplesPerSec);
|
|
|
|
DWORD bAlign = dsb->pwfx->nBlockAlign, pAlign = dsb->device->pwfx->nBlockAlign;
|
2011-01-20 16:41:04 +01:00
|
|
|
WAVEFORMATEXTENSIBLE *pwfxe;
|
|
|
|
BOOL ieee = FALSE;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
TRACE("(%p)\n",dsb);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2011-01-20 16:41:04 +01:00
|
|
|
pwfxe = (WAVEFORMATEXTENSIBLE *) dsb->pwfx;
|
|
|
|
|
|
|
|
if ((pwfxe->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) || ((pwfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
|
|
|
|
&& (IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))))
|
|
|
|
ieee = TRUE;
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* calculate the 10ms write lead */
|
2005-02-25 20:16:57 +01:00
|
|
|
dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
|
|
|
if ((dsb->pwfx->wBitsPerSample == dsb->device->pwfx->wBitsPerSample) &&
|
2011-01-20 16:41:04 +01:00
|
|
|
(dsb->pwfx->nChannels == dsb->device->pwfx->nChannels) && !needresample && !ieee)
|
2007-07-29 23:47:01 +02:00
|
|
|
needremix = FALSE;
|
|
|
|
HeapFree(GetProcessHeap(), 0, dsb->tmp_buffer);
|
|
|
|
dsb->tmp_buffer = NULL;
|
|
|
|
dsb->max_buffer_len = dsb->freqAcc = dsb->freqAccNext = 0;
|
|
|
|
dsb->freqneeded = needresample;
|
|
|
|
|
2011-01-20 16:41:04 +01:00
|
|
|
if (ieee)
|
|
|
|
dsb->convert = convertbpp[4][dsb->device->pwfx->wBitsPerSample/8 - 1];
|
|
|
|
else
|
|
|
|
dsb->convert = convertbpp[dsb->pwfx->wBitsPerSample/8 - 1][dsb->device->pwfx->wBitsPerSample/8 - 1];
|
2007-11-07 20:31:53 +01:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
dsb->resampleinmixer = FALSE;
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
if (needremix)
|
|
|
|
{
|
|
|
|
if (needresample)
|
|
|
|
DSOUND_RecalcFreqAcc(dsb);
|
|
|
|
else
|
|
|
|
dsb->tmp_buffer_len = dsb->buflen / bAlign * pAlign;
|
|
|
|
dsb->max_buffer_len = dsb->tmp_buffer_len;
|
2008-03-04 05:18:04 +01:00
|
|
|
if ((dsb->max_buffer_len <= dsb->device->buflen || dsb->max_buffer_len < ds_snd_shadow_maxsize * 1024 * 1024) && ds_snd_shadow_maxsize >= 0)
|
|
|
|
dsb->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, dsb->max_buffer_len);
|
|
|
|
if (dsb->tmp_buffer)
|
|
|
|
FillMemory(dsb->tmp_buffer, dsb->tmp_buffer_len, dsb->device->pwfx->wBitsPerSample == 8 ? 128 : 0);
|
|
|
|
else
|
|
|
|
dsb->resampleinmixer = TRUE;
|
2007-07-29 23:47:01 +02:00
|
|
|
}
|
|
|
|
else dsb->max_buffer_len = dsb->tmp_buffer_len = dsb->buflen;
|
|
|
|
dsb->buf_mixpos = DSOUND_secpos_to_bufpos(dsb, dsb->sec_mixpos, 0, NULL);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Check for application callback requests for when the play position
|
|
|
|
* reaches certain points.
|
|
|
|
*
|
|
|
|
* The offsets that will be triggered will be those between the recorded
|
|
|
|
* "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
|
|
|
|
* beyond that position.
|
|
|
|
*/
|
2007-07-29 21:39:46 +02:00
|
|
|
void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
DWORD offset;
|
|
|
|
LPDSBPOSITIONNOTIFY event;
|
2003-09-02 01:58:43 +02:00
|
|
|
TRACE("(%p,%d)\n",dsb,len);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
if (dsb->nrofnotifies == 0)
|
2002-06-13 21:15:06 +02:00
|
|
|
return;
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
|
2007-07-06 22:57:48 +02:00
|
|
|
dsb, dsb->buflen, playpos, len);
|
2003-09-02 01:58:43 +02:00
|
|
|
for (i = 0; i < dsb->nrofnotifies ; i++) {
|
|
|
|
event = dsb->notifies + i;
|
2002-06-13 21:15:06 +02:00
|
|
|
offset = event->dwOffset;
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("checking %d, position %d, event = %p\n",
|
2002-06-13 21:15:06 +02:00
|
|
|
i, offset, event->hEventNotify);
|
|
|
|
/* DSBPN_OFFSETSTOP has to be the last element. So this is */
|
|
|
|
/* OK. [Inside DirectX, p274] */
|
2008-07-24 13:12:39 +02:00
|
|
|
/* Windows does not seem to enforce this, and some apps rely */
|
|
|
|
/* on that, so we can't stop there. */
|
2002-06-13 21:15:06 +02:00
|
|
|
/* */
|
|
|
|
/* This also means we can't sort the entries by offset, */
|
|
|
|
/* because DSBPN_OFFSETSTOP == -1 */
|
|
|
|
if (offset == DSBPN_OFFSETSTOP) {
|
|
|
|
if (dsb->state == STATE_STOPPED) {
|
|
|
|
SetEvent(event->hEventNotify);
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2008-07-24 13:12:39 +02:00
|
|
|
}
|
|
|
|
continue;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-06 22:57:48 +02:00
|
|
|
if ((playpos + len) >= dsb->buflen) {
|
|
|
|
if ((offset < ((playpos + len) % dsb->buflen)) ||
|
|
|
|
(offset >= playpos)) {
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 21:15:06 +02:00
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-06 22:57:48 +02:00
|
|
|
if ((offset >= playpos) && (offset < (playpos + len))) {
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 21:15:06 +02:00
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Copy a single frame from the given input buffer to the given output buffer.
|
|
|
|
* Translate 8 <-> 16 bits and mono <-> stereo
|
|
|
|
*/
|
2009-01-15 09:58:45 +01:00
|
|
|
static inline void cp_fields(const IDirectSoundBufferImpl *dsb, const BYTE *ibuf, BYTE *obuf,
|
|
|
|
UINT istride, UINT ostride, UINT count, UINT freqAcc, UINT adj)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-11-07 20:31:53 +01:00
|
|
|
DirectSoundDevice *device = dsb->device;
|
|
|
|
INT istep = dsb->pwfx->wBitsPerSample / 8, ostep = device->pwfx->wBitsPerSample / 8;
|
|
|
|
|
2010-06-08 00:28:56 +02:00
|
|
|
if (device->pwfx->nChannels == dsb->pwfx->nChannels ||
|
2011-03-01 21:57:35 +01:00
|
|
|
(device->pwfx->nChannels == 2 && dsb->pwfx->nChannels == 6) ||
|
|
|
|
(device->pwfx->nChannels == 6 && dsb->pwfx->nChannels == 2)) {
|
2009-01-15 09:58:45 +01:00
|
|
|
dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
|
2011-03-01 21:57:35 +01:00
|
|
|
if (device->pwfx->nChannels == 2 || dsb->pwfx->nChannels == 2)
|
2009-01-15 09:58:45 +01:00
|
|
|
dsb->convert(ibuf + istep, obuf + ostep, istride, ostride, count, freqAcc, adj);
|
2011-03-01 21:57:35 +01:00
|
|
|
return;
|
2007-11-07 20:31:53 +01:00
|
|
|
}
|
2003-01-16 01:20:07 +01:00
|
|
|
|
2007-11-07 20:31:53 +01:00
|
|
|
if (device->pwfx->nChannels == 1 && dsb->pwfx->nChannels == 2)
|
|
|
|
{
|
2009-01-15 09:58:45 +01:00
|
|
|
dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
|
2011-03-01 21:57:35 +01:00
|
|
|
return;
|
2007-11-07 20:31:53 +01:00
|
|
|
}
|
2003-01-16 01:20:07 +01:00
|
|
|
|
2007-11-07 20:31:53 +01:00
|
|
|
if (device->pwfx->nChannels == 2 && dsb->pwfx->nChannels == 1)
|
|
|
|
{
|
2009-01-15 09:58:45 +01:00
|
|
|
dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
|
|
|
|
dsb->convert(ibuf, obuf + ostep, istride, ostride, count, freqAcc, adj);
|
2011-03-01 21:57:35 +01:00
|
|
|
return;
|
2007-11-07 20:31:53 +01:00
|
|
|
}
|
2011-03-01 21:57:35 +01:00
|
|
|
|
|
|
|
WARN("Unable to remap channels: device=%u, buffer=%u\n", device->pwfx->nChannels,
|
|
|
|
dsb->pwfx->nChannels);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
2007-07-29 23:47:01 +02:00
|
|
|
* Calculate the distance between two buffer offsets, taking wraparound
|
|
|
|
* into account.
|
|
|
|
*/
|
|
|
|
static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
|
|
|
|
{
|
|
|
|
/* If these asserts fail, the problem is not here, but in the underlying code */
|
|
|
|
assert(ptr1 < buflen);
|
|
|
|
assert(ptr2 < buflen);
|
|
|
|
if (ptr1 >= ptr2) {
|
|
|
|
return ptr1 - ptr2;
|
|
|
|
} else {
|
|
|
|
return buflen + ptr1 - ptr2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Mix at most the given amount of data into the allocated temporary buffer
|
|
|
|
* of the given secondary buffer, starting from the dsb's first currently
|
|
|
|
* unsampled frame (writepos), translating frequency (pitch), stereo/mono
|
|
|
|
* and bits-per-sample so that it is ideal for the primary buffer.
|
|
|
|
* Doesn't perform any mixing - this is a straight copy/convert operation.
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
|
|
|
* dsb = the secondary buffer
|
2007-07-29 23:47:01 +02:00
|
|
|
* writepos = Starting position of changed buffer
|
|
|
|
* len = number of bytes to resample from writepos
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
2008-03-04 05:18:04 +01:00
|
|
|
* NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
|
2007-02-21 20:16:22 +01:00
|
|
|
*/
|
2008-03-04 05:18:04 +01:00
|
|
|
void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD len, BOOL inmixer)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2008-09-30 21:41:08 +02:00
|
|
|
INT size;
|
2008-04-16 23:09:30 +02:00
|
|
|
BYTE *ibp, *obp, *obp_begin;
|
2004-08-18 02:30:37 +02:00
|
|
|
INT iAdvance = dsb->pwfx->nBlockAlign;
|
2006-01-06 12:35:20 +01:00
|
|
|
INT oAdvance = dsb->device->pwfx->nBlockAlign;
|
2008-03-04 05:18:04 +01:00
|
|
|
DWORD freqAcc, target_writepos = 0, overshot, maxlen;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
/* We resample only when needed */
|
|
|
|
if ((dsb->tmp_buffer && inmixer) || (!dsb->tmp_buffer && !inmixer) || dsb->resampleinmixer != inmixer)
|
2007-07-29 23:47:01 +02:00
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
assert(writepos + len <= dsb->buflen);
|
|
|
|
if (inmixer && writepos + len < dsb->buflen)
|
|
|
|
len += dsb->pwfx->nBlockAlign;
|
|
|
|
|
|
|
|
maxlen = DSOUND_secpos_to_bufpos(dsb, len, 0, NULL);
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
ibp = dsb->buffer->memory + writepos;
|
2008-03-04 05:18:04 +01:00
|
|
|
if (!inmixer)
|
|
|
|
obp_begin = dsb->tmp_buffer;
|
|
|
|
else if (dsb->device->tmp_buffer_len < maxlen || !dsb->device->tmp_buffer)
|
|
|
|
{
|
|
|
|
dsb->device->tmp_buffer_len = maxlen;
|
|
|
|
if (dsb->device->tmp_buffer)
|
|
|
|
dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, maxlen);
|
|
|
|
else
|
|
|
|
dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, maxlen);
|
|
|
|
obp_begin = dsb->device->tmp_buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
obp_begin = dsb->device->tmp_buffer;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("(%p, %p)\n", dsb, ibp);
|
2009-01-15 09:58:45 +01:00
|
|
|
size = len / iAdvance;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
/* Check for same sample rate */
|
2006-01-06 12:35:20 +01:00
|
|
|
if (dsb->freq == dsb->device->pwfx->nSamplesPerSec) {
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("(%p) Same sample rate %d = primary %d\n", dsb,
|
2006-01-06 12:35:20 +01:00
|
|
|
dsb->freq, dsb->device->pwfx->nSamplesPerSec);
|
2008-03-04 05:18:04 +01:00
|
|
|
obp = obp_begin;
|
|
|
|
if (!inmixer)
|
|
|
|
obp += writepos/iAdvance*oAdvance;
|
|
|
|
|
2009-01-15 09:58:45 +01:00
|
|
|
cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, 0, 1 << DSOUND_FREQSHIFT);
|
2007-07-29 23:47:01 +02:00
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mix in different sample rates */
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("(%p) Adjusting frequency: %d -> %d\n", dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
target_writepos = DSOUND_secpos_to_bufpos(dsb, writepos, dsb->sec_mixpos, &freqAcc);
|
|
|
|
overshot = freqAcc >> DSOUND_FREQSHIFT;
|
|
|
|
if (overshot)
|
|
|
|
{
|
|
|
|
if (overshot >= size)
|
|
|
|
return;
|
|
|
|
size -= overshot;
|
|
|
|
writepos += overshot * iAdvance;
|
|
|
|
if (writepos >= dsb->buflen)
|
|
|
|
return;
|
|
|
|
ibp = dsb->buffer->memory + writepos;
|
|
|
|
freqAcc &= (1 << DSOUND_FREQSHIFT) - 1;
|
|
|
|
TRACE("Overshot: %d, freqAcc: %04x\n", overshot, freqAcc);
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
if (!inmixer)
|
|
|
|
obp = obp_begin + target_writepos;
|
|
|
|
else obp = obp_begin;
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/* FIXME: Small problem here when we're overwriting buf_mixpos, it then STILL uses old freqAcc, not sure if it matters or not */
|
2009-01-15 09:58:45 +01:00
|
|
|
cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, freqAcc, dsb->freqAdjust);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/** Apply volume to the given soundbuffer from (primary) position writepos and length len
|
|
|
|
* Returns: NULL if no volume needs to be applied
|
|
|
|
* or else a memory handle that holds 'len' volume adjusted buffer */
|
2008-03-04 05:18:04 +01:00
|
|
|
static LPBYTE DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT len)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2003-05-02 23:23:16 +02:00
|
|
|
INT i;
|
2007-07-29 23:47:01 +02:00
|
|
|
BYTE *bpc;
|
|
|
|
INT16 *bps, *mems;
|
|
|
|
DWORD vLeft, vRight;
|
|
|
|
INT nChannels = dsb->device->pwfx->nChannels;
|
2008-03-04 05:18:04 +01:00
|
|
|
LPBYTE mem = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos;
|
|
|
|
|
|
|
|
if (dsb->resampleinmixer)
|
|
|
|
mem = dsb->device->tmp_buffer;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("(%p,%d)\n",dsb,len);
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
|
|
|
|
dsb->volpan.dwTotalRightAmpFactor);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2007-10-13 20:29:40 +02:00
|
|
|
if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
|
|
|
|
(!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
|
|
|
|
!(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
|
|
|
return NULL; /* Nothing to do */
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
if (nChannels != 1 && nChannels != 2)
|
|
|
|
{
|
|
|
|
FIXME("There is no support for %d channels\n", nChannels);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dsb->device->pwfx->wBitsPerSample != 8 && dsb->device->pwfx->wBitsPerSample != 16)
|
|
|
|
{
|
|
|
|
FIXME("There is no support for %d bpp\n", dsb->device->pwfx->wBitsPerSample);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dsb->device->tmp_buffer_len < len || !dsb->device->tmp_buffer)
|
|
|
|
{
|
2008-03-04 05:18:04 +01:00
|
|
|
/* If we just resampled in DSOUND_MixToTemporary, we shouldn't need to resize here */
|
|
|
|
assert(!dsb->resampleinmixer);
|
2007-07-29 23:47:01 +02:00
|
|
|
dsb->device->tmp_buffer_len = len;
|
|
|
|
if (dsb->device->tmp_buffer)
|
|
|
|
dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, len);
|
|
|
|
else
|
|
|
|
dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
}
|
2008-03-04 05:18:04 +01:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
bpc = dsb->device->tmp_buffer;
|
|
|
|
bps = (INT16 *)bpc;
|
|
|
|
mems = (INT16 *)mem;
|
|
|
|
vLeft = dsb->volpan.dwTotalLeftAmpFactor;
|
|
|
|
if (nChannels > 1)
|
|
|
|
vRight = dsb->volpan.dwTotalRightAmpFactor;
|
|
|
|
else
|
|
|
|
vRight = vLeft;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
switch (dsb->device->pwfx->wBitsPerSample) {
|
2003-05-02 23:23:16 +02:00
|
|
|
case 8:
|
|
|
|
/* 8-bit WAV is unsigned, but we need to operate */
|
|
|
|
/* on signed data for this to work properly */
|
2008-05-20 18:15:15 +02:00
|
|
|
for (i = 0; i < len-1; i+=2) {
|
2007-12-31 22:38:23 +01:00
|
|
|
*(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
|
|
|
|
*(bpc++) = (((*(mem++) - 128) * vRight) >> 16) + 128;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-29 23:47:01 +02:00
|
|
|
if (len % 2 == 1 && nChannels == 1)
|
2007-12-31 22:38:23 +01:00
|
|
|
*(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
|
2003-05-02 23:23:16 +02:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
/* 16-bit WAV is signed -- much better */
|
2008-05-20 18:15:15 +02:00
|
|
|
for (i = 0; i < len-3; i += 4) {
|
2007-07-29 23:47:01 +02:00
|
|
|
*(bps++) = (*(mems++) * vLeft) >> 16;
|
|
|
|
*(bps++) = (*(mems++) * vRight) >> 16;
|
2003-05-02 23:23:16 +02:00
|
|
|
}
|
2007-07-29 23:47:01 +02:00
|
|
|
if (len % 4 == 2 && nChannels == 1)
|
|
|
|
*(bps++) = ((INT)*(mems++) * vLeft) >> 16;
|
2003-05-02 23:23:16 +02:00
|
|
|
break;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-29 23:47:01 +02:00
|
|
|
return dsb->device->tmp_buffer;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Mix (at most) the given number of bytes into the given position of the
|
|
|
|
* device buffer, from the secondary buffer "dsb" (starting at the current
|
|
|
|
* mix position for that buffer).
|
|
|
|
*
|
|
|
|
* Returns the number of bytes actually mixed into the device buffer. This
|
|
|
|
* will match fraglen unless the end of the secondary buffer is reached
|
|
|
|
* (and it is not looping).
|
|
|
|
*
|
|
|
|
* dsb = the secondary buffer to mix from
|
|
|
|
* writepos = position (offset) in device buffer to write at
|
|
|
|
* fraglen = number of bytes to mix
|
|
|
|
*/
|
2002-06-13 21:15:06 +02:00
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
|
|
|
|
{
|
2007-11-07 20:32:22 +01:00
|
|
|
INT len = fraglen, ilen;
|
2007-07-29 23:47:01 +02:00
|
|
|
BYTE *ibuf = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos, *volbuf;
|
2007-11-07 20:32:22 +01:00
|
|
|
DWORD oldpos, mixbufpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("buf_mixpos=%d/%d sec_mixpos=%d/%d\n", dsb->buf_mixpos, dsb->tmp_buffer_len, dsb->sec_mixpos, dsb->buflen);
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
assert(dsb->buf_mixpos + len <= dsb->tmp_buffer_len);
|
2005-02-25 20:16:57 +01:00
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
if (len % dsb->device->pwfx->nBlockAlign) {
|
|
|
|
INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
|
2005-02-25 20:16:57 +01:00
|
|
|
ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
|
2007-06-13 17:05:49 +02:00
|
|
|
len -= len % nBlockAlign; /* data alignment */
|
2005-02-25 20:16:57 +01:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
/* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
|
|
|
|
DSOUND_MixToTemporary(dsb, dsb->sec_mixpos, DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos+len) - dsb->sec_mixpos, TRUE);
|
|
|
|
if (dsb->resampleinmixer)
|
|
|
|
ibuf = dsb->device->tmp_buffer;
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/* Apply volume if needed */
|
2008-03-04 05:18:04 +01:00
|
|
|
volbuf = DSOUND_MixerVol(dsb, len);
|
2007-07-29 23:47:01 +02:00
|
|
|
if (volbuf)
|
|
|
|
ibuf = volbuf;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-11-07 20:32:22 +01:00
|
|
|
mixbufpos = DSOUND_bufpos_to_mixpos(dsb->device, writepos);
|
2007-02-21 20:16:22 +01:00
|
|
|
/* Now mix the temporary buffer into the devices main buffer */
|
2007-11-07 20:32:22 +01:00
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
|
|
|
dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD todo = dsb->device->buflen - writepos;
|
|
|
|
dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, todo);
|
|
|
|
dsb->device->mixfunction(ibuf + todo, dsb->device->mix_buffer, len - todo);
|
2007-07-29 23:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
oldpos = dsb->sec_mixpos;
|
|
|
|
dsb->buf_mixpos += len;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
|
2007-11-07 20:32:22 +01:00
|
|
|
if (dsb->buf_mixpos > dsb->tmp_buffer_len)
|
|
|
|
ERR("Mixpos (%u) past buflen (%u), capping...\n", dsb->buf_mixpos, dsb->tmp_buffer_len);
|
2007-07-29 23:47:01 +02:00
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING) {
|
|
|
|
dsb->buf_mixpos -= dsb->tmp_buffer_len;
|
|
|
|
} else if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
|
|
|
|
dsb->buf_mixpos = dsb->sec_mixpos = 0;
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
}
|
|
|
|
DSOUND_RecalcFreqAcc(dsb);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
dsb->sec_mixpos = DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos);
|
|
|
|
ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
|
2007-07-12 13:21:43 +02:00
|
|
|
/* check for notification positions */
|
|
|
|
if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
|
|
|
|
dsb->state != STATE_STARTING) {
|
2007-07-29 23:47:01 +02:00
|
|
|
DSOUND_CheckEvent(dsb, oldpos, ilen);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-07-12 13:21:43 +02:00
|
|
|
/* increase mix position */
|
|
|
|
dsb->primary_mixpos += len;
|
2007-07-29 23:47:01 +02:00
|
|
|
if (dsb->primary_mixpos >= dsb->device->buflen)
|
|
|
|
dsb->primary_mixpos -= dsb->device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Mix some frames from the given secondary buffer "dsb" into the device
|
|
|
|
* primary buffer.
|
|
|
|
*
|
|
|
|
* dsb = the secondary buffer
|
2007-07-29 23:47:01 +02:00
|
|
|
* playpos = the current play position in the device buffer (primary buffer)
|
2007-02-21 20:16:22 +01:00
|
|
|
* writepos = the current safe-to-write position in the device buffer
|
|
|
|
* mixlen = the maximum number of bytes in the primary buffer to mix, from the
|
|
|
|
* current writepos.
|
|
|
|
*
|
|
|
|
* Returns: the number of bytes beyond the writepos that were mixed.
|
|
|
|
*/
|
2007-07-29 21:24:21 +02:00
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-10-23 15:30:30 +02:00
|
|
|
/* The buffer's primary_mixpos may be before or after the device
|
2007-02-21 20:16:22 +01:00
|
|
|
* buffer's mixpos, but both must be ahead of writepos. */
|
2007-07-29 23:47:01 +02:00
|
|
|
DWORD primary_done;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 21:24:21 +02:00
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("writepos=%d, buf_mixpos=%d, primary_mixpos=%d, mixlen=%d\n", writepos, dsb->buf_mixpos, dsb->primary_mixpos, mixlen);
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("looping=%d, leadin=%d, buflen=%d\n", dsb->playflags, dsb->leadin, dsb->tmp_buffer_len);
|
|
|
|
|
|
|
|
/* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
|
|
|
|
if (dsb->leadin && dsb->state == STATE_STARTING)
|
|
|
|
{
|
|
|
|
if (mixlen > 2 * dsb->device->fraglen)
|
|
|
|
{
|
|
|
|
dsb->primary_mixpos += mixlen - 2 * dsb->device->fraglen;
|
|
|
|
dsb->primary_mixpos %= dsb->device->buflen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dsb->leadin = FALSE;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* calculate how much pre-buffering has already been done for this buffer */
|
|
|
|
primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
|
|
|
|
|
|
|
|
/* sanity */
|
|
|
|
if(mixlen < primary_done)
|
|
|
|
{
|
|
|
|
/* Should *NEVER* happen */
|
2007-07-29 23:47:01 +02:00
|
|
|
ERR("Fatal error. Under/Overflow? primary_done=%d, mixpos=%d/%d (%d/%d), primary_mixpos=%d, writepos=%d, mixlen=%d\n", primary_done,dsb->buf_mixpos,dsb->tmp_buffer_len,dsb->sec_mixpos, dsb->buflen, dsb->primary_mixpos, writepos, mixlen);
|
2010-03-10 09:02:44 +01:00
|
|
|
dsb->primary_mixpos = writepos + mixlen;
|
|
|
|
dsb->primary_mixpos %= dsb->device->buflen;
|
|
|
|
return mixlen;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2008-04-02 22:10:47 +02:00
|
|
|
/* take into account already mixed data */
|
2007-07-12 13:21:43 +02:00
|
|
|
mixlen -= primary_done;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-12 13:21:43 +02:00
|
|
|
TRACE("primary_done=%d, mixlen (primary) = %i\n", primary_done, mixlen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
if (!mixlen)
|
2007-11-10 20:15:21 +01:00
|
|
|
return primary_done;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
|
|
|
/* First try to mix to the end of the buffer if possible
|
|
|
|
* Theoretically it would allow for better optimization
|
|
|
|
*/
|
|
|
|
if (mixlen + dsb->buf_mixpos >= dsb->tmp_buffer_len)
|
2007-07-12 13:21:43 +02:00
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
DWORD newmixed, mixfirst = dsb->tmp_buffer_len - dsb->buf_mixpos;
|
|
|
|
newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
|
|
|
|
mixlen -= newmixed;
|
|
|
|
|
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING)
|
|
|
|
while (newmixed && mixlen)
|
2007-07-12 13:21:43 +02:00
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
mixfirst = (dsb->tmp_buffer_len < mixlen ? dsb->tmp_buffer_len : mixlen);
|
|
|
|
newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
|
|
|
|
mixlen -= newmixed;
|
2007-07-12 13:21:43 +02:00
|
|
|
}
|
2007-07-06 22:57:48 +02:00
|
|
|
}
|
2007-07-29 23:47:01 +02:00
|
|
|
else DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixlen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* re-calculate the primary done */
|
|
|
|
primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
|
2003-03-17 22:23:12 +01:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("new primary_mixpos=%d, total mixed data=%d\n", dsb->primary_mixpos, primary_done);
|
2003-03-17 22:23:12 +01:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* Report back the total prebuffered amount for this buffer */
|
|
|
|
return primary_done;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* For a DirectSoundDevice, go through all the currently playing buffers and
|
|
|
|
* mix them in to the device buffer.
|
|
|
|
*
|
|
|
|
* writepos = the current safe-to-write position in the primary buffer
|
|
|
|
* mixlen = the maximum amount to mix into the primary buffer
|
|
|
|
* (beyond the current writepos)
|
|
|
|
* recover = true if the sound device may have been reset and the write
|
|
|
|
* position in the device buffer changed
|
2007-06-13 17:05:49 +02:00
|
|
|
* all_stopped = reports back if all buffers have stopped
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
|
|
|
* Returns: the length beyond the writepos that was mixed to.
|
|
|
|
*/
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2011-02-11 01:21:05 +01:00
|
|
|
static DWORD DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos, DWORD mixlen, BOOL recover, BOOL *all_stopped)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-06-13 17:05:49 +02:00
|
|
|
INT i, len;
|
|
|
|
DWORD minlen = 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
IDirectSoundBufferImpl *dsb;
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* unless we find a running buffer, all have stopped */
|
|
|
|
*all_stopped = TRUE;
|
|
|
|
|
2007-07-29 21:24:21 +02:00
|
|
|
TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
|
2005-05-31 11:31:37 +02:00
|
|
|
for (i = 0; i < device->nrofbuffers; i++) {
|
|
|
|
dsb = device->buffers[i];
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
if (dsb->buflen && dsb->state && !dsb->hwbuf) {
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
|
2011-02-11 01:21:05 +01:00
|
|
|
RtlAcquireResourceShared(&dsb->lock, TRUE);
|
2007-06-13 17:05:49 +02:00
|
|
|
/* if buffer is stopping it is stopped now */
|
2002-06-13 21:15:06 +02:00
|
|
|
if (dsb->state == STATE_STOPPING) {
|
|
|
|
dsb->state = STATE_STOPPED;
|
2007-07-06 22:57:48 +02:00
|
|
|
DSOUND_CheckEvent(dsb, 0, 0);
|
2007-07-29 23:47:01 +02:00
|
|
|
} else if (dsb->state != STATE_STOPPED) {
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* if recovering, reset the mix position */
|
2002-06-13 21:15:06 +02:00
|
|
|
if ((dsb->state == STATE_STARTING) || recover) {
|
|
|
|
dsb->primary_mixpos = writepos;
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* if the buffer was starting, it must be playing now */
|
2002-06-13 21:15:06 +02:00
|
|
|
if (dsb->state == STATE_STARTING)
|
|
|
|
dsb->state = STATE_PLAYING;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2008-03-14 11:54:35 +01:00
|
|
|
/* mix next buffer into the main buffer */
|
|
|
|
len = DSOUND_MixOne(dsb, writepos, mixlen);
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
if (!minlen) minlen = len;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/* record the minimum length mixed from all buffers */
|
2007-06-13 17:05:49 +02:00
|
|
|
/* we only want to return the length which *all* buffers have mixed */
|
2007-07-29 23:47:01 +02:00
|
|
|
else if (len) minlen = (len < minlen) ? len : minlen;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
*all_stopped = FALSE;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-29 21:01:43 +02:00
|
|
|
RtlReleaseResource(&dsb->lock);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("Mixed at least %d from all buffers\n", minlen);
|
|
|
|
return minlen;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/**
|
|
|
|
* Add buffers to the emulated wave device system.
|
|
|
|
*
|
|
|
|
* device = The current dsound playback device
|
|
|
|
* force = If TRUE, the function will buffer up as many frags as possible,
|
|
|
|
* even though and will ignore the actual state of the primary buffer.
|
|
|
|
*
|
|
|
|
* Returns: None
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void DSOUND_WaveQueue(DirectSoundDevice *device, BOOL force)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-06-13 17:05:49 +02:00
|
|
|
DWORD prebuf_frags, wave_writepos, wave_fragpos, i;
|
|
|
|
TRACE("(%p)\n", device);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* calculate the current wave frag position */
|
2007-07-30 16:44:50 +02:00
|
|
|
wave_fragpos = (device->pwplay + device->pwqueue) % device->helfrags;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2008-04-02 22:10:47 +02:00
|
|
|
/* calculate the current wave write position */
|
2007-06-13 17:05:49 +02:00
|
|
|
wave_writepos = wave_fragpos * device->fraglen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-06 15:34:33 +02:00
|
|
|
TRACE("wave_fragpos = %i, wave_writepos = %i, pwqueue = %i, prebuf = %i\n",
|
|
|
|
wave_fragpos, wave_writepos, device->pwqueue, device->prebuf);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-09-06 17:35:58 +02:00
|
|
|
if (!force)
|
|
|
|
{
|
2007-06-13 17:05:49 +02:00
|
|
|
/* check remaining prebuffered frags */
|
2007-09-06 17:35:58 +02:00
|
|
|
prebuf_frags = device->mixpos / device->fraglen;
|
|
|
|
if (prebuf_frags == device->helfrags)
|
|
|
|
--prebuf_frags;
|
|
|
|
TRACE("wave_fragpos = %d, mixpos_frags = %d\n", wave_fragpos, prebuf_frags);
|
|
|
|
if (prebuf_frags < wave_fragpos)
|
|
|
|
prebuf_frags += device->helfrags;
|
|
|
|
prebuf_frags -= wave_fragpos;
|
|
|
|
TRACE("wanted prebuf_frags = %d\n", prebuf_frags);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-09-06 17:35:58 +02:00
|
|
|
else
|
2007-06-13 17:05:49 +02:00
|
|
|
/* buffer the maximum amount of frags */
|
|
|
|
prebuf_frags = device->prebuf;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* limit to the queue we have left */
|
2007-09-06 17:35:58 +02:00
|
|
|
if ((prebuf_frags + device->pwqueue) > device->prebuf)
|
2007-06-13 17:05:49 +02:00
|
|
|
prebuf_frags = device->prebuf - device->pwqueue;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("prebuf_frags = %i\n", prebuf_frags);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* adjust queue */
|
|
|
|
device->pwqueue += prebuf_frags;
|
|
|
|
|
|
|
|
/* get out of CS when calling the wave system */
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
/* **** */
|
|
|
|
|
|
|
|
/* queue up the new buffers */
|
|
|
|
for(i=0; i<prebuf_frags; i++){
|
|
|
|
TRACE("queueing wave buffer %i\n", wave_fragpos);
|
2007-07-30 16:44:50 +02:00
|
|
|
waveOutWrite(device->hwo, &device->pwave[wave_fragpos], sizeof(WAVEHDR));
|
2007-06-13 17:05:49 +02:00
|
|
|
wave_fragpos++;
|
2007-07-30 16:44:50 +02:00
|
|
|
wave_fragpos %= device->helfrags;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
|
|
|
|
|
|
|
TRACE("queue now = %i\n", device->pwqueue);
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Perform mixing for a Direct Sound device. That is, go through all the
|
|
|
|
* secondary buffers (the sound bites currently playing) and mix them in
|
|
|
|
* to the primary buffer (the device buffer).
|
|
|
|
*/
|
2006-08-02 13:26:14 +02:00
|
|
|
static void DSOUND_PerformMix(DirectSoundDevice *device)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
TRACE("(%p)\n", device);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
2007-06-13 17:05:49 +02:00
|
|
|
BOOL recover = FALSE, all_stopped = FALSE;
|
2007-11-07 20:32:22 +01:00
|
|
|
DWORD playpos, writepos, writelead, maxq, frag, prebuff_max, prebuff_left, size1, size2, mixplaypos, mixplaypos2;
|
2007-06-13 17:05:49 +02:00
|
|
|
LPVOID buf1, buf2;
|
|
|
|
BOOL lock = (device->hwbuf && !(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK));
|
|
|
|
int nfiller;
|
|
|
|
|
|
|
|
/* the sound of silence */
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
|
|
|
|
|
|
|
/* get the position in the primary buffer */
|
|
|
|
if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
|
2005-05-31 11:31:37 +02:00
|
|
|
playpos,writepos,device->playpos,device->mixpos,device->buflen);
|
|
|
|
assert(device->playpos < device->buflen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2007-11-07 20:32:22 +01:00
|
|
|
mixplaypos = DSOUND_bufpos_to_mixpos(device, device->playpos);
|
|
|
|
mixplaypos2 = DSOUND_bufpos_to_mixpos(device, playpos);
|
2008-09-04 12:07:37 +02:00
|
|
|
|
|
|
|
/* calc maximum prebuff */
|
|
|
|
prebuff_max = (device->prebuf * device->fraglen);
|
|
|
|
if (!device->hwbuf && playpos + prebuff_max >= device->helfrags * device->fraglen)
|
|
|
|
prebuff_max += device->buflen - device->helfrags * device->fraglen;
|
|
|
|
|
|
|
|
/* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
|
|
|
|
prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
|
|
|
|
writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos);
|
|
|
|
|
|
|
|
/* check for underrun. underrun occurs when the write position passes the mix position
|
|
|
|
* also wipe out just-played sound data */
|
|
|
|
if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
|
|
|
|
if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
|
|
|
|
WARN("Probable buffer underrun\n");
|
|
|
|
else TRACE("Buffer starting or buffer underrun\n");
|
|
|
|
|
|
|
|
/* recover mixing for all buffers */
|
|
|
|
recover = TRUE;
|
|
|
|
|
|
|
|
/* reset mix position to write position */
|
|
|
|
device->mixpos = writepos;
|
|
|
|
|
|
|
|
ZeroMemory(device->mix_buffer, device->mix_buffer_len);
|
|
|
|
ZeroMemory(device->buffer, device->buflen);
|
|
|
|
} else if (playpos < device->playpos) {
|
2007-06-13 17:05:49 +02:00
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
buf2 = device->buffer;
|
|
|
|
size1 = device->buflen - device->playpos;
|
|
|
|
size2 = playpos;
|
2007-11-07 20:32:22 +01:00
|
|
|
FillMemory(device->mix_buffer + mixplaypos, device->mix_buffer_len - mixplaypos, 0);
|
|
|
|
FillMemory(device->mix_buffer, mixplaypos2, 0);
|
2007-06-13 17:05:49 +02:00
|
|
|
if (lock)
|
|
|
|
IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
|
|
|
|
FillMemory(buf1, size1, nfiller);
|
|
|
|
if (playpos && (!buf2 || !size2))
|
|
|
|
FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__, device->playpos, device->mixpos, playpos, writepos);
|
|
|
|
FillMemory(buf2, size2, nfiller);
|
|
|
|
if (lock)
|
|
|
|
IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
|
2002-06-13 21:15:06 +02:00
|
|
|
} else {
|
2007-06-13 17:05:49 +02:00
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
buf2 = NULL;
|
|
|
|
size1 = playpos - device->playpos;
|
|
|
|
size2 = 0;
|
2007-11-07 20:32:22 +01:00
|
|
|
FillMemory(device->mix_buffer + mixplaypos, mixplaypos2 - mixplaypos, 0);
|
2007-06-13 17:05:49 +02:00
|
|
|
if (lock)
|
|
|
|
IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
|
|
|
|
FillMemory(buf1, size1, nfiller);
|
|
|
|
if (buf2 && size2)
|
|
|
|
{
|
|
|
|
FIXME("%d: There should be no additional buffer here!!\n", __LINE__);
|
|
|
|
FillMemory(buf2, size2, nfiller);
|
|
|
|
}
|
|
|
|
if (lock)
|
|
|
|
IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
device->playpos = playpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* find the maximum we can prebuffer from current write position */
|
|
|
|
maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0;
|
|
|
|
|
|
|
|
TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
|
|
|
|
prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead);
|
|
|
|
|
|
|
|
if (lock)
|
2007-07-29 21:27:41 +02:00
|
|
|
IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, writepos, maxq, 0);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* do the mixing */
|
2011-02-11 01:21:05 +01:00
|
|
|
frag = DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2007-11-07 20:32:22 +01:00
|
|
|
if (frag + writepos > device->buflen)
|
|
|
|
{
|
|
|
|
DWORD todo = device->buflen - writepos;
|
|
|
|
device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, todo);
|
|
|
|
device->normfunction(device->mix_buffer, device->buffer, frag - todo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, frag);
|
|
|
|
|
2008-04-02 22:10:47 +02:00
|
|
|
/* update the mix position, taking wrap-around into account */
|
2007-06-13 17:05:49 +02:00
|
|
|
device->mixpos = writepos + frag;
|
2005-05-31 11:31:37 +02:00
|
|
|
device->mixpos %= device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
if (lock)
|
|
|
|
{
|
|
|
|
DWORD frag2 = (frag > size1 ? frag - size1 : 0);
|
|
|
|
frag -= frag2;
|
|
|
|
if (frag2 > size2)
|
|
|
|
{
|
|
|
|
FIXME("Buffering too much! (%d, %d, %d, %d)\n", maxq, frag, size2, frag2 - size2);
|
|
|
|
frag2 = size2;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
IDsDriverBuffer_Unlock(device->hwbuf, buf1, frag, buf2, frag2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update prebuff left */
|
|
|
|
prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
|
|
|
|
|
|
|
|
/* check if have a whole fragment */
|
|
|
|
if (prebuff_left >= device->fraglen){
|
|
|
|
|
|
|
|
/* update the wave queue if using wave system */
|
2007-09-06 17:35:58 +02:00
|
|
|
if (!device->hwbuf)
|
|
|
|
DSOUND_WaveQueue(device, FALSE);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* buffers are full. start playing if applicable */
|
|
|
|
if(device->state == STATE_STARTING){
|
|
|
|
TRACE("started primary buffer\n");
|
|
|
|
if(DSOUND_PrimaryPlay(device) != DS_OK){
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
2007-06-13 17:05:49 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* we are playing now */
|
|
|
|
device->state = STATE_PLAYING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* buffers are full. start stopping if applicable */
|
|
|
|
if(device->state == STATE_STOPPED){
|
2007-06-26 10:38:04 +02:00
|
|
|
TRACE("restarting primary buffer\n");
|
2007-06-13 17:05:49 +02:00
|
|
|
if(DSOUND_PrimaryPlay(device) != DS_OK){
|
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* start stopping again. as soon as there is no more data, it will stop */
|
|
|
|
device->state = STATE_STOPPING;
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* if device was stopping, its for sure stopped when all buffers have stopped */
|
|
|
|
else if((all_stopped == TRUE) && (device->state == STATE_STOPPING)){
|
|
|
|
TRACE("All buffers have stopped. Stopping primary buffer\n");
|
|
|
|
device->state = STATE_STOPPED;
|
|
|
|
|
|
|
|
/* stop the primary buffer now */
|
|
|
|
DSOUND_PrimaryStop(device);
|
|
|
|
}
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
} else {
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* update the wave queue if using wave system */
|
2007-09-06 17:35:58 +02:00
|
|
|
if (!device->hwbuf)
|
2007-06-13 17:05:49 +02:00
|
|
|
DSOUND_WaveQueue(device, TRUE);
|
2007-07-29 21:27:41 +02:00
|
|
|
else
|
|
|
|
/* Keep alsa happy, which needs GetPosition called once every 10 ms */
|
|
|
|
IDsDriverBuffer_GetPosition(device->hwbuf, NULL, NULL);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->state == STATE_STARTING) {
|
|
|
|
if (DSOUND_PrimaryPlay(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
device->state = STATE_PLAYING;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
else if (device->state == STATE_STOPPING) {
|
|
|
|
if (DSOUND_PrimaryStop(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryStop failed\n");
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
device->state = STATE_STOPPED;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
/* **** */
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2006-11-12 14:39:11 +01:00
|
|
|
void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
|
|
|
|
DWORD_PTR dw1, DWORD_PTR dw2)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2004-09-07 21:32:21 +02:00
|
|
|
DWORD start_time = GetTickCount();
|
2007-07-29 23:47:01 +02:00
|
|
|
DWORD end_time;
|
2003-06-28 00:22:15 +02:00
|
|
|
TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("entering at %d\n", start_time);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (DSOUND_renderer[device->drvdesc.dnDevNode] != device) {
|
2002-06-13 21:15:06 +02:00
|
|
|
ERR("dsound died without killing us?\n");
|
|
|
|
timeKillEvent(timerID);
|
|
|
|
timeEndPeriod(DS_TIME_RES);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->ref)
|
|
|
|
DSOUND_PerformMix(device);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
RtlReleaseResource(&(device->buffer_list_lock));
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2004-09-07 21:32:21 +02:00
|
|
|
end_time = GetTickCount();
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-10 07:52:09 +02:00
|
|
|
void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2008-08-10 07:52:09 +02:00
|
|
|
TRACE("(%p,%x,%lx,%lx,%lx)\n",hwo,msg,dwUser,dw1,dw2);
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("entering at %d, msg=%08x(%s)\n", GetTickCount(), msg,
|
2003-03-15 01:54:11 +01:00
|
|
|
msg==MM_WOM_DONE ? "MM_WOM_DONE" : msg==MM_WOM_CLOSE ? "MM_WOM_CLOSE" :
|
|
|
|
msg==MM_WOM_OPEN ? "MM_WOM_OPEN" : "UNKNOWN");
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* check if packet completed from wave driver */
|
2002-06-13 21:15:06 +02:00
|
|
|
if (msg == MM_WOM_DONE) {
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* **** */
|
2005-05-31 11:31:37 +02:00
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
TRACE("done playing primary pos=%d\n", device->pwplay * device->fraglen);
|
|
|
|
|
|
|
|
/* update playpos */
|
|
|
|
device->pwplay++;
|
2007-07-30 16:44:50 +02:00
|
|
|
device->pwplay %= device->helfrags;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* sanity */
|
|
|
|
if(device->pwqueue == 0){
|
|
|
|
ERR("Wave queue corrupted!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update queue */
|
2005-05-31 11:31:37 +02:00
|
|
|
device->pwqueue--;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2007-06-13 17:05:49 +02:00
|
|
|
/* **** */
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
TRACE("completed\n");
|
|
|
|
}
|