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_snd_shadow_maxsize;
39 extern int ds_hw_accel;
40 extern int ds_default_sample_rate;
41 extern int ds_default_bits_per_sample;
43 /*****************************************************************************
44 * Predeclare the interface implementation structures
46 typedef struct IDirectSoundImpl IDirectSoundImpl;
47 typedef struct IDirectSound_IUnknown IDirectSound_IUnknown;
48 typedef struct IDirectSound_IDirectSound IDirectSound_IDirectSound;
49 typedef struct IDirectSound8_IUnknown IDirectSound8_IUnknown;
50 typedef struct IDirectSound8_IDirectSound IDirectSound8_IDirectSound;
51 typedef struct IDirectSound8_IDirectSound8 IDirectSound8_IDirectSound8;
52 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
53 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
54 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
55 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
56 typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
57 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
58 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
59 typedef struct IKsBufferPropertySetImpl IKsBufferPropertySetImpl;
60 typedef struct IKsPrivatePropertySetImpl IKsPrivatePropertySetImpl;
61 typedef struct PrimaryBufferImpl PrimaryBufferImpl;
62 typedef struct SecondaryBufferImpl SecondaryBufferImpl;
63 typedef struct DirectSoundDevice DirectSoundDevice;
64 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
66 /* dsound_convert.h */
67 typedef void (*bitsconvertfunc)(const void *, void *, UINT, UINT, INT, UINT, UINT);
68 extern const bitsconvertfunc convertbpp[4][4];
69 typedef void (*mixfunc)(const void *, void *, unsigned);
70 extern const mixfunc mixfunctions[4];
71 typedef void (*normfunc)(const void *, void *, unsigned);
72 extern const normfunc normfunctions[4];
74 /*****************************************************************************
75 * IDirectSoundDevice implementation structure
77 struct DirectSoundDevice
89 UINT timerID, pwplay, pwqueue, prebuf, helfrags;
91 PIDSDRIVERBUFFER hwbuf;
93 DWORD writelead, buflen, state, playpos, mixpos;
95 IDirectSoundBufferImpl** buffers;
96 RTL_RWLOCK buffer_list_lock;
97 CRITICAL_SECTION mixlock;
98 PrimaryBufferImpl* primary;
100 DWORD speaker_config;
101 LPBYTE tmp_buffer, mix_buffer;
102 DWORD tmp_buffer_len, mix_buffer_len;
107 normfunc normfunction;
109 /* DirectSound3DListener fields */
110 IDirectSound3DListenerImpl* listener;
112 BOOL ds3dl_need_recalc;
115 /* reference counted buffer memory for duplicated buffer memory */
116 typedef struct BufferMemory
123 ULONG DirectSoundDevice_Release(DirectSoundDevice * device);
124 HRESULT DirectSoundDevice_Initialize(
125 DirectSoundDevice ** ppDevice,
127 HRESULT DirectSoundDevice_AddBuffer(
128 DirectSoundDevice * device,
129 IDirectSoundBufferImpl * pDSB);
130 HRESULT DirectSoundDevice_RemoveBuffer(
131 DirectSoundDevice * device,
132 IDirectSoundBufferImpl * pDSB);
133 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps);
134 HRESULT DirectSoundDevice_CreateSoundBuffer(
135 DirectSoundDevice * device,
136 LPCDSBUFFERDESC dsbd,
137 LPLPDIRECTSOUNDBUFFER ppdsb,
140 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
141 DirectSoundDevice * device,
142 LPDIRECTSOUNDBUFFER psb,
143 LPLPDIRECTSOUNDBUFFER ppdsb);
144 HRESULT DirectSoundDevice_SetCooperativeLevel(
145 DirectSoundDevice * devcie,
148 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device);
149 HRESULT DirectSoundDevice_GetSpeakerConfig(
150 DirectSoundDevice * device,
151 LPDWORD lpdwSpeakerConfig);
152 HRESULT DirectSoundDevice_SetSpeakerConfig(
153 DirectSoundDevice * device,
155 HRESULT DirectSoundDevice_VerifyCertification(DirectSoundDevice * device,
156 LPDWORD pdwCertified);
158 /*****************************************************************************
159 * IDirectSoundBuffer implementation structure
161 struct IDirectSoundBufferImpl
163 /* FIXME: document */
164 /* IUnknown fields */
165 const IDirectSoundBuffer8Vtbl *lpVtbl;
167 /* IDirectSoundBufferImpl fields */
168 SecondaryBufferImpl* secondary;
169 DirectSoundDevice* device;
171 PIDSDRIVERBUFFER hwbuf;
173 BufferMemory* buffer;
175 DWORD playflags,state,leadin;
176 DWORD writelead,buflen;
177 DWORD nAvgBytesPerSec;
178 DWORD freq, tmp_buffer_len, max_buffer_len;
181 /* used for frequency conversion (PerfectPitch) */
182 ULONG freqneeded, freqAdjust, freqAcc, freqAccNext, resampleinmixer;
183 /* used for mixing */
184 DWORD primary_mixpos, buf_mixpos, sec_mixpos;
186 /* IDirectSoundNotifyImpl fields */
187 IDirectSoundNotifyImpl* notify;
188 LPDSBPOSITIONNOTIFY notifies;
190 PIDSDRIVERNOTIFY hwnotify;
192 /* DirectSound3DBuffer fields */
193 IDirectSound3DBufferImpl* ds3db;
194 DS3DBUFFER ds3db_ds3db;
196 BOOL ds3db_need_recalc;
198 /* IKsPropertySet fields */
199 IKsBufferPropertySetImpl* iks;
200 bitsconvertfunc convert;
204 HRESULT IDirectSoundBufferImpl_Create(
205 DirectSoundDevice *device,
206 IDirectSoundBufferImpl **ppdsb,
207 LPCDSBUFFERDESC dsbd);
208 HRESULT IDirectSoundBufferImpl_Destroy(
209 IDirectSoundBufferImpl *pdsb);
210 HRESULT IDirectSoundBufferImpl_Duplicate(
211 DirectSoundDevice *device,
212 IDirectSoundBufferImpl **ppdsb,
213 IDirectSoundBufferImpl *pdsb);
215 /*****************************************************************************
216 * SecondaryBuffer implementation structure
218 struct SecondaryBufferImpl
220 const IDirectSoundBuffer8Vtbl *lpVtbl;
222 IDirectSoundBufferImpl* dsb;
225 HRESULT SecondaryBufferImpl_Create(
226 IDirectSoundBufferImpl *dsb,
227 SecondaryBufferImpl **pdsb);
229 /*****************************************************************************
230 * PrimaryBuffer implementation structure
232 struct PrimaryBufferImpl
234 const IDirectSoundBufferVtbl *lpVtbl;
236 DirectSoundDevice* device;
239 HRESULT PrimaryBufferImpl_Create(
240 DirectSoundDevice * device,
241 PrimaryBufferImpl **ppdsb,
242 LPCDSBUFFERDESC dsbd);
244 /*****************************************************************************
245 * DirectSoundCaptureDevice implementation structure
247 struct DirectSoundCaptureDevice
249 /* IDirectSoundCaptureImpl fields */
253 /* DirectSound driver stuff */
255 DSDRIVERDESC drvdesc;
256 DSCDRIVERCAPS drvcaps;
257 PIDSCDRIVERBUFFER hwbuf;
259 /* wave driver info */
268 IDirectSoundCaptureBufferImpl* capture_buffer;
273 CRITICAL_SECTION lock;
276 HRESULT DirectSoundCaptureDevice_Initialize(
277 DirectSoundCaptureDevice ** ppDevice,
279 ULONG DirectSoundCaptureDevice_Release(
280 DirectSoundCaptureDevice * device);
282 /*****************************************************************************
283 * IDirectSoundCaptureBuffer implementation structure
285 struct IDirectSoundCaptureBufferImpl
287 /* IUnknown fields */
288 const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
291 /* IDirectSoundCaptureBufferImpl fields */
292 DirectSoundCaptureDevice* device;
293 /* FIXME: don't need this */
294 LPDSCBUFFERDESC pdscbd;
297 /* IDirectSoundCaptureNotifyImpl fields */
298 IDirectSoundCaptureNotifyImpl* notify;
299 LPDSBPOSITIONNOTIFY notifies;
301 PIDSDRIVERNOTIFY hwnotify;
304 HRESULT IDirectSoundCaptureBufferImpl_Create(
305 DirectSoundCaptureDevice *device,
306 IDirectSoundCaptureBufferImpl ** ppobj,
307 LPCDSCBUFFERDESC lpcDSCBufferDesc);
309 /*****************************************************************************
310 * IDirectSound3DListener implementation structure
312 struct IDirectSound3DListenerImpl
314 /* IUnknown fields */
315 const IDirectSound3DListenerVtbl *lpVtbl;
317 /* IDirectSound3DListenerImpl fields */
318 DirectSoundDevice* device;
321 HRESULT IDirectSound3DListenerImpl_Create(
322 DirectSoundDevice *device,
323 IDirectSound3DListenerImpl **pdsl);
325 /*****************************************************************************
326 * IKsBufferPropertySet implementation structure
328 struct IKsBufferPropertySetImpl
330 /* IUnknown fields */
331 const IKsPropertySetVtbl *lpVtbl;
333 /* IKsPropertySetImpl fields */
334 IDirectSoundBufferImpl* dsb;
337 HRESULT IKsBufferPropertySetImpl_Create(
338 IDirectSoundBufferImpl *dsb,
339 IKsBufferPropertySetImpl **piks);
340 HRESULT IKsBufferPropertySetImpl_Destroy(
341 IKsBufferPropertySetImpl *piks);
343 /*****************************************************************************
344 * IKsPrivatePropertySet implementation structure
346 struct IKsPrivatePropertySetImpl
348 /* IUnknown fields */
349 const IKsPropertySetVtbl *lpVtbl;
353 HRESULT IKsPrivatePropertySetImpl_Create(
355 IKsPrivatePropertySetImpl **piks);
357 /*****************************************************************************
358 * IDirectSound3DBuffer implementation structure
360 struct IDirectSound3DBufferImpl
362 /* IUnknown fields */
363 const IDirectSound3DBufferVtbl *lpVtbl;
365 /* IDirectSound3DBufferImpl fields */
366 IDirectSoundBufferImpl* dsb;
369 HRESULT IDirectSound3DBufferImpl_Create(
370 IDirectSoundBufferImpl *dsb,
371 IDirectSound3DBufferImpl **pds3db);
372 HRESULT IDirectSound3DBufferImpl_Destroy(
373 IDirectSound3DBufferImpl *pds3db);
375 /*******************************************************************************
380 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS);
381 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS);
385 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign);
386 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
387 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
388 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
389 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
390 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
391 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex);
392 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced);
393 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave);
397 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD);
400 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos);
401 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len);
402 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
403 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
404 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
405 void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen, BOOL inmixer);
406 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot);
408 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
409 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
413 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);
417 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC);
418 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8);
419 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
420 LPDIRECTSOUNDCAPTURE iface,
421 LPCDSCBUFFERDESC lpcDSCBufferDesc,
422 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
424 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
425 LPDIRECTSOUNDCAPTURE iface,
426 LPDSCCAPS lpDSCCaps);
427 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
428 LPDIRECTSOUNDCAPTURE iface,
431 #define STATE_STOPPED 0
432 #define STATE_STARTING 1
433 #define STATE_PLAYING 2
434 #define STATE_CAPTURING 2
435 #define STATE_STOPPING 3
437 #define DSOUND_FREQSHIFT (20)
439 extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
440 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
442 extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
443 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
445 HRESULT mmErr(UINT err);
446 void setup_dsound_options(void);
447 const char * dumpCooperativeLevel(DWORD level);