src/audio/xaudio2/SDL_xaudio2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 04 Aug 2011 00:31:11 -0400
changeset 5593 ab22ca13c47f
parent 5592 2e88d0742f4d
child 5615 5e060b67c73d
permissions -rw-r--r--
Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
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
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
    27
#define INITGUID 1
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#include "SDL_xaudio2.h"
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
static __inline__ char *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
utf16_to_utf8(const WCHAR *S)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    /* !!! 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
    34
    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
    35
                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
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
    38
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
    39
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
    40
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    void *ptr = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
        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
    48
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    } 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
    50
        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
    51
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    } 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
    53
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        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
    55
        return;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        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
    61
            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
    62
            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
    63
                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
    64
                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
    65
            }
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
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
}
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
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
VoiceCBOnBufferEnd(THIS_ void *data)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    /* 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
    76
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    ReleaseSemaphore(this->hidden->semaphore, 1, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
static void STDMETHODCALLTYPE
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
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
    /* !!! 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
    84
    SDL_assert(0 && "write me!");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
/* no-op callbacks... */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
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
    90
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
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
    92
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
    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
static Uint8 *
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
XAUDIO2_GetDeviceBuf(_THIS)
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
    return this->hidden->nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
XAUDIO2_PlayDevice(_THIS)
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
    XAUDIO2_BUFFER buffer;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    Uint8 *mixbuf = this->hidden->mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    Uint8 *nextbuf = this->hidden->nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    const int mixlen = this->hidden->mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    HRESULT result = S_OK;
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
    if (!this->enabled) { /* shutting down? */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        return;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    /* Submit the next filled buffer */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    SDL_zero(buffer);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    buffer.AudioBytes = mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    buffer.pAudioData = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    buffer.pContext = this;
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
    if (nextbuf == mixbuf) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
        nextbuf += mixlen;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        nextbuf = mixbuf;
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
    this->hidden->nextbuf = nextbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
        /* !!! 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
   131
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    if (result != S_OK) {  /* uhoh, panic! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
        this->enabled = 0;
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
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
XAUDIO2_WaitDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    if (this->enabled) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
XAUDIO2_WaitDone(_THIS)
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
    IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    XAUDIO2_VOICE_STATE state;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    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
   153
    IXAudio2SourceVoice_Discontinuity(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    IXAudio2SourceVoice_GetState(source, &state);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    while (state.BuffersQueued > 0) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        WaitForSingleObject(this->hidden->semaphore, INFINITE);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        IXAudio2SourceVoice_GetState(source, &state);
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
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
XAUDIO2_CloseDevice(_THIS)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    if (this->hidden != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        IXAudio2 *ixa2 = this->hidden->ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        IXAudio2SourceVoice *source = this->hidden->source;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        if (source != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
            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
   172
            IXAudio2SourceVoice_FlushSourceBuffers(source);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
            IXAudio2SourceVoice_DestroyVoice(source);
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
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
            IXAudio2_StopEngine(ixa2);
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 (mastering != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
            IXAudio2MasteringVoice_DestroyVoice(mastering);
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
        if (ixa2 != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
        if (this->hidden->mixbuf != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
            SDL_free(this->hidden->mixbuf);
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
        if (this->hidden->semaphore != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
            CloseHandle(this->hidden->semaphore);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
        SDL_free(this->hidden);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        this->hidden = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
}
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
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
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
   198
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    HRESULT result = S_OK;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    WAVEFORMATEX waveformat;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    int valid_format = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    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
   203
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    IXAudio2SourceVoice *source = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    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
   206
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
	static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
	    VoiceCBOnVoiceProcessPassStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        VoiceCBOnVoiceProcessPassEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        VoiceCBOnStreamEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        VoiceCBOnBufferStart,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
        VoiceCBOnBufferEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        VoiceCBOnLoopEnd,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        VoiceCBOnVoiceError
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
	static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    if (iscapture) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
        SDL_SetError("XAudio2: capture devices unsupported.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    } 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
   223
        SDL_SetError("XAudio2: XAudio2Create() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    if (devname != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
        UINT32 devcount = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        UINT32 i = 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
        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
   232
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
            SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
            return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
        for (i = 0; i < devcount; i++) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
            XAUDIO2_DEVICE_DETAILS details;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
            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
   239
                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
   240
                if (str != NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
                    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
   242
                    SDL_free(str);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
                    if (match) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
                        devId = i;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
                        break;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
                    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
                }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
            }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
        if (i == devcount) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
            IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
            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
   254
            return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    /* 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
   259
    this->hidden = (struct SDL_PrivateAudioData *)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        SDL_malloc((sizeof *this->hidden));
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    if (this->hidden == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        IXAudio2_Release(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
        SDL_OutOfMemory();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    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
   267
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    this->hidden->ixa2 = ixa2;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    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
   270
    if (this->hidden->semaphore == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
        SDL_SetError("XAudio2: CreateSemaphore() failed!");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        return 0;
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
    while ((!valid_format) && (test_format)) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        switch (test_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        case AUDIO_U8:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
        case AUDIO_S16:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
        case AUDIO_S32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
        case AUDIO_F32:
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
            this->spec.format = test_format;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
            valid_format = 1;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
            break;
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
        test_format = SDL_NextAudioFormat();
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    if (!valid_format) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
        SDL_SetError("XAudio2: Unsupported audio format");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    /* 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
   296
    SDL_CalculateAudioSpec(&this->spec);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    /* 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
   299
    this->hidden->mixlen = this->spec.size;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    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
   301
    if (this->hidden->mixbuf == NULL) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        SDL_OutOfMemory();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    this->hidden->nextbuf = this->hidden->mixbuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    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
   308
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    /* 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
   310
       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
   311
       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
   312
       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
   313
       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
   314
       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
   315
    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
   316
                                           XAUDIO2_DEFAULT_CHANNELS,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
                                           this->spec.freq, 0, devId, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        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
   321
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    SDL_zero(waveformat);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    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
   326
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    } else {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    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
   331
    waveformat.nChannels = this->spec.channels;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    waveformat.nSamplesPerSec = this->spec.freq;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    waveformat.nBlockAlign =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    waveformat.nAvgBytesPerSec =
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
                                        XAUDIO2_VOICE_NOSRC |
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
                                        XAUDIO2_VOICE_NOPITCH,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
                                        1.0f, &callbacks, NULL, NULL);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        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
   345
        return 0;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    }
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    this->hidden->source = source;
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
    /* Start everything playing! */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    result = IXAudio2_StartEngine(ixa2);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        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
   354
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    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
   358
    if (result != S_OK) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        XAUDIO2_CloseDevice(this);
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
        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
   361
        return 0;
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
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    return 1; /* good to go. */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
static void
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
XAUDIO2_Deinitialize(void)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
}
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
static int
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
{
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    /* 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
   377
    IXAudio2 *ixa2 = NULL;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    if (FAILED(WIN_CoInitialize())) {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        SDL_SetError("XAudio2: CoInitialize() failed");
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
    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
   384
        WIN_CoUninitialize();
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
        SDL_SetError("XAudio2: XAudio2Create() failed");
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
        return 0;  /* not available. */
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
    IXAudio2_Release(ixa2);
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
    /* Set the function pointers */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    impl->DetectDevices = XAUDIO2_DetectDevices;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    impl->OpenDevice = XAUDIO2_OpenDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    impl->PlayDevice = XAUDIO2_PlayDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    impl->WaitDevice = XAUDIO2_WaitDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    impl->WaitDone = XAUDIO2_WaitDone;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    impl->CloseDevice = XAUDIO2_CloseDevice;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    impl->Deinitialize = XAUDIO2_Deinitialize;
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    return 1;   /* this audio target is available. */
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
}
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
AudioBootStrap XAUDIO2_bootstrap = {
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
};
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
/* vi: set ts=4 sw=4 expandtab: */