src/audio/xaudio2/SDL_xaudio2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Oct 2013 23:15:27 -0700
changeset 7830 722c44cad199
parent 7719 31b5f9ff36ca
child 7923 d9ecdf71effb
child 8535 e8ee0708ef5c
permissions -rw-r--r--
Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
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
7830
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    51
/* Fixes bug 1210 where some versions of gcc need named parameters */
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    52
#ifdef __GNUC__
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    53
#ifdef THIS
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    54
#undef THIS
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    55
#endif
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    56
#define THIS    INTERFACE *p
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    57
#ifdef THIS_
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    58
#undef THIS_
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    59
#endif
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    60
#define THIS_   INTERFACE *p,
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    61
#endif
722c44cad199 Fixed 1598 - Mingwin build fails on src/audio/xaudio2/SDL_xaudio2.c
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
    62
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    63
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
    64
{
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    65
    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
    66
    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
    67
    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
    68
    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
    69
    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
    70
    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
    71
    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
    72
};
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    73
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    74
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
    75
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
    76
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
    77
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        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
    84
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    } 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
    86
        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
    87
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    } 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
    89
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        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
    91
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    }
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
    for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
        XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
7689
c64896ddeeae XAudio2/DirectSound: Use the usual Windows string convert (thanks, Norfanin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7226
diff changeset
    97
            char *str = WIN_StringToUTF8(details.DisplayName);
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            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
    99
                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
   100
                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
   101
            }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    IXAudio2_Release(ixa2);
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
VoiceCBOnBufferEnd(THIS_ void *data)
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
    /* 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
   112
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
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
    /* !!! 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
   120
    SDL_assert(0 && "write me!");
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
/* no-op callbacks... */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
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
   126
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
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
   128
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
   129
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
static Uint8 *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
XAUDIO2_GetDeviceBuf(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    return this->hidden->nextbuf;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
XAUDIO2_PlayDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    XAUDIO2_BUFFER buffer;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    Uint8 *mixbuf = this->hidden->mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    Uint8 *nextbuf = this->hidden->nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    const int mixlen = this->hidden->mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    HRESULT result = S_OK;
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
    if (!this->enabled) { /* shutting down? */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        return;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    /* Submit the next filled buffer */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    SDL_zero(buffer);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    buffer.AudioBytes = mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    buffer.pAudioData = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    buffer.pContext = this;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    if (nextbuf == mixbuf) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        nextbuf += mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        nextbuf = mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    this->hidden->nextbuf = nextbuf;
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
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        /* !!! 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
   167
    }
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
    if (result != S_OK) {  /* uhoh, panic! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
        this->enabled = 0;
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_WaitDevice(_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
    if (this->enabled) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
XAUDIO2_WaitDone(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    XAUDIO2_VOICE_STATE state;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    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
   189
    IXAudio2SourceVoice_Discontinuity(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    while (state.BuffersQueued > 0) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
}
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
XAUDIO2_CloseDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    if (this->hidden != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        IXAudio2 *ixa2 = this->hidden->ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
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 (source != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
            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
   208
            IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
            IXAudio2SourceVoice_DestroyVoice(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
            IXAudio2_StopEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        if (mastering != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
            IXAudio2MasteringVoice_DestroyVoice(mastering);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
        }
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7689
diff changeset
   220
        SDL_free(this->hidden->mixbuf);
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        if (this->hidden->semaphore != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
            CloseHandle(this->hidden->semaphore);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
        SDL_free(this->hidden);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        this->hidden = NULL;
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
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
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
   232
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    HRESULT result = S_OK;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    WAVEFORMATEX waveformat;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    int valid_format = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    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
   237
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    IXAudio2SourceVoice *source = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    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
   240
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   241
    static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   242
        VoiceCBOnVoiceProcessPassStart,
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        VoiceCBOnVoiceProcessPassEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        VoiceCBOnStreamEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        VoiceCBOnBufferStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        VoiceCBOnBufferEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        VoiceCBOnLoopEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        VoiceCBOnVoiceError
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   249
    };
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   251
    static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
5592
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 (iscapture) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   254
        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
   255
    } 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
   256
        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
   257
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    if (devname != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
        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
   264
            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
   265
            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
   266
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
        for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
            XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
            if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
7689
c64896ddeeae XAudio2/DirectSound: Use the usual Windows string convert (thanks, Norfanin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7226
diff changeset
   270
                char *str = WIN_StringToUTF8(details.DisplayName);
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
                if (str != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
                    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
   273
                    SDL_free(str);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
                    if (match) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
                        devId = i;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
                        break;
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
        }
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
        if (i == devcount) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
            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
   284
            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
   285
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    /* 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
   289
    this->hidden = (struct SDL_PrivateAudioData *)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        SDL_malloc((sizeof *this->hidden));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    if (this->hidden == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
        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
   293
        return SDL_OutOfMemory();
5592
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
    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
   296
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    this->hidden->ixa2 = ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    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
   299
    if (this->hidden->semaphore == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        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
   301
        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
   302
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    while ((!valid_format) && (test_format)) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        switch (test_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        case AUDIO_U8:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        case AUDIO_S16:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
        case AUDIO_S32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        case AUDIO_F32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
            this->spec.format = test_format;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
            valid_format = 1;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
            break;
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
        test_format = SDL_NextAudioFormat();
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    if (!valid_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        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
   319
        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
   320
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    /* 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
   323
    SDL_CalculateAudioSpec(&this->spec);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    /* 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
   326
    this->hidden->mixlen = this->spec.size;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    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
   328
    if (this->hidden->mixbuf == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        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
   330
        return SDL_OutOfMemory();
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    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
   333
    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
   334
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    /* 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
   336
       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
   337
       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
   338
       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
   339
       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
   340
       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
   341
    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
   342
                                           XAUDIO2_DEFAULT_CHANNELS,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
                                           this->spec.freq, 0, devId, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        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
   346
        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
   347
    }
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
    SDL_zero(waveformat);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    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
   351
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    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
   356
    waveformat.nChannels = this->spec.channels;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    waveformat.nSamplesPerSec = this->spec.freq;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    waveformat.nBlockAlign =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    waveformat.nAvgBytesPerSec =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
                                        XAUDIO2_VOICE_NOSRC |
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
                                        XAUDIO2_VOICE_NOPITCH,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
                                        1.0f, &callbacks, NULL, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
        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
   369
        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
   370
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    this->hidden->source = source;
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
    /* Start everything playing! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    result = IXAudio2_StartEngine(ixa2);
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 engine");
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    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
   381
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
        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
   383
        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
   384
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   386
    return 0; /* good to go. */
5592
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
XAUDIO2_Deinitialize(void)
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
    WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
}
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
#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
   396
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   397
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
{
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   401
#ifndef SDL_XAUDIO2_HAS_SDK
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   402
    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
   403
    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
   404
#else
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    /* 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
   406
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    if (FAILED(WIN_CoInitialize())) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
        SDL_SetError("XAudio2: CoInitialize() failed");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    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
   413
        WIN_CoUninitialize();
7218
08c403ba173e Changed three similar error messages to contain more context.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
   414
        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
   415
        return 0;  /* not available. */
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
    IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    /* Set the function pointers */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
    impl->DetectDevices = XAUDIO2_DetectDevices;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    impl->OpenDevice = XAUDIO2_OpenDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    impl->PlayDevice = XAUDIO2_PlayDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    impl->WaitDevice = XAUDIO2_WaitDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    impl->WaitDone = XAUDIO2_WaitDone;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    impl->CloseDevice = XAUDIO2_CloseDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    impl->Deinitialize = XAUDIO2_Deinitialize;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    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
   430
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
AudioBootStrap XAUDIO2_bootstrap = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
};
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
5636
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   437
#endif  /* SDL_AUDIO_DRIVER_XAUDIO2 */
44bfdab5eaf1 Further XAudio2 build test cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 5635
diff changeset
   438
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
/* vi: set ts=4 sw=4 expandtab: */