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

Latest commit

 

History

History
577 lines (505 loc) · 18.8 KB

SDL_xaudio2.c

File metadata and controls

577 lines (505 loc) · 18.8 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
*/
Nov 23, 2012
Nov 23, 2012
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* WinRT NOTICE:
A number of changes were warranted to SDL's XAudio2 backend in order to
get it compiling for Windows RT.
When compiling for WinRT, XAudio2.h requires that it be compiled in a C++
file, and not a straight C file. Trying to compile it as C leads to lots
of errors, at least with MSVC 2012 and Windows SDK 8.0, as of Nov 22, 2012.
To address this specific issue, a few changes were made to SDL_xaudio2.c:
1. SDL_xaudio2.c is compiled as a C++ file in WinRT builds. Exported
symbols, namely XAUDIO2_bootstrap, uses 'extern "C"' to make sure the
rest of SDL can access it. Non-WinRT builds continue to compile
SDL_xaudio2.c as a C file.
2. A macro redefines variables named 'this' to '_this', to prevent compiler
errors (C2355 in Visual C++) related to 'this' being a reserverd keyword.
This hack may need to be altered in the future, particularly if C++'s
'this' keyword needs to be used (within SDL_xaudio2.c). At the time
WinRT support was initially added to SDL's XAudio2 backend, this
capability was not needed.
3. The C-style macros to invoke XAudio2's COM-based methods were
rewritten to be C++-friendly. These are provided in the file,
SDL_xaudio2_winrthelpers.h.
4. IXAudio2::CreateSourceVoice, when used in C++, requires its callbacks to
be specified via a C++ class. SDL's XAudio2 backend was written with
C-style callbacks. A class to bridge these two interfaces,
SDL_XAudio2VoiceCallback, was written to make XAudio2 happy. Its methods
just call SDL's existing, C-style callbacks.
5. Multiple checks for the __cplusplus macro were made, in appropriate
places.
A few additional changes to SDL's XAudio2 backend were warranted by API
changes to Windows. Many, but not all of these are documented by Microsoft
at:
http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx
1. Windows' thread synchronization function, CreateSemaphore, was removed
from Windows RT. SDL's semaphore API was substituted instead.
2. The method calls, IXAudio2::GetDeviceCount and IXAudio2::GetDeviceDetails
were removed from the XAudio2 API. Microsoft is telling developers to
use APIs in Windows::Foundation instead.
For SDL, the missing methods were reimplemented using the APIs Microsoft
said to use.
3. CoInitialize and CoUninitialize are not available in Windows RT.
These calls were removed, as COM will have been initialized earlier,
at least by the call to the WinRT app's main function
(aka 'int main(Platform::Array<Platform::String^>^)). (DLudwig:
This was my understanding of how WinRT: the 'main' function uses
a tag of [MTAThread], which should initialize COM. My understanding
of COM is somewhat limited, and I may be incorrect here.)
4. IXAudio2::CreateMasteringVoice changed its integer-based 'DeviceIndex'
argument to a string-based one, 'szDeviceId'. In Windows RT, the
string-based argument will be used.
*/
78
79
80
81
#include "SDL_config.h"
#if SDL_AUDIO_DRIVER_XAUDIO2
Nov 23, 2012
Nov 23, 2012
82
83
84
#ifdef __cplusplus
extern "C" {
#endif
85
86
87
88
89
#include "../../core/windows/SDL_windows.h"
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "../SDL_sysaudio.h"
#include "SDL_assert.h"
Nov 23, 2012
Nov 23, 2012
90
91
92
#ifdef __cplusplus
}
#endif
Nov 23, 2012
Nov 23, 2012
94
95
96
97
#if defined(__WINRT__)
# define SDL_XAUDIO2_HAS_SDK 1
#endif
#if defined(__WIN32__)
98
99
100
101
102
103
#include <dxsdkver.h> /* XAudio2 exists as of the March 2008 DirectX SDK */
#if (!defined(_DXSDK_BUILD_MAJOR) || (_DXSDK_BUILD_MAJOR < 1284))
# pragma message("Your DirectX SDK is too old. Disabling XAudio2 support.")
#else
# define SDL_XAUDIO2_HAS_SDK 1
#endif
Nov 23, 2012
Nov 23, 2012
104
#endif
105
106
107
108
109
110
111
112
113
#ifdef SDL_XAUDIO2_HAS_SDK
#define INITGUID 1
#include <XAudio2.h>
/* Hidden "this" pointer for the audio functions */
#define _THIS SDL_AudioDevice *this
Nov 23, 2012
Nov 23, 2012
114
115
116
117
118
#ifdef __cplusplus
#define this _this
#include "SDL_xaudio2_winrthelpers.h"
#endif
119
120
121
122
123
struct SDL_PrivateAudioData
{
IXAudio2 *ixa2;
IXAudio2SourceVoice *source;
IXAudio2MasteringVoice *mastering;
Nov 23, 2012
Nov 23, 2012
124
SDL_sem * semaphore;
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Uint8 *mixbuf;
int mixlen;
Uint8 *nextbuf;
};
static void
XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
{
IXAudio2 *ixa2 = NULL;
UINT32 devcount = 0;
UINT32 i = 0;
void *ptr = NULL;
if (iscapture) {
SDL_SetError("XAudio2: capture devices unsupported.");
return;
} else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
SDL_SetError("XAudio2: XAudio2Create() failed.");
return;
} else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
IXAudio2_Release(ixa2);
return;
}
for (i = 0; i < devcount; i++) {
XAUDIO2_DEVICE_DETAILS details;
if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
Nov 24, 2012
Nov 24, 2012
154
char *str = WIN_StringToUTF8(details.DisplayName);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
if (str != NULL) {
addfn(str);
SDL_free(str); /* addfn() made a copy of the string. */
}
}
}
IXAudio2_Release(ixa2);
}
static void STDMETHODCALLTYPE
VoiceCBOnBufferEnd(THIS_ void *data)
{
/* Just signal the SDL audio thread and get out of XAudio2's way. */
SDL_AudioDevice *this = (SDL_AudioDevice *) data;
Nov 23, 2012
Nov 23, 2012
170
SDL_SemPost(this->hidden->semaphore);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
}
static void STDMETHODCALLTYPE
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
{
/* !!! FIXME: attempt to recover, or mark device disconnected. */
SDL_assert(0 && "write me!");
}
/* no-op callbacks... */
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassStart(THIS_ UINT32 b) {}
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
static void STDMETHODCALLTYPE VoiceCBOnBufferStart(THIS_ void *data) {}
static void STDMETHODCALLTYPE VoiceCBOnLoopEnd(THIS_ void *data) {}
Nov 23, 2012
Nov 23, 2012
187
188
189
190
191
192
193
194
#if defined(__cplusplus)
class SDL_XAudio2VoiceCallback : public IXAudio2VoiceCallback
{
public:
STDMETHOD_(void, OnBufferEnd)(void *pBufferContext) {
VoiceCBOnBufferEnd(pBufferContext);
}
STDMETHOD_(void, OnBufferStart)(void *pBufferContext) {
Dec 30, 2012
Dec 30, 2012
195
VoiceCBOnBufferStart(pBufferContext);
Nov 23, 2012
Nov 23, 2012
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}
STDMETHOD_(void, OnLoopEnd)(void *pBufferContext) {
VoiceCBOnLoopEnd(pBufferContext);
}
STDMETHOD_(void, OnStreamEnd)() {
VoiceCBOnStreamEnd();
}
STDMETHOD_(void, OnVoiceError)(void *pBufferContext, HRESULT Error) {
VoiceCBOnVoiceError(pBufferContext, Error);
}
STDMETHOD_(void, OnVoiceProcessingPassEnd)() {
VoiceCBOnVoiceProcessPassEnd();
}
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32 BytesRequired) {
VoiceCBOnVoiceProcessPassStart(BytesRequired);
}
};
#endif
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
static Uint8 *
XAUDIO2_GetDeviceBuf(_THIS)
{
return this->hidden->nextbuf;
}
static void
XAUDIO2_PlayDevice(_THIS)
{
XAUDIO2_BUFFER buffer;
Uint8 *mixbuf = this->hidden->mixbuf;
Uint8 *nextbuf = this->hidden->nextbuf;
const int mixlen = this->hidden->mixlen;
IXAudio2SourceVoice *source = this->hidden->source;
HRESULT result = S_OK;
if (!this->enabled) { /* shutting down? */
return;
}
/* Submit the next filled buffer */
SDL_zero(buffer);
buffer.AudioBytes = mixlen;
buffer.pAudioData = nextbuf;
buffer.pContext = this;
if (nextbuf == mixbuf) {
nextbuf += mixlen;
} else {
nextbuf = mixbuf;
}
this->hidden->nextbuf = nextbuf;
result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
/* !!! FIXME: possibly disconnected or temporary lost. Recover? */
}
if (result != S_OK) { /* uhoh, panic! */
IXAudio2SourceVoice_FlushSourceBuffers(source);
this->enabled = 0;
}
}
static void
XAUDIO2_WaitDevice(_THIS)
{
if (this->enabled) {
Nov 23, 2012
Nov 23, 2012
263
SDL_SemWait(this->hidden->semaphore);
264
265
266
267
268
269
270
271
272
273
274
275
}
}
static void
XAUDIO2_WaitDone(_THIS)
{
IXAudio2SourceVoice *source = this->hidden->source;
XAUDIO2_VOICE_STATE state;
SDL_assert(!this->enabled); /* flag that stops playing. */
IXAudio2SourceVoice_Discontinuity(source);
IXAudio2SourceVoice_GetState(source, &state);
while (state.BuffersQueued > 0) {
Nov 23, 2012
Nov 23, 2012
276
SDL_SemWait(this->hidden->semaphore);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
IXAudio2SourceVoice_GetState(source, &state);
}
}
static void
XAUDIO2_CloseDevice(_THIS)
{
if (this->hidden != NULL) {
IXAudio2 *ixa2 = this->hidden->ixa2;
IXAudio2SourceVoice *source = this->hidden->source;
IXAudio2MasteringVoice *mastering = this->hidden->mastering;
if (source != NULL) {
IXAudio2SourceVoice_Stop(source, 0, XAUDIO2_COMMIT_NOW);
IXAudio2SourceVoice_FlushSourceBuffers(source);
IXAudio2SourceVoice_DestroyVoice(source);
}
if (ixa2 != NULL) {
IXAudio2_StopEngine(ixa2);
}
if (mastering != NULL) {
IXAudio2MasteringVoice_DestroyVoice(mastering);
}
if (ixa2 != NULL) {
IXAudio2_Release(ixa2);
}
if (this->hidden->mixbuf != NULL) {
SDL_free(this->hidden->mixbuf);
}
if (this->hidden->semaphore != NULL) {
Nov 24, 2012
Nov 24, 2012
308
SDL_DestroySemaphore(this->hidden->semaphore);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
}
SDL_free(this->hidden);
this->hidden = NULL;
}
}
static int
XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
{
HRESULT result = S_OK;
WAVEFORMATEX waveformat;
int valid_format = 0;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
IXAudio2 *ixa2 = NULL;
IXAudio2SourceVoice *source = NULL;
Nov 23, 2012
Nov 23, 2012
325
#if defined(__WINRT__)
Nov 24, 2012
Nov 24, 2012
326
327
WCHAR devIdBuffer[256];
LPCWSTR devId = 0;
Nov 23, 2012
Nov 23, 2012
328
#else
329
UINT32 devId = 0; /* 0 == system default device. */
Nov 23, 2012
Nov 23, 2012
330
#endif
Nov 23, 2012
Nov 23, 2012
332
333
334
#if defined(__cplusplus)
static SDL_XAudio2VoiceCallback callbacks;
#else
335
336
337
338
339
340
341
342
343
344
345
static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
VoiceCBOnVoiceProcessPassStart,
VoiceCBOnVoiceProcessPassEnd,
VoiceCBOnStreamEnd,
VoiceCBOnBufferStart,
VoiceCBOnBufferEnd,
VoiceCBOnLoopEnd,
VoiceCBOnVoiceError
};
static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
Nov 23, 2012
Nov 23, 2012
346
347
348
#endif // ! defined(__cplusplus)
#if defined(__WINRT__)
Nov 24, 2012
Nov 24, 2012
349
SDL_zero(devIdBuffer);
Nov 23, 2012
Nov 23, 2012
350
#endif
Mar 31, 2013
Mar 31, 2013
353
return SDL_SetError("XAudio2: capture devices unsupported.");
354
} else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
Mar 31, 2013
Mar 31, 2013
355
return SDL_SetError("XAudio2: XAudio2Create() failed.");
Nov 25, 2012
Nov 25, 2012
357
/*
Nov 24, 2012
Nov 24, 2012
358
359
360
361
362
363
364
365
XAUDIO2_DEBUG_CONFIGURATION debugConfig;
debugConfig.TraceMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS | XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_FUNC_CALLS | XAUDIO2_LOG_TIMING | XAUDIO2_LOG_LOCKS | XAUDIO2_LOG_MEMORY | XAUDIO2_LOG_STREAMING;
debugConfig.BreakMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS;
debugConfig.LogThreadID = TRUE;
debugConfig.LogFileline = TRUE;
debugConfig.LogFunctionName = TRUE;
debugConfig.LogTiming = TRUE;
ixa2->SetDebugConfiguration(&debugConfig);
Nov 25, 2012
Nov 25, 2012
366
*/
Jan 29, 2013
Jan 29, 2013
368
#if ! defined(__WINRT__) || WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
369
370
371
372
373
374
if (devname != NULL) {
UINT32 devcount = 0;
UINT32 i = 0;
if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
IXAudio2_Release(ixa2);
Mar 31, 2013
Mar 31, 2013
375
return SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
376
377
378
379
}
for (i = 0; i < devcount; i++) {
XAUDIO2_DEVICE_DETAILS details;
if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
Nov 24, 2012
Nov 24, 2012
380
char *str = WIN_StringToUTF8(details.DisplayName);
381
382
383
384
if (str != NULL) {
const int match = (SDL_strcmp(str, devname) == 0);
SDL_free(str);
if (match) {
Nov 23, 2012
Nov 23, 2012
385
#if defined(__WINRT__)
Nov 24, 2012
Nov 24, 2012
386
387
wcsncpy_s(devIdBuffer, ARRAYSIZE(devIdBuffer), details.DeviceID, _TRUNCATE);
devId = (LPCWSTR) &devIdBuffer;
Nov 23, 2012
Nov 23, 2012
388
#else
Nov 23, 2012
Nov 23, 2012
390
#endif
391
392
393
394
395
396
397
398
break;
}
}
}
}
if (i == devcount) {
IXAudio2_Release(ixa2);
Mar 31, 2013
Mar 31, 2013
399
return SDL_SetError("XAudio2: Requested device not found.");
Jan 29, 2013
Jan 29, 2013
402
#endif
403
404
405
406
407
408
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
IXAudio2_Release(ixa2);
Mar 31, 2013
Mar 31, 2013
409
return SDL_OutOfMemory();
410
411
412
413
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
this->hidden->ixa2 = ixa2;
Nov 23, 2012
Nov 23, 2012
414
this->hidden->semaphore = SDL_CreateSemaphore(1);
415
416
if (this->hidden->semaphore == NULL) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
417
return SDL_SetError("XAudio2: CreateSemaphore() failed!");
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
}
while ((!valid_format) && (test_format)) {
switch (test_format) {
case AUDIO_U8:
case AUDIO_S16:
case AUDIO_S32:
case AUDIO_F32:
this->spec.format = test_format;
valid_format = 1;
break;
}
test_format = SDL_NextAudioFormat();
}
if (!valid_format) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
435
return SDL_SetError("XAudio2: Unsupported audio format");
436
437
438
439
440
441
442
443
444
445
}
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(&this->spec);
/* We feed a Source, it feeds the Mastering, which feeds the device. */
this->hidden->mixlen = this->spec.size;
this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
446
return SDL_OutOfMemory();
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
}
this->hidden->nextbuf = this->hidden->mixbuf;
SDL_memset(this->hidden->mixbuf, 0, 2 * this->hidden->mixlen);
/* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On
Xbox360, this means 5.1 output, but on Windows, it means "figure out
what the system has." It might be preferable to let XAudio2 blast
stereo output to appropriate surround sound configurations
instead of clamping to 2 channels, even though we'll configure the
Source Voice for whatever number of channels you supply. */
result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering,
XAUDIO2_DEFAULT_CHANNELS,
this->spec.freq, 0, devId, NULL);
if (result != S_OK) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
462
return SDL_SetError("XAudio2: Couldn't create mastering voice");
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
}
SDL_zero(waveformat);
if (SDL_AUDIO_ISFLOAT(this->spec.format)) {
waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
} else {
waveformat.wFormatTag = WAVE_FORMAT_PCM;
}
waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
waveformat.nChannels = this->spec.channels;
waveformat.nSamplesPerSec = this->spec.freq;
waveformat.nBlockAlign =
waveformat.nChannels * (waveformat.wBitsPerSample / 8);
waveformat.nAvgBytesPerSec =
waveformat.nSamplesPerSec * waveformat.nBlockAlign;
Nov 24, 2012
Nov 24, 2012
478
waveformat.cbSize = sizeof(waveformat);
Nov 24, 2012
Nov 24, 2012
480
#ifdef __WINRT__
Dec 30, 2012
Dec 30, 2012
481
482
483
484
// DLudwig: for now, make XAudio2 do sample rate conversion, just to
// get the loopwave test to work.
//
// TODO, WinRT: consider removing WinRT-specific source-voice creation code from SDL_xaudio2.c
Nov 24, 2012
Nov 24, 2012
485
486
487
488
result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
0,
1.0f, &callbacks, NULL, NULL);
#else
489
490
491
492
result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
XAUDIO2_VOICE_NOSRC |
XAUDIO2_VOICE_NOPITCH,
1.0f, &callbacks, NULL, NULL);
Nov 24, 2012
Nov 24, 2012
493
494
#endif
495
496
if (result != S_OK) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
497
return SDL_SetError("XAudio2: Couldn't create source voice");
498
499
500
501
502
503
504
}
this->hidden->source = source;
/* Start everything playing! */
result = IXAudio2_StartEngine(ixa2);
if (result != S_OK) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
505
return SDL_SetError("XAudio2: Couldn't start engine");
506
507
508
509
510
}
result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW);
if (result != S_OK) {
XAUDIO2_CloseDevice(this);
Mar 31, 2013
Mar 31, 2013
511
return SDL_SetError("XAudio2: Couldn't start source voice");
Mar 31, 2013
Mar 31, 2013
514
return 0; /* good to go. */
515
516
517
518
519
}
static void
XAUDIO2_Deinitialize(void)
{
Nov 23, 2012
Nov 23, 2012
520
#if defined(__WIN32__)
521
WIN_CoUninitialize();
Nov 23, 2012
Nov 23, 2012
522
#endif
523
524
525
526
527
528
529
530
531
532
533
534
535
536
}
#endif /* SDL_XAUDIO2_HAS_SDK */
static int
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
{
#ifndef SDL_XAUDIO2_HAS_SDK
SDL_SetError("XAudio2: SDL was built without XAudio2 support (old DirectX SDK).");
return 0; /* no XAudio2 support, ever. Update your SDK! */
#else
/* XAudio2Create() is a macro that uses COM; we don't load the .dll */
IXAudio2 *ixa2 = NULL;
Nov 23, 2012
Nov 23, 2012
537
#if defined(__WIN32__)
Nov 24, 2012
Nov 24, 2012
538
// TODO, WinRT: Investigate using CoInitializeEx here
539
540
541
542
if (FAILED(WIN_CoInitialize())) {
SDL_SetError("XAudio2: CoInitialize() failed");
return 0;
}
Nov 23, 2012
Nov 23, 2012
543
#endif
544
545
if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
Nov 23, 2012
Nov 23, 2012
546
#if defined(__WIN32__)
547
WIN_CoUninitialize();
Nov 23, 2012
Nov 23, 2012
548
#endif
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
SDL_SetError("XAudio2: XAudio2Create() failed");
return 0; /* not available. */
}
IXAudio2_Release(ixa2);
/* Set the function pointers */
impl->DetectDevices = XAUDIO2_DetectDevices;
impl->OpenDevice = XAUDIO2_OpenDevice;
impl->PlayDevice = XAUDIO2_PlayDevice;
impl->WaitDevice = XAUDIO2_WaitDevice;
impl->WaitDone = XAUDIO2_WaitDone;
impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
impl->CloseDevice = XAUDIO2_CloseDevice;
impl->Deinitialize = XAUDIO2_Deinitialize;
return 1; /* this audio target is available. */
#endif
}
Nov 23, 2012
Nov 23, 2012
568
569
570
#if defined(__cplusplus)
extern "C"
#endif
571
572
573
574
575
576
577
AudioBootStrap XAUDIO2_bootstrap = {
"xaudio2", "XAudio2", XAUDIO2_Init, 0
};
#endif /* SDL_AUDIO_DRIVER_XAUDIO2 */
/* vi: set ts=4 sw=4 expandtab: */