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.

213 lines
7.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef SND_CHANNELS_H
  7. #define SND_CHANNELS_H
  8. #include "mathlib/vector.h"
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. class CSfxTable;
  13. class CAudioMixer;
  14. typedef int SoundSource;
  15. // DO NOT REORDER: indices to fvolume arrays in channel_t
  16. #define IFRONT_LEFT 0 // NOTE: must correspond to order of fvolume array below!
  17. #define IFRONT_RIGHT 1
  18. #define IREAR_LEFT 2
  19. #define IREAR_RIGHT 3
  20. #define IFRONT_CENTER 4
  21. #define IFRONT_CENTER0 5 // dummy slot - center channel is mono, but mixers reference volume[1] slot
  22. #define IFRONT_LEFTD 6 // start of doppler right array
  23. #define IFRONT_RIGHTD 7
  24. #define IREAR_LEFTD 8
  25. #define IREAR_RIGHTD 9
  26. #define IFRONT_CENTERD 10
  27. #define IFRONT_CENTERD0 11 // dummy slot - center channel is mono, but mixers reference volume[1] slot
  28. #define CCHANVOLUMES 12
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Each currently playing wave is stored in a channel
  31. //-----------------------------------------------------------------------------
  32. // NOTE: 128bytes. These are memset to zero at some points. Do not add virtuals without changing that pattern.
  33. // UNDONE: now 300 bytes...
  34. struct channel_t
  35. {
  36. int guid; // incremented each time a channel is allocated (to match with channel free in tools, etc.)
  37. int userdata; // user specified data for syncing to tools
  38. CSfxTable *sfx; // the actual sound
  39. CAudioMixer *pMixer; // The sound's instance data for this channel
  40. // speaker channel volumes, indexed using IFRONT_LEFT to IFRONT_CENTER.
  41. // NOTE: never access these fvolume[] elements directly! Use channel helpers in snd_dma.cpp.
  42. float fvolume[CCHANVOLUMES]; // 0.0-255.0 current output volumes
  43. float fvolume_target[CCHANVOLUMES]; // 0.0-255.0 target output volumes
  44. float fvolume_inc[CCHANVOLUMES]; // volume increment, per frame, moves volume[i] to vol_target[i] (per spatialization)
  45. uint nFreeChannelAtSampleTime;
  46. SoundSource soundsource; // see iclientsound.h for description.
  47. int entchannel; // sound channel (CHAN_STREAM, CHAN_VOICE, etc.)
  48. int speakerentity; // if a sound is being played through a speaker entity (e.g., on a monitor,), this is the
  49. // entity upon which to show the lips moving, if the sound has sentence data
  50. short master_vol; // 0-255 master volume
  51. short basePitch; // base pitch percent (100% is normal pitch playback)
  52. float pitch; // real-time pitch after any modulation or shift by dynamic data
  53. int mixgroups[8]; // sound belongs to these mixgroups: world, actor, player weapon, explosion etc.
  54. int last_mixgroupid;// last mixgroupid selected
  55. float last_vol; // last volume after spatialization
  56. Vector origin; // origin of sound effect
  57. Vector direction; // direction of the sound
  58. float dist_mult; // distance multiplier (attenuation/clipK)
  59. float dspmix; // 0 - 1.0 proportion of dsp to mix with original sound, based on distance
  60. float dspface; // -1.0 - 1.0 (1.0 = facing listener)
  61. float distmix; // 0 - 1.0 proportion based on distance from listner (1.0 - 100% wav right - far)
  62. float dsp_mix_min; // for dspmix calculation - set by current preset in SND_GetDspMix
  63. float dsp_mix_max; // for dspmix calculation - set by current preset in SND_GetDspMix
  64. float radius; // Radius of this sound effect (spatialization is different within the radius)
  65. float ob_gain; // gain drop if sound source obscured from listener
  66. float ob_gain_target; // target gain while crossfading between ob_gain & ob_gain_target
  67. float ob_gain_inc; // crossfade increment
  68. short activeIndex;
  69. char wavtype; // 0 default, CHAR_DOPPLER, CHAR_DIRECTIONAL, CHAR_DISTVARIANT
  70. char pad;
  71. char sample_prev[8]; // last sample(s) in previous input data buffer - space for 2, 16 bit, stereo samples
  72. int initialStreamPosition;
  73. int special_dsp;
  74. union
  75. {
  76. unsigned int flagsword;
  77. struct
  78. {
  79. bool bUpdatePositions : 1; // if true, assume sound source can move and update according to entity
  80. bool isSentence : 1; // true if playing linked sentence
  81. bool bdry : 1; // if true, bypass all dsp processing for this sound (ie: music)
  82. bool bSpeaker : 1; // true if sound is playing through in-game speaker entity.
  83. bool bstereowav : 1; // if true, a stereo .wav file is the sample data source
  84. bool delayed_start : 1; // If true, sound had a delay and so same sound on same channel won't channel steal from it
  85. bool fromserver : 1; // for snd_show, networked sounds get colored differently than local sounds
  86. bool bfirstpass : 1; // true if this is first time sound is spatialized
  87. bool bTraced : 1; // true if channel was already checked this frame for obscuring
  88. bool bfast_pitch : 1; // true if using low quality pitch (fast, but no interpolation)
  89. bool m_bIsFreeingChannel : 1; // true when inside S_FreeChannel - prevents reentrance
  90. bool m_bCompatibilityAttenuation : 1; // True when we want to use goldsrc compatibility mode for the attenuation
  91. // In that case, dist_mul is set to a relatively meaningful value in StartDynamic/StartStaticSound,
  92. // but we interpret it totally differently in SND_GetGain.
  93. bool m_bShouldPause : 1; // if true, sound should pause when the game is paused
  94. bool m_bIgnorePhonemes : 1; // if true, we don't want to drive animation w/ phoneme data
  95. } flags;
  96. };
  97. };
  98. //-----------------------------------------------------------------------------
  99. //-----------------------------------------------------------------------------
  100. #define MAX_CHANNELS 128
  101. #define MAX_DYNAMIC_CHANNELS 64
  102. //-----------------------------------------------------------------------------
  103. //-----------------------------------------------------------------------------
  104. extern channel_t channels[MAX_CHANNELS];
  105. // 0 to MAX_DYNAMIC_CHANNELS-1 = normal entity sounds
  106. // MAX_DYNAMIC_CHANNELS to total_channels = static sounds
  107. extern int total_channels;
  108. class CChannelList
  109. {
  110. public:
  111. int Count();
  112. int GetChannelIndex( int listIndex );
  113. channel_t *GetChannel( int listIndex );
  114. void RemoveChannelFromList( int listIndex );
  115. bool IsQuashed( int listIndex );
  116. int m_count;
  117. short m_list[MAX_CHANNELS];
  118. bool m_quashed[MAX_CHANNELS]; // if true, the channel should be advanced, but not mixed, because it's been heuristically suppressed
  119. CUtlVector< int > m_nSpecialDSPs;
  120. bool m_hasSpeakerChannels : 1;
  121. bool m_hasDryChannels : 1;
  122. bool m_has11kChannels : 1;
  123. bool m_has22kChannels : 1;
  124. bool m_has44kChannels : 1;
  125. };
  126. inline int CChannelList::Count()
  127. {
  128. return m_count;
  129. }
  130. inline int CChannelList::GetChannelIndex( int listIndex )
  131. {
  132. return m_list[listIndex];
  133. }
  134. inline channel_t *CChannelList::GetChannel( int listIndex )
  135. {
  136. return &channels[GetChannelIndex(listIndex)];
  137. }
  138. inline bool CChannelList::IsQuashed( int listIndex )
  139. {
  140. return m_quashed[listIndex];
  141. }
  142. inline void CChannelList::RemoveChannelFromList( int listIndex )
  143. {
  144. // decrease the count by one, and swap the deleted channel with
  145. // the last one.
  146. m_count--;
  147. if ( m_count > 0 && listIndex != m_count )
  148. {
  149. m_list[listIndex] = m_list[m_count];
  150. m_quashed[listIndex] = m_quashed[m_count];
  151. }
  152. }
  153. class CActiveChannels
  154. {
  155. public:
  156. void Add( channel_t *pChannel );
  157. void Remove( channel_t *pChannel );
  158. void GetActiveChannels( CChannelList &list );
  159. void Init();
  160. int GetActiveCount() { return m_count; }
  161. private:
  162. int m_count;
  163. short m_list[MAX_CHANNELS];
  164. };
  165. extern CActiveChannels g_ActiveChannels;
  166. //=============================================================================
  167. #endif // SND_CHANNELS_H