This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_dx5audio.c
517 lines (454 loc) · 15.5 KB
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
*/
21
#include "SDL_config.h"
22
23
24
25
/* Allow access to a raw mixing buffer */
#include "SDL_timer.h"
26
#include "SDL_loadso.h"
27
#include "SDL_audio.h"
28
#include "../SDL_audio_c.h"
29
30
#include "SDL_dx5audio.h"
31
32
33
34
35
36
37
/* !!! FIXME: move this somewhere that other drivers can use it... */
#if defined(_WIN32_WCE)
#define WINDOWS_OS_NAME "Windows CE/PocketPC"
#elif defined(WIN64)
#define WINDOWS_OS_NAME "Win64"
#else
#define WINDOWS_OS_NAME "Win32"
38
#endif
39
40
/* DirectX function pointers for audio */
41
static void* DSoundDLL = NULL;
42
43
static HRESULT(WINAPI * DSoundCreate) (LPGUID, LPDIRECTSOUND *, LPUNKNOWN) =
NULL;
44
45
static void
46
DSOUND_Unload(void)
47
{
48
49
DSoundCreate = NULL;
50
if (DSoundDLL != NULL) {
51
52
SDL_UnloadObject(DSoundDLL);
DSoundDLL = NULL;
53
}
54
55
56
}
57
58
static int
DSOUND_Load(void)
59
{
60
int loaded = 0;
61
62
DSOUND_Unload();
63
64
DSoundDLL = SDL_LoadObject("DSOUND.DLL");
65
66
67
68
69
if (DSoundDLL == NULL) {
SDL_SetError("DirectSound: failed to load DSOUND.DLL");
} else {
/* Now make sure we have DirectX 5 or better... */
/* (DirectSoundCaptureCreate was added in DX5) */
70
if (!SDL_LoadFunction(DSoundDLL, "DirectSoundCaptureCreate")) {
71
72
SDL_SetError("DirectSound: System doesn't appear to have DX5.");
} else {
73
DSoundCreate = SDL_LoadFunction(DSoundDLL, "DirectSoundCreate");
74
75
}
76
77
78
79
80
81
if (!DSoundCreate) {
SDL_SetError("DirectSound: Failed to find DirectSoundCreate");
} else {
loaded = 1;
}
}
82
83
84
85
if (!loaded) {
DSOUND_Unload();
}
86
87
return loaded;
88
89
90
}
91
92
static void
SetDSerror(const char *function, int code)
93
{
94
95
96
97
98
99
static const char *error;
static char errbuf[1024];
errbuf[0] = 0;
switch (code) {
case E_NOINTERFACE:
100
error = "Unsupported interface -- Is DirectX 5.0 or later installed?";
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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;
143
144
145
146
147
}
/* DirectSound needs to be associated with a window */
static HWND mainwin = NULL;
/* */
148
149
void
150
DSOUND_SoundFocus(HWND hwnd)
151
{
152
/* !!! FIXME: probably broken with multi-window support in SDL 1.3 ... */
153
mainwin = hwnd;
154
155
}
156
static void
157
DSOUND_ThreadInit(_THIS)
158
{
159
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
160
161
}
162
static void
163
DSOUND_WaitDevice(_THIS)
164
{
165
166
167
168
DWORD status = 0;
DWORD cursor = 0;
DWORD junk = 0;
HRESULT result = DS_OK;
169
170
171
172
/* Semi-busy wait, since we have no way of getting play notification
on a primary mixing buffer located in hardware (DirectX 5.0)
*/
173
174
result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf,
&junk, &cursor);
175
176
if (result != DS_OK) {
if (result == DSERR_BUFFERLOST) {
177
IDirectSoundBuffer_Restore(this->hidden->mixbuf);
178
}
179
#ifdef DEBUG_SOUND
180
SetDSerror("DirectSound GetCurrentPosition", result);
181
#endif
182
183
184
return;
}
185
while ((cursor / this->hidden->mixlen) == this->hidden->lastchunk) {
186
187
188
189
/* FIXME: find out how much time is left and sleep that long */
SDL_Delay(1);
/* Try to restore a lost sound buffer */
190
IDirectSoundBuffer_GetStatus(this->hidden->mixbuf, &status);
191
if ((status & DSBSTATUS_BUFFERLOST)) {
192
193
IDirectSoundBuffer_Restore(this->hidden->mixbuf);
IDirectSoundBuffer_GetStatus(this->hidden->mixbuf, &status);
194
195
196
197
198
if ((status & DSBSTATUS_BUFFERLOST)) {
break;
}
}
if (!(status & DSBSTATUS_PLAYING)) {
199
200
result = IDirectSoundBuffer_Play(this->hidden->mixbuf, 0, 0,
DSBPLAY_LOOPING);
201
202
203
if (result == DS_OK) {
continue;
}
204
#ifdef DEBUG_SOUND
205
SetDSerror("DirectSound Play", result);
206
#endif
207
208
209
210
return;
}
/* Find out where we are playing */
211
result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf,
212
213
214
215
216
217
&junk, &cursor);
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return;
}
}
218
219
}
220
static void
221
DSOUND_PlayDevice(_THIS)
222
{
223
/* Unlock the buffer, allowing it to play */
224
225
226
227
if (this->hidden->locked_buf) {
IDirectSoundBuffer_Unlock(this->hidden->mixbuf,
this->hidden->locked_buf,
this->hidden->mixlen, NULL, 0);
228
}
229
230
231
}
232
static Uint8 *
233
DSOUND_GetDeviceBuf(_THIS)
234
{
235
236
237
238
DWORD cursor = 0;
DWORD junk = 0;
HRESULT result = DS_OK;
DWORD rawlen = 0;
239
240
/* Figure out which blocks to fill next */
241
242
243
this->hidden->locked_buf = NULL;
result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf,
&junk, &cursor);
244
if (result == DSERR_BUFFERLOST) {
245
246
IDirectSoundBuffer_Restore(this->hidden->mixbuf);
result = IDirectSoundBuffer_GetCurrentPosition(this->hidden->mixbuf,
247
248
249
250
251
252
&junk, &cursor);
}
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return (NULL);
}
253
cursor /= this->hidden->mixlen;
254
#ifdef DEBUG_SOUND
255
256
257
/* Detect audio dropouts */
{
DWORD spot = cursor;
258
259
if (spot < this->hidden->lastchunk) {
spot += this->hidden->num_buffers;
260
}
261
if (spot > this->hidden->lastchunk + 1) {
262
fprintf(stderr, "Audio dropout, missed %d fragments\n",
263
(spot - (this->hidden->lastchunk + 1)));
264
265
}
}
266
#endif
267
268
269
this->hidden->lastchunk = cursor;
cursor = (cursor + 1) % this->hidden->num_buffers;
cursor *= this->hidden->mixlen;
270
271
/* Lock the audio buffer */
272
273
result = IDirectSoundBuffer_Lock(this->hidden->mixbuf, cursor,
this->hidden->mixlen,
274
(LPVOID *) & this->hidden->locked_buf,
275
&rawlen, NULL, &junk, 0);
276
if (result == DSERR_BUFFERLOST) {
277
278
279
IDirectSoundBuffer_Restore(this->hidden->mixbuf);
result = IDirectSoundBuffer_Lock(this->hidden->mixbuf, cursor,
this->hidden->mixlen,
280
281
282
(LPVOID *) & this->
hidden->locked_buf, &rawlen, NULL,
&junk, 0);
283
284
285
286
287
}
if (result != DS_OK) {
SetDSerror("DirectSound Lock", result);
return (NULL);
}
288
return (this->hidden->locked_buf);
289
290
}
291
static void
292
DSOUND_WaitDone(_THIS)
293
{
294
Uint8 *stream = DSOUND_GetDeviceBuf(this);
295
296
297
/* Wait for the playing chunk to finish */
if (stream != NULL) {
298
299
SDL_memset(stream, this->spec.silence, this->hidden->mixlen);
DSOUND_PlayDevice(this);
300
}
301
DSOUND_WaitDevice(this);
302
303
/* Stop the looping sound buffer */
304
IDirectSoundBuffer_Stop(this->hidden->mixbuf);
305
306
}
307
static void
308
DSOUND_CloseDevice(_THIS)
309
{
310
311
312
313
314
315
316
317
318
if (this->hidden != NULL) {
if (this->hidden->sound != NULL) {
if (this->hidden->mixbuf != NULL) {
/* Clean up the audio buffer */
IDirectSoundBuffer_Release(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
IDirectSound_Release(this->hidden->sound);
this->hidden->sound = NULL;
319
}
320
321
322
SDL_free(this->hidden);
this->hidden = NULL;
323
}
324
325
326
327
328
}
/* This function tries to create a secondary audio buffer, and returns the
number of audio chunks available in the created buffer.
*/
329
static int
330
CreateSecondary(_THIS, HWND focus, WAVEFORMATEX * wavefmt)
331
{
332
LPDIRECTSOUND sndObj = this->hidden->sound;
333
LPDIRECTSOUNDBUFFER *sndbuf = &this->hidden->mixbuf;
334
Uint32 chunksize = this->spec.size;
335
const int numchunks = 8;
336
HRESULT result = DS_OK;
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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) {
SetDSerror("DirectSound SetCooperativeLevel", result);
return (-1);
}
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* Try to create the secondary buffer */
SDL_memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);
format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
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) {
386
SDL_memset(pvAudioPtr1, this->spec.silence, dwAudioBytes1);
387
388
389
390
391
392
393
IDirectSoundBuffer_Unlock(*sndbuf,
(LPVOID) pvAudioPtr1, dwAudioBytes1,
(LPVOID) pvAudioPtr2, dwAudioBytes2);
}
/* We're ready to go */
return (numchunks);
394
395
}
396
static int
397
DSOUND_OpenDevice(_THIS, const char *devname, int iscapture)
398
{
399
HRESULT result;
400
401
402
WAVEFORMATEX waveformat;
int valid_format = 0;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
403
404
405
/* !!! FIXME: handle devname */
/* !!! FIXME: handle iscapture */
406
407
408
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
409
SDL_malloc((sizeof *this->hidden));
410
411
412
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
413
}
414
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
415
416
417
while ((!valid_format) && (test_format)) {
switch (test_format) {
418
419
420
421
422
423
case AUDIO_U8:
case AUDIO_S16:
case AUDIO_S32:
this->spec.format = test_format;
valid_format = 1;
break;
424
425
}
test_format = SDL_NextAudioFormat();
426
427
}
428
429
430
431
if (!valid_format) {
DSOUND_CloseDevice(this);
SDL_SetError("DirectSound: Unsupported audio format");
return 0;
432
}
433
434
435
SDL_memset(&waveformat, 0, sizeof(waveformat));
waveformat.wFormatTag = WAVE_FORMAT_PCM;
436
437
438
waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
waveformat.nChannels = this->spec.channels;
waveformat.nSamplesPerSec = this->spec.freq;
439
440
441
442
443
444
waveformat.nBlockAlign =
waveformat.nChannels * (waveformat.wBitsPerSample / 8);
waveformat.nAvgBytesPerSec =
waveformat.nSamplesPerSec * waveformat.nBlockAlign;
/* Update the fragment size as size in bytes */
445
SDL_CalculateAudioSpec(&this->spec);
446
447
/* Open the audio device */
448
result = DSoundCreate(NULL, &this->hidden->sound, NULL);
449
if (result != DS_OK) {
450
DSOUND_CloseDevice(this);
451
SetDSerror("DirectSoundCreate", result);
452
return 0;
453
454
455
}
/* Create the audio buffer to which we write */
456
457
458
459
this->hidden->num_buffers = CreateSecondary(this, mainwin, &waveformat);
if (this->hidden->num_buffers < 0) {
DSOUND_CloseDevice(this);
return 0;
460
}
461
462
463
464
/* The buffer will auto-start playing in DSOUND_WaitDevice() */
this->hidden->mixlen = this->spec.size;
465
return 1; /* good to go. */
466
467
468
469
470
471
472
473
474
475
476
}
static void
DSOUND_Deinitialize(void)
{
DSOUND_Unload();
}
static int
477
DSOUND_Init(SDL_AudioDriverImpl * impl)
478
479
480
481
482
483
484
485
{
OSVERSIONINFO ver;
/*
* 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.
*/
486
SDL_memset(&ver, '\0', sizeof(OSVERSIONINFO));
487
488
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&ver);
489
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
490
if (ver.dwMajorVersion <= 4) {
491
return 0; /* NT4.0 or earlier. Disable dsound support. */
492
493
}
}
494
495
496
if (!DSOUND_Load()) {
return 0;
497
}
498
499
500
501
502
503
504
505
506
507
/* Set the function pointers */
impl->OpenDevice = DSOUND_OpenDevice;
impl->PlayDevice = DSOUND_PlayDevice;
impl->WaitDevice = DSOUND_WaitDevice;
impl->WaitDone = DSOUND_WaitDone;
impl->ThreadInit = DSOUND_ThreadInit;
impl->GetDeviceBuf = DSOUND_GetDeviceBuf;
impl->CloseDevice = DSOUND_CloseDevice;
impl->Deinitialize = DSOUND_Deinitialize;
508
impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME */
509
510
return 1; /* this audio target is available. */
511
512
}
513
514
515
516
AudioBootStrap DSOUND_bootstrap = {
"dsound", WINDOWS_OS_NAME "DirectSound", DSOUND_Init, 0
};
517
/* vi: set ts=4 sw=4 expandtab: */