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.

200 lines
4.0 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #if !defined( MOUTHINFO_H )
  14. #define MOUTHINFO_H
  15. #ifdef _WIN32
  16. #pragma once
  17. #endif
  18. class CAudioSource;
  19. #pragma pack(push,4)
  20. class CVoiceData
  21. {
  22. public:
  23. CVoiceData( void )
  24. {
  25. m_flElapsed = 0.0f;
  26. m_pAudioSource = NULL;
  27. m_bIgnorePhonemes = false;
  28. }
  29. void SetElapsedTime( float t )
  30. {
  31. m_flElapsed = t;
  32. }
  33. float GetElapsedTime() const
  34. {
  35. return m_flElapsed;
  36. }
  37. void SetSource( CAudioSource *source, bool bIgnorePhonemes )
  38. {
  39. m_pAudioSource = source;
  40. m_bIgnorePhonemes = bIgnorePhonemes;
  41. }
  42. bool ShouldIgnorePhonemes() const
  43. {
  44. return m_bIgnorePhonemes;
  45. }
  46. CAudioSource *GetSource()
  47. {
  48. return m_pAudioSource;
  49. }
  50. private:
  51. float m_flElapsed;
  52. CAudioSource *m_pAudioSource;
  53. bool m_bIgnorePhonemes;
  54. };
  55. #define UNKNOWN_VOICE_SOURCE -1
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Describes position of mouth for lip syncing
  58. //-----------------------------------------------------------------------------
  59. class CMouthInfo
  60. {
  61. public:
  62. // 0 = mouth closed, 255 = mouth agape
  63. byte mouthopen;
  64. // counter for running average
  65. byte sndcount;
  66. // running average
  67. int sndavg;
  68. public:
  69. CMouthInfo( void ) { m_nVoiceSources = 0; m_needsEnvelope = false; }
  70. ~CMouthInfo( void ) { ClearVoiceSources(); }
  71. int GetNumVoiceSources( void );
  72. CVoiceData *GetVoiceSource( int number );
  73. void ClearVoiceSources( void );
  74. int GetIndexForSource( CAudioSource *source );
  75. bool IsSourceReferenced( CAudioSource *source );
  76. CVoiceData *AddSource( CAudioSource *source, bool bIgnorePhonemes );
  77. void RemoveSource( CAudioSource *source );
  78. void RemoveSourceByIndex( int index );
  79. bool IsActive( void );
  80. bool NeedsEnvelope() { return m_needsEnvelope != 0; }
  81. void ActivateEnvelope() { m_needsEnvelope = true; }
  82. private:
  83. enum
  84. {
  85. MAX_VOICE_DATA = 4
  86. };
  87. short m_nVoiceSources;
  88. short m_needsEnvelope;
  89. CVoiceData m_VoiceSources[ MAX_VOICE_DATA ];
  90. };
  91. #pragma pack(pop)
  92. inline bool CMouthInfo::IsActive( void )
  93. {
  94. return ( GetNumVoiceSources() > 0 ) ? true : false;
  95. }
  96. inline int CMouthInfo::GetNumVoiceSources( void )
  97. {
  98. return m_nVoiceSources;
  99. }
  100. inline CVoiceData *CMouthInfo::GetVoiceSource( int number )
  101. {
  102. if ( number < 0 || number >= m_nVoiceSources )
  103. return NULL;
  104. return &m_VoiceSources[ number ];
  105. }
  106. inline void CMouthInfo::ClearVoiceSources( void )
  107. {
  108. m_nVoiceSources = 0;
  109. }
  110. inline int CMouthInfo::GetIndexForSource( CAudioSource *source )
  111. {
  112. for ( int i = 0; i < m_nVoiceSources; i++ )
  113. {
  114. CVoiceData *v = &m_VoiceSources[ i ];
  115. if ( !v )
  116. continue;
  117. if ( v->GetSource() == source )
  118. return i;
  119. }
  120. return UNKNOWN_VOICE_SOURCE;
  121. }
  122. inline bool CMouthInfo::IsSourceReferenced( CAudioSource *source )
  123. {
  124. if ( GetIndexForSource( source ) != UNKNOWN_VOICE_SOURCE )
  125. return true;
  126. return false;
  127. }
  128. inline void CMouthInfo::RemoveSource( CAudioSource *source )
  129. {
  130. int idx = GetIndexForSource( source );
  131. if ( idx == UNKNOWN_VOICE_SOURCE )
  132. return;
  133. RemoveSourceByIndex( idx );
  134. }
  135. inline void CMouthInfo::RemoveSourceByIndex( int index )
  136. {
  137. if ( index < 0 || index >= m_nVoiceSources )
  138. return;
  139. --m_nVoiceSources;
  140. if ( m_nVoiceSources > 0 )
  141. {
  142. m_VoiceSources[ index ] = m_VoiceSources[ m_nVoiceSources ];
  143. }
  144. }
  145. inline CVoiceData *CMouthInfo::AddSource( CAudioSource *source, bool bIgnorePhonemes )
  146. {
  147. int idx = GetIndexForSource( source );
  148. if ( idx == UNKNOWN_VOICE_SOURCE )
  149. {
  150. if ( m_nVoiceSources < MAX_VOICE_DATA )
  151. {
  152. idx = m_nVoiceSources++;
  153. }
  154. else
  155. {
  156. // No room!
  157. return NULL;
  158. }
  159. }
  160. CVoiceData *data = &m_VoiceSources[ idx ];
  161. data->SetSource( source, bIgnorePhonemes );
  162. data->SetElapsedTime( 0.0f );
  163. return data;
  164. }
  165. #endif // MOUTHINFO_H