winealsa: Unify the checks for wBitsPerSample.
[wine] / dlls / 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 /* 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 */
25
26 #include "wingdi.h"
27 #include "mmdeviceapi.h"
28 #include "audioclient.h"
29 #include "mmsystem.h"
30
31 #include "wine/list.h"
32
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;
38
39 /*****************************************************************************
40  * Predeclare the interface implementation structures
41  */
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 IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
53 typedef struct IDirectSound3DListenerImpl    IDirectSound3DListenerImpl;
54 typedef struct IDirectSound3DBufferImpl      IDirectSound3DBufferImpl;
55 typedef struct IKsBufferPropertySetImpl      IKsBufferPropertySetImpl;
56 typedef struct DirectSoundDevice             DirectSoundDevice;
57 typedef struct DirectSoundCaptureDevice      DirectSoundCaptureDevice;
58
59 /* dsound_convert.h */
60 typedef void (*bitsconvertfunc)(const void *, void *, UINT, UINT, INT, UINT, UINT);
61 extern const bitsconvertfunc convertbpp[5][4] DECLSPEC_HIDDEN;
62 typedef void (*mixfunc)(const void *, void *, unsigned);
63 extern const mixfunc mixfunctions[4] DECLSPEC_HIDDEN;
64 typedef void (*normfunc)(const void *, void *, unsigned);
65 extern const normfunc normfunctions[4] DECLSPEC_HIDDEN;
66
67 typedef struct _DSVOLUMEPAN
68 {
69     DWORD       dwTotalLeftAmpFactor;
70     DWORD       dwTotalRightAmpFactor;
71     LONG        lVolume;
72     DWORD       dwVolAmpFactor;
73     LONG        lPan;
74     DWORD       dwPanLeftAmpFactor;
75     DWORD       dwPanRightAmpFactor;
76 } DSVOLUMEPAN,*PDSVOLUMEPAN;
77
78 /*****************************************************************************
79  * IDirectSoundDevice implementation structure
80  */
81 struct DirectSoundDevice
82 {
83     LONG                        ref;
84
85     GUID                        guid;
86     DSCAPS                      drvcaps;
87     DWORD                       priolevel;
88     PWAVEFORMATEX               pwfx;
89     UINT                        timerID, pwplay, pwqueue, prebuf, helfrags;
90     UINT64                      last_pos_bytes;
91     DWORD                       fraglen;
92     LPBYTE                      buffer;
93     DWORD                       writelead, buflen, state, playpos, mixpos;
94     int                         nrofbuffers;
95     IDirectSoundBufferImpl**    buffers;
96     RTL_RWLOCK                  buffer_list_lock;
97     CRITICAL_SECTION            mixlock;
98     IDirectSoundBufferImpl     *primary;
99     DWORD                       speaker_config;
100     LPBYTE                      tmp_buffer, mix_buffer;
101     DWORD                       tmp_buffer_len, mix_buffer_len;
102
103     DSVOLUMEPAN                 volpan;
104
105     mixfunc mixfunction;
106     normfunc normfunction;
107
108     /* DirectSound3DListener fields */
109     IDirectSound3DListenerImpl* listener;
110     DS3DLISTENER                ds3dl;
111     BOOL                        ds3dl_need_recalc;
112
113     IMMDevice *mmdevice;
114     IAudioClient *client;
115     IAudioClock *clock;
116     IAudioStreamVolume *volume;
117     IAudioRenderClient *render;
118
119     struct list entry;
120 };
121
122 /* reference counted buffer memory for duplicated buffer memory */
123 typedef struct BufferMemory
124 {
125     LONG                        ref;
126     LPBYTE                      memory;
127     struct list buffers;
128 } BufferMemory;
129
130 ULONG DirectSoundDevice_Release(DirectSoundDevice * device) DECLSPEC_HIDDEN;
131 HRESULT DirectSoundDevice_Initialize(
132     DirectSoundDevice ** ppDevice,
133     LPCGUID lpcGUID) DECLSPEC_HIDDEN;
134 HRESULT DirectSoundDevice_AddBuffer(
135     DirectSoundDevice * device,
136     IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
137 HRESULT DirectSoundDevice_RemoveBuffer(
138     DirectSoundDevice * device,
139     IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
140 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps) DECLSPEC_HIDDEN;
141 HRESULT DirectSoundDevice_CreateSoundBuffer(
142     DirectSoundDevice * device,
143     LPCDSBUFFERDESC dsbd,
144     LPLPDIRECTSOUNDBUFFER ppdsb,
145     LPUNKNOWN lpunk,
146     BOOL from8) DECLSPEC_HIDDEN;
147 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
148     DirectSoundDevice * device,
149     LPDIRECTSOUNDBUFFER psb,
150     LPLPDIRECTSOUNDBUFFER ppdsb) DECLSPEC_HIDDEN;
151 HRESULT DirectSoundDevice_SetCooperativeLevel(
152     DirectSoundDevice * devcie,
153     HWND hwnd,
154     DWORD level) DECLSPEC_HIDDEN;
155 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device) DECLSPEC_HIDDEN;
156 HRESULT DirectSoundDevice_GetSpeakerConfig(
157     DirectSoundDevice * device,
158     LPDWORD lpdwSpeakerConfig) DECLSPEC_HIDDEN;
159 HRESULT DirectSoundDevice_SetSpeakerConfig(
160     DirectSoundDevice * device,
161     DWORD config) DECLSPEC_HIDDEN;
162 HRESULT DirectSoundDevice_VerifyCertification(DirectSoundDevice * device,
163     LPDWORD pdwCertified) DECLSPEC_HIDDEN;
164
165 /*****************************************************************************
166  * IDirectSoundBuffer implementation structure
167  */
168 struct IDirectSoundBufferImpl
169 {
170     IDirectSoundBuffer8         IDirectSoundBuffer8_iface;
171     LONG                        numIfaces; /* "in use interfaces" refcount */
172     LONG                        ref;
173     /* IDirectSoundBufferImpl fields */
174     DirectSoundDevice*          device;
175     RTL_RWLOCK                  lock;
176     PWAVEFORMATEX               pwfx;
177     BufferMemory*               buffer;
178     DWORD                       playflags,state,leadin;
179     DWORD                       writelead,buflen;
180     DWORD                       nAvgBytesPerSec;
181     DWORD                       freq, tmp_buffer_len;
182     DSVOLUMEPAN                 volpan;
183     DSBUFFERDESC                dsbd;
184     /* used for frequency conversion (PerfectPitch) */
185     ULONG                       freqneeded, freqAdjust, freqAcc, freqAccNext;
186     /* used for mixing */
187     DWORD                       primary_mixpos, buf_mixpos, sec_mixpos;
188
189     /* IDirectSoundNotifyImpl fields */
190     IDirectSoundNotifyImpl*     notify;
191     LPDSBPOSITIONNOTIFY         notifies;
192     int                         nrofnotifies;
193
194     /* DirectSound3DBuffer fields */
195     IDirectSound3DBufferImpl*   ds3db;
196     DS3DBUFFER                  ds3db_ds3db;
197     LONG                        ds3db_lVolume;
198     BOOL                        ds3db_need_recalc;
199
200     /* IKsPropertySet fields */
201     IKsBufferPropertySetImpl*   iks;
202     bitsconvertfunc convert;
203     struct list entry;
204 };
205
206 HRESULT IDirectSoundBufferImpl_Create(
207     DirectSoundDevice *device,
208     IDirectSoundBufferImpl **ppdsb,
209     LPCDSBUFFERDESC dsbd) DECLSPEC_HIDDEN;
210 HRESULT IDirectSoundBufferImpl_Destroy(
211     IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
212 HRESULT IDirectSoundBufferImpl_Duplicate(
213     DirectSoundDevice *device,
214     IDirectSoundBufferImpl **ppdsb,
215     IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
216 void secondarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
217
218 /*****************************************************************************
219  * DirectSoundCaptureDevice implementation structure
220  */
221 struct DirectSoundCaptureDevice
222 {
223     GUID                               guid;
224     LONG                               ref;
225
226     DSCCAPS                            drvcaps;
227
228     LPBYTE                             buffer;
229     DWORD                              buflen, write_pos_bytes;
230
231     PWAVEFORMATEX                      pwfx;
232
233     IDirectSoundCaptureBufferImpl*     capture_buffer;
234     DWORD                              state;
235     UINT timerID;
236     CRITICAL_SECTION                   lock;
237
238     IMMDevice *mmdevice;
239     IAudioClient *client;
240     IAudioCaptureClient *capture;
241
242     struct list entry;
243 };
244
245 /*****************************************************************************
246  * IDirectSoundCaptureBuffer implementation structure
247  */
248 struct IDirectSoundCaptureBufferImpl
249 {
250     /* IUnknown fields */
251     const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
252     LONG                                ref;
253
254     /* IDirectSoundCaptureBufferImpl fields */
255     DirectSoundCaptureDevice*           device;
256     /* FIXME: don't need this */
257     LPDSCBUFFERDESC                     pdscbd;
258     DWORD                               flags;
259
260     /* IDirectSoundCaptureNotifyImpl fields */
261     IDirectSoundCaptureNotifyImpl*      notify;
262     LPDSBPOSITIONNOTIFY                 notifies;
263     int                                 nrofnotifies;
264 };
265
266 /*****************************************************************************
267  *  IDirectSound3DListener implementation structure
268  */
269 struct IDirectSound3DListenerImpl
270 {
271     /* IUnknown fields */
272     const IDirectSound3DListenerVtbl *lpVtbl;
273     LONG                        ref;
274     /* IDirectSound3DListenerImpl fields */
275     DirectSoundDevice*          device;
276 };
277
278 HRESULT IDirectSound3DListenerImpl_Create(
279     DirectSoundDevice           *device,
280     IDirectSound3DListenerImpl **pdsl) DECLSPEC_HIDDEN;
281
282 /*****************************************************************************
283  *  IKsBufferPropertySet implementation structure
284  */
285 struct IKsBufferPropertySetImpl
286 {
287     /* IUnknown fields */
288     const IKsPropertySetVtbl   *lpVtbl;
289     LONG                        ref;
290     /* IKsPropertySetImpl fields */
291     IDirectSoundBufferImpl*     dsb;
292 };
293
294 HRESULT IKsBufferPropertySetImpl_Create(
295     IDirectSoundBufferImpl *dsb,
296     IKsBufferPropertySetImpl **piks) DECLSPEC_HIDDEN;
297 HRESULT IKsBufferPropertySetImpl_Destroy(
298     IKsBufferPropertySetImpl *piks) DECLSPEC_HIDDEN;
299
300 HRESULT IKsPrivatePropertySetImpl_Create(REFIID riid, IKsPropertySet **piks) DECLSPEC_HIDDEN;
301
302 /*****************************************************************************
303  * IDirectSound3DBuffer implementation structure
304  */
305 struct IDirectSound3DBufferImpl
306 {
307     /* IUnknown fields */
308     const IDirectSound3DBufferVtbl *lpVtbl;
309     LONG                        ref;
310     /* IDirectSound3DBufferImpl fields */
311     IDirectSoundBufferImpl*     dsb;
312 };
313
314 HRESULT IDirectSound3DBufferImpl_Create(
315     IDirectSoundBufferImpl *dsb,
316     IDirectSound3DBufferImpl **pds3db) DECLSPEC_HIDDEN;
317 HRESULT IDirectSound3DBufferImpl_Destroy(
318     IDirectSound3DBufferImpl *pds3db) DECLSPEC_HIDDEN;
319
320 /*******************************************************************************
321  */
322
323 /* dsound.c */
324
325 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS) DECLSPEC_HIDDEN;
326 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS) DECLSPEC_HIDDEN;
327
328 /* primary.c */
329
330 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign) DECLSPEC_HIDDEN;
331 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device) DECLSPEC_HIDDEN;
332 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device) DECLSPEC_HIDDEN;
333 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device) DECLSPEC_HIDDEN;
334 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device) DECLSPEC_HIDDEN;
335 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos) DECLSPEC_HIDDEN;
336 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
337 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) DECLSPEC_HIDDEN;
338 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
339     const DSBUFFERDESC *dsbd) DECLSPEC_HIDDEN;
340 void primarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
341 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
342
343 /* duplex.c */
344  
345 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD) DECLSPEC_HIDDEN;
346
347 /* mixer.c */
348 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos) DECLSPEC_HIDDEN;
349 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len) DECLSPEC_HIDDEN;
350 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
351 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
352 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
353 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot) DECLSPEC_HIDDEN;
354
355 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) DECLSPEC_HIDDEN;
356
357 /* sound3d.c */
358
359 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
360
361 /* capture.c */
362  
363 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC) DECLSPEC_HIDDEN;
364 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8) DECLSPEC_HIDDEN;
365
366 #define STATE_STOPPED   0
367 #define STATE_STARTING  1
368 #define STATE_PLAYING   2
369 #define STATE_CAPTURING 2
370 #define STATE_STOPPING  3
371
372 #define DSOUND_FREQSHIFT (20)
373
374 extern CRITICAL_SECTION DSOUND_renderers_lock DECLSPEC_HIDDEN;
375 extern CRITICAL_SECTION DSOUND_capturers_lock DECLSPEC_HIDDEN;
376 extern struct list DSOUND_capturers DECLSPEC_HIDDEN;
377 extern struct list DSOUND_renderers DECLSPEC_HIDDEN;
378
379 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
380 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
381
382 extern WCHAR wine_vxd_drv[] DECLSPEC_HIDDEN;
383
384 void setup_dsound_options(void) DECLSPEC_HIDDEN;
385 const char * dumpCooperativeLevel(DWORD level) DECLSPEC_HIDDEN;
386
387 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device) DECLSPEC_HIDDEN;
388
389 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
390         DWORD depth, WORD channels) DECLSPEC_HIDDEN;
391 UINT DSOUND_create_timer(LPTIMECALLBACK cb, DWORD_PTR user) DECLSPEC_HIDDEN;
392 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
393         LPDSENUMCALLBACKW cb, void *user) DECLSPEC_HIDDEN;