src/audio/xaudio2/SDL_xaudio2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 26 May 2013 12:20:23 -0700
changeset 7226 1c440669e32c
parent 7218 08c403ba173e
child 7689 c64896ddeeae
child 8477 ad08f0d710f3
permissions -rw-r--r--
Fixed compiling XAudio2 code with mingw64
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
7226
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    31
#ifdef __GNUC__
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    32
/* The configure script already did any necessary checking */
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    33
#  define SDL_XAUDIO2_HAS_SDK 1
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    34
#else
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    35
#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
    36
#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
    37
#  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
    38
#else
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
    39
#  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
    40
#endif
7226
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    41
#endif /* __GNUC__ */
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
    43
#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
    44
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    45
#define INITGUID 1
7226
1c440669e32c Fixed compiling XAudio2 code with mingw64
Sam Lantinga <slouken@libsdl.org>
parents: 7218
diff changeset
    46
#include <xaudio2.h>
5635
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
/* Hidden "this" pointer for the audio functions */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
    49
#define _THIS   SDL_AudioDevice *this
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    50
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    51
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
    52
{
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    53
    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
    54
    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
    55
    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
    56
    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
    57
    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
    58
    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
    59
    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
    60
};
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    61
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    62
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
static __inline__ char *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
utf16_to_utf8(const WCHAR *S)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    /* !!! 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
    67
    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
    68
                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
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
    71
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
    72
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
    73
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
        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
    80
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
7218
08c403ba173e Changed three similar error messages to contain more context.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    82
        SDL_SetError("XAudio2: XAudio2Create() failed at detection.");
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
    } 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
    85
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        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
    87
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        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
    93
            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
    94
            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
    95
                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
    96
                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
    97
            }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    IXAudio2_Release(ixa2);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
VoiceCBOnBufferEnd(THIS_ void *data)
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
    /* 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
   108
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
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
    /* !!! 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
   116
    SDL_assert(0 && "write me!");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
/* no-op callbacks... */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
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
   122
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
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
   124
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
   125
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
static Uint8 *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
XAUDIO2_GetDeviceBuf(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    return this->hidden->nextbuf;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
XAUDIO2_PlayDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    XAUDIO2_BUFFER buffer;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    Uint8 *mixbuf = this->hidden->mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    Uint8 *nextbuf = this->hidden->nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    const int mixlen = this->hidden->mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    HRESULT result = S_OK;
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
    if (!this->enabled) { /* shutting down? */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        return;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    /* Submit the next filled buffer */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    SDL_zero(buffer);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    buffer.AudioBytes = mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    buffer.pAudioData = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    buffer.pContext = this;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    if (nextbuf == mixbuf) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
        nextbuf += mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        nextbuf = mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    this->hidden->nextbuf = nextbuf;
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
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        /* !!! 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
   163
    }
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
    if (result != S_OK) {  /* uhoh, panic! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        this->enabled = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
XAUDIO2_WaitDevice(_THIS)
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
    if (this->enabled) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
XAUDIO2_WaitDone(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    XAUDIO2_VOICE_STATE state;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    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
   185
    IXAudio2SourceVoice_Discontinuity(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    while (state.BuffersQueued > 0) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
}
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
XAUDIO2_CloseDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    if (this->hidden != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        IXAudio2 *ixa2 = this->hidden->ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        if (source != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
            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
   204
            IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
            IXAudio2SourceVoice_DestroyVoice(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
            IXAudio2_StopEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        if (mastering != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
            IXAudio2MasteringVoice_DestroyVoice(mastering);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        if (this->hidden->mixbuf != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
            SDL_free(this->hidden->mixbuf);
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
        if (this->hidden->semaphore != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
            CloseHandle(this->hidden->semaphore);
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
        SDL_free(this->hidden);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        this->hidden = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
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
   230
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    HRESULT result = S_OK;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    WAVEFORMATEX waveformat;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    int valid_format = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    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
   235
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    IXAudio2SourceVoice *source = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    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
   238
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   239
    static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   240
        VoiceCBOnVoiceProcessPassStart,
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
        VoiceCBOnVoiceProcessPassEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
        VoiceCBOnStreamEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        VoiceCBOnBufferStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        VoiceCBOnBufferEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        VoiceCBOnLoopEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        VoiceCBOnVoiceError
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   247
    };
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   249
    static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    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
   252
        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
   253
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
7218
08c403ba173e Changed three similar error messages to contain more context.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
   254
        return SDL_SetError("XAudio2: XAudio2Create() failed at open.");
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    }
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 (devname != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
        UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
        UINT32 i = 0;
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
        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
   262
            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
   263
            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
   264
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
        for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
            XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
            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
   268
                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
   269
                if (str != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
                    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
   271
                    SDL_free(str);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
                    if (match) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
                        devId = i;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
                        break;
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
                }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
            }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        }
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
        if (i == devcount) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
            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
   282
            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
   283
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    /* 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
   287
    this->hidden = (struct SDL_PrivateAudioData *)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        SDL_malloc((sizeof *this->hidden));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    if (this->hidden == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        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
   291
        return SDL_OutOfMemory();
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    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
   294
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    this->hidden->ixa2 = ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    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
   297
    if (this->hidden->semaphore == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        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
   299
        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
   300
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    while ((!valid_format) && (test_format)) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        switch (test_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        case AUDIO_U8:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        case AUDIO_S16:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        case AUDIO_S32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        case AUDIO_F32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
            this->spec.format = test_format;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
            valid_format = 1;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
            break;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
        test_format = SDL_NextAudioFormat();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    }
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
    if (!valid_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
        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
   317
        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
   318
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    /* 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
   321
    SDL_CalculateAudioSpec(&this->spec);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    /* 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
   324
    this->hidden->mixlen = this->spec.size;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    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
   326
    if (this->hidden->mixbuf == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        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
   328
        return SDL_OutOfMemory();
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    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
   331
    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
   332
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    /* 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
   334
       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
   335
       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
   336
       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
   337
       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
   338
       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
   339
    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
   340
                                           XAUDIO2_DEFAULT_CHANNELS,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
                                           this->spec.freq, 0, devId, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
        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
   344
        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
   345
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    SDL_zero(waveformat);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    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
   349
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    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
   354
    waveformat.nChannels = this->spec.channels;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    waveformat.nSamplesPerSec = this->spec.freq;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    waveformat.nBlockAlign =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    waveformat.nAvgBytesPerSec =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
                                        XAUDIO2_VOICE_NOSRC |
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
                                        XAUDIO2_VOICE_NOPITCH,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
                                        1.0f, &callbacks, NULL, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
        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
   367
        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
   368
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    this->hidden->source = source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    /* Start everything playing! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    result = IXAudio2_StartEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        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
   375
        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
   376
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    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
   379
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        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
   381
        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
   382
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   384
    return 0; /* good to go. */
5592
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
XAUDIO2_Deinitialize(void)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   393
#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
   394
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   395
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
{
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   399
#ifndef SDL_XAUDIO2_HAS_SDK
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   400
    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
   401
    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
   402
#else
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
    /* 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
   404
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    if (FAILED(WIN_CoInitialize())) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
        SDL_SetError("XAudio2: CoInitialize() failed");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    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
   411
        WIN_CoUninitialize();
7218
08c403ba173e Changed three similar error messages to contain more context.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
   412
        SDL_SetError("XAudio2: XAudio2Create() failed at initialization");
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
        return 0;  /* not available. */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
    IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
    /* Set the function pointers */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    impl->DetectDevices = XAUDIO2_DetectDevices;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    impl->OpenDevice = XAUDIO2_OpenDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
    impl->PlayDevice = XAUDIO2_PlayDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    impl->WaitDevice = XAUDIO2_WaitDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    impl->WaitDone = XAUDIO2_WaitDone;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    impl->CloseDevice = XAUDIO2_CloseDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    impl->Deinitialize = XAUDIO2_Deinitialize;
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
    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
   428
#endif
5592
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
AudioBootStrap XAUDIO2_bootstrap = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
};
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   435
#endif  /* SDL_AUDIO_DRIVER_XAUDIO2 */
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   436
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
/* vi: set ts=4 sw=4 expandtab: */