Don't skip usable X fonts. Add support for koi8 fonts.
[wine/hacks.git] / dlls / dsound / dsound_main.c
blobe82831b59d58f014175c744a5c099069da7deabd
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 */
6 /*
7 * Note: This file requires multithread ability. It is not possible to
8 * implement the stuff in a single thread anyway. And most DirectX apps
9 * require threading themselves.
11 * Most thread locking is complete. There may be a few race
12 * conditions still lurking.
14 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
15 * and a Turtle Beach Tropez+.
17 * TODO:
18 * Implement DirectSoundCapture API
19 * Implement SetCooperativeLevel properly (need to address focus issues)
20 * Use wavetable synth for static buffers if available
21 * Implement DirectSound3DBuffers (stubs in place)
22 * Use hardware 3D support if available (OSS support may be needed first)
23 * Add support for APIs other than OSS: ALSA (http://alsa.jcu.cz/)
24 * and esound (http://www.gnome.org), for instance
26 * FIXME: Status needs updating.
28 * Status:
29 * - Wing Commander 4/W95:
30 * The intromovie plays without problems. Nearly lipsynchron.
31 * - DiscWorld 2
32 * The sound works, but noticeable chunks are left out (from the sound and
33 * the animation). Don't know why yet.
34 * - Diablo:
35 * Sound works, but slows down the movieplayer.
36 * - XvT:
37 * Doesn't sound yet.
38 * - Monkey Island 3:
39 * The background sound of the startscreen works ;)
40 * - WingCommander Prophecy Demo:
41 * Sound works for the intromovie.
42 * - Total Annihilation (1998/12/04):
43 * Sound plays perfectly in the game, but the Smacker movies
44 * (http://www.smacker.com/) play silently.
45 * - A-10 Cuba! Demo (1998/12/04):
46 * Sound works properly (for some people).
47 * - dsstream.exe, from DirectX 5.2 SDK (1998/12/04):
48 * Works properly, but requires "-dll -winmm".
49 * - dsshow.exe, from DirectX 5.2 SDK (1998/12/04):
50 * Initializes the DLL properly with CoCreateInstance(), but the
51 * FileOpen dialog box is broken - could not test properly
54 #include "config.h"
55 #include <assert.h>
56 #include <errno.h>
57 #include <sys/types.h>
58 #include <sys/time.h>
59 #include <sys/fcntl.h>
60 #include <unistd.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <math.h> /* Insomnia - pow() function */
64 #include "dsound.h"
65 #include "winuser.h"
66 #include "winerror.h"
67 #include "wine/obj_base.h"
68 #include "thread.h"
69 #include "debugtools.h"
71 DEFAULT_DEBUG_CHANNEL(dsound)
74 /*****************************************************************************
75 * Predeclare the interface implementation structures
77 typedef struct IDirectSoundImpl IDirectSoundImpl;
78 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
79 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
80 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
81 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
83 /*****************************************************************************
84 * IDirectSound implementation structure
86 struct IDirectSoundImpl
88 /* IUnknown fields */
89 ICOM_VFIELD(IDirectSound);
90 DWORD ref;
91 /* IDirectSoundImpl fields */
92 DWORD priolevel;
93 int nrofbuffers;
94 IDirectSoundBufferImpl** buffers;
95 IDirectSoundBufferImpl* primary;
96 IDirectSound3DListenerImpl* listener;
97 WAVEFORMATEX wfx; /* current main waveformat */
100 /*****************************************************************************
101 * IDirectSoundBuffer implementation structure
103 struct IDirectSoundBufferImpl
105 /* IUnknown fields */
106 ICOM_VFIELD(IDirectSoundBuffer);
107 DWORD ref;
108 /* IDirectSoundBufferImpl fields */
109 WAVEFORMATEX wfx;
110 LPBYTE buffer;
111 IDirectSound3DBufferImpl* ds3db;
112 DWORD playflags,playing;
113 DWORD playpos,writepos,buflen;
114 DWORD nAvgBytesPerSec;
115 DWORD freq;
116 ULONG freqAdjust;
117 LONG volume,pan;
118 LONG lVolAdjust,rVolAdjust;
119 IDirectSoundBufferImpl* parent; /* for duplicates */
120 IDirectSoundImpl* dsound;
121 DSBUFFERDESC dsbd;
122 LPDSBPOSITIONNOTIFY notifies;
123 int nrofnotifies;
124 CRITICAL_SECTION lock;
127 /*****************************************************************************
128 * IDirectSoundNotify implementation structure
130 struct IDirectSoundNotifyImpl
132 /* IUnknown fields */
133 ICOM_VFIELD(IDirectSoundNotify);
134 DWORD ref;
135 /* IDirectSoundNotifyImpl fields */
136 IDirectSoundBufferImpl* dsb;
139 /*****************************************************************************
140 * IDirectSound3DListener implementation structure
142 struct IDirectSound3DListenerImpl
144 /* IUnknown fields */
145 ICOM_VFIELD(IDirectSound3DListener);
146 DWORD ref;
147 /* IDirectSound3DListenerImpl fields */
148 IDirectSoundBufferImpl* dsb;
149 DS3DLISTENER ds3dl;
150 CRITICAL_SECTION lock;
153 /*****************************************************************************
154 * IDirectSound3DBuffer implementation structure
156 struct IDirectSound3DBufferImpl
158 /* IUnknown fields */
159 ICOM_VFIELD(IDirectSound3DBuffer);
160 DWORD ref;
161 /* IDirectSound3DBufferImpl fields */
162 IDirectSoundBufferImpl* dsb;
163 DS3DBUFFER ds3db;
164 LPBYTE buffer;
165 DWORD buflen;
166 CRITICAL_SECTION lock;
170 #ifdef HAVE_OSS
171 # include <sys/ioctl.h>
172 # ifdef HAVE_MACHINE_SOUNDCARD_H
173 # include <machine/soundcard.h>
174 # endif
175 # ifdef HAVE_SYS_SOUNDCARD_H
176 # include <sys/soundcard.h>
177 # endif
179 /* #define USE_DSOUND3D 1 */
181 #define DSOUND_FRAGLEN (primarybuf->wfx.nAvgBytesPerSec >> 4)
182 #define DSOUND_FREQSHIFT (14)
184 static int audiofd = -1;
185 static int audioOK = 0;
187 static IDirectSoundImpl* dsound = NULL;
189 static IDirectSoundBufferImpl* primarybuf = NULL;
191 static int DSOUND_setformat(LPWAVEFORMATEX wfex);
192 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
193 static void DSOUND_CloseAudio(void);
195 #endif
198 HRESULT WINAPI DirectSoundEnumerateA(
199 LPDSENUMCALLBACKA enumcb,
200 LPVOID context)
202 TRACE("enumcb = %p, context = %p\n", enumcb, context);
204 #ifdef HAVE_OSS
205 if (enumcb != NULL)
206 enumcb(NULL,"WINE DirectSound using Open Sound System",
207 "sound",context);
208 #endif
210 return DS_OK;
213 #ifdef HAVE_OSS
214 static void _dump_DSBCAPS(DWORD xmask) {
215 struct {
216 DWORD mask;
217 char *name;
218 } flags[] = {
219 #define FE(x) { x, #x },
220 FE(DSBCAPS_PRIMARYBUFFER)
221 FE(DSBCAPS_STATIC)
222 FE(DSBCAPS_LOCHARDWARE)
223 FE(DSBCAPS_LOCSOFTWARE)
224 FE(DSBCAPS_CTRLFREQUENCY)
225 FE(DSBCAPS_CTRLPAN)
226 FE(DSBCAPS_CTRLVOLUME)
227 FE(DSBCAPS_CTRLDEFAULT)
228 FE(DSBCAPS_CTRLALL)
229 FE(DSBCAPS_STICKYFOCUS)
230 FE(DSBCAPS_GETCURRENTPOSITION2)
232 int i;
234 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
235 if (flags[i].mask & xmask)
236 fprintf(stderr,"%s ",flags[i].name);
239 /*******************************************************************************
240 * IDirectSound3DBuffer
243 /* IUnknown methods */
244 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
245 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
247 ICOM_THIS(IDirectSound3DBufferImpl,iface);
248 char xbuf[50];
250 WINE_StringFromCLSID(riid,xbuf);
251 TRACE("(%p,%s,%p)\n",This,xbuf,ppobj);
252 return E_FAIL;
255 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
257 ICOM_THIS(IDirectSound3DBufferImpl,iface);
258 This->ref++;
259 return This->ref;
262 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
264 ICOM_THIS(IDirectSound3DBufferImpl,iface);
265 if(--This->ref)
266 return This->ref;
268 HeapFree(GetProcessHeap(),0,This->buffer);
269 HeapFree(GetProcessHeap(),0,This);
271 return S_OK;
274 /* IDirectSound3DBuffer methods */
275 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
276 LPDIRECTSOUND3DBUFFER iface,
277 LPDS3DBUFFER lpDs3dBuffer)
279 FIXME("stub\n");
280 return DS_OK;
283 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
284 LPDIRECTSOUND3DBUFFER iface,
285 LPDWORD lpdwInsideConeAngle,
286 LPDWORD lpdwOutsideConeAngle)
288 FIXME("stub\n");
289 return DS_OK;
292 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
293 LPDIRECTSOUND3DBUFFER iface,
294 LPD3DVECTOR lpvConeOrientation)
296 FIXME("stub\n");
297 return DS_OK;
300 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
301 LPDIRECTSOUND3DBUFFER iface,
302 LPLONG lplConeOutsideVolume)
304 FIXME("stub\n");
305 return DS_OK;
308 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
309 LPDIRECTSOUND3DBUFFER iface,
310 LPD3DVALUE lpfMaxDistance)
312 FIXME("stub\n");
313 return DS_OK;
316 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
317 LPDIRECTSOUND3DBUFFER iface,
318 LPD3DVALUE lpfMinDistance)
320 FIXME("stub\n");
321 return DS_OK;
324 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
325 LPDIRECTSOUND3DBUFFER iface,
326 LPDWORD lpdwMode)
328 FIXME("stub\n");
329 return DS_OK;
332 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
333 LPDIRECTSOUND3DBUFFER iface,
334 LPD3DVECTOR lpvPosition)
336 FIXME("stub\n");
337 return DS_OK;
340 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
341 LPDIRECTSOUND3DBUFFER iface,
342 LPD3DVECTOR lpvVelocity)
344 FIXME("stub\n");
345 return DS_OK;
348 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
349 LPDIRECTSOUND3DBUFFER iface,
350 LPCDS3DBUFFER lpcDs3dBuffer,
351 DWORD dwApply)
353 FIXME("stub\n");
354 return DS_OK;
357 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
358 LPDIRECTSOUND3DBUFFER iface,
359 DWORD dwInsideConeAngle,
360 DWORD dwOutsideConeAngle,
361 DWORD dwApply)
363 FIXME("stub\n");
364 return DS_OK;
367 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
368 LPDIRECTSOUND3DBUFFER iface,
369 D3DVALUE x, D3DVALUE y, D3DVALUE z,
370 DWORD dwApply)
372 FIXME("stub\n");
373 return DS_OK;
376 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
377 LPDIRECTSOUND3DBUFFER iface,
378 LONG lConeOutsideVolume,
379 DWORD dwApply)
381 FIXME("stub\n");
382 return DS_OK;
385 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
386 LPDIRECTSOUND3DBUFFER iface,
387 D3DVALUE fMaxDistance,
388 DWORD dwApply)
390 FIXME("stub\n");
391 return DS_OK;
394 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
395 LPDIRECTSOUND3DBUFFER iface,
396 D3DVALUE fMinDistance,
397 DWORD dwApply)
399 FIXME("stub\n");
400 return DS_OK;
403 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
404 LPDIRECTSOUND3DBUFFER iface,
405 DWORD dwMode,
406 DWORD dwApply)
408 ICOM_THIS(IDirectSound3DBufferImpl,iface);
409 TRACE("mode = %lx\n", dwMode);
410 This->ds3db.dwMode = dwMode;
411 return DS_OK;
414 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
415 LPDIRECTSOUND3DBUFFER iface,
416 D3DVALUE x, D3DVALUE y, D3DVALUE z,
417 DWORD dwApply)
419 FIXME("stub\n");
420 return DS_OK;
423 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
424 LPDIRECTSOUND3DBUFFER iface,
425 D3DVALUE x, D3DVALUE y, D3DVALUE z,
426 DWORD dwApply)
428 FIXME("stub\n");
429 return DS_OK;
432 ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
434 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
435 /* IUnknown methods */
436 IDirectSound3DBufferImpl_QueryInterface,
437 IDirectSound3DBufferImpl_AddRef,
438 IDirectSound3DBufferImpl_Release,
439 /* IDirectSound3DBuffer methods */
440 IDirectSound3DBufferImpl_GetAllParameters,
441 IDirectSound3DBufferImpl_GetConeAngles,
442 IDirectSound3DBufferImpl_GetConeOrientation,
443 IDirectSound3DBufferImpl_GetConeOutsideVolume,
444 IDirectSound3DBufferImpl_GetMaxDistance,
445 IDirectSound3DBufferImpl_GetMinDistance,
446 IDirectSound3DBufferImpl_GetMode,
447 IDirectSound3DBufferImpl_GetPosition,
448 IDirectSound3DBufferImpl_GetVelocity,
449 IDirectSound3DBufferImpl_SetAllParameters,
450 IDirectSound3DBufferImpl_SetConeAngles,
451 IDirectSound3DBufferImpl_SetConeOrientation,
452 IDirectSound3DBufferImpl_SetConeOutsideVolume,
453 IDirectSound3DBufferImpl_SetMaxDistance,
454 IDirectSound3DBufferImpl_SetMinDistance,
455 IDirectSound3DBufferImpl_SetMode,
456 IDirectSound3DBufferImpl_SetPosition,
457 IDirectSound3DBufferImpl_SetVelocity,
460 #ifdef USE_DSOUND3D
461 static int DSOUND_Create3DBuffer(IDirectSoundBufferImpl* dsb)
463 DWORD i, temp, iSize, oSize, offset;
464 LPBYTE bIbuf, bObuf, bTbuf = NULL;
465 LPWORD wIbuf, wObuf, wTbuf = NULL;
467 /* Inside DirectX says it's stupid but allowed */
468 if (dsb->wfx.nChannels == 2) {
469 /* Convert to mono */
470 if (dsb->wfx.wBitsPerSample == 16) {
471 iSize = dsb->buflen / 4;
472 wTbuf = malloc(dsb->buflen / 2);
473 if (wTbuf == NULL)
474 return DSERR_OUTOFMEMORY;
475 for (i = 0; i < iSize; i++)
476 wTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
477 wIbuf = wTbuf;
478 } else {
479 iSize = dsb->buflen / 2;
480 bTbuf = malloc(dsb->buflen / 2);
481 if (bTbuf == NULL)
482 return DSERR_OUTOFMEMORY;
483 for (i = 0; i < iSize; i++)
484 bTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
485 bIbuf = bTbuf;
487 } else {
488 if (dsb->wfx.wBitsPerSample == 16) {
489 iSize = dsb->buflen / 2;
490 wIbuf = (LPWORD) dsb->buffer;
491 } else {
492 bIbuf = (LPBYTE) dsb->buffer;
493 iSize = dsb->buflen;
497 if (primarybuf->wfx.wBitsPerSample == 16) {
498 wObuf = (LPWORD) dsb->ds3db->buffer;
499 oSize = dsb->ds3db->buflen / 2;
500 } else {
501 bObuf = (LPBYTE) dsb->ds3db->buffer;
502 oSize = dsb->ds3db->buflen;
505 offset = primarybuf->wfx.nSamplesPerSec / 100; /* 10ms */
506 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
507 for (i = 0; i < iSize; i++) {
508 temp = wIbuf[i];
509 if (i >= offset)
510 temp += wIbuf[i - offset] >> 9;
511 else
512 temp += wIbuf[i + iSize - offset] >> 9;
513 wObuf[i * 2] = temp;
514 wObuf[(i * 2) + 1] = temp;
516 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
517 for (i = 0; i < iSize; i++) {
518 temp = bIbuf[i];
519 if (i >= offset)
520 temp += bIbuf[i - offset] >> 5;
521 else
522 temp += bIbuf[i + iSize - offset] >> 5;
523 bObuf[i * 2] = temp;
524 bObuf[(i * 2) + 1] = temp;
527 if (wTbuf)
528 free(wTbuf);
529 if (bTbuf)
530 free(bTbuf);
532 return DS_OK;
534 #endif
535 /*******************************************************************************
536 * IDirectSound3DListener
539 /* IUnknown methods */
540 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
541 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
543 ICOM_THIS(IDirectSound3DListenerImpl,iface);
544 char xbuf[50];
546 WINE_StringFromCLSID(riid,xbuf);
547 TRACE("(%p,%s,%p)\n",This,xbuf,ppobj);
548 return E_FAIL;
551 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
553 ICOM_THIS(IDirectSound3DListenerImpl,iface);
554 This->ref++;
555 return This->ref;
558 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
560 ICOM_THIS(IDirectSound3DListenerImpl,iface);
561 This->ref--;
562 return This->ref;
565 /* IDirectSound3DListener methods */
566 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
567 LPDIRECTSOUND3DLISTENER iface,
568 LPDS3DLISTENER lpDS3DL)
570 FIXME("stub\n");
571 return DS_OK;
574 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
575 LPDIRECTSOUND3DLISTENER iface,
576 LPD3DVALUE lpfDistanceFactor)
578 FIXME("stub\n");
579 return DS_OK;
582 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
583 LPDIRECTSOUND3DLISTENER iface,
584 LPD3DVALUE lpfDopplerFactor)
586 FIXME("stub\n");
587 return DS_OK;
590 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
591 LPDIRECTSOUND3DLISTENER iface,
592 LPD3DVECTOR lpvOrientFront,
593 LPD3DVECTOR lpvOrientTop)
595 FIXME("stub\n");
596 return DS_OK;
599 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
600 LPDIRECTSOUND3DLISTENER iface,
601 LPD3DVECTOR lpvPosition)
603 FIXME("stub\n");
604 return DS_OK;
607 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
608 LPDIRECTSOUND3DLISTENER iface,
609 LPD3DVALUE lpfRolloffFactor)
611 FIXME("stub\n");
612 return DS_OK;
615 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
616 LPDIRECTSOUND3DLISTENER iface,
617 LPD3DVECTOR lpvVelocity)
619 FIXME("stub\n");
620 return DS_OK;
623 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
624 LPDIRECTSOUND3DLISTENER iface,
625 LPCDS3DLISTENER lpcDS3DL,
626 DWORD dwApply)
628 FIXME("stub\n");
629 return DS_OK;
632 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
633 LPDIRECTSOUND3DLISTENER iface,
634 D3DVALUE fDistanceFactor,
635 DWORD dwApply)
637 FIXME("stub\n");
638 return DS_OK;
641 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
642 LPDIRECTSOUND3DLISTENER iface,
643 D3DVALUE fDopplerFactor,
644 DWORD dwApply)
646 FIXME("stub\n");
647 return DS_OK;
650 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
651 LPDIRECTSOUND3DLISTENER iface,
652 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
653 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
654 DWORD dwApply)
656 FIXME("stub\n");
657 return DS_OK;
660 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
661 LPDIRECTSOUND3DLISTENER iface,
662 D3DVALUE x, D3DVALUE y, D3DVALUE z,
663 DWORD dwApply)
665 FIXME("stub\n");
666 return DS_OK;
669 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
670 LPDIRECTSOUND3DLISTENER iface,
671 D3DVALUE fRolloffFactor,
672 DWORD dwApply)
674 FIXME("stub\n");
675 return DS_OK;
678 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
679 LPDIRECTSOUND3DLISTENER iface,
680 D3DVALUE x, D3DVALUE y, D3DVALUE z,
681 DWORD dwApply)
683 FIXME("stub\n");
684 return DS_OK;
687 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
688 LPDIRECTSOUND3DLISTENER iface)
691 FIXME("stub\n");
692 return DS_OK;
695 ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
697 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
698 /* IUnknown methods */
699 IDirectSound3DListenerImpl_QueryInterface,
700 IDirectSound3DListenerImpl_AddRef,
701 IDirectSound3DListenerImpl_Release,
702 /* IDirectSound3DListener methods */
703 IDirectSound3DListenerImpl_GetAllParameter,
704 IDirectSound3DListenerImpl_GetDistanceFactor,
705 IDirectSound3DListenerImpl_GetDopplerFactor,
706 IDirectSound3DListenerImpl_GetOrientation,
707 IDirectSound3DListenerImpl_GetPosition,
708 IDirectSound3DListenerImpl_GetRolloffFactor,
709 IDirectSound3DListenerImpl_GetVelocity,
710 IDirectSound3DListenerImpl_SetAllParameters,
711 IDirectSound3DListenerImpl_SetDistanceFactor,
712 IDirectSound3DListenerImpl_SetDopplerFactor,
713 IDirectSound3DListenerImpl_SetOrientation,
714 IDirectSound3DListenerImpl_SetPosition,
715 IDirectSound3DListenerImpl_SetRolloffFactor,
716 IDirectSound3DListenerImpl_SetVelocity,
717 IDirectSound3DListenerImpl_CommitDeferredSettings,
720 /*******************************************************************************
721 * IDirectSoundNotify
723 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
724 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
726 ICOM_THIS(IDirectSoundNotifyImpl,iface);
727 char xbuf[50];
729 WINE_StringFromCLSID(riid,xbuf);
730 TRACE("(%p,%s,%p)\n",This,xbuf,ppobj);
731 return E_FAIL;
734 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
735 ICOM_THIS(IDirectSoundNotifyImpl,iface);
736 return ++(This->ref);
739 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
740 ICOM_THIS(IDirectSoundNotifyImpl,iface);
741 This->ref--;
742 if (!This->ref) {
743 IDirectSoundNotify_Release((LPDIRECTSOUNDBUFFER)This->dsb);
744 HeapFree(GetProcessHeap(),0,This);
745 return S_OK;
747 return This->ref;
750 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
751 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
753 ICOM_THIS(IDirectSoundNotifyImpl,iface);
754 int i;
756 if (TRACE_ON(dsound)) {
757 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
758 for (i=0;i<howmuch;i++)
759 TRACE("notify at %ld to 0x%08lx\n",
760 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
762 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
763 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
764 notify,
765 howmuch*sizeof(DSBPOSITIONNOTIFY)
767 This->dsb->nrofnotifies+=howmuch;
769 return S_OK;
772 ICOM_VTABLE(IDirectSoundNotify) dsnvt =
774 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
775 IDirectSoundNotifyImpl_QueryInterface,
776 IDirectSoundNotifyImpl_AddRef,
777 IDirectSoundNotifyImpl_Release,
778 IDirectSoundNotifyImpl_SetNotificationPositions,
781 /*******************************************************************************
782 * IDirectSoundBuffer
785 /* This sets this format for the <em>Primary Buffer Only</em> */
786 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
787 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
788 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX wfex
790 ICOM_THIS(IDirectSoundBufferImpl,iface);
791 IDirectSoundBufferImpl** dsb;
792 int i;
794 /* Let's be pedantic! */
795 if ((wfex == NULL) ||
796 (wfex->wFormatTag != WAVE_FORMAT_PCM) ||
797 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
798 (wfex->nSamplesPerSec < 1) ||
799 (wfex->nBlockAlign < 1) || (wfex->nChannels > 4) ||
800 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
801 TRACE("failed pedantic check!\n");
802 return DSERR_INVALIDPARAM;
805 /* **** */
806 EnterCriticalSection(&(primarybuf->lock));
808 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
809 dsb = dsound->buffers;
810 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
811 /* **** */
812 EnterCriticalSection(&((*dsb)->lock));
814 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
815 wfex->nSamplesPerSec;
817 LeaveCriticalSection(&((*dsb)->lock));
818 /* **** */
822 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
824 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld"
825 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
826 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
827 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
828 wfex->wBitsPerSample, wfex->cbSize);
830 primarybuf->wfx.nAvgBytesPerSec =
831 This->wfx.nSamplesPerSec * This->wfx.nBlockAlign;
833 DSOUND_CloseAudio();
835 LeaveCriticalSection(&(primarybuf->lock));
836 /* **** */
838 return DS_OK;
841 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
842 LPDIRECTSOUNDBUFFER iface,LONG vol
844 ICOM_THIS(IDirectSoundBufferImpl,iface);
845 double temp;
847 TRACE("(%p,%ld)\n",This,vol);
849 /* I'm not sure if we need this for primary buffer */
850 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
851 return DSERR_CONTROLUNAVAIL;
853 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
854 return DSERR_INVALIDPARAM;
856 /* This needs to adjust the soundcard volume when */
857 /* called for the primary buffer */
858 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
859 FIXME("Volume control of primary unimplemented.\n");
860 This->volume = vol;
861 return DS_OK;
864 /* **** */
865 EnterCriticalSection(&(This->lock));
867 This->volume = vol;
869 temp = (double) (This->volume - (This->pan > 0 ? This->pan : 0));
870 This->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
871 temp = (double) (This->volume + (This->pan < 0 ? This->pan : 0));
872 This->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
874 LeaveCriticalSection(&(This->lock));
875 /* **** */
877 TRACE("left = %lx, right = %lx\n", This->lVolAdjust, This->rVolAdjust);
879 return DS_OK;
882 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
883 LPDIRECTSOUNDBUFFER iface,LPLONG vol
885 ICOM_THIS(IDirectSoundBufferImpl,iface);
886 TRACE("(%p,%p)\n",This,vol);
888 if (vol == NULL)
889 return DSERR_INVALIDPARAM;
891 *vol = This->volume;
892 return DS_OK;
895 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
896 LPDIRECTSOUNDBUFFER iface,DWORD freq
898 ICOM_THIS(IDirectSoundBufferImpl,iface);
899 TRACE("(%p,%ld)\n",This,freq);
901 /* You cannot set the frequency of the primary buffer */
902 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
903 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
904 return DSERR_CONTROLUNAVAIL;
906 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
907 return DSERR_INVALIDPARAM;
909 /* **** */
910 EnterCriticalSection(&(This->lock));
912 This->freq = freq;
913 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
914 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
916 LeaveCriticalSection(&(This->lock));
917 /* **** */
919 return DS_OK;
922 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
923 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
925 ICOM_THIS(IDirectSoundBufferImpl,iface);
926 TRACE("(%p,%08lx,%08lx,%08lx)\n",
927 This,reserved1,reserved2,flags
929 This->playflags = flags;
930 This->playing = 1;
931 return DS_OK;
934 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
936 ICOM_THIS(IDirectSoundBufferImpl,iface);
937 TRACE("(%p)\n",This);
939 /* **** */
940 EnterCriticalSection(&(This->lock));
942 This->playing = 0;
943 DSOUND_CheckEvent(This, 0);
945 LeaveCriticalSection(&(This->lock));
946 /* **** */
948 return DS_OK;
951 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface) {
952 ICOM_THIS(IDirectSoundBufferImpl,iface);
953 /* TRACE(dsound,"(%p) ref was %ld\n",This, This->ref); */
955 return ++(This->ref);
957 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER iface) {
958 ICOM_THIS(IDirectSoundBufferImpl,iface);
959 int i;
961 /* TRACE(dsound,"(%p) ref was %ld\n",This, This->ref); */
963 if (--This->ref)
964 return This->ref;
966 for (i=0;i<This->dsound->nrofbuffers;i++)
967 if (This->dsound->buffers[i] == This)
968 break;
969 if (i < This->dsound->nrofbuffers) {
970 /* Put the last buffer of the list in the (now empty) position */
971 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
972 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*This->dsound->nrofbuffers);
973 This->dsound->nrofbuffers--;
974 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
977 DeleteCriticalSection(&(This->lock));
979 if (This->ds3db && ICOM_VTBL(This->ds3db))
980 IDirectSound3DBuffer_Release((LPDIRECTSOUND3DBUFFER)This->ds3db);
982 if (This->parent)
983 /* this is a duplicate buffer */
984 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->parent);
985 else
986 /* this is a toplevel buffer */
987 HeapFree(GetProcessHeap(),0,This->buffer);
989 HeapFree(GetProcessHeap(),0,This);
991 if (This == primarybuf)
992 primarybuf = NULL;
994 return DS_OK;
997 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
998 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
1000 ICOM_THIS(IDirectSoundBufferImpl,iface);
1001 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1002 if (playpos) *playpos = This->playpos;
1003 if (writepos) *writepos = This->writepos;
1004 TRACE("playpos = %ld, writepos = %ld\n", playpos?*playpos:0, writepos?*writepos:0);
1005 return DS_OK;
1008 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
1009 LPDIRECTSOUNDBUFFER iface,LPDWORD status
1011 ICOM_THIS(IDirectSoundBufferImpl,iface);
1012 TRACE("(%p,%p)\n",This,status);
1014 if (status == NULL)
1015 return DSERR_INVALIDPARAM;
1017 *status = 0;
1018 if (This->playing)
1019 *status |= DSBSTATUS_PLAYING;
1020 if (This->playflags & DSBPLAY_LOOPING)
1021 *status |= DSBSTATUS_LOOPING;
1023 return DS_OK;
1027 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
1028 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
1030 ICOM_THIS(IDirectSoundBufferImpl,iface);
1031 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1033 if (wfsize>sizeof(This->wfx))
1034 wfsize = sizeof(This->wfx);
1035 if (lpwf) { /* NULL is valid */
1036 memcpy(lpwf,&(This->wfx),wfsize);
1037 if (wfwritten)
1038 *wfwritten = wfsize;
1039 } else
1040 if (wfwritten)
1041 *wfwritten = sizeof(This->wfx);
1042 else
1043 return DSERR_INVALIDPARAM;
1045 return DS_OK;
1048 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
1049 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
1051 ICOM_THIS(IDirectSoundBufferImpl,iface);
1053 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1054 This,
1055 writecursor,
1056 writebytes,
1057 lplpaudioptr1,
1058 audiobytes1,
1059 lplpaudioptr2,
1060 audiobytes2,
1061 flags
1063 if (flags & DSBLOCK_FROMWRITECURSOR)
1064 writecursor += This->writepos;
1065 if (flags & DSBLOCK_ENTIREBUFFER)
1066 writebytes = This->buflen;
1067 if (writebytes > This->buflen)
1068 writebytes = This->buflen;
1070 assert(audiobytes1!=audiobytes2);
1071 assert(lplpaudioptr1!=lplpaudioptr2);
1072 if (writecursor+writebytes <= This->buflen) {
1073 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1074 *audiobytes1 = writebytes;
1075 if (lplpaudioptr2)
1076 *(LPBYTE*)lplpaudioptr2 = NULL;
1077 if (audiobytes2)
1078 *audiobytes2 = 0;
1079 TRACE("->%ld.0\n",writebytes);
1080 } else {
1081 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1082 *audiobytes1 = This->buflen-writecursor;
1083 if (lplpaudioptr2)
1084 *(LPBYTE*)lplpaudioptr2 = This->buffer;
1085 if (audiobytes2)
1086 *audiobytes2 = writebytes-(This->buflen-writecursor);
1087 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
1089 /* No. See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 21 */
1090 /* This->writepos=(writecursor+writebytes)%This->buflen; */
1091 return DS_OK;
1094 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
1095 LPDIRECTSOUNDBUFFER iface,DWORD newpos
1097 ICOM_THIS(IDirectSoundBufferImpl,iface);
1098 TRACE("(%p,%ld)\n",This,newpos);
1100 /* **** */
1101 EnterCriticalSection(&(This->lock));
1103 This->playpos = newpos;
1105 LeaveCriticalSection(&(This->lock));
1106 /* **** */
1108 return DS_OK;
1111 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
1112 LPDIRECTSOUNDBUFFER iface,LONG pan
1114 ICOM_THIS(IDirectSoundBufferImpl,iface);
1115 double temp;
1117 TRACE("(%p,%ld)\n",This,pan);
1119 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
1120 return DSERR_INVALIDPARAM;
1122 /* You cannot set the pan of the primary buffer */
1123 /* and you cannot use both pan and 3D controls */
1124 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1125 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
1126 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1127 return DSERR_CONTROLUNAVAIL;
1129 /* **** */
1130 EnterCriticalSection(&(This->lock));
1132 This->pan = pan;
1134 temp = (double) (This->volume - (This->pan > 0 ? This->pan : 0));
1135 This->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
1136 temp = (double) (This->volume + (This->pan < 0 ? This->pan : 0));
1137 This->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
1139 LeaveCriticalSection(&(This->lock));
1140 /* **** */
1142 return DS_OK;
1145 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
1146 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1148 ICOM_THIS(IDirectSoundBufferImpl,iface);
1149 TRACE("(%p,%p)\n",This,pan);
1151 if (pan == NULL)
1152 return DSERR_INVALIDPARAM;
1154 *pan = This->pan;
1156 return DS_OK;
1159 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
1160 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1162 ICOM_THIS(IDirectSoundBufferImpl,iface);
1163 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
1165 /* There is really nothing to do here. Should someone */
1166 /* choose to implement static buffers in hardware (by */
1167 /* using a wave table synth, for example) this is where */
1168 /* you'd want to do the loading. For software buffers, */
1169 /* which is what we currently use, we need do nothing. */
1171 #if 0
1172 /* It's also the place to pre-process 3D buffers... */
1174 /* This is highly experimental and liable to break things */
1175 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
1176 DSOUND_Create3DBuffer(This);
1177 #endif
1179 return DS_OK;
1182 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
1183 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1185 ICOM_THIS(IDirectSoundBufferImpl,iface);
1186 TRACE("(%p,%p)\n",This,freq);
1188 if (freq == NULL)
1189 return DSERR_INVALIDPARAM;
1191 *freq = This->freq;
1193 return DS_OK;
1196 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
1197 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
1199 ICOM_THIS(IDirectSoundBufferImpl,iface);
1200 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
1201 printf("Re-Init!!!\n");
1202 return DSERR_ALREADYINITIALIZED;
1205 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
1206 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1208 ICOM_THIS(IDirectSoundBufferImpl,iface);
1209 TRACE("(%p)->(%p)\n",This,caps);
1211 if (caps == NULL)
1212 return DSERR_INVALIDPARAM;
1214 /* I think we should check this value, not set it. See */
1215 /* Inside DirectX, p215. That should apply here, too. */
1216 caps->dwSize = sizeof(*caps);
1218 caps->dwFlags = This->dsbd.dwFlags | DSBCAPS_LOCSOFTWARE;
1219 caps->dwBufferBytes = This->dsbd.dwBufferBytes;
1220 /* This value represents the speed of the "unlock" command.
1221 As unlock is quite fast (it does not do anything), I put
1222 4096 ko/s = 4 Mo / s */
1223 caps->dwUnlockTransferRate = 4096;
1224 caps->dwPlayCpuOverhead = 0;
1226 return DS_OK;
1229 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
1230 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1232 ICOM_THIS(IDirectSoundBufferImpl,iface);
1233 char xbuf[50];
1235 WINE_StringFromCLSID(riid,xbuf);
1236 TRACE("(%p,%s,%p)\n",This,xbuf,ppobj);
1238 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1239 IDirectSoundNotifyImpl *dsn;
1241 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1242 dsn->ref = 1;
1243 dsn->dsb = This;
1244 IDirectSoundBuffer_AddRef(iface);
1245 ICOM_VTBL(dsn) = &dsnvt;
1246 *ppobj = (LPVOID)dsn;
1247 return S_OK;
1250 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1251 *ppobj = This->ds3db;
1252 if (*ppobj)
1253 return DS_OK;
1256 return E_FAIL;
1259 static ICOM_VTABLE(IDirectSoundBuffer) dsbvt =
1261 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1262 IDirectSoundBufferImpl_QueryInterface,
1263 IDirectSoundBufferImpl_AddRef,
1264 IDirectSoundBufferImpl_Release,
1265 IDirectSoundBufferImpl_GetCaps,
1266 IDirectSoundBufferImpl_GetCurrentPosition,
1267 IDirectSoundBufferImpl_GetFormat,
1268 IDirectSoundBufferImpl_GetVolume,
1269 IDirectSoundBufferImpl_GetPan,
1270 IDirectSoundBufferImpl_GetFrequency,
1271 IDirectSoundBufferImpl_GetStatus,
1272 IDirectSoundBufferImpl_Initialize,
1273 IDirectSoundBufferImpl_Lock,
1274 IDirectSoundBufferImpl_Play,
1275 IDirectSoundBufferImpl_SetCurrentPosition,
1276 IDirectSoundBufferImpl_SetFormat,
1277 IDirectSoundBufferImpl_SetVolume,
1278 IDirectSoundBufferImpl_SetPan,
1279 IDirectSoundBufferImpl_SetFrequency,
1280 IDirectSoundBufferImpl_Stop,
1281 IDirectSoundBufferImpl_Unlock
1284 /*******************************************************************************
1285 * IDirectSound
1288 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
1289 LPDIRECTSOUND iface,HWND hwnd,DWORD level
1291 ICOM_THIS(IDirectSoundImpl,iface);
1292 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
1293 return DS_OK;
1296 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
1297 LPDIRECTSOUND iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1299 ICOM_THIS(IDirectSoundImpl,iface);
1300 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1301 LPWAVEFORMATEX wfex;
1303 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ippdsb,lpunk);
1305 if ((This == NULL) || (dsbd == NULL) || (ippdsb == NULL))
1306 return DSERR_INVALIDPARAM;
1308 if (TRACE_ON(dsound)) {
1309 TRACE("(size=%ld)\n",dsbd->dwSize);
1310 TRACE("(flags=0x%08lx\n",dsbd->dwFlags);
1311 _dump_DSBCAPS(dsbd->dwFlags);
1312 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1313 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1316 wfex = dsbd->lpwfxFormat;
1318 if (wfex)
1319 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld"
1320 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1321 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1322 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1323 wfex->wBitsPerSample, wfex->cbSize);
1325 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1326 if (primarybuf) {
1327 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
1328 *ippdsb = primarybuf;
1329 primarybuf->dsbd.dwFlags = dsbd->dwFlags;
1330 return DS_OK;
1331 } /* Else create primarybuf */
1334 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1335 if (*ippdsb == NULL)
1336 return DSERR_OUTOFMEMORY;
1337 (*ippdsb)->ref = 1;
1339 TRACE("Created buffer at %p\n", *ippdsb);
1341 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1342 (*ippdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1343 (*ippdsb)->freq = dsound->wfx.nSamplesPerSec;
1344 } else {
1345 (*ippdsb)->buflen = dsbd->dwBufferBytes;
1346 (*ippdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1348 (*ippdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ippdsb)->buflen);
1349 if ((*ippdsb)->buffer == NULL) {
1350 HeapFree(GetProcessHeap(),0,(*ippdsb));
1351 *ippdsb = NULL;
1352 return DSERR_OUTOFMEMORY;
1354 /* It's not necessary to initialize values to zero since */
1355 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1356 (*ippdsb)->playpos = 0;
1357 (*ippdsb)->writepos = 0;
1358 (*ippdsb)->parent = NULL;
1359 ICOM_VTBL(*ippdsb) = &dsbvt;
1360 (*ippdsb)->dsound = This;
1361 (*ippdsb)->playing = 0;
1362 (*ippdsb)->lVolAdjust = (1 << 15);
1363 (*ippdsb)->rVolAdjust = (1 << 15);
1365 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1366 (*ippdsb)->freqAdjust = ((*ippdsb)->freq << DSOUND_FREQSHIFT) /
1367 primarybuf->wfx.nSamplesPerSec;
1368 (*ippdsb)->nAvgBytesPerSec = (*ippdsb)->freq *
1369 dsbd->lpwfxFormat->nBlockAlign;
1372 memcpy(&((*ippdsb)->dsbd),dsbd,sizeof(*dsbd));
1374 /* register buffer */
1375 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1376 This->buffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
1377 This->buffers[This->nrofbuffers] = *ippdsb;
1378 This->nrofbuffers++;
1380 IDirectSound_AddRef(iface);
1382 if (dsbd->lpwfxFormat)
1383 memcpy(&((*ippdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ippdsb)->wfx));
1385 InitializeCriticalSection(&((*ippdsb)->lock));
1387 #if USE_DSOUND3D
1388 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
1389 IDirectSound3DBufferImpl *ds3db;
1391 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1392 0,sizeof(*ds3db));
1393 ds3db->ref = 1;
1394 ds3db->dsb = (*ippdsb);
1395 ICOM_VTBL(ds3db) = &ds3dbvt;
1396 (*ippdsb)->ds3db = ds3db;
1397 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1398 ds3db->ds3db.vPosition.x.x = 0.0;
1399 ds3db->ds3db.vPosition.y.y = 0.0;
1400 ds3db->ds3db.vPosition.z.z = 0.0;
1401 ds3db->ds3db.vVelocity.x.x = 0.0;
1402 ds3db->ds3db.vVelocity.y.y = 0.0;
1403 ds3db->ds3db.vVelocity.z.z = 0.0;
1404 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1405 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1406 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1407 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1408 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1409 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1410 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1411 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1412 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1413 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1414 (*ippdsb)->wfx.nBlockAlign;
1415 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1416 if (ds3db->buffer == NULL) {
1417 ds3db->buflen = 0;
1418 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1421 #endif
1422 return DS_OK;
1425 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
1426 LPDIRECTSOUND iface,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
1428 ICOM_THIS(IDirectSoundImpl,iface);
1429 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
1430 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1431 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
1433 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1435 IDirectSoundBuffer_AddRef(pdsb);
1436 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
1437 (*ippdsb)->ref = 1;
1438 (*ippdsb)->playpos = 0;
1439 (*ippdsb)->writepos = 0;
1440 (*ippdsb)->dsound = This;
1441 (*ippdsb)->parent = ipdsb;
1442 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
1443 /* register buffer */
1444 This->buffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
1445 This->buffers[This->nrofbuffers] = *ippdsb;
1446 This->nrofbuffers++;
1447 IDirectSound_AddRef(iface);
1448 return DS_OK;
1452 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND iface,LPDSCAPS caps) {
1453 ICOM_THIS(IDirectSoundImpl,iface);
1454 TRACE("(%p,%p)\n",This,caps);
1455 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
1457 if (caps == NULL)
1458 return DSERR_INVALIDPARAM;
1460 /* We should check this value, not set it. See Inside DirectX, p215. */
1461 caps->dwSize = sizeof(*caps);
1463 caps->dwFlags =
1464 DSCAPS_PRIMARYSTEREO |
1465 DSCAPS_PRIMARY16BIT |
1466 DSCAPS_SECONDARYSTEREO |
1467 DSCAPS_SECONDARY16BIT |
1468 DSCAPS_CONTINUOUSRATE;
1469 /* FIXME: query OSS */
1470 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1471 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1473 caps->dwPrimaryBuffers = 1;
1475 caps->dwMaxHwMixingAllBuffers = 0;
1476 caps->dwMaxHwMixingStaticBuffers = 0;
1477 caps->dwMaxHwMixingStreamingBuffers = 0;
1479 caps->dwFreeHwMixingAllBuffers = 0;
1480 caps->dwFreeHwMixingStaticBuffers = 0;
1481 caps->dwFreeHwMixingStreamingBuffers = 0;
1483 caps->dwMaxHw3DAllBuffers = 0;
1484 caps->dwMaxHw3DStaticBuffers = 0;
1485 caps->dwMaxHw3DStreamingBuffers = 0;
1487 caps->dwFreeHw3DAllBuffers = 0;
1488 caps->dwFreeHw3DStaticBuffers = 0;
1489 caps->dwFreeHw3DStreamingBuffers = 0;
1491 caps->dwTotalHwMemBytes = 0;
1493 caps->dwFreeHwMemBytes = 0;
1495 caps->dwMaxContigFreeHwMemBytes = 0;
1497 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
1499 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
1501 return DS_OK;
1504 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND iface) {
1505 ICOM_THIS(IDirectSoundImpl,iface);
1506 return ++(This->ref);
1509 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND iface) {
1510 ICOM_THIS(IDirectSoundImpl,iface);
1511 TRACE("(%p), ref was %ld\n",This,This->ref);
1512 if (!--(This->ref)) {
1513 DSOUND_CloseAudio();
1514 while(IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)primarybuf)); /* Deallocate */
1515 FIXME("need to release all buffers!\n");
1516 HeapFree(GetProcessHeap(),0,This);
1517 dsound = NULL;
1518 return S_OK;
1520 return This->ref;
1523 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
1524 LPDIRECTSOUND iface,DWORD config
1526 ICOM_THIS(IDirectSoundImpl,iface);
1527 FIXME("(%p,0x%08lx):stub\n",This,config);
1528 return DS_OK;
1531 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
1532 LPDIRECTSOUND iface,REFIID riid,LPVOID *ppobj
1534 ICOM_THIS(IDirectSoundImpl,iface);
1535 char xbuf[50];
1537 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1539 if (This->listener) {
1540 *ppobj = This->listener;
1541 return DS_OK;
1543 This->listener = (IDirectSound3DListenerImpl*)HeapAlloc(
1544 GetProcessHeap(), 0, sizeof(*(This->listener)));
1545 This->listener->ref = 1;
1546 ICOM_VTBL(This->listener) = &ds3dlvt;
1547 IDirectSound_AddRef(iface);
1548 This->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
1549 This->listener->ds3dl.vPosition.x.x = 0.0;
1550 This->listener->ds3dl.vPosition.y.y = 0.0;
1551 This->listener->ds3dl.vPosition.z.z = 0.0;
1552 This->listener->ds3dl.vVelocity.x.x = 0.0;
1553 This->listener->ds3dl.vVelocity.y.y = 0.0;
1554 This->listener->ds3dl.vVelocity.z.z = 0.0;
1555 This->listener->ds3dl.vOrientFront.x.x = 0.0;
1556 This->listener->ds3dl.vOrientFront.y.y = 0.0;
1557 This->listener->ds3dl.vOrientFront.z.z = 1.0;
1558 This->listener->ds3dl.vOrientTop.x.x = 0.0;
1559 This->listener->ds3dl.vOrientTop.y.y = 1.0;
1560 This->listener->ds3dl.vOrientTop.z.z = 0.0;
1561 This->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1562 This->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1563 This->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1564 *ppobj = (LPVOID)This->listener;
1565 return DS_OK;
1568 WINE_StringFromCLSID(riid,xbuf);
1569 TRACE("(%p,%s,%p)\n",This,xbuf,ppobj);
1570 return E_FAIL;
1573 static HRESULT WINAPI IDirectSoundImpl_Compact(
1574 LPDIRECTSOUND iface)
1576 ICOM_THIS(IDirectSoundImpl,iface);
1577 TRACE("(%p)\n", This);
1578 return DS_OK;
1581 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
1582 LPDIRECTSOUND iface,
1583 LPDWORD lpdwSpeakerConfig)
1585 ICOM_THIS(IDirectSoundImpl,iface);
1586 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
1587 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
1588 return DS_OK;
1591 static HRESULT WINAPI IDirectSoundImpl_Initialize(
1592 LPDIRECTSOUND iface,
1593 LPGUID lpGuid)
1595 ICOM_THIS(IDirectSoundImpl,iface);
1596 TRACE("(%p, %p)\n", This, lpGuid);
1597 return DS_OK;
1600 static ICOM_VTABLE(IDirectSound) dsvt =
1602 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1603 IDirectSoundImpl_QueryInterface,
1604 IDirectSoundImpl_AddRef,
1605 IDirectSoundImpl_Release,
1606 IDirectSoundImpl_CreateSoundBuffer,
1607 IDirectSoundImpl_GetCaps,
1608 IDirectSoundImpl_DuplicateSoundBuffer,
1609 IDirectSoundImpl_SetCooperativeLevel,
1610 IDirectSoundImpl_Compact,
1611 IDirectSoundImpl_GetSpeakerConfig,
1612 IDirectSoundImpl_SetSpeakerConfig,
1613 IDirectSoundImpl_Initialize
1617 /* See http://www.opensound.com/pguide/audio.html for more details */
1619 static int
1620 DSOUND_setformat(LPWAVEFORMATEX wfex) {
1621 int xx,channels,speed,format,nformat;
1623 if (!audioOK) {
1624 TRACE("(%p) deferred\n", wfex);
1625 return 0;
1627 switch (wfex->wFormatTag) {
1628 default:
1629 WARN("unknown WAVE_FORMAT tag %d\n",wfex->wFormatTag);
1630 return DSERR_BADFORMAT;
1631 case WAVE_FORMAT_PCM:
1632 break;
1634 if (wfex->wBitsPerSample==8)
1635 format = AFMT_U8;
1636 else
1637 format = AFMT_S16_LE;
1639 if (-1==ioctl(audiofd,SNDCTL_DSP_GETFMTS,&xx)) {
1640 perror("ioctl SNDCTL_DSP_GETFMTS");
1641 return -1;
1643 if ((xx&format)!=format) {/* format unsupported */
1644 FIXME("SNDCTL_DSP_GETFMTS: format not supported\n");
1645 return -1;
1647 nformat = format;
1648 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFMT,&nformat)) {
1649 perror("ioctl SNDCTL_DSP_SETFMT");
1650 return -1;
1652 if (nformat!=format) {/* didn't work */
1653 FIXME("SNDCTL_DSP_GETFMTS: format not set\n");
1654 return -1;
1657 channels = wfex->nChannels-1;
1658 if (-1==ioctl(audiofd,SNDCTL_DSP_STEREO,&channels)) {
1659 perror("ioctl SNDCTL_DSP_STEREO");
1660 return -1;
1662 speed = wfex->nSamplesPerSec;
1663 if (-1==ioctl(audiofd,SNDCTL_DSP_SPEED,&speed)) {
1664 perror("ioctl SNDCTL_DSP_SPEED");
1665 return -1;
1667 TRACE("(freq=%ld,channels=%d,bits=%d)\n",
1668 wfex->nSamplesPerSec,wfex->nChannels,wfex->wBitsPerSample
1670 return 0;
1673 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
1675 int i;
1676 DWORD offset;
1677 LPDSBPOSITIONNOTIFY event;
1679 if (dsb->nrofnotifies == 0)
1680 return;
1682 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
1683 dsb, dsb->buflen, dsb->playpos, len);
1684 for (i = 0; i < dsb->nrofnotifies ; i++) {
1685 event = dsb->notifies + i;
1686 offset = event->dwOffset;
1687 TRACE("checking %d, position %ld, event = %d\n",
1688 i, offset, event->hEventNotify);
1689 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
1690 /* OK. [Inside DirectX, p274] */
1691 /* */
1692 /* This also means we can't sort the entries by offset, */
1693 /* because DSBPN_OFFSETSTOP == -1 */
1694 if (offset == DSBPN_OFFSETSTOP) {
1695 if (dsb->playing == 0) {
1696 SetEvent(event->hEventNotify);
1697 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1698 return;
1699 } else
1700 return;
1702 if ((dsb->playpos + len) >= dsb->buflen) {
1703 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
1704 (offset >= dsb->playpos)) {
1705 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1706 SetEvent(event->hEventNotify);
1708 } else {
1709 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
1710 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1711 SetEvent(event->hEventNotify);
1717 /* WAV format info can be found at: */
1718 /* */
1719 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
1720 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
1721 /* */
1722 /* Import points to remember: */
1723 /* */
1724 /* 8-bit WAV is unsigned */
1725 /* 16-bit WAV is signed */
1727 static inline INT16 cvtU8toS16(BYTE byte)
1729 INT16 s = (byte - 128) << 8;
1731 return s;
1734 static inline BYTE cvtS16toU8(INT16 word)
1736 BYTE b = (word + 32768) >> 8;
1738 return b;
1742 /* We should be able to optimize these two inline functions */
1743 /* so that we aren't doing 8->16->8 conversions when it is */
1744 /* not necessary. But this is still a WIP. Optimize later. */
1745 static inline void get_fields(const IDirectSoundBufferImpl *dsb, BYTE *buf, INT *fl, INT *fr)
1747 INT16 *bufs = (INT16 *) buf;
1749 /* TRACE(dsound, "(%p)", buf); */
1750 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
1751 *fl = cvtU8toS16(*buf);
1752 *fr = cvtU8toS16(*(buf + 1));
1753 return;
1756 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
1757 *fl = *bufs;
1758 *fr = *(bufs + 1);
1759 return;
1762 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
1763 *fl = cvtU8toS16(*buf);
1764 *fr = *fl;
1765 return;
1768 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
1769 *fl = *bufs;
1770 *fr = *bufs;
1771 return;
1774 FIXME("get_fields found an unsupported configuration\n");
1775 return;
1778 static inline void set_fields(BYTE *buf, INT fl, INT fr)
1780 INT16 *bufs = (INT16 *) buf;
1782 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
1783 *buf = cvtS16toU8(fl);
1784 *(buf + 1) = cvtS16toU8(fr);
1785 return;
1788 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
1789 *bufs = fl;
1790 *(bufs + 1) = fr;
1791 return;
1794 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
1795 *buf = cvtS16toU8((fl + fr) >> 1);
1796 return;
1799 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
1800 *bufs = (fl + fr) >> 1;
1801 return;
1803 FIXME("set_fields found an unsupported configuration\n");
1804 return;
1807 /* Now with PerfectPitch (tm) technology */
1808 static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1810 INT i, size, ipos, ilen, fieldL, fieldR;
1811 BYTE *ibp, *obp;
1812 INT iAdvance = dsb->wfx.nBlockAlign;
1813 INT oAdvance = primarybuf->wfx.nBlockAlign;
1815 ibp = dsb->buffer + dsb->playpos;
1816 obp = buf;
1818 TRACE("(%p, %p, %p), playpos=%8.8lx\n", dsb, ibp, obp, dsb->playpos);
1819 /* Check for the best case */
1820 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
1821 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
1822 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
1823 TRACE("(%p) Best case\n", dsb);
1824 if ((ibp + len) < (BYTE *)(dsb->buffer + dsb->buflen))
1825 memcpy(obp, ibp, len);
1826 else { /* wrap */
1827 memcpy(obp, ibp, dsb->buflen - dsb->playpos);
1828 memcpy(obp + (dsb->buflen - dsb->playpos),
1829 dsb->buffer,
1830 len - (dsb->buflen - dsb->playpos));
1832 return len;
1835 /* Check for same sample rate */
1836 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
1837 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
1838 dsb->freq, primarybuf->wfx.nSamplesPerSec);
1839 ilen = 0;
1840 for (i = 0; i < len; i += oAdvance) {
1841 get_fields(dsb, ibp, &fieldL, &fieldR);
1842 ibp += iAdvance;
1843 ilen += iAdvance;
1844 set_fields(obp, fieldL, fieldR);
1845 obp += oAdvance;
1846 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
1847 ibp = dsb->buffer; /* wrap */
1849 return (ilen);
1852 /* Mix in different sample rates */
1853 /* */
1854 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
1855 /* Patent Pending :-] */
1857 TRACE("(%p) Adjusting frequency: %ld -> %ld\n",
1858 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
1860 size = len / oAdvance;
1861 ilen = ((size * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance;
1862 for (i = 0; i < size; i++) {
1864 ipos = (((i * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance) + dsb->playpos;
1866 if (ipos >= dsb->buflen)
1867 ipos %= dsb->buflen; /* wrap */
1869 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
1870 set_fields(obp, fieldL, fieldR);
1871 obp += oAdvance;
1873 return ilen;
1876 static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1878 INT i, inc = primarybuf->wfx.wBitsPerSample >> 3;
1879 BYTE *bpc = buf;
1880 INT16 *bps = (INT16 *) buf;
1882 TRACE("(%p) left = %lx, right = %lx\n", dsb,
1883 dsb->lVolAdjust, dsb->rVolAdjust);
1884 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->pan == 0)) &&
1885 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volume == 0)) &&
1886 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
1887 return; /* Nothing to do */
1889 /* If we end up with some bozo coder using panning or 3D sound */
1890 /* with a mono primary buffer, it could sound very weird using */
1891 /* this method. Oh well, tough patooties. */
1893 for (i = 0; i < len; i += inc) {
1894 INT val;
1896 switch (inc) {
1898 case 1:
1899 /* 8-bit WAV is unsigned, but we need to operate */
1900 /* on signed data for this to work properly */
1901 val = *bpc - 128;
1902 val = ((val * (i & inc ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1903 *bpc = val + 128;
1904 bpc++;
1905 break;
1906 case 2:
1907 /* 16-bit WAV is signed -- much better */
1908 val = *bps;
1909 val = ((val * ((i & inc) ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1910 *bps = val;
1911 bps++;
1912 break;
1913 default:
1914 /* Very ugly! */
1915 FIXME("MixerVol had a nasty error\n");
1920 #ifdef USE_DSOUND3D
1921 static void DSOUND_Mixer3D(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1923 BYTE *ibp, *obp;
1924 DWORD buflen, playpos;
1926 buflen = dsb->ds3db->buflen;
1927 playpos = (dsb->playpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
1928 ibp = dsb->ds3db->buffer + playpos;
1929 obp = buf;
1931 if (playpos > buflen) {
1932 FIXME("Major breakage");
1933 return;
1936 if (len <= (playpos + buflen))
1937 memcpy(obp, ibp, len);
1938 else { /* wrap */
1939 memcpy(obp, ibp, buflen - playpos);
1940 memcpy(obp + (buflen - playpos),
1941 dsb->buffer,
1942 len - (buflen - playpos));
1944 return;
1946 #endif
1948 static void *tmp_buffer;
1949 static size_t tmp_buffer_len = 0;
1951 static void *DSOUND_tmpbuffer(size_t len)
1953 if (len>tmp_buffer_len) {
1954 void *new_buffer = realloc(tmp_buffer, len);
1955 if (new_buffer) {
1956 tmp_buffer = new_buffer;
1957 tmp_buffer_len = len;
1959 return new_buffer;
1961 return tmp_buffer;
1964 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb)
1966 INT i, len, ilen, temp, field;
1967 INT advance = primarybuf->wfx.wBitsPerSample >> 3;
1968 BYTE *buf, *ibuf, *obuf;
1969 INT16 *ibufs, *obufs;
1971 len = DSOUND_FRAGLEN; /* The most we will use */
1972 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
1973 temp = MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buflen,
1974 dsb->nAvgBytesPerSec) -
1975 MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->playpos,
1976 dsb->nAvgBytesPerSec);
1977 len = (len > temp) ? temp : len;
1979 len &= ~3; /* 4 byte alignment */
1981 if (len == 0) {
1982 /* This should only happen if we aren't looping and temp < 4 */
1984 /* We skip the remainder, so check for possible events */
1985 DSOUND_CheckEvent(dsb, dsb->buflen - dsb->playpos);
1986 /* Stop */
1987 dsb->playing = 0;
1988 dsb->writepos = 0;
1989 dsb->playpos = 0;
1990 /* Check for DSBPN_OFFSETSTOP */
1991 DSOUND_CheckEvent(dsb, 0);
1992 return 0;
1995 /* Been seeing segfaults in malloc() for some reason... */
1996 TRACE("allocating buffer (size = %d)\n", len);
1997 if ((buf = ibuf = (BYTE *) DSOUND_tmpbuffer(len)) == NULL)
1998 return 0;
2000 TRACE("MixInBuffer (%p) len = %d\n", dsb, len);
2002 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
2003 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
2004 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
2005 DSOUND_MixerVol(dsb, ibuf, len);
2007 obuf = primarybuf->buffer + primarybuf->playpos;
2008 for (i = 0; i < len; i += advance) {
2009 obufs = (INT16 *) obuf;
2010 ibufs = (INT16 *) ibuf;
2011 if (primarybuf->wfx.wBitsPerSample == 8) {
2012 /* 8-bit WAV is unsigned */
2013 field = (*ibuf - 128);
2014 field += (*obuf - 128);
2015 field = field > 127 ? 127 : field;
2016 field = field < -128 ? -128 : field;
2017 *obuf = field + 128;
2018 } else {
2019 /* 16-bit WAV is signed */
2020 field = *ibufs;
2021 field += *obufs;
2022 field = field > 32767 ? 32767 : field;
2023 field = field < -32768 ? -32768 : field;
2024 *obufs = field;
2026 ibuf += advance;
2027 obuf += advance;
2028 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
2029 obuf = primarybuf->buffer;
2031 /* free(buf); */
2033 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
2034 DSOUND_CheckEvent(dsb, ilen);
2036 dsb->playpos += ilen;
2037 dsb->writepos = dsb->playpos + ilen;
2039 if (dsb->playpos >= dsb->buflen) {
2040 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2041 dsb->playing = 0;
2042 dsb->writepos = 0;
2043 dsb->playpos = 0;
2044 DSOUND_CheckEvent(dsb, 0); /* For DSBPN_OFFSETSTOP */
2045 } else
2046 dsb->playpos %= dsb->buflen; /* wrap */
2049 if (dsb->writepos >= dsb->buflen)
2050 dsb->writepos %= dsb->buflen;
2052 return len;
2055 static DWORD WINAPI DSOUND_MixPrimary(void)
2057 INT i, len, maxlen = 0;
2058 IDirectSoundBufferImpl *dsb;
2060 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2061 dsb = dsound->buffers[i];
2063 if (!dsb || !(ICOM_VTBL(dsb)))
2064 continue;
2065 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
2066 if (dsb->buflen && dsb->playing) {
2067 EnterCriticalSection(&(dsb->lock));
2068 len = DSOUND_MixInBuffer(dsb);
2069 maxlen = len > maxlen ? len : maxlen;
2070 LeaveCriticalSection(&(dsb->lock));
2072 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)dsb);
2075 return maxlen;
2078 static int DSOUND_OpenAudio(void)
2080 int audioFragment;
2082 if (primarybuf == NULL)
2083 return DSERR_OUTOFMEMORY;
2085 while (audiofd >= 0)
2087 sleep(5);
2088 /* we will most likely not get one, avoid excessive opens ... */
2089 if (audiofd == -ENODEV)
2090 return -1;
2091 audiofd = open("/dev/audio",O_WRONLY);
2092 if (audiofd==-1) {
2093 /* Don't worry if sound is busy at the moment */
2094 if ((errno != EBUSY) && (errno != ENODEV))
2095 perror("open /dev/audio");
2096 audiofd = -errno;
2097 return -1; /* -1 */
2100 /* We should probably do something here if SETFRAGMENT fails... */
2101 audioFragment=0x0002000c;
2102 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFRAGMENT,&audioFragment))
2103 perror("ioctl SETFRAGMENT");
2105 audioOK = 1;
2106 DSOUND_setformat(&(primarybuf->wfx));
2108 return 0;
2111 static void DSOUND_CloseAudio(void)
2113 int neutral;
2115 neutral = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2116 audioOK = 0; /* race condition */
2117 Sleep(5);
2118 /* It's possible we've been called with audio closed */
2119 /* from SetFormat()... this is just to force a call */
2120 /* to OpenAudio() to reset the hardware properly */
2121 if (audiofd >= 0)
2122 close(audiofd);
2123 primarybuf->playpos = 0;
2124 primarybuf->writepos = DSOUND_FRAGLEN;
2125 memset(primarybuf->buffer, neutral, primarybuf->buflen);
2126 audiofd = -1;
2127 TRACE("Audio stopped\n");
2130 static int DSOUND_WriteAudio(char *buf, int len)
2132 int result, left = 0;
2134 while (left < len) {
2135 result = write(audiofd, buf + left, len - left);
2136 if (result == -1) {
2137 if (errno == EINTR)
2138 continue;
2139 else
2140 return result;
2142 left += result;
2144 return 0;
2147 static void DSOUND_OutputPrimary(int len)
2149 int neutral, flen1, flen2;
2150 char *frag1, *frag2;
2152 /* This is a bad place for this. We need to clear the */
2153 /* buffer with a neutral value, for unsigned 8-bit WAVE */
2154 /* that's 128, for signed 16-bit it's 0 */
2155 neutral = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2157 /* **** */
2158 EnterCriticalSection(&(primarybuf->lock));
2160 /* Write out the */
2161 if ((audioOK == 1) || (DSOUND_OpenAudio() == 0)) {
2162 if (primarybuf->playpos + len >= primarybuf->buflen) {
2163 frag1 = primarybuf->buffer + primarybuf->playpos;
2164 flen1 = primarybuf->buflen - primarybuf->playpos;
2165 frag2 = primarybuf->buffer;
2166 flen2 = len - (primarybuf->buflen - primarybuf->playpos);
2167 if (DSOUND_WriteAudio(frag1, flen1) != 0) {
2168 perror("DSOUND_WriteAudio");
2169 LeaveCriticalSection(&(primarybuf->lock));
2170 ExitThread(0);
2172 memset(frag1, neutral, flen1);
2173 if (DSOUND_WriteAudio(frag2, flen2) != 0) {
2174 perror("DSOUND_WriteAudio");
2175 LeaveCriticalSection(&(primarybuf->lock));
2176 ExitThread(0);
2178 memset(frag2, neutral, flen2);
2179 } else {
2180 frag1 = primarybuf->buffer + primarybuf->playpos;
2181 flen1 = len;
2182 if (DSOUND_WriteAudio(frag1, flen1) != 0) {
2183 perror("DSOUND_WriteAudio");
2184 LeaveCriticalSection(&(primarybuf->lock));
2185 ExitThread(0);
2187 memset(frag1, neutral, flen1);
2189 } else {
2190 /* Can't play audio at the moment -- we need to sleep */
2191 /* to make up for the time we'd be blocked in write() */
2192 /* to /dev/audio */
2193 Sleep(60);
2195 primarybuf->playpos += len;
2196 if (primarybuf->playpos >= primarybuf->buflen)
2197 primarybuf->playpos %= primarybuf->buflen;
2198 primarybuf->writepos = primarybuf->playpos + DSOUND_FRAGLEN;
2199 if (primarybuf->writepos >= primarybuf->buflen)
2200 primarybuf->writepos %= primarybuf->buflen;
2202 LeaveCriticalSection(&(primarybuf->lock));
2203 /* **** */
2206 static DWORD WINAPI DSOUND_thread(LPVOID arg)
2208 int len;
2210 TRACE("dsound is at pid %d\n",getpid());
2211 while (1) {
2212 if (!dsound) {
2213 WARN("DSOUND thread giving up.\n");
2214 ExitThread(0);
2216 #if 0
2217 /* EP: since the thread creating this thread can
2218 * die before the end of the DSOUND one, this
2219 * test is of no use
2220 * What shall be tested is whether the DSOUND thread
2221 * is the last one in the process
2223 if (getppid()==1) {
2224 WARN("DSOUND father died? Giving up.\n");
2225 ExitThread(0);
2227 #endif
2228 /* RACE: dsound could be deleted */
2229 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
2230 if (primarybuf == NULL) {
2231 /* Should never happen */
2232 WARN("Lost the primary buffer!\n");
2233 IDirectSound_Release((LPDIRECTSOUND)dsound);
2234 ExitThread(0);
2237 /* **** */
2238 EnterCriticalSection(&(primarybuf->lock));
2239 len = DSOUND_MixPrimary();
2240 LeaveCriticalSection(&(primarybuf->lock));
2241 /* **** */
2243 if (primarybuf->playing)
2244 len = DSOUND_FRAGLEN > len ? DSOUND_FRAGLEN : len;
2245 if (len) {
2246 /* This does all the work */
2247 DSOUND_OutputPrimary(len);
2248 } else {
2249 /* no buffers playing -- close and wait */
2250 if (audioOK)
2251 DSOUND_CloseAudio();
2252 Sleep(100);
2254 IDirectSound_Release((LPDIRECTSOUND)dsound);
2256 ExitThread(0);
2259 #endif /* HAVE_OSS */
2261 HRESULT WINAPI DirectSoundCreate(REFGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
2263 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
2264 if (lpGUID)
2265 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
2266 else
2267 TRACE("DirectSoundCreate (%p)\n", ippDS);
2269 #ifdef HAVE_OSS
2271 if (ippDS == NULL)
2272 return DSERR_INVALIDPARAM;
2274 if (primarybuf) {
2275 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
2276 *ippDS = dsound;
2277 return DS_OK;
2280 /* Check that we actually have audio capabilities */
2281 /* If we do, whether it's busy or not, we continue */
2282 /* otherwise we return with DSERR_NODRIVER */
2284 audiofd = open("/dev/audio",O_WRONLY);
2285 if (audiofd == -1) {
2286 audiofd = -errno;
2287 if (errno == ENODEV) {
2288 MESSAGE("No sound hardware found, but continuing anyway.\n");
2289 } else if (errno == EBUSY) {
2290 MESSAGE("Sound device busy, will keep trying.\n");
2291 } else {
2292 MESSAGE("Unexpected error (%d) while checking for sound support.\n",errno);
2293 return DSERR_GENERIC;
2295 } else {
2296 close(audiofd);
2297 audiofd = -1;
2300 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundImpl));
2301 if (*ippDS == NULL)
2302 return DSERR_OUTOFMEMORY;
2304 (*ippDS)->ref = 1;
2305 ICOM_VTBL(*ippDS) = &dsvt;
2306 (*ippDS)->buffers = NULL;
2307 (*ippDS)->nrofbuffers = 0;
2309 (*ippDS)->wfx.wFormatTag = 1;
2310 (*ippDS)->wfx.nChannels = 2;
2311 (*ippDS)->wfx.nSamplesPerSec = 22050;
2312 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
2313 (*ippDS)->wfx.nBlockAlign = 2;
2314 (*ippDS)->wfx.wBitsPerSample = 8;
2316 if (!dsound) {
2317 HANDLE hnd;
2318 DWORD xid;
2320 dsound = (*ippDS);
2321 if (primarybuf == NULL) {
2322 DSBUFFERDESC dsbd;
2323 HRESULT hr;
2325 dsbd.dwSize = sizeof(DSBUFFERDESC);
2326 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
2327 dsbd.dwBufferBytes = 0;
2328 dsbd.lpwfxFormat = &(dsound->wfx);
2329 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, (LPDIRECTSOUNDBUFFER*)&primarybuf, NULL);
2330 if (hr != DS_OK)
2331 return hr;
2332 dsound->primary = primarybuf;
2334 memset(primarybuf->buffer, 128, primarybuf->buflen);
2335 hnd = CreateThread(NULL,0,DSOUND_thread,0,0,&xid);
2337 return DS_OK;
2338 #else
2339 MessageBoxA(0,"DirectSound needs the Open Sound System Driver, which has not been found by ./configure.","WINE DirectSound",MB_OK|MB_ICONSTOP);
2340 return DSERR_NODRIVER;
2341 #endif
2344 /*******************************************************************************
2345 * DirectSound ClassFactory
2347 typedef struct
2349 /* IUnknown fields */
2350 ICOM_VFIELD(IClassFactory);
2351 DWORD ref;
2352 } IClassFactoryImpl;
2354 static HRESULT WINAPI
2355 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
2356 ICOM_THIS(IClassFactoryImpl,iface);
2357 char buf[80];
2359 if (HIWORD(riid))
2360 WINE_StringFromCLSID(riid,buf);
2361 else
2362 sprintf(buf,"<guid-0x%04x>",LOWORD(riid));
2363 FIXME("(%p)->(%s,%p),stub!\n",This,buf,ppobj);
2364 return E_NOINTERFACE;
2367 static ULONG WINAPI
2368 DSCF_AddRef(LPCLASSFACTORY iface) {
2369 ICOM_THIS(IClassFactoryImpl,iface);
2370 return ++(This->ref);
2373 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
2374 ICOM_THIS(IClassFactoryImpl,iface);
2375 /* static class, won't be freed */
2376 return --(This->ref);
2379 static HRESULT WINAPI DSCF_CreateInstance(
2380 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2382 ICOM_THIS(IClassFactoryImpl,iface);
2383 char buf[80];
2385 WINE_StringFromCLSID(riid,buf);
2386 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,buf,ppobj);
2387 if ( IsEqualGUID( &IID_IDirectSound, riid ) ) {
2388 /* FIXME: reuse already created dsound if present? */
2389 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
2391 return E_NOINTERFACE;
2394 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2395 ICOM_THIS(IClassFactoryImpl,iface);
2396 FIXME("(%p)->(%d),stub!\n",This,dolock);
2397 return S_OK;
2400 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
2401 DSCF_QueryInterface,
2402 DSCF_AddRef,
2403 DSCF_Release,
2404 DSCF_CreateInstance,
2405 DSCF_LockServer
2407 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
2409 /*******************************************************************************
2410 * DllGetClassObject [DSOUND.4]
2411 * Retrieves class object from a DLL object
2413 * NOTES
2414 * Docs say returns STDAPI
2416 * PARAMS
2417 * rclsid [I] CLSID for the class object
2418 * riid [I] Reference to identifier of interface for class object
2419 * ppv [O] Address of variable to receive interface pointer for riid
2421 * RETURNS
2422 * Success: S_OK
2423 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
2424 * E_UNEXPECTED
2426 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
2428 char buf[80],xbuf[80];
2430 if (HIWORD(rclsid))
2431 WINE_StringFromCLSID(rclsid,xbuf);
2432 else
2433 sprintf(xbuf,"<guid-0x%04x>",LOWORD(rclsid));
2434 if (HIWORD(riid))
2435 WINE_StringFromCLSID(riid,buf);
2436 else
2437 sprintf(buf,"<guid-0x%04x>",LOWORD(riid));
2438 WINE_StringFromCLSID(riid,xbuf);
2439 TRACE("(%p,%p,%p)\n", xbuf, buf, ppv);
2440 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
2441 *ppv = (LPVOID)&DSOUND_CF;
2442 IClassFactory_AddRef((IClassFactory*)*ppv);
2443 return S_OK;
2446 FIXME("(%p,%p,%p): no interface found.\n", xbuf, buf, ppv);
2447 return CLASS_E_CLASSNOTAVAILABLE;
2451 /*******************************************************************************
2452 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
2454 * RETURNS
2455 * Success: S_OK
2456 * Failure: S_FALSE
2458 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
2460 FIXME("(void): stub\n");
2461 return S_FALSE;