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 2 /* Resolution of multimedia timer */
24 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
26 #include "wine/list.h"
28 /* direct sound hardware acceleration levels */
29 #define DS_HW_ACCEL_FULL 0 /* default on Windows 98 */
30 #define DS_HW_ACCEL_STANDARD 1 /* default on Windows 2000 */
31 #define DS_HW_ACCEL_BASIC 2
32 #define DS_HW_ACCEL_EMULATION 3
34 extern int ds_emuldriver;
35 extern int ds_hel_buflen;
36 extern int ds_snd_queue_max;
37 extern int ds_snd_queue_min;
38 extern int ds_hw_accel;
39 extern int ds_default_playback;
40 extern int ds_default_capture;
41 extern int ds_default_sample_rate;
42 extern int ds_default_bits_per_sample;
44 /*****************************************************************************
45 * Predeclare the interface implementation structures
47 typedef struct IDirectSoundImpl IDirectSoundImpl;
48 typedef struct IDirectSound_IUnknown IDirectSound_IUnknown;
49 typedef struct IDirectSound_IDirectSound IDirectSound_IDirectSound;
50 typedef struct IDirectSound8_IUnknown IDirectSound8_IUnknown;
51 typedef struct IDirectSound8_IDirectSound IDirectSound8_IDirectSound;
52 typedef struct IDirectSound8_IDirectSound8 IDirectSound8_IDirectSound8;
53 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
54 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
55 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
56 typedef struct IDirectSoundFullDuplexImpl IDirectSoundFullDuplexImpl;
57 typedef struct IDirectSoundFullDuplex_IUnknown IDirectSoundFullDuplex_IUnknown;
58 typedef struct IDirectSoundFullDuplex_IDirectSound IDirectSoundFullDuplex_IDirectSound;
59 typedef struct IDirectSoundFullDuplex_IDirectSound8 IDirectSoundFullDuplex_IDirectSound8;
60 typedef struct IDirectSoundFullDuplex_IDirectSoundCapture IDirectSoundFullDuplex_IDirectSoundCapture;
61 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
62 typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
63 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
64 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
65 typedef struct IKsBufferPropertySetImpl IKsBufferPropertySetImpl;
66 typedef struct IKsPrivatePropertySetImpl IKsPrivatePropertySetImpl;
67 typedef struct PrimaryBufferImpl PrimaryBufferImpl;
68 typedef struct SecondaryBufferImpl SecondaryBufferImpl;
69 typedef struct DirectSoundDevice DirectSoundDevice;
70 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
72 /*****************************************************************************
73 * IDirectSoundDevice implementation structure
75 struct DirectSoundDevice
87 UINT timerID, pwplay, pwqueue, prebuf, helfrags;
89 PIDSDRIVERBUFFER hwbuf;
91 DWORD writelead, buflen, state, playpos, mixpos;
93 IDirectSoundBufferImpl** buffers;
94 RTL_RWLOCK buffer_list_lock;
95 CRITICAL_SECTION mixlock;
96 PrimaryBufferImpl* primary;
100 DWORD tmp_buffer_len;
102 /* DirectSound3DListener fields */
103 IDirectSound3DListenerImpl* listener;
105 BOOL ds3dl_need_recalc;
108 /* reference counted buffer memory for duplicated buffer memory */
109 typedef struct BufferMemory
116 ULONG DirectSoundDevice_Release(DirectSoundDevice * device);
117 HRESULT DirectSoundDevice_Initialize(
118 DirectSoundDevice ** ppDevice,
120 HRESULT DirectSoundDevice_AddBuffer(
121 DirectSoundDevice * device,
122 IDirectSoundBufferImpl * pDSB);
123 HRESULT DirectSoundDevice_RemoveBuffer(
124 DirectSoundDevice * device,
125 IDirectSoundBufferImpl * pDSB);
126 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps);
127 HRESULT DirectSoundDevice_CreateSoundBuffer(
128 DirectSoundDevice * device,
129 LPCDSBUFFERDESC dsbd,
130 LPLPDIRECTSOUNDBUFFER ppdsb,
133 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
134 DirectSoundDevice * device,
135 LPDIRECTSOUNDBUFFER psb,
136 LPLPDIRECTSOUNDBUFFER ppdsb);
137 HRESULT DirectSoundDevice_SetCooperativeLevel(
138 DirectSoundDevice * devcie,
141 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device);
142 HRESULT DirectSoundDevice_GetSpeakerConfig(
143 DirectSoundDevice * device,
144 LPDWORD lpdwSpeakerConfig);
145 HRESULT DirectSoundDevice_SetSpeakerConfig(
146 DirectSoundDevice * device,
149 /*****************************************************************************
150 * IDirectSoundBuffer implementation structure
152 struct IDirectSoundBufferImpl
154 /* FIXME: document */
155 /* IUnknown fields */
156 const IDirectSoundBuffer8Vtbl *lpVtbl;
158 /* IDirectSoundBufferImpl fields */
159 SecondaryBufferImpl* secondary;
160 DirectSoundDevice* device;
162 PIDSDRIVERBUFFER hwbuf;
164 BufferMemory* buffer;
166 DWORD playflags,state,leadin;
167 DWORD writelead,buflen;
168 DWORD nAvgBytesPerSec;
169 DWORD freq, tmp_buffer_len, max_buffer_len;
172 /* used for frequency conversion (PerfectPitch) */
173 ULONG freqneeded, freqAdjust, freqAcc, freqAccNext;
174 /* used for mixing */
175 DWORD primary_mixpos, buf_mixpos, sec_mixpos;
177 /* IDirectSoundNotifyImpl fields */
178 IDirectSoundNotifyImpl* notify;
179 LPDSBPOSITIONNOTIFY notifies;
181 PIDSDRIVERNOTIFY hwnotify;
183 /* DirectSound3DBuffer fields */
184 IDirectSound3DBufferImpl* ds3db;
185 DS3DBUFFER ds3db_ds3db;
187 BOOL ds3db_need_recalc;
189 /* IKsPropertySet fields */
190 IKsBufferPropertySetImpl* iks;
195 HRESULT IDirectSoundBufferImpl_Create(
196 DirectSoundDevice *device,
197 IDirectSoundBufferImpl **ppdsb,
198 LPCDSBUFFERDESC dsbd);
199 HRESULT IDirectSoundBufferImpl_Destroy(
200 IDirectSoundBufferImpl *pdsb);
201 HRESULT IDirectSoundBufferImpl_Duplicate(
202 DirectSoundDevice *device,
203 IDirectSoundBufferImpl **ppdsb,
204 IDirectSoundBufferImpl *pdsb);
206 /*****************************************************************************
207 * SecondaryBuffer implementation structure
209 struct SecondaryBufferImpl
211 const IDirectSoundBuffer8Vtbl *lpVtbl;
213 IDirectSoundBufferImpl* dsb;
216 HRESULT SecondaryBufferImpl_Create(
217 IDirectSoundBufferImpl *dsb,
218 SecondaryBufferImpl **pdsb);
220 /*****************************************************************************
221 * PrimaryBuffer implementation structure
223 struct PrimaryBufferImpl
225 const IDirectSoundBufferVtbl *lpVtbl;
227 DirectSoundDevice* device;
230 HRESULT PrimaryBufferImpl_Create(
231 DirectSoundDevice * device,
232 PrimaryBufferImpl **ppdsb,
233 LPCDSBUFFERDESC dsbd);
235 /*****************************************************************************
236 * DirectSoundCaptureDevice implementation structure
238 struct DirectSoundCaptureDevice
240 /* IDirectSoundCaptureImpl fields */
244 /* DirectSound driver stuff */
246 DSDRIVERDESC drvdesc;
247 DSCDRIVERCAPS drvcaps;
248 PIDSCDRIVERBUFFER hwbuf;
250 /* wave driver info */
259 IDirectSoundCaptureBufferImpl* capture_buffer;
264 CRITICAL_SECTION lock;
267 HRESULT DirectSoundCaptureDevice_Initialize(
268 DirectSoundCaptureDevice ** ppDevice,
270 ULONG DirectSoundCaptureDevice_Release(
271 DirectSoundCaptureDevice * device);
273 /*****************************************************************************
274 * IDirectSoundCaptureBuffer implementation structure
276 struct IDirectSoundCaptureBufferImpl
278 /* IUnknown fields */
279 const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
282 /* IDirectSoundCaptureBufferImpl fields */
283 DirectSoundCaptureDevice* device;
284 /* FIXME: don't need this */
285 LPDSCBUFFERDESC pdscbd;
288 /* IDirectSoundCaptureNotifyImpl fields */
289 IDirectSoundCaptureNotifyImpl* notify;
290 LPDSBPOSITIONNOTIFY notifies;
292 PIDSDRIVERNOTIFY hwnotify;
295 HRESULT IDirectSoundCaptureBufferImpl_Create(
296 DirectSoundCaptureDevice *device,
297 IDirectSoundCaptureBufferImpl ** ppobj,
298 LPCDSCBUFFERDESC lpcDSCBufferDesc);
300 /*****************************************************************************
301 * IDirectSoundFullDuplex implementation structure
303 struct IDirectSoundFullDuplexImpl
305 /* IUnknown fields */
306 const IDirectSoundFullDuplexVtbl *lpVtbl;
309 /* IDirectSoundFullDuplexImpl fields */
310 DirectSoundDevice *renderer_device;
311 DirectSoundCaptureDevice *capture_device;
316 LPDIRECTSOUNDCAPTURE pDSC;
319 /*****************************************************************************
320 * IDirectSoundFullDuplex COM components
322 struct IDirectSoundFullDuplex_IUnknown {
323 const IUnknownVtbl *lpVtbl;
325 IDirectSoundFullDuplexImpl *pdsfd;
328 struct IDirectSoundFullDuplex_IDirectSound {
329 const IDirectSoundVtbl *lpVtbl;
331 IDirectSoundFullDuplexImpl *pdsfd;
334 struct IDirectSoundFullDuplex_IDirectSound8 {
335 const IDirectSound8Vtbl *lpVtbl;
337 IDirectSoundFullDuplexImpl *pdsfd;
340 struct IDirectSoundFullDuplex_IDirectSoundCapture {
341 const IDirectSoundCaptureVtbl *lpVtbl;
343 IDirectSoundFullDuplexImpl *pdsfd;
346 /*****************************************************************************
347 * IDirectSound3DListener implementation structure
349 struct IDirectSound3DListenerImpl
351 /* IUnknown fields */
352 const IDirectSound3DListenerVtbl *lpVtbl;
354 /* IDirectSound3DListenerImpl fields */
355 DirectSoundDevice* device;
358 HRESULT IDirectSound3DListenerImpl_Create(
359 DirectSoundDevice *device,
360 IDirectSound3DListenerImpl **pdsl);
362 /*****************************************************************************
363 * IKsBufferPropertySet implementation structure
365 struct IKsBufferPropertySetImpl
367 /* IUnknown fields */
368 const IKsPropertySetVtbl *lpVtbl;
370 /* IKsPropertySetImpl fields */
371 IDirectSoundBufferImpl* dsb;
374 HRESULT IKsBufferPropertySetImpl_Create(
375 IDirectSoundBufferImpl *dsb,
376 IKsBufferPropertySetImpl **piks);
377 HRESULT IKsBufferPropertySetImpl_Destroy(
378 IKsBufferPropertySetImpl *piks);
380 /*****************************************************************************
381 * IKsPrivatePropertySet implementation structure
383 struct IKsPrivatePropertySetImpl
385 /* IUnknown fields */
386 const IKsPropertySetVtbl *lpVtbl;
390 HRESULT IKsPrivatePropertySetImpl_Create(
392 IKsPrivatePropertySetImpl **piks);
394 /*****************************************************************************
395 * IDirectSound3DBuffer implementation structure
397 struct IDirectSound3DBufferImpl
399 /* IUnknown fields */
400 const IDirectSound3DBufferVtbl *lpVtbl;
402 /* IDirectSound3DBufferImpl fields */
403 IDirectSoundBufferImpl* dsb;
406 HRESULT IDirectSound3DBufferImpl_Create(
407 IDirectSoundBufferImpl *dsb,
408 IDirectSound3DBufferImpl **pds3db);
409 HRESULT IDirectSound3DBufferImpl_Destroy(
410 IDirectSound3DBufferImpl *pds3db);
412 /*******************************************************************************
417 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS);
418 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS);
422 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign);
423 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
424 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
425 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
426 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
427 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
428 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced);
429 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave);
433 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD);
437 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len);
438 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
439 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
440 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
441 void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen);
442 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot);
444 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
445 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
449 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);
453 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC);
454 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8);
455 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
456 LPDIRECTSOUNDCAPTURE iface,
457 LPCDSCBUFFERDESC lpcDSCBufferDesc,
458 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
460 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
461 LPDIRECTSOUNDCAPTURE iface,
462 LPDSCCAPS lpDSCCaps);
463 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
464 LPDIRECTSOUNDCAPTURE iface,
467 #define STATE_STOPPED 0
468 #define STATE_STARTING 1
469 #define STATE_PLAYING 2
470 #define STATE_CAPTURING 2
471 #define STATE_STOPPING 3
473 #define DSOUND_FREQSHIFT (20)
475 extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
476 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
478 extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
479 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
481 HRESULT mmErr(UINT err);
482 void setup_dsound_options(void);
483 const char * dumpCooperativeLevel(DWORD level);