WinRT: got XAudio2 sort-of working (it plays stuff poorly, then crashes)
authorDavid Ludwig <dludwig@pobox.com>
Sat, 24 Nov 2012 11:19:06 -0500
changeset 8361eefad2ec4f76
parent 8360 7f1bc00e59fc
child 8362 ec8b86ada110
WinRT: got XAudio2 sort-of working (it plays stuff poorly, then crashes)
src/audio/xaudio2/SDL_xaudio2.c
src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp
     1.1 --- a/src/audio/xaudio2/SDL_xaudio2.c	Sat Nov 24 11:17:23 2012 -0500
     1.2 +++ b/src/audio/xaudio2/SDL_xaudio2.c	Sat Nov 24 11:19:06 2012 -0500
     1.3 @@ -128,14 +128,6 @@
     1.4  };
     1.5  
     1.6  
     1.7 -static __inline__ char *
     1.8 -utf16_to_utf8(const WCHAR *S)
     1.9 -{
    1.10 -    /* !!! FIXME: this should be UTF-16, not UCS-2! */
    1.11 -    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
    1.12 -                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
    1.13 -}
    1.14 -
    1.15  static void
    1.16  XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
    1.17  {
    1.18 @@ -159,7 +151,7 @@
    1.19      for (i = 0; i < devcount; i++) {
    1.20          XAUDIO2_DEVICE_DETAILS details;
    1.21          if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
    1.22 -            char *str = utf16_to_utf8(details.DisplayName);
    1.23 +            char *str = WIN_StringToUTF8(details.DisplayName);
    1.24              if (str != NULL) {
    1.25                  addfn(str);
    1.26                  SDL_free(str);  /* addfn() made a copy of the string. */
    1.27 @@ -313,7 +305,7 @@
    1.28              SDL_free(this->hidden->mixbuf);
    1.29          }
    1.30          if (this->hidden->semaphore != NULL) {
    1.31 -            CloseHandle(this->hidden->semaphore);
    1.32 +            SDL_DestroySemaphore(this->hidden->semaphore);
    1.33          }
    1.34  
    1.35          SDL_free(this->hidden);
    1.36 @@ -331,7 +323,8 @@
    1.37      IXAudio2 *ixa2 = NULL;
    1.38      IXAudio2SourceVoice *source = NULL;
    1.39  #if defined(__WINRT__)
    1.40 -    WCHAR devId[256];
    1.41 +    WCHAR devIdBuffer[256];
    1.42 +    LPCWSTR devId = 0;
    1.43  #else
    1.44      UINT32 devId = 0;  /* 0 == system default device. */
    1.45  #endif
    1.46 @@ -353,7 +346,7 @@
    1.47  #endif // ! defined(__cplusplus)
    1.48  
    1.49  #if defined(__WINRT__)
    1.50 -    SDL_zero(devId);
    1.51 +    SDL_zero(devIdBuffer);
    1.52  #endif
    1.53  
    1.54      if (iscapture) {
    1.55 @@ -363,6 +356,14 @@
    1.56          SDL_SetError("XAudio2: XAudio2Create() failed.");
    1.57          return 0;
    1.58      }
    1.59 +    XAUDIO2_DEBUG_CONFIGURATION debugConfig;
    1.60 +    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;
    1.61 +    debugConfig.BreakMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS;
    1.62 +    debugConfig.LogThreadID = TRUE;
    1.63 +    debugConfig.LogFileline = TRUE;
    1.64 +    debugConfig.LogFunctionName = TRUE;
    1.65 +    debugConfig.LogTiming = TRUE;
    1.66 +    ixa2->SetDebugConfiguration(&debugConfig);
    1.67  
    1.68      if (devname != NULL) {
    1.69          UINT32 devcount = 0;
    1.70 @@ -376,13 +377,14 @@
    1.71          for (i = 0; i < devcount; i++) {
    1.72              XAUDIO2_DEVICE_DETAILS details;
    1.73              if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
    1.74 -                char *str = utf16_to_utf8(details.DisplayName);
    1.75 +                char *str = WIN_StringToUTF8(details.DisplayName);
    1.76                  if (str != NULL) {
    1.77                      const int match = (SDL_strcmp(str, devname) == 0);
    1.78                      SDL_free(str);
    1.79                      if (match) {
    1.80  #if defined(__WINRT__)
    1.81 -                        wcsncpy_s(devId, ARRAYSIZE(devId), details.DeviceID, _TRUNCATE);
    1.82 +                        wcsncpy_s(devIdBuffer, ARRAYSIZE(devIdBuffer), details.DeviceID, _TRUNCATE);
    1.83 +                        devId = (LPCWSTR) &devIdBuffer;
    1.84  #else
    1.85                          devId = i;
    1.86  #endif
    1.87 @@ -478,11 +480,19 @@
    1.88          waveformat.nChannels * (waveformat.wBitsPerSample / 8);
    1.89      waveformat.nAvgBytesPerSec =
    1.90          waveformat.nSamplesPerSec * waveformat.nBlockAlign;
    1.91 +    waveformat.cbSize = sizeof(waveformat);
    1.92  
    1.93 +#ifdef __WINRT__
    1.94 +    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
    1.95 +                                        0,
    1.96 +                                        1.0f, &callbacks, NULL, NULL);
    1.97 +#else
    1.98      result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
    1.99                                          XAUDIO2_VOICE_NOSRC |
   1.100                                          XAUDIO2_VOICE_NOPITCH,
   1.101                                          1.0f, &callbacks, NULL, NULL);
   1.102 +
   1.103 +#endif
   1.104      if (result != S_OK) {
   1.105          XAUDIO2_CloseDevice(this);
   1.106          SDL_SetError("XAudio2: Couldn't create source voice");
   1.107 @@ -529,6 +539,7 @@
   1.108      /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
   1.109      IXAudio2 *ixa2 = NULL;
   1.110  #if defined(__WIN32__)
   1.111 +    // TODO, WinRT: Investigate using CoInitializeEx here
   1.112      if (FAILED(WIN_CoInitialize())) {
   1.113          SDL_SetError("XAudio2: CoInitialize() failed");
   1.114          return 0;
     2.1 --- a/src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp	Sat Nov 24 11:17:23 2012 -0500
     2.2 +++ b/src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp	Sat Nov 24 11:19:06 2012 -0500
     2.3 @@ -8,6 +8,7 @@
     2.4  
     2.5  HRESULT IXAudio2_GetDeviceCount(IXAudio2 * ixa2, UINT32 * devcount)
     2.6  {
     2.7 +    // TODO, WinRT: make xaudio2 device enumeration only happen once, and in the background
     2.8      auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
     2.9      while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
    2.10      {