src/audio/xaudio2/SDL_xaudio2.c
author David Ludwig <dludwig@pobox.com>
Wed, 04 Sep 2013 20:20:36 -0400
changeset 8518 bc44448125eb
parent 8500 f25ef28d61e8
child 8519 0a334fc866b0
permissions -rw-r--r--
WinRT: minor code cleanup in SDL_xaudio2.c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     1
/*
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     2
  Simple DirectMedia Layer
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     4
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     5
  This software is provided 'as-is', without any express or implied
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     7
  arising from the use of this software.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     8
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    11
  freely, subject to the following restrictions:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    12
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    14
     claim that you wrote the original software. If you use this software
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    16
     appreciated but is not required.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    18
     misrepresented as being the original software.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    20
*/
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    21
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    22
/* WinRT NOTICE:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    23
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    24
   A number of changes were warranted to SDL's XAudio2 backend in order to
8500
f25ef28d61e8 WinRT: more "Windows RT" to "WinRT" renaming
David Ludwig <dludwig@pobox.com>
parents: 8478
diff changeset
    25
   get it compiling for WinRT.
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    26
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    27
   When compiling for WinRT, XAudio2.h requires that it be compiled in a C++
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    28
   file, and not a straight C file.  Trying to compile it as C leads to lots
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    29
   of errors, at least with MSVC 2012 and Windows SDK 8.0, as of Nov 22, 2012.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    30
   To address this specific issue, a few changes were made to SDL_xaudio2.c:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    31
   
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    32
   1. SDL_xaudio2.c is compiled as a C++ file in WinRT builds.  Exported
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    33
      symbols, namely XAUDIO2_bootstrap, uses 'extern "C"' to make sure the
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    34
      rest of SDL can access it.  Non-WinRT builds continue to compile
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    35
      SDL_xaudio2.c as a C file.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    36
   2. A macro redefines variables named 'this' to '_this', to prevent compiler
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    37
      errors (C2355 in Visual C++) related to 'this' being a reserverd keyword.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    38
      This hack may need to be altered in the future, particularly if C++'s
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    39
      'this' keyword needs to be used (within SDL_xaudio2.c).  At the time
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    40
      WinRT support was initially added to SDL's XAudio2 backend, this
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    41
      capability was not needed.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    42
   3. The C-style macros to invoke XAudio2's COM-based methods were
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    43
      rewritten to be C++-friendly.  These are provided in the file,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    44
      SDL_xaudio2_winrthelpers.h.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    45
   4. IXAudio2::CreateSourceVoice, when used in C++, requires its callbacks to
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    46
      be specified via a C++ class.  SDL's XAudio2 backend was written with
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    47
      C-style callbacks.  A class to bridge these two interfaces,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    48
      SDL_XAudio2VoiceCallback, was written to make XAudio2 happy.  Its methods
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    49
      just call SDL's existing, C-style callbacks.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    50
   5. Multiple checks for the __cplusplus macro were made, in appropriate
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    51
      places.  
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    52
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    53
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    54
   A few additional changes to SDL's XAudio2 backend were warranted by API
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    55
   changes to Windows.  Many, but not all of these are documented by Microsoft
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    56
   at:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    57
   http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    58
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    59
   1. Windows' thread synchronization function, CreateSemaphore, was removed
8500
f25ef28d61e8 WinRT: more "Windows RT" to "WinRT" renaming
David Ludwig <dludwig@pobox.com>
parents: 8478
diff changeset
    60
      from WinRT.  SDL's semaphore API was substituted instead.
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    61
   2. The method calls, IXAudio2::GetDeviceCount and IXAudio2::GetDeviceDetails
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    62
      were removed from the XAudio2 API.  Microsoft is telling developers to
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    63
      use APIs in Windows::Foundation instead.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    64
      For SDL, the missing methods were reimplemented using the APIs Microsoft
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    65
      said to use.
8500
f25ef28d61e8 WinRT: more "Windows RT" to "WinRT" renaming
David Ludwig <dludwig@pobox.com>
parents: 8478
diff changeset
    66
   3. CoInitialize and CoUninitialize are not available in WinRT.
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    67
      These calls were removed, as COM will have been initialized earlier,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    68
      at least by the call to the WinRT app's main function
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    69
      (aka 'int main(Platform::Array<Platform::String^>^)).  (DLudwig:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    70
      This was my understanding of how WinRT: the 'main' function uses
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    71
      a tag of [MTAThread], which should initialize COM.  My understanding
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    72
      of COM is somewhat limited, and I may be incorrect here.)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    73
   4. IXAudio2::CreateMasteringVoice changed its integer-based 'DeviceIndex'
8500
f25ef28d61e8 WinRT: more "Windows RT" to "WinRT" renaming
David Ludwig <dludwig@pobox.com>
parents: 8478
diff changeset
    74
      argument to a string-based one, 'szDeviceId'.  In WinRT, the
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    75
      string-based argument will be used.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    76
*/
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    77
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    78
#include "SDL_config.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    79
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    80
#if SDL_AUDIO_DRIVER_XAUDIO2
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    81
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    82
#ifdef __cplusplus
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    83
extern "C" {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    84
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    85
#include "../../core/windows/SDL_windows.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    86
#include "SDL_audio.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    87
#include "../SDL_audio_c.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    88
#include "../SDL_sysaudio.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    89
#include "SDL_assert.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    90
#ifdef __cplusplus
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    91
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    92
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    93
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    94
#ifdef __GNUC__
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    95
/* The configure script already did any necessary checking */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    96
#  define SDL_XAUDIO2_HAS_SDK 1
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    97
#elif defined(__WINRT__)
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
    98
/* WinRT always has access to the .the XAudio 2 SDK */
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
    99
#  define SDL_XAUDIO2_HAS_SDK
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   100
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   101
#include <dxsdkver.h> /* XAudio2 exists as of the March 2008 DirectX SDK */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   102
#if (!defined(_DXSDK_BUILD_MAJOR) || (_DXSDK_BUILD_MAJOR < 1284))
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   103
#  pragma message("Your DirectX SDK is too old. Disabling XAudio2 support.")
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   104
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   105
#  define SDL_XAUDIO2_HAS_SDK 1
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   106
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   107
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   108
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   109
#ifdef SDL_XAUDIO2_HAS_SDK
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   110
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   111
#define INITGUID 1
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   112
#include <xaudio2.h>
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   113
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   114
/* Hidden "this" pointer for the audio functions */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   115
#define _THIS   SDL_AudioDevice *this
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   116
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   117
#ifdef __cplusplus
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   118
#define this _this
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   119
#include "SDL_xaudio2_winrthelpers.h"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   120
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   121
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   122
struct SDL_PrivateAudioData
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   123
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   124
    IXAudio2 *ixa2;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   125
    IXAudio2SourceVoice *source;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   126
    IXAudio2MasteringVoice *mastering;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   127
    SDL_sem * semaphore;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   128
    Uint8 *mixbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   129
    int mixlen;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   130
    Uint8 *nextbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   131
};
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   132
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   133
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   134
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   135
XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   136
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   137
    IXAudio2 *ixa2 = NULL;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   138
    UINT32 devcount = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   139
    UINT32 i = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   140
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   141
    if (iscapture) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   142
        SDL_SetError("XAudio2: capture devices unsupported.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   143
        return;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   144
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   145
        SDL_SetError("XAudio2: XAudio2Create() failed at detection.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   146
        return;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   147
    } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   148
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   149
        IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   150
        return;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   151
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   152
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   153
    for (i = 0; i < devcount; i++) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   154
        XAUDIO2_DEVICE_DETAILS details;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   155
        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   156
            char *str = WIN_StringToUTF8(details.DisplayName);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   157
            if (str != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   158
                addfn(str);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   159
                SDL_free(str);  /* addfn() made a copy of the string. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   160
            }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   161
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   162
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   163
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   164
    IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   165
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   166
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   167
static void STDMETHODCALLTYPE
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   168
VoiceCBOnBufferEnd(THIS_ void *data)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   169
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   170
    /* Just signal the SDL audio thread and get out of XAudio2's way. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   171
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   172
    SDL_SemPost(this->hidden->semaphore);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   173
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   174
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   175
static void STDMETHODCALLTYPE
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   176
VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   177
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   178
    /* !!! FIXME: attempt to recover, or mark device disconnected. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   179
    SDL_assert(0 && "write me!");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   180
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   181
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   182
/* no-op callbacks... */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   183
static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   184
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassStart(THIS_ UINT32 b) {}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   185
static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   186
static void STDMETHODCALLTYPE VoiceCBOnBufferStart(THIS_ void *data) {}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   187
static void STDMETHODCALLTYPE VoiceCBOnLoopEnd(THIS_ void *data) {}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   188
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   189
#if defined(__cplusplus)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   190
class SDL_XAudio2VoiceCallback : public IXAudio2VoiceCallback
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   191
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   192
public:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   193
    STDMETHOD_(void, OnBufferEnd)(void *pBufferContext) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   194
        VoiceCBOnBufferEnd(pBufferContext);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   195
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   196
    STDMETHOD_(void, OnBufferStart)(void *pBufferContext) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   197
        VoiceCBOnBufferStart(pBufferContext);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   198
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   199
    STDMETHOD_(void, OnLoopEnd)(void *pBufferContext) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   200
        VoiceCBOnLoopEnd(pBufferContext);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   201
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   202
    STDMETHOD_(void, OnStreamEnd)() {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   203
        VoiceCBOnStreamEnd();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   204
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   205
    STDMETHOD_(void, OnVoiceError)(void *pBufferContext, HRESULT Error) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   206
        VoiceCBOnVoiceError(pBufferContext, Error);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   207
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   208
    STDMETHOD_(void, OnVoiceProcessingPassEnd)() {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   209
        VoiceCBOnVoiceProcessPassEnd();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   210
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   211
    STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32 BytesRequired) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   212
        VoiceCBOnVoiceProcessPassStart(BytesRequired);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   213
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   214
};
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   215
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   216
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   217
static Uint8 *
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   218
XAUDIO2_GetDeviceBuf(_THIS)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   219
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   220
    return this->hidden->nextbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   221
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   222
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   223
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   224
XAUDIO2_PlayDevice(_THIS)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   225
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   226
    XAUDIO2_BUFFER buffer;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   227
    Uint8 *mixbuf = this->hidden->mixbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   228
    Uint8 *nextbuf = this->hidden->nextbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   229
    const int mixlen = this->hidden->mixlen;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   230
    IXAudio2SourceVoice *source = this->hidden->source;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   231
    HRESULT result = S_OK;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   232
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   233
    if (!this->enabled) { /* shutting down? */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   234
        return;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   235
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   236
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   237
    /* Submit the next filled buffer */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   238
    SDL_zero(buffer);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   239
    buffer.AudioBytes = mixlen;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   240
    buffer.pAudioData = nextbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   241
    buffer.pContext = this;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   242
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   243
    if (nextbuf == mixbuf) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   244
        nextbuf += mixlen;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   245
    } else {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   246
        nextbuf = mixbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   247
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   248
    this->hidden->nextbuf = nextbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   249
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   250
    result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   251
    if (result == XAUDIO2_E_DEVICE_INVALIDATED) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   252
        /* !!! FIXME: possibly disconnected or temporary lost. Recover? */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   253
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   254
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   255
    if (result != S_OK) {  /* uhoh, panic! */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   256
        IXAudio2SourceVoice_FlushSourceBuffers(source);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   257
        this->enabled = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   258
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   259
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   260
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   261
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   262
XAUDIO2_WaitDevice(_THIS)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   263
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   264
    if (this->enabled) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   265
        SDL_SemWait(this->hidden->semaphore);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   266
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   267
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   268
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   269
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   270
XAUDIO2_WaitDone(_THIS)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   271
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   272
    IXAudio2SourceVoice *source = this->hidden->source;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   273
    XAUDIO2_VOICE_STATE state;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   274
    SDL_assert(!this->enabled);  /* flag that stops playing. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   275
    IXAudio2SourceVoice_Discontinuity(source);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   276
    IXAudio2SourceVoice_GetState(source, &state);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   277
    while (state.BuffersQueued > 0) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   278
        SDL_SemWait(this->hidden->semaphore);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   279
        IXAudio2SourceVoice_GetState(source, &state);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   280
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   281
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   282
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   283
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   284
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   285
XAUDIO2_CloseDevice(_THIS)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   286
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   287
    if (this->hidden != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   288
        IXAudio2 *ixa2 = this->hidden->ixa2;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   289
        IXAudio2SourceVoice *source = this->hidden->source;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   290
        IXAudio2MasteringVoice *mastering = this->hidden->mastering;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   291
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   292
        if (source != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   293
            IXAudio2SourceVoice_Stop(source, 0, XAUDIO2_COMMIT_NOW);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   294
            IXAudio2SourceVoice_FlushSourceBuffers(source);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   295
            IXAudio2SourceVoice_DestroyVoice(source);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   296
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   297
        if (ixa2 != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   298
            IXAudio2_StopEngine(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   299
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   300
        if (mastering != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   301
            IXAudio2MasteringVoice_DestroyVoice(mastering);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   302
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   303
        if (ixa2 != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   304
            IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   305
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   306
        if (this->hidden->mixbuf != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   307
            SDL_free(this->hidden->mixbuf);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   308
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   309
        if (this->hidden->semaphore != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   310
            SDL_DestroySemaphore(this->hidden->semaphore);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   311
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   312
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   313
        SDL_free(this->hidden);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   314
        this->hidden = NULL;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   315
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   316
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   317
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   318
static int
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   319
XAUDIO2_OpenDevice(_THIS, const char *devname, int iscapture)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   320
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   321
    HRESULT result = S_OK;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   322
    WAVEFORMATEX waveformat;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   323
    int valid_format = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   324
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   325
    IXAudio2 *ixa2 = NULL;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   326
    IXAudio2SourceVoice *source = NULL;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   327
#if defined(__WINRT__)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   328
    LPCWSTR devId = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   329
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   330
    UINT32 devId = 0;  /* 0 == system default device. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   331
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   332
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   333
#if defined(__cplusplus)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   334
    static SDL_XAudio2VoiceCallback callbacks;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   335
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   336
	static IXAudio2VoiceCallbackVtbl callbacks_vtable = {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   337
	    VoiceCBOnVoiceProcessPassStart,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   338
        VoiceCBOnVoiceProcessPassEnd,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   339
        VoiceCBOnStreamEnd,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   340
        VoiceCBOnBufferStart,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   341
        VoiceCBOnBufferEnd,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   342
        VoiceCBOnLoopEnd,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   343
        VoiceCBOnVoiceError
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   344
    };
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   345
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   346
	static IXAudio2VoiceCallback callbacks = { &callbacks_vtable };
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   347
#endif // ! defined(__cplusplus)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   348
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   349
    if (iscapture) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   350
        return SDL_SetError("XAudio2: capture devices unsupported.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   351
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   352
        return SDL_SetError("XAudio2: XAudio2Create() failed at open.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   353
    }
8518
bc44448125eb WinRT: minor code cleanup in SDL_xaudio2.c
David Ludwig <dludwig@pobox.com>
parents: 8500
diff changeset
   354
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   355
    /*
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   356
    XAUDIO2_DEBUG_CONFIGURATION debugConfig;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   357
    debugConfig.TraceMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS | XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_FUNC_CALLS | XAUDIO2_LOG_TIMING | XAUDIO2_LOG_LOCKS | XAUDIO2_LOG_MEMORY | XAUDIO2_LOG_STREAMING;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   358
    debugConfig.BreakMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   359
    debugConfig.LogThreadID = TRUE;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   360
    debugConfig.LogFileline = TRUE;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   361
    debugConfig.LogFunctionName = TRUE;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   362
    debugConfig.LogTiming = TRUE;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   363
    ixa2->SetDebugConfiguration(&debugConfig);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   364
    */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   365
8518
bc44448125eb WinRT: minor code cleanup in SDL_xaudio2.c
David Ludwig <dludwig@pobox.com>
parents: 8500
diff changeset
   366
#if ! defined(__WINRT__)
8477
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   367
    if (devname != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   368
        UINT32 devcount = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   369
        UINT32 i = 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   370
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   371
        if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   372
            IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   373
            return SDL_SetError("XAudio2: IXAudio2_GetDeviceCount() failed.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   374
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   375
        for (i = 0; i < devcount; i++) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   376
            XAUDIO2_DEVICE_DETAILS details;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   377
            if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   378
                char *str = WIN_StringToUTF8(details.DisplayName);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   379
                if (str != NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   380
                    const int match = (SDL_strcmp(str, devname) == 0);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   381
                    SDL_free(str);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   382
                    if (match) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   383
                        devId = i;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   384
                        break;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   385
                    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   386
                }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   387
            }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   388
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   389
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   390
        if (i == devcount) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   391
            IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   392
            return SDL_SetError("XAudio2: Requested device not found.");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   393
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   394
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   395
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   396
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   397
    /* Initialize all variables that we clean on shutdown */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   398
    this->hidden = (struct SDL_PrivateAudioData *)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   399
        SDL_malloc((sizeof *this->hidden));
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   400
    if (this->hidden == NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   401
        IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   402
        return SDL_OutOfMemory();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   403
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   404
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   405
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   406
    this->hidden->ixa2 = ixa2;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   407
    this->hidden->semaphore = SDL_CreateSemaphore(1);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   408
    if (this->hidden->semaphore == NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   409
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   410
        return SDL_SetError("XAudio2: CreateSemaphore() failed!");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   411
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   412
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   413
    while ((!valid_format) && (test_format)) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   414
        switch (test_format) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   415
        case AUDIO_U8:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   416
        case AUDIO_S16:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   417
        case AUDIO_S32:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   418
        case AUDIO_F32:
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   419
            this->spec.format = test_format;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   420
            valid_format = 1;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   421
            break;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   422
        }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   423
        test_format = SDL_NextAudioFormat();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   424
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   425
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   426
    if (!valid_format) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   427
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   428
        return SDL_SetError("XAudio2: Unsupported audio format");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   429
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   430
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   431
    /* Update the fragment size as size in bytes */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   432
    SDL_CalculateAudioSpec(&this->spec);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   433
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   434
    /* We feed a Source, it feeds the Mastering, which feeds the device. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   435
    this->hidden->mixlen = this->spec.size;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   436
    this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   437
    if (this->hidden->mixbuf == NULL) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   438
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   439
        return SDL_OutOfMemory();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   440
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   441
    this->hidden->nextbuf = this->hidden->mixbuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   442
    SDL_memset(this->hidden->mixbuf, 0, 2 * this->hidden->mixlen);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   443
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   444
    /* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   445
       Xbox360, this means 5.1 output, but on Windows, it means "figure out
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   446
       what the system has." It might be preferable to let XAudio2 blast
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   447
       stereo output to appropriate surround sound configurations
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   448
       instead of clamping to 2 channels, even though we'll configure the
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   449
       Source Voice for whatever number of channels you supply. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   450
    result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   451
                                           XAUDIO2_DEFAULT_CHANNELS,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   452
                                           this->spec.freq, 0, devId, NULL);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   453
    if (result != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   454
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   455
        return SDL_SetError("XAudio2: Couldn't create mastering voice");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   456
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   457
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   458
    SDL_zero(waveformat);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   459
    if (SDL_AUDIO_ISFLOAT(this->spec.format)) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   460
        waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   461
    } else {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   462
        waveformat.wFormatTag = WAVE_FORMAT_PCM;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   463
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   464
    waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   465
    waveformat.nChannels = this->spec.channels;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   466
    waveformat.nSamplesPerSec = this->spec.freq;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   467
    waveformat.nBlockAlign =
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   468
        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   469
    waveformat.nAvgBytesPerSec =
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   470
        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   471
    waveformat.cbSize = sizeof(waveformat);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   472
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   473
#ifdef __WINRT__
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   474
    // DLudwig: for now, make XAudio2 do sample rate conversion, just to
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   475
    // get the loopwave test to work.
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   476
    //
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   477
    // TODO, WinRT: consider removing WinRT-specific source-voice creation code from SDL_xaudio2.c
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   478
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   479
                                        0,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   480
                                        1.0f, &callbacks, NULL, NULL);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   481
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   482
    result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   483
                                        XAUDIO2_VOICE_NOSRC |
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   484
                                        XAUDIO2_VOICE_NOPITCH,
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   485
                                        1.0f, &callbacks, NULL, NULL);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   486
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   487
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   488
    if (result != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   489
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   490
        return SDL_SetError("XAudio2: Couldn't create source voice");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   491
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   492
    this->hidden->source = source;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   493
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   494
    /* Start everything playing! */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   495
    result = IXAudio2_StartEngine(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   496
    if (result != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   497
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   498
        return SDL_SetError("XAudio2: Couldn't start engine");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   499
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   500
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   501
    result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   502
    if (result != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   503
        XAUDIO2_CloseDevice(this);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   504
        return SDL_SetError("XAudio2: Couldn't start source voice");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   505
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   506
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   507
    return 0; /* good to go. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   508
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   509
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   510
static void
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   511
XAUDIO2_Deinitialize(void)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   512
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   513
#if defined(__WIN32__)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   514
    WIN_CoUninitialize();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   515
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   516
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   517
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   518
#endif  /* SDL_XAUDIO2_HAS_SDK */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   519
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   520
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   521
static int
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   522
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   523
{
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   524
#ifndef SDL_XAUDIO2_HAS_SDK
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   525
    SDL_SetError("XAudio2: SDL was built without XAudio2 support (old DirectX SDK).");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   526
    return 0;  /* no XAudio2 support, ever. Update your SDK! */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   527
#else
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   528
    /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   529
    IXAudio2 *ixa2 = NULL;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   530
#if defined(__WIN32__)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   531
    // TODO, WinRT: Investigate using CoInitializeEx here
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   532
    if (FAILED(WIN_CoInitialize())) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   533
        SDL_SetError("XAudio2: CoInitialize() failed");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   534
        return 0;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   535
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   536
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   537
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   538
    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   539
#if defined(__WIN32__)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   540
        WIN_CoUninitialize();
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   541
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   542
        SDL_SetError("XAudio2: XAudio2Create() failed at initialization");
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   543
        return 0;  /* not available. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   544
    }
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   545
    IXAudio2_Release(ixa2);
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   546
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   547
    /* Set the function pointers */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   548
    impl->DetectDevices = XAUDIO2_DetectDevices;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   549
    impl->OpenDevice = XAUDIO2_OpenDevice;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   550
    impl->PlayDevice = XAUDIO2_PlayDevice;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   551
    impl->WaitDevice = XAUDIO2_WaitDevice;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   552
    impl->WaitDone = XAUDIO2_WaitDone;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   553
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   554
    impl->CloseDevice = XAUDIO2_CloseDevice;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   555
    impl->Deinitialize = XAUDIO2_Deinitialize;
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   556
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   557
    return 1;   /* this audio target is available. */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   558
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   559
}
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   560
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   561
#if defined(__cplusplus)
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   562
extern "C"
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   563
#endif
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   564
AudioBootStrap XAUDIO2_bootstrap = {
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   565
    "xaudio2", "XAudio2", XAUDIO2_Init, 0
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   566
};
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   567
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   568
#endif  /* SDL_AUDIO_DRIVER_XAUDIO2 */
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   569
ad08f0d710f3 WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
David Ludwig <dludwig@pobox.com>
parents: 8460 7226
diff changeset
   570
/* vi: set ts=4 sw=4 expandtab: */