llvoicevisualizer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @file llvoicevisualizer.h
  3. * @brief Draws in-world speaking indicators.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. //-----------------------------------------------------------------------------
  33. // VOICE VISUALIZER (latest update to this info: Jan 18, 2007)
  34. // author: JJ Ventrella, Linden Lab
  35. //
  36. // The Voice Visualizer is responsible for taking realtime signals from actual
  37. // users speaking and visualizing this speech in two forms:
  38. //
  39. // 1.- as a dynamic sound symbol (also referred to as the "voice indicator"
  40. // that appears over the avatar's head);
  41. // 2.- as gesticulation events that are used to trigger avatr gestures.
  42. //
  43. // The input for the voice visualizer is a continual stream of voice amplitudes.
  44. //-----------------------------------------------------------------------------
  45. #ifndef LL_LLVOICEVISUALIZER_H
  46. #define LL_LLVOICEVISUALIZER_H
  47. #include "llhudeffect.h"
  48. class LLViewerTexture;
  49. //-----------------------------------------------------------------------------
  50. // The values of voice gesticulation represent energy levels for avatar
  51. // animation, based on amplitude surge events parsed from the voice signal.
  52. // These are made available so that the appropriate kind of avatar animation
  53. // can be triggered, and thereby simulate the physical motion effects of
  54. // speech. It is recommended that multiple body parts be animated as well as
  55. // lips, such as head, shoulders, and hands, with large gestures used when the
  56. // energy level is high.
  57. //-----------------------------------------------------------------------------
  58. enum VoiceGesticulationLevel
  59. {
  60. VOICE_GESTICULATION_LEVEL_OFF = -1,
  61. VOICE_GESTICULATION_LEVEL_LOW = 0,
  62. VOICE_GESTICULATION_LEVEL_MEDIUM,
  63. VOICE_GESTICULATION_LEVEL_HIGH,
  64. NUM_VOICE_GESTICULATION_LEVELS
  65. };
  66. constexpr U32 NUM_VOICE_WAVES = 7;
  67. class LLVoiceVisualizer final : public LLHUDEffect
  68. {
  69. friend class LLHUDObject;
  70. protected:
  71. LOG_CLASS(LLVoiceVisualizer);
  72. public:
  73. LLVoiceVisualizer(U8 type);
  74. // Whether or not the user is voice enabled
  75. LL_INLINE void setVoiceEnabled(bool b) { mVoiceEnabled = b; }
  76. // 'pos' should be the position of the speaking avatar's head
  77. LL_INLINE void setVoiceSourceWorldPosition(const LLVector3& pos)
  78. {
  79. mVoiceSourceWorldPosition = pos;
  80. }
  81. // Called when the avatar starts speaking
  82. void setStartSpeaking();
  83. // Called when the avatar stops speaking
  84. void setStopSpeaking();
  85. // Is the avatar currently speaking ?
  86. LL_INLINE bool getCurrentlySpeaking() { return mCurrentlySpeaking; }
  87. // The lower range of meaningful amplitude for setting gesticulation level
  88. LL_INLINE void setMinGesticulationAmplitude(F32 a)
  89. {
  90. mMinGesticulationAmplitude = a;
  91. }
  92. // The upper range of meaningful amplitude for setting gesticulation level
  93. LL_INLINE void setMaxGesticulationAmplitude(F32 a)
  94. {
  95. mMaxGesticulationAmplitude = a;
  96. }
  97. // How loud the avatar is speaking (ranges from 0 to 1)
  98. LL_INLINE void setSpeakingAmplitude(F32 a) { mSpeakingAmplitude = a; }
  99. // Based on voice amplitude, returns the current "energy level" of the
  100. // avatar's speech.
  101. VoiceGesticulationLevel getCurrentGesticulationLevel();
  102. void lipSyncOohAah(F32& ooh, F32& aah);
  103. // LLHUDEffect overrides
  104. void markDead() override;
  105. void packData(LLMessageSystem* mesgsys) override;
  106. void unpackData(LLMessageSystem *mesgsys, S32 blocknum) override;
  107. // LLHUDObject override
  108. void render() override;
  109. //-------------------------------------------------------------------------
  110. // "setMaxGesticulationAmplitude" and "setMinGesticulationAmplitude" allow
  111. // for the tuning of the gesticulation level detector to be responsive to
  112. // different kinds of signals. For instance, we may find that the average
  113. // voice amplitude rarely exceeds 0.7 (in a range from 0 to 1), and
  114. // therefore we may want to set 0.7 as the max, so we can more easily catch
  115. // all the variance within that range. Also, we may find that there is
  116. // often noise below a certain range like 0.1 and so we would want to set
  117. // 0.1 as the min so as not to accidentally use this as signal.
  118. //-------------------------------------------------------------------------
  119. void setMaxGesticulationAmplitude();
  120. void setMinGesticulationAmplitude();
  121. static void setPreferences();
  122. // Converts a string of digits to an array of floats
  123. static void lipStringToF32s(std::string& in_string, F32*& out, U32& count);
  124. private:
  125. struct SoundSymbol
  126. {
  127. LLPointer<LLViewerTexture> mTexture[NUM_VOICE_WAVES];
  128. LLVector3 mPosition;
  129. F64 mWaveFadeOutStartTime[NUM_VOICE_WAVES];
  130. F32 mWaveExpansion[NUM_VOICE_WAVES];
  131. F32 mWaveOpacity[NUM_VOICE_WAVES];
  132. bool mWaveActive[NUM_VOICE_WAVES];
  133. bool mActive;
  134. };
  135. // The sound symbol that appears over the avatar's head
  136. SoundSymbol mSoundSymbol;
  137. // Needed at every step to update the sound symbol
  138. LLVector3 mVoiceSourceWorldPosition;
  139. // Current (frame) time in seconds
  140. LLFrameTimer mTimer;
  141. // Time stamp in seconds when speaking started
  142. F64 mStartTime;
  143. // Current time in seconds, captured every step
  144. F64 mCurrentTime;
  145. // Copy of "current time" from last frame
  146. F64 mPreviousTime;
  147. // This should be set as often as possible when the user is speaking
  148. F32 mSpeakingAmplitude;
  149. // This is the upper-limit of the envelope of detectable gesticulation leves
  150. F32 mMaxGesticulationAmplitude;
  151. // This is the lower-limit of the envelope of detectable gesticulation leves
  152. F32 mMinGesticulationAmplitude;
  153. // When false, no rendering happens
  154. bool mVoiceEnabled;
  155. // Is the user currently speaking ?
  156. bool mCurrentlySpeaking;
  157. // The babble loop of amplitudes for the ooh morph
  158. static F32* sOoh;
  159. // The babble loop of amplitudes for the ooh morph
  160. static F32* sAah;
  161. // The number of entries in the ooh loop
  162. static U32 sOohs;
  163. // The number of entries in the aah loop
  164. static U32 sAahs;
  165. // Frames per second for the babble loop
  166. static F32 sOohAahRate;
  167. // The power transfer characteristics for the ooh amplitude
  168. static F32* sOohPowerTransfer;
  169. // The number of entries in the ooh transfer characteristics
  170. static U32 sOohPowerTransfers;
  171. // The number of entries in the ooh transfer characteristics as a float
  172. static F32 sOohPowerTransfersf;
  173. // The power transfer characteristics for the aah amplitude
  174. static F32* sAahPowerTransfer;
  175. // The number of entries in the aah transfer characteristics
  176. static U32 sAahPowerTransfers;
  177. // The number of entries in the aah transfer characteristics as a float
  178. static F32 sAahPowerTransfersf;
  179. // 'true' when in babble loop, 'false' when disabled
  180. static bool sLipSyncEnabled;
  181. // The first instance will initialize the static members
  182. static bool sPrefsInitialized;
  183. };
  184. #endif // LL_LLVOICEVISUALIZER_H