Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
733 lines (664 loc) · 20.9 KB

SDL_dx5audio.c

File metadata and controls

733 lines (664 loc) · 20.9 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
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
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
/* Allow access to a raw mixing buffer */
#include "SDL_timer.h"
#include "SDL_audio.h"
Feb 16, 2006
Feb 16, 2006
28
#include "../SDL_audio_c.h"
Apr 26, 2001
Apr 26, 2001
29
30
31
32
33
34
#include "SDL_dx5audio.h"
/* Define this if you want to use DirectX 6 DirectSoundNotify interface */
//#define USE_POSITION_NOTIFY
/* DirectX function pointers for audio */
Jul 10, 2006
Jul 10, 2006
35
HRESULT(WINAPI * DSoundCreate) (LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
Apr 26, 2001
Apr 26, 2001
36
37
/* Audio driver functions */
Jul 10, 2006
Jul 10, 2006
38
static int DX5_OpenAudio(_THIS, SDL_AudioSpec * spec);
Apr 26, 2001
Apr 26, 2001
39
40
41
42
43
44
45
46
47
48
49
50
static void DX5_ThreadInit(_THIS);
static void DX5_WaitAudio_BusyWait(_THIS);
#ifdef USE_POSITION_NOTIFY
static void DX6_WaitAudio_EventWait(_THIS);
#endif
static void DX5_PlayAudio(_THIS);
static Uint8 *DX5_GetAudioBuf(_THIS);
static void DX5_WaitDone(_THIS);
static void DX5_CloseAudio(_THIS);
/* Audio driver bootstrap functions */
Jul 10, 2006
Jul 10, 2006
51
52
static int
Audio_Available(void)
Apr 26, 2001
Apr 26, 2001
53
{
Jul 10, 2006
Jul 10, 2006
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
HINSTANCE DSoundDLL;
int dsound_ok;
/* Version check DSOUND.DLL (Is DirectX okay?) */
dsound_ok = 0;
DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
if (DSoundDLL != NULL) {
/* We just use basic DirectSound, we're okay */
/* Yay! */
/* Unfortunately, the sound drivers on NT have
higher latencies than the audio buffers used
by many SDL applications, so there are gaps
in the audio - it sounds terrible. Punt for now.
*/
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&ver);
switch (ver.dwPlatformId) {
case VER_PLATFORM_WIN32_NT:
if (ver.dwMajorVersion > 4) {
/* Win2K */
dsound_ok = 1;
} else {
/* WinNT */
dsound_ok = 0;
}
break;
default:
/* Win95 or Win98 */
dsound_ok = 1;
break;
}
/* Now check for DirectX 5 or better - otherwise
* we will fail later in DX5_OpenAudio without a chance
* to fall back to the DIB driver. */
if (dsound_ok) {
/* DirectSoundCaptureCreate was added in DX5 */
if (!GetProcAddress(DSoundDLL, TEXT("DirectSoundCaptureCreate")))
dsound_ok = 0;
}
/* Clean up.. */
FreeLibrary(DSoundDLL);
}
return (dsound_ok);
Apr 26, 2001
Apr 26, 2001
99
100
101
102
103
}
/* Functions for loading the DirectX functions dynamically */
static HINSTANCE DSoundDLL = NULL;
Jul 10, 2006
Jul 10, 2006
104
105
static void
DX5_Unload(void)
Apr 26, 2001
Apr 26, 2001
106
{
Jul 10, 2006
Jul 10, 2006
107
108
109
110
111
if (DSoundDLL != NULL) {
FreeLibrary(DSoundDLL);
DSoundCreate = NULL;
DSoundDLL = NULL;
}
Apr 26, 2001
Apr 26, 2001
112
}
Jul 10, 2006
Jul 10, 2006
113
114
static int
DX5_Load(void)
Apr 26, 2001
Apr 26, 2001
115
{
Jul 10, 2006
Jul 10, 2006
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
int status;
DX5_Unload();
DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
if (DSoundDLL != NULL) {
DSoundCreate = (void *) GetProcAddress(DSoundDLL,
TEXT("DirectSoundCreate"));
}
if (DSoundDLL && DSoundCreate) {
status = 0;
} else {
DX5_Unload();
status = -1;
}
return status;
Apr 26, 2001
Apr 26, 2001
131
132
}
Jul 10, 2006
Jul 10, 2006
133
134
static void
Audio_DeleteDevice(SDL_AudioDevice * device)
Apr 26, 2001
Apr 26, 2001
135
{
Jul 10, 2006
Jul 10, 2006
136
137
138
DX5_Unload();
SDL_free(device->hidden);
SDL_free(device);
Apr 26, 2001
Apr 26, 2001
139
140
}
Jul 10, 2006
Jul 10, 2006
141
142
static SDL_AudioDevice *
Audio_CreateDevice(int devindex)
Apr 26, 2001
Apr 26, 2001
143
{
Jul 10, 2006
Jul 10, 2006
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
SDL_AudioDevice *this;
/* Load DirectX */
if (DX5_Load() < 0) {
return (NULL);
}
/* Initialize all variables that we clean on shutdown */
this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
if (this) {
SDL_memset(this, 0, (sizeof *this));
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
}
if ((this == NULL) || (this->hidden == NULL)) {
SDL_OutOfMemory();
if (this) {
SDL_free(this);
}
return (0);
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
/* Set the function pointers */
this->OpenAudio = DX5_OpenAudio;
this->ThreadInit = DX5_ThreadInit;
this->WaitAudio = DX5_WaitAudio_BusyWait;
this->PlayAudio = DX5_PlayAudio;
this->GetAudioBuf = DX5_GetAudioBuf;
this->WaitDone = DX5_WaitDone;
this->CloseAudio = DX5_CloseAudio;
this->free = Audio_DeleteDevice;
return this;
Apr 26, 2001
Apr 26, 2001
179
180
181
}
AudioBootStrap DSOUND_bootstrap = {
Jul 10, 2006
Jul 10, 2006
182
183
"dsound", "Win95/98/2000 DirectSound",
Audio_Available, Audio_CreateDevice
Apr 26, 2001
Apr 26, 2001
184
185
};
Jul 10, 2006
Jul 10, 2006
186
187
static void
SetDSerror(const char *function, int code)
Apr 26, 2001
Apr 26, 2001
188
{
Jul 10, 2006
Jul 10, 2006
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
static const char *error;
static char errbuf[1024];
errbuf[0] = 0;
switch (code) {
case E_NOINTERFACE:
error =
"Unsupported interface\n-- Is DirectX 5.0 or later installed?";
break;
case DSERR_ALLOCATED:
error = "Audio device in use";
break;
case DSERR_BADFORMAT:
error = "Unsupported audio format";
break;
case DSERR_BUFFERLOST:
error = "Mixing buffer was lost";
break;
case DSERR_CONTROLUNAVAIL:
error = "Control requested is not available";
break;
case DSERR_INVALIDCALL:
error = "Invalid call for the current state";
break;
case DSERR_INVALIDPARAM:
error = "Invalid parameter";
break;
case DSERR_NODRIVER:
error = "No audio device found";
break;
case DSERR_OUTOFMEMORY:
error = "Out of memory";
break;
case DSERR_PRIOLEVELNEEDED:
error = "Caller doesn't have priority";
break;
case DSERR_UNSUPPORTED:
error = "Function not supported";
break;
default:
SDL_snprintf(errbuf, SDL_arraysize(errbuf),
"%s: Unknown DirectSound error: 0x%x", function, code);
break;
}
if (!errbuf[0]) {
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
error);
}
SDL_SetError("%s", errbuf);
return;
Apr 26, 2001
Apr 26, 2001
239
240
241
242
243
}
/* DirectSound needs to be associated with a window */
static HWND mainwin = NULL;
/* */
Jul 10, 2006
Jul 10, 2006
244
245
void
DX5_SoundFocus(HWND hwnd)
Apr 26, 2001
Apr 26, 2001
246
{
Jul 10, 2006
Jul 10, 2006
247
mainwin = hwnd;
Apr 26, 2001
Apr 26, 2001
248
249
}
Jul 10, 2006
Jul 10, 2006
250
251
static void
DX5_ThreadInit(_THIS)
Apr 26, 2001
Apr 26, 2001
252
{
Jul 10, 2006
Jul 10, 2006
253
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
Apr 26, 2001
Apr 26, 2001
254
255
}
Jul 10, 2006
Jul 10, 2006
256
257
static void
DX5_WaitAudio_BusyWait(_THIS)
Apr 26, 2001
Apr 26, 2001
258
{
Jul 10, 2006
Jul 10, 2006
259
260
261
262
263
264
265
266
267
268
269
270
DWORD status;
DWORD cursor, junk;
HRESULT result;
/* Semi-busy wait, since we have no way of getting play notification
on a primary mixing buffer located in hardware (DirectX 5.0)
*/
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
if (result != DS_OK) {
if (result == DSERR_BUFFERLOST) {
IDirectSoundBuffer_Restore(mixbuf);
}
Apr 26, 2001
Apr 26, 2001
271
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
272
SetDSerror("DirectSound GetCurrentPosition", result);
Apr 26, 2001
Apr 26, 2001
273
#endif
Jul 10, 2006
Jul 10, 2006
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
return;
}
while ((cursor / mixlen) == lastchunk) {
/* FIXME: find out how much time is left and sleep that long */
SDL_Delay(1);
/* Try to restore a lost sound buffer */
IDirectSoundBuffer_GetStatus(mixbuf, &status);
if ((status & DSBSTATUS_BUFFERLOST)) {
IDirectSoundBuffer_Restore(mixbuf);
IDirectSoundBuffer_GetStatus(mixbuf, &status);
if ((status & DSBSTATUS_BUFFERLOST)) {
break;
}
}
if (!(status & DSBSTATUS_PLAYING)) {
result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
if (result == DS_OK) {
continue;
}
Apr 26, 2001
Apr 26, 2001
295
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
296
SetDSerror("DirectSound Play", result);
Apr 26, 2001
Apr 26, 2001
297
#endif
Jul 10, 2006
Jul 10, 2006
298
299
300
301
302
303
304
305
306
307
308
return;
}
/* Find out where we are playing */
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
&junk, &cursor);
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return;
}
}
Apr 26, 2001
Apr 26, 2001
309
310
311
}
#ifdef USE_POSITION_NOTIFY
Jul 10, 2006
Jul 10, 2006
312
313
static void
DX6_WaitAudio_EventWait(_THIS)
Apr 26, 2001
Apr 26, 2001
314
{
Jul 10, 2006
Jul 10, 2006
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
DWORD status;
HRESULT result;
/* Try to restore a lost sound buffer */
IDirectSoundBuffer_GetStatus(mixbuf, &status);
if ((status & DSBSTATUS_BUFFERLOST)) {
IDirectSoundBuffer_Restore(mixbuf);
IDirectSoundBuffer_GetStatus(mixbuf, &status);
if ((status & DSBSTATUS_BUFFERLOST)) {
return;
}
}
if (!(status & DSBSTATUS_PLAYING)) {
result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
330
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
331
SetDSerror("DirectSound Play", result);
Apr 26, 2001
Apr 26, 2001
332
#endif
Jul 10, 2006
Jul 10, 2006
333
334
335
336
return;
}
}
WaitForSingleObject(audio_event, INFINITE);
Apr 26, 2001
Apr 26, 2001
337
338
339
}
#endif /* USE_POSITION_NOTIFY */
Jul 10, 2006
Jul 10, 2006
340
341
static void
DX5_PlayAudio(_THIS)
Apr 26, 2001
Apr 26, 2001
342
{
Jul 10, 2006
Jul 10, 2006
343
344
345
346
/* Unlock the buffer, allowing it to play */
if (locked_buf) {
IDirectSoundBuffer_Unlock(mixbuf, locked_buf, mixlen, NULL, 0);
}
Apr 26, 2001
Apr 26, 2001
347
348
349
}
Jul 10, 2006
Jul 10, 2006
350
351
static Uint8 *
DX5_GetAudioBuf(_THIS)
Apr 26, 2001
Apr 26, 2001
352
{
Jul 10, 2006
Jul 10, 2006
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
DWORD cursor, junk;
HRESULT result;
DWORD rawlen;
/* Figure out which blocks to fill next */
locked_buf = NULL;
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
if (result == DSERR_BUFFERLOST) {
IDirectSoundBuffer_Restore(mixbuf);
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
&junk, &cursor);
}
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return (NULL);
}
cursor /= mixlen;
Jun 23, 2006
Jun 23, 2006
370
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
371
372
373
374
375
376
377
378
379
380
381
/* Detect audio dropouts */
{
DWORD spot = cursor;
if (spot < lastchunk) {
spot += NUM_BUFFERS;
}
if (spot > lastchunk + 1) {
fprintf(stderr, "Audio dropout, missed %d fragments\n",
(spot - (lastchunk + 1)));
}
}
Jun 23, 2006
Jun 23, 2006
382
#endif
Jul 10, 2006
Jul 10, 2006
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
lastchunk = cursor;
cursor = (cursor + 1) % NUM_BUFFERS;
cursor *= mixlen;
/* Lock the audio buffer */
result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
(LPVOID *) & locked_buf, &rawlen, NULL,
&junk, 0);
if (result == DSERR_BUFFERLOST) {
IDirectSoundBuffer_Restore(mixbuf);
result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
(LPVOID *) & locked_buf, &rawlen,
NULL, &junk, 0);
}
if (result != DS_OK) {
SetDSerror("DirectSound Lock", result);
return (NULL);
}
return (locked_buf);
Apr 26, 2001
Apr 26, 2001
402
403
}
Jul 10, 2006
Jul 10, 2006
404
405
static void
DX5_WaitDone(_THIS)
Apr 26, 2001
Apr 26, 2001
406
{
Jul 10, 2006
Jul 10, 2006
407
408
409
410
411
412
413
414
415
416
417
418
Uint8 *stream;
/* Wait for the playing chunk to finish */
stream = this->GetAudioBuf(this);
if (stream != NULL) {
SDL_memset(stream, silence, mixlen);
this->PlayAudio(this);
}
this->WaitAudio(this);
/* Stop the looping sound buffer */
IDirectSoundBuffer_Stop(mixbuf);
Apr 26, 2001
Apr 26, 2001
419
420
}
Jul 10, 2006
Jul 10, 2006
421
422
static void
DX5_CloseAudio(_THIS)
Apr 26, 2001
Apr 26, 2001
423
{
Jul 10, 2006
Jul 10, 2006
424
425
426
427
428
429
430
431
432
433
434
435
436
if (sound != NULL) {
if (mixbuf != NULL) {
/* Clean up the audio buffer */
IDirectSoundBuffer_Release(mixbuf);
mixbuf = NULL;
}
if (audio_event != NULL) {
CloseHandle(audio_event);
audio_event = NULL;
}
IDirectSound_Release(sound);
sound = NULL;
}
Apr 26, 2001
Apr 26, 2001
437
438
}
Aug 24, 2002
Aug 24, 2002
439
#ifdef USE_PRIMARY_BUFFER
Apr 26, 2001
Apr 26, 2001
440
441
442
/* This function tries to create a primary audio buffer, and returns the
number of audio chunks available in the created buffer.
*/
Jul 10, 2006
Jul 10, 2006
443
444
445
446
static int
CreatePrimary(LPDIRECTSOUND sndObj, HWND focus,
LPDIRECTSOUNDBUFFER * sndbuf, WAVEFORMATEX * wavefmt,
Uint32 chunksize)
Apr 26, 2001
Apr 26, 2001
447
{
Jul 10, 2006
Jul 10, 2006
448
449
450
451
452
453
454
455
456
HRESULT result;
DSBUFFERDESC format;
DSBCAPS caps;
int numchunks;
/* Try to set primary mixing privileges */
result = IDirectSound_SetCooperativeLevel(sndObj, focus,
DSSCL_WRITEPRIMARY);
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
457
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
458
SetDSerror("DirectSound SetCooperativeLevel", result);
Apr 26, 2001
Apr 26, 2001
459
#endif
Jul 10, 2006
Jul 10, 2006
460
461
462
463
464
465
466
467
return (-1);
}
/* Try to create the primary buffer */
SDL_memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);
format.dwFlags = (DSBCAPS_PRIMARYBUFFER | DSBCAPS_GETCURRENTPOSITION2);
format.dwFlags |= DSBCAPS_STICKYFOCUS;
Apr 26, 2001
Apr 26, 2001
468
#ifdef USE_POSITION_NOTIFY
Jul 10, 2006
Jul 10, 2006
469
format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
Apr 26, 2001
Apr 26, 2001
470
#endif
Jul 10, 2006
Jul 10, 2006
471
472
result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
473
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
474
SetDSerror("DirectSound CreateSoundBuffer", result);
Apr 26, 2001
Apr 26, 2001
475
#endif
Jul 10, 2006
Jul 10, 2006
476
477
478
479
480
481
482
483
return (-1);
}
/* Check the size of the fragment buffer */
SDL_memset(&caps, 0, sizeof(caps));
caps.dwSize = sizeof(caps);
result = IDirectSoundBuffer_GetCaps(*sndbuf, &caps);
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
484
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
485
SetDSerror("DirectSound GetCaps", result);
Apr 26, 2001
Apr 26, 2001
486
#endif
Jul 10, 2006
Jul 10, 2006
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
IDirectSoundBuffer_Release(*sndbuf);
return (-1);
}
if ((chunksize > caps.dwBufferBytes) ||
((caps.dwBufferBytes % chunksize) != 0)) {
/* The primary buffer size is not a multiple of 'chunksize'
-- this hopefully doesn't happen when 'chunksize' is a
power of 2.
*/
IDirectSoundBuffer_Release(*sndbuf);
SDL_SetError
("Primary buffer size is: %d, cannot break it into chunks of %d bytes\n",
caps.dwBufferBytes, chunksize);
return (-1);
}
numchunks = (caps.dwBufferBytes / chunksize);
/* Set the primary audio format */
result = IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
507
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
508
SetDSerror("DirectSound SetFormat", result);
Apr 26, 2001
Apr 26, 2001
509
#endif
Jul 10, 2006
Jul 10, 2006
510
511
512
513
IDirectSoundBuffer_Release(*sndbuf);
return (-1);
}
return (numchunks);
Apr 26, 2001
Apr 26, 2001
514
}
Aug 24, 2002
Aug 24, 2002
515
#endif /* USE_PRIMARY_BUFFER */
Apr 26, 2001
Apr 26, 2001
516
517
518
519
/* This function tries to create a secondary audio buffer, and returns the
number of audio chunks available in the created buffer.
*/
Jul 10, 2006
Jul 10, 2006
520
521
522
523
static int
CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
LPDIRECTSOUNDBUFFER * sndbuf, WAVEFORMATEX * wavefmt,
Uint32 chunksize)
Apr 26, 2001
Apr 26, 2001
524
{
Jul 10, 2006
Jul 10, 2006
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
const int numchunks = 8;
HRESULT result;
DSBUFFERDESC format;
LPVOID pvAudioPtr1, pvAudioPtr2;
DWORD dwAudioBytes1, dwAudioBytes2;
/* Try to set primary mixing privileges */
if (focus) {
result = IDirectSound_SetCooperativeLevel(sndObj,
focus, DSSCL_PRIORITY);
} else {
result = IDirectSound_SetCooperativeLevel(sndObj,
GetDesktopWindow(),
DSSCL_NORMAL);
}
if (result != DS_OK) {
Apr 26, 2001
Apr 26, 2001
541
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
542
SetDSerror("DirectSound SetCooperativeLevel", result);
Apr 26, 2001
Apr 26, 2001
543
#endif
Jul 10, 2006
Jul 10, 2006
544
545
return (-1);
}
Apr 26, 2001
Apr 26, 2001
546
Jul 10, 2006
Jul 10, 2006
547
548
549
550
/* Try to create the secondary buffer */
SDL_memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);
format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
Apr 26, 2001
Apr 26, 2001
551
#ifdef USE_POSITION_NOTIFY
Jul 10, 2006
Jul 10, 2006
552
format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
Apr 26, 2001
Apr 26, 2001
553
#endif
Jul 10, 2006
Jul 10, 2006
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
if (!focus) {
format.dwFlags |= DSBCAPS_GLOBALFOCUS;
} else {
format.dwFlags |= DSBCAPS_STICKYFOCUS;
}
format.dwBufferBytes = numchunks * chunksize;
if ((format.dwBufferBytes < DSBSIZE_MIN) ||
(format.dwBufferBytes > DSBSIZE_MAX)) {
SDL_SetError("Sound buffer size must be between %d and %d",
DSBSIZE_MIN / numchunks, DSBSIZE_MAX / numchunks);
return (-1);
}
format.dwReserved = 0;
format.lpwfxFormat = wavefmt;
result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
if (result != DS_OK) {
SetDSerror("DirectSound CreateSoundBuffer", result);
return (-1);
}
IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
/* Silence the initial audio buffer */
result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes,
(LPVOID *) & pvAudioPtr1, &dwAudioBytes1,
(LPVOID *) & pvAudioPtr2, &dwAudioBytes2,
DSBLOCK_ENTIREBUFFER);
if (result == DS_OK) {
if (wavefmt->wBitsPerSample == 8) {
SDL_memset(pvAudioPtr1, 0x80, dwAudioBytes1);
} else {
SDL_memset(pvAudioPtr1, 0x00, dwAudioBytes1);
}
IDirectSoundBuffer_Unlock(*sndbuf,
(LPVOID) pvAudioPtr1, dwAudioBytes1,
(LPVOID) pvAudioPtr2, dwAudioBytes2);
}
/* We're ready to go */
return (numchunks);
Apr 26, 2001
Apr 26, 2001
593
594
595
596
}
/* This function tries to set position notify events on the mixing buffer */
#ifdef USE_POSITION_NOTIFY
Jul 10, 2006
Jul 10, 2006
597
598
static int
CreateAudioEvent(_THIS)
Apr 26, 2001
Apr 26, 2001
599
{
Jul 10, 2006
Jul 10, 2006
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
LPDIRECTSOUNDNOTIFY notify;
DSBPOSITIONNOTIFY *notify_positions;
int i, retval;
HRESULT result;
/* Default to fail on exit */
retval = -1;
notify = NULL;
/* Query for the interface */
result = IDirectSoundBuffer_QueryInterface(mixbuf,
&IID_IDirectSoundNotify,
(void *) &notify);
if (result != DS_OK) {
goto done;
}
/* Allocate the notify structures */
notify_positions = (DSBPOSITIONNOTIFY *) SDL_malloc(NUM_BUFFERS *
sizeof
(*notify_positions));
if (notify_positions == NULL) {
goto done;
}
/* Create the notify event */
audio_event = CreateEvent(NULL, FALSE, FALSE, NULL);
if (audio_event == NULL) {
goto done;
}
/* Set up the notify structures */
for (i = 0; i < NUM_BUFFERS; ++i) {
notify_positions[i].dwOffset = i * mixlen;
notify_positions[i].hEventNotify = audio_event;
}
result = IDirectSoundNotify_SetNotificationPositions(notify,
NUM_BUFFERS,
notify_positions);
if (result == DS_OK) {
retval = 0;
}
done:
if (notify != NULL) {
IDirectSoundNotify_Release(notify);
}
return (retval);
Apr 26, 2001
Apr 26, 2001
647
648
649
}
#endif /* USE_POSITION_NOTIFY */
Jul 10, 2006
Jul 10, 2006
650
651
static int
DX5_OpenAudio(_THIS, SDL_AudioSpec * spec)
Apr 26, 2001
Apr 26, 2001
652
{
Jul 10, 2006
Jul 10, 2006
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
HRESULT result;
WAVEFORMATEX waveformat;
/* Set basic WAVE format parameters */
SDL_memset(&waveformat, 0, sizeof(waveformat));
waveformat.wFormatTag = WAVE_FORMAT_PCM;
/* Determine the audio parameters from the AudioSpec */
switch (spec->format & 0xFF) {
case 8:
/* Unsigned 8 bit audio data */
spec->format = AUDIO_U8;
silence = 0x80;
waveformat.wBitsPerSample = 8;
break;
case 16:
/* Signed 16 bit audio data */
spec->format = AUDIO_S16;
silence = 0x00;
waveformat.wBitsPerSample = 16;
break;
default:
SDL_SetError("Unsupported audio format");
return (-1);
}
waveformat.nChannels = spec->channels;
waveformat.nSamplesPerSec = spec->freq;
waveformat.nBlockAlign =
waveformat.nChannels * (waveformat.wBitsPerSample / 8);
waveformat.nAvgBytesPerSec =
waveformat.nSamplesPerSec * waveformat.nBlockAlign;
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(spec);
/* Open the audio device */
result = DSoundCreate(NULL, &sound, NULL);
if (result != DS_OK) {
SetDSerror("DirectSoundCreate", result);
return (-1);
}
/* Create the audio buffer to which we write */
NUM_BUFFERS = -1;
May 19, 2002
May 19, 2002
697
#ifdef USE_PRIMARY_BUFFER
Jul 10, 2006
Jul 10, 2006
698
699
700
701
if (mainwin) {
NUM_BUFFERS = CreatePrimary(sound, mainwin, &mixbuf,
&waveformat, spec->size);
}
May 19, 2002
May 19, 2002
702
#endif /* USE_PRIMARY_BUFFER */
Jul 10, 2006
Jul 10, 2006
703
704
705
706
707
708
if (NUM_BUFFERS < 0) {
NUM_BUFFERS = CreateSecondary(sound, mainwin, &mixbuf,
&waveformat, spec->size);
if (NUM_BUFFERS < 0) {
return (-1);
}
Apr 26, 2001
Apr 26, 2001
709
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
710
fprintf(stderr, "Using secondary audio buffer\n");
Apr 26, 2001
Apr 26, 2001
711
#endif
Jul 10, 2006
Jul 10, 2006
712
}
Apr 26, 2001
Apr 26, 2001
713
#ifdef DEBUG_SOUND
Jul 10, 2006
Jul 10, 2006
714
715
else
fprintf(stderr, "Using primary audio buffer\n");
Apr 26, 2001
Apr 26, 2001
716
717
#endif
Jul 10, 2006
Jul 10, 2006
718
719
720
/* The buffer will auto-start playing in DX5_WaitAudio() */
lastchunk = 0;
mixlen = spec->size;
Apr 26, 2001
Apr 26, 2001
721
722
#ifdef USE_POSITION_NOTIFY
Jul 10, 2006
Jul 10, 2006
723
724
725
726
727
728
/* See if we can use DirectX 6 event notification */
if (CreateAudioEvent(this) == 0) {
this->WaitAudio = DX6_WaitAudio_EventWait;
} else {
this->WaitAudio = DX5_WaitAudio_BusyWait;
}
Apr 26, 2001
Apr 26, 2001
729
#endif
Jul 10, 2006
Jul 10, 2006
730
return (0);
Apr 26, 2001
Apr 26, 2001
731
732
}
Jul 10, 2006
Jul 10, 2006
733
/* vi: set ts=4 sw=4 expandtab: */