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