dsound: Don't make the capture buffer object address public until it is prepared.
[wine] / dlls / dsound / primary.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  * TODO:
22  *      When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23  *       it leaves dsound in unusable (not really open) state.
24  */
25
26 #include <stdarg.h>
27
28 #define COBJMACROS
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "mmsystem.h"
35 #include "winternl.h"
36 #include "mmddk.h"
37 #include "wine/debug.h"
38 #include "dsound.h"
39 #include "dsound_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
42
43 /** Calculate how long a fragment length of about 10 ms should be in frames
44  *
45  * nSamplesPerSec: Frequency rate in samples per second
46  * nBlockAlign: Size of a single blockalign
47  *
48  * Returns:
49  * Size in bytes of a single fragment
50  */
51 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
52 {
53     /* Given a timer delay of 10ms, the fragment size is approximately:
54      *     fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
55      * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
56      *
57      * ALSA uses buffers that are powers of 2. Because of this, fraglen
58      * is rounded up to the nearest power of 2:
59      */
60
61     if (nSamplesPerSec <= 12800)
62         return 128 * nBlockAlign;
63
64     if (nSamplesPerSec <= 25600)
65         return 256 * nBlockAlign;
66
67     if (nSamplesPerSec <= 51200)
68         return 512 * nBlockAlign;
69
70     return 1024 * nBlockAlign;
71 }
72
73 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
74 {
75     TRACE("(%p)\n", device);
76
77     device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
78     device->helfrags = device->buflen / device->fraglen;
79     TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
80
81     /* calculate the 10ms write lead */
82     device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
83 }
84
85 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
86 {
87     UINT prebuf_frames;
88     REFERENCE_TIME prebuf_rt;
89     HRESULT hres;
90
91     TRACE("(%p, %d)\n", device, forcewave);
92
93     if(device->client){
94         IAudioClient_Release(device->client);
95         device->client = NULL;
96     }
97     if(device->render){
98         IAudioRenderClient_Release(device->render);
99         device->render = NULL;
100     }
101     if(device->clock){
102         IAudioClock_Release(device->clock);
103         device->clock = NULL;
104     }
105     if(device->volume){
106         IAudioStreamVolume_Release(device->volume);
107         device->volume = NULL;
108     }
109
110     hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
111             CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
112     if(FAILED(hres)){
113         WARN("Activate failed: %08x\n", hres);
114         return hres;
115     }
116
117     prebuf_frames = device->prebuf * DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign) / device->pwfx->nBlockAlign;
118     prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
119
120     hres = IAudioClient_Initialize(device->client,
121             AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
122             prebuf_rt, 50000, device->pwfx, NULL);
123     if(FAILED(hres)){
124         IAudioClient_Release(device->client);
125         device->client = NULL;
126         WARN("Initialize failed: %08x\n", hres);
127         return hres;
128     }
129
130     hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
131             (void**)&device->render);
132     if(FAILED(hres)){
133         IAudioClient_Release(device->client);
134         device->client = NULL;
135         WARN("GetService failed: %08x\n", hres);
136         return hres;
137     }
138
139     hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
140             (void**)&device->clock);
141     if(FAILED(hres)){
142         IAudioClient_Release(device->client);
143         IAudioRenderClient_Release(device->render);
144         device->client = NULL;
145         device->render = NULL;
146         WARN("GetService failed: %08x\n", hres);
147         return hres;
148     }
149
150     hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
151             (void**)&device->volume);
152     if(FAILED(hres)){
153         IAudioClient_Release(device->client);
154         IAudioRenderClient_Release(device->render);
155         IAudioClock_Release(device->clock);
156         device->client = NULL;
157         device->render = NULL;
158         device->clock = NULL;
159         WARN("GetService failed: %08x\n", hres);
160         return hres;
161     }
162
163     return S_OK;
164 }
165
166 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
167 {
168         DWORD buflen;
169         LPBYTE newbuf;
170
171         TRACE("(%p)\n", device);
172
173         /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
174            on windows this size is always fixed (tested on win-xp) */
175         if (!device->buflen)
176                 device->buflen = ds_hel_buflen;
177         buflen = device->buflen;
178         buflen -= buflen % device->pwfx->nBlockAlign;
179         device->buflen = buflen;
180
181         device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
182         device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
183         if (!device->mix_buffer)
184                 return DSERR_OUTOFMEMORY;
185
186         if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
187         else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
188
189     TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
190
191     /* reallocate emulated primary buffer */
192     if (device->buffer)
193         newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
194     else
195         newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
196
197     if (!newbuf) {
198         ERR("failed to allocate primary buffer\n");
199         return DSERR_OUTOFMEMORY;
200         /* but the old buffer might still exist and must be re-prepared */
201     }
202
203     DSOUND_RecalcPrimary(device);
204
205     device->buffer = newbuf;
206
207     TRACE("fraglen=%d\n", device->fraglen);
208
209         device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
210         device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
211         FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
212         FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
213         device->last_pos_bytes = device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
214         return DS_OK;
215 }
216
217
218 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
219 {
220     HRESULT hr;
221
222     TRACE("(%p)\n", device);
223
224     device->pwqueue = (DWORD)-1; /* resetting queues */
225
226     if(device->client){
227         hr = IAudioClient_Stop(device->client);
228         if(FAILED(hr))
229             WARN("Stop failed: %08x\n", hr);
230     }
231
232     /* clear the queue */
233     device->pwqueue = 0;
234 }
235
236 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
237 {
238         HRESULT err = DS_OK;
239         TRACE("(%p)\n", device);
240
241         device->buflen = ds_hel_buflen;
242         err = DSOUND_PrimaryOpen(device);
243
244         if (err != DS_OK) {
245                 WARN("DSOUND_PrimaryOpen failed\n");
246                 return err;
247         }
248
249         device->state = STATE_STOPPED;
250         return DS_OK;
251 }
252
253 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
254 {
255         TRACE("(%p)\n", device);
256
257         /* **** */
258         EnterCriticalSection(&(device->mixlock));
259
260         DSOUND_PrimaryClose(device);
261         HeapFree(GetProcessHeap(),0,device->pwfx);
262         device->pwfx=NULL;
263
264         LeaveCriticalSection(&(device->mixlock));
265         /* **** */
266
267         return DS_OK;
268 }
269
270 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
271 {
272     HRESULT hr;
273
274     TRACE("(%p)\n", device);
275
276     hr = IAudioClient_Start(device->client);
277     if(FAILED(hr)){
278         WARN("Start failed: %08x\n", hr);
279         return hr;
280     }
281
282     return DS_OK;
283 }
284
285 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
286 {
287     HRESULT hr;
288
289     TRACE("(%p)\n", device);
290
291     hr = IAudioClient_Stop(device->client);
292     if(FAILED(hr)){
293         WARN("Stop failed: %08x\n", hr);
294         return hr;
295     }
296
297     return DS_OK;
298 }
299
300 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
301 {
302         TRACE("(%p,%p,%p)\n", device, playpos, writepos);
303
304         TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
305
306         /* check if playpos was requested */
307         if (playpos)
308                 /* use the cached play position */
309                 *playpos = device->pwplay * device->fraglen;
310
311         /* check if writepos was requested */
312         if (writepos)
313                 /* the writepos is the first non-queued position */
314                 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
315
316         TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
317         return DS_OK;
318 }
319
320 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
321 {
322         if (wfex->wFormatTag == WAVE_FORMAT_PCM)
323                 return sizeof(WAVEFORMATEX);
324         else
325                 return sizeof(WAVEFORMATEX) + wfex->cbSize;
326 }
327
328 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
329 {
330         DWORD size = DSOUND_GetFormatSize(wfex);
331         LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
332         if (pwfx == NULL) {
333                 WARN("out of memory\n");
334         } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
335                 CopyMemory(pwfx, wfex, size);
336         } else {
337                 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
338                 pwfx->cbSize=0;
339                 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
340                         WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
341                         pwfx->nBlockAlign  = pwfx->nChannels * pwfx->wBitsPerSample/8;
342                 }
343                 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
344                         WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
345                         pwfx->nAvgBytesPerSec  = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
346                 }
347         }
348         return pwfx;
349 }
350
351 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
352 {
353         HRESULT err = DSERR_BUFFERLOST;
354         int i;
355         WAVEFORMATEX *old_fmt;
356         WAVEFORMATEXTENSIBLE *fmtex;
357         BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
358
359         TRACE("(%p,%p)\n", device, passed_fmt);
360
361         if (device->priolevel == DSSCL_NORMAL) {
362                 WARN("failed priority check!\n");
363                 return DSERR_PRIOLEVELNEEDED;
364         }
365
366         /* Let's be pedantic! */
367         if (passed_fmt == NULL) {
368                 WARN("invalid parameter: passed_fmt==NULL!\n");
369                 return DSERR_INVALIDPARAM;
370         }
371         TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
372                           "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
373                   passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
374                   passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
375                   passed_fmt->wBitsPerSample, passed_fmt->cbSize);
376
377         /* **** */
378         RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
379         EnterCriticalSection(&(device->mixlock));
380
381         old_fmt = device->pwfx;
382         device->pwfx = DSOUND_CopyFormat(passed_fmt);
383         fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
384         if (device->pwfx == NULL) {
385                 device->pwfx = old_fmt;
386                 old_fmt = NULL;
387                 err = DSERR_OUTOFMEMORY;
388                 goto done;
389         }
390
391         DSOUND_PrimaryClose(device);
392
393         err = DSOUND_ReopenDevice(device, FALSE);
394         if(SUCCEEDED(err))
395                 goto opened;
396
397         /* requested format failed, so try others */
398         if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
399                 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
400                 device->pwfx->wBitsPerSample = 32;
401                 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
402                 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
403
404                 err = DSOUND_ReopenDevice(device, FALSE);
405                 if(SUCCEEDED(err))
406                         goto opened;
407         }
408
409         if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
410                          IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
411                 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
412                 device->pwfx->wBitsPerSample = 32;
413                 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
414                 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
415
416                 err = DSOUND_ReopenDevice(device, FALSE);
417                 if(SUCCEEDED(err))
418                         goto opened;
419         }
420
421         device->pwfx->wBitsPerSample = 32;
422         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
423         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
424         err = DSOUND_ReopenDevice(device, FALSE);
425         if(SUCCEEDED(err))
426                 goto opened;
427
428         device->pwfx->wBitsPerSample = 16;
429         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
430         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
431         err = DSOUND_ReopenDevice(device, FALSE);
432         if(SUCCEEDED(err))
433                 goto opened;
434
435         device->pwfx->wBitsPerSample = 8;
436         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
437         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
438         err = DSOUND_ReopenDevice(device, FALSE);
439         if(SUCCEEDED(err))
440                 goto opened;
441
442         device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
443         device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
444         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
445         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
446         err = DSOUND_ReopenDevice(device, FALSE);
447         if(SUCCEEDED(err))
448                 goto opened;
449
450         device->pwfx->wBitsPerSample = 32;
451         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
452         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
453         err = DSOUND_ReopenDevice(device, FALSE);
454         if(SUCCEEDED(err))
455                 goto opened;
456
457         device->pwfx->wBitsPerSample = 16;
458         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
459         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
460         err = DSOUND_ReopenDevice(device, FALSE);
461         if(SUCCEEDED(err))
462                 goto opened;
463
464         device->pwfx->wBitsPerSample = 8;
465         device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
466         device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
467         err = DSOUND_ReopenDevice(device, FALSE);
468         if(SUCCEEDED(err))
469                 goto opened;
470
471         WARN("No formats could be opened\n");
472         goto done;
473
474 opened:
475         err = DSOUND_PrimaryOpen(device);
476         if (err != DS_OK) {
477                 WARN("DSOUND_PrimaryOpen failed\n");
478                 goto done;
479         }
480
481         if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
482         {
483                 DSOUND_PrimaryClose(device);
484                 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
485                 err = DSOUND_ReopenDevice(device, TRUE);
486                 if (FAILED(err))
487                         WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
488                 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
489                         WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
490         }
491
492         device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
493         device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
494         FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
495         device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
496         device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
497
498         if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
499                         old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
500                         old_fmt->nChannels != device->pwfx->nChannels) {
501                 IDirectSoundBufferImpl** dsb = device->buffers;
502                 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
503                         /* **** */
504                         RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
505
506                         (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
507                         DSOUND_RecalcFormat((*dsb));
508                         DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
509                         (*dsb)->primary_mixpos = 0;
510
511                         RtlReleaseResource(&(*dsb)->lock);
512                         /* **** */
513                 }
514         }
515
516 done:
517         LeaveCriticalSection(&(device->mixlock));
518         RtlReleaseResource(&(device->buffer_list_lock));
519         /* **** */
520
521         HeapFree(GetProcessHeap(), 0, old_fmt);
522         return err;
523 }
524
525 /*******************************************************************************
526  *              PrimaryBuffer
527  */
528 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
529 {
530     /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
531     return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
532 }
533
534 /* This sets this format for the <em>Primary Buffer Only</em> */
535 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
536 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
537     LPDIRECTSOUNDBUFFER iface,
538     LPCWAVEFORMATEX wfex)
539 {
540     IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
541     TRACE("(%p,%p)\n", iface, wfex);
542     return primarybuffer_SetFormat(This->device, wfex);
543 }
544
545 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
546         LPDIRECTSOUNDBUFFER iface,LONG vol
547 ) {
548         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
549         DirectSoundDevice *device = This->device;
550         HRESULT hr;
551         float lvol, rvol;
552
553         TRACE("(%p,%d)\n", iface, vol);
554
555         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
556                 WARN("control unavailable\n");
557                 return DSERR_CONTROLUNAVAIL;
558         }
559
560         if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
561                 WARN("invalid parameter: vol = %d\n", vol);
562                 return DSERR_INVALIDPARAM;
563         }
564
565         /* **** */
566         EnterCriticalSection(&device->mixlock);
567
568         hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
569         if(FAILED(hr)){
570                 LeaveCriticalSection(&device->mixlock);
571                 WARN("GetChannelVolume failed: %08x\n", hr);
572                 return hr;
573         }
574
575         if(device->pwfx->nChannels > 1){
576                 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
577                 if(FAILED(hr)){
578                         LeaveCriticalSection(&device->mixlock);
579                         WARN("GetChannelVolume failed: %08x\n", hr);
580                         return hr;
581                 }
582         }else
583                 rvol = 1;
584
585         device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
586         device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
587
588         DSOUND_AmpFactorToVolPan(&device->volpan);
589         if (vol != device->volpan.lVolume) {
590                 device->volpan.lVolume=vol;
591                 DSOUND_RecalcVolPan(&device->volpan);
592                 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
593                 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
594                 if(FAILED(hr)){
595                         LeaveCriticalSection(&device->mixlock);
596                         WARN("SetChannelVolume failed: %08x\n", hr);
597                         return hr;
598                 }
599
600                 if(device->pwfx->nChannels > 1){
601                         rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
602                         hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
603                         if(FAILED(hr)){
604                                 LeaveCriticalSection(&device->mixlock);
605                                 WARN("SetChannelVolume failed: %08x\n", hr);
606                                 return hr;
607                         }
608                 }
609         }
610
611         LeaveCriticalSection(&(device->mixlock));
612         /* **** */
613
614         return DS_OK;
615 }
616
617 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
618         LPDIRECTSOUNDBUFFER iface,LPLONG vol
619 ) {
620         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
621         DirectSoundDevice *device = This->device;
622         float lvol, rvol;
623         HRESULT hr;
624         TRACE("(%p,%p)\n", iface, vol);
625
626         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
627                 WARN("control unavailable\n");
628                 return DSERR_CONTROLUNAVAIL;
629         }
630
631         if (vol == NULL) {
632                 WARN("invalid parameter: vol = NULL\n");
633                 return DSERR_INVALIDPARAM;
634         }
635
636         EnterCriticalSection(&device->mixlock);
637
638         hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
639         if(FAILED(hr)){
640                 LeaveCriticalSection(&device->mixlock);
641                 WARN("GetChannelVolume failed: %08x\n", hr);
642                 return hr;
643         }
644
645         if(device->pwfx->nChannels > 1){
646                 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
647                 if(FAILED(hr)){
648                         LeaveCriticalSection(&device->mixlock);
649                         WARN("GetChannelVolume failed: %08x\n", hr);
650                         return hr;
651                 }
652         }else
653                 rvol = 1;
654
655         device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
656         device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
657
658         DSOUND_AmpFactorToVolPan(&device->volpan);
659         *vol = device->volpan.lVolume;
660
661         LeaveCriticalSection(&device->mixlock);
662
663         return DS_OK;
664 }
665
666 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
667         LPDIRECTSOUNDBUFFER iface,DWORD freq
668 ) {
669         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
670         TRACE("(%p,%d)\n",This,freq);
671
672         /* You cannot set the frequency of the primary buffer */
673         WARN("control unavailable\n");
674         return DSERR_CONTROLUNAVAIL;
675 }
676
677 static HRESULT WINAPI PrimaryBufferImpl_Play(
678         LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
679 ) {
680         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
681         DirectSoundDevice *device = This->device;
682         TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
683
684         if (!(flags & DSBPLAY_LOOPING)) {
685                 WARN("invalid parameter: flags = %08x\n", flags);
686                 return DSERR_INVALIDPARAM;
687         }
688
689         /* **** */
690         EnterCriticalSection(&(device->mixlock));
691
692         if (device->state == STATE_STOPPED)
693                 device->state = STATE_STARTING;
694         else if (device->state == STATE_STOPPING)
695                 device->state = STATE_PLAYING;
696
697         LeaveCriticalSection(&(device->mixlock));
698         /* **** */
699
700         return DS_OK;
701 }
702
703 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
704 {
705         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
706         DirectSoundDevice *device = This->device;
707         TRACE("(%p)\n", iface);
708
709         /* **** */
710         EnterCriticalSection(&(device->mixlock));
711
712         if (device->state == STATE_PLAYING)
713                 device->state = STATE_STOPPING;
714         else if (device->state == STATE_STARTING)
715                 device->state = STATE_STOPPED;
716
717         LeaveCriticalSection(&(device->mixlock));
718         /* **** */
719
720         return DS_OK;
721 }
722
723 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
724 {
725     IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
726     ULONG ref = InterlockedIncrement(&(This->ref));
727     TRACE("(%p) ref was %d\n", This, ref - 1);
728     if(ref == 1)
729         InterlockedIncrement(&This->numIfaces);
730     return ref;
731 }
732
733 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
734 {
735     This->device->primary = NULL;
736     HeapFree(GetProcessHeap(), 0, This);
737     TRACE("(%p) released\n", This);
738 }
739
740 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
741 {
742     IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
743     DWORD ref = InterlockedDecrement(&(This->ref));
744     TRACE("(%p) ref was %d\n", This, ref + 1);
745
746     if (!ref && !InterlockedDecrement(&This->numIfaces))
747         primarybuffer_destroy(This);
748     return ref;
749 }
750
751 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
752         LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
753 ) {
754         HRESULT hres;
755         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
756         DirectSoundDevice *device = This->device;
757         TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
758
759         /* **** */
760         EnterCriticalSection(&(device->mixlock));
761
762         hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
763         if (hres != DS_OK) {
764                 WARN("DSOUND_PrimaryGetPosition failed\n");
765                 LeaveCriticalSection(&(device->mixlock));
766                 return hres;
767         }
768         if (writepos) {
769                 if (device->state != STATE_STOPPED)
770                         /* apply the documented 10ms lead to writepos */
771                         *writepos += device->writelead;
772                 while (*writepos >= device->buflen) *writepos -= device->buflen;
773         }
774
775         LeaveCriticalSection(&(device->mixlock));
776         /* **** */
777
778         TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
779         return DS_OK;
780 }
781
782 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
783         LPDIRECTSOUNDBUFFER iface,LPDWORD status
784 ) {
785         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
786         DirectSoundDevice *device = This->device;
787         TRACE("(%p,%p)\n", iface, status);
788
789         if (status == NULL) {
790                 WARN("invalid parameter: status == NULL\n");
791                 return DSERR_INVALIDPARAM;
792         }
793
794         *status = 0;
795         if ((device->state == STATE_STARTING) ||
796             (device->state == STATE_PLAYING))
797                 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
798
799         TRACE("status=%x\n", *status);
800         return DS_OK;
801 }
802
803
804 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
805     LPDIRECTSOUNDBUFFER iface,
806     LPWAVEFORMATEX lpwf,
807     DWORD wfsize,
808     LPDWORD wfwritten)
809 {
810     DWORD size;
811     IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
812     DirectSoundDevice *device = This->device;
813     TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
814
815     size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
816
817     if (lpwf) { /* NULL is valid */
818         if (wfsize >= size) {
819             CopyMemory(lpwf,device->pwfx,size);
820             if (wfwritten)
821                 *wfwritten = size;
822         } else {
823             WARN("invalid parameter: wfsize too small\n");
824             if (wfwritten)
825                 *wfwritten = 0;
826             return DSERR_INVALIDPARAM;
827         }
828     } else {
829         if (wfwritten)
830             *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
831         else {
832             WARN("invalid parameter: wfwritten == NULL\n");
833             return DSERR_INVALIDPARAM;
834         }
835     }
836
837     return DS_OK;
838 }
839
840 static HRESULT WINAPI PrimaryBufferImpl_Lock(
841         LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
842 ) {
843         HRESULT hres;
844         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
845         DirectSoundDevice *device = This->device;
846         TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
847                 iface,
848                 writecursor,
849                 writebytes,
850                 lplpaudioptr1,
851                 audiobytes1,
852                 lplpaudioptr2,
853                 audiobytes2,
854                 flags,
855                 GetTickCount()
856         );
857
858         if (!audiobytes1)
859             return DSERR_INVALIDPARAM;
860
861         if (device->priolevel != DSSCL_WRITEPRIMARY) {
862                 WARN("failed priority check!\n");
863                 return DSERR_PRIOLEVELNEEDED;
864         }
865
866         /* when this flag is set, writecursor is meaningless and must be calculated */
867         if (flags & DSBLOCK_FROMWRITECURSOR) {
868                 /* GetCurrentPosition does too much magic to duplicate here */
869                 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
870                 if (hres != DS_OK) {
871                         WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
872                         return hres;
873                 }
874         }
875
876         /* when this flag is set, writebytes is meaningless and must be set */
877         if (flags & DSBLOCK_ENTIREBUFFER)
878                 writebytes = device->buflen;
879
880         if (writecursor >= device->buflen) {
881                 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
882                      writecursor, device->buflen);
883                 return DSERR_INVALIDPARAM;
884         }
885
886         if (writebytes > device->buflen) {
887                 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
888                      writebytes, device->buflen);
889                 return DSERR_INVALIDPARAM;
890         }
891
892         if (writecursor+writebytes <= device->buflen) {
893                 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
894                 *audiobytes1 = writebytes;
895                 if (lplpaudioptr2)
896                         *(LPBYTE*)lplpaudioptr2 = NULL;
897                 if (audiobytes2)
898                         *audiobytes2 = 0;
899                 TRACE("->%d.0\n",writebytes);
900         } else {
901                 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
902                 *audiobytes1 = device->buflen-writecursor;
903                 if (lplpaudioptr2)
904                         *(LPBYTE*)lplpaudioptr2 = device->buffer;
905                 if (audiobytes2)
906                         *audiobytes2 = writebytes-(device->buflen-writecursor);
907                 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
908         }
909         return DS_OK;
910 }
911
912 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
913         LPDIRECTSOUNDBUFFER iface,DWORD newpos
914 ) {
915         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
916         TRACE("(%p,%d)\n",This,newpos);
917
918         /* You cannot set the position of the primary buffer */
919         WARN("invalid call\n");
920         return DSERR_INVALIDCALL;
921 }
922
923 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
924         LPDIRECTSOUNDBUFFER iface,LONG pan
925 ) {
926         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
927         DirectSoundDevice *device = This->device;
928         float lvol, rvol;
929         HRESULT hr;
930         TRACE("(%p,%d)\n", iface, pan);
931
932         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
933                 WARN("control unavailable\n");
934                 return DSERR_CONTROLUNAVAIL;
935         }
936
937         if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
938                 WARN("invalid parameter: pan = %d\n", pan);
939                 return DSERR_INVALIDPARAM;
940         }
941
942         /* **** */
943         EnterCriticalSection(&device->mixlock);
944
945         hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
946         if(FAILED(hr)){
947                 LeaveCriticalSection(&device->mixlock);
948                 WARN("GetChannelVolume failed: %08x\n", hr);
949                 return hr;
950         }
951
952         if(device->pwfx->nChannels > 1){
953                 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
954                 if(FAILED(hr)){
955                         LeaveCriticalSection(&device->mixlock);
956                         WARN("GetChannelVolume failed: %08x\n", hr);
957                         return hr;
958                 }
959         }else
960                 rvol = 1;
961
962         device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
963         device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
964
965         DSOUND_AmpFactorToVolPan(&device->volpan);
966         if (pan != device->volpan.lPan) {
967                 device->volpan.lPan=pan;
968                 DSOUND_RecalcVolPan(&device->volpan);
969
970                 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
971                 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
972                 if(FAILED(hr)){
973                         LeaveCriticalSection(&device->mixlock);
974                         WARN("SetChannelVolume failed: %08x\n", hr);
975                         return hr;
976                 }
977
978                 if(device->pwfx->nChannels > 1){
979                         rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
980                         hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
981                         if(FAILED(hr)){
982                                 LeaveCriticalSection(&device->mixlock);
983                                 WARN("SetChannelVolume failed: %08x\n", hr);
984                                 return hr;
985                         }
986                 }
987         }
988
989         LeaveCriticalSection(&device->mixlock);
990         /* **** */
991
992         return DS_OK;
993 }
994
995 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
996         LPDIRECTSOUNDBUFFER iface,LPLONG pan
997 ) {
998         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
999         DirectSoundDevice *device = This->device;
1000         float lvol, rvol;
1001         HRESULT hr;
1002         TRACE("(%p,%p)\n", iface, pan);
1003
1004         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1005                 WARN("control unavailable\n");
1006                 return DSERR_CONTROLUNAVAIL;
1007         }
1008
1009         if (pan == NULL) {
1010                 WARN("invalid parameter: pan == NULL\n");
1011                 return DSERR_INVALIDPARAM;
1012         }
1013
1014         EnterCriticalSection(&device->mixlock);
1015
1016         hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1017         if(FAILED(hr)){
1018                 LeaveCriticalSection(&device->mixlock);
1019                 WARN("GetChannelVolume failed: %08x\n", hr);
1020                 return hr;
1021         }
1022
1023         if(device->pwfx->nChannels > 1){
1024                 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1025                 if(FAILED(hr)){
1026                         LeaveCriticalSection(&device->mixlock);
1027                         WARN("GetChannelVolume failed: %08x\n", hr);
1028                         return hr;
1029                 }
1030         }else
1031                 rvol = 1;
1032
1033         device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1034         device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1035
1036         DSOUND_AmpFactorToVolPan(&device->volpan);
1037         *pan = device->volpan.lPan;
1038
1039         LeaveCriticalSection(&device->mixlock);
1040
1041         return DS_OK;
1042 }
1043
1044 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
1045         LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1046 ) {
1047         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1048         DirectSoundDevice *device = This->device;
1049         TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1050
1051         if (device->priolevel != DSSCL_WRITEPRIMARY) {
1052                 WARN("failed priority check!\n");
1053                 return DSERR_PRIOLEVELNEEDED;
1054         }
1055
1056     if((p1 && ((BYTE*)p1 < device->buffer ||
1057                     (BYTE*)p1 >= device->buffer + device->buflen)) ||
1058             (p2 && ((BYTE*)p2 < device->buffer ||
1059                     (BYTE*)p2 >= device->buffer + device->buflen)))
1060         return DSERR_INVALIDPARAM;
1061
1062         return DS_OK;
1063 }
1064
1065 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1066         LPDIRECTSOUNDBUFFER iface
1067 ) {
1068         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1069         FIXME("(%p):stub\n",This);
1070         return DS_OK;
1071 }
1072
1073 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1074         LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1075 ) {
1076         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1077         DirectSoundDevice *device = This->device;
1078         TRACE("(%p,%p)\n", iface, freq);
1079
1080         if (freq == NULL) {
1081                 WARN("invalid parameter: freq == NULL\n");
1082                 return DSERR_INVALIDPARAM;
1083         }
1084
1085         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1086                 WARN("control unavailable\n");
1087                 return DSERR_CONTROLUNAVAIL;
1088         }
1089
1090         *freq = device->pwfx->nSamplesPerSec;
1091         TRACE("-> %d\n", *freq);
1092
1093         return DS_OK;
1094 }
1095
1096 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1097         LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1098 ) {
1099         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1100         WARN("(%p) already initialized\n", This);
1101         return DSERR_ALREADYINITIALIZED;
1102 }
1103
1104 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1105         LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1106 ) {
1107         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1108         DirectSoundDevice *device = This->device;
1109         TRACE("(%p,%p)\n", iface, caps);
1110
1111         if (caps == NULL) {
1112                 WARN("invalid parameter: caps == NULL\n");
1113                 return DSERR_INVALIDPARAM;
1114         }
1115
1116         if (caps->dwSize < sizeof(*caps)) {
1117                 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1118                 return DSERR_INVALIDPARAM;
1119         }
1120
1121         caps->dwFlags = This->dsbd.dwFlags;
1122         caps->dwBufferBytes = device->buflen;
1123
1124         /* Windows reports these as zero */
1125         caps->dwUnlockTransferRate = 0;
1126         caps->dwPlayCpuOverhead = 0;
1127
1128         return DS_OK;
1129 }
1130
1131 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1132         LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1133 ) {
1134         IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1135         DirectSoundDevice *device = This->device;
1136         TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1137
1138         if (ppobj == NULL) {
1139                 WARN("invalid parameter\n");
1140                 return E_INVALIDARG;
1141         }
1142
1143         *ppobj = NULL;  /* assume failure */
1144
1145         if ( IsEqualGUID(riid, &IID_IUnknown) ||
1146              IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1147                 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1148                 *ppobj = This;
1149                 return S_OK;
1150         }
1151
1152         /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1153         /* a primary buffer can't have a DirectSoundBuffer8 interface */
1154         if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1155                 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1156                 return E_NOINTERFACE;
1157         }
1158
1159         if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1160                 ERR("app requested IDirectSoundNotify on primary buffer\n");
1161                 /* FIXME: should we support this? */
1162                 return E_NOINTERFACE;
1163         }
1164
1165         if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1166                 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1167                 return E_NOINTERFACE;
1168         }
1169
1170         if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1171                 if (!device->listener)
1172                         IDirectSound3DListenerImpl_Create(device, &device->listener);
1173                 if (device->listener) {
1174                         *ppobj = device->listener;
1175                         IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1176                         return S_OK;
1177                 }
1178
1179                 WARN("IID_IDirectSound3DListener failed\n");
1180                 return E_NOINTERFACE;
1181         }
1182
1183         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1184                 FIXME("app requested IKsPropertySet on primary buffer\n");
1185                 return E_NOINTERFACE;
1186         }
1187
1188         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1189         return E_NOINTERFACE;
1190 }
1191
1192 static const IDirectSoundBufferVtbl dspbvt =
1193 {
1194         PrimaryBufferImpl_QueryInterface,
1195         PrimaryBufferImpl_AddRef,
1196         PrimaryBufferImpl_Release,
1197         PrimaryBufferImpl_GetCaps,
1198         PrimaryBufferImpl_GetCurrentPosition,
1199         PrimaryBufferImpl_GetFormat,
1200         PrimaryBufferImpl_GetVolume,
1201         PrimaryBufferImpl_GetPan,
1202         PrimaryBufferImpl_GetFrequency,
1203         PrimaryBufferImpl_GetStatus,
1204         PrimaryBufferImpl_Initialize,
1205         PrimaryBufferImpl_Lock,
1206         PrimaryBufferImpl_Play,
1207         PrimaryBufferImpl_SetCurrentPosition,
1208         PrimaryBufferImpl_SetFormat,
1209         PrimaryBufferImpl_SetVolume,
1210         PrimaryBufferImpl_SetPan,
1211         PrimaryBufferImpl_SetFrequency,
1212         PrimaryBufferImpl_Stop,
1213         PrimaryBufferImpl_Unlock,
1214         PrimaryBufferImpl_Restore
1215 };
1216
1217 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1218         const DSBUFFERDESC *dsbd)
1219 {
1220         IDirectSoundBufferImpl *dsb;
1221         TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1222
1223         if (dsbd->lpwfxFormat) {
1224                 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1225                 *ppdsb = NULL;
1226                 return DSERR_INVALIDPARAM;
1227         }
1228
1229         dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1230
1231         if (dsb == NULL) {
1232                 WARN("out of memory\n");
1233                 *ppdsb = NULL;
1234                 return DSERR_OUTOFMEMORY;
1235         }
1236
1237         dsb->ref = 1;
1238         dsb->numIfaces = 1;
1239         dsb->device = device;
1240         dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1241         dsb->dsbd = *dsbd;
1242
1243         TRACE("Created primary buffer at %p\n", dsb);
1244         TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1245                 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1246                 device->pwfx->wFormatTag, device->pwfx->nChannels,
1247                 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1248                 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1249                 device->pwfx->cbSize);
1250
1251         *ppdsb = dsb;
1252         return S_OK;
1253 }