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
|
2007-07-29 21:47:01 +00:00
|
|
|
* Copyright 2007 Maarten Lankhorst
|
2011-01-20 15:41:04 +00:00
|
|
|
* Copyright 2011 Owen Rudge for CodeWeavers
|
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 */
|
|
|
|
|
2011-09-27 13:51:07 +00:00
|
|
|
#define COBJMACROS
|
2015-03-11 10:30:29 +00:00
|
|
|
|
2002-06-13 19:15:06 +00:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "mmsystem.h"
|
2011-01-20 15:41:04 +00:00
|
|
|
#include "wingdi.h"
|
|
|
|
#include "mmreg.h"
|
2002-06-13 19:15:06 +00:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "dsound.h"
|
2011-01-20 15:41:04 +00:00
|
|
|
#include "ks.h"
|
|
|
|
#include "ksmedia.h"
|
2002-06-13 19:15:06 +00:00
|
|
|
#include "dsound_private.h"
|
2012-05-06 18:06:13 +00:00
|
|
|
#include "fir.h"
|
2002-06-13 19:15:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Vol=%ld Pan=%ld\n", volpan->lVolume, volpan->lPan);
|
2002-06-13 19:15:06 +00: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 21:32:24 +00:00
|
|
|
volpan->dwTotalAmpFactor[0] = (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));
|
2014-12-27 21:32:24 +00:00
|
|
|
volpan->dwTotalAmpFactor[1] = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("left = %lx, right = %lx\n", volpan->dwTotalAmpFactor[0], volpan->dwTotalAmpFactor[1]);
|
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);
|
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("left=%lx, right=%lx\n",volpan->dwTotalAmpFactor[0],volpan->dwTotalAmpFactor[1]);
|
2014-12-27 21:32:24 +00:00
|
|
|
if (volpan->dwTotalAmpFactor[0]==0)
|
2004-07-23 19:06:31 +00:00
|
|
|
left=-10000;
|
|
|
|
else
|
2014-12-27 21:32:24 +00:00
|
|
|
left=600 * log(((double)volpan->dwTotalAmpFactor[0]) / 0xffff) / log(2);
|
|
|
|
if (volpan->dwTotalAmpFactor[1]==0)
|
2004-07-23 19:06:31 +00:00
|
|
|
right=-10000;
|
|
|
|
else
|
2014-12-27 21:32:24 +00:00
|
|
|
right=600 * log(((double)volpan->dwTotalAmpFactor[1]) / 0xffff) / log(2);
|
2004-07-23 19:06:31 +00:00
|
|
|
if (left<right)
|
2004-08-02 18:47:09 +00:00
|
|
|
volpan->lVolume=right;
|
2004-07-23 19:06:31 +00:00
|
|
|
else
|
2004-08-02 18:47:09 +00:00
|
|
|
volpan->lVolume=left;
|
2004-07-23 19:06:31 +00:00
|
|
|
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;
|
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Vol=%ld Pan=%ld\n", volpan->lVolume, volpan->lPan);
|
2004-07-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
2007-07-29 21:47:01 +00: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 19:15:06 +00:00
|
|
|
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
|
|
|
|
{
|
2011-12-12 20:07:49 +00:00
|
|
|
DWORD ichannels = dsb->pwfx->nChannels;
|
|
|
|
DWORD ochannels = dsb->device->pwfx->nChannels;
|
2023-04-26 04:59:30 +00:00
|
|
|
LONG64 oldFreqAdjustDen = dsb->freqAdjustDen;
|
2011-01-20 15:41:04 +00:00
|
|
|
WAVEFORMATEXTENSIBLE *pwfxe;
|
|
|
|
BOOL ieee = FALSE;
|
2007-07-29 21:47:01 +00:00
|
|
|
|
2003-09-01 23:58:43 +00:00
|
|
|
TRACE("(%p)\n",dsb);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2011-01-20 15:41:04 +00:00
|
|
|
pwfxe = (WAVEFORMATEXTENSIBLE *) dsb->pwfx;
|
2014-12-27 22:41:07 +00:00
|
|
|
dsb->freqAdjustNum = dsb->freq;
|
|
|
|
dsb->freqAdjustDen = dsb->device->pwfx->nSamplesPerSec;
|
2011-01-20 15:41:04 +00: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 18:06:13 +00: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 22:41:07 +00:00
|
|
|
if (dsb->freqAdjustNum / dsb->freqAdjustDen > 0) {
|
2012-05-06 18:06:13 +00:00
|
|
|
/**
|
|
|
|
* Yes, round it a bit to make sure that the
|
|
|
|
* linear interpolation factor never changes.
|
|
|
|
*/
|
2014-12-27 22:41:07 +00:00
|
|
|
dsb->firstep = fir_step * dsb->freqAdjustDen / dsb->freqAdjustNum;
|
2012-05-06 18:06:13 +00:00
|
|
|
} else {
|
|
|
|
dsb->firstep = fir_step;
|
|
|
|
}
|
|
|
|
dsb->firgain = (float)dsb->firstep / fir_step;
|
|
|
|
|
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;
|
2023-04-23 11:05:43 +00:00
|
|
|
dsb->maxwritelead = (DSBFREQUENCY_MAX / 100) * dsb->pwfx->nBlockAlign;
|
2007-07-29 21:47:01 +00:00
|
|
|
|
2023-04-26 04:59:30 +00:00
|
|
|
if (oldFreqAdjustDen)
|
|
|
|
dsb->freqAccNum = (dsb->freqAccNum * dsb->freqAdjustDen + oldFreqAdjustDen / 2) / oldFreqAdjustDen;
|
2007-07-29 21:47:01 +00:00
|
|
|
|
2011-12-12 20:07:49 +00:00
|
|
|
dsb->get_aux = ieee ? getbpp[4] : getbpp[dsb->pwfx->wBitsPerSample/8 - 1];
|
2012-05-15 13:29:34 +00:00
|
|
|
dsb->put_aux = putieee32;
|
2011-12-12 20:07:49 +00:00
|
|
|
|
|
|
|
dsb->get = dsb->get_aux;
|
|
|
|
dsb->put = dsb->put_aux;
|
|
|
|
|
|
|
|
if (ichannels == ochannels)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = ichannels;
|
|
|
|
if (ichannels > 32) {
|
2022-02-10 07:41:37 +00:00
|
|
|
FIXME("Copying %lu channels is unsupported, limiting to first 32\n", ichannels);
|
2011-12-12 20:07:49 +00:00
|
|
|
dsb->mix_channels = 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ichannels == 1)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 1;
|
2015-01-06 19:27:10 +00:00
|
|
|
|
|
|
|
if (ochannels == 2)
|
|
|
|
dsb->put = put_mono2stereo;
|
|
|
|
else if (ochannels == 4)
|
|
|
|
dsb->put = put_mono2quad;
|
2015-01-06 19:27:21 +00:00
|
|
|
else if (ochannels == 6)
|
|
|
|
dsb->put = put_mono2surround51;
|
2011-12-12 20:07:49 +00:00
|
|
|
}
|
|
|
|
else if (ochannels == 1)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 1;
|
|
|
|
dsb->get = get_mono;
|
|
|
|
}
|
2015-01-06 19:27:10 +00:00
|
|
|
else if (ichannels == 2 && ochannels == 4)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 2;
|
|
|
|
dsb->put = put_stereo2quad;
|
|
|
|
}
|
2015-01-06 19:27:21 +00:00
|
|
|
else if (ichannels == 2 && ochannels == 6)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 2;
|
|
|
|
dsb->put = put_stereo2surround51;
|
|
|
|
}
|
2016-05-19 14:11:13 +00:00
|
|
|
else if (ichannels == 6 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 6;
|
|
|
|
dsb->put = put_surround512stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2019-04-23 18:04:23 +00:00
|
|
|
else if (ichannels == 8 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 8;
|
|
|
|
dsb->put = put_surround712stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2016-05-19 14:11:16 +00:00
|
|
|
else if (ichannels == 4 && ochannels == 2)
|
|
|
|
{
|
|
|
|
dsb->mix_channels = 4;
|
|
|
|
dsb->put = put_quad2stereo;
|
|
|
|
dsb->put_aux = putieee32_sum;
|
|
|
|
}
|
2011-12-12 20:07:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ichannels > 2)
|
2022-02-10 07:41:37 +00:00
|
|
|
FIXME("Conversion from %lu to %lu channels is not implemented, falling back to stereo\n", ichannels, ochannels);
|
2011-12-12 20:07:49 +00:00
|
|
|
dsb->mix_channels = 2;
|
|
|
|
}
|
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
|
|
|
{
|
2014-12-03 20:06:38 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Not stopped: first notify: %u (%lu), left notify: %u (%lu), range: [%lu,%lu)\n",
|
2014-12-19 15:10:46 +00:00
|
|
|
first, dsb->notifies[first].dwOffset,
|
|
|
|
left, dsb->notifies[left].dwOffset,
|
|
|
|
playpos, (playpos + len) % dsb->buflen);
|
2014-12-03 20:06:38 +00:00
|
|
|
|
|
|
|
/* send notifications in range */
|
2014-12-19 15:10:46 +00: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 20:06:38 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Signalling %p (%lu)\n", dsb->notifies[check].hEventNotify, dsb->notifies[check].dwOffset);
|
2014-12-19 15:10:46 +00:00
|
|
|
SetEvent(dsb->notifies[check].hEventNotify);
|
|
|
|
}
|
2014-12-03 20:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(playpos + len > dsb->buflen){
|
|
|
|
for(check = first; check < left; ++check){
|
|
|
|
if(dsb->notifies[check].dwOffset >= (playpos + len) % dsb->buflen)
|
|
|
|
break;
|
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Signalling %p (%lu)\n", dsb->notifies[check].hEventNotify, dsb->notifies[check].dwOffset);
|
2014-12-03 20:06:38 +00:00
|
|
|
SetEvent(dsb->notifies[check].hEventNotify);
|
|
|
|
}
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:01:06 +00:00
|
|
|
static inline float get_current_sample(const IDirectSoundBufferImpl *dsb,
|
2021-09-14 10:05:18 +00:00
|
|
|
BYTE *buffer, DWORD buflen, DWORD mixpos, DWORD channel)
|
2012-05-01 20:01:06 +00:00
|
|
|
{
|
2021-09-14 10:05:18 +00:00
|
|
|
if (mixpos >= buflen && !(dsb->playflags & DSBPLAY_LOOPING))
|
2012-05-01 20:01:06 +00:00
|
|
|
return 0.0f;
|
2021-09-14 10:05:18 +00:00
|
|
|
return dsb->get(dsb, buffer + (mixpos % buflen), channel);
|
2012-05-01 20:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-15 13:29:34 +00:00
|
|
|
static UINT cp_fields_noresample(IDirectSoundBufferImpl *dsb, UINT count)
|
2012-05-06 18:06:13 +00:00
|
|
|
{
|
|
|
|
UINT istride = dsb->pwfx->nBlockAlign;
|
2012-05-15 13:29:34 +00:00
|
|
|
UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
|
2021-09-14 10:05:18 +00:00
|
|
|
UINT committed_samples = 0;
|
2012-05-06 18:06:13 +00:00
|
|
|
DWORD channel, i;
|
2021-09-14 10:05:18 +00:00
|
|
|
|
2021-12-06 19:08:24 +00:00
|
|
|
if (!secondarybuffer_is_audible(dsb))
|
|
|
|
return count;
|
|
|
|
|
2021-09-14 10:05:18 +00:00
|
|
|
if(dsb->use_committed) {
|
|
|
|
committed_samples = (dsb->writelead - dsb->committed_mixpos) / istride;
|
|
|
|
committed_samples = committed_samples <= count ? committed_samples : count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < committed_samples; i++)
|
|
|
|
for (channel = 0; channel < dsb->mix_channels; channel++)
|
|
|
|
dsb->put(dsb, i * ostride, channel, get_current_sample(dsb, dsb->committedbuff,
|
|
|
|
dsb->writelead, dsb->committed_mixpos + i * istride, channel));
|
|
|
|
|
|
|
|
for (; i < count; i++)
|
2012-05-06 18:06:13 +00:00
|
|
|
for (channel = 0; channel < dsb->mix_channels; channel++)
|
2021-09-14 10:05:18 +00:00
|
|
|
dsb->put(dsb, i * ostride, channel, get_current_sample(dsb, dsb->buffer->memory,
|
|
|
|
dsb->buflen, dsb->sec_mixpos + i * istride, channel));
|
|
|
|
|
2012-05-06 18:06:13 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:41:07 +00:00
|
|
|
static UINT cp_fields_resample(IDirectSoundBufferImpl *dsb, UINT count, LONG64 *freqAccNum)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-05-06 18:06:13 +00:00
|
|
|
UINT i, channel;
|
|
|
|
UINT istride = dsb->pwfx->nBlockAlign;
|
2012-05-15 13:29:34 +00:00
|
|
|
UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
|
2021-09-14 10:05:18 +00:00
|
|
|
UINT committed_samples = 0;
|
2011-11-07 16:25:49 +00:00
|
|
|
|
2014-12-27 22:41:07 +00:00
|
|
|
LONG64 freqAcc_start = *freqAccNum;
|
|
|
|
LONG64 freqAcc_end = freqAcc_start + count * dsb->freqAdjustNum;
|
2012-05-06 18:06:13 +00:00
|
|
|
UINT dsbfirstep = dsb->firstep;
|
|
|
|
UINT channels = dsb->mix_channels;
|
2014-12-27 22:41:07 +00:00
|
|
|
UINT max_ipos = (freqAcc_start + count * dsb->freqAdjustNum) / dsb->freqAdjustDen;
|
2012-05-06 18:06:13 +00:00
|
|
|
|
|
|
|
UINT fir_cachesize = (fir_len + dsbfirstep - 2) / dsbfirstep;
|
|
|
|
UINT required_input = max_ipos + fir_cachesize;
|
2016-05-17 18:41:04 +00:00
|
|
|
float *intermediate, *fir_copy, *itmp;
|
|
|
|
|
|
|
|
DWORD len = required_input * channels;
|
|
|
|
len += fir_cachesize;
|
|
|
|
len *= sizeof(float);
|
|
|
|
|
2021-12-06 19:08:24 +00:00
|
|
|
*freqAccNum = freqAcc_end % dsb->freqAdjustDen;
|
|
|
|
|
|
|
|
if (!secondarybuffer_is_audible(dsb))
|
|
|
|
return max_ipos;
|
|
|
|
|
2016-05-17 18:41:04 +00:00
|
|
|
if (!dsb->device->cp_buffer) {
|
2023-02-10 06:36:32 +00:00
|
|
|
dsb->device->cp_buffer = malloc(len);
|
2016-05-17 18:41:04 +00:00
|
|
|
dsb->device->cp_buffer_len = len;
|
|
|
|
} else if (len > dsb->device->cp_buffer_len) {
|
2023-02-10 06:36:32 +00:00
|
|
|
dsb->device->cp_buffer = realloc(dsb->device->cp_buffer, len);
|
2016-05-17 18:41:04 +00:00
|
|
|
dsb->device->cp_buffer_len = len;
|
|
|
|
}
|
2012-05-06 18:06:13 +00:00
|
|
|
|
2016-05-17 18:41:04 +00:00
|
|
|
fir_copy = dsb->device->cp_buffer;
|
|
|
|
intermediate = fir_copy + fir_cachesize;
|
2012-05-06 18:06:13 +00:00
|
|
|
|
2021-09-14 10:05:18 +00:00
|
|
|
if(dsb->use_committed) {
|
|
|
|
committed_samples = (dsb->writelead - dsb->committed_mixpos) / istride;
|
|
|
|
committed_samples = committed_samples <= required_input ? committed_samples : required_input;
|
|
|
|
}
|
2012-05-06 18:06:13 +00: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 18:41:04 +00:00
|
|
|
itmp = intermediate;
|
2021-09-14 10:05:18 +00:00
|
|
|
for (channel = 0; channel < channels; channel++) {
|
|
|
|
for (i = 0; i < committed_samples; i++)
|
|
|
|
*(itmp++) = get_current_sample(dsb, dsb->committedbuff,
|
|
|
|
dsb->writelead, dsb->committed_mixpos + i * istride, channel);
|
|
|
|
for (; i < required_input; i++)
|
|
|
|
*(itmp++) = get_current_sample(dsb, dsb->buffer->memory,
|
|
|
|
dsb->buflen, dsb->sec_mixpos + i * istride, channel);
|
|
|
|
}
|
2012-05-06 18:06:13 +00:00
|
|
|
|
|
|
|
for(i = 0; i < count; ++i) {
|
2014-12-27 22:41:07 +00: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 18:06:13 +00: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 19:31:53 +00:00
|
|
|
}
|
2012-05-01 20:01:32 +00:00
|
|
|
|
2012-05-06 18:06:13 +00:00
|
|
|
return max_ipos;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:41:07 +00:00
|
|
|
static void cp_fields(IDirectSoundBufferImpl *dsb, UINT count, LONG64 *freqAccNum)
|
2012-05-06 18:06:13 +00:00
|
|
|
{
|
|
|
|
DWORD ipos, adv;
|
|
|
|
|
2014-12-27 22:41:07 +00:00
|
|
|
if (dsb->freqAdjustNum == dsb->freqAdjustDen)
|
|
|
|
adv = cp_fields_noresample(dsb, count); /* *freqAccNum is unmodified */
|
2012-05-06 18:06:13 +00:00
|
|
|
else
|
2014-12-27 22:41:07 +00:00
|
|
|
adv = cp_fields_resample(dsb, count, freqAccNum);
|
2012-05-06 18:06:13 +00:00
|
|
|
|
|
|
|
ipos = dsb->sec_mixpos + adv * dsb->pwfx->nBlockAlign;
|
2012-05-01 20:01:32 +00:00
|
|
|
if (ipos >= dsb->buflen) {
|
|
|
|
if (dsb->playflags & DSBPLAY_LOOPING)
|
|
|
|
ipos %= dsb->buflen;
|
|
|
|
else {
|
|
|
|
ipos = 0;
|
|
|
|
dsb->state = STATE_STOPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsb->sec_mixpos = ipos;
|
2021-09-14 10:05:18 +00:00
|
|
|
|
|
|
|
if(dsb->use_committed) {
|
|
|
|
dsb->committed_mixpos += adv * dsb->pwfx->nBlockAlign;
|
|
|
|
if(dsb->committed_mixpos >= dsb->writelead)
|
|
|
|
dsb->use_committed = FALSE;
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
2007-02-21 19:16:22 +00:00
|
|
|
/**
|
2007-07-29 21:47:01 +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 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 19:16:22 +00:00
|
|
|
*
|
|
|
|
* dsb = the secondary buffer
|
2007-07-29 21:47:01 +00:00
|
|
|
* writepos = Starting position of changed buffer
|
|
|
|
* len = number of bytes to resample from writepos
|
2007-02-21 19:16:22 +00:00
|
|
|
*
|
2008-03-04 04:18:04 +00:00
|
|
|
* NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
|
2007-02-21 19:16:22 +00:00
|
|
|
*/
|
2012-05-15 13:29:34 +00:00
|
|
|
static void DSOUND_MixToTemporary(IDirectSoundBufferImpl *dsb, DWORD frames)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-05-15 13:29:34 +00:00
|
|
|
UINT size_bytes = frames * sizeof(float) * dsb->device->pwfx->nChannels;
|
2015-03-10 23:51:35 +00:00
|
|
|
HRESULT hr;
|
|
|
|
int i;
|
2008-03-04 04:18:04 +00:00
|
|
|
|
2012-05-15 13:29:34 +00:00
|
|
|
if (dsb->device->tmp_buffer_len < size_bytes || !dsb->device->tmp_buffer)
|
2008-03-04 04:18:04 +00:00
|
|
|
{
|
2012-05-15 13:29:34 +00:00
|
|
|
dsb->device->tmp_buffer_len = size_bytes;
|
2023-02-10 06:36:32 +00:00
|
|
|
dsb->device->tmp_buffer = realloc(dsb->device->tmp_buffer, size_bytes);
|
2008-03-04 04:18:04 +00:00
|
|
|
}
|
2016-12-19 21:17:23 +00:00
|
|
|
if(dsb->put_aux == putieee32_sum)
|
|
|
|
memset(dsb->device->tmp_buffer, 0, dsb->device->tmp_buffer_len);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2014-12-27 22:41:07 +00:00
|
|
|
cp_fields(dsb, frames, &dsb->freqAccNum);
|
2015-03-10 23:51:35 +00: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 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-15 13:29:34 +00:00
|
|
|
static void DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT frames)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2003-05-02 21:23:16 +00:00
|
|
|
INT i;
|
2014-12-27 21:32:24 +00:00
|
|
|
float vols[DS_MAX_CHANNELS];
|
2012-05-15 13:29:34 +00:00
|
|
|
UINT channels = dsb->device->pwfx->nChannels, chan;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2012-05-15 13:29:34 +00:00
|
|
|
TRACE("(%p,%d)\n",dsb,frames);
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("left = %lx, right = %lx\n", dsb->volpan.dwTotalAmpFactor[0],
|
2014-12-27 21:32:24 +00:00
|
|
|
dsb->volpan.dwTotalAmpFactor[1]);
|
2003-05-02 21:23:16 +00:00
|
|
|
|
2007-10-13 18:29:40 +00: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 13:29:34 +00:00
|
|
|
return; /* Nothing to do */
|
2007-10-13 18:29:40 +00:00
|
|
|
|
2014-12-27 21:32:24 +00:00
|
|
|
if (channels > DS_MAX_CHANNELS)
|
2007-07-29 21:47:01 +00:00
|
|
|
{
|
2012-05-15 13:29:34 +00:00
|
|
|
FIXME("There is no support for %u channels\n", channels);
|
|
|
|
return;
|
2007-07-29 21:47:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-27 21:32:24 +00:00
|
|
|
for (i = 0; i < channels; ++i)
|
|
|
|
vols[i] = dsb->volpan.dwTotalAmpFactor[i] / ((float)0xFFFF);
|
|
|
|
|
2012-05-15 13:29:34 +00:00
|
|
|
for(i = 0; i < frames; ++i){
|
|
|
|
for(chan = 0; chan < channels; ++chan){
|
2014-12-27 21:32:24 +00:00
|
|
|
dsb->device->tmp_buffer[i * channels + chan] *= vols[chan];
|
2003-05-02 21:23:16 +00:00
|
|
|
}
|
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
|
|
|
|
* fraglen = number of bytes to mix
|
|
|
|
*/
|
2017-03-01 16:00:59 +00:00
|
|
|
static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, float *mix_buffer, DWORD frames)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-05-15 13:29:34 +00:00
|
|
|
float *ibuf;
|
2012-05-14 20:20:02 +00:00
|
|
|
DWORD oldpos;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("sec_mixpos=%ld/%ld\n", dsb->sec_mixpos, dsb->buflen);
|
|
|
|
TRACE("(%p, frames=%ld)\n",dsb,frames);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2008-03-04 04:18:04 +00:00
|
|
|
/* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
|
2012-05-01 20:01:32 +00:00
|
|
|
oldpos = dsb->sec_mixpos;
|
2012-05-15 13:29:34 +00:00
|
|
|
DSOUND_MixToTemporary(dsb, frames);
|
2011-11-04 06:02:23 +00:00
|
|
|
ibuf = dsb->device->tmp_buffer;
|
2008-03-04 04:18:04 +00:00
|
|
|
|
2021-12-06 19:08:24 +00:00
|
|
|
if (secondarybuffer_is_audible(dsb)) {
|
|
|
|
/* Apply volume if needed */
|
|
|
|
DSOUND_MixerVol(dsb, frames);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2021-12-06 19:08:24 +00:00
|
|
|
mixieee32(ibuf, mix_buffer, frames * dsb->device->pwfx->nChannels);
|
|
|
|
}
|
2007-07-29 21:47:01 +00:00
|
|
|
|
2007-07-12 11:21:43 +00:00
|
|
|
/* check for notification positions */
|
|
|
|
if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
|
|
|
|
dsb->state != STATE_STARTING) {
|
2012-05-01 20:01:32 +00:00
|
|
|
INT ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
|
2007-07-29 21:47:01 +00:00
|
|
|
DSOUND_CheckEvent(dsb, oldpos, ilen);
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
return frames;
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
2007-07-29 21:47:01 +00:00
|
|
|
* playpos = the current play position in the device buffer (primary buffer)
|
2017-03-01 16:00:59 +00:00
|
|
|
* frames = the maximum number of frames in the primary buffer to mix, from the
|
2007-02-21 19:16:22 +00:00
|
|
|
* current writepos.
|
|
|
|
*
|
2017-03-01 16:00:59 +00:00
|
|
|
* Returns: the number of frames beyond the writepos that were mixed.
|
2007-02-21 19:16:22 +00:00
|
|
|
*/
|
2017-03-01 16:00:59 +00:00
|
|
|
static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, float *mix_buffer, DWORD frames)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-05-14 20:19:50 +00:00
|
|
|
DWORD primary_done = 0;
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("(%p, frames=%ld)\n",dsb,frames);
|
|
|
|
TRACE("looping=%ld, leadin=%ld\n", dsb->playflags, dsb->leadin);
|
2007-07-29 21:47:01 +00:00
|
|
|
|
|
|
|
/* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
|
2012-05-14 20:19:50 +00:00
|
|
|
/* FIXME: Is this needed? */
|
|
|
|
if (dsb->leadin && dsb->state == STATE_STARTING) {
|
2017-03-01 16:00:59 +00: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 22:41:07 +00:00
|
|
|
dsb->pwfx->nBlockAlign * dsb->freqAdjustNum / dsb->freqAdjustDen;
|
2007-07-29 21:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2012-05-14 20:19:50 +00:00
|
|
|
dsb->leadin = FALSE;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("frames (primary) = %li\n", frames);
|
2007-07-29 21:47:01 +00:00
|
|
|
|
|
|
|
/* First try to mix to the end of the buffer if possible
|
|
|
|
* Theoretically it would allow for better optimization
|
|
|
|
*/
|
2017-03-01 16:00:59 +00:00
|
|
|
primary_done += DSOUND_MixInBuffer(dsb, mix_buffer, frames);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("total mixed data=%ld\n", primary_done);
|
2003-03-17 21:23:12 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2017-03-01 16:00:59 +00:00
|
|
|
* frames = the maximum amount to mix into the primary buffer
|
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
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
static void DSOUND_MixToPrimary(const DirectSoundDevice *device, float *mix_buffer, DWORD frames, BOOL *all_stopped)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-05-14 17:31:01 +00:00
|
|
|
INT i;
|
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;
|
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("(frames %ld)\n", frames);
|
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
|
|
|
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("MixToPrimary for %p, state=%ld\n", dsb, dsb->state);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2011-09-24 14:02:52 +00:00
|
|
|
if (dsb->buflen && dsb->state) {
|
2022-02-10 07:41:37 +00:00
|
|
|
TRACE("Checking %p, frames=%ld\n", dsb, frames);
|
2020-02-03 15:02:56 +00:00
|
|
|
AcquireSRWLockShared(&dsb->lock);
|
2021-06-05 03:28:47 +00:00
|
|
|
if (dsb->state != STATE_STOPPED) {
|
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
|
|
|
|
2008-03-14 10:54:35 +00:00
|
|
|
/* mix next buffer into the main buffer */
|
2017-03-01 16:00:59 +00:00
|
|
|
DSOUND_MixOne(dsb, mix_buffer, frames);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
*all_stopped = FALSE;
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|
2020-02-03 15:02:56 +00:00
|
|
|
ReleaseSRWLockShared(&dsb->lock);
|
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
|
|
|
|
*/
|
|
|
|
|
2016-05-12 13:43:23 +00:00
|
|
|
static void DSOUND_WaveQueue(DirectSoundDevice *device, LPBYTE pos, DWORD bytes)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2011-09-27 13:51:07 +00:00
|
|
|
BYTE *buffer;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2007-06-13 15:05:49 +00:00
|
|
|
TRACE("(%p)\n", device);
|
2002-06-13 19:15:06 +00:00
|
|
|
|
2016-05-12 13:43:23 +00:00
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, bytes / device->pwfx->nBlockAlign, &buffer);
|
2011-09-27 13:51:07 +00:00
|
|
|
if(FAILED(hr)){
|
2022-02-10 07:41:37 +00:00
|
|
|
WARN("GetBuffer failed: %08lx\n", hr);
|
2016-05-16 14:55:26 +00:00
|
|
|
return;
|
2011-09-27 13:51:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 13:43:23 +00:00
|
|
|
memcpy(buffer, pos, bytes);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2016-05-12 13:43:23 +00:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, bytes / device->pwfx->nBlockAlign, 0);
|
2016-05-16 14:55:26 +00:00
|
|
|
if(FAILED(hr)) {
|
2022-02-10 07:41:37 +00:00
|
|
|
ERR("ReleaseBuffer failed: %08lx\n", hr);
|
2016-05-16 14:55:26 +00:00
|
|
|
IAudioRenderClient_ReleaseBuffer(device->render, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
2011-09-27 13:51:07 +00:00
|
|
|
|
2016-05-12 13:43:23 +00:00
|
|
|
device->pad += bytes;
|
2007-06-13 15:05:49 +00:00
|
|
|
}
|
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).
|
2012-05-15 13:29:34 +00:00
|
|
|
*
|
|
|
|
* The mixing procedure goes:
|
|
|
|
*
|
|
|
|
* secondary->buffer (secondary format)
|
|
|
|
* =[Resample]=> device->tmp_buffer (float format)
|
|
|
|
* =[Volume]=> device->tmp_buffer (float format)
|
2016-05-12 13:43:44 +00:00
|
|
|
* =[Reformat]=> device->buffer (device format, skipped on float)
|
2007-02-21 19:16:22 +00:00
|
|
|
*/
|
2006-08-02 11:26:14 +00:00
|
|
|
static void DSOUND_PerformMix(DirectSoundDevice *device)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2022-01-31 13:15:17 +00:00
|
|
|
DWORD block, pad_bytes, frames;
|
|
|
|
UINT32 pad_frames;
|
2011-09-27 13:51:07 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
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
|
|
|
/* **** */
|
2011-09-27 13:51:07 +00:00
|
|
|
EnterCriticalSection(&device->mixlock);
|
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
hr = IAudioClient_GetCurrentPadding(device->client, &pad_frames);
|
2011-09-27 13:51:07 +00:00
|
|
|
if(FAILED(hr)){
|
2022-02-10 07:41:37 +00:00
|
|
|
WARN("GetCurrentPadding failed: %08lx\n", hr);
|
2012-05-15 19:41:24 +00:00
|
|
|
LeaveCriticalSection(&device->mixlock);
|
2011-09-27 13:51:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-12 13:43:23 +00:00
|
|
|
block = device->pwfx->nBlockAlign;
|
2017-03-01 16:00:59 +00:00
|
|
|
pad_bytes = pad_frames * block;
|
|
|
|
device->playpos += device->pad - pad_bytes;
|
2016-05-12 13:43:23 +00:00
|
|
|
device->playpos %= device->buflen;
|
2017-03-01 16:00:59 +00:00
|
|
|
device->pad = pad_bytes;
|
2016-05-12 13:43:23 +00:00
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
frames = device->ac_frames - pad_frames;
|
|
|
|
if(!frames){
|
2016-05-12 13:43:23 +00:00
|
|
|
/* nothing to do! */
|
|
|
|
LeaveCriticalSection(&device->mixlock);
|
|
|
|
return;
|
2011-09-27 13:51:07 +00:00
|
|
|
}
|
2017-03-01 16:00:59 +00:00
|
|
|
if (frames > device->frag_frames * 3)
|
|
|
|
frames = device->frag_frames * 3;
|
2016-05-12 13:43:23 +00:00
|
|
|
|
2005-05-31 09:31:37 +00:00
|
|
|
if (device->priolevel != DSSCL_WRITEPRIMARY) {
|
2016-05-12 13:43:23 +00:00
|
|
|
BOOL all_stopped = FALSE;
|
2007-06-13 15:05:49 +00:00
|
|
|
int nfiller;
|
2016-05-12 13:43:44 +00:00
|
|
|
void *buffer = NULL;
|
2007-06-13 15:05:49 +00:00
|
|
|
|
|
|
|
/* the sound of silence */
|
|
|
|
nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
|
|
|
|
|
2008-09-04 10:07:37 +00:00
|
|
|
/* check for underrun. underrun occurs when the write position passes the mix position
|
|
|
|
* also wipe out just-played sound data */
|
2017-03-01 16:00:59 +00:00
|
|
|
if (!pad_frames)
|
2016-05-12 13:43:23 +00:00
|
|
|
WARN("Probable buffer underrun\n");
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2018-05-08 07:55:11 +00:00
|
|
|
hr = IAudioRenderClient_GetBuffer(device->render, frames, (BYTE **)&buffer);
|
2016-05-12 13:43:44 +00:00
|
|
|
if(FAILED(hr)){
|
2022-02-10 07:41:37 +00:00
|
|
|
WARN("GetBuffer failed: %08lx\n", hr);
|
2016-05-12 13:43:44 +00:00
|
|
|
LeaveCriticalSection(&device->mixlock);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-14 20:20:02 +00:00
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
memset(buffer, nfiller, frames * block);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2016-05-12 13:43:44 +00:00
|
|
|
if (!device->normfunction)
|
2017-03-01 16:00:59 +00:00
|
|
|
DSOUND_MixToPrimary(device, buffer, frames, &all_stopped);
|
2016-05-12 13:43:44 +00:00
|
|
|
else {
|
|
|
|
memset(device->buffer, nfiller, device->buflen);
|
2007-11-07 19:32:22 +00:00
|
|
|
|
2016-05-12 13:43:44 +00:00
|
|
|
/* do the mixing */
|
2017-03-01 16:00:59 +00:00
|
|
|
DSOUND_MixToPrimary(device, (float*)device->buffer, frames, &all_stopped);
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2017-03-01 16:01:00 +00:00
|
|
|
device->normfunction(device->buffer, buffer, frames * device->pwfx->nChannels);
|
2016-05-12 13:43:23 +00:00
|
|
|
}
|
2007-06-13 15:05:49 +00:00
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(device->render, frames, 0);
|
2016-05-12 13:43:44 +00:00
|
|
|
if(FAILED(hr))
|
2022-02-10 07:41:37 +00:00
|
|
|
ERR("ReleaseBuffer failed: %08lx\n", hr);
|
2016-05-12 13:43:44 +00:00
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
device->pad += frames * block;
|
2016-05-17 18:40:51 +00:00
|
|
|
} else if (!device->stopped) {
|
2017-03-01 16:00:59 +00:00
|
|
|
DWORD writepos = (device->playpos + pad_bytes) % device->buflen;
|
|
|
|
DWORD bytes = frames * block;
|
2017-03-01 16:00:58 +00:00
|
|
|
|
2017-03-01 16:00:59 +00:00
|
|
|
if (bytes > device->buflen)
|
|
|
|
bytes = device->buflen;
|
|
|
|
if (writepos + bytes > device->buflen) {
|
2016-05-12 13:43:23 +00:00
|
|
|
DSOUND_WaveQueue(device, device->buffer + writepos, device->buflen - writepos);
|
2017-03-01 16:00:59 +00:00
|
|
|
DSOUND_WaveQueue(device, device->buffer, writepos + bytes - device->buflen);
|
2016-05-12 13:43:23 +00:00
|
|
|
} else
|
2017-03-01 16:00:59 +00:00
|
|
|
DSOUND_WaveQueue(device, device->buffer + writepos, bytes);
|
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
|
|
|
}
|
|
|
|
|
2012-12-19 09:30:04 +00:00
|
|
|
DWORD CALLBACK DSOUND_mixthread(void *p)
|
2002-06-13 19:15:06 +00:00
|
|
|
{
|
2012-12-19 09:30:04 +00:00
|
|
|
DirectSoundDevice *dev = p;
|
|
|
|
TRACE("(%p)\n", dev);
|
2022-11-02 21:23:35 +00:00
|
|
|
SetThreadDescription(GetCurrentThread(), L"wine_dsound_mixer");
|
2003-06-27 22:22:15 +00:00
|
|
|
|
2012-12-19 09:30:04 +00:00
|
|
|
while (dev->ref) {
|
|
|
|
DWORD ret;
|
2003-06-27 22:22:15 +00:00
|
|
|
|
2012-12-19 09:30:04 +00: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)
|
2022-02-10 07:41:37 +00:00
|
|
|
WARN("wait returned error %lu %08lx!\n", GetLastError(), GetLastError());
|
2012-12-19 09:30:04 +00:00
|
|
|
else if (ret != WAIT_OBJECT_0)
|
2022-02-10 07:41:37 +00:00
|
|
|
WARN("wait returned %08lx!\n", ret);
|
2012-12-19 09:30:04 +00:00
|
|
|
if (!dev->ref)
|
|
|
|
break;
|
|
|
|
|
2020-02-03 15:02:17 +00:00
|
|
|
AcquireSRWLockShared(&dev->buffer_list_lock);
|
2012-12-19 09:30:04 +00:00
|
|
|
DSOUND_PerformMix(dev);
|
2020-02-03 15:02:17 +00:00
|
|
|
ReleaseSRWLockShared(&dev->buffer_list_lock);
|
2012-12-19 09:30:04 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2002-06-13 19:15:06 +00:00
|
|
|
}
|