Visual C++ backend for rbuild (for now just a hacked mingw backend) and related compi...
[reactos.git] / dll / directx / dsound / buffer.c
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <stdarg.h>
23
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "mmsystem.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "dsound.h"
33 #include "dsdriver.h"
34 #include "dsound_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
37
38 /*******************************************************************************
39 * IDirectSoundNotify
40 */
41 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
42 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
43 ) {
44 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
45 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
46
47 if (This->dsb == NULL) {
48 WARN("invalid parameter\n");
49 return E_INVALIDARG;
50 }
51
52 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
53 }
54
55 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
56 {
57 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
58 ULONG ref = InterlockedIncrement(&(This->ref));
59 TRACE("(%p) ref was %ld\n", This, ref - 1);
60 return ref;
61 }
62
63 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
64 {
65 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
66 ULONG ref = InterlockedDecrement(&(This->ref));
67 TRACE("(%p) ref was %ld\n", This, ref + 1);
68
69 if (!ref) {
70 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
71 This->dsb->notify = NULL;
72 HeapFree(GetProcessHeap(), 0, This);
73 TRACE("(%p) released\n", This);
74 }
75 return ref;
76 }
77
78 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
79 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
80 ) {
81 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
82 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
83
84 if (howmuch > 0 && notify == NULL) {
85 WARN("invalid parameter: notify == NULL\n");
86 return DSERR_INVALIDPARAM;
87 }
88
89 if (TRACE_ON(dsound)) {
90 unsigned int i;
91 for (i=0;i<howmuch;i++)
92 TRACE("notify at %ld to %p\n",
93 notify[i].dwOffset,notify[i].hEventNotify);
94 }
95
96 if (This->dsb->hwnotify) {
97 HRESULT hres;
98 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
99 if (hres != DS_OK)
100 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
101 return hres;
102 } else if (howmuch > 0) {
103 /* Make an internal copy of the caller-supplied array.
104 * Replace the existing copy if one is already present. */
105 if (This->dsb->notifies)
106 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
107 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
108 else
109 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
110 howmuch * sizeof(DSBPOSITIONNOTIFY));
111
112 if (This->dsb->notifies == NULL) {
113 WARN("out of memory\n");
114 return DSERR_OUTOFMEMORY;
115 }
116 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
117 This->dsb->nrofnotifies = howmuch;
118 } else {
119 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
120 This->dsb->notifies = NULL;
121 This->dsb->nrofnotifies = 0;
122 }
123
124 return S_OK;
125 }
126
127 static const IDirectSoundNotifyVtbl dsnvt =
128 {
129 IDirectSoundNotifyImpl_QueryInterface,
130 IDirectSoundNotifyImpl_AddRef,
131 IDirectSoundNotifyImpl_Release,
132 IDirectSoundNotifyImpl_SetNotificationPositions,
133 };
134
135 HRESULT WINAPI IDirectSoundNotifyImpl_Create(
136 IDirectSoundBufferImpl * dsb,
137 IDirectSoundNotifyImpl **pdsn)
138 {
139 IDirectSoundNotifyImpl * dsn;
140 TRACE("(%p,%p)\n",dsb,pdsn);
141
142 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
143
144 if (dsn == NULL) {
145 WARN("out of memory\n");
146 return DSERR_OUTOFMEMORY;
147 }
148
149 dsn->ref = 0;
150 dsn->lpVtbl = &dsnvt;
151 dsn->dsb = dsb;
152 dsb->notify = dsn;
153 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
154
155 *pdsn = dsn;
156 return DS_OK;
157 }
158
159 HRESULT WINAPI IDirectSoundNotifyImpl_Destroy(
160 IDirectSoundNotifyImpl *pdsn)
161 {
162 TRACE("(%p)\n",pdsn);
163
164 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
165
166 return DS_OK;
167 }
168
169 /*******************************************************************************
170 * IDirectSoundBuffer
171 */
172
173 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
174 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
175 ) {
176 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
177
178 TRACE("(%p,%p)\n",This,wfex);
179 /* This method is not available on secondary buffers */
180 WARN("invalid call\n");
181 return DSERR_INVALIDCALL;
182 }
183
184 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
185 LPDIRECTSOUNDBUFFER8 iface,LONG vol
186 ) {
187 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
188 LONG oldVol;
189 HRESULT hres = DS_OK;
190
191 TRACE("(%p,%ld)\n",This,vol);
192
193 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
194 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
195 return DSERR_CONTROLUNAVAIL;
196 }
197
198 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
199 WARN("invalid parameter: vol = %ld\n", vol);
200 return DSERR_INVALIDPARAM;
201 }
202
203 /* **** */
204 EnterCriticalSection(&(This->lock));
205
206 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
207 oldVol = This->ds3db_lVolume;
208 This->ds3db_lVolume = vol;
209 } else {
210 oldVol = This->volpan.lVolume;
211 This->volpan.lVolume = vol;
212 if (vol != oldVol)
213 DSOUND_RecalcVolPan(&(This->volpan));
214 }
215
216 if (vol != oldVol) {
217 if (This->hwbuf) {
218 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
219 if (hres != DS_OK)
220 WARN("IDsDriverBuffer_SetVolumePan failed\n");
221 } else
222 DSOUND_ForceRemix(This);
223 }
224
225 LeaveCriticalSection(&(This->lock));
226 /* **** */
227
228 return hres;
229 }
230
231 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
232 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
233 ) {
234 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
235 TRACE("(%p,%p)\n",This,vol);
236
237 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
238 WARN("control unavailable\n");
239 return DSERR_CONTROLUNAVAIL;
240 }
241
242 if (vol == NULL) {
243 WARN("invalid parameter: vol == NULL\n");
244 return DSERR_INVALIDPARAM;
245 }
246
247 *vol = This->volpan.lVolume;
248
249 return DS_OK;
250 }
251
252 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
253 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
254 ) {
255 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
256 DWORD oldFreq;
257
258 TRACE("(%p,%ld)\n",This,freq);
259
260 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
261 WARN("control unavailable\n");
262 return DSERR_CONTROLUNAVAIL;
263 }
264
265 if (freq == DSBFREQUENCY_ORIGINAL)
266 freq = This->pwfx->nSamplesPerSec;
267
268 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
269 WARN("invalid parameter: freq = %ld\n", freq);
270 return DSERR_INVALIDPARAM;
271 }
272
273 /* **** */
274 EnterCriticalSection(&(This->lock));
275
276 oldFreq = This->freq;
277 This->freq = freq;
278 if (freq != oldFreq) {
279 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->device->pwfx->nSamplesPerSec;
280 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
281 DSOUND_RecalcFormat(This);
282 if (!This->hwbuf)
283 DSOUND_ForceRemix(This);
284 }
285
286 LeaveCriticalSection(&(This->lock));
287 /* **** */
288
289 return DS_OK;
290 }
291
292 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
293 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
294 ) {
295 HRESULT hres = DS_OK;
296 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
297 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
298
299 /* **** */
300 EnterCriticalSection(&(This->lock));
301
302 This->playflags = flags;
303 if (This->state == STATE_STOPPED) {
304 This->leadin = TRUE;
305 This->startpos = This->buf_mixpos;
306 This->state = STATE_STARTING;
307 } else if (This->state == STATE_STOPPING)
308 This->state = STATE_PLAYING;
309 if (This->hwbuf) {
310 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
311 if (hres != DS_OK)
312 WARN("IDsDriverBuffer_Play failed\n");
313 else
314 This->state = STATE_PLAYING;
315 }
316
317 LeaveCriticalSection(&(This->lock));
318 /* **** */
319
320 return hres;
321 }
322
323 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
324 {
325 HRESULT hres = DS_OK;
326 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
327 TRACE("(%p)\n",This);
328
329 /* **** */
330 EnterCriticalSection(&(This->lock));
331
332 if (This->state == STATE_PLAYING)
333 This->state = STATE_STOPPING;
334 else if (This->state == STATE_STARTING)
335 This->state = STATE_STOPPED;
336 if (This->hwbuf) {
337 hres = IDsDriverBuffer_Stop(This->hwbuf);
338 if (hres != DS_OK)
339 WARN("IDsDriverBuffer_Stop failed\n");
340 else
341 This->state = STATE_STOPPED;
342 }
343 DSOUND_CheckEvent(This, 0);
344
345 LeaveCriticalSection(&(This->lock));
346 /* **** */
347
348 return hres;
349 }
350
351 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
352 {
353 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
354 ULONG ref = InterlockedIncrement(&(This->ref));
355 TRACE("(%p) ref was %ld\n", This, ref - 1);
356 return ref;
357 }
358
359 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
360 {
361 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
362 ULONG ref = InterlockedDecrement(&(This->ref));
363 TRACE("(%p) ref was %ld\n", This, ref + 1);
364
365 if (!ref) {
366 DSOUND_RemoveBuffer(This->dsound, This);
367
368 This->lock.DebugInfo->Spare[0] = 0;
369 DeleteCriticalSection(&(This->lock));
370
371 if (This->hwbuf) {
372 IDsDriverBuffer_Release(This->hwbuf);
373 if (This->dsound->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
374 This->buffer->ref--;
375 if (This->buffer->ref==0) {
376 HeapFree(GetProcessHeap(),0,This->buffer->memory);
377 HeapFree(GetProcessHeap(),0,This->buffer);
378 }
379 }
380 } else {
381 This->buffer->ref--;
382 if (This->buffer->ref==0) {
383 HeapFree(GetProcessHeap(),0,This->buffer->memory);
384 HeapFree(GetProcessHeap(),0,This->buffer);
385 }
386 }
387
388 HeapFree(GetProcessHeap(), 0, This->notifies);
389 HeapFree(GetProcessHeap(), 0, This->pwfx);
390 HeapFree(GetProcessHeap(), 0, This);
391
392 TRACE("(%p) released\n", This);
393 }
394 return ref;
395 }
396
397 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This, DWORD pplay, DWORD pwrite)
398 {
399 DWORD bplay = This->buf_mixpos;
400 DWORD pmix = This->primary_mixpos;
401 TRACE("(%p, pplay=%lu, pwrite=%lu)\n", This, pplay, pwrite);
402
403 /* the actual primary play position (pplay) is always behind last mixed (pmix),
404 * unless the computer is too slow or something */
405 /* we need to know how far away we are from there */
406 if (pmix < pplay) pmix += This->dsound->device->buflen; /* wraparound */
407 pmix -= pplay;
408 /* detect buffer underrun */
409 if (pwrite < pplay) pwrite += This->dsound->device->buflen; /* wraparound */
410 pwrite -= pplay;
411 if (pmix > (ds_snd_queue_max * This->dsound->device->fraglen + pwrite + This->dsound->device->writelead)) {
412 WARN("detected an underrun: primary queue was %ld\n",pmix);
413 pmix = 0;
414 }
415 /* divide the offset by its sample size */
416 pmix /= This->dsound->device->pwfx->nBlockAlign;
417 TRACE("primary back-samples=%ld\n",pmix);
418 /* adjust for our frequency */
419 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
420 /* multiply by our own sample size */
421 pmix *= This->pwfx->nBlockAlign;
422 TRACE("this back-offset=%ld\n", pmix);
423 /* subtract from our last mixed position */
424 while (bplay < pmix) bplay += This->buflen; /* wraparound */
425 bplay -= pmix;
426 if (This->leadin && ((bplay < This->startpos) || (bplay > This->buf_mixpos))) {
427 /* seems we haven't started playing yet */
428 TRACE("this still in lead-in phase\n");
429 bplay = This->startpos;
430 }
431 /* return the result */
432 return bplay;
433 }
434
435 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
436 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
437 ) {
438 HRESULT hres;
439 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
440 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
441 if (This->hwbuf) {
442 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
443 if (hres != DS_OK) {
444 WARN("IDsDriverBuffer_GetPosition failed\n");
445 return hres;
446 }
447 } else {
448 if (playpos && (This->state != STATE_PLAYING)) {
449 /* we haven't been merged into the primary buffer (yet) */
450 *playpos = This->buf_mixpos;
451 } else if (playpos) {
452 DWORD pplay, pwrite;
453 /* let's get this exact; first, recursively call GetPosition on the primary */
454 EnterCriticalSection(&(This->dsound->device->mixlock));
455 if (DSOUND_PrimaryGetPosition(This->dsound->device, &pplay, &pwrite) != DS_OK)
456 WARN("DSOUND_PrimaryGetPosition failed\n");
457 /* detect HEL mode underrun */
458 if (!(This->dsound->device->hwbuf || This->dsound->device->pwqueue))
459 TRACE("detected an underrun\n");
460 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->device->hwbuf) {
461 /* calculate play position using this */
462 *playpos = DSOUND_CalcPlayPosition(This, pplay, pwrite);
463 } else {
464 /* (unless the app isn't using GETCURRENTPOSITION2) */
465 /* don't know exactly how this should be handled...
466 * the docs says that play cursor is reported as directly
467 * behind write cursor, hmm... */
468 /* let's just do what might work for Half-Life */
469 DWORD wp;
470 wp = (This->dsound->device->pwplay + ds_hel_margin) * This->dsound->device->fraglen;
471 wp %= This->dsound->device->buflen;
472 *playpos = DSOUND_CalcPlayPosition(This, wp, pwrite);
473 }
474 LeaveCriticalSection(&(This->dsound->device->mixlock));
475 }
476 if (writepos)
477 *writepos = This->buf_mixpos;
478 }
479 if (writepos) {
480 if (This->state != STATE_STOPPED) {
481 /* apply the documented 10ms lead to writepos */
482 *writepos += This->writelead;
483 }
484 *writepos %= This->buflen;
485 }
486 if (playpos)
487 This->last_playpos = *playpos;
488 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
489 return DS_OK;
490 }
491
492 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
493 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
494 ) {
495 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
496 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
497
498 if (status == NULL) {
499 WARN("invalid parameter: status = NULL\n");
500 return DSERR_INVALIDPARAM;
501 }
502
503 *status = 0;
504 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
505 *status |= DSBSTATUS_PLAYING;
506 if (This->playflags & DSBPLAY_LOOPING)
507 *status |= DSBSTATUS_LOOPING;
508 }
509
510 TRACE("status=%lx\n", *status);
511 return DS_OK;
512 }
513
514
515 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
516 LPDIRECTSOUNDBUFFER8 iface,
517 LPWAVEFORMATEX lpwf,
518 DWORD wfsize,
519 LPDWORD wfwritten)
520 {
521 DWORD size;
522 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
523 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
524
525 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
526
527 if (lpwf) { /* NULL is valid */
528 if (wfsize >= size) {
529 CopyMemory(lpwf,This->pwfx,size);
530 if (wfwritten)
531 *wfwritten = size;
532 } else {
533 WARN("invalid parameter: wfsize too small\n");
534 if (wfwritten)
535 *wfwritten = 0;
536 return DSERR_INVALIDPARAM;
537 }
538 } else {
539 if (wfwritten)
540 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
541 else {
542 WARN("invalid parameter: wfwritten == NULL\n");
543 return DSERR_INVALIDPARAM;
544 }
545 }
546
547 return DS_OK;
548 }
549
550 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
551 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
552 ) {
553 HRESULT hres = DS_OK;
554 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
555
556 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
557 This,
558 writecursor,
559 writebytes,
560 lplpaudioptr1,
561 audiobytes1,
562 lplpaudioptr2,
563 audiobytes2,
564 flags,
565 GetTickCount()
566 );
567
568 if (flags & DSBLOCK_FROMWRITECURSOR) {
569 DWORD writepos;
570 /* GetCurrentPosition does too much magic to duplicate here */
571 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
572 if (hres != DS_OK) {
573 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
574 return hres;
575 }
576 writecursor += writepos;
577 }
578 writecursor %= This->buflen;
579 if (flags & DSBLOCK_ENTIREBUFFER)
580 writebytes = This->buflen;
581 if (writebytes > This->buflen)
582 writebytes = This->buflen;
583
584 EnterCriticalSection(&(This->lock));
585
586 if ((writebytes == This->buflen) &&
587 ((This->state == STATE_STARTING) ||
588 (This->state == STATE_PLAYING)))
589 /* some games, like Half-Life, try to be clever (not) and
590 * keep one secondary buffer, and mix sounds into it itself,
591 * locking the entire buffer every time... so we can just forget
592 * about tracking the last-written-to-position... */
593 This->probably_valid_to = (DWORD)-1;
594 else
595 This->probably_valid_to = writecursor;
596
597 if (!(This->dsound->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
598 hres = IDsDriverBuffer_Lock(This->hwbuf,
599 lplpaudioptr1, audiobytes1,
600 lplpaudioptr2, audiobytes2,
601 writecursor, writebytes,
602 0);
603 if (hres != DS_OK) {
604 WARN("IDsDriverBuffer_Lock failed\n");
605 LeaveCriticalSection(&(This->lock));
606 return hres;
607 }
608 } else {
609 BOOL remix = FALSE;
610 if (writecursor+writebytes <= This->buflen) {
611 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
612 *audiobytes1 = writebytes;
613 if (lplpaudioptr2)
614 *(LPBYTE*)lplpaudioptr2 = NULL;
615 if (audiobytes2)
616 *audiobytes2 = 0;
617 TRACE("->%ld.0\n",writebytes);
618 } else {
619 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
620 *audiobytes1 = This->buflen-writecursor;
621 if (lplpaudioptr2)
622 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
623 if (audiobytes2)
624 *audiobytes2 = writebytes-(This->buflen-writecursor);
625 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
626 }
627 if (This->state == STATE_PLAYING) {
628 /* if the segment between playpos and buf_mixpos is touched,
629 * we need to cancel some mixing */
630 /* we'll assume that the app always calls GetCurrentPosition before
631 * locking a playing buffer, so that last_playpos is up-to-date */
632 if (This->buf_mixpos >= This->last_playpos) {
633 if (This->buf_mixpos > writecursor &&
634 This->last_playpos < writecursor+writebytes)
635 remix = TRUE;
636 } else {
637 if (This->buf_mixpos > writecursor ||
638 This->last_playpos < writecursor+writebytes)
639 remix = TRUE;
640 }
641 if (remix) {
642 TRACE("locking prebuffered region, ouch\n");
643 DSOUND_MixCancelAt(This, writecursor);
644 }
645 }
646 }
647
648 LeaveCriticalSection(&(This->lock));
649 return DS_OK;
650 }
651
652 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
653 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
654 ) {
655 HRESULT hres = DS_OK;
656 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
657 TRACE("(%p,%ld)\n",This,newpos);
658
659 /* **** */
660 EnterCriticalSection(&(This->lock));
661
662 newpos %= This->buflen;
663 This->buf_mixpos = newpos;
664 if (This->hwbuf) {
665 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
666 if (hres != DS_OK)
667 WARN("IDsDriverBuffer_SetPosition failed\n");
668 }
669
670 LeaveCriticalSection(&(This->lock));
671 /* **** */
672
673 return hres;
674 }
675
676 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
677 LPDIRECTSOUNDBUFFER8 iface,LONG pan
678 ) {
679 HRESULT hres = DS_OK;
680 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
681
682 TRACE("(%p,%ld)\n",This,pan);
683
684 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
685 WARN("invalid parameter: pan = %ld\n", pan);
686 return DSERR_INVALIDPARAM;
687 }
688
689 /* You cannot use both pan and 3D controls */
690 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
691 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
692 WARN("control unavailable\n");
693 return DSERR_CONTROLUNAVAIL;
694 }
695
696 /* **** */
697 EnterCriticalSection(&(This->lock));
698
699 if (This->volpan.lPan != pan) {
700 This->volpan.lPan = pan;
701 DSOUND_RecalcVolPan(&(This->volpan));
702
703 if (This->hwbuf) {
704 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
705 if (hres != DS_OK)
706 WARN("IDsDriverBuffer_SetVolumePan failed\n");
707 } else
708 DSOUND_ForceRemix(This);
709 }
710
711 LeaveCriticalSection(&(This->lock));
712 /* **** */
713
714 return hres;
715 }
716
717 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
718 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
719 ) {
720 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
721 TRACE("(%p,%p)\n",This,pan);
722
723 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
724 WARN("control unavailable\n");
725 return DSERR_CONTROLUNAVAIL;
726 }
727
728 if (pan == NULL) {
729 WARN("invalid parameter: pan = NULL\n");
730 return DSERR_INVALIDPARAM;
731 }
732
733 *pan = This->volpan.lPan;
734
735 return DS_OK;
736 }
737
738 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
739 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
740 ) {
741 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
742 DWORD probably_valid_to;
743 HRESULT hres = DS_OK;
744
745 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
746
747 /* **** */
748 EnterCriticalSection(&(This->lock));
749
750 if (!(This->dsound->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
751 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
752 if (hres != DS_OK)
753 WARN("IDsDriverBuffer_Unlock failed\n");
754 }
755
756 if (hres == DS_OK) {
757 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer->memory) + x2;
758 else probably_valid_to = (((LPBYTE)p1)-This->buffer->memory) + x1;
759 probably_valid_to %= This->buflen;
760 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
761 ((This->state == STATE_STARTING) ||
762 (This->state == STATE_PLAYING)))
763 /* see IDirectSoundBufferImpl_Lock */
764 probably_valid_to = (DWORD)-1;
765 This->probably_valid_to = probably_valid_to;
766 }
767
768 LeaveCriticalSection(&(This->lock));
769 /* **** */
770
771 TRACE("probably_valid_to=%ld\n", This->probably_valid_to);
772 return hres;
773 }
774
775 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
776 LPDIRECTSOUNDBUFFER8 iface
777 ) {
778 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
779 FIXME("(%p):stub\n",This);
780 return DS_OK;
781 }
782
783 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
784 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
785 ) {
786 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
787 TRACE("(%p,%p)\n",This,freq);
788
789 if (freq == NULL) {
790 WARN("invalid parameter: freq = NULL\n");
791 return DSERR_INVALIDPARAM;
792 }
793
794 *freq = This->freq;
795 TRACE("-> %ld\n", *freq);
796
797 return DS_OK;
798 }
799
800 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
801 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
802 ) {
803 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
804 DWORD u;
805
806 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
807
808 if (pdwResultCodes)
809 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
810
811 WARN("control unavailable\n");
812 return DSERR_CONTROLUNAVAIL;
813 }
814
815 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
816 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
817 ) {
818 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
819 DWORD u;
820
821 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
822
823 if (pdwResultCodes)
824 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
825
826 WARN("control unavailable\n");
827 return DSERR_CONTROLUNAVAIL;
828 }
829
830 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
831 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
832 ) {
833 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
834
835 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
836
837 WARN("control unavailable\n");
838 return DSERR_CONTROLUNAVAIL;
839 }
840
841 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
842 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
843 ) {
844 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
845 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
846 DPRINTF("Re-Init!!!\n");
847 WARN("already initialized\n");
848 return DSERR_ALREADYINITIALIZED;
849 }
850
851 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
852 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
853 ) {
854 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
855 TRACE("(%p)->(%p)\n",This,caps);
856
857 if (caps == NULL) {
858 WARN("invalid parameter: caps == NULL\n");
859 return DSERR_INVALIDPARAM;
860 }
861
862 if (caps->dwSize < sizeof(*caps)) {
863 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps->dwSize, sizeof(*caps));
864 return DSERR_INVALIDPARAM;
865 }
866
867 caps->dwFlags = This->dsbd.dwFlags;
868 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
869 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
870
871 caps->dwBufferBytes = This->buflen;
872
873 /* This value represents the speed of the "unlock" command.
874 As unlock is quite fast (it does not do anything), I put
875 4096 ko/s = 4 Mo / s */
876 /* FIXME: hwbuf speed */
877 caps->dwUnlockTransferRate = 4096;
878 caps->dwPlayCpuOverhead = 0;
879
880 return DS_OK;
881 }
882
883 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
884 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
885 ) {
886 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
887
888 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
889
890 if (ppobj == NULL) {
891 WARN("invalid parameter\n");
892 return E_INVALIDARG;
893 }
894
895 *ppobj = NULL; /* assume failure */
896
897 if ( IsEqualGUID(riid, &IID_IUnknown) ||
898 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
899 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
900 if (!This->dsb)
901 SecondaryBufferImpl_Create(This, &(This->dsb));
902 if (This->dsb) {
903 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
904 *ppobj = This->dsb;
905 return S_OK;
906 }
907 WARN("IID_IDirectSoundBuffer\n");
908 return E_NOINTERFACE;
909 }
910
911 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
912 if (!This->notify)
913 IDirectSoundNotifyImpl_Create(This, &(This->notify));
914 if (This->notify) {
915 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
916 *ppobj = This->notify;
917 return S_OK;
918 }
919 WARN("IID_IDirectSoundNotify\n");
920 return E_NOINTERFACE;
921 }
922
923 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
924 if (!This->ds3db)
925 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
926 if (This->ds3db) {
927 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
928 *ppobj = This->ds3db;
929 return S_OK;
930 }
931 WARN("IID_IDirectSound3DBuffer\n");
932 return E_NOINTERFACE;
933 }
934
935 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
936 ERR("app requested IDirectSound3DListener on secondary buffer\n");
937 return E_NOINTERFACE;
938 }
939
940 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
941 if (!This->iks)
942 IKsBufferPropertySetImpl_Create(This, &(This->iks));
943 if (This->iks) {
944 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
945 *ppobj = This->iks;
946 return S_OK;
947 }
948 WARN("IID_IKsPropertySet\n");
949 return E_NOINTERFACE;
950 }
951
952 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
953
954 return E_NOINTERFACE;
955 }
956
957 static const IDirectSoundBuffer8Vtbl dsbvt =
958 {
959 IDirectSoundBufferImpl_QueryInterface,
960 IDirectSoundBufferImpl_AddRef,
961 IDirectSoundBufferImpl_Release,
962 IDirectSoundBufferImpl_GetCaps,
963 IDirectSoundBufferImpl_GetCurrentPosition,
964 IDirectSoundBufferImpl_GetFormat,
965 IDirectSoundBufferImpl_GetVolume,
966 IDirectSoundBufferImpl_GetPan,
967 IDirectSoundBufferImpl_GetFrequency,
968 IDirectSoundBufferImpl_GetStatus,
969 IDirectSoundBufferImpl_Initialize,
970 IDirectSoundBufferImpl_Lock,
971 IDirectSoundBufferImpl_Play,
972 IDirectSoundBufferImpl_SetCurrentPosition,
973 IDirectSoundBufferImpl_SetFormat,
974 IDirectSoundBufferImpl_SetVolume,
975 IDirectSoundBufferImpl_SetPan,
976 IDirectSoundBufferImpl_SetFrequency,
977 IDirectSoundBufferImpl_Stop,
978 IDirectSoundBufferImpl_Unlock,
979 IDirectSoundBufferImpl_Restore,
980 IDirectSoundBufferImpl_SetFX,
981 IDirectSoundBufferImpl_AcquireResources,
982 IDirectSoundBufferImpl_GetObjectInPath
983 };
984
985 HRESULT WINAPI IDirectSoundBufferImpl_Create(
986 IDirectSoundImpl *ds,
987 IDirectSoundBufferImpl **pdsb,
988 LPCDSBUFFERDESC dsbd)
989 {
990 IDirectSoundBufferImpl *dsb;
991 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
992 HRESULT err = DS_OK;
993 DWORD capf = 0;
994 int use_hw, alloc_size, cp_size;
995 TRACE("(%p,%p,%p)\n",ds,pdsb,dsbd);
996
997 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
998 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
999 *pdsb = NULL;
1000 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1001 }
1002
1003 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1004
1005 if (dsb == 0) {
1006 WARN("out of memory\n");
1007 *pdsb = NULL;
1008 return DSERR_OUTOFMEMORY;
1009 }
1010
1011 TRACE("Created buffer at %p\n", dsb);
1012
1013 dsb->ref = 0;
1014 dsb->dsb = 0;
1015 dsb->dsound = ds;
1016 dsb->lpVtbl = &dsbvt;
1017 dsb->iks = NULL;
1018
1019 /* size depends on version */
1020 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1021
1022 /* variable sized struct so calculate size based on format */
1023 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1024 alloc_size = sizeof(WAVEFORMATEX);
1025 cp_size = sizeof(PCMWAVEFORMAT);
1026 } else
1027 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1028
1029 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1030 if (dsb->pwfx == NULL) {
1031 WARN("out of memory\n");
1032 HeapFree(GetProcessHeap(),0,dsb);
1033 *pdsb = NULL;
1034 return DSERR_OUTOFMEMORY;
1035 }
1036
1037 CopyMemory(dsb->pwfx, wfex, cp_size);
1038
1039 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1040 dsb->buflen = dsbd->dwBufferBytes +
1041 (dsbd->lpwfxFormat->nBlockAlign -
1042 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1043 else
1044 dsb->buflen = dsbd->dwBufferBytes;
1045
1046 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1047 dsb->notify = NULL;
1048 dsb->notifies = NULL;
1049 dsb->nrofnotifies = 0;
1050 dsb->hwnotify = 0;
1051
1052 /* Check necessary hardware mixing capabilities */
1053 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1054 else capf |= DSCAPS_SECONDARYMONO;
1055 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1056 else capf |= DSCAPS_SECONDARY8BIT;
1057
1058 use_hw = (ds->device->drvcaps.dwFlags & capf) == capf;
1059 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw, capf, ds->device->drvcaps.dwFlags);
1060
1061 /* FIXME: check hardware sample rate mixing capabilities */
1062 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1063 /* FIXME: check whether any hardware buffers are left */
1064 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1065
1066 /* Allocate an empty buffer */
1067 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1068 if (dsb->buffer == NULL) {
1069 WARN("out of memory\n");
1070 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1071 HeapFree(GetProcessHeap(),0,dsb);
1072 *pdsb = NULL;
1073 return DSERR_OUTOFMEMORY;
1074 }
1075
1076 /* Allocate system memory for buffer if applicable */
1077 if ((ds->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1078 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1079 if (dsb->buffer->memory == NULL) {
1080 WARN("out of memory\n");
1081 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1082 HeapFree(GetProcessHeap(),0,dsb->buffer);
1083 HeapFree(GetProcessHeap(),0,dsb);
1084 *pdsb = NULL;
1085 return DSERR_OUTOFMEMORY;
1086 }
1087 dsb->buffer->ref = 1;
1088 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1089 }
1090
1091 /* Allocate the hardware buffer */
1092 if (use_hw) {
1093 err = IDsDriver_CreateSoundBuffer(ds->device->driver,wfex,dsbd->dwFlags,0,
1094 &(dsb->buflen),&(dsb->buffer->memory),
1095 (LPVOID*)&(dsb->hwbuf));
1096 /* fall back to software buffer on failure */
1097 if (err != DS_OK) {
1098 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1099 use_hw = 0;
1100 if (ds->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1101 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1102 if (dsb->buffer->memory == NULL) {
1103 WARN("out of memory\n");
1104 HeapFree(GetProcessHeap(),0,dsb->buffer);
1105 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1106 HeapFree(GetProcessHeap(),0,dsb);
1107 *pdsb = NULL;
1108 return DSERR_OUTOFMEMORY;
1109 }
1110 dsb->buffer->ref = 1;
1111 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1112 }
1113 err = DS_OK;
1114 }
1115 }
1116
1117 /* calculate fragment size and write lead */
1118 DSOUND_RecalcFormat(dsb);
1119
1120 /* It's not necessary to initialize values to zero since */
1121 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1122 dsb->playpos = 0;
1123 dsb->buf_mixpos = 0;
1124 dsb->state = STATE_STOPPED;
1125
1126 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1127 ds->device->pwfx->nSamplesPerSec;
1128 dsb->nAvgBytesPerSec = dsb->freq *
1129 dsbd->lpwfxFormat->nBlockAlign;
1130
1131 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1132 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1133 dsb->ds3db_ds3db.vPosition.x = 0.0;
1134 dsb->ds3db_ds3db.vPosition.y = 0.0;
1135 dsb->ds3db_ds3db.vPosition.z = 0.0;
1136 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1137 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1138 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1139 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1140 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1141 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1142 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1143 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1144 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1145 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1146 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1147 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1148
1149 dsb->ds3db_need_recalc = FALSE;
1150 DSOUND_Calc3DBuffer(dsb);
1151 } else
1152 DSOUND_RecalcVolPan(&(dsb->volpan));
1153
1154 InitializeCriticalSection(&(dsb->lock));
1155 dsb->lock.DebugInfo->Spare[0] = (DWORD_PTR)"DSOUNDBUFFER_lock";
1156
1157 /* register buffer if not primary */
1158 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1159 err = DSOUND_AddBuffer(ds, dsb);
1160 if (err != DS_OK) {
1161 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1162 HeapFree(GetProcessHeap(),0,dsb->buffer);
1163 dsb->lock.DebugInfo->Spare[0] = 0;
1164 DeleteCriticalSection(&(dsb->lock));
1165 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1166 HeapFree(GetProcessHeap(),0,dsb);
1167 dsb = NULL;
1168 }
1169 }
1170
1171 *pdsb = dsb;
1172 return err;
1173 }
1174
1175 HRESULT WINAPI IDirectSoundBufferImpl_Destroy(
1176 IDirectSoundBufferImpl *pdsb)
1177 {
1178 TRACE("(%p)\n",pdsb);
1179
1180 /* This keeps the *_Destroy functions from possibly deleting
1181 * this object until it is ready to be deleted */
1182 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1183
1184 if (pdsb->iks) {
1185 WARN("iks not NULL\n");
1186 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1187 pdsb->iks = NULL;
1188 }
1189
1190 if (pdsb->ds3db) {
1191 WARN("ds3db not NULL\n");
1192 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1193 pdsb->ds3db = NULL;
1194 }
1195
1196 if (pdsb->notify) {
1197 WARN("notify not NULL\n");
1198 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1199 pdsb->notify = NULL;
1200 }
1201
1202 if (pdsb->dsb) {
1203 WARN("dsb not NULL\n");
1204 SecondaryBufferImpl_Destroy(pdsb->dsb);
1205 pdsb->dsb = NULL;
1206 }
1207
1208 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1209
1210 return S_OK;
1211 }
1212
1213 /*******************************************************************************
1214 * SecondaryBuffer
1215 */
1216
1217 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1218 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1219 {
1220 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1221 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1222
1223 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1224 }
1225
1226 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1227 {
1228 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
1229 ULONG ref = InterlockedIncrement(&(This->ref));
1230 TRACE("(%p) ref was %ld\n", This, ref - 1);
1231 return ref;
1232 }
1233
1234 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1235 {
1236 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
1237 ULONG ref = InterlockedDecrement(&(This->ref));
1238 TRACE("(%p) ref was %ld\n", This, ref + 1);
1239
1240 if (!ref) {
1241 This->dsb->dsb = NULL;
1242 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1243 HeapFree(GetProcessHeap(), 0, This);
1244 TRACE("(%p) released\n", This);
1245 }
1246 return ref;
1247 }
1248
1249 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1250 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1251 {
1252 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1253 TRACE("(%p)->(%p)\n",This,caps);
1254
1255 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1256 }
1257
1258 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1259 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1260 {
1261 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1262 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1263
1264 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1265 }
1266
1267 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1268 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1269 {
1270 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1271 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1272
1273 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1274 }
1275
1276 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1277 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1278 {
1279 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1280 TRACE("(%p,%p)\n",This,vol);
1281
1282 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1283 }
1284
1285 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1286 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1287 {
1288 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1289 TRACE("(%p,%p)\n",This,pan);
1290
1291 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1292 }
1293
1294 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1295 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1296 {
1297 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1298 TRACE("(%p,%p)\n",This,freq);
1299
1300 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1301 }
1302
1303 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1304 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1305 {
1306 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1307 TRACE("(%p,%p)\n",This,status);
1308
1309 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1310 }
1311
1312 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1313 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1314 {
1315 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1316 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1317
1318 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1319 }
1320
1321 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1322 LPDIRECTSOUNDBUFFER8 iface,
1323 DWORD writecursor,
1324 DWORD writebytes,
1325 LPVOID lplpaudioptr1,
1326 LPDWORD audiobytes1,
1327 LPVOID lplpaudioptr2,
1328 LPDWORD audiobytes2,
1329 DWORD dwFlags)
1330 {
1331 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1332 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1333 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1334
1335 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1336 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1337 }
1338
1339 static HRESULT WINAPI SecondaryBufferImpl_Play(
1340 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1341 {
1342 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1343 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
1344
1345 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1346 }
1347
1348 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1349 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1350 {
1351 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1352 TRACE("(%p,%ld)\n",This,newpos);
1353
1354 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1355 }
1356
1357 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1358 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1359 {
1360 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1361 TRACE("(%p,%p)\n",This,wfex);
1362
1363 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1364 }
1365
1366 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1367 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1368 {
1369 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1370 TRACE("(%p,%ld)\n",This,vol);
1371
1372 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1373 }
1374
1375 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1376 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1377 {
1378 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1379 TRACE("(%p,%ld)\n",This,pan);
1380
1381 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1382 }
1383
1384 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1385 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1386 {
1387 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1388 TRACE("(%p,%ld)\n",This,freq);
1389
1390 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1391 }
1392
1393 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1394 {
1395 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1396 TRACE("(%p)\n",This);
1397
1398 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1399 }
1400
1401 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1402 LPDIRECTSOUNDBUFFER8 iface,
1403 LPVOID lpvAudioPtr1,
1404 DWORD dwAudioBytes1,
1405 LPVOID lpvAudioPtr2,
1406 DWORD dwAudioBytes2)
1407 {
1408 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1409 TRACE("(%p,%p,%ld,%p,%ld)\n",
1410 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1411
1412 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1413 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1414 }
1415
1416 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1417 LPDIRECTSOUNDBUFFER8 iface)
1418 {
1419 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1420 TRACE("(%p)\n",This);
1421
1422 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1423 }
1424
1425 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1426 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1427 {
1428 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1429 TRACE("(%p,%lu,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1430
1431 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1432 }
1433
1434 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1435 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1436 {
1437 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1438 TRACE("(%p,%08lu,%lu,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1439
1440 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1441 }
1442
1443 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1444 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1445 {
1446 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1447 TRACE("(%p,%s,%lu,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1448
1449 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1450 }
1451
1452 static const IDirectSoundBuffer8Vtbl sbvt =
1453 {
1454 SecondaryBufferImpl_QueryInterface,
1455 SecondaryBufferImpl_AddRef,
1456 SecondaryBufferImpl_Release,
1457 SecondaryBufferImpl_GetCaps,
1458 SecondaryBufferImpl_GetCurrentPosition,
1459 SecondaryBufferImpl_GetFormat,
1460 SecondaryBufferImpl_GetVolume,
1461 SecondaryBufferImpl_GetPan,
1462 SecondaryBufferImpl_GetFrequency,
1463 SecondaryBufferImpl_GetStatus,
1464 SecondaryBufferImpl_Initialize,
1465 SecondaryBufferImpl_Lock,
1466 SecondaryBufferImpl_Play,
1467 SecondaryBufferImpl_SetCurrentPosition,
1468 SecondaryBufferImpl_SetFormat,
1469 SecondaryBufferImpl_SetVolume,
1470 SecondaryBufferImpl_SetPan,
1471 SecondaryBufferImpl_SetFrequency,
1472 SecondaryBufferImpl_Stop,
1473 SecondaryBufferImpl_Unlock,
1474 SecondaryBufferImpl_Restore,
1475 SecondaryBufferImpl_SetFX,
1476 SecondaryBufferImpl_AcquireResources,
1477 SecondaryBufferImpl_GetObjectInPath
1478 };
1479
1480 HRESULT WINAPI SecondaryBufferImpl_Create(
1481 IDirectSoundBufferImpl *dsb,
1482 SecondaryBufferImpl **psb)
1483 {
1484 SecondaryBufferImpl *sb;
1485 TRACE("(%p,%p)\n",dsb,psb);
1486
1487 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1488
1489 if (sb == 0) {
1490 WARN("out of memory\n");
1491 *psb = NULL;
1492 return DSERR_OUTOFMEMORY;
1493 }
1494 sb->ref = 0;
1495 sb->dsb = dsb;
1496 sb->lpVtbl = &sbvt;
1497
1498 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1499 *psb = sb;
1500 return S_OK;
1501 }
1502
1503 HRESULT WINAPI SecondaryBufferImpl_Destroy(
1504 SecondaryBufferImpl *pdsb)
1505 {
1506 TRACE("(%p)\n",pdsb);
1507
1508 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1509
1510 return S_OK;
1511 }