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 */
27 #include "mmdeviceapi.h"
28 #include "audioclient.h"
31 #include "wine/list.h"
33 extern int ds_emuldriver DECLSPEC_HIDDEN;
34 extern int ds_hel_buflen DECLSPEC_HIDDEN;
35 extern int ds_snd_queue_max DECLSPEC_HIDDEN;
36 extern int ds_snd_shadow_maxsize DECLSPEC_HIDDEN;
37 extern int ds_default_sample_rate DECLSPEC_HIDDEN;
38 extern int ds_default_bits_per_sample DECLSPEC_HIDDEN;
40 /*****************************************************************************
41 * Predeclare the interface implementation structures
43 typedef struct IDirectSoundImpl IDirectSoundImpl;
44 typedef struct IDirectSound_IUnknown IDirectSound_IUnknown;
45 typedef struct IDirectSound_IDirectSound IDirectSound_IDirectSound;
46 typedef struct IDirectSound8_IUnknown IDirectSound8_IUnknown;
47 typedef struct IDirectSound8_IDirectSound IDirectSound8_IDirectSound;
48 typedef struct IDirectSound8_IDirectSound8 IDirectSound8_IDirectSound8;
49 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
50 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
51 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
52 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
53 typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
54 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
55 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
56 typedef struct IKsBufferPropertySetImpl IKsBufferPropertySetImpl;
57 typedef struct DirectSoundDevice DirectSoundDevice;
58 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
60 /* dsound_convert.h */
61 typedef void (*bitsconvertfunc)(const void *, void *, UINT, UINT, INT, UINT, UINT);
62 extern const bitsconvertfunc convertbpp[5][4] DECLSPEC_HIDDEN;
63 typedef void (*mixfunc)(const void *, void *, unsigned);
64 extern const mixfunc mixfunctions[4] DECLSPEC_HIDDEN;
65 typedef void (*normfunc)(const void *, void *, unsigned);
66 extern const normfunc normfunctions[4] DECLSPEC_HIDDEN;
68 typedef struct _DSVOLUMEPAN
70 DWORD dwTotalLeftAmpFactor;
71 DWORD dwTotalRightAmpFactor;
75 DWORD dwPanLeftAmpFactor;
76 DWORD dwPanRightAmpFactor;
77 } DSVOLUMEPAN,*PDSVOLUMEPAN;
79 /*****************************************************************************
80 * IDirectSoundDevice implementation structure
82 struct DirectSoundDevice
90 UINT timerID, pwplay, pwqueue, prebuf, helfrags;
91 UINT64 last_pos_bytes;
94 DWORD writelead, buflen, state, playpos, mixpos;
96 IDirectSoundBufferImpl** buffers;
97 RTL_RWLOCK buffer_list_lock;
98 CRITICAL_SECTION mixlock;
99 IDirectSoundBufferImpl *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 IAudioClient *client;
117 IAudioStreamVolume *volume;
118 IAudioRenderClient *render;
123 /* reference counted buffer memory for duplicated buffer memory */
124 typedef struct BufferMemory
131 ULONG DirectSoundDevice_Release(DirectSoundDevice * device) DECLSPEC_HIDDEN;
132 HRESULT DirectSoundDevice_Initialize(
133 DirectSoundDevice ** ppDevice,
134 LPCGUID lpcGUID) DECLSPEC_HIDDEN;
135 HRESULT DirectSoundDevice_AddBuffer(
136 DirectSoundDevice * device,
137 IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
138 HRESULT DirectSoundDevice_RemoveBuffer(
139 DirectSoundDevice * device,
140 IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
141 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps) DECLSPEC_HIDDEN;
142 HRESULT DirectSoundDevice_CreateSoundBuffer(
143 DirectSoundDevice * device,
144 LPCDSBUFFERDESC dsbd,
145 LPLPDIRECTSOUNDBUFFER ppdsb,
147 BOOL from8) DECLSPEC_HIDDEN;
148 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
149 DirectSoundDevice * device,
150 LPDIRECTSOUNDBUFFER psb,
151 LPLPDIRECTSOUNDBUFFER ppdsb) DECLSPEC_HIDDEN;
152 HRESULT DirectSoundDevice_SetCooperativeLevel(
153 DirectSoundDevice * devcie,
155 DWORD level) DECLSPEC_HIDDEN;
156 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device) DECLSPEC_HIDDEN;
157 HRESULT DirectSoundDevice_GetSpeakerConfig(
158 DirectSoundDevice * device,
159 LPDWORD lpdwSpeakerConfig) DECLSPEC_HIDDEN;
160 HRESULT DirectSoundDevice_SetSpeakerConfig(
161 DirectSoundDevice * device,
162 DWORD config) DECLSPEC_HIDDEN;
163 HRESULT DirectSoundDevice_VerifyCertification(DirectSoundDevice * device,
164 LPDWORD pdwCertified) DECLSPEC_HIDDEN;
166 /*****************************************************************************
167 * IDirectSoundBuffer implementation structure
169 struct IDirectSoundBufferImpl
171 IDirectSoundBuffer8 IDirectSoundBuffer8_iface;
172 LONG numIfaces; /* "in use interfaces" refcount */
174 /* IDirectSoundBufferImpl fields */
175 DirectSoundDevice* device;
178 BufferMemory* buffer;
180 DWORD playflags,state,leadin;
181 DWORD writelead,buflen;
182 DWORD nAvgBytesPerSec;
183 DWORD freq, tmp_buffer_len, max_buffer_len;
186 /* used for frequency conversion (PerfectPitch) */
187 ULONG freqneeded, freqAdjust, freqAcc, freqAccNext, resampleinmixer;
188 /* used for mixing */
189 DWORD primary_mixpos, buf_mixpos, sec_mixpos;
191 /* IDirectSoundNotifyImpl fields */
192 IDirectSoundNotifyImpl* notify;
193 LPDSBPOSITIONNOTIFY notifies;
196 /* DirectSound3DBuffer fields */
197 IDirectSound3DBufferImpl* ds3db;
198 DS3DBUFFER ds3db_ds3db;
200 BOOL ds3db_need_recalc;
202 /* IKsPropertySet fields */
203 IKsBufferPropertySetImpl* iks;
204 bitsconvertfunc convert;
208 HRESULT IDirectSoundBufferImpl_Create(
209 DirectSoundDevice *device,
210 IDirectSoundBufferImpl **ppdsb,
211 LPCDSBUFFERDESC dsbd) DECLSPEC_HIDDEN;
212 HRESULT IDirectSoundBufferImpl_Destroy(
213 IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
214 HRESULT IDirectSoundBufferImpl_Duplicate(
215 DirectSoundDevice *device,
216 IDirectSoundBufferImpl **ppdsb,
217 IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
218 void secondarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
220 /*****************************************************************************
221 * DirectSoundCaptureDevice implementation structure
223 struct DirectSoundCaptureDevice
231 DWORD buflen, write_pos_bytes;
235 IDirectSoundCaptureBufferImpl* capture_buffer;
238 CRITICAL_SECTION lock;
241 IAudioClient *client;
242 IAudioCaptureClient *capture;
247 /*****************************************************************************
248 * IDirectSoundCaptureBuffer implementation structure
250 struct IDirectSoundCaptureBufferImpl
252 /* IUnknown fields */
253 const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
256 /* IDirectSoundCaptureBufferImpl fields */
257 DirectSoundCaptureDevice* device;
258 /* FIXME: don't need this */
259 LPDSCBUFFERDESC pdscbd;
262 /* IDirectSoundCaptureNotifyImpl fields */
263 IDirectSoundCaptureNotifyImpl* notify;
264 LPDSBPOSITIONNOTIFY notifies;
268 /*****************************************************************************
269 * IDirectSound3DListener implementation structure
271 struct IDirectSound3DListenerImpl
273 /* IUnknown fields */
274 const IDirectSound3DListenerVtbl *lpVtbl;
276 /* IDirectSound3DListenerImpl fields */
277 DirectSoundDevice* device;
280 HRESULT IDirectSound3DListenerImpl_Create(
281 DirectSoundDevice *device,
282 IDirectSound3DListenerImpl **pdsl) DECLSPEC_HIDDEN;
284 /*****************************************************************************
285 * IKsBufferPropertySet implementation structure
287 struct IKsBufferPropertySetImpl
289 /* IUnknown fields */
290 const IKsPropertySetVtbl *lpVtbl;
292 /* IKsPropertySetImpl fields */
293 IDirectSoundBufferImpl* dsb;
296 HRESULT IKsBufferPropertySetImpl_Create(
297 IDirectSoundBufferImpl *dsb,
298 IKsBufferPropertySetImpl **piks) DECLSPEC_HIDDEN;
299 HRESULT IKsBufferPropertySetImpl_Destroy(
300 IKsBufferPropertySetImpl *piks) DECLSPEC_HIDDEN;
302 HRESULT IKsPrivatePropertySetImpl_Create(REFIID riid, IKsPropertySet **piks) DECLSPEC_HIDDEN;
304 /*****************************************************************************
305 * IDirectSound3DBuffer implementation structure
307 struct IDirectSound3DBufferImpl
309 /* IUnknown fields */
310 const IDirectSound3DBufferVtbl *lpVtbl;
312 /* IDirectSound3DBufferImpl fields */
313 IDirectSoundBufferImpl* dsb;
316 HRESULT IDirectSound3DBufferImpl_Create(
317 IDirectSoundBufferImpl *dsb,
318 IDirectSound3DBufferImpl **pds3db) DECLSPEC_HIDDEN;
319 HRESULT IDirectSound3DBufferImpl_Destroy(
320 IDirectSound3DBufferImpl *pds3db) DECLSPEC_HIDDEN;
322 /*******************************************************************************
327 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS) DECLSPEC_HIDDEN;
328 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS) DECLSPEC_HIDDEN;
332 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign) DECLSPEC_HIDDEN;
333 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device) DECLSPEC_HIDDEN;
334 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device) DECLSPEC_HIDDEN;
335 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device) DECLSPEC_HIDDEN;
336 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device) DECLSPEC_HIDDEN;
337 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos) DECLSPEC_HIDDEN;
338 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
339 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) DECLSPEC_HIDDEN;
340 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
341 const DSBUFFERDESC *dsbd) DECLSPEC_HIDDEN;
342 void primarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
343 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
347 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD) DECLSPEC_HIDDEN;
350 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos) DECLSPEC_HIDDEN;
351 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len) DECLSPEC_HIDDEN;
352 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
353 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
354 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
355 void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen, BOOL inmixer) DECLSPEC_HIDDEN;
356 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot) DECLSPEC_HIDDEN;
358 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) DECLSPEC_HIDDEN;
362 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
366 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC) DECLSPEC_HIDDEN;
367 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8) DECLSPEC_HIDDEN;
369 #define STATE_STOPPED 0
370 #define STATE_STARTING 1
371 #define STATE_PLAYING 2
372 #define STATE_CAPTURING 2
373 #define STATE_STOPPING 3
375 #define DSOUND_FREQSHIFT (20)
377 extern CRITICAL_SECTION DSOUND_renderers_lock DECLSPEC_HIDDEN;
378 extern CRITICAL_SECTION DSOUND_capturers_lock DECLSPEC_HIDDEN;
379 extern struct list DSOUND_capturers DECLSPEC_HIDDEN;
380 extern struct list DSOUND_renderers DECLSPEC_HIDDEN;
382 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
383 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
385 extern WCHAR wine_vxd_drv[] DECLSPEC_HIDDEN;
387 HRESULT mmErr(UINT err) DECLSPEC_HIDDEN;
388 void setup_dsound_options(void) DECLSPEC_HIDDEN;
389 const char * dumpCooperativeLevel(DWORD level) DECLSPEC_HIDDEN;
391 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device) DECLSPEC_HIDDEN;
393 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
394 DWORD depth, WORD channels) DECLSPEC_HIDDEN;
395 UINT DSOUND_create_timer(LPTIMECALLBACK cb, DWORD_PTR user) DECLSPEC_HIDDEN;
396 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
397 LPDSENUMCALLBACKW cb, void *user) DECLSPEC_HIDDEN;