src/audio/xaudio2/SDL_xaudio2.c
author Ryan C. Gordon
Thu, 04 Aug 2011 01:07:09 -0400
changeset 5592 2e88d0742f4d
child 5593 ab22ca13c47f
permissions -rw-r--r--
Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
icculus@5592
     1
/*
icculus@5592
     2
  Simple DirectMedia Layer
icculus@5592
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
icculus@5592
     4
icculus@5592
     5
  This software is provided 'as-is', without any express or implied
icculus@5592
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@5592
     7
  arising from the use of this software.
icculus@5592
     8
icculus@5592
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@5592
    10
  including commercial applications, and to alter it and redistribute it
icculus@5592
    11
  freely, subject to the following restrictions:
icculus@5592
    12
icculus@5592
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@5592
    14
     claim that you wrote the original software. If you use this software
icculus@5592
    15
     in a product, an acknowledgment in the product documentation would be
icculus@5592
    16
     appreciated but is not required.
icculus@5592
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@5592
    18
     misrepresented as being the original software.
icculus@5592
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@5592
    20
*/
icculus@5592
    21
#include "SDL_config.h"
icculus@5592
    22
#include "../../core/windows/SDL_windows.h"
icculus@5592
    23
#include "SDL_audio.h"
icculus@5592
    24
#include "../SDL_audio_c.h"
icculus@5592
    25
#include "SDL_assert.h"
icculus@5592
    26
icculus@5592
    27
#define INITGUID 1
icculus@5592
    28
#include "SDL_xaudio2.h"
icculus@5592
    29
icculus@5592
    30
/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
icculus@5592
    31
 * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
icculus@5592
    32
 * !!! FIXME:  and not in drivers.
icculus@5592
    33
 */
icculus@5592
    34
static void
icculus@5592
    35
FreeXAudio2AudioDevices(char ***devices, int *devCount)
icculus@5592
    36
{
icculus@5592
    37
    int i = *devCount;
icculus@5592
    38
    if ((i > 0) && (*devices != NULL)) {
icculus@5592
    39
        while (i--) {
icculus@5592
    40
            SDL_free((*devices)[i]);
icculus@5592
    41
        }
icculus@5592
    42
    }
icculus@5592
    43
icculus@5592
    44
    if (*devices != NULL) {
icculus@5592
    45
        SDL_free(*devices);
icculus@5592
    46
    }
icculus@5592
    47
icculus@5592
    48
    *devices = NULL;
icculus@5592
    49
    *devCount = 0;
icculus@5592
    50
}
icculus@5592
    51
icculus@5592
    52
icculus@5592
    53
static char **outputDevices = NULL;
icculus@5592
    54
static int outputDeviceCount = 0;
icculus@5592
    55
icculus@5592
    56
static __inline__ char *
icculus@5592
    57
utf16_to_utf8(const WCHAR *S)
icculus@5592
    58
{
icculus@5592
    59
    /* !!! FIXME: this should be UTF-16, not UCS-2! */
icculus@5592
    60
    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
icculus@5592
    61
                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
icculus@5592
    62
}
icculus@5592
    63
icculus@5592
    64
static int
icculus@5592
    65
XAUDIO2_DetectDevices(int iscapture)
icculus@5592
    66
{
icculus@5592
    67
    IXAudio2 *ixa2 = NULL;
icculus@5592
    68
    UINT32 devcount = 0;
icculus@5592
    69
    UINT32 i = 0;
icculus@5592
    70
    void *ptr = NULL;
icculus@5592
    71
icculus@5592
    72
    if (!iscapture) {
icculus@5592
    73
        FreeXAudio2AudioDevices(&outputDevices, &outputDeviceCount);
icculus@5592
    74
    }
icculus@5592
    75
icculus@5592
    76
    if (iscapture) {
icculus@5592
    77
        SDL_SetError("XAudio2: capture devices unsupported.");
icculus@5592
    78
        return 0;
icculus@5592
    79
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
icculus@5592
    80
        SDL_SetError("XAudio2: XAudio2Create() failed.");
icculus@5592
    81
        return 0;
icculus@5592
    82
    } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
icculus@5592
    83
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
icculus@5592
    84
        IXAudio2_Release(ixa2);
icculus@5592
    85
        return 0;
icculus@5592
    86
    } else if ((ptr = SDL_malloc(sizeof (char *) * devcount)) == NULL) {
icculus@5592
    87
        SDL_OutOfMemory();
icculus@5592
    88
        IXAudio2_Release(ixa2);
icculus@5592
    89
        return 0;
icculus@5592
    90
    }
icculus@5592
    91
icculus@5592
    92
    outputDevices = (char **) ptr;
icculus@5592
    93
    for (i = 0; i < devcount; i++) {
icculus@5592
    94
        XAUDIO2_DEVICE_DETAILS details;
icculus@5592
    95
        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
icculus@5592
    96
            char *str = utf16_to_utf8(details.DisplayName);
icculus@5592
    97
            if (str != NULL) {
icculus@5592
    98
                outputDevices[outputDeviceCount++] = str;
icculus@5592
    99
            }
icculus@5592
   100
        }
icculus@5592
   101
    }
icculus@5592
   102
icculus@5592
   103
    IXAudio2_Release(ixa2);
icculus@5592
   104
icculus@5592
   105
    return outputDeviceCount;
icculus@5592
   106
}
icculus@5592
   107
icculus@5592
   108
static const char *
icculus@5592
   109
XAUDIO2_GetDeviceName(int index, int iscapture)
icculus@5592
   110
{
icculus@5592
   111
    if ((!iscapture) && (index < outputDeviceCount)) {
icculus@5592
   112
        return outputDevices[index];
icculus@5592
   113
    }
icculus@5592
   114
icculus@5592
   115
    SDL_SetError("XAudio2: No such device");
icculus@5592
   116
    return NULL;
icculus@5592
   117
}
icculus@5592
   118
icculus@5592
   119
static void STDMETHODCALLTYPE
icculus@5592
   120
VoiceCBOnBufferEnd(THIS_ void *data)
icculus@5592
   121
{
icculus@5592
   122
    /* Just signal the SDL audio thread and get out of XAudio2's way. */
icculus@5592
   123
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
icculus@5592
   124
    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
icculus@5592
   125
}
icculus@5592
   126
icculus@5592
   127
static void STDMETHODCALLTYPE
icculus@5592
   128
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
icculus@5592
   129
{
icculus@5592
   130
    /* !!! FIXME: attempt to recover, or mark device disconnected. */
icculus@5592
   131
    SDL_assert(0 && "write me!");
icculus@5592
   132
}
icculus@5592
   133
icculus@5592
   134
/* no-op callbacks... */
icculus@5592
   135
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
icculus@5592
   136
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassStart(THIS_ UINT32 b) {}
icculus@5592
   137
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
icculus@5592
   138
static void STDMETHODCALLTYPE VoiceCBOnBufferStart(THIS_ void *data) {}
icculus@5592
   139
static void STDMETHODCALLTYPE VoiceCBOnLoopEnd(THIS_ void *data) {}
icculus@5592
   140
icculus@5592
   141
icculus@5592
   142
static Uint8 *
icculus@5592
   143
XAUDIO2_GetDeviceBuf(_THIS)
icculus@5592
   144
{
icculus@5592
   145
    return this->hidden->nextbuf;
icculus@5592
   146
}
icculus@5592
   147
icculus@5592
   148
static void
icculus@5592
   149
XAUDIO2_PlayDevice(_THIS)
icculus@5592
   150
{
icculus@5592
   151
    XAUDIO2_BUFFER buffer;
icculus@5592
   152
    Uint8 *mixbuf = this->hidden->mixbuf;
icculus@5592
   153
    Uint8 *nextbuf = this->hidden->nextbuf;
icculus@5592
   154
    const int mixlen = this->hidden->mixlen;
icculus@5592
   155
    IXAudio2SourceVoice *source = this->hidden->source;
icculus@5592
   156
    HRESULT result = S_OK;
icculus@5592
   157
icculus@5592
   158
    if (!this->enabled) { /* shutting down? */
icculus@5592
   159
        return;
icculus@5592
   160
    }
icculus@5592
   161
icculus@5592
   162
    /* Submit the next filled buffer */
icculus@5592
   163
    SDL_zero(buffer);
icculus@5592
   164
    buffer.AudioBytes = mixlen;
icculus@5592
   165
    buffer.pAudioData = nextbuf;
icculus@5592
   166
    buffer.pContext = this;
icculus@5592
   167
icculus@5592
   168
    if (nextbuf == mixbuf) {
icculus@5592
   169
        nextbuf += mixlen;
icculus@5592
   170
    } else {
icculus@5592
   171
        nextbuf = mixbuf;
icculus@5592
   172
    }
icculus@5592
   173
    this->hidden->nextbuf = nextbuf;
icculus@5592
   174
icculus@5592
   175
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
icculus@5592
   176
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
icculus@5592
   177
        /* !!! FIXME: possibly disconnected or temporary lost. Recover? */
icculus@5592
   178
    }
icculus@5592
   179
icculus@5592
   180
    if (result != S_OK) {  /* uhoh, panic! */
icculus@5592
   181
        IXAudio2SourceVoice_FlushSourceBuffers(source);
icculus@5592
   182
        this->enabled = 0;
icculus@5592
   183
    }
icculus@5592
   184
}
icculus@5592
   185
icculus@5592
   186
static void
icculus@5592
   187
XAUDIO2_WaitDevice(_THIS)
icculus@5592
   188
{
icculus@5592
   189
    if (this->enabled) {
icculus@5592
   190
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
icculus@5592
   191
    }
icculus@5592
   192
}
icculus@5592
   193
icculus@5592
   194
static void
icculus@5592
   195
XAUDIO2_WaitDone(_THIS)
icculus@5592
   196
{
icculus@5592
   197
    IXAudio2SourceVoice *source = this->hidden->source;
icculus@5592
   198
    XAUDIO2_VOICE_STATE state;
icculus@5592
   199
    SDL_assert(!this->enabled);  /* flag that stops playing. */
icculus@5592
   200
    IXAudio2SourceVoice_Discontinuity(source);
icculus@5592
   201
    IXAudio2SourceVoice_GetState(source, &state);
icculus@5592
   202
    while (state.BuffersQueued > 0) {
icculus@5592
   203
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
icculus@5592
   204
        IXAudio2SourceVoice_GetState(source, &state);
icculus@5592
   205
    }
icculus@5592
   206
}
icculus@5592
   207
icculus@5592
   208
icculus@5592
   209
static void
icculus@5592
   210
XAUDIO2_CloseDevice(_THIS)
icculus@5592
   211
{
icculus@5592
   212
    if (this->hidden != NULL) {
icculus@5592
   213
        IXAudio2 *ixa2 = this->hidden->ixa2;
icculus@5592
   214
        IXAudio2SourceVoice *source = this->hidden->source;
icculus@5592
   215
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
icculus@5592
   216
icculus@5592
   217
        if (source != NULL) {
icculus@5592
   218
            IXAudio2SourceVoice_Stop(source, 0, XAUDIO2_COMMIT_NOW);
icculus@5592
   219
            IXAudio2SourceVoice_FlushSourceBuffers(source);
icculus@5592
   220
            IXAudio2SourceVoice_DestroyVoice(source);
icculus@5592
   221
        }
icculus@5592
   222
        if (ixa2 != NULL) {
icculus@5592
   223
            IXAudio2_StopEngine(ixa2);
icculus@5592
   224
        }
icculus@5592
   225
        if (mastering != NULL) {
icculus@5592
   226
            IXAudio2MasteringVoice_DestroyVoice(mastering);
icculus@5592
   227
        }
icculus@5592
   228
        if (ixa2 != NULL) {
icculus@5592
   229
            IXAudio2_Release(ixa2);
icculus@5592
   230
        }
icculus@5592
   231
        if (this->hidden->mixbuf != NULL) {
icculus@5592
   232
            SDL_free(this->hidden->mixbuf);
icculus@5592
   233
        }
icculus@5592
   234
        if (this->hidden->semaphore != NULL) {
icculus@5592
   235
            CloseHandle(this->hidden->semaphore);
icculus@5592
   236
        }
icculus@5592
   237
icculus@5592
   238
        SDL_free(this->hidden);
icculus@5592
   239
        this->hidden = NULL;
icculus@5592
   240
    }
icculus@5592
   241
}
icculus@5592
   242
icculus@5592
   243
static int
icculus@5592
   244
XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
icculus@5592
   245
{
icculus@5592
   246
    HRESULT result = S_OK;
icculus@5592
   247
    WAVEFORMATEX waveformat;
icculus@5592
   248
    int valid_format = 0;
icculus@5592
   249
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@5592
   250
    IXAudio2 *ixa2 = NULL;
icculus@5592
   251
    IXAudio2SourceVoice *source = NULL;
icculus@5592
   252
    UINT32 devId = 0;  /* 0 == system default device. */
icculus@5592
   253
icculus@5592
   254
	static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
icculus@5592
   255
	    VoiceCBOnVoiceProcessPassStart,
icculus@5592
   256
        VoiceCBOnVoiceProcessPassEnd,
icculus@5592
   257
        VoiceCBOnStreamEnd,
icculus@5592
   258
        VoiceCBOnBufferStart,
icculus@5592
   259
        VoiceCBOnBufferEnd,
icculus@5592
   260
        VoiceCBOnLoopEnd,
icculus@5592
   261
        VoiceCBOnVoiceError
icculus@5592
   262
	};
icculus@5592
   263
icculus@5592
   264
	static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
icculus@5592
   265
icculus@5592
   266
    if (iscapture) {
icculus@5592
   267
        SDL_SetError("XAudio2: capture devices unsupported.");
icculus@5592
   268
        return 0;
icculus@5592
   269
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
icculus@5592
   270
        SDL_SetError("XAudio2: XAudio2Create() failed.");
icculus@5592
   271
        return 0;
icculus@5592
   272
    }
icculus@5592
   273
icculus@5592
   274
    if (devname != NULL) {
icculus@5592
   275
        UINT32 devcount = 0;
icculus@5592
   276
        UINT32 i = 0;
icculus@5592
   277
icculus@5592
   278
        if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
icculus@5592
   279
            IXAudio2_Release(ixa2);
icculus@5592
   280
            SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
icculus@5592
   281
            return 0;
icculus@5592
   282
        }
icculus@5592
   283
        for (i = 0; i < devcount; i++) {
icculus@5592
   284
            XAUDIO2_DEVICE_DETAILS details;
icculus@5592
   285
            if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
icculus@5592
   286
                char *str = utf16_to_utf8(details.DisplayName);
icculus@5592
   287
                if (str != NULL) {
icculus@5592
   288
                    const int match = (SDL_strcmp(str, devname) == 0);
icculus@5592
   289
                    SDL_free(str);
icculus@5592
   290
                    if (match) {
icculus@5592
   291
                        devId = i;
icculus@5592
   292
                        break;
icculus@5592
   293
                    }
icculus@5592
   294
                }
icculus@5592
   295
            }
icculus@5592
   296
        }
icculus@5592
   297
icculus@5592
   298
        if (i == devcount) {
icculus@5592
   299
            IXAudio2_Release(ixa2);
icculus@5592
   300
            SDL_SetError("XAudio2: Requested device not found.");
icculus@5592
   301
            return 0;
icculus@5592
   302
        }
icculus@5592
   303
    }
icculus@5592
   304
icculus@5592
   305
    /* Initialize all variables that we clean on shutdown */
icculus@5592
   306
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@5592
   307
        SDL_malloc((sizeof *this->hidden));
icculus@5592
   308
    if (this->hidden == NULL) {
icculus@5592
   309
        IXAudio2_Release(ixa2);
icculus@5592
   310
        SDL_OutOfMemory();
icculus@5592
   311
        return 0;
icculus@5592
   312
    }
icculus@5592
   313
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@5592
   314
icculus@5592
   315
    this->hidden->ixa2 = ixa2;
icculus@5592
   316
    this->hidden->semaphore = CreateSemaphore(NULL, 1, 2, NULL);
icculus@5592
   317
    if (this->hidden->semaphore == NULL) {
icculus@5592
   318
        XAUDIO2_CloseDevice(this);
icculus@5592
   319
        SDL_SetError("XAudio2: CreateSemaphore() failed!");
icculus@5592
   320
        return 0;
icculus@5592
   321
    }
icculus@5592
   322
icculus@5592
   323
    while ((!valid_format) && (test_format)) {
icculus@5592
   324
        switch (test_format) {
icculus@5592
   325
        case AUDIO_U8:
icculus@5592
   326
        case AUDIO_S16:
icculus@5592
   327
        case AUDIO_S32:
icculus@5592
   328
        case AUDIO_F32:
icculus@5592
   329
            this->spec.format = test_format;
icculus@5592
   330
            valid_format = 1;
icculus@5592
   331
            break;
icculus@5592
   332
        }
icculus@5592
   333
        test_format = SDL_NextAudioFormat();
icculus@5592
   334
    }
icculus@5592
   335
icculus@5592
   336
    if (!valid_format) {
icculus@5592
   337
        XAUDIO2_CloseDevice(this);
icculus@5592
   338
        SDL_SetError("XAudio2: Unsupported audio format");
icculus@5592
   339
        return 0;
icculus@5592
   340
    }
icculus@5592
   341
icculus@5592
   342
    /* Update the fragment size as size in bytes */
icculus@5592
   343
    SDL_CalculateAudioSpec(&this->spec);
icculus@5592
   344
icculus@5592
   345
    /* We feed a Source, it feeds the Mastering, which feeds the device. */
icculus@5592
   346
    this->hidden->mixlen = this->spec.size;
icculus@5592
   347
    this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen);
icculus@5592
   348
    if (this->hidden->mixbuf == NULL) {
icculus@5592
   349
        XAUDIO2_CloseDevice(this);
icculus@5592
   350
        SDL_OutOfMemory();
icculus@5592
   351
        return 0;
icculus@5592
   352
    }
icculus@5592
   353
    this->hidden->nextbuf = this->hidden->mixbuf;
icculus@5592
   354
    SDL_memset(this->hidden->mixbuf, '\0', 2 * this->hidden->mixlen);
icculus@5592
   355
icculus@5592
   356
    /* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On
icculus@5592
   357
       Xbox360, this means 5.1 output, but on Windows, it means "figure out
icculus@5592
   358
       what the system has." It might be preferable to let XAudio2 blast
icculus@5592
   359
       stereo output to appropriate surround sound configurations
icculus@5592
   360
       instead of clamping to 2 channels, even though we'll configure the
icculus@5592
   361
       Source Voice for whatever number of channels you supply. */
icculus@5592
   362
    result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering,
icculus@5592
   363
                                           XAUDIO2_DEFAULT_CHANNELS,
icculus@5592
   364
                                           this->spec.freq, 0, devId, NULL);
icculus@5592
   365
    if (result != S_OK) {
icculus@5592
   366
        XAUDIO2_CloseDevice(this);
icculus@5592
   367
        SDL_SetError("XAudio2: Couldn't create mastering voice");
icculus@5592
   368
        return 0;
icculus@5592
   369
    }
icculus@5592
   370
icculus@5592
   371
    SDL_zero(waveformat);
icculus@5592
   372
    if (SDL_AUDIO_ISFLOAT(this->spec.format)) {
icculus@5592
   373
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
icculus@5592
   374
    } else {
icculus@5592
   375
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
icculus@5592
   376
    }
icculus@5592
   377
    waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
icculus@5592
   378
    waveformat.nChannels = this->spec.channels;
icculus@5592
   379
    waveformat.nSamplesPerSec = this->spec.freq;
icculus@5592
   380
    waveformat.nBlockAlign =
icculus@5592
   381
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
icculus@5592
   382
    waveformat.nAvgBytesPerSec =
icculus@5592
   383
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
icculus@5592
   384
icculus@5592
   385
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
icculus@5592
   386
                                        XAUDIO2_VOICE_NOSRC |
icculus@5592
   387
                                        XAUDIO2_VOICE_NOPITCH,
icculus@5592
   388
                                        1.0f, &callbacks, NULL, NULL);
icculus@5592
   389
    if (result != S_OK) {
icculus@5592
   390
        XAUDIO2_CloseDevice(this);
icculus@5592
   391
        SDL_SetError("XAudio2: Couldn't create source voice");
icculus@5592
   392
        return 0;
icculus@5592
   393
    }
icculus@5592
   394
    this->hidden->source = source;
icculus@5592
   395
icculus@5592
   396
    /* Start everything playing! */
icculus@5592
   397
    result = IXAudio2_StartEngine(ixa2);
icculus@5592
   398
    if (result != S_OK) {
icculus@5592
   399
        XAUDIO2_CloseDevice(this);
icculus@5592
   400
        SDL_SetError("XAudio2: Couldn't start engine");
icculus@5592
   401
        return 0;
icculus@5592
   402
    }
icculus@5592
   403
icculus@5592
   404
    result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW);
icculus@5592
   405
    if (result != S_OK) {
icculus@5592
   406
        XAUDIO2_CloseDevice(this);
icculus@5592
   407
        SDL_SetError("XAudio2: Couldn't start source voice");
icculus@5592
   408
        return 0;
icculus@5592
   409
    }
icculus@5592
   410
icculus@5592
   411
    return 1; /* good to go. */
icculus@5592
   412
}
icculus@5592
   413
icculus@5592
   414
static void
icculus@5592
   415
XAUDIO2_Deinitialize(void)
icculus@5592
   416
{
icculus@5592
   417
    WIN_CoUninitialize();
icculus@5592
   418
}
icculus@5592
   419
icculus@5592
   420
static int
icculus@5592
   421
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
icculus@5592
   422
{
icculus@5592
   423
    /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
icculus@5592
   424
    IXAudio2 *ixa2 = NULL;
icculus@5592
   425
    if (FAILED(WIN_CoInitialize())) {
icculus@5592
   426
        SDL_SetError("XAudio2: CoInitialize() failed");
icculus@5592
   427
        return 0;
icculus@5592
   428
    }
icculus@5592
   429
icculus@5592
   430
    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
icculus@5592
   431
        WIN_CoUninitialize();
icculus@5592
   432
        SDL_SetError("XAudio2: XAudio2Create() failed");
icculus@5592
   433
        return 0;  /* not available. */
icculus@5592
   434
    }
icculus@5592
   435
    IXAudio2_Release(ixa2);
icculus@5592
   436
icculus@5592
   437
    /* Set the function pointers */
icculus@5592
   438
    impl->DetectDevices = XAUDIO2_DetectDevices;
icculus@5592
   439
    impl->GetDeviceName = XAUDIO2_GetDeviceName;
icculus@5592
   440
    impl->OpenDevice = XAUDIO2_OpenDevice;
icculus@5592
   441
    impl->PlayDevice = XAUDIO2_PlayDevice;
icculus@5592
   442
    impl->WaitDevice = XAUDIO2_WaitDevice;
icculus@5592
   443
    impl->WaitDone = XAUDIO2_WaitDone;
icculus@5592
   444
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
icculus@5592
   445
    impl->CloseDevice = XAUDIO2_CloseDevice;
icculus@5592
   446
    impl->Deinitialize = XAUDIO2_Deinitialize;
icculus@5592
   447
icculus@5592
   448
    return 1;   /* this audio target is available. */
icculus@5592
   449
}
icculus@5592
   450
icculus@5592
   451
AudioBootStrap XAUDIO2_bootstrap = {
icculus@5592
   452
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
icculus@5592
   453
};
icculus@5592
   454
icculus@5592
   455
/* vi: set ts=4 sw=4 expandtab: */