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

Commit

Permalink
WinRT: got XAudio2 sort-of working (it plays stuff poorly, then crashes)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLudwig committed Nov 24, 2012
1 parent 551b558 commit 0e68df4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/audio/xaudio2/SDL_xaudio2.c
Expand Up @@ -128,14 +128,6 @@ struct SDL_PrivateAudioData
};


static __inline__ char *
utf16_to_utf8(const WCHAR *S)
{
/* !!! FIXME: this should be UTF-16, not UCS-2! */
return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
(SDL_wcslen(S)+1)*sizeof(WCHAR));
}

static void
XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
{
Expand All @@ -159,7 +151,7 @@ XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
for (i = 0; i < devcount; i++) {
XAUDIO2_DEVICE_DETAILS details;
if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
char *str = utf16_to_utf8(details.DisplayName);
char *str = WIN_StringToUTF8(details.DisplayName);
if (str != NULL) {
addfn(str);
SDL_free(str); /* addfn() made a copy of the string. */
Expand Down Expand Up @@ -313,7 +305,7 @@ XAUDIO2_CloseDevice(_THIS)
SDL_free(this->hidden->mixbuf);
}
if (this->hidden->semaphore != NULL) {
CloseHandle(this->hidden->semaphore);
SDL_DestroySemaphore(this->hidden->semaphore);
}

SDL_free(this->hidden);
Expand All @@ -331,7 +323,8 @@ XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
IXAudio2 *ixa2 = NULL;
IXAudio2SourceVoice *source = NULL;
#if defined(__WINRT__)
WCHAR devId[256];
WCHAR devIdBuffer[256];
LPCWSTR devId = 0;
#else
UINT32 devId = 0; /* 0 == system default device. */
#endif
Expand All @@ -353,7 +346,7 @@ XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
#endif // ! defined(__cplusplus)

#if defined(__WINRT__)
SDL_zero(devId);
SDL_zero(devIdBuffer);
#endif

if (iscapture) {
Expand All @@ -363,6 +356,14 @@ XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
SDL_SetError("XAudio2: XAudio2Create() failed.");
return 0;
}
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);

if (devname != NULL) {
UINT32 devcount = 0;
Expand All @@ -376,13 +377,14 @@ XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
for (i = 0; i < devcount; i++) {
XAUDIO2_DEVICE_DETAILS details;
if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
char *str = utf16_to_utf8(details.DisplayName);
char *str = WIN_StringToUTF8(details.DisplayName);
if (str != NULL) {
const int match = (SDL_strcmp(str, devname) == 0);
SDL_free(str);
if (match) {
#if defined(__WINRT__)
wcsncpy_s(devId, ARRAYSIZE(devId), details.DeviceID, _TRUNCATE);
wcsncpy_s(devIdBuffer, ARRAYSIZE(devIdBuffer), details.DeviceID, _TRUNCATE);
devId = (LPCWSTR) &devIdBuffer;
#else
devId = i;
#endif
Expand Down Expand Up @@ -478,11 +480,19 @@ XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
waveformat.nChannels * (waveformat.wBitsPerSample / 8);
waveformat.nAvgBytesPerSec =
waveformat.nSamplesPerSec * waveformat.nBlockAlign;
waveformat.cbSize = sizeof(waveformat);

#ifdef __WINRT__
result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
0,
1.0f, &callbacks, NULL, NULL);
#else
result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
XAUDIO2_VOICE_NOSRC |
XAUDIO2_VOICE_NOPITCH,
1.0f, &callbacks, NULL, NULL);

#endif
if (result != S_OK) {
XAUDIO2_CloseDevice(this);
SDL_SetError("XAudio2: Couldn't create source voice");
Expand Down Expand Up @@ -529,6 +539,7 @@ XAUDIO2_Init(SDL_AudioDriverImpl * impl)
/* XAudio2Create() is a macro that uses COM; we don't load the .dll */
IXAudio2 *ixa2 = NULL;
#if defined(__WIN32__)
// TODO, WinRT: Investigate using CoInitializeEx here
if (FAILED(WIN_CoInitialize())) {
SDL_SetError("XAudio2: CoInitialize() failed");
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp
Expand Up @@ -8,6 +8,7 @@ using Windows::Devices::Enumeration::DeviceInformationCollection;

HRESULT IXAudio2_GetDeviceCount(IXAudio2 * ixa2, UINT32 * devcount)
{
// TODO, WinRT: make xaudio2 device enumeration only happen once, and in the background
auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
{
Expand Down

0 comments on commit 0e68df4

Please sign in to comment.