d3dx8: Add WINAPI to the prototypes of D3DXMatrixTransformation.
[wine/multimedia.git] / dlls / dsound / primary.c
blobeec681c3dfc1c828e1db055e1f1695fcc8f529c2
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
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.
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.
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
22 #include <stdarg.h>
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "winternl.h"
31 #include "mmddk.h"
32 #include "wine/debug.h"
33 #include "dsound.h"
34 #include "dsdriver.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39 /** Calculate how long a fragment length of about 10 ms should be in frames
41 * nSamplesPerSec: Frequency rate in samples per second
42 * nBlockAlign: Size of a single blockalign
44 * Returns:
45 * Size in bytes of a single fragment
47 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
49 DWORD fraglen = 512 * nBlockAlign;
51 /* Compensate for only being roughly accurate */
52 if (nSamplesPerSec <= 26000)
53 fraglen /= 2;
55 if (nSamplesPerSec <= 12000)
56 fraglen /= 2;
58 if (nSamplesPerSec >= 80000)
59 fraglen *= 2;
61 return fraglen;
64 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
66 TRACE("(%p)\n", device);
68 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
69 device->helfrags = device->buflen / device->fraglen;
70 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
72 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
73 device->writelead = 0;
74 else
75 /* calculate the 10ms write lead */
76 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
79 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
81 HRESULT hres = DS_OK;
82 TRACE("(%p, %d)\n", device, forcewave);
84 if (device->driver)
86 IDsDriver_Close(device->driver);
87 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
88 waveOutClose(device->hwo);
89 IDsDriver_Release(device->driver);
90 device->driver = NULL;
91 device->buffer = NULL;
92 device->hwo = 0;
94 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
95 waveOutClose(device->hwo);
97 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
98 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
99 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
101 /* Get driver description */
102 if (device->driver) {
103 DWORD wod = device->drvdesc.dnDevNode;
104 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
105 device->drvdesc.dnDevNode = wod;
106 if (FAILED(hres)) {
107 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
108 IDsDriver_Release(device->driver);
109 device->driver = NULL;
113 /* if no DirectSound interface available, use WINMM API instead */
114 if (!device->driver)
115 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
117 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
119 DWORD flags = CALLBACK_FUNCTION;
121 if (device->driver)
122 flags |= WAVE_DIRECTSOUND;
124 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
125 if (FAILED(hres)) {
126 WARN("waveOutOpen failed\n");
127 if (device->driver)
129 IDsDriver_Release(device->driver);
130 device->driver = NULL;
132 return hres;
136 if (device->driver)
137 hres = IDsDriver_Open(device->driver);
139 return hres;
142 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
144 DWORD buflen;
145 HRESULT err = DS_OK;
146 TRACE("(%p)\n", device);
148 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
149 on windows this size is always fixed (tested on win-xp) */
150 if (!device->buflen)
151 device->buflen = ds_hel_buflen;
152 buflen = device->buflen;
153 buflen -= buflen % device->pwfx->nBlockAlign;
154 device->buflen = buflen;
156 if (device->driver)
158 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
159 DSBCAPS_PRIMARYBUFFER,0,
160 &(device->buflen),&(device->buffer),
161 (LPVOID*)&(device->hwbuf));
163 if (err != DS_OK) {
164 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
165 err = DSOUND_ReopenDevice(device, TRUE);
166 if (FAILED(err))
168 WARN("Falling back to waveout failed too! Giving up\n");
169 return err;
172 DSOUND_RecalcPrimary(device);
173 device->prebuf = ds_snd_queue_max;
174 if (device->helfrags < ds_snd_queue_min)
176 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
177 device->buflen = buflen;
178 IDsDriverBuffer_Release(device->hwbuf);
179 device->hwbuf = NULL;
180 err = DSOUND_ReopenDevice(device, TRUE);
181 if (FAILED(err))
183 WARN("Falling back to waveout failed too! Giving up\n");
184 return err;
187 else if (device->helfrags < ds_snd_queue_max)
188 device->prebuf = device->helfrags;
191 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
192 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
193 if (!device->mix_buffer)
195 if (device->hwbuf)
196 IDsDriverBuffer_Release(device->hwbuf);
197 device->hwbuf = NULL;
198 return DSERR_OUTOFMEMORY;
201 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
202 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
204 /* are we using waveOut stuff? */
205 if (!device->driver) {
206 LPBYTE newbuf;
207 LPWAVEHDR headers = NULL;
208 DWORD overshot;
209 unsigned int c;
211 /* Start in pause mode, to allow buffers to get filled */
212 waveOutPause(device->hwo);
214 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
216 /* reallocate emulated primary buffer */
217 if (device->buffer)
218 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
219 else
220 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
222 if (!newbuf) {
223 ERR("failed to allocate primary buffer\n");
224 return DSERR_OUTOFMEMORY;
225 /* but the old buffer might still exist and must be re-prepared */
228 DSOUND_RecalcPrimary(device);
229 if (device->pwave)
230 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
231 else
232 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
234 if (!headers) {
235 ERR("failed to allocate wave headers\n");
236 HeapFree(GetProcessHeap(), 0, newbuf);
237 DSOUND_RecalcPrimary(device);
238 return DSERR_OUTOFMEMORY;
241 device->buffer = newbuf;
242 device->pwave = headers;
244 /* prepare fragment headers */
245 for (c=0; c<device->helfrags; c++) {
246 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
247 device->pwave[c].dwBufferLength = device->fraglen;
248 device->pwave[c].dwUser = (DWORD)device;
249 device->pwave[c].dwFlags = 0;
250 device->pwave[c].dwLoops = 0;
251 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
252 if (err != DS_OK) {
253 while (c--)
254 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
255 break;
259 overshot = device->buflen % device->fraglen;
260 /* sanity */
261 if(overshot)
263 overshot -= overshot % device->pwfx->nBlockAlign;
264 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
267 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
269 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
270 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
271 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
272 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
273 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
274 return err;
278 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
280 TRACE("(%p)\n", device);
282 /* are we using waveOut stuff? */
283 if (!device->hwbuf) {
284 unsigned c;
286 /* get out of CS when calling the wave system */
287 LeaveCriticalSection(&(device->mixlock));
288 /* **** */
289 device->pwqueue = (DWORD)-1; /* resetting queues */
290 waveOutReset(device->hwo);
291 for (c=0; c<device->helfrags; c++)
292 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
293 /* **** */
294 EnterCriticalSection(&(device->mixlock));
296 /* clear the queue */
297 device->pwqueue = 0;
298 } else {
299 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
300 if (!ref)
301 device->hwbuf = 0;
302 else
303 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
307 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
309 HRESULT err = DS_OK;
310 TRACE("(%p)\n", device);
312 device->buflen = ds_hel_buflen;
313 err = DSOUND_PrimaryOpen(device);
315 if (err != DS_OK) {
316 WARN("DSOUND_PrimaryOpen failed\n");
317 return err;
320 device->state = STATE_STOPPED;
321 return DS_OK;
324 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
326 TRACE("(%p)\n", device);
328 /* **** */
329 EnterCriticalSection(&(device->mixlock));
331 DSOUND_PrimaryClose(device);
332 if (device->driver) {
333 if (device->hwbuf) {
334 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
335 device->hwbuf = 0;
337 } else
338 HeapFree(GetProcessHeap(),0,device->pwave);
339 HeapFree(GetProcessHeap(),0,device->pwfx);
340 device->pwfx=NULL;
342 LeaveCriticalSection(&(device->mixlock));
343 /* **** */
345 return DS_OK;
348 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
350 HRESULT err = DS_OK;
351 TRACE("(%p)\n", device);
353 if (device->hwbuf) {
354 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
355 if (err != DS_OK)
356 WARN("IDsDriverBuffer_Play failed\n");
357 } else {
358 err = mmErr(waveOutRestart(device->hwo));
359 if (err != DS_OK)
360 WARN("waveOutRestart failed\n");
363 return err;
366 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
368 HRESULT err = DS_OK;
369 TRACE("(%p)\n", device);
371 if (device->hwbuf) {
372 err = IDsDriverBuffer_Stop(device->hwbuf);
373 if (err == DSERR_BUFFERLOST) {
374 DSOUND_PrimaryClose(device);
375 err = DSOUND_ReopenDevice(device, FALSE);
376 if (FAILED(err))
377 ERR("DSOUND_ReopenDevice failed\n");
378 else
380 err = DSOUND_PrimaryOpen(device);
381 if (FAILED(err))
382 WARN("DSOUND_PrimaryOpen failed\n");
384 } else if (err != DS_OK) {
385 WARN("IDsDriverBuffer_Stop failed\n");
387 } else {
389 /* don't call the wave system with the lock set */
390 LeaveCriticalSection(&(device->mixlock));
391 /* **** */
393 err = mmErr(waveOutPause(device->hwo));
395 /* **** */
396 EnterCriticalSection(&(device->mixlock));
398 if (err != DS_OK)
399 WARN("waveOutPause failed\n");
402 return err;
405 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
407 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
409 if (device->hwbuf) {
410 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
411 if (err) {
412 WARN("IDsDriverBuffer_GetPosition failed\n");
413 return err;
415 } else {
416 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
418 /* check if playpos was requested */
419 if (playpos)
420 /* use the cached play position */
421 *playpos = device->pwplay * device->fraglen;
423 /* check if writepos was requested */
424 if (writepos)
425 /* the writepos is the first non-queued position */
426 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
428 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
429 return DS_OK;
432 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
434 HRESULT err = DSERR_BUFFERLOST;
435 int i, alloc_size, cp_size;
436 DWORD nSamplesPerSec, bpp, chans;
437 TRACE("(%p,%p)\n", device, wfex);
439 if (device->priolevel == DSSCL_NORMAL) {
440 WARN("failed priority check!\n");
441 return DSERR_PRIOLEVELNEEDED;
444 /* Let's be pedantic! */
445 if (wfex == NULL) {
446 WARN("invalid parameter: wfex==NULL!\n");
447 return DSERR_INVALIDPARAM;
449 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
450 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
451 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
452 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
453 wfex->wBitsPerSample, wfex->cbSize);
455 /* **** */
456 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
457 EnterCriticalSection(&(device->mixlock));
459 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
460 alloc_size = sizeof(WAVEFORMATEX);
461 cp_size = sizeof(PCMWAVEFORMAT);
462 } else
463 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
465 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
467 nSamplesPerSec = device->pwfx->nSamplesPerSec;
468 bpp = device->pwfx->wBitsPerSample;
469 chans = device->pwfx->nChannels;
471 CopyMemory(device->pwfx, wfex, cp_size);
473 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
474 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
476 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
477 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
479 err = DSERR_BUFFERLOST;
480 CopyMemory(device->pwfx, wfex, cp_size);
483 if (err != DSERR_BUFFERLOST && FAILED(err)) {
484 WARN("IDsDriverBuffer_SetFormat failed\n");
485 if (!forced)
486 err = DS_OK;
487 goto done;
490 if (err == S_FALSE)
492 /* ALSA specific: S_FALSE tells that recreation was successful,
493 * but size and location may be changed, and buffer has to be restarted
494 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
495 * and the entire re-initialization will occur anyway
497 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
498 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
500 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
501 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
502 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
503 err = DS_OK;
505 DSOUND_RecalcPrimary(device);
508 if (err == DSERR_BUFFERLOST)
510 DSOUND_PrimaryClose(device);
512 err = DSOUND_ReopenDevice(device, FALSE);
513 if (FAILED(err))
515 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
516 goto done;
518 err = DSOUND_PrimaryOpen(device);
519 if (err != DS_OK) {
520 WARN("DSOUND_PrimaryOpen failed\n");
521 goto done;
524 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
526 DSOUND_PrimaryClose(device);
527 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
528 err = DSOUND_ReopenDevice(device, TRUE);
529 if (FAILED(err))
530 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
531 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
532 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
536 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
537 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
538 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
539 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
540 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
542 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
543 IDirectSoundBufferImpl** dsb = device->buffers;
544 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
545 /* **** */
546 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
548 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
549 DSOUND_RecalcFormat((*dsb));
550 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen);
551 (*dsb)->primary_mixpos = 0;
553 RtlReleaseResource(&(*dsb)->lock);
554 /* **** */
558 done:
559 LeaveCriticalSection(&(device->mixlock));
560 RtlReleaseResource(&(device->buffer_list_lock));
561 /* **** */
563 return err;
566 /*******************************************************************************
567 * PrimaryBuffer
569 /* This sets this format for the <em>Primary Buffer Only</em> */
570 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
571 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
572 LPDIRECTSOUNDBUFFER iface,
573 LPCWAVEFORMATEX wfex)
575 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
576 TRACE("(%p,%p)\n", iface, wfex);
577 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
580 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
581 LPDIRECTSOUNDBUFFER iface,LONG vol
583 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
584 DWORD ampfactors;
585 DSVOLUMEPAN volpan;
586 HRESULT hres = DS_OK;
587 TRACE("(%p,%d)\n", iface, vol);
589 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
590 WARN("control unavailable\n");
591 return DSERR_CONTROLUNAVAIL;
594 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
595 WARN("invalid parameter: vol = %d\n", vol);
596 return DSERR_INVALIDPARAM;
599 /* **** */
600 EnterCriticalSection(&(device->mixlock));
602 waveOutGetVolume(device->hwo, &ampfactors);
603 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
604 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
605 DSOUND_AmpFactorToVolPan(&volpan);
606 if (vol != volpan.lVolume) {
607 volpan.lVolume=vol;
608 DSOUND_RecalcVolPan(&volpan);
609 if (device->hwbuf) {
610 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
611 if (hres != DS_OK)
612 WARN("IDsDriverBuffer_SetVolumePan failed\n");
613 } else {
614 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
615 waveOutSetVolume(device->hwo, ampfactors);
619 LeaveCriticalSection(&(device->mixlock));
620 /* **** */
622 return hres;
625 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
626 LPDIRECTSOUNDBUFFER iface,LPLONG vol
628 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
629 DWORD ampfactors;
630 DSVOLUMEPAN volpan;
631 TRACE("(%p,%p)\n", iface, vol);
633 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
634 WARN("control unavailable\n");
635 return DSERR_CONTROLUNAVAIL;
638 if (vol == NULL) {
639 WARN("invalid parameter: vol = NULL\n");
640 return DSERR_INVALIDPARAM;
643 waveOutGetVolume(device->hwo, &ampfactors);
644 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
645 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
646 DSOUND_AmpFactorToVolPan(&volpan);
647 *vol = volpan.lVolume;
648 return DS_OK;
651 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
652 LPDIRECTSOUNDBUFFER iface,DWORD freq
654 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
655 TRACE("(%p,%d)\n",This,freq);
657 /* You cannot set the frequency of the primary buffer */
658 WARN("control unavailable\n");
659 return DSERR_CONTROLUNAVAIL;
662 static HRESULT WINAPI PrimaryBufferImpl_Play(
663 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
665 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
666 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
668 if (!(flags & DSBPLAY_LOOPING)) {
669 WARN("invalid parameter: flags = %08x\n", flags);
670 return DSERR_INVALIDPARAM;
673 /* **** */
674 EnterCriticalSection(&(device->mixlock));
676 if (device->state == STATE_STOPPED)
677 device->state = STATE_STARTING;
678 else if (device->state == STATE_STOPPING)
679 device->state = STATE_PLAYING;
681 LeaveCriticalSection(&(device->mixlock));
682 /* **** */
684 return DS_OK;
687 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
689 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
690 TRACE("(%p)\n", iface);
692 /* **** */
693 EnterCriticalSection(&(device->mixlock));
695 if (device->state == STATE_PLAYING)
696 device->state = STATE_STOPPING;
697 else if (device->state == STATE_STARTING)
698 device->state = STATE_STOPPED;
700 LeaveCriticalSection(&(device->mixlock));
701 /* **** */
703 return DS_OK;
706 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
708 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
709 ULONG ref = InterlockedIncrement(&(This->ref));
710 TRACE("(%p) ref was %d\n", This, ref - 1);
711 return ref;
714 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
716 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
717 DWORD ref = InterlockedDecrement(&(This->ref));
718 TRACE("(%p) ref was %d\n", This, ref + 1);
720 if (!ref) {
721 This->device->primary = NULL;
722 HeapFree(GetProcessHeap(), 0, This);
723 TRACE("(%p) released\n", This);
725 return ref;
728 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
729 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
731 HRESULT hres;
732 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
733 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
735 /* **** */
736 EnterCriticalSection(&(device->mixlock));
738 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
739 if (hres != DS_OK) {
740 WARN("DSOUND_PrimaryGetPosition failed\n");
741 LeaveCriticalSection(&(device->mixlock));
742 return hres;
744 if (writepos) {
745 if (device->state != STATE_STOPPED)
746 /* apply the documented 10ms lead to writepos */
747 *writepos += device->writelead;
748 while (*writepos >= device->buflen) *writepos -= device->buflen;
751 LeaveCriticalSection(&(device->mixlock));
752 /* **** */
754 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
755 return DS_OK;
758 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
759 LPDIRECTSOUNDBUFFER iface,LPDWORD status
761 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
762 TRACE("(%p,%p)\n", iface, status);
764 if (status == NULL) {
765 WARN("invalid parameter: status == NULL\n");
766 return DSERR_INVALIDPARAM;
769 *status = 0;
770 if ((device->state == STATE_STARTING) ||
771 (device->state == STATE_PLAYING))
772 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
774 TRACE("status=%x\n", *status);
775 return DS_OK;
779 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
780 LPDIRECTSOUNDBUFFER iface,
781 LPWAVEFORMATEX lpwf,
782 DWORD wfsize,
783 LPDWORD wfwritten)
785 DWORD size;
786 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
787 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
789 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
791 if (lpwf) { /* NULL is valid */
792 if (wfsize >= size) {
793 CopyMemory(lpwf,device->pwfx,size);
794 if (wfwritten)
795 *wfwritten = size;
796 } else {
797 WARN("invalid parameter: wfsize too small\n");
798 if (wfwritten)
799 *wfwritten = 0;
800 return DSERR_INVALIDPARAM;
802 } else {
803 if (wfwritten)
804 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
805 else {
806 WARN("invalid parameter: wfwritten == NULL\n");
807 return DSERR_INVALIDPARAM;
811 return DS_OK;
814 static HRESULT WINAPI PrimaryBufferImpl_Lock(
815 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
817 HRESULT hres;
818 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
819 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
820 iface,
821 writecursor,
822 writebytes,
823 lplpaudioptr1,
824 audiobytes1,
825 lplpaudioptr2,
826 audiobytes2,
827 flags,
828 GetTickCount()
831 if (device->priolevel != DSSCL_WRITEPRIMARY) {
832 WARN("failed priority check!\n");
833 return DSERR_PRIOLEVELNEEDED;
836 /* when this flag is set, writecursor is meaningless and must be calculated */
837 if (flags & DSBLOCK_FROMWRITECURSOR) {
838 /* GetCurrentPosition does too much magic to duplicate here */
839 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
840 if (hres != DS_OK) {
841 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
842 return hres;
846 /* when this flag is set, writebytes is meaningless and must be set */
847 if (flags & DSBLOCK_ENTIREBUFFER)
848 writebytes = device->buflen;
850 if (writecursor >= device->buflen) {
851 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
852 writecursor, device->buflen);
853 return DSERR_INVALIDPARAM;
856 if (writebytes > device->buflen) {
857 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
858 writebytes, device->buflen);
859 return DSERR_INVALIDPARAM;
862 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
863 hres = IDsDriverBuffer_Lock(device->hwbuf,
864 lplpaudioptr1, audiobytes1,
865 lplpaudioptr2, audiobytes2,
866 writecursor, writebytes,
868 if (hres != DS_OK) {
869 WARN("IDsDriverBuffer_Lock failed\n");
870 return hres;
872 } else {
873 if (writecursor+writebytes <= device->buflen) {
874 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
875 *audiobytes1 = writebytes;
876 if (lplpaudioptr2)
877 *(LPBYTE*)lplpaudioptr2 = NULL;
878 if (audiobytes2)
879 *audiobytes2 = 0;
880 TRACE("->%d.0\n",writebytes);
881 } else {
882 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
883 *audiobytes1 = device->buflen-writecursor;
884 if (lplpaudioptr2)
885 *(LPBYTE*)lplpaudioptr2 = device->buffer;
886 if (audiobytes2)
887 *audiobytes2 = writebytes-(device->buflen-writecursor);
888 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
891 return DS_OK;
894 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
895 LPDIRECTSOUNDBUFFER iface,DWORD newpos
897 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
898 TRACE("(%p,%d)\n",This,newpos);
900 /* You cannot set the position of the primary buffer */
901 WARN("invalid call\n");
902 return DSERR_INVALIDCALL;
905 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
906 LPDIRECTSOUNDBUFFER iface,LONG pan
908 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
909 DWORD ampfactors;
910 DSVOLUMEPAN volpan;
911 HRESULT hres = DS_OK;
912 TRACE("(%p,%d)\n", iface, pan);
914 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
915 WARN("control unavailable\n");
916 return DSERR_CONTROLUNAVAIL;
919 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
920 WARN("invalid parameter: pan = %d\n", pan);
921 return DSERR_INVALIDPARAM;
924 /* **** */
925 EnterCriticalSection(&(device->mixlock));
927 waveOutGetVolume(device->hwo, &ampfactors);
928 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
929 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
930 DSOUND_AmpFactorToVolPan(&volpan);
931 if (pan != volpan.lPan) {
932 volpan.lPan=pan;
933 DSOUND_RecalcVolPan(&volpan);
934 if (device->hwbuf) {
935 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
936 if (hres != DS_OK)
937 WARN("IDsDriverBuffer_SetVolumePan failed\n");
938 } else {
939 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
940 waveOutSetVolume(device->hwo, ampfactors);
944 LeaveCriticalSection(&(device->mixlock));
945 /* **** */
947 return hres;
950 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
951 LPDIRECTSOUNDBUFFER iface,LPLONG pan
953 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
954 DWORD ampfactors;
955 DSVOLUMEPAN volpan;
956 TRACE("(%p,%p)\n", iface, pan);
958 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
959 WARN("control unavailable\n");
960 return DSERR_CONTROLUNAVAIL;
963 if (pan == NULL) {
964 WARN("invalid parameter: pan == NULL\n");
965 return DSERR_INVALIDPARAM;
968 waveOutGetVolume(device->hwo, &ampfactors);
969 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
970 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
971 DSOUND_AmpFactorToVolPan(&volpan);
972 *pan = volpan.lPan;
973 return DS_OK;
976 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
977 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
979 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
980 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
982 if (device->priolevel != DSSCL_WRITEPRIMARY) {
983 WARN("failed priority check!\n");
984 return DSERR_PRIOLEVELNEEDED;
987 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
988 HRESULT hres;
990 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
991 if (hres != DS_OK) {
992 WARN("IDsDriverBuffer_Unlock failed\n");
993 return hres;
997 return DS_OK;
1000 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1001 LPDIRECTSOUNDBUFFER iface
1003 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1004 FIXME("(%p):stub\n",This);
1005 return DS_OK;
1008 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1009 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1011 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1012 TRACE("(%p,%p)\n", iface, freq);
1014 if (freq == NULL) {
1015 WARN("invalid parameter: freq == NULL\n");
1016 return DSERR_INVALIDPARAM;
1019 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1020 WARN("control unavailable\n");
1021 return DSERR_CONTROLUNAVAIL;
1024 *freq = device->pwfx->nSamplesPerSec;
1025 TRACE("-> %d\n", *freq);
1027 return DS_OK;
1030 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1031 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1033 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1034 WARN("(%p) already initialized\n", This);
1035 return DSERR_ALREADYINITIALIZED;
1038 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1039 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1041 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1042 TRACE("(%p,%p)\n", iface, caps);
1044 if (caps == NULL) {
1045 WARN("invalid parameter: caps == NULL\n");
1046 return DSERR_INVALIDPARAM;
1049 if (caps->dwSize < sizeof(*caps)) {
1050 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1051 return DSERR_INVALIDPARAM;
1054 caps->dwFlags = device->dsbd.dwFlags;
1055 caps->dwBufferBytes = device->buflen;
1057 /* Windows reports these as zero */
1058 caps->dwUnlockTransferRate = 0;
1059 caps->dwPlayCpuOverhead = 0;
1061 return DS_OK;
1064 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1065 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1067 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1068 DirectSoundDevice *device = This->device;
1069 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1071 if (ppobj == NULL) {
1072 WARN("invalid parameter\n");
1073 return E_INVALIDARG;
1076 *ppobj = NULL; /* assume failure */
1078 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1079 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1080 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1081 *ppobj = This;
1082 return S_OK;
1085 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1086 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1087 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1088 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1089 return E_NOINTERFACE;
1092 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1093 ERR("app requested IDirectSoundNotify on primary buffer\n");
1094 /* FIXME: should we support this? */
1095 return E_NOINTERFACE;
1098 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1099 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1100 return E_NOINTERFACE;
1103 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1104 if (!device->listener)
1105 IDirectSound3DListenerImpl_Create(device, &device->listener);
1106 if (device->listener) {
1107 *ppobj = device->listener;
1108 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1109 return S_OK;
1112 WARN("IID_IDirectSound3DListener failed\n");
1113 return E_NOINTERFACE;
1116 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1117 FIXME("app requested IKsPropertySet on primary buffer\n");
1118 return E_NOINTERFACE;
1121 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1122 return E_NOINTERFACE;
1125 static const IDirectSoundBufferVtbl dspbvt =
1127 PrimaryBufferImpl_QueryInterface,
1128 PrimaryBufferImpl_AddRef,
1129 PrimaryBufferImpl_Release,
1130 PrimaryBufferImpl_GetCaps,
1131 PrimaryBufferImpl_GetCurrentPosition,
1132 PrimaryBufferImpl_GetFormat,
1133 PrimaryBufferImpl_GetVolume,
1134 PrimaryBufferImpl_GetPan,
1135 PrimaryBufferImpl_GetFrequency,
1136 PrimaryBufferImpl_GetStatus,
1137 PrimaryBufferImpl_Initialize,
1138 PrimaryBufferImpl_Lock,
1139 PrimaryBufferImpl_Play,
1140 PrimaryBufferImpl_SetCurrentPosition,
1141 PrimaryBufferImpl_SetFormat,
1142 PrimaryBufferImpl_SetVolume,
1143 PrimaryBufferImpl_SetPan,
1144 PrimaryBufferImpl_SetFrequency,
1145 PrimaryBufferImpl_Stop,
1146 PrimaryBufferImpl_Unlock,
1147 PrimaryBufferImpl_Restore
1150 HRESULT PrimaryBufferImpl_Create(
1151 DirectSoundDevice * device,
1152 PrimaryBufferImpl ** ppdsb,
1153 LPCDSBUFFERDESC dsbd)
1155 PrimaryBufferImpl *dsb;
1156 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1158 if (dsbd->lpwfxFormat) {
1159 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1160 *ppdsb = NULL;
1161 return DSERR_INVALIDPARAM;
1164 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1166 if (dsb == NULL) {
1167 WARN("out of memory\n");
1168 *ppdsb = NULL;
1169 return DSERR_OUTOFMEMORY;
1172 dsb->ref = 0;
1173 dsb->device = device;
1174 dsb->lpVtbl = &dspbvt;
1176 CopyMemory(&device->dsbd, dsbd, sizeof(*dsbd));
1178 TRACE("Created primary buffer at %p\n", dsb);
1179 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1180 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1181 device->pwfx->wFormatTag, device->pwfx->nChannels,
1182 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1183 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1184 device->pwfx->cbSize);
1186 *ppdsb = dsb;
1187 return S_OK;