3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 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 /* Linux does not support better timing than 10ms */
23 #define DS_TIME_RES 10 /* Resolution of multimedia timer */
24 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
26 #define DS_HEL_FRAGS 48 /* HEL only: number of waveOut fragments in primary buffer
27 * (changing this won't help you) */
29 /* direct sound hardware acceleration levels */
30 #define DS_HW_ACCEL_FULL 0 /* default on Windows 98 */
31 #define DS_HW_ACCEL_STANDARD 1 /* default on Windows 2000 */
32 #define DS_HW_ACCEL_BASIC 2
33 #define DS_HW_ACCEL_EMULATION 3
35 extern int ds_emuldriver;
36 extern int ds_hel_margin;
37 extern int ds_hel_queue;
38 extern int ds_snd_queue_max;
39 extern int ds_snd_queue_min;
40 extern int ds_hw_accel;
41 extern int ds_default_playback;
42 extern int ds_default_capture;
43 extern int ds_default_sample_rate;
44 extern int ds_default_bits_per_sample;
46 /*****************************************************************************
47 * Predeclare the interface implementation structures
49 typedef struct IDirectSoundImpl IDirectSoundImpl;
50 typedef struct IDirectSound_IUnknown IDirectSound_IUnknown;
51 typedef struct IDirectSound_IDirectSound IDirectSound_IDirectSound;
52 typedef struct IDirectSound8_IUnknown IDirectSound8_IUnknown;
53 typedef struct IDirectSound8_IDirectSound IDirectSound8_IDirectSound;
54 typedef struct IDirectSound8_IDirectSound8 IDirectSound8_IDirectSound8;
55 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
56 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
57 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
58 typedef struct IDirectSoundFullDuplexImpl IDirectSoundFullDuplexImpl;
59 typedef struct IDirectSoundFullDuplex_IUnknown IDirectSoundFullDuplex_IUnknown;
60 typedef struct IDirectSoundFullDuplex_IDirectSound IDirectSoundFullDuplex_IDirectSound;
61 typedef struct IDirectSoundFullDuplex_IDirectSound8 IDirectSoundFullDuplex_IDirectSound8;
62 typedef struct IDirectSoundFullDuplex_IDirectSoundCapture IDirectSoundFullDuplex_IDirectSoundCapture;
63 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
64 typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
65 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
66 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
67 typedef struct IKsBufferPropertySetImpl IKsBufferPropertySetImpl;
68 typedef struct IKsPrivatePropertySetImpl IKsPrivatePropertySetImpl;
69 typedef struct PrimaryBufferImpl PrimaryBufferImpl;
70 typedef struct SecondaryBufferImpl SecondaryBufferImpl;
71 typedef struct IClassFactoryImpl IClassFactoryImpl;
72 typedef struct DirectSoundDevice DirectSoundDevice;
73 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
75 /*****************************************************************************
76 * IDirectSound implementation structure
78 struct IDirectSoundImpl
82 DirectSoundDevice *device;
88 HRESULT IDirectSoundImpl_Create(
89 LPDIRECTSOUND8 * ppds);
91 /*****************************************************************************
92 * IDirectSoundDevice implementation structure
94 struct DirectSoundDevice
100 DSDRIVERDESC drvdesc;
101 DSDRIVERCAPS drvcaps;
105 LPWAVEHDR pwave[DS_HEL_FRAGS];
106 UINT timerID, pwplay, pwwrite, pwqueue, prebuf, precount;
108 PIDSDRIVERBUFFER hwbuf;
110 DWORD writelead, buflen, state, playpos, mixpos;
113 IDirectSoundBufferImpl** buffers;
114 RTL_RWLOCK buffer_list_lock;
115 CRITICAL_SECTION mixlock;
116 PrimaryBufferImpl* primary;
118 DWORD speaker_config;
120 DWORD tmp_buffer_len;
122 /* DirectSound3DListener fields */
123 IDirectSound3DListenerImpl* listener;
125 BOOL ds3dl_need_recalc;
128 /* reference counted buffer memory for duplicated buffer memory */
129 typedef struct BufferMemory
135 HRESULT DirectSoundDevice_Create(DirectSoundDevice ** ppDevice);
136 ULONG DirectSoundDevice_AddRef(DirectSoundDevice * device);
137 ULONG DirectSoundDevice_Release(DirectSoundDevice * device);
138 HRESULT DirectSoundDevice_Initialize(
139 DirectSoundDevice ** ppDevice,
141 HRESULT DirectSoundDevice_AddBuffer(
142 DirectSoundDevice * device,
143 IDirectSoundBufferImpl * pDSB);
144 HRESULT DirectSoundDevice_RemoveBuffer(
145 DirectSoundDevice * device,
146 IDirectSoundBufferImpl * pDSB);
147 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps);
148 HRESULT DirectSoundDevice_CreateSoundBuffer(
149 DirectSoundDevice * device,
150 LPCDSBUFFERDESC dsbd,
151 LPLPDIRECTSOUNDBUFFER ppdsb,
154 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
155 DirectSoundDevice * device,
156 LPDIRECTSOUNDBUFFER psb,
157 LPLPDIRECTSOUNDBUFFER ppdsb);
158 HRESULT DirectSoundDevice_SetCooperativeLevel(
159 DirectSoundDevice * devcie,
162 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device);
163 HRESULT DirectSoundDevice_GetSpeakerConfig(
164 DirectSoundDevice * device,
165 LPDWORD lpdwSpeakerConfig);
166 HRESULT DirectSoundDevice_SetSpeakerConfig(
167 DirectSoundDevice * device,
169 HRESULT DirectSoundDevice_VerifyCertification(
170 DirectSoundDevice * device,
171 LPDWORD pdwCertified);
173 /*****************************************************************************
174 * IDirectSound COM components
176 struct IDirectSound_IUnknown {
177 const IUnknownVtbl *lpVtbl;
182 HRESULT IDirectSound_IUnknown_Create(
186 struct IDirectSound_IDirectSound {
187 const IDirectSoundVtbl *lpVtbl;
192 HRESULT IDirectSound_IDirectSound_Create(
194 LPDIRECTSOUND * ppds);
196 /*****************************************************************************
197 * IDirectSound8 COM components
199 struct IDirectSound8_IUnknown {
200 const IUnknownVtbl *lpVtbl;
205 HRESULT IDirectSound8_IUnknown_Create(
209 struct IDirectSound8_IDirectSound {
210 const IDirectSoundVtbl *lpVtbl;
215 HRESULT IDirectSound8_IDirectSound_Create(
217 LPDIRECTSOUND * ppds);
219 struct IDirectSound8_IDirectSound8 {
220 const IDirectSound8Vtbl *lpVtbl;
225 HRESULT IDirectSound8_IDirectSound8_Create(
227 LPDIRECTSOUND8 * ppds);
229 /*****************************************************************************
230 * IDirectSoundBuffer implementation structure
232 struct IDirectSoundBufferImpl
234 /* FIXME: document */
235 /* IUnknown fields */
236 const IDirectSoundBuffer8Vtbl *lpVtbl;
238 /* IDirectSoundBufferImpl fields */
239 SecondaryBufferImpl* secondary;
240 DirectSoundDevice* device;
241 CRITICAL_SECTION lock;
242 PIDSDRIVERBUFFER hwbuf;
244 BufferMemory* buffer;
245 DWORD playflags,state,leadin;
246 DWORD playpos,startpos,writelead,buflen;
247 DWORD nAvgBytesPerSec;
249 DSVOLUMEPAN volpan, cvolpan;
251 /* used for frequency conversion (PerfectPitch) */
252 ULONG freqAdjust, freqAcc;
253 /* used for intelligent (well, sort of) prebuffering */
254 DWORD probably_valid_to, last_playpos;
255 DWORD primary_mixpos, buf_mixpos;
258 /* IDirectSoundNotifyImpl fields */
259 IDirectSoundNotifyImpl* notify;
260 LPDSBPOSITIONNOTIFY notifies;
262 PIDSDRIVERNOTIFY hwnotify;
264 /* DirectSound3DBuffer fields */
265 IDirectSound3DBufferImpl* ds3db;
266 DS3DBUFFER ds3db_ds3db;
268 BOOL ds3db_need_recalc;
270 /* IKsPropertySet fields */
271 IKsBufferPropertySetImpl* iks;
274 HRESULT IDirectSoundBufferImpl_Create(
275 DirectSoundDevice *device,
276 IDirectSoundBufferImpl **ppdsb,
277 LPCDSBUFFERDESC dsbd);
278 HRESULT IDirectSoundBufferImpl_Destroy(
279 IDirectSoundBufferImpl *pdsb);
280 HRESULT IDirectSoundBufferImpl_Duplicate(
281 DirectSoundDevice *device,
282 IDirectSoundBufferImpl **ppdsb,
283 IDirectSoundBufferImpl *pdsb);
285 /*****************************************************************************
286 * SecondaryBuffer implementation structure
288 struct SecondaryBufferImpl
290 const IDirectSoundBuffer8Vtbl *lpVtbl;
292 IDirectSoundBufferImpl* dsb;
295 HRESULT SecondaryBufferImpl_Create(
296 IDirectSoundBufferImpl *dsb,
297 SecondaryBufferImpl **pdsb);
298 HRESULT SecondaryBufferImpl_Destroy(
299 SecondaryBufferImpl *pdsb);
301 /*****************************************************************************
302 * PrimaryBuffer implementation structure
304 struct PrimaryBufferImpl
306 const IDirectSoundBuffer8Vtbl *lpVtbl;
308 DirectSoundDevice* device;
311 HRESULT PrimaryBufferImpl_Create(
312 DirectSoundDevice * device,
313 PrimaryBufferImpl **ppdsb,
314 LPCDSBUFFERDESC dsbd);
316 /*****************************************************************************
317 * IDirectSoundCapture implementation structure
319 struct IDirectSoundCaptureImpl
321 /* IUnknown fields */
322 const IDirectSoundCaptureVtbl *lpVtbl;
325 DirectSoundCaptureDevice *device;
328 HRESULT IDirectSoundCaptureImpl_Create(
329 LPDIRECTSOUNDCAPTURE8 * ppds);
331 /*****************************************************************************
332 * DirectSoundCaptureDevice implementation structure
334 struct DirectSoundCaptureDevice
336 /* IDirectSoundCaptureImpl fields */
340 /* DirectSound driver stuff */
342 DSDRIVERDESC drvdesc;
343 DSCDRIVERCAPS drvcaps;
344 PIDSCDRIVERBUFFER hwbuf;
346 /* wave driver info */
356 IDirectSoundCaptureBufferImpl* capture_buffer;
361 CRITICAL_SECTION lock;
364 HRESULT DirectSoundCaptureDevice_Create(
365 DirectSoundCaptureDevice ** ppDevice);
366 HRESULT DirectSoundCaptureDevice_Initialize(
367 DirectSoundCaptureDevice ** ppDevice,
369 ULONG DirectSoundCaptureDevice_AddRef(
370 DirectSoundCaptureDevice * device);
371 ULONG DirectSoundCaptureDevice_Release(
372 DirectSoundCaptureDevice * device);
374 /*****************************************************************************
375 * IDirectSoundCaptureBuffer implementation structure
377 struct IDirectSoundCaptureBufferImpl
379 /* IUnknown fields */
380 const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
383 /* IDirectSoundCaptureBufferImpl fields */
384 DirectSoundCaptureDevice* device;
385 /* FIXME: don't need this */
386 LPDSCBUFFERDESC pdscbd;
389 /* IDirectSoundCaptureNotifyImpl fields */
390 IDirectSoundCaptureNotifyImpl* notify;
391 LPDSBPOSITIONNOTIFY notifies;
393 PIDSDRIVERNOTIFY hwnotify;
396 HRESULT IDirectSoundCaptureBufferImpl_Create(
397 DirectSoundCaptureDevice *device,
398 IDirectSoundCaptureBufferImpl ** ppobj,
399 LPCDSCBUFFERDESC lpcDSCBufferDesc);
401 /*****************************************************************************
402 * IDirectSoundFullDuplex implementation structure
404 struct IDirectSoundFullDuplexImpl
406 /* IUnknown fields */
407 const IDirectSoundFullDuplexVtbl *lpVtbl;
410 /* IDirectSoundFullDuplexImpl fields */
411 DirectSoundDevice *renderer_device;
412 DirectSoundCaptureDevice *capture_device;
417 LPDIRECTSOUNDCAPTURE pDSC;
420 /*****************************************************************************
421 * IDirectSoundFullDuplex COM components
423 struct IDirectSoundFullDuplex_IUnknown {
424 const IUnknownVtbl *lpVtbl;
426 IDirectSoundFullDuplexImpl *pdsfd;
429 HRESULT IDirectSoundFullDuplex_IUnknown_Create(
430 LPDIRECTSOUNDFULLDUPLEX pdsfd,
433 struct IDirectSoundFullDuplex_IDirectSound {
434 const IDirectSoundVtbl *lpVtbl;
436 IDirectSoundFullDuplexImpl *pdsfd;
439 HRESULT IDirectSoundFullDuplex_IDirectSound_Create(
440 LPDIRECTSOUNDFULLDUPLEX pdsfd,
441 LPDIRECTSOUND * ppds);
443 struct IDirectSoundFullDuplex_IDirectSound8 {
444 const IDirectSound8Vtbl *lpVtbl;
446 IDirectSoundFullDuplexImpl *pdsfd;
449 HRESULT IDirectSoundFullDuplex_IDirectSound8_Create(
450 LPDIRECTSOUNDFULLDUPLEX pdsfd,
451 LPDIRECTSOUND8 * ppds8);
453 struct IDirectSoundFullDuplex_IDirectSoundCapture {
454 const IDirectSoundCaptureVtbl *lpVtbl;
456 IDirectSoundFullDuplexImpl *pdsfd;
459 HRESULT IDirectSoundFullDuplex_IDirectSoundCapture_Create(
460 LPDIRECTSOUNDFULLDUPLEX pdsfd,
461 LPDIRECTSOUNDCAPTURE8 * ppdsc8);
463 /*****************************************************************************
464 * IDirectSoundNotify implementation structure
466 struct IDirectSoundNotifyImpl
468 /* IUnknown fields */
469 const IDirectSoundNotifyVtbl *lpVtbl;
471 IDirectSoundBufferImpl* dsb;
474 HRESULT IDirectSoundNotifyImpl_Create(
475 IDirectSoundBufferImpl *dsb,
476 IDirectSoundNotifyImpl **pdsn);
477 HRESULT IDirectSoundNotifyImpl_Destroy(
478 IDirectSoundNotifyImpl *pdsn);
480 /*****************************************************************************
481 * IDirectSoundCaptureNotify implementation structure
483 struct IDirectSoundCaptureNotifyImpl
485 /* IUnknown fields */
486 const IDirectSoundNotifyVtbl *lpVtbl;
488 IDirectSoundCaptureBufferImpl* dscb;
491 HRESULT IDirectSoundCaptureNotifyImpl_Create(
492 IDirectSoundCaptureBufferImpl *dscb,
493 IDirectSoundCaptureNotifyImpl ** pdscn);
495 /*****************************************************************************
496 * IDirectSound3DListener implementation structure
498 struct IDirectSound3DListenerImpl
500 /* IUnknown fields */
501 const IDirectSound3DListenerVtbl *lpVtbl;
503 /* IDirectSound3DListenerImpl fields */
504 DirectSoundDevice* device;
507 HRESULT IDirectSound3DListenerImpl_Create(
508 DirectSoundDevice *device,
509 IDirectSound3DListenerImpl **pdsl);
511 /*****************************************************************************
512 * IKsBufferPropertySet implementation structure
514 struct IKsBufferPropertySetImpl
516 /* IUnknown fields */
517 const IKsPropertySetVtbl *lpVtbl;
519 /* IKsPropertySetImpl fields */
520 IDirectSoundBufferImpl* dsb;
523 HRESULT IKsBufferPropertySetImpl_Create(
524 IDirectSoundBufferImpl *dsb,
525 IKsBufferPropertySetImpl **piks);
526 HRESULT IKsBufferPropertySetImpl_Destroy(
527 IKsBufferPropertySetImpl *piks);
529 /*****************************************************************************
530 * IKsPrivatePropertySet implementation structure
532 struct IKsPrivatePropertySetImpl
534 /* IUnknown fields */
535 const IKsPropertySetVtbl *lpVtbl;
539 HRESULT IKsPrivatePropertySetImpl_Create(
540 IKsPrivatePropertySetImpl **piks);
542 /*****************************************************************************
543 * IDirectSound3DBuffer implementation structure
545 struct IDirectSound3DBufferImpl
547 /* IUnknown fields */
548 const IDirectSound3DBufferVtbl *lpVtbl;
550 /* IDirectSound3DBufferImpl fields */
551 IDirectSoundBufferImpl* dsb;
554 HRESULT IDirectSound3DBufferImpl_Create(
555 IDirectSoundBufferImpl *dsb,
556 IDirectSound3DBufferImpl **pds3db);
557 HRESULT IDirectSound3DBufferImpl_Destroy(
558 IDirectSound3DBufferImpl *pds3db);
560 /*******************************************************************************
561 * DirectSound ClassFactory implementation structure
563 struct IClassFactoryImpl
565 /* IUnknown fields */
566 const IClassFactoryVtbl *lpVtbl;
570 extern IClassFactoryImpl DSOUND_CAPTURE_CF;
571 extern IClassFactoryImpl DSOUND_FULLDUPLEX_CF;
573 /*******************************************************************************
578 HRESULT DSOUND_Create(LPDIRECTSOUND *ppDS, IUnknown *pUnkOuter);
579 HRESULT DSOUND_Create8(LPDIRECTSOUND8 *ppDS, IUnknown *pUnkOuter);
583 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
584 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
585 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
586 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
587 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
588 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex);
592 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This, DWORD pplay, DWORD pwrite);
596 void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
597 void DSOUND_ForceRemix(IDirectSoundBufferImpl *dsb);
598 void DSOUND_MixCancelAt(IDirectSoundBufferImpl *dsb, DWORD buf_writepos);
599 void DSOUND_WaveQueue(DirectSoundDevice *device, DWORD mixq);
600 void DSOUND_PerformMix(DirectSoundDevice *device);
601 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
602 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
603 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
604 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
605 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
609 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);
613 HRESULT DSOUND_FullDuplexCreate(LPDIRECTSOUNDFULLDUPLEX* ppDSFD, IUnknown *pUnkOuter);
617 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
618 LPDIRECTSOUNDCAPTURE iface,
619 LPCDSCBUFFERDESC lpcDSCBufferDesc,
620 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
622 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
623 LPDIRECTSOUNDCAPTURE iface,
624 LPDSCCAPS lpDSCCaps);
625 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
626 LPDIRECTSOUNDCAPTURE iface,
629 #define STATE_STOPPED 0
630 #define STATE_STARTING 1
631 #define STATE_PLAYING 2
632 #define STATE_CAPTURING 2
633 #define STATE_STOPPING 3
635 #define DSOUND_FREQSHIFT (14)
637 extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
638 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
640 extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
641 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
643 HRESULT mmErr(UINT err);
644 void setup_dsound_options(void);
645 const char * get_device_id(LPCGUID pGuid);
646 const char * dumpCooperativeLevel(DWORD level);