Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Aug 2011 01:07:09 -0400
changeset 55922e88d0742f4d
parent 5591 17beb16fa838
child 5593 ab22ca13c47f
Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
VisualC/SDL/SDL_VS2005.vcproj
VisualC/SDL/SDL_VS2008.vcproj
VisualC/SDL/SDL_VS2010.vcxproj
configure.in
include/SDL_config.h.in
include/SDL_config_windows.h
src/audio/SDL_audio.c
src/audio/xaudio2/SDL_xaudio2.c
src/audio/xaudio2/SDL_xaudio2.h
     1.1 --- a/VisualC/SDL/SDL_VS2005.vcproj	Wed Aug 03 04:22:47 2011 -0400
     1.2 +++ b/VisualC/SDL/SDL_VS2005.vcproj	Thu Aug 04 01:07:09 2011 -0400
     1.3 @@ -848,6 +848,14 @@
     1.4  			>
     1.5  		</File>
     1.6  		<File
     1.7 +			RelativePath="..\..\src\audio\xaudio2\SDL_xaudio2.c"
     1.8 +			>
     1.9 +		</File>
    1.10 +		<File
    1.11 +			RelativePath="..\..\src\audio\xaudio2\SDL_xaudio2.h"
    1.12 +			>
    1.13 +		</File>
    1.14 +		<File
    1.15  			RelativePath="..\..\src\joystick\windows\SDL_dxjoystick.c"
    1.16  			>
    1.17  		</File>
     2.1 --- a/VisualC/SDL/SDL_VS2008.vcproj	Wed Aug 03 04:22:47 2011 -0400
     2.2 +++ b/VisualC/SDL/SDL_VS2008.vcproj	Thu Aug 04 01:07:09 2011 -0400
     2.3 @@ -837,6 +837,14 @@
     2.4  			>
     2.5  		</File>
     2.6  		<File
     2.7 +			RelativePath="..\..\src\audio\xaudio2\SDL_xaudio2.c"
     2.8 +			>
     2.9 +		</File>
    2.10 +		<File
    2.11 +			RelativePath="..\..\src\audio\xaudio2\SDL_xaudio2.h"
    2.12 +			>
    2.13 +		</File>
    2.14 +		<File
    2.15  			RelativePath="..\..\src\joystick\windows\SDL_dxjoystick.c"
    2.16  			>
    2.17  		</File>
     3.1 --- a/VisualC/SDL/SDL_VS2010.vcxproj	Wed Aug 03 04:22:47 2011 -0400
     3.2 +++ b/VisualC/SDL/SDL_VS2010.vcxproj	Thu Aug 04 01:07:09 2011 -0400
     3.3 @@ -280,6 +280,7 @@
     3.4      <ClInclude Include="..\..\src\audio\disk\SDL_diskaudio.h" />
     3.5      <ClInclude Include="..\..\src\audio\dummy\SDL_dummyaudio.h" />
     3.6      <ClInclude Include="..\..\src\audio\directsound\SDL_directsound.h" />
     3.7 +    <ClInclude Include="..\..\src\audio\xaudio2\SDL_xaudio2.h" />
     3.8      <ClInclude Include="..\..\src\SDL_error_c.h" />
     3.9      <ClInclude Include="..\..\src\SDL_hints_c.h" />
    3.10      <ClInclude Include="..\..\src\events\SDL_events_c.h" />
    3.11 @@ -381,6 +382,7 @@
    3.12      <ClCompile Include="..\..\src\audio\disk\SDL_diskaudio.c" />
    3.13      <ClCompile Include="..\..\src\audio\dummy\SDL_dummyaudio.c" />
    3.14      <ClCompile Include="..\..\src\audio\directsound\SDL_directsound.c" />
    3.15 +    <ClCompile Include="..\..\src\audio\xaudio2\SDL_xaudio2.c" />
    3.16      <ClCompile Include="..\..\src\joystick\windows\SDL_dxjoystick.c" />
    3.17      <ClCompile Include="..\..\src\SDL_error.c" />
    3.18      <ClCompile Include="..\..\src\events\SDL_events.c" />
     4.1 --- a/configure.in	Wed Aug 03 04:22:47 2011 -0400
     4.2 +++ b/configure.in	Thu Aug 04 01:07:09 2011 -0400
     4.3 @@ -1726,6 +1726,7 @@
     4.4          AC_CHECK_HEADER(ddraw.h, have_ddraw=yes)
     4.5          AC_CHECK_HEADER(dsound.h, have_dsound=yes)
     4.6          AC_CHECK_HEADER(dinput.h, have_dinput=yes)
     4.7 +        AC_CHECK_HEADER(xaudio2.h, have_xaudio2=yes)
     4.8      fi
     4.9  }
    4.10  
    4.11 @@ -2063,6 +2064,10 @@
    4.12                  AC_DEFINE(SDL_AUDIO_DRIVER_DSOUND, 1, [ ])
    4.13                  SOURCES="$SOURCES $srcdir/src/audio/directsound/*.c"
    4.14              fi
    4.15 +            if test x$have_xaudio2 = xyes; then
    4.16 +                AC_DEFINE(SDL_AUDIO_DRIVER_XAUDIO2, 1, [ ])
    4.17 +                SOURCES="$SOURCES $srcdir/src/audio/xaudio2/*.c"
    4.18 +            fi
    4.19              have_audio=yes
    4.20          fi
    4.21          # Set up dummy files for the joystick for now
    4.22 @@ -2150,6 +2155,10 @@
    4.23                  AC_DEFINE(SDL_AUDIO_DRIVER_DSOUND, 1, [ ])
    4.24                  SOURCES="$SOURCES $srcdir/src/audio/directsound/*.c"
    4.25              fi
    4.26 +            if test x$have_xaudio2 = xyes; then
    4.27 +                AC_DEFINE(SDL_AUDIO_DRIVER_XAUDIO2, 1, [ ])
    4.28 +                SOURCES="$SOURCES $srcdir/src/audio/xaudio2/*.c"
    4.29 +            fi
    4.30              have_audio=yes
    4.31          fi
    4.32          # Set up files for the joystick library
     5.1 --- a/include/SDL_config.h.in	Wed Aug 03 04:22:47 2011 -0400
     5.2 +++ b/include/SDL_config.h.in	Thu Aug 04 01:07:09 2011 -0400
     5.3 @@ -184,6 +184,7 @@
     5.4  #undef SDL_AUDIO_DRIVER_COREAUDIO
     5.5  #undef SDL_AUDIO_DRIVER_DISK
     5.6  #undef SDL_AUDIO_DRIVER_DUMMY
     5.7 +#undef SDL_AUDIO_DRIVER_XAUDIO2
     5.8  #undef SDL_AUDIO_DRIVER_DSOUND
     5.9  #undef SDL_AUDIO_DRIVER_ESD
    5.10  #undef SDL_AUDIO_DRIVER_ESD_DYNAMIC
     6.1 --- a/include/SDL_config_windows.h	Wed Aug 03 04:22:47 2011 -0400
     6.2 +++ b/include/SDL_config_windows.h	Thu Aug 04 01:07:09 2011 -0400
     6.3 @@ -145,6 +145,7 @@
     6.4  /* Enable various audio drivers */
     6.5  #ifndef _WIN32_WCE
     6.6  #define SDL_AUDIO_DRIVER_DSOUND	1
     6.7 +#define SDL_AUDIO_DRIVER_XAUDIO2	1
     6.8  #endif
     6.9  #define SDL_AUDIO_DRIVER_WINMM	1
    6.10  #define SDL_AUDIO_DRIVER_DISK	1
     7.1 --- a/src/audio/SDL_audio.c	Wed Aug 03 04:22:47 2011 -0400
     7.2 +++ b/src/audio/SDL_audio.c	Thu Aug 04 01:07:09 2011 -0400
     7.3 @@ -52,6 +52,7 @@
     7.4  extern AudioBootStrap ARTS_bootstrap;
     7.5  extern AudioBootStrap ESD_bootstrap;
     7.6  extern AudioBootStrap NAS_bootstrap;
     7.7 +extern AudioBootStrap XAUDIO2_bootstrap;
     7.8  extern AudioBootStrap DSOUND_bootstrap;
     7.9  extern AudioBootStrap WINMM_bootstrap;
    7.10  extern AudioBootStrap PAUDIO_bootstrap;
    7.11 @@ -97,6 +98,9 @@
    7.12  #if SDL_AUDIO_DRIVER_NAS
    7.13      &NAS_bootstrap,
    7.14  #endif
    7.15 +#if SDL_AUDIO_DRIVER_XAUDIO2
    7.16 +    &XAUDIO2_bootstrap,
    7.17 +#endif
    7.18  #if SDL_AUDIO_DRIVER_DSOUND
    7.19      &DSOUND_bootstrap,
    7.20  #endif
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/audio/xaudio2/SDL_xaudio2.c	Thu Aug 04 01:07:09 2011 -0400
     8.3 @@ -0,0 +1,455 @@
     8.4 +/*
     8.5 +  Simple DirectMedia Layer
     8.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     8.7 +
     8.8 +  This software is provided 'as-is', without any express or implied
     8.9 +  warranty.  In no event will the authors be held liable for any damages
    8.10 +  arising from the use of this software.
    8.11 +
    8.12 +  Permission is granted to anyone to use this software for any purpose,
    8.13 +  including commercial applications, and to alter it and redistribute it
    8.14 +  freely, subject to the following restrictions:
    8.15 +
    8.16 +  1. The origin of this software must not be misrepresented; you must not
    8.17 +     claim that you wrote the original software. If you use this software
    8.18 +     in a product, an acknowledgment in the product documentation would be
    8.19 +     appreciated but is not required.
    8.20 +  2. Altered source versions must be plainly marked as such, and must not be
    8.21 +     misrepresented as being the original software.
    8.22 +  3. This notice may not be removed or altered from any source distribution.
    8.23 +*/
    8.24 +#include "SDL_config.h"
    8.25 +#include "../../core/windows/SDL_windows.h"
    8.26 +#include "SDL_audio.h"
    8.27 +#include "../SDL_audio_c.h"
    8.28 +#include "SDL_assert.h"
    8.29 +
    8.30 +#define INITGUID 1
    8.31 +#include "SDL_xaudio2.h"
    8.32 +
    8.33 +/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
    8.34 + * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
    8.35 + * !!! FIXME:  and not in drivers.
    8.36 + */
    8.37 +static void
    8.38 +FreeXAudio2AudioDevices(char ***devices, int *devCount)
    8.39 +{
    8.40 +    int i = *devCount;
    8.41 +    if ((i > 0) && (*devices != NULL)) {
    8.42 +        while (i--) {
    8.43 +            SDL_free((*devices)[i]);
    8.44 +        }
    8.45 +    }
    8.46 +
    8.47 +    if (*devices != NULL) {
    8.48 +        SDL_free(*devices);
    8.49 +    }
    8.50 +
    8.51 +    *devices = NULL;
    8.52 +    *devCount = 0;
    8.53 +}
    8.54 +
    8.55 +
    8.56 +static char **outputDevices = NULL;
    8.57 +static int outputDeviceCount = 0;
    8.58 +
    8.59 +static __inline__ char *
    8.60 +utf16_to_utf8(const WCHAR *S)
    8.61 +{
    8.62 +    /* !!! FIXME: this should be UTF-16, not UCS-2! */
    8.63 +    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
    8.64 +                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
    8.65 +}
    8.66 +
    8.67 +static int
    8.68 +XAUDIO2_DetectDevices(int iscapture)
    8.69 +{
    8.70 +    IXAudio2 *ixa2 = NULL;
    8.71 +    UINT32 devcount = 0;
    8.72 +    UINT32 i = 0;
    8.73 +    void *ptr = NULL;
    8.74 +
    8.75 +    if (!iscapture) {
    8.76 +        FreeXAudio2AudioDevices(&outputDevices, &outputDeviceCount);
    8.77 +    }
    8.78 +
    8.79 +    if (iscapture) {
    8.80 +        SDL_SetError("XAudio2: capture devices unsupported.");
    8.81 +        return 0;
    8.82 +    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
    8.83 +        SDL_SetError("XAudio2: XAudio2Create() failed.");
    8.84 +        return 0;
    8.85 +    } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
    8.86 +        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
    8.87 +        IXAudio2_Release(ixa2);
    8.88 +        return 0;
    8.89 +    } else if ((ptr = SDL_malloc(sizeof (char *) * devcount)) == NULL) {
    8.90 +        SDL_OutOfMemory();
    8.91 +        IXAudio2_Release(ixa2);
    8.92 +        return 0;
    8.93 +    }
    8.94 +
    8.95 +    outputDevices = (char **) ptr;
    8.96 +    for (i = 0; i < devcount; i++) {
    8.97 +        XAUDIO2_DEVICE_DETAILS details;
    8.98 +        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
    8.99 +            char *str = utf16_to_utf8(details.DisplayName);
   8.100 +            if (str != NULL) {
   8.101 +                outputDevices[outputDeviceCount++] = str;
   8.102 +            }
   8.103 +        }
   8.104 +    }
   8.105 +
   8.106 +    IXAudio2_Release(ixa2);
   8.107 +
   8.108 +    return outputDeviceCount;
   8.109 +}
   8.110 +
   8.111 +static const char *
   8.112 +XAUDIO2_GetDeviceName(int index, int iscapture)
   8.113 +{
   8.114 +    if ((!iscapture) && (index < outputDeviceCount)) {
   8.115 +        return outputDevices[index];
   8.116 +    }
   8.117 +
   8.118 +    SDL_SetError("XAudio2: No such device");
   8.119 +    return NULL;
   8.120 +}
   8.121 +
   8.122 +static void STDMETHODCALLTYPE
   8.123 +VoiceCBOnBufferEnd(THIS_ void *data)
   8.124 +{
   8.125 +    /* Just signal the SDL audio thread and get out of XAudio2's way. */
   8.126 +    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
   8.127 +    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
   8.128 +}
   8.129 +
   8.130 +static void STDMETHODCALLTYPE
   8.131 +VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
   8.132 +{
   8.133 +    /* !!! FIXME: attempt to recover, or mark device disconnected. */
   8.134 +    SDL_assert(0 && "write me!");
   8.135 +}
   8.136 +
   8.137 +/* no-op callbacks... */
   8.138 +static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
   8.139 +static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassStart(THIS_ UINT32 b) {}
   8.140 +static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
   8.141 +static void STDMETHODCALLTYPE VoiceCBOnBufferStart(THIS_ void *data) {}
   8.142 +static void STDMETHODCALLTYPE VoiceCBOnLoopEnd(THIS_ void *data) {}
   8.143 +
   8.144 +
   8.145 +static Uint8 *
   8.146 +XAUDIO2_GetDeviceBuf(_THIS)
   8.147 +{
   8.148 +    return this->hidden->nextbuf;
   8.149 +}
   8.150 +
   8.151 +static void
   8.152 +XAUDIO2_PlayDevice(_THIS)
   8.153 +{
   8.154 +    XAUDIO2_BUFFER buffer;
   8.155 +    Uint8 *mixbuf = this->hidden->mixbuf;
   8.156 +    Uint8 *nextbuf = this->hidden->nextbuf;
   8.157 +    const int mixlen = this->hidden->mixlen;
   8.158 +    IXAudio2SourceVoice *source = this->hidden->source;
   8.159 +    HRESULT result = S_OK;
   8.160 +
   8.161 +    if (!this->enabled) { /* shutting down? */
   8.162 +        return;
   8.163 +    }
   8.164 +
   8.165 +    /* Submit the next filled buffer */
   8.166 +    SDL_zero(buffer);
   8.167 +    buffer.AudioBytes = mixlen;
   8.168 +    buffer.pAudioData = nextbuf;
   8.169 +    buffer.pContext = this;
   8.170 +
   8.171 +    if (nextbuf == mixbuf) {
   8.172 +        nextbuf += mixlen;
   8.173 +    } else {
   8.174 +        nextbuf = mixbuf;
   8.175 +    }
   8.176 +    this->hidden->nextbuf = nextbuf;
   8.177 +
   8.178 +    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
   8.179 +    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
   8.180 +        /* !!! FIXME: possibly disconnected or temporary lost. Recover? */
   8.181 +    }
   8.182 +
   8.183 +    if (result != S_OK) {  /* uhoh, panic! */
   8.184 +        IXAudio2SourceVoice_FlushSourceBuffers(source);
   8.185 +        this->enabled = 0;
   8.186 +    }
   8.187 +}
   8.188 +
   8.189 +static void
   8.190 +XAUDIO2_WaitDevice(_THIS)
   8.191 +{
   8.192 +    if (this->enabled) {
   8.193 +        WaitForSingleObject(this->hidden->semaphore, INFINITE);
   8.194 +    }
   8.195 +}
   8.196 +
   8.197 +static void
   8.198 +XAUDIO2_WaitDone(_THIS)
   8.199 +{
   8.200 +    IXAudio2SourceVoice *source = this->hidden->source;
   8.201 +    XAUDIO2_VOICE_STATE state;
   8.202 +    SDL_assert(!this->enabled);  /* flag that stops playing. */
   8.203 +    IXAudio2SourceVoice_Discontinuity(source);
   8.204 +    IXAudio2SourceVoice_GetState(source, &state);
   8.205 +    while (state.BuffersQueued > 0) {
   8.206 +        WaitForSingleObject(this->hidden->semaphore, INFINITE);
   8.207 +        IXAudio2SourceVoice_GetState(source, &state);
   8.208 +    }
   8.209 +}
   8.210 +
   8.211 +
   8.212 +static void
   8.213 +XAUDIO2_CloseDevice(_THIS)
   8.214 +{
   8.215 +    if (this->hidden != NULL) {
   8.216 +        IXAudio2 *ixa2 = this->hidden->ixa2;
   8.217 +        IXAudio2SourceVoice *source = this->hidden->source;
   8.218 +        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
   8.219 +
   8.220 +        if (source != NULL) {
   8.221 +            IXAudio2SourceVoice_Stop(source, 0, XAUDIO2_COMMIT_NOW);
   8.222 +            IXAudio2SourceVoice_FlushSourceBuffers(source);
   8.223 +            IXAudio2SourceVoice_DestroyVoice(source);
   8.224 +        }
   8.225 +        if (ixa2 != NULL) {
   8.226 +            IXAudio2_StopEngine(ixa2);
   8.227 +        }
   8.228 +        if (mastering != NULL) {
   8.229 +            IXAudio2MasteringVoice_DestroyVoice(mastering);
   8.230 +        }
   8.231 +        if (ixa2 != NULL) {
   8.232 +            IXAudio2_Release(ixa2);
   8.233 +        }
   8.234 +        if (this->hidden->mixbuf != NULL) {
   8.235 +            SDL_free(this->hidden->mixbuf);
   8.236 +        }
   8.237 +        if (this->hidden->semaphore != NULL) {
   8.238 +            CloseHandle(this->hidden->semaphore);
   8.239 +        }
   8.240 +
   8.241 +        SDL_free(this->hidden);
   8.242 +        this->hidden = NULL;
   8.243 +    }
   8.244 +}
   8.245 +
   8.246 +static int
   8.247 +XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
   8.248 +{
   8.249 +    HRESULT result = S_OK;
   8.250 +    WAVEFORMATEX waveformat;
   8.251 +    int valid_format = 0;
   8.252 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   8.253 +    IXAudio2 *ixa2 = NULL;
   8.254 +    IXAudio2SourceVoice *source = NULL;
   8.255 +    UINT32 devId = 0;  /* 0 == system default device. */
   8.256 +
   8.257 +	static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
   8.258 +	    VoiceCBOnVoiceProcessPassStart,
   8.259 +        VoiceCBOnVoiceProcessPassEnd,
   8.260 +        VoiceCBOnStreamEnd,
   8.261 +        VoiceCBOnBufferStart,
   8.262 +        VoiceCBOnBufferEnd,
   8.263 +        VoiceCBOnLoopEnd,
   8.264 +        VoiceCBOnVoiceError
   8.265 +	};
   8.266 +
   8.267 +	static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
   8.268 +
   8.269 +    if (iscapture) {
   8.270 +        SDL_SetError("XAudio2: capture devices unsupported.");
   8.271 +        return 0;
   8.272 +    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
   8.273 +        SDL_SetError("XAudio2: XAudio2Create() failed.");
   8.274 +        return 0;
   8.275 +    }
   8.276 +
   8.277 +    if (devname != NULL) {
   8.278 +        UINT32 devcount = 0;
   8.279 +        UINT32 i = 0;
   8.280 +
   8.281 +        if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
   8.282 +            IXAudio2_Release(ixa2);
   8.283 +            SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
   8.284 +            return 0;
   8.285 +        }
   8.286 +        for (i = 0; i < devcount; i++) {
   8.287 +            XAUDIO2_DEVICE_DETAILS details;
   8.288 +            if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
   8.289 +                char *str = utf16_to_utf8(details.DisplayName);
   8.290 +                if (str != NULL) {
   8.291 +                    const int match = (SDL_strcmp(str, devname) == 0);
   8.292 +                    SDL_free(str);
   8.293 +                    if (match) {
   8.294 +                        devId = i;
   8.295 +                        break;
   8.296 +                    }
   8.297 +                }
   8.298 +            }
   8.299 +        }
   8.300 +
   8.301 +        if (i == devcount) {
   8.302 +            IXAudio2_Release(ixa2);
   8.303 +            SDL_SetError("XAudio2: Requested device not found.");
   8.304 +            return 0;
   8.305 +        }
   8.306 +    }
   8.307 +
   8.308 +    /* Initialize all variables that we clean on shutdown */
   8.309 +    this->hidden = (struct SDL_PrivateAudioData *)
   8.310 +        SDL_malloc((sizeof *this->hidden));
   8.311 +    if (this->hidden == NULL) {
   8.312 +        IXAudio2_Release(ixa2);
   8.313 +        SDL_OutOfMemory();
   8.314 +        return 0;
   8.315 +    }
   8.316 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   8.317 +
   8.318 +    this->hidden->ixa2 = ixa2;
   8.319 +    this->hidden->semaphore = CreateSemaphore(NULL, 1, 2, NULL);
   8.320 +    if (this->hidden->semaphore == NULL) {
   8.321 +        XAUDIO2_CloseDevice(this);
   8.322 +        SDL_SetError("XAudio2: CreateSemaphore() failed!");
   8.323 +        return 0;
   8.324 +    }
   8.325 +
   8.326 +    while ((!valid_format) && (test_format)) {
   8.327 +        switch (test_format) {
   8.328 +        case AUDIO_U8:
   8.329 +        case AUDIO_S16:
   8.330 +        case AUDIO_S32:
   8.331 +        case AUDIO_F32:
   8.332 +            this->spec.format = test_format;
   8.333 +            valid_format = 1;
   8.334 +            break;
   8.335 +        }
   8.336 +        test_format = SDL_NextAudioFormat();
   8.337 +    }
   8.338 +
   8.339 +    if (!valid_format) {
   8.340 +        XAUDIO2_CloseDevice(this);
   8.341 +        SDL_SetError("XAudio2: Unsupported audio format");
   8.342 +        return 0;
   8.343 +    }
   8.344 +
   8.345 +    /* Update the fragment size as size in bytes */
   8.346 +    SDL_CalculateAudioSpec(&this->spec);
   8.347 +
   8.348 +    /* We feed a Source, it feeds the Mastering, which feeds the device. */
   8.349 +    this->hidden->mixlen = this->spec.size;
   8.350 +    this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen);
   8.351 +    if (this->hidden->mixbuf == NULL) {
   8.352 +        XAUDIO2_CloseDevice(this);
   8.353 +        SDL_OutOfMemory();
   8.354 +        return 0;
   8.355 +    }
   8.356 +    this->hidden->nextbuf = this->hidden->mixbuf;
   8.357 +    SDL_memset(this->hidden->mixbuf, '\0', 2 * this->hidden->mixlen);
   8.358 +
   8.359 +    /* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On
   8.360 +       Xbox360, this means 5.1 output, but on Windows, it means "figure out
   8.361 +       what the system has." It might be preferable to let XAudio2 blast
   8.362 +       stereo output to appropriate surround sound configurations
   8.363 +       instead of clamping to 2 channels, even though we'll configure the
   8.364 +       Source Voice for whatever number of channels you supply. */
   8.365 +    result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering,
   8.366 +                                           XAUDIO2_DEFAULT_CHANNELS,
   8.367 +                                           this->spec.freq, 0, devId, NULL);
   8.368 +    if (result != S_OK) {
   8.369 +        XAUDIO2_CloseDevice(this);
   8.370 +        SDL_SetError("XAudio2: Couldn't create mastering voice");
   8.371 +        return 0;
   8.372 +    }
   8.373 +
   8.374 +    SDL_zero(waveformat);
   8.375 +    if (SDL_AUDIO_ISFLOAT(this->spec.format)) {
   8.376 +        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
   8.377 +    } else {
   8.378 +        waveformat.wFormatTag = WAVE_FORMAT_PCM;
   8.379 +    }
   8.380 +    waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
   8.381 +    waveformat.nChannels = this->spec.channels;
   8.382 +    waveformat.nSamplesPerSec = this->spec.freq;
   8.383 +    waveformat.nBlockAlign =
   8.384 +        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
   8.385 +    waveformat.nAvgBytesPerSec =
   8.386 +        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
   8.387 +
   8.388 +    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
   8.389 +                                        XAUDIO2_VOICE_NOSRC |
   8.390 +                                        XAUDIO2_VOICE_NOPITCH,
   8.391 +                                        1.0f, &callbacks, NULL, NULL);
   8.392 +    if (result != S_OK) {
   8.393 +        XAUDIO2_CloseDevice(this);
   8.394 +        SDL_SetError("XAudio2: Couldn't create source voice");
   8.395 +        return 0;
   8.396 +    }
   8.397 +    this->hidden->source = source;
   8.398 +
   8.399 +    /* Start everything playing! */
   8.400 +    result = IXAudio2_StartEngine(ixa2);
   8.401 +    if (result != S_OK) {
   8.402 +        XAUDIO2_CloseDevice(this);
   8.403 +        SDL_SetError("XAudio2: Couldn't start engine");
   8.404 +        return 0;
   8.405 +    }
   8.406 +
   8.407 +    result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW);
   8.408 +    if (result != S_OK) {
   8.409 +        XAUDIO2_CloseDevice(this);
   8.410 +        SDL_SetError("XAudio2: Couldn't start source voice");
   8.411 +        return 0;
   8.412 +    }
   8.413 +
   8.414 +    return 1; /* good to go. */
   8.415 +}
   8.416 +
   8.417 +static void
   8.418 +XAUDIO2_Deinitialize(void)
   8.419 +{
   8.420 +    WIN_CoUninitialize();
   8.421 +}
   8.422 +
   8.423 +static int
   8.424 +XAUDIO2_Init(SDL_AudioDriverImpl * impl)
   8.425 +{
   8.426 +    /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
   8.427 +    IXAudio2 *ixa2 = NULL;
   8.428 +    if (FAILED(WIN_CoInitialize())) {
   8.429 +        SDL_SetError("XAudio2: CoInitialize() failed");
   8.430 +        return 0;
   8.431 +    }
   8.432 +
   8.433 +    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
   8.434 +        WIN_CoUninitialize();
   8.435 +        SDL_SetError("XAudio2: XAudio2Create() failed");
   8.436 +        return 0;  /* not available. */
   8.437 +    }
   8.438 +    IXAudio2_Release(ixa2);
   8.439 +
   8.440 +    /* Set the function pointers */
   8.441 +    impl->DetectDevices = XAUDIO2_DetectDevices;
   8.442 +    impl->GetDeviceName = XAUDIO2_GetDeviceName;
   8.443 +    impl->OpenDevice = XAUDIO2_OpenDevice;
   8.444 +    impl->PlayDevice = XAUDIO2_PlayDevice;
   8.445 +    impl->WaitDevice = XAUDIO2_WaitDevice;
   8.446 +    impl->WaitDone = XAUDIO2_WaitDone;
   8.447 +    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
   8.448 +    impl->CloseDevice = XAUDIO2_CloseDevice;
   8.449 +    impl->Deinitialize = XAUDIO2_Deinitialize;
   8.450 +
   8.451 +    return 1;   /* this audio target is available. */
   8.452 +}
   8.453 +
   8.454 +AudioBootStrap XAUDIO2_bootstrap = {
   8.455 +    "xaudio2", "XAudio2", XAUDIO2_Init, 0
   8.456 +};
   8.457 +
   8.458 +/* vi: set ts=4 sw=4 expandtab: */
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/audio/xaudio2/SDL_xaudio2.h	Thu Aug 04 01:07:09 2011 -0400
     9.3 @@ -0,0 +1,46 @@
     9.4 +/*
     9.5 +  Simple DirectMedia Layer
     9.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     9.7 +
     9.8 +  This software is provided 'as-is', without any express or implied
     9.9 +  warranty.  In no event will the authors be held liable for any damages
    9.10 +  arising from the use of this software.
    9.11 +
    9.12 +  Permission is granted to anyone to use this software for any purpose,
    9.13 +  including commercial applications, and to alter it and redistribute it
    9.14 +  freely, subject to the following restrictions:
    9.15 +
    9.16 +  1. The origin of this software must not be misrepresented; you must not
    9.17 +     claim that you wrote the original software. If you use this software
    9.18 +     in a product, an acknowledgment in the product documentation would be
    9.19 +     appreciated but is not required.
    9.20 +  2. Altered source versions must be plainly marked as such, and must not be
    9.21 +     misrepresented as being the original software.
    9.22 +  3. This notice may not be removed or altered from any source distribution.
    9.23 +*/
    9.24 +#include "SDL_config.h"
    9.25 +
    9.26 +#ifndef _SDL_xaudio2_h
    9.27 +#define _SDL_xaudio2_h
    9.28 +
    9.29 +#include "../SDL_sysaudio.h"
    9.30 +
    9.31 +#include <XAudio2.h>
    9.32 +
    9.33 +/* Hidden "this" pointer for the audio functions */
    9.34 +#define _THIS	SDL_AudioDevice *this
    9.35 +
    9.36 +struct SDL_PrivateAudioData
    9.37 +{
    9.38 +    IXAudio2 *ixa2;
    9.39 +    IXAudio2SourceVoice *source;
    9.40 +    IXAudio2MasteringVoice *mastering;
    9.41 +    HANDLE semaphore;
    9.42 +    Uint8 *mixbuf;
    9.43 +    int mixlen;
    9.44 +    Uint8 *nextbuf;
    9.45 +};
    9.46 +
    9.47 +#endif /* _SDL_xaudio2_h */
    9.48 +
    9.49 +/* vi: set ts=4 sw=4 expandtab: */