dsound_private.h 17.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  			DirectSound
 *
 * Copyright 1998 Marcus Meissner
 * Copyright 1998 Rob Riggs
 * Copyright 2000-2001 TransGaming Technologies, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25
#define DS_TIME_DEL 10  /* Delay of multimedia timer callback, and duration of HEL fragment */

26 27
#include "wine/list.h"

28 29 30 31 32 33
/* direct sound hardware acceleration levels */
#define DS_HW_ACCEL_FULL        0	/* default on Windows 98 */
#define DS_HW_ACCEL_STANDARD    1	/* default on Windows 2000 */
#define DS_HW_ACCEL_BASIC       2
#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 41
extern int ds_default_playback;
extern int ds_default_capture;
42 43
extern int ds_default_sample_rate;
extern int ds_default_bits_per_sample;
44 45 46 47

/*****************************************************************************
 * Predeclare the interface implementation structures
 */
48 49 50 51 52 53 54 55
typedef struct IDirectSoundImpl              IDirectSoundImpl;
typedef struct IDirectSound_IUnknown         IDirectSound_IUnknown;
typedef struct IDirectSound_IDirectSound     IDirectSound_IDirectSound;
typedef struct IDirectSound8_IUnknown        IDirectSound8_IUnknown;
typedef struct IDirectSound8_IDirectSound    IDirectSound8_IDirectSound;
typedef struct IDirectSound8_IDirectSound8   IDirectSound8_IDirectSound8;
typedef struct IDirectSoundBufferImpl        IDirectSoundBufferImpl;
typedef struct IDirectSoundCaptureImpl       IDirectSoundCaptureImpl;
56
typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
57
typedef struct IDirectSoundFullDuplexImpl    IDirectSoundFullDuplexImpl;
58 59 60 61
typedef struct IDirectSoundFullDuplex_IUnknown IDirectSoundFullDuplex_IUnknown;
typedef struct IDirectSoundFullDuplex_IDirectSound IDirectSoundFullDuplex_IDirectSound;
typedef struct IDirectSoundFullDuplex_IDirectSound8 IDirectSoundFullDuplex_IDirectSound8;
typedef struct IDirectSoundFullDuplex_IDirectSoundCapture IDirectSoundFullDuplex_IDirectSoundCapture;
62
typedef struct IDirectSoundNotifyImpl        IDirectSoundNotifyImpl;
63
typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
64 65 66 67 68 69
typedef struct IDirectSound3DListenerImpl    IDirectSound3DListenerImpl;
typedef struct IDirectSound3DBufferImpl      IDirectSound3DBufferImpl;
typedef struct IKsBufferPropertySetImpl      IKsBufferPropertySetImpl;
typedef struct IKsPrivatePropertySetImpl     IKsPrivatePropertySetImpl;
typedef struct PrimaryBufferImpl             PrimaryBufferImpl;
typedef struct SecondaryBufferImpl           SecondaryBufferImpl;
70
typedef struct DirectSoundDevice             DirectSoundDevice;
71
typedef struct DirectSoundCaptureDevice      DirectSoundCaptureDevice;
72

73 74 75
/* dsound_convert.h */
typedef void (*bitsconvertfunc)(const void *, void *);
extern const bitsconvertfunc convertbpp[4][4];
76 77 78 79
typedef void (*mixfunc)(const void *, void *, unsigned);
extern const mixfunc mixfunctions[4];
typedef void (*normfunc)(const void *, void *, unsigned);
extern const normfunc normfunctions[4];
80

81 82 83
/*****************************************************************************
 * IDirectSoundDevice implementation structure
 */
84 85
struct DirectSoundDevice
{
86
    LONG                        ref;
87

88
    GUID                        guid;
89 90 91 92
    PIDSDRIVER                  driver;
    DSDRIVERDESC                drvdesc;
    DSDRIVERCAPS                drvcaps;
    DWORD                       priolevel;
93
    PWAVEFORMATEX               pwfx;
94
    HWAVEOUT                    hwo;
95 96
    LPWAVEHDR                   pwave;
    UINT                        timerID, pwplay, pwqueue, prebuf, helfrags;
97 98 99 100 101 102
    DWORD                       fraglen;
    PIDSDRIVERBUFFER            hwbuf;
    LPBYTE                      buffer;
    DWORD                       writelead, buflen, state, playpos, mixpos;
    int                         nrofbuffers;
    IDirectSoundBufferImpl**    buffers;
103
    RTL_RWLOCK                  buffer_list_lock;
104
    CRITICAL_SECTION            mixlock;
105 106
    PrimaryBufferImpl*          primary;
    DSBUFFERDESC                dsbd;
107
    DWORD                       speaker_config;
108 109 110 111 112
    LPBYTE                      tmp_buffer, mix_buffer;
    DWORD                       tmp_buffer_len, mix_buffer_len;

    mixfunc mixfunction;
    normfunc normfunction;
113 114 115 116 117

    /* DirectSound3DListener fields */
    IDirectSound3DListenerImpl*	listener;
    DS3DLISTENER                ds3dl;
    BOOL                        ds3dl_need_recalc;
118 119
};

120 121 122
/* reference counted buffer memory for duplicated buffer memory */
typedef struct BufferMemory
{
123
    LONG                        ref;
124
    LPBYTE                      memory;
125
    struct list buffers;
126 127
} BufferMemory;

128
ULONG DirectSoundDevice_Release(DirectSoundDevice * device);
129 130 131
HRESULT DirectSoundDevice_Initialize(
    DirectSoundDevice ** ppDevice,
    LPCGUID lpcGUID);
132
HRESULT DirectSoundDevice_AddBuffer(
133
    DirectSoundDevice * device,
134 135
    IDirectSoundBufferImpl * pDSB);
HRESULT DirectSoundDevice_RemoveBuffer(
136
    DirectSoundDevice * device,
137
    IDirectSoundBufferImpl * pDSB);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps);
HRESULT DirectSoundDevice_CreateSoundBuffer(
    DirectSoundDevice * device,
    LPCDSBUFFERDESC dsbd,
    LPLPDIRECTSOUNDBUFFER ppdsb,
    LPUNKNOWN lpunk,
    BOOL from8);
HRESULT DirectSoundDevice_DuplicateSoundBuffer(
    DirectSoundDevice * device,
    LPDIRECTSOUNDBUFFER psb,
    LPLPDIRECTSOUNDBUFFER ppdsb);
HRESULT DirectSoundDevice_SetCooperativeLevel(
    DirectSoundDevice * devcie,
    HWND hwnd,
    DWORD level);
HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device);
HRESULT DirectSoundDevice_GetSpeakerConfig(
    DirectSoundDevice * device,
    LPDWORD lpdwSpeakerConfig);
HRESULT DirectSoundDevice_SetSpeakerConfig(
    DirectSoundDevice * device,
    DWORD config);
160

161 162 163 164 165 166 167
/*****************************************************************************
 * IDirectSoundBuffer implementation structure
 */
struct IDirectSoundBufferImpl
{
    /* FIXME: document */
    /* IUnknown fields */
168
    const IDirectSoundBuffer8Vtbl *lpVtbl;
169
    LONG                        ref;
170
    /* IDirectSoundBufferImpl fields */
171
    SecondaryBufferImpl*        secondary;
Robert Reif's avatar
Robert Reif committed
172
    DirectSoundDevice*          device;
173
    RTL_RWLOCK                  lock;
174
    PIDSDRIVERBUFFER            hwbuf;
175
    PWAVEFORMATEX               pwfx;
176
    BufferMemory*               buffer;
177
    LPBYTE                      tmp_buffer;
178
    DWORD                       playflags,state,leadin;
179
    DWORD                       writelead,buflen;
180
    DWORD                       nAvgBytesPerSec;
181
    DWORD                       freq, tmp_buffer_len, max_buffer_len;
182
    DSVOLUMEPAN                 volpan;
183
    DSBUFFERDESC                dsbd;
184
    /* used for frequency conversion (PerfectPitch) */
185
    ULONG                       freqneeded, freqAdjust, freqAcc, freqAccNext, resampleinmixer;
186 187
    /* used for mixing */
    DWORD                       primary_mixpos, buf_mixpos, sec_mixpos;
188

189
    /* IDirectSoundNotifyImpl fields */
190
    IDirectSoundNotifyImpl*     notify;
191 192 193
    LPDSBPOSITIONNOTIFY         notifies;
    int                         nrofnotifies;
    PIDSDRIVERNOTIFY            hwnotify;
194 195

    /* DirectSound3DBuffer fields */
196
    IDirectSound3DBufferImpl*   ds3db;
197 198 199
    DS3DBUFFER                  ds3db_ds3db;
    LONG                        ds3db_lVolume;
    BOOL                        ds3db_need_recalc;
200 201 202

    /* IKsPropertySet fields */
    IKsBufferPropertySetImpl*   iks;
203
    bitsconvertfunc convert;
204
    struct list entry;
205 206
};

207
HRESULT IDirectSoundBufferImpl_Create(
Robert Reif's avatar
Robert Reif committed
208 209
    DirectSoundDevice *device,
    IDirectSoundBufferImpl **ppdsb,
210
    LPCDSBUFFERDESC dsbd);
211
HRESULT IDirectSoundBufferImpl_Destroy(
212
    IDirectSoundBufferImpl *pdsb);
213 214 215 216
HRESULT IDirectSoundBufferImpl_Duplicate(
    DirectSoundDevice *device,
    IDirectSoundBufferImpl **ppdsb,
    IDirectSoundBufferImpl *pdsb);
217

218 219 220 221 222
/*****************************************************************************
 * SecondaryBuffer implementation structure
 */
struct SecondaryBufferImpl
{
223
    const IDirectSoundBuffer8Vtbl *lpVtbl;
224
    LONG                        ref;
225 226 227
    IDirectSoundBufferImpl*     dsb;
};

228
HRESULT SecondaryBufferImpl_Create(
229 230 231 232 233 234 235 236
    IDirectSoundBufferImpl *dsb,
    SecondaryBufferImpl **pdsb);

/*****************************************************************************
 * PrimaryBuffer implementation structure
 */
struct PrimaryBufferImpl
{
237
    const IDirectSoundBufferVtbl *lpVtbl;
238
    LONG                        ref;
Robert Reif's avatar
Robert Reif committed
239
    DirectSoundDevice*          device;
240 241
};

242
HRESULT PrimaryBufferImpl_Create(
Robert Reif's avatar
Robert Reif committed
243 244
    DirectSoundDevice * device,
    PrimaryBufferImpl **ppdsb,
245
    LPCDSBUFFERDESC dsbd);
246

Robert Reif's avatar
Robert Reif committed
247 248 249
/*****************************************************************************
 * DirectSoundCaptureDevice implementation structure
 */
250 251
struct DirectSoundCaptureDevice
{
252
    /* IDirectSoundCaptureImpl fields */
253
    GUID                               guid;
254
    LONG                               ref;
255

256 257 258 259 260
    /* DirectSound driver stuff */
    PIDSCDRIVER                        driver;
    DSDRIVERDESC                       drvdesc;
    DSCDRIVERCAPS                      drvcaps;
    PIDSCDRIVERBUFFER                  hwbuf;
261

262 263
    /* wave driver info */
    HWAVEIN                            hwi;
264

265
    /* more stuff */
266 267 268
    LPBYTE                             buffer;
    DWORD                              buflen;

269
    PWAVEFORMATEX                      pwfx;
270

271 272
    IDirectSoundCaptureBufferImpl*     capture_buffer;
    DWORD                              state;
273
    LPWAVEHDR                          pwave;
274 275
    int                                nrofpwaves;
    int                                index;
276 277 278
    CRITICAL_SECTION                   lock;
};

279 280 281
HRESULT DirectSoundCaptureDevice_Initialize(
    DirectSoundCaptureDevice ** ppDevice,
    LPCGUID lpcGUID);
Robert Reif's avatar
Robert Reif committed
282 283
ULONG DirectSoundCaptureDevice_Release(
    DirectSoundCaptureDevice * device);
284

285 286 287 288 289
/*****************************************************************************
 * IDirectSoundCaptureBuffer implementation structure
 */
struct IDirectSoundCaptureBufferImpl
{
290
    /* IUnknown fields */
291
    const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
292
    LONG                                ref;
293 294

    /* IDirectSoundCaptureBufferImpl fields */
295
    DirectSoundCaptureDevice*           device;
296
    /* FIXME: don't need this */
297 298
    LPDSCBUFFERDESC                     pdscbd;
    DWORD                               flags;
299 300 301 302 303 304

    /* IDirectSoundCaptureNotifyImpl fields */
    IDirectSoundCaptureNotifyImpl*      notify;
    LPDSBPOSITIONNOTIFY                 notifies;
    int                                 nrofnotifies;
    PIDSDRIVERNOTIFY                    hwnotify;
305 306
};

307 308 309 310
HRESULT IDirectSoundCaptureBufferImpl_Create(
    DirectSoundCaptureDevice *device,
    IDirectSoundCaptureBufferImpl ** ppobj,
    LPCDSCBUFFERDESC lpcDSCBufferDesc);
Robert Reif's avatar
Robert Reif committed
311

312 313 314 315 316 317
/*****************************************************************************
 * IDirectSoundFullDuplex implementation structure
 */
struct IDirectSoundFullDuplexImpl
{
    /* IUnknown fields */
318
    const IDirectSoundFullDuplexVtbl *lpVtbl;
319
    LONG                              ref;
320 321

    /* IDirectSoundFullDuplexImpl fields */
322 323 324 325 326 327 328
    DirectSoundDevice                *renderer_device;
    DirectSoundCaptureDevice         *capture_device;

    LPUNKNOWN                         pUnknown;
    LPDIRECTSOUND                     pDS;
    LPDIRECTSOUND8                    pDS8;
    LPDIRECTSOUNDCAPTURE              pDSC;
329 330
};

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/*****************************************************************************
 * IDirectSoundFullDuplex COM components
 */
struct IDirectSoundFullDuplex_IUnknown {
    const IUnknownVtbl         *lpVtbl;
    LONG                        ref;
    IDirectSoundFullDuplexImpl *pdsfd;
};

struct IDirectSoundFullDuplex_IDirectSound {
    const IDirectSoundVtbl     *lpVtbl;
    LONG                        ref;
    IDirectSoundFullDuplexImpl *pdsfd;
};

struct IDirectSoundFullDuplex_IDirectSound8 {
    const IDirectSound8Vtbl    *lpVtbl;
    LONG                        ref;
    IDirectSoundFullDuplexImpl *pdsfd;
};

struct IDirectSoundFullDuplex_IDirectSoundCapture {
    const IDirectSoundCaptureVtbl *lpVtbl;
    LONG                           ref;
    IDirectSoundFullDuplexImpl    *pdsfd;
};

358 359 360 361 362 363
/*****************************************************************************
 *  IDirectSound3DListener implementation structure
 */
struct IDirectSound3DListenerImpl
{
    /* IUnknown fields */
364
    const IDirectSound3DListenerVtbl *lpVtbl;
365
    LONG                        ref;
366
    /* IDirectSound3DListenerImpl fields */
Robert Reif's avatar
Robert Reif committed
367
    DirectSoundDevice*          device;
368 369
};

370
HRESULT IDirectSound3DListenerImpl_Create(
Robert Reif's avatar
Robert Reif committed
371
    DirectSoundDevice           *device,
372
    IDirectSound3DListenerImpl **pdsl);
373 374

/*****************************************************************************
375
 *  IKsBufferPropertySet implementation structure
376
 */
377
struct IKsBufferPropertySetImpl
378 379
{
    /* IUnknown fields */
380
    const IKsPropertySetVtbl   *lpVtbl;
381
    LONG 			ref;
382 383 384 385
    /* IKsPropertySetImpl fields */
    IDirectSoundBufferImpl*	dsb;
};

386
HRESULT IKsBufferPropertySetImpl_Create(
387 388
    IDirectSoundBufferImpl *dsb,
    IKsBufferPropertySetImpl **piks);
389
HRESULT IKsBufferPropertySetImpl_Destroy(
390
    IKsBufferPropertySetImpl *piks);
391 392 393 394 395 396 397

/*****************************************************************************
 *  IKsPrivatePropertySet implementation structure
 */
struct IKsPrivatePropertySetImpl
{
    /* IUnknown fields */
398
    const IKsPropertySetVtbl   *lpVtbl;
399
    LONG 			ref;
400 401
};

402
HRESULT IKsPrivatePropertySetImpl_Create(
403
    REFIID riid,
404
    IKsPrivatePropertySetImpl **piks);
405 406 407 408 409 410 411

/*****************************************************************************
 * IDirectSound3DBuffer implementation structure
 */
struct IDirectSound3DBufferImpl
{
    /* IUnknown fields */
412
    const IDirectSound3DBufferVtbl *lpVtbl;
413
    LONG                        ref;
414
    /* IDirectSound3DBufferImpl fields */
415
    IDirectSoundBufferImpl*     dsb;
416 417
};

418
HRESULT IDirectSound3DBufferImpl_Create(
419 420
    IDirectSoundBufferImpl *dsb,
    IDirectSound3DBufferImpl **pds3db);
421
HRESULT IDirectSound3DBufferImpl_Destroy(
422
    IDirectSound3DBufferImpl *pds3db);
423

424 425
/*******************************************************************************
 */
426

427 428
/* dsound.c */

429 430
HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS);
HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS);
431

432 433
/* primary.c */

434
DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign);
435 436 437 438 439
HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
440
HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced);
441
HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave);
442

443 444 445 446
/* duplex.c */
 
HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD);

447
/* mixer.c */
448
DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos);
449
void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len);
450 451 452
void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
453
void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen, BOOL inmixer);
454 455
DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot);

456
void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
457 458
void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);

459 460 461 462
/* sound3d.c */

void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);

463
/* capture.c */
464 465 466
 
HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC);
HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8);
467 468 469 470 471 472 473 474 475 476 477 478
HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
    LPDIRECTSOUNDCAPTURE iface,
    LPCDSCBUFFERDESC lpcDSCBufferDesc,
    LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
    LPUNKNOWN pUnk);
HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
    LPDIRECTSOUNDCAPTURE iface,
    LPDSCCAPS lpDSCCaps);
HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
    LPDIRECTSOUNDCAPTURE iface,
    LPCGUID lpcGUID);

479 480 481 482 483
#define STATE_STOPPED   0
#define STATE_STARTING  1
#define STATE_PLAYING   2
#define STATE_CAPTURING 2
#define STATE_STOPPING  3
484

485
#define DSOUND_FREQSHIFT (20)
486

487
extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
488
extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
489 490

extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
491
extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
492

493 494 495
HRESULT mmErr(UINT err);
void setup_dsound_options(void);
const char * dumpCooperativeLevel(DWORD level);