xref: /reactos/dll/directx/wine/dsound/mixer.c (revision c2c66aff)
1*c2c66affSColin Finck /*  			DirectSound
2*c2c66affSColin Finck  *
3*c2c66affSColin Finck  * Copyright 1998 Marcus Meissner
4*c2c66affSColin Finck  * Copyright 1998 Rob Riggs
5*c2c66affSColin Finck  * Copyright 2000-2002 TransGaming Technologies, Inc.
6*c2c66affSColin Finck  * Copyright 2007 Peter Dons Tychsen
7*c2c66affSColin Finck  * Copyright 2007 Maarten Lankhorst
8*c2c66affSColin Finck  * Copyright 2011 Owen Rudge for CodeWeavers
9*c2c66affSColin Finck  *
10*c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
11*c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
12*c2c66affSColin Finck  * License as published by the Free Software Foundation; either
13*c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
14*c2c66affSColin Finck  *
15*c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
16*c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18*c2c66affSColin Finck  * Lesser General Public License for more details.
19*c2c66affSColin Finck  *
20*c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
21*c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
22*c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23*c2c66affSColin Finck  */
24*c2c66affSColin Finck 
25*c2c66affSColin Finck #include "dsound_private.h"
26*c2c66affSColin Finck 
DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)27*c2c66affSColin Finck void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
28*c2c66affSColin Finck {
29*c2c66affSColin Finck 	double temp;
30*c2c66affSColin Finck 	TRACE("(%p)\n",volpan);
31*c2c66affSColin Finck 
32*c2c66affSColin Finck 	TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
33*c2c66affSColin Finck 	/* the AmpFactors are expressed in 16.16 fixed point */
34*c2c66affSColin Finck 	volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 0xffff);
35*c2c66affSColin Finck 	/* FIXME: dwPan{Left|Right}AmpFactor */
36*c2c66affSColin Finck 
37*c2c66affSColin Finck 	/* FIXME: use calculated vol and pan ampfactors */
38*c2c66affSColin Finck 	temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
39*c2c66affSColin Finck 	volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
40*c2c66affSColin Finck 	temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
41*c2c66affSColin Finck 	volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
42*c2c66affSColin Finck 
43*c2c66affSColin Finck 	TRACE("left = %x, right = %x\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
44*c2c66affSColin Finck }
45*c2c66affSColin Finck 
DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)46*c2c66affSColin Finck void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
47*c2c66affSColin Finck {
48*c2c66affSColin Finck     double left,right;
49*c2c66affSColin Finck     TRACE("(%p)\n",volpan);
50*c2c66affSColin Finck 
51*c2c66affSColin Finck     TRACE("left=%x, right=%x\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
52*c2c66affSColin Finck     if (volpan->dwTotalLeftAmpFactor==0)
53*c2c66affSColin Finck         left=-10000;
54*c2c66affSColin Finck     else
55*c2c66affSColin Finck         left=600 * log(((double)volpan->dwTotalLeftAmpFactor) / 0xffff) / log(2);
56*c2c66affSColin Finck     if (volpan->dwTotalRightAmpFactor==0)
57*c2c66affSColin Finck         right=-10000;
58*c2c66affSColin Finck     else
59*c2c66affSColin Finck         right=600 * log(((double)volpan->dwTotalRightAmpFactor) / 0xffff) / log(2);
60*c2c66affSColin Finck     if (left<right)
61*c2c66affSColin Finck     {
62*c2c66affSColin Finck         volpan->lVolume=right;
63*c2c66affSColin Finck         volpan->dwVolAmpFactor=volpan->dwTotalRightAmpFactor;
64*c2c66affSColin Finck     }
65*c2c66affSColin Finck     else
66*c2c66affSColin Finck     {
67*c2c66affSColin Finck         volpan->lVolume=left;
68*c2c66affSColin Finck         volpan->dwVolAmpFactor=volpan->dwTotalLeftAmpFactor;
69*c2c66affSColin Finck     }
70*c2c66affSColin Finck     if (volpan->lVolume < -10000)
71*c2c66affSColin Finck         volpan->lVolume=-10000;
72*c2c66affSColin Finck     volpan->lPan=right-left;
73*c2c66affSColin Finck     if (volpan->lPan < -10000)
74*c2c66affSColin Finck         volpan->lPan=-10000;
75*c2c66affSColin Finck 
76*c2c66affSColin Finck     TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
77*c2c66affSColin Finck }
78*c2c66affSColin Finck 
79*c2c66affSColin Finck /** Convert a primary buffer position to a pointer position for device->mix_buffer
80*c2c66affSColin Finck  * device: DirectSoundDevice for which to calculate
81*c2c66affSColin Finck  * pos: Primary buffer position to converts
82*c2c66affSColin Finck  * Returns: Offset for mix_buffer
83*c2c66affSColin Finck  */
DSOUND_bufpos_to_mixpos(const DirectSoundDevice * device,DWORD pos)84*c2c66affSColin Finck DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos)
85*c2c66affSColin Finck {
86*c2c66affSColin Finck     DWORD ret = pos * 32 / device->pwfx->wBitsPerSample;
87*c2c66affSColin Finck     if (device->pwfx->wBitsPerSample == 32)
88*c2c66affSColin Finck         ret *= 2;
89*c2c66affSColin Finck     return ret;
90*c2c66affSColin Finck }
91*c2c66affSColin Finck 
92*c2c66affSColin Finck /* NOTE: Not all secpos have to always be mapped to a bufpos, other way around is always the case
93*c2c66affSColin Finck  * DWORD64 is used here because a single DWORD wouldn't be big enough to fit the freqAcc for big buffers
94*c2c66affSColin Finck  */
95*c2c66affSColin Finck /** This function converts a 'native' sample pointer to a resampled pointer that fits for primary
96*c2c66affSColin Finck  * secmixpos is used to decide which freqAcc is needed
97*c2c66affSColin Finck  * overshot tells what the 'actual' secpos is now (optional)
98*c2c66affSColin Finck  */
DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl * dsb,DWORD secpos,DWORD secmixpos,DWORD * overshot)99*c2c66affSColin Finck DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot)
100*c2c66affSColin Finck {
101*c2c66affSColin Finck 	DWORD64 framelen = secpos / dsb->pwfx->nBlockAlign;
102*c2c66affSColin Finck 	DWORD64 freqAdjust = dsb->freqAdjust;
103*c2c66affSColin Finck 	DWORD64 acc, freqAcc;
104*c2c66affSColin Finck 
105*c2c66affSColin Finck 	if (secpos < secmixpos)
106*c2c66affSColin Finck 		freqAcc = dsb->freqAccNext;
107*c2c66affSColin Finck 	else freqAcc = dsb->freqAcc;
108*c2c66affSColin Finck 	acc = (framelen << DSOUND_FREQSHIFT) + (freqAdjust - 1 - freqAcc);
109*c2c66affSColin Finck 	acc /= freqAdjust;
110*c2c66affSColin Finck 	if (overshot)
111*c2c66affSColin Finck 	{
112*c2c66affSColin Finck 		DWORD64 oshot = acc * freqAdjust + freqAcc;
113*c2c66affSColin Finck 		assert(oshot >= framelen << DSOUND_FREQSHIFT);
114*c2c66affSColin Finck 		oshot -= framelen << DSOUND_FREQSHIFT;
115*c2c66affSColin Finck 		*overshot = (DWORD)oshot;
116*c2c66affSColin Finck 		assert(*overshot < dsb->freqAdjust);
117*c2c66affSColin Finck 	}
118*c2c66affSColin Finck 	return (DWORD)acc * dsb->device->pwfx->nBlockAlign;
119*c2c66affSColin Finck }
120*c2c66affSColin Finck 
121*c2c66affSColin Finck /** Convert a resampled pointer that fits for primary to a 'native' sample pointer
122*c2c66affSColin Finck  * freqAccNext is used here rather than freqAcc: In case the app wants to fill up to
123*c2c66affSColin Finck  * the play position it won't overwrite it
124*c2c66affSColin Finck  */
DSOUND_bufpos_to_secpos(const IDirectSoundBufferImpl * dsb,DWORD bufpos)125*c2c66affSColin Finck static DWORD DSOUND_bufpos_to_secpos(const IDirectSoundBufferImpl *dsb, DWORD bufpos)
126*c2c66affSColin Finck {
127*c2c66affSColin Finck 	DWORD oAdv = dsb->device->pwfx->nBlockAlign, iAdv = dsb->pwfx->nBlockAlign, pos;
128*c2c66affSColin Finck 	DWORD64 framelen;
129*c2c66affSColin Finck 	DWORD64 acc;
130*c2c66affSColin Finck 
131*c2c66affSColin Finck 	framelen = bufpos/oAdv;
132*c2c66affSColin Finck 	acc = framelen * (DWORD64)dsb->freqAdjust + (DWORD64)dsb->freqAccNext;
133*c2c66affSColin Finck 	acc = acc >> DSOUND_FREQSHIFT;
134*c2c66affSColin Finck 	pos = (DWORD)acc * iAdv;
135*c2c66affSColin Finck 	if (pos >= dsb->buflen)
136*c2c66affSColin Finck 		/* Because of differences between freqAcc and freqAccNext, this might happen */
137*c2c66affSColin Finck 		pos = dsb->buflen - iAdv;
138*c2c66affSColin Finck 	TRACE("Converted %d/%d to %d/%d\n", bufpos, dsb->tmp_buffer_len, pos, dsb->buflen);
139*c2c66affSColin Finck 	return pos;
140*c2c66affSColin Finck }
141*c2c66affSColin Finck 
142*c2c66affSColin Finck /**
143*c2c66affSColin Finck  * Move freqAccNext to freqAcc, and find new values for buffer length and freqAccNext
144*c2c66affSColin Finck  */
DSOUND_RecalcFreqAcc(IDirectSoundBufferImpl * dsb)145*c2c66affSColin Finck static void DSOUND_RecalcFreqAcc(IDirectSoundBufferImpl *dsb)
146*c2c66affSColin Finck {
147*c2c66affSColin Finck 	if (!dsb->freqneeded) return;
148*c2c66affSColin Finck 	dsb->freqAcc = dsb->freqAccNext;
149*c2c66affSColin Finck 	dsb->tmp_buffer_len = DSOUND_secpos_to_bufpos(dsb, dsb->buflen, 0, &dsb->freqAccNext);
150*c2c66affSColin Finck 	TRACE("New freqadjust: %04x, new buflen: %d\n", dsb->freqAccNext, dsb->tmp_buffer_len);
151*c2c66affSColin Finck }
152*c2c66affSColin Finck 
153*c2c66affSColin Finck /**
154*c2c66affSColin Finck  * Recalculate the size for temporary buffer, and new writelead
155*c2c66affSColin Finck  * Should be called when one of the following things occur:
156*c2c66affSColin Finck  * - Primary buffer format is changed
157*c2c66affSColin Finck  * - This buffer format (frequency) is changed
158*c2c66affSColin Finck  *
159*c2c66affSColin Finck  * After this, DSOUND_MixToTemporary(dsb, 0, dsb->buflen) should
160*c2c66affSColin Finck  * be called to refill the temporary buffer with data.
161*c2c66affSColin Finck  */
DSOUND_RecalcFormat(IDirectSoundBufferImpl * dsb)162*c2c66affSColin Finck void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
163*c2c66affSColin Finck {
164*c2c66affSColin Finck 	BOOL needremix = TRUE, needresample = (dsb->freq != dsb->device->pwfx->nSamplesPerSec);
165*c2c66affSColin Finck 	DWORD bAlign = dsb->pwfx->nBlockAlign, pAlign = dsb->device->pwfx->nBlockAlign;
166*c2c66affSColin Finck 	WAVEFORMATEXTENSIBLE *pwfxe;
167*c2c66affSColin Finck 	BOOL ieee = FALSE;
168*c2c66affSColin Finck 
169*c2c66affSColin Finck 	TRACE("(%p)\n",dsb);
170*c2c66affSColin Finck 
171*c2c66affSColin Finck 	pwfxe = (WAVEFORMATEXTENSIBLE *) dsb->pwfx;
172*c2c66affSColin Finck 
173*c2c66affSColin Finck 	if ((pwfxe->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) || ((pwfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
174*c2c66affSColin Finck 	    && (IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))))
175*c2c66affSColin Finck 		ieee = TRUE;
176*c2c66affSColin Finck 
177*c2c66affSColin Finck 	/* calculate the 10ms write lead */
178*c2c66affSColin Finck 	dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
179*c2c66affSColin Finck 
180*c2c66affSColin Finck 	if ((dsb->pwfx->wBitsPerSample == dsb->device->pwfx->wBitsPerSample) &&
181*c2c66affSColin Finck 	    (dsb->pwfx->nChannels == dsb->device->pwfx->nChannels) && !needresample && !ieee)
182*c2c66affSColin Finck 		needremix = FALSE;
183*c2c66affSColin Finck 	HeapFree(GetProcessHeap(), 0, dsb->tmp_buffer);
184*c2c66affSColin Finck 	dsb->tmp_buffer = NULL;
185*c2c66affSColin Finck 	dsb->max_buffer_len = dsb->freqAcc = dsb->freqAccNext = 0;
186*c2c66affSColin Finck 	dsb->freqneeded = needresample;
187*c2c66affSColin Finck 
188*c2c66affSColin Finck 	if (ieee)
189*c2c66affSColin Finck 		dsb->convert = convertbpp[4][dsb->device->pwfx->wBitsPerSample/8 - 1];
190*c2c66affSColin Finck 	else
191*c2c66affSColin Finck 		dsb->convert = convertbpp[dsb->pwfx->wBitsPerSample/8 - 1][dsb->device->pwfx->wBitsPerSample/8 - 1];
192*c2c66affSColin Finck 
193*c2c66affSColin Finck 	dsb->resampleinmixer = FALSE;
194*c2c66affSColin Finck 
195*c2c66affSColin Finck 	if (needremix)
196*c2c66affSColin Finck 	{
197*c2c66affSColin Finck 		if (needresample)
198*c2c66affSColin Finck 			DSOUND_RecalcFreqAcc(dsb);
199*c2c66affSColin Finck 		else
200*c2c66affSColin Finck 			dsb->tmp_buffer_len = dsb->buflen / bAlign * pAlign;
201*c2c66affSColin Finck 		dsb->max_buffer_len = dsb->tmp_buffer_len;
202*c2c66affSColin Finck 		if ((dsb->max_buffer_len <= dsb->device->buflen || dsb->max_buffer_len < ds_snd_shadow_maxsize * 1024 * 1024) && ds_snd_shadow_maxsize >= 0)
203*c2c66affSColin Finck 			dsb->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, dsb->max_buffer_len);
204*c2c66affSColin Finck 		if (dsb->tmp_buffer)
205*c2c66affSColin Finck 			FillMemory(dsb->tmp_buffer, dsb->tmp_buffer_len, dsb->device->pwfx->wBitsPerSample == 8 ? 128 : 0);
206*c2c66affSColin Finck 		else
207*c2c66affSColin Finck 			dsb->resampleinmixer = TRUE;
208*c2c66affSColin Finck 	}
209*c2c66affSColin Finck 	else dsb->max_buffer_len = dsb->tmp_buffer_len = dsb->buflen;
210*c2c66affSColin Finck 	dsb->buf_mixpos = DSOUND_secpos_to_bufpos(dsb, dsb->sec_mixpos, 0, NULL);
211*c2c66affSColin Finck }
212*c2c66affSColin Finck 
213*c2c66affSColin Finck /**
214*c2c66affSColin Finck  * Check for application callback requests for when the play position
215*c2c66affSColin Finck  * reaches certain points.
216*c2c66affSColin Finck  *
217*c2c66affSColin Finck  * The offsets that will be triggered will be those between the recorded
218*c2c66affSColin Finck  * "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
219*c2c66affSColin Finck  * beyond that position.
220*c2c66affSColin Finck  */
DSOUND_CheckEvent(const IDirectSoundBufferImpl * dsb,DWORD playpos,int len)221*c2c66affSColin Finck void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
222*c2c66affSColin Finck {
223*c2c66affSColin Finck 	int			i;
224*c2c66affSColin Finck 	DWORD			offset;
225*c2c66affSColin Finck 	LPDSBPOSITIONNOTIFY	event;
226*c2c66affSColin Finck 	TRACE("(%p,%d)\n",dsb,len);
227*c2c66affSColin Finck 
228*c2c66affSColin Finck 	if (dsb->nrofnotifies == 0)
229*c2c66affSColin Finck 		return;
230*c2c66affSColin Finck 
231*c2c66affSColin Finck 	TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
232*c2c66affSColin Finck 		dsb, dsb->buflen, playpos, len);
233*c2c66affSColin Finck 	for (i = 0; i < dsb->nrofnotifies ; i++) {
234*c2c66affSColin Finck 		event = dsb->notifies + i;
235*c2c66affSColin Finck 		offset = event->dwOffset;
236*c2c66affSColin Finck 		TRACE("checking %d, position %d, event = %p\n",
237*c2c66affSColin Finck 			i, offset, event->hEventNotify);
238*c2c66affSColin Finck 		/* DSBPN_OFFSETSTOP has to be the last element. So this is */
239*c2c66affSColin Finck 		/* OK. [Inside DirectX, p274] */
240*c2c66affSColin Finck 		/* Windows does not seem to enforce this, and some apps rely */
241*c2c66affSColin Finck 		/* on that, so we can't stop there. */
242*c2c66affSColin Finck 		/*  */
243*c2c66affSColin Finck 		/* This also means we can't sort the entries by offset, */
244*c2c66affSColin Finck 		/* because DSBPN_OFFSETSTOP == -1 */
245*c2c66affSColin Finck 		if (offset == DSBPN_OFFSETSTOP) {
246*c2c66affSColin Finck 			if (dsb->state == STATE_STOPPED) {
247*c2c66affSColin Finck 				SetEvent(event->hEventNotify);
248*c2c66affSColin Finck 				TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
249*c2c66affSColin Finck 			}
250*c2c66affSColin Finck                         continue;
251*c2c66affSColin Finck 		}
252*c2c66affSColin Finck 		if ((playpos + len) >= dsb->buflen) {
253*c2c66affSColin Finck 			if ((offset < ((playpos + len) % dsb->buflen)) ||
254*c2c66affSColin Finck 			    (offset >= playpos)) {
255*c2c66affSColin Finck 				TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
256*c2c66affSColin Finck 				SetEvent(event->hEventNotify);
257*c2c66affSColin Finck 			}
258*c2c66affSColin Finck 		} else {
259*c2c66affSColin Finck 			if ((offset >= playpos) && (offset < (playpos + len))) {
260*c2c66affSColin Finck 				TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
261*c2c66affSColin Finck 				SetEvent(event->hEventNotify);
262*c2c66affSColin Finck 			}
263*c2c66affSColin Finck 		}
264*c2c66affSColin Finck 	}
265*c2c66affSColin Finck }
266*c2c66affSColin Finck 
267*c2c66affSColin Finck /**
268*c2c66affSColin Finck  * Copy a single frame from the given input buffer to the given output buffer.
269*c2c66affSColin Finck  * Translate 8 <-> 16 bits and mono <-> stereo
270*c2c66affSColin Finck  */
cp_fields(const IDirectSoundBufferImpl * dsb,const BYTE * ibuf,BYTE * obuf,UINT istride,UINT ostride,UINT count,UINT freqAcc,UINT adj)271*c2c66affSColin Finck static inline void cp_fields(const IDirectSoundBufferImpl *dsb, const BYTE *ibuf, BYTE *obuf,
272*c2c66affSColin Finck         UINT istride, UINT ostride, UINT count, UINT freqAcc, UINT adj)
273*c2c66affSColin Finck {
274*c2c66affSColin Finck     DirectSoundDevice *device = dsb->device;
275*c2c66affSColin Finck     INT istep = dsb->pwfx->wBitsPerSample / 8, ostep = device->pwfx->wBitsPerSample / 8;
276*c2c66affSColin Finck 
277*c2c66affSColin Finck     if (device->pwfx->nChannels == dsb->pwfx->nChannels ||
278*c2c66affSColin Finck         (device->pwfx->nChannels == 2 && dsb->pwfx->nChannels == 6) ||
279*c2c66affSColin Finck         (device->pwfx->nChannels == 8 && dsb->pwfx->nChannels == 2) ||
280*c2c66affSColin Finck         (device->pwfx->nChannels == 6 && dsb->pwfx->nChannels == 2)) {
281*c2c66affSColin Finck         dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
282*c2c66affSColin Finck         if (device->pwfx->nChannels == 2 || dsb->pwfx->nChannels == 2)
283*c2c66affSColin Finck             dsb->convert(ibuf + istep, obuf + ostep, istride, ostride, count, freqAcc, adj);
284*c2c66affSColin Finck         return;
285*c2c66affSColin Finck     }
286*c2c66affSColin Finck 
287*c2c66affSColin Finck     if (device->pwfx->nChannels == 1 && dsb->pwfx->nChannels == 2)
288*c2c66affSColin Finck     {
289*c2c66affSColin Finck         dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
290*c2c66affSColin Finck         return;
291*c2c66affSColin Finck     }
292*c2c66affSColin Finck 
293*c2c66affSColin Finck     if (device->pwfx->nChannels == 2 && dsb->pwfx->nChannels == 1)
294*c2c66affSColin Finck     {
295*c2c66affSColin Finck         dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
296*c2c66affSColin Finck         dsb->convert(ibuf, obuf + ostep, istride, ostride, count, freqAcc, adj);
297*c2c66affSColin Finck         return;
298*c2c66affSColin Finck     }
299*c2c66affSColin Finck 
300*c2c66affSColin Finck     WARN("Unable to remap channels: device=%u, buffer=%u\n", device->pwfx->nChannels,
301*c2c66affSColin Finck             dsb->pwfx->nChannels);
302*c2c66affSColin Finck }
303*c2c66affSColin Finck 
304*c2c66affSColin Finck /**
305*c2c66affSColin Finck  * Calculate the distance between two buffer offsets, taking wraparound
306*c2c66affSColin Finck  * into account.
307*c2c66affSColin Finck  */
DSOUND_BufPtrDiff(DWORD buflen,DWORD ptr1,DWORD ptr2)308*c2c66affSColin Finck static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
309*c2c66affSColin Finck {
310*c2c66affSColin Finck /* If these asserts fail, the problem is not here, but in the underlying code */
311*c2c66affSColin Finck 	assert(ptr1 < buflen);
312*c2c66affSColin Finck 	assert(ptr2 < buflen);
313*c2c66affSColin Finck 	if (ptr1 >= ptr2) {
314*c2c66affSColin Finck 		return ptr1 - ptr2;
315*c2c66affSColin Finck 	} else {
316*c2c66affSColin Finck 		return buflen + ptr1 - ptr2;
317*c2c66affSColin Finck 	}
318*c2c66affSColin Finck }
319*c2c66affSColin Finck /**
320*c2c66affSColin Finck  * Mix at most the given amount of data into the allocated temporary buffer
321*c2c66affSColin Finck  * of the given secondary buffer, starting from the dsb's first currently
322*c2c66affSColin Finck  * unsampled frame (writepos), translating frequency (pitch), stereo/mono
323*c2c66affSColin Finck  * and bits-per-sample so that it is ideal for the primary buffer.
324*c2c66affSColin Finck  * Doesn't perform any mixing - this is a straight copy/convert operation.
325*c2c66affSColin Finck  *
326*c2c66affSColin Finck  * dsb = the secondary buffer
327*c2c66affSColin Finck  * writepos = Starting position of changed buffer
328*c2c66affSColin Finck  * len = number of bytes to resample from writepos
329*c2c66affSColin Finck  *
330*c2c66affSColin Finck  * NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
331*c2c66affSColin Finck  */
DSOUND_MixToTemporary(const IDirectSoundBufferImpl * dsb,DWORD writepos,DWORD len,BOOL inmixer)332*c2c66affSColin Finck void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD len, BOOL inmixer)
333*c2c66affSColin Finck {
334*c2c66affSColin Finck 	INT	size;
335*c2c66affSColin Finck 	BYTE	*ibp, *obp, *obp_begin;
336*c2c66affSColin Finck 	INT	iAdvance = dsb->pwfx->nBlockAlign;
337*c2c66affSColin Finck 	INT	oAdvance = dsb->device->pwfx->nBlockAlign;
338*c2c66affSColin Finck 	DWORD freqAcc, target_writepos = 0, overshot, maxlen;
339*c2c66affSColin Finck 
340*c2c66affSColin Finck 	/* We resample only when needed */
341*c2c66affSColin Finck 	if ((dsb->tmp_buffer && inmixer) || (!dsb->tmp_buffer && !inmixer) || dsb->resampleinmixer != inmixer)
342*c2c66affSColin Finck 		return;
343*c2c66affSColin Finck 
344*c2c66affSColin Finck 	assert(writepos + len <= dsb->buflen);
345*c2c66affSColin Finck 	if (inmixer && writepos + len < dsb->buflen)
346*c2c66affSColin Finck 		len += dsb->pwfx->nBlockAlign;
347*c2c66affSColin Finck 
348*c2c66affSColin Finck 	maxlen = DSOUND_secpos_to_bufpos(dsb, len, 0, NULL);
349*c2c66affSColin Finck 
350*c2c66affSColin Finck 	ibp = dsb->buffer->memory + writepos;
351*c2c66affSColin Finck 	if (!inmixer)
352*c2c66affSColin Finck 		obp_begin = dsb->tmp_buffer;
353*c2c66affSColin Finck 	else if (dsb->device->tmp_buffer_len < maxlen || !dsb->device->tmp_buffer)
354*c2c66affSColin Finck 	{
355*c2c66affSColin Finck 		dsb->device->tmp_buffer_len = maxlen;
356*c2c66affSColin Finck 		if (dsb->device->tmp_buffer)
357*c2c66affSColin Finck 			dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, maxlen);
358*c2c66affSColin Finck 		else
359*c2c66affSColin Finck 			dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, maxlen);
360*c2c66affSColin Finck 		obp_begin = dsb->device->tmp_buffer;
361*c2c66affSColin Finck 	}
362*c2c66affSColin Finck 	else
363*c2c66affSColin Finck 		obp_begin = dsb->device->tmp_buffer;
364*c2c66affSColin Finck 
365*c2c66affSColin Finck 	TRACE("(%p, %p)\n", dsb, ibp);
366*c2c66affSColin Finck 	size = len / iAdvance;
367*c2c66affSColin Finck 
368*c2c66affSColin Finck 	/* Check for same sample rate */
369*c2c66affSColin Finck 	if (dsb->freq == dsb->device->pwfx->nSamplesPerSec) {
370*c2c66affSColin Finck 		TRACE("(%p) Same sample rate %d = primary %d\n", dsb,
371*c2c66affSColin Finck 			dsb->freq, dsb->device->pwfx->nSamplesPerSec);
372*c2c66affSColin Finck 		obp = obp_begin;
373*c2c66affSColin Finck 		if (!inmixer)
374*c2c66affSColin Finck 			 obp += writepos/iAdvance*oAdvance;
375*c2c66affSColin Finck 
376*c2c66affSColin Finck 		cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, 0, 1 << DSOUND_FREQSHIFT);
377*c2c66affSColin Finck 		return;
378*c2c66affSColin Finck 	}
379*c2c66affSColin Finck 
380*c2c66affSColin Finck 	/* Mix in different sample rates */
381*c2c66affSColin Finck 	TRACE("(%p) Adjusting frequency: %d -> %d\n", dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec);
382*c2c66affSColin Finck 
383*c2c66affSColin Finck 	target_writepos = DSOUND_secpos_to_bufpos(dsb, writepos, dsb->sec_mixpos, &freqAcc);
384*c2c66affSColin Finck 	overshot = freqAcc >> DSOUND_FREQSHIFT;
385*c2c66affSColin Finck 	if (overshot)
386*c2c66affSColin Finck 	{
387*c2c66affSColin Finck 		if (overshot >= size)
388*c2c66affSColin Finck 			return;
389*c2c66affSColin Finck 		size -= overshot;
390*c2c66affSColin Finck 		writepos += overshot * iAdvance;
391*c2c66affSColin Finck 		if (writepos >= dsb->buflen)
392*c2c66affSColin Finck 			return;
393*c2c66affSColin Finck 		ibp = dsb->buffer->memory + writepos;
394*c2c66affSColin Finck 		freqAcc &= (1 << DSOUND_FREQSHIFT) - 1;
395*c2c66affSColin Finck 		TRACE("Overshot: %d, freqAcc: %04x\n", overshot, freqAcc);
396*c2c66affSColin Finck 	}
397*c2c66affSColin Finck 
398*c2c66affSColin Finck 	if (!inmixer)
399*c2c66affSColin Finck 		obp = obp_begin + target_writepos;
400*c2c66affSColin Finck 	else obp = obp_begin;
401*c2c66affSColin Finck 
402*c2c66affSColin Finck 	/* FIXME: Small problem here when we're overwriting buf_mixpos, it then STILL uses old freqAcc, not sure if it matters or not */
403*c2c66affSColin Finck 	cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, freqAcc, dsb->freqAdjust);
404*c2c66affSColin Finck }
405*c2c66affSColin Finck 
406*c2c66affSColin Finck /** Apply volume to the given soundbuffer from (primary) position writepos and length len
407*c2c66affSColin Finck  * Returns: NULL if no volume needs to be applied
408*c2c66affSColin Finck  * or else a memory handle that holds 'len' volume adjusted buffer */
DSOUND_MixerVol(const IDirectSoundBufferImpl * dsb,INT len)409*c2c66affSColin Finck static LPBYTE DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT len)
410*c2c66affSColin Finck {
411*c2c66affSColin Finck 	INT	i;
412*c2c66affSColin Finck 	BYTE	*bpc;
413*c2c66affSColin Finck 	INT16	*bps, *mems;
414*c2c66affSColin Finck 	DWORD vLeft, vRight;
415*c2c66affSColin Finck 	INT nChannels = dsb->device->pwfx->nChannels;
416*c2c66affSColin Finck 	LPBYTE mem = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos;
417*c2c66affSColin Finck 
418*c2c66affSColin Finck 	if (dsb->resampleinmixer)
419*c2c66affSColin Finck 		mem = dsb->device->tmp_buffer;
420*c2c66affSColin Finck 
421*c2c66affSColin Finck 	TRACE("(%p,%d)\n",dsb,len);
422*c2c66affSColin Finck 	TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
423*c2c66affSColin Finck 		dsb->volpan.dwTotalRightAmpFactor);
424*c2c66affSColin Finck 
425*c2c66affSColin Finck 	if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
426*c2c66affSColin Finck 	    (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
427*c2c66affSColin Finck 	     !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
428*c2c66affSColin Finck 		return NULL; /* Nothing to do */
429*c2c66affSColin Finck 
430*c2c66affSColin Finck 	if (nChannels != 1 && nChannels != 2)
431*c2c66affSColin Finck 	{
432*c2c66affSColin Finck 		FIXME("There is no support for %d channels\n", nChannels);
433*c2c66affSColin Finck 		return NULL;
434*c2c66affSColin Finck 	}
435*c2c66affSColin Finck 
436*c2c66affSColin Finck 	if (dsb->device->pwfx->wBitsPerSample != 8 && dsb->device->pwfx->wBitsPerSample != 16)
437*c2c66affSColin Finck 	{
438*c2c66affSColin Finck 		FIXME("There is no support for %d bpp\n", dsb->device->pwfx->wBitsPerSample);
439*c2c66affSColin Finck 		return NULL;
440*c2c66affSColin Finck 	}
441*c2c66affSColin Finck 
442*c2c66affSColin Finck 	if (dsb->device->tmp_buffer_len < len || !dsb->device->tmp_buffer)
443*c2c66affSColin Finck 	{
444*c2c66affSColin Finck 		/* If we just resampled in DSOUND_MixToTemporary, we shouldn't need to resize here */
445*c2c66affSColin Finck 		assert(!dsb->resampleinmixer);
446*c2c66affSColin Finck 		dsb->device->tmp_buffer_len = len;
447*c2c66affSColin Finck 		if (dsb->device->tmp_buffer)
448*c2c66affSColin Finck 			dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, len);
449*c2c66affSColin Finck 		else
450*c2c66affSColin Finck 			dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
451*c2c66affSColin Finck 	}
452*c2c66affSColin Finck 
453*c2c66affSColin Finck 	bpc = dsb->device->tmp_buffer;
454*c2c66affSColin Finck 	bps = (INT16 *)bpc;
455*c2c66affSColin Finck 	mems = (INT16 *)mem;
456*c2c66affSColin Finck 	vLeft = dsb->volpan.dwTotalLeftAmpFactor;
457*c2c66affSColin Finck 	if (nChannels > 1)
458*c2c66affSColin Finck 		vRight = dsb->volpan.dwTotalRightAmpFactor;
459*c2c66affSColin Finck 	else
460*c2c66affSColin Finck 		vRight = vLeft;
461*c2c66affSColin Finck 
462*c2c66affSColin Finck 	switch (dsb->device->pwfx->wBitsPerSample) {
463*c2c66affSColin Finck 	case 8:
464*c2c66affSColin Finck 		/* 8-bit WAV is unsigned, but we need to operate */
465*c2c66affSColin Finck 		/* on signed data for this to work properly */
466*c2c66affSColin Finck 		for (i = 0; i < len-1; i+=2) {
467*c2c66affSColin Finck 			*(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
468*c2c66affSColin Finck 			*(bpc++) = (((*(mem++) - 128) * vRight) >> 16) + 128;
469*c2c66affSColin Finck 		}
470*c2c66affSColin Finck 		if (len % 2 == 1 && nChannels == 1)
471*c2c66affSColin Finck 			*(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
472*c2c66affSColin Finck 		break;
473*c2c66affSColin Finck 	case 16:
474*c2c66affSColin Finck 		/* 16-bit WAV is signed -- much better */
475*c2c66affSColin Finck 		for (i = 0; i < len-3; i += 4) {
476*c2c66affSColin Finck 			*(bps++) = (*(mems++) * vLeft) >> 16;
477*c2c66affSColin Finck 			*(bps++) = (*(mems++) * vRight) >> 16;
478*c2c66affSColin Finck 		}
479*c2c66affSColin Finck 		if (len % 4 == 2 && nChannels == 1)
480*c2c66affSColin Finck 			*(bps++) = ((INT)*(mems++) * vLeft) >> 16;
481*c2c66affSColin Finck 		break;
482*c2c66affSColin Finck 	}
483*c2c66affSColin Finck 	return dsb->device->tmp_buffer;
484*c2c66affSColin Finck }
485*c2c66affSColin Finck 
486*c2c66affSColin Finck /**
487*c2c66affSColin Finck  * Mix (at most) the given number of bytes into the given position of the
488*c2c66affSColin Finck  * device buffer, from the secondary buffer "dsb" (starting at the current
489*c2c66affSColin Finck  * mix position for that buffer).
490*c2c66affSColin Finck  *
491*c2c66affSColin Finck  * Returns the number of bytes actually mixed into the device buffer. This
492*c2c66affSColin Finck  * will match fraglen unless the end of the secondary buffer is reached
493*c2c66affSColin Finck  * (and it is not looping).
494*c2c66affSColin Finck  *
495*c2c66affSColin Finck  * dsb  = the secondary buffer to mix from
496*c2c66affSColin Finck  * writepos = position (offset) in device buffer to write at
497*c2c66affSColin Finck  * fraglen = number of bytes to mix
498*c2c66affSColin Finck  */
DSOUND_MixInBuffer(IDirectSoundBufferImpl * dsb,DWORD writepos,DWORD fraglen)499*c2c66affSColin Finck static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
500*c2c66affSColin Finck {
501*c2c66affSColin Finck 	INT len = fraglen, ilen;
502*c2c66affSColin Finck 	BYTE *ibuf = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos, *volbuf;
503*c2c66affSColin Finck 	DWORD oldpos, mixbufpos;
504*c2c66affSColin Finck 
505*c2c66affSColin Finck 	TRACE("buf_mixpos=%d/%d sec_mixpos=%d/%d\n", dsb->buf_mixpos, dsb->tmp_buffer_len, dsb->sec_mixpos, dsb->buflen);
506*c2c66affSColin Finck 	TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
507*c2c66affSColin Finck 
508*c2c66affSColin Finck 	assert(dsb->buf_mixpos + len <= dsb->tmp_buffer_len);
509*c2c66affSColin Finck 
510*c2c66affSColin Finck 	if (len % dsb->device->pwfx->nBlockAlign) {
511*c2c66affSColin Finck 		INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
512*c2c66affSColin Finck 		ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
513*c2c66affSColin Finck 		len -= len % nBlockAlign; /* data alignment */
514*c2c66affSColin Finck 	}
515*c2c66affSColin Finck 
516*c2c66affSColin Finck 	/* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
517*c2c66affSColin Finck 	DSOUND_MixToTemporary(dsb, dsb->sec_mixpos, DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos+len) - dsb->sec_mixpos, TRUE);
518*c2c66affSColin Finck 	if (dsb->resampleinmixer)
519*c2c66affSColin Finck 		ibuf = dsb->device->tmp_buffer;
520*c2c66affSColin Finck 
521*c2c66affSColin Finck 	/* Apply volume if needed */
522*c2c66affSColin Finck 	volbuf = DSOUND_MixerVol(dsb, len);
523*c2c66affSColin Finck 	if (volbuf)
524*c2c66affSColin Finck 		ibuf = volbuf;
525*c2c66affSColin Finck 
526*c2c66affSColin Finck 	mixbufpos = DSOUND_bufpos_to_mixpos(dsb->device, writepos);
527*c2c66affSColin Finck 	/* Now mix the temporary buffer into the devices main buffer */
528*c2c66affSColin Finck 	if ((writepos + len) <= dsb->device->buflen)
529*c2c66affSColin Finck 		dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, len);
530*c2c66affSColin Finck 	else
531*c2c66affSColin Finck 	{
532*c2c66affSColin Finck 		DWORD todo = dsb->device->buflen - writepos;
533*c2c66affSColin Finck 		dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, todo);
534*c2c66affSColin Finck 		dsb->device->mixfunction(ibuf + todo, dsb->device->mix_buffer, len - todo);
535*c2c66affSColin Finck 	}
536*c2c66affSColin Finck 
537*c2c66affSColin Finck 	oldpos = dsb->sec_mixpos;
538*c2c66affSColin Finck 	dsb->buf_mixpos += len;
539*c2c66affSColin Finck 
540*c2c66affSColin Finck 	if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
541*c2c66affSColin Finck 		if (dsb->buf_mixpos > dsb->tmp_buffer_len)
542*c2c66affSColin Finck 			ERR("Mixpos (%u) past buflen (%u), capping...\n", dsb->buf_mixpos, dsb->tmp_buffer_len);
543*c2c66affSColin Finck 		if (dsb->playflags & DSBPLAY_LOOPING) {
544*c2c66affSColin Finck 			dsb->buf_mixpos -= dsb->tmp_buffer_len;
545*c2c66affSColin Finck 		} else if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
546*c2c66affSColin Finck 			dsb->buf_mixpos = dsb->sec_mixpos = 0;
547*c2c66affSColin Finck 			dsb->state = STATE_STOPPED;
548*c2c66affSColin Finck 		}
549*c2c66affSColin Finck 		DSOUND_RecalcFreqAcc(dsb);
550*c2c66affSColin Finck 	}
551*c2c66affSColin Finck 
552*c2c66affSColin Finck 	dsb->sec_mixpos = DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos);
553*c2c66affSColin Finck 	ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
554*c2c66affSColin Finck 	/* check for notification positions */
555*c2c66affSColin Finck 	if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
556*c2c66affSColin Finck 	    dsb->state != STATE_STARTING) {
557*c2c66affSColin Finck 		DSOUND_CheckEvent(dsb, oldpos, ilen);
558*c2c66affSColin Finck 	}
559*c2c66affSColin Finck 
560*c2c66affSColin Finck 	/* increase mix position */
561*c2c66affSColin Finck 	dsb->primary_mixpos += len;
562*c2c66affSColin Finck 	if (dsb->primary_mixpos >= dsb->device->buflen)
563*c2c66affSColin Finck 		dsb->primary_mixpos -= dsb->device->buflen;
564*c2c66affSColin Finck 	return len;
565*c2c66affSColin Finck }
566*c2c66affSColin Finck 
567*c2c66affSColin Finck /**
568*c2c66affSColin Finck  * Mix some frames from the given secondary buffer "dsb" into the device
569*c2c66affSColin Finck  * primary buffer.
570*c2c66affSColin Finck  *
571*c2c66affSColin Finck  * dsb = the secondary buffer
572*c2c66affSColin Finck  * playpos = the current play position in the device buffer (primary buffer)
573*c2c66affSColin Finck  * writepos = the current safe-to-write position in the device buffer
574*c2c66affSColin Finck  * mixlen = the maximum number of bytes in the primary buffer to mix, from the
575*c2c66affSColin Finck  *          current writepos.
576*c2c66affSColin Finck  *
577*c2c66affSColin Finck  * Returns: the number of bytes beyond the writepos that were mixed.
578*c2c66affSColin Finck  */
DSOUND_MixOne(IDirectSoundBufferImpl * dsb,DWORD writepos,DWORD mixlen)579*c2c66affSColin Finck static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
580*c2c66affSColin Finck {
581*c2c66affSColin Finck 	/* The buffer's primary_mixpos may be before or after the device
582*c2c66affSColin Finck 	 * buffer's mixpos, but both must be ahead of writepos. */
583*c2c66affSColin Finck 	DWORD primary_done;
584*c2c66affSColin Finck 
585*c2c66affSColin Finck 	TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
586*c2c66affSColin Finck 	TRACE("writepos=%d, buf_mixpos=%d, primary_mixpos=%d, mixlen=%d\n", writepos, dsb->buf_mixpos, dsb->primary_mixpos, mixlen);
587*c2c66affSColin Finck 	TRACE("looping=%d, leadin=%d, buflen=%d\n", dsb->playflags, dsb->leadin, dsb->tmp_buffer_len);
588*c2c66affSColin Finck 
589*c2c66affSColin Finck 	/* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
590*c2c66affSColin Finck 	if (dsb->leadin && dsb->state == STATE_STARTING)
591*c2c66affSColin Finck 	{
592*c2c66affSColin Finck 		if (mixlen > 2 * dsb->device->fraglen)
593*c2c66affSColin Finck 		{
594*c2c66affSColin Finck 			dsb->primary_mixpos += mixlen - 2 * dsb->device->fraglen;
595*c2c66affSColin Finck 			dsb->primary_mixpos %= dsb->device->buflen;
596*c2c66affSColin Finck 		}
597*c2c66affSColin Finck 	}
598*c2c66affSColin Finck 	dsb->leadin = FALSE;
599*c2c66affSColin Finck 
600*c2c66affSColin Finck 	/* calculate how much pre-buffering has already been done for this buffer */
601*c2c66affSColin Finck 	primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
602*c2c66affSColin Finck 
603*c2c66affSColin Finck 	/* sanity */
604*c2c66affSColin Finck 	if(mixlen < primary_done)
605*c2c66affSColin Finck 	{
606*c2c66affSColin Finck 		/* Should *NEVER* happen */
607*c2c66affSColin Finck 		ERR("Fatal error. Under/Overflow? primary_done=%d, mixpos=%d/%d (%d/%d), primary_mixpos=%d, writepos=%d, mixlen=%d\n", primary_done,dsb->buf_mixpos,dsb->tmp_buffer_len,dsb->sec_mixpos, dsb->buflen, dsb->primary_mixpos, writepos, mixlen);
608*c2c66affSColin Finck 		dsb->primary_mixpos = writepos + mixlen;
609*c2c66affSColin Finck 		dsb->primary_mixpos %= dsb->device->buflen;
610*c2c66affSColin Finck 		return mixlen;
611*c2c66affSColin Finck 	}
612*c2c66affSColin Finck 
613*c2c66affSColin Finck 	/* take into account already mixed data */
614*c2c66affSColin Finck 	mixlen -= primary_done;
615*c2c66affSColin Finck 
616*c2c66affSColin Finck 	TRACE("primary_done=%d, mixlen (primary) = %i\n", primary_done, mixlen);
617*c2c66affSColin Finck 
618*c2c66affSColin Finck 	if (!mixlen)
619*c2c66affSColin Finck 		return primary_done;
620*c2c66affSColin Finck 
621*c2c66affSColin Finck 	/* First try to mix to the end of the buffer if possible
622*c2c66affSColin Finck 	 * Theoretically it would allow for better optimization
623*c2c66affSColin Finck 	*/
624*c2c66affSColin Finck 	if (mixlen + dsb->buf_mixpos >= dsb->tmp_buffer_len)
625*c2c66affSColin Finck 	{
626*c2c66affSColin Finck 		DWORD newmixed, mixfirst = dsb->tmp_buffer_len - dsb->buf_mixpos;
627*c2c66affSColin Finck 		newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
628*c2c66affSColin Finck 		mixlen -= newmixed;
629*c2c66affSColin Finck 
630*c2c66affSColin Finck 		if (dsb->playflags & DSBPLAY_LOOPING)
631*c2c66affSColin Finck 			while (newmixed && mixlen)
632*c2c66affSColin Finck 			{
633*c2c66affSColin Finck 				mixfirst = (dsb->tmp_buffer_len < mixlen ? dsb->tmp_buffer_len : mixlen);
634*c2c66affSColin Finck 				newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
635*c2c66affSColin Finck 				mixlen -= newmixed;
636*c2c66affSColin Finck 			}
637*c2c66affSColin Finck 	}
638*c2c66affSColin Finck 	else DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixlen);
639*c2c66affSColin Finck 
640*c2c66affSColin Finck 	/* re-calculate the primary done */
641*c2c66affSColin Finck 	primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
642*c2c66affSColin Finck 
643*c2c66affSColin Finck 	TRACE("new primary_mixpos=%d, total mixed data=%d\n", dsb->primary_mixpos, primary_done);
644*c2c66affSColin Finck 
645*c2c66affSColin Finck 	/* Report back the total prebuffered amount for this buffer */
646*c2c66affSColin Finck 	return primary_done;
647*c2c66affSColin Finck }
648*c2c66affSColin Finck 
649*c2c66affSColin Finck /**
650*c2c66affSColin Finck  * For a DirectSoundDevice, go through all the currently playing buffers and
651*c2c66affSColin Finck  * mix them in to the device buffer.
652*c2c66affSColin Finck  *
653*c2c66affSColin Finck  * writepos = the current safe-to-write position in the primary buffer
654*c2c66affSColin Finck  * mixlen = the maximum amount to mix into the primary buffer
655*c2c66affSColin Finck  *          (beyond the current writepos)
656*c2c66affSColin Finck  * recover = true if the sound device may have been reset and the write
657*c2c66affSColin Finck  *           position in the device buffer changed
658*c2c66affSColin Finck  * all_stopped = reports back if all buffers have stopped
659*c2c66affSColin Finck  *
660*c2c66affSColin Finck  * Returns:  the length beyond the writepos that was mixed to.
661*c2c66affSColin Finck  */
662*c2c66affSColin Finck 
DSOUND_MixToPrimary(const DirectSoundDevice * device,DWORD writepos,DWORD mixlen,BOOL recover,BOOL * all_stopped)663*c2c66affSColin Finck static DWORD DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos, DWORD mixlen, BOOL recover, BOOL *all_stopped)
664*c2c66affSColin Finck {
665*c2c66affSColin Finck 	INT i, len;
666*c2c66affSColin Finck 	DWORD minlen = 0;
667*c2c66affSColin Finck 	IDirectSoundBufferImpl	*dsb;
668*c2c66affSColin Finck 
669*c2c66affSColin Finck 	/* unless we find a running buffer, all have stopped */
670*c2c66affSColin Finck 	*all_stopped = TRUE;
671*c2c66affSColin Finck 
672*c2c66affSColin Finck 	TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
673*c2c66affSColin Finck 	for (i = 0; i < device->nrofbuffers; i++) {
674*c2c66affSColin Finck 		dsb = device->buffers[i];
675*c2c66affSColin Finck 
676*c2c66affSColin Finck 		TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
677*c2c66affSColin Finck 
678*c2c66affSColin Finck 		if (dsb->buflen && dsb->state && !dsb->hwbuf) {
679*c2c66affSColin Finck 			TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
680*c2c66affSColin Finck 			RtlAcquireResourceShared(&dsb->lock, TRUE);
681*c2c66affSColin Finck 			/* if buffer is stopping it is stopped now */
682*c2c66affSColin Finck 			if (dsb->state == STATE_STOPPING) {
683*c2c66affSColin Finck 				dsb->state = STATE_STOPPED;
684*c2c66affSColin Finck 				DSOUND_CheckEvent(dsb, 0, 0);
685*c2c66affSColin Finck 			} else if (dsb->state != STATE_STOPPED) {
686*c2c66affSColin Finck 
687*c2c66affSColin Finck 				/* if recovering, reset the mix position */
688*c2c66affSColin Finck 				if ((dsb->state == STATE_STARTING) || recover) {
689*c2c66affSColin Finck 					dsb->primary_mixpos = writepos;
690*c2c66affSColin Finck 				}
691*c2c66affSColin Finck 
692*c2c66affSColin Finck 				/* if the buffer was starting, it must be playing now */
693*c2c66affSColin Finck 				if (dsb->state == STATE_STARTING)
694*c2c66affSColin Finck 					dsb->state = STATE_PLAYING;
695*c2c66affSColin Finck 
696*c2c66affSColin Finck 				/* mix next buffer into the main buffer */
697*c2c66affSColin Finck 				len = DSOUND_MixOne(dsb, writepos, mixlen);
698*c2c66affSColin Finck 
699*c2c66affSColin Finck 				if (!minlen) minlen = len;
700*c2c66affSColin Finck 
701*c2c66affSColin Finck 				/* record the minimum length mixed from all buffers */
702*c2c66affSColin Finck 				/* we only want to return the length which *all* buffers have mixed */
703*c2c66affSColin Finck 				else if (len) minlen = (len < minlen) ? len : minlen;
704*c2c66affSColin Finck 
705*c2c66affSColin Finck 				*all_stopped = FALSE;
706*c2c66affSColin Finck 			}
707*c2c66affSColin Finck 			RtlReleaseResource(&dsb->lock);
708*c2c66affSColin Finck 		}
709*c2c66affSColin Finck 	}
710*c2c66affSColin Finck 
711*c2c66affSColin Finck 	TRACE("Mixed at least %d from all buffers\n", minlen);
712*c2c66affSColin Finck 	return minlen;
713*c2c66affSColin Finck }
714*c2c66affSColin Finck 
715*c2c66affSColin Finck /**
716*c2c66affSColin Finck  * Add buffers to the emulated wave device system.
717*c2c66affSColin Finck  *
718*c2c66affSColin Finck  * device = The current dsound playback device
719*c2c66affSColin Finck  * force = If TRUE, the function will buffer up as many frags as possible,
720*c2c66affSColin Finck  *         even though and will ignore the actual state of the primary buffer.
721*c2c66affSColin Finck  *
722*c2c66affSColin Finck  * Returns:  None
723*c2c66affSColin Finck  */
724*c2c66affSColin Finck 
DSOUND_WaveQueue(DirectSoundDevice * device,BOOL force)725*c2c66affSColin Finck static void DSOUND_WaveQueue(DirectSoundDevice *device, BOOL force)
726*c2c66affSColin Finck {
727*c2c66affSColin Finck 	DWORD prebuf_frags, wave_writepos, wave_fragpos, i;
728*c2c66affSColin Finck 	TRACE("(%p)\n", device);
729*c2c66affSColin Finck 
730*c2c66affSColin Finck 	/* calculate the current wave frag position */
731*c2c66affSColin Finck 	wave_fragpos = (device->pwplay + device->pwqueue) % device->helfrags;
732*c2c66affSColin Finck 
733*c2c66affSColin Finck 	/* calculate the current wave write position */
734*c2c66affSColin Finck 	wave_writepos = wave_fragpos * device->fraglen;
735*c2c66affSColin Finck 
736*c2c66affSColin Finck 	TRACE("wave_fragpos = %i, wave_writepos = %i, pwqueue = %i, prebuf = %i\n",
737*c2c66affSColin Finck 		wave_fragpos, wave_writepos, device->pwqueue, device->prebuf);
738*c2c66affSColin Finck 
739*c2c66affSColin Finck 	if (!force)
740*c2c66affSColin Finck 	{
741*c2c66affSColin Finck 		/* check remaining prebuffered frags */
742*c2c66affSColin Finck 		prebuf_frags = device->mixpos / device->fraglen;
743*c2c66affSColin Finck 		if (prebuf_frags == device->helfrags)
744*c2c66affSColin Finck 			--prebuf_frags;
745*c2c66affSColin Finck 		TRACE("wave_fragpos = %d, mixpos_frags = %d\n", wave_fragpos, prebuf_frags);
746*c2c66affSColin Finck 		if (prebuf_frags < wave_fragpos)
747*c2c66affSColin Finck 			prebuf_frags += device->helfrags;
748*c2c66affSColin Finck 		prebuf_frags -= wave_fragpos;
749*c2c66affSColin Finck 		TRACE("wanted prebuf_frags = %d\n", prebuf_frags);
750*c2c66affSColin Finck 	}
751*c2c66affSColin Finck 	else
752*c2c66affSColin Finck 		/* buffer the maximum amount of frags */
753*c2c66affSColin Finck 		prebuf_frags = device->prebuf;
754*c2c66affSColin Finck 
755*c2c66affSColin Finck 	/* limit to the queue we have left */
756*c2c66affSColin Finck 	if ((prebuf_frags + device->pwqueue) > device->prebuf)
757*c2c66affSColin Finck 		prebuf_frags = device->prebuf - device->pwqueue;
758*c2c66affSColin Finck 
759*c2c66affSColin Finck 	TRACE("prebuf_frags = %i\n", prebuf_frags);
760*c2c66affSColin Finck 
761*c2c66affSColin Finck 	/* adjust queue */
762*c2c66affSColin Finck 	device->pwqueue += prebuf_frags;
763*c2c66affSColin Finck 
764*c2c66affSColin Finck 	/* get out of CS when calling the wave system */
765*c2c66affSColin Finck 	LeaveCriticalSection(&(device->mixlock));
766*c2c66affSColin Finck 	/* **** */
767*c2c66affSColin Finck 
768*c2c66affSColin Finck 	/* queue up the new buffers */
769*c2c66affSColin Finck 	for(i=0; i<prebuf_frags; i++){
770*c2c66affSColin Finck 		TRACE("queueing wave buffer %i\n", wave_fragpos);
771*c2c66affSColin Finck 		waveOutWrite(device->hwo, &device->pwave[wave_fragpos], sizeof(WAVEHDR));
772*c2c66affSColin Finck 		wave_fragpos++;
773*c2c66affSColin Finck 		wave_fragpos %= device->helfrags;
774*c2c66affSColin Finck 	}
775*c2c66affSColin Finck 
776*c2c66affSColin Finck 	/* **** */
777*c2c66affSColin Finck 	EnterCriticalSection(&(device->mixlock));
778*c2c66affSColin Finck 
779*c2c66affSColin Finck 	TRACE("queue now = %i\n", device->pwqueue);
780*c2c66affSColin Finck }
781*c2c66affSColin Finck 
782*c2c66affSColin Finck /**
783*c2c66affSColin Finck  * Perform mixing for a Direct Sound device. That is, go through all the
784*c2c66affSColin Finck  * secondary buffers (the sound bites currently playing) and mix them in
785*c2c66affSColin Finck  * to the primary buffer (the device buffer).
786*c2c66affSColin Finck  */
DSOUND_PerformMix(DirectSoundDevice * device)787*c2c66affSColin Finck static void DSOUND_PerformMix(DirectSoundDevice *device)
788*c2c66affSColin Finck {
789*c2c66affSColin Finck 	TRACE("(%p)\n", device);
790*c2c66affSColin Finck 
791*c2c66affSColin Finck 	/* **** */
792*c2c66affSColin Finck 	EnterCriticalSection(&(device->mixlock));
793*c2c66affSColin Finck 
794*c2c66affSColin Finck 	if (device->priolevel != DSSCL_WRITEPRIMARY) {
795*c2c66affSColin Finck 		BOOL recover = FALSE, all_stopped = FALSE;
796*c2c66affSColin Finck 		DWORD playpos, writepos, writelead, maxq, frag, prebuff_max, prebuff_left, size1, size2, mixplaypos, mixplaypos2;
797*c2c66affSColin Finck 		LPVOID buf1, buf2;
798*c2c66affSColin Finck 		BOOL lock = (device->hwbuf && !(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK));
799*c2c66affSColin Finck 		int nfiller;
800*c2c66affSColin Finck 
801*c2c66affSColin Finck 		/* the sound of silence */
802*c2c66affSColin Finck 		nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
803*c2c66affSColin Finck 
804*c2c66affSColin Finck 		/* get the position in the primary buffer */
805*c2c66affSColin Finck 		if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){
806*c2c66affSColin Finck 			LeaveCriticalSection(&(device->mixlock));
807*c2c66affSColin Finck 			return;
808*c2c66affSColin Finck 		}
809*c2c66affSColin Finck 
810*c2c66affSColin Finck 		TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
811*c2c66affSColin Finck 		      playpos,writepos,device->playpos,device->mixpos,device->buflen);
812*c2c66affSColin Finck 		assert(device->playpos < device->buflen);
813*c2c66affSColin Finck 
814*c2c66affSColin Finck 		mixplaypos = DSOUND_bufpos_to_mixpos(device, device->playpos);
815*c2c66affSColin Finck 		mixplaypos2 = DSOUND_bufpos_to_mixpos(device, playpos);
816*c2c66affSColin Finck 
817*c2c66affSColin Finck 		/* calc maximum prebuff */
818*c2c66affSColin Finck 		prebuff_max = (device->prebuf * device->fraglen);
819*c2c66affSColin Finck 		if (!device->hwbuf && playpos + prebuff_max >= device->helfrags * device->fraglen)
820*c2c66affSColin Finck 			prebuff_max += device->buflen - device->helfrags * device->fraglen;
821*c2c66affSColin Finck 
822*c2c66affSColin Finck 		/* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
823*c2c66affSColin Finck 		prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
824*c2c66affSColin Finck 		writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos);
825*c2c66affSColin Finck 
826*c2c66affSColin Finck 		/* check for underrun. underrun occurs when the write position passes the mix position
827*c2c66affSColin Finck 		 * also wipe out just-played sound data */
828*c2c66affSColin Finck 		if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
829*c2c66affSColin Finck 			if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
830*c2c66affSColin Finck 				WARN("Probable buffer underrun\n");
831*c2c66affSColin Finck 			else TRACE("Buffer starting or buffer underrun\n");
832*c2c66affSColin Finck 
833*c2c66affSColin Finck 			/* recover mixing for all buffers */
834*c2c66affSColin Finck 			recover = TRUE;
835*c2c66affSColin Finck 
836*c2c66affSColin Finck 			/* reset mix position to write position */
837*c2c66affSColin Finck 			device->mixpos = writepos;
838*c2c66affSColin Finck 
839*c2c66affSColin Finck 			ZeroMemory(device->mix_buffer, device->mix_buffer_len);
840*c2c66affSColin Finck 			ZeroMemory(device->buffer, device->buflen);
841*c2c66affSColin Finck 		} else if (playpos < device->playpos) {
842*c2c66affSColin Finck 			buf1 = device->buffer + device->playpos;
843*c2c66affSColin Finck 			buf2 = device->buffer;
844*c2c66affSColin Finck 			size1 = device->buflen - device->playpos;
845*c2c66affSColin Finck 			size2 = playpos;
846*c2c66affSColin Finck 			FillMemory(device->mix_buffer + mixplaypos, device->mix_buffer_len - mixplaypos, 0);
847*c2c66affSColin Finck 			FillMemory(device->mix_buffer, mixplaypos2, 0);
848*c2c66affSColin Finck 			if (lock)
849*c2c66affSColin Finck 				IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
850*c2c66affSColin Finck 			FillMemory(buf1, size1, nfiller);
851*c2c66affSColin Finck 			if (playpos && (!buf2 || !size2))
852*c2c66affSColin Finck 				FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__, device->playpos, device->mixpos, playpos, writepos);
853*c2c66affSColin Finck 			FillMemory(buf2, size2, nfiller);
854*c2c66affSColin Finck 			if (lock)
855*c2c66affSColin Finck 				IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
856*c2c66affSColin Finck 		} else {
857*c2c66affSColin Finck 			buf1 = device->buffer + device->playpos;
858*c2c66affSColin Finck 			buf2 = NULL;
859*c2c66affSColin Finck 			size1 = playpos - device->playpos;
860*c2c66affSColin Finck 			size2 = 0;
861*c2c66affSColin Finck 			FillMemory(device->mix_buffer + mixplaypos, mixplaypos2 - mixplaypos, 0);
862*c2c66affSColin Finck 			if (lock)
863*c2c66affSColin Finck 				IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
864*c2c66affSColin Finck 			FillMemory(buf1, size1, nfiller);
865*c2c66affSColin Finck 			if (buf2 && size2)
866*c2c66affSColin Finck 			{
867*c2c66affSColin Finck 				FIXME("%d: There should be no additional buffer here!!\n", __LINE__);
868*c2c66affSColin Finck 				FillMemory(buf2, size2, nfiller);
869*c2c66affSColin Finck 			}
870*c2c66affSColin Finck 			if (lock)
871*c2c66affSColin Finck 				IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
872*c2c66affSColin Finck 		}
873*c2c66affSColin Finck 		device->playpos = playpos;
874*c2c66affSColin Finck 
875*c2c66affSColin Finck 		/* find the maximum we can prebuffer from current write position */
876*c2c66affSColin Finck 		maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0;
877*c2c66affSColin Finck 
878*c2c66affSColin Finck 		TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
879*c2c66affSColin Finck 			prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead);
880*c2c66affSColin Finck 
881*c2c66affSColin Finck 		if (lock)
882*c2c66affSColin Finck 			IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, writepos, maxq, 0);
883*c2c66affSColin Finck 
884*c2c66affSColin Finck 		/* do the mixing */
885*c2c66affSColin Finck 		frag = DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped);
886*c2c66affSColin Finck 
887*c2c66affSColin Finck 		if (frag + writepos > device->buflen)
888*c2c66affSColin Finck 		{
889*c2c66affSColin Finck 			DWORD todo = device->buflen - writepos;
890*c2c66affSColin Finck 			device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, todo);
891*c2c66affSColin Finck 			device->normfunction(device->mix_buffer, device->buffer, frag - todo);
892*c2c66affSColin Finck 		}
893*c2c66affSColin Finck 		else
894*c2c66affSColin Finck 			device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, frag);
895*c2c66affSColin Finck 
896*c2c66affSColin Finck 		/* update the mix position, taking wrap-around into account */
897*c2c66affSColin Finck 		device->mixpos = writepos + frag;
898*c2c66affSColin Finck 		device->mixpos %= device->buflen;
899*c2c66affSColin Finck 
900*c2c66affSColin Finck 		if (lock)
901*c2c66affSColin Finck 		{
902*c2c66affSColin Finck 			DWORD frag2 = (frag > size1 ? frag - size1 : 0);
903*c2c66affSColin Finck 			frag -= frag2;
904*c2c66affSColin Finck 			if (frag2 > size2)
905*c2c66affSColin Finck 			{
906*c2c66affSColin Finck 				FIXME("Buffering too much! (%d, %d, %d, %d)\n", maxq, frag, size2, frag2 - size2);
907*c2c66affSColin Finck 				frag2 = size2;
908*c2c66affSColin Finck 			}
909*c2c66affSColin Finck 			IDsDriverBuffer_Unlock(device->hwbuf, buf1, frag, buf2, frag2);
910*c2c66affSColin Finck 		}
911*c2c66affSColin Finck 
912*c2c66affSColin Finck 		/* update prebuff left */
913*c2c66affSColin Finck 		prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
914*c2c66affSColin Finck 
915*c2c66affSColin Finck 		/* check if have a whole fragment */
916*c2c66affSColin Finck 		if (prebuff_left >= device->fraglen){
917*c2c66affSColin Finck 
918*c2c66affSColin Finck 			/* update the wave queue if using wave system */
919*c2c66affSColin Finck 			if (!device->hwbuf)
920*c2c66affSColin Finck 				DSOUND_WaveQueue(device, FALSE);
921*c2c66affSColin Finck 
922*c2c66affSColin Finck 			/* buffers are full. start playing if applicable */
923*c2c66affSColin Finck 			if(device->state == STATE_STARTING){
924*c2c66affSColin Finck 				TRACE("started primary buffer\n");
925*c2c66affSColin Finck 				if(DSOUND_PrimaryPlay(device) != DS_OK){
926*c2c66affSColin Finck 					WARN("DSOUND_PrimaryPlay failed\n");
927*c2c66affSColin Finck 				}
928*c2c66affSColin Finck 				else{
929*c2c66affSColin Finck 					/* we are playing now */
930*c2c66affSColin Finck 					device->state = STATE_PLAYING;
931*c2c66affSColin Finck 				}
932*c2c66affSColin Finck 			}
933*c2c66affSColin Finck 
934*c2c66affSColin Finck 			/* buffers are full. start stopping if applicable */
935*c2c66affSColin Finck 			if(device->state == STATE_STOPPED){
936*c2c66affSColin Finck 				TRACE("restarting primary buffer\n");
937*c2c66affSColin Finck 				if(DSOUND_PrimaryPlay(device) != DS_OK){
938*c2c66affSColin Finck 					WARN("DSOUND_PrimaryPlay failed\n");
939*c2c66affSColin Finck 				}
940*c2c66affSColin Finck 				else{
941*c2c66affSColin Finck 					/* start stopping again. as soon as there is no more data, it will stop */
942*c2c66affSColin Finck 					device->state = STATE_STOPPING;
943*c2c66affSColin Finck 				}
944*c2c66affSColin Finck 			}
945*c2c66affSColin Finck 		}
946*c2c66affSColin Finck 
947*c2c66affSColin Finck 		/* if device was stopping, its for sure stopped when all buffers have stopped */
948*c2c66affSColin Finck 		else if((all_stopped != FALSE) && (device->state == STATE_STOPPING)){
949*c2c66affSColin Finck 			TRACE("All buffers have stopped. Stopping primary buffer\n");
950*c2c66affSColin Finck 			device->state = STATE_STOPPED;
951*c2c66affSColin Finck 
952*c2c66affSColin Finck 			/* stop the primary buffer now */
953*c2c66affSColin Finck 			DSOUND_PrimaryStop(device);
954*c2c66affSColin Finck 		}
955*c2c66affSColin Finck 
956*c2c66affSColin Finck 	} else {
957*c2c66affSColin Finck 
958*c2c66affSColin Finck 		/* update the wave queue if using wave system */
959*c2c66affSColin Finck 		if (!device->hwbuf)
960*c2c66affSColin Finck 			DSOUND_WaveQueue(device, TRUE);
961*c2c66affSColin Finck 		else
962*c2c66affSColin Finck 			/* Keep alsa happy, which needs GetPosition called once every 10 ms */
963*c2c66affSColin Finck 			IDsDriverBuffer_GetPosition(device->hwbuf, NULL, NULL);
964*c2c66affSColin Finck 
965*c2c66affSColin Finck 		/* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
966*c2c66affSColin Finck 		if (device->state == STATE_STARTING) {
967*c2c66affSColin Finck 			if (DSOUND_PrimaryPlay(device) != DS_OK)
968*c2c66affSColin Finck 				WARN("DSOUND_PrimaryPlay failed\n");
969*c2c66affSColin Finck 			else
970*c2c66affSColin Finck 				device->state = STATE_PLAYING;
971*c2c66affSColin Finck 		}
972*c2c66affSColin Finck 		else if (device->state == STATE_STOPPING) {
973*c2c66affSColin Finck 			if (DSOUND_PrimaryStop(device) != DS_OK)
974*c2c66affSColin Finck 				WARN("DSOUND_PrimaryStop failed\n");
975*c2c66affSColin Finck 			else
976*c2c66affSColin Finck 				device->state = STATE_STOPPED;
977*c2c66affSColin Finck 		}
978*c2c66affSColin Finck 	}
979*c2c66affSColin Finck 
980*c2c66affSColin Finck 	LeaveCriticalSection(&(device->mixlock));
981*c2c66affSColin Finck 	/* **** */
982*c2c66affSColin Finck }
983*c2c66affSColin Finck 
DSOUND_timer(UINT timerID,UINT msg,DWORD_PTR dwUser,DWORD_PTR dw1,DWORD_PTR dw2)984*c2c66affSColin Finck void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
985*c2c66affSColin Finck                            DWORD_PTR dw1, DWORD_PTR dw2)
986*c2c66affSColin Finck {
987*c2c66affSColin Finck 	DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
988*c2c66affSColin Finck 	DWORD start_time =  GetTickCount();
989*c2c66affSColin Finck 	DWORD end_time;
990*c2c66affSColin Finck 	TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
991*c2c66affSColin Finck 	TRACE("entering at %d\n", start_time);
992*c2c66affSColin Finck 
993*c2c66affSColin Finck 	if (DSOUND_renderer[device->drvdesc.dnDevNode] != device) {
994*c2c66affSColin Finck 		ERR("dsound died without killing us?\n");
995*c2c66affSColin Finck 		timeKillEvent(timerID);
996*c2c66affSColin Finck 		timeEndPeriod(DS_TIME_RES);
997*c2c66affSColin Finck 		return;
998*c2c66affSColin Finck 	}
999*c2c66affSColin Finck 
1000*c2c66affSColin Finck 	RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
1001*c2c66affSColin Finck 
1002*c2c66affSColin Finck 	if (device->ref)
1003*c2c66affSColin Finck 		DSOUND_PerformMix(device);
1004*c2c66affSColin Finck 
1005*c2c66affSColin Finck 	RtlReleaseResource(&(device->buffer_list_lock));
1006*c2c66affSColin Finck 
1007*c2c66affSColin Finck 	end_time = GetTickCount();
1008*c2c66affSColin Finck 	TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
1009*c2c66affSColin Finck }
1010*c2c66affSColin Finck 
DSOUND_callback(HWAVEOUT hwo,UINT msg,DWORD_PTR dwUser,DWORD_PTR dw1,DWORD_PTR dw2)1011*c2c66affSColin Finck void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1012*c2c66affSColin Finck {
1013*c2c66affSColin Finck 	DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
1014*c2c66affSColin Finck 	TRACE("(%p,%x,%lx,%lx,%lx)\n",hwo,msg,dwUser,dw1,dw2);
1015*c2c66affSColin Finck 	TRACE("entering at %d, msg=%08x(%s)\n", GetTickCount(), msg,
1016*c2c66affSColin Finck 		msg==MM_WOM_DONE ? "MM_WOM_DONE" : msg==MM_WOM_CLOSE ? "MM_WOM_CLOSE" :
1017*c2c66affSColin Finck 		msg==MM_WOM_OPEN ? "MM_WOM_OPEN" : "UNKNOWN");
1018*c2c66affSColin Finck 
1019*c2c66affSColin Finck 	/* check if packet completed from wave driver */
1020*c2c66affSColin Finck 	if (msg == MM_WOM_DONE) {
1021*c2c66affSColin Finck 
1022*c2c66affSColin Finck 		/* **** */
1023*c2c66affSColin Finck 		EnterCriticalSection(&(device->mixlock));
1024*c2c66affSColin Finck 
1025*c2c66affSColin Finck 		TRACE("done playing primary pos=%d\n", device->pwplay * device->fraglen);
1026*c2c66affSColin Finck 
1027*c2c66affSColin Finck 		/* update playpos */
1028*c2c66affSColin Finck 		device->pwplay++;
1029*c2c66affSColin Finck 		device->pwplay %= device->helfrags;
1030*c2c66affSColin Finck 
1031*c2c66affSColin Finck 		/* sanity */
1032*c2c66affSColin Finck 		if(device->pwqueue == 0){
1033*c2c66affSColin Finck 			ERR("Wave queue corrupted!\n");
1034*c2c66affSColin Finck 		}
1035*c2c66affSColin Finck 
1036*c2c66affSColin Finck 		/* update queue */
1037*c2c66affSColin Finck 		device->pwqueue--;
1038*c2c66affSColin Finck 
1039*c2c66affSColin Finck 		LeaveCriticalSection(&(device->mixlock));
1040*c2c66affSColin Finck 		/* **** */
1041*c2c66affSColin Finck 	}
1042*c2c66affSColin Finck 	TRACE("completed\n");
1043*c2c66affSColin Finck }
1044