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