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.

272 lines
10 KiB

  1. //========= Copyright 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 "vstdlib/random.h"
  14. #include "soundflags.h"
  15. #include "mathlib/compressed_vector.h"
  16. #include "appframework/IAppSystem.h"
  17. #define SOUNDEMITTERSYSTEM_INTERFACE_VERSION "VSoundEmitter002"
  18. #define SOUNDGENDER_MACRO "$gender"
  19. #define SOUNDGENDER_MACRO_LENGTH 7 // Length of above including $
  20. typedef short HSOUNDSCRIPTHANDLE;
  21. #define SOUNDEMITTER_INVALID_HANDLE (HSOUNDSCRIPTHANDLE)-1
  22. class IFileList;
  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. }
  41. int channel;
  42. float volume;
  43. int pitch;
  44. int pitchlow, pitchhigh;
  45. soundlevel_t soundlevel;
  46. // For weapon sounds...
  47. bool play_to_owner_only;
  48. int count;
  49. char soundname[ 128 ];
  50. int delay_msec;
  51. };
  52. // 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
  53. const char *SoundLevelToString( soundlevel_t level );
  54. const char *ChannelToString( int channel );
  55. const char *VolumeToString( float volume );
  56. const char *PitchToString( float pitch );
  57. soundlevel_t TextToSoundLevel( const char *key );
  58. int TextToChannel( const char *name );
  59. enum gender_t
  60. {
  61. GENDER_NONE = 0,
  62. GENDER_MALE,
  63. GENDER_FEMALE,
  64. };
  65. #pragma pack(1)
  66. struct SoundFile
  67. {
  68. SoundFile()
  69. {
  70. symbol = UTL_INVAL_SYMBOL;
  71. gender = GENDER_NONE;
  72. available = true;
  73. COMPILE_TIME_ASSERT( sizeof(SoundFile) == 4 );
  74. }
  75. CUtlSymbol symbol;
  76. byte gender;
  77. byte available;
  78. };
  79. #pragma pack()
  80. #pragma pack(1)
  81. template<typename T>
  82. struct sound_interval_t
  83. {
  84. T start;
  85. T range;
  86. interval_t &ToInterval( interval_t &dest ) const { dest.start = start; dest.range = range; return dest; }
  87. void FromInterval( const interval_t &from ) { start = from.start; range = from.range; }
  88. float Random() const { return RandomFloat( start, start + range ); }
  89. };
  90. #pragma pack()
  91. typedef sound_interval_t<float16_with_assign> volume_interval_t;
  92. typedef sound_interval_t<uint16> soundlevel_interval_t;
  93. typedef sound_interval_t<uint8> pitch_interval_t;
  94. #pragma pack(1)
  95. struct CSoundParametersInternal
  96. {
  97. CSoundParametersInternal();
  98. ~CSoundParametersInternal();
  99. void CopyFrom( const CSoundParametersInternal& src );
  100. bool operator == ( const CSoundParametersInternal& other ) const;
  101. const char *VolumeToString( void ) const;
  102. const char *ChannelToString( void ) const;
  103. const char *SoundLevelToString( void ) const;
  104. const char *PitchToString( void ) const;
  105. void VolumeFromString( const char *sz );
  106. void ChannelFromString( const char *sz );
  107. void PitchFromString( const char *sz );
  108. void SoundLevelFromString( const char *sz );
  109. int GetChannel() const { return channel; }
  110. const volume_interval_t &GetVolume() const { return volume; }
  111. const pitch_interval_t &GetPitch() const { return pitch; }
  112. const soundlevel_interval_t &GetSoundLevel() const { return soundlevel; }
  113. int GetDelayMsec() const { return delay_msec; }
  114. bool OnlyPlayToOwner() const { return play_to_owner_only; }
  115. bool HadMissingWaveFiles() const { return had_missing_wave_files; }
  116. bool UsesGenderToken() const { return uses_gender_token; }
  117. bool ShouldPreload() const { return m_bShouldPreload; }
  118. void SetChannel( int newChannel ) { channel = newChannel; }
  119. void SetVolume( float start, float range = 0.0 ) { volume.start = start; volume.range = range; }
  120. void SetPitch( float start, float range = 0.0 ) { pitch.start = start; pitch.range = range; }
  121. void SetSoundLevel( float start, float range = 0.0 ) { soundlevel.start = start; soundlevel.range = range; }
  122. void SetDelayMsec( int delay ) { delay_msec = delay; }
  123. void SetShouldPreload( bool bShouldPreload ) { m_bShouldPreload = bShouldPreload; }
  124. void SetOnlyPlayToOwner( bool b ) { play_to_owner_only = b; }
  125. void SetHadMissingWaveFiles( bool b ) { had_missing_wave_files = b; }
  126. void SetUsesGenderToken( bool b ) { uses_gender_token = b; }
  127. void AddSoundName( const SoundFile &soundFile ) { AddToTail( &m_pSoundNames, &m_nSoundNames, soundFile ); }
  128. int NumSoundNames() const { return m_nSoundNames; }
  129. SoundFile * GetSoundNames() { return ( m_nSoundNames == 1 ) ? (SoundFile *)&m_pSoundNames : m_pSoundNames; }
  130. const SoundFile *GetSoundNames() const { return ( m_nSoundNames == 1 ) ? (SoundFile *)&m_pSoundNames : m_pSoundNames; }
  131. void AddConvertedName( const SoundFile &soundFile ) { AddToTail( &m_pConvertedNames, &m_nConvertedNames, soundFile ); }
  132. int NumConvertedNames() const { return m_nConvertedNames; }
  133. SoundFile * GetConvertedNames() { return ( m_nConvertedNames == 1 ) ? (SoundFile *)&m_pConvertedNames : m_pConvertedNames; }
  134. const SoundFile *GetConvertedNames() const { return ( m_nConvertedNames == 1 ) ? (SoundFile *)&m_pConvertedNames : m_pConvertedNames; }
  135. private:
  136. void operator=( const CSoundParametersInternal& src ); // disallow implicit copies
  137. CSoundParametersInternal( const CSoundParametersInternal& src );
  138. void AddToTail( SoundFile **pDest, uint16 *pDestCount, const SoundFile &source );
  139. SoundFile * m_pSoundNames; // 4
  140. SoundFile * m_pConvertedNames; // 8
  141. uint16 m_nSoundNames; // 10
  142. uint16 m_nConvertedNames; // 12
  143. volume_interval_t volume; // 16
  144. soundlevel_interval_t soundlevel; // 20
  145. pitch_interval_t pitch; // 22
  146. uint16 channel; // 24
  147. uint16 delay_msec; // 26
  148. bool play_to_owner_only:1; // For weapon sounds... // 27
  149. // Internal use, for warning about missing .wav files
  150. bool had_missing_wave_files:1;
  151. bool uses_gender_token:1;
  152. bool m_bShouldPreload:1;
  153. byte reserved; // 28
  154. };
  155. #pragma pack()
  156. //-----------------------------------------------------------------------------
  157. // Purpose: Base class for sound emitter system handling (can be used by tools)
  158. //-----------------------------------------------------------------------------
  159. abstract_class ISoundEmitterSystemBase : public IAppSystem
  160. {
  161. public:
  162. // Init, shutdown called after we know what mod is running
  163. virtual bool ModInit() = 0;
  164. virtual void ModShutdown() = 0;
  165. virtual int GetSoundIndex( const char *pName ) const = 0;
  166. virtual bool IsValidIndex( int index ) = 0;
  167. virtual int GetSoundCount( void ) = 0;
  168. virtual const char *GetSoundName( int index ) = 0;
  169. virtual bool GetParametersForSound( const char *soundname, CSoundParameters& params, gender_t gender, bool isbeingemitted = false ) = 0;
  170. virtual const char *GetWaveName( CUtlSymbol& sym ) = 0;
  171. virtual CUtlSymbol AddWaveName( const char *name ) = 0;
  172. virtual soundlevel_t LookupSoundLevel( const char *soundname ) = 0;
  173. virtual const char *GetWavFileForSound( const char *soundname, char const *actormodel ) = 0;
  174. virtual const char *GetWavFileForSound( const char *soundname, gender_t gender ) = 0;
  175. virtual int CheckForMissingWavFiles( bool verbose ) = 0;
  176. virtual const char *GetSourceFileForSound( int index ) const = 0;
  177. // Iteration methods
  178. virtual int First() const = 0;
  179. virtual int Next( int i ) const = 0;
  180. virtual int InvalidIndex() const = 0;
  181. virtual CSoundParametersInternal *InternalGetParametersForSound( int index ) = 0;
  182. // The host application is responsible for dealing with dirty sound scripts, etc.
  183. virtual bool AddSound( const char *soundname, const char *scriptfile, const CSoundParametersInternal& params ) = 0;
  184. virtual void RemoveSound( const char *soundname ) = 0;
  185. virtual void MoveSound( const char *soundname, const char *newscript ) = 0;
  186. virtual void RenameSound( const char *soundname, const char *newname ) = 0;
  187. virtual void UpdateSoundParameters( const char *soundname, const CSoundParametersInternal& params ) = 0;
  188. virtual int GetNumSoundScripts() const = 0;
  189. virtual char const *GetSoundScriptName( int index ) const = 0;
  190. virtual bool IsSoundScriptDirty( int index ) const = 0;
  191. virtual int FindSoundScript( const char *name ) const = 0;
  192. virtual void SaveChangesToSoundScript( int scriptindex ) = 0;
  193. virtual void ExpandSoundNameMacros( CSoundParametersInternal& params, char const *wavename ) = 0;
  194. virtual gender_t GetActorGender( char const *actormodel ) = 0;
  195. virtual void GenderExpandString( char const *actormodel, char const *in, char *out, int maxlen ) = 0;
  196. virtual void GenderExpandString( gender_t gender, char const *in, char *out, int maxlen ) = 0;
  197. virtual bool IsUsingGenderToken( char const *soundname ) = 0;
  198. // For blowing away caches based on filetimstamps of the manifest, or of any of the
  199. // .txt files that are read into the sound emitter system
  200. virtual unsigned int GetManifestFileTimeChecksum() = 0;
  201. // Called from both client and server (single player) or just one (server only in dedicated server and client only if connected to a remote server)
  202. // Called by LevelInitPreEntity to override sound scripts for the mod with level specific overrides based on custom mapnames, etc.
  203. virtual void AddSoundOverrides( char const *scriptfile, bool bPreload = false ) = 0;
  204. // Called by either client or server in LevelShutdown to clear out custom overrides
  205. virtual void ClearSoundOverrides() = 0;
  206. virtual bool GetParametersForSoundEx( const char *soundname, HSOUNDSCRIPTHANDLE& handle, CSoundParameters& params, gender_t gender, bool isbeingemitted = false ) = 0;
  207. virtual soundlevel_t LookupSoundLevelByHandle( char const *soundname, HSOUNDSCRIPTHANDLE& handle ) = 0;
  208. virtual void ReloadSoundEntriesInList( IFileList *pFilesToReload ) = 0;
  209. // Called by either client or server to force ModShutdown and ModInit
  210. virtual void Flush() = 0;
  211. };
  212. #endif // ISOUNDEMITTERSYSTEMBASE_H