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_hel_buflen DECLSPEC_HIDDEN;
34 extern int ds_snd_queue_max DECLSPEC_HIDDEN;
35 extern int ds_snd_shadow_maxsize DECLSPEC_HIDDEN;
36 extern int ds_default_sample_rate DECLSPEC_HIDDEN;
37 extern int ds_default_bits_per_sample DECLSPEC_HIDDEN;
39 /*****************************************************************************
40 * Predeclare the interface implementation structures
42 typedef struct IDirectSoundImpl IDirectSoundImpl;
43 typedef struct IDirectSound_IUnknown IDirectSound_IUnknown;
44 typedef struct IDirectSound_IDirectSound IDirectSound_IDirectSound;
45 typedef struct IDirectSound8_IUnknown IDirectSound8_IUnknown;
46 typedef struct IDirectSound8_IDirectSound IDirectSound8_IDirectSound;
47 typedef struct IDirectSound8_IDirectSound8 IDirectSound8_IDirectSound8;
48 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
49 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
50 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
51 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
52 typedef struct DirectSoundDevice DirectSoundDevice;
53 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
55 /* dsound_convert.h */
56 typedef void (*bitsconvertfunc)(const void *, void *, UINT, UINT, INT, UINT, UINT);
57 extern const bitsconvertfunc convertbpp[5][4] DECLSPEC_HIDDEN;
58 typedef void (*mixfunc)(const void *, void *, unsigned);
59 extern const mixfunc mixfunctions[4] DECLSPEC_HIDDEN;
60 typedef void (*normfunc)(const void *, void *, unsigned);
61 extern const normfunc normfunctions[4] DECLSPEC_HIDDEN;
63 typedef struct _DSVOLUMEPAN
65 DWORD dwTotalLeftAmpFactor;
66 DWORD dwTotalRightAmpFactor;
70 DWORD dwPanLeftAmpFactor;
71 DWORD dwPanRightAmpFactor;
72 } DSVOLUMEPAN,*PDSVOLUMEPAN;
74 /*****************************************************************************
75 * IDirectSoundDevice implementation structure
77 struct DirectSoundDevice
85 UINT timerID, pwplay, pwqueue, prebuf, helfrags;
86 UINT64 last_pos_bytes;
89 DWORD writelead, buflen, state, playpos, mixpos;
91 IDirectSoundBufferImpl** buffers;
92 RTL_RWLOCK buffer_list_lock;
93 CRITICAL_SECTION mixlock;
94 IDirectSoundBufferImpl *primary;
96 LPBYTE tmp_buffer, mix_buffer;
97 DWORD tmp_buffer_len, mix_buffer_len;
102 normfunc normfunction;
104 /* DirectSound3DListener fields */
106 BOOL ds3dl_need_recalc;
109 IAudioClient *client;
111 IAudioStreamVolume *volume;
112 IAudioRenderClient *render;
117 /* reference counted buffer memory for duplicated buffer memory */
118 typedef struct BufferMemory
125 ULONG DirectSoundDevice_Release(DirectSoundDevice * device) DECLSPEC_HIDDEN;
126 HRESULT DirectSoundDevice_Initialize(
127 DirectSoundDevice ** ppDevice,
128 LPCGUID lpcGUID) DECLSPEC_HIDDEN;
129 HRESULT DirectSoundDevice_AddBuffer(
130 DirectSoundDevice * device,
131 IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
132 HRESULT DirectSoundDevice_RemoveBuffer(
133 DirectSoundDevice * device,
134 IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
135 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps) DECLSPEC_HIDDEN;
136 HRESULT DirectSoundDevice_CreateSoundBuffer(
137 DirectSoundDevice * device,
138 LPCDSBUFFERDESC dsbd,
139 LPLPDIRECTSOUNDBUFFER ppdsb,
141 BOOL from8) DECLSPEC_HIDDEN;
142 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
143 DirectSoundDevice * device,
144 LPDIRECTSOUNDBUFFER psb,
145 LPLPDIRECTSOUNDBUFFER ppdsb) DECLSPEC_HIDDEN;
146 HRESULT DirectSoundDevice_SetCooperativeLevel(
147 DirectSoundDevice * devcie,
149 DWORD level) DECLSPEC_HIDDEN;
150 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device) DECLSPEC_HIDDEN;
151 HRESULT DirectSoundDevice_GetSpeakerConfig(
152 DirectSoundDevice * device,
153 LPDWORD lpdwSpeakerConfig) DECLSPEC_HIDDEN;
154 HRESULT DirectSoundDevice_SetSpeakerConfig(
155 DirectSoundDevice * device,
156 DWORD config) DECLSPEC_HIDDEN;
157 HRESULT DirectSoundDevice_VerifyCertification(DirectSoundDevice * device,
158 LPDWORD pdwCertified) DECLSPEC_HIDDEN;
160 /*****************************************************************************
161 * IDirectSoundBuffer implementation structure
163 struct IDirectSoundBufferImpl
165 IDirectSoundBuffer8 IDirectSoundBuffer8_iface;
166 IDirectSound3DListener IDirectSound3DListener_iface; /* only primary buffer */
167 IDirectSound3DBuffer IDirectSound3DBuffer_iface; /* only secondary buffer */
168 IKsPropertySet IKsPropertySet_iface;
169 LONG numIfaces; /* "in use interfaces" refcount */
170 LONG ref, ref3D, refiks;
171 /* IDirectSoundBufferImpl fields */
172 DirectSoundDevice* device;
175 BufferMemory* buffer;
176 DWORD playflags,state,leadin;
177 DWORD writelead,buflen;
178 DWORD nAvgBytesPerSec;
179 DWORD freq, tmp_buffer_len;
182 /* used for frequency conversion (PerfectPitch) */
183 ULONG freqneeded, freqAdjust, freqAcc, freqAccNext;
184 /* used for mixing */
185 DWORD primary_mixpos, buf_mixpos, sec_mixpos;
187 /* IDirectSoundNotifyImpl fields */
188 IDirectSoundNotifyImpl* notify;
189 LPDSBPOSITIONNOTIFY notifies;
192 /* DirectSound3DBuffer fields */
193 DS3DBUFFER ds3db_ds3db;
195 BOOL ds3db_need_recalc;
196 /* IKsPropertySet fields */
197 bitsconvertfunc convert;
201 HRESULT IDirectSoundBufferImpl_Create(
202 DirectSoundDevice *device,
203 IDirectSoundBufferImpl **ppdsb,
204 LPCDSBUFFERDESC dsbd) DECLSPEC_HIDDEN;
205 HRESULT IDirectSoundBufferImpl_Destroy(
206 IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
207 HRESULT IDirectSoundBufferImpl_Duplicate(
208 DirectSoundDevice *device,
209 IDirectSoundBufferImpl **ppdsb,
210 IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
211 void secondarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
212 const IDirectSound3DListenerVtbl ds3dlvt DECLSPEC_HIDDEN;
213 const IDirectSound3DBufferVtbl ds3dbvt DECLSPEC_HIDDEN;
214 const IKsPropertySetVtbl iksbvt DECLSPEC_HIDDEN;
216 /*****************************************************************************
217 * DirectSoundCaptureDevice implementation structure
219 struct DirectSoundCaptureDevice
227 DWORD buflen, write_pos_bytes;
231 IDirectSoundCaptureBufferImpl* capture_buffer;
234 CRITICAL_SECTION lock;
237 IAudioClient *client;
238 IAudioCaptureClient *capture;
243 /*****************************************************************************
244 * IDirectSoundCaptureBuffer implementation structure
246 struct IDirectSoundCaptureBufferImpl
248 IDirectSoundCaptureBuffer8 IDirectSoundCaptureBuffer8_iface;
249 IDirectSoundNotify IDirectSoundNotify_iface;
250 LONG numIfaces; /* "in use interfaces" refcount */
252 /* IDirectSoundCaptureBuffer fields */
253 DirectSoundCaptureDevice* device;
254 LPDSCBUFFERDESC pdscbd;
256 /* IDirectSoundNotify fields */
257 LPDSBPOSITIONNOTIFY notifies;
261 HRESULT IKsPrivatePropertySetImpl_Create(REFIID riid, IKsPropertySet **piks) DECLSPEC_HIDDEN;
263 /*******************************************************************************
268 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS) DECLSPEC_HIDDEN;
269 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS) DECLSPEC_HIDDEN;
273 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign) DECLSPEC_HIDDEN;
274 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device) DECLSPEC_HIDDEN;
275 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device) DECLSPEC_HIDDEN;
276 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device) DECLSPEC_HIDDEN;
277 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device) DECLSPEC_HIDDEN;
278 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos) DECLSPEC_HIDDEN;
279 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
280 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) DECLSPEC_HIDDEN;
281 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
282 const DSBUFFERDESC *dsbd) DECLSPEC_HIDDEN;
283 void primarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
284 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
288 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD) DECLSPEC_HIDDEN;
291 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos) DECLSPEC_HIDDEN;
292 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len) DECLSPEC_HIDDEN;
293 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
294 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
295 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
296 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot) DECLSPEC_HIDDEN;
298 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) DECLSPEC_HIDDEN;
302 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
306 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC) DECLSPEC_HIDDEN;
307 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8) DECLSPEC_HIDDEN;
309 #define STATE_STOPPED 0
310 #define STATE_STARTING 1
311 #define STATE_PLAYING 2
312 #define STATE_CAPTURING 2
313 #define STATE_STOPPING 3
315 #define DSOUND_FREQSHIFT (20)
317 extern CRITICAL_SECTION DSOUND_renderers_lock DECLSPEC_HIDDEN;
318 extern CRITICAL_SECTION DSOUND_capturers_lock DECLSPEC_HIDDEN;
319 extern struct list DSOUND_capturers DECLSPEC_HIDDEN;
320 extern struct list DSOUND_renderers DECLSPEC_HIDDEN;
322 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
323 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
325 extern WCHAR wine_vxd_drv[] DECLSPEC_HIDDEN;
327 void setup_dsound_options(void) DECLSPEC_HIDDEN;
328 const char * dumpCooperativeLevel(DWORD level) DECLSPEC_HIDDEN;
330 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device) DECLSPEC_HIDDEN;
332 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
333 DWORD depth, WORD channels) DECLSPEC_HIDDEN;
334 UINT DSOUND_create_timer(LPTIMECALLBACK cb, DWORD_PTR user) DECLSPEC_HIDDEN;
335 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
336 LPDSENUMCALLBACKW cb, void *user) DECLSPEC_HIDDEN;