src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp
author David Ludwig
Fri, 06 Sep 2013 19:07:15 -0400
changeset 8519 0a334fc866b0
parent 8397 8cd319461894
child 8582 c3e9a2b93517
permissions -rw-r--r--
WinRT: made SDL_xaudio2.c compile as C code when building for WinRT

XAudio2 2.8's header file, xaudio2.h, doesn't compile in plain C code for WinRT
apps, not automatically at least. Initially, this file was adapted to compile
as C++, however these changes are now deprecated in favor of some preprocessor
based hacks that should get xaudio2.h to compile (while making sure XAudio2
still works).
     1 
     2 #include <xaudio2.h>
     3 #include "SDL_xaudio2_winrthelpers.h"
     4 
     5 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
     6 using Windows::Devices::Enumeration::DeviceClass;
     7 using Windows::Devices::Enumeration::DeviceInformation;
     8 using Windows::Devices::Enumeration::DeviceInformationCollection;
     9 #endif
    10 
    11 extern "C" HRESULT __cdecl IXAudio2_GetDeviceCount(IXAudio2 * ixa2, UINT32 * devcount)
    12 {
    13 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    14     // There doesn't seem to be any audio device enumeration on Windows Phone.
    15     // In lieu of this, just treat things as if there is one and only one
    16     // audio device.
    17     *devcount = 1;
    18     return S_OK;
    19 #else
    20     // TODO, WinRT: make xaudio2 device enumeration only happen once, and in the background
    21     auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
    22     while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
    23     {
    24     }
    25  
    26     DeviceInformationCollection^ devices = operation->GetResults();
    27     *devcount = devices->Size;
    28     return S_OK;
    29 #endif
    30 }
    31 
    32 extern "C" HRESULT IXAudio2_GetDeviceDetails(IXAudio2 * unused, UINT32 index, XAUDIO2_DEVICE_DETAILS * details)
    33 {
    34 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    35     // Windows Phone doesn't seem to have the same device enumeration APIs that
    36     // Windows 8/RT has, or it doesn't have them at all.  In lieu of this,
    37     // just treat things as if there is one, and only one, default device.
    38     if (index != 0)
    39     {
    40         return XAUDIO2_E_INVALID_CALL;
    41     }
    42 
    43     if (details)
    44     {
    45         wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), L"default", _TRUNCATE);
    46         wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), L"default", _TRUNCATE);
    47     }
    48     return S_OK;
    49 #else
    50     auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
    51     while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
    52     {
    53     }
    54  
    55     DeviceInformationCollection^ devices = operation->GetResults();
    56     if (index >= devices->Size)
    57     {
    58         return XAUDIO2_E_INVALID_CALL;
    59     }
    60 
    61     DeviceInformation^ d = devices->GetAt(index);
    62     if (details)
    63     {
    64         wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), d->Id->Data(), _TRUNCATE);
    65         wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), d->Name->Data(), _TRUNCATE);
    66     }
    67     return S_OK;
    68 #endif
    69 }