Sync to trunk revision 61757.
[reactos.git] / dll / directx / wine / dsound / dsound_private.h
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <wine/config.h>
23
24 #include <assert.h>
25 #include <math.h>
26 #include <stdarg.h>
27
28 #define WIN32_NO_STATUS
29 #define _INC_WINDOWS
30 #define COM_NO_WINDOWS_H
31
32 #define COBJMACROS
33 #define NONAMELESSSTRUCT
34 #define NONAMELESSUNION
35
36 #include <windef.h>
37 #include <winbase.h>
38 #include <winnls.h>
39 #include <wingdi.h>
40 #include <winternl.h>
41 #include <objbase.h>
42 #include <mmdeviceapi.h>
43 #include <audioclient.h>
44 #include <mmsystem.h>
45 #include <dsound.h>
46 #include <dsconf.h>
47 #include <vfwmsgs.h>
48 #include <devpkey.h>
49
50 #include <wine/debug.h>
51 #include <wine/list.h>
52
53 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54
55 /* Linux does not support better timing than 10ms */
56 #define DS_TIME_RES 2 /* Resolution of multimedia timer */
57 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
58
59 extern int ds_hel_buflen DECLSPEC_HIDDEN;
60 extern int ds_snd_queue_max DECLSPEC_HIDDEN;
61
62 /*****************************************************************************
63 * Predeclare the interface implementation structures
64 */
65 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
66 typedef struct DirectSoundDevice DirectSoundDevice;
67
68 /* dsound_convert.h */
69 typedef float (*bitsgetfunc)(const IDirectSoundBufferImpl *, DWORD, DWORD);
70 typedef void (*bitsputfunc)(const IDirectSoundBufferImpl *, DWORD, DWORD, float);
71 extern const bitsgetfunc getbpp[5] DECLSPEC_HIDDEN;
72 void putieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value) DECLSPEC_HIDDEN;
73 void mixieee32(float *src, float *dst, unsigned samples) DECLSPEC_HIDDEN;
74 typedef void (*normfunc)(const void *, void *, unsigned);
75 extern const normfunc normfunctions[5] DECLSPEC_HIDDEN;
76
77 typedef struct _DSVOLUMEPAN
78 {
79 DWORD dwTotalLeftAmpFactor;
80 DWORD dwTotalRightAmpFactor;
81 LONG lVolume;
82 DWORD dwVolAmpFactor;
83 LONG lPan;
84 DWORD dwPanLeftAmpFactor;
85 DWORD dwPanRightAmpFactor;
86 } DSVOLUMEPAN,*PDSVOLUMEPAN;
87
88 /*****************************************************************************
89 * IDirectSoundDevice implementation structure
90 */
91 struct DirectSoundDevice
92 {
93 LONG ref;
94
95 GUID guid;
96 DSCAPS drvcaps;
97 DWORD priolevel, sleeptime;
98 PWAVEFORMATEX pwfx, primary_pwfx;
99 UINT playing_offs_bytes, in_mmdev_bytes, prebuf;
100 DWORD fraglen;
101 LPBYTE buffer;
102 DWORD writelead, buflen, state, playpos, mixpos;
103 int nrofbuffers;
104 IDirectSoundBufferImpl** buffers;
105 RTL_RWLOCK buffer_list_lock;
106 CRITICAL_SECTION mixlock;
107 IDirectSoundBufferImpl *primary;
108 DWORD speaker_config;
109 float *mix_buffer, *tmp_buffer;
110 DWORD tmp_buffer_len, mix_buffer_len;
111
112 DSVOLUMEPAN volpan;
113
114 normfunc normfunction;
115
116 /* DirectSound3DListener fields */
117 DS3DLISTENER ds3dl;
118 BOOL ds3dl_need_recalc;
119
120 IMMDevice *mmdevice;
121 IAudioClient *client;
122 IAudioClock *clock;
123 IAudioStreamVolume *volume;
124 IAudioRenderClient *render;
125
126 HANDLE sleepev, thread;
127 struct list entry;
128 };
129
130 /* reference counted buffer memory for duplicated buffer memory */
131 typedef struct BufferMemory
132 {
133 LONG ref;
134 LPBYTE memory;
135 struct list buffers;
136 } BufferMemory;
137
138 ULONG DirectSoundDevice_Release(DirectSoundDevice * device) DECLSPEC_HIDDEN;
139 HRESULT DirectSoundDevice_Initialize(
140 DirectSoundDevice ** ppDevice,
141 LPCGUID lpcGUID) DECLSPEC_HIDDEN;
142 HRESULT DirectSoundDevice_AddBuffer(
143 DirectSoundDevice * device,
144 IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
145 void DirectSoundDevice_RemoveBuffer(DirectSoundDevice * device, IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
146 HRESULT DirectSoundDevice_CreateSoundBuffer(
147 DirectSoundDevice * device,
148 LPCDSBUFFERDESC dsbd,
149 LPLPDIRECTSOUNDBUFFER ppdsb,
150 LPUNKNOWN lpunk,
151 BOOL from8) DECLSPEC_HIDDEN;
152 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
153 DirectSoundDevice * device,
154 LPDIRECTSOUNDBUFFER psb,
155 LPLPDIRECTSOUNDBUFFER ppdsb) DECLSPEC_HIDDEN;
156
157 /*****************************************************************************
158 * IDirectSoundBuffer implementation structure
159 */
160 struct IDirectSoundBufferImpl
161 {
162 IDirectSoundBuffer8 IDirectSoundBuffer8_iface;
163 IDirectSoundNotify IDirectSoundNotify_iface;
164 IDirectSound3DListener IDirectSound3DListener_iface; /* only primary buffer */
165 IDirectSound3DBuffer IDirectSound3DBuffer_iface; /* only secondary buffer */
166 IKsPropertySet IKsPropertySet_iface;
167 LONG numIfaces; /* "in use interfaces" refcount */
168 LONG ref, refn, ref3D, refiks;
169 /* IDirectSoundBufferImpl fields */
170 DirectSoundDevice* device;
171 RTL_RWLOCK lock;
172 PWAVEFORMATEX pwfx;
173 BufferMemory* buffer;
174 DWORD playflags,state,leadin;
175 DWORD writelead,buflen;
176 DWORD nAvgBytesPerSec;
177 DWORD freq;
178 DSVOLUMEPAN volpan;
179 DSBUFFERDESC dsbd;
180 /* used for frequency conversion (PerfectPitch) */
181 ULONG freqneeded;
182 DWORD firstep;
183 float freqAcc, freqAdjust, firgain;
184 /* used for mixing */
185 DWORD sec_mixpos;
186
187 /* IDirectSoundNotify fields */
188 LPDSBPOSITIONNOTIFY notifies;
189 int nrofnotifies;
190 /* DirectSound3DBuffer fields */
191 DS3DBUFFER ds3db_ds3db;
192 LONG ds3db_lVolume;
193 BOOL ds3db_need_recalc;
194 /* Used for bit depth conversion */
195 int mix_channels;
196 bitsgetfunc get, get_aux;
197 bitsputfunc put, put_aux;
198
199 struct list entry;
200 };
201
202 float get_mono(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) DECLSPEC_HIDDEN;
203 void put_mono2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value) DECLSPEC_HIDDEN;
204
205 HRESULT IDirectSoundBufferImpl_Create(
206 DirectSoundDevice *device,
207 IDirectSoundBufferImpl **ppdsb,
208 LPCDSBUFFERDESC dsbd) DECLSPEC_HIDDEN;
209 HRESULT IDirectSoundBufferImpl_Duplicate(
210 DirectSoundDevice *device,
211 IDirectSoundBufferImpl **ppdsb,
212 IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
213 void secondarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
214 const IDirectSound3DListenerVtbl ds3dlvt DECLSPEC_HIDDEN;
215 const IDirectSound3DBufferVtbl ds3dbvt DECLSPEC_HIDDEN;
216 const IKsPropertySetVtbl iksbvt DECLSPEC_HIDDEN;
217
218 HRESULT IKsPrivatePropertySetImpl_Create(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
219
220 /*******************************************************************************
221 */
222
223 /* dsound.c */
224
225 HRESULT DSOUND_Create(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
226 HRESULT DSOUND_Create8(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
227 HRESULT IDirectSoundImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_ds8) DECLSPEC_HIDDEN;
228
229 /* primary.c */
230
231 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device) DECLSPEC_HIDDEN;
232 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device) DECLSPEC_HIDDEN;
233 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device) DECLSPEC_HIDDEN;
234 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device) DECLSPEC_HIDDEN;
235 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos) DECLSPEC_HIDDEN;
236 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
237 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) DECLSPEC_HIDDEN;
238 HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device) DECLSPEC_HIDDEN;
239 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
240 const DSBUFFERDESC *dsbd) DECLSPEC_HIDDEN;
241 void primarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
242 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
243 LONG capped_refcount_dec(LONG *ref) DECLSPEC_HIDDEN;
244
245 /* duplex.c */
246
247 HRESULT DSOUND_FullDuplexCreate(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
248
249 /* mixer.c */
250 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len) DECLSPEC_HIDDEN;
251 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
252 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
253 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
254 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, float *overshot) DECLSPEC_HIDDEN;
255
256 DWORD CALLBACK DSOUND_mixthread(void *ptr) DECLSPEC_HIDDEN;
257
258 /* sound3d.c */
259
260 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
261
262 /* capture.c */
263
264 HRESULT DSOUND_CaptureCreate(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
265 HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppv) DECLSPEC_HIDDEN;
266 HRESULT IDirectSoundCaptureImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_dsc8) DECLSPEC_HIDDEN;
267
268 #define STATE_STOPPED 0
269 #define STATE_STARTING 1
270 #define STATE_PLAYING 2
271 #define STATE_CAPTURING 2
272 #define STATE_STOPPING 3
273
274 extern CRITICAL_SECTION DSOUND_renderers_lock DECLSPEC_HIDDEN;
275 extern CRITICAL_SECTION DSOUND_capturers_lock DECLSPEC_HIDDEN;
276 extern struct list DSOUND_capturers DECLSPEC_HIDDEN;
277 extern struct list DSOUND_renderers DECLSPEC_HIDDEN;
278
279 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
280 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
281
282 extern WCHAR wine_vxd_drv[] DECLSPEC_HIDDEN;
283
284 void setup_dsound_options(void) DECLSPEC_HIDDEN;
285
286 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device) DECLSPEC_HIDDEN;
287
288 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
289 DWORD depth, WORD channels) DECLSPEC_HIDDEN;
290 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
291 LPDSENUMCALLBACKW cb, void *user) DECLSPEC_HIDDEN;