2002-06-13 19:15:06 +00:00
|
|
|
|
/* DirectSound
|
|
|
|
|
*
|
|
|
|
|
* Copyright 1998 Marcus Meissner
|
|
|
|
|
* Copyright 1998 Rob Riggs
|
|
|
|
|
* Copyright 2000-2002 TransGaming Technologies, Inc.
|
2007-06-13 15:05:49 +00:00
|
|
|
|
* Copyright 2007 Peter Dons Tychsen
|
2002-06-13 19:15:06 +00: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 12:49:52 +00:00
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
2003-09-05 23:08:26 +00:00
|
|
|
|
#include <stdarg.h>
|
2002-06-13 19:15:06 +00:00
|
|
|
|
#include <math.h> /* Insomnia - pow() function */
|
|
|
|
|
|
2005-01-24 13:31:27 +00:00
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
|
#define NONAMELESSUNION
|
2002-06-13 19:15:06 +00:00
|
|
|
|
#include "windef.h"
|
|
|
|
|
#include "winbase.h"
|
2005-10-17 09:24:50 +00:00
|
|
|
|
#include "winuser.h"
|
2002-06-13 19:15:06 +00:00
|
|
|
|
#include "mmsystem.h"
|
2002-09-12 22:07:02 +00:00
|
|
|
|
#include "winternl.h"
|
2002-06-13 19:15:06 +00: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-27 22:22:15 +00:00
|
|
|
|
TRACE("(%p)\n",volpan);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* the AmpFactors are expressed in 16.16 fixed point */
|
2003-09-19 00:10:51 +00:00
|
|
|
|
volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 0xffff);
|
2002-06-13 19:15:06 +00: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 00:10:51 +00:00
|
|
|
|
volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
|
2003-09-19 00:10:51 +00:00
|
|
|
|
volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("left = %x, right = %x\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-07-23 19:06:31 +00:00
|
|
|
|
void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
|
{
|
|
|
|
|
double left,right;
|
|
|
|
|
TRACE("(%p)\n",volpan);
|
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("left=%x, right=%x\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
|
2004-07-23 19:06:31 +00: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 18:47:09 +00:00
|
|
|
|
volpan->lVolume=right;
|
2004-07-23 19:06:31 +00:00
|
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalRightAmpFactor;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-08-02 18:47:09 +00:00
|
|
|
|
volpan->lVolume=left;
|
2004-07-23 19:06:31 +00:00
|
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalLeftAmpFactor;
|
|
|
|
|
}
|
|
|
|
|
if (volpan->lVolume < -10000)
|
|
|
|
|
volpan->lVolume=-10000;
|
2004-08-02 18:47:09 +00:00
|
|
|
|
volpan->lPan=right-left;
|
2004-07-23 19:06:31 +00:00
|
|
|
|
if (volpan->lPan < -10000)
|
|
|
|
|
volpan->lPan=-10000;
|
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2004-07-23 19:06:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
|
|
|
|
|
{
|
2003-09-01 23:58:43 +00:00
|
|
|
|
TRACE("(%p)\n",dsb);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
|
|
|
|
/* calculate the 10ms write lead */
|
2005-02-25 19:16:57 +00:00
|
|
|
|
dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00: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 19:39:46 +00:00
|
|
|
|
void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
DWORD offset;
|
|
|
|
|
LPDSBPOSITIONNOTIFY event;
|
2003-09-01 23:58:43 +00:00
|
|
|
|
TRACE("(%p,%d)\n",dsb,len);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2003-09-01 23:58:43 +00:00
|
|
|
|
if (dsb->nrofnotifies == 0)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
|
2007-07-06 20:57:48 +00:00
|
|
|
|
dsb, dsb->buflen, playpos, len);
|
2003-09-01 23:58:43 +00:00
|
|
|
|
for (i = 0; i < dsb->nrofnotifies ; i++) {
|
|
|
|
|
event = dsb->notifies + i;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
offset = event->dwOffset;
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("checking %d, position %d, event = %p\n",
|
2002-06-13 19:15:06 +00: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-18 23:48:57 +00:00
|
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
return;
|
|
|
|
|
} else
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-07-06 20:57:48 +00:00
|
|
|
|
if ((playpos + len) >= dsb->buflen) {
|
|
|
|
|
if ((offset < ((playpos + len) % dsb->buflen)) ||
|
|
|
|
|
(offset >= playpos)) {
|
2002-10-18 23:48:57 +00:00
|
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2007-07-06 20:57:48 +00:00
|
|
|
|
if ((offset >= playpos) && (offset < (playpos + len))) {
|
2002-10-18 23:48:57 +00:00
|
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-16 00:20:07 +00: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 19:15:06 +00:00
|
|
|
|
{
|
2003-01-16 00:20:07 +00:00
|
|
|
|
return (short)((b+(b << 8))-32768);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-16 00:20:07 +00:00
|
|
|
|
static inline BYTE cvtS16toU8(INT16 s)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2003-01-16 00:20:07 +00:00
|
|
|
|
return (s >> 8) ^ (unsigned char)0x80;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copy a single frame from the given input buffer to the given output buffer.
|
|
|
|
|
* Translate 8 <-> 16 bits and mono <-> stereo
|
|
|
|
|
*/
|
2007-04-28 14:39:28 +00:00
|
|
|
|
static inline void cp_fields(const IDirectSoundBufferImpl *dsb, const BYTE *ibuf, BYTE *obuf )
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2006-01-06 11:35:20 +00:00
|
|
|
|
DirectSoundDevice * device = dsb->device;
|
2003-01-16 00:20:07 +00:00
|
|
|
|
INT fl,fr;
|
|
|
|
|
|
2004-08-18 00:30:37 +00:00
|
|
|
|
if (dsb->pwfx->wBitsPerSample == 8) {
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 8 &&
|
|
|
|
|
device->pwfx->nChannels == dsb->pwfx->nChannels) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* avoid needless 8->16->8 conversion */
|
2003-01-16 00:20:07 +00:00
|
|
|
|
*obuf=*ibuf;
|
2004-08-18 00:30:37 +00:00
|
|
|
|
if (dsb->pwfx->nChannels==2)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*(obuf+1)=*(ibuf+1);
|
2003-01-16 00:20:07 +00:00
|
|
|
|
return;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2003-01-16 00:20:07 +00:00
|
|
|
|
fl = cvtU8toS16(*ibuf);
|
2004-08-18 00:30:37 +00:00
|
|
|
|
fr = (dsb->pwfx->nChannels==2 ? cvtU8toS16(*(ibuf + 1)) : fl);
|
2003-01-16 00:20:07 +00:00
|
|
|
|
} else {
|
2007-04-28 14:39:28 +00:00
|
|
|
|
fl = *((const INT16 *)ibuf);
|
|
|
|
|
fr = (dsb->pwfx->nChannels==2 ? *(((const INT16 *)ibuf) + 1) : fl);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2003-01-16 00:20:07 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->nChannels == 2) {
|
|
|
|
|
if (device->pwfx->wBitsPerSample == 8) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*obuf = cvtS16toU8(fl);
|
|
|
|
|
*(obuf + 1) = cvtS16toU8(fr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 16) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*((INT16 *)obuf) = fl;
|
|
|
|
|
*(((INT16 *)obuf) + 1) = fr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->nChannels == 1) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
fl = (fl + fr) >> 1;
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 8) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*obuf = cvtS16toU8(fl);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->pwfx->wBitsPerSample == 16) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
*((INT16 *)obuf) = fl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Mix at most the given amount of data into the given device buffer from the
|
|
|
|
|
* given secondary buffer, starting from the dsb's first currently unmixed
|
|
|
|
|
* frame (buf_mixpos), translating frequency (pitch), stereo/mono and
|
|
|
|
|
* bits-per-sample. The secondary buffer sample is looped if it is not
|
|
|
|
|
* long enough and it is a looping buffer.
|
|
|
|
|
* (Doesn't perform any mixing - this is a straight copy operation).
|
|
|
|
|
*
|
|
|
|
|
* Now with PerfectPitch (tm) technology
|
|
|
|
|
*
|
|
|
|
|
* dsb = the secondary buffer
|
|
|
|
|
* buf = the device buffer
|
|
|
|
|
* len = number of bytes to store in the device buffer
|
|
|
|
|
*
|
|
|
|
|
* Returns: the number of bytes read from the secondary buffer
|
|
|
|
|
* (ie. len, adjusted for frequency, number of channels and sample size,
|
|
|
|
|
* and limited by buffer length for non-looping buffers)
|
|
|
|
|
*/
|
2002-06-13 19:15:06 +00:00
|
|
|
|
static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
|
|
|
|
|
{
|
|
|
|
|
INT i, size, ipos, ilen;
|
|
|
|
|
BYTE *ibp, *obp;
|
2004-08-18 00:30:37 +00:00
|
|
|
|
INT iAdvance = dsb->pwfx->nBlockAlign;
|
2006-01-06 11:35:20 +00:00
|
|
|
|
INT oAdvance = dsb->device->pwfx->nBlockAlign;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2003-06-27 22:22:15 +00:00
|
|
|
|
ibp = dsb->buffer->memory + dsb->buf_mixpos;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
obp = buf;
|
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p, %p, %p), buf_mixpos=%d\n", dsb, ibp, obp, dsb->buf_mixpos);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* Check for the best case */
|
2006-01-06 11:35:20 +00: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 17:10:54 +00:00
|
|
|
|
INT bytesleft = dsb->buflen - dsb->buf_mixpos;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
TRACE("(%p) Best case\n", dsb);
|
|
|
|
|
if (len <= bytesleft )
|
2005-01-24 12:40:45 +00:00
|
|
|
|
CopyMemory(obp, ibp, len);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
else { /* wrap */
|
2005-01-24 12:40:45 +00:00
|
|
|
|
CopyMemory(obp, ibp, bytesleft);
|
|
|
|
|
CopyMemory(obp + bytesleft, dsb->buffer->memory, len - bytesleft);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for same sample rate */
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if (dsb->freq == dsb->device->pwfx->nSamplesPerSec) {
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p) Same sample rate %d = primary %d\n", dsb,
|
2006-01-06 11:35:20 +00:00
|
|
|
|
dsb->freq, dsb->device->pwfx->nSamplesPerSec);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
ilen = 0;
|
|
|
|
|
for (i = 0; i < len; i += oAdvance) {
|
|
|
|
|
cp_fields(dsb, ibp, obp );
|
|
|
|
|
ibp += iAdvance;
|
|
|
|
|
ilen += iAdvance;
|
|
|
|
|
obp += oAdvance;
|
2003-06-27 22:22:15 +00:00
|
|
|
|
if (ibp >= (BYTE *)(dsb->buffer->memory + dsb->buflen))
|
|
|
|
|
ibp = dsb->buffer->memory; /* wrap */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
return (ilen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Mix in different sample rates */
|
|
|
|
|
/* */
|
|
|
|
|
/* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
|
|
|
|
|
/* Patent Pending :-] */
|
|
|
|
|
|
2007-07-06 13:34:33 +00:00
|
|
|
|
/* Patent enhancements (c) 2000 Ove K<>ven,
|
2002-06-13 19:15:06 +00:00
|
|
|
|
* TransGaming Technologies Inc. */
|
|
|
|
|
|
|
|
|
|
/* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
|
2006-01-06 11:35:20 +00:00
|
|
|
|
dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec); */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
|
|
|
|
size = len / oAdvance;
|
|
|
|
|
ilen = 0;
|
|
|
|
|
ipos = dsb->buf_mixpos;
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2003-06-27 22:22:15 +00:00
|
|
|
|
cp_fields(dsb, (dsb->buffer->memory + ipos), obp);
|
2002-06-13 19:15:06 +00: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 00:30:37 +00:00
|
|
|
|
ipos %= dsb->buflen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ilen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
|
|
|
|
|
{
|
2003-05-02 21:23:16 +00:00
|
|
|
|
INT i;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
BYTE *bpc = buf;
|
|
|
|
|
INT16 *bps = (INT16 *) buf;
|
|
|
|
|
|
2003-05-02 21:23:16 +00:00
|
|
|
|
TRACE("(%p,%p,%d)\n",dsb,buf,len);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
|
|
|
|
|
dsb->volpan.dwTotalRightAmpFactor);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
|
|
|
|
|
(!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
|
2002-06-13 19:15:06 +00:00
|
|
|
|
!(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 11:35:20 +00:00
|
|
|
|
switch (dsb->device->pwfx->wBitsPerSample) {
|
2003-05-02 21:23:16 +00:00
|
|
|
|
case 8:
|
|
|
|
|
/* 8-bit WAV is unsigned, but we need to operate */
|
|
|
|
|
/* on signed data for this to work properly */
|
2006-01-06 11:35:20 +00:00
|
|
|
|
switch (dsb->device->pwfx->nChannels) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
case 1:
|
2003-05-02 21:23:16 +00:00
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
INT val = *bpc - 128;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
val = (val * dsb->volpan.dwTotalLeftAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
*bpc = val + 128;
|
|
|
|
|
bpc++;
|
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2003-05-02 21:23:16 +00:00
|
|
|
|
for (i = 0; i < len; i+=2) {
|
|
|
|
|
INT val = *bpc - 128;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
val = (val * dsb->volpan.dwTotalLeftAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
*bpc++ = val + 128;
|
|
|
|
|
val = *bpc - 128;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
val = (val * dsb->volpan.dwTotalRightAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
*bpc = val + 128;
|
|
|
|
|
bpc++;
|
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 11:35:20 +00:00
|
|
|
|
FIXME("doesn't support %d channels\n", dsb->device->pwfx->nChannels);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
break;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2003-05-02 21:23:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
/* 16-bit WAV is signed -- much better */
|
2006-01-06 11:35:20 +00:00
|
|
|
|
switch (dsb->device->pwfx->nChannels) {
|
2003-05-02 21:23:16 +00:00
|
|
|
|
case 1:
|
|
|
|
|
for (i = 0; i < len; i += 2) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
*bps = (*bps * dsb->volpan.dwTotalLeftAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
bps++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
for (i = 0; i < len; i += 4) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
*bps = (*bps * dsb->volpan.dwTotalLeftAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
bps++;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
*bps = (*bps * dsb->volpan.dwTotalRightAmpFactor) >> 16;
|
2003-05-02 21:23:16 +00:00
|
|
|
|
bps++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 11:35:20 +00:00
|
|
|
|
FIXME("doesn't support %d channels\n", dsb->device->pwfx->nChannels);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-01-06 11:35:20 +00:00
|
|
|
|
FIXME("doesn't support %d bit samples\n", dsb->device->pwfx->wBitsPerSample);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
break;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Make sure the device's tmp_buffer is at least the given size. Return a
|
|
|
|
|
* pointer to it.
|
|
|
|
|
*/
|
2005-05-31 09:31:37 +00:00
|
|
|
|
static LPBYTE DSOUND_tmpbuffer(DirectSoundDevice *device, DWORD len)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p,%d)\n", device, len);
|
2005-01-24 11:27:05 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (len > device->tmp_buffer_len) {
|
|
|
|
|
if (device->tmp_buffer)
|
|
|
|
|
device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, device->tmp_buffer, len);
|
2005-01-24 11:27:05 +00:00
|
|
|
|
else
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
|
2005-01-24 11:27:05 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->tmp_buffer_len = len;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2005-01-24 11:27:05 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
return device->tmp_buffer;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00: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 19:15:06 +00:00
|
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
|
|
|
|
|
{
|
2005-02-25 19:16:57 +00:00
|
|
|
|
INT i, len, ilen, field, todo;
|
2004-08-12 20:01:32 +00:00
|
|
|
|
BYTE *buf, *ibuf;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
len = fraglen;
|
|
|
|
|
if (!(dsb->playflags & DSBPLAY_LOOPING)) {
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/* This buffer is not looping, so make sure the requested
|
|
|
|
|
* length will not take us past the end of the buffer */
|
2005-06-20 14:12:03 +00:00
|
|
|
|
int secondary_remainder = dsb->buflen - dsb->buf_mixpos;
|
2006-01-06 11:35:20 +00:00
|
|
|
|
int adjusted_remainder = MulDiv(dsb->device->pwfx->nAvgBytesPerSec, secondary_remainder, dsb->nAvgBytesPerSec);
|
2005-06-21 09:43:29 +00:00
|
|
|
|
assert(adjusted_remainder >= 0);
|
2007-07-12 10:26:19 +00:00
|
|
|
|
adjusted_remainder -= adjusted_remainder % dsb->device->pwfx->nBlockAlign; /* data alignment */
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/* The adjusted remainder must be at least one sample,
|
|
|
|
|
* otherwise we will never reach the end of the
|
|
|
|
|
* secondary buffer, as there will perpetually be a
|
|
|
|
|
* fractional remainder */
|
2005-06-20 14:12:03 +00: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 19:15:06 +00:00
|
|
|
|
}
|
2005-02-25 19:16:57 +00:00
|
|
|
|
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if (len % dsb->device->pwfx->nBlockAlign) {
|
|
|
|
|
INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
|
2005-02-25 19:16:57 +00:00
|
|
|
|
ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
len -= len % nBlockAlign; /* data alignment */
|
2005-02-25 19:16:57 +00:00
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* Create temp buffer to hold actual resulting data */
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if ((buf = ibuf = DSOUND_tmpbuffer(dsb->device, len)) == NULL)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("MixInBuffer (%p) len = %d, dest = %d\n", dsb, len, writepos);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/* first, copy the data from the DirectSoundBuffer into the temporary
|
|
|
|
|
buffer, translating frequency/bits-per-sample/number-of-channels
|
|
|
|
|
to match the device settings */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
ilen = DSOUND_MixerNorm(dsb, ibuf, len);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* then apply the correct volume, if necessary */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
|
2003-06-27 22:22:15 +00:00
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) ||
|
|
|
|
|
(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
2002-06-13 19:15:06 +00:00
|
|
|
|
DSOUND_MixerVol(dsb, ibuf, len);
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/* Now mix the temporary buffer into the devices main buffer */
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if (dsb->device->pwfx->wBitsPerSample == 8) {
|
|
|
|
|
BYTE *obuf = dsb->device->buffer + writepos;
|
2004-08-12 20:01:32 +00:00
|
|
|
|
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 20:01:32 +00:00
|
|
|
|
todo = len;
|
|
|
|
|
else
|
2006-01-06 11:35:20 +00:00
|
|
|
|
todo = dsb->device->buflen - writepos;
|
2004-08-12 20:01:32 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* 8-bit WAV is unsigned */
|
2004-08-12 20:01:32 +00:00
|
|
|
|
field = (*ibuf++ - 128);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
field += (*obuf - 128);
|
2004-08-12 20:01:32 +00:00
|
|
|
|
if (field > 127) field = 127;
|
|
|
|
|
else if (field < -128) field = -128;
|
|
|
|
|
*obuf++ = field + 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (todo < len) {
|
|
|
|
|
todo = len - todo;
|
2006-01-06 11:35:20 +00:00
|
|
|
|
obuf = dsb->device->buffer;
|
2004-08-12 20:01:32 +00: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 11:35:20 +00:00
|
|
|
|
obufs = (INT16 *)(dsb->device->buffer + writepos);
|
2004-08-12 20:01:32 +00:00
|
|
|
|
|
2006-01-06 11:35:20 +00:00
|
|
|
|
if ((writepos + len) <= dsb->device->buflen)
|
2004-08-12 20:01:32 +00:00
|
|
|
|
todo = len / 2;
|
|
|
|
|
else
|
2006-01-06 11:35:20 +00:00
|
|
|
|
todo = (dsb->device->buflen - writepos) / 2;
|
2004-08-12 20:01:32 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < todo; i++) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* 16-bit WAV is signed */
|
2004-08-12 20:01:32 +00:00
|
|
|
|
field = *ibufs++;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
field += *obufs;
|
2004-08-12 20:01:32 +00:00
|
|
|
|
if (field > 32767) field = 32767;
|
|
|
|
|
else if (field < -32768) field = -32768;
|
|
|
|
|
*obufs++ = field;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2004-08-12 20:01:32 +00:00
|
|
|
|
|
|
|
|
|
if (todo < (len / 2)) {
|
|
|
|
|
todo = (len / 2) - todo;
|
2006-01-06 11:35:20 +00:00
|
|
|
|
obufs = (INT16 *)dsb->device->buffer;
|
2004-08-12 20:01:32 +00: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 19:15:06 +00: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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
|
/* check for notification positions */
|
|
|
|
|
if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
|
|
|
|
|
dsb->state != STATE_STARTING) {
|
|
|
|
|
DSOUND_CheckEvent(dsb, dsb->buf_mixpos, ilen);
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
dsb->buf_mixpos += ilen;
|
|
|
|
|
|
|
|
|
|
if (dsb->buf_mixpos >= dsb->buflen) {
|
2003-03-17 21:23:12 +00:00
|
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* wrap */
|
2004-08-18 00:30:37 +00:00
|
|
|
|
dsb->buf_mixpos %= dsb->buflen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if (dsb->leadin && (dsb->startpos <= dsb->buf_mixpos))
|
|
|
|
|
dsb->leadin = FALSE; /* HACK: see above */
|
2005-08-03 19:13:58 +00:00
|
|
|
|
} else if (dsb->buf_mixpos > dsb->buflen) {
|
2006-11-12 13:40:35 +00:00
|
|
|
|
ERR("Mixpos (%u) past buflen (%u), capping...\n", dsb->buf_mixpos, dsb->buflen);
|
2005-08-03 19:13:58 +00:00
|
|
|
|
dsb->buf_mixpos = dsb->buflen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
|
/* increase mix position */
|
|
|
|
|
dsb->primary_mixpos += len;
|
|
|
|
|
dsb->primary_mixpos %= dsb->device->buflen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:17:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calculate the distance between two buffer offsets, taking wraparound
|
|
|
|
|
* into account.
|
|
|
|
|
*/
|
|
|
|
|
static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
|
|
|
|
|
{
|
|
|
|
|
if (ptr1 >= ptr2) {
|
|
|
|
|
return ptr1 - ptr2;
|
|
|
|
|
} else {
|
|
|
|
|
return buflen + ptr1 - ptr2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Mix some frames from the given secondary buffer "dsb" into the device
|
|
|
|
|
* primary buffer.
|
|
|
|
|
*
|
|
|
|
|
* dsb = the secondary buffer
|
|
|
|
|
* 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 19:24:21 +00:00
|
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/* The buffer's primary_mixpos may be before or after the the device
|
|
|
|
|
* buffer's mixpos, but both must be ahead of writepos. */
|
2007-07-12 10:26:19 +00:00
|
|
|
|
DWORD primary_done, buflen = dsb->buflen / dsb->pwfx->nBlockAlign * dsb->device->pwfx->nBlockAlign;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-07-29 19:24:21 +00:00
|
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
TRACE("writepos=%d, buf_mixpos=%d, primary_mixpos=%d, mixlen=%d\n", writepos, dsb->buf_mixpos, dsb->primary_mixpos, mixlen);
|
|
|
|
|
TRACE("looping=%d, startpos=%d, leadin=%d, buflen=%d\n", dsb->playflags, dsb->startpos, dsb->leadin, dsb->buflen);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00: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 19:24:21 +00:00
|
|
|
|
ERR("Fatal error. Under/Overflow? primary_done=%d, mixpos=%d, primary_mixpos=%d, writepos=%d, mixlen=%d\n", primary_done,dsb->buf_mixpos,dsb->primary_mixpos, writepos, mixlen);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* take into acount already mixed data */
|
2007-07-12 11:21:43 +00:00
|
|
|
|
mixlen -= primary_done;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
|
TRACE("primary_done=%d, mixlen (primary) = %i\n", primary_done, mixlen);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
|
if ((dsb->playflags & DSBPLAY_LOOPING) && mixlen > buflen)
|
|
|
|
|
{
|
|
|
|
|
while (mixlen > buflen)
|
|
|
|
|
{
|
|
|
|
|
DWORD mixedlength = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, buflen);
|
|
|
|
|
mixlen -= buflen;
|
|
|
|
|
if (!mixedlength)
|
|
|
|
|
{
|
|
|
|
|
mixlen = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2007-07-06 20:57:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
|
/* clip to valid length */
|
|
|
|
|
mixlen = (buflen < mixlen) ? buflen : mixlen;
|
|
|
|
|
TRACE("mixlen (buffer)=%d\n", mixlen);
|
|
|
|
|
|
|
|
|
|
if (mixlen)
|
|
|
|
|
/* mix more data */
|
|
|
|
|
mixlen = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixlen);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
TRACE("new primary_mixpos=%d, mixed data len=%d, buffer left = %d\n",
|
|
|
|
|
dsb->primary_mixpos, mixlen, (dsb->buflen - dsb->buf_mixpos));
|
|
|
|
|
|
|
|
|
|
/* re-calculate the primary done */
|
|
|
|
|
primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
|
2003-03-17 21:23:12 +00:00
|
|
|
|
|
|
|
|
|
/* check if buffer should be considered complete */
|
2007-06-13 15:05:49 +00:00
|
|
|
|
if (((dsb->buflen - dsb->buf_mixpos) < dsb->writelead) &&
|
2003-03-17 21:23:12 +00:00
|
|
|
|
!(dsb->playflags & DSBPLAY_LOOPING)) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
TRACE("Buffer reached end. Stopped\n");
|
|
|
|
|
|
2003-03-17 21:23:12 +00:00
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
|
dsb->buf_mixpos = 0;
|
|
|
|
|
dsb->leadin = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* Report back the total prebuffered amount for this buffer */
|
|
|
|
|
return primary_done;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* For a DirectSoundDevice, go through all the currently playing buffers and
|
|
|
|
|
* mix them in to the device buffer.
|
|
|
|
|
*
|
|
|
|
|
* playpos = the current play position in the primary 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 15:05:49 +00:00
|
|
|
|
* all_stopped = reports back if all buffers have stopped
|
2007-02-21 19:16:22 +00:00
|
|
|
|
*
|
|
|
|
|
* Returns: the length beyond the writepos that was mixed to.
|
|
|
|
|
*/
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2007-07-29 19:24:21 +00:00
|
|
|
|
static DWORD DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos,
|
2007-06-13 15:05:49 +00:00
|
|
|
|
DWORD mixlen, BOOL recover, BOOL *all_stopped)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2007-06-13 15:05:49 +00:00
|
|
|
|
INT i, len;
|
|
|
|
|
DWORD minlen = 0;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
IDirectSoundBufferImpl *dsb;
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* unless we find a running buffer, all have stopped */
|
|
|
|
|
*all_stopped = TRUE;
|
|
|
|
|
|
2007-07-29 19:24:21 +00:00
|
|
|
|
TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
|
2005-05-31 09:31:37 +00:00
|
|
|
|
for (i = 0; i < device->nrofbuffers; i++) {
|
|
|
|
|
dsb = device->buffers[i];
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
|
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if (dsb->buflen && dsb->state && !dsb->hwbuf) {
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
|
2007-07-29 19:01:43 +00:00
|
|
|
|
RtlAcquireResourceShared(&dsb->lock, TRUE);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* if buffer is stopping it is stopped now */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if (dsb->state == STATE_STOPPING) {
|
|
|
|
|
dsb->state = STATE_STOPPED;
|
2007-07-06 20:57:48 +00:00
|
|
|
|
DSOUND_CheckEvent(dsb, 0, 0);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
} else {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* if recovering, reset the mix position */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if ((dsb->state == STATE_STARTING) || recover) {
|
|
|
|
|
dsb->primary_mixpos = writepos;
|
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* mix next buffer into the main buffer */
|
2007-07-29 19:24:21 +00:00
|
|
|
|
len = DSOUND_MixOne(dsb, writepos, mixlen);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* if the buffer was starting, it must be playing now */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if (dsb->state == STATE_STARTING)
|
|
|
|
|
dsb->state = STATE_PLAYING;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* check if min-len should be initialized */
|
|
|
|
|
if(minlen == 0) minlen = len;
|
|
|
|
|
|
|
|
|
|
/* record the minimum length mixed from all buffers */
|
|
|
|
|
/* we only want to return the length which *all* buffers have mixed */
|
|
|
|
|
if(len != 0) minlen = (len < minlen) ? len : minlen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(dsb->state != STATE_STOPPED){
|
|
|
|
|
*all_stopped = FALSE;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2007-07-29 19:01:43 +00:00
|
|
|
|
RtlReleaseResource(&dsb->lock);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
TRACE("Mixed at least %d from all buffers\n", minlen);
|
|
|
|
|
|
|
|
|
|
return minlen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00: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 19:15:06 +00:00
|
|
|
|
{
|
2007-06-13 15:05:49 +00:00
|
|
|
|
DWORD prebuf_frags, wave_writepos, wave_fragpos, i;
|
|
|
|
|
TRACE("(%p)\n", device);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* calculate the current wave frag position */
|
|
|
|
|
wave_fragpos = (device->pwplay + device->pwqueue) % DS_HEL_FRAGS;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* calculte the current wave write position */
|
|
|
|
|
wave_writepos = wave_fragpos * device->fraglen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-07-06 13:34:33 +00:00
|
|
|
|
TRACE("wave_fragpos = %i, wave_writepos = %i, pwqueue = %i, prebuf = %i\n",
|
|
|
|
|
wave_fragpos, wave_writepos, device->pwqueue, device->prebuf);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
if(force == FALSE){
|
|
|
|
|
/* check remaining prebuffered frags */
|
|
|
|
|
prebuf_frags = DSOUND_BufPtrDiff(device->buflen, device->mixpos, wave_writepos);
|
|
|
|
|
prebuf_frags = prebuf_frags / device->fraglen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
else{
|
|
|
|
|
/* buffer the maximum amount of frags */
|
|
|
|
|
prebuf_frags = device->prebuf;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* limit to the queue we have left */
|
|
|
|
|
if((prebuf_frags + device->pwqueue) > device->prebuf)
|
|
|
|
|
prebuf_frags = device->prebuf - device->pwqueue;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
TRACE("prebuf_frags = %i\n", prebuf_frags);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00: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);
|
|
|
|
|
waveOutWrite(device->hwo, device->pwave[wave_fragpos], sizeof(WAVEHDR));
|
|
|
|
|
wave_fragpos++;
|
|
|
|
|
wave_fragpos %= DS_HEL_FRAGS;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* **** */
|
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
|
|
|
|
|
|
|
|
|
TRACE("queue now = %i\n", device->pwqueue);
|
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-02-21 19:16:22 +00: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 11:26:14 +00:00
|
|
|
|
static void DSOUND_PerformMix(DirectSoundDevice *device)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
TRACE("(%p)\n", device);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* **** */
|
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
BOOL recover = FALSE, all_stopped = FALSE;
|
|
|
|
|
DWORD playpos, writepos, writelead, maxq, frag, prebuff_max, prebuff_left, size1, size2;
|
|
|
|
|
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 19:15:06 +00:00
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
|
2005-05-31 09:31:37 +00:00
|
|
|
|
playpos,writepos,device->playpos,device->mixpos,device->buflen);
|
|
|
|
|
assert(device->playpos < device->buflen);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* wipe out just-played sound data */
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (playpos < device->playpos) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
|
buf2 = device->buffer;
|
|
|
|
|
size1 = device->buflen - device->playpos;
|
|
|
|
|
size2 = playpos;
|
|
|
|
|
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 19:15:06 +00:00
|
|
|
|
} else {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
|
buf2 = NULL;
|
|
|
|
|
size1 = playpos - device->playpos;
|
|
|
|
|
size2 = 0;
|
|
|
|
|
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 19:15:06 +00:00
|
|
|
|
}
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->playpos = playpos;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* calc maximum prebuff */
|
|
|
|
|
prebuff_max = (device->prebuf * device->fraglen);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
/* check for underrun. underrun occurs when the write position passes the mix position */
|
|
|
|
|
if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
|
2007-07-29 19:24:21 +00:00
|
|
|
|
if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
|
|
|
|
|
WARN("Probable buffer underrun\n");
|
|
|
|
|
else TRACE("Buffer starting or buffer underrun\n");
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* recover mixing for all buffers */
|
|
|
|
|
recover = TRUE;
|
|
|
|
|
|
|
|
|
|
/* reset mix position to write position */
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->mixpos = writepos;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
|
if (lock)
|
2007-07-29 19:27:41 +00:00
|
|
|
|
IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, writepos, maxq, 0);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* do the mixing */
|
2007-07-29 19:24:21 +00:00
|
|
|
|
frag = DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* update the mix position, taking wrap-around into acount */
|
|
|
|
|
device->mixpos = writepos + frag;
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->mixpos %= device->buflen;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
2007-06-13 15:05:49 +00: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 19:15:06 +00:00
|
|
|
|
}
|
2007-06-13 15:05:49 +00: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 */
|
|
|
|
|
if(device->hwbuf == NULL){
|
|
|
|
|
DSOUND_WaveQueue(device,TRUE);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2007-06-13 15:05:49 +00: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 03:39:13 +00:00
|
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
2007-06-13 15:05:49 +00: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 08:38:04 +00:00
|
|
|
|
TRACE("restarting primary buffer\n");
|
2007-06-13 15:05:49 +00: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 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-06-13 15:05:49 +00: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 19:15:06 +00:00
|
|
|
|
} else {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* update the wave queue if using wave system */
|
2007-07-29 19:27:41 +00:00
|
|
|
|
if(device->hwbuf == NULL)
|
2007-06-13 15:05:49 +00:00
|
|
|
|
DSOUND_WaveQueue(device, TRUE);
|
2007-07-29 19:27:41 +00:00
|
|
|
|
else
|
|
|
|
|
/* Keep alsa happy, which needs GetPosition called once every 10 ms */
|
|
|
|
|
IDsDriverBuffer_GetPosition(device->hwbuf, NULL, NULL);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
|
/* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->state == STATE_STARTING) {
|
|
|
|
|
if (DSOUND_PrimaryPlay(device) != DS_OK)
|
2003-05-22 03:39:13 +00:00
|
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
|
else
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->state = STATE_PLAYING;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
2005-05-31 09:31:37 +00:00
|
|
|
|
else if (device->state == STATE_STOPPING) {
|
|
|
|
|
if (DSOUND_PrimaryStop(device) != DS_OK)
|
2003-05-22 03:39:13 +00:00
|
|
|
|
WARN("DSOUND_PrimaryStop failed\n");
|
|
|
|
|
else
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->state = STATE_STOPPED;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
|
/* **** */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-11-12 13:39:11 +00:00
|
|
|
|
void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
|
|
|
|
|
DWORD_PTR dw1, DWORD_PTR dw2)
|
2002-06-13 19:15:06 +00:00
|
|
|
|
{
|
2005-05-31 09:31:37 +00:00
|
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2004-09-07 19:32:21 +00:00
|
|
|
|
DWORD start_time = GetTickCount();
|
|
|
|
|
DWORD end_time;
|
2003-06-27 22:22:15 +00:00
|
|
|
|
TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("entering at %d\n", start_time);
|
2003-06-27 22:22:15 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (DSOUND_renderer[device->drvdesc.dnDevNode] != device) {
|
2002-06-13 19:15:06 +00:00
|
|
|
|
ERR("dsound died without killing us?\n");
|
|
|
|
|
timeKillEvent(timerID);
|
|
|
|
|
timeEndPeriod(DS_TIME_RES);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
|
2003-06-27 22:22:15 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
if (device->ref)
|
|
|
|
|
DSOUND_PerformMix(device);
|
2003-06-27 22:22:15 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
RtlReleaseResource(&(device->buffer_list_lock));
|
2003-06-27 22:22:15 +00:00
|
|
|
|
|
2004-09-07 19:32:21 +00:00
|
|
|
|
end_time = GetTickCount();
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
|
|
|
|
|
{
|
2005-05-31 09:31:37 +00:00
|
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2006-11-12 13:40:35 +00:00
|
|
|
|
TRACE("(%p,%x,%x,%x,%x)\n",hwo,msg,dwUser,dw1,dw2);
|
|
|
|
|
TRACE("entering at %d, msg=%08x(%s)\n", GetTickCount(), msg,
|
2003-03-15 00:54:11 +00: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 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* check if packet completed from wave driver */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
if (msg == MM_WOM_DONE) {
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
/* **** */
|
2005-05-31 09:31:37 +00:00
|
|
|
|
EnterCriticalSection(&(device->mixlock));
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
|
|
TRACE("done playing primary pos=%d\n", device->pwplay * device->fraglen);
|
|
|
|
|
|
|
|
|
|
/* update playpos */
|
|
|
|
|
device->pwplay++;
|
|
|
|
|
device->pwplay %= DS_HEL_FRAGS;
|
|
|
|
|
|
|
|
|
|
/* sanity */
|
|
|
|
|
if(device->pwqueue == 0){
|
|
|
|
|
ERR("Wave queue corrupted!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* update queue */
|
2005-05-31 09:31:37 +00:00
|
|
|
|
device->pwqueue--;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
2007-06-13 15:05:49 +00:00
|
|
|
|
/* **** */
|
2002-06-13 19:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
TRACE("completed\n");
|
|
|
|
|
}
|