Skip to content

Commit

Permalink
[engine] Make XAudio2 default audio engine on Windows
Browse files Browse the repository at this point in the history
DirectSound8 is obsolete and preserved for backward compatibility.
  • Loading branch information
dimhotepus committed Oct 19, 2024
1 parent 920509a commit dd79b87
Show file tree
Hide file tree
Showing 13 changed files with 1,569 additions and 1,194 deletions.
591 changes: 316 additions & 275 deletions engine/audio/private/snd_dev_direct.cpp

Large diffs are not rendered by default.

1,814 changes: 1,028 additions & 786 deletions engine/audio/private/snd_dev_xaudio.cpp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion engine/audio/private/snd_dev_xaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class CXboxVoice
};

CXboxVoice *Audio_GetXVoice( void );
IXAudio2 *Audio_GetXAudio2( void );

#endif

Expand Down
2 changes: 1 addition & 1 deletion engine/audio/private/snd_dma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ typedef struct

static soundfade_t soundfade; // Client sound fading singleton object

// 0)headphones 2)stereo speakers 4)quad 5)5point1
// 0)headphones 2)stereo speakers 4)quad 5)5point1 7)7point1
// autodetected from windows settings
ConVar snd_surround( "snd_surround_speakers", "-1", FCVAR_INTERNAL_USE );
ConVar snd_legacy_surround( "snd_legacy_surround", "0", FCVAR_ARCHIVE );
Expand Down
4 changes: 2 additions & 2 deletions engine/audio/private/snd_wave_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ extern double realtime;
#define TF_XBOX_WAV_MEMORY_CACHE ( 24 * 1024 * 1024 ) // Team Fortress uses a larger cache

// Dev builds will be missing soundcaches and hitch sometimes, we only care if its being properly launched from steam where sound caches should be complete.
ConVar snd_async_spew_blocking( "snd_async_spew_blocking", "1", 0, "Spew message to console any time async sound loading blocks on file i/o. ( 0=Off, 1=With -steam only, 2=Always" );
ConVar snd_async_spew_blocking( "snd_async_spew_blocking", "1", 0, "Spew message to console any time async sound loading blocks on file I/O. (0=Off, 1=With -steam only, 2=Always)" );
ConVar snd_async_spew( "snd_async_spew", "0", 0, "Spew all async sound reads, including success" );
ConVar snd_async_fullyasync( "snd_async_fullyasync", "0", 0, "All playback is fully async (sound doesn't play until data arrives)." );
ConVar snd_async_stream_spew( "snd_async_stream_spew", "0", 0, "Spew streaming info ( 0=Off, 1=streams, 2=buffers" );
ConVar snd_async_stream_spew( "snd_async_stream_spew", "0", 0, "Spew streaming info (0=Off, 1=streams, 2=buffers)" );

static bool SndAsyncSpewBlocking()
{
Expand Down
196 changes: 111 additions & 85 deletions engine/audio/private/snd_win.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=====================================================================================//
// Purpose: Sound device dispatch and initialization.

#include "audio_pch.h"

Expand All @@ -17,135 +15,164 @@ ConVar snd_audioqueue( "snd_audioqueue", "1" );

#endif

#include "snd_dev_xaudio.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// dimhotepus: Allow to override sound device.
ConVar snd_device_override( "snd_device_override", "0", FCVAR_ARCHIVE,
"Allow to override used sound device.\nPossible values:\n"
"0\tAutodetection\n"
#ifdef PLATFORM_WINDOWS
"1\tXAudio 2 [default]\n"
"2\tDirect Sound 8\n",
#elif defined(OSX)
"1\tMac Audio Queue [default]\n"
"2\tOpen AL\n",
#elif defined(USE_SDL)
"1\tSDL [default]",
#endif
true,
0,
true,
#if defined(PLATFORM_WINDOWS)
2
#elif defined(OSX)
2
#elif defined(USE_SDL)
1
#endif
);

/**
* @brief Audio device kind.
*/
enum class AudioDeviceKind {
/**
* @brief Autodetect.
*/
kAutodetect = 0
#ifdef PLATFORM_WINDOWS
, kXAudio2 = 1
, kDirectSound8 = 2
#elif defined(OSX)
, kMacAudioQueue = 1
, kOpenAL = 2
#elif defined(USE_SDL)
, kSDL = 2
#endif
};

bool snd_firsttime = true;

/*
* Global variables. Must be visible to window-procedure function
* so it can unlock and free the data block after it has been played.
*/
IAudioDevice *g_AudioDevice = NULL;

/*
==================
S_BlockSound
==================
*/
void S_BlockSound( void )
IAudioDevice *g_AudioDevice = nullptr;

void S_BlockSound()
{
if ( !g_AudioDevice )
return;

g_AudioDevice->Pause();
}

/*
==================
S_UnblockSound
==================
*/
void S_UnblockSound( void )
void S_UnblockSound()
{
if ( !g_AudioDevice )
return;

g_AudioDevice->UnPause();
}

/*
==================
AutoDetectInit
Try to find a sound device to mix for.
Returns a CAudioNULLDevice if nothing is found.
==================
*/
/**
* @brief Autodetect and find sound device to mix on.
* @param Unused.
* @return Audio device to mix on. Null device when no sound.
*/
IAudioDevice *IAudioDevice::AutoDetectInit()
{
IAudioDevice *pDevice = NULL;
IAudioDevice *pDevice = nullptr;

if ( IsPC() )
{
#if defined( WIN32 ) && !defined( USE_SDL )
if ( snd_firsttime )
{
pDevice = Audio_CreateDirectSoundDevice();
if ( snd_firsttime )
{
const auto device_override =
static_cast<AudioDeviceKind>(snd_device_override.GetInt());

switch (device_override) {
default:
Warning( "Unknown 'snd_device_override' con var value %d. Using autodetection.\n",
to_underlying(device_override) );
[[fallthrough]];

case AudioDeviceKind::kAutodetect:
case AudioDeviceKind::kXAudio2:
{
pDevice = Audio_CreateXAudioDevice();
if ( pDevice )
{
// XAudio2 requires threaded mixing.
S_EnableThreadedMixing( true );
}
}
break;

case AudioDeviceKind::kDirectSound8:
pDevice = Audio_CreateDirectSoundDevice();
break;
}
}
#elif defined(OSX)
if ( !CommandLine()->CheckParm( "-snd_openal" ) )
{
DevMsg( "Using AudioQueue Interface\n" );
const auto device_override =
static_cast<AudioDeviceKind>(snd_device_override.GetInt());

switch (device_override) {
default:
Warning( "Unknown 'snd_device_override' con var value %d. Using autodetection.\n",
to_underlying(device_override) );
[[fallthrough]];

case AudioDeviceKind::kAutodetect:
case AudioDeviceKind::kMacAudioQueue:
pDevice = Audio_CreateMacAudioQueueDevice();
}
if ( !pDevice )
{
DevMsg( "Using OpenAL Interface\n" );
pDevice = Audio_CreateOpenALDevice(); // fall back to openAL if the audio queue fails
}
#elif defined( USE_SDL )
DevMsg( "Trying SDL Audio Interface\n" );
pDevice = Audio_CreateSDLAudioDevice();

#ifdef NEVER
// Jul 2012. mikesart. E-mail exchange with Ryan Gordon after figuring out that
// Audio_CreatePulseAudioDevice() wasn't working on Ubuntu 12.04 (lots of stuttering).
//
// > I installed libpulse-dev, rebuilt SDL, and now SDL is using pulse
// > audio and everything is working great. However I'm wondering if we
// > need to fall back to PulseAudio in our codebase if SDL is doing that
// > for us. I mean, is it worth me going through and debugging our Pulse
// > Audio path or should I just remove it?
//
// Remove it...it never worked well, and only remained in case there were
// concerns about relying on SDL. The SDL codepath is way easier to read,
// simpler to maintain, and handles all sorts of strange audio backends,
// including Pulse.
if ( !pDevice )
{
DevMsg( "Trying PulseAudio Interface\n" );
pDevice = Audio_CreatePulseAudioDevice(); // fall back to PulseAudio if SDL fails
}
#endif // NEVER
break;

#else
#error "Please define your platform"
#endif
case AudioDeviceKind::kOpenAL:
pDevice = Audio_CreateOpenALDevice();
break;
}
#if defined( _X360 )
else

if ( !pDevice )
{
pDevice = Audio_CreateXAudioDevice( true );
if ( pDevice )
{
// xaudio requires threaded mixing
S_EnableThreadedMixing( true );
}
// fall back to openAL if the audio queue fails
DevMsg( "Using OpenAL Interface\n" );
pDevice = Audio_CreateOpenALDevice();
}
#elif defined( USE_SDL )
DevMsg( "Trying SDL Audio Interface\n" );
pDevice = Audio_CreateSDLAudioDevice();
#else
#error "Please define your platform"
#endif

snd_firsttime = false;

if ( !pDevice )
{
if ( snd_firsttime )
DevMsg( "No sound device initialized\n" );
DevMsg( "No sound device initialized.\n" );

return Audio_GetNullDevice();
}

return pDevice;
}

/*
==============
SNDDMA_Shutdown
Reset the sound device for exiting
===============
*/
void SNDDMA_Shutdown( void )
void SNDDMA_Shutdown()
{
if ( g_AudioDevice != Audio_GetNullDevice() )
{
Expand All @@ -159,4 +186,3 @@ void SNDDMA_Shutdown( void )
g_AudioDevice = Audio_GetNullDevice();
}
}

1 change: 1 addition & 0 deletions engine/audio/private/sound_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//=============================================================================//

#include "basetypes.h"
#include "sound.h"
#include "snd_fixedint.h"

#ifndef SOUND_PRIVATE_H
Expand Down
3 changes: 1 addition & 2 deletions engine/engine.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,7 @@ $Project "engine"
}
}

// X360 only audio files
$File "audio\private\snd_dev_xaudio.cpp" [$X360] \
$File "audio\private\snd_dev_xaudio.cpp" \
"audio\private\snd_wave_mixer_xma.cpp" [$X360]
{
$Configuration
Expand Down
12 changes: 7 additions & 5 deletions engine/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@
#include "soundservice.h"
#include "profile.h"
#include "steam/isteamremotestorage.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#include "audio_pch.h"
#endif
#if defined( LINUX )
#include <locale.h>
#include "include/SDL3/SDL.h"
Expand All @@ -135,7 +131,6 @@
#include "audio/private/voice_mixer_controls.h"

#include "ixboxsystem.h"
extern IXboxSystem *g_pXboxSystem;

extern ConVar cl_cloud_settings;
extern ConVar cl_logofile;
Expand Down Expand Up @@ -3562,6 +3557,11 @@ void Host_InitGpu()
Cbuf_AddText("star_gpu");
}

void Host_InitAudio()
{
Cbuf_AddText("star_audio_render");
}

void Host_InitOperatingSystem()
{
Cbuf_AddText("star_os");
Expand Down Expand Up @@ -3972,6 +3972,8 @@ void Host_Init( bool bDedicated )
Host_InitRam();
// Print GPU info.
Host_InitGpu();
// Print Audio info.
Host_InitAudio();
// Print OS info.
Host_InitOperatingSystem();

Expand Down
Loading

0 comments on commit dd79b87

Please sign in to comment.