src/audio/xaudio2/SDL_xaudio2.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 22 Aug 2011 14:37:45 -0400
changeset 5635 bef0f093c9da
parent 5634 093e60544778
child 5636 44bfdab5eaf1
permissions -rw-r--r--
Removed SDL_xaudio2.h ... no real need for this to be separate.
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include "../../core/windows/SDL_windows.h"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#include "SDL_audio.h"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#include "../SDL_audio_c.h"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#include "SDL_assert.h"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    27
#include "../SDL_sysaudio.h"
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    28
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    29
#if SDL_AUDIO_DRIVER_XAUDIO2
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    30
#include <dxsdkver.h> /* XAudio2 exists as of the March 2008 DirectX SDK */
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    31
#if (defined(_DXSDK_BUILD_MAJOR) && (_DXSDK_BUILD_MAJOR >= 1284))
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    32
#   define SDL_HAVE_XAUDIO2_H 1
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    33
#endif
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    34
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
5634
093e60544778 Let XAudio2 target be removed from the build by removing it from SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 5615
diff changeset
    36
#ifdef SDL_HAVE_XAUDIO2_H
5615
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
    37
5635
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    38
#define INITGUID 1
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    39
#include <XAudio2.h>
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    40
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    41
/* Hidden "this" pointer for the audio functions */
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    42
#define _THIS	SDL_AudioDevice *this
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    43
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    44
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
    45
{
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    46
    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
    47
    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
    48
    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
    49
    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
    50
    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
    51
    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
    52
    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
    53
};
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    54
bef0f093c9da Removed SDL_xaudio2.h ... no real need for this to be separate.
Ryan C. Gordon <icculus@icculus.org>
parents: 5634
diff changeset
    55
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
static __inline__ char *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
utf16_to_utf8(const WCHAR *S)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    /* !!! 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
    60
    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
    61
                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
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
    64
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
    65
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
    66
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    void *ptr = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        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
    74
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
        SDL_SetError("XAudio2: XAudio2Create() failed.");
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
    77
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    } 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
    79
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
        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
    81
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        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
    87
            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
    88
            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
    89
                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
    90
                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
    91
            }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
VoiceCBOnBufferEnd(THIS_ void *data)
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
    /* 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
   102
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    /* !!! 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
   110
    SDL_assert(0 && "write me!");
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
/* no-op callbacks... */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
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
   116
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
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
   118
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
   119
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
static Uint8 *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
XAUDIO2_GetDeviceBuf(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    return this->hidden->nextbuf;
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 void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
XAUDIO2_PlayDevice(_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
    XAUDIO2_BUFFER buffer;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    Uint8 *mixbuf = this->hidden->mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    Uint8 *nextbuf = this->hidden->nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    const int mixlen = this->hidden->mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    HRESULT result = S_OK;
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
    if (!this->enabled) { /* shutting down? */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        return;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    /* Submit the next filled buffer */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    SDL_zero(buffer);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    buffer.AudioBytes = mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    buffer.pAudioData = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    buffer.pContext = this;
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 (nextbuf == mixbuf) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        nextbuf += mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
        nextbuf = mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    this->hidden->nextbuf = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        /* !!! 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
   157
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    if (result != S_OK) {  /* uhoh, panic! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        this->enabled = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    }
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
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
XAUDIO2_WaitDevice(_THIS)
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
    if (this->enabled) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
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
}
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
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
XAUDIO2_WaitDone(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    XAUDIO2_VOICE_STATE state;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    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
   179
    IXAudio2SourceVoice_Discontinuity(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    while (state.BuffersQueued > 0) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    }
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
XAUDIO2_CloseDevice(_THIS)
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
    if (this->hidden != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        IXAudio2 *ixa2 = this->hidden->ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
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
        if (source != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
            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
   198
            IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
            IXAudio2SourceVoice_DestroyVoice(source);
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 (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
            IXAudio2_StopEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        if (mastering != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
            IXAudio2MasteringVoice_DestroyVoice(mastering);
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_Release(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 (this->hidden->mixbuf != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
            SDL_free(this->hidden->mixbuf);
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 (this->hidden->semaphore != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
            CloseHandle(this->hidden->semaphore);
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        SDL_free(this->hidden);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        this->hidden = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
}
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
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
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
   224
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    HRESULT result = S_OK;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    WAVEFORMATEX waveformat;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    int valid_format = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    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
   229
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    IXAudio2SourceVoice *source = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    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
   232
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
	static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
	    VoiceCBOnVoiceProcessPassStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
        VoiceCBOnVoiceProcessPassEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
        VoiceCBOnStreamEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        VoiceCBOnBufferStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
        VoiceCBOnBufferEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
        VoiceCBOnLoopEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        VoiceCBOnVoiceError
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
	};
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
	static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        SDL_SetError("XAudio2: capture devices unsupported.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        SDL_SetError("XAudio2: XAudio2Create() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    if (devname != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
            SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
            return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
            XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
            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
   265
                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
   266
                if (str != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
                    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
   268
                    SDL_free(str);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
                    if (match) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
                        devId = i;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
                        break;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
                    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
                }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
            }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        if (i == devcount) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
            SDL_SetError("XAudio2: Requested device not found.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
            return 0;
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
    }
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
    /* 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
   285
    this->hidden = (struct SDL_PrivateAudioData *)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        SDL_malloc((sizeof *this->hidden));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    if (this->hidden == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
        SDL_OutOfMemory();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    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
   293
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    this->hidden->ixa2 = ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    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
   296
    if (this->hidden->semaphore == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        SDL_SetError("XAudio2: CreateSemaphore() failed!");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        return 0;
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);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
        SDL_SetError("XAudio2: Unsupported audio format");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    /* 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
   322
    SDL_CalculateAudioSpec(&this->spec);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    /* 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
   325
    this->hidden->mixlen = this->spec.size;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    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
   327
    if (this->hidden->mixbuf == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        SDL_OutOfMemory();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        return 0;
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;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    SDL_memset(this->hidden->mixbuf, '\0', 2 * this->hidden->mixlen);
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);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
        SDL_SetError("XAudio2: Couldn't create mastering voice");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    SDL_zero(waveformat);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    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
   352
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    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
   357
    waveformat.nChannels = this->spec.channels;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    waveformat.nSamplesPerSec = this->spec.freq;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    waveformat.nBlockAlign =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    waveformat.nAvgBytesPerSec =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
                                        XAUDIO2_VOICE_NOSRC |
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
                                        XAUDIO2_VOICE_NOPITCH,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
                                        1.0f, &callbacks, NULL, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        SDL_SetError("XAudio2: Couldn't create source voice");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
        return 0;
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
    this->hidden->source = source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    /* Start everything playing! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    result = IXAudio2_StartEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        SDL_SetError("XAudio2: Couldn't start engine");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    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
   384
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
        SDL_SetError("XAudio2: Couldn't start source voice");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    return 1; /* good to go. */
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
XAUDIO2_Deinitialize(void)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
5615
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   399
#endif  /* SDL_HAVE_XAUDIO2_H */
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   400
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   401
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
{
5615
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   405
#if !SDL_HAVE_XAUDIO2_H
5e060b67c73d Make sure XAudio2 is supported by the DirectX headers at compile time.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   406
    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
   407
#else
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    /* 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
   409
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    if (FAILED(WIN_CoInitialize())) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        SDL_SetError("XAudio2: CoInitialize() failed");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    }
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
    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
   416
        WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
        SDL_SetError("XAudio2: XAudio2Create() failed");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
        return 0;  /* not available. */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
    IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    /* Set the function pointers */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    impl->DetectDevices = XAUDIO2_DetectDevices;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    impl->OpenDevice = XAUDIO2_OpenDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    impl->PlayDevice = XAUDIO2_PlayDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    impl->WaitDevice = XAUDIO2_WaitDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    impl->WaitDone = XAUDIO2_WaitDone;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    impl->CloseDevice = XAUDIO2_CloseDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    impl->Deinitialize = XAUDIO2_Deinitialize;
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
    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
   433
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
}
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
AudioBootStrap XAUDIO2_bootstrap = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
};
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
/* vi: set ts=4 sw=4 expandtab: */