2002-06-13 21:15:06 +02:00
|
|
|
/* DirectSound
|
|
|
|
*
|
|
|
|
* Copyright 1998 Marcus Meissner
|
|
|
|
* Copyright 1998 Rob Riggs
|
|
|
|
* Copyright 2000-2002 TransGaming Technologies, Inc.
|
2007-06-13 17:05:49 +02:00
|
|
|
* Copyright 2007 Peter Dons Tychsen
|
2007-07-29 23:47:01 +02:00
|
|
|
* Copyright 2007 Maarten Lankhorst
|
2011-01-20 16:41:04 +01:00
|
|
|
* Copyright 2011 Owen Rudge for CodeWeavers
|
2002-06-13 21:15:06 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-06-13 21:15:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2002-06-13 21:15:06 +02:00
|
|
|
#include <math.h> /* Insomnia - pow() function */
|
|
|
|
|
2011-09-27 15:51:07 +02:00
|
|
|
#define COBJMACROS
|
2005-01-24 14:31:27 +01:00
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
#define NONAMELESSUNION
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "mmsystem.h"
|
2011-01-20 16:41:04 +01:00
|
|
|
#include "wingdi.h"
|
|
|
|
#include "mmreg.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "dsound.h"
|
2011-01-20 16:41:04 +01:00
|
|
|
#include "ks.h"
|
|
|
|
#include "ksmedia.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
#include "dsound_private.h"
|
2012-05-06 20:06:13 +02:00
|
|
|
#include "fir.h"
|
2002-06-13 21:15:06 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dsound);
|
|
|
|
|
|
|
|
void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
{
|
|
|
|
double temp;
|
2003-06-28 00:22:15 +02:00
|
|
|
TRACE("(%p)\n",volpan);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2002-06-13 21:15:06 +02:00
|
|
|
/* the AmpFactors are expressed in 16.16 fixed point */
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
/* FIXME: dwPan{Left|Right}AmpFactor */
|
|
|
|
|
|
|
|
/* FIXME: use calculated vol and pan ampfactors */
|
|
|
|
temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
|
2003-09-19 02:10:51 +02:00
|
|
|
volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("left = %x, right = %x\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2004-07-23 21:06:31 +02:00
|
|
|
void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
|
|
|
|
{
|
|
|
|
double left,right;
|
|
|
|
TRACE("(%p)\n",volpan);
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("left=%x, right=%x\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
|
2004-07-23 21:06:31 +02:00
|
|
|
if (volpan->dwTotalLeftAmpFactor==0)
|
|
|
|
left=-10000;
|
|
|
|
else
|
|
|
|
left=600 * log(((double)volpan->dwTotalLeftAmpFactor) / 0xffff) / log(2);
|
|
|
|
if (volpan->dwTotalRightAmpFactor==0)
|
|
|
|
right=-10000;
|
|
|
|
else
|
|
|
|
right=600 * log(((double)volpan->dwTotalRightAmpFactor) / 0xffff) / log(2);
|
|
|
|
if (left<right)
|
|
|
|
{
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=right;
|
2004-07-23 21:06:31 +02:00
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalRightAmpFactor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=left;
|
2004-07-23 21:06:31 +02:00
|
|
|
volpan->dwVolAmpFactor=volpan->dwTotalLeftAmpFactor;
|
|
|
|
}
|
|
|
|
if (volpan->lVolume < -10000)
|
|
|
|
volpan->lVolume=-10000;
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lPan=right-left;
|
2004-07-23 21:06:31 +02:00
|
|
|
if (volpan->lPan < -10000)
|
|
|
|
volpan->lPan=-10000;
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
|
2004-07-23 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/**
|
|
|
|
* Recalculate the size for temporary buffer, and new writelead
|
|
|
|
* Should be called when one of the following things occur:
|
|
|
|
* - Primary buffer format is changed
|
|
|
|
* - This buffer format (frequency) is changed
|
|
|
|
*/
|
2002-06-13 21:15:06 +02:00
|
|
|
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
|
|
|
|
{
|
2011-12-12 21:07:49 +01:00
|
|
|
DWORD ichannels = dsb->pwfx->nChannels;
|
|
|
|
DWORD ochannels = dsb->device->pwfx->nChannels;
|
2011-01-20 16:41:04 +01:00
|
|
|
WAVEFORMATEXTENSIBLE *pwfxe;
|
|
|
|
BOOL ieee = FALSE;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
TRACE("(%p)\n",dsb);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2011-01-20 16:41:04 +01:00
|
|
|
pwfxe = (WAVEFORMATEXTENSIBLE *) dsb->pwfx;
|
|
|
|
|
|
|
|
if ((pwfxe->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) || ((pwfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
|
|
|
|
&& (IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))))
|
|
|
|
ieee = TRUE;
|
|
|
|
|
2012-05-06 20:06:13 +02:00
|
|
|
/**
|
|
|
|
* Recalculate FIR step and gain.
|
|
|
|
*
|
|
|
|
* firstep says how many points of the FIR exist per one
|
|
|
|
* sample in the secondary buffer. firgain specifies what
|
|
|
|
* to multiply the FIR output by in order to attenuate it correctly.
|
|
|
|
*/
|
|
|
|
if (dsb->freqAdjust > 1.0f) {
|
|
|
|
/**
|
|
|
|
* Yes, round it a bit to make sure that the
|
|
|
|
* linear interpolation factor never changes.
|
|
|
|
*/
|
|
|
|
dsb->firstep = ceil(fir_step / dsb->freqAdjust);
|
|
|
|
} else {
|
|
|
|
dsb->firstep = fir_step;
|
|
|
|
}
|
|
|
|
dsb->firgain = (float)dsb->firstep / fir_step;
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* calculate the 10ms write lead */
|
2005-02-25 20:16:57 +01:00
|
|
|
dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2012-05-01 22:01:32 +02:00
|
|
|
dsb->freqAcc = 0;
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2011-12-12 21:07:49 +01:00
|
|
|
dsb->get_aux = ieee ? getbpp[4] : getbpp[dsb->pwfx->wBitsPerSample/8 - 1];
|
2012-05-15 15:29:34 +02:00
|
|
|
dsb->put_aux = putieee32;
|
2011-12-12 21:07:49 +01:00
|
|
|
|
|
|
|
dsb->get = dsb->get_aux;
|
|
|
|
dsb->put = dsb->put_aux;
|
|
|
|
|
|
|
|
if (ichannels == ochannels)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = ichannels;
|
|
|
|
if (ichannels > 32) {
|
|
|
|
FIXME("Copying %u channels is unsupported, limiting to first 32\n", ichannels);
|
|
|
|
dsb->mix_channels = 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ichannels == 1)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 1;
|
|
|
|
dsb->put = put_mono2stereo;
|
|
|
|
}
|
|
|
|
else if (ochannels == 1)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 1;
|
|
|
|
dsb->get = get_mono;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ichannels > 2)
|
|
|
|
FIXME("Conversion from %u to %u channels is not implemented, falling back to stereo\n", ichannels, ochannels);
|
|
|
|
dsb->mix_channels = 2;
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Check for application callback requests for when the play position
|
|
|
|
* reaches certain points.
|
|
|
|
*
|
|
|
|
* The offsets that will be triggered will be those between the recorded
|
|
|
|
* "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
|
|
|
|
* beyond that position.
|
|
|
|
*/
|
2007-07-29 21:39:46 +02:00
|
|
|
void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
DWORD offset;
|
|
|
|
LPDSBPOSITIONNOTIFY event;
|
2003-09-02 01:58:43 +02:00
|
|
|
TRACE("(%p,%d)\n",dsb,len);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2003-09-02 01:58:43 +02:00
|
|
|
if (dsb->nrofnotifies == 0)
|
2002-06-13 21:15:06 +02:00
|
|
|
return;
|
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
|
2007-07-06 22:57:48 +02:00
|
|
|
dsb, dsb->buflen, playpos, len);
|
2003-09-02 01:58:43 +02:00
|
|
|
for (i = 0; i < dsb->nrofnotifies ; i++) {
|
|
|
|
event = dsb->notifies + i;
|
2002-06-13 21:15:06 +02:00
|
|
|
offset = event->dwOffset;
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("checking %d, position %d, event = %p\n",
|
2002-06-13 21:15:06 +02:00
|
|
|
i, offset, event->hEventNotify);
|
|
|
|
/* DSBPN_OFFSETSTOP has to be the last element. So this is */
|
|
|
|
/* OK. [Inside DirectX, p274] */
|
2008-07-24 13:12:39 +02:00
|
|
|
/* Windows does not seem to enforce this, and some apps rely */
|
|
|
|
/* on that, so we can't stop there. */
|
2002-06-13 21:15:06 +02:00
|
|
|
/* */
|
|
|
|
/* This also means we can't sort the entries by offset, */
|
|
|
|
/* because DSBPN_OFFSETSTOP == -1 */
|
|
|
|
if (offset == DSBPN_OFFSETSTOP) {
|
|
|
|
if (dsb->state == STATE_STOPPED) {
|
|
|
|
SetEvent(event->hEventNotify);
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2008-07-24 13:12:39 +02:00
|
|
|
}
|
|
|
|
continue;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-06 22:57:48 +02:00
|
|
|
if ((playpos + len) >= dsb->buflen) {
|
|
|
|
if ((offset < ((playpos + len) % dsb->buflen)) ||
|
|
|
|
(offset >= playpos)) {
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 21:15:06 +02:00
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-06 22:57:48 +02:00
|
|
|
if ((offset >= playpos) && (offset < (playpos + len))) {
|
2002-10-19 01:48:57 +02:00
|
|
|
TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
|
2002-06-13 21:15:06 +02:00
|
|
|
SetEvent(event->hEventNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 22:01:06 +02:00
|
|
|
static inline float get_current_sample(const IDirectSoundBufferImpl *dsb,
|
|
|
|
DWORD mixpos, DWORD channel)
|
|
|
|
{
|
|
|
|
if (mixpos >= dsb->buflen && !(dsb->playflags & DSBPLAY_LOOPING))
|
|
|
|
return 0.0f;
|
|
|
|
return dsb->get(dsb, mixpos % dsb->buflen, channel);
|
|
|
|
}
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
static UINT cp_fields_noresample(IDirectSoundBufferImpl *dsb, UINT count)
|
2012-05-06 20:06:13 +02:00
|
|
|
{
|
|
|
|
UINT istride = dsb->pwfx->nBlockAlign;
|
2012-05-15 15:29:34 +02:00
|
|
|
UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
|
2012-05-06 20:06:13 +02:00
|
|
|
DWORD channel, i;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
for (channel = 0; channel < dsb->mix_channels; channel++)
|
|
|
|
dsb->put(dsb, i * ostride, channel, get_current_sample(dsb,
|
|
|
|
dsb->sec_mixpos + i * istride, channel));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
static UINT cp_fields_resample(IDirectSoundBufferImpl *dsb, UINT count, float *freqAcc)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-06 20:06:13 +02:00
|
|
|
UINT i, channel;
|
|
|
|
UINT istride = dsb->pwfx->nBlockAlign;
|
2012-05-15 15:29:34 +02:00
|
|
|
UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
|
2011-11-07 17:25:49 +01:00
|
|
|
|
2012-05-06 20:06:13 +02:00
|
|
|
float freqAdjust = dsb->freqAdjust;
|
|
|
|
float freqAcc_start = *freqAcc;
|
|
|
|
float freqAcc_end = freqAcc_start + count * freqAdjust;
|
|
|
|
UINT dsbfirstep = dsb->firstep;
|
|
|
|
UINT channels = dsb->mix_channels;
|
|
|
|
UINT max_ipos = freqAcc_start + count * freqAdjust;
|
|
|
|
|
|
|
|
UINT fir_cachesize = (fir_len + dsbfirstep - 2) / dsbfirstep;
|
|
|
|
UINT required_input = max_ipos + fir_cachesize;
|
|
|
|
|
|
|
|
float* intermediate = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
sizeof(float) * required_input * channels);
|
|
|
|
|
|
|
|
float* fir_copy = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
sizeof(float) * fir_cachesize);
|
|
|
|
|
|
|
|
/* Important: this buffer MUST be non-interleaved
|
|
|
|
* if you want -msse3 to have any effect.
|
|
|
|
* This is good for CPU cache effects, too.
|
|
|
|
*/
|
|
|
|
float* itmp = intermediate;
|
|
|
|
for (channel = 0; channel < channels; channel++)
|
|
|
|
for (i = 0; i < required_input; i++)
|
|
|
|
*(itmp++) = get_current_sample(dsb,
|
|
|
|
dsb->sec_mixpos + i * istride, channel);
|
|
|
|
|
|
|
|
for(i = 0; i < count; ++i) {
|
|
|
|
float total_fir_steps = (freqAcc_start + i * freqAdjust) * dsbfirstep;
|
|
|
|
UINT int_fir_steps = total_fir_steps;
|
|
|
|
UINT ipos = int_fir_steps / dsbfirstep;
|
|
|
|
|
|
|
|
UINT idx = (ipos + 1) * dsbfirstep - int_fir_steps - 1;
|
|
|
|
float rem = int_fir_steps + 1.0 - total_fir_steps;
|
|
|
|
|
|
|
|
int fir_used = 0;
|
|
|
|
while (idx < fir_len - 1) {
|
|
|
|
fir_copy[fir_used++] = fir[idx] * (1.0 - rem) + fir[idx + 1] * rem;
|
|
|
|
idx += dsb->firstep;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(fir_used <= fir_cachesize);
|
|
|
|
assert(ipos + fir_used <= required_input);
|
|
|
|
|
|
|
|
for (channel = 0; channel < dsb->mix_channels; channel++) {
|
|
|
|
int j;
|
|
|
|
float sum = 0.0;
|
|
|
|
float* cache = &intermediate[channel * required_input + ipos];
|
|
|
|
for (j = 0; j < fir_used; j++)
|
|
|
|
sum += fir_copy[j] * cache[j];
|
|
|
|
dsb->put(dsb, i * ostride, channel, sum * dsb->firgain);
|
|
|
|
}
|
2007-11-07 20:31:53 +01:00
|
|
|
}
|
2012-05-01 22:01:32 +02:00
|
|
|
|
2012-05-06 20:06:13 +02:00
|
|
|
freqAcc_end -= (int)freqAcc_end;
|
|
|
|
*freqAcc = freqAcc_end;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, fir_copy);
|
|
|
|
HeapFree(GetProcessHeap(), 0, intermediate);
|
|
|
|
|
|
|
|
return max_ipos;
|
|
|
|
}
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
static void cp_fields(IDirectSoundBufferImpl *dsb, UINT count, float *freqAcc)
|
2012-05-06 20:06:13 +02:00
|
|
|
{
|
|
|
|
DWORD ipos, adv;
|
|
|
|
|
|
|
|
if (dsb->freqAdjust == 1.0)
|
2012-05-15 15:29:34 +02:00
|
|
|
adv = cp_fields_noresample(dsb, count); /* *freqAcc is unmodified */
|
2012-05-06 20:06:13 +02:00
|
|
|
else
|
2012-05-15 15:29:34 +02:00
|
|
|
adv = cp_fields_resample(dsb, count, freqAcc);
|
2012-05-06 20:06:13 +02:00
|
|
|
|
|
|
|
ipos = dsb->sec_mixpos + adv * dsb->pwfx->nBlockAlign;
|
2012-05-01 22:01:32 +02:00
|
|
|
if (ipos >= dsb->buflen) {
|
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING)
|
|
|
|
ipos %= dsb->buflen;
|
|
|
|
else {
|
|
|
|
ipos = 0;
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsb->sec_mixpos = ipos;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
2007-07-29 23:47:01 +02:00
|
|
|
* Calculate the distance between two buffer offsets, taking wraparound
|
|
|
|
* into account.
|
|
|
|
*/
|
|
|
|
static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
|
|
|
|
{
|
|
|
|
/* If these asserts fail, the problem is not here, but in the underlying code */
|
|
|
|
assert(ptr1 < buflen);
|
|
|
|
assert(ptr2 < buflen);
|
|
|
|
if (ptr1 >= ptr2) {
|
|
|
|
return ptr1 - ptr2;
|
|
|
|
} else {
|
|
|
|
return buflen + ptr1 - ptr2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Mix at most the given amount of data into the allocated temporary buffer
|
|
|
|
* of the given secondary buffer, starting from the dsb's first currently
|
|
|
|
* unsampled frame (writepos), translating frequency (pitch), stereo/mono
|
|
|
|
* and bits-per-sample so that it is ideal for the primary buffer.
|
|
|
|
* Doesn't perform any mixing - this is a straight copy/convert operation.
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
|
|
|
* dsb = the secondary buffer
|
2007-07-29 23:47:01 +02:00
|
|
|
* writepos = Starting position of changed buffer
|
|
|
|
* len = number of bytes to resample from writepos
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
2008-03-04 05:18:04 +01:00
|
|
|
* NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
|
2007-02-21 20:16:22 +01:00
|
|
|
*/
|
2012-05-15 15:29:34 +02:00
|
|
|
static void DSOUND_MixToTemporary(IDirectSoundBufferImpl *dsb, DWORD frames)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-15 15:29:34 +02:00
|
|
|
UINT size_bytes = frames * sizeof(float) * dsb->device->pwfx->nChannels;
|
2008-03-04 05:18:04 +01:00
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
if (dsb->device->tmp_buffer_len < size_bytes || !dsb->device->tmp_buffer)
|
2008-03-04 05:18:04 +01:00
|
|
|
{
|
2012-05-15 15:29:34 +02:00
|
|
|
dsb->device->tmp_buffer_len = size_bytes;
|
2008-03-04 05:18:04 +01:00
|
|
|
if (dsb->device->tmp_buffer)
|
2012-05-15 15:29:34 +02:00
|
|
|
dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, size_bytes);
|
2008-03-04 05:18:04 +01:00
|
|
|
else
|
2012-05-15 15:29:34 +02:00
|
|
|
dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, size_bytes);
|
2008-03-04 05:18:04 +01:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
cp_fields(dsb, frames, &dsb->freqAcc);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
static void DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT frames)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2003-05-02 23:23:16 +02:00
|
|
|
INT i;
|
2012-05-15 15:29:34 +02:00
|
|
|
float vLeft, vRight;
|
|
|
|
UINT channels = dsb->device->pwfx->nChannels, chan;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
TRACE("(%p,%d)\n",dsb,frames);
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
|
|
|
|
dsb->volpan.dwTotalRightAmpFactor);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2007-10-13 20:29:40 +02:00
|
|
|
if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
|
|
|
|
(!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
|
|
|
|
!(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
|
2012-05-15 15:29:34 +02:00
|
|
|
return; /* Nothing to do */
|
2007-10-13 20:29:40 +02:00
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
if (channels != 1 && channels != 2)
|
2007-07-29 23:47:01 +02:00
|
|
|
{
|
2012-05-15 15:29:34 +02:00
|
|
|
FIXME("There is no support for %u channels\n", channels);
|
|
|
|
return;
|
2007-07-29 23:47:01 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
vLeft = dsb->volpan.dwTotalLeftAmpFactor / ((float)0xFFFF);
|
|
|
|
vRight = dsb->volpan.dwTotalRightAmpFactor / ((float)0xFFFF);
|
|
|
|
for(i = 0; i < frames; ++i){
|
|
|
|
for(chan = 0; chan < channels; ++chan){
|
|
|
|
if(chan == 0)
|
|
|
|
dsb->device->tmp_buffer[i * channels + chan] *= vLeft;
|
|
|
|
else
|
|
|
|
dsb->device->tmp_buffer[i * channels + chan] *= vRight;
|
2003-05-02 23:23:16 +02:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Mix (at most) the given number of bytes into the given position of the
|
|
|
|
* device buffer, from the secondary buffer "dsb" (starting at the current
|
|
|
|
* mix position for that buffer).
|
|
|
|
*
|
|
|
|
* Returns the number of bytes actually mixed into the device buffer. This
|
|
|
|
* will match fraglen unless the end of the secondary buffer is reached
|
|
|
|
* (and it is not looping).
|
|
|
|
*
|
|
|
|
* dsb = the secondary buffer to mix from
|
|
|
|
* writepos = position (offset) in device buffer to write at
|
|
|
|
* fraglen = number of bytes to mix
|
|
|
|
*/
|
2002-06-13 21:15:06 +02:00
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
|
|
|
|
{
|
2012-05-01 22:01:32 +02:00
|
|
|
INT len = fraglen;
|
2012-05-15 15:29:34 +02:00
|
|
|
float *ibuf;
|
2012-05-14 22:20:02 +02:00
|
|
|
DWORD oldpos;
|
2012-05-15 15:29:34 +02:00
|
|
|
UINT frames = fraglen / dsb->device->pwfx->nBlockAlign;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-01 22:01:32 +02:00
|
|
|
TRACE("sec_mixpos=%d/%d\n", dsb->sec_mixpos, dsb->buflen);
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2006-01-06 12:35:20 +01:00
|
|
|
if (len % dsb->device->pwfx->nBlockAlign) {
|
|
|
|
INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
|
2005-02-25 20:16:57 +01:00
|
|
|
ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
|
2007-06-13 17:05:49 +02:00
|
|
|
len -= len % nBlockAlign; /* data alignment */
|
2005-02-25 20:16:57 +01:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2008-03-04 05:18:04 +01:00
|
|
|
/* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
|
2012-05-01 22:01:32 +02:00
|
|
|
oldpos = dsb->sec_mixpos;
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
DSOUND_MixToTemporary(dsb, frames);
|
2011-11-04 07:02:23 +01:00
|
|
|
ibuf = dsb->device->tmp_buffer;
|
2008-03-04 05:18:04 +01:00
|
|
|
|
2007-07-29 23:47:01 +02:00
|
|
|
/* Apply volume if needed */
|
2012-05-15 15:29:34 +02:00
|
|
|
DSOUND_MixerVol(dsb, frames);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
mixieee32(ibuf, dsb->device->mix_buffer, frames * dsb->device->pwfx->nChannels);
|
2007-07-29 23:47:01 +02:00
|
|
|
|
2007-07-12 13:21:43 +02:00
|
|
|
/* check for notification positions */
|
|
|
|
if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
|
|
|
|
dsb->state != STATE_STARTING) {
|
2012-05-01 22:01:32 +02:00
|
|
|
INT ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
|
2007-07-29 23:47:01 +02:00
|
|
|
DSOUND_CheckEvent(dsb, oldpos, ilen);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Mix some frames from the given secondary buffer "dsb" into the device
|
|
|
|
* primary buffer.
|
|
|
|
*
|
|
|
|
* dsb = the secondary buffer
|
2007-07-29 23:47:01 +02:00
|
|
|
* playpos = the current play position in the device buffer (primary buffer)
|
2007-02-21 20:16:22 +01:00
|
|
|
* writepos = the current safe-to-write position in the device buffer
|
|
|
|
* mixlen = the maximum number of bytes in the primary buffer to mix, from the
|
|
|
|
* current writepos.
|
|
|
|
*
|
|
|
|
* Returns: the number of bytes beyond the writepos that were mixed.
|
|
|
|
*/
|
2007-07-29 21:24:21 +02:00
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-14 22:19:50 +02:00
|
|
|
DWORD primary_done = 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-07-29 21:24:21 +02:00
|
|
|
TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
|
2012-05-14 22:19:50 +02:00
|
|
|
TRACE("writepos=%d, mixlen=%d\n", writepos, mixlen);
|
2012-05-01 22:01:32 +02:00
|
|
|
TRACE("looping=%d, leadin=%d\n", dsb->playflags, dsb->leadin);
|
2007-07-29 23:47:01 +02:00
|
|
|
|
|
|
|
/* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
|
2012-05-14 22:19:50 +02:00
|
|
|
/* FIXME: Is this needed? */
|
|
|
|
if (dsb->leadin && dsb->state == STATE_STARTING) {
|
|
|
|
if (mixlen > 2 * dsb->device->fraglen) {
|
|
|
|
primary_done = mixlen - 2 * dsb->device->fraglen;
|
|
|
|
mixlen = 2 * dsb->device->fraglen;
|
|
|
|
writepos += primary_done;
|
|
|
|
dsb->sec_mixpos += (primary_done / dsb->device->pwfx->nBlockAlign) *
|
|
|
|
dsb->pwfx->nBlockAlign * dsb->freqAdjust;
|
2007-07-29 23:47:01 +02:00
|
|
|
}
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-14 22:19:50 +02:00
|
|
|
dsb->leadin = FALSE;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 22:19:50 +02:00
|
|
|
TRACE("mixlen (primary) = %i\n", mixlen);
|
2007-07-29 23:47:01 +02:00
|
|
|
|
|
|
|
/* First try to mix to the end of the buffer if possible
|
|
|
|
* Theoretically it would allow for better optimization
|
|
|
|
*/
|
2012-05-14 22:19:50 +02:00
|
|
|
primary_done += DSOUND_MixInBuffer(dsb, writepos, mixlen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 22:19:50 +02:00
|
|
|
TRACE("total mixed data=%d\n", primary_done);
|
2003-03-17 22:23:12 +01:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* Report back the total prebuffered amount for this buffer */
|
|
|
|
return primary_done;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* For a DirectSoundDevice, go through all the currently playing buffers and
|
|
|
|
* mix them in to the device buffer.
|
|
|
|
*
|
|
|
|
* writepos = the current safe-to-write position in the primary buffer
|
|
|
|
* mixlen = the maximum amount to mix into the primary buffer
|
|
|
|
* (beyond the current writepos)
|
|
|
|
* recover = true if the sound device may have been reset and the write
|
|
|
|
* position in the device buffer changed
|
2007-06-13 17:05:49 +02:00
|
|
|
* all_stopped = reports back if all buffers have stopped
|
2007-02-21 20:16:22 +01:00
|
|
|
*
|
|
|
|
* Returns: the length beyond the writepos that was mixed to.
|
|
|
|
*/
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 19:31:01 +02:00
|
|
|
static void DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos, DWORD mixlen, BOOL recover, BOOL *all_stopped)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-14 19:31:01 +02:00
|
|
|
INT i;
|
2002-06-13 21:15:06 +02:00
|
|
|
IDirectSoundBufferImpl *dsb;
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* unless we find a running buffer, all have stopped */
|
|
|
|
*all_stopped = TRUE;
|
|
|
|
|
2007-07-29 21:24:21 +02:00
|
|
|
TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
|
2005-05-31 11:31:37 +02:00
|
|
|
for (i = 0; i < device->nrofbuffers; i++) {
|
|
|
|
dsb = device->buffers[i];
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
|
|
|
|
|
2011-09-24 16:02:52 +02:00
|
|
|
if (dsb->buflen && dsb->state) {
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
|
2011-02-11 01:21:05 +01:00
|
|
|
RtlAcquireResourceShared(&dsb->lock, TRUE);
|
2007-06-13 17:05:49 +02:00
|
|
|
/* if buffer is stopping it is stopped now */
|
2002-06-13 21:15:06 +02:00
|
|
|
if (dsb->state == STATE_STOPPING) {
|
|
|
|
dsb->state = STATE_STOPPED;
|
2007-07-06 22:57:48 +02:00
|
|
|
DSOUND_CheckEvent(dsb, 0, 0);
|
2007-07-29 23:47:01 +02:00
|
|
|
} else if (dsb->state != STATE_STOPPED) {
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* if the buffer was starting, it must be playing now */
|
2002-06-13 21:15:06 +02:00
|
|
|
if (dsb->state == STATE_STARTING)
|
|
|
|
dsb->state = STATE_PLAYING;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2008-03-14 11:54:35 +01:00
|
|
|
/* mix next buffer into the main buffer */
|
2012-05-14 19:31:01 +02:00
|
|
|
DSOUND_MixOne(dsb, writepos, mixlen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
*all_stopped = FALSE;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-07-29 21:01:43 +02:00
|
|
|
RtlReleaseResource(&dsb->lock);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/**
|
|
|
|
* Add buffers to the emulated wave device system.
|
|
|
|
*
|
|
|
|
* device = The current dsound playback device
|
|
|
|
* force = If TRUE, the function will buffer up as many frags as possible,
|
|
|
|
* even though and will ignore the actual state of the primary buffer.
|
|
|
|
*
|
|
|
|
* Returns: None
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void DSOUND_WaveQueue(DirectSoundDevice *device, BOOL force)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-14 19:31:10 +02:00
|
|
|
DWORD prebuf_frames, prebuf_bytes, read_offs_bytes;
|
2011-09-27 15:51:07 +02:00
|
|
|
BYTE *buffer;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
TRACE("(%p)\n", device);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
read_offs_bytes = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
TRACE("read_offs_bytes = %u, playing_offs_bytes = %u, in_mmdev_bytes: %u, prebuf = %u\n",
|
|
|
|
read_offs_bytes, device->playing_offs_bytes, device->in_mmdev_bytes, device->prebuf);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-09-06 17:35:58 +02:00
|
|
|
if (!force)
|
|
|
|
{
|
2012-05-14 19:31:10 +02:00
|
|
|
if(device->mixpos < device->playing_offs_bytes)
|
|
|
|
prebuf_bytes = device->mixpos + device->buflen - device->playing_offs_bytes;
|
|
|
|
else
|
|
|
|
prebuf_bytes = device->mixpos - device->playing_offs_bytes;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-09-06 17:35:58 +02:00
|
|
|
else
|
2007-06-13 17:05:49 +02:00
|
|
|
/* buffer the maximum amount of frags */
|
2012-05-14 19:31:10 +02:00
|
|
|
prebuf_bytes = device->prebuf * device->fraglen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* limit to the queue we have left */
|
2012-05-14 19:31:10 +02:00
|
|
|
if(device->in_mmdev_bytes + prebuf_bytes > device->prebuf * device->fraglen)
|
|
|
|
prebuf_bytes = device->prebuf * device->fraglen - device->in_mmdev_bytes;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
TRACE("prebuf_bytes = %u\n", prebuf_bytes);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
if(!prebuf_bytes)
|
2011-09-27 15:51:07 +02:00
|
|
|
return;
|
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
device->in_mmdev_bytes += prebuf_bytes;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
if(prebuf_bytes + read_offs_bytes > device->buflen){
|
|
|
|
DWORD chunk_bytes = device->buflen - read_offs_bytes;
|
|
|
|
prebuf_frames = chunk_bytes / device->pwfx->nBlockAlign;
|
|
|
|
prebuf_bytes -= chunk_bytes;
|
|
|
|
}else{
|
|
|
|
prebuf_frames = prebuf_bytes / device->pwfx->nBlockAlign;
|
|
|
|
prebuf_bytes = 0;
|
|
|
|
}
|
2011-09-27 15:51:07 +02:00
|
|
|
|
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, prebuf_frames, &buffer);
|
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("GetBuffer failed: %08x\n", hr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
memcpy(buffer, device->buffer + read_offs_bytes,
|
2011-09-27 15:51:07 +02:00
|
|
|
prebuf_frames * device->pwfx->nBlockAlign);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2011-09-27 15:51:07 +02:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, prebuf_frames, 0);
|
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("ReleaseBuffer failed: %08x\n", hr);
|
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2011-09-27 15:51:07 +02:00
|
|
|
/* check if anything wrapped */
|
2012-05-14 19:31:10 +02:00
|
|
|
if(prebuf_bytes > 0){
|
|
|
|
prebuf_frames = prebuf_bytes / device->pwfx->nBlockAlign;
|
2011-09-27 15:51:07 +02:00
|
|
|
|
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, prebuf_frames, &buffer);
|
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("GetBuffer failed: %08x\n", hr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, device->buffer, prebuf_frames * device->pwfx->nBlockAlign);
|
|
|
|
|
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, prebuf_frames, 0);
|
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("ReleaseBuffer failed: %08x\n", hr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 19:31:10 +02:00
|
|
|
TRACE("in_mmdev_bytes now = %i\n", device->in_mmdev_bytes);
|
2007-06-13 17:05:49 +02:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-02-21 20:16:22 +01:00
|
|
|
/**
|
|
|
|
* Perform mixing for a Direct Sound device. That is, go through all the
|
|
|
|
* secondary buffers (the sound bites currently playing) and mix them in
|
|
|
|
* to the primary buffer (the device buffer).
|
2012-05-15 15:29:34 +02:00
|
|
|
*
|
|
|
|
* The mixing procedure goes:
|
|
|
|
*
|
|
|
|
* secondary->buffer (secondary format)
|
|
|
|
* =[Resample]=> device->tmp_buffer (float format)
|
|
|
|
* =[Volume]=> device->tmp_buffer (float format)
|
|
|
|
* =[Mix]=> device->mix_buffer (float format)
|
|
|
|
* =[Reformat]=> device->buffer (device format)
|
2007-02-21 20:16:22 +01:00
|
|
|
*/
|
2006-08-02 13:26:14 +02:00
|
|
|
static void DSOUND_PerformMix(DirectSoundDevice *device)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-15 21:41:24 +02:00
|
|
|
UINT32 pad, to_mix_frags, to_mix_bytes;
|
2011-09-27 15:51:07 +02:00
|
|
|
HRESULT hr;
|
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
TRACE("(%p)\n", device);
|
2003-05-02 23:23:16 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* **** */
|
2011-09-27 15:51:07 +02:00
|
|
|
EnterCriticalSection(&device->mixlock);
|
|
|
|
|
2012-05-15 21:41:24 +02:00
|
|
|
hr = IAudioClient_GetCurrentPadding(device->client, &pad);
|
2011-09-27 15:51:07 +02:00
|
|
|
if(FAILED(hr)){
|
2012-05-15 21:41:24 +02:00
|
|
|
WARN("GetCurrentPadding failed: %08x\n", hr);
|
|
|
|
LeaveCriticalSection(&device->mixlock);
|
2011-09-27 15:51:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-15 21:41:24 +02:00
|
|
|
to_mix_frags = device->prebuf - (pad * device->pwfx->nBlockAlign + device->fraglen - 1) / device->fraglen;
|
|
|
|
|
|
|
|
to_mix_bytes = to_mix_frags * device->fraglen;
|
2011-09-27 15:51:07 +02:00
|
|
|
|
2012-05-15 21:41:24 +02:00
|
|
|
if(device->in_mmdev_bytes > 0){
|
|
|
|
DWORD delta_bytes = min(to_mix_bytes, device->in_mmdev_bytes);
|
|
|
|
device->in_mmdev_bytes -= delta_bytes;
|
|
|
|
device->playing_offs_bytes += delta_bytes;
|
2012-05-14 19:31:10 +02:00
|
|
|
device->playing_offs_bytes %= device->buflen;
|
2011-09-27 15:51:07 +02:00
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
2007-06-13 17:05:49 +02:00
|
|
|
BOOL recover = FALSE, all_stopped = FALSE;
|
2012-05-14 22:20:02 +02:00
|
|
|
DWORD playpos, writepos, writelead, maxq, prebuff_max, prebuff_left, size1, size2;
|
2007-06-13 17:05:49 +02:00
|
|
|
LPVOID buf1, buf2;
|
|
|
|
int nfiller;
|
|
|
|
|
|
|
|
/* the sound of silence */
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
|
|
|
|
|
|
|
/* get the position in the primary buffer */
|
|
|
|
if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
return;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
|
2011-09-27 15:51:07 +02:00
|
|
|
playpos,writepos,device->playpos,device->mixpos,device->buflen);
|
2005-05-31 11:31:37 +02:00
|
|
|
assert(device->playpos < device->buflen);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2008-09-04 12:07:37 +02:00
|
|
|
/* calc maximum prebuff */
|
|
|
|
prebuff_max = (device->prebuf * device->fraglen);
|
|
|
|
|
|
|
|
/* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
|
|
|
|
prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
|
|
|
|
writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos);
|
|
|
|
|
|
|
|
/* check for underrun. underrun occurs when the write position passes the mix position
|
|
|
|
* also wipe out just-played sound data */
|
|
|
|
if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
|
|
|
|
if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
|
|
|
|
WARN("Probable buffer underrun\n");
|
|
|
|
else TRACE("Buffer starting or buffer underrun\n");
|
|
|
|
|
|
|
|
/* recover mixing for all buffers */
|
|
|
|
recover = TRUE;
|
|
|
|
|
|
|
|
/* reset mix position to write position */
|
|
|
|
device->mixpos = writepos;
|
|
|
|
|
|
|
|
ZeroMemory(device->buffer, device->buflen);
|
|
|
|
} else if (playpos < device->playpos) {
|
2007-06-13 17:05:49 +02:00
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
buf2 = device->buffer;
|
|
|
|
size1 = device->buflen - device->playpos;
|
|
|
|
size2 = playpos;
|
|
|
|
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);
|
2002-06-13 21:15:06 +02:00
|
|
|
} else {
|
2007-06-13 17:05:49 +02:00
|
|
|
buf1 = device->buffer + device->playpos;
|
|
|
|
buf2 = NULL;
|
|
|
|
size1 = playpos - device->playpos;
|
|
|
|
size2 = 0;
|
|
|
|
FillMemory(buf1, size1, 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
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* find the maximum we can prebuffer from current write position */
|
|
|
|
maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0;
|
|
|
|
|
|
|
|
TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
|
|
|
|
prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead);
|
|
|
|
|
2012-05-14 22:20:02 +02:00
|
|
|
ZeroMemory(device->mix_buffer, device->mix_buffer_len);
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* do the mixing */
|
2012-05-14 19:31:01 +02:00
|
|
|
DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2012-05-14 19:31:01 +02:00
|
|
|
if (maxq + writepos > device->buflen)
|
2007-11-07 20:32:22 +01:00
|
|
|
{
|
|
|
|
DWORD todo = device->buflen - writepos;
|
2012-05-14 22:20:02 +02:00
|
|
|
DWORD offs_float = (todo / device->pwfx->nBlockAlign) * device->pwfx->nChannels;
|
|
|
|
device->normfunction(device->mix_buffer, device->buffer + writepos, todo);
|
|
|
|
device->normfunction(device->mix_buffer + offs_float, device->buffer, maxq - todo);
|
2007-11-07 20:32:22 +01:00
|
|
|
}
|
|
|
|
else
|
2012-05-14 22:20:02 +02:00
|
|
|
device->normfunction(device->mix_buffer, device->buffer + writepos, maxq);
|
2007-11-07 20:32:22 +01:00
|
|
|
|
2008-04-02 22:10:47 +02:00
|
|
|
/* update the mix position, taking wrap-around into account */
|
2012-05-14 19:31:01 +02:00
|
|
|
device->mixpos = writepos + maxq;
|
2005-05-31 11:31:37 +02:00
|
|
|
device->mixpos %= device->buflen;
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2007-06-13 17:05:49 +02:00
|
|
|
/* update prebuff left */
|
|
|
|
prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
|
|
|
|
|
|
|
|
/* check if have a whole fragment */
|
|
|
|
if (prebuff_left >= device->fraglen){
|
|
|
|
|
2011-09-24 16:02:52 +02:00
|
|
|
/* update the wave queue */
|
2011-09-27 15:51:07 +02:00
|
|
|
DSOUND_WaveQueue(device, FALSE);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* buffers are full. start playing if applicable */
|
|
|
|
if(device->state == STATE_STARTING){
|
|
|
|
TRACE("started primary buffer\n");
|
|
|
|
if(DSOUND_PrimaryPlay(device) != DS_OK){
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
2007-06-13 17:05:49 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* we are playing now */
|
|
|
|
device->state = STATE_PLAYING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* buffers are full. start stopping if applicable */
|
|
|
|
if(device->state == STATE_STOPPED){
|
2007-06-26 10:38:04 +02:00
|
|
|
TRACE("restarting primary buffer\n");
|
2007-06-13 17:05:49 +02:00
|
|
|
if(DSOUND_PrimaryPlay(device) != DS_OK){
|
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* start stopping again. as soon as there is no more data, it will stop */
|
|
|
|
device->state = STATE_STOPPING;
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* if device was stopping, its for sure stopped when all buffers have stopped */
|
|
|
|
else if((all_stopped == TRUE) && (device->state == STATE_STOPPING)){
|
|
|
|
TRACE("All buffers have stopped. Stopping primary buffer\n");
|
|
|
|
device->state = STATE_STOPPED;
|
|
|
|
|
|
|
|
/* stop the primary buffer now */
|
|
|
|
DSOUND_PrimaryStop(device);
|
|
|
|
}
|
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
} else {
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2011-09-27 15:51:07 +02:00
|
|
|
DSOUND_WaveQueue(device, TRUE);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2002-06-13 21:15:06 +02:00
|
|
|
/* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->state == STATE_STARTING) {
|
|
|
|
if (DSOUND_PrimaryPlay(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryPlay failed\n");
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
device->state = STATE_PLAYING;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
2005-05-31 11:31:37 +02:00
|
|
|
else if (device->state == STATE_STOPPING) {
|
|
|
|
if (DSOUND_PrimaryStop(device) != DS_OK)
|
2003-05-22 05:39:13 +02:00
|
|
|
WARN("DSOUND_PrimaryStop failed\n");
|
|
|
|
else
|
2005-05-31 11:31:37 +02:00
|
|
|
device->state = STATE_STOPPED;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&(device->mixlock));
|
|
|
|
/* **** */
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2006-11-12 14:39:11 +01:00
|
|
|
void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
|
|
|
|
DWORD_PTR dw1, DWORD_PTR dw2)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2007-07-29 23:47:01 +02:00
|
|
|
DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
|
2004-09-07 21:32:21 +02:00
|
|
|
DWORD start_time = GetTickCount();
|
2007-07-29 23:47:01 +02:00
|
|
|
DWORD end_time;
|
2003-06-28 00:22:15 +02:00
|
|
|
TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
|
2007-07-29 23:47:01 +02:00
|
|
|
TRACE("entering at %d\n", start_time);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->ref)
|
|
|
|
DSOUND_PerformMix(device);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
RtlReleaseResource(&(device->buffer_list_lock));
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2004-09-07 21:32:21 +02:00
|
|
|
end_time = GetTickCount();
|
2006-11-12 14:40:35 +01:00
|
|
|
TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|