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
|
2015-03-11 11:30:29 +01:00
|
|
|
|
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 */
|
|
|
|
|
|
|
|
/* FIXME: use calculated vol and pan ampfactors */
|
|
|
|
temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
|
2014-12-27 22:32:24 +01:00
|
|
|
volpan->dwTotalAmpFactor[0] = (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));
|
2014-12-27 22:32:24 +01:00
|
|
|
volpan->dwTotalAmpFactor[1] = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2014-12-27 22:32:24 +01:00
|
|
|
TRACE("left = %x, right = %x\n", volpan->dwTotalAmpFactor[0], volpan->dwTotalAmpFactor[1]);
|
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);
|
|
|
|
|
2014-12-27 22:32:24 +01:00
|
|
|
TRACE("left=%x, right=%x\n",volpan->dwTotalAmpFactor[0],volpan->dwTotalAmpFactor[1]);
|
|
|
|
if (volpan->dwTotalAmpFactor[0]==0)
|
2004-07-23 21:06:31 +02:00
|
|
|
left=-10000;
|
|
|
|
else
|
2014-12-27 22:32:24 +01:00
|
|
|
left=600 * log(((double)volpan->dwTotalAmpFactor[0]) / 0xffff) / log(2);
|
|
|
|
if (volpan->dwTotalAmpFactor[1]==0)
|
2004-07-23 21:06:31 +02:00
|
|
|
right=-10000;
|
|
|
|
else
|
2014-12-27 22:32:24 +01:00
|
|
|
right=600 * log(((double)volpan->dwTotalAmpFactor[1]) / 0xffff) / log(2);
|
2004-07-23 21:06:31 +02:00
|
|
|
if (left<right)
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=right;
|
2004-07-23 21:06:31 +02:00
|
|
|
else
|
2004-08-02 20:47:09 +02:00
|
|
|
volpan->lVolume=left;
|
2004-07-23 21:06:31 +02:00
|
|
|
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;
|
2014-12-27 23:41:07 +01:00
|
|
|
dsb->freqAdjustNum = dsb->freq;
|
|
|
|
dsb->freqAdjustDen = dsb->device->pwfx->nSamplesPerSec;
|
2011-01-20 16:41:04 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-12-27 23:41:07 +01:00
|
|
|
if (dsb->freqAdjustNum / dsb->freqAdjustDen > 0) {
|
2012-05-06 20:06:13 +02:00
|
|
|
/**
|
|
|
|
* Yes, round it a bit to make sure that the
|
|
|
|
* linear interpolation factor never changes.
|
|
|
|
*/
|
2014-12-27 23:41:07 +01:00
|
|
|
dsb->firstep = fir_step * dsb->freqAdjustDen / dsb->freqAdjustNum;
|
2012-05-06 20:06:13 +02:00
|
|
|
} 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
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
dsb->freqAccNum = 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;
|
2015-01-06 20:27:10 +01:00
|
|
|
|
|
|
|
if (ochannels == 2)
|
|
|
|
dsb->put = put_mono2stereo;
|
|
|
|
else if (ochannels == 4)
|
|
|
|
dsb->put = put_mono2quad;
|
2015-01-06 20:27:21 +01:00
|
|
|
else if (ochannels == 6)
|
|
|
|
dsb->put = put_mono2surround51;
|
2011-12-12 21:07:49 +01:00
|
|
|
}
|
|
|
|
else if (ochannels == 1)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 1;
|
|
|
|
dsb->get = get_mono;
|
|
|
|
}
|
2015-01-06 20:27:10 +01:00
|
|
|
else if (ichannels == 2 && ochannels == 4)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 2;
|
|
|
|
dsb->put = put_stereo2quad;
|
|
|
|
}
|
2015-01-06 20:27:21 +01:00
|
|
|
else if (ichannels == 2 && ochannels == 6)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 2;
|
|
|
|
dsb->put = put_stereo2surround51;
|
|
|
|
}
|
2016-05-19 16:11:13 +02:00
|
|
|
else if (ichannels == 6 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 6;
|
|
|
|
dsb->put = put_surround512stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2019-04-23 20:04:23 +02:00
|
|
|
else if (ichannels == 8 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 8;
|
|
|
|
dsb->put = put_surround712stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2016-05-19 16:11:16 +02:00
|
|
|
else if (ichannels == 4 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 4;
|
|
|
|
dsb->put = put_quad2stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2011-12-12 21:07:49 +01:00
|
|
|
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
|
|
|
{
|
2014-12-03 21:06:38 +01:00
|
|
|
int first, left, right, check;
|
|
|
|
|
|
|
|
if(dsb->nrofnotifies == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(dsb->state == STATE_STOPPED){
|
|
|
|
TRACE("Stopped...\n");
|
|
|
|
/* DSBPN_OFFSETSTOP notifies are always at the start of the sorted array */
|
|
|
|
for(left = 0; left < dsb->nrofnotifies; ++left){
|
|
|
|
if(dsb->notifies[left].dwOffset != DSBPN_OFFSETSTOP)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TRACE("Signalling %p\n", dsb->notifies[left].hEventNotify);
|
|
|
|
SetEvent(dsb->notifies[left].hEventNotify);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(first = 0; first < dsb->nrofnotifies && dsb->notifies[first].dwOffset == DSBPN_OFFSETSTOP; ++first)
|
|
|
|
;
|
|
|
|
|
|
|
|
if(first == dsb->nrofnotifies)
|
|
|
|
return;
|
|
|
|
|
|
|
|
check = left = first;
|
|
|
|
right = dsb->nrofnotifies - 1;
|
|
|
|
|
|
|
|
/* find leftmost notify that is greater than playpos */
|
|
|
|
while(left != right){
|
|
|
|
check = left + (right - left) / 2;
|
|
|
|
if(dsb->notifies[check].dwOffset < playpos)
|
|
|
|
left = check + 1;
|
|
|
|
else if(dsb->notifies[check].dwOffset > playpos)
|
|
|
|
right = check;
|
|
|
|
else{
|
|
|
|
left = check;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 16:10:46 +01:00
|
|
|
TRACE("Not stopped: first notify: %u (%u), left notify: %u (%u), range: [%u,%u)\n",
|
|
|
|
first, dsb->notifies[first].dwOffset,
|
|
|
|
left, dsb->notifies[left].dwOffset,
|
|
|
|
playpos, (playpos + len) % dsb->buflen);
|
2014-12-03 21:06:38 +01:00
|
|
|
|
|
|
|
/* send notifications in range */
|
2014-12-19 16:10:46 +01:00
|
|
|
if(dsb->notifies[left].dwOffset >= playpos){
|
|
|
|
for(check = left; check < dsb->nrofnotifies; ++check){
|
|
|
|
if(dsb->notifies[check].dwOffset >= playpos + len)
|
|
|
|
break;
|
2014-12-03 21:06:38 +01:00
|
|
|
|
2014-12-19 16:10:46 +01:00
|
|
|
TRACE("Signalling %p (%u)\n", dsb->notifies[check].hEventNotify, dsb->notifies[check].dwOffset);
|
|
|
|
SetEvent(dsb->notifies[check].hEventNotify);
|
|
|
|
}
|
2014-12-03 21:06:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(playpos + len > dsb->buflen){
|
|
|
|
for(check = first; check < left; ++check){
|
|
|
|
if(dsb->notifies[check].dwOffset >= (playpos + len) % dsb->buflen)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TRACE("Signalling %p (%u)\n", dsb->notifies[check].hEventNotify, dsb->notifies[check].dwOffset);
|
|
|
|
SetEvent(dsb->notifies[check].hEventNotify);
|
|
|
|
}
|
|
|
|
}
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
static UINT cp_fields_resample(IDirectSoundBufferImpl *dsb, UINT count, LONG64 *freqAccNum)
|
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
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
LONG64 freqAcc_start = *freqAccNum;
|
|
|
|
LONG64 freqAcc_end = freqAcc_start + count * dsb->freqAdjustNum;
|
2012-05-06 20:06:13 +02:00
|
|
|
UINT dsbfirstep = dsb->firstep;
|
|
|
|
UINT channels = dsb->mix_channels;
|
2014-12-27 23:41:07 +01:00
|
|
|
UINT max_ipos = (freqAcc_start + count * dsb->freqAdjustNum) / dsb->freqAdjustDen;
|
2012-05-06 20:06:13 +02:00
|
|
|
|
|
|
|
UINT fir_cachesize = (fir_len + dsbfirstep - 2) / dsbfirstep;
|
|
|
|
UINT required_input = max_ipos + fir_cachesize;
|
2016-05-17 20:41:04 +02:00
|
|
|
float *intermediate, *fir_copy, *itmp;
|
|
|
|
|
|
|
|
DWORD len = required_input * channels;
|
|
|
|
len += fir_cachesize;
|
|
|
|
len *= sizeof(float);
|
|
|
|
|
|
|
|
if (!dsb->device->cp_buffer) {
|
|
|
|
dsb->device->cp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
dsb->device->cp_buffer_len = len;
|
|
|
|
} else if (len > dsb->device->cp_buffer_len) {
|
|
|
|
dsb->device->cp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->cp_buffer, len);
|
|
|
|
dsb->device->cp_buffer_len = len;
|
|
|
|
}
|
2012-05-06 20:06:13 +02:00
|
|
|
|
2016-05-17 20:41:04 +02:00
|
|
|
fir_copy = dsb->device->cp_buffer;
|
|
|
|
intermediate = fir_copy + fir_cachesize;
|
2012-05-06 20:06:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Important: this buffer MUST be non-interleaved
|
|
|
|
* if you want -msse3 to have any effect.
|
|
|
|
* This is good for CPU cache effects, too.
|
|
|
|
*/
|
2016-05-17 20:41:04 +02:00
|
|
|
itmp = intermediate;
|
2012-05-06 20:06:13 +02:00
|
|
|
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) {
|
2014-12-27 23:41:07 +01:00
|
|
|
UINT int_fir_steps = (freqAcc_start + i * dsb->freqAdjustNum) * dsbfirstep / dsb->freqAdjustDen;
|
|
|
|
float total_fir_steps = (freqAcc_start + i * dsb->freqAdjustNum) * dsbfirstep / (float)dsb->freqAdjustDen;
|
2012-05-06 20:06:13 +02:00
|
|
|
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
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
*freqAccNum = freqAcc_end % dsb->freqAdjustDen;
|
2012-05-06 20:06:13 +02:00
|
|
|
|
|
|
|
return max_ipos;
|
|
|
|
}
|
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
static void cp_fields(IDirectSoundBufferImpl *dsb, UINT count, LONG64 *freqAccNum)
|
2012-05-06 20:06:13 +02:00
|
|
|
{
|
|
|
|
DWORD ipos, adv;
|
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
if (dsb->freqAdjustNum == dsb->freqAdjustDen)
|
|
|
|
adv = cp_fields_noresample(dsb, count); /* *freqAccNum is unmodified */
|
2012-05-06 20:06:13 +02:00
|
|
|
else
|
2014-12-27 23:41:07 +01:00
|
|
|
adv = cp_fields_resample(dsb, count, freqAccNum);
|
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;
|
2015-03-11 00:51:35 +01:00
|
|
|
HRESULT hr;
|
|
|
|
int i;
|
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
|
|
|
}
|
2016-12-19 22:17:23 +01:00
|
|
|
if(dsb->put_aux == putieee32_sum)
|
|
|
|
memset(dsb->device->tmp_buffer, 0, dsb->device->tmp_buffer_len);
|
2002-06-13 21:15:06 +02:00
|
|
|
|
2014-12-27 23:41:07 +01:00
|
|
|
cp_fields(dsb, frames, &dsb->freqAccNum);
|
2015-03-11 00:51:35 +01:00
|
|
|
|
|
|
|
if (size_bytes > 0) {
|
|
|
|
for (i = 0; i < dsb->num_filters; i++) {
|
|
|
|
if (dsb->filters[i].inplace) {
|
|
|
|
hr = IMediaObjectInPlace_Process(dsb->filters[i].inplace, size_bytes, (BYTE*)dsb->device->tmp_buffer, 0, DMO_INPLACE_NORMAL);
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
WARN("IMediaObjectInPlace_Process failed for filter %u\n", i);
|
|
|
|
} else
|
|
|
|
WARN("filter %u has no inplace object - unsupported\n", i);
|
|
|
|
}
|
|
|
|
}
|
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;
|
2014-12-27 22:32:24 +01:00
|
|
|
float vols[DS_MAX_CHANNELS];
|
2012-05-15 15:29:34 +02:00
|
|
|
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);
|
2014-12-27 22:32:24 +01:00
|
|
|
TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalAmpFactor[0],
|
|
|
|
dsb->volpan.dwTotalAmpFactor[1]);
|
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
|
|
|
|
2014-12-27 22:32:24 +01:00
|
|
|
if (channels > DS_MAX_CHANNELS)
|
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
|
|
|
}
|
|
|
|
|
2014-12-27 22:32:24 +01:00
|
|
|
for (i = 0; i < channels; ++i)
|
|
|
|
vols[i] = dsb->volpan.dwTotalAmpFactor[i] / ((float)0xFFFF);
|
|
|
|
|
2012-05-15 15:29:34 +02:00
|
|
|
for(i = 0; i < frames; ++i){
|
|
|
|
for(chan = 0; chan < channels; ++chan){
|
2014-12-27 22:32:24 +01:00
|
|
|
dsb->device->tmp_buffer[i * channels + chan] *= vols[chan];
|
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
|
|
|
|
* fraglen = number of bytes to mix
|
|
|
|
*/
|
2017-03-01 17:00:59 +01:00
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, float *mix_buffer, DWORD frames)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-05-15 15:29:34 +02:00
|
|
|
float *ibuf;
|
2012-05-14 22:20:02 +02:00
|
|
|
DWORD oldpos;
|
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);
|
2017-03-01 17:00:59 +01:00
|
|
|
TRACE("(%p, frames=%d)\n",dsb,frames);
|
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
|
|
|
|
2016-05-12 15:43:44 +02:00
|
|
|
mixieee32(ibuf, 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
|
|
|
}
|
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
return frames;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2017-03-01 17:00:59 +01:00
|
|
|
* frames = the maximum number of frames in the primary buffer to mix, from the
|
2007-02-21 20:16:22 +01:00
|
|
|
* current writepos.
|
|
|
|
*
|
2017-03-01 17:00:59 +01:00
|
|
|
* Returns: the number of frames beyond the writepos that were mixed.
|
2007-02-21 20:16:22 +01:00
|
|
|
*/
|
2017-03-01 17:00:59 +01:00
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, float *mix_buffer, DWORD frames)
|
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
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
TRACE("(%p, frames=%d)\n",dsb,frames);
|
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) {
|
2017-03-01 17:00:59 +01:00
|
|
|
if (frames > 2 * dsb->device->frag_frames) {
|
|
|
|
primary_done = frames - 2 * dsb->device->frag_frames;
|
|
|
|
frames = 2 * dsb->device->frag_frames;
|
|
|
|
dsb->sec_mixpos += primary_done *
|
2014-12-27 23:41:07 +01:00
|
|
|
dsb->pwfx->nBlockAlign * dsb->freqAdjustNum / dsb->freqAdjustDen;
|
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
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
TRACE("frames (primary) = %i\n", frames);
|
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
|
|
|
|
*/
|
2017-03-01 17:00:59 +01:00
|
|
|
primary_done += DSOUND_MixInBuffer(dsb, mix_buffer, frames);
|
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.
|
|
|
|
*
|
2017-03-01 17:00:59 +01:00
|
|
|
* frames = the maximum amount to mix into the primary buffer
|
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
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
static void DSOUND_MixToPrimary(const DirectSoundDevice *device, float *mix_buffer, DWORD frames, 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;
|
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
TRACE("(frames %d)\n", frames);
|
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) {
|
2017-03-01 17:00:59 +01:00
|
|
|
TRACE("Checking %p, frames=%d\n", dsb, frames);
|
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 */
|
2017-03-01 17:00:59 +01:00
|
|
|
DSOUND_MixOne(dsb, mix_buffer, frames);
|
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
|
|
|
|
*/
|
|
|
|
|
2016-05-12 15:43:23 +02:00
|
|
|
static void DSOUND_WaveQueue(DirectSoundDevice *device, LPBYTE pos, DWORD bytes)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
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
|
|
|
|
2016-05-12 15:43:23 +02:00
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, bytes / device->pwfx->nBlockAlign, &buffer);
|
2011-09-27 15:51:07 +02:00
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("GetBuffer failed: %08x\n", hr);
|
2016-05-16 16:55:26 +02:00
|
|
|
return;
|
2011-09-27 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 15:43:23 +02:00
|
|
|
memcpy(buffer, pos, bytes);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2016-05-12 15:43:23 +02:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, bytes / device->pwfx->nBlockAlign, 0);
|
2016-05-16 16:55:26 +02:00
|
|
|
if(FAILED(hr)) {
|
|
|
|
ERR("ReleaseBuffer failed: %08x\n", hr);
|
|
|
|
IAudioRenderClient_ReleaseBuffer(device->render, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
2011-09-27 15:51:07 +02:00
|
|
|
|
2016-05-12 15:43:23 +02:00
|
|
|
device->pad += 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)
|
2016-05-12 15:43:44 +02:00
|
|
|
* =[Reformat]=> device->buffer (device format, skipped on float)
|
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
|
|
|
{
|
2017-03-01 17:00:59 +01:00
|
|
|
DWORD block, pad_frames, pad_bytes, frames;
|
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);
|
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
hr = IAudioClient_GetCurrentPadding(device->client, &pad_frames);
|
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;
|
|
|
|
}
|
2016-05-12 15:43:23 +02:00
|
|
|
block = device->pwfx->nBlockAlign;
|
2017-03-01 17:00:59 +01:00
|
|
|
pad_bytes = pad_frames * block;
|
|
|
|
device->playpos += device->pad - pad_bytes;
|
2016-05-12 15:43:23 +02:00
|
|
|
device->playpos %= device->buflen;
|
2017-03-01 17:00:59 +01:00
|
|
|
device->pad = pad_bytes;
|
2016-05-12 15:43:23 +02:00
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
frames = device->ac_frames - pad_frames;
|
|
|
|
if(!frames){
|
2016-05-12 15:43:23 +02:00
|
|
|
/* nothing to do! */
|
|
|
|
LeaveCriticalSection(&device->mixlock);
|
|
|
|
return;
|
2011-09-27 15:51:07 +02:00
|
|
|
}
|
2017-03-01 17:00:59 +01:00
|
|
|
if (frames > device->frag_frames * 3)
|
|
|
|
frames = device->frag_frames * 3;
|
2016-05-12 15:43:23 +02:00
|
|
|
|
2005-05-31 11:31:37 +02:00
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
2016-05-12 15:43:23 +02:00
|
|
|
BOOL all_stopped = FALSE;
|
2007-06-13 17:05:49 +02:00
|
|
|
int nfiller;
|
2016-05-12 15:43:44 +02:00
|
|
|
void *buffer = NULL;
|
2007-06-13 17:05:49 +02:00
|
|
|
|
|
|
|
/* the sound of silence */
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
|
|
|
|
2008-09-04 12:07:37 +02:00
|
|
|
/* check for underrun. underrun occurs when the write position passes the mix position
|
|
|
|
* also wipe out just-played sound data */
|
2017-03-01 17:00:59 +01:00
|
|
|
if (!pad_frames)
|
2016-05-12 15:43:23 +02:00
|
|
|
WARN("Probable buffer underrun\n");
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2018-05-08 09:55:11 +02:00
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, frames, (BYTE **)&buffer);
|
2016-05-12 15:43:44 +02:00
|
|
|
if(FAILED(hr)){
|
|
|
|
WARN("GetBuffer failed: %08x\n", hr);
|
|
|
|
LeaveCriticalSection(&device->mixlock);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-14 22:20:02 +02:00
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
memset(buffer, nfiller, frames * block);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2016-05-12 15:43:44 +02:00
|
|
|
if (!device->normfunction)
|
2017-03-01 17:00:59 +01:00
|
|
|
DSOUND_MixToPrimary(device, buffer, frames, &all_stopped);
|
2016-05-12 15:43:44 +02:00
|
|
|
else {
|
|
|
|
memset(device->buffer, nfiller, device->buflen);
|
2007-11-07 20:32:22 +01:00
|
|
|
|
2016-05-12 15:43:44 +02:00
|
|
|
/* do the mixing */
|
2017-03-01 17:00:59 +01:00
|
|
|
DSOUND_MixToPrimary(device, (float*)device->buffer, frames, &all_stopped);
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2017-03-01 17:01:00 +01:00
|
|
|
device->normfunction(device->buffer, buffer, frames * device->pwfx->nChannels);
|
2016-05-12 15:43:23 +02:00
|
|
|
}
|
2007-06-13 17:05:49 +02:00
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, frames, 0);
|
2016-05-12 15:43:44 +02:00
|
|
|
if(FAILED(hr))
|
|
|
|
ERR("ReleaseBuffer failed: %08x\n", hr);
|
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
device->pad += frames * block;
|
2016-05-17 20:40:51 +02:00
|
|
|
} else if (!device->stopped) {
|
2017-03-01 17:00:59 +01:00
|
|
|
DWORD writepos = (device->playpos + pad_bytes) % device->buflen;
|
|
|
|
DWORD bytes = frames * block;
|
2017-03-01 17:00:58 +01:00
|
|
|
|
2017-03-01 17:00:59 +01:00
|
|
|
if (bytes > device->buflen)
|
|
|
|
bytes = device->buflen;
|
|
|
|
if (writepos + bytes > device->buflen) {
|
2016-05-12 15:43:23 +02:00
|
|
|
DSOUND_WaveQueue(device, device->buffer + writepos, device->buflen - writepos);
|
2017-03-01 17:00:59 +01:00
|
|
|
DSOUND_WaveQueue(device, device->buffer, writepos + bytes - device->buflen);
|
2016-05-12 15:43:23 +02:00
|
|
|
} else
|
2017-03-01 17:00:59 +01:00
|
|
|
DSOUND_WaveQueue(device, device->buffer + writepos, bytes);
|
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
|
|
|
}
|
|
|
|
|
2012-12-19 10:30:04 +01:00
|
|
|
DWORD CALLBACK DSOUND_mixthread(void *p)
|
2002-06-13 21:15:06 +02:00
|
|
|
{
|
2012-12-19 10:30:04 +01:00
|
|
|
DirectSoundDevice *dev = p;
|
|
|
|
TRACE("(%p)\n", dev);
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2012-12-19 10:30:04 +01:00
|
|
|
while (dev->ref) {
|
|
|
|
DWORD ret;
|
2003-06-28 00:22:15 +02:00
|
|
|
|
2012-12-19 10:30:04 +01:00
|
|
|
/*
|
|
|
|
* Some audio drivers are retarded and won't fire after being
|
|
|
|
* stopped, add a timeout to handle this.
|
|
|
|
*/
|
|
|
|
ret = WaitForSingleObject(dev->sleepev, dev->sleeptime);
|
|
|
|
if (ret == WAIT_FAILED)
|
|
|
|
WARN("wait returned error %u %08x!\n", GetLastError(), GetLastError());
|
|
|
|
else if (ret != WAIT_OBJECT_0)
|
|
|
|
WARN("wait returned %08x!\n", ret);
|
|
|
|
if (!dev->ref)
|
|
|
|
break;
|
|
|
|
|
|
|
|
RtlAcquireResourceShared(&(dev->buffer_list_lock), TRUE);
|
|
|
|
DSOUND_PerformMix(dev);
|
|
|
|
RtlReleaseResource(&(dev->buffer_list_lock));
|
|
|
|
}
|
|
|
|
return 0;
|
2002-06-13 21:15:06 +02:00
|
|
|
}
|