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.

311 lines
12 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef ISOUNDEMITTERSYSTEMBASE_H
  8. #define ISOUNDEMITTERSYSTEMBASE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/utldict.h"
  13. #include "soundflags.h"
  14. #include "mathlib/compressed_vector.h"
  15. #include "appframework/iappsystem.h"
  16. #include "tier3/tier3.h"
  17. DECLARE_LOGGING_CHANNEL( LOG_SND_EMITTERSYSTEM );
  18. #define SOUNDGENDER_MACRO "$gender"
  19. #define SOUNDGENDER_MACRO_LENGTH 7 // Length of above including $
  20. class KeyValues;
  21. typedef unsigned int HSOUNDSCRIPTHASH;
  22. #define SOUNDEMITTER_INVALID_HASH (HSOUNDSCRIPTHASH)-1
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. struct CSoundParameters
  27. {
  28. CSoundParameters()
  29. {
  30. channel = CHAN_AUTO; // 0
  31. volume = VOL_NORM; // 1.0f
  32. pitch = PITCH_NORM; // 100
  33. pitchlow = PITCH_NORM;
  34. pitchhigh = PITCH_NORM;
  35. soundlevel = SNDLVL_NORM; // 75dB
  36. soundname[ 0 ] = 0;
  37. play_to_owner_only = false;
  38. count = 0;
  39. delay_msec = 0;
  40. m_nSoundEntryVersion = 1;
  41. m_hSoundScriptHash = SOUNDEMITTER_INVALID_HASH;
  42. m_pOperatorsKV = NULL;
  43. m_nRandomSeed = -1;
  44. m_bHRTFFollowEntity = false;
  45. m_bHRTFBilinear = false;
  46. }
  47. int channel;
  48. float volume;
  49. int pitch;
  50. int pitchlow, pitchhigh;
  51. soundlevel_t soundlevel;
  52. // For weapon sounds...
  53. bool play_to_owner_only;
  54. int count;
  55. char soundname[ 128 ];
  56. int delay_msec;
  57. HSOUNDSCRIPTHASH m_hSoundScriptHash;
  58. int m_nSoundEntryVersion;
  59. KeyValues *m_pOperatorsKV;
  60. int m_nRandomSeed;
  61. bool m_bHRTFFollowEntity;
  62. bool m_bHRTFBilinear;
  63. };
  64. // A bit of a hack, but these are just utility function which are implemented in the SouneParametersInternal.cpp file which all users of this lib also compile
  65. const char *SoundLevelToString( soundlevel_t level );
  66. const char *ChannelToString( int channel );
  67. const char *VolumeToString( float volume );
  68. const char *PitchToString( float pitch );
  69. soundlevel_t TextToSoundLevel( const char *key );
  70. int TextToChannel( const char *name );
  71. enum gender_t
  72. {
  73. GENDER_NONE = 0,
  74. GENDER_MALE,
  75. GENDER_FEMALE,
  76. };
  77. #pragma pack(1)
  78. struct SoundFile
  79. {
  80. SoundFile()
  81. {
  82. symbol = UTL_INVAL_SYMBOL;
  83. gender = GENDER_NONE;
  84. available = true;
  85. COMPILE_TIME_ASSERT( sizeof(SoundFile) == 4 );
  86. }
  87. CUtlSymbol symbol;
  88. byte gender;
  89. byte available;
  90. };
  91. #pragma pack()
  92. #pragma pack(1)
  93. template<typename T>
  94. struct sound_interval_t
  95. {
  96. T start;
  97. T range;
  98. interval_t &ToInterval( interval_t &dest ) const { dest.start = start; dest.range = range; return dest; }
  99. void FromInterval( const interval_t &from ) { start = from.start; range = from.range; }
  100. float Random() const { return RandomFloat( start, start+range ); }
  101. };
  102. #pragma pack()
  103. typedef sound_interval_t<float16_with_assign> volume_interval_t;
  104. typedef sound_interval_t<uint16> soundlevel_interval_t;
  105. typedef sound_interval_t<uint8> pitch_interval_t;
  106. #pragma pack(1)
  107. struct CSoundParametersInternal
  108. {
  109. CSoundParametersInternal();
  110. ~CSoundParametersInternal();
  111. void CopyFrom( const CSoundParametersInternal& src );
  112. bool operator == ( const CSoundParametersInternal& other ) const;
  113. const char *VolumeToString( void ) const;
  114. const char *ChannelToString( void ) const;
  115. const char *SoundLevelToString( void ) const;
  116. const char *PitchToString( void ) const;
  117. void VolumeFromString( const char *sz );
  118. void ChannelFromString( const char *sz );
  119. void PitchFromString( const char *sz );
  120. void SoundLevelFromString( const char *sz );
  121. int GetChannel() const { return channel; }
  122. const volume_interval_t &GetVolume() const { return volume; }
  123. const pitch_interval_t &GetPitch() const { return pitch; }
  124. const soundlevel_interval_t &GetSoundLevel() const { return soundlevel; }
  125. int GetDelayMsec() const { return delay_msec; }
  126. int GetSoundEntryVersion() const { return m_nSoundEntryVersion; }
  127. bool OnlyPlayToOwner() const { return play_to_owner_only; }
  128. bool HadMissingWaveFiles() const { return had_missing_wave_files; }
  129. bool UsesGenderToken() const { return uses_gender_token; }
  130. bool ShouldPreload() const { return m_bShouldPreload; }
  131. bool ShouldAutoCache() const { return m_bShouldAutoCache; }
  132. bool HasCached() const { return m_bHasCached; }
  133. bool HasHRTFFollowEntity() const { return m_bHRTFFollowEntity; }
  134. bool HasHRTFBilinear() const { return m_bHRTFBilinear; }
  135. void SetChannel( int newChannel ) { channel = newChannel; }
  136. void SetVolume( float start, float range = 0.0 ) { volume.start = ( uint8 )start; volume.range = ( uint8 )range; }
  137. void SetPitch( float start, float range = 0.0 ) { pitch.start = ( uint8 )start; pitch.range = ( uint8 )range; }
  138. void SetSoundLevel( float start, float range = 0.0 ) { soundlevel.start = ( uint16 )start; soundlevel.range = ( uint16 )range; }
  139. void SetDelayMsec( int delay ) { delay_msec = delay; }
  140. void SetSoundEntryVersion( int gameSoundVersion ) { m_nSoundEntryVersion = gameSoundVersion; }
  141. void SetShouldPreload( bool bShouldPreload ) { m_bShouldPreload = bShouldPreload; }
  142. void SetShouldAutoCache( bool bShouldAutoCache ) { m_bShouldAutoCache = bShouldAutoCache; }
  143. void SetOnlyPlayToOwner( bool b ) { play_to_owner_only = b; }
  144. void SetHadMissingWaveFiles( bool b ) { had_missing_wave_files = b; }
  145. void SetUsesGenderToken( bool b ) { uses_gender_token = b; }
  146. void SetCached( bool b ) { m_bHasCached = b; }
  147. void SetHRTFFollowEntity( bool b ) { m_bHRTFFollowEntity = b; }
  148. void SetHRTFBilinear( bool b ) { m_bHRTFBilinear = b; }
  149. void AddSoundName( const SoundFile &soundFile ) { AddToTail( &m_pSoundNames, &m_nSoundNames, soundFile ); }
  150. int NumSoundNames() const { return m_nSoundNames; }
  151. SoundFile * GetSoundNames() { return ( m_nSoundNames == 1 ) ? (SoundFile *)&m_pSoundNames : m_pSoundNames; }
  152. const SoundFile *GetSoundNames() const { return ( m_nSoundNames == 1 ) ? (SoundFile *)&m_pSoundNames : m_pSoundNames; }
  153. void AddConvertedName( const SoundFile &soundFile ) { AddToTail( &m_pConvertedNames, &m_nConvertedNames, soundFile ); }
  154. int NumConvertedNames() const { return m_nConvertedNames; }
  155. SoundFile * GetConvertedNames() { return ( m_nConvertedNames == 1 ) ? (SoundFile *)&m_pConvertedNames : m_pConvertedNames; }
  156. const SoundFile *GetConvertedNames() const { return ( m_nConvertedNames == 1 ) ? (SoundFile *)&m_pConvertedNames : m_pConvertedNames; }
  157. // Sound Operator System: this should be optimized into something less heavy
  158. KeyValues *GetOperatorsKV( void ) const { return m_pOperatorsKV; }
  159. void SetOperatorsKV( KeyValues *src );
  160. private:
  161. void operator=( const CSoundParametersInternal& src ); // disallow implicit copies
  162. CSoundParametersInternal( const CSoundParametersInternal& src );
  163. void AddToTail( SoundFile **pDest, uint16 *pDestCount, const SoundFile &source );
  164. SoundFile * m_pSoundNames; // 4
  165. SoundFile * m_pConvertedNames; // 8
  166. uint16 m_nSoundNames; // 10
  167. uint16 m_nConvertedNames; // 12
  168. volume_interval_t volume; // 16
  169. soundlevel_interval_t soundlevel; // 20
  170. pitch_interval_t pitch; // 22
  171. uint16 channel; // 24
  172. uint16 delay_msec; // 26
  173. uint16 m_nSoundEntryVersion; // 28
  174. bool play_to_owner_only:1; // For weapon sounds... // 29
  175. // Internal use, for warning about missing .wav files
  176. bool had_missing_wave_files:1;
  177. bool uses_gender_token:1;
  178. bool m_bShouldPreload:1;
  179. bool m_bHasCached:1;
  180. bool m_bShouldAutoCache:1;
  181. bool m_bHRTFFollowEntity : 1;
  182. bool m_bHRTFBilinear : 1;
  183. byte reserved; // 30
  184. KeyValues * m_pGameData; // 34
  185. KeyValues * m_pOperatorsKV; // 38
  186. };
  187. #pragma pack()
  188. //-----------------------------------------------------------------------------
  189. // Purpose: Base class for sound emitter system handling (can be used by tools)
  190. //-----------------------------------------------------------------------------
  191. abstract_class ISoundEmitterSystemBase : public IAppSystem
  192. {
  193. public:
  194. // Unused, left in the interface so I don't have to rebuild all
  195. virtual void Unused1() {}
  196. virtual void Unused2() {}
  197. virtual int GetSoundIndex( const char *pName ) const = 0;
  198. virtual bool IsValidIndex( int index ) = 0;
  199. virtual int GetSoundCount( void ) = 0;
  200. virtual const char *GetSoundName( int index ) = 0;
  201. virtual bool GetParametersForSound( const char *soundname, CSoundParameters& params, gender_t gender, bool isbeingemitted = false ) = 0;
  202. virtual const char *GetWaveName( CUtlSymbol& sym ) = 0;
  203. virtual CUtlSymbol AddWaveName( const char *name ) = 0;
  204. virtual soundlevel_t LookupSoundLevel( const char *soundname ) = 0;
  205. virtual const char *GetWavFileForSound( const char *soundname, char const *actormodel ) = 0;
  206. virtual const char *GetWavFileForSound( const char *soundname, gender_t gender ) = 0;
  207. virtual int CheckForMissingWavFiles( bool verbose ) = 0;
  208. virtual const char *GetSourceFileForSound( int index ) const = 0;
  209. // Iteration methods
  210. virtual int First() const = 0;
  211. virtual int Next( int i ) const = 0;
  212. virtual int InvalidIndex() const = 0;
  213. virtual CSoundParametersInternal *InternalGetParametersForSound( int index ) = 0;
  214. // The host application is responsible for dealing with dirty sound scripts, etc.
  215. virtual bool AddSound( const char *soundname, const char *scriptfile, const CSoundParametersInternal& params ) = 0;
  216. virtual void RemoveSound( const char *soundname ) = 0;
  217. virtual void MoveSound( const char *soundname, const char *newscript ) = 0;
  218. virtual void RenameSound( const char *soundname, const char *newname ) = 0;
  219. virtual void UpdateSoundParameters( const char *soundname, const CSoundParametersInternal& params ) = 0;
  220. virtual int GetNumSoundScripts() const = 0;
  221. virtual char const *GetSoundScriptName( int index ) const = 0;
  222. virtual bool IsSoundScriptDirty( int index ) const = 0;
  223. virtual int FindSoundScript( const char *name ) const = 0;
  224. virtual void SaveChangesToSoundScript( int scriptindex ) = 0;
  225. virtual void ExpandSoundNameMacros( CSoundParametersInternal& params, char const *wavename ) = 0;
  226. virtual gender_t GetActorGender( char const *actormodel ) = 0;
  227. virtual void GenderExpandString( char const *actormodel, char const *in, char *out, int maxlen ) = 0;
  228. virtual void GenderExpandString( gender_t gender, char const *in, char *out, int maxlen ) = 0;
  229. virtual bool IsUsingGenderToken( char const *soundname ) = 0;
  230. // For blowing away caches based on filetimstamps of the manifest, or of any of the
  231. // .txt files that are read into the sound emitter system
  232. virtual unsigned int GetManifestFileTimeChecksum() = 0;
  233. virtual bool GetParametersForSoundEx( const char *soundname, HSOUNDSCRIPTHASH& handle, CSoundParameters& params, gender_t gender, bool isbeingemitted = false ) = 0;
  234. virtual soundlevel_t LookupSoundLevelByHandle( char const *soundname, HSOUNDSCRIPTHASH& handle ) = 0;
  235. virtual KeyValues *GetOperatorKVByHandle( HSOUNDSCRIPTHASH& handle ) = 0;
  236. virtual char const *GetSoundNameForHash( HSOUNDSCRIPTHASH hash ) const = 0; // Returns NULL if hash not found!!!
  237. virtual int GetSoundIndexForHash( HSOUNDSCRIPTHASH hash ) const = 0;
  238. virtual HSOUNDSCRIPTHASH HashSoundName( char const *pchSndName ) const = 0;
  239. virtual bool IsValidHash( HSOUNDSCRIPTHASH hash ) const = 0;
  240. virtual void DescribeSound( char const *soundname ) = 0;
  241. // Flush and reload
  242. virtual void Flush() = 0;
  243. virtual void AddSoundsFromFile( const char *filename, bool bPreload, bool bAutoCache, bool bIsOverride = false ) = 0;
  244. };
  245. #endif // ISOUNDEMITTERSYSTEMBASE_H