Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "audio_pch.h"
  7. #if defined( USE_SDL )
  8. #include "snd_dev_sdl.h"
  9. #endif
  10. #ifdef OSX
  11. #include "snd_dev_openal.h"
  12. #include "snd_dev_mac_audioqueue.h"
  13. ConVar snd_audioqueue( "snd_audioqueue", "1" );
  14. #endif
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. bool snd_firsttime = true;
  18. /*
  19. * Global variables. Must be visible to window-procedure function
  20. * so it can unlock and free the data block after it has been played.
  21. */
  22. IAudioDevice *g_AudioDevice = NULL;
  23. /*
  24. ==================
  25. S_BlockSound
  26. ==================
  27. */
  28. void S_BlockSound( void )
  29. {
  30. if ( !g_AudioDevice )
  31. return;
  32. g_AudioDevice->Pause();
  33. }
  34. /*
  35. ==================
  36. S_UnblockSound
  37. ==================
  38. */
  39. void S_UnblockSound( void )
  40. {
  41. if ( !g_AudioDevice )
  42. return;
  43. g_AudioDevice->UnPause();
  44. }
  45. /*
  46. ==================
  47. AutoDetectInit
  48. Try to find a sound device to mix for.
  49. Returns a CAudioNULLDevice if nothing is found.
  50. ==================
  51. */
  52. IAudioDevice *IAudioDevice::AutoDetectInit( bool waveOnly )
  53. {
  54. IAudioDevice *pDevice = NULL;
  55. if ( IsPC() )
  56. {
  57. #if defined( WIN32 ) && !defined( USE_SDL )
  58. if ( waveOnly )
  59. {
  60. pDevice = Audio_CreateWaveDevice();
  61. if ( !pDevice )
  62. goto NULLDEVICE;
  63. }
  64. if ( !pDevice )
  65. {
  66. if ( snd_firsttime )
  67. {
  68. pDevice = Audio_CreateDirectSoundDevice();
  69. }
  70. }
  71. // if DirectSound didn't succeed in initializing, try to initialize
  72. // waveOut sound, unless DirectSound failed because the hardware is
  73. // already allocated (in which case the user has already chosen not
  74. // to have sound)
  75. // UNDONE: JAY: This doesn't test for the hardware being in use anymore, REVISIT
  76. if ( !pDevice )
  77. {
  78. pDevice = Audio_CreateWaveDevice();
  79. }
  80. #elif defined(OSX)
  81. if ( !CommandLine()->CheckParm( "-snd_openal" ) )
  82. {
  83. DevMsg( "Using AudioQueue Interface\n" );
  84. pDevice = Audio_CreateMacAudioQueueDevice();
  85. }
  86. if ( !pDevice )
  87. {
  88. DevMsg( "Using OpenAL Interface\n" );
  89. pDevice = Audio_CreateOpenALDevice(); // fall back to openAL if the audio queue fails
  90. }
  91. #elif defined( USE_SDL )
  92. DevMsg( "Trying SDL Audio Interface\n" );
  93. pDevice = Audio_CreateSDLAudioDevice();
  94. #ifdef NEVER
  95. // Jul 2012. mikesart. E-mail exchange with Ryan Gordon after figuring out that
  96. // Audio_CreatePulseAudioDevice() wasn't working on Ubuntu 12.04 (lots of stuttering).
  97. //
  98. // > I installed libpulse-dev, rebuilt SDL, and now SDL is using pulse
  99. // > audio and everything is working great. However I'm wondering if we
  100. // > need to fall back to PulseAudio in our codebase if SDL is doing that
  101. // > for us. I mean, is it worth me going through and debugging our Pulse
  102. // > Audio path or should I just remove it?
  103. //
  104. // Remove it...it never worked well, and only remained in case there were
  105. // concerns about relying on SDL. The SDL codepath is way easier to read,
  106. // simpler to maintain, and handles all sorts of strange audio backends,
  107. // including Pulse.
  108. if ( !pDevice )
  109. {
  110. DevMsg( "Trying PulseAudio Interface\n" );
  111. pDevice = Audio_CreatePulseAudioDevice(); // fall back to PulseAudio if SDL fails
  112. }
  113. #endif // NEVER
  114. #else
  115. #error
  116. #endif
  117. }
  118. #if defined( _X360 )
  119. else
  120. {
  121. pDevice = Audio_CreateXAudioDevice( true );
  122. if ( pDevice )
  123. {
  124. // xaudio requires threaded mixing
  125. S_EnableThreadedMixing( true );
  126. }
  127. }
  128. #endif
  129. #if defined( WIN32 ) && !defined( USE_SDL )
  130. NULLDEVICE:
  131. #endif
  132. snd_firsttime = false;
  133. if ( !pDevice )
  134. {
  135. if ( snd_firsttime )
  136. DevMsg( "No sound device initialized\n" );
  137. return Audio_GetNullDevice();
  138. }
  139. return pDevice;
  140. }
  141. /*
  142. ==============
  143. SNDDMA_Shutdown
  144. Reset the sound device for exiting
  145. ===============
  146. */
  147. void SNDDMA_Shutdown( void )
  148. {
  149. if ( g_AudioDevice != Audio_GetNullDevice() )
  150. {
  151. if ( g_AudioDevice )
  152. {
  153. g_AudioDevice->Shutdown();
  154. delete g_AudioDevice;
  155. }
  156. // the NULL device is always valid
  157. g_AudioDevice = Audio_GetNullDevice();
  158. }
  159. }