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_Initialize(
365 DirectSoundCaptureDevice ** ppDevice,
367 ULONG DirectSoundCaptureDevice_Release(
368 DirectSoundCaptureDevice * device);
370 /*****************************************************************************
371 * IDirectSoundCaptureBuffer implementation structure
373 struct IDirectSoundCaptureBufferImpl
375 /* IUnknown fields */
376 const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
379 /* IDirectSoundCaptureBufferImpl fields */
380 DirectSoundCaptureDevice* device;
381 /* FIXME: don't need this */
382 LPDSCBUFFERDESC pdscbd;
385 /* IDirectSoundCaptureNotifyImpl fields */
386 IDirectSoundCaptureNotifyImpl* notify;
387 LPDSBPOSITIONNOTIFY notifies;
389 PIDSDRIVERNOTIFY hwnotify;
392 HRESULT IDirectSoundCaptureBufferImpl_Create(
393 DirectSoundCaptureDevice *device,
394 IDirectSoundCaptureBufferImpl ** ppobj,
395 LPCDSCBUFFERDESC lpcDSCBufferDesc);
397 /*****************************************************************************
398 * IDirectSoundFullDuplex implementation structure
400 struct IDirectSoundFullDuplexImpl
402 /* IUnknown fields */
403 const IDirectSoundFullDuplexVtbl *lpVtbl;
406 /* IDirectSoundFullDuplexImpl fields */
407 DirectSoundDevice *renderer_device;
408 DirectSoundCaptureDevice *capture_device;
413 LPDIRECTSOUNDCAPTURE pDSC;
416 /*****************************************************************************
417 * IDirectSoundFullDuplex COM components
419 struct IDirectSoundFullDuplex_IUnknown {
420 const IUnknownVtbl *lpVtbl;
422 IDirectSoundFullDuplexImpl *pdsfd;
425 struct IDirectSoundFullDuplex_IDirectSound {
426 const IDirectSoundVtbl *lpVtbl;
428 IDirectSoundFullDuplexImpl *pdsfd;
431 struct IDirectSoundFullDuplex_IDirectSound8 {
432 const IDirectSound8Vtbl *lpVtbl;
434 IDirectSoundFullDuplexImpl *pdsfd;
437 struct IDirectSoundFullDuplex_IDirectSoundCapture {
438 const IDirectSoundCaptureVtbl *lpVtbl;
440 IDirectSoundFullDuplexImpl *pdsfd;
443 /*****************************************************************************
444 * IDirectSoundNotify implementation structure
446 struct IDirectSoundNotifyImpl
448 /* IUnknown fields */
449 const IDirectSoundNotifyVtbl *lpVtbl;
451 IDirectSoundBufferImpl* dsb;
454 HRESULT IDirectSoundNotifyImpl_Create(
455 IDirectSoundBufferImpl *dsb,
456 IDirectSoundNotifyImpl **pdsn);
457 HRESULT IDirectSoundNotifyImpl_Destroy(
458 IDirectSoundNotifyImpl *pdsn);
460 /*****************************************************************************
461 * IDirectSoundCaptureNotify implementation structure
463 struct IDirectSoundCaptureNotifyImpl
465 /* IUnknown fields */
466 const IDirectSoundNotifyVtbl *lpVtbl;
468 IDirectSoundCaptureBufferImpl* dscb;
471 HRESULT IDirectSoundCaptureNotifyImpl_Create(
472 IDirectSoundCaptureBufferImpl *dscb,
473 IDirectSoundCaptureNotifyImpl ** pdscn);
475 /*****************************************************************************
476 * IDirectSound3DListener implementation structure
478 struct IDirectSound3DListenerImpl
480 /* IUnknown fields */
481 const IDirectSound3DListenerVtbl *lpVtbl;
483 /* IDirectSound3DListenerImpl fields */
484 DirectSoundDevice* device;
487 HRESULT IDirectSound3DListenerImpl_Create(
488 DirectSoundDevice *device,
489 IDirectSound3DListenerImpl **pdsl);
491 /*****************************************************************************
492 * IKsBufferPropertySet implementation structure
494 struct IKsBufferPropertySetImpl
496 /* IUnknown fields */
497 const IKsPropertySetVtbl *lpVtbl;
499 /* IKsPropertySetImpl fields */
500 IDirectSoundBufferImpl* dsb;
503 HRESULT IKsBufferPropertySetImpl_Create(
504 IDirectSoundBufferImpl *dsb,
505 IKsBufferPropertySetImpl **piks);
506 HRESULT IKsBufferPropertySetImpl_Destroy(
507 IKsBufferPropertySetImpl *piks);
509 /*****************************************************************************
510 * IKsPrivatePropertySet implementation structure
512 struct IKsPrivatePropertySetImpl
514 /* IUnknown fields */
515 const IKsPropertySetVtbl *lpVtbl;
519 HRESULT IKsPrivatePropertySetImpl_Create(
520 IKsPrivatePropertySetImpl **piks);
522 /*****************************************************************************
523 * IDirectSound3DBuffer implementation structure
525 struct IDirectSound3DBufferImpl
527 /* IUnknown fields */
528 const IDirectSound3DBufferVtbl *lpVtbl;
530 /* IDirectSound3DBufferImpl fields */
531 IDirectSoundBufferImpl* dsb;
534 HRESULT IDirectSound3DBufferImpl_Create(
535 IDirectSoundBufferImpl *dsb,
536 IDirectSound3DBufferImpl **pds3db);
537 HRESULT IDirectSound3DBufferImpl_Destroy(
538 IDirectSound3DBufferImpl *pds3db);
540 /*******************************************************************************
541 * DirectSound ClassFactory implementation structure
543 struct IClassFactoryImpl
545 /* IUnknown fields */
546 const IClassFactoryVtbl *lpVtbl;
550 extern IClassFactoryImpl DSOUND_CAPTURE_CF;
551 extern IClassFactoryImpl DSOUND_FULLDUPLEX_CF;
553 /*******************************************************************************
558 HRESULT DSOUND_Create(LPDIRECTSOUND *ppDS, IUnknown *pUnkOuter);
559 HRESULT DSOUND_Create8(LPDIRECTSOUND8 *ppDS, IUnknown *pUnkOuter);
563 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
564 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
565 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
566 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
567 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
568 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex);
572 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This, DWORD pplay, DWORD pwrite);
576 void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
577 void DSOUND_ForceRemix(IDirectSoundBufferImpl *dsb);
578 void DSOUND_MixCancelAt(IDirectSoundBufferImpl *dsb, DWORD buf_writepos);
579 void DSOUND_WaveQueue(DirectSoundDevice *device, DWORD mixq);
580 void DSOUND_PerformMix(DirectSoundDevice *device);
581 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
582 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
583 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
584 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
585 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
589 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);
593 HRESULT DSOUND_FullDuplexCreate(LPDIRECTSOUNDFULLDUPLEX* ppDSFD, IUnknown *pUnkOuter);
597 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
598 LPDIRECTSOUNDCAPTURE iface,
599 LPCDSCBUFFERDESC lpcDSCBufferDesc,
600 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
602 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
603 LPDIRECTSOUNDCAPTURE iface,
604 LPDSCCAPS lpDSCCaps);
605 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
606 LPDIRECTSOUNDCAPTURE iface,
609 #define STATE_STOPPED 0
610 #define STATE_STARTING 1
611 #define STATE_PLAYING 2
612 #define STATE_CAPTURING 2
613 #define STATE_STOPPING 3
615 #define DSOUND_FREQSHIFT (14)
617 extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
618 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
620 extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
621 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
623 HRESULT mmErr(UINT err);
624 void setup_dsound_options(void);
625 const char * get_device_id(LPCGUID pGuid);
626 const char * dumpCooperativeLevel(DWORD level);