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