Counter Strike : Global Offensive Source Code
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.

128 lines
4.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Sound code shared between server and client
  4. //
  5. //=============================================================================//
  6. #include <math.h>
  7. #include "convar.h"
  8. #include "sound.h"
  9. // NOTE: This has to be the last file included!
  10. #include "tier0/memdbgon.h"
  11. void DbReferenceChanged( IConVar *var, const char *pOldValue, float flOldValue );
  12. ConVar snd_refdist( "snd_refdist", "36", FCVAR_CHEAT, "Reference distance for snd_refdb" );
  13. ConVar snd_refdb( "snd_refdb", "60", FCVAR_CHEAT, "Reference dB at snd_refdist", &DbReferenceChanged );
  14. float snd_refdb_dist_mult = pow( 10.0f, 60.0f / 20.0f );
  15. ConVar snd_foliage_db_loss( "snd_foliage_db_loss", "4", FCVAR_CHEAT, "foliage dB loss per 1200 units" );
  16. ConVar snd_gain( "snd_gain", "1", FCVAR_CHEAT );
  17. ConVar snd_gain_max( "snd_gain_max", "1", FCVAR_CHEAT );
  18. ConVar snd_gain_min( "snd_gain_min", "0.01", FCVAR_CHEAT );
  19. // precomputed Db multipliers
  20. void DbReferenceChanged( IConVar *var, const char *pOldValue, float flOldValue )
  21. {
  22. snd_refdb_dist_mult = pow( 10.0f, snd_refdb.GetFloat() / 20.0f );
  23. }
  24. // calculate gain based on atmospheric attenuation.
  25. // as gain excedes threshold, round off (compress) towards 1.0 using spline
  26. #define SND_GAIN_COMP_EXP_MAX 2.5f // Increasing SND_GAIN_COMP_EXP_MAX fits compression curve more closely
  27. // to original gain curve as it approaches 1.0.
  28. #define SND_GAIN_COMP_EXP_MIN 0.8f
  29. //#define SND_GAIN_COMP_EXP_MIN 1.8f
  30. #define SND_GAIN_COMP_THRESH 0.5f // gain value above which gain curve is rounded to approach 1.0
  31. #define SND_DB_MAX 140.0f // max db of any sound source
  32. #define SND_DB_MED 90.0f // db at which compression curve changes
  33. #define SNDLVL_TO_DIST_MULT( sndlvl ) ( sndlvl ? ((snd_refdb_dist_mult / FastPow10( (float)sndlvl / 20 )) / snd_refdist.GetFloat()) : 0 )
  34. #define DIST_MULT_TO_SNDLVL( dist_mult ) (soundlevel_t)(int)( dist_mult ? ( 20 * log10( (float)(snd_refdb_dist_mult / (dist_mult * snd_refdist.GetFloat()) )) ) : 0 )
  35. float SND_GetGainFromMult( float gain, float dist_mult, vec_t dist )
  36. {
  37. // test additional attenuation
  38. // at 30c, 14.7psi, 60% humidity, 1000Hz == 0.22dB / 100ft.
  39. // dense foliage is roughly 2dB / 100ft
  40. float additional_dB_loss = snd_foliage_db_loss.GetFloat() * (dist / 1200);
  41. float additional_dist_mult = FastPow10( additional_dB_loss / 20);
  42. float relative_dist = dist * dist_mult * additional_dist_mult;
  43. // hard code clamp gain to 10x normal (assumes volume and external clipping)
  44. if (relative_dist > 0.1)
  45. {
  46. gain *= (1/relative_dist);
  47. }
  48. else
  49. gain *= 10.0;
  50. // if gain passess threshold, compress gain curve such that gain smoothly approaches 1.0
  51. if ( gain > SND_GAIN_COMP_THRESH )
  52. {
  53. float snd_gain_comp_power = SND_GAIN_COMP_EXP_MAX;
  54. soundlevel_t sndlvl = DIST_MULT_TO_SNDLVL( dist_mult );
  55. float Y;
  56. // decrease compression curve fit for higher sndlvl values
  57. if ( sndlvl > SND_DB_MED )
  58. {
  59. // snd_gain_power varies from max to min as sndlvl varies from 90 to 140
  60. snd_gain_comp_power = RemapVal ((float)sndlvl, SND_DB_MED, SND_DB_MAX, SND_GAIN_COMP_EXP_MAX, SND_GAIN_COMP_EXP_MIN);
  61. }
  62. // calculate crossover point
  63. Y = -1.0 / ( FastPow(SND_GAIN_COMP_THRESH, snd_gain_comp_power) * (SND_GAIN_COMP_THRESH - 1) );
  64. // calculate compressed gain
  65. gain = 1.0 - 1.0 / (Y * FastPow( gain, snd_gain_comp_power ) );
  66. gain = gain * snd_gain_max.GetFloat();
  67. }
  68. if ( gain < snd_gain_min.GetFloat() )
  69. {
  70. // sounds less than snd_gain_min fall off to 0 in distance it took them to fall to snd_gain_min
  71. gain = snd_gain_min.GetFloat() * (2.0 - relative_dist * snd_gain_min.GetFloat());
  72. if (gain <= 0.0)
  73. gain = 0.001; // don't propagate 0 gain
  74. }
  75. return gain;
  76. }
  77. float S_GetGainFromSoundLevel( soundlevel_t soundlevel, vec_t dist )
  78. {
  79. // this effecively means that gain is effecting falloff,
  80. // which it shouldn't do and should be removed
  81. // FIX ME: Morasky
  82. float gain = snd_gain.GetFloat();
  83. float dist_mult = SNDLVL_TO_DIST_MULT( soundlevel );
  84. if ( dist_mult )
  85. {
  86. gain = SND_GetGainFromMult( gain, dist_mult, dist );
  87. }
  88. return gain;
  89. }