2002-06-13 21:15:06 +02:00
|
|
|
|
/* DirectSound
|
|
|
|
|
*
|
|
|
|
|
* Copyright 1998 Marcus Meissner
|
|
|
|
|
* Copyright 1998 Rob Riggs
|
|
|
|
|
* Copyright 2000-2002 TransGaming Technologies, Inc.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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"
|
2005-10-17 11:24:50 +02:00
|
|
|
|
#include "winuser.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#include "mmsystem.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
|
#include "winreg.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"
|
|
|
|
|
#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
|
|
|
|
|
2004-07-23 21:06:31 +02:00
|
|
|
|
TRACE("Vol=%ld Pan=%ld\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
|
|
|
|
|
|
|
|
|
TRACE("left = %lx, right = %lx\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-23 21:06:31 +02:00
|
|
|
|
void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
|
{
|
|
|
|
|
double left,right;
|
|
|
|
|
TRACE("(%p)\n",volpan);
|
|
|
|
|
|
|
|
|
|
TRACE("left=%lx, right=%lx\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
TRACE("Vol=%ld Pan=%ld\n", volpan->lVolume, volpan->lPan);
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
|
|
|
|
|
{
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p)\n",dsb);
|
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;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
|
|
|
|
|
dsb, dsb->buflen, dsb->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;
|
2002-10-19 01:48:57 +02:00
|
|
|
|
TRACE("checking %d, position %ld, 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] */
|
|
|
|
|
/* */
|
|
|
|
|
/* 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);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
return;
|
|
|
|
|
} else
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((dsb->playpos + len) >= dsb->buflen) {
|
|
|
|
|
if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
|
|
|
|
|
(offset >= dsb->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 {
|
|
|
|
|
if ((offset >= dsb->playpos) && (offset < (dsb->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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-16 01:20:07 +01:00
|
|
|
|
/* WAV format info can be found at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.cwi.nl/ftp/audio/AudioFormats.part2
|
|
|
|
|
* ftp://ftp.cwi.nl/pub/audio/RIFF-format
|
|
|
|
|
*
|
|
|
|
|
* Import points to remember:
|
|
|
|
|
* 8-bit WAV is unsigned
|
|
|
|
|
* 16-bit WAV is signed
|
|
|
|
|
*/
|
|
|
|
|
/* Use the same formulas as pcmconverter.c */
|
|
|
|
|
static inline INT16 cvtU8toS16(BYTE b)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
2003-01-16 01:20:07 +01:00
|
|
|
|
return (short)((b+(b << 8))-32768);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-16 01:20:07 +01:00
|
|
|
|
static inline BYTE cvtS16toU8(INT16 s)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
2003-01-16 01:20:07 +01:00
|
|
|
|
return (s >> 8) ^ (unsigned char)0x80;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void cp_fields(const IDirectSoundBufferImpl *dsb, BYTE *ibuf, BYTE *obuf )
|
|
|
|
|
{
|
2006-01-06 12:35:20 +01:00
|
|
|
|
DirectSoundDevice * device = dsb->device;
|
2003-01-16 01:20:07 +01:00
|
|
|
|
INT fl,fr;
|
|
|
|
|
|
2004-08-18 02:30:37 +02:00
|
|
|
|
if (dsb->pwfx->wBitsPerSample == 8) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 8 &&
|
|
|
|
|
device->pwfx->nChannels == dsb->pwfx->nChannels) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* avoid needless 8->16->8 conversion */
|
2003-01-16 01:20:07 +01:00
|
|
|
|
*obuf=*ibuf;
|
2004-08-18 02:30:37 +02:00
|
|
|
|
if (dsb->pwfx->nChannels==2)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
*(obuf+1)=*(ibuf+1);
|
2003-01-16 01:20:07 +01:00
|
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2003-01-16 01:20:07 +01:00
|
|
|
|
fl = cvtU8toS16(*ibuf);
|
2004-08-18 02:30:37 +02:00
|
|
|
|
fr = (dsb->pwfx->nChannels==2 ? cvtU8toS16(*(ibuf + 1)) : fl);
|
2003-01-16 01:20:07 +01:00
|
|
|
|
} else {
|
|
|
|
|
fl = *((INT16 *)ibuf);
|
2004-08-18 02:30:37 +02:00
|
|
|
|
fr = (dsb->pwfx->nChannels==2 ? *(((INT16 *)ibuf) + 1) : fl);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2003-01-16 01:20:07 +01:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->nChannels == 2) {
|
|
|
|
|
if (device->pwfx->wBitsPerSample == 8) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
*obuf = cvtS16toU8(fl);
|
|
|
|
|
*(obuf + 1) = cvtS16toU8(fr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 16) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
*((INT16 *)obuf) = fl;
|
|
|
|
|
*(((INT16 *)obuf) + 1) = fr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->nChannels == 1) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
fl = (fl + fr) >> 1;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 8) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
*obuf = cvtS16toU8(fl);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 16) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
*((INT16 *)obuf) = fl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now with PerfectPitch (tm) technology */
|
|
|
|
|
static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
|
|
|
|
|
{
|
|
|
|
|
INT i, size, ipos, ilen;
|
|
|
|
|
BYTE *ibp, *obp;
|
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;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2003-06-28 00:22:15 +02:00
|
|
|
|
ibp = dsb->buffer->memory + dsb->buf_mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
obp = buf;
|
|
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %p), buf_mixpos=%ld\n", dsb, ibp, obp, dsb->buf_mixpos);
|
|
|
|
|
/* Check for the best case */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((dsb->freq == dsb->device->pwfx->nSamplesPerSec) &&
|
|
|
|
|
(dsb->pwfx->wBitsPerSample == dsb->device->pwfx->wBitsPerSample) &&
|
|
|
|
|
(dsb->pwfx->nChannels == dsb->device->pwfx->nChannels)) {
|
2005-02-10 18:10:54 +01:00
|
|
|
|
INT bytesleft = dsb->buflen - dsb->buf_mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
TRACE("(%p) Best case\n", dsb);
|
|
|
|
|
if (len <= bytesleft )
|
2005-01-24 13:40:45 +01:00
|
|
|
|
CopyMemory(obp, ibp, len);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
else { /* wrap */
|
2005-01-24 13:40:45 +01:00
|
|
|
|
CopyMemory(obp, ibp, bytesleft);
|
|
|
|
|
CopyMemory(obp + bytesleft, dsb->buffer->memory, len - bytesleft);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for same sample rate */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if (dsb->freq == dsb->device->pwfx->nSamplesPerSec) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb->freq, dsb->device->pwfx->nSamplesPerSec);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
ilen = 0;
|
|
|
|
|
for (i = 0; i < len; i += oAdvance) {
|
|
|
|
|
cp_fields(dsb, ibp, obp );
|
|
|
|
|
ibp += iAdvance;
|
|
|
|
|
ilen += iAdvance;
|
|
|
|
|
obp += oAdvance;
|
2003-06-28 00:22:15 +02:00
|
|
|
|
if (ibp >= (BYTE *)(dsb->buffer->memory + dsb->buflen))
|
|
|
|
|
ibp = dsb->buffer->memory; /* wrap */
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
return (ilen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Mix in different sample rates */
|
|
|
|
|
/* */
|
|
|
|
|
/* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
|
|
|
|
|
/* Patent Pending :-] */
|
|
|
|
|
|
|
|
|
|
/* Patent enhancements (c) 2000 Ove K<>ven,
|
|
|
|
|
* TransGaming Technologies Inc. */
|
|
|
|
|
|
|
|
|
|
/* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec); */
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
size = len / oAdvance;
|
|
|
|
|
ilen = 0;
|
|
|
|
|
ipos = dsb->buf_mixpos;
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2003-06-28 00:22:15 +02:00
|
|
|
|
cp_fields(dsb, (dsb->buffer->memory + ipos), obp);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
obp += oAdvance;
|
|
|
|
|
dsb->freqAcc += dsb->freqAdjust;
|
|
|
|
|
if (dsb->freqAcc >= (1<<DSOUND_FREQSHIFT)) {
|
|
|
|
|
ULONG adv = (dsb->freqAcc>>DSOUND_FREQSHIFT) * iAdvance;
|
|
|
|
|
dsb->freqAcc &= (1<<DSOUND_FREQSHIFT)-1;
|
|
|
|
|
ipos += adv; ilen += adv;
|
2004-08-18 02:30:37 +02:00
|
|
|
|
ipos %= dsb->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ilen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
|
|
|
|
|
{
|
2003-05-02 23:23:16 +02:00
|
|
|
|
INT i;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
BYTE *bpc = buf;
|
|
|
|
|
INT16 *bps = (INT16 *) buf;
|
|
|
|
|
|
2003-05-02 23:23:16 +02:00
|
|
|
|
TRACE("(%p,%p,%d)\n",dsb,buf,len);
|
|
|
|
|
TRACE("left = %lx, right = %lx\n", dsb->cvolpan.dwTotalLeftAmpFactor,
|
|
|
|
|
dsb->cvolpan.dwTotalRightAmpFactor);
|
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->cvolpan.lPan == 0)) &&
|
|
|
|
|
(!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->cvolpan.lVolume == 0)) &&
|
|
|
|
|
!(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
|
|
|
|
return; /* Nothing to do */
|
|
|
|
|
|
|
|
|
|
/* If we end up with some bozo coder using panning or 3D sound */
|
|
|
|
|
/* with a mono primary buffer, it could sound very weird using */
|
|
|
|
|
/* this method. Oh well, tough patooties. */
|
|
|
|
|
|
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 */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
switch (dsb->device->pwfx->nChannels) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
case 1:
|
2003-05-02 23:23:16 +02:00
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
INT val = *bpc - 128;
|
|
|
|
|
val = (val * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
|
|
|
|
|
*bpc = val + 128;
|
|
|
|
|
bpc++;
|
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2003-05-02 23:23:16 +02:00
|
|
|
|
for (i = 0; i < len; i+=2) {
|
|
|
|
|
INT val = *bpc - 128;
|
|
|
|
|
val = (val * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
|
|
|
|
|
*bpc++ = val + 128;
|
|
|
|
|
val = *bpc - 128;
|
|
|
|
|
val = (val * dsb->cvolpan.dwTotalRightAmpFactor) >> 16;
|
|
|
|
|
*bpc = val + 128;
|
|
|
|
|
bpc++;
|
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 12:35:20 +01:00
|
|
|
|
FIXME("doesn't support %d channels\n", dsb->device->pwfx->nChannels);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
break;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2003-05-02 23:23:16 +02:00
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
/* 16-bit WAV is signed -- much better */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
switch (dsb->device->pwfx->nChannels) {
|
2003-05-02 23:23:16 +02:00
|
|
|
|
case 1:
|
|
|
|
|
for (i = 0; i < len; i += 2) {
|
|
|
|
|
*bps = (*bps * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
|
|
|
|
|
bps++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
for (i = 0; i < len; i += 4) {
|
|
|
|
|
*bps = (*bps * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
|
|
|
|
|
bps++;
|
|
|
|
|
*bps = (*bps * dsb->cvolpan.dwTotalRightAmpFactor) >> 16;
|
|
|
|
|
bps++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 12:35:20 +01:00
|
|
|
|
FIXME("doesn't support %d channels\n", dsb->device->pwfx->nChannels);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 12:35:20 +01:00
|
|
|
|
FIXME("doesn't support %d bit samples\n", dsb->device->pwfx->wBitsPerSample);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
break;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
static LPBYTE DSOUND_tmpbuffer(DirectSoundDevice *device, DWORD len)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("(%p,%ld)\n", device, len);
|
2005-01-24 12:27:05 +01:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (len > device->tmp_buffer_len) {
|
|
|
|
|
if (device->tmp_buffer)
|
|
|
|
|
device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, device->tmp_buffer, len);
|
2005-01-24 12:27:05 +01:00
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
|
2005-01-24 12:27:05 +01:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->tmp_buffer_len = len;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2005-01-24 12:27:05 +01:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
return device->tmp_buffer;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
|
|
|
|
|
{
|
2005-02-25 20:16:57 +01:00
|
|
|
|
INT i, len, ilen, field, todo;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
BYTE *buf, *ibuf;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p,%ld,%ld)\n",dsb,writepos,fraglen);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
len = fraglen;
|
|
|
|
|
if (!(dsb->playflags & DSBPLAY_LOOPING)) {
|
2005-06-20 16:12:03 +02:00
|
|
|
|
int secondary_remainder = dsb->buflen - dsb->buf_mixpos;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
int adjusted_remainder = MulDiv(dsb->device->pwfx->nAvgBytesPerSec, secondary_remainder, dsb->nAvgBytesPerSec);
|
2005-06-21 11:43:29 +02:00
|
|
|
|
assert(adjusted_remainder >= 0);
|
2005-06-20 16:12:03 +02:00
|
|
|
|
TRACE("secondary_remainder = %d, adjusted_remainder = %d, len = %d\n", secondary_remainder, adjusted_remainder, len);
|
|
|
|
|
if (adjusted_remainder < len) {
|
|
|
|
|
TRACE("clipping len to remainder of secondary buffer\n");
|
|
|
|
|
len = adjusted_remainder;
|
|
|
|
|
}
|
|
|
|
|
if (len == 0)
|
|
|
|
|
return 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
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);
|
2005-03-19 18:09:33 +01:00
|
|
|
|
len = (len / nBlockAlign) * nBlockAlign; /* data alignment */
|
2005-02-25 20:16:57 +01:00
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((buf = ibuf = DSOUND_tmpbuffer(dsb->device, len)) == NULL)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb, len, writepos);
|
|
|
|
|
|
|
|
|
|
ilen = DSOUND_MixerNorm(dsb, ibuf, len);
|
|
|
|
|
if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
|
2003-06-28 00:22:15 +02:00
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) ||
|
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
2002-06-13 21:15:06 +02:00
|
|
|
|
DSOUND_MixerVol(dsb, ibuf, len);
|
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if (dsb->device->pwfx->wBitsPerSample == 8) {
|
|
|
|
|
BYTE *obuf = dsb->device->buffer + writepos;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 22:01:32 +02:00
|
|
|
|
todo = len;
|
|
|
|
|
else
|
2006-01-06 12:35:20 +01:00
|
|
|
|
todo = dsb->device->buflen - writepos;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* 8-bit WAV is unsigned */
|
2004-08-12 22:01:32 +02:00
|
|
|
|
field = (*ibuf++ - 128);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
field += (*obuf - 128);
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 127) field = 127;
|
|
|
|
|
else if (field < -128) field = -128;
|
|
|
|
|
*obuf++ = field + 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (todo < len) {
|
|
|
|
|
todo = len - todo;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obuf = dsb->device->buffer;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
|
|
|
|
/* 8-bit WAV is unsigned */
|
|
|
|
|
field = (*ibuf++ - 128);
|
|
|
|
|
field += (*obuf - 128);
|
|
|
|
|
if (field > 127) field = 127;
|
|
|
|
|
else if (field < -128) field = -128;
|
|
|
|
|
*obuf++ = field + 128;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
INT16 *ibufs, *obufs;
|
|
|
|
|
|
|
|
|
|
ibufs = (INT16 *) ibuf;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obufs = (INT16 *)(dsb->device->buffer + writepos);
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 22:01:32 +02:00
|
|
|
|
todo = len / 2;
|
|
|
|
|
else
|
2006-01-06 12:35:20 +01:00
|
|
|
|
todo = (dsb->device->buflen - writepos) / 2;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* 16-bit WAV is signed */
|
2004-08-12 22:01:32 +02:00
|
|
|
|
field = *ibufs++;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
field += *obufs;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 32767) field = 32767;
|
|
|
|
|
else if (field < -32768) field = -32768;
|
|
|
|
|
*obufs++ = field;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
if (todo < (len / 2)) {
|
|
|
|
|
todo = (len / 2) - todo;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obufs = (INT16 *)dsb->device->buffer;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
|
|
|
|
/* 16-bit WAV is signed */
|
|
|
|
|
field = *ibufs++;
|
|
|
|
|
field += *obufs;
|
|
|
|
|
if (field > 32767) field = 32767;
|
|
|
|
|
else if (field < -32768) field = -32768;
|
|
|
|
|
*obufs++ = field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
if (dsb->leadin && (dsb->startpos > dsb->buf_mixpos) && (dsb->startpos <= dsb->buf_mixpos + ilen)) {
|
|
|
|
|
/* HACK... leadin should be reset when the PLAY position reaches the startpos,
|
|
|
|
|
* not the MIX position... but if the sound buffer is bigger than our prebuffering
|
|
|
|
|
* (which must be the case for the streaming buffers that need this hack anyway)
|
|
|
|
|
* plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
|
|
|
|
|
dsb->leadin = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dsb->buf_mixpos += ilen;
|
|
|
|
|
|
|
|
|
|
if (dsb->buf_mixpos >= dsb->buflen) {
|
2003-03-17 22:23:12 +01:00
|
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* wrap */
|
2004-08-18 02:30:37 +02:00
|
|
|
|
dsb->buf_mixpos %= dsb->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (dsb->leadin && (dsb->startpos <= dsb->buf_mixpos))
|
|
|
|
|
dsb->leadin = FALSE; /* HACK: see above */
|
2005-08-03 21:13:58 +02:00
|
|
|
|
} else if (dsb->buf_mixpos > dsb->buflen) {
|
|
|
|
|
ERR("Mixpos (%lu) past buflen (%lu), capping...\n", dsb->buf_mixpos, dsb->buflen);
|
|
|
|
|
dsb->buf_mixpos = dsb->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DSOUND_PhaseCancel(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD len)
|
|
|
|
|
{
|
2005-02-25 20:16:57 +01:00
|
|
|
|
INT ilen, field;
|
2004-09-08 03:23:57 +02:00
|
|
|
|
UINT i, todo;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
BYTE *buf, *ibuf;
|
|
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p,%ld,%ld)\n",dsb,writepos,len);
|
2002-06-13 21:15:06 +02: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 = %ld, block size = %d\n", len, nBlockAlign);
|
2005-03-19 18:09:33 +01:00
|
|
|
|
len = (len / nBlockAlign) * nBlockAlign; /* data alignment */
|
2005-02-25 20:16:57 +01:00
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((buf = ibuf = DSOUND_tmpbuffer(dsb->device, len)) == NULL)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
TRACE("PhaseCancel (%p) len = %ld, dest = %ld\n", dsb, len, writepos);
|
|
|
|
|
|
|
|
|
|
ilen = DSOUND_MixerNorm(dsb, ibuf, len);
|
|
|
|
|
if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
|
2003-06-28 00:22:15 +02:00
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) ||
|
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
2002-06-13 21:15:06 +02:00
|
|
|
|
DSOUND_MixerVol(dsb, ibuf, len);
|
|
|
|
|
|
|
|
|
|
/* subtract instead of add, to phase out premixed data */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if (dsb->device->pwfx->wBitsPerSample == 8) {
|
|
|
|
|
BYTE *obuf = dsb->device->buffer + writepos;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 22:01:32 +02:00
|
|
|
|
todo = len;
|
|
|
|
|
else
|
2006-01-06 12:35:20 +01:00
|
|
|
|
todo = dsb->device->buflen - writepos;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* 8-bit WAV is unsigned */
|
2005-11-01 11:18:55 +01:00
|
|
|
|
field = (*obuf - 128);
|
|
|
|
|
field -= (*ibuf++ - 128);
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 127) field = 127;
|
|
|
|
|
else if (field < -128) field = -128;
|
|
|
|
|
*obuf++ = field + 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (todo < len) {
|
|
|
|
|
todo = len - todo;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obuf = dsb->device->buffer;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
|
|
|
|
/* 8-bit WAV is unsigned */
|
2005-11-01 11:18:55 +01:00
|
|
|
|
field = (*obuf - 128);
|
|
|
|
|
field -= (*ibuf++ - 128);
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 127) field = 127;
|
|
|
|
|
else if (field < -128) field = -128;
|
|
|
|
|
*obuf++ = field + 128;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
INT16 *ibufs, *obufs;
|
|
|
|
|
|
|
|
|
|
ibufs = (INT16 *) ibuf;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obufs = (INT16 *)(dsb->device->buffer + writepos);
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 22:01:32 +02:00
|
|
|
|
todo = len / 2;
|
|
|
|
|
else
|
2006-01-06 12:35:20 +01:00
|
|
|
|
todo = (dsb->device->buflen - writepos) / 2;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* 16-bit WAV is signed */
|
2005-11-01 11:18:55 +01:00
|
|
|
|
field = *obufs;
|
|
|
|
|
field -= *ibufs++;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 32767) field = 32767;
|
|
|
|
|
else if (field < -32768) field = -32768;
|
|
|
|
|
*obufs++ = field;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
if (todo < (len / 2)) {
|
|
|
|
|
todo = (len / 2) - todo;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
obufs = (INT16 *)dsb->device->buffer;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
|
|
|
|
/* 16-bit WAV is signed */
|
2005-11-01 11:18:55 +01:00
|
|
|
|
field = *obufs;
|
|
|
|
|
field -= *ibufs++;
|
2004-08-12 22:01:32 +02:00
|
|
|
|
if (field > 32767) field = 32767;
|
|
|
|
|
else if (field < -32768) field = -32768;
|
|
|
|
|
*obufs++ = field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DSOUND_MixCancel(IDirectSoundBufferImpl *dsb, DWORD writepos, BOOL cancel)
|
|
|
|
|
{
|
|
|
|
|
DWORD size, flen, len, npos, nlen;
|
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;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* determine amount of premixed data to cancel */
|
|
|
|
|
DWORD primary_done =
|
2006-01-06 12:35:20 +01:00
|
|
|
|
((dsb->primary_mixpos < writepos) ? dsb->device->buflen : 0) +
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->primary_mixpos - writepos;
|
|
|
|
|
|
|
|
|
|
TRACE("(%p, %ld), buf_mixpos=%ld\n", dsb, writepos, dsb->buf_mixpos);
|
|
|
|
|
|
|
|
|
|
/* backtrack the mix position */
|
|
|
|
|
size = primary_done / oAdvance;
|
|
|
|
|
flen = size * dsb->freqAdjust;
|
|
|
|
|
len = (flen >> DSOUND_FREQSHIFT) * iAdvance;
|
|
|
|
|
flen &= (1<<DSOUND_FREQSHIFT)-1;
|
|
|
|
|
while (dsb->freqAcc < flen) {
|
|
|
|
|
len += iAdvance;
|
|
|
|
|
dsb->freqAcc += 1<<DSOUND_FREQSHIFT;
|
|
|
|
|
}
|
|
|
|
|
len %= dsb->buflen;
|
|
|
|
|
npos = ((dsb->buf_mixpos < len) ? dsb->buflen : 0) +
|
|
|
|
|
dsb->buf_mixpos - len;
|
|
|
|
|
if (dsb->leadin && (dsb->startpos > npos) && (dsb->startpos <= npos + len)) {
|
|
|
|
|
/* stop backtracking at startpos */
|
|
|
|
|
npos = dsb->startpos;
|
|
|
|
|
len = ((dsb->buf_mixpos < npos) ? dsb->buflen : 0) +
|
|
|
|
|
dsb->buf_mixpos - npos;
|
|
|
|
|
flen = dsb->freqAcc;
|
2004-08-18 02:30:37 +02:00
|
|
|
|
nlen = len / dsb->pwfx->nBlockAlign;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
nlen = ((nlen << DSOUND_FREQSHIFT) + flen) / dsb->freqAdjust;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
nlen *= dsb->device->pwfx->nBlockAlign;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
writepos =
|
2006-01-06 12:35:20 +01:00
|
|
|
|
((dsb->primary_mixpos < nlen) ? dsb->device->buflen : 0) +
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->primary_mixpos - nlen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dsb->freqAcc -= flen;
|
|
|
|
|
dsb->buf_mixpos = npos;
|
|
|
|
|
dsb->primary_mixpos = writepos;
|
|
|
|
|
|
|
|
|
|
TRACE("new buf_mixpos=%ld, primary_mixpos=%ld (len=%ld)\n",
|
|
|
|
|
dsb->buf_mixpos, dsb->primary_mixpos, len);
|
|
|
|
|
|
|
|
|
|
if (cancel) DSOUND_PhaseCancel(dsb, writepos, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DSOUND_MixCancelAt(IDirectSoundBufferImpl *dsb, DWORD buf_writepos)
|
|
|
|
|
{
|
|
|
|
|
#if 0
|
|
|
|
|
DWORD i, size, flen, len, npos, nlen;
|
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;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* determine amount of premixed data to cancel */
|
|
|
|
|
DWORD buf_done =
|
|
|
|
|
((dsb->buf_mixpos < buf_writepos) ? dsb->buflen : 0) +
|
|
|
|
|
dsb->buf_mixpos - buf_writepos;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WARN("(%p, %ld), buf_mixpos=%ld\n", dsb, buf_writepos, dsb->buf_mixpos);
|
|
|
|
|
/* since this is not implemented yet, just cancel *ALL* prebuffering for now
|
|
|
|
|
* (which is faster anyway when there's only a single secondary buffer) */
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb->device->need_remix = TRUE;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DSOUND_ForceRemix(IDirectSoundBufferImpl *dsb)
|
|
|
|
|
{
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p)\n",dsb);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
EnterCriticalSection(&dsb->lock);
|
2005-05-09 11:23:54 +02:00
|
|
|
|
if (dsb->state == STATE_PLAYING)
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb->device->need_remix = TRUE;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
LeaveCriticalSection(&dsb->lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD playpos, DWORD writepos, DWORD mixlen)
|
|
|
|
|
{
|
|
|
|
|
DWORD len, slen;
|
|
|
|
|
/* determine this buffer's write position */
|
2005-05-09 11:23:54 +02:00
|
|
|
|
DWORD buf_writepos = DSOUND_CalcPlayPosition(dsb, writepos, writepos);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* determine how much already-mixed data exists */
|
|
|
|
|
DWORD buf_done =
|
|
|
|
|
((dsb->buf_mixpos < buf_writepos) ? dsb->buflen : 0) +
|
|
|
|
|
dsb->buf_mixpos - buf_writepos;
|
|
|
|
|
DWORD primary_done =
|
2006-01-06 12:35:20 +01:00
|
|
|
|
((dsb->primary_mixpos < writepos) ? dsb->device->buflen : 0) +
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->primary_mixpos - writepos;
|
|
|
|
|
DWORD adv_done =
|
2006-01-06 12:35:20 +01:00
|
|
|
|
((dsb->device->mixpos < writepos) ? dsb->device->buflen : 0) +
|
|
|
|
|
dsb->device->mixpos - writepos;
|
2003-03-17 22:23:12 +01:00
|
|
|
|
DWORD played =
|
|
|
|
|
((buf_writepos < dsb->playpos) ? dsb->buflen : 0) +
|
|
|
|
|
buf_writepos - dsb->playpos;
|
|
|
|
|
DWORD buf_left = dsb->buflen - buf_writepos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
int still_behind;
|
|
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p,%ld,%ld,%ld)\n",dsb,playpos,writepos,mixlen);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
TRACE("buf_writepos=%ld, primary_writepos=%ld\n", buf_writepos, writepos);
|
|
|
|
|
TRACE("buf_done=%ld, primary_done=%ld\n", buf_done, primary_done);
|
|
|
|
|
TRACE("buf_mixpos=%ld, primary_mixpos=%ld, mixlen=%ld\n", dsb->buf_mixpos, dsb->primary_mixpos,
|
|
|
|
|
mixlen);
|
|
|
|
|
TRACE("looping=%ld, startpos=%ld, leadin=%ld\n", dsb->playflags, dsb->startpos, dsb->leadin);
|
|
|
|
|
|
2003-03-17 22:23:12 +01:00
|
|
|
|
/* check for notification positions */
|
|
|
|
|
if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
|
|
|
|
|
dsb->state != STATE_STARTING) {
|
|
|
|
|
DSOUND_CheckEvent(dsb, played);
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* save write position for non-GETCURRENTPOSITION2... */
|
|
|
|
|
dsb->playpos = buf_writepos;
|
|
|
|
|
|
|
|
|
|
/* check whether CalcPlayPosition detected a mixing underrun */
|
|
|
|
|
if ((buf_done == 0) && (dsb->primary_mixpos != writepos)) {
|
|
|
|
|
/* it did, but did we have more to play? */
|
|
|
|
|
if ((dsb->playflags & DSBPLAY_LOOPING) ||
|
|
|
|
|
(dsb->buf_mixpos < dsb->buflen)) {
|
|
|
|
|
/* yes, have to recover */
|
|
|
|
|
ERR("underrun on sound buffer %p\n", dsb);
|
|
|
|
|
TRACE("recovering from underrun: primary_mixpos=%ld\n", writepos);
|
|
|
|
|
}
|
|
|
|
|
dsb->primary_mixpos = writepos;
|
|
|
|
|
primary_done = 0;
|
|
|
|
|
}
|
|
|
|
|
/* determine how far ahead we should mix */
|
|
|
|
|
if (((dsb->playflags & DSBPLAY_LOOPING) ||
|
|
|
|
|
(dsb->leadin && (dsb->probably_valid_to != 0))) &&
|
|
|
|
|
!(dsb->dsbd.dwFlags & DSBCAPS_STATIC)) {
|
|
|
|
|
/* if this is a streaming buffer, it typically means that
|
|
|
|
|
* we should defer mixing past probably_valid_to as long
|
|
|
|
|
* as we can, to avoid unnecessary remixing */
|
|
|
|
|
/* the heavy-looking calculations shouldn't be that bad,
|
|
|
|
|
* as any game isn't likely to be have more than 1 or 2
|
|
|
|
|
* streaming buffers in use at any time anyway... */
|
|
|
|
|
DWORD probably_valid_left =
|
|
|
|
|
(dsb->probably_valid_to == (DWORD)-1) ? dsb->buflen :
|
|
|
|
|
((dsb->probably_valid_to < buf_writepos) ? dsb->buflen : 0) +
|
|
|
|
|
dsb->probably_valid_to - buf_writepos;
|
|
|
|
|
/* check for leadin condition */
|
|
|
|
|
if ((probably_valid_left == 0) &&
|
|
|
|
|
(dsb->probably_valid_to == dsb->startpos) &&
|
|
|
|
|
dsb->leadin)
|
|
|
|
|
probably_valid_left = dsb->buflen;
|
|
|
|
|
TRACE("streaming buffer probably_valid_to=%ld, probably_valid_left=%ld\n",
|
|
|
|
|
dsb->probably_valid_to, probably_valid_left);
|
|
|
|
|
/* check whether the app's time is already up */
|
|
|
|
|
if (probably_valid_left < dsb->writelead) {
|
|
|
|
|
WARN("probably_valid_to now within writelead, possible streaming underrun\n");
|
|
|
|
|
/* once we pass the point of no return,
|
|
|
|
|
* no reason to hold back anymore */
|
|
|
|
|
dsb->probably_valid_to = (DWORD)-1;
|
|
|
|
|
/* we just have to go ahead and mix what we have,
|
|
|
|
|
* there's no telling what the app is thinking anyway */
|
|
|
|
|
} else {
|
|
|
|
|
/* adjust for our frequency and our sample size */
|
|
|
|
|
probably_valid_left = MulDiv(probably_valid_left,
|
|
|
|
|
1 << DSOUND_FREQSHIFT,
|
2004-08-18 02:30:37 +02:00
|
|
|
|
dsb->pwfx->nBlockAlign * dsb->freqAdjust) *
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb->device->pwfx->nBlockAlign;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* check whether to clip mix_len */
|
|
|
|
|
if (probably_valid_left < mixlen) {
|
|
|
|
|
TRACE("clipping to probably_valid_left=%ld\n", probably_valid_left);
|
|
|
|
|
mixlen = probably_valid_left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* cut mixlen with what's already been mixed */
|
|
|
|
|
if (mixlen < primary_done) {
|
|
|
|
|
/* huh? and still CalcPlayPosition didn't
|
|
|
|
|
* detect an underrun? */
|
|
|
|
|
FIXME("problem with underrun detection (mixlen=%ld < primary_done=%ld)\n", mixlen, primary_done);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
len = mixlen - primary_done;
|
|
|
|
|
TRACE("remaining mixlen=%ld\n", len);
|
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if (len < dsb->device->fraglen) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* smaller than a fragment, wait until it gets larger
|
|
|
|
|
* before we take the mixing overhead */
|
|
|
|
|
TRACE("mixlen not worth it, deferring mixing\n");
|
2003-03-17 22:23:12 +01:00
|
|
|
|
still_behind = 1;
|
|
|
|
|
goto post_mix;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ok, we know how much to mix, let's go */
|
|
|
|
|
still_behind = (adv_done > primary_done);
|
|
|
|
|
while (len) {
|
2006-01-06 12:35:20 +01:00
|
|
|
|
slen = dsb->device->buflen - dsb->primary_mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (slen > len) slen = len;
|
|
|
|
|
slen = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, slen);
|
|
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
|
if ((dsb->primary_mixpos < dsb->device->mixpos) &&
|
|
|
|
|
(dsb->primary_mixpos + slen >= dsb->device->mixpos))
|
2002-06-13 21:15:06 +02:00
|
|
|
|
still_behind = FALSE;
|
|
|
|
|
|
|
|
|
|
dsb->primary_mixpos += slen; len -= slen;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
dsb->primary_mixpos %= dsb->device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
if ((dsb->state == STATE_STOPPED) || !slen) break;
|
|
|
|
|
}
|
2006-01-06 12:35:20 +01:00
|
|
|
|
TRACE("new primary_mixpos=%ld, primary_advbase=%ld\n", dsb->primary_mixpos, dsb->device->mixpos);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
TRACE("mixed data len=%ld, still_behind=%d\n", mixlen-len, still_behind);
|
2003-03-17 22:23:12 +01:00
|
|
|
|
|
|
|
|
|
post_mix:
|
|
|
|
|
/* check if buffer should be considered complete */
|
|
|
|
|
if (buf_left < dsb->writelead &&
|
|
|
|
|
!(dsb->playflags & DSBPLAY_LOOPING)) {
|
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
|
dsb->playpos = 0;
|
|
|
|
|
dsb->last_playpos = 0;
|
|
|
|
|
dsb->buf_mixpos = 0;
|
|
|
|
|
dsb->leadin = FALSE;
|
2005-05-09 11:23:54 +02:00
|
|
|
|
dsb->need_remix = FALSE;
|
2003-03-17 22:23:12 +01:00
|
|
|
|
DSOUND_CheckEvent(dsb, buf_left);
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* return how far we think the primary buffer can
|
|
|
|
|
* advance its underrun detector...*/
|
|
|
|
|
if (still_behind) return 0;
|
|
|
|
|
if ((mixlen - len) < primary_done) return 0;
|
2006-01-06 12:35:20 +01:00
|
|
|
|
slen = ((dsb->primary_mixpos < dsb->device->mixpos) ?
|
|
|
|
|
dsb->device->buflen : 0) + dsb->primary_mixpos -
|
|
|
|
|
dsb->device->mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (slen > mixlen) {
|
|
|
|
|
/* the primary_done and still_behind checks above should have worked */
|
|
|
|
|
FIXME("problem with advancement calculation (advlen=%ld > mixlen=%ld)\n", slen, mixlen);
|
|
|
|
|
slen = 0;
|
|
|
|
|
}
|
|
|
|
|
return slen;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
static DWORD DSOUND_MixToPrimary(DirectSoundDevice *device, DWORD playpos, DWORD writepos, DWORD mixlen, BOOL recover)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
|
|
|
|
INT i, len, maxlen = 0;
|
|
|
|
|
IDirectSoundBufferImpl *dsb;
|
|
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%ld,%ld,%ld,%d)\n", playpos, 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
|
|
|
|
|
|
|
|
|
if (dsb->buflen && dsb->state && !dsb->hwbuf) {
|
|
|
|
|
TRACE("Checking %p, mixlen=%ld\n", dsb, mixlen);
|
|
|
|
|
EnterCriticalSection(&(dsb->lock));
|
|
|
|
|
if (dsb->state == STATE_STOPPING) {
|
|
|
|
|
DSOUND_MixCancel(dsb, writepos, TRUE);
|
|
|
|
|
dsb->state = STATE_STOPPED;
|
2003-03-17 22:23:12 +01:00
|
|
|
|
DSOUND_CheckEvent(dsb, 0);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else {
|
|
|
|
|
if ((dsb->state == STATE_STARTING) || recover) {
|
|
|
|
|
dsb->primary_mixpos = writepos;
|
2004-09-07 21:32:21 +02:00
|
|
|
|
dsb->cvolpan = dsb->volpan;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->need_remix = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else if (dsb->need_remix) {
|
|
|
|
|
DSOUND_MixCancel(dsb, writepos, TRUE);
|
2004-09-07 21:32:21 +02:00
|
|
|
|
dsb->cvolpan = dsb->volpan;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->need_remix = FALSE;
|
|
|
|
|
}
|
|
|
|
|
len = DSOUND_MixOne(dsb, playpos, writepos, mixlen);
|
|
|
|
|
if (dsb->state == STATE_STARTING)
|
|
|
|
|
dsb->state = STATE_PLAYING;
|
|
|
|
|
maxlen = (len > maxlen) ? len : maxlen;
|
|
|
|
|
}
|
|
|
|
|
LeaveCriticalSection(&(dsb->lock));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return maxlen;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
static void DSOUND_MixReset(DirectSoundDevice *device, DWORD writepos)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
|
|
|
|
INT i;
|
|
|
|
|
IDirectSoundBufferImpl *dsb;
|
|
|
|
|
int nfiller;
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("(%p,%ld)\n", device, writepos);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
/* the sound of silence */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
/* reset all buffer mix positions */
|
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
|
|
|
|
|
|
|
|
|
if (dsb->buflen && dsb->state && !dsb->hwbuf) {
|
|
|
|
|
TRACE("Resetting %p\n", dsb);
|
|
|
|
|
EnterCriticalSection(&(dsb->lock));
|
|
|
|
|
if (dsb->state == STATE_STOPPING) {
|
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
|
}
|
|
|
|
|
else if (dsb->state == STATE_STARTING) {
|
|
|
|
|
/* nothing */
|
|
|
|
|
} else {
|
|
|
|
|
DSOUND_MixCancel(dsb, writepos, FALSE);
|
2004-09-07 21:32:21 +02:00
|
|
|
|
dsb->cvolpan = dsb->volpan;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
dsb->need_remix = FALSE;
|
|
|
|
|
}
|
|
|
|
|
LeaveCriticalSection(&(dsb->lock));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* wipe out premixed data */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->mixpos < writepos) {
|
|
|
|
|
FillMemory(device->buffer + writepos, device->buflen - writepos, nfiller);
|
|
|
|
|
FillMemory(device->buffer, device->mixpos, nfiller);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
FillMemory(device->buffer + writepos, device->mixpos - writepos, nfiller);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* reset primary mix position */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->mixpos = writepos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
static void DSOUND_CheckReset(DirectSoundDevice *device, DWORD writepos)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("(%p,%ld)\n",device,writepos);
|
|
|
|
|
if (device->need_remix) {
|
|
|
|
|
DSOUND_MixReset(device, writepos);
|
|
|
|
|
device->need_remix = FALSE;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* maximize Half-Life performance */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->prebuf = ds_snd_queue_min;
|
|
|
|
|
device->precount = 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->precount++;
|
|
|
|
|
if (device->precount >= 4) {
|
|
|
|
|
if (device->prebuf < ds_snd_queue_max)
|
|
|
|
|
device->prebuf++;
|
|
|
|
|
device->precount = 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("premix adjust: %d\n", device->prebuf);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
void DSOUND_WaveQueue(DirectSoundDevice *device, DWORD mixq)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("(%p,%ld)\n", device, mixq);
|
|
|
|
|
if (mixq + device->pwqueue > ds_hel_queue) mixq = ds_hel_queue - device->pwqueue;
|
|
|
|
|
TRACE("queueing %ld buffers, starting at %d\n", mixq, device->pwwrite);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
for (; mixq; mixq--) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
waveOutWrite(device->hwo, device->pwave[device->pwwrite], sizeof(WAVEHDR));
|
|
|
|
|
device->pwwrite++;
|
|
|
|
|
if (device->pwwrite >= DS_HEL_FRAGS) device->pwwrite = 0;
|
|
|
|
|
device->pwqueue++;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* #define SYNC_CALLBACK */
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
void DSOUND_PerformMix(DirectSoundDevice *device)
|
2002-06-13 21:15:06 +02:00
|
|
|
|
{
|
|
|
|
|
int nfiller;
|
|
|
|
|
BOOL forced;
|
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
TRACE("(%p)\n", device);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* the sound of silence */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
/* whether the primary is forced to play even without secondary buffers */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
forced = ((device->state == STATE_PLAYING) || (device->state == STATE_STARTING));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
|
|
|
|
BOOL paused = ((device->state == STATE_STOPPED) || (device->state == STATE_STARTING));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* FIXME: document variables */
|
|
|
|
|
DWORD playpos, writepos, inq, maxq, frag;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->hwbuf) {
|
|
|
|
|
hres = IDsDriverBuffer_GetPosition(device->hwbuf, &playpos, &writepos);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (hres) {
|
2003-06-28 00:22:15 +02:00
|
|
|
|
WARN("IDsDriverBuffer_GetPosition failed\n");
|
2002-06-13 21:15:06 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* Well, we *could* do Just-In-Time mixing using the writepos,
|
|
|
|
|
* but that's a little bit ambitious and unnecessary... */
|
|
|
|
|
/* rather add our safety margin to the writepos, if we're playing */
|
|
|
|
|
if (!paused) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
writepos += device->writelead;
|
|
|
|
|
writepos %= device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else writepos = playpos;
|
2003-05-22 05:39:13 +02:00
|
|
|
|
} else {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
playpos = device->pwplay * device->fraglen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
writepos = playpos;
|
|
|
|
|
if (!paused) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
writepos += ds_hel_margin * device->fraglen;
|
|
|
|
|
writepos %= device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-05-22 05:39:13 +02:00
|
|
|
|
TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld, buflen=%ld\n",
|
2005-05-31 11:31:37 +02:00
|
|
|
|
playpos,writepos,device->playpos,device->mixpos,device->buflen);
|
|
|
|
|
assert(device->playpos < device->buflen);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* wipe out just-played sound data */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (playpos < device->playpos) {
|
|
|
|
|
FillMemory(device->buffer + device->playpos, device->buflen - device->playpos, nfiller);
|
|
|
|
|
FillMemory(device->buffer, playpos, nfiller);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
FillMemory(device->buffer + device->playpos, playpos - device->playpos, nfiller);
|
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
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
/* reset mixing if necessary */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
DSOUND_CheckReset(device, writepos);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
/* check how much prebuffering is left */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
inq = device->mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (inq < writepos)
|
2005-05-31 11:31:37 +02:00
|
|
|
|
inq += device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
inq -= writepos;
|
|
|
|
|
|
|
|
|
|
/* find the maximum we can prebuffer */
|
|
|
|
|
if (!paused) {
|
|
|
|
|
maxq = playpos;
|
|
|
|
|
if (maxq < writepos)
|
2005-05-31 11:31:37 +02:00
|
|
|
|
maxq += device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
maxq -= writepos;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
} else maxq = device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
|
/* clip maxq to device->prebuf */
|
|
|
|
|
frag = device->prebuf * device->fraglen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (maxq > frag) maxq = frag;
|
|
|
|
|
|
|
|
|
|
/* check for consistency */
|
|
|
|
|
if (inq > maxq) {
|
|
|
|
|
/* the playback position must have passed our last
|
|
|
|
|
* mixed position, i.e. it's an underrun, or we have
|
|
|
|
|
* nothing more to play */
|
|
|
|
|
TRACE("reached end of mixed data (inq=%ld, maxq=%ld)\n", inq, maxq);
|
|
|
|
|
inq = 0;
|
|
|
|
|
/* stop the playback now, to allow buffers to refill */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->state == STATE_PLAYING) {
|
|
|
|
|
device->state = STATE_STARTING;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
else if (device->state == STATE_STOPPING) {
|
|
|
|
|
device->state = STATE_STOPPED;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* how can we have an underrun if we aren't playing? */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
WARN("unexpected primary state (%ld)\n", device->state);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
#ifdef SYNC_CALLBACK
|
|
|
|
|
/* DSOUND_callback may need this lock */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#endif
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (DSOUND_PrimaryStop(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
|
WARN("DSOUND_PrimaryStop failed\n");
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#ifdef SYNC_CALLBACK
|
2005-05-31 11:31:37 +02:00
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#endif
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->hwbuf) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* the Stop is supposed to reset play position to beginning of buffer */
|
|
|
|
|
/* unfortunately, OSS is not able to do so, so get current pointer */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
hres = IDsDriverBuffer_GetPosition(device->hwbuf, &playpos, NULL);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (hres) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2003-06-28 00:22:15 +02:00
|
|
|
|
WARN("IDsDriverBuffer_GetPosition failed\n");
|
2002-06-13 21:15:06 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
playpos = device->pwplay * device->fraglen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
writepos = playpos;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->playpos = playpos;
|
|
|
|
|
device->mixpos = writepos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
inq = 0;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
maxq = device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (maxq > frag) maxq = frag;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
FillMemory(device->buffer, device->buflen, nfiller);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
paused = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* do the mixing */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
frag = DSOUND_MixToPrimary(device, playpos, writepos, maxq, paused);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (forced) frag = maxq - inq;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->mixpos += frag;
|
|
|
|
|
device->mixpos %= device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
|
|
if (frag) {
|
|
|
|
|
/* buffers have been filled, restart playback */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->state == STATE_STARTING) {
|
|
|
|
|
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_STOPPED) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* the dsound is supposed to play if there's something to play
|
|
|
|
|
* even if it is reported as stopped, so don't let this confuse you */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->state = STATE_STOPPING;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (paused) {
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (DSOUND_PrimaryPlay(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
|
else
|
|
|
|
|
TRACE("starting playback\n");
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
} else {
|
|
|
|
|
/* 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
|
|
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2004-09-07 21:32:21 +02:00
|
|
|
|
DWORD start_time = GetTickCount();
|
|
|
|
|
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);
|
2004-09-07 21:32:21 +02:00
|
|
|
|
TRACE("entering at %ld\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();
|
|
|
|
|
TRACE("completed processing at %ld, duration = %ld\n", end_time, end_time - start_time);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
|
|
|
|
|
{
|
2005-05-31 11:31:37 +02:00
|
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2003-09-02 01:58:43 +02:00
|
|
|
|
TRACE("(%p,%x,%lx,%lx,%lx)\n",hwo,msg,dwUser,dw1,dw2);
|
2003-03-15 01:54:11 +01:00
|
|
|
|
TRACE("entering at %ld, msg=%08x(%s)\n", GetTickCount(), msg,
|
|
|
|
|
msg==MM_WOM_DONE ? "MM_WOM_DONE" : msg==MM_WOM_CLOSE ? "MM_WOM_CLOSE" :
|
|
|
|
|
msg==MM_WOM_OPEN ? "MM_WOM_OPEN" : "UNKNOWN");
|
2002-06-13 21:15:06 +02:00
|
|
|
|
if (msg == MM_WOM_DONE) {
|
|
|
|
|
DWORD inq, mixq, fraglen, buflen, pwplay, playpos, mixpos;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (device->pwqueue == (DWORD)-1) {
|
2002-06-13 21:15:06 +02:00
|
|
|
|
TRACE("completed due to reset\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* it could be a bad idea to enter critical section here... if there's lock contention,
|
|
|
|
|
* the resulting scheduling delays might obstruct the winmm player thread */
|
|
|
|
|
#ifdef SYNC_CALLBACK
|
2005-05-31 11:31:37 +02:00
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#endif
|
|
|
|
|
/* retrieve current values */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
fraglen = device->fraglen;
|
|
|
|
|
buflen = device->buflen;
|
|
|
|
|
pwplay = device->pwplay;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
playpos = pwplay * fraglen;
|
2005-05-31 11:31:37 +02:00
|
|
|
|
mixpos = device->mixpos;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* check remaining mixed data */
|
|
|
|
|
inq = ((mixpos < playpos) ? buflen : 0) + mixpos - playpos;
|
|
|
|
|
mixq = inq / fraglen;
|
|
|
|
|
if ((inq - (mixq * fraglen)) > 0) mixq++;
|
|
|
|
|
/* complete the playing buffer */
|
|
|
|
|
TRACE("done playing primary pos=%ld\n", playpos);
|
|
|
|
|
pwplay++;
|
|
|
|
|
if (pwplay >= DS_HEL_FRAGS) pwplay = 0;
|
|
|
|
|
/* write new values */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
device->pwplay = pwplay;
|
|
|
|
|
device->pwqueue--;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
/* queue new buffer if we have data for it */
|
2005-05-31 11:31:37 +02:00
|
|
|
|
if (inq>1) DSOUND_WaveQueue(device, inq-1);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#ifdef SYNC_CALLBACK
|
2005-05-31 11:31:37 +02:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2002-06-13 21:15:06 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
TRACE("completed\n");
|
|
|
|
|
}
|